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