• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ibcm_reject_msg(int reason)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 
cm_deref_id(struct cm_id_private * cm_id_priv)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 
cm_alloc_msg(struct cm_id_private * cm_id_priv,struct ib_mad_send_buf ** msg)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 
cm_alloc_response_msg_no_ah(struct cm_port * port,struct ib_mad_recv_wc * mad_recv_wc)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 
cm_create_response_msg_ah(struct cm_port * port,struct ib_mad_recv_wc * mad_recv_wc,struct ib_mad_send_buf * msg)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 
cm_free_msg(struct ib_mad_send_buf * msg)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 
cm_alloc_response_msg(struct cm_port * port,struct ib_mad_recv_wc * mad_recv_wc,struct ib_mad_send_buf ** msg)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 
cm_copy_private_data(const void * private_data,u8 private_data_len)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 
cm_set_private_data(struct cm_id_private * cm_id_priv,void * private_data,u8 private_data_len)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 
cm_init_av_for_lap(struct cm_port * port,struct ib_wc * wc,struct ib_grh * grh,struct cm_av * av)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 
cm_init_av_for_response(struct cm_port * port,struct ib_wc * wc,struct ib_grh * grh,struct cm_av * av)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 
add_cm_id_to_port_list(struct cm_id_private * cm_id_priv,struct cm_av * av,struct cm_port * port)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 *
get_cm_port_from_path(struct sa_path_rec * path,const struct ib_gid_attr * attr)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 
cm_init_av_by_path(struct sa_path_rec * path,const struct ib_gid_attr * sgid_attr,struct cm_av * av,struct cm_id_private * cm_id_priv)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 
cm_local_id(__be32 local_id)582 static u32 cm_local_id(__be32 local_id)
583 {
584 	return (__force u32) (local_id ^ cm.random_id_operand);
585 }
586 
cm_acquire_id(__be32 local_id,__be32 remote_id)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  */
be32_lt(__be32 a,__be32 b)606 static int be32_lt(__be32 a, __be32 b)
607 {
608 	return (__force u32) a < (__force u32) b;
609 }
610 
be32_gt(__be32 a,__be32 b)611 static int be32_gt(__be32 a, __be32 b)
612 {
613 	return (__force u32) a > (__force u32) b;
614 }
615 
be64_lt(__be64 a,__be64 b)616 static int be64_lt(__be64 a, __be64 b)
617 {
618 	return (__force u64) a < (__force u64) b;
619 }
620 
be64_gt(__be64 a,__be64 b)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  */
cm_insert_listen(struct cm_id_private * cm_id_priv,ib_cm_handler shared_handler)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 
cm_find_listen(struct ib_device * device,__be64 service_id)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 
cm_insert_remote_id(struct cm_timewait_info * timewait_info)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 
cm_find_remote_id(__be64 remote_ca_guid,__be32 remote_id)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 
cm_insert_remote_qpn(struct cm_timewait_info * timewait_info)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 
cm_insert_remote_sidr(struct cm_id_private * cm_id_priv)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 
cm_alloc_id_priv(struct ib_device * device,ib_cm_handler cm_handler,void * context)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  */
cm_finalize_id(struct cm_id_private * cm_id_priv)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 
ib_create_cm_id(struct ib_device * device,ib_cm_handler cm_handler,void * context)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 
cm_dequeue_work(struct cm_id_private * cm_id_priv)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 
cm_free_work(struct cm_work * work)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 
cm_queue_work_unlock(struct cm_id_private * cm_id_priv,struct cm_work * work)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 
cm_convert_to_ms(int iba_time)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  */
cm_ack_timeout(u8 ca_ack_delay,u8 packet_life_time)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 
cm_remove_remote(struct cm_id_private * cm_id_priv)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 
cm_create_timewait_info(__be32 local_id)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 
cm_enter_timewait(struct cm_id_private * cm_id_priv)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 
cm_reset_to_idle(struct cm_id_private * cm_id_priv)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 
cm_destroy_id(struct ib_cm_id * cm_id,int err)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 
ib_destroy_cm_id(struct ib_cm_id * cm_id)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 
cm_init_listen(struct cm_id_private * cm_id_priv,__be64 service_id,__be64 service_mask)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  */
ib_cm_listen(struct ib_cm_id * cm_id,__be64 service_id,__be64 service_mask)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  */
ib_cm_insert_listen(struct ib_device * device,ib_cm_handler cm_handler,__be64 service_id)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 		ib_destroy_cm_id(&cm_id_priv->id);
1285 		return ERR_PTR(err);
1286 	}
1287 
1288 	spin_lock_irq(&cm_id_priv->lock);
1289 	listen_id_priv = cm_insert_listen(cm_id_priv, cm_handler);
1290 	if (listen_id_priv != cm_id_priv) {
1291 		spin_unlock_irq(&cm_id_priv->lock);
1292 		ib_destroy_cm_id(&cm_id_priv->id);
1293 		if (!listen_id_priv)
1294 			return ERR_PTR(-EINVAL);
1295 		return &listen_id_priv->id;
1296 	}
1297 	cm_id_priv->id.state = IB_CM_LISTEN;
1298 	spin_unlock_irq(&cm_id_priv->lock);
1299 
1300 	/*
1301 	 * A listen ID does not need to be in the xarray since it does not
1302 	 * receive mads, is not placed in the remote_id or remote_qpn rbtree,
1303 	 * and does not enter timewait.
1304 	 */
1305 
1306 	return &cm_id_priv->id;
1307 }
1308 EXPORT_SYMBOL(ib_cm_insert_listen);
1309 
cm_form_tid(struct cm_id_private * cm_id_priv)1310 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv)
1311 {
1312 	u64 hi_tid, low_tid;
1313 
1314 	hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1315 	low_tid  = (u64)cm_id_priv->id.local_id;
1316 	return cpu_to_be64(hi_tid | low_tid);
1317 }
1318 
cm_format_mad_hdr(struct ib_mad_hdr * hdr,__be16 attr_id,__be64 tid)1319 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1320 			      __be16 attr_id, __be64 tid)
1321 {
1322 	hdr->base_version  = IB_MGMT_BASE_VERSION;
1323 	hdr->mgmt_class	   = IB_MGMT_CLASS_CM;
1324 	hdr->class_version = IB_CM_CLASS_VERSION;
1325 	hdr->method	   = IB_MGMT_METHOD_SEND;
1326 	hdr->attr_id	   = attr_id;
1327 	hdr->tid	   = tid;
1328 }
1329 
cm_format_mad_ece_hdr(struct ib_mad_hdr * hdr,__be16 attr_id,__be64 tid,u32 attr_mod)1330 static void cm_format_mad_ece_hdr(struct ib_mad_hdr *hdr, __be16 attr_id,
1331 				  __be64 tid, u32 attr_mod)
1332 {
1333 	cm_format_mad_hdr(hdr, attr_id, tid);
1334 	hdr->attr_mod = cpu_to_be32(attr_mod);
1335 }
1336 
cm_format_req(struct cm_req_msg * req_msg,struct cm_id_private * cm_id_priv,struct ib_cm_req_param * param)1337 static void cm_format_req(struct cm_req_msg *req_msg,
1338 			  struct cm_id_private *cm_id_priv,
1339 			  struct ib_cm_req_param *param)
1340 {
1341 	struct sa_path_rec *pri_path = param->primary_path;
1342 	struct sa_path_rec *alt_path = param->alternate_path;
1343 	bool pri_ext = false;
1344 
1345 	if (pri_path->rec_type == SA_PATH_REC_TYPE_OPA)
1346 		pri_ext = opa_is_extended_lid(pri_path->opa.dlid,
1347 					      pri_path->opa.slid);
1348 
1349 	cm_format_mad_ece_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1350 			      cm_form_tid(cm_id_priv), param->ece.attr_mod);
1351 
1352 	IBA_SET(CM_REQ_LOCAL_COMM_ID, req_msg,
1353 		be32_to_cpu(cm_id_priv->id.local_id));
1354 	IBA_SET(CM_REQ_SERVICE_ID, req_msg, be64_to_cpu(param->service_id));
1355 	IBA_SET(CM_REQ_LOCAL_CA_GUID, req_msg,
1356 		be64_to_cpu(cm_id_priv->id.device->node_guid));
1357 	IBA_SET(CM_REQ_LOCAL_QPN, req_msg, param->qp_num);
1358 	IBA_SET(CM_REQ_INITIATOR_DEPTH, req_msg, param->initiator_depth);
1359 	IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg,
1360 		param->remote_cm_response_timeout);
1361 	cm_req_set_qp_type(req_msg, param->qp_type);
1362 	IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg, param->flow_control);
1363 	IBA_SET(CM_REQ_STARTING_PSN, req_msg, param->starting_psn);
1364 	IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg,
1365 		param->local_cm_response_timeout);
1366 	IBA_SET(CM_REQ_PARTITION_KEY, req_msg,
1367 		be16_to_cpu(param->primary_path->pkey));
1368 	IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg,
1369 		param->primary_path->mtu);
1370 	IBA_SET(CM_REQ_MAX_CM_RETRIES, req_msg, param->max_cm_retries);
1371 
1372 	if (param->qp_type != IB_QPT_XRC_INI) {
1373 		IBA_SET(CM_REQ_RESPONDER_RESOURCES, req_msg,
1374 			param->responder_resources);
1375 		IBA_SET(CM_REQ_RETRY_COUNT, req_msg, param->retry_count);
1376 		IBA_SET(CM_REQ_RNR_RETRY_COUNT, req_msg,
1377 			param->rnr_retry_count);
1378 		IBA_SET(CM_REQ_SRQ, req_msg, param->srq);
1379 	}
1380 
1381 	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) =
1382 		pri_path->sgid;
1383 	*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) =
1384 		pri_path->dgid;
1385 	if (pri_ext) {
1386 		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg)
1387 			->global.interface_id =
1388 			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid));
1389 		IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg)
1390 			->global.interface_id =
1391 			OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid));
1392 	}
1393 	if (pri_path->hop_limit <= 1) {
1394 		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1395 			be16_to_cpu(pri_ext ? 0 :
1396 					      htons(ntohl(sa_path_get_slid(
1397 						      pri_path)))));
1398 		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1399 			be16_to_cpu(pri_ext ? 0 :
1400 					      htons(ntohl(sa_path_get_dlid(
1401 						      pri_path)))));
1402 	} else {
1403 		/* Work-around until there's a way to obtain remote LID info */
1404 		IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1405 			be16_to_cpu(IB_LID_PERMISSIVE));
1406 		IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1407 			be16_to_cpu(IB_LID_PERMISSIVE));
1408 	}
1409 	IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg,
1410 		be32_to_cpu(pri_path->flow_label));
1411 	IBA_SET(CM_REQ_PRIMARY_PACKET_RATE, req_msg, pri_path->rate);
1412 	IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg, pri_path->traffic_class);
1413 	IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg, pri_path->hop_limit);
1414 	IBA_SET(CM_REQ_PRIMARY_SL, req_msg, pri_path->sl);
1415 	IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg,
1416 		(pri_path->hop_limit <= 1));
1417 	IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg,
1418 		cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1419 			       pri_path->packet_life_time));
1420 
1421 	if (alt_path) {
1422 		bool alt_ext = false;
1423 
1424 		if (alt_path->rec_type == SA_PATH_REC_TYPE_OPA)
1425 			alt_ext = opa_is_extended_lid(alt_path->opa.dlid,
1426 						      alt_path->opa.slid);
1427 
1428 		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg) =
1429 			alt_path->sgid;
1430 		*IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg) =
1431 			alt_path->dgid;
1432 		if (alt_ext) {
1433 			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1434 					req_msg)
1435 				->global.interface_id =
1436 				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid));
1437 			IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID,
1438 					req_msg)
1439 				->global.interface_id =
1440 				OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid));
1441 		}
1442 		if (alt_path->hop_limit <= 1) {
1443 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1444 				be16_to_cpu(
1445 					alt_ext ? 0 :
1446 						  htons(ntohl(sa_path_get_slid(
1447 							  alt_path)))));
1448 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1449 				be16_to_cpu(
1450 					alt_ext ? 0 :
1451 						  htons(ntohl(sa_path_get_dlid(
1452 							  alt_path)))));
1453 		} else {
1454 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1455 				be16_to_cpu(IB_LID_PERMISSIVE));
1456 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1457 				be16_to_cpu(IB_LID_PERMISSIVE));
1458 		}
1459 		IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg,
1460 			be32_to_cpu(alt_path->flow_label));
1461 		IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg, alt_path->rate);
1462 		IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg,
1463 			alt_path->traffic_class);
1464 		IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg,
1465 			alt_path->hop_limit);
1466 		IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, alt_path->sl);
1467 		IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg,
1468 			(alt_path->hop_limit <= 1));
1469 		IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg,
1470 			cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1471 				       alt_path->packet_life_time));
1472 	}
1473 	IBA_SET(CM_REQ_VENDOR_ID, req_msg, param->ece.vendor_id);
1474 
1475 	if (param->private_data && param->private_data_len)
1476 		IBA_SET_MEM(CM_REQ_PRIVATE_DATA, req_msg, param->private_data,
1477 			    param->private_data_len);
1478 }
1479 
cm_validate_req_param(struct ib_cm_req_param * param)1480 static int cm_validate_req_param(struct ib_cm_req_param *param)
1481 {
1482 	if (!param->primary_path)
1483 		return -EINVAL;
1484 
1485 	if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1486 	    param->qp_type != IB_QPT_XRC_INI)
1487 		return -EINVAL;
1488 
1489 	if (param->private_data &&
1490 	    param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1491 		return -EINVAL;
1492 
1493 	if (param->alternate_path &&
1494 	    (param->alternate_path->pkey != param->primary_path->pkey ||
1495 	     param->alternate_path->mtu != param->primary_path->mtu))
1496 		return -EINVAL;
1497 
1498 	return 0;
1499 }
1500 
ib_send_cm_req(struct ib_cm_id * cm_id,struct ib_cm_req_param * param)1501 int ib_send_cm_req(struct ib_cm_id *cm_id,
1502 		   struct ib_cm_req_param *param)
1503 {
1504 	struct cm_id_private *cm_id_priv;
1505 	struct cm_req_msg *req_msg;
1506 	unsigned long flags;
1507 	int ret;
1508 
1509 	ret = cm_validate_req_param(param);
1510 	if (ret)
1511 		return ret;
1512 
1513 	/* Verify that we're not in timewait. */
1514 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1515 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1516 	if (cm_id->state != IB_CM_IDLE || WARN_ON(cm_id_priv->timewait_info)) {
1517 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1518 		ret = -EINVAL;
1519 		goto out;
1520 	}
1521 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1522 
1523 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1524 							    id.local_id);
1525 	if (IS_ERR(cm_id_priv->timewait_info)) {
1526 		ret = PTR_ERR(cm_id_priv->timewait_info);
1527 		cm_id_priv->timewait_info = NULL;
1528 		goto out;
1529 	}
1530 
1531 	ret = cm_init_av_by_path(param->primary_path,
1532 				 param->ppath_sgid_attr, &cm_id_priv->av,
1533 				 cm_id_priv);
1534 	if (ret)
1535 		goto out;
1536 	if (param->alternate_path) {
1537 		ret = cm_init_av_by_path(param->alternate_path, NULL,
1538 					 &cm_id_priv->alt_av, cm_id_priv);
1539 		if (ret)
1540 			goto out;
1541 	}
1542 	cm_id->service_id = param->service_id;
1543 	cm_id->service_mask = ~cpu_to_be64(0);
1544 	cm_id_priv->timeout_ms = cm_convert_to_ms(
1545 				    param->primary_path->packet_life_time) * 2 +
1546 				 cm_convert_to_ms(
1547 				    param->remote_cm_response_timeout);
1548 	cm_id_priv->max_cm_retries = param->max_cm_retries;
1549 	cm_id_priv->initiator_depth = param->initiator_depth;
1550 	cm_id_priv->responder_resources = param->responder_resources;
1551 	cm_id_priv->retry_count = param->retry_count;
1552 	cm_id_priv->path_mtu = param->primary_path->mtu;
1553 	cm_id_priv->pkey = param->primary_path->pkey;
1554 	cm_id_priv->qp_type = param->qp_type;
1555 
1556 	ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1557 	if (ret)
1558 		goto out;
1559 
1560 	req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1561 	cm_format_req(req_msg, cm_id_priv, param);
1562 	cm_id_priv->tid = req_msg->hdr.tid;
1563 	cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1564 	cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1565 
1566 	cm_id_priv->local_qpn = cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
1567 	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
1568 
1569 	trace_icm_send_req(&cm_id_priv->id);
1570 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1571 	ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1572 	if (ret) {
1573 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1574 		goto error2;
1575 	}
1576 	BUG_ON(cm_id->state != IB_CM_IDLE);
1577 	cm_id->state = IB_CM_REQ_SENT;
1578 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1579 	return 0;
1580 
1581 error2:	cm_free_msg(cm_id_priv->msg);
1582 out:	return ret;
1583 }
1584 EXPORT_SYMBOL(ib_send_cm_req);
1585 
cm_issue_rej(struct cm_port * port,struct ib_mad_recv_wc * mad_recv_wc,enum ib_cm_rej_reason reason,enum cm_msg_response msg_rejected,void * ari,u8 ari_length)1586 static int cm_issue_rej(struct cm_port *port,
1587 			struct ib_mad_recv_wc *mad_recv_wc,
1588 			enum ib_cm_rej_reason reason,
1589 			enum cm_msg_response msg_rejected,
1590 			void *ari, u8 ari_length)
1591 {
1592 	struct ib_mad_send_buf *msg = NULL;
1593 	struct cm_rej_msg *rej_msg, *rcv_msg;
1594 	int ret;
1595 
1596 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1597 	if (ret)
1598 		return ret;
1599 
1600 	/* We just need common CM header information.  Cast to any message. */
1601 	rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1602 	rej_msg = (struct cm_rej_msg *) msg->mad;
1603 
1604 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1605 	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1606 		IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg));
1607 	IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1608 		IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1609 	IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, msg_rejected);
1610 	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1611 
1612 	if (ari && ari_length) {
1613 		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1614 		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1615 	}
1616 
1617 	trace_icm_issue_rej(
1618 		IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg),
1619 		IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1620 	ret = ib_post_send_mad(msg, NULL);
1621 	if (ret)
1622 		cm_free_msg(msg);
1623 
1624 	return ret;
1625 }
1626 
cm_req_has_alt_path(struct cm_req_msg * req_msg)1627 static bool cm_req_has_alt_path(struct cm_req_msg *req_msg)
1628 {
1629 	return ((cpu_to_be16(
1630 			IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg))) ||
1631 		(ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1632 					       req_msg))));
1633 }
1634 
cm_path_set_rec_type(struct ib_device * ib_device,u8 port_num,struct sa_path_rec * path,union ib_gid * gid)1635 static void cm_path_set_rec_type(struct ib_device *ib_device, u8 port_num,
1636 				 struct sa_path_rec *path, union ib_gid *gid)
1637 {
1638 	if (ib_is_opa_gid(gid) && rdma_cap_opa_ah(ib_device, port_num))
1639 		path->rec_type = SA_PATH_REC_TYPE_OPA;
1640 	else
1641 		path->rec_type = SA_PATH_REC_TYPE_IB;
1642 }
1643 
cm_format_path_lid_from_req(struct cm_req_msg * req_msg,struct sa_path_rec * primary_path,struct sa_path_rec * alt_path,struct ib_wc * wc)1644 static void cm_format_path_lid_from_req(struct cm_req_msg *req_msg,
1645 					struct sa_path_rec *primary_path,
1646 					struct sa_path_rec *alt_path,
1647 					struct ib_wc *wc)
1648 {
1649 	u32 lid;
1650 
1651 	if (primary_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1652 		sa_path_set_dlid(primary_path, wc->slid);
1653 		sa_path_set_slid(primary_path,
1654 				 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
1655 					 req_msg));
1656 	} else {
1657 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1658 			CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg));
1659 		sa_path_set_dlid(primary_path, lid);
1660 
1661 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1662 			CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg));
1663 		sa_path_set_slid(primary_path, lid);
1664 	}
1665 
1666 	if (!cm_req_has_alt_path(req_msg))
1667 		return;
1668 
1669 	if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1670 		sa_path_set_dlid(alt_path,
1671 				 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
1672 					 req_msg));
1673 		sa_path_set_slid(alt_path,
1674 				 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
1675 					 req_msg));
1676 	} else {
1677 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1678 			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg));
1679 		sa_path_set_dlid(alt_path, lid);
1680 
1681 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1682 			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg));
1683 		sa_path_set_slid(alt_path, lid);
1684 	}
1685 }
1686 
cm_format_paths_from_req(struct cm_req_msg * req_msg,struct sa_path_rec * primary_path,struct sa_path_rec * alt_path,struct ib_wc * wc)1687 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1688 				     struct sa_path_rec *primary_path,
1689 				     struct sa_path_rec *alt_path,
1690 				     struct ib_wc *wc)
1691 {
1692 	primary_path->dgid =
1693 		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg);
1694 	primary_path->sgid =
1695 		*IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg);
1696 	primary_path->flow_label =
1697 		cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg));
1698 	primary_path->hop_limit = IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg);
1699 	primary_path->traffic_class =
1700 		IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg);
1701 	primary_path->reversible = 1;
1702 	primary_path->pkey =
1703 		cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1704 	primary_path->sl = IBA_GET(CM_REQ_PRIMARY_SL, req_msg);
1705 	primary_path->mtu_selector = IB_SA_EQ;
1706 	primary_path->mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1707 	primary_path->rate_selector = IB_SA_EQ;
1708 	primary_path->rate = IBA_GET(CM_REQ_PRIMARY_PACKET_RATE, req_msg);
1709 	primary_path->packet_life_time_selector = IB_SA_EQ;
1710 	primary_path->packet_life_time =
1711 		IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg);
1712 	primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1713 	primary_path->service_id =
1714 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1715 	if (sa_path_is_roce(primary_path))
1716 		primary_path->roce.route_resolved = false;
1717 
1718 	if (cm_req_has_alt_path(req_msg)) {
1719 		alt_path->dgid = *IBA_GET_MEM_PTR(
1720 			CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg);
1721 		alt_path->sgid = *IBA_GET_MEM_PTR(
1722 			CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg);
1723 		alt_path->flow_label = cpu_to_be32(
1724 			IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg));
1725 		alt_path->hop_limit =
1726 			IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg);
1727 		alt_path->traffic_class =
1728 			IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg);
1729 		alt_path->reversible = 1;
1730 		alt_path->pkey =
1731 			cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1732 		alt_path->sl = IBA_GET(CM_REQ_ALTERNATE_SL, req_msg);
1733 		alt_path->mtu_selector = IB_SA_EQ;
1734 		alt_path->mtu =
1735 			IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1736 		alt_path->rate_selector = IB_SA_EQ;
1737 		alt_path->rate = IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg);
1738 		alt_path->packet_life_time_selector = IB_SA_EQ;
1739 		alt_path->packet_life_time =
1740 			IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg);
1741 		alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1742 		alt_path->service_id =
1743 			cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1744 
1745 		if (sa_path_is_roce(alt_path))
1746 			alt_path->roce.route_resolved = false;
1747 	}
1748 	cm_format_path_lid_from_req(req_msg, primary_path, alt_path, wc);
1749 }
1750 
cm_get_bth_pkey(struct cm_work * work)1751 static u16 cm_get_bth_pkey(struct cm_work *work)
1752 {
1753 	struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1754 	u8 port_num = work->port->port_num;
1755 	u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1756 	u16 pkey;
1757 	int ret;
1758 
1759 	ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1760 	if (ret) {
1761 		dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1762 				     port_num, pkey_index, ret);
1763 		return 0;
1764 	}
1765 
1766 	return pkey;
1767 }
1768 
1769 /**
1770  * Convert OPA SGID to IB SGID
1771  * ULPs (such as IPoIB) do not understand OPA GIDs and will
1772  * reject them as the local_gid will not match the sgid. Therefore,
1773  * change the pathrec's SGID to an IB SGID.
1774  *
1775  * @work: Work completion
1776  * @path: Path record
1777  */
cm_opa_to_ib_sgid(struct cm_work * work,struct sa_path_rec * path)1778 static void cm_opa_to_ib_sgid(struct cm_work *work,
1779 			      struct sa_path_rec *path)
1780 {
1781 	struct ib_device *dev = work->port->cm_dev->ib_device;
1782 	u8 port_num = work->port->port_num;
1783 
1784 	if (rdma_cap_opa_ah(dev, port_num) &&
1785 	    (ib_is_opa_gid(&path->sgid))) {
1786 		union ib_gid sgid;
1787 
1788 		if (rdma_query_gid(dev, port_num, 0, &sgid)) {
1789 			dev_warn(&dev->dev,
1790 				 "Error updating sgid in CM request\n");
1791 			return;
1792 		}
1793 
1794 		path->sgid = sgid;
1795 	}
1796 }
1797 
cm_format_req_event(struct cm_work * work,struct cm_id_private * cm_id_priv,struct ib_cm_id * listen_id)1798 static void cm_format_req_event(struct cm_work *work,
1799 				struct cm_id_private *cm_id_priv,
1800 				struct ib_cm_id *listen_id)
1801 {
1802 	struct cm_req_msg *req_msg;
1803 	struct ib_cm_req_event_param *param;
1804 
1805 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1806 	param = &work->cm_event.param.req_rcvd;
1807 	param->listen_id = listen_id;
1808 	param->bth_pkey = cm_get_bth_pkey(work);
1809 	param->port = cm_id_priv->av.port->port_num;
1810 	param->primary_path = &work->path[0];
1811 	cm_opa_to_ib_sgid(work, param->primary_path);
1812 	if (cm_req_has_alt_path(req_msg)) {
1813 		param->alternate_path = &work->path[1];
1814 		cm_opa_to_ib_sgid(work, param->alternate_path);
1815 	} else {
1816 		param->alternate_path = NULL;
1817 	}
1818 	param->remote_ca_guid =
1819 		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
1820 	param->remote_qkey = IBA_GET(CM_REQ_LOCAL_Q_KEY, req_msg);
1821 	param->remote_qpn = IBA_GET(CM_REQ_LOCAL_QPN, req_msg);
1822 	param->qp_type = cm_req_get_qp_type(req_msg);
1823 	param->starting_psn = IBA_GET(CM_REQ_STARTING_PSN, req_msg);
1824 	param->responder_resources = IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
1825 	param->initiator_depth = IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
1826 	param->local_cm_response_timeout =
1827 		IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg);
1828 	param->flow_control = IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg);
1829 	param->remote_cm_response_timeout =
1830 		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg);
1831 	param->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
1832 	param->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
1833 	param->srq = IBA_GET(CM_REQ_SRQ, req_msg);
1834 	param->ppath_sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
1835 	param->ece.vendor_id = IBA_GET(CM_REQ_VENDOR_ID, req_msg);
1836 	param->ece.attr_mod = be32_to_cpu(req_msg->hdr.attr_mod);
1837 
1838 	work->cm_event.private_data =
1839 		IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA, req_msg);
1840 }
1841 
cm_process_work(struct cm_id_private * cm_id_priv,struct cm_work * work)1842 static void cm_process_work(struct cm_id_private *cm_id_priv,
1843 			    struct cm_work *work)
1844 {
1845 	int ret;
1846 
1847 	/* We will typically only have the current event to report. */
1848 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1849 	cm_free_work(work);
1850 
1851 	while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1852 		spin_lock_irq(&cm_id_priv->lock);
1853 		work = cm_dequeue_work(cm_id_priv);
1854 		spin_unlock_irq(&cm_id_priv->lock);
1855 		if (!work)
1856 			return;
1857 
1858 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1859 						&work->cm_event);
1860 		cm_free_work(work);
1861 	}
1862 	cm_deref_id(cm_id_priv);
1863 	if (ret)
1864 		cm_destroy_id(&cm_id_priv->id, ret);
1865 }
1866 
cm_format_mra(struct cm_mra_msg * mra_msg,struct cm_id_private * cm_id_priv,enum cm_msg_response msg_mraed,u8 service_timeout,const void * private_data,u8 private_data_len)1867 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1868 			  struct cm_id_private *cm_id_priv,
1869 			  enum cm_msg_response msg_mraed, u8 service_timeout,
1870 			  const void *private_data, u8 private_data_len)
1871 {
1872 	cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1873 	IBA_SET(CM_MRA_MESSAGE_MRAED, mra_msg, msg_mraed);
1874 	IBA_SET(CM_MRA_LOCAL_COMM_ID, mra_msg,
1875 		be32_to_cpu(cm_id_priv->id.local_id));
1876 	IBA_SET(CM_MRA_REMOTE_COMM_ID, mra_msg,
1877 		be32_to_cpu(cm_id_priv->id.remote_id));
1878 	IBA_SET(CM_MRA_SERVICE_TIMEOUT, mra_msg, service_timeout);
1879 
1880 	if (private_data && private_data_len)
1881 		IBA_SET_MEM(CM_MRA_PRIVATE_DATA, mra_msg, private_data,
1882 			    private_data_len);
1883 }
1884 
cm_format_rej(struct cm_rej_msg * rej_msg,struct cm_id_private * cm_id_priv,enum ib_cm_rej_reason reason,void * ari,u8 ari_length,const void * private_data,u8 private_data_len,enum ib_cm_state state)1885 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1886 			  struct cm_id_private *cm_id_priv,
1887 			  enum ib_cm_rej_reason reason, void *ari,
1888 			  u8 ari_length, const void *private_data,
1889 			  u8 private_data_len, enum ib_cm_state state)
1890 {
1891 	lockdep_assert_held(&cm_id_priv->lock);
1892 
1893 	cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1894 	IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1895 		be32_to_cpu(cm_id_priv->id.remote_id));
1896 
1897 	switch (state) {
1898 	case IB_CM_REQ_RCVD:
1899 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, be32_to_cpu(0));
1900 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1901 		break;
1902 	case IB_CM_MRA_REQ_SENT:
1903 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1904 			be32_to_cpu(cm_id_priv->id.local_id));
1905 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1906 		break;
1907 	case IB_CM_REP_RCVD:
1908 	case IB_CM_MRA_REP_SENT:
1909 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1910 			be32_to_cpu(cm_id_priv->id.local_id));
1911 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REP);
1912 		break;
1913 	default:
1914 		IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1915 			be32_to_cpu(cm_id_priv->id.local_id));
1916 		IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg,
1917 			CM_MSG_RESPONSE_OTHER);
1918 		break;
1919 	}
1920 
1921 	IBA_SET(CM_REJ_REASON, rej_msg, reason);
1922 	if (ari && ari_length) {
1923 		IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1924 		IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1925 	}
1926 
1927 	if (private_data && private_data_len)
1928 		IBA_SET_MEM(CM_REJ_PRIVATE_DATA, rej_msg, private_data,
1929 			    private_data_len);
1930 }
1931 
cm_dup_req_handler(struct cm_work * work,struct cm_id_private * cm_id_priv)1932 static void cm_dup_req_handler(struct cm_work *work,
1933 			       struct cm_id_private *cm_id_priv)
1934 {
1935 	struct ib_mad_send_buf *msg = NULL;
1936 	int ret;
1937 
1938 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1939 			counter[CM_REQ_COUNTER]);
1940 
1941 	/* Quick state check to discard duplicate REQs. */
1942 	spin_lock_irq(&cm_id_priv->lock);
1943 	if (cm_id_priv->id.state == IB_CM_REQ_RCVD) {
1944 		spin_unlock_irq(&cm_id_priv->lock);
1945 		return;
1946 	}
1947 	spin_unlock_irq(&cm_id_priv->lock);
1948 
1949 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1950 	if (ret)
1951 		return;
1952 
1953 	spin_lock_irq(&cm_id_priv->lock);
1954 	switch (cm_id_priv->id.state) {
1955 	case IB_CM_MRA_REQ_SENT:
1956 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1957 			      CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1958 			      cm_id_priv->private_data,
1959 			      cm_id_priv->private_data_len);
1960 		break;
1961 	case IB_CM_TIMEWAIT:
1962 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv,
1963 			      IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0,
1964 			      IB_CM_TIMEWAIT);
1965 		break;
1966 	default:
1967 		goto unlock;
1968 	}
1969 	spin_unlock_irq(&cm_id_priv->lock);
1970 
1971 	trace_icm_send_dup_req(&cm_id_priv->id);
1972 	ret = ib_post_send_mad(msg, NULL);
1973 	if (ret)
1974 		goto free;
1975 	return;
1976 
1977 unlock:	spin_unlock_irq(&cm_id_priv->lock);
1978 free:	cm_free_msg(msg);
1979 }
1980 
cm_match_req(struct cm_work * work,struct cm_id_private * cm_id_priv)1981 static struct cm_id_private * cm_match_req(struct cm_work *work,
1982 					   struct cm_id_private *cm_id_priv)
1983 {
1984 	struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1985 	struct cm_timewait_info *timewait_info;
1986 	struct cm_req_msg *req_msg;
1987 
1988 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1989 
1990 	/* Check for possible duplicate REQ. */
1991 	spin_lock_irq(&cm.lock);
1992 	timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1993 	if (timewait_info) {
1994 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
1995 					   timewait_info->work.remote_id);
1996 		spin_unlock_irq(&cm.lock);
1997 		if (cur_cm_id_priv) {
1998 			cm_dup_req_handler(work, cur_cm_id_priv);
1999 			cm_deref_id(cur_cm_id_priv);
2000 		}
2001 		return NULL;
2002 	}
2003 
2004 	/* Check for stale connections. */
2005 	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2006 	if (timewait_info) {
2007 		cm_remove_remote(cm_id_priv);
2008 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2009 					   timewait_info->work.remote_id);
2010 
2011 		spin_unlock_irq(&cm.lock);
2012 		cm_issue_rej(work->port, work->mad_recv_wc,
2013 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
2014 			     NULL, 0);
2015 		if (cur_cm_id_priv) {
2016 			ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2017 			cm_deref_id(cur_cm_id_priv);
2018 		}
2019 		return NULL;
2020 	}
2021 
2022 	/* Find matching listen request. */
2023 	listen_cm_id_priv = cm_find_listen(
2024 		cm_id_priv->id.device,
2025 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)));
2026 	if (!listen_cm_id_priv) {
2027 		cm_remove_remote(cm_id_priv);
2028 		spin_unlock_irq(&cm.lock);
2029 		cm_issue_rej(work->port, work->mad_recv_wc,
2030 			     IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
2031 			     NULL, 0);
2032 		return NULL;
2033 	}
2034 	spin_unlock_irq(&cm.lock);
2035 	return listen_cm_id_priv;
2036 }
2037 
2038 /*
2039  * Work-around for inter-subnet connections.  If the LIDs are permissive,
2040  * we need to override the LID/SL data in the REQ with the LID information
2041  * in the work completion.
2042  */
cm_process_routed_req(struct cm_req_msg * req_msg,struct ib_wc * wc)2043 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
2044 {
2045 	if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg)) {
2046 		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
2047 					req_msg)) == IB_LID_PERMISSIVE) {
2048 			IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
2049 				be16_to_cpu(ib_lid_be16(wc->slid)));
2050 			IBA_SET(CM_REQ_PRIMARY_SL, req_msg, wc->sl);
2051 		}
2052 
2053 		if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
2054 					req_msg)) == IB_LID_PERMISSIVE)
2055 			IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
2056 				wc->dlid_path_bits);
2057 	}
2058 
2059 	if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg)) {
2060 		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
2061 					req_msg)) == IB_LID_PERMISSIVE) {
2062 			IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
2063 				be16_to_cpu(ib_lid_be16(wc->slid)));
2064 			IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, wc->sl);
2065 		}
2066 
2067 		if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
2068 					req_msg)) == IB_LID_PERMISSIVE)
2069 			IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
2070 				wc->dlid_path_bits);
2071 	}
2072 }
2073 
cm_req_handler(struct cm_work * work)2074 static int cm_req_handler(struct cm_work *work)
2075 {
2076 	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
2077 	struct cm_req_msg *req_msg;
2078 	const struct ib_global_route *grh;
2079 	const struct ib_gid_attr *gid_attr;
2080 	int ret;
2081 
2082 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
2083 
2084 	cm_id_priv =
2085 		cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
2086 	if (IS_ERR(cm_id_priv))
2087 		return PTR_ERR(cm_id_priv);
2088 
2089 	cm_id_priv->id.remote_id =
2090 		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg));
2091 	cm_id_priv->id.service_id =
2092 		cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
2093 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
2094 	cm_id_priv->tid = req_msg->hdr.tid;
2095 	cm_id_priv->timeout_ms = cm_convert_to_ms(
2096 		IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg));
2097 	cm_id_priv->max_cm_retries = IBA_GET(CM_REQ_MAX_CM_RETRIES, req_msg);
2098 	cm_id_priv->remote_qpn =
2099 		cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
2100 	cm_id_priv->initiator_depth =
2101 		IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
2102 	cm_id_priv->responder_resources =
2103 		IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
2104 	cm_id_priv->path_mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
2105 	cm_id_priv->pkey = cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
2106 	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
2107 	cm_id_priv->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
2108 	cm_id_priv->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
2109 	cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
2110 
2111 	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2112 				      work->mad_recv_wc->recv_buf.grh,
2113 				      &cm_id_priv->av);
2114 	if (ret)
2115 		goto destroy;
2116 	cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
2117 							    id.local_id);
2118 	if (IS_ERR(cm_id_priv->timewait_info)) {
2119 		ret = PTR_ERR(cm_id_priv->timewait_info);
2120 		cm_id_priv->timewait_info = NULL;
2121 		goto destroy;
2122 	}
2123 	cm_id_priv->timewait_info->work.remote_id = cm_id_priv->id.remote_id;
2124 	cm_id_priv->timewait_info->remote_ca_guid =
2125 		cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
2126 	cm_id_priv->timewait_info->remote_qpn = cm_id_priv->remote_qpn;
2127 
2128 	/*
2129 	 * Note that the ID pointer is not in the xarray at this point,
2130 	 * so this set is only visible to the local thread.
2131 	 */
2132 	cm_id_priv->id.state = IB_CM_REQ_RCVD;
2133 
2134 	listen_cm_id_priv = cm_match_req(work, cm_id_priv);
2135 	if (!listen_cm_id_priv) {
2136 		trace_icm_no_listener_err(&cm_id_priv->id);
2137 		cm_id_priv->id.state = IB_CM_IDLE;
2138 		ret = -EINVAL;
2139 		goto destroy;
2140 	}
2141 
2142 	if (cm_id_priv->av.ah_attr.type != RDMA_AH_ATTR_TYPE_ROCE)
2143 		cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
2144 
2145 	memset(&work->path[0], 0, sizeof(work->path[0]));
2146 	if (cm_req_has_alt_path(req_msg))
2147 		memset(&work->path[1], 0, sizeof(work->path[1]));
2148 	grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
2149 	gid_attr = grh->sgid_attr;
2150 
2151 	if (gid_attr &&
2152 	    rdma_protocol_roce(work->port->cm_dev->ib_device,
2153 			       work->port->port_num)) {
2154 		work->path[0].rec_type =
2155 			sa_conv_gid_to_pathrec_type(gid_attr->gid_type);
2156 	} else {
2157 		cm_path_set_rec_type(
2158 			work->port->cm_dev->ib_device, work->port->port_num,
2159 			&work->path[0],
2160 			IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID,
2161 					req_msg));
2162 	}
2163 	if (cm_req_has_alt_path(req_msg))
2164 		work->path[1].rec_type = work->path[0].rec_type;
2165 	cm_format_paths_from_req(req_msg, &work->path[0],
2166 				 &work->path[1], work->mad_recv_wc->wc);
2167 	if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE)
2168 		sa_path_set_dmac(&work->path[0],
2169 				 cm_id_priv->av.ah_attr.roce.dmac);
2170 	work->path[0].hop_limit = grh->hop_limit;
2171 	ret = cm_init_av_by_path(&work->path[0], gid_attr, &cm_id_priv->av,
2172 				 cm_id_priv);
2173 	if (ret) {
2174 		int err;
2175 
2176 		err = rdma_query_gid(work->port->cm_dev->ib_device,
2177 				     work->port->port_num, 0,
2178 				     &work->path[0].sgid);
2179 		if (err)
2180 			ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2181 				       NULL, 0, NULL, 0);
2182 		else
2183 			ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2184 				       &work->path[0].sgid,
2185 				       sizeof(work->path[0].sgid),
2186 				       NULL, 0);
2187 		goto rejected;
2188 	}
2189 	if (cm_req_has_alt_path(req_msg)) {
2190 		ret = cm_init_av_by_path(&work->path[1], NULL,
2191 					 &cm_id_priv->alt_av, cm_id_priv);
2192 		if (ret) {
2193 			ib_send_cm_rej(&cm_id_priv->id,
2194 				       IB_CM_REJ_INVALID_ALT_GID,
2195 				       &work->path[0].sgid,
2196 				       sizeof(work->path[0].sgid), NULL, 0);
2197 			goto rejected;
2198 		}
2199 	}
2200 
2201 	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
2202 	cm_id_priv->id.context = listen_cm_id_priv->id.context;
2203 	cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
2204 
2205 	/* Now MAD handlers can see the new ID */
2206 	spin_lock_irq(&cm_id_priv->lock);
2207 	cm_finalize_id(cm_id_priv);
2208 
2209 	/* Refcount belongs to the event, pairs with cm_process_work() */
2210 	refcount_inc(&cm_id_priv->refcount);
2211 	cm_queue_work_unlock(cm_id_priv, work);
2212 	/*
2213 	 * Since this ID was just created and was not made visible to other MAD
2214 	 * handlers until the cm_finalize_id() above we know that the
2215 	 * cm_process_work() will deliver the event and the listen_cm_id
2216 	 * embedded in the event can be derefed here.
2217 	 */
2218 	cm_deref_id(listen_cm_id_priv);
2219 	return 0;
2220 
2221 rejected:
2222 	cm_deref_id(listen_cm_id_priv);
2223 destroy:
2224 	ib_destroy_cm_id(&cm_id_priv->id);
2225 	return ret;
2226 }
2227 
cm_format_rep(struct cm_rep_msg * rep_msg,struct cm_id_private * cm_id_priv,struct ib_cm_rep_param * param)2228 static void cm_format_rep(struct cm_rep_msg *rep_msg,
2229 			  struct cm_id_private *cm_id_priv,
2230 			  struct ib_cm_rep_param *param)
2231 {
2232 	cm_format_mad_ece_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid,
2233 			      param->ece.attr_mod);
2234 	IBA_SET(CM_REP_LOCAL_COMM_ID, rep_msg,
2235 		be32_to_cpu(cm_id_priv->id.local_id));
2236 	IBA_SET(CM_REP_REMOTE_COMM_ID, rep_msg,
2237 		be32_to_cpu(cm_id_priv->id.remote_id));
2238 	IBA_SET(CM_REP_STARTING_PSN, rep_msg, param->starting_psn);
2239 	IBA_SET(CM_REP_RESPONDER_RESOURCES, rep_msg,
2240 		param->responder_resources);
2241 	IBA_SET(CM_REP_TARGET_ACK_DELAY, rep_msg,
2242 		cm_id_priv->av.port->cm_dev->ack_delay);
2243 	IBA_SET(CM_REP_FAILOVER_ACCEPTED, rep_msg, param->failover_accepted);
2244 	IBA_SET(CM_REP_RNR_RETRY_COUNT, rep_msg, param->rnr_retry_count);
2245 	IBA_SET(CM_REP_LOCAL_CA_GUID, rep_msg,
2246 		be64_to_cpu(cm_id_priv->id.device->node_guid));
2247 
2248 	if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
2249 		IBA_SET(CM_REP_INITIATOR_DEPTH, rep_msg,
2250 			param->initiator_depth);
2251 		IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg,
2252 			param->flow_control);
2253 		IBA_SET(CM_REP_SRQ, rep_msg, param->srq);
2254 		IBA_SET(CM_REP_LOCAL_QPN, rep_msg, param->qp_num);
2255 	} else {
2256 		IBA_SET(CM_REP_SRQ, rep_msg, 1);
2257 		IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER, rep_msg, param->qp_num);
2258 	}
2259 
2260 	IBA_SET(CM_REP_VENDOR_ID_L, rep_msg, param->ece.vendor_id);
2261 	IBA_SET(CM_REP_VENDOR_ID_M, rep_msg, param->ece.vendor_id >> 8);
2262 	IBA_SET(CM_REP_VENDOR_ID_H, rep_msg, param->ece.vendor_id >> 16);
2263 
2264 	if (param->private_data && param->private_data_len)
2265 		IBA_SET_MEM(CM_REP_PRIVATE_DATA, rep_msg, param->private_data,
2266 			    param->private_data_len);
2267 }
2268 
ib_send_cm_rep(struct ib_cm_id * cm_id,struct ib_cm_rep_param * param)2269 int ib_send_cm_rep(struct ib_cm_id *cm_id,
2270 		   struct ib_cm_rep_param *param)
2271 {
2272 	struct cm_id_private *cm_id_priv;
2273 	struct ib_mad_send_buf *msg;
2274 	struct cm_rep_msg *rep_msg;
2275 	unsigned long flags;
2276 	int ret;
2277 
2278 	if (param->private_data &&
2279 	    param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
2280 		return -EINVAL;
2281 
2282 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2283 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2284 	if (cm_id->state != IB_CM_REQ_RCVD &&
2285 	    cm_id->state != IB_CM_MRA_REQ_SENT) {
2286 		trace_icm_send_rep_err(cm_id_priv->id.local_id, cm_id->state);
2287 		ret = -EINVAL;
2288 		goto out;
2289 	}
2290 
2291 	ret = cm_alloc_msg(cm_id_priv, &msg);
2292 	if (ret)
2293 		goto out;
2294 
2295 	rep_msg = (struct cm_rep_msg *) msg->mad;
2296 	cm_format_rep(rep_msg, cm_id_priv, param);
2297 	msg->timeout_ms = cm_id_priv->timeout_ms;
2298 	msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
2299 
2300 	trace_icm_send_rep(cm_id);
2301 	ret = ib_post_send_mad(msg, NULL);
2302 	if (ret) {
2303 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2304 		cm_free_msg(msg);
2305 		return ret;
2306 	}
2307 
2308 	cm_id->state = IB_CM_REP_SENT;
2309 	cm_id_priv->msg = msg;
2310 	cm_id_priv->initiator_depth = param->initiator_depth;
2311 	cm_id_priv->responder_resources = param->responder_resources;
2312 	cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2313 	WARN_ONCE(param->qp_num & 0xFF000000,
2314 		  "IBTA declares QPN to be 24 bits, but it is 0x%X\n",
2315 		  param->qp_num);
2316 	cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
2317 
2318 out:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2319 	return ret;
2320 }
2321 EXPORT_SYMBOL(ib_send_cm_rep);
2322 
cm_format_rtu(struct cm_rtu_msg * rtu_msg,struct cm_id_private * cm_id_priv,const void * private_data,u8 private_data_len)2323 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
2324 			  struct cm_id_private *cm_id_priv,
2325 			  const void *private_data,
2326 			  u8 private_data_len)
2327 {
2328 	cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
2329 	IBA_SET(CM_RTU_LOCAL_COMM_ID, rtu_msg,
2330 		be32_to_cpu(cm_id_priv->id.local_id));
2331 	IBA_SET(CM_RTU_REMOTE_COMM_ID, rtu_msg,
2332 		be32_to_cpu(cm_id_priv->id.remote_id));
2333 
2334 	if (private_data && private_data_len)
2335 		IBA_SET_MEM(CM_RTU_PRIVATE_DATA, rtu_msg, private_data,
2336 			    private_data_len);
2337 }
2338 
ib_send_cm_rtu(struct ib_cm_id * cm_id,const void * private_data,u8 private_data_len)2339 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
2340 		   const void *private_data,
2341 		   u8 private_data_len)
2342 {
2343 	struct cm_id_private *cm_id_priv;
2344 	struct ib_mad_send_buf *msg;
2345 	unsigned long flags;
2346 	void *data;
2347 	int ret;
2348 
2349 	if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
2350 		return -EINVAL;
2351 
2352 	data = cm_copy_private_data(private_data, private_data_len);
2353 	if (IS_ERR(data))
2354 		return PTR_ERR(data);
2355 
2356 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2357 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2358 	if (cm_id->state != IB_CM_REP_RCVD &&
2359 	    cm_id->state != IB_CM_MRA_REP_SENT) {
2360 		trace_icm_send_cm_rtu_err(cm_id);
2361 		ret = -EINVAL;
2362 		goto error;
2363 	}
2364 
2365 	ret = cm_alloc_msg(cm_id_priv, &msg);
2366 	if (ret)
2367 		goto error;
2368 
2369 	cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2370 		      private_data, private_data_len);
2371 
2372 	trace_icm_send_rtu(cm_id);
2373 	ret = ib_post_send_mad(msg, NULL);
2374 	if (ret) {
2375 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2376 		cm_free_msg(msg);
2377 		kfree(data);
2378 		return ret;
2379 	}
2380 
2381 	cm_id->state = IB_CM_ESTABLISHED;
2382 	cm_set_private_data(cm_id_priv, data, private_data_len);
2383 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2384 	return 0;
2385 
2386 error:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2387 	kfree(data);
2388 	return ret;
2389 }
2390 EXPORT_SYMBOL(ib_send_cm_rtu);
2391 
cm_format_rep_event(struct cm_work * work,enum ib_qp_type qp_type)2392 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
2393 {
2394 	struct cm_rep_msg *rep_msg;
2395 	struct ib_cm_rep_event_param *param;
2396 
2397 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2398 	param = &work->cm_event.param.rep_rcvd;
2399 	param->remote_ca_guid =
2400 		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2401 	param->remote_qkey = IBA_GET(CM_REP_LOCAL_Q_KEY, rep_msg);
2402 	param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
2403 	param->starting_psn = IBA_GET(CM_REP_STARTING_PSN, rep_msg);
2404 	param->responder_resources = IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2405 	param->initiator_depth = IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2406 	param->target_ack_delay = IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2407 	param->failover_accepted = IBA_GET(CM_REP_FAILOVER_ACCEPTED, rep_msg);
2408 	param->flow_control = IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg);
2409 	param->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2410 	param->srq = IBA_GET(CM_REP_SRQ, rep_msg);
2411 	param->ece.vendor_id = IBA_GET(CM_REP_VENDOR_ID_H, rep_msg) << 16;
2412 	param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_M, rep_msg) << 8;
2413 	param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_L, rep_msg);
2414 	param->ece.attr_mod = be32_to_cpu(rep_msg->hdr.attr_mod);
2415 
2416 	work->cm_event.private_data =
2417 		IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA, rep_msg);
2418 }
2419 
cm_dup_rep_handler(struct cm_work * work)2420 static void cm_dup_rep_handler(struct cm_work *work)
2421 {
2422 	struct cm_id_private *cm_id_priv;
2423 	struct cm_rep_msg *rep_msg;
2424 	struct ib_mad_send_buf *msg = NULL;
2425 	int ret;
2426 
2427 	rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2428 	cm_id_priv = cm_acquire_id(
2429 		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)),
2430 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)));
2431 	if (!cm_id_priv)
2432 		return;
2433 
2434 	atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2435 			counter[CM_REP_COUNTER]);
2436 	ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
2437 	if (ret)
2438 		goto deref;
2439 
2440 	spin_lock_irq(&cm_id_priv->lock);
2441 	if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
2442 		cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2443 			      cm_id_priv->private_data,
2444 			      cm_id_priv->private_data_len);
2445 	else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
2446 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2447 			      CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
2448 			      cm_id_priv->private_data,
2449 			      cm_id_priv->private_data_len);
2450 	else
2451 		goto unlock;
2452 	spin_unlock_irq(&cm_id_priv->lock);
2453 
2454 	trace_icm_send_dup_rep(&cm_id_priv->id);
2455 	ret = ib_post_send_mad(msg, NULL);
2456 	if (ret)
2457 		goto free;
2458 	goto deref;
2459 
2460 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2461 free:	cm_free_msg(msg);
2462 deref:	cm_deref_id(cm_id_priv);
2463 }
2464 
cm_rep_handler(struct cm_work * work)2465 static int cm_rep_handler(struct cm_work *work)
2466 {
2467 	struct cm_id_private *cm_id_priv;
2468 	struct cm_rep_msg *rep_msg;
2469 	int ret;
2470 	struct cm_id_private *cur_cm_id_priv;
2471 	struct cm_timewait_info *timewait_info;
2472 
2473 	rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2474 	cm_id_priv = cm_acquire_id(
2475 		cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 0);
2476 	if (!cm_id_priv) {
2477 		cm_dup_rep_handler(work);
2478 		trace_icm_remote_no_priv_err(
2479 			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2480 		return -EINVAL;
2481 	}
2482 
2483 	cm_format_rep_event(work, cm_id_priv->qp_type);
2484 
2485 	spin_lock_irq(&cm_id_priv->lock);
2486 	switch (cm_id_priv->id.state) {
2487 	case IB_CM_REQ_SENT:
2488 	case IB_CM_MRA_REQ_RCVD:
2489 		break;
2490 	default:
2491 		ret = -EINVAL;
2492 		trace_icm_rep_unknown_err(
2493 			IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2494 			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg),
2495 			cm_id_priv->id.state);
2496 		spin_unlock_irq(&cm_id_priv->lock);
2497 		goto error;
2498 	}
2499 
2500 	cm_id_priv->timewait_info->work.remote_id =
2501 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2502 	cm_id_priv->timewait_info->remote_ca_guid =
2503 		cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2504 	cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2505 
2506 	spin_lock(&cm.lock);
2507 	/* Check for duplicate REP. */
2508 	if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2509 		spin_unlock(&cm.lock);
2510 		spin_unlock_irq(&cm_id_priv->lock);
2511 		ret = -EINVAL;
2512 		trace_icm_insert_failed_err(
2513 			 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2514 		goto error;
2515 	}
2516 	/* Check for a stale connection. */
2517 	timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2518 	if (timewait_info) {
2519 		cm_remove_remote(cm_id_priv);
2520 		cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2521 					   timewait_info->work.remote_id);
2522 
2523 		spin_unlock(&cm.lock);
2524 		spin_unlock_irq(&cm_id_priv->lock);
2525 		cm_issue_rej(work->port, work->mad_recv_wc,
2526 			     IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2527 			     NULL, 0);
2528 		ret = -EINVAL;
2529 		trace_icm_staleconn_err(
2530 			IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2531 			IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2532 
2533 		if (cur_cm_id_priv) {
2534 			ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2535 			cm_deref_id(cur_cm_id_priv);
2536 		}
2537 
2538 		goto error;
2539 	}
2540 	spin_unlock(&cm.lock);
2541 
2542 	cm_id_priv->id.state = IB_CM_REP_RCVD;
2543 	cm_id_priv->id.remote_id =
2544 		cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2545 	cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2546 	cm_id_priv->initiator_depth =
2547 		IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2548 	cm_id_priv->responder_resources =
2549 		IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2550 	cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2551 	cm_id_priv->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2552 	cm_id_priv->target_ack_delay =
2553 		IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2554 	cm_id_priv->av.timeout =
2555 			cm_ack_timeout(cm_id_priv->target_ack_delay,
2556 				       cm_id_priv->av.timeout - 1);
2557 	cm_id_priv->alt_av.timeout =
2558 			cm_ack_timeout(cm_id_priv->target_ack_delay,
2559 				       cm_id_priv->alt_av.timeout - 1);
2560 
2561 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2562 	cm_queue_work_unlock(cm_id_priv, work);
2563 	return 0;
2564 
2565 error:
2566 	cm_deref_id(cm_id_priv);
2567 	return ret;
2568 }
2569 
cm_establish_handler(struct cm_work * work)2570 static int cm_establish_handler(struct cm_work *work)
2571 {
2572 	struct cm_id_private *cm_id_priv;
2573 
2574 	/* See comment in cm_establish about lookup. */
2575 	cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2576 	if (!cm_id_priv)
2577 		return -EINVAL;
2578 
2579 	spin_lock_irq(&cm_id_priv->lock);
2580 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2581 		spin_unlock_irq(&cm_id_priv->lock);
2582 		goto out;
2583 	}
2584 
2585 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2586 	cm_queue_work_unlock(cm_id_priv, work);
2587 	return 0;
2588 out:
2589 	cm_deref_id(cm_id_priv);
2590 	return -EINVAL;
2591 }
2592 
cm_rtu_handler(struct cm_work * work)2593 static int cm_rtu_handler(struct cm_work *work)
2594 {
2595 	struct cm_id_private *cm_id_priv;
2596 	struct cm_rtu_msg *rtu_msg;
2597 
2598 	rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2599 	cm_id_priv = cm_acquire_id(
2600 		cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID, rtu_msg)),
2601 		cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID, rtu_msg)));
2602 	if (!cm_id_priv)
2603 		return -EINVAL;
2604 
2605 	work->cm_event.private_data =
2606 		IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA, rtu_msg);
2607 
2608 	spin_lock_irq(&cm_id_priv->lock);
2609 	if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2610 	    cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2611 		spin_unlock_irq(&cm_id_priv->lock);
2612 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2613 				counter[CM_RTU_COUNTER]);
2614 		goto out;
2615 	}
2616 	cm_id_priv->id.state = IB_CM_ESTABLISHED;
2617 
2618 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2619 	cm_queue_work_unlock(cm_id_priv, work);
2620 	return 0;
2621 out:
2622 	cm_deref_id(cm_id_priv);
2623 	return -EINVAL;
2624 }
2625 
cm_format_dreq(struct cm_dreq_msg * dreq_msg,struct cm_id_private * cm_id_priv,const void * private_data,u8 private_data_len)2626 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2627 			  struct cm_id_private *cm_id_priv,
2628 			  const void *private_data,
2629 			  u8 private_data_len)
2630 {
2631 	cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2632 			  cm_form_tid(cm_id_priv));
2633 	IBA_SET(CM_DREQ_LOCAL_COMM_ID, dreq_msg,
2634 		be32_to_cpu(cm_id_priv->id.local_id));
2635 	IBA_SET(CM_DREQ_REMOTE_COMM_ID, dreq_msg,
2636 		be32_to_cpu(cm_id_priv->id.remote_id));
2637 	IBA_SET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg,
2638 		be32_to_cpu(cm_id_priv->remote_qpn));
2639 
2640 	if (private_data && private_data_len)
2641 		IBA_SET_MEM(CM_DREQ_PRIVATE_DATA, dreq_msg, private_data,
2642 			    private_data_len);
2643 }
2644 
cm_send_dreq_locked(struct cm_id_private * cm_id_priv,const void * private_data,u8 private_data_len)2645 static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv,
2646 			       const void *private_data, u8 private_data_len)
2647 {
2648 	struct ib_mad_send_buf *msg;
2649 	int ret;
2650 
2651 	lockdep_assert_held(&cm_id_priv->lock);
2652 
2653 	if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2654 		return -EINVAL;
2655 
2656 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2657 		trace_icm_dreq_skipped(&cm_id_priv->id);
2658 		return -EINVAL;
2659 	}
2660 
2661 	if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2662 	    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2663 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2664 
2665 	ret = cm_alloc_msg(cm_id_priv, &msg);
2666 	if (ret) {
2667 		cm_enter_timewait(cm_id_priv);
2668 		return ret;
2669 	}
2670 
2671 	cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2672 		       private_data, private_data_len);
2673 	msg->timeout_ms = cm_id_priv->timeout_ms;
2674 	msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2675 
2676 	trace_icm_send_dreq(&cm_id_priv->id);
2677 	ret = ib_post_send_mad(msg, NULL);
2678 	if (ret) {
2679 		cm_enter_timewait(cm_id_priv);
2680 		cm_free_msg(msg);
2681 		return ret;
2682 	}
2683 
2684 	cm_id_priv->id.state = IB_CM_DREQ_SENT;
2685 	cm_id_priv->msg = msg;
2686 	return 0;
2687 }
2688 
ib_send_cm_dreq(struct ib_cm_id * cm_id,const void * private_data,u8 private_data_len)2689 int ib_send_cm_dreq(struct ib_cm_id *cm_id, const void *private_data,
2690 		    u8 private_data_len)
2691 {
2692 	struct cm_id_private *cm_id_priv =
2693 		container_of(cm_id, struct cm_id_private, id);
2694 	unsigned long flags;
2695 	int ret;
2696 
2697 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2698 	ret = cm_send_dreq_locked(cm_id_priv, private_data, private_data_len);
2699 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2700 	return ret;
2701 }
2702 EXPORT_SYMBOL(ib_send_cm_dreq);
2703 
cm_format_drep(struct cm_drep_msg * drep_msg,struct cm_id_private * cm_id_priv,const void * private_data,u8 private_data_len)2704 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2705 			  struct cm_id_private *cm_id_priv,
2706 			  const void *private_data,
2707 			  u8 private_data_len)
2708 {
2709 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2710 	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2711 		be32_to_cpu(cm_id_priv->id.local_id));
2712 	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2713 		be32_to_cpu(cm_id_priv->id.remote_id));
2714 
2715 	if (private_data && private_data_len)
2716 		IBA_SET_MEM(CM_DREP_PRIVATE_DATA, drep_msg, private_data,
2717 			    private_data_len);
2718 }
2719 
cm_send_drep_locked(struct cm_id_private * cm_id_priv,void * private_data,u8 private_data_len)2720 static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
2721 			       void *private_data, u8 private_data_len)
2722 {
2723 	struct ib_mad_send_buf *msg;
2724 	int ret;
2725 
2726 	lockdep_assert_held(&cm_id_priv->lock);
2727 
2728 	if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2729 		return -EINVAL;
2730 
2731 	if (cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2732 		trace_icm_send_drep_err(&cm_id_priv->id);
2733 		kfree(private_data);
2734 		return -EINVAL;
2735 	}
2736 
2737 	cm_set_private_data(cm_id_priv, private_data, private_data_len);
2738 	cm_enter_timewait(cm_id_priv);
2739 
2740 	ret = cm_alloc_msg(cm_id_priv, &msg);
2741 	if (ret)
2742 		return ret;
2743 
2744 	cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2745 		       private_data, private_data_len);
2746 
2747 	trace_icm_send_drep(&cm_id_priv->id);
2748 	ret = ib_post_send_mad(msg, NULL);
2749 	if (ret) {
2750 		cm_free_msg(msg);
2751 		return ret;
2752 	}
2753 	return 0;
2754 }
2755 
ib_send_cm_drep(struct ib_cm_id * cm_id,const void * private_data,u8 private_data_len)2756 int ib_send_cm_drep(struct ib_cm_id *cm_id, const void *private_data,
2757 		    u8 private_data_len)
2758 {
2759 	struct cm_id_private *cm_id_priv =
2760 		container_of(cm_id, struct cm_id_private, id);
2761 	unsigned long flags;
2762 	void *data;
2763 	int ret;
2764 
2765 	data = cm_copy_private_data(private_data, private_data_len);
2766 	if (IS_ERR(data))
2767 		return PTR_ERR(data);
2768 
2769 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2770 	ret = cm_send_drep_locked(cm_id_priv, data, private_data_len);
2771 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2772 	return ret;
2773 }
2774 EXPORT_SYMBOL(ib_send_cm_drep);
2775 
cm_issue_drep(struct cm_port * port,struct ib_mad_recv_wc * mad_recv_wc)2776 static int cm_issue_drep(struct cm_port *port,
2777 			 struct ib_mad_recv_wc *mad_recv_wc)
2778 {
2779 	struct ib_mad_send_buf *msg = NULL;
2780 	struct cm_dreq_msg *dreq_msg;
2781 	struct cm_drep_msg *drep_msg;
2782 	int ret;
2783 
2784 	ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2785 	if (ret)
2786 		return ret;
2787 
2788 	dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2789 	drep_msg = (struct cm_drep_msg *) msg->mad;
2790 
2791 	cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2792 	IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2793 		IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg));
2794 	IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2795 		IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2796 
2797 	trace_icm_issue_drep(
2798 		IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2799 		IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2800 	ret = ib_post_send_mad(msg, NULL);
2801 	if (ret)
2802 		cm_free_msg(msg);
2803 
2804 	return ret;
2805 }
2806 
cm_dreq_handler(struct cm_work * work)2807 static int cm_dreq_handler(struct cm_work *work)
2808 {
2809 	struct cm_id_private *cm_id_priv;
2810 	struct cm_dreq_msg *dreq_msg;
2811 	struct ib_mad_send_buf *msg = NULL;
2812 
2813 	dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2814 	cm_id_priv = cm_acquire_id(
2815 		cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)),
2816 		cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg)));
2817 	if (!cm_id_priv) {
2818 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2819 				counter[CM_DREQ_COUNTER]);
2820 		cm_issue_drep(work->port, work->mad_recv_wc);
2821 		trace_icm_no_priv_err(
2822 			IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2823 			IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2824 		return -EINVAL;
2825 	}
2826 
2827 	work->cm_event.private_data =
2828 		IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA, dreq_msg);
2829 
2830 	spin_lock_irq(&cm_id_priv->lock);
2831 	if (cm_id_priv->local_qpn !=
2832 	    cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg)))
2833 		goto unlock;
2834 
2835 	switch (cm_id_priv->id.state) {
2836 	case IB_CM_REP_SENT:
2837 	case IB_CM_DREQ_SENT:
2838 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2839 		break;
2840 	case IB_CM_ESTABLISHED:
2841 		if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2842 		    cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2843 			ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2844 		break;
2845 	case IB_CM_MRA_REP_RCVD:
2846 		break;
2847 	case IB_CM_TIMEWAIT:
2848 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2849 				counter[CM_DREQ_COUNTER]);
2850 		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
2851 		if (IS_ERR(msg))
2852 			goto unlock;
2853 
2854 		cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2855 			       cm_id_priv->private_data,
2856 			       cm_id_priv->private_data_len);
2857 		spin_unlock_irq(&cm_id_priv->lock);
2858 
2859 		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
2860 		    ib_post_send_mad(msg, NULL))
2861 			cm_free_msg(msg);
2862 		goto deref;
2863 	case IB_CM_DREQ_RCVD:
2864 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2865 				counter[CM_DREQ_COUNTER]);
2866 		goto unlock;
2867 	default:
2868 		trace_icm_dreq_unknown_err(&cm_id_priv->id);
2869 		goto unlock;
2870 	}
2871 	cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2872 	cm_id_priv->tid = dreq_msg->hdr.tid;
2873 	cm_queue_work_unlock(cm_id_priv, work);
2874 	return 0;
2875 
2876 unlock:	spin_unlock_irq(&cm_id_priv->lock);
2877 deref:	cm_deref_id(cm_id_priv);
2878 	return -EINVAL;
2879 }
2880 
cm_drep_handler(struct cm_work * work)2881 static int cm_drep_handler(struct cm_work *work)
2882 {
2883 	struct cm_id_private *cm_id_priv;
2884 	struct cm_drep_msg *drep_msg;
2885 
2886 	drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2887 	cm_id_priv = cm_acquire_id(
2888 		cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID, drep_msg)),
2889 		cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID, drep_msg)));
2890 	if (!cm_id_priv)
2891 		return -EINVAL;
2892 
2893 	work->cm_event.private_data =
2894 		IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA, drep_msg);
2895 
2896 	spin_lock_irq(&cm_id_priv->lock);
2897 	if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2898 	    cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2899 		spin_unlock_irq(&cm_id_priv->lock);
2900 		goto out;
2901 	}
2902 	cm_enter_timewait(cm_id_priv);
2903 
2904 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2905 	cm_queue_work_unlock(cm_id_priv, work);
2906 	return 0;
2907 out:
2908 	cm_deref_id(cm_id_priv);
2909 	return -EINVAL;
2910 }
2911 
cm_send_rej_locked(struct cm_id_private * cm_id_priv,enum ib_cm_rej_reason reason,void * ari,u8 ari_length,const void * private_data,u8 private_data_len)2912 static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
2913 			      enum ib_cm_rej_reason reason, void *ari,
2914 			      u8 ari_length, const void *private_data,
2915 			      u8 private_data_len)
2916 {
2917 	enum ib_cm_state state = cm_id_priv->id.state;
2918 	struct ib_mad_send_buf *msg;
2919 	int ret;
2920 
2921 	lockdep_assert_held(&cm_id_priv->lock);
2922 
2923 	if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2924 	    (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2925 		return -EINVAL;
2926 
2927 	trace_icm_send_rej(&cm_id_priv->id, reason);
2928 
2929 	switch (state) {
2930 	case IB_CM_REQ_SENT:
2931 	case IB_CM_MRA_REQ_RCVD:
2932 	case IB_CM_REQ_RCVD:
2933 	case IB_CM_MRA_REQ_SENT:
2934 	case IB_CM_REP_RCVD:
2935 	case IB_CM_MRA_REP_SENT:
2936 		cm_reset_to_idle(cm_id_priv);
2937 		ret = cm_alloc_msg(cm_id_priv, &msg);
2938 		if (ret)
2939 			return ret;
2940 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2941 			      ari, ari_length, private_data, private_data_len,
2942 			      state);
2943 		break;
2944 	case IB_CM_REP_SENT:
2945 	case IB_CM_MRA_REP_RCVD:
2946 		cm_enter_timewait(cm_id_priv);
2947 		ret = cm_alloc_msg(cm_id_priv, &msg);
2948 		if (ret)
2949 			return ret;
2950 		cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2951 			      ari, ari_length, private_data, private_data_len,
2952 			      state);
2953 		break;
2954 	default:
2955 		trace_icm_send_unknown_rej_err(&cm_id_priv->id);
2956 		return -EINVAL;
2957 	}
2958 
2959 	ret = ib_post_send_mad(msg, NULL);
2960 	if (ret) {
2961 		cm_free_msg(msg);
2962 		return ret;
2963 	}
2964 
2965 	return 0;
2966 }
2967 
ib_send_cm_rej(struct ib_cm_id * cm_id,enum ib_cm_rej_reason reason,void * ari,u8 ari_length,const void * private_data,u8 private_data_len)2968 int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason,
2969 		   void *ari, u8 ari_length, const void *private_data,
2970 		   u8 private_data_len)
2971 {
2972 	struct cm_id_private *cm_id_priv =
2973 		container_of(cm_id, struct cm_id_private, id);
2974 	unsigned long flags;
2975 	int ret;
2976 
2977 	spin_lock_irqsave(&cm_id_priv->lock, flags);
2978 	ret = cm_send_rej_locked(cm_id_priv, reason, ari, ari_length,
2979 				 private_data, private_data_len);
2980 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2981 	return ret;
2982 }
2983 EXPORT_SYMBOL(ib_send_cm_rej);
2984 
cm_format_rej_event(struct cm_work * work)2985 static void cm_format_rej_event(struct cm_work *work)
2986 {
2987 	struct cm_rej_msg *rej_msg;
2988 	struct ib_cm_rej_event_param *param;
2989 
2990 	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2991 	param = &work->cm_event.param.rej_rcvd;
2992 	param->ari = IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg);
2993 	param->ari_length = IBA_GET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg);
2994 	param->reason = IBA_GET(CM_REJ_REASON, rej_msg);
2995 	work->cm_event.private_data =
2996 		IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA, rej_msg);
2997 }
2998 
cm_acquire_rejected_id(struct cm_rej_msg * rej_msg)2999 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
3000 {
3001 	struct cm_id_private *cm_id_priv;
3002 	__be32 remote_id;
3003 
3004 	remote_id = cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID, rej_msg));
3005 
3006 	if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_TIMEOUT) {
3007 		cm_id_priv = cm_find_remote_id(
3008 			*((__be64 *)IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg)),
3009 			remote_id);
3010 	} else if (IBA_GET(CM_REJ_MESSAGE_REJECTED, rej_msg) ==
3011 		   CM_MSG_RESPONSE_REQ)
3012 		cm_id_priv = cm_acquire_id(
3013 			cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
3014 			0);
3015 	else
3016 		cm_id_priv = cm_acquire_id(
3017 			cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
3018 			remote_id);
3019 
3020 	return cm_id_priv;
3021 }
3022 
cm_rej_handler(struct cm_work * work)3023 static int cm_rej_handler(struct cm_work *work)
3024 {
3025 	struct cm_id_private *cm_id_priv;
3026 	struct cm_rej_msg *rej_msg;
3027 
3028 	rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
3029 	cm_id_priv = cm_acquire_rejected_id(rej_msg);
3030 	if (!cm_id_priv)
3031 		return -EINVAL;
3032 
3033 	cm_format_rej_event(work);
3034 
3035 	spin_lock_irq(&cm_id_priv->lock);
3036 	switch (cm_id_priv->id.state) {
3037 	case IB_CM_REQ_SENT:
3038 	case IB_CM_MRA_REQ_RCVD:
3039 	case IB_CM_REP_SENT:
3040 	case IB_CM_MRA_REP_RCVD:
3041 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3042 		fallthrough;
3043 	case IB_CM_REQ_RCVD:
3044 	case IB_CM_MRA_REQ_SENT:
3045 		if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN)
3046 			cm_enter_timewait(cm_id_priv);
3047 		else
3048 			cm_reset_to_idle(cm_id_priv);
3049 		break;
3050 	case IB_CM_DREQ_SENT:
3051 		ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3052 		fallthrough;
3053 	case IB_CM_REP_RCVD:
3054 	case IB_CM_MRA_REP_SENT:
3055 		cm_enter_timewait(cm_id_priv);
3056 		break;
3057 	case IB_CM_ESTABLISHED:
3058 		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
3059 		    cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
3060 			if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
3061 				ib_cancel_mad(cm_id_priv->av.port->mad_agent,
3062 					      cm_id_priv->msg);
3063 			cm_enter_timewait(cm_id_priv);
3064 			break;
3065 		}
3066 		fallthrough;
3067 	default:
3068 		trace_icm_rej_unknown_err(&cm_id_priv->id);
3069 		spin_unlock_irq(&cm_id_priv->lock);
3070 		goto out;
3071 	}
3072 
3073 	cm_queue_work_unlock(cm_id_priv, work);
3074 	return 0;
3075 out:
3076 	cm_deref_id(cm_id_priv);
3077 	return -EINVAL;
3078 }
3079 
ib_send_cm_mra(struct ib_cm_id * cm_id,u8 service_timeout,const void * private_data,u8 private_data_len)3080 int ib_send_cm_mra(struct ib_cm_id *cm_id,
3081 		   u8 service_timeout,
3082 		   const void *private_data,
3083 		   u8 private_data_len)
3084 {
3085 	struct cm_id_private *cm_id_priv;
3086 	struct ib_mad_send_buf *msg;
3087 	enum ib_cm_state cm_state;
3088 	enum ib_cm_lap_state lap_state;
3089 	enum cm_msg_response msg_response;
3090 	void *data;
3091 	unsigned long flags;
3092 	int ret;
3093 
3094 	if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
3095 		return -EINVAL;
3096 
3097 	data = cm_copy_private_data(private_data, private_data_len);
3098 	if (IS_ERR(data))
3099 		return PTR_ERR(data);
3100 
3101 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3102 
3103 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3104 	switch(cm_id_priv->id.state) {
3105 	case IB_CM_REQ_RCVD:
3106 		cm_state = IB_CM_MRA_REQ_SENT;
3107 		lap_state = cm_id->lap_state;
3108 		msg_response = CM_MSG_RESPONSE_REQ;
3109 		break;
3110 	case IB_CM_REP_RCVD:
3111 		cm_state = IB_CM_MRA_REP_SENT;
3112 		lap_state = cm_id->lap_state;
3113 		msg_response = CM_MSG_RESPONSE_REP;
3114 		break;
3115 	case IB_CM_ESTABLISHED:
3116 		if (cm_id->lap_state == IB_CM_LAP_RCVD) {
3117 			cm_state = cm_id->state;
3118 			lap_state = IB_CM_MRA_LAP_SENT;
3119 			msg_response = CM_MSG_RESPONSE_OTHER;
3120 			break;
3121 		}
3122 		fallthrough;
3123 	default:
3124 		trace_icm_send_mra_unknown_err(&cm_id_priv->id);
3125 		ret = -EINVAL;
3126 		goto error1;
3127 	}
3128 
3129 	if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
3130 		ret = cm_alloc_msg(cm_id_priv, &msg);
3131 		if (ret)
3132 			goto error1;
3133 
3134 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3135 			      msg_response, service_timeout,
3136 			      private_data, private_data_len);
3137 		trace_icm_send_mra(cm_id);
3138 		ret = ib_post_send_mad(msg, NULL);
3139 		if (ret)
3140 			goto error2;
3141 	}
3142 
3143 	cm_id->state = cm_state;
3144 	cm_id->lap_state = lap_state;
3145 	cm_id_priv->service_timeout = service_timeout;
3146 	cm_set_private_data(cm_id_priv, data, private_data_len);
3147 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3148 	return 0;
3149 
3150 error1:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3151 	kfree(data);
3152 	return ret;
3153 
3154 error2:	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3155 	kfree(data);
3156 	cm_free_msg(msg);
3157 	return ret;
3158 }
3159 EXPORT_SYMBOL(ib_send_cm_mra);
3160 
cm_acquire_mraed_id(struct cm_mra_msg * mra_msg)3161 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
3162 {
3163 	switch (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg)) {
3164 	case CM_MSG_RESPONSE_REQ:
3165 		return cm_acquire_id(
3166 			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3167 			0);
3168 	case CM_MSG_RESPONSE_REP:
3169 	case CM_MSG_RESPONSE_OTHER:
3170 		return cm_acquire_id(
3171 			cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3172 			cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID, mra_msg)));
3173 	default:
3174 		return NULL;
3175 	}
3176 }
3177 
cm_mra_handler(struct cm_work * work)3178 static int cm_mra_handler(struct cm_work *work)
3179 {
3180 	struct cm_id_private *cm_id_priv;
3181 	struct cm_mra_msg *mra_msg;
3182 	int timeout;
3183 
3184 	mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
3185 	cm_id_priv = cm_acquire_mraed_id(mra_msg);
3186 	if (!cm_id_priv)
3187 		return -EINVAL;
3188 
3189 	work->cm_event.private_data =
3190 		IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA, mra_msg);
3191 	work->cm_event.param.mra_rcvd.service_timeout =
3192 		IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg);
3193 	timeout = cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg)) +
3194 		  cm_convert_to_ms(cm_id_priv->av.timeout);
3195 
3196 	spin_lock_irq(&cm_id_priv->lock);
3197 	switch (cm_id_priv->id.state) {
3198 	case IB_CM_REQ_SENT:
3199 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3200 			    CM_MSG_RESPONSE_REQ ||
3201 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3202 				  cm_id_priv->msg, timeout))
3203 			goto out;
3204 		cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
3205 		break;
3206 	case IB_CM_REP_SENT:
3207 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3208 			    CM_MSG_RESPONSE_REP ||
3209 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3210 				  cm_id_priv->msg, timeout))
3211 			goto out;
3212 		cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
3213 		break;
3214 	case IB_CM_ESTABLISHED:
3215 		if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3216 			    CM_MSG_RESPONSE_OTHER ||
3217 		    cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
3218 		    ib_modify_mad(cm_id_priv->av.port->mad_agent,
3219 				  cm_id_priv->msg, timeout)) {
3220 			if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
3221 				atomic_long_inc(&work->port->
3222 						counter_group[CM_RECV_DUPLICATES].
3223 						counter[CM_MRA_COUNTER]);
3224 			goto out;
3225 		}
3226 		cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
3227 		break;
3228 	case IB_CM_MRA_REQ_RCVD:
3229 	case IB_CM_MRA_REP_RCVD:
3230 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3231 				counter[CM_MRA_COUNTER]);
3232 		fallthrough;
3233 	default:
3234 		trace_icm_mra_unknown_err(&cm_id_priv->id);
3235 		goto out;
3236 	}
3237 
3238 	cm_id_priv->msg->context[1] = (void *) (unsigned long)
3239 				      cm_id_priv->id.state;
3240 	cm_queue_work_unlock(cm_id_priv, work);
3241 	return 0;
3242 out:
3243 	spin_unlock_irq(&cm_id_priv->lock);
3244 	cm_deref_id(cm_id_priv);
3245 	return -EINVAL;
3246 }
3247 
cm_format_path_lid_from_lap(struct cm_lap_msg * lap_msg,struct sa_path_rec * path)3248 static void cm_format_path_lid_from_lap(struct cm_lap_msg *lap_msg,
3249 					struct sa_path_rec *path)
3250 {
3251 	u32 lid;
3252 
3253 	if (path->rec_type != SA_PATH_REC_TYPE_OPA) {
3254 		sa_path_set_dlid(path, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID,
3255 					       lap_msg));
3256 		sa_path_set_slid(path, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID,
3257 					       lap_msg));
3258 	} else {
3259 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3260 			CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg));
3261 		sa_path_set_dlid(path, lid);
3262 
3263 		lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3264 			CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg));
3265 		sa_path_set_slid(path, lid);
3266 	}
3267 }
3268 
cm_format_path_from_lap(struct cm_id_private * cm_id_priv,struct sa_path_rec * path,struct cm_lap_msg * lap_msg)3269 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
3270 				    struct sa_path_rec *path,
3271 				    struct cm_lap_msg *lap_msg)
3272 {
3273 	path->dgid = *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg);
3274 	path->sgid =
3275 		*IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg);
3276 	path->flow_label =
3277 		cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL, lap_msg));
3278 	path->hop_limit = IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT, lap_msg);
3279 	path->traffic_class = IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS, lap_msg);
3280 	path->reversible = 1;
3281 	path->pkey = cm_id_priv->pkey;
3282 	path->sl = IBA_GET(CM_LAP_ALTERNATE_SL, lap_msg);
3283 	path->mtu_selector = IB_SA_EQ;
3284 	path->mtu = cm_id_priv->path_mtu;
3285 	path->rate_selector = IB_SA_EQ;
3286 	path->rate = IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE, lap_msg);
3287 	path->packet_life_time_selector = IB_SA_EQ;
3288 	path->packet_life_time =
3289 		IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT, lap_msg);
3290 	path->packet_life_time -= (path->packet_life_time > 0);
3291 	cm_format_path_lid_from_lap(lap_msg, path);
3292 }
3293 
cm_lap_handler(struct cm_work * work)3294 static int cm_lap_handler(struct cm_work *work)
3295 {
3296 	struct cm_id_private *cm_id_priv;
3297 	struct cm_lap_msg *lap_msg;
3298 	struct ib_cm_lap_event_param *param;
3299 	struct ib_mad_send_buf *msg = NULL;
3300 	int ret;
3301 
3302 	/* Currently Alternate path messages are not supported for
3303 	 * RoCE link layer.
3304 	 */
3305 	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3306 			       work->port->port_num))
3307 		return -EINVAL;
3308 
3309 	/* todo: verify LAP request and send reject APR if invalid. */
3310 	lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
3311 	cm_id_priv = cm_acquire_id(
3312 		cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID, lap_msg)),
3313 		cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID, lap_msg)));
3314 	if (!cm_id_priv)
3315 		return -EINVAL;
3316 
3317 	param = &work->cm_event.param.lap_rcvd;
3318 	memset(&work->path[0], 0, sizeof(work->path[1]));
3319 	cm_path_set_rec_type(work->port->cm_dev->ib_device,
3320 			     work->port->port_num, &work->path[0],
3321 			     IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID,
3322 					     lap_msg));
3323 	param->alternate_path = &work->path[0];
3324 	cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
3325 	work->cm_event.private_data =
3326 		IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA, lap_msg);
3327 
3328 	spin_lock_irq(&cm_id_priv->lock);
3329 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
3330 		goto unlock;
3331 
3332 	switch (cm_id_priv->id.lap_state) {
3333 	case IB_CM_LAP_UNINIT:
3334 	case IB_CM_LAP_IDLE:
3335 		break;
3336 	case IB_CM_MRA_LAP_SENT:
3337 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3338 				counter[CM_LAP_COUNTER]);
3339 		msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc);
3340 		if (IS_ERR(msg))
3341 			goto unlock;
3342 
3343 		cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3344 			      CM_MSG_RESPONSE_OTHER,
3345 			      cm_id_priv->service_timeout,
3346 			      cm_id_priv->private_data,
3347 			      cm_id_priv->private_data_len);
3348 		spin_unlock_irq(&cm_id_priv->lock);
3349 
3350 		if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
3351 		    ib_post_send_mad(msg, NULL))
3352 			cm_free_msg(msg);
3353 		goto deref;
3354 	case IB_CM_LAP_RCVD:
3355 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3356 				counter[CM_LAP_COUNTER]);
3357 		goto unlock;
3358 	default:
3359 		goto unlock;
3360 	}
3361 
3362 	ret = cm_init_av_for_lap(work->port, work->mad_recv_wc->wc,
3363 				 work->mad_recv_wc->recv_buf.grh,
3364 				 &cm_id_priv->av);
3365 	if (ret)
3366 		goto unlock;
3367 
3368 	ret = cm_init_av_by_path(param->alternate_path, NULL,
3369 				 &cm_id_priv->alt_av, cm_id_priv);
3370 	if (ret)
3371 		goto unlock;
3372 
3373 	cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
3374 	cm_id_priv->tid = lap_msg->hdr.tid;
3375 	cm_queue_work_unlock(cm_id_priv, work);
3376 	return 0;
3377 
3378 unlock:	spin_unlock_irq(&cm_id_priv->lock);
3379 deref:	cm_deref_id(cm_id_priv);
3380 	return -EINVAL;
3381 }
3382 
cm_apr_handler(struct cm_work * work)3383 static int cm_apr_handler(struct cm_work *work)
3384 {
3385 	struct cm_id_private *cm_id_priv;
3386 	struct cm_apr_msg *apr_msg;
3387 
3388 	/* Currently Alternate path messages are not supported for
3389 	 * RoCE link layer.
3390 	 */
3391 	if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3392 			       work->port->port_num))
3393 		return -EINVAL;
3394 
3395 	apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3396 	cm_id_priv = cm_acquire_id(
3397 		cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID, apr_msg)),
3398 		cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID, apr_msg)));
3399 	if (!cm_id_priv)
3400 		return -EINVAL; /* Unmatched reply. */
3401 
3402 	work->cm_event.param.apr_rcvd.ap_status =
3403 		IBA_GET(CM_APR_AR_STATUS, apr_msg);
3404 	work->cm_event.param.apr_rcvd.apr_info =
3405 		IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION, apr_msg);
3406 	work->cm_event.param.apr_rcvd.info_len =
3407 		IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH, apr_msg);
3408 	work->cm_event.private_data =
3409 		IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA, apr_msg);
3410 
3411 	spin_lock_irq(&cm_id_priv->lock);
3412 	if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
3413 	    (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
3414 	     cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
3415 		spin_unlock_irq(&cm_id_priv->lock);
3416 		goto out;
3417 	}
3418 	cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
3419 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3420 	cm_id_priv->msg = NULL;
3421 	cm_queue_work_unlock(cm_id_priv, work);
3422 	return 0;
3423 out:
3424 	cm_deref_id(cm_id_priv);
3425 	return -EINVAL;
3426 }
3427 
cm_timewait_handler(struct cm_work * work)3428 static int cm_timewait_handler(struct cm_work *work)
3429 {
3430 	struct cm_timewait_info *timewait_info;
3431 	struct cm_id_private *cm_id_priv;
3432 
3433 	timewait_info = container_of(work, struct cm_timewait_info, work);
3434 	spin_lock_irq(&cm.lock);
3435 	list_del(&timewait_info->list);
3436 	spin_unlock_irq(&cm.lock);
3437 
3438 	cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3439 				   timewait_info->work.remote_id);
3440 	if (!cm_id_priv)
3441 		return -EINVAL;
3442 
3443 	spin_lock_irq(&cm_id_priv->lock);
3444 	if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3445 	    cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3446 		spin_unlock_irq(&cm_id_priv->lock);
3447 		goto out;
3448 	}
3449 	cm_id_priv->id.state = IB_CM_IDLE;
3450 	cm_queue_work_unlock(cm_id_priv, work);
3451 	return 0;
3452 out:
3453 	cm_deref_id(cm_id_priv);
3454 	return -EINVAL;
3455 }
3456 
cm_format_sidr_req(struct cm_sidr_req_msg * sidr_req_msg,struct cm_id_private * cm_id_priv,struct ib_cm_sidr_req_param * param)3457 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3458 			       struct cm_id_private *cm_id_priv,
3459 			       struct ib_cm_sidr_req_param *param)
3460 {
3461 	cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3462 			  cm_form_tid(cm_id_priv));
3463 	IBA_SET(CM_SIDR_REQ_REQUESTID, sidr_req_msg,
3464 		be32_to_cpu(cm_id_priv->id.local_id));
3465 	IBA_SET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg,
3466 		be16_to_cpu(param->path->pkey));
3467 	IBA_SET(CM_SIDR_REQ_SERVICEID, sidr_req_msg,
3468 		be64_to_cpu(param->service_id));
3469 
3470 	if (param->private_data && param->private_data_len)
3471 		IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg,
3472 			    param->private_data, param->private_data_len);
3473 }
3474 
ib_send_cm_sidr_req(struct ib_cm_id * cm_id,struct ib_cm_sidr_req_param * param)3475 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3476 			struct ib_cm_sidr_req_param *param)
3477 {
3478 	struct cm_id_private *cm_id_priv;
3479 	struct ib_mad_send_buf *msg;
3480 	unsigned long flags;
3481 	int ret;
3482 
3483 	if (!param->path || (param->private_data &&
3484 	     param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3485 		return -EINVAL;
3486 
3487 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3488 	ret = cm_init_av_by_path(param->path, param->sgid_attr,
3489 				 &cm_id_priv->av,
3490 				 cm_id_priv);
3491 	if (ret)
3492 		goto out;
3493 
3494 	cm_id->service_id = param->service_id;
3495 	cm_id->service_mask = ~cpu_to_be64(0);
3496 	cm_id_priv->timeout_ms = param->timeout_ms;
3497 	cm_id_priv->max_cm_retries = param->max_cm_retries;
3498 	ret = cm_alloc_msg(cm_id_priv, &msg);
3499 	if (ret)
3500 		goto out;
3501 
3502 	cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3503 			   param);
3504 	msg->timeout_ms = cm_id_priv->timeout_ms;
3505 	msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3506 
3507 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3508 	if (cm_id->state == IB_CM_IDLE) {
3509 		trace_icm_send_sidr_req(&cm_id_priv->id);
3510 		ret = ib_post_send_mad(msg, NULL);
3511 	} else {
3512 		ret = -EINVAL;
3513 	}
3514 
3515 	if (ret) {
3516 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3517 		cm_free_msg(msg);
3518 		goto out;
3519 	}
3520 	cm_id->state = IB_CM_SIDR_REQ_SENT;
3521 	cm_id_priv->msg = msg;
3522 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3523 out:
3524 	return ret;
3525 }
3526 EXPORT_SYMBOL(ib_send_cm_sidr_req);
3527 
cm_format_sidr_req_event(struct cm_work * work,const struct cm_id_private * rx_cm_id,struct ib_cm_id * listen_id)3528 static void cm_format_sidr_req_event(struct cm_work *work,
3529 				     const struct cm_id_private *rx_cm_id,
3530 				     struct ib_cm_id *listen_id)
3531 {
3532 	struct cm_sidr_req_msg *sidr_req_msg;
3533 	struct ib_cm_sidr_req_event_param *param;
3534 
3535 	sidr_req_msg = (struct cm_sidr_req_msg *)
3536 				work->mad_recv_wc->recv_buf.mad;
3537 	param = &work->cm_event.param.sidr_req_rcvd;
3538 	param->pkey = IBA_GET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg);
3539 	param->listen_id = listen_id;
3540 	param->service_id =
3541 		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3542 	param->bth_pkey = cm_get_bth_pkey(work);
3543 	param->port = work->port->port_num;
3544 	param->sgid_attr = rx_cm_id->av.ah_attr.grh.sgid_attr;
3545 	work->cm_event.private_data =
3546 		IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg);
3547 }
3548 
cm_sidr_req_handler(struct cm_work * work)3549 static int cm_sidr_req_handler(struct cm_work *work)
3550 {
3551 	struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
3552 	struct cm_sidr_req_msg *sidr_req_msg;
3553 	struct ib_wc *wc;
3554 	int ret;
3555 
3556 	cm_id_priv =
3557 		cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
3558 	if (IS_ERR(cm_id_priv))
3559 		return PTR_ERR(cm_id_priv);
3560 
3561 	/* Record SGID/SLID and request ID for lookup. */
3562 	sidr_req_msg = (struct cm_sidr_req_msg *)
3563 				work->mad_recv_wc->recv_buf.mad;
3564 
3565 	cm_id_priv->id.remote_id =
3566 		cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID, sidr_req_msg));
3567 	cm_id_priv->id.service_id =
3568 		cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3569 	cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3570 	cm_id_priv->tid = sidr_req_msg->hdr.tid;
3571 
3572 	wc = work->mad_recv_wc->wc;
3573 	cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3574 	cm_id_priv->av.dgid.global.interface_id = 0;
3575 	ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3576 				      work->mad_recv_wc->recv_buf.grh,
3577 				      &cm_id_priv->av);
3578 	if (ret)
3579 		goto out;
3580 
3581 	spin_lock_irq(&cm.lock);
3582 	listen_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3583 	if (listen_cm_id_priv) {
3584 		spin_unlock_irq(&cm.lock);
3585 		atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3586 				counter[CM_SIDR_REQ_COUNTER]);
3587 		goto out; /* Duplicate message. */
3588 	}
3589 	cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3590 	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
3591 					   cm_id_priv->id.service_id);
3592 	if (!listen_cm_id_priv) {
3593 		spin_unlock_irq(&cm.lock);
3594 		ib_send_cm_sidr_rep(&cm_id_priv->id,
3595 				    &(struct ib_cm_sidr_rep_param){
3596 					    .status = IB_SIDR_UNSUPPORTED });
3597 		goto out; /* No match. */
3598 	}
3599 	spin_unlock_irq(&cm.lock);
3600 
3601 	cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
3602 	cm_id_priv->id.context = listen_cm_id_priv->id.context;
3603 
3604 	/*
3605 	 * A SIDR ID does not need to be in the xarray since it does not receive
3606 	 * mads, is not placed in the remote_id or remote_qpn rbtree, and does
3607 	 * not enter timewait.
3608 	 */
3609 
3610 	cm_format_sidr_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
3611 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
3612 	cm_free_work(work);
3613 	/*
3614 	 * A pointer to the listen_cm_id is held in the event, so this deref
3615 	 * must be after the event is delivered above.
3616 	 */
3617 	cm_deref_id(listen_cm_id_priv);
3618 	if (ret)
3619 		cm_destroy_id(&cm_id_priv->id, ret);
3620 	return 0;
3621 out:
3622 	ib_destroy_cm_id(&cm_id_priv->id);
3623 	return -EINVAL;
3624 }
3625 
cm_format_sidr_rep(struct cm_sidr_rep_msg * sidr_rep_msg,struct cm_id_private * cm_id_priv,struct ib_cm_sidr_rep_param * param)3626 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3627 			       struct cm_id_private *cm_id_priv,
3628 			       struct ib_cm_sidr_rep_param *param)
3629 {
3630 	cm_format_mad_ece_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3631 			      cm_id_priv->tid, param->ece.attr_mod);
3632 	IBA_SET(CM_SIDR_REP_REQUESTID, sidr_rep_msg,
3633 		be32_to_cpu(cm_id_priv->id.remote_id));
3634 	IBA_SET(CM_SIDR_REP_STATUS, sidr_rep_msg, param->status);
3635 	IBA_SET(CM_SIDR_REP_QPN, sidr_rep_msg, param->qp_num);
3636 	IBA_SET(CM_SIDR_REP_SERVICEID, sidr_rep_msg,
3637 		be64_to_cpu(cm_id_priv->id.service_id));
3638 	IBA_SET(CM_SIDR_REP_Q_KEY, sidr_rep_msg, param->qkey);
3639 	IBA_SET(CM_SIDR_REP_VENDOR_ID_L, sidr_rep_msg,
3640 		param->ece.vendor_id & 0xFF);
3641 	IBA_SET(CM_SIDR_REP_VENDOR_ID_H, sidr_rep_msg,
3642 		(param->ece.vendor_id >> 8) & 0xFF);
3643 
3644 	if (param->info && param->info_length)
3645 		IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION, sidr_rep_msg,
3646 			    param->info, param->info_length);
3647 
3648 	if (param->private_data && param->private_data_len)
3649 		IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg,
3650 			    param->private_data, param->private_data_len);
3651 }
3652 
cm_send_sidr_rep_locked(struct cm_id_private * cm_id_priv,struct ib_cm_sidr_rep_param * param)3653 static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
3654 				   struct ib_cm_sidr_rep_param *param)
3655 {
3656 	struct ib_mad_send_buf *msg;
3657 	unsigned long flags;
3658 	int ret;
3659 
3660 	lockdep_assert_held(&cm_id_priv->lock);
3661 
3662 	if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3663 	    (param->private_data &&
3664 	     param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3665 		return -EINVAL;
3666 
3667 	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_RCVD)
3668 		return -EINVAL;
3669 
3670 	ret = cm_alloc_msg(cm_id_priv, &msg);
3671 	if (ret)
3672 		return ret;
3673 
3674 	cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3675 			   param);
3676 	trace_icm_send_sidr_rep(&cm_id_priv->id);
3677 	ret = ib_post_send_mad(msg, NULL);
3678 	if (ret) {
3679 		cm_free_msg(msg);
3680 		return ret;
3681 	}
3682 	cm_id_priv->id.state = IB_CM_IDLE;
3683 	spin_lock_irqsave(&cm.lock, flags);
3684 	if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3685 		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3686 		RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3687 	}
3688 	spin_unlock_irqrestore(&cm.lock, flags);
3689 	return 0;
3690 }
3691 
ib_send_cm_sidr_rep(struct ib_cm_id * cm_id,struct ib_cm_sidr_rep_param * param)3692 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3693 			struct ib_cm_sidr_rep_param *param)
3694 {
3695 	struct cm_id_private *cm_id_priv =
3696 		container_of(cm_id, struct cm_id_private, id);
3697 	unsigned long flags;
3698 	int ret;
3699 
3700 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3701 	ret = cm_send_sidr_rep_locked(cm_id_priv, param);
3702 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3703 	return ret;
3704 }
3705 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3706 
cm_format_sidr_rep_event(struct cm_work * work,const struct cm_id_private * cm_id_priv)3707 static void cm_format_sidr_rep_event(struct cm_work *work,
3708 				     const struct cm_id_private *cm_id_priv)
3709 {
3710 	struct cm_sidr_rep_msg *sidr_rep_msg;
3711 	struct ib_cm_sidr_rep_event_param *param;
3712 
3713 	sidr_rep_msg = (struct cm_sidr_rep_msg *)
3714 				work->mad_recv_wc->recv_buf.mad;
3715 	param = &work->cm_event.param.sidr_rep_rcvd;
3716 	param->status = IBA_GET(CM_SIDR_REP_STATUS, sidr_rep_msg);
3717 	param->qkey = IBA_GET(CM_SIDR_REP_Q_KEY, sidr_rep_msg);
3718 	param->qpn = IBA_GET(CM_SIDR_REP_QPN, sidr_rep_msg);
3719 	param->info = IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION,
3720 				      sidr_rep_msg);
3721 	param->info_len = IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH,
3722 				  sidr_rep_msg);
3723 	param->sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
3724 	work->cm_event.private_data =
3725 		IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg);
3726 }
3727 
cm_sidr_rep_handler(struct cm_work * work)3728 static int cm_sidr_rep_handler(struct cm_work *work)
3729 {
3730 	struct cm_sidr_rep_msg *sidr_rep_msg;
3731 	struct cm_id_private *cm_id_priv;
3732 
3733 	sidr_rep_msg = (struct cm_sidr_rep_msg *)
3734 				work->mad_recv_wc->recv_buf.mad;
3735 	cm_id_priv = cm_acquire_id(
3736 		cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID, sidr_rep_msg)), 0);
3737 	if (!cm_id_priv)
3738 		return -EINVAL; /* Unmatched reply. */
3739 
3740 	spin_lock_irq(&cm_id_priv->lock);
3741 	if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3742 		spin_unlock_irq(&cm_id_priv->lock);
3743 		goto out;
3744 	}
3745 	cm_id_priv->id.state = IB_CM_IDLE;
3746 	ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3747 	spin_unlock_irq(&cm_id_priv->lock);
3748 
3749 	cm_format_sidr_rep_event(work, cm_id_priv);
3750 	cm_process_work(cm_id_priv, work);
3751 	return 0;
3752 out:
3753 	cm_deref_id(cm_id_priv);
3754 	return -EINVAL;
3755 }
3756 
cm_process_send_error(struct ib_mad_send_buf * msg,enum ib_wc_status wc_status)3757 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3758 				  enum ib_wc_status wc_status)
3759 {
3760 	struct cm_id_private *cm_id_priv;
3761 	struct ib_cm_event cm_event;
3762 	enum ib_cm_state state;
3763 	int ret;
3764 
3765 	memset(&cm_event, 0, sizeof cm_event);
3766 	cm_id_priv = msg->context[0];
3767 
3768 	/* Discard old sends or ones without a response. */
3769 	spin_lock_irq(&cm_id_priv->lock);
3770 	state = (enum ib_cm_state) (unsigned long) msg->context[1];
3771 	if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3772 		goto discard;
3773 
3774 	trace_icm_mad_send_err(state, wc_status);
3775 	switch (state) {
3776 	case IB_CM_REQ_SENT:
3777 	case IB_CM_MRA_REQ_RCVD:
3778 		cm_reset_to_idle(cm_id_priv);
3779 		cm_event.event = IB_CM_REQ_ERROR;
3780 		break;
3781 	case IB_CM_REP_SENT:
3782 	case IB_CM_MRA_REP_RCVD:
3783 		cm_reset_to_idle(cm_id_priv);
3784 		cm_event.event = IB_CM_REP_ERROR;
3785 		break;
3786 	case IB_CM_DREQ_SENT:
3787 		cm_enter_timewait(cm_id_priv);
3788 		cm_event.event = IB_CM_DREQ_ERROR;
3789 		break;
3790 	case IB_CM_SIDR_REQ_SENT:
3791 		cm_id_priv->id.state = IB_CM_IDLE;
3792 		cm_event.event = IB_CM_SIDR_REQ_ERROR;
3793 		break;
3794 	default:
3795 		goto discard;
3796 	}
3797 	spin_unlock_irq(&cm_id_priv->lock);
3798 	cm_event.param.send_status = wc_status;
3799 
3800 	/* No other events can occur on the cm_id at this point. */
3801 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3802 	cm_free_msg(msg);
3803 	if (ret)
3804 		ib_destroy_cm_id(&cm_id_priv->id);
3805 	return;
3806 discard:
3807 	spin_unlock_irq(&cm_id_priv->lock);
3808 	cm_free_msg(msg);
3809 }
3810 
cm_send_handler(struct ib_mad_agent * mad_agent,struct ib_mad_send_wc * mad_send_wc)3811 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3812 			    struct ib_mad_send_wc *mad_send_wc)
3813 {
3814 	struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3815 	struct cm_port *port;
3816 	u16 attr_index;
3817 
3818 	port = mad_agent->context;
3819 	attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3820 				  msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3821 
3822 	/*
3823 	 * If the send was in response to a received message (context[0] is not
3824 	 * set to a cm_id), and is not a REJ, then it is a send that was
3825 	 * manually retried.
3826 	 */
3827 	if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3828 		msg->retries = 1;
3829 
3830 	atomic_long_add(1 + msg->retries,
3831 			&port->counter_group[CM_XMIT].counter[attr_index]);
3832 	if (msg->retries)
3833 		atomic_long_add(msg->retries,
3834 				&port->counter_group[CM_XMIT_RETRIES].
3835 				counter[attr_index]);
3836 
3837 	switch (mad_send_wc->status) {
3838 	case IB_WC_SUCCESS:
3839 	case IB_WC_WR_FLUSH_ERR:
3840 		cm_free_msg(msg);
3841 		break;
3842 	default:
3843 		if (msg->context[0] && msg->context[1])
3844 			cm_process_send_error(msg, mad_send_wc->status);
3845 		else
3846 			cm_free_msg(msg);
3847 		break;
3848 	}
3849 }
3850 
cm_work_handler(struct work_struct * _work)3851 static void cm_work_handler(struct work_struct *_work)
3852 {
3853 	struct cm_work *work = container_of(_work, struct cm_work, work.work);
3854 	int ret;
3855 
3856 	switch (work->cm_event.event) {
3857 	case IB_CM_REQ_RECEIVED:
3858 		ret = cm_req_handler(work);
3859 		break;
3860 	case IB_CM_MRA_RECEIVED:
3861 		ret = cm_mra_handler(work);
3862 		break;
3863 	case IB_CM_REJ_RECEIVED:
3864 		ret = cm_rej_handler(work);
3865 		break;
3866 	case IB_CM_REP_RECEIVED:
3867 		ret = cm_rep_handler(work);
3868 		break;
3869 	case IB_CM_RTU_RECEIVED:
3870 		ret = cm_rtu_handler(work);
3871 		break;
3872 	case IB_CM_USER_ESTABLISHED:
3873 		ret = cm_establish_handler(work);
3874 		break;
3875 	case IB_CM_DREQ_RECEIVED:
3876 		ret = cm_dreq_handler(work);
3877 		break;
3878 	case IB_CM_DREP_RECEIVED:
3879 		ret = cm_drep_handler(work);
3880 		break;
3881 	case IB_CM_SIDR_REQ_RECEIVED:
3882 		ret = cm_sidr_req_handler(work);
3883 		break;
3884 	case IB_CM_SIDR_REP_RECEIVED:
3885 		ret = cm_sidr_rep_handler(work);
3886 		break;
3887 	case IB_CM_LAP_RECEIVED:
3888 		ret = cm_lap_handler(work);
3889 		break;
3890 	case IB_CM_APR_RECEIVED:
3891 		ret = cm_apr_handler(work);
3892 		break;
3893 	case IB_CM_TIMEWAIT_EXIT:
3894 		ret = cm_timewait_handler(work);
3895 		break;
3896 	default:
3897 		trace_icm_handler_err(work->cm_event.event);
3898 		ret = -EINVAL;
3899 		break;
3900 	}
3901 	if (ret)
3902 		cm_free_work(work);
3903 }
3904 
cm_establish(struct ib_cm_id * cm_id)3905 static int cm_establish(struct ib_cm_id *cm_id)
3906 {
3907 	struct cm_id_private *cm_id_priv;
3908 	struct cm_work *work;
3909 	unsigned long flags;
3910 	int ret = 0;
3911 	struct cm_device *cm_dev;
3912 
3913 	cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3914 	if (!cm_dev)
3915 		return -ENODEV;
3916 
3917 	work = kmalloc(sizeof *work, GFP_ATOMIC);
3918 	if (!work)
3919 		return -ENOMEM;
3920 
3921 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3922 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3923 	switch (cm_id->state)
3924 	{
3925 	case IB_CM_REP_SENT:
3926 	case IB_CM_MRA_REP_RCVD:
3927 		cm_id->state = IB_CM_ESTABLISHED;
3928 		break;
3929 	case IB_CM_ESTABLISHED:
3930 		ret = -EISCONN;
3931 		break;
3932 	default:
3933 		trace_icm_establish_err(cm_id);
3934 		ret = -EINVAL;
3935 		break;
3936 	}
3937 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3938 
3939 	if (ret) {
3940 		kfree(work);
3941 		goto out;
3942 	}
3943 
3944 	/*
3945 	 * The CM worker thread may try to destroy the cm_id before it
3946 	 * can execute this work item.  To prevent potential deadlock,
3947 	 * we need to find the cm_id once we're in the context of the
3948 	 * worker thread, rather than holding a reference on it.
3949 	 */
3950 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
3951 	work->local_id = cm_id->local_id;
3952 	work->remote_id = cm_id->remote_id;
3953 	work->mad_recv_wc = NULL;
3954 	work->cm_event.event = IB_CM_USER_ESTABLISHED;
3955 
3956 	/* Check if the device started its remove_one */
3957 	spin_lock_irqsave(&cm.lock, flags);
3958 	if (!cm_dev->going_down) {
3959 		queue_delayed_work(cm.wq, &work->work, 0);
3960 	} else {
3961 		kfree(work);
3962 		ret = -ENODEV;
3963 	}
3964 	spin_unlock_irqrestore(&cm.lock, flags);
3965 
3966 out:
3967 	return ret;
3968 }
3969 
cm_migrate(struct ib_cm_id * cm_id)3970 static int cm_migrate(struct ib_cm_id *cm_id)
3971 {
3972 	struct cm_id_private *cm_id_priv;
3973 	struct cm_av tmp_av;
3974 	unsigned long flags;
3975 	int tmp_send_port_not_ready;
3976 	int ret = 0;
3977 
3978 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3979 	spin_lock_irqsave(&cm_id_priv->lock, flags);
3980 	if (cm_id->state == IB_CM_ESTABLISHED &&
3981 	    (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3982 	     cm_id->lap_state == IB_CM_LAP_IDLE)) {
3983 		cm_id->lap_state = IB_CM_LAP_IDLE;
3984 		/* Swap address vector */
3985 		tmp_av = cm_id_priv->av;
3986 		cm_id_priv->av = cm_id_priv->alt_av;
3987 		cm_id_priv->alt_av = tmp_av;
3988 		/* Swap port send ready state */
3989 		tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
3990 		cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
3991 		cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
3992 	} else
3993 		ret = -EINVAL;
3994 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3995 
3996 	return ret;
3997 }
3998 
ib_cm_notify(struct ib_cm_id * cm_id,enum ib_event_type event)3999 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
4000 {
4001 	int ret;
4002 
4003 	switch (event) {
4004 	case IB_EVENT_COMM_EST:
4005 		ret = cm_establish(cm_id);
4006 		break;
4007 	case IB_EVENT_PATH_MIG:
4008 		ret = cm_migrate(cm_id);
4009 		break;
4010 	default:
4011 		ret = -EINVAL;
4012 	}
4013 	return ret;
4014 }
4015 EXPORT_SYMBOL(ib_cm_notify);
4016 
cm_recv_handler(struct ib_mad_agent * mad_agent,struct ib_mad_send_buf * send_buf,struct ib_mad_recv_wc * mad_recv_wc)4017 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
4018 			    struct ib_mad_send_buf *send_buf,
4019 			    struct ib_mad_recv_wc *mad_recv_wc)
4020 {
4021 	struct cm_port *port = mad_agent->context;
4022 	struct cm_work *work;
4023 	enum ib_cm_event_type event;
4024 	bool alt_path = false;
4025 	u16 attr_id;
4026 	int paths = 0;
4027 	int going_down = 0;
4028 
4029 	switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
4030 	case CM_REQ_ATTR_ID:
4031 		alt_path = cm_req_has_alt_path((struct cm_req_msg *)
4032 						mad_recv_wc->recv_buf.mad);
4033 		paths = 1 + (alt_path != 0);
4034 		event = IB_CM_REQ_RECEIVED;
4035 		break;
4036 	case CM_MRA_ATTR_ID:
4037 		event = IB_CM_MRA_RECEIVED;
4038 		break;
4039 	case CM_REJ_ATTR_ID:
4040 		event = IB_CM_REJ_RECEIVED;
4041 		break;
4042 	case CM_REP_ATTR_ID:
4043 		event = IB_CM_REP_RECEIVED;
4044 		break;
4045 	case CM_RTU_ATTR_ID:
4046 		event = IB_CM_RTU_RECEIVED;
4047 		break;
4048 	case CM_DREQ_ATTR_ID:
4049 		event = IB_CM_DREQ_RECEIVED;
4050 		break;
4051 	case CM_DREP_ATTR_ID:
4052 		event = IB_CM_DREP_RECEIVED;
4053 		break;
4054 	case CM_SIDR_REQ_ATTR_ID:
4055 		event = IB_CM_SIDR_REQ_RECEIVED;
4056 		break;
4057 	case CM_SIDR_REP_ATTR_ID:
4058 		event = IB_CM_SIDR_REP_RECEIVED;
4059 		break;
4060 	case CM_LAP_ATTR_ID:
4061 		paths = 1;
4062 		event = IB_CM_LAP_RECEIVED;
4063 		break;
4064 	case CM_APR_ATTR_ID:
4065 		event = IB_CM_APR_RECEIVED;
4066 		break;
4067 	default:
4068 		ib_free_recv_mad(mad_recv_wc);
4069 		return;
4070 	}
4071 
4072 	attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
4073 	atomic_long_inc(&port->counter_group[CM_RECV].
4074 			counter[attr_id - CM_ATTR_ID_OFFSET]);
4075 
4076 	work = kmalloc(struct_size(work, path, paths), GFP_KERNEL);
4077 	if (!work) {
4078 		ib_free_recv_mad(mad_recv_wc);
4079 		return;
4080 	}
4081 
4082 	INIT_DELAYED_WORK(&work->work, cm_work_handler);
4083 	work->cm_event.event = event;
4084 	work->mad_recv_wc = mad_recv_wc;
4085 	work->port = port;
4086 
4087 	/* Check if the device started its remove_one */
4088 	spin_lock_irq(&cm.lock);
4089 	if (!port->cm_dev->going_down)
4090 		queue_delayed_work(cm.wq, &work->work, 0);
4091 	else
4092 		going_down = 1;
4093 	spin_unlock_irq(&cm.lock);
4094 
4095 	if (going_down) {
4096 		kfree(work);
4097 		ib_free_recv_mad(mad_recv_wc);
4098 	}
4099 }
4100 
cm_init_qp_init_attr(struct cm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)4101 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
4102 				struct ib_qp_attr *qp_attr,
4103 				int *qp_attr_mask)
4104 {
4105 	unsigned long flags;
4106 	int ret;
4107 
4108 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4109 	switch (cm_id_priv->id.state) {
4110 	case IB_CM_REQ_SENT:
4111 	case IB_CM_MRA_REQ_RCVD:
4112 	case IB_CM_REQ_RCVD:
4113 	case IB_CM_MRA_REQ_SENT:
4114 	case IB_CM_REP_RCVD:
4115 	case IB_CM_MRA_REP_SENT:
4116 	case IB_CM_REP_SENT:
4117 	case IB_CM_MRA_REP_RCVD:
4118 	case IB_CM_ESTABLISHED:
4119 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
4120 				IB_QP_PKEY_INDEX | IB_QP_PORT;
4121 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
4122 		if (cm_id_priv->responder_resources)
4123 			qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
4124 						    IB_ACCESS_REMOTE_ATOMIC;
4125 		qp_attr->pkey_index = cm_id_priv->av.pkey_index;
4126 		qp_attr->port_num = cm_id_priv->av.port->port_num;
4127 		ret = 0;
4128 		break;
4129 	default:
4130 		trace_icm_qp_init_err(&cm_id_priv->id);
4131 		ret = -EINVAL;
4132 		break;
4133 	}
4134 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4135 	return ret;
4136 }
4137 
cm_init_qp_rtr_attr(struct cm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)4138 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
4139 			       struct ib_qp_attr *qp_attr,
4140 			       int *qp_attr_mask)
4141 {
4142 	unsigned long flags;
4143 	int ret;
4144 
4145 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4146 	switch (cm_id_priv->id.state) {
4147 	case IB_CM_REQ_RCVD:
4148 	case IB_CM_MRA_REQ_SENT:
4149 	case IB_CM_REP_RCVD:
4150 	case IB_CM_MRA_REP_SENT:
4151 	case IB_CM_REP_SENT:
4152 	case IB_CM_MRA_REP_RCVD:
4153 	case IB_CM_ESTABLISHED:
4154 		*qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
4155 				IB_QP_DEST_QPN | IB_QP_RQ_PSN;
4156 		qp_attr->ah_attr = cm_id_priv->av.ah_attr;
4157 		qp_attr->path_mtu = cm_id_priv->path_mtu;
4158 		qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
4159 		qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
4160 		if (cm_id_priv->qp_type == IB_QPT_RC ||
4161 		    cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
4162 			*qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
4163 					 IB_QP_MIN_RNR_TIMER;
4164 			qp_attr->max_dest_rd_atomic =
4165 					cm_id_priv->responder_resources;
4166 			qp_attr->min_rnr_timer = 0;
4167 		}
4168 		if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4169 			*qp_attr_mask |= IB_QP_ALT_PATH;
4170 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4171 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4172 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4173 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4174 		}
4175 		ret = 0;
4176 		break;
4177 	default:
4178 		trace_icm_qp_rtr_err(&cm_id_priv->id);
4179 		ret = -EINVAL;
4180 		break;
4181 	}
4182 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4183 	return ret;
4184 }
4185 
cm_init_qp_rts_attr(struct cm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)4186 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
4187 			       struct ib_qp_attr *qp_attr,
4188 			       int *qp_attr_mask)
4189 {
4190 	unsigned long flags;
4191 	int ret;
4192 
4193 	spin_lock_irqsave(&cm_id_priv->lock, flags);
4194 	switch (cm_id_priv->id.state) {
4195 	/* Allow transition to RTS before sending REP */
4196 	case IB_CM_REQ_RCVD:
4197 	case IB_CM_MRA_REQ_SENT:
4198 
4199 	case IB_CM_REP_RCVD:
4200 	case IB_CM_MRA_REP_SENT:
4201 	case IB_CM_REP_SENT:
4202 	case IB_CM_MRA_REP_RCVD:
4203 	case IB_CM_ESTABLISHED:
4204 		if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
4205 			*qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
4206 			qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
4207 			switch (cm_id_priv->qp_type) {
4208 			case IB_QPT_RC:
4209 			case IB_QPT_XRC_INI:
4210 				*qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4211 						 IB_QP_MAX_QP_RD_ATOMIC;
4212 				qp_attr->retry_cnt = cm_id_priv->retry_count;
4213 				qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
4214 				qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
4215 				fallthrough;
4216 			case IB_QPT_XRC_TGT:
4217 				*qp_attr_mask |= IB_QP_TIMEOUT;
4218 				qp_attr->timeout = cm_id_priv->av.timeout;
4219 				break;
4220 			default:
4221 				break;
4222 			}
4223 			if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4224 				*qp_attr_mask |= IB_QP_PATH_MIG_STATE;
4225 				qp_attr->path_mig_state = IB_MIG_REARM;
4226 			}
4227 		} else {
4228 			*qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
4229 			qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4230 			qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4231 			qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4232 			qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4233 			qp_attr->path_mig_state = IB_MIG_REARM;
4234 		}
4235 		ret = 0;
4236 		break;
4237 	default:
4238 		trace_icm_qp_rts_err(&cm_id_priv->id);
4239 		ret = -EINVAL;
4240 		break;
4241 	}
4242 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4243 	return ret;
4244 }
4245 
ib_cm_init_qp_attr(struct ib_cm_id * cm_id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)4246 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
4247 		       struct ib_qp_attr *qp_attr,
4248 		       int *qp_attr_mask)
4249 {
4250 	struct cm_id_private *cm_id_priv;
4251 	int ret;
4252 
4253 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
4254 	switch (qp_attr->qp_state) {
4255 	case IB_QPS_INIT:
4256 		ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
4257 		break;
4258 	case IB_QPS_RTR:
4259 		ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
4260 		break;
4261 	case IB_QPS_RTS:
4262 		ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
4263 		break;
4264 	default:
4265 		ret = -EINVAL;
4266 		break;
4267 	}
4268 	return ret;
4269 }
4270 EXPORT_SYMBOL(ib_cm_init_qp_attr);
4271 
cm_show_counter(struct kobject * obj,struct attribute * attr,char * buf)4272 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
4273 			       char *buf)
4274 {
4275 	struct cm_counter_group *group;
4276 	struct cm_counter_attribute *cm_attr;
4277 
4278 	group = container_of(obj, struct cm_counter_group, obj);
4279 	cm_attr = container_of(attr, struct cm_counter_attribute, attr);
4280 
4281 	return sprintf(buf, "%ld\n",
4282 		       atomic_long_read(&group->counter[cm_attr->index]));
4283 }
4284 
4285 static const struct sysfs_ops cm_counter_ops = {
4286 	.show = cm_show_counter
4287 };
4288 
4289 static struct kobj_type cm_counter_obj_type = {
4290 	.sysfs_ops = &cm_counter_ops,
4291 	.default_attrs = cm_counter_default_attrs
4292 };
4293 
cm_create_port_fs(struct cm_port * port)4294 static int cm_create_port_fs(struct cm_port *port)
4295 {
4296 	int i, ret;
4297 
4298 	for (i = 0; i < CM_COUNTER_GROUPS; i++) {
4299 		ret = ib_port_register_module_stat(port->cm_dev->ib_device,
4300 						   port->port_num,
4301 						   &port->counter_group[i].obj,
4302 						   &cm_counter_obj_type,
4303 						   counter_group_names[i]);
4304 		if (ret)
4305 			goto error;
4306 	}
4307 
4308 	return 0;
4309 
4310 error:
4311 	while (i--)
4312 		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4313 	return ret;
4314 
4315 }
4316 
cm_remove_port_fs(struct cm_port * port)4317 static void cm_remove_port_fs(struct cm_port *port)
4318 {
4319 	int i;
4320 
4321 	for (i = 0; i < CM_COUNTER_GROUPS; i++)
4322 		ib_port_unregister_module_stat(&port->counter_group[i].obj);
4323 
4324 }
4325 
cm_add_one(struct ib_device * ib_device)4326 static int cm_add_one(struct ib_device *ib_device)
4327 {
4328 	struct cm_device *cm_dev;
4329 	struct cm_port *port;
4330 	struct ib_mad_reg_req reg_req = {
4331 		.mgmt_class = IB_MGMT_CLASS_CM,
4332 		.mgmt_class_version = IB_CM_CLASS_VERSION,
4333 	};
4334 	struct ib_port_modify port_modify = {
4335 		.set_port_cap_mask = IB_PORT_CM_SUP
4336 	};
4337 	unsigned long flags;
4338 	int ret;
4339 	int count = 0;
4340 	unsigned int i;
4341 
4342 	cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt),
4343 			 GFP_KERNEL);
4344 	if (!cm_dev)
4345 		return -ENOMEM;
4346 
4347 	cm_dev->ib_device = ib_device;
4348 	cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
4349 	cm_dev->going_down = 0;
4350 
4351 	set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
4352 	rdma_for_each_port (ib_device, i) {
4353 		if (!rdma_cap_ib_cm(ib_device, i))
4354 			continue;
4355 
4356 		port = kzalloc(sizeof *port, GFP_KERNEL);
4357 		if (!port) {
4358 			ret = -ENOMEM;
4359 			goto error1;
4360 		}
4361 
4362 		cm_dev->port[i-1] = port;
4363 		port->cm_dev = cm_dev;
4364 		port->port_num = i;
4365 
4366 		INIT_LIST_HEAD(&port->cm_priv_prim_list);
4367 		INIT_LIST_HEAD(&port->cm_priv_altr_list);
4368 
4369 		ret = cm_create_port_fs(port);
4370 		if (ret)
4371 			goto error1;
4372 
4373 		port->mad_agent = ib_register_mad_agent(ib_device, i,
4374 							IB_QPT_GSI,
4375 							&reg_req,
4376 							0,
4377 							cm_send_handler,
4378 							cm_recv_handler,
4379 							port,
4380 							0);
4381 		if (IS_ERR(port->mad_agent)) {
4382 			ret = PTR_ERR(port->mad_agent);
4383 			goto error2;
4384 		}
4385 
4386 		ret = ib_modify_port(ib_device, i, 0, &port_modify);
4387 		if (ret)
4388 			goto error3;
4389 
4390 		count++;
4391 	}
4392 
4393 	if (!count) {
4394 		ret = -EOPNOTSUPP;
4395 		goto free;
4396 	}
4397 
4398 	ib_set_client_data(ib_device, &cm_client, cm_dev);
4399 
4400 	write_lock_irqsave(&cm.device_lock, flags);
4401 	list_add_tail(&cm_dev->list, &cm.device_list);
4402 	write_unlock_irqrestore(&cm.device_lock, flags);
4403 	return 0;
4404 
4405 error3:
4406 	ib_unregister_mad_agent(port->mad_agent);
4407 error2:
4408 	cm_remove_port_fs(port);
4409 error1:
4410 	port_modify.set_port_cap_mask = 0;
4411 	port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4412 	kfree(port);
4413 	while (--i) {
4414 		if (!rdma_cap_ib_cm(ib_device, i))
4415 			continue;
4416 
4417 		port = cm_dev->port[i-1];
4418 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4419 		ib_unregister_mad_agent(port->mad_agent);
4420 		cm_remove_port_fs(port);
4421 		kfree(port);
4422 	}
4423 free:
4424 	kfree(cm_dev);
4425 	return ret;
4426 }
4427 
cm_remove_one(struct ib_device * ib_device,void * client_data)4428 static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4429 {
4430 	struct cm_device *cm_dev = client_data;
4431 	struct cm_port *port;
4432 	struct cm_id_private *cm_id_priv;
4433 	struct ib_mad_agent *cur_mad_agent;
4434 	struct ib_port_modify port_modify = {
4435 		.clr_port_cap_mask = IB_PORT_CM_SUP
4436 	};
4437 	unsigned long flags;
4438 	unsigned int i;
4439 
4440 	write_lock_irqsave(&cm.device_lock, flags);
4441 	list_del(&cm_dev->list);
4442 	write_unlock_irqrestore(&cm.device_lock, flags);
4443 
4444 	spin_lock_irq(&cm.lock);
4445 	cm_dev->going_down = 1;
4446 	spin_unlock_irq(&cm.lock);
4447 
4448 	rdma_for_each_port (ib_device, i) {
4449 		if (!rdma_cap_ib_cm(ib_device, i))
4450 			continue;
4451 
4452 		port = cm_dev->port[i-1];
4453 		ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4454 		/* Mark all the cm_id's as not valid */
4455 		spin_lock_irq(&cm.lock);
4456 		list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
4457 			cm_id_priv->altr_send_port_not_ready = 1;
4458 		list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
4459 			cm_id_priv->prim_send_port_not_ready = 1;
4460 		spin_unlock_irq(&cm.lock);
4461 		/*
4462 		 * We flush the queue here after the going_down set, this
4463 		 * verify that no new works will be queued in the recv handler,
4464 		 * after that we can call the unregister_mad_agent
4465 		 */
4466 		flush_workqueue(cm.wq);
4467 		spin_lock_irq(&cm.state_lock);
4468 		cur_mad_agent = port->mad_agent;
4469 		port->mad_agent = NULL;
4470 		spin_unlock_irq(&cm.state_lock);
4471 		ib_unregister_mad_agent(cur_mad_agent);
4472 		cm_remove_port_fs(port);
4473 		kfree(port);
4474 	}
4475 
4476 	kfree(cm_dev);
4477 }
4478 
ib_cm_init(void)4479 static int __init ib_cm_init(void)
4480 {
4481 	int ret;
4482 
4483 	INIT_LIST_HEAD(&cm.device_list);
4484 	rwlock_init(&cm.device_lock);
4485 	spin_lock_init(&cm.lock);
4486 	spin_lock_init(&cm.state_lock);
4487 	cm.listen_service_table = RB_ROOT;
4488 	cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4489 	cm.remote_id_table = RB_ROOT;
4490 	cm.remote_qp_table = RB_ROOT;
4491 	cm.remote_sidr_table = RB_ROOT;
4492 	xa_init_flags(&cm.local_id_table, XA_FLAGS_ALLOC);
4493 	get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4494 	INIT_LIST_HEAD(&cm.timewait_list);
4495 
4496 	cm.wq = alloc_workqueue("ib_cm", 0, 1);
4497 	if (!cm.wq) {
4498 		ret = -ENOMEM;
4499 		goto error2;
4500 	}
4501 
4502 	ret = ib_register_client(&cm_client);
4503 	if (ret)
4504 		goto error3;
4505 
4506 	return 0;
4507 error3:
4508 	destroy_workqueue(cm.wq);
4509 error2:
4510 	return ret;
4511 }
4512 
ib_cm_cleanup(void)4513 static void __exit ib_cm_cleanup(void)
4514 {
4515 	struct cm_timewait_info *timewait_info, *tmp;
4516 
4517 	spin_lock_irq(&cm.lock);
4518 	list_for_each_entry(timewait_info, &cm.timewait_list, list)
4519 		cancel_delayed_work(&timewait_info->work.work);
4520 	spin_unlock_irq(&cm.lock);
4521 
4522 	ib_unregister_client(&cm_client);
4523 	destroy_workqueue(cm.wq);
4524 
4525 	list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4526 		list_del(&timewait_info->list);
4527 		kfree(timewait_info);
4528 	}
4529 
4530 	WARN_ON(!xa_empty(&cm.local_id_table));
4531 }
4532 
4533 module_init(ib_cm_init);
4534 module_exit(ib_cm_cleanup);
4535