1 /*
2  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4  * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35 
36 #include <linux/completion.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/idr.h>
42 #include <linux/inetdevice.h>
43 #include <linux/slab.h>
44 
45 #include <net/tcp.h>
46 #include <net/ipv6.h>
47 
48 #include <rdma/rdma_cm.h>
49 #include <rdma/rdma_cm_ib.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_cm.h>
52 #include <rdma/ib_sa.h>
53 #include <rdma/iw_cm.h>
54 
55 MODULE_AUTHOR("Sean Hefty");
56 MODULE_DESCRIPTION("Generic RDMA CM Agent");
57 MODULE_LICENSE("Dual BSD/GPL");
58 
59 #define CMA_CM_RESPONSE_TIMEOUT 20
60 #define CMA_MAX_CM_RETRIES 15
61 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
62 #define CMA_IBOE_PACKET_LIFETIME 18
63 
64 static void cma_add_one(struct ib_device *device);
65 static void cma_remove_one(struct ib_device *device);
66 
67 static struct ib_client cma_client = {
68 	.name   = "cma",
69 	.add    = cma_add_one,
70 	.remove = cma_remove_one
71 };
72 
73 static struct ib_sa_client sa_client;
74 static struct rdma_addr_client addr_client;
75 static LIST_HEAD(dev_list);
76 static LIST_HEAD(listen_any_list);
77 static DEFINE_MUTEX(lock);
78 static struct workqueue_struct *cma_wq;
79 static DEFINE_IDR(sdp_ps);
80 static DEFINE_IDR(tcp_ps);
81 static DEFINE_IDR(udp_ps);
82 static DEFINE_IDR(ipoib_ps);
83 
84 struct cma_device {
85 	struct list_head	list;
86 	struct ib_device	*device;
87 	struct completion	comp;
88 	atomic_t		refcount;
89 	struct list_head	id_list;
90 };
91 
92 enum cma_state {
93 	CMA_IDLE,
94 	CMA_ADDR_QUERY,
95 	CMA_ADDR_RESOLVED,
96 	CMA_ROUTE_QUERY,
97 	CMA_ROUTE_RESOLVED,
98 	CMA_CONNECT,
99 	CMA_DISCONNECT,
100 	CMA_ADDR_BOUND,
101 	CMA_LISTEN,
102 	CMA_DEVICE_REMOVAL,
103 	CMA_DESTROYING
104 };
105 
106 struct rdma_bind_list {
107 	struct idr		*ps;
108 	struct hlist_head	owners;
109 	unsigned short		port;
110 };
111 
112 /*
113  * Device removal can occur at anytime, so we need extra handling to
114  * serialize notifying the user of device removal with other callbacks.
115  * We do this by disabling removal notification while a callback is in process,
116  * and reporting it after the callback completes.
117  */
118 struct rdma_id_private {
119 	struct rdma_cm_id	id;
120 
121 	struct rdma_bind_list	*bind_list;
122 	struct hlist_node	node;
123 	struct list_head	list; /* listen_any_list or cma_device.list */
124 	struct list_head	listen_list; /* per device listens */
125 	struct cma_device	*cma_dev;
126 	struct list_head	mc_list;
127 
128 	int			internal_id;
129 	enum cma_state		state;
130 	spinlock_t		lock;
131 	struct mutex		qp_mutex;
132 
133 	struct completion	comp;
134 	atomic_t		refcount;
135 	struct mutex		handler_mutex;
136 
137 	int			backlog;
138 	int			timeout_ms;
139 	struct ib_sa_query	*query;
140 	int			query_id;
141 	union {
142 		struct ib_cm_id	*ib;
143 		struct iw_cm_id	*iw;
144 	} cm_id;
145 
146 	u32			seq_num;
147 	u32			qkey;
148 	u32			qp_num;
149 	u8			srq;
150 	u8			tos;
151 };
152 
153 struct cma_multicast {
154 	struct rdma_id_private *id_priv;
155 	union {
156 		struct ib_sa_multicast *ib;
157 	} multicast;
158 	struct list_head	list;
159 	void			*context;
160 	struct sockaddr_storage	addr;
161 	struct kref		mcref;
162 };
163 
164 struct cma_work {
165 	struct work_struct	work;
166 	struct rdma_id_private	*id;
167 	enum cma_state		old_state;
168 	enum cma_state		new_state;
169 	struct rdma_cm_event	event;
170 };
171 
172 struct cma_ndev_work {
173 	struct work_struct	work;
174 	struct rdma_id_private	*id;
175 	struct rdma_cm_event	event;
176 };
177 
178 struct iboe_mcast_work {
179 	struct work_struct	 work;
180 	struct rdma_id_private	*id;
181 	struct cma_multicast	*mc;
182 };
183 
184 union cma_ip_addr {
185 	struct in6_addr ip6;
186 	struct {
187 		__be32 pad[3];
188 		__be32 addr;
189 	} ip4;
190 };
191 
192 struct cma_hdr {
193 	u8 cma_version;
194 	u8 ip_version;	/* IP version: 7:4 */
195 	__be16 port;
196 	union cma_ip_addr src_addr;
197 	union cma_ip_addr dst_addr;
198 };
199 
200 struct sdp_hh {
201 	u8 bsdh[16];
202 	u8 sdp_version; /* Major version: 7:4 */
203 	u8 ip_version;	/* IP version: 7:4 */
204 	u8 sdp_specific1[10];
205 	__be16 port;
206 	__be16 sdp_specific2;
207 	union cma_ip_addr src_addr;
208 	union cma_ip_addr dst_addr;
209 };
210 
211 struct sdp_hah {
212 	u8 bsdh[16];
213 	u8 sdp_version;
214 };
215 
216 #define CMA_VERSION 0x00
217 #define SDP_MAJ_VERSION 0x2
218 
cma_comp(struct rdma_id_private * id_priv,enum cma_state comp)219 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
220 {
221 	unsigned long flags;
222 	int ret;
223 
224 	spin_lock_irqsave(&id_priv->lock, flags);
225 	ret = (id_priv->state == comp);
226 	spin_unlock_irqrestore(&id_priv->lock, flags);
227 	return ret;
228 }
229 
cma_comp_exch(struct rdma_id_private * id_priv,enum cma_state comp,enum cma_state exch)230 static int cma_comp_exch(struct rdma_id_private *id_priv,
231 			 enum cma_state comp, enum cma_state exch)
232 {
233 	unsigned long flags;
234 	int ret;
235 
236 	spin_lock_irqsave(&id_priv->lock, flags);
237 	if ((ret = (id_priv->state == comp)))
238 		id_priv->state = exch;
239 	spin_unlock_irqrestore(&id_priv->lock, flags);
240 	return ret;
241 }
242 
cma_exch(struct rdma_id_private * id_priv,enum cma_state exch)243 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
244 			       enum cma_state exch)
245 {
246 	unsigned long flags;
247 	enum cma_state old;
248 
249 	spin_lock_irqsave(&id_priv->lock, flags);
250 	old = id_priv->state;
251 	id_priv->state = exch;
252 	spin_unlock_irqrestore(&id_priv->lock, flags);
253 	return old;
254 }
255 
cma_get_ip_ver(struct cma_hdr * hdr)256 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
257 {
258 	return hdr->ip_version >> 4;
259 }
260 
cma_set_ip_ver(struct cma_hdr * hdr,u8 ip_ver)261 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
262 {
263 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
264 }
265 
sdp_get_majv(u8 sdp_version)266 static inline u8 sdp_get_majv(u8 sdp_version)
267 {
268 	return sdp_version >> 4;
269 }
270 
sdp_get_ip_ver(struct sdp_hh * hh)271 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
272 {
273 	return hh->ip_version >> 4;
274 }
275 
sdp_set_ip_ver(struct sdp_hh * hh,u8 ip_ver)276 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
277 {
278 	hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
279 }
280 
cma_is_ud_ps(enum rdma_port_space ps)281 static inline int cma_is_ud_ps(enum rdma_port_space ps)
282 {
283 	return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
284 }
285 
cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)286 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
287 			      struct cma_device *cma_dev)
288 {
289 	atomic_inc(&cma_dev->refcount);
290 	id_priv->cma_dev = cma_dev;
291 	id_priv->id.device = cma_dev->device;
292 	id_priv->id.route.addr.dev_addr.transport =
293 		rdma_node_get_transport(cma_dev->device->node_type);
294 	list_add_tail(&id_priv->list, &cma_dev->id_list);
295 }
296 
cma_deref_dev(struct cma_device * cma_dev)297 static inline void cma_deref_dev(struct cma_device *cma_dev)
298 {
299 	if (atomic_dec_and_test(&cma_dev->refcount))
300 		complete(&cma_dev->comp);
301 }
302 
release_mc(struct kref * kref)303 static inline void release_mc(struct kref *kref)
304 {
305 	struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
306 
307 	kfree(mc->multicast.ib);
308 	kfree(mc);
309 }
310 
cma_release_dev(struct rdma_id_private * id_priv)311 static void cma_release_dev(struct rdma_id_private *id_priv)
312 {
313 	mutex_lock(&lock);
314 	list_del(&id_priv->list);
315 	cma_deref_dev(id_priv->cma_dev);
316 	id_priv->cma_dev = NULL;
317 	mutex_unlock(&lock);
318 }
319 
cma_set_qkey(struct rdma_id_private * id_priv)320 static int cma_set_qkey(struct rdma_id_private *id_priv)
321 {
322 	struct ib_sa_mcmember_rec rec;
323 	int ret = 0;
324 
325 	if (id_priv->qkey)
326 		return 0;
327 
328 	switch (id_priv->id.ps) {
329 	case RDMA_PS_UDP:
330 		id_priv->qkey = RDMA_UDP_QKEY;
331 		break;
332 	case RDMA_PS_IPOIB:
333 		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
334 		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
335 					     id_priv->id.port_num, &rec.mgid,
336 					     &rec);
337 		if (!ret)
338 			id_priv->qkey = be32_to_cpu(rec.qkey);
339 		break;
340 	default:
341 		break;
342 	}
343 	return ret;
344 }
345 
find_gid_port(struct ib_device * device,union ib_gid * gid,u8 port_num)346 static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
347 {
348 	int i;
349 	int err;
350 	struct ib_port_attr props;
351 	union ib_gid tmp;
352 
353 	err = ib_query_port(device, port_num, &props);
354 	if (err)
355 		return 1;
356 
357 	for (i = 0; i < props.gid_tbl_len; ++i) {
358 		err = ib_query_gid(device, port_num, i, &tmp);
359 		if (err)
360 			return 1;
361 		if (!memcmp(&tmp, gid, sizeof tmp))
362 			return 0;
363 	}
364 
365 	return -EAGAIN;
366 }
367 
cma_acquire_dev(struct rdma_id_private * id_priv)368 static int cma_acquire_dev(struct rdma_id_private *id_priv)
369 {
370 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
371 	struct cma_device *cma_dev;
372 	union ib_gid gid, iboe_gid;
373 	int ret = -ENODEV;
374 	u8 port;
375 	enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
376 		IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
377 
378 	mutex_lock(&lock);
379 	iboe_addr_get_sgid(dev_addr, &iboe_gid);
380 	memcpy(&gid, dev_addr->src_dev_addr +
381 	       rdma_addr_gid_offset(dev_addr), sizeof gid);
382 	list_for_each_entry(cma_dev, &dev_list, list) {
383 		for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
384 			if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
385 				if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
386 				    rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
387 					ret = find_gid_port(cma_dev->device, &iboe_gid, port);
388 				else
389 					ret = find_gid_port(cma_dev->device, &gid, port);
390 
391 				if (!ret) {
392 					id_priv->id.port_num = port;
393 					goto out;
394 				} else if (ret == 1)
395 					break;
396 			}
397 		}
398 	}
399 
400 out:
401 	if (!ret)
402 		cma_attach_to_dev(id_priv, cma_dev);
403 
404 	mutex_unlock(&lock);
405 	return ret;
406 }
407 
cma_deref_id(struct rdma_id_private * id_priv)408 static void cma_deref_id(struct rdma_id_private *id_priv)
409 {
410 	if (atomic_dec_and_test(&id_priv->refcount))
411 		complete(&id_priv->comp);
412 }
413 
cma_disable_callback(struct rdma_id_private * id_priv,enum cma_state state)414 static int cma_disable_callback(struct rdma_id_private *id_priv,
415 			      enum cma_state state)
416 {
417 	mutex_lock(&id_priv->handler_mutex);
418 	if (id_priv->state != state) {
419 		mutex_unlock(&id_priv->handler_mutex);
420 		return -EINVAL;
421 	}
422 	return 0;
423 }
424 
cma_has_cm_dev(struct rdma_id_private * id_priv)425 static int cma_has_cm_dev(struct rdma_id_private *id_priv)
426 {
427 	return (id_priv->id.device && id_priv->cm_id.ib);
428 }
429 
rdma_create_id(rdma_cm_event_handler event_handler,void * context,enum rdma_port_space ps)430 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
431 				  void *context, enum rdma_port_space ps)
432 {
433 	struct rdma_id_private *id_priv;
434 
435 	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
436 	if (!id_priv)
437 		return ERR_PTR(-ENOMEM);
438 
439 	id_priv->state = CMA_IDLE;
440 	id_priv->id.context = context;
441 	id_priv->id.event_handler = event_handler;
442 	id_priv->id.ps = ps;
443 	spin_lock_init(&id_priv->lock);
444 	mutex_init(&id_priv->qp_mutex);
445 	init_completion(&id_priv->comp);
446 	atomic_set(&id_priv->refcount, 1);
447 	mutex_init(&id_priv->handler_mutex);
448 	INIT_LIST_HEAD(&id_priv->listen_list);
449 	INIT_LIST_HEAD(&id_priv->mc_list);
450 	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
451 
452 	return &id_priv->id;
453 }
454 EXPORT_SYMBOL(rdma_create_id);
455 
cma_init_ud_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)456 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
457 {
458 	struct ib_qp_attr qp_attr;
459 	int qp_attr_mask, ret;
460 
461 	qp_attr.qp_state = IB_QPS_INIT;
462 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
463 	if (ret)
464 		return ret;
465 
466 	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
467 	if (ret)
468 		return ret;
469 
470 	qp_attr.qp_state = IB_QPS_RTR;
471 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
472 	if (ret)
473 		return ret;
474 
475 	qp_attr.qp_state = IB_QPS_RTS;
476 	qp_attr.sq_psn = 0;
477 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
478 
479 	return ret;
480 }
481 
cma_init_conn_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)482 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
483 {
484 	struct ib_qp_attr qp_attr;
485 	int qp_attr_mask, ret;
486 
487 	qp_attr.qp_state = IB_QPS_INIT;
488 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
489 	if (ret)
490 		return ret;
491 
492 	return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
493 }
494 
rdma_create_qp(struct rdma_cm_id * id,struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)495 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
496 		   struct ib_qp_init_attr *qp_init_attr)
497 {
498 	struct rdma_id_private *id_priv;
499 	struct ib_qp *qp;
500 	int ret;
501 
502 	id_priv = container_of(id, struct rdma_id_private, id);
503 	if (id->device != pd->device)
504 		return -EINVAL;
505 
506 	qp = ib_create_qp(pd, qp_init_attr);
507 	if (IS_ERR(qp))
508 		return PTR_ERR(qp);
509 
510 	if (cma_is_ud_ps(id_priv->id.ps))
511 		ret = cma_init_ud_qp(id_priv, qp);
512 	else
513 		ret = cma_init_conn_qp(id_priv, qp);
514 	if (ret)
515 		goto err;
516 
517 	id->qp = qp;
518 	id_priv->qp_num = qp->qp_num;
519 	id_priv->srq = (qp->srq != NULL);
520 	return 0;
521 err:
522 	ib_destroy_qp(qp);
523 	return ret;
524 }
525 EXPORT_SYMBOL(rdma_create_qp);
526 
rdma_destroy_qp(struct rdma_cm_id * id)527 void rdma_destroy_qp(struct rdma_cm_id *id)
528 {
529 	struct rdma_id_private *id_priv;
530 
531 	id_priv = container_of(id, struct rdma_id_private, id);
532 	mutex_lock(&id_priv->qp_mutex);
533 	ib_destroy_qp(id_priv->id.qp);
534 	id_priv->id.qp = NULL;
535 	mutex_unlock(&id_priv->qp_mutex);
536 }
537 EXPORT_SYMBOL(rdma_destroy_qp);
538 
cma_modify_qp_rtr(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)539 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
540 			     struct rdma_conn_param *conn_param)
541 {
542 	struct ib_qp_attr qp_attr;
543 	int qp_attr_mask, ret;
544 
545 	mutex_lock(&id_priv->qp_mutex);
546 	if (!id_priv->id.qp) {
547 		ret = 0;
548 		goto out;
549 	}
550 
551 	/* Need to update QP attributes from default values. */
552 	qp_attr.qp_state = IB_QPS_INIT;
553 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
554 	if (ret)
555 		goto out;
556 
557 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
558 	if (ret)
559 		goto out;
560 
561 	qp_attr.qp_state = IB_QPS_RTR;
562 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
563 	if (ret)
564 		goto out;
565 
566 	if (conn_param)
567 		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
568 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
569 out:
570 	mutex_unlock(&id_priv->qp_mutex);
571 	return ret;
572 }
573 
cma_modify_qp_rts(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)574 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
575 			     struct rdma_conn_param *conn_param)
576 {
577 	struct ib_qp_attr qp_attr;
578 	int qp_attr_mask, ret;
579 
580 	mutex_lock(&id_priv->qp_mutex);
581 	if (!id_priv->id.qp) {
582 		ret = 0;
583 		goto out;
584 	}
585 
586 	qp_attr.qp_state = IB_QPS_RTS;
587 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
588 	if (ret)
589 		goto out;
590 
591 	if (conn_param)
592 		qp_attr.max_rd_atomic = conn_param->initiator_depth;
593 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
594 out:
595 	mutex_unlock(&id_priv->qp_mutex);
596 	return ret;
597 }
598 
cma_modify_qp_err(struct rdma_id_private * id_priv)599 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
600 {
601 	struct ib_qp_attr qp_attr;
602 	int ret;
603 
604 	mutex_lock(&id_priv->qp_mutex);
605 	if (!id_priv->id.qp) {
606 		ret = 0;
607 		goto out;
608 	}
609 
610 	qp_attr.qp_state = IB_QPS_ERR;
611 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
612 out:
613 	mutex_unlock(&id_priv->qp_mutex);
614 	return ret;
615 }
616 
cma_ib_init_qp_attr(struct rdma_id_private * id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)617 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
618 			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
619 {
620 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
621 	int ret;
622 	u16 pkey;
623 
624 	if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
625 	    IB_LINK_LAYER_INFINIBAND)
626 		pkey = ib_addr_get_pkey(dev_addr);
627 	else
628 		pkey = 0xffff;
629 
630 	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
631 				  pkey, &qp_attr->pkey_index);
632 	if (ret)
633 		return ret;
634 
635 	qp_attr->port_num = id_priv->id.port_num;
636 	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
637 
638 	if (cma_is_ud_ps(id_priv->id.ps)) {
639 		ret = cma_set_qkey(id_priv);
640 		if (ret)
641 			return ret;
642 
643 		qp_attr->qkey = id_priv->qkey;
644 		*qp_attr_mask |= IB_QP_QKEY;
645 	} else {
646 		qp_attr->qp_access_flags = 0;
647 		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
648 	}
649 	return 0;
650 }
651 
rdma_init_qp_attr(struct rdma_cm_id * id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)652 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
653 		       int *qp_attr_mask)
654 {
655 	struct rdma_id_private *id_priv;
656 	int ret = 0;
657 
658 	id_priv = container_of(id, struct rdma_id_private, id);
659 	switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
660 	case RDMA_TRANSPORT_IB:
661 		if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
662 			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
663 		else
664 			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
665 						 qp_attr_mask);
666 		if (qp_attr->qp_state == IB_QPS_RTR)
667 			qp_attr->rq_psn = id_priv->seq_num;
668 		break;
669 	case RDMA_TRANSPORT_IWARP:
670 		if (!id_priv->cm_id.iw) {
671 			qp_attr->qp_access_flags = 0;
672 			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
673 		} else
674 			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
675 						 qp_attr_mask);
676 		break;
677 	default:
678 		ret = -ENOSYS;
679 		break;
680 	}
681 
682 	return ret;
683 }
684 EXPORT_SYMBOL(rdma_init_qp_attr);
685 
cma_zero_addr(struct sockaddr * addr)686 static inline int cma_zero_addr(struct sockaddr *addr)
687 {
688 	struct in6_addr *ip6;
689 
690 	if (addr->sa_family == AF_INET)
691 		return ipv4_is_zeronet(
692 			((struct sockaddr_in *)addr)->sin_addr.s_addr);
693 	else {
694 		ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
695 		return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
696 			ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
697 	}
698 }
699 
cma_loopback_addr(struct sockaddr * addr)700 static inline int cma_loopback_addr(struct sockaddr *addr)
701 {
702 	if (addr->sa_family == AF_INET)
703 		return ipv4_is_loopback(
704 			((struct sockaddr_in *) addr)->sin_addr.s_addr);
705 	else
706 		return ipv6_addr_loopback(
707 			&((struct sockaddr_in6 *) addr)->sin6_addr);
708 }
709 
cma_any_addr(struct sockaddr * addr)710 static inline int cma_any_addr(struct sockaddr *addr)
711 {
712 	return cma_zero_addr(addr) || cma_loopback_addr(addr);
713 }
714 
cma_port(struct sockaddr * addr)715 static inline __be16 cma_port(struct sockaddr *addr)
716 {
717 	if (addr->sa_family == AF_INET)
718 		return ((struct sockaddr_in *) addr)->sin_port;
719 	else
720 		return ((struct sockaddr_in6 *) addr)->sin6_port;
721 }
722 
cma_any_port(struct sockaddr * addr)723 static inline int cma_any_port(struct sockaddr *addr)
724 {
725 	return !cma_port(addr);
726 }
727 
cma_get_net_info(void * hdr,enum rdma_port_space ps,u8 * ip_ver,__be16 * port,union cma_ip_addr ** src,union cma_ip_addr ** dst)728 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
729 			    u8 *ip_ver, __be16 *port,
730 			    union cma_ip_addr **src, union cma_ip_addr **dst)
731 {
732 	switch (ps) {
733 	case RDMA_PS_SDP:
734 		if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
735 		    SDP_MAJ_VERSION)
736 			return -EINVAL;
737 
738 		*ip_ver	= sdp_get_ip_ver(hdr);
739 		*port	= ((struct sdp_hh *) hdr)->port;
740 		*src	= &((struct sdp_hh *) hdr)->src_addr;
741 		*dst	= &((struct sdp_hh *) hdr)->dst_addr;
742 		break;
743 	default:
744 		if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
745 			return -EINVAL;
746 
747 		*ip_ver	= cma_get_ip_ver(hdr);
748 		*port	= ((struct cma_hdr *) hdr)->port;
749 		*src	= &((struct cma_hdr *) hdr)->src_addr;
750 		*dst	= &((struct cma_hdr *) hdr)->dst_addr;
751 		break;
752 	}
753 
754 	if (*ip_ver != 4 && *ip_ver != 6)
755 		return -EINVAL;
756 	return 0;
757 }
758 
cma_save_net_info(struct rdma_addr * addr,struct rdma_addr * listen_addr,u8 ip_ver,__be16 port,union cma_ip_addr * src,union cma_ip_addr * dst)759 static void cma_save_net_info(struct rdma_addr *addr,
760 			      struct rdma_addr *listen_addr,
761 			      u8 ip_ver, __be16 port,
762 			      union cma_ip_addr *src, union cma_ip_addr *dst)
763 {
764 	struct sockaddr_in *listen4, *ip4;
765 	struct sockaddr_in6 *listen6, *ip6;
766 
767 	switch (ip_ver) {
768 	case 4:
769 		listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
770 		ip4 = (struct sockaddr_in *) &addr->src_addr;
771 		ip4->sin_family = listen4->sin_family;
772 		ip4->sin_addr.s_addr = dst->ip4.addr;
773 		ip4->sin_port = listen4->sin_port;
774 
775 		ip4 = (struct sockaddr_in *) &addr->dst_addr;
776 		ip4->sin_family = listen4->sin_family;
777 		ip4->sin_addr.s_addr = src->ip4.addr;
778 		ip4->sin_port = port;
779 		break;
780 	case 6:
781 		listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
782 		ip6 = (struct sockaddr_in6 *) &addr->src_addr;
783 		ip6->sin6_family = listen6->sin6_family;
784 		ip6->sin6_addr = dst->ip6;
785 		ip6->sin6_port = listen6->sin6_port;
786 
787 		ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
788 		ip6->sin6_family = listen6->sin6_family;
789 		ip6->sin6_addr = src->ip6;
790 		ip6->sin6_port = port;
791 		break;
792 	default:
793 		break;
794 	}
795 }
796 
cma_user_data_offset(enum rdma_port_space ps)797 static inline int cma_user_data_offset(enum rdma_port_space ps)
798 {
799 	switch (ps) {
800 	case RDMA_PS_SDP:
801 		return 0;
802 	default:
803 		return sizeof(struct cma_hdr);
804 	}
805 }
806 
cma_cancel_route(struct rdma_id_private * id_priv)807 static void cma_cancel_route(struct rdma_id_private *id_priv)
808 {
809 	switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
810 	case IB_LINK_LAYER_INFINIBAND:
811 		if (id_priv->query)
812 			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
813 		break;
814 	default:
815 		break;
816 	}
817 }
818 
cma_cancel_listens(struct rdma_id_private * id_priv)819 static void cma_cancel_listens(struct rdma_id_private *id_priv)
820 {
821 	struct rdma_id_private *dev_id_priv;
822 
823 	/*
824 	 * Remove from listen_any_list to prevent added devices from spawning
825 	 * additional listen requests.
826 	 */
827 	mutex_lock(&lock);
828 	list_del(&id_priv->list);
829 
830 	while (!list_empty(&id_priv->listen_list)) {
831 		dev_id_priv = list_entry(id_priv->listen_list.next,
832 					 struct rdma_id_private, listen_list);
833 		/* sync with device removal to avoid duplicate destruction */
834 		list_del_init(&dev_id_priv->list);
835 		list_del(&dev_id_priv->listen_list);
836 		mutex_unlock(&lock);
837 
838 		rdma_destroy_id(&dev_id_priv->id);
839 		mutex_lock(&lock);
840 	}
841 	mutex_unlock(&lock);
842 }
843 
cma_cancel_operation(struct rdma_id_private * id_priv,enum cma_state state)844 static void cma_cancel_operation(struct rdma_id_private *id_priv,
845 				 enum cma_state state)
846 {
847 	switch (state) {
848 	case CMA_ADDR_QUERY:
849 		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
850 		break;
851 	case CMA_ROUTE_QUERY:
852 		cma_cancel_route(id_priv);
853 		break;
854 	case CMA_LISTEN:
855 		if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
856 				&& !id_priv->cma_dev)
857 			cma_cancel_listens(id_priv);
858 		break;
859 	default:
860 		break;
861 	}
862 }
863 
cma_release_port(struct rdma_id_private * id_priv)864 static void cma_release_port(struct rdma_id_private *id_priv)
865 {
866 	struct rdma_bind_list *bind_list = id_priv->bind_list;
867 
868 	if (!bind_list)
869 		return;
870 
871 	mutex_lock(&lock);
872 	hlist_del(&id_priv->node);
873 	if (hlist_empty(&bind_list->owners)) {
874 		idr_remove(bind_list->ps, bind_list->port);
875 		kfree(bind_list);
876 	}
877 	mutex_unlock(&lock);
878 }
879 
cma_leave_mc_groups(struct rdma_id_private * id_priv)880 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
881 {
882 	struct cma_multicast *mc;
883 
884 	while (!list_empty(&id_priv->mc_list)) {
885 		mc = container_of(id_priv->mc_list.next,
886 				  struct cma_multicast, list);
887 		list_del(&mc->list);
888 		switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
889 		case IB_LINK_LAYER_INFINIBAND:
890 			ib_sa_free_multicast(mc->multicast.ib);
891 			kfree(mc);
892 			break;
893 		case IB_LINK_LAYER_ETHERNET:
894 			kref_put(&mc->mcref, release_mc);
895 			break;
896 		default:
897 			break;
898 		}
899 	}
900 }
901 
rdma_destroy_id(struct rdma_cm_id * id)902 void rdma_destroy_id(struct rdma_cm_id *id)
903 {
904 	struct rdma_id_private *id_priv;
905 	enum cma_state state;
906 
907 	id_priv = container_of(id, struct rdma_id_private, id);
908 	state = cma_exch(id_priv, CMA_DESTROYING);
909 	cma_cancel_operation(id_priv, state);
910 
911 	/*
912 	 * Wait for any active callback to finish.  New callbacks will find
913 	 * the id_priv state set to destroying and abort.
914 	 */
915 	mutex_lock(&id_priv->handler_mutex);
916 	mutex_unlock(&id_priv->handler_mutex);
917 
918 	if (id_priv->cma_dev) {
919 		switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
920 		case RDMA_TRANSPORT_IB:
921 			if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
922 				ib_destroy_cm_id(id_priv->cm_id.ib);
923 			break;
924 		case RDMA_TRANSPORT_IWARP:
925 			if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
926 				iw_destroy_cm_id(id_priv->cm_id.iw);
927 			break;
928 		default:
929 			break;
930 		}
931 		cma_leave_mc_groups(id_priv);
932 		cma_release_dev(id_priv);
933 	}
934 
935 	cma_release_port(id_priv);
936 	cma_deref_id(id_priv);
937 	wait_for_completion(&id_priv->comp);
938 
939 	if (id_priv->internal_id)
940 		cma_deref_id(id_priv->id.context);
941 
942 	kfree(id_priv->id.route.path_rec);
943 	kfree(id_priv);
944 }
945 EXPORT_SYMBOL(rdma_destroy_id);
946 
cma_rep_recv(struct rdma_id_private * id_priv)947 static int cma_rep_recv(struct rdma_id_private *id_priv)
948 {
949 	int ret;
950 
951 	ret = cma_modify_qp_rtr(id_priv, NULL);
952 	if (ret)
953 		goto reject;
954 
955 	ret = cma_modify_qp_rts(id_priv, NULL);
956 	if (ret)
957 		goto reject;
958 
959 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
960 	if (ret)
961 		goto reject;
962 
963 	return 0;
964 reject:
965 	cma_modify_qp_err(id_priv);
966 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
967 		       NULL, 0, NULL, 0);
968 	return ret;
969 }
970 
cma_verify_rep(struct rdma_id_private * id_priv,void * data)971 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
972 {
973 	if (id_priv->id.ps == RDMA_PS_SDP &&
974 	    sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
975 	    SDP_MAJ_VERSION)
976 		return -EINVAL;
977 
978 	return 0;
979 }
980 
cma_set_rep_event_data(struct rdma_cm_event * event,struct ib_cm_rep_event_param * rep_data,void * private_data)981 static void cma_set_rep_event_data(struct rdma_cm_event *event,
982 				   struct ib_cm_rep_event_param *rep_data,
983 				   void *private_data)
984 {
985 	event->param.conn.private_data = private_data;
986 	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
987 	event->param.conn.responder_resources = rep_data->responder_resources;
988 	event->param.conn.initiator_depth = rep_data->initiator_depth;
989 	event->param.conn.flow_control = rep_data->flow_control;
990 	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
991 	event->param.conn.srq = rep_data->srq;
992 	event->param.conn.qp_num = rep_data->remote_qpn;
993 }
994 
cma_ib_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)995 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
996 {
997 	struct rdma_id_private *id_priv = cm_id->context;
998 	struct rdma_cm_event event;
999 	int ret = 0;
1000 
1001 	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1002 		cma_disable_callback(id_priv, CMA_CONNECT)) ||
1003 	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1004 		cma_disable_callback(id_priv, CMA_DISCONNECT)))
1005 		return 0;
1006 
1007 	memset(&event, 0, sizeof event);
1008 	switch (ib_event->event) {
1009 	case IB_CM_REQ_ERROR:
1010 	case IB_CM_REP_ERROR:
1011 		event.event = RDMA_CM_EVENT_UNREACHABLE;
1012 		event.status = -ETIMEDOUT;
1013 		break;
1014 	case IB_CM_REP_RECEIVED:
1015 		event.status = cma_verify_rep(id_priv, ib_event->private_data);
1016 		if (event.status)
1017 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1018 		else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
1019 			event.status = cma_rep_recv(id_priv);
1020 			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1021 						     RDMA_CM_EVENT_ESTABLISHED;
1022 		} else
1023 			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1024 		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1025 				       ib_event->private_data);
1026 		break;
1027 	case IB_CM_RTU_RECEIVED:
1028 	case IB_CM_USER_ESTABLISHED:
1029 		event.event = RDMA_CM_EVENT_ESTABLISHED;
1030 		break;
1031 	case IB_CM_DREQ_ERROR:
1032 		event.status = -ETIMEDOUT; /* fall through */
1033 	case IB_CM_DREQ_RECEIVED:
1034 	case IB_CM_DREP_RECEIVED:
1035 		if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
1036 			goto out;
1037 		event.event = RDMA_CM_EVENT_DISCONNECTED;
1038 		break;
1039 	case IB_CM_TIMEWAIT_EXIT:
1040 		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1041 		break;
1042 	case IB_CM_MRA_RECEIVED:
1043 		/* ignore event */
1044 		goto out;
1045 	case IB_CM_REJ_RECEIVED:
1046 		cma_modify_qp_err(id_priv);
1047 		event.status = ib_event->param.rej_rcvd.reason;
1048 		event.event = RDMA_CM_EVENT_REJECTED;
1049 		event.param.conn.private_data = ib_event->private_data;
1050 		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1051 		break;
1052 	default:
1053 		printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
1054 		       ib_event->event);
1055 		goto out;
1056 	}
1057 
1058 	ret = id_priv->id.event_handler(&id_priv->id, &event);
1059 	if (ret) {
1060 		/* Destroy the CM ID by returning a non-zero value. */
1061 		id_priv->cm_id.ib = NULL;
1062 		cma_exch(id_priv, CMA_DESTROYING);
1063 		mutex_unlock(&id_priv->handler_mutex);
1064 		rdma_destroy_id(&id_priv->id);
1065 		return ret;
1066 	}
1067 out:
1068 	mutex_unlock(&id_priv->handler_mutex);
1069 	return ret;
1070 }
1071 
cma_new_conn_id(struct rdma_cm_id * listen_id,struct ib_cm_event * ib_event)1072 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1073 					       struct ib_cm_event *ib_event)
1074 {
1075 	struct rdma_id_private *id_priv;
1076 	struct rdma_cm_id *id;
1077 	struct rdma_route *rt;
1078 	union cma_ip_addr *src, *dst;
1079 	__be16 port;
1080 	u8 ip_ver;
1081 	int ret;
1082 
1083 	if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1084 			     &ip_ver, &port, &src, &dst))
1085 		goto err;
1086 
1087 	id = rdma_create_id(listen_id->event_handler, listen_id->context,
1088 			    listen_id->ps);
1089 	if (IS_ERR(id))
1090 		goto err;
1091 
1092 	cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1093 			  ip_ver, port, src, dst);
1094 
1095 	rt = &id->route;
1096 	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1097 	rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1098 			       GFP_KERNEL);
1099 	if (!rt->path_rec)
1100 		goto destroy_id;
1101 
1102 	rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1103 	if (rt->num_paths == 2)
1104 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1105 
1106 	if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1107 		rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1108 		rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1109 		ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1110 	} else {
1111 		ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1112 					&rt->addr.dev_addr);
1113 		if (ret)
1114 			goto destroy_id;
1115 	}
1116 	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1117 
1118 	id_priv = container_of(id, struct rdma_id_private, id);
1119 	id_priv->state = CMA_CONNECT;
1120 	return id_priv;
1121 
1122 destroy_id:
1123 	rdma_destroy_id(id);
1124 err:
1125 	return NULL;
1126 }
1127 
cma_new_udp_id(struct rdma_cm_id * listen_id,struct ib_cm_event * ib_event)1128 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1129 					      struct ib_cm_event *ib_event)
1130 {
1131 	struct rdma_id_private *id_priv;
1132 	struct rdma_cm_id *id;
1133 	union cma_ip_addr *src, *dst;
1134 	__be16 port;
1135 	u8 ip_ver;
1136 	int ret;
1137 
1138 	id = rdma_create_id(listen_id->event_handler, listen_id->context,
1139 			    listen_id->ps);
1140 	if (IS_ERR(id))
1141 		return NULL;
1142 
1143 
1144 	if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1145 			     &ip_ver, &port, &src, &dst))
1146 		goto err;
1147 
1148 	cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1149 			  ip_ver, port, src, dst);
1150 
1151 	if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1152 		ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1153 					&id->route.addr.dev_addr);
1154 		if (ret)
1155 			goto err;
1156 	}
1157 
1158 	id_priv = container_of(id, struct rdma_id_private, id);
1159 	id_priv->state = CMA_CONNECT;
1160 	return id_priv;
1161 err:
1162 	rdma_destroy_id(id);
1163 	return NULL;
1164 }
1165 
cma_set_req_event_data(struct rdma_cm_event * event,struct ib_cm_req_event_param * req_data,void * private_data,int offset)1166 static void cma_set_req_event_data(struct rdma_cm_event *event,
1167 				   struct ib_cm_req_event_param *req_data,
1168 				   void *private_data, int offset)
1169 {
1170 	event->param.conn.private_data = private_data + offset;
1171 	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1172 	event->param.conn.responder_resources = req_data->responder_resources;
1173 	event->param.conn.initiator_depth = req_data->initiator_depth;
1174 	event->param.conn.flow_control = req_data->flow_control;
1175 	event->param.conn.retry_count = req_data->retry_count;
1176 	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1177 	event->param.conn.srq = req_data->srq;
1178 	event->param.conn.qp_num = req_data->remote_qpn;
1179 }
1180 
cma_req_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)1181 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1182 {
1183 	struct rdma_id_private *listen_id, *conn_id;
1184 	struct rdma_cm_event event;
1185 	int offset, ret;
1186 
1187 	listen_id = cm_id->context;
1188 	if (cma_disable_callback(listen_id, CMA_LISTEN))
1189 		return -ECONNABORTED;
1190 
1191 	memset(&event, 0, sizeof event);
1192 	offset = cma_user_data_offset(listen_id->id.ps);
1193 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1194 	if (cma_is_ud_ps(listen_id->id.ps)) {
1195 		conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1196 		event.param.ud.private_data = ib_event->private_data + offset;
1197 		event.param.ud.private_data_len =
1198 				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1199 	} else {
1200 		conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1201 		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1202 				       ib_event->private_data, offset);
1203 	}
1204 	if (!conn_id) {
1205 		ret = -ENOMEM;
1206 		goto out;
1207 	}
1208 
1209 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1210 	ret = cma_acquire_dev(conn_id);
1211 	if (ret)
1212 		goto release_conn_id;
1213 
1214 	conn_id->cm_id.ib = cm_id;
1215 	cm_id->context = conn_id;
1216 	cm_id->cm_handler = cma_ib_handler;
1217 
1218 	/*
1219 	 * Protect against the user destroying conn_id from another thread
1220 	 * until we're done accessing it.
1221 	 */
1222 	atomic_inc(&conn_id->refcount);
1223 	ret = conn_id->id.event_handler(&conn_id->id, &event);
1224 	if (!ret) {
1225 		/*
1226 		 * Acquire mutex to prevent user executing rdma_destroy_id()
1227 		 * while we're accessing the cm_id.
1228 		 */
1229 		mutex_lock(&lock);
1230 		if (cma_comp(conn_id, CMA_CONNECT) &&
1231 		    !cma_is_ud_ps(conn_id->id.ps))
1232 			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1233 		mutex_unlock(&lock);
1234 		mutex_unlock(&conn_id->handler_mutex);
1235 		cma_deref_id(conn_id);
1236 		goto out;
1237 	}
1238 	cma_deref_id(conn_id);
1239 
1240 	/* Destroy the CM ID by returning a non-zero value. */
1241 	conn_id->cm_id.ib = NULL;
1242 
1243 release_conn_id:
1244 	cma_exch(conn_id, CMA_DESTROYING);
1245 	mutex_unlock(&conn_id->handler_mutex);
1246 	rdma_destroy_id(&conn_id->id);
1247 
1248 out:
1249 	mutex_unlock(&listen_id->handler_mutex);
1250 	return ret;
1251 }
1252 
cma_get_service_id(enum rdma_port_space ps,struct sockaddr * addr)1253 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1254 {
1255 	return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1256 }
1257 
cma_set_compare_data(enum rdma_port_space ps,struct sockaddr * addr,struct ib_cm_compare_data * compare)1258 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1259 				 struct ib_cm_compare_data *compare)
1260 {
1261 	struct cma_hdr *cma_data, *cma_mask;
1262 	struct sdp_hh *sdp_data, *sdp_mask;
1263 	__be32 ip4_addr;
1264 	struct in6_addr ip6_addr;
1265 
1266 	memset(compare, 0, sizeof *compare);
1267 	cma_data = (void *) compare->data;
1268 	cma_mask = (void *) compare->mask;
1269 	sdp_data = (void *) compare->data;
1270 	sdp_mask = (void *) compare->mask;
1271 
1272 	switch (addr->sa_family) {
1273 	case AF_INET:
1274 		ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1275 		if (ps == RDMA_PS_SDP) {
1276 			sdp_set_ip_ver(sdp_data, 4);
1277 			sdp_set_ip_ver(sdp_mask, 0xF);
1278 			sdp_data->dst_addr.ip4.addr = ip4_addr;
1279 			sdp_mask->dst_addr.ip4.addr = htonl(~0);
1280 		} else {
1281 			cma_set_ip_ver(cma_data, 4);
1282 			cma_set_ip_ver(cma_mask, 0xF);
1283 			cma_data->dst_addr.ip4.addr = ip4_addr;
1284 			cma_mask->dst_addr.ip4.addr = htonl(~0);
1285 		}
1286 		break;
1287 	case AF_INET6:
1288 		ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1289 		if (ps == RDMA_PS_SDP) {
1290 			sdp_set_ip_ver(sdp_data, 6);
1291 			sdp_set_ip_ver(sdp_mask, 0xF);
1292 			sdp_data->dst_addr.ip6 = ip6_addr;
1293 			memset(&sdp_mask->dst_addr.ip6, 0xFF,
1294 			       sizeof sdp_mask->dst_addr.ip6);
1295 		} else {
1296 			cma_set_ip_ver(cma_data, 6);
1297 			cma_set_ip_ver(cma_mask, 0xF);
1298 			cma_data->dst_addr.ip6 = ip6_addr;
1299 			memset(&cma_mask->dst_addr.ip6, 0xFF,
1300 			       sizeof cma_mask->dst_addr.ip6);
1301 		}
1302 		break;
1303 	default:
1304 		break;
1305 	}
1306 }
1307 
cma_iw_handler(struct iw_cm_id * iw_id,struct iw_cm_event * iw_event)1308 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1309 {
1310 	struct rdma_id_private *id_priv = iw_id->context;
1311 	struct rdma_cm_event event;
1312 	struct sockaddr_in *sin;
1313 	int ret = 0;
1314 
1315 	if (cma_disable_callback(id_priv, CMA_CONNECT))
1316 		return 0;
1317 
1318 	memset(&event, 0, sizeof event);
1319 	switch (iw_event->event) {
1320 	case IW_CM_EVENT_CLOSE:
1321 		event.event = RDMA_CM_EVENT_DISCONNECTED;
1322 		break;
1323 	case IW_CM_EVENT_CONNECT_REPLY:
1324 		sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1325 		*sin = iw_event->local_addr;
1326 		sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1327 		*sin = iw_event->remote_addr;
1328 		switch (iw_event->status) {
1329 		case 0:
1330 			event.event = RDMA_CM_EVENT_ESTABLISHED;
1331 			break;
1332 		case -ECONNRESET:
1333 		case -ECONNREFUSED:
1334 			event.event = RDMA_CM_EVENT_REJECTED;
1335 			break;
1336 		case -ETIMEDOUT:
1337 			event.event = RDMA_CM_EVENT_UNREACHABLE;
1338 			break;
1339 		default:
1340 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1341 			break;
1342 		}
1343 		break;
1344 	case IW_CM_EVENT_ESTABLISHED:
1345 		event.event = RDMA_CM_EVENT_ESTABLISHED;
1346 		break;
1347 	default:
1348 		BUG_ON(1);
1349 	}
1350 
1351 	event.status = iw_event->status;
1352 	event.param.conn.private_data = iw_event->private_data;
1353 	event.param.conn.private_data_len = iw_event->private_data_len;
1354 	ret = id_priv->id.event_handler(&id_priv->id, &event);
1355 	if (ret) {
1356 		/* Destroy the CM ID by returning a non-zero value. */
1357 		id_priv->cm_id.iw = NULL;
1358 		cma_exch(id_priv, CMA_DESTROYING);
1359 		mutex_unlock(&id_priv->handler_mutex);
1360 		rdma_destroy_id(&id_priv->id);
1361 		return ret;
1362 	}
1363 
1364 	mutex_unlock(&id_priv->handler_mutex);
1365 	return ret;
1366 }
1367 
iw_conn_req_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)1368 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1369 			       struct iw_cm_event *iw_event)
1370 {
1371 	struct rdma_cm_id *new_cm_id;
1372 	struct rdma_id_private *listen_id, *conn_id;
1373 	struct sockaddr_in *sin;
1374 	struct net_device *dev = NULL;
1375 	struct rdma_cm_event event;
1376 	int ret;
1377 	struct ib_device_attr attr;
1378 
1379 	listen_id = cm_id->context;
1380 	if (cma_disable_callback(listen_id, CMA_LISTEN))
1381 		return -ECONNABORTED;
1382 
1383 	/* Create a new RDMA id for the new IW CM ID */
1384 	new_cm_id = rdma_create_id(listen_id->id.event_handler,
1385 				   listen_id->id.context,
1386 				   RDMA_PS_TCP);
1387 	if (IS_ERR(new_cm_id)) {
1388 		ret = -ENOMEM;
1389 		goto out;
1390 	}
1391 	conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1392 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1393 	conn_id->state = CMA_CONNECT;
1394 
1395 	dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
1396 	if (!dev) {
1397 		ret = -EADDRNOTAVAIL;
1398 		mutex_unlock(&conn_id->handler_mutex);
1399 		rdma_destroy_id(new_cm_id);
1400 		goto out;
1401 	}
1402 	ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1403 	if (ret) {
1404 		mutex_unlock(&conn_id->handler_mutex);
1405 		rdma_destroy_id(new_cm_id);
1406 		goto out;
1407 	}
1408 
1409 	ret = cma_acquire_dev(conn_id);
1410 	if (ret) {
1411 		mutex_unlock(&conn_id->handler_mutex);
1412 		rdma_destroy_id(new_cm_id);
1413 		goto out;
1414 	}
1415 
1416 	conn_id->cm_id.iw = cm_id;
1417 	cm_id->context = conn_id;
1418 	cm_id->cm_handler = cma_iw_handler;
1419 
1420 	sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1421 	*sin = iw_event->local_addr;
1422 	sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1423 	*sin = iw_event->remote_addr;
1424 
1425 	ret = ib_query_device(conn_id->id.device, &attr);
1426 	if (ret) {
1427 		mutex_unlock(&conn_id->handler_mutex);
1428 		rdma_destroy_id(new_cm_id);
1429 		goto out;
1430 	}
1431 
1432 	memset(&event, 0, sizeof event);
1433 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1434 	event.param.conn.private_data = iw_event->private_data;
1435 	event.param.conn.private_data_len = iw_event->private_data_len;
1436 	event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1437 	event.param.conn.responder_resources = attr.max_qp_rd_atom;
1438 
1439 	/*
1440 	 * Protect against the user destroying conn_id from another thread
1441 	 * until we're done accessing it.
1442 	 */
1443 	atomic_inc(&conn_id->refcount);
1444 	ret = conn_id->id.event_handler(&conn_id->id, &event);
1445 	if (ret) {
1446 		/* User wants to destroy the CM ID */
1447 		conn_id->cm_id.iw = NULL;
1448 		cma_exch(conn_id, CMA_DESTROYING);
1449 		mutex_unlock(&conn_id->handler_mutex);
1450 		cma_deref_id(conn_id);
1451 		rdma_destroy_id(&conn_id->id);
1452 		goto out;
1453 	}
1454 
1455 	mutex_unlock(&conn_id->handler_mutex);
1456 	cma_deref_id(conn_id);
1457 
1458 out:
1459 	if (dev)
1460 		dev_put(dev);
1461 	mutex_unlock(&listen_id->handler_mutex);
1462 	return ret;
1463 }
1464 
cma_ib_listen(struct rdma_id_private * id_priv)1465 static int cma_ib_listen(struct rdma_id_private *id_priv)
1466 {
1467 	struct ib_cm_compare_data compare_data;
1468 	struct sockaddr *addr;
1469 	__be64 svc_id;
1470 	int ret;
1471 
1472 	id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1473 					    id_priv);
1474 	if (IS_ERR(id_priv->cm_id.ib))
1475 		return PTR_ERR(id_priv->cm_id.ib);
1476 
1477 	addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
1478 	svc_id = cma_get_service_id(id_priv->id.ps, addr);
1479 	if (cma_any_addr(addr))
1480 		ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1481 	else {
1482 		cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1483 		ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1484 	}
1485 
1486 	if (ret) {
1487 		ib_destroy_cm_id(id_priv->cm_id.ib);
1488 		id_priv->cm_id.ib = NULL;
1489 	}
1490 
1491 	return ret;
1492 }
1493 
cma_iw_listen(struct rdma_id_private * id_priv,int backlog)1494 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1495 {
1496 	int ret;
1497 	struct sockaddr_in *sin;
1498 
1499 	id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1500 					    iw_conn_req_handler,
1501 					    id_priv);
1502 	if (IS_ERR(id_priv->cm_id.iw))
1503 		return PTR_ERR(id_priv->cm_id.iw);
1504 
1505 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1506 	id_priv->cm_id.iw->local_addr = *sin;
1507 
1508 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1509 
1510 	if (ret) {
1511 		iw_destroy_cm_id(id_priv->cm_id.iw);
1512 		id_priv->cm_id.iw = NULL;
1513 	}
1514 
1515 	return ret;
1516 }
1517 
cma_listen_handler(struct rdma_cm_id * id,struct rdma_cm_event * event)1518 static int cma_listen_handler(struct rdma_cm_id *id,
1519 			      struct rdma_cm_event *event)
1520 {
1521 	struct rdma_id_private *id_priv = id->context;
1522 
1523 	id->context = id_priv->id.context;
1524 	id->event_handler = id_priv->id.event_handler;
1525 	return id_priv->id.event_handler(id, event);
1526 }
1527 
cma_listen_on_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)1528 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1529 			      struct cma_device *cma_dev)
1530 {
1531 	struct rdma_id_private *dev_id_priv;
1532 	struct rdma_cm_id *id;
1533 	int ret;
1534 
1535 	id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1536 	if (IS_ERR(id))
1537 		return;
1538 
1539 	dev_id_priv = container_of(id, struct rdma_id_private, id);
1540 
1541 	dev_id_priv->state = CMA_ADDR_BOUND;
1542 	memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1543 	       ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
1544 
1545 	cma_attach_to_dev(dev_id_priv, cma_dev);
1546 	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1547 	atomic_inc(&id_priv->refcount);
1548 	dev_id_priv->internal_id = 1;
1549 
1550 	ret = rdma_listen(id, id_priv->backlog);
1551 	if (ret)
1552 		printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
1553 		       "listening on device %s\n", ret, cma_dev->device->name);
1554 }
1555 
cma_listen_on_all(struct rdma_id_private * id_priv)1556 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1557 {
1558 	struct cma_device *cma_dev;
1559 
1560 	mutex_lock(&lock);
1561 	list_add_tail(&id_priv->list, &listen_any_list);
1562 	list_for_each_entry(cma_dev, &dev_list, list)
1563 		cma_listen_on_dev(id_priv, cma_dev);
1564 	mutex_unlock(&lock);
1565 }
1566 
rdma_listen(struct rdma_cm_id * id,int backlog)1567 int rdma_listen(struct rdma_cm_id *id, int backlog)
1568 {
1569 	struct rdma_id_private *id_priv;
1570 	int ret;
1571 
1572 	id_priv = container_of(id, struct rdma_id_private, id);
1573 	if (id_priv->state == CMA_IDLE) {
1574 		((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
1575 		ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
1576 		if (ret)
1577 			return ret;
1578 	}
1579 
1580 	if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1581 		return -EINVAL;
1582 
1583 	id_priv->backlog = backlog;
1584 	if (id->device) {
1585 		switch (rdma_node_get_transport(id->device->node_type)) {
1586 		case RDMA_TRANSPORT_IB:
1587 			ret = cma_ib_listen(id_priv);
1588 			if (ret)
1589 				goto err;
1590 			break;
1591 		case RDMA_TRANSPORT_IWARP:
1592 			ret = cma_iw_listen(id_priv, backlog);
1593 			if (ret)
1594 				goto err;
1595 			break;
1596 		default:
1597 			ret = -ENOSYS;
1598 			goto err;
1599 		}
1600 	} else
1601 		cma_listen_on_all(id_priv);
1602 
1603 	return 0;
1604 err:
1605 	id_priv->backlog = 0;
1606 	cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1607 	return ret;
1608 }
1609 EXPORT_SYMBOL(rdma_listen);
1610 
rdma_set_service_type(struct rdma_cm_id * id,int tos)1611 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1612 {
1613 	struct rdma_id_private *id_priv;
1614 
1615 	id_priv = container_of(id, struct rdma_id_private, id);
1616 	id_priv->tos = (u8) tos;
1617 }
1618 EXPORT_SYMBOL(rdma_set_service_type);
1619 
cma_query_handler(int status,struct ib_sa_path_rec * path_rec,void * context)1620 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1621 			      void *context)
1622 {
1623 	struct cma_work *work = context;
1624 	struct rdma_route *route;
1625 
1626 	route = &work->id->id.route;
1627 
1628 	if (!status) {
1629 		route->num_paths = 1;
1630 		*route->path_rec = *path_rec;
1631 	} else {
1632 		work->old_state = CMA_ROUTE_QUERY;
1633 		work->new_state = CMA_ADDR_RESOLVED;
1634 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1635 		work->event.status = status;
1636 	}
1637 
1638 	queue_work(cma_wq, &work->work);
1639 }
1640 
cma_query_ib_route(struct rdma_id_private * id_priv,int timeout_ms,struct cma_work * work)1641 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1642 			      struct cma_work *work)
1643 {
1644 	struct rdma_addr *addr = &id_priv->id.route.addr;
1645 	struct ib_sa_path_rec path_rec;
1646 	ib_sa_comp_mask comp_mask;
1647 	struct sockaddr_in6 *sin6;
1648 
1649 	memset(&path_rec, 0, sizeof path_rec);
1650 	rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1651 	rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
1652 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
1653 	path_rec.numb_path = 1;
1654 	path_rec.reversible = 1;
1655 	path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1656 							(struct sockaddr *) &addr->dst_addr);
1657 
1658 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1659 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1660 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1661 
1662 	if (addr->src_addr.ss_family == AF_INET) {
1663 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1664 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1665 	} else {
1666 		sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1667 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1668 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1669 	}
1670 
1671 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1672 					       id_priv->id.port_num, &path_rec,
1673 					       comp_mask, timeout_ms,
1674 					       GFP_KERNEL, cma_query_handler,
1675 					       work, &id_priv->query);
1676 
1677 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1678 }
1679 
cma_work_handler(struct work_struct * _work)1680 static void cma_work_handler(struct work_struct *_work)
1681 {
1682 	struct cma_work *work = container_of(_work, struct cma_work, work);
1683 	struct rdma_id_private *id_priv = work->id;
1684 	int destroy = 0;
1685 
1686 	mutex_lock(&id_priv->handler_mutex);
1687 	if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1688 		goto out;
1689 
1690 	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1691 		cma_exch(id_priv, CMA_DESTROYING);
1692 		destroy = 1;
1693 	}
1694 out:
1695 	mutex_unlock(&id_priv->handler_mutex);
1696 	cma_deref_id(id_priv);
1697 	if (destroy)
1698 		rdma_destroy_id(&id_priv->id);
1699 	kfree(work);
1700 }
1701 
cma_ndev_work_handler(struct work_struct * _work)1702 static void cma_ndev_work_handler(struct work_struct *_work)
1703 {
1704 	struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1705 	struct rdma_id_private *id_priv = work->id;
1706 	int destroy = 0;
1707 
1708 	mutex_lock(&id_priv->handler_mutex);
1709 	if (id_priv->state == CMA_DESTROYING ||
1710 	    id_priv->state == CMA_DEVICE_REMOVAL)
1711 		goto out;
1712 
1713 	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1714 		cma_exch(id_priv, CMA_DESTROYING);
1715 		destroy = 1;
1716 	}
1717 
1718 out:
1719 	mutex_unlock(&id_priv->handler_mutex);
1720 	cma_deref_id(id_priv);
1721 	if (destroy)
1722 		rdma_destroy_id(&id_priv->id);
1723 	kfree(work);
1724 }
1725 
cma_resolve_ib_route(struct rdma_id_private * id_priv,int timeout_ms)1726 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1727 {
1728 	struct rdma_route *route = &id_priv->id.route;
1729 	struct cma_work *work;
1730 	int ret;
1731 
1732 	work = kzalloc(sizeof *work, GFP_KERNEL);
1733 	if (!work)
1734 		return -ENOMEM;
1735 
1736 	work->id = id_priv;
1737 	INIT_WORK(&work->work, cma_work_handler);
1738 	work->old_state = CMA_ROUTE_QUERY;
1739 	work->new_state = CMA_ROUTE_RESOLVED;
1740 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1741 
1742 	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1743 	if (!route->path_rec) {
1744 		ret = -ENOMEM;
1745 		goto err1;
1746 	}
1747 
1748 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
1749 	if (ret)
1750 		goto err2;
1751 
1752 	return 0;
1753 err2:
1754 	kfree(route->path_rec);
1755 	route->path_rec = NULL;
1756 err1:
1757 	kfree(work);
1758 	return ret;
1759 }
1760 
rdma_set_ib_paths(struct rdma_cm_id * id,struct ib_sa_path_rec * path_rec,int num_paths)1761 int rdma_set_ib_paths(struct rdma_cm_id *id,
1762 		      struct ib_sa_path_rec *path_rec, int num_paths)
1763 {
1764 	struct rdma_id_private *id_priv;
1765 	int ret;
1766 
1767 	id_priv = container_of(id, struct rdma_id_private, id);
1768 	if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1769 		return -EINVAL;
1770 
1771 	id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1772 				     GFP_KERNEL);
1773 	if (!id->route.path_rec) {
1774 		ret = -ENOMEM;
1775 		goto err;
1776 	}
1777 
1778 	id->route.num_paths = num_paths;
1779 	return 0;
1780 err:
1781 	cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1782 	return ret;
1783 }
1784 EXPORT_SYMBOL(rdma_set_ib_paths);
1785 
cma_resolve_iw_route(struct rdma_id_private * id_priv,int timeout_ms)1786 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1787 {
1788 	struct cma_work *work;
1789 
1790 	work = kzalloc(sizeof *work, GFP_KERNEL);
1791 	if (!work)
1792 		return -ENOMEM;
1793 
1794 	work->id = id_priv;
1795 	INIT_WORK(&work->work, cma_work_handler);
1796 	work->old_state = CMA_ROUTE_QUERY;
1797 	work->new_state = CMA_ROUTE_RESOLVED;
1798 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1799 	queue_work(cma_wq, &work->work);
1800 	return 0;
1801 }
1802 
cma_resolve_iboe_route(struct rdma_id_private * id_priv)1803 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1804 {
1805 	struct rdma_route *route = &id_priv->id.route;
1806 	struct rdma_addr *addr = &route->addr;
1807 	struct cma_work *work;
1808 	int ret;
1809 	struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1810 	struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1811 	struct net_device *ndev = NULL;
1812 	u16 vid;
1813 
1814 	if (src_addr->sin_family != dst_addr->sin_family)
1815 		return -EINVAL;
1816 
1817 	work = kzalloc(sizeof *work, GFP_KERNEL);
1818 	if (!work)
1819 		return -ENOMEM;
1820 
1821 	work->id = id_priv;
1822 	INIT_WORK(&work->work, cma_work_handler);
1823 
1824 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1825 	if (!route->path_rec) {
1826 		ret = -ENOMEM;
1827 		goto err1;
1828 	}
1829 
1830 	route->num_paths = 1;
1831 
1832 	if (addr->dev_addr.bound_dev_if)
1833 		ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1834 	if (!ndev) {
1835 		ret = -ENODEV;
1836 		goto err2;
1837 	}
1838 
1839 	vid = rdma_vlan_dev_vlan_id(ndev);
1840 
1841 	iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1842 	iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1843 
1844 	route->path_rec->hop_limit = 1;
1845 	route->path_rec->reversible = 1;
1846 	route->path_rec->pkey = cpu_to_be16(0xffff);
1847 	route->path_rec->mtu_selector = IB_SA_EQ;
1848 	route->path_rec->sl = id_priv->tos >> 5;
1849 
1850 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1851 	route->path_rec->rate_selector = IB_SA_EQ;
1852 	route->path_rec->rate = iboe_get_rate(ndev);
1853 	dev_put(ndev);
1854 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
1855 	route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1856 	if (!route->path_rec->mtu) {
1857 		ret = -EINVAL;
1858 		goto err2;
1859 	}
1860 
1861 	work->old_state = CMA_ROUTE_QUERY;
1862 	work->new_state = CMA_ROUTE_RESOLVED;
1863 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1864 	work->event.status = 0;
1865 
1866 	queue_work(cma_wq, &work->work);
1867 
1868 	return 0;
1869 
1870 err2:
1871 	kfree(route->path_rec);
1872 	route->path_rec = NULL;
1873 err1:
1874 	kfree(work);
1875 	return ret;
1876 }
1877 
rdma_resolve_route(struct rdma_cm_id * id,int timeout_ms)1878 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1879 {
1880 	struct rdma_id_private *id_priv;
1881 	int ret;
1882 
1883 	id_priv = container_of(id, struct rdma_id_private, id);
1884 	if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1885 		return -EINVAL;
1886 
1887 	atomic_inc(&id_priv->refcount);
1888 	switch (rdma_node_get_transport(id->device->node_type)) {
1889 	case RDMA_TRANSPORT_IB:
1890 		switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1891 		case IB_LINK_LAYER_INFINIBAND:
1892 			ret = cma_resolve_ib_route(id_priv, timeout_ms);
1893 			break;
1894 		case IB_LINK_LAYER_ETHERNET:
1895 			ret = cma_resolve_iboe_route(id_priv);
1896 			break;
1897 		default:
1898 			ret = -ENOSYS;
1899 		}
1900 		break;
1901 	case RDMA_TRANSPORT_IWARP:
1902 		ret = cma_resolve_iw_route(id_priv, timeout_ms);
1903 		break;
1904 	default:
1905 		ret = -ENOSYS;
1906 		break;
1907 	}
1908 	if (ret)
1909 		goto err;
1910 
1911 	return 0;
1912 err:
1913 	cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1914 	cma_deref_id(id_priv);
1915 	return ret;
1916 }
1917 EXPORT_SYMBOL(rdma_resolve_route);
1918 
cma_bind_loopback(struct rdma_id_private * id_priv)1919 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1920 {
1921 	struct cma_device *cma_dev;
1922 	struct ib_port_attr port_attr;
1923 	union ib_gid gid;
1924 	u16 pkey;
1925 	int ret;
1926 	u8 p;
1927 
1928 	mutex_lock(&lock);
1929 	if (list_empty(&dev_list)) {
1930 		ret = -ENODEV;
1931 		goto out;
1932 	}
1933 	list_for_each_entry(cma_dev, &dev_list, list)
1934 		for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1935 			if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1936 			    port_attr.state == IB_PORT_ACTIVE)
1937 				goto port_found;
1938 
1939 	p = 1;
1940 	cma_dev = list_entry(dev_list.next, struct cma_device, list);
1941 
1942 port_found:
1943 	ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1944 	if (ret)
1945 		goto out;
1946 
1947 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1948 	if (ret)
1949 		goto out;
1950 
1951 	id_priv->id.route.addr.dev_addr.dev_type =
1952 		(rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
1953 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
1954 
1955 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1956 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1957 	id_priv->id.port_num = p;
1958 	cma_attach_to_dev(id_priv, cma_dev);
1959 out:
1960 	mutex_unlock(&lock);
1961 	return ret;
1962 }
1963 
addr_handler(int status,struct sockaddr * src_addr,struct rdma_dev_addr * dev_addr,void * context)1964 static void addr_handler(int status, struct sockaddr *src_addr,
1965 			 struct rdma_dev_addr *dev_addr, void *context)
1966 {
1967 	struct rdma_id_private *id_priv = context;
1968 	struct rdma_cm_event event;
1969 
1970 	memset(&event, 0, sizeof event);
1971 	mutex_lock(&id_priv->handler_mutex);
1972 	if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED))
1973 		goto out;
1974 
1975 	if (!status && !id_priv->cma_dev)
1976 		status = cma_acquire_dev(id_priv);
1977 
1978 	if (status) {
1979 		if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1980 			goto out;
1981 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
1982 		event.status = status;
1983 	} else {
1984 		memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1985 		       ip_addr_size(src_addr));
1986 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1987 	}
1988 
1989 	if (id_priv->id.event_handler(&id_priv->id, &event)) {
1990 		cma_exch(id_priv, CMA_DESTROYING);
1991 		mutex_unlock(&id_priv->handler_mutex);
1992 		cma_deref_id(id_priv);
1993 		rdma_destroy_id(&id_priv->id);
1994 		return;
1995 	}
1996 out:
1997 	mutex_unlock(&id_priv->handler_mutex);
1998 	cma_deref_id(id_priv);
1999 }
2000 
cma_resolve_loopback(struct rdma_id_private * id_priv)2001 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2002 {
2003 	struct cma_work *work;
2004 	struct sockaddr *src, *dst;
2005 	union ib_gid gid;
2006 	int ret;
2007 
2008 	work = kzalloc(sizeof *work, GFP_KERNEL);
2009 	if (!work)
2010 		return -ENOMEM;
2011 
2012 	if (!id_priv->cma_dev) {
2013 		ret = cma_bind_loopback(id_priv);
2014 		if (ret)
2015 			goto err;
2016 	}
2017 
2018 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2019 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2020 
2021 	src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2022 	if (cma_zero_addr(src)) {
2023 		dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
2024 		if ((src->sa_family = dst->sa_family) == AF_INET) {
2025 			((struct sockaddr_in *) src)->sin_addr.s_addr =
2026 				((struct sockaddr_in *) dst)->sin_addr.s_addr;
2027 		} else {
2028 			ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
2029 				       &((struct sockaddr_in6 *) dst)->sin6_addr);
2030 		}
2031 	}
2032 
2033 	work->id = id_priv;
2034 	INIT_WORK(&work->work, cma_work_handler);
2035 	work->old_state = CMA_ADDR_QUERY;
2036 	work->new_state = CMA_ADDR_RESOLVED;
2037 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2038 	queue_work(cma_wq, &work->work);
2039 	return 0;
2040 err:
2041 	kfree(work);
2042 	return ret;
2043 }
2044 
cma_bind_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,struct sockaddr * dst_addr)2045 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2046 			 struct sockaddr *dst_addr)
2047 {
2048 	if (!src_addr || !src_addr->sa_family) {
2049 		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2050 		if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2051 			((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2052 				((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2053 		}
2054 	}
2055 	return rdma_bind_addr(id, src_addr);
2056 }
2057 
rdma_resolve_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,struct sockaddr * dst_addr,int timeout_ms)2058 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2059 		      struct sockaddr *dst_addr, int timeout_ms)
2060 {
2061 	struct rdma_id_private *id_priv;
2062 	int ret;
2063 
2064 	id_priv = container_of(id, struct rdma_id_private, id);
2065 	if (id_priv->state == CMA_IDLE) {
2066 		ret = cma_bind_addr(id, src_addr, dst_addr);
2067 		if (ret)
2068 			return ret;
2069 	}
2070 
2071 	if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
2072 		return -EINVAL;
2073 
2074 	atomic_inc(&id_priv->refcount);
2075 	memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2076 	if (cma_any_addr(dst_addr))
2077 		ret = cma_resolve_loopback(id_priv);
2078 	else
2079 		ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
2080 				      dst_addr, &id->route.addr.dev_addr,
2081 				      timeout_ms, addr_handler, id_priv);
2082 	if (ret)
2083 		goto err;
2084 
2085 	return 0;
2086 err:
2087 	cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
2088 	cma_deref_id(id_priv);
2089 	return ret;
2090 }
2091 EXPORT_SYMBOL(rdma_resolve_addr);
2092 
cma_bind_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)2093 static void cma_bind_port(struct rdma_bind_list *bind_list,
2094 			  struct rdma_id_private *id_priv)
2095 {
2096 	struct sockaddr_in *sin;
2097 
2098 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2099 	sin->sin_port = htons(bind_list->port);
2100 	id_priv->bind_list = bind_list;
2101 	hlist_add_head(&id_priv->node, &bind_list->owners);
2102 }
2103 
cma_alloc_port(struct idr * ps,struct rdma_id_private * id_priv,unsigned short snum)2104 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2105 			  unsigned short snum)
2106 {
2107 	struct rdma_bind_list *bind_list;
2108 	int port, ret;
2109 
2110 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2111 	if (!bind_list)
2112 		return -ENOMEM;
2113 
2114 	do {
2115 		ret = idr_get_new_above(ps, bind_list, snum, &port);
2116 	} while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2117 
2118 	if (ret)
2119 		goto err1;
2120 
2121 	if (port != snum) {
2122 		ret = -EADDRNOTAVAIL;
2123 		goto err2;
2124 	}
2125 
2126 	bind_list->ps = ps;
2127 	bind_list->port = (unsigned short) port;
2128 	cma_bind_port(bind_list, id_priv);
2129 	return 0;
2130 err2:
2131 	idr_remove(ps, port);
2132 err1:
2133 	kfree(bind_list);
2134 	return ret;
2135 }
2136 
cma_alloc_any_port(struct idr * ps,struct rdma_id_private * id_priv)2137 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2138 {
2139 	static unsigned int last_used_port;
2140 	int low, high, remaining;
2141 	unsigned int rover;
2142 
2143 	inet_get_local_port_range(&low, &high);
2144 	remaining = (high - low) + 1;
2145 	rover = net_random() % remaining + low;
2146 retry:
2147 	if (last_used_port != rover &&
2148 	    !idr_find(ps, (unsigned short) rover)) {
2149 		int ret = cma_alloc_port(ps, id_priv, rover);
2150 		/*
2151 		 * Remember previously used port number in order to avoid
2152 		 * re-using same port immediately after it is closed.
2153 		 */
2154 		if (!ret)
2155 			last_used_port = rover;
2156 		if (ret != -EADDRNOTAVAIL)
2157 			return ret;
2158 	}
2159 	if (--remaining) {
2160 		rover++;
2161 		if ((rover < low) || (rover > high))
2162 			rover = low;
2163 		goto retry;
2164 	}
2165 	return -EADDRNOTAVAIL;
2166 }
2167 
cma_use_port(struct idr * ps,struct rdma_id_private * id_priv)2168 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2169 {
2170 	struct rdma_id_private *cur_id;
2171 	struct sockaddr_in *sin, *cur_sin;
2172 	struct rdma_bind_list *bind_list;
2173 	struct hlist_node *node;
2174 	unsigned short snum;
2175 
2176 	sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2177 	snum = ntohs(sin->sin_port);
2178 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2179 		return -EACCES;
2180 
2181 	bind_list = idr_find(ps, snum);
2182 	if (!bind_list)
2183 		return cma_alloc_port(ps, id_priv, snum);
2184 
2185 	/*
2186 	 * We don't support binding to any address if anyone is bound to
2187 	 * a specific address on the same port.
2188 	 */
2189 	if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr))
2190 		return -EADDRNOTAVAIL;
2191 
2192 	hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2193 		if (cma_any_addr((struct sockaddr *) &cur_id->id.route.addr.src_addr))
2194 			return -EADDRNOTAVAIL;
2195 
2196 		cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
2197 		if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
2198 			return -EADDRINUSE;
2199 	}
2200 
2201 	cma_bind_port(bind_list, id_priv);
2202 	return 0;
2203 }
2204 
cma_get_port(struct rdma_id_private * id_priv)2205 static int cma_get_port(struct rdma_id_private *id_priv)
2206 {
2207 	struct idr *ps;
2208 	int ret;
2209 
2210 	switch (id_priv->id.ps) {
2211 	case RDMA_PS_SDP:
2212 		ps = &sdp_ps;
2213 		break;
2214 	case RDMA_PS_TCP:
2215 		ps = &tcp_ps;
2216 		break;
2217 	case RDMA_PS_UDP:
2218 		ps = &udp_ps;
2219 		break;
2220 	case RDMA_PS_IPOIB:
2221 		ps = &ipoib_ps;
2222 		break;
2223 	default:
2224 		return -EPROTONOSUPPORT;
2225 	}
2226 
2227 	mutex_lock(&lock);
2228 	if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
2229 		ret = cma_alloc_any_port(ps, id_priv);
2230 	else
2231 		ret = cma_use_port(ps, id_priv);
2232 	mutex_unlock(&lock);
2233 
2234 	return ret;
2235 }
2236 
cma_check_linklocal(struct rdma_dev_addr * dev_addr,struct sockaddr * addr)2237 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2238 			       struct sockaddr *addr)
2239 {
2240 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2241 	struct sockaddr_in6 *sin6;
2242 
2243 	if (addr->sa_family != AF_INET6)
2244 		return 0;
2245 
2246 	sin6 = (struct sockaddr_in6 *) addr;
2247 	if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2248 	    !sin6->sin6_scope_id)
2249 			return -EINVAL;
2250 
2251 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
2252 #endif
2253 	return 0;
2254 }
2255 
rdma_bind_addr(struct rdma_cm_id * id,struct sockaddr * addr)2256 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2257 {
2258 	struct rdma_id_private *id_priv;
2259 	int ret;
2260 
2261 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
2262 		return -EAFNOSUPPORT;
2263 
2264 	id_priv = container_of(id, struct rdma_id_private, id);
2265 	if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
2266 		return -EINVAL;
2267 
2268 	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2269 	if (ret)
2270 		goto err1;
2271 
2272 	if (!cma_any_addr(addr)) {
2273 		ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
2274 		if (ret)
2275 			goto err1;
2276 
2277 		ret = cma_acquire_dev(id_priv);
2278 		if (ret)
2279 			goto err1;
2280 	}
2281 
2282 	memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2283 	ret = cma_get_port(id_priv);
2284 	if (ret)
2285 		goto err2;
2286 
2287 	return 0;
2288 err2:
2289 	if (id_priv->cma_dev)
2290 		cma_release_dev(id_priv);
2291 err1:
2292 	cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2293 	return ret;
2294 }
2295 EXPORT_SYMBOL(rdma_bind_addr);
2296 
cma_format_hdr(void * hdr,enum rdma_port_space ps,struct rdma_route * route)2297 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2298 			  struct rdma_route *route)
2299 {
2300 	struct cma_hdr *cma_hdr;
2301 	struct sdp_hh *sdp_hdr;
2302 
2303 	if (route->addr.src_addr.ss_family == AF_INET) {
2304 		struct sockaddr_in *src4, *dst4;
2305 
2306 		src4 = (struct sockaddr_in *) &route->addr.src_addr;
2307 		dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2308 
2309 		switch (ps) {
2310 		case RDMA_PS_SDP:
2311 			sdp_hdr = hdr;
2312 			if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2313 				return -EINVAL;
2314 			sdp_set_ip_ver(sdp_hdr, 4);
2315 			sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2316 			sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2317 			sdp_hdr->port = src4->sin_port;
2318 			break;
2319 		default:
2320 			cma_hdr = hdr;
2321 			cma_hdr->cma_version = CMA_VERSION;
2322 			cma_set_ip_ver(cma_hdr, 4);
2323 			cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2324 			cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2325 			cma_hdr->port = src4->sin_port;
2326 			break;
2327 		}
2328 	} else {
2329 		struct sockaddr_in6 *src6, *dst6;
2330 
2331 		src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2332 		dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2333 
2334 		switch (ps) {
2335 		case RDMA_PS_SDP:
2336 			sdp_hdr = hdr;
2337 			if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2338 				return -EINVAL;
2339 			sdp_set_ip_ver(sdp_hdr, 6);
2340 			sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2341 			sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2342 			sdp_hdr->port = src6->sin6_port;
2343 			break;
2344 		default:
2345 			cma_hdr = hdr;
2346 			cma_hdr->cma_version = CMA_VERSION;
2347 			cma_set_ip_ver(cma_hdr, 6);
2348 			cma_hdr->src_addr.ip6 = src6->sin6_addr;
2349 			cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2350 			cma_hdr->port = src6->sin6_port;
2351 			break;
2352 		}
2353 	}
2354 	return 0;
2355 }
2356 
cma_sidr_rep_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)2357 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2358 				struct ib_cm_event *ib_event)
2359 {
2360 	struct rdma_id_private *id_priv = cm_id->context;
2361 	struct rdma_cm_event event;
2362 	struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2363 	int ret = 0;
2364 
2365 	if (cma_disable_callback(id_priv, CMA_CONNECT))
2366 		return 0;
2367 
2368 	memset(&event, 0, sizeof event);
2369 	switch (ib_event->event) {
2370 	case IB_CM_SIDR_REQ_ERROR:
2371 		event.event = RDMA_CM_EVENT_UNREACHABLE;
2372 		event.status = -ETIMEDOUT;
2373 		break;
2374 	case IB_CM_SIDR_REP_RECEIVED:
2375 		event.param.ud.private_data = ib_event->private_data;
2376 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2377 		if (rep->status != IB_SIDR_SUCCESS) {
2378 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2379 			event.status = ib_event->param.sidr_rep_rcvd.status;
2380 			break;
2381 		}
2382 		ret = cma_set_qkey(id_priv);
2383 		if (ret) {
2384 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
2385 			event.status = -EINVAL;
2386 			break;
2387 		}
2388 		if (id_priv->qkey != rep->qkey) {
2389 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2390 			event.status = -EINVAL;
2391 			break;
2392 		}
2393 		ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2394 				     id_priv->id.route.path_rec,
2395 				     &event.param.ud.ah_attr);
2396 		event.param.ud.qp_num = rep->qpn;
2397 		event.param.ud.qkey = rep->qkey;
2398 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2399 		event.status = 0;
2400 		break;
2401 	default:
2402 		printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
2403 		       ib_event->event);
2404 		goto out;
2405 	}
2406 
2407 	ret = id_priv->id.event_handler(&id_priv->id, &event);
2408 	if (ret) {
2409 		/* Destroy the CM ID by returning a non-zero value. */
2410 		id_priv->cm_id.ib = NULL;
2411 		cma_exch(id_priv, CMA_DESTROYING);
2412 		mutex_unlock(&id_priv->handler_mutex);
2413 		rdma_destroy_id(&id_priv->id);
2414 		return ret;
2415 	}
2416 out:
2417 	mutex_unlock(&id_priv->handler_mutex);
2418 	return ret;
2419 }
2420 
cma_resolve_ib_udp(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)2421 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2422 			      struct rdma_conn_param *conn_param)
2423 {
2424 	struct ib_cm_sidr_req_param req;
2425 	struct rdma_route *route;
2426 	int ret;
2427 
2428 	req.private_data_len = sizeof(struct cma_hdr) +
2429 			       conn_param->private_data_len;
2430 	req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2431 	if (!req.private_data)
2432 		return -ENOMEM;
2433 
2434 	if (conn_param->private_data && conn_param->private_data_len)
2435 		memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2436 		       conn_param->private_data, conn_param->private_data_len);
2437 
2438 	route = &id_priv->id.route;
2439 	ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2440 	if (ret)
2441 		goto out;
2442 
2443 	id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2444 					    cma_sidr_rep_handler, id_priv);
2445 	if (IS_ERR(id_priv->cm_id.ib)) {
2446 		ret = PTR_ERR(id_priv->cm_id.ib);
2447 		goto out;
2448 	}
2449 
2450 	req.path = route->path_rec;
2451 	req.service_id = cma_get_service_id(id_priv->id.ps,
2452 					    (struct sockaddr *) &route->addr.dst_addr);
2453 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2454 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
2455 
2456 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2457 	if (ret) {
2458 		ib_destroy_cm_id(id_priv->cm_id.ib);
2459 		id_priv->cm_id.ib = NULL;
2460 	}
2461 out:
2462 	kfree(req.private_data);
2463 	return ret;
2464 }
2465 
cma_connect_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)2466 static int cma_connect_ib(struct rdma_id_private *id_priv,
2467 			  struct rdma_conn_param *conn_param)
2468 {
2469 	struct ib_cm_req_param req;
2470 	struct rdma_route *route;
2471 	void *private_data;
2472 	int offset, ret;
2473 
2474 	memset(&req, 0, sizeof req);
2475 	offset = cma_user_data_offset(id_priv->id.ps);
2476 	req.private_data_len = offset + conn_param->private_data_len;
2477 	private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2478 	if (!private_data)
2479 		return -ENOMEM;
2480 
2481 	if (conn_param->private_data && conn_param->private_data_len)
2482 		memcpy(private_data + offset, conn_param->private_data,
2483 		       conn_param->private_data_len);
2484 
2485 	id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2486 					    id_priv);
2487 	if (IS_ERR(id_priv->cm_id.ib)) {
2488 		ret = PTR_ERR(id_priv->cm_id.ib);
2489 		goto out;
2490 	}
2491 
2492 	route = &id_priv->id.route;
2493 	ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2494 	if (ret)
2495 		goto out;
2496 	req.private_data = private_data;
2497 
2498 	req.primary_path = &route->path_rec[0];
2499 	if (route->num_paths == 2)
2500 		req.alternate_path = &route->path_rec[1];
2501 
2502 	req.service_id = cma_get_service_id(id_priv->id.ps,
2503 					    (struct sockaddr *) &route->addr.dst_addr);
2504 	req.qp_num = id_priv->qp_num;
2505 	req.qp_type = IB_QPT_RC;
2506 	req.starting_psn = id_priv->seq_num;
2507 	req.responder_resources = conn_param->responder_resources;
2508 	req.initiator_depth = conn_param->initiator_depth;
2509 	req.flow_control = conn_param->flow_control;
2510 	req.retry_count = conn_param->retry_count;
2511 	req.rnr_retry_count = conn_param->rnr_retry_count;
2512 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2513 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2514 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
2515 	req.srq = id_priv->srq ? 1 : 0;
2516 
2517 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2518 out:
2519 	if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2520 		ib_destroy_cm_id(id_priv->cm_id.ib);
2521 		id_priv->cm_id.ib = NULL;
2522 	}
2523 
2524 	kfree(private_data);
2525 	return ret;
2526 }
2527 
cma_connect_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)2528 static int cma_connect_iw(struct rdma_id_private *id_priv,
2529 			  struct rdma_conn_param *conn_param)
2530 {
2531 	struct iw_cm_id *cm_id;
2532 	struct sockaddr_in* sin;
2533 	int ret;
2534 	struct iw_cm_conn_param iw_param;
2535 
2536 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2537 	if (IS_ERR(cm_id)) {
2538 		ret = PTR_ERR(cm_id);
2539 		goto out;
2540 	}
2541 
2542 	id_priv->cm_id.iw = cm_id;
2543 
2544 	sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2545 	cm_id->local_addr = *sin;
2546 
2547 	sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2548 	cm_id->remote_addr = *sin;
2549 
2550 	ret = cma_modify_qp_rtr(id_priv, conn_param);
2551 	if (ret)
2552 		goto out;
2553 
2554 	iw_param.ord = conn_param->initiator_depth;
2555 	iw_param.ird = conn_param->responder_resources;
2556 	iw_param.private_data = conn_param->private_data;
2557 	iw_param.private_data_len = conn_param->private_data_len;
2558 	if (id_priv->id.qp)
2559 		iw_param.qpn = id_priv->qp_num;
2560 	else
2561 		iw_param.qpn = conn_param->qp_num;
2562 	ret = iw_cm_connect(cm_id, &iw_param);
2563 out:
2564 	if (ret && !IS_ERR(cm_id)) {
2565 		iw_destroy_cm_id(cm_id);
2566 		id_priv->cm_id.iw = NULL;
2567 	}
2568 	return ret;
2569 }
2570 
rdma_connect(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)2571 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2572 {
2573 	struct rdma_id_private *id_priv;
2574 	int ret;
2575 
2576 	id_priv = container_of(id, struct rdma_id_private, id);
2577 	if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2578 		return -EINVAL;
2579 
2580 	if (!id->qp) {
2581 		id_priv->qp_num = conn_param->qp_num;
2582 		id_priv->srq = conn_param->srq;
2583 	}
2584 
2585 	switch (rdma_node_get_transport(id->device->node_type)) {
2586 	case RDMA_TRANSPORT_IB:
2587 		if (cma_is_ud_ps(id->ps))
2588 			ret = cma_resolve_ib_udp(id_priv, conn_param);
2589 		else
2590 			ret = cma_connect_ib(id_priv, conn_param);
2591 		break;
2592 	case RDMA_TRANSPORT_IWARP:
2593 		ret = cma_connect_iw(id_priv, conn_param);
2594 		break;
2595 	default:
2596 		ret = -ENOSYS;
2597 		break;
2598 	}
2599 	if (ret)
2600 		goto err;
2601 
2602 	return 0;
2603 err:
2604 	cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2605 	return ret;
2606 }
2607 EXPORT_SYMBOL(rdma_connect);
2608 
cma_accept_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)2609 static int cma_accept_ib(struct rdma_id_private *id_priv,
2610 			 struct rdma_conn_param *conn_param)
2611 {
2612 	struct ib_cm_rep_param rep;
2613 	int ret;
2614 
2615 	ret = cma_modify_qp_rtr(id_priv, conn_param);
2616 	if (ret)
2617 		goto out;
2618 
2619 	ret = cma_modify_qp_rts(id_priv, conn_param);
2620 	if (ret)
2621 		goto out;
2622 
2623 	memset(&rep, 0, sizeof rep);
2624 	rep.qp_num = id_priv->qp_num;
2625 	rep.starting_psn = id_priv->seq_num;
2626 	rep.private_data = conn_param->private_data;
2627 	rep.private_data_len = conn_param->private_data_len;
2628 	rep.responder_resources = conn_param->responder_resources;
2629 	rep.initiator_depth = conn_param->initiator_depth;
2630 	rep.failover_accepted = 0;
2631 	rep.flow_control = conn_param->flow_control;
2632 	rep.rnr_retry_count = conn_param->rnr_retry_count;
2633 	rep.srq = id_priv->srq ? 1 : 0;
2634 
2635 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2636 out:
2637 	return ret;
2638 }
2639 
cma_accept_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)2640 static int cma_accept_iw(struct rdma_id_private *id_priv,
2641 		  struct rdma_conn_param *conn_param)
2642 {
2643 	struct iw_cm_conn_param iw_param;
2644 	int ret;
2645 
2646 	ret = cma_modify_qp_rtr(id_priv, conn_param);
2647 	if (ret)
2648 		return ret;
2649 
2650 	iw_param.ord = conn_param->initiator_depth;
2651 	iw_param.ird = conn_param->responder_resources;
2652 	iw_param.private_data = conn_param->private_data;
2653 	iw_param.private_data_len = conn_param->private_data_len;
2654 	if (id_priv->id.qp) {
2655 		iw_param.qpn = id_priv->qp_num;
2656 	} else
2657 		iw_param.qpn = conn_param->qp_num;
2658 
2659 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2660 }
2661 
cma_send_sidr_rep(struct rdma_id_private * id_priv,enum ib_cm_sidr_status status,const void * private_data,int private_data_len)2662 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2663 			     enum ib_cm_sidr_status status,
2664 			     const void *private_data, int private_data_len)
2665 {
2666 	struct ib_cm_sidr_rep_param rep;
2667 	int ret;
2668 
2669 	memset(&rep, 0, sizeof rep);
2670 	rep.status = status;
2671 	if (status == IB_SIDR_SUCCESS) {
2672 		ret = cma_set_qkey(id_priv);
2673 		if (ret)
2674 			return ret;
2675 		rep.qp_num = id_priv->qp_num;
2676 		rep.qkey = id_priv->qkey;
2677 	}
2678 	rep.private_data = private_data;
2679 	rep.private_data_len = private_data_len;
2680 
2681 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2682 }
2683 
rdma_accept(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)2684 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2685 {
2686 	struct rdma_id_private *id_priv;
2687 	int ret;
2688 
2689 	id_priv = container_of(id, struct rdma_id_private, id);
2690 	if (!cma_comp(id_priv, CMA_CONNECT))
2691 		return -EINVAL;
2692 
2693 	if (!id->qp && conn_param) {
2694 		id_priv->qp_num = conn_param->qp_num;
2695 		id_priv->srq = conn_param->srq;
2696 	}
2697 
2698 	switch (rdma_node_get_transport(id->device->node_type)) {
2699 	case RDMA_TRANSPORT_IB:
2700 		if (cma_is_ud_ps(id->ps))
2701 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2702 						conn_param->private_data,
2703 						conn_param->private_data_len);
2704 		else if (conn_param)
2705 			ret = cma_accept_ib(id_priv, conn_param);
2706 		else
2707 			ret = cma_rep_recv(id_priv);
2708 		break;
2709 	case RDMA_TRANSPORT_IWARP:
2710 		ret = cma_accept_iw(id_priv, conn_param);
2711 		break;
2712 	default:
2713 		ret = -ENOSYS;
2714 		break;
2715 	}
2716 
2717 	if (ret)
2718 		goto reject;
2719 
2720 	return 0;
2721 reject:
2722 	cma_modify_qp_err(id_priv);
2723 	rdma_reject(id, NULL, 0);
2724 	return ret;
2725 }
2726 EXPORT_SYMBOL(rdma_accept);
2727 
rdma_notify(struct rdma_cm_id * id,enum ib_event_type event)2728 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2729 {
2730 	struct rdma_id_private *id_priv;
2731 	int ret;
2732 
2733 	id_priv = container_of(id, struct rdma_id_private, id);
2734 	if (!cma_has_cm_dev(id_priv))
2735 		return -EINVAL;
2736 
2737 	switch (id->device->node_type) {
2738 	case RDMA_NODE_IB_CA:
2739 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
2740 		break;
2741 	default:
2742 		ret = 0;
2743 		break;
2744 	}
2745 	return ret;
2746 }
2747 EXPORT_SYMBOL(rdma_notify);
2748 
rdma_reject(struct rdma_cm_id * id,const void * private_data,u8 private_data_len)2749 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2750 		u8 private_data_len)
2751 {
2752 	struct rdma_id_private *id_priv;
2753 	int ret;
2754 
2755 	id_priv = container_of(id, struct rdma_id_private, id);
2756 	if (!cma_has_cm_dev(id_priv))
2757 		return -EINVAL;
2758 
2759 	switch (rdma_node_get_transport(id->device->node_type)) {
2760 	case RDMA_TRANSPORT_IB:
2761 		if (cma_is_ud_ps(id->ps))
2762 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2763 						private_data, private_data_len);
2764 		else
2765 			ret = ib_send_cm_rej(id_priv->cm_id.ib,
2766 					     IB_CM_REJ_CONSUMER_DEFINED, NULL,
2767 					     0, private_data, private_data_len);
2768 		break;
2769 	case RDMA_TRANSPORT_IWARP:
2770 		ret = iw_cm_reject(id_priv->cm_id.iw,
2771 				   private_data, private_data_len);
2772 		break;
2773 	default:
2774 		ret = -ENOSYS;
2775 		break;
2776 	}
2777 	return ret;
2778 }
2779 EXPORT_SYMBOL(rdma_reject);
2780 
rdma_disconnect(struct rdma_cm_id * id)2781 int rdma_disconnect(struct rdma_cm_id *id)
2782 {
2783 	struct rdma_id_private *id_priv;
2784 	int ret;
2785 
2786 	id_priv = container_of(id, struct rdma_id_private, id);
2787 	if (!cma_has_cm_dev(id_priv))
2788 		return -EINVAL;
2789 
2790 	switch (rdma_node_get_transport(id->device->node_type)) {
2791 	case RDMA_TRANSPORT_IB:
2792 		ret = cma_modify_qp_err(id_priv);
2793 		if (ret)
2794 			goto out;
2795 		/* Initiate or respond to a disconnect. */
2796 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2797 			ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2798 		break;
2799 	case RDMA_TRANSPORT_IWARP:
2800 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2801 		break;
2802 	default:
2803 		ret = -EINVAL;
2804 		break;
2805 	}
2806 out:
2807 	return ret;
2808 }
2809 EXPORT_SYMBOL(rdma_disconnect);
2810 
cma_ib_mc_handler(int status,struct ib_sa_multicast * multicast)2811 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2812 {
2813 	struct rdma_id_private *id_priv;
2814 	struct cma_multicast *mc = multicast->context;
2815 	struct rdma_cm_event event;
2816 	int ret;
2817 
2818 	id_priv = mc->id_priv;
2819 	if (cma_disable_callback(id_priv, CMA_ADDR_BOUND) &&
2820 	    cma_disable_callback(id_priv, CMA_ADDR_RESOLVED))
2821 		return 0;
2822 
2823 	mutex_lock(&id_priv->qp_mutex);
2824 	if (!status && id_priv->id.qp)
2825 		status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2826 					 multicast->rec.mlid);
2827 	mutex_unlock(&id_priv->qp_mutex);
2828 
2829 	memset(&event, 0, sizeof event);
2830 	event.status = status;
2831 	event.param.ud.private_data = mc->context;
2832 	if (!status) {
2833 		event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2834 		ib_init_ah_from_mcmember(id_priv->id.device,
2835 					 id_priv->id.port_num, &multicast->rec,
2836 					 &event.param.ud.ah_attr);
2837 		event.param.ud.qp_num = 0xFFFFFF;
2838 		event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2839 	} else
2840 		event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2841 
2842 	ret = id_priv->id.event_handler(&id_priv->id, &event);
2843 	if (ret) {
2844 		cma_exch(id_priv, CMA_DESTROYING);
2845 		mutex_unlock(&id_priv->handler_mutex);
2846 		rdma_destroy_id(&id_priv->id);
2847 		return 0;
2848 	}
2849 
2850 	mutex_unlock(&id_priv->handler_mutex);
2851 	return 0;
2852 }
2853 
cma_set_mgid(struct rdma_id_private * id_priv,struct sockaddr * addr,union ib_gid * mgid)2854 static void cma_set_mgid(struct rdma_id_private *id_priv,
2855 			 struct sockaddr *addr, union ib_gid *mgid)
2856 {
2857 	unsigned char mc_map[MAX_ADDR_LEN];
2858 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2859 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2860 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2861 
2862 	if (cma_any_addr(addr)) {
2863 		memset(mgid, 0, sizeof *mgid);
2864 	} else if ((addr->sa_family == AF_INET6) &&
2865 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
2866 								 0xFF10A01B)) {
2867 		/* IPv6 address is an SA assigned MGID. */
2868 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2869 	} else if ((addr->sa_family == AF_INET6)) {
2870 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2871 		if (id_priv->id.ps == RDMA_PS_UDP)
2872 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
2873 		*mgid = *(union ib_gid *) (mc_map + 4);
2874 	} else {
2875 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
2876 		if (id_priv->id.ps == RDMA_PS_UDP)
2877 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
2878 		*mgid = *(union ib_gid *) (mc_map + 4);
2879 	}
2880 }
2881 
cma_join_ib_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)2882 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2883 				 struct cma_multicast *mc)
2884 {
2885 	struct ib_sa_mcmember_rec rec;
2886 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2887 	ib_sa_comp_mask comp_mask;
2888 	int ret;
2889 
2890 	ib_addr_get_mgid(dev_addr, &rec.mgid);
2891 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2892 				     &rec.mgid, &rec);
2893 	if (ret)
2894 		return ret;
2895 
2896 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
2897 	if (id_priv->id.ps == RDMA_PS_UDP)
2898 		rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2899 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
2900 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2901 	rec.join_state = 1;
2902 
2903 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2904 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2905 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2906 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
2907 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2908 
2909 	if (id_priv->id.ps == RDMA_PS_IPOIB)
2910 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2911 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2912 
2913 	mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2914 						id_priv->id.port_num, &rec,
2915 						comp_mask, GFP_KERNEL,
2916 						cma_ib_mc_handler, mc);
2917 	if (IS_ERR(mc->multicast.ib))
2918 		return PTR_ERR(mc->multicast.ib);
2919 
2920 	return 0;
2921 }
2922 
iboe_mcast_work_handler(struct work_struct * work)2923 static void iboe_mcast_work_handler(struct work_struct *work)
2924 {
2925 	struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
2926 	struct cma_multicast *mc = mw->mc;
2927 	struct ib_sa_multicast *m = mc->multicast.ib;
2928 
2929 	mc->multicast.ib->context = mc;
2930 	cma_ib_mc_handler(0, m);
2931 	kref_put(&mc->mcref, release_mc);
2932 	kfree(mw);
2933 }
2934 
cma_iboe_set_mgid(struct sockaddr * addr,union ib_gid * mgid)2935 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
2936 {
2937 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
2938 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
2939 
2940 	if (cma_any_addr(addr)) {
2941 		memset(mgid, 0, sizeof *mgid);
2942 	} else if (addr->sa_family == AF_INET6) {
2943 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2944 	} else {
2945 		mgid->raw[0] = 0xff;
2946 		mgid->raw[1] = 0x0e;
2947 		mgid->raw[2] = 0;
2948 		mgid->raw[3] = 0;
2949 		mgid->raw[4] = 0;
2950 		mgid->raw[5] = 0;
2951 		mgid->raw[6] = 0;
2952 		mgid->raw[7] = 0;
2953 		mgid->raw[8] = 0;
2954 		mgid->raw[9] = 0;
2955 		mgid->raw[10] = 0xff;
2956 		mgid->raw[11] = 0xff;
2957 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
2958 	}
2959 }
2960 
cma_iboe_join_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)2961 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
2962 				   struct cma_multicast *mc)
2963 {
2964 	struct iboe_mcast_work *work;
2965 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2966 	int err;
2967 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
2968 	struct net_device *ndev = NULL;
2969 
2970 	if (cma_zero_addr((struct sockaddr *)&mc->addr))
2971 		return -EINVAL;
2972 
2973 	work = kzalloc(sizeof *work, GFP_KERNEL);
2974 	if (!work)
2975 		return -ENOMEM;
2976 
2977 	mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
2978 	if (!mc->multicast.ib) {
2979 		err = -ENOMEM;
2980 		goto out1;
2981 	}
2982 
2983 	cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
2984 
2985 	mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
2986 	if (id_priv->id.ps == RDMA_PS_UDP)
2987 		mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2988 
2989 	if (dev_addr->bound_dev_if)
2990 		ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
2991 	if (!ndev) {
2992 		err = -ENODEV;
2993 		goto out2;
2994 	}
2995 	mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
2996 	mc->multicast.ib->rec.hop_limit = 1;
2997 	mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
2998 	dev_put(ndev);
2999 	if (!mc->multicast.ib->rec.mtu) {
3000 		err = -EINVAL;
3001 		goto out2;
3002 	}
3003 	iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3004 	work->id = id_priv;
3005 	work->mc = mc;
3006 	INIT_WORK(&work->work, iboe_mcast_work_handler);
3007 	kref_get(&mc->mcref);
3008 	queue_work(cma_wq, &work->work);
3009 
3010 	return 0;
3011 
3012 out2:
3013 	kfree(mc->multicast.ib);
3014 out1:
3015 	kfree(work);
3016 	return err;
3017 }
3018 
rdma_join_multicast(struct rdma_cm_id * id,struct sockaddr * addr,void * context)3019 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3020 			void *context)
3021 {
3022 	struct rdma_id_private *id_priv;
3023 	struct cma_multicast *mc;
3024 	int ret;
3025 
3026 	id_priv = container_of(id, struct rdma_id_private, id);
3027 	if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
3028 	    !cma_comp(id_priv, CMA_ADDR_RESOLVED))
3029 		return -EINVAL;
3030 
3031 	mc = kmalloc(sizeof *mc, GFP_KERNEL);
3032 	if (!mc)
3033 		return -ENOMEM;
3034 
3035 	memcpy(&mc->addr, addr, ip_addr_size(addr));
3036 	mc->context = context;
3037 	mc->id_priv = id_priv;
3038 
3039 	spin_lock(&id_priv->lock);
3040 	list_add(&mc->list, &id_priv->mc_list);
3041 	spin_unlock(&id_priv->lock);
3042 
3043 	switch (rdma_node_get_transport(id->device->node_type)) {
3044 	case RDMA_TRANSPORT_IB:
3045 		switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3046 		case IB_LINK_LAYER_INFINIBAND:
3047 			ret = cma_join_ib_multicast(id_priv, mc);
3048 			break;
3049 		case IB_LINK_LAYER_ETHERNET:
3050 			kref_init(&mc->mcref);
3051 			ret = cma_iboe_join_multicast(id_priv, mc);
3052 			break;
3053 		default:
3054 			ret = -EINVAL;
3055 		}
3056 		break;
3057 	default:
3058 		ret = -ENOSYS;
3059 		break;
3060 	}
3061 
3062 	if (ret) {
3063 		spin_lock_irq(&id_priv->lock);
3064 		list_del(&mc->list);
3065 		spin_unlock_irq(&id_priv->lock);
3066 		kfree(mc);
3067 	}
3068 	return ret;
3069 }
3070 EXPORT_SYMBOL(rdma_join_multicast);
3071 
rdma_leave_multicast(struct rdma_cm_id * id,struct sockaddr * addr)3072 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3073 {
3074 	struct rdma_id_private *id_priv;
3075 	struct cma_multicast *mc;
3076 
3077 	id_priv = container_of(id, struct rdma_id_private, id);
3078 	spin_lock_irq(&id_priv->lock);
3079 	list_for_each_entry(mc, &id_priv->mc_list, list) {
3080 		if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3081 			list_del(&mc->list);
3082 			spin_unlock_irq(&id_priv->lock);
3083 
3084 			if (id->qp)
3085 				ib_detach_mcast(id->qp,
3086 						&mc->multicast.ib->rec.mgid,
3087 						mc->multicast.ib->rec.mlid);
3088 			if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3089 				switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3090 				case IB_LINK_LAYER_INFINIBAND:
3091 					ib_sa_free_multicast(mc->multicast.ib);
3092 					kfree(mc);
3093 					break;
3094 				case IB_LINK_LAYER_ETHERNET:
3095 					kref_put(&mc->mcref, release_mc);
3096 					break;
3097 				default:
3098 					break;
3099 				}
3100 			}
3101 			return;
3102 		}
3103 	}
3104 	spin_unlock_irq(&id_priv->lock);
3105 }
3106 EXPORT_SYMBOL(rdma_leave_multicast);
3107 
cma_netdev_change(struct net_device * ndev,struct rdma_id_private * id_priv)3108 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3109 {
3110 	struct rdma_dev_addr *dev_addr;
3111 	struct cma_ndev_work *work;
3112 
3113 	dev_addr = &id_priv->id.route.addr.dev_addr;
3114 
3115 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
3116 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3117 		printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3118 		       ndev->name, &id_priv->id);
3119 		work = kzalloc(sizeof *work, GFP_KERNEL);
3120 		if (!work)
3121 			return -ENOMEM;
3122 
3123 		INIT_WORK(&work->work, cma_ndev_work_handler);
3124 		work->id = id_priv;
3125 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3126 		atomic_inc(&id_priv->refcount);
3127 		queue_work(cma_wq, &work->work);
3128 	}
3129 
3130 	return 0;
3131 }
3132 
cma_netdev_callback(struct notifier_block * self,unsigned long event,void * ctx)3133 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3134 			       void *ctx)
3135 {
3136 	struct net_device *ndev = (struct net_device *)ctx;
3137 	struct cma_device *cma_dev;
3138 	struct rdma_id_private *id_priv;
3139 	int ret = NOTIFY_DONE;
3140 
3141 	if (dev_net(ndev) != &init_net)
3142 		return NOTIFY_DONE;
3143 
3144 	if (event != NETDEV_BONDING_FAILOVER)
3145 		return NOTIFY_DONE;
3146 
3147 	if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3148 		return NOTIFY_DONE;
3149 
3150 	mutex_lock(&lock);
3151 	list_for_each_entry(cma_dev, &dev_list, list)
3152 		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3153 			ret = cma_netdev_change(ndev, id_priv);
3154 			if (ret)
3155 				goto out;
3156 		}
3157 
3158 out:
3159 	mutex_unlock(&lock);
3160 	return ret;
3161 }
3162 
3163 static struct notifier_block cma_nb = {
3164 	.notifier_call = cma_netdev_callback
3165 };
3166 
cma_add_one(struct ib_device * device)3167 static void cma_add_one(struct ib_device *device)
3168 {
3169 	struct cma_device *cma_dev;
3170 	struct rdma_id_private *id_priv;
3171 
3172 	cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3173 	if (!cma_dev)
3174 		return;
3175 
3176 	cma_dev->device = device;
3177 
3178 	init_completion(&cma_dev->comp);
3179 	atomic_set(&cma_dev->refcount, 1);
3180 	INIT_LIST_HEAD(&cma_dev->id_list);
3181 	ib_set_client_data(device, &cma_client, cma_dev);
3182 
3183 	mutex_lock(&lock);
3184 	list_add_tail(&cma_dev->list, &dev_list);
3185 	list_for_each_entry(id_priv, &listen_any_list, list)
3186 		cma_listen_on_dev(id_priv, cma_dev);
3187 	mutex_unlock(&lock);
3188 }
3189 
cma_remove_id_dev(struct rdma_id_private * id_priv)3190 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3191 {
3192 	struct rdma_cm_event event;
3193 	enum cma_state state;
3194 	int ret = 0;
3195 
3196 	/* Record that we want to remove the device */
3197 	state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
3198 	if (state == CMA_DESTROYING)
3199 		return 0;
3200 
3201 	cma_cancel_operation(id_priv, state);
3202 	mutex_lock(&id_priv->handler_mutex);
3203 
3204 	/* Check for destruction from another callback. */
3205 	if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
3206 		goto out;
3207 
3208 	memset(&event, 0, sizeof event);
3209 	event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
3210 	ret = id_priv->id.event_handler(&id_priv->id, &event);
3211 out:
3212 	mutex_unlock(&id_priv->handler_mutex);
3213 	return ret;
3214 }
3215 
cma_process_remove(struct cma_device * cma_dev)3216 static void cma_process_remove(struct cma_device *cma_dev)
3217 {
3218 	struct rdma_id_private *id_priv;
3219 	int ret;
3220 
3221 	mutex_lock(&lock);
3222 	while (!list_empty(&cma_dev->id_list)) {
3223 		id_priv = list_entry(cma_dev->id_list.next,
3224 				     struct rdma_id_private, list);
3225 
3226 		list_del(&id_priv->listen_list);
3227 		list_del_init(&id_priv->list);
3228 		atomic_inc(&id_priv->refcount);
3229 		mutex_unlock(&lock);
3230 
3231 		ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
3232 		cma_deref_id(id_priv);
3233 		if (ret)
3234 			rdma_destroy_id(&id_priv->id);
3235 
3236 		mutex_lock(&lock);
3237 	}
3238 	mutex_unlock(&lock);
3239 
3240 	cma_deref_dev(cma_dev);
3241 	wait_for_completion(&cma_dev->comp);
3242 }
3243 
cma_remove_one(struct ib_device * device)3244 static void cma_remove_one(struct ib_device *device)
3245 {
3246 	struct cma_device *cma_dev;
3247 
3248 	cma_dev = ib_get_client_data(device, &cma_client);
3249 	if (!cma_dev)
3250 		return;
3251 
3252 	mutex_lock(&lock);
3253 	list_del(&cma_dev->list);
3254 	mutex_unlock(&lock);
3255 
3256 	cma_process_remove(cma_dev);
3257 	kfree(cma_dev);
3258 }
3259 
cma_init(void)3260 static int __init cma_init(void)
3261 {
3262 	int ret;
3263 
3264 	cma_wq = create_singlethread_workqueue("rdma_cm");
3265 	if (!cma_wq)
3266 		return -ENOMEM;
3267 
3268 	ib_sa_register_client(&sa_client);
3269 	rdma_addr_register_client(&addr_client);
3270 	register_netdevice_notifier(&cma_nb);
3271 
3272 	ret = ib_register_client(&cma_client);
3273 	if (ret)
3274 		goto err;
3275 	return 0;
3276 
3277 err:
3278 	unregister_netdevice_notifier(&cma_nb);
3279 	rdma_addr_unregister_client(&addr_client);
3280 	ib_sa_unregister_client(&sa_client);
3281 	destroy_workqueue(cma_wq);
3282 	return ret;
3283 }
3284 
cma_cleanup(void)3285 static void __exit cma_cleanup(void)
3286 {
3287 	ib_unregister_client(&cma_client);
3288 	unregister_netdevice_notifier(&cma_nb);
3289 	rdma_addr_unregister_client(&addr_client);
3290 	ib_sa_unregister_client(&sa_client);
3291 	destroy_workqueue(cma_wq);
3292 	idr_destroy(&sdp_ps);
3293 	idr_destroy(&tcp_ps);
3294 	idr_destroy(&udp_ps);
3295 	idr_destroy(&ipoib_ps);
3296 }
3297 
3298 module_init(cma_init);
3299 module_exit(cma_cleanup);
3300