xref: /openbmc/linux/drivers/infiniband/core/cm.c (revision 3e26a691)
1 /*
2  * Copyright (c) 2004-2007 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
4  * Copyright (c) 2004, 2005 Voltaire Corporation.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. 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/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
50 #include <linux/etherdevice.h>
51 
52 #include <rdma/ib_cache.h>
53 #include <rdma/ib_cm.h>
54 #include "cm_msgs.h"
55 
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("InfiniBand CM");
58 MODULE_LICENSE("Dual BSD/GPL");
59 
60 static void cm_add_one(struct ib_device *device);
61 static void cm_remove_one(struct ib_device *device, void *client_data);
62 
63 static struct ib_client cm_client = {
64 	.name   = "cm",
65 	.add    = cm_add_one,
66 	.remove = cm_remove_one
67 };
68 
69 static struct ib_cm {
70 	spinlock_t lock;
71 	struct list_head device_list;
72 	rwlock_t device_lock;
73 	struct rb_root listen_service_table;
74 	u64 listen_service_id;
75 	/* struct rb_root peer_service_table; todo: fix peer to peer */
76 	struct rb_root remote_qp_table;
77 	struct rb_root remote_id_table;
78 	struct rb_root remote_sidr_table;
79 	struct idr local_id_table;
80 	__be32 random_id_operand;
81 	struct list_head timewait_list;
82 	struct workqueue_struct *wq;
83 } cm;
84 
85 /* Counter indexes ordered by attribute ID */
86 enum {
87 	CM_REQ_COUNTER,
88 	CM_MRA_COUNTER,
89 	CM_REJ_COUNTER,
90 	CM_REP_COUNTER,
91 	CM_RTU_COUNTER,
92 	CM_DREQ_COUNTER,
93 	CM_DREP_COUNTER,
94 	CM_SIDR_REQ_COUNTER,
95 	CM_SIDR_REP_COUNTER,
96 	CM_LAP_COUNTER,
97 	CM_APR_COUNTER,
98 	CM_ATTR_COUNT,
99 	CM_ATTR_ID_OFFSET = 0x0010,
100 };
101 
102 enum {
103 	CM_XMIT,
104 	CM_XMIT_RETRIES,
105 	CM_RECV,
106 	CM_RECV_DUPLICATES,
107 	CM_COUNTER_GROUPS
108 };
109 
110 static char const counter_group_names[CM_COUNTER_GROUPS]
111 				     [sizeof("cm_rx_duplicates")] = {
112 	"cm_tx_msgs", "cm_tx_retries",
113 	"cm_rx_msgs", "cm_rx_duplicates"
114 };
115 
116 struct cm_counter_group {
117 	struct kobject obj;
118 	atomic_long_t counter[CM_ATTR_COUNT];
119 };
120 
121 struct cm_counter_attribute {
122 	struct attribute attr;
123 	int index;
124 };
125 
126 #define CM_COUNTER_ATTR(_name, _index) \
127 struct cm_counter_attribute cm_##_name##_counter_attr = { \
128 	.attr = { .name = __stringify(_name), .mode = 0444 }, \
129 	.index = _index \
130 }
131 
132 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
133 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
134 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
135 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
136 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
137 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
138 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
139 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
140 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
141 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
142 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
143 
144 static struct attribute *cm_counter_default_attrs[] = {
145 	&cm_req_counter_attr.attr,
146 	&cm_mra_counter_attr.attr,
147 	&cm_rej_counter_attr.attr,
148 	&cm_rep_counter_attr.attr,
149 	&cm_rtu_counter_attr.attr,
150 	&cm_dreq_counter_attr.attr,
151 	&cm_drep_counter_attr.attr,
152 	&cm_sidr_req_counter_attr.attr,
153 	&cm_sidr_rep_counter_attr.attr,
154 	&cm_lap_counter_attr.attr,
155 	&cm_apr_counter_attr.attr,
156 	NULL
157 };
158 
159 struct cm_port {
160 	struct cm_device *cm_dev;
161 	struct ib_mad_agent *mad_agent;
162 	struct kobject port_obj;
163 	u8 port_num;
164 	struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
165 };
166 
167 struct cm_device {
168 	struct list_head list;
169 	struct ib_device *ib_device;
170 	struct device *device;
171 	u8 ack_delay;
172 	int going_down;
173 	struct cm_port *port[0];
174 };
175 
176 struct cm_av {
177 	struct cm_port *port;
178 	union ib_gid dgid;
179 	struct ib_ah_attr ah_attr;
180 	u16 pkey_index;
181 	u8 timeout;
182 };
183 
184 struct cm_work {
185 	struct delayed_work work;
186 	struct list_head list;
187 	struct cm_port *port;
188 	struct ib_mad_recv_wc *mad_recv_wc;	/* Received MADs */
189 	__be32 local_id;			/* Established / timewait */
190 	__be32 remote_id;
191 	struct ib_cm_event cm_event;
192 	struct ib_sa_path_rec path[0];
193 };
194 
195 struct cm_timewait_info {
196 	struct cm_work work;			/* Must be first. */
197 	struct list_head list;
198 	struct rb_node remote_qp_node;
199 	struct rb_node remote_id_node;
200 	__be64 remote_ca_guid;
201 	__be32 remote_qpn;
202 	u8 inserted_remote_qp;
203 	u8 inserted_remote_id;
204 };
205 
206 struct cm_id_private {
207 	struct ib_cm_id	id;
208 
209 	struct rb_node service_node;
210 	struct rb_node sidr_id_node;
211 	spinlock_t lock;	/* Do not acquire inside cm.lock */
212 	struct completion comp;
213 	atomic_t refcount;
214 	/* Number of clients sharing this ib_cm_id. Only valid for listeners.
215 	 * Protected by the cm.lock spinlock. */
216 	int listen_sharecount;
217 
218 	struct ib_mad_send_buf *msg;
219 	struct cm_timewait_info *timewait_info;
220 	/* todo: use alternate port on send failure */
221 	struct cm_av av;
222 	struct cm_av alt_av;
223 
224 	void *private_data;
225 	__be64 tid;
226 	__be32 local_qpn;
227 	__be32 remote_qpn;
228 	enum ib_qp_type qp_type;
229 	__be32 sq_psn;
230 	__be32 rq_psn;
231 	int timeout_ms;
232 	enum ib_mtu path_mtu;
233 	__be16 pkey;
234 	u8 private_data_len;
235 	u8 max_cm_retries;
236 	u8 peer_to_peer;
237 	u8 responder_resources;
238 	u8 initiator_depth;
239 	u8 retry_count;
240 	u8 rnr_retry_count;
241 	u8 service_timeout;
242 	u8 target_ack_delay;
243 
244 	struct list_head work_list;
245 	atomic_t work_count;
246 };
247 
248 static void cm_work_handler(struct work_struct *work);
249 
250 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
251 {
252 	if (atomic_dec_and_test(&cm_id_priv->refcount))
253 		complete(&cm_id_priv->comp);
254 }
255 
256 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
257 			struct ib_mad_send_buf **msg)
258 {
259 	struct ib_mad_agent *mad_agent;
260 	struct ib_mad_send_buf *m;
261 	struct ib_ah *ah;
262 
263 	mad_agent = cm_id_priv->av.port->mad_agent;
264 	ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
265 	if (IS_ERR(ah))
266 		return PTR_ERR(ah);
267 
268 	m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
269 			       cm_id_priv->av.pkey_index,
270 			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
271 			       GFP_ATOMIC,
272 			       IB_MGMT_BASE_VERSION);
273 	if (IS_ERR(m)) {
274 		ib_destroy_ah(ah);
275 		return PTR_ERR(m);
276 	}
277 
278 	/* Timeout set by caller if response is expected. */
279 	m->ah = ah;
280 	m->retries = cm_id_priv->max_cm_retries;
281 
282 	atomic_inc(&cm_id_priv->refcount);
283 	m->context[0] = cm_id_priv;
284 	*msg = m;
285 	return 0;
286 }
287 
288 static int cm_alloc_response_msg(struct cm_port *port,
289 				 struct ib_mad_recv_wc *mad_recv_wc,
290 				 struct ib_mad_send_buf **msg)
291 {
292 	struct ib_mad_send_buf *m;
293 	struct ib_ah *ah;
294 
295 	ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
296 				  mad_recv_wc->recv_buf.grh, port->port_num);
297 	if (IS_ERR(ah))
298 		return PTR_ERR(ah);
299 
300 	m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
301 			       0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
302 			       GFP_ATOMIC,
303 			       IB_MGMT_BASE_VERSION);
304 	if (IS_ERR(m)) {
305 		ib_destroy_ah(ah);
306 		return PTR_ERR(m);
307 	}
308 	m->ah = ah;
309 	*msg = m;
310 	return 0;
311 }
312 
313 static void cm_free_msg(struct ib_mad_send_buf *msg)
314 {
315 	ib_destroy_ah(msg->ah);
316 	if (msg->context[0])
317 		cm_deref_id(msg->context[0]);
318 	ib_free_send_mad(msg);
319 }
320 
321 static void * cm_copy_private_data(const void *private_data,
322 				   u8 private_data_len)
323 {
324 	void *data;
325 
326 	if (!private_data || !private_data_len)
327 		return NULL;
328 
329 	data = kmemdup(private_data, private_data_len, GFP_KERNEL);
330 	if (!data)
331 		return ERR_PTR(-ENOMEM);
332 
333 	return data;
334 }
335 
336 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
337 				 void *private_data, u8 private_data_len)
338 {
339 	if (cm_id_priv->private_data && cm_id_priv->private_data_len)
340 		kfree(cm_id_priv->private_data);
341 
342 	cm_id_priv->private_data = private_data;
343 	cm_id_priv->private_data_len = private_data_len;
344 }
345 
346 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
347 				    struct ib_grh *grh, struct cm_av *av)
348 {
349 	av->port = port;
350 	av->pkey_index = wc->pkey_index;
351 	ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
352 			   grh, &av->ah_attr);
353 }
354 
355 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
356 {
357 	struct cm_device *cm_dev;
358 	struct cm_port *port = NULL;
359 	unsigned long flags;
360 	int ret;
361 	u8 p;
362 	struct net_device *ndev = ib_get_ndev_from_path(path);
363 
364 	read_lock_irqsave(&cm.device_lock, flags);
365 	list_for_each_entry(cm_dev, &cm.device_list, list) {
366 		if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
367 					path->gid_type, ndev, &p, NULL)) {
368 			port = cm_dev->port[p-1];
369 			break;
370 		}
371 	}
372 	read_unlock_irqrestore(&cm.device_lock, flags);
373 
374 	if (ndev)
375 		dev_put(ndev);
376 
377 	if (!port)
378 		return -EINVAL;
379 
380 	ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
381 				  be16_to_cpu(path->pkey), &av->pkey_index);
382 	if (ret)
383 		return ret;
384 
385 	av->port = port;
386 	ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
387 			     &av->ah_attr);
388 	av->timeout = path->packet_life_time + 1;
389 
390 	return 0;
391 }
392 
393 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
394 {
395 	unsigned long flags;
396 	int id;
397 
398 	idr_preload(GFP_KERNEL);
399 	spin_lock_irqsave(&cm.lock, flags);
400 
401 	id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
402 
403 	spin_unlock_irqrestore(&cm.lock, flags);
404 	idr_preload_end();
405 
406 	cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
407 	return id < 0 ? id : 0;
408 }
409 
410 static void cm_free_id(__be32 local_id)
411 {
412 	spin_lock_irq(&cm.lock);
413 	idr_remove(&cm.local_id_table,
414 		   (__force int) (local_id ^ cm.random_id_operand));
415 	spin_unlock_irq(&cm.lock);
416 }
417 
418 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
419 {
420 	struct cm_id_private *cm_id_priv;
421 
422 	cm_id_priv = idr_find(&cm.local_id_table,
423 			      (__force int) (local_id ^ cm.random_id_operand));
424 	if (cm_id_priv) {
425 		if (cm_id_priv->id.remote_id == remote_id)
426 			atomic_inc(&cm_id_priv->refcount);
427 		else
428 			cm_id_priv = NULL;
429 	}
430 
431 	return cm_id_priv;
432 }
433 
434 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
435 {
436 	struct cm_id_private *cm_id_priv;
437 
438 	spin_lock_irq(&cm.lock);
439 	cm_id_priv = cm_get_id(local_id, remote_id);
440 	spin_unlock_irq(&cm.lock);
441 
442 	return cm_id_priv;
443 }
444 
445 /*
446  * Trivial helpers to strip endian annotation and compare; the
447  * endianness doesn't actually matter since we just need a stable
448  * order for the RB tree.
449  */
450 static int be32_lt(__be32 a, __be32 b)
451 {
452 	return (__force u32) a < (__force u32) b;
453 }
454 
455 static int be32_gt(__be32 a, __be32 b)
456 {
457 	return (__force u32) a > (__force u32) b;
458 }
459 
460 static int be64_lt(__be64 a, __be64 b)
461 {
462 	return (__force u64) a < (__force u64) b;
463 }
464 
465 static int be64_gt(__be64 a, __be64 b)
466 {
467 	return (__force u64) a > (__force u64) b;
468 }
469 
470 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
471 {
472 	struct rb_node **link = &cm.listen_service_table.rb_node;
473 	struct rb_node *parent = NULL;
474 	struct cm_id_private *cur_cm_id_priv;
475 	__be64 service_id = cm_id_priv->id.service_id;
476 	__be64 service_mask = cm_id_priv->id.service_mask;
477 
478 	while (*link) {
479 		parent = *link;
480 		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
481 					  service_node);
482 		if ((cur_cm_id_priv->id.service_mask & service_id) ==
483 		    (service_mask & cur_cm_id_priv->id.service_id) &&
484 		    (cm_id_priv->id.device == cur_cm_id_priv->id.device))
485 			return cur_cm_id_priv;
486 
487 		if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
488 			link = &(*link)->rb_left;
489 		else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
490 			link = &(*link)->rb_right;
491 		else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
492 			link = &(*link)->rb_left;
493 		else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
494 			link = &(*link)->rb_right;
495 		else
496 			link = &(*link)->rb_right;
497 	}
498 	rb_link_node(&cm_id_priv->service_node, parent, link);
499 	rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
500 	return NULL;
501 }
502 
503 static struct cm_id_private * cm_find_listen(struct ib_device *device,
504 					     __be64 service_id)
505 {
506 	struct rb_node *node = cm.listen_service_table.rb_node;
507 	struct cm_id_private *cm_id_priv;
508 
509 	while (node) {
510 		cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
511 		if ((cm_id_priv->id.service_mask & service_id) ==
512 		     cm_id_priv->id.service_id &&
513 		    (cm_id_priv->id.device == device))
514 			return cm_id_priv;
515 
516 		if (device < cm_id_priv->id.device)
517 			node = node->rb_left;
518 		else if (device > cm_id_priv->id.device)
519 			node = node->rb_right;
520 		else if (be64_lt(service_id, cm_id_priv->id.service_id))
521 			node = node->rb_left;
522 		else if (be64_gt(service_id, cm_id_priv->id.service_id))
523 			node = node->rb_right;
524 		else
525 			node = node->rb_right;
526 	}
527 	return NULL;
528 }
529 
530 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
531 						     *timewait_info)
532 {
533 	struct rb_node **link = &cm.remote_id_table.rb_node;
534 	struct rb_node *parent = NULL;
535 	struct cm_timewait_info *cur_timewait_info;
536 	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
537 	__be32 remote_id = timewait_info->work.remote_id;
538 
539 	while (*link) {
540 		parent = *link;
541 		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
542 					     remote_id_node);
543 		if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
544 			link = &(*link)->rb_left;
545 		else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
546 			link = &(*link)->rb_right;
547 		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
548 			link = &(*link)->rb_left;
549 		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
550 			link = &(*link)->rb_right;
551 		else
552 			return cur_timewait_info;
553 	}
554 	timewait_info->inserted_remote_id = 1;
555 	rb_link_node(&timewait_info->remote_id_node, parent, link);
556 	rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
557 	return NULL;
558 }
559 
560 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
561 						   __be32 remote_id)
562 {
563 	struct rb_node *node = cm.remote_id_table.rb_node;
564 	struct cm_timewait_info *timewait_info;
565 
566 	while (node) {
567 		timewait_info = rb_entry(node, struct cm_timewait_info,
568 					 remote_id_node);
569 		if (be32_lt(remote_id, timewait_info->work.remote_id))
570 			node = node->rb_left;
571 		else if (be32_gt(remote_id, timewait_info->work.remote_id))
572 			node = node->rb_right;
573 		else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
574 			node = node->rb_left;
575 		else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
576 			node = node->rb_right;
577 		else
578 			return timewait_info;
579 	}
580 	return NULL;
581 }
582 
583 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
584 						      *timewait_info)
585 {
586 	struct rb_node **link = &cm.remote_qp_table.rb_node;
587 	struct rb_node *parent = NULL;
588 	struct cm_timewait_info *cur_timewait_info;
589 	__be64 remote_ca_guid = timewait_info->remote_ca_guid;
590 	__be32 remote_qpn = timewait_info->remote_qpn;
591 
592 	while (*link) {
593 		parent = *link;
594 		cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
595 					     remote_qp_node);
596 		if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
597 			link = &(*link)->rb_left;
598 		else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
599 			link = &(*link)->rb_right;
600 		else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
601 			link = &(*link)->rb_left;
602 		else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
603 			link = &(*link)->rb_right;
604 		else
605 			return cur_timewait_info;
606 	}
607 	timewait_info->inserted_remote_qp = 1;
608 	rb_link_node(&timewait_info->remote_qp_node, parent, link);
609 	rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
610 	return NULL;
611 }
612 
613 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
614 						    *cm_id_priv)
615 {
616 	struct rb_node **link = &cm.remote_sidr_table.rb_node;
617 	struct rb_node *parent = NULL;
618 	struct cm_id_private *cur_cm_id_priv;
619 	union ib_gid *port_gid = &cm_id_priv->av.dgid;
620 	__be32 remote_id = cm_id_priv->id.remote_id;
621 
622 	while (*link) {
623 		parent = *link;
624 		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
625 					  sidr_id_node);
626 		if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
627 			link = &(*link)->rb_left;
628 		else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
629 			link = &(*link)->rb_right;
630 		else {
631 			int cmp;
632 			cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
633 				     sizeof *port_gid);
634 			if (cmp < 0)
635 				link = &(*link)->rb_left;
636 			else if (cmp > 0)
637 				link = &(*link)->rb_right;
638 			else
639 				return cur_cm_id_priv;
640 		}
641 	}
642 	rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
643 	rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
644 	return NULL;
645 }
646 
647 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
648 			       enum ib_cm_sidr_status status)
649 {
650 	struct ib_cm_sidr_rep_param param;
651 
652 	memset(&param, 0, sizeof param);
653 	param.status = status;
654 	ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
655 }
656 
657 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
658 				 ib_cm_handler cm_handler,
659 				 void *context)
660 {
661 	struct cm_id_private *cm_id_priv;
662 	int ret;
663 
664 	cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
665 	if (!cm_id_priv)
666 		return ERR_PTR(-ENOMEM);
667 
668 	cm_id_priv->id.state = IB_CM_IDLE;
669 	cm_id_priv->id.device = device;
670 	cm_id_priv->id.cm_handler = cm_handler;
671 	cm_id_priv->id.context = context;
672 	cm_id_priv->id.remote_cm_qpn = 1;
673 	ret = cm_alloc_id(cm_id_priv);
674 	if (ret)
675 		goto error;
676 
677 	spin_lock_init(&cm_id_priv->lock);
678 	init_completion(&cm_id_priv->comp);
679 	INIT_LIST_HEAD(&cm_id_priv->work_list);
680 	atomic_set(&cm_id_priv->work_count, -1);
681 	atomic_set(&cm_id_priv->refcount, 1);
682 	return &cm_id_priv->id;
683 
684 error:
685 	kfree(cm_id_priv);
686 	return ERR_PTR(-ENOMEM);
687 }
688 EXPORT_SYMBOL(ib_create_cm_id);
689 
690 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
691 {
692 	struct cm_work *work;
693 
694 	if (list_empty(&cm_id_priv->work_list))
695 		return NULL;
696 
697 	work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
698 	list_del(&work->list);
699 	return work;
700 }
701 
702 static void cm_free_work(struct cm_work *work)
703 {
704 	if (work->mad_recv_wc)
705 		ib_free_recv_mad(work->mad_recv_wc);
706 	kfree(work);
707 }
708 
709 static inline int cm_convert_to_ms(int iba_time)
710 {
711 	/* approximate conversion to ms from 4.096us x 2^iba_time */
712 	return 1 << max(iba_time - 8, 0);
713 }
714 
715 /*
716  * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
717  * Because of how ack_timeout is stored, adding one doubles the timeout.
718  * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
719  * increment it (round up) only if the other is within 50%.
720  */
721 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
722 {
723 	int ack_timeout = packet_life_time + 1;
724 
725 	if (ack_timeout >= ca_ack_delay)
726 		ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
727 	else
728 		ack_timeout = ca_ack_delay +
729 			      (ack_timeout >= (ca_ack_delay - 1));
730 
731 	return min(31, ack_timeout);
732 }
733 
734 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
735 {
736 	if (timewait_info->inserted_remote_id) {
737 		rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
738 		timewait_info->inserted_remote_id = 0;
739 	}
740 
741 	if (timewait_info->inserted_remote_qp) {
742 		rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
743 		timewait_info->inserted_remote_qp = 0;
744 	}
745 }
746 
747 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
748 {
749 	struct cm_timewait_info *timewait_info;
750 
751 	timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
752 	if (!timewait_info)
753 		return ERR_PTR(-ENOMEM);
754 
755 	timewait_info->work.local_id = local_id;
756 	INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
757 	timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
758 	return timewait_info;
759 }
760 
761 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
762 {
763 	int wait_time;
764 	unsigned long flags;
765 	struct cm_device *cm_dev;
766 
767 	cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
768 	if (!cm_dev)
769 		return;
770 
771 	spin_lock_irqsave(&cm.lock, flags);
772 	cm_cleanup_timewait(cm_id_priv->timewait_info);
773 	list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
774 	spin_unlock_irqrestore(&cm.lock, flags);
775 
776 	/*
777 	 * The cm_id could be destroyed by the user before we exit timewait.
778 	 * To protect against this, we search for the cm_id after exiting
779 	 * timewait before notifying the user that we've exited timewait.
780 	 */
781 	cm_id_priv->id.state = IB_CM_TIMEWAIT;
782 	wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
783 
784 	/* Check if the device started its remove_one */
785 	spin_lock_irqsave(&cm.lock, flags);
786 	if (!cm_dev->going_down)
787 		queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
788 				   msecs_to_jiffies(wait_time));
789 	spin_unlock_irqrestore(&cm.lock, flags);
790 
791 	cm_id_priv->timewait_info = NULL;
792 }
793 
794 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
795 {
796 	unsigned long flags;
797 
798 	cm_id_priv->id.state = IB_CM_IDLE;
799 	if (cm_id_priv->timewait_info) {
800 		spin_lock_irqsave(&cm.lock, flags);
801 		cm_cleanup_timewait(cm_id_priv->timewait_info);
802 		spin_unlock_irqrestore(&cm.lock, flags);
803 		kfree(cm_id_priv->timewait_info);
804 		cm_id_priv->timewait_info = NULL;
805 	}
806 }
807 
808 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
809 {
810 	struct cm_id_private *cm_id_priv;
811 	struct cm_work *work;
812 
813 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
814 retest:
815 	spin_lock_irq(&cm_id_priv->lock);
816 	switch (cm_id->state) {
817 	case IB_CM_LISTEN:
818 		spin_unlock_irq(&cm_id_priv->lock);
819 
820 		spin_lock_irq(&cm.lock);
821 		if (--cm_id_priv->listen_sharecount > 0) {
822 			/* The id is still shared. */
823 			cm_deref_id(cm_id_priv);
824 			spin_unlock_irq(&cm.lock);
825 			return;
826 		}
827 		rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
828 		spin_unlock_irq(&cm.lock);
829 		break;
830 	case IB_CM_SIDR_REQ_SENT:
831 		cm_id->state = IB_CM_IDLE;
832 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
833 		spin_unlock_irq(&cm_id_priv->lock);
834 		break;
835 	case IB_CM_SIDR_REQ_RCVD:
836 		spin_unlock_irq(&cm_id_priv->lock);
837 		cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
838 		spin_lock_irq(&cm.lock);
839 		if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
840 			rb_erase(&cm_id_priv->sidr_id_node,
841 				 &cm.remote_sidr_table);
842 		spin_unlock_irq(&cm.lock);
843 		break;
844 	case IB_CM_REQ_SENT:
845 	case IB_CM_MRA_REQ_RCVD:
846 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
847 		spin_unlock_irq(&cm_id_priv->lock);
848 		ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
849 			       &cm_id_priv->id.device->node_guid,
850 			       sizeof cm_id_priv->id.device->node_guid,
851 			       NULL, 0);
852 		break;
853 	case IB_CM_REQ_RCVD:
854 		if (err == -ENOMEM) {
855 			/* Do not reject to allow future retries. */
856 			cm_reset_to_idle(cm_id_priv);
857 			spin_unlock_irq(&cm_id_priv->lock);
858 		} else {
859 			spin_unlock_irq(&cm_id_priv->lock);
860 			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
861 				       NULL, 0, NULL, 0);
862 		}
863 		break;
864 	case IB_CM_REP_SENT:
865 	case IB_CM_MRA_REP_RCVD:
866 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
867 		/* Fall through */
868 	case IB_CM_MRA_REQ_SENT:
869 	case IB_CM_REP_RCVD:
870 	case IB_CM_MRA_REP_SENT:
871 		spin_unlock_irq(&cm_id_priv->lock);
872 		ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
873 			       NULL, 0, NULL, 0);
874 		break;
875 	case IB_CM_ESTABLISHED:
876 		spin_unlock_irq(&cm_id_priv->lock);
877 		if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
878 			break;
879 		ib_send_cm_dreq(cm_id, NULL, 0);
880 		goto retest;
881 	case IB_CM_DREQ_SENT:
882 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
883 		cm_enter_timewait(cm_id_priv);
884 		spin_unlock_irq(&cm_id_priv->lock);
885 		break;
886 	case IB_CM_DREQ_RCVD:
887 		spin_unlock_irq(&cm_id_priv->lock);
888 		ib_send_cm_drep(cm_id, NULL, 0);
889 		break;
890 	default:
891 		spin_unlock_irq(&cm_id_priv->lock);
892 		break;
893 	}
894 
895 	cm_free_id(cm_id->local_id);
896 	cm_deref_id(cm_id_priv);
897 	wait_for_completion(&cm_id_priv->comp);
898 	while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
899 		cm_free_work(work);
900 	kfree(cm_id_priv->private_data);
901 	kfree(cm_id_priv);
902 }
903 
904 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
905 {
906 	cm_destroy_id(cm_id, 0);
907 }
908 EXPORT_SYMBOL(ib_destroy_cm_id);
909 
910 /**
911  * __ib_cm_listen - Initiates listening on the specified service ID for
912  *   connection and service ID resolution requests.
913  * @cm_id: Connection identifier associated with the listen request.
914  * @service_id: Service identifier matched against incoming connection
915  *   and service ID resolution requests.  The service ID should be specified
916  *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
917  *   assign a service ID to the caller.
918  * @service_mask: Mask applied to service ID used to listen across a
919  *   range of service IDs.  If set to 0, the service ID is matched
920  *   exactly.  This parameter is ignored if %service_id is set to
921  *   IB_CM_ASSIGN_SERVICE_ID.
922  */
923 static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
924 			  __be64 service_mask)
925 {
926 	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
927 	int ret = 0;
928 
929 	service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
930 	service_id &= service_mask;
931 	if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
932 	    (service_id != IB_CM_ASSIGN_SERVICE_ID))
933 		return -EINVAL;
934 
935 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
936 	if (cm_id->state != IB_CM_IDLE)
937 		return -EINVAL;
938 
939 	cm_id->state = IB_CM_LISTEN;
940 	++cm_id_priv->listen_sharecount;
941 
942 	if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
943 		cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
944 		cm_id->service_mask = ~cpu_to_be64(0);
945 	} else {
946 		cm_id->service_id = service_id;
947 		cm_id->service_mask = service_mask;
948 	}
949 	cur_cm_id_priv = cm_insert_listen(cm_id_priv);
950 
951 	if (cur_cm_id_priv) {
952 		cm_id->state = IB_CM_IDLE;
953 		--cm_id_priv->listen_sharecount;
954 		ret = -EBUSY;
955 	}
956 	return ret;
957 }
958 
959 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
960 {
961 	unsigned long flags;
962 	int ret;
963 
964 	spin_lock_irqsave(&cm.lock, flags);
965 	ret = __ib_cm_listen(cm_id, service_id, service_mask);
966 	spin_unlock_irqrestore(&cm.lock, flags);
967 
968 	return ret;
969 }
970 EXPORT_SYMBOL(ib_cm_listen);
971 
972 /**
973  * Create a new listening ib_cm_id and listen on the given service ID.
974  *
975  * If there's an existing ID listening on that same device and service ID,
976  * return it.
977  *
978  * @device: Device associated with the cm_id.  All related communication will
979  * be associated with the specified device.
980  * @cm_handler: Callback invoked to notify the user of CM events.
981  * @service_id: Service identifier matched against incoming connection
982  *   and service ID resolution requests.  The service ID should be specified
983  *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
984  *   assign a service ID to the caller.
985  *
986  * Callers should call ib_destroy_cm_id when done with the listener ID.
987  */
988 struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
989 				     ib_cm_handler cm_handler,
990 				     __be64 service_id)
991 {
992 	struct cm_id_private *cm_id_priv;
993 	struct ib_cm_id *cm_id;
994 	unsigned long flags;
995 	int err = 0;
996 
997 	/* Create an ID in advance, since the creation may sleep */
998 	cm_id = ib_create_cm_id(device, cm_handler, NULL);
999 	if (IS_ERR(cm_id))
1000 		return cm_id;
1001 
1002 	spin_lock_irqsave(&cm.lock, flags);
1003 
1004 	if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1005 		goto new_id;
1006 
1007 	/* Find an existing ID */
1008 	cm_id_priv = cm_find_listen(device, service_id);
1009 	if (cm_id_priv) {
1010 		if (cm_id->cm_handler != cm_handler || cm_id->context) {
1011 			/* Sharing an ib_cm_id with different handlers is not
1012 			 * supported */
1013 			spin_unlock_irqrestore(&cm.lock, flags);
1014 			return ERR_PTR(-EINVAL);
1015 		}
1016 		atomic_inc(&cm_id_priv->refcount);
1017 		++cm_id_priv->listen_sharecount;
1018 		spin_unlock_irqrestore(&cm.lock, flags);
1019 
1020 		ib_destroy_cm_id(cm_id);
1021 		cm_id = &cm_id_priv->id;
1022 		return cm_id;
1023 	}
1024 
1025 new_id:
1026 	/* Use newly created ID */
1027 	err = __ib_cm_listen(cm_id, service_id, 0);
1028 
1029 	spin_unlock_irqrestore(&cm.lock, flags);
1030 
1031 	if (err) {
1032 		ib_destroy_cm_id(cm_id);
1033 		return ERR_PTR(err);
1034 	}
1035 	return cm_id;
1036 }
1037 EXPORT_SYMBOL(ib_cm_insert_listen);
1038 
1039 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1040 			  enum cm_msg_sequence msg_seq)
1041 {
1042 	u64 hi_tid, low_tid;
1043 
1044 	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1045 	low_tid  = (u64) ((__force u32)cm_id_priv->id.local_id |
1046 			  (msg_seq << 30));
1047 	return cpu_to_be64(hi_tid | low_tid);
1048 }
1049 
1050 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1051 			      __be16 attr_id, __be64 tid)
1052 {
1053 	hdr->base_version  = IB_MGMT_BASE_VERSION;
1054 	hdr->mgmt_class	   = IB_MGMT_CLASS_CM;
1055 	hdr->class_version = IB_CM_CLASS_VERSION;
1056 	hdr->method	   = IB_MGMT_METHOD_SEND;
1057 	hdr->attr_id	   = attr_id;
1058 	hdr->tid	   = tid;
1059 }
1060 
1061 static void cm_format_req(struct cm_req_msg *req_msg,
1062 			  struct cm_id_private *cm_id_priv,
1063 			  struct ib_cm_req_param *param)
1064 {
1065 	struct ib_sa_path_rec *pri_path = param->primary_path;
1066 	struct ib_sa_path_rec *alt_path = param->alternate_path;
1067 
1068 	cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1069 			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1070 
1071 	req_msg->local_comm_id = cm_id_priv->id.local_id;
1072 	req_msg->service_id = param->service_id;
1073 	req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1074 	cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1075 	cm_req_set_init_depth(req_msg, param->initiator_depth);
1076 	cm_req_set_remote_resp_timeout(req_msg,
1077 				       param->remote_cm_response_timeout);
1078 	cm_req_set_qp_type(req_msg, param->qp_type);
1079 	cm_req_set_flow_ctrl(req_msg, param->flow_control);
1080 	cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1081 	cm_req_set_local_resp_timeout(req_msg,
1082 				      param->local_cm_response_timeout);
1083 	req_msg->pkey = param->primary_path->pkey;
1084 	cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1085 	cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1086 
1087 	if (param->qp_type != IB_QPT_XRC_INI) {
1088 		cm_req_set_resp_res(req_msg, param->responder_resources);
1089 		cm_req_set_retry_count(req_msg, param->retry_count);
1090 		cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1091 		cm_req_set_srq(req_msg, param->srq);
1092 	}
1093 
1094 	if (pri_path->hop_limit <= 1) {
1095 		req_msg->primary_local_lid = pri_path->slid;
1096 		req_msg->primary_remote_lid = pri_path->dlid;
1097 	} else {
1098 		/* Work-around until there's a way to obtain remote LID info */
1099 		req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1100 		req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1101 	}
1102 	req_msg->primary_local_gid = pri_path->sgid;
1103 	req_msg->primary_remote_gid = pri_path->dgid;
1104 	cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1105 	cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1106 	req_msg->primary_traffic_class = pri_path->traffic_class;
1107 	req_msg->primary_hop_limit = pri_path->hop_limit;
1108 	cm_req_set_primary_sl(req_msg, pri_path->sl);
1109 	cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1110 	cm_req_set_primary_local_ack_timeout(req_msg,
1111 		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1112 			       pri_path->packet_life_time));
1113 
1114 	if (alt_path) {
1115 		if (alt_path->hop_limit <= 1) {
1116 			req_msg->alt_local_lid = alt_path->slid;
1117 			req_msg->alt_remote_lid = alt_path->dlid;
1118 		} else {
1119 			req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1120 			req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1121 		}
1122 		req_msg->alt_local_gid = alt_path->sgid;
1123 		req_msg->alt_remote_gid = alt_path->dgid;
1124 		cm_req_set_alt_flow_label(req_msg,
1125 					  alt_path->flow_label);
1126 		cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1127 		req_msg->alt_traffic_class = alt_path->traffic_class;
1128 		req_msg->alt_hop_limit = alt_path->hop_limit;
1129 		cm_req_set_alt_sl(req_msg, alt_path->sl);
1130 		cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1131 		cm_req_set_alt_local_ack_timeout(req_msg,
1132 			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1133 				       alt_path->packet_life_time));
1134 	}
1135 
1136 	if (param->private_data && param->private_data_len)
1137 		memcpy(req_msg->private_data, param->private_data,
1138 		       param->private_data_len);
1139 }
1140 
1141 static int cm_validate_req_param(struct ib_cm_req_param *param)
1142 {
1143 	/* peer-to-peer not supported */
1144 	if (param->peer_to_peer)
1145 		return -EINVAL;
1146 
1147 	if (!param->primary_path)
1148 		return -EINVAL;
1149 
1150 	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1151 	    param->qp_type != IB_QPT_XRC_INI)
1152 		return -EINVAL;
1153 
1154 	if (param->private_data &&
1155 	    param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1156 		return -EINVAL;
1157 
1158 	if (param->alternate_path &&
1159 	    (param->alternate_path->pkey != param->primary_path->pkey ||
1160 	     param->alternate_path->mtu != param->primary_path->mtu))
1161 		return -EINVAL;
1162 
1163 	return 0;
1164 }
1165 
1166 int ib_send_cm_req(struct ib_cm_id *cm_id,
1167 		   struct ib_cm_req_param *param)
1168 {
1169 	struct cm_id_private *cm_id_priv;
1170 	struct cm_req_msg *req_msg;
1171 	unsigned long flags;
1172 	int ret;
1173 
1174 	ret = cm_validate_req_param(param);
1175 	if (ret)
1176 		return ret;
1177 
1178 	/* Verify that we're not in timewait. */
1179 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1180 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1181 	if (cm_id->state != IB_CM_IDLE) {
1182 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1183 		ret = -EINVAL;
1184 		goto out;
1185 	}
1186 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1187 
1188 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1189 							    id.local_id);
1190 	if (IS_ERR(cm_id_priv->timewait_info)) {
1191 		ret = PTR_ERR(cm_id_priv->timewait_info);
1192 		goto out;
1193 	}
1194 
1195 	ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1196 	if (ret)
1197 		goto error1;
1198 	if (param->alternate_path) {
1199 		ret = cm_init_av_by_path(param->alternate_path,
1200 					 &cm_id_priv->alt_av);
1201 		if (ret)
1202 			goto error1;
1203 	}
1204 	cm_id->service_id = param->service_id;
1205 	cm_id->service_mask = ~cpu_to_be64(0);
1206 	cm_id_priv->timeout_ms = cm_convert_to_ms(
1207 				    param->primary_path->packet_life_time) * 2 +
1208 				 cm_convert_to_ms(
1209 				    param->remote_cm_response_timeout);
1210 	cm_id_priv->max_cm_retries = param->max_cm_retries;
1211 	cm_id_priv->initiator_depth = param->initiator_depth;
1212 	cm_id_priv->responder_resources = param->responder_resources;
1213 	cm_id_priv->retry_count = param->retry_count;
1214 	cm_id_priv->path_mtu = param->primary_path->mtu;
1215 	cm_id_priv->pkey = param->primary_path->pkey;
1216 	cm_id_priv->qp_type = param->qp_type;
1217 
1218 	ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1219 	if (ret)
1220 		goto error1;
1221 
1222 	req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1223 	cm_format_req(req_msg, cm_id_priv, param);
1224 	cm_id_priv->tid = req_msg->hdr.tid;
1225 	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1226 	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1227 
1228 	cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1229 	cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1230 
1231 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1232 	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1233 	if (ret) {
1234 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1235 		goto error2;
1236 	}
1237 	BUG_ON(cm_id->state != IB_CM_IDLE);
1238 	cm_id->state = IB_CM_REQ_SENT;
1239 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1240 	return 0;
1241 
1242 error2:	cm_free_msg(cm_id_priv->msg);
1243 error1:	kfree(cm_id_priv->timewait_info);
1244 out:	return ret;
1245 }
1246 EXPORT_SYMBOL(ib_send_cm_req);
1247 
1248 static int cm_issue_rej(struct cm_port *port,
1249 			struct ib_mad_recv_wc *mad_recv_wc,
1250 			enum ib_cm_rej_reason reason,
1251 			enum cm_msg_response msg_rejected,
1252 			void *ari, u8 ari_length)
1253 {
1254 	struct ib_mad_send_buf *msg = NULL;
1255 	struct cm_rej_msg *rej_msg, *rcv_msg;
1256 	int ret;
1257 
1258 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1259 	if (ret)
1260 		return ret;
1261 
1262 	/* We just need common CM header information.  Cast to any message. */
1263 	rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1264 	rej_msg = (struct cm_rej_msg *) msg->mad;
1265 
1266 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1267 	rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1268 	rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1269 	cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1270 	rej_msg->reason = cpu_to_be16(reason);
1271 
1272 	if (ari && ari_length) {
1273 		cm_rej_set_reject_info_len(rej_msg, ari_length);
1274 		memcpy(rej_msg->ari, ari, ari_length);
1275 	}
1276 
1277 	ret = ib_post_send_mad(msg, NULL);
1278 	if (ret)
1279 		cm_free_msg(msg);
1280 
1281 	return ret;
1282 }
1283 
1284 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1285 				    __be32 local_qpn, __be32 remote_qpn)
1286 {
1287 	return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1288 		((local_ca_guid == remote_ca_guid) &&
1289 		 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1290 }
1291 
1292 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1293 					    struct ib_sa_path_rec *primary_path,
1294 					    struct ib_sa_path_rec *alt_path)
1295 {
1296 	memset(primary_path, 0, sizeof *primary_path);
1297 	primary_path->dgid = req_msg->primary_local_gid;
1298 	primary_path->sgid = req_msg->primary_remote_gid;
1299 	primary_path->dlid = req_msg->primary_local_lid;
1300 	primary_path->slid = req_msg->primary_remote_lid;
1301 	primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1302 	primary_path->hop_limit = req_msg->primary_hop_limit;
1303 	primary_path->traffic_class = req_msg->primary_traffic_class;
1304 	primary_path->reversible = 1;
1305 	primary_path->pkey = req_msg->pkey;
1306 	primary_path->sl = cm_req_get_primary_sl(req_msg);
1307 	primary_path->mtu_selector = IB_SA_EQ;
1308 	primary_path->mtu = cm_req_get_path_mtu(req_msg);
1309 	primary_path->rate_selector = IB_SA_EQ;
1310 	primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1311 	primary_path->packet_life_time_selector = IB_SA_EQ;
1312 	primary_path->packet_life_time =
1313 		cm_req_get_primary_local_ack_timeout(req_msg);
1314 	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1315 	primary_path->service_id = req_msg->service_id;
1316 
1317 	if (req_msg->alt_local_lid) {
1318 		memset(alt_path, 0, sizeof *alt_path);
1319 		alt_path->dgid = req_msg->alt_local_gid;
1320 		alt_path->sgid = req_msg->alt_remote_gid;
1321 		alt_path->dlid = req_msg->alt_local_lid;
1322 		alt_path->slid = req_msg->alt_remote_lid;
1323 		alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1324 		alt_path->hop_limit = req_msg->alt_hop_limit;
1325 		alt_path->traffic_class = req_msg->alt_traffic_class;
1326 		alt_path->reversible = 1;
1327 		alt_path->pkey = req_msg->pkey;
1328 		alt_path->sl = cm_req_get_alt_sl(req_msg);
1329 		alt_path->mtu_selector = IB_SA_EQ;
1330 		alt_path->mtu = cm_req_get_path_mtu(req_msg);
1331 		alt_path->rate_selector = IB_SA_EQ;
1332 		alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1333 		alt_path->packet_life_time_selector = IB_SA_EQ;
1334 		alt_path->packet_life_time =
1335 			cm_req_get_alt_local_ack_timeout(req_msg);
1336 		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1337 		alt_path->service_id = req_msg->service_id;
1338 	}
1339 }
1340 
1341 static u16 cm_get_bth_pkey(struct cm_work *work)
1342 {
1343 	struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1344 	u8 port_num = work->port->port_num;
1345 	u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1346 	u16 pkey;
1347 	int ret;
1348 
1349 	ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1350 	if (ret) {
1351 		dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1352 				     port_num, pkey_index, ret);
1353 		return 0;
1354 	}
1355 
1356 	return pkey;
1357 }
1358 
1359 static void cm_format_req_event(struct cm_work *work,
1360 				struct cm_id_private *cm_id_priv,
1361 				struct ib_cm_id *listen_id)
1362 {
1363 	struct cm_req_msg *req_msg;
1364 	struct ib_cm_req_event_param *param;
1365 
1366 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1367 	param = &work->cm_event.param.req_rcvd;
1368 	param->listen_id = listen_id;
1369 	param->bth_pkey = cm_get_bth_pkey(work);
1370 	param->port = cm_id_priv->av.port->port_num;
1371 	param->primary_path = &work->path[0];
1372 	if (req_msg->alt_local_lid)
1373 		param->alternate_path = &work->path[1];
1374 	else
1375 		param->alternate_path = NULL;
1376 	param->remote_ca_guid = req_msg->local_ca_guid;
1377 	param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1378 	param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1379 	param->qp_type = cm_req_get_qp_type(req_msg);
1380 	param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1381 	param->responder_resources = cm_req_get_init_depth(req_msg);
1382 	param->initiator_depth = cm_req_get_resp_res(req_msg);
1383 	param->local_cm_response_timeout =
1384 					cm_req_get_remote_resp_timeout(req_msg);
1385 	param->flow_control = cm_req_get_flow_ctrl(req_msg);
1386 	param->remote_cm_response_timeout =
1387 					cm_req_get_local_resp_timeout(req_msg);
1388 	param->retry_count = cm_req_get_retry_count(req_msg);
1389 	param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1390 	param->srq = cm_req_get_srq(req_msg);
1391 	work->cm_event.private_data = &req_msg->private_data;
1392 }
1393 
1394 static void cm_process_work(struct cm_id_private *cm_id_priv,
1395 			    struct cm_work *work)
1396 {
1397 	int ret;
1398 
1399 	/* We will typically only have the current event to report. */
1400 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1401 	cm_free_work(work);
1402 
1403 	while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1404 		spin_lock_irq(&cm_id_priv->lock);
1405 		work = cm_dequeue_work(cm_id_priv);
1406 		spin_unlock_irq(&cm_id_priv->lock);
1407 		BUG_ON(!work);
1408 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1409 						&work->cm_event);
1410 		cm_free_work(work);
1411 	}
1412 	cm_deref_id(cm_id_priv);
1413 	if (ret)
1414 		cm_destroy_id(&cm_id_priv->id, ret);
1415 }
1416 
1417 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1418 			  struct cm_id_private *cm_id_priv,
1419 			  enum cm_msg_response msg_mraed, u8 service_timeout,
1420 			  const void *private_data, u8 private_data_len)
1421 {
1422 	cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1423 	cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1424 	mra_msg->local_comm_id = cm_id_priv->id.local_id;
1425 	mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1426 	cm_mra_set_service_timeout(mra_msg, service_timeout);
1427 
1428 	if (private_data && private_data_len)
1429 		memcpy(mra_msg->private_data, private_data, private_data_len);
1430 }
1431 
1432 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1433 			  struct cm_id_private *cm_id_priv,
1434 			  enum ib_cm_rej_reason reason,
1435 			  void *ari,
1436 			  u8 ari_length,
1437 			  const void *private_data,
1438 			  u8 private_data_len)
1439 {
1440 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1441 	rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1442 
1443 	switch(cm_id_priv->id.state) {
1444 	case IB_CM_REQ_RCVD:
1445 		rej_msg->local_comm_id = 0;
1446 		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1447 		break;
1448 	case IB_CM_MRA_REQ_SENT:
1449 		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1450 		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1451 		break;
1452 	case IB_CM_REP_RCVD:
1453 	case IB_CM_MRA_REP_SENT:
1454 		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1455 		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1456 		break;
1457 	default:
1458 		rej_msg->local_comm_id = cm_id_priv->id.local_id;
1459 		cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1460 		break;
1461 	}
1462 
1463 	rej_msg->reason = cpu_to_be16(reason);
1464 	if (ari && ari_length) {
1465 		cm_rej_set_reject_info_len(rej_msg, ari_length);
1466 		memcpy(rej_msg->ari, ari, ari_length);
1467 	}
1468 
1469 	if (private_data && private_data_len)
1470 		memcpy(rej_msg->private_data, private_data, private_data_len);
1471 }
1472 
1473 static void cm_dup_req_handler(struct cm_work *work,
1474 			       struct cm_id_private *cm_id_priv)
1475 {
1476 	struct ib_mad_send_buf *msg = NULL;
1477 	int ret;
1478 
1479 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1480 			counter[CM_REQ_COUNTER]);
1481 
1482 	/* Quick state check to discard duplicate REQs. */
1483 	if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1484 		return;
1485 
1486 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1487 	if (ret)
1488 		return;
1489 
1490 	spin_lock_irq(&cm_id_priv->lock);
1491 	switch (cm_id_priv->id.state) {
1492 	case IB_CM_MRA_REQ_SENT:
1493 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1494 			      CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1495 			      cm_id_priv->private_data,
1496 			      cm_id_priv->private_data_len);
1497 		break;
1498 	case IB_CM_TIMEWAIT:
1499 		cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1500 			      IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1501 		break;
1502 	default:
1503 		goto unlock;
1504 	}
1505 	spin_unlock_irq(&cm_id_priv->lock);
1506 
1507 	ret = ib_post_send_mad(msg, NULL);
1508 	if (ret)
1509 		goto free;
1510 	return;
1511 
1512 unlock:	spin_unlock_irq(&cm_id_priv->lock);
1513 free:	cm_free_msg(msg);
1514 }
1515 
1516 static struct cm_id_private * cm_match_req(struct cm_work *work,
1517 					   struct cm_id_private *cm_id_priv)
1518 {
1519 	struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1520 	struct cm_timewait_info *timewait_info;
1521 	struct cm_req_msg *req_msg;
1522 
1523 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1524 
1525 	/* Check for possible duplicate REQ. */
1526 	spin_lock_irq(&cm.lock);
1527 	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1528 	if (timewait_info) {
1529 		cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1530 					   timewait_info->work.remote_id);
1531 		spin_unlock_irq(&cm.lock);
1532 		if (cur_cm_id_priv) {
1533 			cm_dup_req_handler(work, cur_cm_id_priv);
1534 			cm_deref_id(cur_cm_id_priv);
1535 		}
1536 		return NULL;
1537 	}
1538 
1539 	/* Check for stale connections. */
1540 	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1541 	if (timewait_info) {
1542 		cm_cleanup_timewait(cm_id_priv->timewait_info);
1543 		spin_unlock_irq(&cm.lock);
1544 		cm_issue_rej(work->port, work->mad_recv_wc,
1545 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1546 			     NULL, 0);
1547 		return NULL;
1548 	}
1549 
1550 	/* Find matching listen request. */
1551 	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1552 					   req_msg->service_id);
1553 	if (!listen_cm_id_priv) {
1554 		cm_cleanup_timewait(cm_id_priv->timewait_info);
1555 		spin_unlock_irq(&cm.lock);
1556 		cm_issue_rej(work->port, work->mad_recv_wc,
1557 			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1558 			     NULL, 0);
1559 		goto out;
1560 	}
1561 	atomic_inc(&listen_cm_id_priv->refcount);
1562 	atomic_inc(&cm_id_priv->refcount);
1563 	cm_id_priv->id.state = IB_CM_REQ_RCVD;
1564 	atomic_inc(&cm_id_priv->work_count);
1565 	spin_unlock_irq(&cm.lock);
1566 out:
1567 	return listen_cm_id_priv;
1568 }
1569 
1570 /*
1571  * Work-around for inter-subnet connections.  If the LIDs are permissive,
1572  * we need to override the LID/SL data in the REQ with the LID information
1573  * in the work completion.
1574  */
1575 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1576 {
1577 	if (!cm_req_get_primary_subnet_local(req_msg)) {
1578 		if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1579 			req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1580 			cm_req_set_primary_sl(req_msg, wc->sl);
1581 		}
1582 
1583 		if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1584 			req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1585 	}
1586 
1587 	if (!cm_req_get_alt_subnet_local(req_msg)) {
1588 		if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1589 			req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1590 			cm_req_set_alt_sl(req_msg, wc->sl);
1591 		}
1592 
1593 		if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1594 			req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1595 	}
1596 }
1597 
1598 static int cm_req_handler(struct cm_work *work)
1599 {
1600 	struct ib_cm_id *cm_id;
1601 	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1602 	struct cm_req_msg *req_msg;
1603 	union ib_gid gid;
1604 	struct ib_gid_attr gid_attr;
1605 	int ret;
1606 
1607 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1608 
1609 	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1610 	if (IS_ERR(cm_id))
1611 		return PTR_ERR(cm_id);
1612 
1613 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1614 	cm_id_priv->id.remote_id = req_msg->local_comm_id;
1615 	cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1616 				work->mad_recv_wc->recv_buf.grh,
1617 				&cm_id_priv->av);
1618 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1619 							    id.local_id);
1620 	if (IS_ERR(cm_id_priv->timewait_info)) {
1621 		ret = PTR_ERR(cm_id_priv->timewait_info);
1622 		goto destroy;
1623 	}
1624 	cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1625 	cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1626 	cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1627 
1628 	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1629 	if (!listen_cm_id_priv) {
1630 		ret = -EINVAL;
1631 		kfree(cm_id_priv->timewait_info);
1632 		goto destroy;
1633 	}
1634 
1635 	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1636 	cm_id_priv->id.context = listen_cm_id_priv->id.context;
1637 	cm_id_priv->id.service_id = req_msg->service_id;
1638 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1639 
1640 	cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1641 	cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1642 
1643 	memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
1644 	work->path[0].hop_limit = cm_id_priv->av.ah_attr.grh.hop_limit;
1645 	ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
1646 				work->port->port_num,
1647 				cm_id_priv->av.ah_attr.grh.sgid_index,
1648 				&gid, &gid_attr);
1649 	if (!ret) {
1650 		if (gid_attr.ndev) {
1651 			work->path[0].ifindex = gid_attr.ndev->ifindex;
1652 			work->path[0].net = dev_net(gid_attr.ndev);
1653 			dev_put(gid_attr.ndev);
1654 		}
1655 		work->path[0].gid_type = gid_attr.gid_type;
1656 		ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1657 	}
1658 	if (ret) {
1659 		int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
1660 					    work->port->port_num, 0,
1661 					    &work->path[0].sgid,
1662 					    &gid_attr);
1663 		if (!err && gid_attr.ndev) {
1664 			work->path[0].ifindex = gid_attr.ndev->ifindex;
1665 			work->path[0].net = dev_net(gid_attr.ndev);
1666 			dev_put(gid_attr.ndev);
1667 		}
1668 		work->path[0].gid_type = gid_attr.gid_type;
1669 		ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1670 			       &work->path[0].sgid, sizeof work->path[0].sgid,
1671 			       NULL, 0);
1672 		goto rejected;
1673 	}
1674 	if (req_msg->alt_local_lid) {
1675 		ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1676 		if (ret) {
1677 			ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1678 				       &work->path[0].sgid,
1679 				       sizeof work->path[0].sgid, NULL, 0);
1680 			goto rejected;
1681 		}
1682 	}
1683 	cm_id_priv->tid = req_msg->hdr.tid;
1684 	cm_id_priv->timeout_ms = cm_convert_to_ms(
1685 					cm_req_get_local_resp_timeout(req_msg));
1686 	cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1687 	cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1688 	cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1689 	cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1690 	cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1691 	cm_id_priv->pkey = req_msg->pkey;
1692 	cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1693 	cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1694 	cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1695 	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1696 
1697 	cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1698 	cm_process_work(cm_id_priv, work);
1699 	cm_deref_id(listen_cm_id_priv);
1700 	return 0;
1701 
1702 rejected:
1703 	atomic_dec(&cm_id_priv->refcount);
1704 	cm_deref_id(listen_cm_id_priv);
1705 destroy:
1706 	ib_destroy_cm_id(cm_id);
1707 	return ret;
1708 }
1709 
1710 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1711 			  struct cm_id_private *cm_id_priv,
1712 			  struct ib_cm_rep_param *param)
1713 {
1714 	cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1715 	rep_msg->local_comm_id = cm_id_priv->id.local_id;
1716 	rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1717 	cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1718 	rep_msg->resp_resources = param->responder_resources;
1719 	cm_rep_set_target_ack_delay(rep_msg,
1720 				    cm_id_priv->av.port->cm_dev->ack_delay);
1721 	cm_rep_set_failover(rep_msg, param->failover_accepted);
1722 	cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1723 	rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1724 
1725 	if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1726 		rep_msg->initiator_depth = param->initiator_depth;
1727 		cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1728 		cm_rep_set_srq(rep_msg, param->srq);
1729 		cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1730 	} else {
1731 		cm_rep_set_srq(rep_msg, 1);
1732 		cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
1733 	}
1734 
1735 	if (param->private_data && param->private_data_len)
1736 		memcpy(rep_msg->private_data, param->private_data,
1737 		       param->private_data_len);
1738 }
1739 
1740 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1741 		   struct ib_cm_rep_param *param)
1742 {
1743 	struct cm_id_private *cm_id_priv;
1744 	struct ib_mad_send_buf *msg;
1745 	struct cm_rep_msg *rep_msg;
1746 	unsigned long flags;
1747 	int ret;
1748 
1749 	if (param->private_data &&
1750 	    param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1751 		return -EINVAL;
1752 
1753 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1754 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1755 	if (cm_id->state != IB_CM_REQ_RCVD &&
1756 	    cm_id->state != IB_CM_MRA_REQ_SENT) {
1757 		ret = -EINVAL;
1758 		goto out;
1759 	}
1760 
1761 	ret = cm_alloc_msg(cm_id_priv, &msg);
1762 	if (ret)
1763 		goto out;
1764 
1765 	rep_msg = (struct cm_rep_msg *) msg->mad;
1766 	cm_format_rep(rep_msg, cm_id_priv, param);
1767 	msg->timeout_ms = cm_id_priv->timeout_ms;
1768 	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1769 
1770 	ret = ib_post_send_mad(msg, NULL);
1771 	if (ret) {
1772 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1773 		cm_free_msg(msg);
1774 		return ret;
1775 	}
1776 
1777 	cm_id->state = IB_CM_REP_SENT;
1778 	cm_id_priv->msg = msg;
1779 	cm_id_priv->initiator_depth = param->initiator_depth;
1780 	cm_id_priv->responder_resources = param->responder_resources;
1781 	cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1782 	cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
1783 
1784 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1785 	return ret;
1786 }
1787 EXPORT_SYMBOL(ib_send_cm_rep);
1788 
1789 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1790 			  struct cm_id_private *cm_id_priv,
1791 			  const void *private_data,
1792 			  u8 private_data_len)
1793 {
1794 	cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1795 	rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1796 	rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1797 
1798 	if (private_data && private_data_len)
1799 		memcpy(rtu_msg->private_data, private_data, private_data_len);
1800 }
1801 
1802 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1803 		   const void *private_data,
1804 		   u8 private_data_len)
1805 {
1806 	struct cm_id_private *cm_id_priv;
1807 	struct ib_mad_send_buf *msg;
1808 	unsigned long flags;
1809 	void *data;
1810 	int ret;
1811 
1812 	if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1813 		return -EINVAL;
1814 
1815 	data = cm_copy_private_data(private_data, private_data_len);
1816 	if (IS_ERR(data))
1817 		return PTR_ERR(data);
1818 
1819 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1820 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1821 	if (cm_id->state != IB_CM_REP_RCVD &&
1822 	    cm_id->state != IB_CM_MRA_REP_SENT) {
1823 		ret = -EINVAL;
1824 		goto error;
1825 	}
1826 
1827 	ret = cm_alloc_msg(cm_id_priv, &msg);
1828 	if (ret)
1829 		goto error;
1830 
1831 	cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1832 		      private_data, private_data_len);
1833 
1834 	ret = ib_post_send_mad(msg, NULL);
1835 	if (ret) {
1836 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1837 		cm_free_msg(msg);
1838 		kfree(data);
1839 		return ret;
1840 	}
1841 
1842 	cm_id->state = IB_CM_ESTABLISHED;
1843 	cm_set_private_data(cm_id_priv, data, private_data_len);
1844 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1845 	return 0;
1846 
1847 error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1848 	kfree(data);
1849 	return ret;
1850 }
1851 EXPORT_SYMBOL(ib_send_cm_rtu);
1852 
1853 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
1854 {
1855 	struct cm_rep_msg *rep_msg;
1856 	struct ib_cm_rep_event_param *param;
1857 
1858 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1859 	param = &work->cm_event.param.rep_rcvd;
1860 	param->remote_ca_guid = rep_msg->local_ca_guid;
1861 	param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1862 	param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
1863 	param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1864 	param->responder_resources = rep_msg->initiator_depth;
1865 	param->initiator_depth = rep_msg->resp_resources;
1866 	param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1867 	param->failover_accepted = cm_rep_get_failover(rep_msg);
1868 	param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1869 	param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1870 	param->srq = cm_rep_get_srq(rep_msg);
1871 	work->cm_event.private_data = &rep_msg->private_data;
1872 }
1873 
1874 static void cm_dup_rep_handler(struct cm_work *work)
1875 {
1876 	struct cm_id_private *cm_id_priv;
1877 	struct cm_rep_msg *rep_msg;
1878 	struct ib_mad_send_buf *msg = NULL;
1879 	int ret;
1880 
1881 	rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1882 	cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1883 				   rep_msg->local_comm_id);
1884 	if (!cm_id_priv)
1885 		return;
1886 
1887 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1888 			counter[CM_REP_COUNTER]);
1889 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1890 	if (ret)
1891 		goto deref;
1892 
1893 	spin_lock_irq(&cm_id_priv->lock);
1894 	if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1895 		cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1896 			      cm_id_priv->private_data,
1897 			      cm_id_priv->private_data_len);
1898 	else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1899 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1900 			      CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1901 			      cm_id_priv->private_data,
1902 			      cm_id_priv->private_data_len);
1903 	else
1904 		goto unlock;
1905 	spin_unlock_irq(&cm_id_priv->lock);
1906 
1907 	ret = ib_post_send_mad(msg, NULL);
1908 	if (ret)
1909 		goto free;
1910 	goto deref;
1911 
1912 unlock:	spin_unlock_irq(&cm_id_priv->lock);
1913 free:	cm_free_msg(msg);
1914 deref:	cm_deref_id(cm_id_priv);
1915 }
1916 
1917 static int cm_rep_handler(struct cm_work *work)
1918 {
1919 	struct cm_id_private *cm_id_priv;
1920 	struct cm_rep_msg *rep_msg;
1921 	int ret;
1922 
1923 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1924 	cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1925 	if (!cm_id_priv) {
1926 		cm_dup_rep_handler(work);
1927 		return -EINVAL;
1928 	}
1929 
1930 	cm_format_rep_event(work, cm_id_priv->qp_type);
1931 
1932 	spin_lock_irq(&cm_id_priv->lock);
1933 	switch (cm_id_priv->id.state) {
1934 	case IB_CM_REQ_SENT:
1935 	case IB_CM_MRA_REQ_RCVD:
1936 		break;
1937 	default:
1938 		spin_unlock_irq(&cm_id_priv->lock);
1939 		ret = -EINVAL;
1940 		goto error;
1941 	}
1942 
1943 	cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1944 	cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1945 	cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
1946 
1947 	spin_lock(&cm.lock);
1948 	/* Check for duplicate REP. */
1949 	if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1950 		spin_unlock(&cm.lock);
1951 		spin_unlock_irq(&cm_id_priv->lock);
1952 		ret = -EINVAL;
1953 		goto error;
1954 	}
1955 	/* Check for a stale connection. */
1956 	if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1957 		rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1958 			 &cm.remote_id_table);
1959 		cm_id_priv->timewait_info->inserted_remote_id = 0;
1960 		spin_unlock(&cm.lock);
1961 		spin_unlock_irq(&cm_id_priv->lock);
1962 		cm_issue_rej(work->port, work->mad_recv_wc,
1963 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1964 			     NULL, 0);
1965 		ret = -EINVAL;
1966 		goto error;
1967 	}
1968 	spin_unlock(&cm.lock);
1969 
1970 	cm_id_priv->id.state = IB_CM_REP_RCVD;
1971 	cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1972 	cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
1973 	cm_id_priv->initiator_depth = rep_msg->resp_resources;
1974 	cm_id_priv->responder_resources = rep_msg->initiator_depth;
1975 	cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1976 	cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1977 	cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1978 	cm_id_priv->av.timeout =
1979 			cm_ack_timeout(cm_id_priv->target_ack_delay,
1980 				       cm_id_priv->av.timeout - 1);
1981 	cm_id_priv->alt_av.timeout =
1982 			cm_ack_timeout(cm_id_priv->target_ack_delay,
1983 				       cm_id_priv->alt_av.timeout - 1);
1984 
1985 	/* todo: handle peer_to_peer */
1986 
1987 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1988 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
1989 	if (!ret)
1990 		list_add_tail(&work->list, &cm_id_priv->work_list);
1991 	spin_unlock_irq(&cm_id_priv->lock);
1992 
1993 	if (ret)
1994 		cm_process_work(cm_id_priv, work);
1995 	else
1996 		cm_deref_id(cm_id_priv);
1997 	return 0;
1998 
1999 error:
2000 	cm_deref_id(cm_id_priv);
2001 	return ret;
2002 }
2003 
2004 static int cm_establish_handler(struct cm_work *work)
2005 {
2006 	struct cm_id_private *cm_id_priv;
2007 	int ret;
2008 
2009 	/* See comment in cm_establish about lookup. */
2010 	cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2011 	if (!cm_id_priv)
2012 		return -EINVAL;
2013 
2014 	spin_lock_irq(&cm_id_priv->lock);
2015 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2016 		spin_unlock_irq(&cm_id_priv->lock);
2017 		goto out;
2018 	}
2019 
2020 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2021 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2022 	if (!ret)
2023 		list_add_tail(&work->list, &cm_id_priv->work_list);
2024 	spin_unlock_irq(&cm_id_priv->lock);
2025 
2026 	if (ret)
2027 		cm_process_work(cm_id_priv, work);
2028 	else
2029 		cm_deref_id(cm_id_priv);
2030 	return 0;
2031 out:
2032 	cm_deref_id(cm_id_priv);
2033 	return -EINVAL;
2034 }
2035 
2036 static int cm_rtu_handler(struct cm_work *work)
2037 {
2038 	struct cm_id_private *cm_id_priv;
2039 	struct cm_rtu_msg *rtu_msg;
2040 	int ret;
2041 
2042 	rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2043 	cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
2044 				   rtu_msg->local_comm_id);
2045 	if (!cm_id_priv)
2046 		return -EINVAL;
2047 
2048 	work->cm_event.private_data = &rtu_msg->private_data;
2049 
2050 	spin_lock_irq(&cm_id_priv->lock);
2051 	if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2052 	    cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2053 		spin_unlock_irq(&cm_id_priv->lock);
2054 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2055 				counter[CM_RTU_COUNTER]);
2056 		goto out;
2057 	}
2058 	cm_id_priv->id.state = IB_CM_ESTABLISHED;
2059 
2060 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2061 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2062 	if (!ret)
2063 		list_add_tail(&work->list, &cm_id_priv->work_list);
2064 	spin_unlock_irq(&cm_id_priv->lock);
2065 
2066 	if (ret)
2067 		cm_process_work(cm_id_priv, work);
2068 	else
2069 		cm_deref_id(cm_id_priv);
2070 	return 0;
2071 out:
2072 	cm_deref_id(cm_id_priv);
2073 	return -EINVAL;
2074 }
2075 
2076 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2077 			  struct cm_id_private *cm_id_priv,
2078 			  const void *private_data,
2079 			  u8 private_data_len)
2080 {
2081 	cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2082 			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2083 	dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2084 	dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2085 	cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2086 
2087 	if (private_data && private_data_len)
2088 		memcpy(dreq_msg->private_data, private_data, private_data_len);
2089 }
2090 
2091 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2092 		    const void *private_data,
2093 		    u8 private_data_len)
2094 {
2095 	struct cm_id_private *cm_id_priv;
2096 	struct ib_mad_send_buf *msg;
2097 	unsigned long flags;
2098 	int ret;
2099 
2100 	if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2101 		return -EINVAL;
2102 
2103 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2104 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2105 	if (cm_id->state != IB_CM_ESTABLISHED) {
2106 		ret = -EINVAL;
2107 		goto out;
2108 	}
2109 
2110 	if (cm_id->lap_state == IB_CM_LAP_SENT ||
2111 	    cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2112 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2113 
2114 	ret = cm_alloc_msg(cm_id_priv, &msg);
2115 	if (ret) {
2116 		cm_enter_timewait(cm_id_priv);
2117 		goto out;
2118 	}
2119 
2120 	cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2121 		       private_data, private_data_len);
2122 	msg->timeout_ms = cm_id_priv->timeout_ms;
2123 	msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2124 
2125 	ret = ib_post_send_mad(msg, NULL);
2126 	if (ret) {
2127 		cm_enter_timewait(cm_id_priv);
2128 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2129 		cm_free_msg(msg);
2130 		return ret;
2131 	}
2132 
2133 	cm_id->state = IB_CM_DREQ_SENT;
2134 	cm_id_priv->msg = msg;
2135 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2136 	return ret;
2137 }
2138 EXPORT_SYMBOL(ib_send_cm_dreq);
2139 
2140 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2141 			  struct cm_id_private *cm_id_priv,
2142 			  const void *private_data,
2143 			  u8 private_data_len)
2144 {
2145 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2146 	drep_msg->local_comm_id = cm_id_priv->id.local_id;
2147 	drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2148 
2149 	if (private_data && private_data_len)
2150 		memcpy(drep_msg->private_data, private_data, private_data_len);
2151 }
2152 
2153 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2154 		    const void *private_data,
2155 		    u8 private_data_len)
2156 {
2157 	struct cm_id_private *cm_id_priv;
2158 	struct ib_mad_send_buf *msg;
2159 	unsigned long flags;
2160 	void *data;
2161 	int ret;
2162 
2163 	if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2164 		return -EINVAL;
2165 
2166 	data = cm_copy_private_data(private_data, private_data_len);
2167 	if (IS_ERR(data))
2168 		return PTR_ERR(data);
2169 
2170 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2171 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2172 	if (cm_id->state != IB_CM_DREQ_RCVD) {
2173 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2174 		kfree(data);
2175 		return -EINVAL;
2176 	}
2177 
2178 	cm_set_private_data(cm_id_priv, data, private_data_len);
2179 	cm_enter_timewait(cm_id_priv);
2180 
2181 	ret = cm_alloc_msg(cm_id_priv, &msg);
2182 	if (ret)
2183 		goto out;
2184 
2185 	cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2186 		       private_data, private_data_len);
2187 
2188 	ret = ib_post_send_mad(msg, NULL);
2189 	if (ret) {
2190 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2191 		cm_free_msg(msg);
2192 		return ret;
2193 	}
2194 
2195 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2196 	return ret;
2197 }
2198 EXPORT_SYMBOL(ib_send_cm_drep);
2199 
2200 static int cm_issue_drep(struct cm_port *port,
2201 			 struct ib_mad_recv_wc *mad_recv_wc)
2202 {
2203 	struct ib_mad_send_buf *msg = NULL;
2204 	struct cm_dreq_msg *dreq_msg;
2205 	struct cm_drep_msg *drep_msg;
2206 	int ret;
2207 
2208 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2209 	if (ret)
2210 		return ret;
2211 
2212 	dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2213 	drep_msg = (struct cm_drep_msg *) msg->mad;
2214 
2215 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2216 	drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2217 	drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2218 
2219 	ret = ib_post_send_mad(msg, NULL);
2220 	if (ret)
2221 		cm_free_msg(msg);
2222 
2223 	return ret;
2224 }
2225 
2226 static int cm_dreq_handler(struct cm_work *work)
2227 {
2228 	struct cm_id_private *cm_id_priv;
2229 	struct cm_dreq_msg *dreq_msg;
2230 	struct ib_mad_send_buf *msg = NULL;
2231 	int ret;
2232 
2233 	dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2234 	cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2235 				   dreq_msg->local_comm_id);
2236 	if (!cm_id_priv) {
2237 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2238 				counter[CM_DREQ_COUNTER]);
2239 		cm_issue_drep(work->port, work->mad_recv_wc);
2240 		return -EINVAL;
2241 	}
2242 
2243 	work->cm_event.private_data = &dreq_msg->private_data;
2244 
2245 	spin_lock_irq(&cm_id_priv->lock);
2246 	if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2247 		goto unlock;
2248 
2249 	switch (cm_id_priv->id.state) {
2250 	case IB_CM_REP_SENT:
2251 	case IB_CM_DREQ_SENT:
2252 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2253 		break;
2254 	case IB_CM_ESTABLISHED:
2255 		if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2256 		    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2257 			ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2258 		break;
2259 	case IB_CM_MRA_REP_RCVD:
2260 		break;
2261 	case IB_CM_TIMEWAIT:
2262 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2263 				counter[CM_DREQ_COUNTER]);
2264 		if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2265 			goto unlock;
2266 
2267 		cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2268 			       cm_id_priv->private_data,
2269 			       cm_id_priv->private_data_len);
2270 		spin_unlock_irq(&cm_id_priv->lock);
2271 
2272 		if (ib_post_send_mad(msg, NULL))
2273 			cm_free_msg(msg);
2274 		goto deref;
2275 	case IB_CM_DREQ_RCVD:
2276 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2277 				counter[CM_DREQ_COUNTER]);
2278 		goto unlock;
2279 	default:
2280 		goto unlock;
2281 	}
2282 	cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2283 	cm_id_priv->tid = dreq_msg->hdr.tid;
2284 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2285 	if (!ret)
2286 		list_add_tail(&work->list, &cm_id_priv->work_list);
2287 	spin_unlock_irq(&cm_id_priv->lock);
2288 
2289 	if (ret)
2290 		cm_process_work(cm_id_priv, work);
2291 	else
2292 		cm_deref_id(cm_id_priv);
2293 	return 0;
2294 
2295 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2296 deref:	cm_deref_id(cm_id_priv);
2297 	return -EINVAL;
2298 }
2299 
2300 static int cm_drep_handler(struct cm_work *work)
2301 {
2302 	struct cm_id_private *cm_id_priv;
2303 	struct cm_drep_msg *drep_msg;
2304 	int ret;
2305 
2306 	drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2307 	cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2308 				   drep_msg->local_comm_id);
2309 	if (!cm_id_priv)
2310 		return -EINVAL;
2311 
2312 	work->cm_event.private_data = &drep_msg->private_data;
2313 
2314 	spin_lock_irq(&cm_id_priv->lock);
2315 	if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2316 	    cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2317 		spin_unlock_irq(&cm_id_priv->lock);
2318 		goto out;
2319 	}
2320 	cm_enter_timewait(cm_id_priv);
2321 
2322 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2323 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2324 	if (!ret)
2325 		list_add_tail(&work->list, &cm_id_priv->work_list);
2326 	spin_unlock_irq(&cm_id_priv->lock);
2327 
2328 	if (ret)
2329 		cm_process_work(cm_id_priv, work);
2330 	else
2331 		cm_deref_id(cm_id_priv);
2332 	return 0;
2333 out:
2334 	cm_deref_id(cm_id_priv);
2335 	return -EINVAL;
2336 }
2337 
2338 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2339 		   enum ib_cm_rej_reason reason,
2340 		   void *ari,
2341 		   u8 ari_length,
2342 		   const void *private_data,
2343 		   u8 private_data_len)
2344 {
2345 	struct cm_id_private *cm_id_priv;
2346 	struct ib_mad_send_buf *msg;
2347 	unsigned long flags;
2348 	int ret;
2349 
2350 	if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2351 	    (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2352 		return -EINVAL;
2353 
2354 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2355 
2356 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2357 	switch (cm_id->state) {
2358 	case IB_CM_REQ_SENT:
2359 	case IB_CM_MRA_REQ_RCVD:
2360 	case IB_CM_REQ_RCVD:
2361 	case IB_CM_MRA_REQ_SENT:
2362 	case IB_CM_REP_RCVD:
2363 	case IB_CM_MRA_REP_SENT:
2364 		ret = cm_alloc_msg(cm_id_priv, &msg);
2365 		if (!ret)
2366 			cm_format_rej((struct cm_rej_msg *) msg->mad,
2367 				      cm_id_priv, reason, ari, ari_length,
2368 				      private_data, private_data_len);
2369 
2370 		cm_reset_to_idle(cm_id_priv);
2371 		break;
2372 	case IB_CM_REP_SENT:
2373 	case IB_CM_MRA_REP_RCVD:
2374 		ret = cm_alloc_msg(cm_id_priv, &msg);
2375 		if (!ret)
2376 			cm_format_rej((struct cm_rej_msg *) msg->mad,
2377 				      cm_id_priv, reason, ari, ari_length,
2378 				      private_data, private_data_len);
2379 
2380 		cm_enter_timewait(cm_id_priv);
2381 		break;
2382 	default:
2383 		ret = -EINVAL;
2384 		goto out;
2385 	}
2386 
2387 	if (ret)
2388 		goto out;
2389 
2390 	ret = ib_post_send_mad(msg, NULL);
2391 	if (ret)
2392 		cm_free_msg(msg);
2393 
2394 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2395 	return ret;
2396 }
2397 EXPORT_SYMBOL(ib_send_cm_rej);
2398 
2399 static void cm_format_rej_event(struct cm_work *work)
2400 {
2401 	struct cm_rej_msg *rej_msg;
2402 	struct ib_cm_rej_event_param *param;
2403 
2404 	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2405 	param = &work->cm_event.param.rej_rcvd;
2406 	param->ari = rej_msg->ari;
2407 	param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2408 	param->reason = __be16_to_cpu(rej_msg->reason);
2409 	work->cm_event.private_data = &rej_msg->private_data;
2410 }
2411 
2412 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2413 {
2414 	struct cm_timewait_info *timewait_info;
2415 	struct cm_id_private *cm_id_priv;
2416 	__be32 remote_id;
2417 
2418 	remote_id = rej_msg->local_comm_id;
2419 
2420 	if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2421 		spin_lock_irq(&cm.lock);
2422 		timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2423 						  remote_id);
2424 		if (!timewait_info) {
2425 			spin_unlock_irq(&cm.lock);
2426 			return NULL;
2427 		}
2428 		cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2429 				      (timewait_info->work.local_id ^
2430 				       cm.random_id_operand));
2431 		if (cm_id_priv) {
2432 			if (cm_id_priv->id.remote_id == remote_id)
2433 				atomic_inc(&cm_id_priv->refcount);
2434 			else
2435 				cm_id_priv = NULL;
2436 		}
2437 		spin_unlock_irq(&cm.lock);
2438 	} else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2439 		cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2440 	else
2441 		cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2442 
2443 	return cm_id_priv;
2444 }
2445 
2446 static int cm_rej_handler(struct cm_work *work)
2447 {
2448 	struct cm_id_private *cm_id_priv;
2449 	struct cm_rej_msg *rej_msg;
2450 	int ret;
2451 
2452 	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2453 	cm_id_priv = cm_acquire_rejected_id(rej_msg);
2454 	if (!cm_id_priv)
2455 		return -EINVAL;
2456 
2457 	cm_format_rej_event(work);
2458 
2459 	spin_lock_irq(&cm_id_priv->lock);
2460 	switch (cm_id_priv->id.state) {
2461 	case IB_CM_REQ_SENT:
2462 	case IB_CM_MRA_REQ_RCVD:
2463 	case IB_CM_REP_SENT:
2464 	case IB_CM_MRA_REP_RCVD:
2465 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2466 		/* fall through */
2467 	case IB_CM_REQ_RCVD:
2468 	case IB_CM_MRA_REQ_SENT:
2469 		if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2470 			cm_enter_timewait(cm_id_priv);
2471 		else
2472 			cm_reset_to_idle(cm_id_priv);
2473 		break;
2474 	case IB_CM_DREQ_SENT:
2475 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2476 		/* fall through */
2477 	case IB_CM_REP_RCVD:
2478 	case IB_CM_MRA_REP_SENT:
2479 		cm_enter_timewait(cm_id_priv);
2480 		break;
2481 	case IB_CM_ESTABLISHED:
2482 		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2483 		    cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2484 			if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2485 				ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2486 					      cm_id_priv->msg);
2487 			cm_enter_timewait(cm_id_priv);
2488 			break;
2489 		}
2490 		/* fall through */
2491 	default:
2492 		spin_unlock_irq(&cm_id_priv->lock);
2493 		ret = -EINVAL;
2494 		goto out;
2495 	}
2496 
2497 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2498 	if (!ret)
2499 		list_add_tail(&work->list, &cm_id_priv->work_list);
2500 	spin_unlock_irq(&cm_id_priv->lock);
2501 
2502 	if (ret)
2503 		cm_process_work(cm_id_priv, work);
2504 	else
2505 		cm_deref_id(cm_id_priv);
2506 	return 0;
2507 out:
2508 	cm_deref_id(cm_id_priv);
2509 	return -EINVAL;
2510 }
2511 
2512 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2513 		   u8 service_timeout,
2514 		   const void *private_data,
2515 		   u8 private_data_len)
2516 {
2517 	struct cm_id_private *cm_id_priv;
2518 	struct ib_mad_send_buf *msg;
2519 	enum ib_cm_state cm_state;
2520 	enum ib_cm_lap_state lap_state;
2521 	enum cm_msg_response msg_response;
2522 	void *data;
2523 	unsigned long flags;
2524 	int ret;
2525 
2526 	if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2527 		return -EINVAL;
2528 
2529 	data = cm_copy_private_data(private_data, private_data_len);
2530 	if (IS_ERR(data))
2531 		return PTR_ERR(data);
2532 
2533 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2534 
2535 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2536 	switch(cm_id_priv->id.state) {
2537 	case IB_CM_REQ_RCVD:
2538 		cm_state = IB_CM_MRA_REQ_SENT;
2539 		lap_state = cm_id->lap_state;
2540 		msg_response = CM_MSG_RESPONSE_REQ;
2541 		break;
2542 	case IB_CM_REP_RCVD:
2543 		cm_state = IB_CM_MRA_REP_SENT;
2544 		lap_state = cm_id->lap_state;
2545 		msg_response = CM_MSG_RESPONSE_REP;
2546 		break;
2547 	case IB_CM_ESTABLISHED:
2548 		if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2549 			cm_state = cm_id->state;
2550 			lap_state = IB_CM_MRA_LAP_SENT;
2551 			msg_response = CM_MSG_RESPONSE_OTHER;
2552 			break;
2553 		}
2554 	default:
2555 		ret = -EINVAL;
2556 		goto error1;
2557 	}
2558 
2559 	if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2560 		ret = cm_alloc_msg(cm_id_priv, &msg);
2561 		if (ret)
2562 			goto error1;
2563 
2564 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2565 			      msg_response, service_timeout,
2566 			      private_data, private_data_len);
2567 		ret = ib_post_send_mad(msg, NULL);
2568 		if (ret)
2569 			goto error2;
2570 	}
2571 
2572 	cm_id->state = cm_state;
2573 	cm_id->lap_state = lap_state;
2574 	cm_id_priv->service_timeout = service_timeout;
2575 	cm_set_private_data(cm_id_priv, data, private_data_len);
2576 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2577 	return 0;
2578 
2579 error1:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2580 	kfree(data);
2581 	return ret;
2582 
2583 error2:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2584 	kfree(data);
2585 	cm_free_msg(msg);
2586 	return ret;
2587 }
2588 EXPORT_SYMBOL(ib_send_cm_mra);
2589 
2590 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2591 {
2592 	switch (cm_mra_get_msg_mraed(mra_msg)) {
2593 	case CM_MSG_RESPONSE_REQ:
2594 		return cm_acquire_id(mra_msg->remote_comm_id, 0);
2595 	case CM_MSG_RESPONSE_REP:
2596 	case CM_MSG_RESPONSE_OTHER:
2597 		return cm_acquire_id(mra_msg->remote_comm_id,
2598 				     mra_msg->local_comm_id);
2599 	default:
2600 		return NULL;
2601 	}
2602 }
2603 
2604 static int cm_mra_handler(struct cm_work *work)
2605 {
2606 	struct cm_id_private *cm_id_priv;
2607 	struct cm_mra_msg *mra_msg;
2608 	int timeout, ret;
2609 
2610 	mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2611 	cm_id_priv = cm_acquire_mraed_id(mra_msg);
2612 	if (!cm_id_priv)
2613 		return -EINVAL;
2614 
2615 	work->cm_event.private_data = &mra_msg->private_data;
2616 	work->cm_event.param.mra_rcvd.service_timeout =
2617 					cm_mra_get_service_timeout(mra_msg);
2618 	timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2619 		  cm_convert_to_ms(cm_id_priv->av.timeout);
2620 
2621 	spin_lock_irq(&cm_id_priv->lock);
2622 	switch (cm_id_priv->id.state) {
2623 	case IB_CM_REQ_SENT:
2624 		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2625 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2626 				  cm_id_priv->msg, timeout))
2627 			goto out;
2628 		cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2629 		break;
2630 	case IB_CM_REP_SENT:
2631 		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2632 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2633 				  cm_id_priv->msg, timeout))
2634 			goto out;
2635 		cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2636 		break;
2637 	case IB_CM_ESTABLISHED:
2638 		if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2639 		    cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2640 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
2641 				  cm_id_priv->msg, timeout)) {
2642 			if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2643 				atomic_long_inc(&work->port->
2644 						counter_group[CM_RECV_DUPLICATES].
2645 						counter[CM_MRA_COUNTER]);
2646 			goto out;
2647 		}
2648 		cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2649 		break;
2650 	case IB_CM_MRA_REQ_RCVD:
2651 	case IB_CM_MRA_REP_RCVD:
2652 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2653 				counter[CM_MRA_COUNTER]);
2654 		/* fall through */
2655 	default:
2656 		goto out;
2657 	}
2658 
2659 	cm_id_priv->msg->context[1] = (void *) (unsigned long)
2660 				      cm_id_priv->id.state;
2661 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2662 	if (!ret)
2663 		list_add_tail(&work->list, &cm_id_priv->work_list);
2664 	spin_unlock_irq(&cm_id_priv->lock);
2665 
2666 	if (ret)
2667 		cm_process_work(cm_id_priv, work);
2668 	else
2669 		cm_deref_id(cm_id_priv);
2670 	return 0;
2671 out:
2672 	spin_unlock_irq(&cm_id_priv->lock);
2673 	cm_deref_id(cm_id_priv);
2674 	return -EINVAL;
2675 }
2676 
2677 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2678 			  struct cm_id_private *cm_id_priv,
2679 			  struct ib_sa_path_rec *alternate_path,
2680 			  const void *private_data,
2681 			  u8 private_data_len)
2682 {
2683 	cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2684 			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2685 	lap_msg->local_comm_id = cm_id_priv->id.local_id;
2686 	lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2687 	cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2688 	/* todo: need remote CM response timeout */
2689 	cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2690 	lap_msg->alt_local_lid = alternate_path->slid;
2691 	lap_msg->alt_remote_lid = alternate_path->dlid;
2692 	lap_msg->alt_local_gid = alternate_path->sgid;
2693 	lap_msg->alt_remote_gid = alternate_path->dgid;
2694 	cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2695 	cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2696 	lap_msg->alt_hop_limit = alternate_path->hop_limit;
2697 	cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2698 	cm_lap_set_sl(lap_msg, alternate_path->sl);
2699 	cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2700 	cm_lap_set_local_ack_timeout(lap_msg,
2701 		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2702 			       alternate_path->packet_life_time));
2703 
2704 	if (private_data && private_data_len)
2705 		memcpy(lap_msg->private_data, private_data, private_data_len);
2706 }
2707 
2708 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2709 		   struct ib_sa_path_rec *alternate_path,
2710 		   const void *private_data,
2711 		   u8 private_data_len)
2712 {
2713 	struct cm_id_private *cm_id_priv;
2714 	struct ib_mad_send_buf *msg;
2715 	unsigned long flags;
2716 	int ret;
2717 
2718 	if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2719 		return -EINVAL;
2720 
2721 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2722 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2723 	if (cm_id->state != IB_CM_ESTABLISHED ||
2724 	    (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2725 	     cm_id->lap_state != IB_CM_LAP_IDLE)) {
2726 		ret = -EINVAL;
2727 		goto out;
2728 	}
2729 
2730 	ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2731 	if (ret)
2732 		goto out;
2733 	cm_id_priv->alt_av.timeout =
2734 			cm_ack_timeout(cm_id_priv->target_ack_delay,
2735 				       cm_id_priv->alt_av.timeout - 1);
2736 
2737 	ret = cm_alloc_msg(cm_id_priv, &msg);
2738 	if (ret)
2739 		goto out;
2740 
2741 	cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2742 		      alternate_path, private_data, private_data_len);
2743 	msg->timeout_ms = cm_id_priv->timeout_ms;
2744 	msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2745 
2746 	ret = ib_post_send_mad(msg, NULL);
2747 	if (ret) {
2748 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2749 		cm_free_msg(msg);
2750 		return ret;
2751 	}
2752 
2753 	cm_id->lap_state = IB_CM_LAP_SENT;
2754 	cm_id_priv->msg = msg;
2755 
2756 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2757 	return ret;
2758 }
2759 EXPORT_SYMBOL(ib_send_cm_lap);
2760 
2761 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2762 				    struct ib_sa_path_rec *path,
2763 				    struct cm_lap_msg *lap_msg)
2764 {
2765 	memset(path, 0, sizeof *path);
2766 	path->dgid = lap_msg->alt_local_gid;
2767 	path->sgid = lap_msg->alt_remote_gid;
2768 	path->dlid = lap_msg->alt_local_lid;
2769 	path->slid = lap_msg->alt_remote_lid;
2770 	path->flow_label = cm_lap_get_flow_label(lap_msg);
2771 	path->hop_limit = lap_msg->alt_hop_limit;
2772 	path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2773 	path->reversible = 1;
2774 	path->pkey = cm_id_priv->pkey;
2775 	path->sl = cm_lap_get_sl(lap_msg);
2776 	path->mtu_selector = IB_SA_EQ;
2777 	path->mtu = cm_id_priv->path_mtu;
2778 	path->rate_selector = IB_SA_EQ;
2779 	path->rate = cm_lap_get_packet_rate(lap_msg);
2780 	path->packet_life_time_selector = IB_SA_EQ;
2781 	path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2782 	path->packet_life_time -= (path->packet_life_time > 0);
2783 }
2784 
2785 static int cm_lap_handler(struct cm_work *work)
2786 {
2787 	struct cm_id_private *cm_id_priv;
2788 	struct cm_lap_msg *lap_msg;
2789 	struct ib_cm_lap_event_param *param;
2790 	struct ib_mad_send_buf *msg = NULL;
2791 	int ret;
2792 
2793 	/* todo: verify LAP request and send reject APR if invalid. */
2794 	lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2795 	cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2796 				   lap_msg->local_comm_id);
2797 	if (!cm_id_priv)
2798 		return -EINVAL;
2799 
2800 	param = &work->cm_event.param.lap_rcvd;
2801 	param->alternate_path = &work->path[0];
2802 	cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2803 	work->cm_event.private_data = &lap_msg->private_data;
2804 
2805 	spin_lock_irq(&cm_id_priv->lock);
2806 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2807 		goto unlock;
2808 
2809 	switch (cm_id_priv->id.lap_state) {
2810 	case IB_CM_LAP_UNINIT:
2811 	case IB_CM_LAP_IDLE:
2812 		break;
2813 	case IB_CM_MRA_LAP_SENT:
2814 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2815 				counter[CM_LAP_COUNTER]);
2816 		if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2817 			goto unlock;
2818 
2819 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2820 			      CM_MSG_RESPONSE_OTHER,
2821 			      cm_id_priv->service_timeout,
2822 			      cm_id_priv->private_data,
2823 			      cm_id_priv->private_data_len);
2824 		spin_unlock_irq(&cm_id_priv->lock);
2825 
2826 		if (ib_post_send_mad(msg, NULL))
2827 			cm_free_msg(msg);
2828 		goto deref;
2829 	case IB_CM_LAP_RCVD:
2830 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2831 				counter[CM_LAP_COUNTER]);
2832 		goto unlock;
2833 	default:
2834 		goto unlock;
2835 	}
2836 
2837 	cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2838 	cm_id_priv->tid = lap_msg->hdr.tid;
2839 	cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2840 				work->mad_recv_wc->recv_buf.grh,
2841 				&cm_id_priv->av);
2842 	cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2843 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2844 	if (!ret)
2845 		list_add_tail(&work->list, &cm_id_priv->work_list);
2846 	spin_unlock_irq(&cm_id_priv->lock);
2847 
2848 	if (ret)
2849 		cm_process_work(cm_id_priv, work);
2850 	else
2851 		cm_deref_id(cm_id_priv);
2852 	return 0;
2853 
2854 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2855 deref:	cm_deref_id(cm_id_priv);
2856 	return -EINVAL;
2857 }
2858 
2859 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2860 			  struct cm_id_private *cm_id_priv,
2861 			  enum ib_cm_apr_status status,
2862 			  void *info,
2863 			  u8 info_length,
2864 			  const void *private_data,
2865 			  u8 private_data_len)
2866 {
2867 	cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2868 	apr_msg->local_comm_id = cm_id_priv->id.local_id;
2869 	apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2870 	apr_msg->ap_status = (u8) status;
2871 
2872 	if (info && info_length) {
2873 		apr_msg->info_length = info_length;
2874 		memcpy(apr_msg->info, info, info_length);
2875 	}
2876 
2877 	if (private_data && private_data_len)
2878 		memcpy(apr_msg->private_data, private_data, private_data_len);
2879 }
2880 
2881 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2882 		   enum ib_cm_apr_status status,
2883 		   void *info,
2884 		   u8 info_length,
2885 		   const void *private_data,
2886 		   u8 private_data_len)
2887 {
2888 	struct cm_id_private *cm_id_priv;
2889 	struct ib_mad_send_buf *msg;
2890 	unsigned long flags;
2891 	int ret;
2892 
2893 	if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2894 	    (info && info_length > IB_CM_APR_INFO_LENGTH))
2895 		return -EINVAL;
2896 
2897 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2898 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2899 	if (cm_id->state != IB_CM_ESTABLISHED ||
2900 	    (cm_id->lap_state != IB_CM_LAP_RCVD &&
2901 	     cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2902 		ret = -EINVAL;
2903 		goto out;
2904 	}
2905 
2906 	ret = cm_alloc_msg(cm_id_priv, &msg);
2907 	if (ret)
2908 		goto out;
2909 
2910 	cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2911 		      info, info_length, private_data, private_data_len);
2912 	ret = ib_post_send_mad(msg, NULL);
2913 	if (ret) {
2914 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2915 		cm_free_msg(msg);
2916 		return ret;
2917 	}
2918 
2919 	cm_id->lap_state = IB_CM_LAP_IDLE;
2920 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2921 	return ret;
2922 }
2923 EXPORT_SYMBOL(ib_send_cm_apr);
2924 
2925 static int cm_apr_handler(struct cm_work *work)
2926 {
2927 	struct cm_id_private *cm_id_priv;
2928 	struct cm_apr_msg *apr_msg;
2929 	int ret;
2930 
2931 	apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2932 	cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2933 				   apr_msg->local_comm_id);
2934 	if (!cm_id_priv)
2935 		return -EINVAL; /* Unmatched reply. */
2936 
2937 	work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2938 	work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2939 	work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2940 	work->cm_event.private_data = &apr_msg->private_data;
2941 
2942 	spin_lock_irq(&cm_id_priv->lock);
2943 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2944 	    (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2945 	     cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2946 		spin_unlock_irq(&cm_id_priv->lock);
2947 		goto out;
2948 	}
2949 	cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2950 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2951 	cm_id_priv->msg = NULL;
2952 
2953 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2954 	if (!ret)
2955 		list_add_tail(&work->list, &cm_id_priv->work_list);
2956 	spin_unlock_irq(&cm_id_priv->lock);
2957 
2958 	if (ret)
2959 		cm_process_work(cm_id_priv, work);
2960 	else
2961 		cm_deref_id(cm_id_priv);
2962 	return 0;
2963 out:
2964 	cm_deref_id(cm_id_priv);
2965 	return -EINVAL;
2966 }
2967 
2968 static int cm_timewait_handler(struct cm_work *work)
2969 {
2970 	struct cm_timewait_info *timewait_info;
2971 	struct cm_id_private *cm_id_priv;
2972 	int ret;
2973 
2974 	timewait_info = (struct cm_timewait_info *)work;
2975 	spin_lock_irq(&cm.lock);
2976 	list_del(&timewait_info->list);
2977 	spin_unlock_irq(&cm.lock);
2978 
2979 	cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2980 				   timewait_info->work.remote_id);
2981 	if (!cm_id_priv)
2982 		return -EINVAL;
2983 
2984 	spin_lock_irq(&cm_id_priv->lock);
2985 	if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2986 	    cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2987 		spin_unlock_irq(&cm_id_priv->lock);
2988 		goto out;
2989 	}
2990 	cm_id_priv->id.state = IB_CM_IDLE;
2991 	ret = atomic_inc_and_test(&cm_id_priv->work_count);
2992 	if (!ret)
2993 		list_add_tail(&work->list, &cm_id_priv->work_list);
2994 	spin_unlock_irq(&cm_id_priv->lock);
2995 
2996 	if (ret)
2997 		cm_process_work(cm_id_priv, work);
2998 	else
2999 		cm_deref_id(cm_id_priv);
3000 	return 0;
3001 out:
3002 	cm_deref_id(cm_id_priv);
3003 	return -EINVAL;
3004 }
3005 
3006 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3007 			       struct cm_id_private *cm_id_priv,
3008 			       struct ib_cm_sidr_req_param *param)
3009 {
3010 	cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3011 			  cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
3012 	sidr_req_msg->request_id = cm_id_priv->id.local_id;
3013 	sidr_req_msg->pkey = param->path->pkey;
3014 	sidr_req_msg->service_id = param->service_id;
3015 
3016 	if (param->private_data && param->private_data_len)
3017 		memcpy(sidr_req_msg->private_data, param->private_data,
3018 		       param->private_data_len);
3019 }
3020 
3021 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3022 			struct ib_cm_sidr_req_param *param)
3023 {
3024 	struct cm_id_private *cm_id_priv;
3025 	struct ib_mad_send_buf *msg;
3026 	unsigned long flags;
3027 	int ret;
3028 
3029 	if (!param->path || (param->private_data &&
3030 	     param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3031 		return -EINVAL;
3032 
3033 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3034 	ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
3035 	if (ret)
3036 		goto out;
3037 
3038 	cm_id->service_id = param->service_id;
3039 	cm_id->service_mask = ~cpu_to_be64(0);
3040 	cm_id_priv->timeout_ms = param->timeout_ms;
3041 	cm_id_priv->max_cm_retries = param->max_cm_retries;
3042 	ret = cm_alloc_msg(cm_id_priv, &msg);
3043 	if (ret)
3044 		goto out;
3045 
3046 	cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3047 			   param);
3048 	msg->timeout_ms = cm_id_priv->timeout_ms;
3049 	msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3050 
3051 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3052 	if (cm_id->state == IB_CM_IDLE)
3053 		ret = ib_post_send_mad(msg, NULL);
3054 	else
3055 		ret = -EINVAL;
3056 
3057 	if (ret) {
3058 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3059 		cm_free_msg(msg);
3060 		goto out;
3061 	}
3062 	cm_id->state = IB_CM_SIDR_REQ_SENT;
3063 	cm_id_priv->msg = msg;
3064 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3065 out:
3066 	return ret;
3067 }
3068 EXPORT_SYMBOL(ib_send_cm_sidr_req);
3069 
3070 static void cm_format_sidr_req_event(struct cm_work *work,
3071 				     struct ib_cm_id *listen_id)
3072 {
3073 	struct cm_sidr_req_msg *sidr_req_msg;
3074 	struct ib_cm_sidr_req_event_param *param;
3075 
3076 	sidr_req_msg = (struct cm_sidr_req_msg *)
3077 				work->mad_recv_wc->recv_buf.mad;
3078 	param = &work->cm_event.param.sidr_req_rcvd;
3079 	param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
3080 	param->listen_id = listen_id;
3081 	param->service_id = sidr_req_msg->service_id;
3082 	param->bth_pkey = cm_get_bth_pkey(work);
3083 	param->port = work->port->port_num;
3084 	work->cm_event.private_data = &sidr_req_msg->private_data;
3085 }
3086 
3087 static int cm_sidr_req_handler(struct cm_work *work)
3088 {
3089 	struct ib_cm_id *cm_id;
3090 	struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3091 	struct cm_sidr_req_msg *sidr_req_msg;
3092 	struct ib_wc *wc;
3093 
3094 	cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
3095 	if (IS_ERR(cm_id))
3096 		return PTR_ERR(cm_id);
3097 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3098 
3099 	/* Record SGID/SLID and request ID for lookup. */
3100 	sidr_req_msg = (struct cm_sidr_req_msg *)
3101 				work->mad_recv_wc->recv_buf.mad;
3102 	wc = work->mad_recv_wc->wc;
3103 	cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3104 	cm_id_priv->av.dgid.global.interface_id = 0;
3105 	cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3106 				work->mad_recv_wc->recv_buf.grh,
3107 				&cm_id_priv->av);
3108 	cm_id_priv->id.remote_id = sidr_req_msg->request_id;
3109 	cm_id_priv->tid = sidr_req_msg->hdr.tid;
3110 	atomic_inc(&cm_id_priv->work_count);
3111 
3112 	spin_lock_irq(&cm.lock);
3113 	cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3114 	if (cur_cm_id_priv) {
3115 		spin_unlock_irq(&cm.lock);
3116 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3117 				counter[CM_SIDR_REQ_COUNTER]);
3118 		goto out; /* Duplicate message. */
3119 	}
3120 	cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3121 	cur_cm_id_priv = cm_find_listen(cm_id->device,
3122 					sidr_req_msg->service_id);
3123 	if (!cur_cm_id_priv) {
3124 		spin_unlock_irq(&cm.lock);
3125 		cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3126 		goto out; /* No match. */
3127 	}
3128 	atomic_inc(&cur_cm_id_priv->refcount);
3129 	atomic_inc(&cm_id_priv->refcount);
3130 	spin_unlock_irq(&cm.lock);
3131 
3132 	cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3133 	cm_id_priv->id.context = cur_cm_id_priv->id.context;
3134 	cm_id_priv->id.service_id = sidr_req_msg->service_id;
3135 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3136 
3137 	cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3138 	cm_process_work(cm_id_priv, work);
3139 	cm_deref_id(cur_cm_id_priv);
3140 	return 0;
3141 out:
3142 	ib_destroy_cm_id(&cm_id_priv->id);
3143 	return -EINVAL;
3144 }
3145 
3146 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3147 			       struct cm_id_private *cm_id_priv,
3148 			       struct ib_cm_sidr_rep_param *param)
3149 {
3150 	cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3151 			  cm_id_priv->tid);
3152 	sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3153 	sidr_rep_msg->status = param->status;
3154 	cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3155 	sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3156 	sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3157 
3158 	if (param->info && param->info_length)
3159 		memcpy(sidr_rep_msg->info, param->info, param->info_length);
3160 
3161 	if (param->private_data && param->private_data_len)
3162 		memcpy(sidr_rep_msg->private_data, param->private_data,
3163 		       param->private_data_len);
3164 }
3165 
3166 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3167 			struct ib_cm_sidr_rep_param *param)
3168 {
3169 	struct cm_id_private *cm_id_priv;
3170 	struct ib_mad_send_buf *msg;
3171 	unsigned long flags;
3172 	int ret;
3173 
3174 	if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3175 	    (param->private_data &&
3176 	     param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3177 		return -EINVAL;
3178 
3179 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3180 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3181 	if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3182 		ret = -EINVAL;
3183 		goto error;
3184 	}
3185 
3186 	ret = cm_alloc_msg(cm_id_priv, &msg);
3187 	if (ret)
3188 		goto error;
3189 
3190 	cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3191 			   param);
3192 	ret = ib_post_send_mad(msg, NULL);
3193 	if (ret) {
3194 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3195 		cm_free_msg(msg);
3196 		return ret;
3197 	}
3198 	cm_id->state = IB_CM_IDLE;
3199 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3200 
3201 	spin_lock_irqsave(&cm.lock, flags);
3202 	if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3203 		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3204 		RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3205 	}
3206 	spin_unlock_irqrestore(&cm.lock, flags);
3207 	return 0;
3208 
3209 error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3210 	return ret;
3211 }
3212 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3213 
3214 static void cm_format_sidr_rep_event(struct cm_work *work)
3215 {
3216 	struct cm_sidr_rep_msg *sidr_rep_msg;
3217 	struct ib_cm_sidr_rep_event_param *param;
3218 
3219 	sidr_rep_msg = (struct cm_sidr_rep_msg *)
3220 				work->mad_recv_wc->recv_buf.mad;
3221 	param = &work->cm_event.param.sidr_rep_rcvd;
3222 	param->status = sidr_rep_msg->status;
3223 	param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3224 	param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3225 	param->info = &sidr_rep_msg->info;
3226 	param->info_len = sidr_rep_msg->info_length;
3227 	work->cm_event.private_data = &sidr_rep_msg->private_data;
3228 }
3229 
3230 static int cm_sidr_rep_handler(struct cm_work *work)
3231 {
3232 	struct cm_sidr_rep_msg *sidr_rep_msg;
3233 	struct cm_id_private *cm_id_priv;
3234 
3235 	sidr_rep_msg = (struct cm_sidr_rep_msg *)
3236 				work->mad_recv_wc->recv_buf.mad;
3237 	cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3238 	if (!cm_id_priv)
3239 		return -EINVAL; /* Unmatched reply. */
3240 
3241 	spin_lock_irq(&cm_id_priv->lock);
3242 	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3243 		spin_unlock_irq(&cm_id_priv->lock);
3244 		goto out;
3245 	}
3246 	cm_id_priv->id.state = IB_CM_IDLE;
3247 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3248 	spin_unlock_irq(&cm_id_priv->lock);
3249 
3250 	cm_format_sidr_rep_event(work);
3251 	cm_process_work(cm_id_priv, work);
3252 	return 0;
3253 out:
3254 	cm_deref_id(cm_id_priv);
3255 	return -EINVAL;
3256 }
3257 
3258 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3259 				  enum ib_wc_status wc_status)
3260 {
3261 	struct cm_id_private *cm_id_priv;
3262 	struct ib_cm_event cm_event;
3263 	enum ib_cm_state state;
3264 	int ret;
3265 
3266 	memset(&cm_event, 0, sizeof cm_event);
3267 	cm_id_priv = msg->context[0];
3268 
3269 	/* Discard old sends or ones without a response. */
3270 	spin_lock_irq(&cm_id_priv->lock);
3271 	state = (enum ib_cm_state) (unsigned long) msg->context[1];
3272 	if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3273 		goto discard;
3274 
3275 	switch (state) {
3276 	case IB_CM_REQ_SENT:
3277 	case IB_CM_MRA_REQ_RCVD:
3278 		cm_reset_to_idle(cm_id_priv);
3279 		cm_event.event = IB_CM_REQ_ERROR;
3280 		break;
3281 	case IB_CM_REP_SENT:
3282 	case IB_CM_MRA_REP_RCVD:
3283 		cm_reset_to_idle(cm_id_priv);
3284 		cm_event.event = IB_CM_REP_ERROR;
3285 		break;
3286 	case IB_CM_DREQ_SENT:
3287 		cm_enter_timewait(cm_id_priv);
3288 		cm_event.event = IB_CM_DREQ_ERROR;
3289 		break;
3290 	case IB_CM_SIDR_REQ_SENT:
3291 		cm_id_priv->id.state = IB_CM_IDLE;
3292 		cm_event.event = IB_CM_SIDR_REQ_ERROR;
3293 		break;
3294 	default:
3295 		goto discard;
3296 	}
3297 	spin_unlock_irq(&cm_id_priv->lock);
3298 	cm_event.param.send_status = wc_status;
3299 
3300 	/* No other events can occur on the cm_id at this point. */
3301 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3302 	cm_free_msg(msg);
3303 	if (ret)
3304 		ib_destroy_cm_id(&cm_id_priv->id);
3305 	return;
3306 discard:
3307 	spin_unlock_irq(&cm_id_priv->lock);
3308 	cm_free_msg(msg);
3309 }
3310 
3311 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3312 			    struct ib_mad_send_wc *mad_send_wc)
3313 {
3314 	struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3315 	struct cm_port *port;
3316 	u16 attr_index;
3317 
3318 	port = mad_agent->context;
3319 	attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3320 				  msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3321 
3322 	/*
3323 	 * If the send was in response to a received message (context[0] is not
3324 	 * set to a cm_id), and is not a REJ, then it is a send that was
3325 	 * manually retried.
3326 	 */
3327 	if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3328 		msg->retries = 1;
3329 
3330 	atomic_long_add(1 + msg->retries,
3331 			&port->counter_group[CM_XMIT].counter[attr_index]);
3332 	if (msg->retries)
3333 		atomic_long_add(msg->retries,
3334 				&port->counter_group[CM_XMIT_RETRIES].
3335 				counter[attr_index]);
3336 
3337 	switch (mad_send_wc->status) {
3338 	case IB_WC_SUCCESS:
3339 	case IB_WC_WR_FLUSH_ERR:
3340 		cm_free_msg(msg);
3341 		break;
3342 	default:
3343 		if (msg->context[0] && msg->context[1])
3344 			cm_process_send_error(msg, mad_send_wc->status);
3345 		else
3346 			cm_free_msg(msg);
3347 		break;
3348 	}
3349 }
3350 
3351 static void cm_work_handler(struct work_struct *_work)
3352 {
3353 	struct cm_work *work = container_of(_work, struct cm_work, work.work);
3354 	int ret;
3355 
3356 	switch (work->cm_event.event) {
3357 	case IB_CM_REQ_RECEIVED:
3358 		ret = cm_req_handler(work);
3359 		break;
3360 	case IB_CM_MRA_RECEIVED:
3361 		ret = cm_mra_handler(work);
3362 		break;
3363 	case IB_CM_REJ_RECEIVED:
3364 		ret = cm_rej_handler(work);
3365 		break;
3366 	case IB_CM_REP_RECEIVED:
3367 		ret = cm_rep_handler(work);
3368 		break;
3369 	case IB_CM_RTU_RECEIVED:
3370 		ret = cm_rtu_handler(work);
3371 		break;
3372 	case IB_CM_USER_ESTABLISHED:
3373 		ret = cm_establish_handler(work);
3374 		break;
3375 	case IB_CM_DREQ_RECEIVED:
3376 		ret = cm_dreq_handler(work);
3377 		break;
3378 	case IB_CM_DREP_RECEIVED:
3379 		ret = cm_drep_handler(work);
3380 		break;
3381 	case IB_CM_SIDR_REQ_RECEIVED:
3382 		ret = cm_sidr_req_handler(work);
3383 		break;
3384 	case IB_CM_SIDR_REP_RECEIVED:
3385 		ret = cm_sidr_rep_handler(work);
3386 		break;
3387 	case IB_CM_LAP_RECEIVED:
3388 		ret = cm_lap_handler(work);
3389 		break;
3390 	case IB_CM_APR_RECEIVED:
3391 		ret = cm_apr_handler(work);
3392 		break;
3393 	case IB_CM_TIMEWAIT_EXIT:
3394 		ret = cm_timewait_handler(work);
3395 		break;
3396 	default:
3397 		ret = -EINVAL;
3398 		break;
3399 	}
3400 	if (ret)
3401 		cm_free_work(work);
3402 }
3403 
3404 static int cm_establish(struct ib_cm_id *cm_id)
3405 {
3406 	struct cm_id_private *cm_id_priv;
3407 	struct cm_work *work;
3408 	unsigned long flags;
3409 	int ret = 0;
3410 	struct cm_device *cm_dev;
3411 
3412 	cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3413 	if (!cm_dev)
3414 		return -ENODEV;
3415 
3416 	work = kmalloc(sizeof *work, GFP_ATOMIC);
3417 	if (!work)
3418 		return -ENOMEM;
3419 
3420 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3421 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3422 	switch (cm_id->state)
3423 	{
3424 	case IB_CM_REP_SENT:
3425 	case IB_CM_MRA_REP_RCVD:
3426 		cm_id->state = IB_CM_ESTABLISHED;
3427 		break;
3428 	case IB_CM_ESTABLISHED:
3429 		ret = -EISCONN;
3430 		break;
3431 	default:
3432 		ret = -EINVAL;
3433 		break;
3434 	}
3435 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3436 
3437 	if (ret) {
3438 		kfree(work);
3439 		goto out;
3440 	}
3441 
3442 	/*
3443 	 * The CM worker thread may try to destroy the cm_id before it
3444 	 * can execute this work item.  To prevent potential deadlock,
3445 	 * we need to find the cm_id once we're in the context of the
3446 	 * worker thread, rather than holding a reference on it.
3447 	 */
3448 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3449 	work->local_id = cm_id->local_id;
3450 	work->remote_id = cm_id->remote_id;
3451 	work->mad_recv_wc = NULL;
3452 	work->cm_event.event = IB_CM_USER_ESTABLISHED;
3453 
3454 	/* Check if the device started its remove_one */
3455 	spin_lock_irq(&cm.lock);
3456 	if (!cm_dev->going_down) {
3457 		queue_delayed_work(cm.wq, &work->work, 0);
3458 	} else {
3459 		kfree(work);
3460 		ret = -ENODEV;
3461 	}
3462 	spin_unlock_irq(&cm.lock);
3463 
3464 out:
3465 	return ret;
3466 }
3467 
3468 static int cm_migrate(struct ib_cm_id *cm_id)
3469 {
3470 	struct cm_id_private *cm_id_priv;
3471 	unsigned long flags;
3472 	int ret = 0;
3473 
3474 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3475 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3476 	if (cm_id->state == IB_CM_ESTABLISHED &&
3477 	    (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3478 	     cm_id->lap_state == IB_CM_LAP_IDLE)) {
3479 		cm_id->lap_state = IB_CM_LAP_IDLE;
3480 		cm_id_priv->av = cm_id_priv->alt_av;
3481 	} else
3482 		ret = -EINVAL;
3483 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3484 
3485 	return ret;
3486 }
3487 
3488 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3489 {
3490 	int ret;
3491 
3492 	switch (event) {
3493 	case IB_EVENT_COMM_EST:
3494 		ret = cm_establish(cm_id);
3495 		break;
3496 	case IB_EVENT_PATH_MIG:
3497 		ret = cm_migrate(cm_id);
3498 		break;
3499 	default:
3500 		ret = -EINVAL;
3501 	}
3502 	return ret;
3503 }
3504 EXPORT_SYMBOL(ib_cm_notify);
3505 
3506 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3507 			    struct ib_mad_send_buf *send_buf,
3508 			    struct ib_mad_recv_wc *mad_recv_wc)
3509 {
3510 	struct cm_port *port = mad_agent->context;
3511 	struct cm_work *work;
3512 	enum ib_cm_event_type event;
3513 	u16 attr_id;
3514 	int paths = 0;
3515 	int going_down = 0;
3516 
3517 	switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3518 	case CM_REQ_ATTR_ID:
3519 		paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3520 						    alt_local_lid != 0);
3521 		event = IB_CM_REQ_RECEIVED;
3522 		break;
3523 	case CM_MRA_ATTR_ID:
3524 		event = IB_CM_MRA_RECEIVED;
3525 		break;
3526 	case CM_REJ_ATTR_ID:
3527 		event = IB_CM_REJ_RECEIVED;
3528 		break;
3529 	case CM_REP_ATTR_ID:
3530 		event = IB_CM_REP_RECEIVED;
3531 		break;
3532 	case CM_RTU_ATTR_ID:
3533 		event = IB_CM_RTU_RECEIVED;
3534 		break;
3535 	case CM_DREQ_ATTR_ID:
3536 		event = IB_CM_DREQ_RECEIVED;
3537 		break;
3538 	case CM_DREP_ATTR_ID:
3539 		event = IB_CM_DREP_RECEIVED;
3540 		break;
3541 	case CM_SIDR_REQ_ATTR_ID:
3542 		event = IB_CM_SIDR_REQ_RECEIVED;
3543 		break;
3544 	case CM_SIDR_REP_ATTR_ID:
3545 		event = IB_CM_SIDR_REP_RECEIVED;
3546 		break;
3547 	case CM_LAP_ATTR_ID:
3548 		paths = 1;
3549 		event = IB_CM_LAP_RECEIVED;
3550 		break;
3551 	case CM_APR_ATTR_ID:
3552 		event = IB_CM_APR_RECEIVED;
3553 		break;
3554 	default:
3555 		ib_free_recv_mad(mad_recv_wc);
3556 		return;
3557 	}
3558 
3559 	attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3560 	atomic_long_inc(&port->counter_group[CM_RECV].
3561 			counter[attr_id - CM_ATTR_ID_OFFSET]);
3562 
3563 	work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3564 		       GFP_KERNEL);
3565 	if (!work) {
3566 		ib_free_recv_mad(mad_recv_wc);
3567 		return;
3568 	}
3569 
3570 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3571 	work->cm_event.event = event;
3572 	work->mad_recv_wc = mad_recv_wc;
3573 	work->port = port;
3574 
3575 	/* Check if the device started its remove_one */
3576 	spin_lock_irq(&cm.lock);
3577 	if (!port->cm_dev->going_down)
3578 		queue_delayed_work(cm.wq, &work->work, 0);
3579 	else
3580 		going_down = 1;
3581 	spin_unlock_irq(&cm.lock);
3582 
3583 	if (going_down) {
3584 		kfree(work);
3585 		ib_free_recv_mad(mad_recv_wc);
3586 	}
3587 }
3588 
3589 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3590 				struct ib_qp_attr *qp_attr,
3591 				int *qp_attr_mask)
3592 {
3593 	unsigned long flags;
3594 	int ret;
3595 
3596 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3597 	switch (cm_id_priv->id.state) {
3598 	case IB_CM_REQ_SENT:
3599 	case IB_CM_MRA_REQ_RCVD:
3600 	case IB_CM_REQ_RCVD:
3601 	case IB_CM_MRA_REQ_SENT:
3602 	case IB_CM_REP_RCVD:
3603 	case IB_CM_MRA_REP_SENT:
3604 	case IB_CM_REP_SENT:
3605 	case IB_CM_MRA_REP_RCVD:
3606 	case IB_CM_ESTABLISHED:
3607 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3608 				IB_QP_PKEY_INDEX | IB_QP_PORT;
3609 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3610 		if (cm_id_priv->responder_resources)
3611 			qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3612 						    IB_ACCESS_REMOTE_ATOMIC;
3613 		qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3614 		qp_attr->port_num = cm_id_priv->av.port->port_num;
3615 		ret = 0;
3616 		break;
3617 	default:
3618 		ret = -EINVAL;
3619 		break;
3620 	}
3621 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3622 	return ret;
3623 }
3624 
3625 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3626 			       struct ib_qp_attr *qp_attr,
3627 			       int *qp_attr_mask)
3628 {
3629 	unsigned long flags;
3630 	int ret;
3631 
3632 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3633 	switch (cm_id_priv->id.state) {
3634 	case IB_CM_REQ_RCVD:
3635 	case IB_CM_MRA_REQ_SENT:
3636 	case IB_CM_REP_RCVD:
3637 	case IB_CM_MRA_REP_SENT:
3638 	case IB_CM_REP_SENT:
3639 	case IB_CM_MRA_REP_RCVD:
3640 	case IB_CM_ESTABLISHED:
3641 		*qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3642 				IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3643 		qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3644 		qp_attr->path_mtu = cm_id_priv->path_mtu;
3645 		qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3646 		qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3647 		if (cm_id_priv->qp_type == IB_QPT_RC ||
3648 		    cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
3649 			*qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3650 					 IB_QP_MIN_RNR_TIMER;
3651 			qp_attr->max_dest_rd_atomic =
3652 					cm_id_priv->responder_resources;
3653 			qp_attr->min_rnr_timer = 0;
3654 		}
3655 		if (cm_id_priv->alt_av.ah_attr.dlid) {
3656 			*qp_attr_mask |= IB_QP_ALT_PATH;
3657 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3658 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3659 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3660 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3661 		}
3662 		ret = 0;
3663 		break;
3664 	default:
3665 		ret = -EINVAL;
3666 		break;
3667 	}
3668 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3669 	return ret;
3670 }
3671 
3672 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3673 			       struct ib_qp_attr *qp_attr,
3674 			       int *qp_attr_mask)
3675 {
3676 	unsigned long flags;
3677 	int ret;
3678 
3679 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3680 	switch (cm_id_priv->id.state) {
3681 	/* Allow transition to RTS before sending REP */
3682 	case IB_CM_REQ_RCVD:
3683 	case IB_CM_MRA_REQ_SENT:
3684 
3685 	case IB_CM_REP_RCVD:
3686 	case IB_CM_MRA_REP_SENT:
3687 	case IB_CM_REP_SENT:
3688 	case IB_CM_MRA_REP_RCVD:
3689 	case IB_CM_ESTABLISHED:
3690 		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3691 			*qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3692 			qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3693 			switch (cm_id_priv->qp_type) {
3694 			case IB_QPT_RC:
3695 			case IB_QPT_XRC_INI:
3696 				*qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3697 						 IB_QP_MAX_QP_RD_ATOMIC;
3698 				qp_attr->retry_cnt = cm_id_priv->retry_count;
3699 				qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3700 				qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3701 				/* fall through */
3702 			case IB_QPT_XRC_TGT:
3703 				*qp_attr_mask |= IB_QP_TIMEOUT;
3704 				qp_attr->timeout = cm_id_priv->av.timeout;
3705 				break;
3706 			default:
3707 				break;
3708 			}
3709 			if (cm_id_priv->alt_av.ah_attr.dlid) {
3710 				*qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3711 				qp_attr->path_mig_state = IB_MIG_REARM;
3712 			}
3713 		} else {
3714 			*qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3715 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3716 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3717 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3718 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3719 			qp_attr->path_mig_state = IB_MIG_REARM;
3720 		}
3721 		ret = 0;
3722 		break;
3723 	default:
3724 		ret = -EINVAL;
3725 		break;
3726 	}
3727 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3728 	return ret;
3729 }
3730 
3731 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3732 		       struct ib_qp_attr *qp_attr,
3733 		       int *qp_attr_mask)
3734 {
3735 	struct cm_id_private *cm_id_priv;
3736 	int ret;
3737 
3738 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3739 	switch (qp_attr->qp_state) {
3740 	case IB_QPS_INIT:
3741 		ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3742 		break;
3743 	case IB_QPS_RTR:
3744 		ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3745 		break;
3746 	case IB_QPS_RTS:
3747 		ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3748 		break;
3749 	default:
3750 		ret = -EINVAL;
3751 		break;
3752 	}
3753 	return ret;
3754 }
3755 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3756 
3757 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3758 			       char *buf)
3759 {
3760 	struct cm_counter_group *group;
3761 	struct cm_counter_attribute *cm_attr;
3762 
3763 	group = container_of(obj, struct cm_counter_group, obj);
3764 	cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3765 
3766 	return sprintf(buf, "%ld\n",
3767 		       atomic_long_read(&group->counter[cm_attr->index]));
3768 }
3769 
3770 static const struct sysfs_ops cm_counter_ops = {
3771 	.show = cm_show_counter
3772 };
3773 
3774 static struct kobj_type cm_counter_obj_type = {
3775 	.sysfs_ops = &cm_counter_ops,
3776 	.default_attrs = cm_counter_default_attrs
3777 };
3778 
3779 static void cm_release_port_obj(struct kobject *obj)
3780 {
3781 	struct cm_port *cm_port;
3782 
3783 	cm_port = container_of(obj, struct cm_port, port_obj);
3784 	kfree(cm_port);
3785 }
3786 
3787 static struct kobj_type cm_port_obj_type = {
3788 	.release = cm_release_port_obj
3789 };
3790 
3791 static char *cm_devnode(struct device *dev, umode_t *mode)
3792 {
3793 	if (mode)
3794 		*mode = 0666;
3795 	return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3796 }
3797 
3798 struct class cm_class = {
3799 	.owner   = THIS_MODULE,
3800 	.name    = "infiniband_cm",
3801 	.devnode = cm_devnode,
3802 };
3803 EXPORT_SYMBOL(cm_class);
3804 
3805 static int cm_create_port_fs(struct cm_port *port)
3806 {
3807 	int i, ret;
3808 
3809 	ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3810 				   &port->cm_dev->device->kobj,
3811 				   "%d", port->port_num);
3812 	if (ret) {
3813 		kfree(port);
3814 		return ret;
3815 	}
3816 
3817 	for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3818 		ret = kobject_init_and_add(&port->counter_group[i].obj,
3819 					   &cm_counter_obj_type,
3820 					   &port->port_obj,
3821 					   "%s", counter_group_names[i]);
3822 		if (ret)
3823 			goto error;
3824 	}
3825 
3826 	return 0;
3827 
3828 error:
3829 	while (i--)
3830 		kobject_put(&port->counter_group[i].obj);
3831 	kobject_put(&port->port_obj);
3832 	return ret;
3833 
3834 }
3835 
3836 static void cm_remove_port_fs(struct cm_port *port)
3837 {
3838 	int i;
3839 
3840 	for (i = 0; i < CM_COUNTER_GROUPS; i++)
3841 		kobject_put(&port->counter_group[i].obj);
3842 
3843 	kobject_put(&port->port_obj);
3844 }
3845 
3846 static void cm_add_one(struct ib_device *ib_device)
3847 {
3848 	struct cm_device *cm_dev;
3849 	struct cm_port *port;
3850 	struct ib_mad_reg_req reg_req = {
3851 		.mgmt_class = IB_MGMT_CLASS_CM,
3852 		.mgmt_class_version = IB_CM_CLASS_VERSION,
3853 	};
3854 	struct ib_port_modify port_modify = {
3855 		.set_port_cap_mask = IB_PORT_CM_SUP
3856 	};
3857 	unsigned long flags;
3858 	int ret;
3859 	int count = 0;
3860 	u8 i;
3861 
3862 	cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3863 			 ib_device->phys_port_cnt, GFP_KERNEL);
3864 	if (!cm_dev)
3865 		return;
3866 
3867 	cm_dev->ib_device = ib_device;
3868 	cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
3869 	cm_dev->going_down = 0;
3870 	cm_dev->device = device_create(&cm_class, &ib_device->dev,
3871 				       MKDEV(0, 0), NULL,
3872 				       "%s", ib_device->name);
3873 	if (IS_ERR(cm_dev->device)) {
3874 		kfree(cm_dev);
3875 		return;
3876 	}
3877 
3878 	set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3879 	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3880 		if (!rdma_cap_ib_cm(ib_device, i))
3881 			continue;
3882 
3883 		port = kzalloc(sizeof *port, GFP_KERNEL);
3884 		if (!port)
3885 			goto error1;
3886 
3887 		cm_dev->port[i-1] = port;
3888 		port->cm_dev = cm_dev;
3889 		port->port_num = i;
3890 
3891 		ret = cm_create_port_fs(port);
3892 		if (ret)
3893 			goto error1;
3894 
3895 		port->mad_agent = ib_register_mad_agent(ib_device, i,
3896 							IB_QPT_GSI,
3897 							&reg_req,
3898 							0,
3899 							cm_send_handler,
3900 							cm_recv_handler,
3901 							port,
3902 							0);
3903 		if (IS_ERR(port->mad_agent))
3904 			goto error2;
3905 
3906 		ret = ib_modify_port(ib_device, i, 0, &port_modify);
3907 		if (ret)
3908 			goto error3;
3909 
3910 		count++;
3911 	}
3912 
3913 	if (!count)
3914 		goto free;
3915 
3916 	ib_set_client_data(ib_device, &cm_client, cm_dev);
3917 
3918 	write_lock_irqsave(&cm.device_lock, flags);
3919 	list_add_tail(&cm_dev->list, &cm.device_list);
3920 	write_unlock_irqrestore(&cm.device_lock, flags);
3921 	return;
3922 
3923 error3:
3924 	ib_unregister_mad_agent(port->mad_agent);
3925 error2:
3926 	cm_remove_port_fs(port);
3927 error1:
3928 	port_modify.set_port_cap_mask = 0;
3929 	port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3930 	while (--i) {
3931 		if (!rdma_cap_ib_cm(ib_device, i))
3932 			continue;
3933 
3934 		port = cm_dev->port[i-1];
3935 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3936 		ib_unregister_mad_agent(port->mad_agent);
3937 		cm_remove_port_fs(port);
3938 	}
3939 free:
3940 	device_unregister(cm_dev->device);
3941 	kfree(cm_dev);
3942 }
3943 
3944 static void cm_remove_one(struct ib_device *ib_device, void *client_data)
3945 {
3946 	struct cm_device *cm_dev = client_data;
3947 	struct cm_port *port;
3948 	struct ib_port_modify port_modify = {
3949 		.clr_port_cap_mask = IB_PORT_CM_SUP
3950 	};
3951 	unsigned long flags;
3952 	int i;
3953 
3954 	if (!cm_dev)
3955 		return;
3956 
3957 	write_lock_irqsave(&cm.device_lock, flags);
3958 	list_del(&cm_dev->list);
3959 	write_unlock_irqrestore(&cm.device_lock, flags);
3960 
3961 	spin_lock_irq(&cm.lock);
3962 	cm_dev->going_down = 1;
3963 	spin_unlock_irq(&cm.lock);
3964 
3965 	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3966 		if (!rdma_cap_ib_cm(ib_device, i))
3967 			continue;
3968 
3969 		port = cm_dev->port[i-1];
3970 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3971 		/*
3972 		 * We flush the queue here after the going_down set, this
3973 		 * verify that no new works will be queued in the recv handler,
3974 		 * after that we can call the unregister_mad_agent
3975 		 */
3976 		flush_workqueue(cm.wq);
3977 		ib_unregister_mad_agent(port->mad_agent);
3978 		cm_remove_port_fs(port);
3979 	}
3980 	device_unregister(cm_dev->device);
3981 	kfree(cm_dev);
3982 }
3983 
3984 static int __init ib_cm_init(void)
3985 {
3986 	int ret;
3987 
3988 	memset(&cm, 0, sizeof cm);
3989 	INIT_LIST_HEAD(&cm.device_list);
3990 	rwlock_init(&cm.device_lock);
3991 	spin_lock_init(&cm.lock);
3992 	cm.listen_service_table = RB_ROOT;
3993 	cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3994 	cm.remote_id_table = RB_ROOT;
3995 	cm.remote_qp_table = RB_ROOT;
3996 	cm.remote_sidr_table = RB_ROOT;
3997 	idr_init(&cm.local_id_table);
3998 	get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3999 	INIT_LIST_HEAD(&cm.timewait_list);
4000 
4001 	ret = class_register(&cm_class);
4002 	if (ret) {
4003 		ret = -ENOMEM;
4004 		goto error1;
4005 	}
4006 
4007 	cm.wq = create_workqueue("ib_cm");
4008 	if (!cm.wq) {
4009 		ret = -ENOMEM;
4010 		goto error2;
4011 	}
4012 
4013 	ret = ib_register_client(&cm_client);
4014 	if (ret)
4015 		goto error3;
4016 
4017 	return 0;
4018 error3:
4019 	destroy_workqueue(cm.wq);
4020 error2:
4021 	class_unregister(&cm_class);
4022 error1:
4023 	idr_destroy(&cm.local_id_table);
4024 	return ret;
4025 }
4026 
4027 static void __exit ib_cm_cleanup(void)
4028 {
4029 	struct cm_timewait_info *timewait_info, *tmp;
4030 
4031 	spin_lock_irq(&cm.lock);
4032 	list_for_each_entry(timewait_info, &cm.timewait_list, list)
4033 		cancel_delayed_work(&timewait_info->work.work);
4034 	spin_unlock_irq(&cm.lock);
4035 
4036 	ib_unregister_client(&cm_client);
4037 	destroy_workqueue(cm.wq);
4038 
4039 	list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4040 		list_del(&timewait_info->list);
4041 		kfree(timewait_info);
4042 	}
4043 
4044 	class_unregister(&cm_class);
4045 	idr_destroy(&cm.local_id_table);
4046 }
4047 
4048 module_init(ib_cm_init);
4049 module_exit(ib_cm_cleanup);
4050 
4051