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