xref: /openbmc/linux/drivers/infiniband/core/cm.c (revision 88f4ede4)
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 #include "cm_trace.h"
31 
32 MODULE_AUTHOR("Sean Hefty");
33 MODULE_DESCRIPTION("InfiniBand CM");
34 MODULE_LICENSE("Dual BSD/GPL");
35 
36 static const char * const ibcm_rej_reason_strs[] = {
37 	[IB_CM_REJ_NO_QP]			= "no QP",
38 	[IB_CM_REJ_NO_EEC]			= "no EEC",
39 	[IB_CM_REJ_NO_RESOURCES]		= "no resources",
40 	[IB_CM_REJ_TIMEOUT]			= "timeout",
41 	[IB_CM_REJ_UNSUPPORTED]			= "unsupported",
42 	[IB_CM_REJ_INVALID_COMM_ID]		= "invalid comm ID",
43 	[IB_CM_REJ_INVALID_COMM_INSTANCE]	= "invalid comm instance",
44 	[IB_CM_REJ_INVALID_SERVICE_ID]		= "invalid service ID",
45 	[IB_CM_REJ_INVALID_TRANSPORT_TYPE]	= "invalid transport type",
46 	[IB_CM_REJ_STALE_CONN]			= "stale conn",
47 	[IB_CM_REJ_RDC_NOT_EXIST]		= "RDC not exist",
48 	[IB_CM_REJ_INVALID_GID]			= "invalid GID",
49 	[IB_CM_REJ_INVALID_LID]			= "invalid LID",
50 	[IB_CM_REJ_INVALID_SL]			= "invalid SL",
51 	[IB_CM_REJ_INVALID_TRAFFIC_CLASS]	= "invalid traffic class",
52 	[IB_CM_REJ_INVALID_HOP_LIMIT]		= "invalid hop limit",
53 	[IB_CM_REJ_INVALID_PACKET_RATE]		= "invalid packet rate",
54 	[IB_CM_REJ_INVALID_ALT_GID]		= "invalid alt GID",
55 	[IB_CM_REJ_INVALID_ALT_LID]		= "invalid alt LID",
56 	[IB_CM_REJ_INVALID_ALT_SL]		= "invalid alt SL",
57 	[IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS]	= "invalid alt traffic class",
58 	[IB_CM_REJ_INVALID_ALT_HOP_LIMIT]	= "invalid alt hop limit",
59 	[IB_CM_REJ_INVALID_ALT_PACKET_RATE]	= "invalid alt packet rate",
60 	[IB_CM_REJ_PORT_CM_REDIRECT]		= "port CM redirect",
61 	[IB_CM_REJ_PORT_REDIRECT]		= "port redirect",
62 	[IB_CM_REJ_INVALID_MTU]			= "invalid MTU",
63 	[IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES]	= "insufficient resp resources",
64 	[IB_CM_REJ_CONSUMER_DEFINED]		= "consumer defined",
65 	[IB_CM_REJ_INVALID_RNR_RETRY]		= "invalid RNR retry",
66 	[IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID]	= "duplicate local comm ID",
67 	[IB_CM_REJ_INVALID_CLASS_VERSION]	= "invalid class version",
68 	[IB_CM_REJ_INVALID_FLOW_LABEL]		= "invalid flow label",
69 	[IB_CM_REJ_INVALID_ALT_FLOW_LABEL]	= "invalid alt flow label",
70 	[IB_CM_REJ_VENDOR_OPTION_NOT_SUPPORTED] =
71 		"vendor option is not supported",
72 };
73 
74 const char *__attribute_const__ ibcm_reject_msg(int reason)
75 {
76 	size_t index = reason;
77 
78 	if (index < ARRAY_SIZE(ibcm_rej_reason_strs) &&
79 	    ibcm_rej_reason_strs[index])
80 		return ibcm_rej_reason_strs[index];
81 	else
82 		return "unrecognized reason";
83 }
84 EXPORT_SYMBOL(ibcm_reject_msg);
85 
86 struct cm_id_private;
87 struct cm_work;
88 static int cm_add_one(struct ib_device *device);
89 static void cm_remove_one(struct ib_device *device, void *client_data);
90 static void cm_process_work(struct cm_id_private *cm_id_priv,
91 			    struct cm_work *work);
92 static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
93 				   struct ib_cm_sidr_rep_param *param);
94 static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv,
95 			       const void *private_data, u8 private_data_len);
96 static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
97 			       void *private_data, u8 private_data_len);
98 static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
99 			      enum ib_cm_rej_reason reason, void *ari,
100 			      u8 ari_length, const void *private_data,
101 			      u8 private_data_len);
102 
103 static struct ib_client cm_client = {
104 	.name   = "cm",
105 	.add    = cm_add_one,
106 	.remove = cm_remove_one
107 };
108 
109 static struct ib_cm {
110 	spinlock_t lock;
111 	struct list_head device_list;
112 	rwlock_t device_lock;
113 	struct rb_root listen_service_table;
114 	u64 listen_service_id;
115 	/* struct rb_root peer_service_table; todo: fix peer to peer */
116 	struct rb_root remote_qp_table;
117 	struct rb_root remote_id_table;
118 	struct rb_root remote_sidr_table;
119 	struct xarray local_id_table;
120 	u32 local_id_next;
121 	__be32 random_id_operand;
122 	struct list_head timewait_list;
123 	struct workqueue_struct *wq;
124 	/* Sync on cm change port state */
125 	spinlock_t state_lock;
126 } cm;
127 
128 /* Counter indexes ordered by attribute ID */
129 enum {
130 	CM_REQ_COUNTER,
131 	CM_MRA_COUNTER,
132 	CM_REJ_COUNTER,
133 	CM_REP_COUNTER,
134 	CM_RTU_COUNTER,
135 	CM_DREQ_COUNTER,
136 	CM_DREP_COUNTER,
137 	CM_SIDR_REQ_COUNTER,
138 	CM_SIDR_REP_COUNTER,
139 	CM_LAP_COUNTER,
140 	CM_APR_COUNTER,
141 	CM_ATTR_COUNT,
142 	CM_ATTR_ID_OFFSET = 0x0010,
143 };
144 
145 enum {
146 	CM_XMIT,
147 	CM_XMIT_RETRIES,
148 	CM_RECV,
149 	CM_RECV_DUPLICATES,
150 	CM_COUNTER_GROUPS
151 };
152 
153 static char const counter_group_names[CM_COUNTER_GROUPS]
154 				     [sizeof("cm_rx_duplicates")] = {
155 	"cm_tx_msgs", "cm_tx_retries",
156 	"cm_rx_msgs", "cm_rx_duplicates"
157 };
158 
159 struct cm_counter_group {
160 	struct kobject obj;
161 	atomic_long_t counter[CM_ATTR_COUNT];
162 };
163 
164 struct cm_counter_attribute {
165 	struct attribute attr;
166 	int index;
167 };
168 
169 #define CM_COUNTER_ATTR(_name, _index) \
170 struct cm_counter_attribute cm_##_name##_counter_attr = { \
171 	.attr = { .name = __stringify(_name), .mode = 0444 }, \
172 	.index = _index \
173 }
174 
175 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
176 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
177 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
178 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
179 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
180 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
181 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
182 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
183 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
184 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
185 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
186 
187 static struct attribute *cm_counter_default_attrs[] = {
188 	&cm_req_counter_attr.attr,
189 	&cm_mra_counter_attr.attr,
190 	&cm_rej_counter_attr.attr,
191 	&cm_rep_counter_attr.attr,
192 	&cm_rtu_counter_attr.attr,
193 	&cm_dreq_counter_attr.attr,
194 	&cm_drep_counter_attr.attr,
195 	&cm_sidr_req_counter_attr.attr,
196 	&cm_sidr_rep_counter_attr.attr,
197 	&cm_lap_counter_attr.attr,
198 	&cm_apr_counter_attr.attr,
199 	NULL
200 };
201 
202 struct cm_port {
203 	struct cm_device *cm_dev;
204 	struct ib_mad_agent *mad_agent;
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(&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(&cm.local_id_table, cm_local_id(cm_id_priv->id.local_id),
882 		 cm_id_priv, GFP_ATOMIC);
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 	__releases(&cm_id_priv->lock)
922 {
923 	bool immediate;
924 
925 	/*
926 	 * To deliver the event to the user callback we have the drop the
927 	 * spinlock, however, we need to ensure that the user callback is single
928 	 * threaded and receives events in the temporal order. If there are
929 	 * already events being processed then thread new events onto a list,
930 	 * the thread currently processing will pick them up.
931 	 */
932 	immediate = atomic_inc_and_test(&cm_id_priv->work_count);
933 	if (!immediate) {
934 		list_add_tail(&work->list, &cm_id_priv->work_list);
935 		/*
936 		 * This routine always consumes incoming reference. Once queued
937 		 * to the work_list then a reference is held by the thread
938 		 * currently running cm_process_work() and this reference is not
939 		 * needed.
940 		 */
941 		cm_deref_id(cm_id_priv);
942 	}
943 	spin_unlock_irq(&cm_id_priv->lock);
944 
945 	if (immediate)
946 		cm_process_work(cm_id_priv, work);
947 }
948 
949 static inline int cm_convert_to_ms(int iba_time)
950 {
951 	/* approximate conversion to ms from 4.096us x 2^iba_time */
952 	return 1 << max(iba_time - 8, 0);
953 }
954 
955 /*
956  * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
957  * Because of how ack_timeout is stored, adding one doubles the timeout.
958  * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
959  * increment it (round up) only if the other is within 50%.
960  */
961 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
962 {
963 	int ack_timeout = packet_life_time + 1;
964 
965 	if (ack_timeout >= ca_ack_delay)
966 		ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
967 	else
968 		ack_timeout = ca_ack_delay +
969 			      (ack_timeout >= (ca_ack_delay - 1));
970 
971 	return min(31, ack_timeout);
972 }
973 
974 static void cm_remove_remote(struct cm_id_private *cm_id_priv)
975 {
976 	struct cm_timewait_info *timewait_info = cm_id_priv->timewait_info;
977 
978 	if (timewait_info->inserted_remote_id) {
979 		rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
980 		timewait_info->inserted_remote_id = 0;
981 	}
982 
983 	if (timewait_info->inserted_remote_qp) {
984 		rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
985 		timewait_info->inserted_remote_qp = 0;
986 	}
987 }
988 
989 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
990 {
991 	struct cm_timewait_info *timewait_info;
992 
993 	timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
994 	if (!timewait_info)
995 		return ERR_PTR(-ENOMEM);
996 
997 	timewait_info->work.local_id = local_id;
998 	INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
999 	timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
1000 	return timewait_info;
1001 }
1002 
1003 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
1004 {
1005 	int wait_time;
1006 	unsigned long flags;
1007 	struct cm_device *cm_dev;
1008 
1009 	lockdep_assert_held(&cm_id_priv->lock);
1010 
1011 	cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
1012 	if (!cm_dev)
1013 		return;
1014 
1015 	spin_lock_irqsave(&cm.lock, flags);
1016 	cm_remove_remote(cm_id_priv);
1017 	list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
1018 	spin_unlock_irqrestore(&cm.lock, flags);
1019 
1020 	/*
1021 	 * The cm_id could be destroyed by the user before we exit timewait.
1022 	 * To protect against this, we search for the cm_id after exiting
1023 	 * timewait before notifying the user that we've exited timewait.
1024 	 */
1025 	cm_id_priv->id.state = IB_CM_TIMEWAIT;
1026 	wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
1027 
1028 	/* Check if the device started its remove_one */
1029 	spin_lock_irqsave(&cm.lock, flags);
1030 	if (!cm_dev->going_down)
1031 		queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
1032 				   msecs_to_jiffies(wait_time));
1033 	spin_unlock_irqrestore(&cm.lock, flags);
1034 
1035 	/*
1036 	 * The timewait_info is converted into a work and gets freed during
1037 	 * cm_free_work() in cm_timewait_handler().
1038 	 */
1039 	BUILD_BUG_ON(offsetof(struct cm_timewait_info, work) != 0);
1040 	cm_id_priv->timewait_info = NULL;
1041 }
1042 
1043 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
1044 {
1045 	unsigned long flags;
1046 
1047 	lockdep_assert_held(&cm_id_priv->lock);
1048 
1049 	cm_id_priv->id.state = IB_CM_IDLE;
1050 	if (cm_id_priv->timewait_info) {
1051 		spin_lock_irqsave(&cm.lock, flags);
1052 		cm_remove_remote(cm_id_priv);
1053 		spin_unlock_irqrestore(&cm.lock, flags);
1054 		kfree(cm_id_priv->timewait_info);
1055 		cm_id_priv->timewait_info = NULL;
1056 	}
1057 }
1058 
1059 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
1060 {
1061 	struct cm_id_private *cm_id_priv;
1062 	struct cm_work *work;
1063 
1064 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1065 	spin_lock_irq(&cm_id_priv->lock);
1066 retest:
1067 	switch (cm_id->state) {
1068 	case IB_CM_LISTEN:
1069 		spin_lock(&cm.lock);
1070 		if (--cm_id_priv->listen_sharecount > 0) {
1071 			/* The id is still shared. */
1072 			WARN_ON(refcount_read(&cm_id_priv->refcount) == 1);
1073 			spin_unlock(&cm.lock);
1074 			spin_unlock_irq(&cm_id_priv->lock);
1075 			cm_deref_id(cm_id_priv);
1076 			return;
1077 		}
1078 		cm_id->state = IB_CM_IDLE;
1079 		rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
1080 		RB_CLEAR_NODE(&cm_id_priv->service_node);
1081 		spin_unlock(&cm.lock);
1082 		break;
1083 	case IB_CM_SIDR_REQ_SENT:
1084 		cm_id->state = IB_CM_IDLE;
1085 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1086 		break;
1087 	case IB_CM_SIDR_REQ_RCVD:
1088 		cm_send_sidr_rep_locked(cm_id_priv,
1089 					&(struct ib_cm_sidr_rep_param){
1090 						.status = IB_SIDR_REJECT });
1091 		/* cm_send_sidr_rep_locked will not move to IDLE if it fails */
1092 		cm_id->state = IB_CM_IDLE;
1093 		break;
1094 	case IB_CM_REQ_SENT:
1095 	case IB_CM_MRA_REQ_RCVD:
1096 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1097 		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_TIMEOUT,
1098 				   &cm_id_priv->id.device->node_guid,
1099 				   sizeof(cm_id_priv->id.device->node_guid),
1100 				   NULL, 0);
1101 		break;
1102 	case IB_CM_REQ_RCVD:
1103 		if (err == -ENOMEM) {
1104 			/* Do not reject to allow future retries. */
1105 			cm_reset_to_idle(cm_id_priv);
1106 		} else {
1107 			cm_send_rej_locked(cm_id_priv,
1108 					   IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
1109 					   NULL, 0);
1110 		}
1111 		break;
1112 	case IB_CM_REP_SENT:
1113 	case IB_CM_MRA_REP_RCVD:
1114 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1115 		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL,
1116 				   0, NULL, 0);
1117 		goto retest;
1118 	case IB_CM_MRA_REQ_SENT:
1119 	case IB_CM_REP_RCVD:
1120 	case IB_CM_MRA_REP_SENT:
1121 		cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL,
1122 				   0, NULL, 0);
1123 		break;
1124 	case IB_CM_ESTABLISHED:
1125 		if (cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
1126 			cm_id->state = IB_CM_IDLE;
1127 			break;
1128 		}
1129 		cm_send_dreq_locked(cm_id_priv, NULL, 0);
1130 		goto retest;
1131 	case IB_CM_DREQ_SENT:
1132 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1133 		cm_enter_timewait(cm_id_priv);
1134 		goto retest;
1135 	case IB_CM_DREQ_RCVD:
1136 		cm_send_drep_locked(cm_id_priv, NULL, 0);
1137 		WARN_ON(cm_id->state != IB_CM_TIMEWAIT);
1138 		goto retest;
1139 	case IB_CM_TIMEWAIT:
1140 		/*
1141 		 * The cm_acquire_id in cm_timewait_handler will stop working
1142 		 * once we do xa_erase below, so just move to idle here for
1143 		 * consistency.
1144 		 */
1145 		cm_id->state = IB_CM_IDLE;
1146 		break;
1147 	case IB_CM_IDLE:
1148 		break;
1149 	}
1150 	WARN_ON(cm_id->state != IB_CM_IDLE);
1151 
1152 	spin_lock(&cm.lock);
1153 	/* Required for cleanup paths related cm_req_handler() */
1154 	if (cm_id_priv->timewait_info) {
1155 		cm_remove_remote(cm_id_priv);
1156 		kfree(cm_id_priv->timewait_info);
1157 		cm_id_priv->timewait_info = NULL;
1158 	}
1159 	if (!list_empty(&cm_id_priv->altr_list) &&
1160 	    (!cm_id_priv->altr_send_port_not_ready))
1161 		list_del(&cm_id_priv->altr_list);
1162 	if (!list_empty(&cm_id_priv->prim_list) &&
1163 	    (!cm_id_priv->prim_send_port_not_ready))
1164 		list_del(&cm_id_priv->prim_list);
1165 	WARN_ON(cm_id_priv->listen_sharecount);
1166 	WARN_ON(!RB_EMPTY_NODE(&cm_id_priv->service_node));
1167 	if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
1168 		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
1169 	spin_unlock(&cm.lock);
1170 	spin_unlock_irq(&cm_id_priv->lock);
1171 
1172 	xa_erase(&cm.local_id_table, cm_local_id(cm_id->local_id));
1173 	cm_deref_id(cm_id_priv);
1174 	wait_for_completion(&cm_id_priv->comp);
1175 	while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
1176 		cm_free_work(work);
1177 
1178 	rdma_destroy_ah_attr(&cm_id_priv->av.ah_attr);
1179 	rdma_destroy_ah_attr(&cm_id_priv->alt_av.ah_attr);
1180 	kfree(cm_id_priv->private_data);
1181 	kfree_rcu(cm_id_priv, rcu);
1182 }
1183 
1184 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
1185 {
1186 	cm_destroy_id(cm_id, 0);
1187 }
1188 EXPORT_SYMBOL(ib_destroy_cm_id);
1189 
1190 static int cm_init_listen(struct cm_id_private *cm_id_priv, __be64 service_id,
1191 			  __be64 service_mask)
1192 {
1193 	service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
1194 	service_id &= service_mask;
1195 	if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
1196 	    (service_id != IB_CM_ASSIGN_SERVICE_ID))
1197 		return -EINVAL;
1198 
1199 	if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
1200 		cm_id_priv->id.service_id = cpu_to_be64(cm.listen_service_id++);
1201 		cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1202 	} else {
1203 		cm_id_priv->id.service_id = service_id;
1204 		cm_id_priv->id.service_mask = service_mask;
1205 	}
1206 	return 0;
1207 }
1208 
1209 /**
1210  * ib_cm_listen - Initiates listening on the specified service ID for
1211  *   connection and service ID resolution requests.
1212  * @cm_id: Connection identifier associated with the listen request.
1213  * @service_id: Service identifier matched against incoming connection
1214  *   and service ID resolution requests.  The service ID should be specified
1215  *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1216  *   assign a service ID to the caller.
1217  * @service_mask: Mask applied to service ID used to listen across a
1218  *   range of service IDs.  If set to 0, the service ID is matched
1219  *   exactly.  This parameter is ignored if %service_id is set to
1220  *   IB_CM_ASSIGN_SERVICE_ID.
1221  */
1222 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
1223 {
1224 	struct cm_id_private *cm_id_priv =
1225 		container_of(cm_id, struct cm_id_private, id);
1226 	unsigned long flags;
1227 	int ret;
1228 
1229 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1230 	if (cm_id_priv->id.state != IB_CM_IDLE) {
1231 		ret = -EINVAL;
1232 		goto out;
1233 	}
1234 
1235 	ret = cm_init_listen(cm_id_priv, service_id, service_mask);
1236 	if (ret)
1237 		goto out;
1238 
1239 	if (!cm_insert_listen(cm_id_priv, NULL)) {
1240 		ret = -EBUSY;
1241 		goto out;
1242 	}
1243 
1244 	cm_id_priv->id.state = IB_CM_LISTEN;
1245 	ret = 0;
1246 
1247 out:
1248 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1249 	return ret;
1250 }
1251 EXPORT_SYMBOL(ib_cm_listen);
1252 
1253 /**
1254  * Create a new listening ib_cm_id and listen on the given service ID.
1255  *
1256  * If there's an existing ID listening on that same device and service ID,
1257  * return it.
1258  *
1259  * @device: Device associated with the cm_id.  All related communication will
1260  * be associated with the specified device.
1261  * @cm_handler: Callback invoked to notify the user of CM events.
1262  * @service_id: Service identifier matched against incoming connection
1263  *   and service ID resolution requests.  The service ID should be specified
1264  *   network-byte order.  If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1265  *   assign a service ID to the caller.
1266  *
1267  * Callers should call ib_destroy_cm_id when done with the listener ID.
1268  */
1269 struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
1270 				     ib_cm_handler cm_handler,
1271 				     __be64 service_id)
1272 {
1273 	struct cm_id_private *listen_id_priv;
1274 	struct cm_id_private *cm_id_priv;
1275 	int err = 0;
1276 
1277 	/* Create an ID in advance, since the creation may sleep */
1278 	cm_id_priv = cm_alloc_id_priv(device, cm_handler, NULL);
1279 	if (IS_ERR(cm_id_priv))
1280 		return ERR_CAST(cm_id_priv);
1281 
1282 	err = cm_init_listen(cm_id_priv, service_id, 0);
1283 	if (err)
1284 		return ERR_PTR(err);
1285 
1286 	spin_lock_irq(&cm_id_priv->lock);
1287 	listen_id_priv = cm_insert_listen(cm_id_priv, cm_handler);
1288 	if (listen_id_priv != cm_id_priv) {
1289 		spin_unlock_irq(&cm_id_priv->lock);
1290 		ib_destroy_cm_id(&cm_id_priv->id);
1291 		if (!listen_id_priv)
1292 			return ERR_PTR(-EINVAL);
1293 		return &listen_id_priv->id;
1294 	}
1295 	cm_id_priv->id.state = IB_CM_LISTEN;
1296 	spin_unlock_irq(&cm_id_priv->lock);
1297 
1298 	/*
1299 	 * A listen ID does not need to be in the xarray since it does not
1300 	 * receive mads, is not placed in the remote_id or remote_qpn rbtree,
1301 	 * and does not enter timewait.
1302 	 */
1303 
1304 	return &cm_id_priv->id;
1305 }
1306 EXPORT_SYMBOL(ib_cm_insert_listen);
1307 
1308 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv)
1309 {
1310 	u64 hi_tid, low_tid;
1311 
1312 	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1313 	low_tid  = (u64)cm_id_priv->id.local_id;
1314 	return cpu_to_be64(hi_tid | low_tid);
1315 }
1316 
1317 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1318 			      __be16 attr_id, __be64 tid)
1319 {
1320 	hdr->base_version  = IB_MGMT_BASE_VERSION;
1321 	hdr->mgmt_class	   = IB_MGMT_CLASS_CM;
1322 	hdr->class_version = IB_CM_CLASS_VERSION;
1323 	hdr->method	   = IB_MGMT_METHOD_SEND;
1324 	hdr->attr_id	   = attr_id;
1325 	hdr->tid	   = tid;
1326 }
1327 
1328 static void cm_format_mad_ece_hdr(struct ib_mad_hdr *hdr, __be16 attr_id,
1329 				  __be64 tid, u32 attr_mod)
1330 {
1331 	cm_format_mad_hdr(hdr, attr_id, tid);
1332 	hdr->attr_mod = cpu_to_be32(attr_mod);
1333 }
1334 
1335 static void cm_format_req(struct cm_req_msg *req_msg,
1336 			  struct cm_id_private *cm_id_priv,
1337 			  struct ib_cm_req_param *param)
1338 {
1339 	struct sa_path_rec *pri_path = param->primary_path;
1340 	struct sa_path_rec *alt_path = param->alternate_path;
1341 	bool pri_ext = false;
1342 
1343 	if (pri_path->rec_type == SA_PATH_REC_TYPE_OPA)
1344 		pri_ext = opa_is_extended_lid(pri_path->opa.dlid,
1345 					      pri_path->opa.slid);
1346 
1347 	cm_format_mad_ece_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1348 			      cm_form_tid(cm_id_priv), param->ece.attr_mod);
1349 
1350 	IBA_SET(CM_REQ_LOCAL_COMM_ID, req_msg,
1351 		be32_to_cpu(cm_id_priv->id.local_id));
1352 	IBA_SET(CM_REQ_SERVICE_ID, req_msg, be64_to_cpu(param->service_id));
1353 	IBA_SET(CM_REQ_LOCAL_CA_GUID, req_msg,
1354 		be64_to_cpu(cm_id_priv->id.device->node_guid));
1355 	IBA_SET(CM_REQ_LOCAL_QPN, req_msg, param->qp_num);
1356 	IBA_SET(CM_REQ_INITIATOR_DEPTH, req_msg, param->initiator_depth);
1357 	IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg,
1358 		param->remote_cm_response_timeout);
1359 	cm_req_set_qp_type(req_msg, param->qp_type);
1360 	IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg, param->flow_control);
1361 	IBA_SET(CM_REQ_STARTING_PSN, req_msg, param->starting_psn);
1362 	IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg,
1363 		param->local_cm_response_timeout);
1364 	IBA_SET(CM_REQ_PARTITION_KEY, req_msg,
1365 		be16_to_cpu(param->primary_path->pkey));
1366 	IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg,
1367 		param->primary_path->mtu);
1368 	IBA_SET(CM_REQ_MAX_CM_RETRIES, req_msg, param->max_cm_retries);
1369 
1370 	if (param->qp_type != IB_QPT_XRC_INI) {
1371 		IBA_SET(CM_REQ_RESPONDER_RESOURCES, req_msg,
1372 			param->responder_resources);
1373 		IBA_SET(CM_REQ_RETRY_COUNT, req_msg, param->retry_count);
1374 		IBA_SET(CM_REQ_RNR_RETRY_COUNT, req_msg,
1375 			param->rnr_retry_count);
1376 		IBA_SET(CM_REQ_SRQ, req_msg, param->srq);
1377 	}
1378 
1379 	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) =
1380 		pri_path->sgid;
1381 	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) =
1382 		pri_path->dgid;
1383 	if (pri_ext) {
1384 		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg)
1385 			->global.interface_id =
1386 			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid));
1387 		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg)
1388 			->global.interface_id =
1389 			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid));
1390 	}
1391 	if (pri_path->hop_limit <= 1) {
1392 		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1393 			be16_to_cpu(pri_ext ? 0 :
1394 					      htons(ntohl(sa_path_get_slid(
1395 						      pri_path)))));
1396 		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1397 			be16_to_cpu(pri_ext ? 0 :
1398 					      htons(ntohl(sa_path_get_dlid(
1399 						      pri_path)))));
1400 	} else {
1401 		/* Work-around until there's a way to obtain remote LID info */
1402 		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1403 			be16_to_cpu(IB_LID_PERMISSIVE));
1404 		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1405 			be16_to_cpu(IB_LID_PERMISSIVE));
1406 	}
1407 	IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg,
1408 		be32_to_cpu(pri_path->flow_label));
1409 	IBA_SET(CM_REQ_PRIMARY_PACKET_RATE, req_msg, pri_path->rate);
1410 	IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg, pri_path->traffic_class);
1411 	IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg, pri_path->hop_limit);
1412 	IBA_SET(CM_REQ_PRIMARY_SL, req_msg, pri_path->sl);
1413 	IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg,
1414 		(pri_path->hop_limit <= 1));
1415 	IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg,
1416 		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1417 			       pri_path->packet_life_time));
1418 
1419 	if (alt_path) {
1420 		bool alt_ext = false;
1421 
1422 		if (alt_path->rec_type == SA_PATH_REC_TYPE_OPA)
1423 			alt_ext = opa_is_extended_lid(alt_path->opa.dlid,
1424 						      alt_path->opa.slid);
1425 
1426 		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg) =
1427 			alt_path->sgid;
1428 		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg) =
1429 			alt_path->dgid;
1430 		if (alt_ext) {
1431 			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1432 					req_msg)
1433 				->global.interface_id =
1434 				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid));
1435 			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID,
1436 					req_msg)
1437 				->global.interface_id =
1438 				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid));
1439 		}
1440 		if (alt_path->hop_limit <= 1) {
1441 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1442 				be16_to_cpu(
1443 					alt_ext ? 0 :
1444 						  htons(ntohl(sa_path_get_slid(
1445 							  alt_path)))));
1446 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1447 				be16_to_cpu(
1448 					alt_ext ? 0 :
1449 						  htons(ntohl(sa_path_get_dlid(
1450 							  alt_path)))));
1451 		} else {
1452 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1453 				be16_to_cpu(IB_LID_PERMISSIVE));
1454 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1455 				be16_to_cpu(IB_LID_PERMISSIVE));
1456 		}
1457 		IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg,
1458 			be32_to_cpu(alt_path->flow_label));
1459 		IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg, alt_path->rate);
1460 		IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg,
1461 			alt_path->traffic_class);
1462 		IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg,
1463 			alt_path->hop_limit);
1464 		IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, alt_path->sl);
1465 		IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg,
1466 			(alt_path->hop_limit <= 1));
1467 		IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg,
1468 			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1469 				       alt_path->packet_life_time));
1470 	}
1471 	IBA_SET(CM_REQ_VENDOR_ID, req_msg, param->ece.vendor_id);
1472 
1473 	if (param->private_data && param->private_data_len)
1474 		IBA_SET_MEM(CM_REQ_PRIVATE_DATA, req_msg, param->private_data,
1475 			    param->private_data_len);
1476 }
1477 
1478 static int cm_validate_req_param(struct ib_cm_req_param *param)
1479 {
1480 	if (!param->primary_path)
1481 		return -EINVAL;
1482 
1483 	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1484 	    param->qp_type != IB_QPT_XRC_INI)
1485 		return -EINVAL;
1486 
1487 	if (param->private_data &&
1488 	    param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1489 		return -EINVAL;
1490 
1491 	if (param->alternate_path &&
1492 	    (param->alternate_path->pkey != param->primary_path->pkey ||
1493 	     param->alternate_path->mtu != param->primary_path->mtu))
1494 		return -EINVAL;
1495 
1496 	return 0;
1497 }
1498 
1499 int ib_send_cm_req(struct ib_cm_id *cm_id,
1500 		   struct ib_cm_req_param *param)
1501 {
1502 	struct cm_id_private *cm_id_priv;
1503 	struct cm_req_msg *req_msg;
1504 	unsigned long flags;
1505 	int ret;
1506 
1507 	ret = cm_validate_req_param(param);
1508 	if (ret)
1509 		return ret;
1510 
1511 	/* Verify that we're not in timewait. */
1512 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1513 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1514 	if (cm_id->state != IB_CM_IDLE || WARN_ON(cm_id_priv->timewait_info)) {
1515 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1516 		ret = -EINVAL;
1517 		goto out;
1518 	}
1519 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1520 
1521 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1522 							    id.local_id);
1523 	if (IS_ERR(cm_id_priv->timewait_info)) {
1524 		ret = PTR_ERR(cm_id_priv->timewait_info);
1525 		goto out;
1526 	}
1527 
1528 	ret = cm_init_av_by_path(param->primary_path,
1529 				 param->ppath_sgid_attr, &cm_id_priv->av,
1530 				 cm_id_priv);
1531 	if (ret)
1532 		goto out;
1533 	if (param->alternate_path) {
1534 		ret = cm_init_av_by_path(param->alternate_path, NULL,
1535 					 &cm_id_priv->alt_av, cm_id_priv);
1536 		if (ret)
1537 			goto out;
1538 	}
1539 	cm_id->service_id = param->service_id;
1540 	cm_id->service_mask = ~cpu_to_be64(0);
1541 	cm_id_priv->timeout_ms = cm_convert_to_ms(
1542 				    param->primary_path->packet_life_time) * 2 +
1543 				 cm_convert_to_ms(
1544 				    param->remote_cm_response_timeout);
1545 	cm_id_priv->max_cm_retries = param->max_cm_retries;
1546 	cm_id_priv->initiator_depth = param->initiator_depth;
1547 	cm_id_priv->responder_resources = param->responder_resources;
1548 	cm_id_priv->retry_count = param->retry_count;
1549 	cm_id_priv->path_mtu = param->primary_path->mtu;
1550 	cm_id_priv->pkey = param->primary_path->pkey;
1551 	cm_id_priv->qp_type = param->qp_type;
1552 
1553 	ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1554 	if (ret)
1555 		goto out;
1556 
1557 	req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1558 	cm_format_req(req_msg, cm_id_priv, param);
1559 	cm_id_priv->tid = req_msg->hdr.tid;
1560 	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1561 	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1562 
1563 	cm_id_priv->local_qpn = cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
1564 	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
1565 
1566 	trace_icm_send_req(&cm_id_priv->id);
1567 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1568 	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1569 	if (ret) {
1570 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1571 		goto error2;
1572 	}
1573 	BUG_ON(cm_id->state != IB_CM_IDLE);
1574 	cm_id->state = IB_CM_REQ_SENT;
1575 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1576 	return 0;
1577 
1578 error2:	cm_free_msg(cm_id_priv->msg);
1579 out:	return ret;
1580 }
1581 EXPORT_SYMBOL(ib_send_cm_req);
1582 
1583 static int cm_issue_rej(struct cm_port *port,
1584 			struct ib_mad_recv_wc *mad_recv_wc,
1585 			enum ib_cm_rej_reason reason,
1586 			enum cm_msg_response msg_rejected,
1587 			void *ari, u8 ari_length)
1588 {
1589 	struct ib_mad_send_buf *msg = NULL;
1590 	struct cm_rej_msg *rej_msg, *rcv_msg;
1591 	int ret;
1592 
1593 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1594 	if (ret)
1595 		return ret;
1596 
1597 	/* We just need common CM header information.  Cast to any message. */
1598 	rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1599 	rej_msg = (struct cm_rej_msg *) msg->mad;
1600 
1601 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1602 	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1603 		IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg));
1604 	IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1605 		IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1606 	IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, msg_rejected);
1607 	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1608 
1609 	if (ari && ari_length) {
1610 		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1611 		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1612 	}
1613 
1614 	trace_icm_issue_rej(
1615 		IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg),
1616 		IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1617 	ret = ib_post_send_mad(msg, NULL);
1618 	if (ret)
1619 		cm_free_msg(msg);
1620 
1621 	return ret;
1622 }
1623 
1624 static bool cm_req_has_alt_path(struct cm_req_msg *req_msg)
1625 {
1626 	return ((cpu_to_be16(
1627 			IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg))) ||
1628 		(ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1629 					       req_msg))));
1630 }
1631 
1632 static void cm_path_set_rec_type(struct ib_device *ib_device, u8 port_num,
1633 				 struct sa_path_rec *path, union ib_gid *gid)
1634 {
1635 	if (ib_is_opa_gid(gid) && rdma_cap_opa_ah(ib_device, port_num))
1636 		path->rec_type = SA_PATH_REC_TYPE_OPA;
1637 	else
1638 		path->rec_type = SA_PATH_REC_TYPE_IB;
1639 }
1640 
1641 static void cm_format_path_lid_from_req(struct cm_req_msg *req_msg,
1642 					struct sa_path_rec *primary_path,
1643 					struct sa_path_rec *alt_path)
1644 {
1645 	u32 lid;
1646 
1647 	if (primary_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1648 		sa_path_set_dlid(primary_path,
1649 				 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
1650 					 req_msg));
1651 		sa_path_set_slid(primary_path,
1652 				 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
1653 					 req_msg));
1654 	} else {
1655 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1656 			CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg));
1657 		sa_path_set_dlid(primary_path, lid);
1658 
1659 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1660 			CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg));
1661 		sa_path_set_slid(primary_path, lid);
1662 	}
1663 
1664 	if (!cm_req_has_alt_path(req_msg))
1665 		return;
1666 
1667 	if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1668 		sa_path_set_dlid(alt_path,
1669 				 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
1670 					 req_msg));
1671 		sa_path_set_slid(alt_path,
1672 				 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
1673 					 req_msg));
1674 	} else {
1675 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1676 			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg));
1677 		sa_path_set_dlid(alt_path, lid);
1678 
1679 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1680 			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg));
1681 		sa_path_set_slid(alt_path, lid);
1682 	}
1683 }
1684 
1685 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1686 				     struct sa_path_rec *primary_path,
1687 				     struct sa_path_rec *alt_path)
1688 {
1689 	primary_path->dgid =
1690 		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg);
1691 	primary_path->sgid =
1692 		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg);
1693 	primary_path->flow_label =
1694 		cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg));
1695 	primary_path->hop_limit = IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg);
1696 	primary_path->traffic_class =
1697 		IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg);
1698 	primary_path->reversible = 1;
1699 	primary_path->pkey =
1700 		cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1701 	primary_path->sl = IBA_GET(CM_REQ_PRIMARY_SL, req_msg);
1702 	primary_path->mtu_selector = IB_SA_EQ;
1703 	primary_path->mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1704 	primary_path->rate_selector = IB_SA_EQ;
1705 	primary_path->rate = IBA_GET(CM_REQ_PRIMARY_PACKET_RATE, req_msg);
1706 	primary_path->packet_life_time_selector = IB_SA_EQ;
1707 	primary_path->packet_life_time =
1708 		IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg);
1709 	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1710 	primary_path->service_id =
1711 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1712 	if (sa_path_is_roce(primary_path))
1713 		primary_path->roce.route_resolved = false;
1714 
1715 	if (cm_req_has_alt_path(req_msg)) {
1716 		alt_path->dgid = *IBA_GET_MEM_PTR(
1717 			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg);
1718 		alt_path->sgid = *IBA_GET_MEM_PTR(
1719 			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg);
1720 		alt_path->flow_label = cpu_to_be32(
1721 			IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg));
1722 		alt_path->hop_limit =
1723 			IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg);
1724 		alt_path->traffic_class =
1725 			IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg);
1726 		alt_path->reversible = 1;
1727 		alt_path->pkey =
1728 			cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1729 		alt_path->sl = IBA_GET(CM_REQ_ALTERNATE_SL, req_msg);
1730 		alt_path->mtu_selector = IB_SA_EQ;
1731 		alt_path->mtu =
1732 			IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1733 		alt_path->rate_selector = IB_SA_EQ;
1734 		alt_path->rate = IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg);
1735 		alt_path->packet_life_time_selector = IB_SA_EQ;
1736 		alt_path->packet_life_time =
1737 			IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg);
1738 		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1739 		alt_path->service_id =
1740 			cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1741 
1742 		if (sa_path_is_roce(alt_path))
1743 			alt_path->roce.route_resolved = false;
1744 	}
1745 	cm_format_path_lid_from_req(req_msg, primary_path, alt_path);
1746 }
1747 
1748 static u16 cm_get_bth_pkey(struct cm_work *work)
1749 {
1750 	struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1751 	u8 port_num = work->port->port_num;
1752 	u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1753 	u16 pkey;
1754 	int ret;
1755 
1756 	ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1757 	if (ret) {
1758 		dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1759 				     port_num, pkey_index, ret);
1760 		return 0;
1761 	}
1762 
1763 	return pkey;
1764 }
1765 
1766 /**
1767  * Convert OPA SGID to IB SGID
1768  * ULPs (such as IPoIB) do not understand OPA GIDs and will
1769  * reject them as the local_gid will not match the sgid. Therefore,
1770  * change the pathrec's SGID to an IB SGID.
1771  *
1772  * @work: Work completion
1773  * @path: Path record
1774  */
1775 static void cm_opa_to_ib_sgid(struct cm_work *work,
1776 			      struct sa_path_rec *path)
1777 {
1778 	struct ib_device *dev = work->port->cm_dev->ib_device;
1779 	u8 port_num = work->port->port_num;
1780 
1781 	if (rdma_cap_opa_ah(dev, port_num) &&
1782 	    (ib_is_opa_gid(&path->sgid))) {
1783 		union ib_gid sgid;
1784 
1785 		if (rdma_query_gid(dev, port_num, 0, &sgid)) {
1786 			dev_warn(&dev->dev,
1787 				 "Error updating sgid in CM request\n");
1788 			return;
1789 		}
1790 
1791 		path->sgid = sgid;
1792 	}
1793 }
1794 
1795 static void cm_format_req_event(struct cm_work *work,
1796 				struct cm_id_private *cm_id_priv,
1797 				struct ib_cm_id *listen_id)
1798 {
1799 	struct cm_req_msg *req_msg;
1800 	struct ib_cm_req_event_param *param;
1801 
1802 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1803 	param = &work->cm_event.param.req_rcvd;
1804 	param->listen_id = listen_id;
1805 	param->bth_pkey = cm_get_bth_pkey(work);
1806 	param->port = cm_id_priv->av.port->port_num;
1807 	param->primary_path = &work->path[0];
1808 	cm_opa_to_ib_sgid(work, param->primary_path);
1809 	if (cm_req_has_alt_path(req_msg)) {
1810 		param->alternate_path = &work->path[1];
1811 		cm_opa_to_ib_sgid(work, param->alternate_path);
1812 	} else {
1813 		param->alternate_path = NULL;
1814 	}
1815 	param->remote_ca_guid =
1816 		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
1817 	param->remote_qkey = IBA_GET(CM_REQ_LOCAL_Q_KEY, req_msg);
1818 	param->remote_qpn = IBA_GET(CM_REQ_LOCAL_QPN, req_msg);
1819 	param->qp_type = cm_req_get_qp_type(req_msg);
1820 	param->starting_psn = IBA_GET(CM_REQ_STARTING_PSN, req_msg);
1821 	param->responder_resources = IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
1822 	param->initiator_depth = IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
1823 	param->local_cm_response_timeout =
1824 		IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg);
1825 	param->flow_control = IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg);
1826 	param->remote_cm_response_timeout =
1827 		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg);
1828 	param->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
1829 	param->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
1830 	param->srq = IBA_GET(CM_REQ_SRQ, req_msg);
1831 	param->ppath_sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
1832 	param->ece.vendor_id = IBA_GET(CM_REQ_VENDOR_ID, req_msg);
1833 	param->ece.attr_mod = be32_to_cpu(req_msg->hdr.attr_mod);
1834 
1835 	work->cm_event.private_data =
1836 		IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA, req_msg);
1837 }
1838 
1839 static void cm_process_work(struct cm_id_private *cm_id_priv,
1840 			    struct cm_work *work)
1841 {
1842 	int ret;
1843 
1844 	/* We will typically only have the current event to report. */
1845 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1846 	cm_free_work(work);
1847 
1848 	while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1849 		spin_lock_irq(&cm_id_priv->lock);
1850 		work = cm_dequeue_work(cm_id_priv);
1851 		spin_unlock_irq(&cm_id_priv->lock);
1852 		if (!work)
1853 			return;
1854 
1855 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1856 						&work->cm_event);
1857 		cm_free_work(work);
1858 	}
1859 	cm_deref_id(cm_id_priv);
1860 	if (ret)
1861 		cm_destroy_id(&cm_id_priv->id, ret);
1862 }
1863 
1864 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1865 			  struct cm_id_private *cm_id_priv,
1866 			  enum cm_msg_response msg_mraed, u8 service_timeout,
1867 			  const void *private_data, u8 private_data_len)
1868 {
1869 	cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1870 	IBA_SET(CM_MRA_MESSAGE_MRAED, mra_msg, msg_mraed);
1871 	IBA_SET(CM_MRA_LOCAL_COMM_ID, mra_msg,
1872 		be32_to_cpu(cm_id_priv->id.local_id));
1873 	IBA_SET(CM_MRA_REMOTE_COMM_ID, mra_msg,
1874 		be32_to_cpu(cm_id_priv->id.remote_id));
1875 	IBA_SET(CM_MRA_SERVICE_TIMEOUT, mra_msg, service_timeout);
1876 
1877 	if (private_data && private_data_len)
1878 		IBA_SET_MEM(CM_MRA_PRIVATE_DATA, mra_msg, private_data,
1879 			    private_data_len);
1880 }
1881 
1882 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1883 			  struct cm_id_private *cm_id_priv,
1884 			  enum ib_cm_rej_reason reason, void *ari,
1885 			  u8 ari_length, const void *private_data,
1886 			  u8 private_data_len, enum ib_cm_state state)
1887 {
1888 	lockdep_assert_held(&cm_id_priv->lock);
1889 
1890 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1891 	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1892 		be32_to_cpu(cm_id_priv->id.remote_id));
1893 
1894 	switch (state) {
1895 	case IB_CM_REQ_RCVD:
1896 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, be32_to_cpu(0));
1897 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1898 		break;
1899 	case IB_CM_MRA_REQ_SENT:
1900 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1901 			be32_to_cpu(cm_id_priv->id.local_id));
1902 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1903 		break;
1904 	case IB_CM_REP_RCVD:
1905 	case IB_CM_MRA_REP_SENT:
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, CM_MSG_RESPONSE_REP);
1909 		break;
1910 	default:
1911 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1912 			be32_to_cpu(cm_id_priv->id.local_id));
1913 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg,
1914 			CM_MSG_RESPONSE_OTHER);
1915 		break;
1916 	}
1917 
1918 	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1919 	if (ari && ari_length) {
1920 		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1921 		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1922 	}
1923 
1924 	if (private_data && private_data_len)
1925 		IBA_SET_MEM(CM_REJ_PRIVATE_DATA, rej_msg, private_data,
1926 			    private_data_len);
1927 }
1928 
1929 static void cm_dup_req_handler(struct cm_work *work,
1930 			       struct cm_id_private *cm_id_priv)
1931 {
1932 	struct ib_mad_send_buf *msg = NULL;
1933 	int ret;
1934 
1935 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1936 			counter[CM_REQ_COUNTER]);
1937 
1938 	/* Quick state check to discard duplicate REQs. */
1939 	spin_lock_irq(&cm_id_priv->lock);
1940 	if (cm_id_priv->id.state == IB_CM_REQ_RCVD) {
1941 		spin_unlock_irq(&cm_id_priv->lock);
1942 		return;
1943 	}
1944 	spin_unlock_irq(&cm_id_priv->lock);
1945 
1946 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1947 	if (ret)
1948 		return;
1949 
1950 	spin_lock_irq(&cm_id_priv->lock);
1951 	switch (cm_id_priv->id.state) {
1952 	case IB_CM_MRA_REQ_SENT:
1953 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1954 			      CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1955 			      cm_id_priv->private_data,
1956 			      cm_id_priv->private_data_len);
1957 		break;
1958 	case IB_CM_TIMEWAIT:
1959 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv,
1960 			      IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0,
1961 			      IB_CM_TIMEWAIT);
1962 		break;
1963 	default:
1964 		goto unlock;
1965 	}
1966 	spin_unlock_irq(&cm_id_priv->lock);
1967 
1968 	trace_icm_send_dup_req(&cm_id_priv->id);
1969 	ret = ib_post_send_mad(msg, NULL);
1970 	if (ret)
1971 		goto free;
1972 	return;
1973 
1974 unlock:	spin_unlock_irq(&cm_id_priv->lock);
1975 free:	cm_free_msg(msg);
1976 }
1977 
1978 static struct cm_id_private * cm_match_req(struct cm_work *work,
1979 					   struct cm_id_private *cm_id_priv)
1980 {
1981 	struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1982 	struct cm_timewait_info *timewait_info;
1983 	struct cm_req_msg *req_msg;
1984 
1985 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1986 
1987 	/* Check for possible duplicate REQ. */
1988 	spin_lock_irq(&cm.lock);
1989 	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1990 	if (timewait_info) {
1991 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
1992 					   timewait_info->work.remote_id);
1993 		spin_unlock_irq(&cm.lock);
1994 		if (cur_cm_id_priv) {
1995 			cm_dup_req_handler(work, cur_cm_id_priv);
1996 			cm_deref_id(cur_cm_id_priv);
1997 		}
1998 		return NULL;
1999 	}
2000 
2001 	/* Check for stale connections. */
2002 	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2003 	if (timewait_info) {
2004 		cm_remove_remote(cm_id_priv);
2005 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2006 					   timewait_info->work.remote_id);
2007 
2008 		spin_unlock_irq(&cm.lock);
2009 		cm_issue_rej(work->port, work->mad_recv_wc,
2010 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
2011 			     NULL, 0);
2012 		if (cur_cm_id_priv) {
2013 			ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2014 			cm_deref_id(cur_cm_id_priv);
2015 		}
2016 		return NULL;
2017 	}
2018 
2019 	/* Find matching listen request. */
2020 	listen_cm_id_priv = cm_find_listen(
2021 		cm_id_priv->id.device,
2022 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)));
2023 	if (!listen_cm_id_priv) {
2024 		cm_remove_remote(cm_id_priv);
2025 		spin_unlock_irq(&cm.lock);
2026 		cm_issue_rej(work->port, work->mad_recv_wc,
2027 			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
2028 			     NULL, 0);
2029 		return NULL;
2030 	}
2031 	spin_unlock_irq(&cm.lock);
2032 	return listen_cm_id_priv;
2033 }
2034 
2035 /*
2036  * Work-around for inter-subnet connections.  If the LIDs are permissive,
2037  * we need to override the LID/SL data in the REQ with the LID information
2038  * in the work completion.
2039  */
2040 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
2041 {
2042 	if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg)) {
2043 		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
2044 					req_msg)) == IB_LID_PERMISSIVE) {
2045 			IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
2046 				be16_to_cpu(ib_lid_be16(wc->slid)));
2047 			IBA_SET(CM_REQ_PRIMARY_SL, req_msg, wc->sl);
2048 		}
2049 
2050 		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
2051 					req_msg)) == IB_LID_PERMISSIVE)
2052 			IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
2053 				wc->dlid_path_bits);
2054 	}
2055 
2056 	if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg)) {
2057 		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
2058 					req_msg)) == IB_LID_PERMISSIVE) {
2059 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
2060 				be16_to_cpu(ib_lid_be16(wc->slid)));
2061 			IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, wc->sl);
2062 		}
2063 
2064 		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
2065 					req_msg)) == IB_LID_PERMISSIVE)
2066 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
2067 				wc->dlid_path_bits);
2068 	}
2069 }
2070 
2071 static int cm_req_handler(struct cm_work *work)
2072 {
2073 	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
2074 	struct cm_req_msg *req_msg;
2075 	const struct ib_global_route *grh;
2076 	const struct ib_gid_attr *gid_attr;
2077 	int ret;
2078 
2079 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
2080 
2081 	cm_id_priv =
2082 		cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
2083 	if (IS_ERR(cm_id_priv))
2084 		return PTR_ERR(cm_id_priv);
2085 
2086 	cm_id_priv->id.remote_id =
2087 		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg));
2088 	cm_id_priv->id.service_id =
2089 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
2090 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
2091 	cm_id_priv->tid = req_msg->hdr.tid;
2092 	cm_id_priv->timeout_ms = cm_convert_to_ms(
2093 		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg));
2094 	cm_id_priv->max_cm_retries = IBA_GET(CM_REQ_MAX_CM_RETRIES, req_msg);
2095 	cm_id_priv->remote_qpn =
2096 		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
2097 	cm_id_priv->initiator_depth =
2098 		IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
2099 	cm_id_priv->responder_resources =
2100 		IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
2101 	cm_id_priv->path_mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
2102 	cm_id_priv->pkey = cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
2103 	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
2104 	cm_id_priv->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
2105 	cm_id_priv->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
2106 	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
2107 
2108 	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2109 				      work->mad_recv_wc->recv_buf.grh,
2110 				      &cm_id_priv->av);
2111 	if (ret)
2112 		goto destroy;
2113 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
2114 							    id.local_id);
2115 	if (IS_ERR(cm_id_priv->timewait_info)) {
2116 		ret = PTR_ERR(cm_id_priv->timewait_info);
2117 		goto destroy;
2118 	}
2119 	cm_id_priv->timewait_info->work.remote_id = cm_id_priv->id.remote_id;
2120 	cm_id_priv->timewait_info->remote_ca_guid =
2121 		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
2122 	cm_id_priv->timewait_info->remote_qpn = cm_id_priv->remote_qpn;
2123 
2124 	/*
2125 	 * Note that the ID pointer is not in the xarray at this point,
2126 	 * so this set is only visible to the local thread.
2127 	 */
2128 	cm_id_priv->id.state = IB_CM_REQ_RCVD;
2129 
2130 	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
2131 	if (!listen_cm_id_priv) {
2132 		trace_icm_no_listener_err(&cm_id_priv->id);
2133 		cm_id_priv->id.state = IB_CM_IDLE;
2134 		ret = -EINVAL;
2135 		goto destroy;
2136 	}
2137 
2138 	cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
2139 
2140 	memset(&work->path[0], 0, sizeof(work->path[0]));
2141 	if (cm_req_has_alt_path(req_msg))
2142 		memset(&work->path[1], 0, sizeof(work->path[1]));
2143 	grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
2144 	gid_attr = grh->sgid_attr;
2145 
2146 	if (gid_attr &&
2147 	    rdma_protocol_roce(work->port->cm_dev->ib_device,
2148 			       work->port->port_num)) {
2149 		work->path[0].rec_type =
2150 			sa_conv_gid_to_pathrec_type(gid_attr->gid_type);
2151 	} else {
2152 		cm_path_set_rec_type(
2153 			work->port->cm_dev->ib_device, work->port->port_num,
2154 			&work->path[0],
2155 			IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID,
2156 					req_msg));
2157 	}
2158 	if (cm_req_has_alt_path(req_msg))
2159 		work->path[1].rec_type = work->path[0].rec_type;
2160 	cm_format_paths_from_req(req_msg, &work->path[0],
2161 				 &work->path[1]);
2162 	if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE)
2163 		sa_path_set_dmac(&work->path[0],
2164 				 cm_id_priv->av.ah_attr.roce.dmac);
2165 	work->path[0].hop_limit = grh->hop_limit;
2166 	ret = cm_init_av_by_path(&work->path[0], gid_attr, &cm_id_priv->av,
2167 				 cm_id_priv);
2168 	if (ret) {
2169 		int err;
2170 
2171 		err = rdma_query_gid(work->port->cm_dev->ib_device,
2172 				     work->port->port_num, 0,
2173 				     &work->path[0].sgid);
2174 		if (err)
2175 			ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2176 				       NULL, 0, NULL, 0);
2177 		else
2178 			ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2179 				       &work->path[0].sgid,
2180 				       sizeof(work->path[0].sgid),
2181 				       NULL, 0);
2182 		goto rejected;
2183 	}
2184 	if (cm_req_has_alt_path(req_msg)) {
2185 		ret = cm_init_av_by_path(&work->path[1], NULL,
2186 					 &cm_id_priv->alt_av, cm_id_priv);
2187 		if (ret) {
2188 			ib_send_cm_rej(&cm_id_priv->id,
2189 				       IB_CM_REJ_INVALID_ALT_GID,
2190 				       &work->path[0].sgid,
2191 				       sizeof(work->path[0].sgid), NULL, 0);
2192 			goto rejected;
2193 		}
2194 	}
2195 
2196 	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
2197 	cm_id_priv->id.context = listen_cm_id_priv->id.context;
2198 	cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
2199 
2200 	/* Now MAD handlers can see the new ID */
2201 	spin_lock_irq(&cm_id_priv->lock);
2202 	cm_finalize_id(cm_id_priv);
2203 
2204 	/* Refcount belongs to the event, pairs with cm_process_work() */
2205 	refcount_inc(&cm_id_priv->refcount);
2206 	cm_queue_work_unlock(cm_id_priv, work);
2207 	/*
2208 	 * Since this ID was just created and was not made visible to other MAD
2209 	 * handlers until the cm_finalize_id() above we know that the
2210 	 * cm_process_work() will deliver the event and the listen_cm_id
2211 	 * embedded in the event can be derefed here.
2212 	 */
2213 	cm_deref_id(listen_cm_id_priv);
2214 	return 0;
2215 
2216 rejected:
2217 	cm_deref_id(listen_cm_id_priv);
2218 destroy:
2219 	ib_destroy_cm_id(&cm_id_priv->id);
2220 	return ret;
2221 }
2222 
2223 static void cm_format_rep(struct cm_rep_msg *rep_msg,
2224 			  struct cm_id_private *cm_id_priv,
2225 			  struct ib_cm_rep_param *param)
2226 {
2227 	cm_format_mad_ece_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid,
2228 			      param->ece.attr_mod);
2229 	IBA_SET(CM_REP_LOCAL_COMM_ID, rep_msg,
2230 		be32_to_cpu(cm_id_priv->id.local_id));
2231 	IBA_SET(CM_REP_REMOTE_COMM_ID, rep_msg,
2232 		be32_to_cpu(cm_id_priv->id.remote_id));
2233 	IBA_SET(CM_REP_STARTING_PSN, rep_msg, param->starting_psn);
2234 	IBA_SET(CM_REP_RESPONDER_RESOURCES, rep_msg,
2235 		param->responder_resources);
2236 	IBA_SET(CM_REP_TARGET_ACK_DELAY, rep_msg,
2237 		cm_id_priv->av.port->cm_dev->ack_delay);
2238 	IBA_SET(CM_REP_FAILOVER_ACCEPTED, rep_msg, param->failover_accepted);
2239 	IBA_SET(CM_REP_RNR_RETRY_COUNT, rep_msg, param->rnr_retry_count);
2240 	IBA_SET(CM_REP_LOCAL_CA_GUID, rep_msg,
2241 		be64_to_cpu(cm_id_priv->id.device->node_guid));
2242 
2243 	if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
2244 		IBA_SET(CM_REP_INITIATOR_DEPTH, rep_msg,
2245 			param->initiator_depth);
2246 		IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg,
2247 			param->flow_control);
2248 		IBA_SET(CM_REP_SRQ, rep_msg, param->srq);
2249 		IBA_SET(CM_REP_LOCAL_QPN, rep_msg, param->qp_num);
2250 	} else {
2251 		IBA_SET(CM_REP_SRQ, rep_msg, 1);
2252 		IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER, rep_msg, param->qp_num);
2253 	}
2254 
2255 	IBA_SET(CM_REP_VENDOR_ID_L, rep_msg, param->ece.vendor_id);
2256 	IBA_SET(CM_REP_VENDOR_ID_M, rep_msg, param->ece.vendor_id >> 8);
2257 	IBA_SET(CM_REP_VENDOR_ID_H, rep_msg, param->ece.vendor_id >> 16);
2258 
2259 	if (param->private_data && param->private_data_len)
2260 		IBA_SET_MEM(CM_REP_PRIVATE_DATA, rep_msg, param->private_data,
2261 			    param->private_data_len);
2262 }
2263 
2264 int ib_send_cm_rep(struct ib_cm_id *cm_id,
2265 		   struct ib_cm_rep_param *param)
2266 {
2267 	struct cm_id_private *cm_id_priv;
2268 	struct ib_mad_send_buf *msg;
2269 	struct cm_rep_msg *rep_msg;
2270 	unsigned long flags;
2271 	int ret;
2272 
2273 	if (param->private_data &&
2274 	    param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
2275 		return -EINVAL;
2276 
2277 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2278 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2279 	if (cm_id->state != IB_CM_REQ_RCVD &&
2280 	    cm_id->state != IB_CM_MRA_REQ_SENT) {
2281 		trace_icm_send_rep_err(cm_id_priv->id.local_id, cm_id->state);
2282 		ret = -EINVAL;
2283 		goto out;
2284 	}
2285 
2286 	ret = cm_alloc_msg(cm_id_priv, &msg);
2287 	if (ret)
2288 		goto out;
2289 
2290 	rep_msg = (struct cm_rep_msg *) msg->mad;
2291 	cm_format_rep(rep_msg, cm_id_priv, param);
2292 	msg->timeout_ms = cm_id_priv->timeout_ms;
2293 	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
2294 
2295 	trace_icm_send_rep(cm_id);
2296 	ret = ib_post_send_mad(msg, NULL);
2297 	if (ret) {
2298 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2299 		cm_free_msg(msg);
2300 		return ret;
2301 	}
2302 
2303 	cm_id->state = IB_CM_REP_SENT;
2304 	cm_id_priv->msg = msg;
2305 	cm_id_priv->initiator_depth = param->initiator_depth;
2306 	cm_id_priv->responder_resources = param->responder_resources;
2307 	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2308 	WARN_ONCE(param->qp_num & 0xFF000000,
2309 		  "IBTA declares QPN to be 24 bits, but it is 0x%X\n",
2310 		  param->qp_num);
2311 	cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
2312 
2313 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2314 	return ret;
2315 }
2316 EXPORT_SYMBOL(ib_send_cm_rep);
2317 
2318 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
2319 			  struct cm_id_private *cm_id_priv,
2320 			  const void *private_data,
2321 			  u8 private_data_len)
2322 {
2323 	cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
2324 	IBA_SET(CM_RTU_LOCAL_COMM_ID, rtu_msg,
2325 		be32_to_cpu(cm_id_priv->id.local_id));
2326 	IBA_SET(CM_RTU_REMOTE_COMM_ID, rtu_msg,
2327 		be32_to_cpu(cm_id_priv->id.remote_id));
2328 
2329 	if (private_data && private_data_len)
2330 		IBA_SET_MEM(CM_RTU_PRIVATE_DATA, rtu_msg, private_data,
2331 			    private_data_len);
2332 }
2333 
2334 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
2335 		   const void *private_data,
2336 		   u8 private_data_len)
2337 {
2338 	struct cm_id_private *cm_id_priv;
2339 	struct ib_mad_send_buf *msg;
2340 	unsigned long flags;
2341 	void *data;
2342 	int ret;
2343 
2344 	if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
2345 		return -EINVAL;
2346 
2347 	data = cm_copy_private_data(private_data, private_data_len);
2348 	if (IS_ERR(data))
2349 		return PTR_ERR(data);
2350 
2351 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2352 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2353 	if (cm_id->state != IB_CM_REP_RCVD &&
2354 	    cm_id->state != IB_CM_MRA_REP_SENT) {
2355 		trace_icm_send_cm_rtu_err(cm_id);
2356 		ret = -EINVAL;
2357 		goto error;
2358 	}
2359 
2360 	ret = cm_alloc_msg(cm_id_priv, &msg);
2361 	if (ret)
2362 		goto error;
2363 
2364 	cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2365 		      private_data, private_data_len);
2366 
2367 	trace_icm_send_rtu(cm_id);
2368 	ret = ib_post_send_mad(msg, NULL);
2369 	if (ret) {
2370 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2371 		cm_free_msg(msg);
2372 		kfree(data);
2373 		return ret;
2374 	}
2375 
2376 	cm_id->state = IB_CM_ESTABLISHED;
2377 	cm_set_private_data(cm_id_priv, data, private_data_len);
2378 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2379 	return 0;
2380 
2381 error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2382 	kfree(data);
2383 	return ret;
2384 }
2385 EXPORT_SYMBOL(ib_send_cm_rtu);
2386 
2387 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
2388 {
2389 	struct cm_rep_msg *rep_msg;
2390 	struct ib_cm_rep_event_param *param;
2391 
2392 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2393 	param = &work->cm_event.param.rep_rcvd;
2394 	param->remote_ca_guid =
2395 		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2396 	param->remote_qkey = IBA_GET(CM_REP_LOCAL_Q_KEY, rep_msg);
2397 	param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
2398 	param->starting_psn = IBA_GET(CM_REP_STARTING_PSN, rep_msg);
2399 	param->responder_resources = IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2400 	param->initiator_depth = IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2401 	param->target_ack_delay = IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2402 	param->failover_accepted = IBA_GET(CM_REP_FAILOVER_ACCEPTED, rep_msg);
2403 	param->flow_control = IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg);
2404 	param->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2405 	param->srq = IBA_GET(CM_REP_SRQ, rep_msg);
2406 	param->ece.vendor_id = IBA_GET(CM_REP_VENDOR_ID_H, rep_msg) << 16;
2407 	param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_M, rep_msg) << 8;
2408 	param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_L, rep_msg);
2409 	param->ece.attr_mod = be32_to_cpu(rep_msg->hdr.attr_mod);
2410 
2411 	work->cm_event.private_data =
2412 		IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA, rep_msg);
2413 }
2414 
2415 static void cm_dup_rep_handler(struct cm_work *work)
2416 {
2417 	struct cm_id_private *cm_id_priv;
2418 	struct cm_rep_msg *rep_msg;
2419 	struct ib_mad_send_buf *msg = NULL;
2420 	int ret;
2421 
2422 	rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2423 	cm_id_priv = cm_acquire_id(
2424 		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)),
2425 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)));
2426 	if (!cm_id_priv)
2427 		return;
2428 
2429 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2430 			counter[CM_REP_COUNTER]);
2431 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
2432 	if (ret)
2433 		goto deref;
2434 
2435 	spin_lock_irq(&cm_id_priv->lock);
2436 	if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
2437 		cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2438 			      cm_id_priv->private_data,
2439 			      cm_id_priv->private_data_len);
2440 	else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
2441 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2442 			      CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
2443 			      cm_id_priv->private_data,
2444 			      cm_id_priv->private_data_len);
2445 	else
2446 		goto unlock;
2447 	spin_unlock_irq(&cm_id_priv->lock);
2448 
2449 	trace_icm_send_dup_rep(&cm_id_priv->id);
2450 	ret = ib_post_send_mad(msg, NULL);
2451 	if (ret)
2452 		goto free;
2453 	goto deref;
2454 
2455 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2456 free:	cm_free_msg(msg);
2457 deref:	cm_deref_id(cm_id_priv);
2458 }
2459 
2460 static int cm_rep_handler(struct cm_work *work)
2461 {
2462 	struct cm_id_private *cm_id_priv;
2463 	struct cm_rep_msg *rep_msg;
2464 	int ret;
2465 	struct cm_id_private *cur_cm_id_priv;
2466 	struct cm_timewait_info *timewait_info;
2467 
2468 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2469 	cm_id_priv = cm_acquire_id(
2470 		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 0);
2471 	if (!cm_id_priv) {
2472 		cm_dup_rep_handler(work);
2473 		trace_icm_remote_no_priv_err(
2474 			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2475 		return -EINVAL;
2476 	}
2477 
2478 	cm_format_rep_event(work, cm_id_priv->qp_type);
2479 
2480 	spin_lock_irq(&cm_id_priv->lock);
2481 	switch (cm_id_priv->id.state) {
2482 	case IB_CM_REQ_SENT:
2483 	case IB_CM_MRA_REQ_RCVD:
2484 		break;
2485 	default:
2486 		ret = -EINVAL;
2487 		trace_icm_rep_unknown_err(
2488 			IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2489 			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg),
2490 			cm_id_priv->id.state);
2491 		spin_unlock_irq(&cm_id_priv->lock);
2492 		goto error;
2493 	}
2494 
2495 	cm_id_priv->timewait_info->work.remote_id =
2496 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2497 	cm_id_priv->timewait_info->remote_ca_guid =
2498 		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2499 	cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2500 
2501 	spin_lock(&cm.lock);
2502 	/* Check for duplicate REP. */
2503 	if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2504 		spin_unlock(&cm.lock);
2505 		spin_unlock_irq(&cm_id_priv->lock);
2506 		ret = -EINVAL;
2507 		trace_icm_insert_failed_err(
2508 			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2509 		goto error;
2510 	}
2511 	/* Check for a stale connection. */
2512 	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2513 	if (timewait_info) {
2514 		cm_remove_remote(cm_id_priv);
2515 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2516 					   timewait_info->work.remote_id);
2517 
2518 		spin_unlock(&cm.lock);
2519 		spin_unlock_irq(&cm_id_priv->lock);
2520 		cm_issue_rej(work->port, work->mad_recv_wc,
2521 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2522 			     NULL, 0);
2523 		ret = -EINVAL;
2524 		trace_icm_staleconn_err(
2525 			IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2526 			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2527 
2528 		if (cur_cm_id_priv) {
2529 			ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2530 			cm_deref_id(cur_cm_id_priv);
2531 		}
2532 
2533 		goto error;
2534 	}
2535 	spin_unlock(&cm.lock);
2536 
2537 	cm_id_priv->id.state = IB_CM_REP_RCVD;
2538 	cm_id_priv->id.remote_id =
2539 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2540 	cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2541 	cm_id_priv->initiator_depth =
2542 		IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2543 	cm_id_priv->responder_resources =
2544 		IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2545 	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2546 	cm_id_priv->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2547 	cm_id_priv->target_ack_delay =
2548 		IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2549 	cm_id_priv->av.timeout =
2550 			cm_ack_timeout(cm_id_priv->target_ack_delay,
2551 				       cm_id_priv->av.timeout - 1);
2552 	cm_id_priv->alt_av.timeout =
2553 			cm_ack_timeout(cm_id_priv->target_ack_delay,
2554 				       cm_id_priv->alt_av.timeout - 1);
2555 
2556 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2557 	cm_queue_work_unlock(cm_id_priv, work);
2558 	return 0;
2559 
2560 error:
2561 	cm_deref_id(cm_id_priv);
2562 	return ret;
2563 }
2564 
2565 static int cm_establish_handler(struct cm_work *work)
2566 {
2567 	struct cm_id_private *cm_id_priv;
2568 
2569 	/* See comment in cm_establish about lookup. */
2570 	cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2571 	if (!cm_id_priv)
2572 		return -EINVAL;
2573 
2574 	spin_lock_irq(&cm_id_priv->lock);
2575 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2576 		spin_unlock_irq(&cm_id_priv->lock);
2577 		goto out;
2578 	}
2579 
2580 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2581 	cm_queue_work_unlock(cm_id_priv, work);
2582 	return 0;
2583 out:
2584 	cm_deref_id(cm_id_priv);
2585 	return -EINVAL;
2586 }
2587 
2588 static int cm_rtu_handler(struct cm_work *work)
2589 {
2590 	struct cm_id_private *cm_id_priv;
2591 	struct cm_rtu_msg *rtu_msg;
2592 
2593 	rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2594 	cm_id_priv = cm_acquire_id(
2595 		cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID, rtu_msg)),
2596 		cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID, rtu_msg)));
2597 	if (!cm_id_priv)
2598 		return -EINVAL;
2599 
2600 	work->cm_event.private_data =
2601 		IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA, rtu_msg);
2602 
2603 	spin_lock_irq(&cm_id_priv->lock);
2604 	if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2605 	    cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2606 		spin_unlock_irq(&cm_id_priv->lock);
2607 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2608 				counter[CM_RTU_COUNTER]);
2609 		goto out;
2610 	}
2611 	cm_id_priv->id.state = IB_CM_ESTABLISHED;
2612 
2613 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2614 	cm_queue_work_unlock(cm_id_priv, work);
2615 	return 0;
2616 out:
2617 	cm_deref_id(cm_id_priv);
2618 	return -EINVAL;
2619 }
2620 
2621 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2622 			  struct cm_id_private *cm_id_priv,
2623 			  const void *private_data,
2624 			  u8 private_data_len)
2625 {
2626 	cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2627 			  cm_form_tid(cm_id_priv));
2628 	IBA_SET(CM_DREQ_LOCAL_COMM_ID, dreq_msg,
2629 		be32_to_cpu(cm_id_priv->id.local_id));
2630 	IBA_SET(CM_DREQ_REMOTE_COMM_ID, dreq_msg,
2631 		be32_to_cpu(cm_id_priv->id.remote_id));
2632 	IBA_SET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg,
2633 		be32_to_cpu(cm_id_priv->remote_qpn));
2634 
2635 	if (private_data && private_data_len)
2636 		IBA_SET_MEM(CM_DREQ_PRIVATE_DATA, dreq_msg, private_data,
2637 			    private_data_len);
2638 }
2639 
2640 static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv,
2641 			       const void *private_data, u8 private_data_len)
2642 {
2643 	struct ib_mad_send_buf *msg;
2644 	int ret;
2645 
2646 	lockdep_assert_held(&cm_id_priv->lock);
2647 
2648 	if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2649 		return -EINVAL;
2650 
2651 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2652 		trace_icm_dreq_skipped(&cm_id_priv->id);
2653 		return -EINVAL;
2654 	}
2655 
2656 	if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2657 	    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2658 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2659 
2660 	ret = cm_alloc_msg(cm_id_priv, &msg);
2661 	if (ret) {
2662 		cm_enter_timewait(cm_id_priv);
2663 		return ret;
2664 	}
2665 
2666 	cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2667 		       private_data, private_data_len);
2668 	msg->timeout_ms = cm_id_priv->timeout_ms;
2669 	msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2670 
2671 	trace_icm_send_dreq(&cm_id_priv->id);
2672 	ret = ib_post_send_mad(msg, NULL);
2673 	if (ret) {
2674 		cm_enter_timewait(cm_id_priv);
2675 		cm_free_msg(msg);
2676 		return ret;
2677 	}
2678 
2679 	cm_id_priv->id.state = IB_CM_DREQ_SENT;
2680 	cm_id_priv->msg = msg;
2681 	return 0;
2682 }
2683 
2684 int ib_send_cm_dreq(struct ib_cm_id *cm_id, const void *private_data,
2685 		    u8 private_data_len)
2686 {
2687 	struct cm_id_private *cm_id_priv =
2688 		container_of(cm_id, struct cm_id_private, id);
2689 	unsigned long flags;
2690 	int ret;
2691 
2692 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2693 	ret = cm_send_dreq_locked(cm_id_priv, private_data, private_data_len);
2694 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2695 	return ret;
2696 }
2697 EXPORT_SYMBOL(ib_send_cm_dreq);
2698 
2699 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2700 			  struct cm_id_private *cm_id_priv,
2701 			  const void *private_data,
2702 			  u8 private_data_len)
2703 {
2704 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2705 	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2706 		be32_to_cpu(cm_id_priv->id.local_id));
2707 	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2708 		be32_to_cpu(cm_id_priv->id.remote_id));
2709 
2710 	if (private_data && private_data_len)
2711 		IBA_SET_MEM(CM_DREP_PRIVATE_DATA, drep_msg, private_data,
2712 			    private_data_len);
2713 }
2714 
2715 static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
2716 			       void *private_data, u8 private_data_len)
2717 {
2718 	struct ib_mad_send_buf *msg;
2719 	int ret;
2720 
2721 	lockdep_assert_held(&cm_id_priv->lock);
2722 
2723 	if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2724 		return -EINVAL;
2725 
2726 	if (cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2727 		trace_icm_send_drep_err(&cm_id_priv->id);
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 	trace_icm_send_drep(&cm_id_priv->id);
2743 	ret = ib_post_send_mad(msg, NULL);
2744 	if (ret) {
2745 		cm_free_msg(msg);
2746 		return ret;
2747 	}
2748 	return 0;
2749 }
2750 
2751 int ib_send_cm_drep(struct ib_cm_id *cm_id, const void *private_data,
2752 		    u8 private_data_len)
2753 {
2754 	struct cm_id_private *cm_id_priv =
2755 		container_of(cm_id, struct cm_id_private, id);
2756 	unsigned long flags;
2757 	void *data;
2758 	int ret;
2759 
2760 	data = cm_copy_private_data(private_data, private_data_len);
2761 	if (IS_ERR(data))
2762 		return PTR_ERR(data);
2763 
2764 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2765 	ret = cm_send_drep_locked(cm_id_priv, data, private_data_len);
2766 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2767 	return ret;
2768 }
2769 EXPORT_SYMBOL(ib_send_cm_drep);
2770 
2771 static int cm_issue_drep(struct cm_port *port,
2772 			 struct ib_mad_recv_wc *mad_recv_wc)
2773 {
2774 	struct ib_mad_send_buf *msg = NULL;
2775 	struct cm_dreq_msg *dreq_msg;
2776 	struct cm_drep_msg *drep_msg;
2777 	int ret;
2778 
2779 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2780 	if (ret)
2781 		return ret;
2782 
2783 	dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2784 	drep_msg = (struct cm_drep_msg *) msg->mad;
2785 
2786 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2787 	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2788 		IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg));
2789 	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2790 		IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2791 
2792 	trace_icm_issue_drep(
2793 		IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2794 		IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2795 	ret = ib_post_send_mad(msg, NULL);
2796 	if (ret)
2797 		cm_free_msg(msg);
2798 
2799 	return ret;
2800 }
2801 
2802 static int cm_dreq_handler(struct cm_work *work)
2803 {
2804 	struct cm_id_private *cm_id_priv;
2805 	struct cm_dreq_msg *dreq_msg;
2806 	struct ib_mad_send_buf *msg = NULL;
2807 
2808 	dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2809 	cm_id_priv = cm_acquire_id(
2810 		cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)),
2811 		cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg)));
2812 	if (!cm_id_priv) {
2813 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2814 				counter[CM_DREQ_COUNTER]);
2815 		cm_issue_drep(work->port, work->mad_recv_wc);
2816 		trace_icm_no_priv_err(
2817 			IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2818 			IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2819 		return -EINVAL;
2820 	}
2821 
2822 	work->cm_event.private_data =
2823 		IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA, dreq_msg);
2824 
2825 	spin_lock_irq(&cm_id_priv->lock);
2826 	if (cm_id_priv->local_qpn !=
2827 	    cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg)))
2828 		goto unlock;
2829 
2830 	switch (cm_id_priv->id.state) {
2831 	case IB_CM_REP_SENT:
2832 	case IB_CM_DREQ_SENT:
2833 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2834 		break;
2835 	case IB_CM_ESTABLISHED:
2836 		if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2837 		    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2838 			ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2839 		break;
2840 	case IB_CM_MRA_REP_RCVD:
2841 		break;
2842 	case IB_CM_TIMEWAIT:
2843 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2844 				counter[CM_DREQ_COUNTER]);
2845 		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
2846 		if (IS_ERR(msg))
2847 			goto unlock;
2848 
2849 		cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2850 			       cm_id_priv->private_data,
2851 			       cm_id_priv->private_data_len);
2852 		spin_unlock_irq(&cm_id_priv->lock);
2853 
2854 		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
2855 		    ib_post_send_mad(msg, NULL))
2856 			cm_free_msg(msg);
2857 		goto deref;
2858 	case IB_CM_DREQ_RCVD:
2859 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2860 				counter[CM_DREQ_COUNTER]);
2861 		goto unlock;
2862 	default:
2863 		trace_icm_dreq_unknown_err(&cm_id_priv->id);
2864 		goto unlock;
2865 	}
2866 	cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2867 	cm_id_priv->tid = dreq_msg->hdr.tid;
2868 	cm_queue_work_unlock(cm_id_priv, work);
2869 	return 0;
2870 
2871 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2872 deref:	cm_deref_id(cm_id_priv);
2873 	return -EINVAL;
2874 }
2875 
2876 static int cm_drep_handler(struct cm_work *work)
2877 {
2878 	struct cm_id_private *cm_id_priv;
2879 	struct cm_drep_msg *drep_msg;
2880 
2881 	drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2882 	cm_id_priv = cm_acquire_id(
2883 		cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID, drep_msg)),
2884 		cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID, drep_msg)));
2885 	if (!cm_id_priv)
2886 		return -EINVAL;
2887 
2888 	work->cm_event.private_data =
2889 		IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA, drep_msg);
2890 
2891 	spin_lock_irq(&cm_id_priv->lock);
2892 	if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2893 	    cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2894 		spin_unlock_irq(&cm_id_priv->lock);
2895 		goto out;
2896 	}
2897 	cm_enter_timewait(cm_id_priv);
2898 
2899 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2900 	cm_queue_work_unlock(cm_id_priv, work);
2901 	return 0;
2902 out:
2903 	cm_deref_id(cm_id_priv);
2904 	return -EINVAL;
2905 }
2906 
2907 static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
2908 			      enum ib_cm_rej_reason reason, void *ari,
2909 			      u8 ari_length, const void *private_data,
2910 			      u8 private_data_len)
2911 {
2912 	enum ib_cm_state state = cm_id_priv->id.state;
2913 	struct ib_mad_send_buf *msg;
2914 	int ret;
2915 
2916 	lockdep_assert_held(&cm_id_priv->lock);
2917 
2918 	if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2919 	    (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2920 		return -EINVAL;
2921 
2922 	switch (state) {
2923 	case IB_CM_REQ_SENT:
2924 	case IB_CM_MRA_REQ_RCVD:
2925 	case IB_CM_REQ_RCVD:
2926 	case IB_CM_MRA_REQ_SENT:
2927 	case IB_CM_REP_RCVD:
2928 	case IB_CM_MRA_REP_SENT:
2929 		cm_reset_to_idle(cm_id_priv);
2930 		ret = cm_alloc_msg(cm_id_priv, &msg);
2931 		if (ret)
2932 			return ret;
2933 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2934 			      ari, ari_length, private_data, private_data_len,
2935 			      state);
2936 		break;
2937 	case IB_CM_REP_SENT:
2938 	case IB_CM_MRA_REP_RCVD:
2939 		cm_enter_timewait(cm_id_priv);
2940 		ret = cm_alloc_msg(cm_id_priv, &msg);
2941 		if (ret)
2942 			return ret;
2943 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2944 			      ari, ari_length, private_data, private_data_len,
2945 			      state);
2946 		break;
2947 	default:
2948 		trace_icm_send_unknown_rej_err(&cm_id_priv->id);
2949 		return -EINVAL;
2950 	}
2951 
2952 	trace_icm_send_rej(&cm_id_priv->id, reason);
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 		fallthrough;
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 		fallthrough;
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 		fallthrough;
3061 	default:
3062 		trace_icm_rej_unknown_err(&cm_id_priv->id);
3063 		spin_unlock_irq(&cm_id_priv->lock);
3064 		goto out;
3065 	}
3066 
3067 	cm_queue_work_unlock(cm_id_priv, work);
3068 	return 0;
3069 out:
3070 	cm_deref_id(cm_id_priv);
3071 	return -EINVAL;
3072 }
3073 
3074 int ib_send_cm_mra(struct ib_cm_id *cm_id,
3075 		   u8 service_timeout,
3076 		   const void *private_data,
3077 		   u8 private_data_len)
3078 {
3079 	struct cm_id_private *cm_id_priv;
3080 	struct ib_mad_send_buf *msg;
3081 	enum ib_cm_state cm_state;
3082 	enum ib_cm_lap_state lap_state;
3083 	enum cm_msg_response msg_response;
3084 	void *data;
3085 	unsigned long flags;
3086 	int ret;
3087 
3088 	if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
3089 		return -EINVAL;
3090 
3091 	data = cm_copy_private_data(private_data, private_data_len);
3092 	if (IS_ERR(data))
3093 		return PTR_ERR(data);
3094 
3095 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3096 
3097 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3098 	switch(cm_id_priv->id.state) {
3099 	case IB_CM_REQ_RCVD:
3100 		cm_state = IB_CM_MRA_REQ_SENT;
3101 		lap_state = cm_id->lap_state;
3102 		msg_response = CM_MSG_RESPONSE_REQ;
3103 		break;
3104 	case IB_CM_REP_RCVD:
3105 		cm_state = IB_CM_MRA_REP_SENT;
3106 		lap_state = cm_id->lap_state;
3107 		msg_response = CM_MSG_RESPONSE_REP;
3108 		break;
3109 	case IB_CM_ESTABLISHED:
3110 		if (cm_id->lap_state == IB_CM_LAP_RCVD) {
3111 			cm_state = cm_id->state;
3112 			lap_state = IB_CM_MRA_LAP_SENT;
3113 			msg_response = CM_MSG_RESPONSE_OTHER;
3114 			break;
3115 		}
3116 		fallthrough;
3117 	default:
3118 		trace_icm_send_mra_unknown_err(&cm_id_priv->id);
3119 		ret = -EINVAL;
3120 		goto error1;
3121 	}
3122 
3123 	if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
3124 		ret = cm_alloc_msg(cm_id_priv, &msg);
3125 		if (ret)
3126 			goto error1;
3127 
3128 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3129 			      msg_response, service_timeout,
3130 			      private_data, private_data_len);
3131 		trace_icm_send_mra(cm_id);
3132 		ret = ib_post_send_mad(msg, NULL);
3133 		if (ret)
3134 			goto error2;
3135 	}
3136 
3137 	cm_id->state = cm_state;
3138 	cm_id->lap_state = lap_state;
3139 	cm_id_priv->service_timeout = service_timeout;
3140 	cm_set_private_data(cm_id_priv, data, private_data_len);
3141 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3142 	return 0;
3143 
3144 error1:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3145 	kfree(data);
3146 	return ret;
3147 
3148 error2:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3149 	kfree(data);
3150 	cm_free_msg(msg);
3151 	return ret;
3152 }
3153 EXPORT_SYMBOL(ib_send_cm_mra);
3154 
3155 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
3156 {
3157 	switch (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg)) {
3158 	case CM_MSG_RESPONSE_REQ:
3159 		return cm_acquire_id(
3160 			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3161 			0);
3162 	case CM_MSG_RESPONSE_REP:
3163 	case CM_MSG_RESPONSE_OTHER:
3164 		return cm_acquire_id(
3165 			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3166 			cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID, mra_msg)));
3167 	default:
3168 		return NULL;
3169 	}
3170 }
3171 
3172 static int cm_mra_handler(struct cm_work *work)
3173 {
3174 	struct cm_id_private *cm_id_priv;
3175 	struct cm_mra_msg *mra_msg;
3176 	int timeout;
3177 
3178 	mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
3179 	cm_id_priv = cm_acquire_mraed_id(mra_msg);
3180 	if (!cm_id_priv)
3181 		return -EINVAL;
3182 
3183 	work->cm_event.private_data =
3184 		IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA, mra_msg);
3185 	work->cm_event.param.mra_rcvd.service_timeout =
3186 		IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg);
3187 	timeout = cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg)) +
3188 		  cm_convert_to_ms(cm_id_priv->av.timeout);
3189 
3190 	spin_lock_irq(&cm_id_priv->lock);
3191 	switch (cm_id_priv->id.state) {
3192 	case IB_CM_REQ_SENT:
3193 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3194 			    CM_MSG_RESPONSE_REQ ||
3195 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3196 				  cm_id_priv->msg, timeout))
3197 			goto out;
3198 		cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
3199 		break;
3200 	case IB_CM_REP_SENT:
3201 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3202 			    CM_MSG_RESPONSE_REP ||
3203 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3204 				  cm_id_priv->msg, timeout))
3205 			goto out;
3206 		cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
3207 		break;
3208 	case IB_CM_ESTABLISHED:
3209 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3210 			    CM_MSG_RESPONSE_OTHER ||
3211 		    cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
3212 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3213 				  cm_id_priv->msg, timeout)) {
3214 			if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
3215 				atomic_long_inc(&work->port->
3216 						counter_group[CM_RECV_DUPLICATES].
3217 						counter[CM_MRA_COUNTER]);
3218 			goto out;
3219 		}
3220 		cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
3221 		break;
3222 	case IB_CM_MRA_REQ_RCVD:
3223 	case IB_CM_MRA_REP_RCVD:
3224 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3225 				counter[CM_MRA_COUNTER]);
3226 		fallthrough;
3227 	default:
3228 		trace_icm_mra_unknown_err(&cm_id_priv->id);
3229 		goto out;
3230 	}
3231 
3232 	cm_id_priv->msg->context[1] = (void *) (unsigned long)
3233 				      cm_id_priv->id.state;
3234 	cm_queue_work_unlock(cm_id_priv, work);
3235 	return 0;
3236 out:
3237 	spin_unlock_irq(&cm_id_priv->lock);
3238 	cm_deref_id(cm_id_priv);
3239 	return -EINVAL;
3240 }
3241 
3242 static void cm_format_path_lid_from_lap(struct cm_lap_msg *lap_msg,
3243 					struct sa_path_rec *path)
3244 {
3245 	u32 lid;
3246 
3247 	if (path->rec_type != SA_PATH_REC_TYPE_OPA) {
3248 		sa_path_set_dlid(path, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID,
3249 					       lap_msg));
3250 		sa_path_set_slid(path, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID,
3251 					       lap_msg));
3252 	} else {
3253 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3254 			CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg));
3255 		sa_path_set_dlid(path, lid);
3256 
3257 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3258 			CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg));
3259 		sa_path_set_slid(path, lid);
3260 	}
3261 }
3262 
3263 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
3264 				    struct sa_path_rec *path,
3265 				    struct cm_lap_msg *lap_msg)
3266 {
3267 	path->dgid = *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg);
3268 	path->sgid =
3269 		*IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg);
3270 	path->flow_label =
3271 		cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL, lap_msg));
3272 	path->hop_limit = IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT, lap_msg);
3273 	path->traffic_class = IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS, lap_msg);
3274 	path->reversible = 1;
3275 	path->pkey = cm_id_priv->pkey;
3276 	path->sl = IBA_GET(CM_LAP_ALTERNATE_SL, lap_msg);
3277 	path->mtu_selector = IB_SA_EQ;
3278 	path->mtu = cm_id_priv->path_mtu;
3279 	path->rate_selector = IB_SA_EQ;
3280 	path->rate = IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE, lap_msg);
3281 	path->packet_life_time_selector = IB_SA_EQ;
3282 	path->packet_life_time =
3283 		IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT, lap_msg);
3284 	path->packet_life_time -= (path->packet_life_time > 0);
3285 	cm_format_path_lid_from_lap(lap_msg, path);
3286 }
3287 
3288 static int cm_lap_handler(struct cm_work *work)
3289 {
3290 	struct cm_id_private *cm_id_priv;
3291 	struct cm_lap_msg *lap_msg;
3292 	struct ib_cm_lap_event_param *param;
3293 	struct ib_mad_send_buf *msg = NULL;
3294 	int ret;
3295 
3296 	/* Currently Alternate path messages are not supported for
3297 	 * RoCE link layer.
3298 	 */
3299 	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3300 			       work->port->port_num))
3301 		return -EINVAL;
3302 
3303 	/* todo: verify LAP request and send reject APR if invalid. */
3304 	lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
3305 	cm_id_priv = cm_acquire_id(
3306 		cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID, lap_msg)),
3307 		cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID, lap_msg)));
3308 	if (!cm_id_priv)
3309 		return -EINVAL;
3310 
3311 	param = &work->cm_event.param.lap_rcvd;
3312 	memset(&work->path[0], 0, sizeof(work->path[1]));
3313 	cm_path_set_rec_type(work->port->cm_dev->ib_device,
3314 			     work->port->port_num, &work->path[0],
3315 			     IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID,
3316 					     lap_msg));
3317 	param->alternate_path = &work->path[0];
3318 	cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
3319 	work->cm_event.private_data =
3320 		IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA, lap_msg);
3321 
3322 	spin_lock_irq(&cm_id_priv->lock);
3323 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
3324 		goto unlock;
3325 
3326 	switch (cm_id_priv->id.lap_state) {
3327 	case IB_CM_LAP_UNINIT:
3328 	case IB_CM_LAP_IDLE:
3329 		break;
3330 	case IB_CM_MRA_LAP_SENT:
3331 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3332 				counter[CM_LAP_COUNTER]);
3333 		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
3334 		if (IS_ERR(msg))
3335 			goto unlock;
3336 
3337 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3338 			      CM_MSG_RESPONSE_OTHER,
3339 			      cm_id_priv->service_timeout,
3340 			      cm_id_priv->private_data,
3341 			      cm_id_priv->private_data_len);
3342 		spin_unlock_irq(&cm_id_priv->lock);
3343 
3344 		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
3345 		    ib_post_send_mad(msg, NULL))
3346 			cm_free_msg(msg);
3347 		goto deref;
3348 	case IB_CM_LAP_RCVD:
3349 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3350 				counter[CM_LAP_COUNTER]);
3351 		goto unlock;
3352 	default:
3353 		goto unlock;
3354 	}
3355 
3356 	ret = cm_init_av_for_lap(work->port, work->mad_recv_wc->wc,
3357 				 work->mad_recv_wc->recv_buf.grh,
3358 				 &cm_id_priv->av);
3359 	if (ret)
3360 		goto unlock;
3361 
3362 	ret = cm_init_av_by_path(param->alternate_path, NULL,
3363 				 &cm_id_priv->alt_av, cm_id_priv);
3364 	if (ret)
3365 		goto unlock;
3366 
3367 	cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
3368 	cm_id_priv->tid = lap_msg->hdr.tid;
3369 	cm_queue_work_unlock(cm_id_priv, work);
3370 	return 0;
3371 
3372 unlock:	spin_unlock_irq(&cm_id_priv->lock);
3373 deref:	cm_deref_id(cm_id_priv);
3374 	return -EINVAL;
3375 }
3376 
3377 static int cm_apr_handler(struct cm_work *work)
3378 {
3379 	struct cm_id_private *cm_id_priv;
3380 	struct cm_apr_msg *apr_msg;
3381 
3382 	/* Currently Alternate path messages are not supported for
3383 	 * RoCE link layer.
3384 	 */
3385 	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3386 			       work->port->port_num))
3387 		return -EINVAL;
3388 
3389 	apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3390 	cm_id_priv = cm_acquire_id(
3391 		cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID, apr_msg)),
3392 		cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID, apr_msg)));
3393 	if (!cm_id_priv)
3394 		return -EINVAL; /* Unmatched reply. */
3395 
3396 	work->cm_event.param.apr_rcvd.ap_status =
3397 		IBA_GET(CM_APR_AR_STATUS, apr_msg);
3398 	work->cm_event.param.apr_rcvd.apr_info =
3399 		IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION, apr_msg);
3400 	work->cm_event.param.apr_rcvd.info_len =
3401 		IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH, apr_msg);
3402 	work->cm_event.private_data =
3403 		IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA, apr_msg);
3404 
3405 	spin_lock_irq(&cm_id_priv->lock);
3406 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
3407 	    (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
3408 	     cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
3409 		spin_unlock_irq(&cm_id_priv->lock);
3410 		goto out;
3411 	}
3412 	cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
3413 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3414 	cm_id_priv->msg = NULL;
3415 	cm_queue_work_unlock(cm_id_priv, work);
3416 	return 0;
3417 out:
3418 	cm_deref_id(cm_id_priv);
3419 	return -EINVAL;
3420 }
3421 
3422 static int cm_timewait_handler(struct cm_work *work)
3423 {
3424 	struct cm_timewait_info *timewait_info;
3425 	struct cm_id_private *cm_id_priv;
3426 
3427 	timewait_info = container_of(work, struct cm_timewait_info, work);
3428 	spin_lock_irq(&cm.lock);
3429 	list_del(&timewait_info->list);
3430 	spin_unlock_irq(&cm.lock);
3431 
3432 	cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3433 				   timewait_info->work.remote_id);
3434 	if (!cm_id_priv)
3435 		return -EINVAL;
3436 
3437 	spin_lock_irq(&cm_id_priv->lock);
3438 	if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3439 	    cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3440 		spin_unlock_irq(&cm_id_priv->lock);
3441 		goto out;
3442 	}
3443 	cm_id_priv->id.state = IB_CM_IDLE;
3444 	cm_queue_work_unlock(cm_id_priv, work);
3445 	return 0;
3446 out:
3447 	cm_deref_id(cm_id_priv);
3448 	return -EINVAL;
3449 }
3450 
3451 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3452 			       struct cm_id_private *cm_id_priv,
3453 			       struct ib_cm_sidr_req_param *param)
3454 {
3455 	cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3456 			  cm_form_tid(cm_id_priv));
3457 	IBA_SET(CM_SIDR_REQ_REQUESTID, sidr_req_msg,
3458 		be32_to_cpu(cm_id_priv->id.local_id));
3459 	IBA_SET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg,
3460 		be16_to_cpu(param->path->pkey));
3461 	IBA_SET(CM_SIDR_REQ_SERVICEID, sidr_req_msg,
3462 		be64_to_cpu(param->service_id));
3463 
3464 	if (param->private_data && param->private_data_len)
3465 		IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg,
3466 			    param->private_data, param->private_data_len);
3467 }
3468 
3469 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3470 			struct ib_cm_sidr_req_param *param)
3471 {
3472 	struct cm_id_private *cm_id_priv;
3473 	struct ib_mad_send_buf *msg;
3474 	unsigned long flags;
3475 	int ret;
3476 
3477 	if (!param->path || (param->private_data &&
3478 	     param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3479 		return -EINVAL;
3480 
3481 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3482 	ret = cm_init_av_by_path(param->path, param->sgid_attr,
3483 				 &cm_id_priv->av,
3484 				 cm_id_priv);
3485 	if (ret)
3486 		goto out;
3487 
3488 	cm_id->service_id = param->service_id;
3489 	cm_id->service_mask = ~cpu_to_be64(0);
3490 	cm_id_priv->timeout_ms = param->timeout_ms;
3491 	cm_id_priv->max_cm_retries = param->max_cm_retries;
3492 	ret = cm_alloc_msg(cm_id_priv, &msg);
3493 	if (ret)
3494 		goto out;
3495 
3496 	cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3497 			   param);
3498 	msg->timeout_ms = cm_id_priv->timeout_ms;
3499 	msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3500 
3501 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3502 	if (cm_id->state == IB_CM_IDLE) {
3503 		trace_icm_send_sidr_req(&cm_id_priv->id);
3504 		ret = ib_post_send_mad(msg, NULL);
3505 	} else {
3506 		ret = -EINVAL;
3507 	}
3508 
3509 	if (ret) {
3510 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3511 		cm_free_msg(msg);
3512 		goto out;
3513 	}
3514 	cm_id->state = IB_CM_SIDR_REQ_SENT;
3515 	cm_id_priv->msg = msg;
3516 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3517 out:
3518 	return ret;
3519 }
3520 EXPORT_SYMBOL(ib_send_cm_sidr_req);
3521 
3522 static void cm_format_sidr_req_event(struct cm_work *work,
3523 				     const struct cm_id_private *rx_cm_id,
3524 				     struct ib_cm_id *listen_id)
3525 {
3526 	struct cm_sidr_req_msg *sidr_req_msg;
3527 	struct ib_cm_sidr_req_event_param *param;
3528 
3529 	sidr_req_msg = (struct cm_sidr_req_msg *)
3530 				work->mad_recv_wc->recv_buf.mad;
3531 	param = &work->cm_event.param.sidr_req_rcvd;
3532 	param->pkey = IBA_GET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg);
3533 	param->listen_id = listen_id;
3534 	param->service_id =
3535 		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3536 	param->bth_pkey = cm_get_bth_pkey(work);
3537 	param->port = work->port->port_num;
3538 	param->sgid_attr = rx_cm_id->av.ah_attr.grh.sgid_attr;
3539 	work->cm_event.private_data =
3540 		IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg);
3541 }
3542 
3543 static int cm_sidr_req_handler(struct cm_work *work)
3544 {
3545 	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
3546 	struct cm_sidr_req_msg *sidr_req_msg;
3547 	struct ib_wc *wc;
3548 	int ret;
3549 
3550 	cm_id_priv =
3551 		cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
3552 	if (IS_ERR(cm_id_priv))
3553 		return PTR_ERR(cm_id_priv);
3554 
3555 	/* Record SGID/SLID and request ID for lookup. */
3556 	sidr_req_msg = (struct cm_sidr_req_msg *)
3557 				work->mad_recv_wc->recv_buf.mad;
3558 
3559 	cm_id_priv->id.remote_id =
3560 		cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID, sidr_req_msg));
3561 	cm_id_priv->id.service_id =
3562 		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3563 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3564 	cm_id_priv->tid = sidr_req_msg->hdr.tid;
3565 
3566 	wc = work->mad_recv_wc->wc;
3567 	cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3568 	cm_id_priv->av.dgid.global.interface_id = 0;
3569 	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3570 				      work->mad_recv_wc->recv_buf.grh,
3571 				      &cm_id_priv->av);
3572 	if (ret)
3573 		goto out;
3574 
3575 	spin_lock_irq(&cm.lock);
3576 	listen_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3577 	if (listen_cm_id_priv) {
3578 		spin_unlock_irq(&cm.lock);
3579 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3580 				counter[CM_SIDR_REQ_COUNTER]);
3581 		goto out; /* Duplicate message. */
3582 	}
3583 	cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3584 	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
3585 					   cm_id_priv->id.service_id);
3586 	if (!listen_cm_id_priv) {
3587 		spin_unlock_irq(&cm.lock);
3588 		ib_send_cm_sidr_rep(&cm_id_priv->id,
3589 				    &(struct ib_cm_sidr_rep_param){
3590 					    .status = IB_SIDR_UNSUPPORTED });
3591 		goto out; /* No match. */
3592 	}
3593 	spin_unlock_irq(&cm.lock);
3594 
3595 	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
3596 	cm_id_priv->id.context = listen_cm_id_priv->id.context;
3597 
3598 	/*
3599 	 * A SIDR ID does not need to be in the xarray since it does not receive
3600 	 * mads, is not placed in the remote_id or remote_qpn rbtree, and does
3601 	 * not enter timewait.
3602 	 */
3603 
3604 	cm_format_sidr_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
3605 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
3606 	cm_free_work(work);
3607 	/*
3608 	 * A pointer to the listen_cm_id is held in the event, so this deref
3609 	 * must be after the event is delivered above.
3610 	 */
3611 	cm_deref_id(listen_cm_id_priv);
3612 	if (ret)
3613 		cm_destroy_id(&cm_id_priv->id, ret);
3614 	return 0;
3615 out:
3616 	ib_destroy_cm_id(&cm_id_priv->id);
3617 	return -EINVAL;
3618 }
3619 
3620 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3621 			       struct cm_id_private *cm_id_priv,
3622 			       struct ib_cm_sidr_rep_param *param)
3623 {
3624 	cm_format_mad_ece_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3625 			      cm_id_priv->tid, param->ece.attr_mod);
3626 	IBA_SET(CM_SIDR_REP_REQUESTID, sidr_rep_msg,
3627 		be32_to_cpu(cm_id_priv->id.remote_id));
3628 	IBA_SET(CM_SIDR_REP_STATUS, sidr_rep_msg, param->status);
3629 	IBA_SET(CM_SIDR_REP_QPN, sidr_rep_msg, param->qp_num);
3630 	IBA_SET(CM_SIDR_REP_SERVICEID, sidr_rep_msg,
3631 		be64_to_cpu(cm_id_priv->id.service_id));
3632 	IBA_SET(CM_SIDR_REP_Q_KEY, sidr_rep_msg, param->qkey);
3633 	IBA_SET(CM_SIDR_REP_VENDOR_ID_L, sidr_rep_msg,
3634 		param->ece.vendor_id & 0xFF);
3635 	IBA_SET(CM_SIDR_REP_VENDOR_ID_H, sidr_rep_msg,
3636 		(param->ece.vendor_id >> 8) & 0xFF);
3637 
3638 	if (param->info && param->info_length)
3639 		IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION, sidr_rep_msg,
3640 			    param->info, param->info_length);
3641 
3642 	if (param->private_data && param->private_data_len)
3643 		IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg,
3644 			    param->private_data, param->private_data_len);
3645 }
3646 
3647 static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
3648 				   struct ib_cm_sidr_rep_param *param)
3649 {
3650 	struct ib_mad_send_buf *msg;
3651 	int ret;
3652 
3653 	lockdep_assert_held(&cm_id_priv->lock);
3654 
3655 	if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3656 	    (param->private_data &&
3657 	     param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3658 		return -EINVAL;
3659 
3660 	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_RCVD)
3661 		return -EINVAL;
3662 
3663 	ret = cm_alloc_msg(cm_id_priv, &msg);
3664 	if (ret)
3665 		return ret;
3666 
3667 	cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3668 			   param);
3669 	trace_icm_send_sidr_rep(&cm_id_priv->id);
3670 	ret = ib_post_send_mad(msg, NULL);
3671 	if (ret) {
3672 		cm_free_msg(msg);
3673 		return ret;
3674 	}
3675 	cm_id_priv->id.state = IB_CM_IDLE;
3676 	spin_lock_irq(&cm.lock);
3677 	if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3678 		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3679 		RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3680 	}
3681 	spin_unlock_irq(&cm.lock);
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 	trace_icm_mad_send_err(state, wc_status);
3768 	switch (state) {
3769 	case IB_CM_REQ_SENT:
3770 	case IB_CM_MRA_REQ_RCVD:
3771 		cm_reset_to_idle(cm_id_priv);
3772 		cm_event.event = IB_CM_REQ_ERROR;
3773 		break;
3774 	case IB_CM_REP_SENT:
3775 	case IB_CM_MRA_REP_RCVD:
3776 		cm_reset_to_idle(cm_id_priv);
3777 		cm_event.event = IB_CM_REP_ERROR;
3778 		break;
3779 	case IB_CM_DREQ_SENT:
3780 		cm_enter_timewait(cm_id_priv);
3781 		cm_event.event = IB_CM_DREQ_ERROR;
3782 		break;
3783 	case IB_CM_SIDR_REQ_SENT:
3784 		cm_id_priv->id.state = IB_CM_IDLE;
3785 		cm_event.event = IB_CM_SIDR_REQ_ERROR;
3786 		break;
3787 	default:
3788 		goto discard;
3789 	}
3790 	spin_unlock_irq(&cm_id_priv->lock);
3791 	cm_event.param.send_status = wc_status;
3792 
3793 	/* No other events can occur on the cm_id at this point. */
3794 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3795 	cm_free_msg(msg);
3796 	if (ret)
3797 		ib_destroy_cm_id(&cm_id_priv->id);
3798 	return;
3799 discard:
3800 	spin_unlock_irq(&cm_id_priv->lock);
3801 	cm_free_msg(msg);
3802 }
3803 
3804 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3805 			    struct ib_mad_send_wc *mad_send_wc)
3806 {
3807 	struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3808 	struct cm_port *port;
3809 	u16 attr_index;
3810 
3811 	port = mad_agent->context;
3812 	attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3813 				  msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3814 
3815 	/*
3816 	 * If the send was in response to a received message (context[0] is not
3817 	 * set to a cm_id), and is not a REJ, then it is a send that was
3818 	 * manually retried.
3819 	 */
3820 	if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3821 		msg->retries = 1;
3822 
3823 	atomic_long_add(1 + msg->retries,
3824 			&port->counter_group[CM_XMIT].counter[attr_index]);
3825 	if (msg->retries)
3826 		atomic_long_add(msg->retries,
3827 				&port->counter_group[CM_XMIT_RETRIES].
3828 				counter[attr_index]);
3829 
3830 	switch (mad_send_wc->status) {
3831 	case IB_WC_SUCCESS:
3832 	case IB_WC_WR_FLUSH_ERR:
3833 		cm_free_msg(msg);
3834 		break;
3835 	default:
3836 		if (msg->context[0] && msg->context[1])
3837 			cm_process_send_error(msg, mad_send_wc->status);
3838 		else
3839 			cm_free_msg(msg);
3840 		break;
3841 	}
3842 }
3843 
3844 static void cm_work_handler(struct work_struct *_work)
3845 {
3846 	struct cm_work *work = container_of(_work, struct cm_work, work.work);
3847 	int ret;
3848 
3849 	switch (work->cm_event.event) {
3850 	case IB_CM_REQ_RECEIVED:
3851 		ret = cm_req_handler(work);
3852 		break;
3853 	case IB_CM_MRA_RECEIVED:
3854 		ret = cm_mra_handler(work);
3855 		break;
3856 	case IB_CM_REJ_RECEIVED:
3857 		ret = cm_rej_handler(work);
3858 		break;
3859 	case IB_CM_REP_RECEIVED:
3860 		ret = cm_rep_handler(work);
3861 		break;
3862 	case IB_CM_RTU_RECEIVED:
3863 		ret = cm_rtu_handler(work);
3864 		break;
3865 	case IB_CM_USER_ESTABLISHED:
3866 		ret = cm_establish_handler(work);
3867 		break;
3868 	case IB_CM_DREQ_RECEIVED:
3869 		ret = cm_dreq_handler(work);
3870 		break;
3871 	case IB_CM_DREP_RECEIVED:
3872 		ret = cm_drep_handler(work);
3873 		break;
3874 	case IB_CM_SIDR_REQ_RECEIVED:
3875 		ret = cm_sidr_req_handler(work);
3876 		break;
3877 	case IB_CM_SIDR_REP_RECEIVED:
3878 		ret = cm_sidr_rep_handler(work);
3879 		break;
3880 	case IB_CM_LAP_RECEIVED:
3881 		ret = cm_lap_handler(work);
3882 		break;
3883 	case IB_CM_APR_RECEIVED:
3884 		ret = cm_apr_handler(work);
3885 		break;
3886 	case IB_CM_TIMEWAIT_EXIT:
3887 		ret = cm_timewait_handler(work);
3888 		break;
3889 	default:
3890 		trace_icm_handler_err(work->cm_event.event);
3891 		ret = -EINVAL;
3892 		break;
3893 	}
3894 	if (ret)
3895 		cm_free_work(work);
3896 }
3897 
3898 static int cm_establish(struct ib_cm_id *cm_id)
3899 {
3900 	struct cm_id_private *cm_id_priv;
3901 	struct cm_work *work;
3902 	unsigned long flags;
3903 	int ret = 0;
3904 	struct cm_device *cm_dev;
3905 
3906 	cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3907 	if (!cm_dev)
3908 		return -ENODEV;
3909 
3910 	work = kmalloc(sizeof *work, GFP_ATOMIC);
3911 	if (!work)
3912 		return -ENOMEM;
3913 
3914 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3915 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3916 	switch (cm_id->state)
3917 	{
3918 	case IB_CM_REP_SENT:
3919 	case IB_CM_MRA_REP_RCVD:
3920 		cm_id->state = IB_CM_ESTABLISHED;
3921 		break;
3922 	case IB_CM_ESTABLISHED:
3923 		ret = -EISCONN;
3924 		break;
3925 	default:
3926 		trace_icm_establish_err(cm_id);
3927 		ret = -EINVAL;
3928 		break;
3929 	}
3930 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3931 
3932 	if (ret) {
3933 		kfree(work);
3934 		goto out;
3935 	}
3936 
3937 	/*
3938 	 * The CM worker thread may try to destroy the cm_id before it
3939 	 * can execute this work item.  To prevent potential deadlock,
3940 	 * we need to find the cm_id once we're in the context of the
3941 	 * worker thread, rather than holding a reference on it.
3942 	 */
3943 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3944 	work->local_id = cm_id->local_id;
3945 	work->remote_id = cm_id->remote_id;
3946 	work->mad_recv_wc = NULL;
3947 	work->cm_event.event = IB_CM_USER_ESTABLISHED;
3948 
3949 	/* Check if the device started its remove_one */
3950 	spin_lock_irqsave(&cm.lock, flags);
3951 	if (!cm_dev->going_down) {
3952 		queue_delayed_work(cm.wq, &work->work, 0);
3953 	} else {
3954 		kfree(work);
3955 		ret = -ENODEV;
3956 	}
3957 	spin_unlock_irqrestore(&cm.lock, flags);
3958 
3959 out:
3960 	return ret;
3961 }
3962 
3963 static int cm_migrate(struct ib_cm_id *cm_id)
3964 {
3965 	struct cm_id_private *cm_id_priv;
3966 	struct cm_av tmp_av;
3967 	unsigned long flags;
3968 	int tmp_send_port_not_ready;
3969 	int ret = 0;
3970 
3971 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3972 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3973 	if (cm_id->state == IB_CM_ESTABLISHED &&
3974 	    (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3975 	     cm_id->lap_state == IB_CM_LAP_IDLE)) {
3976 		cm_id->lap_state = IB_CM_LAP_IDLE;
3977 		/* Swap address vector */
3978 		tmp_av = cm_id_priv->av;
3979 		cm_id_priv->av = cm_id_priv->alt_av;
3980 		cm_id_priv->alt_av = tmp_av;
3981 		/* Swap port send ready state */
3982 		tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
3983 		cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
3984 		cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
3985 	} else
3986 		ret = -EINVAL;
3987 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3988 
3989 	return ret;
3990 }
3991 
3992 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3993 {
3994 	int ret;
3995 
3996 	switch (event) {
3997 	case IB_EVENT_COMM_EST:
3998 		ret = cm_establish(cm_id);
3999 		break;
4000 	case IB_EVENT_PATH_MIG:
4001 		ret = cm_migrate(cm_id);
4002 		break;
4003 	default:
4004 		ret = -EINVAL;
4005 	}
4006 	return ret;
4007 }
4008 EXPORT_SYMBOL(ib_cm_notify);
4009 
4010 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
4011 			    struct ib_mad_send_buf *send_buf,
4012 			    struct ib_mad_recv_wc *mad_recv_wc)
4013 {
4014 	struct cm_port *port = mad_agent->context;
4015 	struct cm_work *work;
4016 	enum ib_cm_event_type event;
4017 	bool alt_path = false;
4018 	u16 attr_id;
4019 	int paths = 0;
4020 	int going_down = 0;
4021 
4022 	switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
4023 	case CM_REQ_ATTR_ID:
4024 		alt_path = cm_req_has_alt_path((struct cm_req_msg *)
4025 						mad_recv_wc->recv_buf.mad);
4026 		paths = 1 + (alt_path != 0);
4027 		event = IB_CM_REQ_RECEIVED;
4028 		break;
4029 	case CM_MRA_ATTR_ID:
4030 		event = IB_CM_MRA_RECEIVED;
4031 		break;
4032 	case CM_REJ_ATTR_ID:
4033 		event = IB_CM_REJ_RECEIVED;
4034 		break;
4035 	case CM_REP_ATTR_ID:
4036 		event = IB_CM_REP_RECEIVED;
4037 		break;
4038 	case CM_RTU_ATTR_ID:
4039 		event = IB_CM_RTU_RECEIVED;
4040 		break;
4041 	case CM_DREQ_ATTR_ID:
4042 		event = IB_CM_DREQ_RECEIVED;
4043 		break;
4044 	case CM_DREP_ATTR_ID:
4045 		event = IB_CM_DREP_RECEIVED;
4046 		break;
4047 	case CM_SIDR_REQ_ATTR_ID:
4048 		event = IB_CM_SIDR_REQ_RECEIVED;
4049 		break;
4050 	case CM_SIDR_REP_ATTR_ID:
4051 		event = IB_CM_SIDR_REP_RECEIVED;
4052 		break;
4053 	case CM_LAP_ATTR_ID:
4054 		paths = 1;
4055 		event = IB_CM_LAP_RECEIVED;
4056 		break;
4057 	case CM_APR_ATTR_ID:
4058 		event = IB_CM_APR_RECEIVED;
4059 		break;
4060 	default:
4061 		ib_free_recv_mad(mad_recv_wc);
4062 		return;
4063 	}
4064 
4065 	attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
4066 	atomic_long_inc(&port->counter_group[CM_RECV].
4067 			counter[attr_id - CM_ATTR_ID_OFFSET]);
4068 
4069 	work = kmalloc(struct_size(work, path, paths), GFP_KERNEL);
4070 	if (!work) {
4071 		ib_free_recv_mad(mad_recv_wc);
4072 		return;
4073 	}
4074 
4075 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
4076 	work->cm_event.event = event;
4077 	work->mad_recv_wc = mad_recv_wc;
4078 	work->port = port;
4079 
4080 	/* Check if the device started its remove_one */
4081 	spin_lock_irq(&cm.lock);
4082 	if (!port->cm_dev->going_down)
4083 		queue_delayed_work(cm.wq, &work->work, 0);
4084 	else
4085 		going_down = 1;
4086 	spin_unlock_irq(&cm.lock);
4087 
4088 	if (going_down) {
4089 		kfree(work);
4090 		ib_free_recv_mad(mad_recv_wc);
4091 	}
4092 }
4093 
4094 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
4095 				struct ib_qp_attr *qp_attr,
4096 				int *qp_attr_mask)
4097 {
4098 	unsigned long flags;
4099 	int ret;
4100 
4101 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4102 	switch (cm_id_priv->id.state) {
4103 	case IB_CM_REQ_SENT:
4104 	case IB_CM_MRA_REQ_RCVD:
4105 	case IB_CM_REQ_RCVD:
4106 	case IB_CM_MRA_REQ_SENT:
4107 	case IB_CM_REP_RCVD:
4108 	case IB_CM_MRA_REP_SENT:
4109 	case IB_CM_REP_SENT:
4110 	case IB_CM_MRA_REP_RCVD:
4111 	case IB_CM_ESTABLISHED:
4112 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
4113 				IB_QP_PKEY_INDEX | IB_QP_PORT;
4114 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
4115 		if (cm_id_priv->responder_resources)
4116 			qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
4117 						    IB_ACCESS_REMOTE_ATOMIC;
4118 		qp_attr->pkey_index = cm_id_priv->av.pkey_index;
4119 		qp_attr->port_num = cm_id_priv->av.port->port_num;
4120 		ret = 0;
4121 		break;
4122 	default:
4123 		trace_icm_qp_init_err(&cm_id_priv->id);
4124 		ret = -EINVAL;
4125 		break;
4126 	}
4127 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4128 	return ret;
4129 }
4130 
4131 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
4132 			       struct ib_qp_attr *qp_attr,
4133 			       int *qp_attr_mask)
4134 {
4135 	unsigned long flags;
4136 	int ret;
4137 
4138 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4139 	switch (cm_id_priv->id.state) {
4140 	case IB_CM_REQ_RCVD:
4141 	case IB_CM_MRA_REQ_SENT:
4142 	case IB_CM_REP_RCVD:
4143 	case IB_CM_MRA_REP_SENT:
4144 	case IB_CM_REP_SENT:
4145 	case IB_CM_MRA_REP_RCVD:
4146 	case IB_CM_ESTABLISHED:
4147 		*qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
4148 				IB_QP_DEST_QPN | IB_QP_RQ_PSN;
4149 		qp_attr->ah_attr = cm_id_priv->av.ah_attr;
4150 		qp_attr->path_mtu = cm_id_priv->path_mtu;
4151 		qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
4152 		qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
4153 		if (cm_id_priv->qp_type == IB_QPT_RC ||
4154 		    cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
4155 			*qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
4156 					 IB_QP_MIN_RNR_TIMER;
4157 			qp_attr->max_dest_rd_atomic =
4158 					cm_id_priv->responder_resources;
4159 			qp_attr->min_rnr_timer = 0;
4160 		}
4161 		if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4162 			*qp_attr_mask |= IB_QP_ALT_PATH;
4163 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4164 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4165 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4166 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4167 		}
4168 		ret = 0;
4169 		break;
4170 	default:
4171 		trace_icm_qp_rtr_err(&cm_id_priv->id);
4172 		ret = -EINVAL;
4173 		break;
4174 	}
4175 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4176 	return ret;
4177 }
4178 
4179 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
4180 			       struct ib_qp_attr *qp_attr,
4181 			       int *qp_attr_mask)
4182 {
4183 	unsigned long flags;
4184 	int ret;
4185 
4186 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4187 	switch (cm_id_priv->id.state) {
4188 	/* Allow transition to RTS before sending REP */
4189 	case IB_CM_REQ_RCVD:
4190 	case IB_CM_MRA_REQ_SENT:
4191 
4192 	case IB_CM_REP_RCVD:
4193 	case IB_CM_MRA_REP_SENT:
4194 	case IB_CM_REP_SENT:
4195 	case IB_CM_MRA_REP_RCVD:
4196 	case IB_CM_ESTABLISHED:
4197 		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
4198 			*qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
4199 			qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
4200 			switch (cm_id_priv->qp_type) {
4201 			case IB_QPT_RC:
4202 			case IB_QPT_XRC_INI:
4203 				*qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4204 						 IB_QP_MAX_QP_RD_ATOMIC;
4205 				qp_attr->retry_cnt = cm_id_priv->retry_count;
4206 				qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
4207 				qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
4208 				fallthrough;
4209 			case IB_QPT_XRC_TGT:
4210 				*qp_attr_mask |= IB_QP_TIMEOUT;
4211 				qp_attr->timeout = cm_id_priv->av.timeout;
4212 				break;
4213 			default:
4214 				break;
4215 			}
4216 			if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4217 				*qp_attr_mask |= IB_QP_PATH_MIG_STATE;
4218 				qp_attr->path_mig_state = IB_MIG_REARM;
4219 			}
4220 		} else {
4221 			*qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
4222 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4223 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4224 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4225 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4226 			qp_attr->path_mig_state = IB_MIG_REARM;
4227 		}
4228 		ret = 0;
4229 		break;
4230 	default:
4231 		trace_icm_qp_rts_err(&cm_id_priv->id);
4232 		ret = -EINVAL;
4233 		break;
4234 	}
4235 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4236 	return ret;
4237 }
4238 
4239 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
4240 		       struct ib_qp_attr *qp_attr,
4241 		       int *qp_attr_mask)
4242 {
4243 	struct cm_id_private *cm_id_priv;
4244 	int ret;
4245 
4246 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
4247 	switch (qp_attr->qp_state) {
4248 	case IB_QPS_INIT:
4249 		ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
4250 		break;
4251 	case IB_QPS_RTR:
4252 		ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
4253 		break;
4254 	case IB_QPS_RTS:
4255 		ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
4256 		break;
4257 	default:
4258 		ret = -EINVAL;
4259 		break;
4260 	}
4261 	return ret;
4262 }
4263 EXPORT_SYMBOL(ib_cm_init_qp_attr);
4264 
4265 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
4266 			       char *buf)
4267 {
4268 	struct cm_counter_group *group;
4269 	struct cm_counter_attribute *cm_attr;
4270 
4271 	group = container_of(obj, struct cm_counter_group, obj);
4272 	cm_attr = container_of(attr, struct cm_counter_attribute, attr);
4273 
4274 	return sprintf(buf, "%ld\n",
4275 		       atomic_long_read(&group->counter[cm_attr->index]));
4276 }
4277 
4278 static const struct sysfs_ops cm_counter_ops = {
4279 	.show = cm_show_counter
4280 };
4281 
4282 static struct kobj_type cm_counter_obj_type = {
4283 	.sysfs_ops = &cm_counter_ops,
4284 	.default_attrs = cm_counter_default_attrs
4285 };
4286 
4287 static int cm_create_port_fs(struct cm_port *port)
4288 {
4289 	int i, ret;
4290 
4291 	for (i = 0; i < CM_COUNTER_GROUPS; i++) {
4292 		ret = ib_port_register_module_stat(port->cm_dev->ib_device,
4293 						   port->port_num,
4294 						   &port->counter_group[i].obj,
4295 						   &cm_counter_obj_type,
4296 						   counter_group_names[i]);
4297 		if (ret)
4298 			goto error;
4299 	}
4300 
4301 	return 0;
4302 
4303 error:
4304 	while (i--)
4305 		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4306 	return ret;
4307 
4308 }
4309 
4310 static void cm_remove_port_fs(struct cm_port *port)
4311 {
4312 	int i;
4313 
4314 	for (i = 0; i < CM_COUNTER_GROUPS; i++)
4315 		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4316 
4317 }
4318 
4319 static int cm_add_one(struct ib_device *ib_device)
4320 {
4321 	struct cm_device *cm_dev;
4322 	struct cm_port *port;
4323 	struct ib_mad_reg_req reg_req = {
4324 		.mgmt_class = IB_MGMT_CLASS_CM,
4325 		.mgmt_class_version = IB_CM_CLASS_VERSION,
4326 	};
4327 	struct ib_port_modify port_modify = {
4328 		.set_port_cap_mask = IB_PORT_CM_SUP
4329 	};
4330 	unsigned long flags;
4331 	int ret;
4332 	int count = 0;
4333 	u8 i;
4334 
4335 	cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt),
4336 			 GFP_KERNEL);
4337 	if (!cm_dev)
4338 		return -ENOMEM;
4339 
4340 	cm_dev->ib_device = ib_device;
4341 	cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
4342 	cm_dev->going_down = 0;
4343 
4344 	set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
4345 	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4346 		if (!rdma_cap_ib_cm(ib_device, i))
4347 			continue;
4348 
4349 		port = kzalloc(sizeof *port, GFP_KERNEL);
4350 		if (!port) {
4351 			ret = -ENOMEM;
4352 			goto error1;
4353 		}
4354 
4355 		cm_dev->port[i-1] = port;
4356 		port->cm_dev = cm_dev;
4357 		port->port_num = i;
4358 
4359 		INIT_LIST_HEAD(&port->cm_priv_prim_list);
4360 		INIT_LIST_HEAD(&port->cm_priv_altr_list);
4361 
4362 		ret = cm_create_port_fs(port);
4363 		if (ret)
4364 			goto error1;
4365 
4366 		port->mad_agent = ib_register_mad_agent(ib_device, i,
4367 							IB_QPT_GSI,
4368 							&reg_req,
4369 							0,
4370 							cm_send_handler,
4371 							cm_recv_handler,
4372 							port,
4373 							0);
4374 		if (IS_ERR(port->mad_agent)) {
4375 			ret = PTR_ERR(port->mad_agent);
4376 			goto error2;
4377 		}
4378 
4379 		ret = ib_modify_port(ib_device, i, 0, &port_modify);
4380 		if (ret)
4381 			goto error3;
4382 
4383 		count++;
4384 	}
4385 
4386 	if (!count) {
4387 		ret = -EOPNOTSUPP;
4388 		goto free;
4389 	}
4390 
4391 	ib_set_client_data(ib_device, &cm_client, cm_dev);
4392 
4393 	write_lock_irqsave(&cm.device_lock, flags);
4394 	list_add_tail(&cm_dev->list, &cm.device_list);
4395 	write_unlock_irqrestore(&cm.device_lock, flags);
4396 	return 0;
4397 
4398 error3:
4399 	ib_unregister_mad_agent(port->mad_agent);
4400 error2:
4401 	cm_remove_port_fs(port);
4402 error1:
4403 	port_modify.set_port_cap_mask = 0;
4404 	port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4405 	kfree(port);
4406 	while (--i) {
4407 		if (!rdma_cap_ib_cm(ib_device, i))
4408 			continue;
4409 
4410 		port = cm_dev->port[i-1];
4411 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4412 		ib_unregister_mad_agent(port->mad_agent);
4413 		cm_remove_port_fs(port);
4414 		kfree(port);
4415 	}
4416 free:
4417 	kfree(cm_dev);
4418 	return ret;
4419 }
4420 
4421 static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4422 {
4423 	struct cm_device *cm_dev = client_data;
4424 	struct cm_port *port;
4425 	struct cm_id_private *cm_id_priv;
4426 	struct ib_mad_agent *cur_mad_agent;
4427 	struct ib_port_modify port_modify = {
4428 		.clr_port_cap_mask = IB_PORT_CM_SUP
4429 	};
4430 	unsigned long flags;
4431 	int i;
4432 
4433 	write_lock_irqsave(&cm.device_lock, flags);
4434 	list_del(&cm_dev->list);
4435 	write_unlock_irqrestore(&cm.device_lock, flags);
4436 
4437 	spin_lock_irq(&cm.lock);
4438 	cm_dev->going_down = 1;
4439 	spin_unlock_irq(&cm.lock);
4440 
4441 	for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4442 		if (!rdma_cap_ib_cm(ib_device, i))
4443 			continue;
4444 
4445 		port = cm_dev->port[i-1];
4446 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4447 		/* Mark all the cm_id's as not valid */
4448 		spin_lock_irq(&cm.lock);
4449 		list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
4450 			cm_id_priv->altr_send_port_not_ready = 1;
4451 		list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
4452 			cm_id_priv->prim_send_port_not_ready = 1;
4453 		spin_unlock_irq(&cm.lock);
4454 		/*
4455 		 * We flush the queue here after the going_down set, this
4456 		 * verify that no new works will be queued in the recv handler,
4457 		 * after that we can call the unregister_mad_agent
4458 		 */
4459 		flush_workqueue(cm.wq);
4460 		spin_lock_irq(&cm.state_lock);
4461 		cur_mad_agent = port->mad_agent;
4462 		port->mad_agent = NULL;
4463 		spin_unlock_irq(&cm.state_lock);
4464 		ib_unregister_mad_agent(cur_mad_agent);
4465 		cm_remove_port_fs(port);
4466 		kfree(port);
4467 	}
4468 
4469 	kfree(cm_dev);
4470 }
4471 
4472 static int __init ib_cm_init(void)
4473 {
4474 	int ret;
4475 
4476 	INIT_LIST_HEAD(&cm.device_list);
4477 	rwlock_init(&cm.device_lock);
4478 	spin_lock_init(&cm.lock);
4479 	spin_lock_init(&cm.state_lock);
4480 	cm.listen_service_table = RB_ROOT;
4481 	cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4482 	cm.remote_id_table = RB_ROOT;
4483 	cm.remote_qp_table = RB_ROOT;
4484 	cm.remote_sidr_table = RB_ROOT;
4485 	xa_init_flags(&cm.local_id_table, XA_FLAGS_ALLOC);
4486 	get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4487 	INIT_LIST_HEAD(&cm.timewait_list);
4488 
4489 	cm.wq = alloc_workqueue("ib_cm", 0, 1);
4490 	if (!cm.wq) {
4491 		ret = -ENOMEM;
4492 		goto error2;
4493 	}
4494 
4495 	ret = ib_register_client(&cm_client);
4496 	if (ret)
4497 		goto error3;
4498 
4499 	return 0;
4500 error3:
4501 	destroy_workqueue(cm.wq);
4502 error2:
4503 	return ret;
4504 }
4505 
4506 static void __exit ib_cm_cleanup(void)
4507 {
4508 	struct cm_timewait_info *timewait_info, *tmp;
4509 
4510 	spin_lock_irq(&cm.lock);
4511 	list_for_each_entry(timewait_info, &cm.timewait_list, list)
4512 		cancel_delayed_work(&timewait_info->work.work);
4513 	spin_unlock_irq(&cm.lock);
4514 
4515 	ib_unregister_client(&cm_client);
4516 	destroy_workqueue(cm.wq);
4517 
4518 	list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4519 		list_del(&timewait_info->list);
4520 		kfree(timewait_info);
4521 	}
4522 
4523 	WARN_ON(!xa_empty(&cm.local_id_table));
4524 }
4525 
4526 module_init(ib_cm_init);
4527 module_exit(ib_cm_cleanup);
4528