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