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