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