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