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