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