xref: /linux/drivers/infiniband/core/cma.c (revision 3f168d2b66d2314fea40614a3b966c1a0b6241a9)
1e51060f0SSean Hefty /*
2e51060f0SSean Hefty  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3e51060f0SSean Hefty  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4e51060f0SSean Hefty  * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5e51060f0SSean Hefty  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
6e51060f0SSean Hefty  *
7e51060f0SSean Hefty  * This Software is licensed under one of the following licenses:
8e51060f0SSean Hefty  *
9e51060f0SSean Hefty  * 1) under the terms of the "Common Public License 1.0" a copy of which is
10e51060f0SSean Hefty  *    available from the Open Source Initiative, see
11e51060f0SSean Hefty  *    http://www.opensource.org/licenses/cpl.php.
12e51060f0SSean Hefty  *
13e51060f0SSean Hefty  * 2) under the terms of the "The BSD License" a copy of which is
14e51060f0SSean Hefty  *    available from the Open Source Initiative, see
15e51060f0SSean Hefty  *    http://www.opensource.org/licenses/bsd-license.php.
16e51060f0SSean Hefty  *
17e51060f0SSean Hefty  * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
18e51060f0SSean Hefty  *    copy of which is available from the Open Source Initiative, see
19e51060f0SSean Hefty  *    http://www.opensource.org/licenses/gpl-license.php.
20e51060f0SSean Hefty  *
21e51060f0SSean Hefty  * Licensee has the right to choose one of the above licenses.
22e51060f0SSean Hefty  *
23e51060f0SSean Hefty  * Redistributions of source code must retain the above copyright
24e51060f0SSean Hefty  * notice and one of the license notices.
25e51060f0SSean Hefty  *
26e51060f0SSean Hefty  * Redistributions in binary form must reproduce both the above copyright
27e51060f0SSean Hefty  * notice, one of the license notices in the documentation
28e51060f0SSean Hefty  * and/or other materials provided with the distribution.
29e51060f0SSean Hefty  *
30e51060f0SSean Hefty  */
31e51060f0SSean Hefty 
32e51060f0SSean Hefty #include <linux/completion.h>
33e51060f0SSean Hefty #include <linux/in.h>
34e51060f0SSean Hefty #include <linux/in6.h>
35e51060f0SSean Hefty #include <linux/mutex.h>
36e51060f0SSean Hefty #include <linux/random.h>
37e51060f0SSean Hefty #include <linux/idr.h>
3807ebafbaSTom Tucker #include <linux/inetdevice.h>
39e51060f0SSean Hefty 
40e51060f0SSean Hefty #include <net/tcp.h>
41e51060f0SSean Hefty 
42e51060f0SSean Hefty #include <rdma/rdma_cm.h>
43e51060f0SSean Hefty #include <rdma/rdma_cm_ib.h>
44e51060f0SSean Hefty #include <rdma/ib_cache.h>
45e51060f0SSean Hefty #include <rdma/ib_cm.h>
46e51060f0SSean Hefty #include <rdma/ib_sa.h>
4707ebafbaSTom Tucker #include <rdma/iw_cm.h>
48e51060f0SSean Hefty 
49e51060f0SSean Hefty MODULE_AUTHOR("Sean Hefty");
50e51060f0SSean Hefty MODULE_DESCRIPTION("Generic RDMA CM Agent");
51e51060f0SSean Hefty MODULE_LICENSE("Dual BSD/GPL");
52e51060f0SSean Hefty 
53e51060f0SSean Hefty #define CMA_CM_RESPONSE_TIMEOUT 20
54d5bb7599SMichael S. Tsirkin #define CMA_MAX_CM_RETRIES 15
55e51060f0SSean Hefty 
56e51060f0SSean Hefty static void cma_add_one(struct ib_device *device);
57e51060f0SSean Hefty static void cma_remove_one(struct ib_device *device);
58e51060f0SSean Hefty 
59e51060f0SSean Hefty static struct ib_client cma_client = {
60e51060f0SSean Hefty 	.name   = "cma",
61e51060f0SSean Hefty 	.add    = cma_add_one,
62e51060f0SSean Hefty 	.remove = cma_remove_one
63e51060f0SSean Hefty };
64e51060f0SSean Hefty 
65c1a0b23bSMichael S. Tsirkin static struct ib_sa_client sa_client;
66e51060f0SSean Hefty static LIST_HEAD(dev_list);
67e51060f0SSean Hefty static LIST_HEAD(listen_any_list);
68e51060f0SSean Hefty static DEFINE_MUTEX(lock);
69e51060f0SSean Hefty static struct workqueue_struct *cma_wq;
70e51060f0SSean Hefty static DEFINE_IDR(sdp_ps);
71e51060f0SSean Hefty static DEFINE_IDR(tcp_ps);
72e51060f0SSean Hefty 
73e51060f0SSean Hefty struct cma_device {
74e51060f0SSean Hefty 	struct list_head	list;
75e51060f0SSean Hefty 	struct ib_device	*device;
76e51060f0SSean Hefty 	__be64			node_guid;
77e51060f0SSean Hefty 	struct completion	comp;
78e51060f0SSean Hefty 	atomic_t		refcount;
79e51060f0SSean Hefty 	struct list_head	id_list;
80e51060f0SSean Hefty };
81e51060f0SSean Hefty 
82e51060f0SSean Hefty enum cma_state {
83e51060f0SSean Hefty 	CMA_IDLE,
84e51060f0SSean Hefty 	CMA_ADDR_QUERY,
85e51060f0SSean Hefty 	CMA_ADDR_RESOLVED,
86e51060f0SSean Hefty 	CMA_ROUTE_QUERY,
87e51060f0SSean Hefty 	CMA_ROUTE_RESOLVED,
88e51060f0SSean Hefty 	CMA_CONNECT,
89e51060f0SSean Hefty 	CMA_DISCONNECT,
90e51060f0SSean Hefty 	CMA_ADDR_BOUND,
91e51060f0SSean Hefty 	CMA_LISTEN,
92e51060f0SSean Hefty 	CMA_DEVICE_REMOVAL,
93e51060f0SSean Hefty 	CMA_DESTROYING
94e51060f0SSean Hefty };
95e51060f0SSean Hefty 
96e51060f0SSean Hefty struct rdma_bind_list {
97e51060f0SSean Hefty 	struct idr		*ps;
98e51060f0SSean Hefty 	struct hlist_head	owners;
99e51060f0SSean Hefty 	unsigned short		port;
100e51060f0SSean Hefty };
101e51060f0SSean Hefty 
102e51060f0SSean Hefty /*
103e51060f0SSean Hefty  * Device removal can occur at anytime, so we need extra handling to
104e51060f0SSean Hefty  * serialize notifying the user of device removal with other callbacks.
105e51060f0SSean Hefty  * We do this by disabling removal notification while a callback is in process,
106e51060f0SSean Hefty  * and reporting it after the callback completes.
107e51060f0SSean Hefty  */
108e51060f0SSean Hefty struct rdma_id_private {
109e51060f0SSean Hefty 	struct rdma_cm_id	id;
110e51060f0SSean Hefty 
111e51060f0SSean Hefty 	struct rdma_bind_list	*bind_list;
112e51060f0SSean Hefty 	struct hlist_node	node;
113e51060f0SSean Hefty 	struct list_head	list;
114e51060f0SSean Hefty 	struct list_head	listen_list;
115e51060f0SSean Hefty 	struct cma_device	*cma_dev;
116e51060f0SSean Hefty 
117e51060f0SSean Hefty 	enum cma_state		state;
118e51060f0SSean Hefty 	spinlock_t		lock;
119e51060f0SSean Hefty 	struct completion	comp;
120e51060f0SSean Hefty 	atomic_t		refcount;
121e51060f0SSean Hefty 	wait_queue_head_t	wait_remove;
122e51060f0SSean Hefty 	atomic_t		dev_remove;
123e51060f0SSean Hefty 
124e51060f0SSean Hefty 	int			backlog;
125e51060f0SSean Hefty 	int			timeout_ms;
126e51060f0SSean Hefty 	struct ib_sa_query	*query;
127e51060f0SSean Hefty 	int			query_id;
128e51060f0SSean Hefty 	union {
129e51060f0SSean Hefty 		struct ib_cm_id	*ib;
13007ebafbaSTom Tucker 		struct iw_cm_id	*iw;
131e51060f0SSean Hefty 	} cm_id;
132e51060f0SSean Hefty 
133e51060f0SSean Hefty 	u32			seq_num;
134e51060f0SSean Hefty 	u32			qp_num;
135e51060f0SSean Hefty 	enum ib_qp_type		qp_type;
136e51060f0SSean Hefty 	u8			srq;
137e51060f0SSean Hefty };
138e51060f0SSean Hefty 
139e51060f0SSean Hefty struct cma_work {
140e51060f0SSean Hefty 	struct work_struct	work;
141e51060f0SSean Hefty 	struct rdma_id_private	*id;
142e51060f0SSean Hefty 	enum cma_state		old_state;
143e51060f0SSean Hefty 	enum cma_state		new_state;
144e51060f0SSean Hefty 	struct rdma_cm_event	event;
145e51060f0SSean Hefty };
146e51060f0SSean Hefty 
147e51060f0SSean Hefty union cma_ip_addr {
148e51060f0SSean Hefty 	struct in6_addr ip6;
149e51060f0SSean Hefty 	struct {
150e51060f0SSean Hefty 		__u32 pad[3];
151e51060f0SSean Hefty 		__u32 addr;
152e51060f0SSean Hefty 	} ip4;
153e51060f0SSean Hefty };
154e51060f0SSean Hefty 
155e51060f0SSean Hefty struct cma_hdr {
156e51060f0SSean Hefty 	u8 cma_version;
157e51060f0SSean Hefty 	u8 ip_version;	/* IP version: 7:4 */
158e51060f0SSean Hefty 	__u16 port;
159e51060f0SSean Hefty 	union cma_ip_addr src_addr;
160e51060f0SSean Hefty 	union cma_ip_addr dst_addr;
161e51060f0SSean Hefty };
162e51060f0SSean Hefty 
163e51060f0SSean Hefty struct sdp_hh {
164e51060f0SSean Hefty 	u8 bsdh[16];
165e51060f0SSean Hefty 	u8 sdp_version; /* Major version: 7:4 */
166e51060f0SSean Hefty 	u8 ip_version;	/* IP version: 7:4 */
167e51060f0SSean Hefty 	u8 sdp_specific1[10];
168e51060f0SSean Hefty 	__u16 port;
169e51060f0SSean Hefty 	__u16 sdp_specific2;
170e51060f0SSean Hefty 	union cma_ip_addr src_addr;
171e51060f0SSean Hefty 	union cma_ip_addr dst_addr;
172e51060f0SSean Hefty };
173e51060f0SSean Hefty 
174e51060f0SSean Hefty struct sdp_hah {
175e51060f0SSean Hefty 	u8 bsdh[16];
176e51060f0SSean Hefty 	u8 sdp_version;
177e51060f0SSean Hefty };
178e51060f0SSean Hefty 
179e51060f0SSean Hefty #define CMA_VERSION 0x00
180e51060f0SSean Hefty #define SDP_MAJ_VERSION 0x2
181e51060f0SSean Hefty 
182e51060f0SSean Hefty static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
183e51060f0SSean Hefty {
184e51060f0SSean Hefty 	unsigned long flags;
185e51060f0SSean Hefty 	int ret;
186e51060f0SSean Hefty 
187e51060f0SSean Hefty 	spin_lock_irqsave(&id_priv->lock, flags);
188e51060f0SSean Hefty 	ret = (id_priv->state == comp);
189e51060f0SSean Hefty 	spin_unlock_irqrestore(&id_priv->lock, flags);
190e51060f0SSean Hefty 	return ret;
191e51060f0SSean Hefty }
192e51060f0SSean Hefty 
193e51060f0SSean Hefty static int cma_comp_exch(struct rdma_id_private *id_priv,
194e51060f0SSean Hefty 			 enum cma_state comp, enum cma_state exch)
195e51060f0SSean Hefty {
196e51060f0SSean Hefty 	unsigned long flags;
197e51060f0SSean Hefty 	int ret;
198e51060f0SSean Hefty 
199e51060f0SSean Hefty 	spin_lock_irqsave(&id_priv->lock, flags);
200e51060f0SSean Hefty 	if ((ret = (id_priv->state == comp)))
201e51060f0SSean Hefty 		id_priv->state = exch;
202e51060f0SSean Hefty 	spin_unlock_irqrestore(&id_priv->lock, flags);
203e51060f0SSean Hefty 	return ret;
204e51060f0SSean Hefty }
205e51060f0SSean Hefty 
206e51060f0SSean Hefty static enum cma_state cma_exch(struct rdma_id_private *id_priv,
207e51060f0SSean Hefty 			       enum cma_state exch)
208e51060f0SSean Hefty {
209e51060f0SSean Hefty 	unsigned long flags;
210e51060f0SSean Hefty 	enum cma_state old;
211e51060f0SSean Hefty 
212e51060f0SSean Hefty 	spin_lock_irqsave(&id_priv->lock, flags);
213e51060f0SSean Hefty 	old = id_priv->state;
214e51060f0SSean Hefty 	id_priv->state = exch;
215e51060f0SSean Hefty 	spin_unlock_irqrestore(&id_priv->lock, flags);
216e51060f0SSean Hefty 	return old;
217e51060f0SSean Hefty }
218e51060f0SSean Hefty 
219e51060f0SSean Hefty static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
220e51060f0SSean Hefty {
221e51060f0SSean Hefty 	return hdr->ip_version >> 4;
222e51060f0SSean Hefty }
223e51060f0SSean Hefty 
224e51060f0SSean Hefty static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
225e51060f0SSean Hefty {
226e51060f0SSean Hefty 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
227e51060f0SSean Hefty }
228e51060f0SSean Hefty 
229e51060f0SSean Hefty static inline u8 sdp_get_majv(u8 sdp_version)
230e51060f0SSean Hefty {
231e51060f0SSean Hefty 	return sdp_version >> 4;
232e51060f0SSean Hefty }
233e51060f0SSean Hefty 
234e51060f0SSean Hefty static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
235e51060f0SSean Hefty {
236e51060f0SSean Hefty 	return hh->ip_version >> 4;
237e51060f0SSean Hefty }
238e51060f0SSean Hefty 
239e51060f0SSean Hefty static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
240e51060f0SSean Hefty {
241e51060f0SSean Hefty 	hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
242e51060f0SSean Hefty }
243e51060f0SSean Hefty 
244e51060f0SSean Hefty static void cma_attach_to_dev(struct rdma_id_private *id_priv,
245e51060f0SSean Hefty 			      struct cma_device *cma_dev)
246e51060f0SSean Hefty {
247e51060f0SSean Hefty 	atomic_inc(&cma_dev->refcount);
248e51060f0SSean Hefty 	id_priv->cma_dev = cma_dev;
249e51060f0SSean Hefty 	id_priv->id.device = cma_dev->device;
250e51060f0SSean Hefty 	list_add_tail(&id_priv->list, &cma_dev->id_list);
251e51060f0SSean Hefty }
252e51060f0SSean Hefty 
253e51060f0SSean Hefty static inline void cma_deref_dev(struct cma_device *cma_dev)
254e51060f0SSean Hefty {
255e51060f0SSean Hefty 	if (atomic_dec_and_test(&cma_dev->refcount))
256e51060f0SSean Hefty 		complete(&cma_dev->comp);
257e51060f0SSean Hefty }
258e51060f0SSean Hefty 
259e51060f0SSean Hefty static void cma_detach_from_dev(struct rdma_id_private *id_priv)
260e51060f0SSean Hefty {
261e51060f0SSean Hefty 	list_del(&id_priv->list);
262e51060f0SSean Hefty 	cma_deref_dev(id_priv->cma_dev);
263e51060f0SSean Hefty 	id_priv->cma_dev = NULL;
264e51060f0SSean Hefty }
265e51060f0SSean Hefty 
26607ebafbaSTom Tucker static int cma_acquire_dev(struct rdma_id_private *id_priv)
267e51060f0SSean Hefty {
26807ebafbaSTom Tucker 	enum rdma_node_type dev_type = id_priv->id.route.addr.dev_addr.dev_type;
269e51060f0SSean Hefty 	struct cma_device *cma_dev;
270f0ee3404SMichael S. Tsirkin 	union ib_gid gid;
271e51060f0SSean Hefty 	int ret = -ENODEV;
272e51060f0SSean Hefty 
27307ebafbaSTom Tucker 	switch (rdma_node_get_transport(dev_type)) {
27407ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
27507ebafbaSTom Tucker 		ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
27607ebafbaSTom Tucker 		break;
27707ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
27807ebafbaSTom Tucker 		iw_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
27907ebafbaSTom Tucker 		break;
28007ebafbaSTom Tucker 	default:
28107ebafbaSTom Tucker 		return -ENODEV;
28207ebafbaSTom Tucker 	}
28361a73c70SSean Hefty 
284e51060f0SSean Hefty 	list_for_each_entry(cma_dev, &dev_list, list) {
285f0ee3404SMichael S. Tsirkin 		ret = ib_find_cached_gid(cma_dev->device, &gid,
286e51060f0SSean Hefty 					 &id_priv->id.port_num, NULL);
287e51060f0SSean Hefty 		if (!ret) {
288e51060f0SSean Hefty 			cma_attach_to_dev(id_priv, cma_dev);
289e51060f0SSean Hefty 			break;
290e51060f0SSean Hefty 		}
291e51060f0SSean Hefty 	}
292e51060f0SSean Hefty 	return ret;
293e51060f0SSean Hefty }
294e51060f0SSean Hefty 
295e51060f0SSean Hefty static void cma_deref_id(struct rdma_id_private *id_priv)
296e51060f0SSean Hefty {
297e51060f0SSean Hefty 	if (atomic_dec_and_test(&id_priv->refcount))
298e51060f0SSean Hefty 		complete(&id_priv->comp);
299e51060f0SSean Hefty }
300e51060f0SSean Hefty 
301e51060f0SSean Hefty static void cma_release_remove(struct rdma_id_private *id_priv)
302e51060f0SSean Hefty {
303e51060f0SSean Hefty 	if (atomic_dec_and_test(&id_priv->dev_remove))
304e51060f0SSean Hefty 		wake_up(&id_priv->wait_remove);
305e51060f0SSean Hefty }
306e51060f0SSean Hefty 
307e51060f0SSean Hefty struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
308e51060f0SSean Hefty 				  void *context, enum rdma_port_space ps)
309e51060f0SSean Hefty {
310e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
311e51060f0SSean Hefty 
312e51060f0SSean Hefty 	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
313e51060f0SSean Hefty 	if (!id_priv)
314e51060f0SSean Hefty 		return ERR_PTR(-ENOMEM);
315e51060f0SSean Hefty 
316e51060f0SSean Hefty 	id_priv->state = CMA_IDLE;
317e51060f0SSean Hefty 	id_priv->id.context = context;
318e51060f0SSean Hefty 	id_priv->id.event_handler = event_handler;
319e51060f0SSean Hefty 	id_priv->id.ps = ps;
320e51060f0SSean Hefty 	spin_lock_init(&id_priv->lock);
321e51060f0SSean Hefty 	init_completion(&id_priv->comp);
322e51060f0SSean Hefty 	atomic_set(&id_priv->refcount, 1);
323e51060f0SSean Hefty 	init_waitqueue_head(&id_priv->wait_remove);
324e51060f0SSean Hefty 	atomic_set(&id_priv->dev_remove, 0);
325e51060f0SSean Hefty 	INIT_LIST_HEAD(&id_priv->listen_list);
326e51060f0SSean Hefty 	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
327e51060f0SSean Hefty 
328e51060f0SSean Hefty 	return &id_priv->id;
329e51060f0SSean Hefty }
330e51060f0SSean Hefty EXPORT_SYMBOL(rdma_create_id);
331e51060f0SSean Hefty 
332e51060f0SSean Hefty static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
333e51060f0SSean Hefty {
334e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
335e51060f0SSean Hefty 	struct rdma_dev_addr *dev_addr;
336e51060f0SSean Hefty 	int ret;
337e51060f0SSean Hefty 
338e51060f0SSean Hefty 	dev_addr = &id_priv->id.route.addr.dev_addr;
339e51060f0SSean Hefty 	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
340e51060f0SSean Hefty 				  ib_addr_get_pkey(dev_addr),
341e51060f0SSean Hefty 				  &qp_attr.pkey_index);
342e51060f0SSean Hefty 	if (ret)
343e51060f0SSean Hefty 		return ret;
344e51060f0SSean Hefty 
345e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_INIT;
346e51060f0SSean Hefty 	qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
347e51060f0SSean Hefty 	qp_attr.port_num = id_priv->id.port_num;
348e51060f0SSean Hefty 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS |
349e51060f0SSean Hefty 					  IB_QP_PKEY_INDEX | IB_QP_PORT);
350e51060f0SSean Hefty }
351e51060f0SSean Hefty 
35207ebafbaSTom Tucker static int cma_init_iw_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
35307ebafbaSTom Tucker {
35407ebafbaSTom Tucker 	struct ib_qp_attr qp_attr;
35507ebafbaSTom Tucker 
35607ebafbaSTom Tucker 	qp_attr.qp_state = IB_QPS_INIT;
35707ebafbaSTom Tucker 	qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
35807ebafbaSTom Tucker 
35907ebafbaSTom Tucker 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS);
36007ebafbaSTom Tucker }
36107ebafbaSTom Tucker 
362e51060f0SSean Hefty int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
363e51060f0SSean Hefty 		   struct ib_qp_init_attr *qp_init_attr)
364e51060f0SSean Hefty {
365e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
366e51060f0SSean Hefty 	struct ib_qp *qp;
367e51060f0SSean Hefty 	int ret;
368e51060f0SSean Hefty 
369e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
370e51060f0SSean Hefty 	if (id->device != pd->device)
371e51060f0SSean Hefty 		return -EINVAL;
372e51060f0SSean Hefty 
373e51060f0SSean Hefty 	qp = ib_create_qp(pd, qp_init_attr);
374e51060f0SSean Hefty 	if (IS_ERR(qp))
375e51060f0SSean Hefty 		return PTR_ERR(qp);
376e51060f0SSean Hefty 
37707ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
37807ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
379e51060f0SSean Hefty 		ret = cma_init_ib_qp(id_priv, qp);
380e51060f0SSean Hefty 		break;
38107ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
38207ebafbaSTom Tucker 		ret = cma_init_iw_qp(id_priv, qp);
38307ebafbaSTom Tucker 		break;
384e51060f0SSean Hefty 	default:
385e51060f0SSean Hefty 		ret = -ENOSYS;
386e51060f0SSean Hefty 		break;
387e51060f0SSean Hefty 	}
388e51060f0SSean Hefty 
389e51060f0SSean Hefty 	if (ret)
390e51060f0SSean Hefty 		goto err;
391e51060f0SSean Hefty 
392e51060f0SSean Hefty 	id->qp = qp;
393e51060f0SSean Hefty 	id_priv->qp_num = qp->qp_num;
394e51060f0SSean Hefty 	id_priv->qp_type = qp->qp_type;
395e51060f0SSean Hefty 	id_priv->srq = (qp->srq != NULL);
396e51060f0SSean Hefty 	return 0;
397e51060f0SSean Hefty err:
398e51060f0SSean Hefty 	ib_destroy_qp(qp);
399e51060f0SSean Hefty 	return ret;
400e51060f0SSean Hefty }
401e51060f0SSean Hefty EXPORT_SYMBOL(rdma_create_qp);
402e51060f0SSean Hefty 
403e51060f0SSean Hefty void rdma_destroy_qp(struct rdma_cm_id *id)
404e51060f0SSean Hefty {
405e51060f0SSean Hefty 	ib_destroy_qp(id->qp);
406e51060f0SSean Hefty }
407e51060f0SSean Hefty EXPORT_SYMBOL(rdma_destroy_qp);
408e51060f0SSean Hefty 
409e51060f0SSean Hefty static int cma_modify_qp_rtr(struct rdma_cm_id *id)
410e51060f0SSean Hefty {
411e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
412e51060f0SSean Hefty 	int qp_attr_mask, ret;
413e51060f0SSean Hefty 
414e51060f0SSean Hefty 	if (!id->qp)
415e51060f0SSean Hefty 		return 0;
416e51060f0SSean Hefty 
417e51060f0SSean Hefty 	/* Need to update QP attributes from default values. */
418e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_INIT;
419e51060f0SSean Hefty 	ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
420e51060f0SSean Hefty 	if (ret)
421e51060f0SSean Hefty 		return ret;
422e51060f0SSean Hefty 
423e51060f0SSean Hefty 	ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
424e51060f0SSean Hefty 	if (ret)
425e51060f0SSean Hefty 		return ret;
426e51060f0SSean Hefty 
427e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_RTR;
428e51060f0SSean Hefty 	ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
429e51060f0SSean Hefty 	if (ret)
430e51060f0SSean Hefty 		return ret;
431e51060f0SSean Hefty 
432e51060f0SSean Hefty 	return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
433e51060f0SSean Hefty }
434e51060f0SSean Hefty 
435e51060f0SSean Hefty static int cma_modify_qp_rts(struct rdma_cm_id *id)
436e51060f0SSean Hefty {
437e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
438e51060f0SSean Hefty 	int qp_attr_mask, ret;
439e51060f0SSean Hefty 
440e51060f0SSean Hefty 	if (!id->qp)
441e51060f0SSean Hefty 		return 0;
442e51060f0SSean Hefty 
443e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_RTS;
444e51060f0SSean Hefty 	ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
445e51060f0SSean Hefty 	if (ret)
446e51060f0SSean Hefty 		return ret;
447e51060f0SSean Hefty 
448e51060f0SSean Hefty 	return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
449e51060f0SSean Hefty }
450e51060f0SSean Hefty 
451e51060f0SSean Hefty static int cma_modify_qp_err(struct rdma_cm_id *id)
452e51060f0SSean Hefty {
453e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
454e51060f0SSean Hefty 
455e51060f0SSean Hefty 	if (!id->qp)
456e51060f0SSean Hefty 		return 0;
457e51060f0SSean Hefty 
458e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_ERR;
459e51060f0SSean Hefty 	return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
460e51060f0SSean Hefty }
461e51060f0SSean Hefty 
462e51060f0SSean Hefty int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
463e51060f0SSean Hefty 		       int *qp_attr_mask)
464e51060f0SSean Hefty {
465e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
466e51060f0SSean Hefty 	int ret;
467e51060f0SSean Hefty 
468e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
46907ebafbaSTom Tucker 	switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
47007ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
471e51060f0SSean Hefty 		ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
472e51060f0SSean Hefty 					 qp_attr_mask);
473e51060f0SSean Hefty 		if (qp_attr->qp_state == IB_QPS_RTR)
474e51060f0SSean Hefty 			qp_attr->rq_psn = id_priv->seq_num;
475e51060f0SSean Hefty 		break;
47607ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
47707ebafbaSTom Tucker 		ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
47807ebafbaSTom Tucker 					qp_attr_mask);
47907ebafbaSTom Tucker 		break;
480e51060f0SSean Hefty 	default:
481e51060f0SSean Hefty 		ret = -ENOSYS;
482e51060f0SSean Hefty 		break;
483e51060f0SSean Hefty 	}
484e51060f0SSean Hefty 
485e51060f0SSean Hefty 	return ret;
486e51060f0SSean Hefty }
487e51060f0SSean Hefty EXPORT_SYMBOL(rdma_init_qp_attr);
488e51060f0SSean Hefty 
489e51060f0SSean Hefty static inline int cma_zero_addr(struct sockaddr *addr)
490e51060f0SSean Hefty {
491e51060f0SSean Hefty 	struct in6_addr *ip6;
492e51060f0SSean Hefty 
493e51060f0SSean Hefty 	if (addr->sa_family == AF_INET)
494e51060f0SSean Hefty 		return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
495e51060f0SSean Hefty 	else {
496e51060f0SSean Hefty 		ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
497e51060f0SSean Hefty 		return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
4985fd571cbSEric Sesterhenn 			ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
499e51060f0SSean Hefty 	}
500e51060f0SSean Hefty }
501e51060f0SSean Hefty 
502e51060f0SSean Hefty static inline int cma_loopback_addr(struct sockaddr *addr)
503e51060f0SSean Hefty {
504e51060f0SSean Hefty 	return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
505e51060f0SSean Hefty }
506e51060f0SSean Hefty 
507e51060f0SSean Hefty static inline int cma_any_addr(struct sockaddr *addr)
508e51060f0SSean Hefty {
509e51060f0SSean Hefty 	return cma_zero_addr(addr) || cma_loopback_addr(addr);
510e51060f0SSean Hefty }
511e51060f0SSean Hefty 
512e51060f0SSean Hefty static inline int cma_any_port(struct sockaddr *addr)
513e51060f0SSean Hefty {
514e51060f0SSean Hefty 	return !((struct sockaddr_in *) addr)->sin_port;
515e51060f0SSean Hefty }
516e51060f0SSean Hefty 
517e51060f0SSean Hefty static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
518e51060f0SSean Hefty 			    u8 *ip_ver, __u16 *port,
519e51060f0SSean Hefty 			    union cma_ip_addr **src, union cma_ip_addr **dst)
520e51060f0SSean Hefty {
521e51060f0SSean Hefty 	switch (ps) {
522e51060f0SSean Hefty 	case RDMA_PS_SDP:
523e51060f0SSean Hefty 		if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
524e51060f0SSean Hefty 		    SDP_MAJ_VERSION)
525e51060f0SSean Hefty 			return -EINVAL;
526e51060f0SSean Hefty 
527e51060f0SSean Hefty 		*ip_ver	= sdp_get_ip_ver(hdr);
528e51060f0SSean Hefty 		*port	= ((struct sdp_hh *) hdr)->port;
529e51060f0SSean Hefty 		*src	= &((struct sdp_hh *) hdr)->src_addr;
530e51060f0SSean Hefty 		*dst	= &((struct sdp_hh *) hdr)->dst_addr;
531e51060f0SSean Hefty 		break;
532e51060f0SSean Hefty 	default:
533e51060f0SSean Hefty 		if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
534e51060f0SSean Hefty 			return -EINVAL;
535e51060f0SSean Hefty 
536e51060f0SSean Hefty 		*ip_ver	= cma_get_ip_ver(hdr);
537e51060f0SSean Hefty 		*port	= ((struct cma_hdr *) hdr)->port;
538e51060f0SSean Hefty 		*src	= &((struct cma_hdr *) hdr)->src_addr;
539e51060f0SSean Hefty 		*dst	= &((struct cma_hdr *) hdr)->dst_addr;
540e51060f0SSean Hefty 		break;
541e51060f0SSean Hefty 	}
542e51060f0SSean Hefty 
543e51060f0SSean Hefty 	if (*ip_ver != 4 && *ip_ver != 6)
544e51060f0SSean Hefty 		return -EINVAL;
545e51060f0SSean Hefty 	return 0;
546e51060f0SSean Hefty }
547e51060f0SSean Hefty 
548e51060f0SSean Hefty static void cma_save_net_info(struct rdma_addr *addr,
549e51060f0SSean Hefty 			      struct rdma_addr *listen_addr,
550e51060f0SSean Hefty 			      u8 ip_ver, __u16 port,
551e51060f0SSean Hefty 			      union cma_ip_addr *src, union cma_ip_addr *dst)
552e51060f0SSean Hefty {
553e51060f0SSean Hefty 	struct sockaddr_in *listen4, *ip4;
554e51060f0SSean Hefty 	struct sockaddr_in6 *listen6, *ip6;
555e51060f0SSean Hefty 
556e51060f0SSean Hefty 	switch (ip_ver) {
557e51060f0SSean Hefty 	case 4:
558e51060f0SSean Hefty 		listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
559e51060f0SSean Hefty 		ip4 = (struct sockaddr_in *) &addr->src_addr;
560e51060f0SSean Hefty 		ip4->sin_family = listen4->sin_family;
561e51060f0SSean Hefty 		ip4->sin_addr.s_addr = dst->ip4.addr;
562e51060f0SSean Hefty 		ip4->sin_port = listen4->sin_port;
563e51060f0SSean Hefty 
564e51060f0SSean Hefty 		ip4 = (struct sockaddr_in *) &addr->dst_addr;
565e51060f0SSean Hefty 		ip4->sin_family = listen4->sin_family;
566e51060f0SSean Hefty 		ip4->sin_addr.s_addr = src->ip4.addr;
567e51060f0SSean Hefty 		ip4->sin_port = port;
568e51060f0SSean Hefty 		break;
569e51060f0SSean Hefty 	case 6:
570e51060f0SSean Hefty 		listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
571e51060f0SSean Hefty 		ip6 = (struct sockaddr_in6 *) &addr->src_addr;
572e51060f0SSean Hefty 		ip6->sin6_family = listen6->sin6_family;
573e51060f0SSean Hefty 		ip6->sin6_addr = dst->ip6;
574e51060f0SSean Hefty 		ip6->sin6_port = listen6->sin6_port;
575e51060f0SSean Hefty 
576e51060f0SSean Hefty 		ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
577e51060f0SSean Hefty 		ip6->sin6_family = listen6->sin6_family;
578e51060f0SSean Hefty 		ip6->sin6_addr = src->ip6;
579e51060f0SSean Hefty 		ip6->sin6_port = port;
580e51060f0SSean Hefty 		break;
581e51060f0SSean Hefty 	default:
582e51060f0SSean Hefty 		break;
583e51060f0SSean Hefty 	}
584e51060f0SSean Hefty }
585e51060f0SSean Hefty 
586e51060f0SSean Hefty static inline int cma_user_data_offset(enum rdma_port_space ps)
587e51060f0SSean Hefty {
588e51060f0SSean Hefty 	switch (ps) {
589e51060f0SSean Hefty 	case RDMA_PS_SDP:
590e51060f0SSean Hefty 		return 0;
591e51060f0SSean Hefty 	default:
592e51060f0SSean Hefty 		return sizeof(struct cma_hdr);
593e51060f0SSean Hefty 	}
594e51060f0SSean Hefty }
595e51060f0SSean Hefty 
596e51060f0SSean Hefty static int cma_notify_user(struct rdma_id_private *id_priv,
597e51060f0SSean Hefty 			   enum rdma_cm_event_type type, int status,
598e51060f0SSean Hefty 			   void *data, u8 data_len)
599e51060f0SSean Hefty {
600e51060f0SSean Hefty 	struct rdma_cm_event event;
601e51060f0SSean Hefty 
602e51060f0SSean Hefty 	event.event = type;
603e51060f0SSean Hefty 	event.status = status;
604e51060f0SSean Hefty 	event.private_data = data;
605e51060f0SSean Hefty 	event.private_data_len = data_len;
606e51060f0SSean Hefty 
607e51060f0SSean Hefty 	return id_priv->id.event_handler(&id_priv->id, &event);
608e51060f0SSean Hefty }
609e51060f0SSean Hefty 
610e51060f0SSean Hefty static void cma_cancel_route(struct rdma_id_private *id_priv)
611e51060f0SSean Hefty {
61207ebafbaSTom Tucker 	switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
61307ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
614e51060f0SSean Hefty 		if (id_priv->query)
615e51060f0SSean Hefty 			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
616e51060f0SSean Hefty 		break;
617e51060f0SSean Hefty 	default:
618e51060f0SSean Hefty 		break;
619e51060f0SSean Hefty 	}
620e51060f0SSean Hefty }
621e51060f0SSean Hefty 
622e51060f0SSean Hefty static inline int cma_internal_listen(struct rdma_id_private *id_priv)
623e51060f0SSean Hefty {
624e51060f0SSean Hefty 	return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
625e51060f0SSean Hefty 	       cma_any_addr(&id_priv->id.route.addr.src_addr);
626e51060f0SSean Hefty }
627e51060f0SSean Hefty 
628e51060f0SSean Hefty static void cma_destroy_listen(struct rdma_id_private *id_priv)
629e51060f0SSean Hefty {
630e51060f0SSean Hefty 	cma_exch(id_priv, CMA_DESTROYING);
631e51060f0SSean Hefty 
632e51060f0SSean Hefty 	if (id_priv->cma_dev) {
63307ebafbaSTom Tucker 		switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
63407ebafbaSTom Tucker 		case RDMA_TRANSPORT_IB:
635e51060f0SSean Hefty 			if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
636e51060f0SSean Hefty 				ib_destroy_cm_id(id_priv->cm_id.ib);
637e51060f0SSean Hefty 			break;
63807ebafbaSTom Tucker 		case RDMA_TRANSPORT_IWARP:
63907ebafbaSTom Tucker 			if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
64007ebafbaSTom Tucker 				iw_destroy_cm_id(id_priv->cm_id.iw);
64107ebafbaSTom Tucker 			break;
642e51060f0SSean Hefty 		default:
643e51060f0SSean Hefty 			break;
644e51060f0SSean Hefty 		}
645e51060f0SSean Hefty 		cma_detach_from_dev(id_priv);
646e51060f0SSean Hefty 	}
647e51060f0SSean Hefty 	list_del(&id_priv->listen_list);
648e51060f0SSean Hefty 
649e51060f0SSean Hefty 	cma_deref_id(id_priv);
650e51060f0SSean Hefty 	wait_for_completion(&id_priv->comp);
651e51060f0SSean Hefty 
652e51060f0SSean Hefty 	kfree(id_priv);
653e51060f0SSean Hefty }
654e51060f0SSean Hefty 
655e51060f0SSean Hefty static void cma_cancel_listens(struct rdma_id_private *id_priv)
656e51060f0SSean Hefty {
657e51060f0SSean Hefty 	struct rdma_id_private *dev_id_priv;
658e51060f0SSean Hefty 
659e51060f0SSean Hefty 	mutex_lock(&lock);
660e51060f0SSean Hefty 	list_del(&id_priv->list);
661e51060f0SSean Hefty 
662e51060f0SSean Hefty 	while (!list_empty(&id_priv->listen_list)) {
663e51060f0SSean Hefty 		dev_id_priv = list_entry(id_priv->listen_list.next,
664e51060f0SSean Hefty 					 struct rdma_id_private, listen_list);
665e51060f0SSean Hefty 		cma_destroy_listen(dev_id_priv);
666e51060f0SSean Hefty 	}
667e51060f0SSean Hefty 	mutex_unlock(&lock);
668e51060f0SSean Hefty }
669e51060f0SSean Hefty 
670e51060f0SSean Hefty static void cma_cancel_operation(struct rdma_id_private *id_priv,
671e51060f0SSean Hefty 				 enum cma_state state)
672e51060f0SSean Hefty {
673e51060f0SSean Hefty 	switch (state) {
674e51060f0SSean Hefty 	case CMA_ADDR_QUERY:
675e51060f0SSean Hefty 		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
676e51060f0SSean Hefty 		break;
677e51060f0SSean Hefty 	case CMA_ROUTE_QUERY:
678e51060f0SSean Hefty 		cma_cancel_route(id_priv);
679e51060f0SSean Hefty 		break;
680e51060f0SSean Hefty 	case CMA_LISTEN:
681e51060f0SSean Hefty 		if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
682e51060f0SSean Hefty 		    !id_priv->cma_dev)
683e51060f0SSean Hefty 			cma_cancel_listens(id_priv);
684e51060f0SSean Hefty 		break;
685e51060f0SSean Hefty 	default:
686e51060f0SSean Hefty 		break;
687e51060f0SSean Hefty 	}
688e51060f0SSean Hefty }
689e51060f0SSean Hefty 
690e51060f0SSean Hefty static void cma_release_port(struct rdma_id_private *id_priv)
691e51060f0SSean Hefty {
692e51060f0SSean Hefty 	struct rdma_bind_list *bind_list = id_priv->bind_list;
693e51060f0SSean Hefty 
694e51060f0SSean Hefty 	if (!bind_list)
695e51060f0SSean Hefty 		return;
696e51060f0SSean Hefty 
697e51060f0SSean Hefty 	mutex_lock(&lock);
698e51060f0SSean Hefty 	hlist_del(&id_priv->node);
699e51060f0SSean Hefty 	if (hlist_empty(&bind_list->owners)) {
700e51060f0SSean Hefty 		idr_remove(bind_list->ps, bind_list->port);
701e51060f0SSean Hefty 		kfree(bind_list);
702e51060f0SSean Hefty 	}
703e51060f0SSean Hefty 	mutex_unlock(&lock);
704e51060f0SSean Hefty }
705e51060f0SSean Hefty 
706e51060f0SSean Hefty void rdma_destroy_id(struct rdma_cm_id *id)
707e51060f0SSean Hefty {
708e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
709e51060f0SSean Hefty 	enum cma_state state;
710e51060f0SSean Hefty 
711e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
712e51060f0SSean Hefty 	state = cma_exch(id_priv, CMA_DESTROYING);
713e51060f0SSean Hefty 	cma_cancel_operation(id_priv, state);
714e51060f0SSean Hefty 
71561a73c70SSean Hefty 	mutex_lock(&lock);
716e51060f0SSean Hefty 	if (id_priv->cma_dev) {
71761a73c70SSean Hefty 		mutex_unlock(&lock);
71807ebafbaSTom Tucker 		switch (rdma_node_get_transport(id->device->node_type)) {
71907ebafbaSTom Tucker 		case RDMA_TRANSPORT_IB:
720e51060f0SSean Hefty 			if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
721e51060f0SSean Hefty 				ib_destroy_cm_id(id_priv->cm_id.ib);
722e51060f0SSean Hefty 			break;
72307ebafbaSTom Tucker 		case RDMA_TRANSPORT_IWARP:
72407ebafbaSTom Tucker 			if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
72507ebafbaSTom Tucker 				iw_destroy_cm_id(id_priv->cm_id.iw);
72607ebafbaSTom Tucker 			break;
727e51060f0SSean Hefty 		default:
728e51060f0SSean Hefty 			break;
729e51060f0SSean Hefty 		}
730e51060f0SSean Hefty 		mutex_lock(&lock);
731e51060f0SSean Hefty 		cma_detach_from_dev(id_priv);
732e51060f0SSean Hefty 	}
73361a73c70SSean Hefty 	mutex_unlock(&lock);
734e51060f0SSean Hefty 
735e51060f0SSean Hefty 	cma_release_port(id_priv);
736e51060f0SSean Hefty 	cma_deref_id(id_priv);
737e51060f0SSean Hefty 	wait_for_completion(&id_priv->comp);
738e51060f0SSean Hefty 
739e51060f0SSean Hefty 	kfree(id_priv->id.route.path_rec);
740e51060f0SSean Hefty 	kfree(id_priv);
741e51060f0SSean Hefty }
742e51060f0SSean Hefty EXPORT_SYMBOL(rdma_destroy_id);
743e51060f0SSean Hefty 
744e51060f0SSean Hefty static int cma_rep_recv(struct rdma_id_private *id_priv)
745e51060f0SSean Hefty {
746e51060f0SSean Hefty 	int ret;
747e51060f0SSean Hefty 
748e51060f0SSean Hefty 	ret = cma_modify_qp_rtr(&id_priv->id);
749e51060f0SSean Hefty 	if (ret)
750e51060f0SSean Hefty 		goto reject;
751e51060f0SSean Hefty 
752e51060f0SSean Hefty 	ret = cma_modify_qp_rts(&id_priv->id);
753e51060f0SSean Hefty 	if (ret)
754e51060f0SSean Hefty 		goto reject;
755e51060f0SSean Hefty 
756e51060f0SSean Hefty 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
757e51060f0SSean Hefty 	if (ret)
758e51060f0SSean Hefty 		goto reject;
759e51060f0SSean Hefty 
760e51060f0SSean Hefty 	return 0;
761e51060f0SSean Hefty reject:
762e51060f0SSean Hefty 	cma_modify_qp_err(&id_priv->id);
763e51060f0SSean Hefty 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
764e51060f0SSean Hefty 		       NULL, 0, NULL, 0);
765e51060f0SSean Hefty 	return ret;
766e51060f0SSean Hefty }
767e51060f0SSean Hefty 
768e51060f0SSean Hefty static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
769e51060f0SSean Hefty {
770e51060f0SSean Hefty 	if (id_priv->id.ps == RDMA_PS_SDP &&
771e51060f0SSean Hefty 	    sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
772e51060f0SSean Hefty 	    SDP_MAJ_VERSION)
773e51060f0SSean Hefty 		return -EINVAL;
774e51060f0SSean Hefty 
775e51060f0SSean Hefty 	return 0;
776e51060f0SSean Hefty }
777e51060f0SSean Hefty 
778e51060f0SSean Hefty static int cma_rtu_recv(struct rdma_id_private *id_priv)
779e51060f0SSean Hefty {
780e51060f0SSean Hefty 	int ret;
781e51060f0SSean Hefty 
782e51060f0SSean Hefty 	ret = cma_modify_qp_rts(&id_priv->id);
783e51060f0SSean Hefty 	if (ret)
784e51060f0SSean Hefty 		goto reject;
785e51060f0SSean Hefty 
786e51060f0SSean Hefty 	return 0;
787e51060f0SSean Hefty reject:
788e51060f0SSean Hefty 	cma_modify_qp_err(&id_priv->id);
789e51060f0SSean Hefty 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
790e51060f0SSean Hefty 		       NULL, 0, NULL, 0);
791e51060f0SSean Hefty 	return ret;
792e51060f0SSean Hefty }
793e51060f0SSean Hefty 
794e51060f0SSean Hefty static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
795e51060f0SSean Hefty {
796e51060f0SSean Hefty 	struct rdma_id_private *id_priv = cm_id->context;
797e51060f0SSean Hefty 	enum rdma_cm_event_type event;
798e51060f0SSean Hefty 	u8 private_data_len = 0;
799e51060f0SSean Hefty 	int ret = 0, status = 0;
800e51060f0SSean Hefty 
801e51060f0SSean Hefty 	atomic_inc(&id_priv->dev_remove);
802e51060f0SSean Hefty 	if (!cma_comp(id_priv, CMA_CONNECT))
803e51060f0SSean Hefty 		goto out;
804e51060f0SSean Hefty 
805e51060f0SSean Hefty 	switch (ib_event->event) {
806e51060f0SSean Hefty 	case IB_CM_REQ_ERROR:
807e51060f0SSean Hefty 	case IB_CM_REP_ERROR:
808e51060f0SSean Hefty 		event = RDMA_CM_EVENT_UNREACHABLE;
809e51060f0SSean Hefty 		status = -ETIMEDOUT;
810e51060f0SSean Hefty 		break;
811e51060f0SSean Hefty 	case IB_CM_REP_RECEIVED:
812e51060f0SSean Hefty 		status = cma_verify_rep(id_priv, ib_event->private_data);
813e51060f0SSean Hefty 		if (status)
814e51060f0SSean Hefty 			event = RDMA_CM_EVENT_CONNECT_ERROR;
815e51060f0SSean Hefty 		else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
816e51060f0SSean Hefty 			status = cma_rep_recv(id_priv);
817e51060f0SSean Hefty 			event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
818e51060f0SSean Hefty 					 RDMA_CM_EVENT_ESTABLISHED;
819e51060f0SSean Hefty 		} else
820e51060f0SSean Hefty 			event = RDMA_CM_EVENT_CONNECT_RESPONSE;
821e51060f0SSean Hefty 		private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
822e51060f0SSean Hefty 		break;
823e51060f0SSean Hefty 	case IB_CM_RTU_RECEIVED:
824e51060f0SSean Hefty 		status = cma_rtu_recv(id_priv);
825e51060f0SSean Hefty 		event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
826e51060f0SSean Hefty 				 RDMA_CM_EVENT_ESTABLISHED;
827e51060f0SSean Hefty 		break;
828e51060f0SSean Hefty 	case IB_CM_DREQ_ERROR:
829e51060f0SSean Hefty 		status = -ETIMEDOUT; /* fall through */
830e51060f0SSean Hefty 	case IB_CM_DREQ_RECEIVED:
831e51060f0SSean Hefty 	case IB_CM_DREP_RECEIVED:
832e51060f0SSean Hefty 		if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
833e51060f0SSean Hefty 			goto out;
834e51060f0SSean Hefty 		event = RDMA_CM_EVENT_DISCONNECTED;
835e51060f0SSean Hefty 		break;
836e51060f0SSean Hefty 	case IB_CM_TIMEWAIT_EXIT:
837e51060f0SSean Hefty 	case IB_CM_MRA_RECEIVED:
838e51060f0SSean Hefty 		/* ignore event */
839e51060f0SSean Hefty 		goto out;
840e51060f0SSean Hefty 	case IB_CM_REJ_RECEIVED:
841e51060f0SSean Hefty 		cma_modify_qp_err(&id_priv->id);
842e51060f0SSean Hefty 		status = ib_event->param.rej_rcvd.reason;
843e51060f0SSean Hefty 		event = RDMA_CM_EVENT_REJECTED;
84474f76fbaSIra Weiny 		private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
845e51060f0SSean Hefty 		break;
846e51060f0SSean Hefty 	default:
847e51060f0SSean Hefty 		printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
848e51060f0SSean Hefty 		       ib_event->event);
849e51060f0SSean Hefty 		goto out;
850e51060f0SSean Hefty 	}
851e51060f0SSean Hefty 
852e51060f0SSean Hefty 	ret = cma_notify_user(id_priv, event, status, ib_event->private_data,
853e51060f0SSean Hefty 			      private_data_len);
854e51060f0SSean Hefty 	if (ret) {
855e51060f0SSean Hefty 		/* Destroy the CM ID by returning a non-zero value. */
856e51060f0SSean Hefty 		id_priv->cm_id.ib = NULL;
857e51060f0SSean Hefty 		cma_exch(id_priv, CMA_DESTROYING);
858e51060f0SSean Hefty 		cma_release_remove(id_priv);
859e51060f0SSean Hefty 		rdma_destroy_id(&id_priv->id);
860e51060f0SSean Hefty 		return ret;
861e51060f0SSean Hefty 	}
862e51060f0SSean Hefty out:
863e51060f0SSean Hefty 	cma_release_remove(id_priv);
864e51060f0SSean Hefty 	return ret;
865e51060f0SSean Hefty }
866e51060f0SSean Hefty 
867e51060f0SSean Hefty static struct rdma_id_private *cma_new_id(struct rdma_cm_id *listen_id,
868e51060f0SSean Hefty 					  struct ib_cm_event *ib_event)
869e51060f0SSean Hefty {
870e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
871e51060f0SSean Hefty 	struct rdma_cm_id *id;
872e51060f0SSean Hefty 	struct rdma_route *rt;
873e51060f0SSean Hefty 	union cma_ip_addr *src, *dst;
874e51060f0SSean Hefty 	__u16 port;
875e51060f0SSean Hefty 	u8 ip_ver;
876e51060f0SSean Hefty 
877e51060f0SSean Hefty 	if (cma_get_net_info(ib_event->private_data, listen_id->ps,
878e51060f0SSean Hefty 			     &ip_ver, &port, &src, &dst))
879e51060f0SSean Hefty 		goto err;
880e51060f0SSean Hefty 
881*3f168d2bSKrishna Kumar 	id = rdma_create_id(listen_id->event_handler, listen_id->context,
882*3f168d2bSKrishna Kumar 			    listen_id->ps);
883*3f168d2bSKrishna Kumar 	if (IS_ERR(id))
884*3f168d2bSKrishna Kumar 		goto err;
885*3f168d2bSKrishna Kumar 
886e51060f0SSean Hefty 	cma_save_net_info(&id->route.addr, &listen_id->route.addr,
887e51060f0SSean Hefty 			  ip_ver, port, src, dst);
888*3f168d2bSKrishna Kumar 
889*3f168d2bSKrishna Kumar 	rt = &id->route;
890*3f168d2bSKrishna Kumar 	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
891*3f168d2bSKrishna Kumar 	rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
892*3f168d2bSKrishna Kumar 			       GFP_KERNEL);
893*3f168d2bSKrishna Kumar 	if (!rt->path_rec)
894*3f168d2bSKrishna Kumar 		goto destroy_id;
895*3f168d2bSKrishna Kumar 
896e51060f0SSean Hefty 	rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
897e51060f0SSean Hefty 	if (rt->num_paths == 2)
898e51060f0SSean Hefty 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
899e51060f0SSean Hefty 
900e51060f0SSean Hefty 	ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
901e51060f0SSean Hefty 	ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
902e51060f0SSean Hefty 	ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
90307ebafbaSTom Tucker 	rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
904e51060f0SSean Hefty 
905e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
906e51060f0SSean Hefty 	id_priv->state = CMA_CONNECT;
907e51060f0SSean Hefty 	return id_priv;
908*3f168d2bSKrishna Kumar 
909*3f168d2bSKrishna Kumar destroy_id:
910e51060f0SSean Hefty 	rdma_destroy_id(id);
911*3f168d2bSKrishna Kumar err:
912e51060f0SSean Hefty 	return NULL;
913e51060f0SSean Hefty }
914e51060f0SSean Hefty 
915e51060f0SSean Hefty static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
916e51060f0SSean Hefty {
917e51060f0SSean Hefty 	struct rdma_id_private *listen_id, *conn_id;
918e51060f0SSean Hefty 	int offset, ret;
919e51060f0SSean Hefty 
920e51060f0SSean Hefty 	listen_id = cm_id->context;
921e51060f0SSean Hefty 	atomic_inc(&listen_id->dev_remove);
922e51060f0SSean Hefty 	if (!cma_comp(listen_id, CMA_LISTEN)) {
923e51060f0SSean Hefty 		ret = -ECONNABORTED;
924e51060f0SSean Hefty 		goto out;
925e51060f0SSean Hefty 	}
926e51060f0SSean Hefty 
927e51060f0SSean Hefty 	conn_id = cma_new_id(&listen_id->id, ib_event);
928e51060f0SSean Hefty 	if (!conn_id) {
929e51060f0SSean Hefty 		ret = -ENOMEM;
930e51060f0SSean Hefty 		goto out;
931e51060f0SSean Hefty 	}
932e51060f0SSean Hefty 
933e51060f0SSean Hefty 	atomic_inc(&conn_id->dev_remove);
93461a73c70SSean Hefty 	mutex_lock(&lock);
93507ebafbaSTom Tucker 	ret = cma_acquire_dev(conn_id);
93661a73c70SSean Hefty 	mutex_unlock(&lock);
937e51060f0SSean Hefty 	if (ret) {
938e51060f0SSean Hefty 		ret = -ENODEV;
9396e35aabeSKrishna Kumar 		cma_exch(conn_id, CMA_DESTROYING);
940e51060f0SSean Hefty 		cma_release_remove(conn_id);
941e51060f0SSean Hefty 		rdma_destroy_id(&conn_id->id);
942e51060f0SSean Hefty 		goto out;
943e51060f0SSean Hefty 	}
944e51060f0SSean Hefty 
945e51060f0SSean Hefty 	conn_id->cm_id.ib = cm_id;
946e51060f0SSean Hefty 	cm_id->context = conn_id;
947e51060f0SSean Hefty 	cm_id->cm_handler = cma_ib_handler;
948e51060f0SSean Hefty 
949e51060f0SSean Hefty 	offset = cma_user_data_offset(listen_id->id.ps);
950e51060f0SSean Hefty 	ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0,
951e51060f0SSean Hefty 			      ib_event->private_data + offset,
952e51060f0SSean Hefty 			      IB_CM_REQ_PRIVATE_DATA_SIZE - offset);
953e51060f0SSean Hefty 	if (ret) {
954e51060f0SSean Hefty 		/* Destroy the CM ID by returning a non-zero value. */
955e51060f0SSean Hefty 		conn_id->cm_id.ib = NULL;
956e51060f0SSean Hefty 		cma_exch(conn_id, CMA_DESTROYING);
957e51060f0SSean Hefty 		cma_release_remove(conn_id);
958e51060f0SSean Hefty 		rdma_destroy_id(&conn_id->id);
959e51060f0SSean Hefty 	}
960e51060f0SSean Hefty out:
961e51060f0SSean Hefty 	cma_release_remove(listen_id);
962e51060f0SSean Hefty 	return ret;
963e51060f0SSean Hefty }
964e51060f0SSean Hefty 
965e51060f0SSean Hefty static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
966e51060f0SSean Hefty {
967e51060f0SSean Hefty 	return cpu_to_be64(((u64)ps << 16) +
968e51060f0SSean Hefty 	       be16_to_cpu(((struct sockaddr_in *) addr)->sin_port));
969e51060f0SSean Hefty }
970e51060f0SSean Hefty 
971e51060f0SSean Hefty static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
972e51060f0SSean Hefty 				 struct ib_cm_compare_data *compare)
973e51060f0SSean Hefty {
974e51060f0SSean Hefty 	struct cma_hdr *cma_data, *cma_mask;
975e51060f0SSean Hefty 	struct sdp_hh *sdp_data, *sdp_mask;
976e51060f0SSean Hefty 	__u32 ip4_addr;
977e51060f0SSean Hefty 	struct in6_addr ip6_addr;
978e51060f0SSean Hefty 
979e51060f0SSean Hefty 	memset(compare, 0, sizeof *compare);
980e51060f0SSean Hefty 	cma_data = (void *) compare->data;
981e51060f0SSean Hefty 	cma_mask = (void *) compare->mask;
982e51060f0SSean Hefty 	sdp_data = (void *) compare->data;
983e51060f0SSean Hefty 	sdp_mask = (void *) compare->mask;
984e51060f0SSean Hefty 
985e51060f0SSean Hefty 	switch (addr->sa_family) {
986e51060f0SSean Hefty 	case AF_INET:
987e51060f0SSean Hefty 		ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
988e51060f0SSean Hefty 		if (ps == RDMA_PS_SDP) {
989e51060f0SSean Hefty 			sdp_set_ip_ver(sdp_data, 4);
990e51060f0SSean Hefty 			sdp_set_ip_ver(sdp_mask, 0xF);
991e51060f0SSean Hefty 			sdp_data->dst_addr.ip4.addr = ip4_addr;
992e51060f0SSean Hefty 			sdp_mask->dst_addr.ip4.addr = ~0;
993e51060f0SSean Hefty 		} else {
994e51060f0SSean Hefty 			cma_set_ip_ver(cma_data, 4);
995e51060f0SSean Hefty 			cma_set_ip_ver(cma_mask, 0xF);
996e51060f0SSean Hefty 			cma_data->dst_addr.ip4.addr = ip4_addr;
997e51060f0SSean Hefty 			cma_mask->dst_addr.ip4.addr = ~0;
998e51060f0SSean Hefty 		}
999e51060f0SSean Hefty 		break;
1000e51060f0SSean Hefty 	case AF_INET6:
1001e51060f0SSean Hefty 		ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1002e51060f0SSean Hefty 		if (ps == RDMA_PS_SDP) {
1003e51060f0SSean Hefty 			sdp_set_ip_ver(sdp_data, 6);
1004e51060f0SSean Hefty 			sdp_set_ip_ver(sdp_mask, 0xF);
1005e51060f0SSean Hefty 			sdp_data->dst_addr.ip6 = ip6_addr;
1006e51060f0SSean Hefty 			memset(&sdp_mask->dst_addr.ip6, 0xFF,
1007e51060f0SSean Hefty 			       sizeof sdp_mask->dst_addr.ip6);
1008e51060f0SSean Hefty 		} else {
1009e51060f0SSean Hefty 			cma_set_ip_ver(cma_data, 6);
1010e51060f0SSean Hefty 			cma_set_ip_ver(cma_mask, 0xF);
1011e51060f0SSean Hefty 			cma_data->dst_addr.ip6 = ip6_addr;
1012e51060f0SSean Hefty 			memset(&cma_mask->dst_addr.ip6, 0xFF,
1013e51060f0SSean Hefty 			       sizeof cma_mask->dst_addr.ip6);
1014e51060f0SSean Hefty 		}
1015e51060f0SSean Hefty 		break;
1016e51060f0SSean Hefty 	default:
1017e51060f0SSean Hefty 		break;
1018e51060f0SSean Hefty 	}
1019e51060f0SSean Hefty }
1020e51060f0SSean Hefty 
102107ebafbaSTom Tucker static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
102207ebafbaSTom Tucker {
102307ebafbaSTom Tucker 	struct rdma_id_private *id_priv = iw_id->context;
102407ebafbaSTom Tucker 	enum rdma_cm_event_type event = 0;
102507ebafbaSTom Tucker 	struct sockaddr_in *sin;
102607ebafbaSTom Tucker 	int ret = 0;
102707ebafbaSTom Tucker 
102807ebafbaSTom Tucker 	atomic_inc(&id_priv->dev_remove);
102907ebafbaSTom Tucker 
103007ebafbaSTom Tucker 	switch (iw_event->event) {
103107ebafbaSTom Tucker 	case IW_CM_EVENT_CLOSE:
103207ebafbaSTom Tucker 		event = RDMA_CM_EVENT_DISCONNECTED;
103307ebafbaSTom Tucker 		break;
103407ebafbaSTom Tucker 	case IW_CM_EVENT_CONNECT_REPLY:
103507ebafbaSTom Tucker 		sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
103607ebafbaSTom Tucker 		*sin = iw_event->local_addr;
103707ebafbaSTom Tucker 		sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
103807ebafbaSTom Tucker 		*sin = iw_event->remote_addr;
103907ebafbaSTom Tucker 		if (iw_event->status)
104007ebafbaSTom Tucker 			event = RDMA_CM_EVENT_REJECTED;
104107ebafbaSTom Tucker 		else
104207ebafbaSTom Tucker 			event = RDMA_CM_EVENT_ESTABLISHED;
104307ebafbaSTom Tucker 		break;
104407ebafbaSTom Tucker 	case IW_CM_EVENT_ESTABLISHED:
104507ebafbaSTom Tucker 		event = RDMA_CM_EVENT_ESTABLISHED;
104607ebafbaSTom Tucker 		break;
104707ebafbaSTom Tucker 	default:
104807ebafbaSTom Tucker 		BUG_ON(1);
104907ebafbaSTom Tucker 	}
105007ebafbaSTom Tucker 
105107ebafbaSTom Tucker 	ret = cma_notify_user(id_priv, event, iw_event->status,
105207ebafbaSTom Tucker 			      iw_event->private_data,
105307ebafbaSTom Tucker 			      iw_event->private_data_len);
105407ebafbaSTom Tucker 	if (ret) {
105507ebafbaSTom Tucker 		/* Destroy the CM ID by returning a non-zero value. */
105607ebafbaSTom Tucker 		id_priv->cm_id.iw = NULL;
105707ebafbaSTom Tucker 		cma_exch(id_priv, CMA_DESTROYING);
105807ebafbaSTom Tucker 		cma_release_remove(id_priv);
105907ebafbaSTom Tucker 		rdma_destroy_id(&id_priv->id);
106007ebafbaSTom Tucker 		return ret;
106107ebafbaSTom Tucker 	}
106207ebafbaSTom Tucker 
106307ebafbaSTom Tucker 	cma_release_remove(id_priv);
106407ebafbaSTom Tucker 	return ret;
106507ebafbaSTom Tucker }
106607ebafbaSTom Tucker 
106707ebafbaSTom Tucker static int iw_conn_req_handler(struct iw_cm_id *cm_id,
106807ebafbaSTom Tucker 			       struct iw_cm_event *iw_event)
106907ebafbaSTom Tucker {
107007ebafbaSTom Tucker 	struct rdma_cm_id *new_cm_id;
107107ebafbaSTom Tucker 	struct rdma_id_private *listen_id, *conn_id;
107207ebafbaSTom Tucker 	struct sockaddr_in *sin;
107307ebafbaSTom Tucker 	struct net_device *dev = NULL;
107407ebafbaSTom Tucker 	int ret;
107507ebafbaSTom Tucker 
107607ebafbaSTom Tucker 	listen_id = cm_id->context;
107707ebafbaSTom Tucker 	atomic_inc(&listen_id->dev_remove);
107807ebafbaSTom Tucker 	if (!cma_comp(listen_id, CMA_LISTEN)) {
107907ebafbaSTom Tucker 		ret = -ECONNABORTED;
108007ebafbaSTom Tucker 		goto out;
108107ebafbaSTom Tucker 	}
108207ebafbaSTom Tucker 
108307ebafbaSTom Tucker 	/* Create a new RDMA id for the new IW CM ID */
108407ebafbaSTom Tucker 	new_cm_id = rdma_create_id(listen_id->id.event_handler,
108507ebafbaSTom Tucker 				   listen_id->id.context,
108607ebafbaSTom Tucker 				   RDMA_PS_TCP);
108707ebafbaSTom Tucker 	if (!new_cm_id) {
108807ebafbaSTom Tucker 		ret = -ENOMEM;
108907ebafbaSTom Tucker 		goto out;
109007ebafbaSTom Tucker 	}
109107ebafbaSTom Tucker 	conn_id = container_of(new_cm_id, struct rdma_id_private, id);
109207ebafbaSTom Tucker 	atomic_inc(&conn_id->dev_remove);
109307ebafbaSTom Tucker 	conn_id->state = CMA_CONNECT;
109407ebafbaSTom Tucker 
109507ebafbaSTom Tucker 	dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
109607ebafbaSTom Tucker 	if (!dev) {
109707ebafbaSTom Tucker 		ret = -EADDRNOTAVAIL;
109807ebafbaSTom Tucker 		cma_release_remove(conn_id);
109907ebafbaSTom Tucker 		rdma_destroy_id(new_cm_id);
110007ebafbaSTom Tucker 		goto out;
110107ebafbaSTom Tucker 	}
110207ebafbaSTom Tucker 	ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
110307ebafbaSTom Tucker 	if (ret) {
110407ebafbaSTom Tucker 		cma_release_remove(conn_id);
110507ebafbaSTom Tucker 		rdma_destroy_id(new_cm_id);
110607ebafbaSTom Tucker 		goto out;
110707ebafbaSTom Tucker 	}
110807ebafbaSTom Tucker 
110961a73c70SSean Hefty 	mutex_lock(&lock);
111007ebafbaSTom Tucker 	ret = cma_acquire_dev(conn_id);
111161a73c70SSean Hefty 	mutex_unlock(&lock);
111207ebafbaSTom Tucker 	if (ret) {
111307ebafbaSTom Tucker 		cma_release_remove(conn_id);
111407ebafbaSTom Tucker 		rdma_destroy_id(new_cm_id);
111507ebafbaSTom Tucker 		goto out;
111607ebafbaSTom Tucker 	}
111707ebafbaSTom Tucker 
111807ebafbaSTom Tucker 	conn_id->cm_id.iw = cm_id;
111907ebafbaSTom Tucker 	cm_id->context = conn_id;
112007ebafbaSTom Tucker 	cm_id->cm_handler = cma_iw_handler;
112107ebafbaSTom Tucker 
112207ebafbaSTom Tucker 	sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
112307ebafbaSTom Tucker 	*sin = iw_event->local_addr;
112407ebafbaSTom Tucker 	sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
112507ebafbaSTom Tucker 	*sin = iw_event->remote_addr;
112607ebafbaSTom Tucker 
112707ebafbaSTom Tucker 	ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0,
112807ebafbaSTom Tucker 			      iw_event->private_data,
112907ebafbaSTom Tucker 			      iw_event->private_data_len);
113007ebafbaSTom Tucker 	if (ret) {
113107ebafbaSTom Tucker 		/* User wants to destroy the CM ID */
113207ebafbaSTom Tucker 		conn_id->cm_id.iw = NULL;
113307ebafbaSTom Tucker 		cma_exch(conn_id, CMA_DESTROYING);
113407ebafbaSTom Tucker 		cma_release_remove(conn_id);
113507ebafbaSTom Tucker 		rdma_destroy_id(&conn_id->id);
113607ebafbaSTom Tucker 	}
113707ebafbaSTom Tucker 
113807ebafbaSTom Tucker out:
113907ebafbaSTom Tucker 	if (dev)
114007ebafbaSTom Tucker 		dev_put(dev);
114107ebafbaSTom Tucker 	cma_release_remove(listen_id);
114207ebafbaSTom Tucker 	return ret;
114307ebafbaSTom Tucker }
114407ebafbaSTom Tucker 
1145e51060f0SSean Hefty static int cma_ib_listen(struct rdma_id_private *id_priv)
1146e51060f0SSean Hefty {
1147e51060f0SSean Hefty 	struct ib_cm_compare_data compare_data;
1148e51060f0SSean Hefty 	struct sockaddr *addr;
1149e51060f0SSean Hefty 	__be64 svc_id;
1150e51060f0SSean Hefty 	int ret;
1151e51060f0SSean Hefty 
1152e51060f0SSean Hefty 	id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1153e51060f0SSean Hefty 					    id_priv);
1154e51060f0SSean Hefty 	if (IS_ERR(id_priv->cm_id.ib))
1155e51060f0SSean Hefty 		return PTR_ERR(id_priv->cm_id.ib);
1156e51060f0SSean Hefty 
1157e51060f0SSean Hefty 	addr = &id_priv->id.route.addr.src_addr;
1158e51060f0SSean Hefty 	svc_id = cma_get_service_id(id_priv->id.ps, addr);
1159e51060f0SSean Hefty 	if (cma_any_addr(addr))
1160e51060f0SSean Hefty 		ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1161e51060f0SSean Hefty 	else {
1162e51060f0SSean Hefty 		cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1163e51060f0SSean Hefty 		ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1164e51060f0SSean Hefty 	}
1165e51060f0SSean Hefty 
1166e51060f0SSean Hefty 	if (ret) {
1167e51060f0SSean Hefty 		ib_destroy_cm_id(id_priv->cm_id.ib);
1168e51060f0SSean Hefty 		id_priv->cm_id.ib = NULL;
1169e51060f0SSean Hefty 	}
1170e51060f0SSean Hefty 
1171e51060f0SSean Hefty 	return ret;
1172e51060f0SSean Hefty }
1173e51060f0SSean Hefty 
117407ebafbaSTom Tucker static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
117507ebafbaSTom Tucker {
117607ebafbaSTom Tucker 	int ret;
117707ebafbaSTom Tucker 	struct sockaddr_in *sin;
117807ebafbaSTom Tucker 
117907ebafbaSTom Tucker 	id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
118007ebafbaSTom Tucker 					    iw_conn_req_handler,
118107ebafbaSTom Tucker 					    id_priv);
118207ebafbaSTom Tucker 	if (IS_ERR(id_priv->cm_id.iw))
118307ebafbaSTom Tucker 		return PTR_ERR(id_priv->cm_id.iw);
118407ebafbaSTom Tucker 
118507ebafbaSTom Tucker 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
118607ebafbaSTom Tucker 	id_priv->cm_id.iw->local_addr = *sin;
118707ebafbaSTom Tucker 
118807ebafbaSTom Tucker 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
118907ebafbaSTom Tucker 
119007ebafbaSTom Tucker 	if (ret) {
119107ebafbaSTom Tucker 		iw_destroy_cm_id(id_priv->cm_id.iw);
119207ebafbaSTom Tucker 		id_priv->cm_id.iw = NULL;
119307ebafbaSTom Tucker 	}
119407ebafbaSTom Tucker 
119507ebafbaSTom Tucker 	return ret;
119607ebafbaSTom Tucker }
119707ebafbaSTom Tucker 
1198e51060f0SSean Hefty static int cma_listen_handler(struct rdma_cm_id *id,
1199e51060f0SSean Hefty 			      struct rdma_cm_event *event)
1200e51060f0SSean Hefty {
1201e51060f0SSean Hefty 	struct rdma_id_private *id_priv = id->context;
1202e51060f0SSean Hefty 
1203e51060f0SSean Hefty 	id->context = id_priv->id.context;
1204e51060f0SSean Hefty 	id->event_handler = id_priv->id.event_handler;
1205e51060f0SSean Hefty 	return id_priv->id.event_handler(id, event);
1206e51060f0SSean Hefty }
1207e51060f0SSean Hefty 
1208e51060f0SSean Hefty static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1209e51060f0SSean Hefty 			      struct cma_device *cma_dev)
1210e51060f0SSean Hefty {
1211e51060f0SSean Hefty 	struct rdma_id_private *dev_id_priv;
1212e51060f0SSean Hefty 	struct rdma_cm_id *id;
1213e51060f0SSean Hefty 	int ret;
1214e51060f0SSean Hefty 
1215e51060f0SSean Hefty 	id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1216e51060f0SSean Hefty 	if (IS_ERR(id))
1217e51060f0SSean Hefty 		return;
1218e51060f0SSean Hefty 
1219e51060f0SSean Hefty 	dev_id_priv = container_of(id, struct rdma_id_private, id);
1220e51060f0SSean Hefty 
1221e51060f0SSean Hefty 	dev_id_priv->state = CMA_ADDR_BOUND;
1222e51060f0SSean Hefty 	memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1223e51060f0SSean Hefty 	       ip_addr_size(&id_priv->id.route.addr.src_addr));
1224e51060f0SSean Hefty 
1225e51060f0SSean Hefty 	cma_attach_to_dev(dev_id_priv, cma_dev);
1226e51060f0SSean Hefty 	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1227e51060f0SSean Hefty 
1228e51060f0SSean Hefty 	ret = rdma_listen(id, id_priv->backlog);
1229e51060f0SSean Hefty 	if (ret)
1230e51060f0SSean Hefty 		goto err;
1231e51060f0SSean Hefty 
1232e51060f0SSean Hefty 	return;
1233e51060f0SSean Hefty err:
1234e51060f0SSean Hefty 	cma_destroy_listen(dev_id_priv);
1235e51060f0SSean Hefty }
1236e51060f0SSean Hefty 
1237e51060f0SSean Hefty static void cma_listen_on_all(struct rdma_id_private *id_priv)
1238e51060f0SSean Hefty {
1239e51060f0SSean Hefty 	struct cma_device *cma_dev;
1240e51060f0SSean Hefty 
1241e51060f0SSean Hefty 	mutex_lock(&lock);
1242e51060f0SSean Hefty 	list_add_tail(&id_priv->list, &listen_any_list);
1243e51060f0SSean Hefty 	list_for_each_entry(cma_dev, &dev_list, list)
1244e51060f0SSean Hefty 		cma_listen_on_dev(id_priv, cma_dev);
1245e51060f0SSean Hefty 	mutex_unlock(&lock);
1246e51060f0SSean Hefty }
1247e51060f0SSean Hefty 
1248e51060f0SSean Hefty static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1249e51060f0SSean Hefty {
1250e51060f0SSean Hefty 	struct sockaddr_in addr_in;
1251e51060f0SSean Hefty 
1252e51060f0SSean Hefty 	memset(&addr_in, 0, sizeof addr_in);
1253e51060f0SSean Hefty 	addr_in.sin_family = af;
1254e51060f0SSean Hefty 	return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1255e51060f0SSean Hefty }
1256e51060f0SSean Hefty 
1257e51060f0SSean Hefty int rdma_listen(struct rdma_cm_id *id, int backlog)
1258e51060f0SSean Hefty {
1259e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1260e51060f0SSean Hefty 	int ret;
1261e51060f0SSean Hefty 
1262e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1263e51060f0SSean Hefty 	if (id_priv->state == CMA_IDLE) {
1264e51060f0SSean Hefty 		ret = cma_bind_any(id, AF_INET);
1265e51060f0SSean Hefty 		if (ret)
1266e51060f0SSean Hefty 			return ret;
1267e51060f0SSean Hefty 	}
1268e51060f0SSean Hefty 
1269e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1270e51060f0SSean Hefty 		return -EINVAL;
1271e51060f0SSean Hefty 
1272e51060f0SSean Hefty 	id_priv->backlog = backlog;
1273e51060f0SSean Hefty 	if (id->device) {
127407ebafbaSTom Tucker 		switch (rdma_node_get_transport(id->device->node_type)) {
127507ebafbaSTom Tucker 		case RDMA_TRANSPORT_IB:
1276e51060f0SSean Hefty 			ret = cma_ib_listen(id_priv);
1277e51060f0SSean Hefty 			if (ret)
1278e51060f0SSean Hefty 				goto err;
1279e51060f0SSean Hefty 			break;
128007ebafbaSTom Tucker 		case RDMA_TRANSPORT_IWARP:
128107ebafbaSTom Tucker 			ret = cma_iw_listen(id_priv, backlog);
128207ebafbaSTom Tucker 			if (ret)
128307ebafbaSTom Tucker 				goto err;
128407ebafbaSTom Tucker 			break;
1285e51060f0SSean Hefty 		default:
1286e51060f0SSean Hefty 			ret = -ENOSYS;
1287e51060f0SSean Hefty 			goto err;
1288e51060f0SSean Hefty 		}
1289e51060f0SSean Hefty 	} else
1290e51060f0SSean Hefty 		cma_listen_on_all(id_priv);
1291e51060f0SSean Hefty 
1292e51060f0SSean Hefty 	return 0;
1293e51060f0SSean Hefty err:
1294e51060f0SSean Hefty 	id_priv->backlog = 0;
1295e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1296e51060f0SSean Hefty 	return ret;
1297e51060f0SSean Hefty }
1298e51060f0SSean Hefty EXPORT_SYMBOL(rdma_listen);
1299e51060f0SSean Hefty 
1300e51060f0SSean Hefty static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1301e51060f0SSean Hefty 			      void *context)
1302e51060f0SSean Hefty {
1303e51060f0SSean Hefty 	struct cma_work *work = context;
1304e51060f0SSean Hefty 	struct rdma_route *route;
1305e51060f0SSean Hefty 
1306e51060f0SSean Hefty 	route = &work->id->id.route;
1307e51060f0SSean Hefty 
1308e51060f0SSean Hefty 	if (!status) {
1309e51060f0SSean Hefty 		route->num_paths = 1;
1310e51060f0SSean Hefty 		*route->path_rec = *path_rec;
1311e51060f0SSean Hefty 	} else {
1312e51060f0SSean Hefty 		work->old_state = CMA_ROUTE_QUERY;
1313e51060f0SSean Hefty 		work->new_state = CMA_ADDR_RESOLVED;
1314e51060f0SSean Hefty 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
13158f0472d3SSean Hefty 		work->event.status = status;
1316e51060f0SSean Hefty 	}
1317e51060f0SSean Hefty 
1318e51060f0SSean Hefty 	queue_work(cma_wq, &work->work);
1319e51060f0SSean Hefty }
1320e51060f0SSean Hefty 
1321e51060f0SSean Hefty static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1322e51060f0SSean Hefty 			      struct cma_work *work)
1323e51060f0SSean Hefty {
1324e51060f0SSean Hefty 	struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1325e51060f0SSean Hefty 	struct ib_sa_path_rec path_rec;
1326e51060f0SSean Hefty 
1327e51060f0SSean Hefty 	memset(&path_rec, 0, sizeof path_rec);
1328f0ee3404SMichael S. Tsirkin 	ib_addr_get_sgid(addr, &path_rec.sgid);
1329f0ee3404SMichael S. Tsirkin 	ib_addr_get_dgid(addr, &path_rec.dgid);
1330e51060f0SSean Hefty 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1331e51060f0SSean Hefty 	path_rec.numb_path = 1;
1332e51060f0SSean Hefty 
1333c1a0b23bSMichael S. Tsirkin 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1334e51060f0SSean Hefty 				id_priv->id.port_num, &path_rec,
1335e51060f0SSean Hefty 				IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1336e51060f0SSean Hefty 				IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1337e51060f0SSean Hefty 				timeout_ms, GFP_KERNEL,
1338e51060f0SSean Hefty 				cma_query_handler, work, &id_priv->query);
1339e51060f0SSean Hefty 
1340e51060f0SSean Hefty 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1341e51060f0SSean Hefty }
1342e51060f0SSean Hefty 
1343e51060f0SSean Hefty static void cma_work_handler(void *data)
1344e51060f0SSean Hefty {
1345e51060f0SSean Hefty 	struct cma_work *work = data;
1346e51060f0SSean Hefty 	struct rdma_id_private *id_priv = work->id;
1347e51060f0SSean Hefty 	int destroy = 0;
1348e51060f0SSean Hefty 
1349e51060f0SSean Hefty 	atomic_inc(&id_priv->dev_remove);
1350e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1351e51060f0SSean Hefty 		goto out;
1352e51060f0SSean Hefty 
1353e51060f0SSean Hefty 	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1354e51060f0SSean Hefty 		cma_exch(id_priv, CMA_DESTROYING);
1355e51060f0SSean Hefty 		destroy = 1;
1356e51060f0SSean Hefty 	}
1357e51060f0SSean Hefty out:
1358e51060f0SSean Hefty 	cma_release_remove(id_priv);
1359e51060f0SSean Hefty 	cma_deref_id(id_priv);
1360e51060f0SSean Hefty 	if (destroy)
1361e51060f0SSean Hefty 		rdma_destroy_id(&id_priv->id);
1362e51060f0SSean Hefty 	kfree(work);
1363e51060f0SSean Hefty }
1364e51060f0SSean Hefty 
1365e51060f0SSean Hefty static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1366e51060f0SSean Hefty {
1367e51060f0SSean Hefty 	struct rdma_route *route = &id_priv->id.route;
1368e51060f0SSean Hefty 	struct cma_work *work;
1369e51060f0SSean Hefty 	int ret;
1370e51060f0SSean Hefty 
1371e51060f0SSean Hefty 	work = kzalloc(sizeof *work, GFP_KERNEL);
1372e51060f0SSean Hefty 	if (!work)
1373e51060f0SSean Hefty 		return -ENOMEM;
1374e51060f0SSean Hefty 
1375e51060f0SSean Hefty 	work->id = id_priv;
1376e51060f0SSean Hefty 	INIT_WORK(&work->work, cma_work_handler, work);
1377e51060f0SSean Hefty 	work->old_state = CMA_ROUTE_QUERY;
1378e51060f0SSean Hefty 	work->new_state = CMA_ROUTE_RESOLVED;
1379e51060f0SSean Hefty 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1380e51060f0SSean Hefty 
1381e51060f0SSean Hefty 	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1382e51060f0SSean Hefty 	if (!route->path_rec) {
1383e51060f0SSean Hefty 		ret = -ENOMEM;
1384e51060f0SSean Hefty 		goto err1;
1385e51060f0SSean Hefty 	}
1386e51060f0SSean Hefty 
1387e51060f0SSean Hefty 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
1388e51060f0SSean Hefty 	if (ret)
1389e51060f0SSean Hefty 		goto err2;
1390e51060f0SSean Hefty 
1391e51060f0SSean Hefty 	return 0;
1392e51060f0SSean Hefty err2:
1393e51060f0SSean Hefty 	kfree(route->path_rec);
1394e51060f0SSean Hefty 	route->path_rec = NULL;
1395e51060f0SSean Hefty err1:
1396e51060f0SSean Hefty 	kfree(work);
1397e51060f0SSean Hefty 	return ret;
1398e51060f0SSean Hefty }
1399e51060f0SSean Hefty 
1400e51060f0SSean Hefty int rdma_set_ib_paths(struct rdma_cm_id *id,
1401e51060f0SSean Hefty 		      struct ib_sa_path_rec *path_rec, int num_paths)
1402e51060f0SSean Hefty {
1403e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1404e51060f0SSean Hefty 	int ret;
1405e51060f0SSean Hefty 
1406e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1407e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1408e51060f0SSean Hefty 		return -EINVAL;
1409e51060f0SSean Hefty 
1410e51060f0SSean Hefty 	id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1411e51060f0SSean Hefty 	if (!id->route.path_rec) {
1412e51060f0SSean Hefty 		ret = -ENOMEM;
1413e51060f0SSean Hefty 		goto err;
1414e51060f0SSean Hefty 	}
1415e51060f0SSean Hefty 
1416e51060f0SSean Hefty 	memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1417e51060f0SSean Hefty 	return 0;
1418e51060f0SSean Hefty err:
1419e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1420e51060f0SSean Hefty 	return ret;
1421e51060f0SSean Hefty }
1422e51060f0SSean Hefty EXPORT_SYMBOL(rdma_set_ib_paths);
1423e51060f0SSean Hefty 
142407ebafbaSTom Tucker static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
142507ebafbaSTom Tucker {
142607ebafbaSTom Tucker 	struct cma_work *work;
142707ebafbaSTom Tucker 
142807ebafbaSTom Tucker 	work = kzalloc(sizeof *work, GFP_KERNEL);
142907ebafbaSTom Tucker 	if (!work)
143007ebafbaSTom Tucker 		return -ENOMEM;
143107ebafbaSTom Tucker 
143207ebafbaSTom Tucker 	work->id = id_priv;
143307ebafbaSTom Tucker 	INIT_WORK(&work->work, cma_work_handler, work);
143407ebafbaSTom Tucker 	work->old_state = CMA_ROUTE_QUERY;
143507ebafbaSTom Tucker 	work->new_state = CMA_ROUTE_RESOLVED;
143607ebafbaSTom Tucker 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
143707ebafbaSTom Tucker 	queue_work(cma_wq, &work->work);
143807ebafbaSTom Tucker 	return 0;
143907ebafbaSTom Tucker }
144007ebafbaSTom Tucker 
1441e51060f0SSean Hefty int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1442e51060f0SSean Hefty {
1443e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1444e51060f0SSean Hefty 	int ret;
1445e51060f0SSean Hefty 
1446e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1447e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1448e51060f0SSean Hefty 		return -EINVAL;
1449e51060f0SSean Hefty 
1450e51060f0SSean Hefty 	atomic_inc(&id_priv->refcount);
145107ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
145207ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
1453e51060f0SSean Hefty 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
1454e51060f0SSean Hefty 		break;
145507ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
145607ebafbaSTom Tucker 		ret = cma_resolve_iw_route(id_priv, timeout_ms);
145707ebafbaSTom Tucker 		break;
1458e51060f0SSean Hefty 	default:
1459e51060f0SSean Hefty 		ret = -ENOSYS;
1460e51060f0SSean Hefty 		break;
1461e51060f0SSean Hefty 	}
1462e51060f0SSean Hefty 	if (ret)
1463e51060f0SSean Hefty 		goto err;
1464e51060f0SSean Hefty 
1465e51060f0SSean Hefty 	return 0;
1466e51060f0SSean Hefty err:
1467e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1468e51060f0SSean Hefty 	cma_deref_id(id_priv);
1469e51060f0SSean Hefty 	return ret;
1470e51060f0SSean Hefty }
1471e51060f0SSean Hefty EXPORT_SYMBOL(rdma_resolve_route);
1472e51060f0SSean Hefty 
1473e51060f0SSean Hefty static int cma_bind_loopback(struct rdma_id_private *id_priv)
1474e51060f0SSean Hefty {
1475e51060f0SSean Hefty 	struct cma_device *cma_dev;
1476e51060f0SSean Hefty 	struct ib_port_attr port_attr;
1477f0ee3404SMichael S. Tsirkin 	union ib_gid gid;
1478e51060f0SSean Hefty 	u16 pkey;
1479e51060f0SSean Hefty 	int ret;
1480e51060f0SSean Hefty 	u8 p;
1481e51060f0SSean Hefty 
1482e51060f0SSean Hefty 	mutex_lock(&lock);
1483e51060f0SSean Hefty 	list_for_each_entry(cma_dev, &dev_list, list)
1484e51060f0SSean Hefty 		for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1485e51060f0SSean Hefty 			if (!ib_query_port (cma_dev->device, p, &port_attr) &&
1486e51060f0SSean Hefty 			    port_attr.state == IB_PORT_ACTIVE)
1487e51060f0SSean Hefty 				goto port_found;
1488e51060f0SSean Hefty 
1489e51060f0SSean Hefty 	if (!list_empty(&dev_list)) {
1490e51060f0SSean Hefty 		p = 1;
1491e51060f0SSean Hefty 		cma_dev = list_entry(dev_list.next, struct cma_device, list);
1492e51060f0SSean Hefty 	} else {
1493e51060f0SSean Hefty 		ret = -ENODEV;
1494e51060f0SSean Hefty 		goto out;
1495e51060f0SSean Hefty 	}
1496e51060f0SSean Hefty 
1497e51060f0SSean Hefty port_found:
1498f0ee3404SMichael S. Tsirkin 	ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1499e51060f0SSean Hefty 	if (ret)
1500e51060f0SSean Hefty 		goto out;
1501e51060f0SSean Hefty 
1502e51060f0SSean Hefty 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1503e51060f0SSean Hefty 	if (ret)
1504e51060f0SSean Hefty 		goto out;
1505e51060f0SSean Hefty 
1506f0ee3404SMichael S. Tsirkin 	ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1507e51060f0SSean Hefty 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1508e51060f0SSean Hefty 	id_priv->id.port_num = p;
1509e51060f0SSean Hefty 	cma_attach_to_dev(id_priv, cma_dev);
1510e51060f0SSean Hefty out:
1511e51060f0SSean Hefty 	mutex_unlock(&lock);
1512e51060f0SSean Hefty 	return ret;
1513e51060f0SSean Hefty }
1514e51060f0SSean Hefty 
1515e51060f0SSean Hefty static void addr_handler(int status, struct sockaddr *src_addr,
1516e51060f0SSean Hefty 			 struct rdma_dev_addr *dev_addr, void *context)
1517e51060f0SSean Hefty {
1518e51060f0SSean Hefty 	struct rdma_id_private *id_priv = context;
1519e51060f0SSean Hefty 	enum rdma_cm_event_type event;
1520e51060f0SSean Hefty 
1521e51060f0SSean Hefty 	atomic_inc(&id_priv->dev_remove);
152261a73c70SSean Hefty 
152361a73c70SSean Hefty 	/*
152461a73c70SSean Hefty 	 * Grab mutex to block rdma_destroy_id() from removing the device while
152561a73c70SSean Hefty 	 * we're trying to acquire it.
152661a73c70SSean Hefty 	 */
152761a73c70SSean Hefty 	mutex_lock(&lock);
152861a73c70SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
152961a73c70SSean Hefty 		mutex_unlock(&lock);
153061a73c70SSean Hefty 		goto out;
153161a73c70SSean Hefty 	}
153261a73c70SSean Hefty 
153361a73c70SSean Hefty 	if (!status && !id_priv->cma_dev)
1534e51060f0SSean Hefty 		status = cma_acquire_dev(id_priv);
153561a73c70SSean Hefty 	mutex_unlock(&lock);
1536e51060f0SSean Hefty 
1537e51060f0SSean Hefty 	if (status) {
153861a73c70SSean Hefty 		if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1539e51060f0SSean Hefty 			goto out;
1540e51060f0SSean Hefty 		event = RDMA_CM_EVENT_ADDR_ERROR;
1541e51060f0SSean Hefty 	} else {
1542e51060f0SSean Hefty 		memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1543e51060f0SSean Hefty 		       ip_addr_size(src_addr));
1544e51060f0SSean Hefty 		event = RDMA_CM_EVENT_ADDR_RESOLVED;
1545e51060f0SSean Hefty 	}
1546e51060f0SSean Hefty 
1547e51060f0SSean Hefty 	if (cma_notify_user(id_priv, event, status, NULL, 0)) {
1548e51060f0SSean Hefty 		cma_exch(id_priv, CMA_DESTROYING);
1549e51060f0SSean Hefty 		cma_release_remove(id_priv);
1550e51060f0SSean Hefty 		cma_deref_id(id_priv);
1551e51060f0SSean Hefty 		rdma_destroy_id(&id_priv->id);
1552e51060f0SSean Hefty 		return;
1553e51060f0SSean Hefty 	}
1554e51060f0SSean Hefty out:
1555e51060f0SSean Hefty 	cma_release_remove(id_priv);
1556e51060f0SSean Hefty 	cma_deref_id(id_priv);
1557e51060f0SSean Hefty }
1558e51060f0SSean Hefty 
1559e51060f0SSean Hefty static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1560e51060f0SSean Hefty {
1561e51060f0SSean Hefty 	struct cma_work *work;
1562e51060f0SSean Hefty 	struct sockaddr_in *src_in, *dst_in;
1563f0ee3404SMichael S. Tsirkin 	union ib_gid gid;
1564e51060f0SSean Hefty 	int ret;
1565e51060f0SSean Hefty 
1566e51060f0SSean Hefty 	work = kzalloc(sizeof *work, GFP_KERNEL);
1567e51060f0SSean Hefty 	if (!work)
1568e51060f0SSean Hefty 		return -ENOMEM;
1569e51060f0SSean Hefty 
1570e51060f0SSean Hefty 	if (!id_priv->cma_dev) {
1571e51060f0SSean Hefty 		ret = cma_bind_loopback(id_priv);
1572e51060f0SSean Hefty 		if (ret)
1573e51060f0SSean Hefty 			goto err;
1574e51060f0SSean Hefty 	}
1575e51060f0SSean Hefty 
1576f0ee3404SMichael S. Tsirkin 	ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1577f0ee3404SMichael S. Tsirkin 	ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1578e51060f0SSean Hefty 
1579e51060f0SSean Hefty 	if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1580e51060f0SSean Hefty 		src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1581e51060f0SSean Hefty 		dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1582e51060f0SSean Hefty 		src_in->sin_family = dst_in->sin_family;
1583e51060f0SSean Hefty 		src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1584e51060f0SSean Hefty 	}
1585e51060f0SSean Hefty 
1586e51060f0SSean Hefty 	work->id = id_priv;
1587e51060f0SSean Hefty 	INIT_WORK(&work->work, cma_work_handler, work);
1588e51060f0SSean Hefty 	work->old_state = CMA_ADDR_QUERY;
1589e51060f0SSean Hefty 	work->new_state = CMA_ADDR_RESOLVED;
1590e51060f0SSean Hefty 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1591e51060f0SSean Hefty 	queue_work(cma_wq, &work->work);
1592e51060f0SSean Hefty 	return 0;
1593e51060f0SSean Hefty err:
1594e51060f0SSean Hefty 	kfree(work);
1595e51060f0SSean Hefty 	return ret;
1596e51060f0SSean Hefty }
1597e51060f0SSean Hefty 
1598e51060f0SSean Hefty static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1599e51060f0SSean Hefty 			 struct sockaddr *dst_addr)
1600e51060f0SSean Hefty {
1601e51060f0SSean Hefty 	if (src_addr && src_addr->sa_family)
1602e51060f0SSean Hefty 		return rdma_bind_addr(id, src_addr);
1603e51060f0SSean Hefty 	else
1604e51060f0SSean Hefty 		return cma_bind_any(id, dst_addr->sa_family);
1605e51060f0SSean Hefty }
1606e51060f0SSean Hefty 
1607e51060f0SSean Hefty int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1608e51060f0SSean Hefty 		      struct sockaddr *dst_addr, int timeout_ms)
1609e51060f0SSean Hefty {
1610e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1611e51060f0SSean Hefty 	int ret;
1612e51060f0SSean Hefty 
1613e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1614e51060f0SSean Hefty 	if (id_priv->state == CMA_IDLE) {
1615e51060f0SSean Hefty 		ret = cma_bind_addr(id, src_addr, dst_addr);
1616e51060f0SSean Hefty 		if (ret)
1617e51060f0SSean Hefty 			return ret;
1618e51060f0SSean Hefty 	}
1619e51060f0SSean Hefty 
1620e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1621e51060f0SSean Hefty 		return -EINVAL;
1622e51060f0SSean Hefty 
1623e51060f0SSean Hefty 	atomic_inc(&id_priv->refcount);
1624e51060f0SSean Hefty 	memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1625e51060f0SSean Hefty 	if (cma_any_addr(dst_addr))
1626e51060f0SSean Hefty 		ret = cma_resolve_loopback(id_priv);
1627e51060f0SSean Hefty 	else
1628e51060f0SSean Hefty 		ret = rdma_resolve_ip(&id->route.addr.src_addr, dst_addr,
1629e51060f0SSean Hefty 				      &id->route.addr.dev_addr,
1630e51060f0SSean Hefty 				      timeout_ms, addr_handler, id_priv);
1631e51060f0SSean Hefty 	if (ret)
1632e51060f0SSean Hefty 		goto err;
1633e51060f0SSean Hefty 
1634e51060f0SSean Hefty 	return 0;
1635e51060f0SSean Hefty err:
1636e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1637e51060f0SSean Hefty 	cma_deref_id(id_priv);
1638e51060f0SSean Hefty 	return ret;
1639e51060f0SSean Hefty }
1640e51060f0SSean Hefty EXPORT_SYMBOL(rdma_resolve_addr);
1641e51060f0SSean Hefty 
1642e51060f0SSean Hefty static void cma_bind_port(struct rdma_bind_list *bind_list,
1643e51060f0SSean Hefty 			  struct rdma_id_private *id_priv)
1644e51060f0SSean Hefty {
1645e51060f0SSean Hefty 	struct sockaddr_in *sin;
1646e51060f0SSean Hefty 
1647e51060f0SSean Hefty 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1648e51060f0SSean Hefty 	sin->sin_port = htons(bind_list->port);
1649e51060f0SSean Hefty 	id_priv->bind_list = bind_list;
1650e51060f0SSean Hefty 	hlist_add_head(&id_priv->node, &bind_list->owners);
1651e51060f0SSean Hefty }
1652e51060f0SSean Hefty 
1653e51060f0SSean Hefty static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1654e51060f0SSean Hefty 			  unsigned short snum)
1655e51060f0SSean Hefty {
1656e51060f0SSean Hefty 	struct rdma_bind_list *bind_list;
1657e51060f0SSean Hefty 	int port, start, ret;
1658e51060f0SSean Hefty 
1659e51060f0SSean Hefty 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1660e51060f0SSean Hefty 	if (!bind_list)
1661e51060f0SSean Hefty 		return -ENOMEM;
1662e51060f0SSean Hefty 
1663e51060f0SSean Hefty 	start = snum ? snum : sysctl_local_port_range[0];
1664e51060f0SSean Hefty 
1665e51060f0SSean Hefty 	do {
1666e51060f0SSean Hefty 		ret = idr_get_new_above(ps, bind_list, start, &port);
1667e51060f0SSean Hefty 	} while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1668e51060f0SSean Hefty 
1669e51060f0SSean Hefty 	if (ret)
1670e51060f0SSean Hefty 		goto err;
1671e51060f0SSean Hefty 
1672e51060f0SSean Hefty 	if ((snum && port != snum) ||
1673e51060f0SSean Hefty 	    (!snum && port > sysctl_local_port_range[1])) {
1674e51060f0SSean Hefty 		idr_remove(ps, port);
1675e51060f0SSean Hefty 		ret = -EADDRNOTAVAIL;
1676e51060f0SSean Hefty 		goto err;
1677e51060f0SSean Hefty 	}
1678e51060f0SSean Hefty 
1679e51060f0SSean Hefty 	bind_list->ps = ps;
1680e51060f0SSean Hefty 	bind_list->port = (unsigned short) port;
1681e51060f0SSean Hefty 	cma_bind_port(bind_list, id_priv);
1682e51060f0SSean Hefty 	return 0;
1683e51060f0SSean Hefty err:
1684e51060f0SSean Hefty 	kfree(bind_list);
1685e51060f0SSean Hefty 	return ret;
1686e51060f0SSean Hefty }
1687e51060f0SSean Hefty 
1688e51060f0SSean Hefty static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1689e51060f0SSean Hefty {
1690e51060f0SSean Hefty 	struct rdma_id_private *cur_id;
1691e51060f0SSean Hefty 	struct sockaddr_in *sin, *cur_sin;
1692e51060f0SSean Hefty 	struct rdma_bind_list *bind_list;
1693e51060f0SSean Hefty 	struct hlist_node *node;
1694e51060f0SSean Hefty 	unsigned short snum;
1695e51060f0SSean Hefty 
1696e51060f0SSean Hefty 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1697e51060f0SSean Hefty 	snum = ntohs(sin->sin_port);
1698e51060f0SSean Hefty 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1699e51060f0SSean Hefty 		return -EACCES;
1700e51060f0SSean Hefty 
1701e51060f0SSean Hefty 	bind_list = idr_find(ps, snum);
1702e51060f0SSean Hefty 	if (!bind_list)
1703e51060f0SSean Hefty 		return cma_alloc_port(ps, id_priv, snum);
1704e51060f0SSean Hefty 
1705e51060f0SSean Hefty 	/*
1706e51060f0SSean Hefty 	 * We don't support binding to any address if anyone is bound to
1707e51060f0SSean Hefty 	 * a specific address on the same port.
1708e51060f0SSean Hefty 	 */
1709e51060f0SSean Hefty 	if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1710e51060f0SSean Hefty 		return -EADDRNOTAVAIL;
1711e51060f0SSean Hefty 
1712e51060f0SSean Hefty 	hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1713e51060f0SSean Hefty 		if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1714e51060f0SSean Hefty 			return -EADDRNOTAVAIL;
1715e51060f0SSean Hefty 
1716e51060f0SSean Hefty 		cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1717e51060f0SSean Hefty 		if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1718e51060f0SSean Hefty 			return -EADDRINUSE;
1719e51060f0SSean Hefty 	}
1720e51060f0SSean Hefty 
1721e51060f0SSean Hefty 	cma_bind_port(bind_list, id_priv);
1722e51060f0SSean Hefty 	return 0;
1723e51060f0SSean Hefty }
1724e51060f0SSean Hefty 
1725e51060f0SSean Hefty static int cma_get_port(struct rdma_id_private *id_priv)
1726e51060f0SSean Hefty {
1727e51060f0SSean Hefty 	struct idr *ps;
1728e51060f0SSean Hefty 	int ret;
1729e51060f0SSean Hefty 
1730e51060f0SSean Hefty 	switch (id_priv->id.ps) {
1731e51060f0SSean Hefty 	case RDMA_PS_SDP:
1732e51060f0SSean Hefty 		ps = &sdp_ps;
1733e51060f0SSean Hefty 		break;
1734e51060f0SSean Hefty 	case RDMA_PS_TCP:
1735e51060f0SSean Hefty 		ps = &tcp_ps;
1736e51060f0SSean Hefty 		break;
1737e51060f0SSean Hefty 	default:
1738e51060f0SSean Hefty 		return -EPROTONOSUPPORT;
1739e51060f0SSean Hefty 	}
1740e51060f0SSean Hefty 
1741e51060f0SSean Hefty 	mutex_lock(&lock);
1742e51060f0SSean Hefty 	if (cma_any_port(&id_priv->id.route.addr.src_addr))
1743e51060f0SSean Hefty 		ret = cma_alloc_port(ps, id_priv, 0);
1744e51060f0SSean Hefty 	else
1745e51060f0SSean Hefty 		ret = cma_use_port(ps, id_priv);
1746e51060f0SSean Hefty 	mutex_unlock(&lock);
1747e51060f0SSean Hefty 
1748e51060f0SSean Hefty 	return ret;
1749e51060f0SSean Hefty }
1750e51060f0SSean Hefty 
1751e51060f0SSean Hefty int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1752e51060f0SSean Hefty {
1753e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1754e51060f0SSean Hefty 	int ret;
1755e51060f0SSean Hefty 
1756e51060f0SSean Hefty 	if (addr->sa_family != AF_INET)
1757e51060f0SSean Hefty 		return -EAFNOSUPPORT;
1758e51060f0SSean Hefty 
1759e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1760e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1761e51060f0SSean Hefty 		return -EINVAL;
1762e51060f0SSean Hefty 
1763e51060f0SSean Hefty 	if (!cma_any_addr(addr)) {
1764e51060f0SSean Hefty 		ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
176561a73c70SSean Hefty 		if (!ret) {
176661a73c70SSean Hefty 			mutex_lock(&lock);
1767e51060f0SSean Hefty 			ret = cma_acquire_dev(id_priv);
176861a73c70SSean Hefty 			mutex_unlock(&lock);
176961a73c70SSean Hefty 		}
1770e51060f0SSean Hefty 		if (ret)
1771e51060f0SSean Hefty 			goto err;
1772e51060f0SSean Hefty 	}
1773e51060f0SSean Hefty 
1774e51060f0SSean Hefty 	memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1775e51060f0SSean Hefty 	ret = cma_get_port(id_priv);
1776e51060f0SSean Hefty 	if (ret)
1777e51060f0SSean Hefty 		goto err;
1778e51060f0SSean Hefty 
1779e51060f0SSean Hefty 	return 0;
1780e51060f0SSean Hefty err:
1781e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
1782e51060f0SSean Hefty 	return ret;
1783e51060f0SSean Hefty }
1784e51060f0SSean Hefty EXPORT_SYMBOL(rdma_bind_addr);
1785e51060f0SSean Hefty 
1786e51060f0SSean Hefty static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
1787e51060f0SSean Hefty 			  struct rdma_route *route)
1788e51060f0SSean Hefty {
1789e51060f0SSean Hefty 	struct sockaddr_in *src4, *dst4;
1790e51060f0SSean Hefty 	struct cma_hdr *cma_hdr;
1791e51060f0SSean Hefty 	struct sdp_hh *sdp_hdr;
1792e51060f0SSean Hefty 
1793e51060f0SSean Hefty 	src4 = (struct sockaddr_in *) &route->addr.src_addr;
1794e51060f0SSean Hefty 	dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
1795e51060f0SSean Hefty 
1796e51060f0SSean Hefty 	switch (ps) {
1797e51060f0SSean Hefty 	case RDMA_PS_SDP:
1798e51060f0SSean Hefty 		sdp_hdr = hdr;
1799e51060f0SSean Hefty 		if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
1800e51060f0SSean Hefty 			return -EINVAL;
1801e51060f0SSean Hefty 		sdp_set_ip_ver(sdp_hdr, 4);
1802e51060f0SSean Hefty 		sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1803e51060f0SSean Hefty 		sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1804e51060f0SSean Hefty 		sdp_hdr->port = src4->sin_port;
1805e51060f0SSean Hefty 		break;
1806e51060f0SSean Hefty 	default:
1807e51060f0SSean Hefty 		cma_hdr = hdr;
1808e51060f0SSean Hefty 		cma_hdr->cma_version = CMA_VERSION;
1809e51060f0SSean Hefty 		cma_set_ip_ver(cma_hdr, 4);
1810e51060f0SSean Hefty 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1811e51060f0SSean Hefty 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1812e51060f0SSean Hefty 		cma_hdr->port = src4->sin_port;
1813e51060f0SSean Hefty 		break;
1814e51060f0SSean Hefty 	}
1815e51060f0SSean Hefty 	return 0;
1816e51060f0SSean Hefty }
1817e51060f0SSean Hefty 
1818e51060f0SSean Hefty static int cma_connect_ib(struct rdma_id_private *id_priv,
1819e51060f0SSean Hefty 			  struct rdma_conn_param *conn_param)
1820e51060f0SSean Hefty {
1821e51060f0SSean Hefty 	struct ib_cm_req_param req;
1822e51060f0SSean Hefty 	struct rdma_route *route;
1823e51060f0SSean Hefty 	void *private_data;
1824e51060f0SSean Hefty 	int offset, ret;
1825e51060f0SSean Hefty 
1826e51060f0SSean Hefty 	memset(&req, 0, sizeof req);
1827e51060f0SSean Hefty 	offset = cma_user_data_offset(id_priv->id.ps);
1828e51060f0SSean Hefty 	req.private_data_len = offset + conn_param->private_data_len;
1829e51060f0SSean Hefty 	private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
1830e51060f0SSean Hefty 	if (!private_data)
1831e51060f0SSean Hefty 		return -ENOMEM;
1832e51060f0SSean Hefty 
1833e51060f0SSean Hefty 	if (conn_param->private_data && conn_param->private_data_len)
1834e51060f0SSean Hefty 		memcpy(private_data + offset, conn_param->private_data,
1835e51060f0SSean Hefty 		       conn_param->private_data_len);
1836e51060f0SSean Hefty 
1837e51060f0SSean Hefty 	id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
1838e51060f0SSean Hefty 					    id_priv);
1839e51060f0SSean Hefty 	if (IS_ERR(id_priv->cm_id.ib)) {
1840e51060f0SSean Hefty 		ret = PTR_ERR(id_priv->cm_id.ib);
1841e51060f0SSean Hefty 		goto out;
1842e51060f0SSean Hefty 	}
1843e51060f0SSean Hefty 
1844e51060f0SSean Hefty 	route = &id_priv->id.route;
1845e51060f0SSean Hefty 	ret = cma_format_hdr(private_data, id_priv->id.ps, route);
1846e51060f0SSean Hefty 	if (ret)
1847e51060f0SSean Hefty 		goto out;
1848e51060f0SSean Hefty 	req.private_data = private_data;
1849e51060f0SSean Hefty 
1850e51060f0SSean Hefty 	req.primary_path = &route->path_rec[0];
1851e51060f0SSean Hefty 	if (route->num_paths == 2)
1852e51060f0SSean Hefty 		req.alternate_path = &route->path_rec[1];
1853e51060f0SSean Hefty 
1854e51060f0SSean Hefty 	req.service_id = cma_get_service_id(id_priv->id.ps,
1855e51060f0SSean Hefty 					    &route->addr.dst_addr);
1856e51060f0SSean Hefty 	req.qp_num = id_priv->qp_num;
1857e51060f0SSean Hefty 	req.qp_type = id_priv->qp_type;
1858e51060f0SSean Hefty 	req.starting_psn = id_priv->seq_num;
1859e51060f0SSean Hefty 	req.responder_resources = conn_param->responder_resources;
1860e51060f0SSean Hefty 	req.initiator_depth = conn_param->initiator_depth;
1861e51060f0SSean Hefty 	req.flow_control = conn_param->flow_control;
1862e51060f0SSean Hefty 	req.retry_count = conn_param->retry_count;
1863e51060f0SSean Hefty 	req.rnr_retry_count = conn_param->rnr_retry_count;
1864e51060f0SSean Hefty 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1865e51060f0SSean Hefty 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1866e51060f0SSean Hefty 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
1867e51060f0SSean Hefty 	req.srq = id_priv->srq ? 1 : 0;
1868e51060f0SSean Hefty 
1869e51060f0SSean Hefty 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
1870e51060f0SSean Hefty out:
1871675a027cSKrishna Kumar 	if (ret && !IS_ERR(id_priv->cm_id.ib)) {
1872675a027cSKrishna Kumar 		ib_destroy_cm_id(id_priv->cm_id.ib);
1873675a027cSKrishna Kumar 		id_priv->cm_id.ib = NULL;
1874675a027cSKrishna Kumar 	}
1875675a027cSKrishna Kumar 
1876e51060f0SSean Hefty 	kfree(private_data);
1877e51060f0SSean Hefty 	return ret;
1878e51060f0SSean Hefty }
1879e51060f0SSean Hefty 
188007ebafbaSTom Tucker static int cma_connect_iw(struct rdma_id_private *id_priv,
188107ebafbaSTom Tucker 			  struct rdma_conn_param *conn_param)
188207ebafbaSTom Tucker {
188307ebafbaSTom Tucker 	struct iw_cm_id *cm_id;
188407ebafbaSTom Tucker 	struct sockaddr_in* sin;
188507ebafbaSTom Tucker 	int ret;
188607ebafbaSTom Tucker 	struct iw_cm_conn_param iw_param;
188707ebafbaSTom Tucker 
188807ebafbaSTom Tucker 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
188907ebafbaSTom Tucker 	if (IS_ERR(cm_id)) {
189007ebafbaSTom Tucker 		ret = PTR_ERR(cm_id);
189107ebafbaSTom Tucker 		goto out;
189207ebafbaSTom Tucker 	}
189307ebafbaSTom Tucker 
189407ebafbaSTom Tucker 	id_priv->cm_id.iw = cm_id;
189507ebafbaSTom Tucker 
189607ebafbaSTom Tucker 	sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
189707ebafbaSTom Tucker 	cm_id->local_addr = *sin;
189807ebafbaSTom Tucker 
189907ebafbaSTom Tucker 	sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
190007ebafbaSTom Tucker 	cm_id->remote_addr = *sin;
190107ebafbaSTom Tucker 
190207ebafbaSTom Tucker 	ret = cma_modify_qp_rtr(&id_priv->id);
1903675a027cSKrishna Kumar 	if (ret)
1904675a027cSKrishna Kumar 		goto out;
190507ebafbaSTom Tucker 
190607ebafbaSTom Tucker 	iw_param.ord = conn_param->initiator_depth;
190707ebafbaSTom Tucker 	iw_param.ird = conn_param->responder_resources;
190807ebafbaSTom Tucker 	iw_param.private_data = conn_param->private_data;
190907ebafbaSTom Tucker 	iw_param.private_data_len = conn_param->private_data_len;
191007ebafbaSTom Tucker 	if (id_priv->id.qp)
191107ebafbaSTom Tucker 		iw_param.qpn = id_priv->qp_num;
191207ebafbaSTom Tucker 	else
191307ebafbaSTom Tucker 		iw_param.qpn = conn_param->qp_num;
191407ebafbaSTom Tucker 	ret = iw_cm_connect(cm_id, &iw_param);
191507ebafbaSTom Tucker out:
1916675a027cSKrishna Kumar 	if (ret && !IS_ERR(cm_id)) {
1917675a027cSKrishna Kumar 		iw_destroy_cm_id(cm_id);
1918675a027cSKrishna Kumar 		id_priv->cm_id.iw = NULL;
1919675a027cSKrishna Kumar 	}
192007ebafbaSTom Tucker 	return ret;
192107ebafbaSTom Tucker }
192207ebafbaSTom Tucker 
1923e51060f0SSean Hefty int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
1924e51060f0SSean Hefty {
1925e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1926e51060f0SSean Hefty 	int ret;
1927e51060f0SSean Hefty 
1928e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
1929e51060f0SSean Hefty 	if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
1930e51060f0SSean Hefty 		return -EINVAL;
1931e51060f0SSean Hefty 
1932e51060f0SSean Hefty 	if (!id->qp) {
1933e51060f0SSean Hefty 		id_priv->qp_num = conn_param->qp_num;
1934e51060f0SSean Hefty 		id_priv->qp_type = conn_param->qp_type;
1935e51060f0SSean Hefty 		id_priv->srq = conn_param->srq;
1936e51060f0SSean Hefty 	}
1937e51060f0SSean Hefty 
193807ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
193907ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
1940e51060f0SSean Hefty 		ret = cma_connect_ib(id_priv, conn_param);
1941e51060f0SSean Hefty 		break;
194207ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
194307ebafbaSTom Tucker 		ret = cma_connect_iw(id_priv, conn_param);
194407ebafbaSTom Tucker 		break;
1945e51060f0SSean Hefty 	default:
1946e51060f0SSean Hefty 		ret = -ENOSYS;
1947e51060f0SSean Hefty 		break;
1948e51060f0SSean Hefty 	}
1949e51060f0SSean Hefty 	if (ret)
1950e51060f0SSean Hefty 		goto err;
1951e51060f0SSean Hefty 
1952e51060f0SSean Hefty 	return 0;
1953e51060f0SSean Hefty err:
1954e51060f0SSean Hefty 	cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
1955e51060f0SSean Hefty 	return ret;
1956e51060f0SSean Hefty }
1957e51060f0SSean Hefty EXPORT_SYMBOL(rdma_connect);
1958e51060f0SSean Hefty 
1959e51060f0SSean Hefty static int cma_accept_ib(struct rdma_id_private *id_priv,
1960e51060f0SSean Hefty 			 struct rdma_conn_param *conn_param)
1961e51060f0SSean Hefty {
1962e51060f0SSean Hefty 	struct ib_cm_rep_param rep;
1963e51060f0SSean Hefty 	int ret;
1964e51060f0SSean Hefty 
1965e51060f0SSean Hefty 	ret = cma_modify_qp_rtr(&id_priv->id);
1966e51060f0SSean Hefty 	if (ret)
1967e51060f0SSean Hefty 		return ret;
1968e51060f0SSean Hefty 
1969e51060f0SSean Hefty 	memset(&rep, 0, sizeof rep);
1970e51060f0SSean Hefty 	rep.qp_num = id_priv->qp_num;
1971e51060f0SSean Hefty 	rep.starting_psn = id_priv->seq_num;
1972e51060f0SSean Hefty 	rep.private_data = conn_param->private_data;
1973e51060f0SSean Hefty 	rep.private_data_len = conn_param->private_data_len;
1974e51060f0SSean Hefty 	rep.responder_resources = conn_param->responder_resources;
1975e51060f0SSean Hefty 	rep.initiator_depth = conn_param->initiator_depth;
1976e51060f0SSean Hefty 	rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
1977e51060f0SSean Hefty 	rep.failover_accepted = 0;
1978e51060f0SSean Hefty 	rep.flow_control = conn_param->flow_control;
1979e51060f0SSean Hefty 	rep.rnr_retry_count = conn_param->rnr_retry_count;
1980e51060f0SSean Hefty 	rep.srq = id_priv->srq ? 1 : 0;
1981e51060f0SSean Hefty 
1982e51060f0SSean Hefty 	return ib_send_cm_rep(id_priv->cm_id.ib, &rep);
1983e51060f0SSean Hefty }
1984e51060f0SSean Hefty 
198507ebafbaSTom Tucker static int cma_accept_iw(struct rdma_id_private *id_priv,
198607ebafbaSTom Tucker 		  struct rdma_conn_param *conn_param)
198707ebafbaSTom Tucker {
198807ebafbaSTom Tucker 	struct iw_cm_conn_param iw_param;
198907ebafbaSTom Tucker 	int ret;
199007ebafbaSTom Tucker 
199107ebafbaSTom Tucker 	ret = cma_modify_qp_rtr(&id_priv->id);
199207ebafbaSTom Tucker 	if (ret)
199307ebafbaSTom Tucker 		return ret;
199407ebafbaSTom Tucker 
199507ebafbaSTom Tucker 	iw_param.ord = conn_param->initiator_depth;
199607ebafbaSTom Tucker 	iw_param.ird = conn_param->responder_resources;
199707ebafbaSTom Tucker 	iw_param.private_data = conn_param->private_data;
199807ebafbaSTom Tucker 	iw_param.private_data_len = conn_param->private_data_len;
199907ebafbaSTom Tucker 	if (id_priv->id.qp) {
200007ebafbaSTom Tucker 		iw_param.qpn = id_priv->qp_num;
200107ebafbaSTom Tucker 	} else
200207ebafbaSTom Tucker 		iw_param.qpn = conn_param->qp_num;
200307ebafbaSTom Tucker 
200407ebafbaSTom Tucker 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
200507ebafbaSTom Tucker }
200607ebafbaSTom Tucker 
2007e51060f0SSean Hefty int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2008e51060f0SSean Hefty {
2009e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2010e51060f0SSean Hefty 	int ret;
2011e51060f0SSean Hefty 
2012e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
2013e51060f0SSean Hefty 	if (!cma_comp(id_priv, CMA_CONNECT))
2014e51060f0SSean Hefty 		return -EINVAL;
2015e51060f0SSean Hefty 
2016e51060f0SSean Hefty 	if (!id->qp && conn_param) {
2017e51060f0SSean Hefty 		id_priv->qp_num = conn_param->qp_num;
2018e51060f0SSean Hefty 		id_priv->qp_type = conn_param->qp_type;
2019e51060f0SSean Hefty 		id_priv->srq = conn_param->srq;
2020e51060f0SSean Hefty 	}
2021e51060f0SSean Hefty 
202207ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
202307ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
2024e51060f0SSean Hefty 		if (conn_param)
2025e51060f0SSean Hefty 			ret = cma_accept_ib(id_priv, conn_param);
2026e51060f0SSean Hefty 		else
2027e51060f0SSean Hefty 			ret = cma_rep_recv(id_priv);
2028e51060f0SSean Hefty 		break;
202907ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
203007ebafbaSTom Tucker 		ret = cma_accept_iw(id_priv, conn_param);
203107ebafbaSTom Tucker 		break;
2032e51060f0SSean Hefty 	default:
2033e51060f0SSean Hefty 		ret = -ENOSYS;
2034e51060f0SSean Hefty 		break;
2035e51060f0SSean Hefty 	}
2036e51060f0SSean Hefty 
2037e51060f0SSean Hefty 	if (ret)
2038e51060f0SSean Hefty 		goto reject;
2039e51060f0SSean Hefty 
2040e51060f0SSean Hefty 	return 0;
2041e51060f0SSean Hefty reject:
2042e51060f0SSean Hefty 	cma_modify_qp_err(id);
2043e51060f0SSean Hefty 	rdma_reject(id, NULL, 0);
2044e51060f0SSean Hefty 	return ret;
2045e51060f0SSean Hefty }
2046e51060f0SSean Hefty EXPORT_SYMBOL(rdma_accept);
2047e51060f0SSean Hefty 
2048e51060f0SSean Hefty int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2049e51060f0SSean Hefty 		u8 private_data_len)
2050e51060f0SSean Hefty {
2051e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2052e51060f0SSean Hefty 	int ret;
2053e51060f0SSean Hefty 
2054e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
2055e51060f0SSean Hefty 	if (!cma_comp(id_priv, CMA_CONNECT))
2056e51060f0SSean Hefty 		return -EINVAL;
2057e51060f0SSean Hefty 
205807ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
205907ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
2060e51060f0SSean Hefty 		ret = ib_send_cm_rej(id_priv->cm_id.ib,
2061e51060f0SSean Hefty 				     IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2062e51060f0SSean Hefty 				     private_data, private_data_len);
2063e51060f0SSean Hefty 		break;
206407ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
206507ebafbaSTom Tucker 		ret = iw_cm_reject(id_priv->cm_id.iw,
206607ebafbaSTom Tucker 				   private_data, private_data_len);
206707ebafbaSTom Tucker 		break;
2068e51060f0SSean Hefty 	default:
2069e51060f0SSean Hefty 		ret = -ENOSYS;
2070e51060f0SSean Hefty 		break;
2071e51060f0SSean Hefty 	}
2072e51060f0SSean Hefty 	return ret;
2073e51060f0SSean Hefty }
2074e51060f0SSean Hefty EXPORT_SYMBOL(rdma_reject);
2075e51060f0SSean Hefty 
2076e51060f0SSean Hefty int rdma_disconnect(struct rdma_cm_id *id)
2077e51060f0SSean Hefty {
2078e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2079e51060f0SSean Hefty 	int ret;
2080e51060f0SSean Hefty 
2081e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
2082e51060f0SSean Hefty 	if (!cma_comp(id_priv, CMA_CONNECT) &&
2083e51060f0SSean Hefty 	    !cma_comp(id_priv, CMA_DISCONNECT))
2084e51060f0SSean Hefty 		return -EINVAL;
2085e51060f0SSean Hefty 
208607ebafbaSTom Tucker 	switch (rdma_node_get_transport(id->device->node_type)) {
208707ebafbaSTom Tucker 	case RDMA_TRANSPORT_IB:
2088e51060f0SSean Hefty 		ret = cma_modify_qp_err(id);
2089e51060f0SSean Hefty 		if (ret)
2090e51060f0SSean Hefty 			goto out;
2091e51060f0SSean Hefty 		/* Initiate or respond to a disconnect. */
2092e51060f0SSean Hefty 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2093e51060f0SSean Hefty 			ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2094e51060f0SSean Hefty 		break;
209507ebafbaSTom Tucker 	case RDMA_TRANSPORT_IWARP:
209607ebafbaSTom Tucker 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
209707ebafbaSTom Tucker 		break;
2098e51060f0SSean Hefty 	default:
209907ebafbaSTom Tucker 		ret = -EINVAL;
2100e51060f0SSean Hefty 		break;
2101e51060f0SSean Hefty 	}
2102e51060f0SSean Hefty out:
2103e51060f0SSean Hefty 	return ret;
2104e51060f0SSean Hefty }
2105e51060f0SSean Hefty EXPORT_SYMBOL(rdma_disconnect);
2106e51060f0SSean Hefty 
2107e51060f0SSean Hefty static void cma_add_one(struct ib_device *device)
2108e51060f0SSean Hefty {
2109e51060f0SSean Hefty 	struct cma_device *cma_dev;
2110e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2111e51060f0SSean Hefty 
2112e51060f0SSean Hefty 	cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2113e51060f0SSean Hefty 	if (!cma_dev)
2114e51060f0SSean Hefty 		return;
2115e51060f0SSean Hefty 
2116e51060f0SSean Hefty 	cma_dev->device = device;
2117e51060f0SSean Hefty 	cma_dev->node_guid = device->node_guid;
2118e51060f0SSean Hefty 	if (!cma_dev->node_guid)
2119e51060f0SSean Hefty 		goto err;
2120e51060f0SSean Hefty 
2121e51060f0SSean Hefty 	init_completion(&cma_dev->comp);
2122e51060f0SSean Hefty 	atomic_set(&cma_dev->refcount, 1);
2123e51060f0SSean Hefty 	INIT_LIST_HEAD(&cma_dev->id_list);
2124e51060f0SSean Hefty 	ib_set_client_data(device, &cma_client, cma_dev);
2125e51060f0SSean Hefty 
2126e51060f0SSean Hefty 	mutex_lock(&lock);
2127e51060f0SSean Hefty 	list_add_tail(&cma_dev->list, &dev_list);
2128e51060f0SSean Hefty 	list_for_each_entry(id_priv, &listen_any_list, list)
2129e51060f0SSean Hefty 		cma_listen_on_dev(id_priv, cma_dev);
2130e51060f0SSean Hefty 	mutex_unlock(&lock);
2131e51060f0SSean Hefty 	return;
2132e51060f0SSean Hefty err:
2133e51060f0SSean Hefty 	kfree(cma_dev);
2134e51060f0SSean Hefty }
2135e51060f0SSean Hefty 
2136e51060f0SSean Hefty static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2137e51060f0SSean Hefty {
2138e51060f0SSean Hefty 	enum cma_state state;
2139e51060f0SSean Hefty 
2140e51060f0SSean Hefty 	/* Record that we want to remove the device */
2141e51060f0SSean Hefty 	state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2142e51060f0SSean Hefty 	if (state == CMA_DESTROYING)
2143e51060f0SSean Hefty 		return 0;
2144e51060f0SSean Hefty 
2145e51060f0SSean Hefty 	cma_cancel_operation(id_priv, state);
2146e51060f0SSean Hefty 	wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2147e51060f0SSean Hefty 
2148e51060f0SSean Hefty 	/* Check for destruction from another callback. */
2149e51060f0SSean Hefty 	if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2150e51060f0SSean Hefty 		return 0;
2151e51060f0SSean Hefty 
2152e51060f0SSean Hefty 	return cma_notify_user(id_priv, RDMA_CM_EVENT_DEVICE_REMOVAL,
2153e51060f0SSean Hefty 			       0, NULL, 0);
2154e51060f0SSean Hefty }
2155e51060f0SSean Hefty 
2156e51060f0SSean Hefty static void cma_process_remove(struct cma_device *cma_dev)
2157e51060f0SSean Hefty {
2158e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2159e51060f0SSean Hefty 	int ret;
2160e51060f0SSean Hefty 
2161e51060f0SSean Hefty 	mutex_lock(&lock);
2162e51060f0SSean Hefty 	while (!list_empty(&cma_dev->id_list)) {
2163e51060f0SSean Hefty 		id_priv = list_entry(cma_dev->id_list.next,
2164e51060f0SSean Hefty 				     struct rdma_id_private, list);
2165e51060f0SSean Hefty 
2166e51060f0SSean Hefty 		if (cma_internal_listen(id_priv)) {
2167e51060f0SSean Hefty 			cma_destroy_listen(id_priv);
2168e51060f0SSean Hefty 			continue;
2169e51060f0SSean Hefty 		}
2170e51060f0SSean Hefty 
217194de178aSKrishna Kumar 		list_del_init(&id_priv->list);
2172e51060f0SSean Hefty 		atomic_inc(&id_priv->refcount);
2173e51060f0SSean Hefty 		mutex_unlock(&lock);
2174e51060f0SSean Hefty 
2175e51060f0SSean Hefty 		ret = cma_remove_id_dev(id_priv);
2176e51060f0SSean Hefty 		cma_deref_id(id_priv);
2177e51060f0SSean Hefty 		if (ret)
2178e51060f0SSean Hefty 			rdma_destroy_id(&id_priv->id);
2179e51060f0SSean Hefty 
2180e51060f0SSean Hefty 		mutex_lock(&lock);
2181e51060f0SSean Hefty 	}
2182e51060f0SSean Hefty 	mutex_unlock(&lock);
2183e51060f0SSean Hefty 
2184e51060f0SSean Hefty 	cma_deref_dev(cma_dev);
2185e51060f0SSean Hefty 	wait_for_completion(&cma_dev->comp);
2186e51060f0SSean Hefty }
2187e51060f0SSean Hefty 
2188e51060f0SSean Hefty static void cma_remove_one(struct ib_device *device)
2189e51060f0SSean Hefty {
2190e51060f0SSean Hefty 	struct cma_device *cma_dev;
2191e51060f0SSean Hefty 
2192e51060f0SSean Hefty 	cma_dev = ib_get_client_data(device, &cma_client);
2193e51060f0SSean Hefty 	if (!cma_dev)
2194e51060f0SSean Hefty 		return;
2195e51060f0SSean Hefty 
2196e51060f0SSean Hefty 	mutex_lock(&lock);
2197e51060f0SSean Hefty 	list_del(&cma_dev->list);
2198e51060f0SSean Hefty 	mutex_unlock(&lock);
2199e51060f0SSean Hefty 
2200e51060f0SSean Hefty 	cma_process_remove(cma_dev);
2201e51060f0SSean Hefty 	kfree(cma_dev);
2202e51060f0SSean Hefty }
2203e51060f0SSean Hefty 
2204e51060f0SSean Hefty static int cma_init(void)
2205e51060f0SSean Hefty {
2206e51060f0SSean Hefty 	int ret;
2207e51060f0SSean Hefty 
2208e51060f0SSean Hefty 	cma_wq = create_singlethread_workqueue("rdma_cm_wq");
2209e51060f0SSean Hefty 	if (!cma_wq)
2210e51060f0SSean Hefty 		return -ENOMEM;
2211e51060f0SSean Hefty 
2212c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&sa_client);
2213c1a0b23bSMichael S. Tsirkin 
2214e51060f0SSean Hefty 	ret = ib_register_client(&cma_client);
2215e51060f0SSean Hefty 	if (ret)
2216e51060f0SSean Hefty 		goto err;
2217e51060f0SSean Hefty 	return 0;
2218e51060f0SSean Hefty 
2219e51060f0SSean Hefty err:
2220c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&sa_client);
2221e51060f0SSean Hefty 	destroy_workqueue(cma_wq);
2222e51060f0SSean Hefty 	return ret;
2223e51060f0SSean Hefty }
2224e51060f0SSean Hefty 
2225e51060f0SSean Hefty static void cma_cleanup(void)
2226e51060f0SSean Hefty {
2227e51060f0SSean Hefty 	ib_unregister_client(&cma_client);
2228c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&sa_client);
2229e51060f0SSean Hefty 	destroy_workqueue(cma_wq);
2230e51060f0SSean Hefty 	idr_destroy(&sdp_ps);
2231e51060f0SSean Hefty 	idr_destroy(&tcp_ps);
2232e51060f0SSean Hefty }
2233e51060f0SSean Hefty 
2234e51060f0SSean Hefty module_init(cma_init);
2235e51060f0SSean Hefty module_exit(cma_cleanup);
2236