1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
4 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
6 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
10 #include <linux/completion.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/idr.h>
16 #include <linux/interrupt.h>
17 #include <linux/random.h>
18 #include <linux/rbtree.h>
19 #include <linux/spinlock.h>
20 #include <linux/slab.h>
21 #include <linux/sysfs.h>
22 #include <linux/workqueue.h>
23 #include <linux/kdev_t.h>
24 #include <linux/etherdevice.h>
26 #include <rdma/ib_cache.h>
27 #include <rdma/ib_cm.h>
29 #include "core_priv.h"
31 MODULE_AUTHOR("Sean Hefty");
32 MODULE_DESCRIPTION("InfiniBand CM");
33 MODULE_LICENSE("Dual BSD/GPL");
35 static const char * const ibcm_rej_reason_strs
[] = {
36 [IB_CM_REJ_NO_QP
] = "no QP",
37 [IB_CM_REJ_NO_EEC
] = "no EEC",
38 [IB_CM_REJ_NO_RESOURCES
] = "no resources",
39 [IB_CM_REJ_TIMEOUT
] = "timeout",
40 [IB_CM_REJ_UNSUPPORTED
] = "unsupported",
41 [IB_CM_REJ_INVALID_COMM_ID
] = "invalid comm ID",
42 [IB_CM_REJ_INVALID_COMM_INSTANCE
] = "invalid comm instance",
43 [IB_CM_REJ_INVALID_SERVICE_ID
] = "invalid service ID",
44 [IB_CM_REJ_INVALID_TRANSPORT_TYPE
] = "invalid transport type",
45 [IB_CM_REJ_STALE_CONN
] = "stale conn",
46 [IB_CM_REJ_RDC_NOT_EXIST
] = "RDC not exist",
47 [IB_CM_REJ_INVALID_GID
] = "invalid GID",
48 [IB_CM_REJ_INVALID_LID
] = "invalid LID",
49 [IB_CM_REJ_INVALID_SL
] = "invalid SL",
50 [IB_CM_REJ_INVALID_TRAFFIC_CLASS
] = "invalid traffic class",
51 [IB_CM_REJ_INVALID_HOP_LIMIT
] = "invalid hop limit",
52 [IB_CM_REJ_INVALID_PACKET_RATE
] = "invalid packet rate",
53 [IB_CM_REJ_INVALID_ALT_GID
] = "invalid alt GID",
54 [IB_CM_REJ_INVALID_ALT_LID
] = "invalid alt LID",
55 [IB_CM_REJ_INVALID_ALT_SL
] = "invalid alt SL",
56 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS
] = "invalid alt traffic class",
57 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT
] = "invalid alt hop limit",
58 [IB_CM_REJ_INVALID_ALT_PACKET_RATE
] = "invalid alt packet rate",
59 [IB_CM_REJ_PORT_CM_REDIRECT
] = "port CM redirect",
60 [IB_CM_REJ_PORT_REDIRECT
] = "port redirect",
61 [IB_CM_REJ_INVALID_MTU
] = "invalid MTU",
62 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES
] = "insufficient resp resources",
63 [IB_CM_REJ_CONSUMER_DEFINED
] = "consumer defined",
64 [IB_CM_REJ_INVALID_RNR_RETRY
] = "invalid RNR retry",
65 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
] = "duplicate local comm ID",
66 [IB_CM_REJ_INVALID_CLASS_VERSION
] = "invalid class version",
67 [IB_CM_REJ_INVALID_FLOW_LABEL
] = "invalid flow label",
68 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL
] = "invalid alt flow label",
71 const char *__attribute_const__
ibcm_reject_msg(int reason
)
73 size_t index
= reason
;
75 if (index
< ARRAY_SIZE(ibcm_rej_reason_strs
) &&
76 ibcm_rej_reason_strs
[index
])
77 return ibcm_rej_reason_strs
[index
];
79 return "unrecognized reason";
81 EXPORT_SYMBOL(ibcm_reject_msg
);
84 static void cm_add_one(struct ib_device
*device
);
85 static void cm_remove_one(struct ib_device
*device
, void *client_data
);
86 static int cm_send_sidr_rep_locked(struct cm_id_private
*cm_id_priv
,
87 struct ib_cm_sidr_rep_param
*param
);
88 static int cm_send_dreq_locked(struct cm_id_private
*cm_id_priv
,
89 const void *private_data
, u8 private_data_len
);
90 static int cm_send_drep_locked(struct cm_id_private
*cm_id_priv
,
91 void *private_data
, u8 private_data_len
);
92 static int cm_send_rej_locked(struct cm_id_private
*cm_id_priv
,
93 enum ib_cm_rej_reason reason
, void *ari
,
94 u8 ari_length
, const void *private_data
,
97 static struct ib_client cm_client
= {
100 .remove
= cm_remove_one
103 static struct ib_cm
{
105 struct list_head device_list
;
106 rwlock_t device_lock
;
107 struct rb_root listen_service_table
;
108 u64 listen_service_id
;
109 /* struct rb_root peer_service_table; todo: fix peer to peer */
110 struct rb_root remote_qp_table
;
111 struct rb_root remote_id_table
;
112 struct rb_root remote_sidr_table
;
113 struct xarray local_id_table
;
115 __be32 random_id_operand
;
116 struct list_head timewait_list
;
117 struct workqueue_struct
*wq
;
118 /* Sync on cm change port state */
119 spinlock_t state_lock
;
122 /* Counter indexes ordered by attribute ID */
136 CM_ATTR_ID_OFFSET
= 0x0010,
147 static char const counter_group_names
[CM_COUNTER_GROUPS
]
148 [sizeof("cm_rx_duplicates")] = {
149 "cm_tx_msgs", "cm_tx_retries",
150 "cm_rx_msgs", "cm_rx_duplicates"
153 struct cm_counter_group
{
155 atomic_long_t counter
[CM_ATTR_COUNT
];
158 struct cm_counter_attribute
{
159 struct attribute attr
;
163 #define CM_COUNTER_ATTR(_name, _index) \
164 struct cm_counter_attribute cm_##_name##_counter_attr = { \
165 .attr = { .name = __stringify(_name), .mode = 0444 }, \
169 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
170 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
171 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
172 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
173 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
174 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
175 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
176 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
177 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
178 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
179 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
181 static struct attribute
*cm_counter_default_attrs
[] = {
182 &cm_req_counter_attr
.attr
,
183 &cm_mra_counter_attr
.attr
,
184 &cm_rej_counter_attr
.attr
,
185 &cm_rep_counter_attr
.attr
,
186 &cm_rtu_counter_attr
.attr
,
187 &cm_dreq_counter_attr
.attr
,
188 &cm_drep_counter_attr
.attr
,
189 &cm_sidr_req_counter_attr
.attr
,
190 &cm_sidr_rep_counter_attr
.attr
,
191 &cm_lap_counter_attr
.attr
,
192 &cm_apr_counter_attr
.attr
,
197 struct cm_device
*cm_dev
;
198 struct ib_mad_agent
*mad_agent
;
199 struct kobject port_obj
;
201 struct list_head cm_priv_prim_list
;
202 struct list_head cm_priv_altr_list
;
203 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
207 struct list_head list
;
208 struct ib_device
*ib_device
;
211 struct cm_port
*port
[];
215 struct cm_port
*port
;
217 struct rdma_ah_attr ah_attr
;
223 struct delayed_work work
;
224 struct list_head list
;
225 struct cm_port
*port
;
226 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
227 __be32 local_id
; /* Established / timewait */
229 struct ib_cm_event cm_event
;
230 struct sa_path_rec path
[];
233 struct cm_timewait_info
{
235 struct list_head list
;
236 struct rb_node remote_qp_node
;
237 struct rb_node remote_id_node
;
238 __be64 remote_ca_guid
;
240 u8 inserted_remote_qp
;
241 u8 inserted_remote_id
;
244 struct cm_id_private
{
247 struct rb_node service_node
;
248 struct rb_node sidr_id_node
;
249 spinlock_t lock
; /* Do not acquire inside cm.lock */
250 struct completion comp
;
252 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
253 * Protected by the cm.lock spinlock. */
254 int listen_sharecount
;
257 struct ib_mad_send_buf
*msg
;
258 struct cm_timewait_info
*timewait_info
;
259 /* todo: use alternate port on send failure */
267 enum ib_qp_type qp_type
;
271 enum ib_mtu path_mtu
;
275 u8 responder_resources
;
282 struct list_head prim_list
;
283 struct list_head altr_list
;
284 /* Indicates that the send port mad is registered and av is set */
285 int prim_send_port_not_ready
;
286 int altr_send_port_not_ready
;
288 struct list_head work_list
;
292 static void cm_work_handler(struct work_struct
*work
);
294 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
296 if (refcount_dec_and_test(&cm_id_priv
->refcount
))
297 complete(&cm_id_priv
->comp
);
300 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
301 struct ib_mad_send_buf
**msg
)
303 struct ib_mad_agent
*mad_agent
;
304 struct ib_mad_send_buf
*m
;
307 unsigned long flags
, flags2
;
310 /* don't let the port to be released till the agent is down */
311 spin_lock_irqsave(&cm
.state_lock
, flags2
);
312 spin_lock_irqsave(&cm
.lock
, flags
);
313 if (!cm_id_priv
->prim_send_port_not_ready
)
314 av
= &cm_id_priv
->av
;
315 else if (!cm_id_priv
->altr_send_port_not_ready
&&
316 (cm_id_priv
->alt_av
.port
))
317 av
= &cm_id_priv
->alt_av
;
319 pr_info("%s: not valid CM id\n", __func__
);
321 spin_unlock_irqrestore(&cm
.lock
, flags
);
324 spin_unlock_irqrestore(&cm
.lock
, flags
);
325 /* Make sure the port haven't released the mad yet */
326 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
328 pr_info("%s: not a valid MAD agent\n", __func__
);
332 ah
= rdma_create_ah(mad_agent
->qp
->pd
, &av
->ah_attr
, 0);
338 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
340 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
342 IB_MGMT_BASE_VERSION
);
344 rdma_destroy_ah(ah
, 0);
349 /* Timeout set by caller if response is expected. */
351 m
->retries
= cm_id_priv
->max_cm_retries
;
353 refcount_inc(&cm_id_priv
->refcount
);
354 m
->context
[0] = cm_id_priv
;
358 spin_unlock_irqrestore(&cm
.state_lock
, flags2
);
362 static struct ib_mad_send_buf
*cm_alloc_response_msg_no_ah(struct cm_port
*port
,
363 struct ib_mad_recv_wc
*mad_recv_wc
)
365 return ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
366 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
368 IB_MGMT_BASE_VERSION
);
371 static int cm_create_response_msg_ah(struct cm_port
*port
,
372 struct ib_mad_recv_wc
*mad_recv_wc
,
373 struct ib_mad_send_buf
*msg
)
377 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
378 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
386 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
389 rdma_destroy_ah(msg
->ah
, 0);
391 cm_deref_id(msg
->context
[0]);
392 ib_free_send_mad(msg
);
395 static int cm_alloc_response_msg(struct cm_port
*port
,
396 struct ib_mad_recv_wc
*mad_recv_wc
,
397 struct ib_mad_send_buf
**msg
)
399 struct ib_mad_send_buf
*m
;
402 m
= cm_alloc_response_msg_no_ah(port
, mad_recv_wc
);
406 ret
= cm_create_response_msg_ah(port
, mad_recv_wc
, m
);
416 static void * cm_copy_private_data(const void *private_data
,
421 if (!private_data
|| !private_data_len
)
424 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
426 return ERR_PTR(-ENOMEM
);
431 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
432 void *private_data
, u8 private_data_len
)
434 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
435 kfree(cm_id_priv
->private_data
);
437 cm_id_priv
->private_data
= private_data
;
438 cm_id_priv
->private_data_len
= private_data_len
;
441 static int cm_init_av_for_lap(struct cm_port
*port
, struct ib_wc
*wc
,
442 struct ib_grh
*grh
, struct cm_av
*av
)
444 struct rdma_ah_attr new_ah_attr
;
448 av
->pkey_index
= wc
->pkey_index
;
451 * av->ah_attr might be initialized based on past wc during incoming
452 * connect request or while sending out connect request. So initialize
453 * a new ah_attr on stack. If initialization fails, old ah_attr is
454 * used for sending any responses. If initialization is successful,
455 * than new ah_attr is used by overwriting old one.
457 ret
= ib_init_ah_attr_from_wc(port
->cm_dev
->ib_device
,
463 rdma_move_ah_attr(&av
->ah_attr
, &new_ah_attr
);
467 static int cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
468 struct ib_grh
*grh
, struct cm_av
*av
)
471 av
->pkey_index
= wc
->pkey_index
;
472 return ib_init_ah_attr_from_wc(port
->cm_dev
->ib_device
,
477 static int add_cm_id_to_port_list(struct cm_id_private
*cm_id_priv
,
479 struct cm_port
*port
)
484 spin_lock_irqsave(&cm
.lock
, flags
);
486 if (&cm_id_priv
->av
== av
)
487 list_add_tail(&cm_id_priv
->prim_list
, &port
->cm_priv_prim_list
);
488 else if (&cm_id_priv
->alt_av
== av
)
489 list_add_tail(&cm_id_priv
->altr_list
, &port
->cm_priv_altr_list
);
493 spin_unlock_irqrestore(&cm
.lock
, flags
);
497 static struct cm_port
*
498 get_cm_port_from_path(struct sa_path_rec
*path
, const struct ib_gid_attr
*attr
)
500 struct cm_device
*cm_dev
;
501 struct cm_port
*port
= NULL
;
505 read_lock_irqsave(&cm
.device_lock
, flags
);
506 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
507 if (cm_dev
->ib_device
== attr
->device
) {
508 port
= cm_dev
->port
[attr
->port_num
- 1];
512 read_unlock_irqrestore(&cm
.device_lock
, flags
);
514 /* SGID attribute can be NULL in following
516 * (a) Alternative path
517 * (b) IB link layer without GRH
518 * (c) LAP send messages
520 read_lock_irqsave(&cm
.device_lock
, flags
);
521 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
522 attr
= rdma_find_gid(cm_dev
->ib_device
,
524 sa_conv_pathrec_to_gid_type(path
),
527 port
= cm_dev
->port
[attr
->port_num
- 1];
531 read_unlock_irqrestore(&cm
.device_lock
, flags
);
533 rdma_put_gid_attr(attr
);
538 static int cm_init_av_by_path(struct sa_path_rec
*path
,
539 const struct ib_gid_attr
*sgid_attr
,
541 struct cm_id_private
*cm_id_priv
)
543 struct rdma_ah_attr new_ah_attr
;
544 struct cm_device
*cm_dev
;
545 struct cm_port
*port
;
548 port
= get_cm_port_from_path(path
, sgid_attr
);
551 cm_dev
= port
->cm_dev
;
553 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
554 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
561 * av->ah_attr might be initialized based on wc or during
562 * request processing time which might have reference to sgid_attr.
563 * So initialize a new ah_attr on stack.
564 * If initialization fails, old ah_attr is used for sending any
565 * responses. If initialization is successful, than new ah_attr
566 * is used by overwriting the old one. So that right ah_attr
567 * can be used to return an error response.
569 ret
= ib_init_ah_attr_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
570 &new_ah_attr
, sgid_attr
);
574 av
->timeout
= path
->packet_life_time
+ 1;
576 ret
= add_cm_id_to_port_list(cm_id_priv
, av
, port
);
578 rdma_destroy_ah_attr(&new_ah_attr
);
581 rdma_move_ah_attr(&av
->ah_attr
, &new_ah_attr
);
585 static u32
cm_local_id(__be32 local_id
)
587 return (__force u32
) (local_id
^ cm
.random_id_operand
);
590 static void cm_free_id(__be32 local_id
)
592 xa_erase_irq(&cm
.local_id_table
, cm_local_id(local_id
));
595 static struct cm_id_private
*cm_acquire_id(__be32 local_id
, __be32 remote_id
)
597 struct cm_id_private
*cm_id_priv
;
600 cm_id_priv
= xa_load(&cm
.local_id_table
, cm_local_id(local_id
));
601 if (!cm_id_priv
|| cm_id_priv
->id
.remote_id
!= remote_id
||
602 !refcount_inc_not_zero(&cm_id_priv
->refcount
))
610 * Trivial helpers to strip endian annotation and compare; the
611 * endianness doesn't actually matter since we just need a stable
612 * order for the RB tree.
614 static int be32_lt(__be32 a
, __be32 b
)
616 return (__force u32
) a
< (__force u32
) b
;
619 static int be32_gt(__be32 a
, __be32 b
)
621 return (__force u32
) a
> (__force u32
) b
;
624 static int be64_lt(__be64 a
, __be64 b
)
626 return (__force u64
) a
< (__force u64
) b
;
629 static int be64_gt(__be64 a
, __be64 b
)
631 return (__force u64
) a
> (__force u64
) b
;
635 * Inserts a new cm_id_priv into the listen_service_table. Returns cm_id_priv
636 * if the new ID was inserted, NULL if it could not be inserted due to a
637 * collision, or the existing cm_id_priv ready for shared usage.
639 static struct cm_id_private
*cm_insert_listen(struct cm_id_private
*cm_id_priv
,
640 ib_cm_handler shared_handler
)
642 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
643 struct rb_node
*parent
= NULL
;
644 struct cm_id_private
*cur_cm_id_priv
;
645 __be64 service_id
= cm_id_priv
->id
.service_id
;
646 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
649 spin_lock_irqsave(&cm
.lock
, flags
);
652 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
654 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
655 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
656 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
)) {
658 * Sharing an ib_cm_id with different handlers is not
661 if (cur_cm_id_priv
->id
.cm_handler
!= shared_handler
||
662 cur_cm_id_priv
->id
.context
||
663 WARN_ON(!cur_cm_id_priv
->id
.cm_handler
)) {
664 spin_unlock_irqrestore(&cm
.lock
, flags
);
667 refcount_inc(&cur_cm_id_priv
->refcount
);
668 cur_cm_id_priv
->listen_sharecount
++;
669 spin_unlock_irqrestore(&cm
.lock
, flags
);
670 return cur_cm_id_priv
;
673 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
674 link
= &(*link
)->rb_left
;
675 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
676 link
= &(*link
)->rb_right
;
677 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
678 link
= &(*link
)->rb_left
;
679 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
680 link
= &(*link
)->rb_right
;
682 link
= &(*link
)->rb_right
;
684 cm_id_priv
->listen_sharecount
++;
685 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
686 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
687 spin_unlock_irqrestore(&cm
.lock
, flags
);
691 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
694 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
695 struct cm_id_private
*cm_id_priv
;
698 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
699 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
700 cm_id_priv
->id
.service_id
&&
701 (cm_id_priv
->id
.device
== device
))
704 if (device
< cm_id_priv
->id
.device
)
705 node
= node
->rb_left
;
706 else if (device
> cm_id_priv
->id
.device
)
707 node
= node
->rb_right
;
708 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
709 node
= node
->rb_left
;
710 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
711 node
= node
->rb_right
;
713 node
= node
->rb_right
;
718 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
721 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
722 struct rb_node
*parent
= NULL
;
723 struct cm_timewait_info
*cur_timewait_info
;
724 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
725 __be32 remote_id
= timewait_info
->work
.remote_id
;
729 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
731 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
732 link
= &(*link
)->rb_left
;
733 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
734 link
= &(*link
)->rb_right
;
735 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
736 link
= &(*link
)->rb_left
;
737 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
738 link
= &(*link
)->rb_right
;
740 return cur_timewait_info
;
742 timewait_info
->inserted_remote_id
= 1;
743 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
744 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
748 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
751 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
752 struct cm_timewait_info
*timewait_info
;
755 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
757 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
758 node
= node
->rb_left
;
759 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
760 node
= node
->rb_right
;
761 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
762 node
= node
->rb_left
;
763 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
764 node
= node
->rb_right
;
766 return timewait_info
;
771 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
774 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
775 struct rb_node
*parent
= NULL
;
776 struct cm_timewait_info
*cur_timewait_info
;
777 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
778 __be32 remote_qpn
= timewait_info
->remote_qpn
;
782 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
784 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
785 link
= &(*link
)->rb_left
;
786 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
787 link
= &(*link
)->rb_right
;
788 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
789 link
= &(*link
)->rb_left
;
790 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
791 link
= &(*link
)->rb_right
;
793 return cur_timewait_info
;
795 timewait_info
->inserted_remote_qp
= 1;
796 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
797 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
801 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
804 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
805 struct rb_node
*parent
= NULL
;
806 struct cm_id_private
*cur_cm_id_priv
;
807 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
808 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
812 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
814 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
815 link
= &(*link
)->rb_left
;
816 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
817 link
= &(*link
)->rb_right
;
820 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
823 link
= &(*link
)->rb_left
;
825 link
= &(*link
)->rb_right
;
827 return cur_cm_id_priv
;
830 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
831 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
835 static struct cm_id_private
*cm_alloc_id_priv(struct ib_device
*device
,
836 ib_cm_handler cm_handler
,
839 struct cm_id_private
*cm_id_priv
;
843 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
845 return ERR_PTR(-ENOMEM
);
847 cm_id_priv
->id
.state
= IB_CM_IDLE
;
848 cm_id_priv
->id
.device
= device
;
849 cm_id_priv
->id
.cm_handler
= cm_handler
;
850 cm_id_priv
->id
.context
= context
;
851 cm_id_priv
->id
.remote_cm_qpn
= 1;
853 RB_CLEAR_NODE(&cm_id_priv
->service_node
);
854 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
855 spin_lock_init(&cm_id_priv
->lock
);
856 init_completion(&cm_id_priv
->comp
);
857 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
858 INIT_LIST_HEAD(&cm_id_priv
->prim_list
);
859 INIT_LIST_HEAD(&cm_id_priv
->altr_list
);
860 atomic_set(&cm_id_priv
->work_count
, -1);
861 refcount_set(&cm_id_priv
->refcount
, 1);
863 ret
= xa_alloc_cyclic_irq(&cm
.local_id_table
, &id
, NULL
, xa_limit_32b
,
864 &cm
.local_id_next
, GFP_KERNEL
);
867 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
877 * Make the ID visible to the MAD handlers and other threads that use the
880 static void cm_finalize_id(struct cm_id_private
*cm_id_priv
)
882 xa_store_irq(&cm
.local_id_table
, cm_local_id(cm_id_priv
->id
.local_id
),
883 cm_id_priv
, GFP_KERNEL
);
886 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
887 ib_cm_handler cm_handler
,
890 struct cm_id_private
*cm_id_priv
;
892 cm_id_priv
= cm_alloc_id_priv(device
, cm_handler
, context
);
893 if (IS_ERR(cm_id_priv
))
894 return ERR_CAST(cm_id_priv
);
896 cm_finalize_id(cm_id_priv
);
897 return &cm_id_priv
->id
;
899 EXPORT_SYMBOL(ib_create_cm_id
);
901 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
903 struct cm_work
*work
;
905 if (list_empty(&cm_id_priv
->work_list
))
908 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
909 list_del(&work
->list
);
913 static void cm_free_work(struct cm_work
*work
)
915 if (work
->mad_recv_wc
)
916 ib_free_recv_mad(work
->mad_recv_wc
);
920 static inline int cm_convert_to_ms(int iba_time
)
922 /* approximate conversion to ms from 4.096us x 2^iba_time */
923 return 1 << max(iba_time
- 8, 0);
927 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
928 * Because of how ack_timeout is stored, adding one doubles the timeout.
929 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
930 * increment it (round up) only if the other is within 50%.
932 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
934 int ack_timeout
= packet_life_time
+ 1;
936 if (ack_timeout
>= ca_ack_delay
)
937 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
939 ack_timeout
= ca_ack_delay
+
940 (ack_timeout
>= (ca_ack_delay
- 1));
942 return min(31, ack_timeout
);
945 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
947 if (timewait_info
->inserted_remote_id
) {
948 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
949 timewait_info
->inserted_remote_id
= 0;
952 if (timewait_info
->inserted_remote_qp
) {
953 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
954 timewait_info
->inserted_remote_qp
= 0;
958 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
960 struct cm_timewait_info
*timewait_info
;
962 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
964 return ERR_PTR(-ENOMEM
);
966 timewait_info
->work
.local_id
= local_id
;
967 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
968 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
969 return timewait_info
;
972 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
976 struct cm_device
*cm_dev
;
978 lockdep_assert_held(&cm_id_priv
->lock
);
980 cm_dev
= ib_get_client_data(cm_id_priv
->id
.device
, &cm_client
);
984 spin_lock_irqsave(&cm
.lock
, flags
);
985 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
986 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
987 spin_unlock_irqrestore(&cm
.lock
, flags
);
990 * The cm_id could be destroyed by the user before we exit timewait.
991 * To protect against this, we search for the cm_id after exiting
992 * timewait before notifying the user that we've exited timewait.
994 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
995 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
997 /* Check if the device started its remove_one */
998 spin_lock_irqsave(&cm
.lock
, flags
);
999 if (!cm_dev
->going_down
)
1000 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
1001 msecs_to_jiffies(wait_time
));
1002 spin_unlock_irqrestore(&cm
.lock
, flags
);
1004 cm_id_priv
->timewait_info
= NULL
;
1007 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
1009 unsigned long flags
;
1011 lockdep_assert_held(&cm_id_priv
->lock
);
1013 cm_id_priv
->id
.state
= IB_CM_IDLE
;
1014 if (cm_id_priv
->timewait_info
) {
1015 spin_lock_irqsave(&cm
.lock
, flags
);
1016 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1017 spin_unlock_irqrestore(&cm
.lock
, flags
);
1018 kfree(cm_id_priv
->timewait_info
);
1019 cm_id_priv
->timewait_info
= NULL
;
1023 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
1025 struct cm_id_private
*cm_id_priv
;
1026 struct cm_work
*work
;
1028 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1029 spin_lock_irq(&cm_id_priv
->lock
);
1031 switch (cm_id
->state
) {
1033 spin_lock(&cm
.lock
);
1034 if (--cm_id_priv
->listen_sharecount
> 0) {
1035 /* The id is still shared. */
1036 WARN_ON(refcount_read(&cm_id_priv
->refcount
) == 1);
1037 spin_unlock(&cm
.lock
);
1038 spin_unlock_irq(&cm_id_priv
->lock
);
1039 cm_deref_id(cm_id_priv
);
1042 cm_id
->state
= IB_CM_IDLE
;
1043 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
1044 RB_CLEAR_NODE(&cm_id_priv
->service_node
);
1045 spin_unlock(&cm
.lock
);
1047 case IB_CM_SIDR_REQ_SENT
:
1048 cm_id
->state
= IB_CM_IDLE
;
1049 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1051 case IB_CM_SIDR_REQ_RCVD
:
1052 cm_send_sidr_rep_locked(cm_id_priv
,
1053 &(struct ib_cm_sidr_rep_param
){
1054 .status
= IB_SIDR_REJECT
});
1055 /* cm_send_sidr_rep_locked will not move to IDLE if it fails */
1056 cm_id
->state
= IB_CM_IDLE
;
1058 case IB_CM_REQ_SENT
:
1059 case IB_CM_MRA_REQ_RCVD
:
1060 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1061 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_TIMEOUT
,
1062 &cm_id_priv
->id
.device
->node_guid
,
1063 sizeof(cm_id_priv
->id
.device
->node_guid
),
1066 case IB_CM_REQ_RCVD
:
1067 if (err
== -ENOMEM
) {
1068 /* Do not reject to allow future retries. */
1069 cm_reset_to_idle(cm_id_priv
);
1071 cm_send_rej_locked(cm_id_priv
,
1072 IB_CM_REJ_CONSUMER_DEFINED
, NULL
, 0,
1076 case IB_CM_REP_SENT
:
1077 case IB_CM_MRA_REP_RCVD
:
1078 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1079 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
1082 case IB_CM_MRA_REQ_SENT
:
1083 case IB_CM_REP_RCVD
:
1084 case IB_CM_MRA_REP_SENT
:
1085 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
1088 case IB_CM_ESTABLISHED
:
1089 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
1090 cm_id
->state
= IB_CM_IDLE
;
1093 cm_send_dreq_locked(cm_id_priv
, NULL
, 0);
1095 case IB_CM_DREQ_SENT
:
1096 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1097 cm_enter_timewait(cm_id_priv
);
1099 case IB_CM_DREQ_RCVD
:
1100 cm_send_drep_locked(cm_id_priv
, NULL
, 0);
1101 WARN_ON(cm_id
->state
!= IB_CM_TIMEWAIT
);
1103 case IB_CM_TIMEWAIT
:
1105 * The cm_acquire_id in cm_timewait_handler will stop working
1106 * once we do cm_free_id() below, so just move to idle here for
1109 cm_id
->state
= IB_CM_IDLE
;
1114 WARN_ON(cm_id
->state
!= IB_CM_IDLE
);
1116 spin_lock(&cm
.lock
);
1117 /* Required for cleanup paths related cm_req_handler() */
1118 if (cm_id_priv
->timewait_info
) {
1119 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1120 kfree(cm_id_priv
->timewait_info
);
1121 cm_id_priv
->timewait_info
= NULL
;
1123 if (!list_empty(&cm_id_priv
->altr_list
) &&
1124 (!cm_id_priv
->altr_send_port_not_ready
))
1125 list_del(&cm_id_priv
->altr_list
);
1126 if (!list_empty(&cm_id_priv
->prim_list
) &&
1127 (!cm_id_priv
->prim_send_port_not_ready
))
1128 list_del(&cm_id_priv
->prim_list
);
1129 WARN_ON(cm_id_priv
->listen_sharecount
);
1130 WARN_ON(!RB_EMPTY_NODE(&cm_id_priv
->service_node
));
1131 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
))
1132 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
1133 spin_unlock(&cm
.lock
);
1134 spin_unlock_irq(&cm_id_priv
->lock
);
1136 cm_free_id(cm_id
->local_id
);
1137 cm_deref_id(cm_id_priv
);
1138 wait_for_completion(&cm_id_priv
->comp
);
1139 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
1142 rdma_destroy_ah_attr(&cm_id_priv
->av
.ah_attr
);
1143 rdma_destroy_ah_attr(&cm_id_priv
->alt_av
.ah_attr
);
1144 kfree(cm_id_priv
->private_data
);
1145 kfree_rcu(cm_id_priv
, rcu
);
1148 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
1150 cm_destroy_id(cm_id
, 0);
1152 EXPORT_SYMBOL(ib_destroy_cm_id
);
1154 static int cm_init_listen(struct cm_id_private
*cm_id_priv
, __be64 service_id
,
1155 __be64 service_mask
)
1157 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
1158 service_id
&= service_mask
;
1159 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
1160 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
1163 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
1164 cm_id_priv
->id
.service_id
= cpu_to_be64(cm
.listen_service_id
++);
1165 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1167 cm_id_priv
->id
.service_id
= service_id
;
1168 cm_id_priv
->id
.service_mask
= service_mask
;
1174 * ib_cm_listen - Initiates listening on the specified service ID for
1175 * connection and service ID resolution requests.
1176 * @cm_id: Connection identifier associated with the listen request.
1177 * @service_id: Service identifier matched against incoming connection
1178 * and service ID resolution requests. The service ID should be specified
1179 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1180 * assign a service ID to the caller.
1181 * @service_mask: Mask applied to service ID used to listen across a
1182 * range of service IDs. If set to 0, the service ID is matched
1183 * exactly. This parameter is ignored if %service_id is set to
1184 * IB_CM_ASSIGN_SERVICE_ID.
1186 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
)
1188 struct cm_id_private
*cm_id_priv
=
1189 container_of(cm_id
, struct cm_id_private
, id
);
1190 unsigned long flags
;
1193 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1194 if (cm_id_priv
->id
.state
!= IB_CM_IDLE
) {
1199 ret
= cm_init_listen(cm_id_priv
, service_id
, service_mask
);
1203 if (!cm_insert_listen(cm_id_priv
, NULL
)) {
1208 cm_id_priv
->id
.state
= IB_CM_LISTEN
;
1212 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1215 EXPORT_SYMBOL(ib_cm_listen
);
1218 * Create a new listening ib_cm_id and listen on the given service ID.
1220 * If there's an existing ID listening on that same device and service ID,
1223 * @device: Device associated with the cm_id. All related communication will
1224 * be associated with the specified device.
1225 * @cm_handler: Callback invoked to notify the user of CM events.
1226 * @service_id: Service identifier matched against incoming connection
1227 * and service ID resolution requests. The service ID should be specified
1228 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1229 * assign a service ID to the caller.
1231 * Callers should call ib_destroy_cm_id when done with the listener ID.
1233 struct ib_cm_id
*ib_cm_insert_listen(struct ib_device
*device
,
1234 ib_cm_handler cm_handler
,
1237 struct cm_id_private
*listen_id_priv
;
1238 struct cm_id_private
*cm_id_priv
;
1241 /* Create an ID in advance, since the creation may sleep */
1242 cm_id_priv
= cm_alloc_id_priv(device
, cm_handler
, NULL
);
1243 if (IS_ERR(cm_id_priv
))
1244 return ERR_CAST(cm_id_priv
);
1246 err
= cm_init_listen(cm_id_priv
, service_id
, 0);
1248 return ERR_PTR(err
);
1250 spin_lock_irq(&cm_id_priv
->lock
);
1251 listen_id_priv
= cm_insert_listen(cm_id_priv
, cm_handler
);
1252 if (listen_id_priv
!= cm_id_priv
) {
1253 spin_unlock_irq(&cm_id_priv
->lock
);
1254 ib_destroy_cm_id(&cm_id_priv
->id
);
1255 if (!listen_id_priv
)
1256 return ERR_PTR(-EINVAL
);
1257 return &listen_id_priv
->id
;
1259 cm_id_priv
->id
.state
= IB_CM_LISTEN
;
1260 spin_unlock_irq(&cm_id_priv
->lock
);
1263 * A listen ID does not need to be in the xarray since it does not
1264 * receive mads, is not placed in the remote_id or remote_qpn rbtree,
1265 * and does not enter timewait.
1268 return &cm_id_priv
->id
;
1270 EXPORT_SYMBOL(ib_cm_insert_listen
);
1272 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
)
1274 u64 hi_tid
, low_tid
;
1276 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
1277 low_tid
= (u64
)cm_id_priv
->id
.local_id
;
1278 return cpu_to_be64(hi_tid
| low_tid
);
1281 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
1282 __be16 attr_id
, __be64 tid
)
1284 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1285 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1286 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1287 hdr
->method
= IB_MGMT_METHOD_SEND
;
1288 hdr
->attr_id
= attr_id
;
1292 static void cm_format_req(struct cm_req_msg
*req_msg
,
1293 struct cm_id_private
*cm_id_priv
,
1294 struct ib_cm_req_param
*param
)
1296 struct sa_path_rec
*pri_path
= param
->primary_path
;
1297 struct sa_path_rec
*alt_path
= param
->alternate_path
;
1298 bool pri_ext
= false;
1300 if (pri_path
->rec_type
== SA_PATH_REC_TYPE_OPA
)
1301 pri_ext
= opa_is_extended_lid(pri_path
->opa
.dlid
,
1302 pri_path
->opa
.slid
);
1304 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1305 cm_form_tid(cm_id_priv
));
1307 IBA_SET(CM_REQ_LOCAL_COMM_ID
, req_msg
,
1308 be32_to_cpu(cm_id_priv
->id
.local_id
));
1309 IBA_SET(CM_REQ_SERVICE_ID
, req_msg
, be64_to_cpu(param
->service_id
));
1310 IBA_SET(CM_REQ_LOCAL_CA_GUID
, req_msg
,
1311 be64_to_cpu(cm_id_priv
->id
.device
->node_guid
));
1312 IBA_SET(CM_REQ_LOCAL_QPN
, req_msg
, param
->qp_num
);
1313 IBA_SET(CM_REQ_INITIATOR_DEPTH
, req_msg
, param
->initiator_depth
);
1314 IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT
, req_msg
,
1315 param
->remote_cm_response_timeout
);
1316 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1317 IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL
, req_msg
, param
->flow_control
);
1318 IBA_SET(CM_REQ_STARTING_PSN
, req_msg
, param
->starting_psn
);
1319 IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
,
1320 param
->local_cm_response_timeout
);
1321 IBA_SET(CM_REQ_PARTITION_KEY
, req_msg
,
1322 be16_to_cpu(param
->primary_path
->pkey
));
1323 IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
,
1324 param
->primary_path
->mtu
);
1325 IBA_SET(CM_REQ_MAX_CM_RETRIES
, req_msg
, param
->max_cm_retries
);
1327 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1328 IBA_SET(CM_REQ_RESPONDER_RESOURCES
, req_msg
,
1329 param
->responder_resources
);
1330 IBA_SET(CM_REQ_RETRY_COUNT
, req_msg
, param
->retry_count
);
1331 IBA_SET(CM_REQ_RNR_RETRY_COUNT
, req_msg
,
1332 param
->rnr_retry_count
);
1333 IBA_SET(CM_REQ_SRQ
, req_msg
, param
->srq
);
1336 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
) =
1338 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
) =
1341 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
)
1342 ->global
.interface_id
=
1343 OPA_MAKE_ID(be32_to_cpu(pri_path
->opa
.slid
));
1344 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
)
1345 ->global
.interface_id
=
1346 OPA_MAKE_ID(be32_to_cpu(pri_path
->opa
.dlid
));
1348 if (pri_path
->hop_limit
<= 1) {
1349 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1350 be16_to_cpu(pri_ext
? 0 :
1351 htons(ntohl(sa_path_get_slid(
1353 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
1354 be16_to_cpu(pri_ext
? 0 :
1355 htons(ntohl(sa_path_get_dlid(
1358 /* Work-around until there's a way to obtain remote LID info */
1359 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1360 be16_to_cpu(IB_LID_PERMISSIVE
));
1361 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
1362 be16_to_cpu(IB_LID_PERMISSIVE
));
1364 IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL
, req_msg
,
1365 be32_to_cpu(pri_path
->flow_label
));
1366 IBA_SET(CM_REQ_PRIMARY_PACKET_RATE
, req_msg
, pri_path
->rate
);
1367 IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS
, req_msg
, pri_path
->traffic_class
);
1368 IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT
, req_msg
, pri_path
->hop_limit
);
1369 IBA_SET(CM_REQ_PRIMARY_SL
, req_msg
, pri_path
->sl
);
1370 IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL
, req_msg
,
1371 (pri_path
->hop_limit
<= 1));
1372 IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT
, req_msg
,
1373 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1374 pri_path
->packet_life_time
));
1377 bool alt_ext
= false;
1379 if (alt_path
->rec_type
== SA_PATH_REC_TYPE_OPA
)
1380 alt_ext
= opa_is_extended_lid(alt_path
->opa
.dlid
,
1381 alt_path
->opa
.slid
);
1383 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
) =
1385 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
) =
1388 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
,
1390 ->global
.interface_id
=
1391 OPA_MAKE_ID(be32_to_cpu(alt_path
->opa
.slid
));
1392 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID
,
1394 ->global
.interface_id
=
1395 OPA_MAKE_ID(be32_to_cpu(alt_path
->opa
.dlid
));
1397 if (alt_path
->hop_limit
<= 1) {
1398 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
1401 htons(ntohl(sa_path_get_slid(
1403 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
1406 htons(ntohl(sa_path_get_dlid(
1409 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
1410 be16_to_cpu(IB_LID_PERMISSIVE
));
1411 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
1412 be16_to_cpu(IB_LID_PERMISSIVE
));
1414 IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL
, req_msg
,
1415 be32_to_cpu(alt_path
->flow_label
));
1416 IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE
, req_msg
, alt_path
->rate
);
1417 IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS
, req_msg
,
1418 alt_path
->traffic_class
);
1419 IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT
, req_msg
,
1420 alt_path
->hop_limit
);
1421 IBA_SET(CM_REQ_ALTERNATE_SL
, req_msg
, alt_path
->sl
);
1422 IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL
, req_msg
,
1423 (alt_path
->hop_limit
<= 1));
1424 IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT
, req_msg
,
1425 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1426 alt_path
->packet_life_time
));
1429 if (param
->private_data
&& param
->private_data_len
)
1430 IBA_SET_MEM(CM_REQ_PRIVATE_DATA
, req_msg
, param
->private_data
,
1431 param
->private_data_len
);
1434 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1436 if (!param
->primary_path
)
1439 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1440 param
->qp_type
!= IB_QPT_XRC_INI
)
1443 if (param
->private_data
&&
1444 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1447 if (param
->alternate_path
&&
1448 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1449 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1455 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1456 struct ib_cm_req_param
*param
)
1458 struct cm_id_private
*cm_id_priv
;
1459 struct cm_req_msg
*req_msg
;
1460 unsigned long flags
;
1463 ret
= cm_validate_req_param(param
);
1467 /* Verify that we're not in timewait. */
1468 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1469 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1470 if (cm_id
->state
!= IB_CM_IDLE
|| WARN_ON(cm_id_priv
->timewait_info
)) {
1471 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1475 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1477 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1479 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1480 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1484 ret
= cm_init_av_by_path(param
->primary_path
,
1485 param
->ppath_sgid_attr
, &cm_id_priv
->av
,
1489 if (param
->alternate_path
) {
1490 ret
= cm_init_av_by_path(param
->alternate_path
, NULL
,
1491 &cm_id_priv
->alt_av
, cm_id_priv
);
1495 cm_id
->service_id
= param
->service_id
;
1496 cm_id
->service_mask
= ~cpu_to_be64(0);
1497 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1498 param
->primary_path
->packet_life_time
) * 2 +
1500 param
->remote_cm_response_timeout
);
1501 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1502 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1503 cm_id_priv
->responder_resources
= param
->responder_resources
;
1504 cm_id_priv
->retry_count
= param
->retry_count
;
1505 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1506 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1507 cm_id_priv
->qp_type
= param
->qp_type
;
1509 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1513 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1514 cm_format_req(req_msg
, cm_id_priv
, param
);
1515 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1516 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1517 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1519 cm_id_priv
->local_qpn
= cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
));
1520 cm_id_priv
->rq_psn
= cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN
, req_msg
));
1522 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1523 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1525 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1528 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1529 cm_id
->state
= IB_CM_REQ_SENT
;
1530 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1533 error2
: cm_free_msg(cm_id_priv
->msg
);
1536 EXPORT_SYMBOL(ib_send_cm_req
);
1538 static int cm_issue_rej(struct cm_port
*port
,
1539 struct ib_mad_recv_wc
*mad_recv_wc
,
1540 enum ib_cm_rej_reason reason
,
1541 enum cm_msg_response msg_rejected
,
1542 void *ari
, u8 ari_length
)
1544 struct ib_mad_send_buf
*msg
= NULL
;
1545 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1548 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1552 /* We just need common CM header information. Cast to any message. */
1553 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1554 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1556 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1557 IBA_SET(CM_REJ_REMOTE_COMM_ID
, rej_msg
,
1558 IBA_GET(CM_REJ_LOCAL_COMM_ID
, rcv_msg
));
1559 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1560 IBA_GET(CM_REJ_REMOTE_COMM_ID
, rcv_msg
));
1561 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, msg_rejected
);
1562 IBA_SET(CM_REJ_REASON
, rej_msg
, reason
);
1564 if (ari
&& ari_length
) {
1565 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
, ari_length
);
1566 IBA_SET_MEM(CM_REJ_ARI
, rej_msg
, ari
, ari_length
);
1569 ret
= ib_post_send_mad(msg
, NULL
);
1576 static bool cm_req_has_alt_path(struct cm_req_msg
*req_msg
)
1578 return ((cpu_to_be16(
1579 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
))) ||
1580 (ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
,
1584 static void cm_path_set_rec_type(struct ib_device
*ib_device
, u8 port_num
,
1585 struct sa_path_rec
*path
, union ib_gid
*gid
)
1587 if (ib_is_opa_gid(gid
) && rdma_cap_opa_ah(ib_device
, port_num
))
1588 path
->rec_type
= SA_PATH_REC_TYPE_OPA
;
1590 path
->rec_type
= SA_PATH_REC_TYPE_IB
;
1593 static void cm_format_path_lid_from_req(struct cm_req_msg
*req_msg
,
1594 struct sa_path_rec
*primary_path
,
1595 struct sa_path_rec
*alt_path
)
1599 if (primary_path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
1600 sa_path_set_dlid(primary_path
,
1601 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID
,
1603 sa_path_set_slid(primary_path
,
1604 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID
,
1607 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1608 CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
));
1609 sa_path_set_dlid(primary_path
, lid
);
1611 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1612 CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
));
1613 sa_path_set_slid(primary_path
, lid
);
1616 if (!cm_req_has_alt_path(req_msg
))
1619 if (alt_path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
1620 sa_path_set_dlid(alt_path
,
1621 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
,
1623 sa_path_set_slid(alt_path
,
1624 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
,
1627 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1628 CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
));
1629 sa_path_set_dlid(alt_path
, lid
);
1631 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1632 CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
));
1633 sa_path_set_slid(alt_path
, lid
);
1637 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1638 struct sa_path_rec
*primary_path
,
1639 struct sa_path_rec
*alt_path
)
1641 primary_path
->dgid
=
1642 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
);
1643 primary_path
->sgid
=
1644 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
);
1645 primary_path
->flow_label
=
1646 cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL
, req_msg
));
1647 primary_path
->hop_limit
= IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT
, req_msg
);
1648 primary_path
->traffic_class
=
1649 IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS
, req_msg
);
1650 primary_path
->reversible
= 1;
1651 primary_path
->pkey
=
1652 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
1653 primary_path
->sl
= IBA_GET(CM_REQ_PRIMARY_SL
, req_msg
);
1654 primary_path
->mtu_selector
= IB_SA_EQ
;
1655 primary_path
->mtu
= IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
1656 primary_path
->rate_selector
= IB_SA_EQ
;
1657 primary_path
->rate
= IBA_GET(CM_REQ_PRIMARY_PACKET_RATE
, req_msg
);
1658 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1659 primary_path
->packet_life_time
=
1660 IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT
, req_msg
);
1661 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1662 primary_path
->service_id
=
1663 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
1664 if (sa_path_is_roce(primary_path
))
1665 primary_path
->roce
.route_resolved
= false;
1667 if (cm_req_has_alt_path(req_msg
)) {
1668 alt_path
->dgid
= *IBA_GET_MEM_PTR(
1669 CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
);
1670 alt_path
->sgid
= *IBA_GET_MEM_PTR(
1671 CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
);
1672 alt_path
->flow_label
= cpu_to_be32(
1673 IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL
, req_msg
));
1674 alt_path
->hop_limit
=
1675 IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT
, req_msg
);
1676 alt_path
->traffic_class
=
1677 IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS
, req_msg
);
1678 alt_path
->reversible
= 1;
1680 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
1681 alt_path
->sl
= IBA_GET(CM_REQ_ALTERNATE_SL
, req_msg
);
1682 alt_path
->mtu_selector
= IB_SA_EQ
;
1684 IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
1685 alt_path
->rate_selector
= IB_SA_EQ
;
1686 alt_path
->rate
= IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE
, req_msg
);
1687 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1688 alt_path
->packet_life_time
=
1689 IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT
, req_msg
);
1690 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1691 alt_path
->service_id
=
1692 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
1694 if (sa_path_is_roce(alt_path
))
1695 alt_path
->roce
.route_resolved
= false;
1697 cm_format_path_lid_from_req(req_msg
, primary_path
, alt_path
);
1700 static u16
cm_get_bth_pkey(struct cm_work
*work
)
1702 struct ib_device
*ib_dev
= work
->port
->cm_dev
->ib_device
;
1703 u8 port_num
= work
->port
->port_num
;
1704 u16 pkey_index
= work
->mad_recv_wc
->wc
->pkey_index
;
1708 ret
= ib_get_cached_pkey(ib_dev
, port_num
, pkey_index
, &pkey
);
1710 dev_warn_ratelimited(&ib_dev
->dev
, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1711 port_num
, pkey_index
, ret
);
1719 * Convert OPA SGID to IB SGID
1720 * ULPs (such as IPoIB) do not understand OPA GIDs and will
1721 * reject them as the local_gid will not match the sgid. Therefore,
1722 * change the pathrec's SGID to an IB SGID.
1724 * @work: Work completion
1725 * @path: Path record
1727 static void cm_opa_to_ib_sgid(struct cm_work
*work
,
1728 struct sa_path_rec
*path
)
1730 struct ib_device
*dev
= work
->port
->cm_dev
->ib_device
;
1731 u8 port_num
= work
->port
->port_num
;
1733 if (rdma_cap_opa_ah(dev
, port_num
) &&
1734 (ib_is_opa_gid(&path
->sgid
))) {
1737 if (rdma_query_gid(dev
, port_num
, 0, &sgid
)) {
1739 "Error updating sgid in CM request\n");
1747 static void cm_format_req_event(struct cm_work
*work
,
1748 struct cm_id_private
*cm_id_priv
,
1749 struct ib_cm_id
*listen_id
)
1751 struct cm_req_msg
*req_msg
;
1752 struct ib_cm_req_event_param
*param
;
1754 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1755 param
= &work
->cm_event
.param
.req_rcvd
;
1756 param
->listen_id
= listen_id
;
1757 param
->bth_pkey
= cm_get_bth_pkey(work
);
1758 param
->port
= cm_id_priv
->av
.port
->port_num
;
1759 param
->primary_path
= &work
->path
[0];
1760 cm_opa_to_ib_sgid(work
, param
->primary_path
);
1761 if (cm_req_has_alt_path(req_msg
)) {
1762 param
->alternate_path
= &work
->path
[1];
1763 cm_opa_to_ib_sgid(work
, param
->alternate_path
);
1765 param
->alternate_path
= NULL
;
1767 param
->remote_ca_guid
=
1768 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID
, req_msg
));
1769 param
->remote_qkey
= IBA_GET(CM_REQ_LOCAL_Q_KEY
, req_msg
);
1770 param
->remote_qpn
= IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
);
1771 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1772 param
->starting_psn
= IBA_GET(CM_REQ_STARTING_PSN
, req_msg
);
1773 param
->responder_resources
= IBA_GET(CM_REQ_INITIATOR_DEPTH
, req_msg
);
1774 param
->initiator_depth
= IBA_GET(CM_REQ_RESPONDER_RESOURCES
, req_msg
);
1775 param
->local_cm_response_timeout
=
1776 IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT
, req_msg
);
1777 param
->flow_control
= IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL
, req_msg
);
1778 param
->remote_cm_response_timeout
=
1779 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
);
1780 param
->retry_count
= IBA_GET(CM_REQ_RETRY_COUNT
, req_msg
);
1781 param
->rnr_retry_count
= IBA_GET(CM_REQ_RNR_RETRY_COUNT
, req_msg
);
1782 param
->srq
= IBA_GET(CM_REQ_SRQ
, req_msg
);
1783 param
->ppath_sgid_attr
= cm_id_priv
->av
.ah_attr
.grh
.sgid_attr
;
1784 work
->cm_event
.private_data
=
1785 IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA
, req_msg
);
1788 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1789 struct cm_work
*work
)
1793 /* We will typically only have the current event to report. */
1794 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1797 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1798 spin_lock_irq(&cm_id_priv
->lock
);
1799 work
= cm_dequeue_work(cm_id_priv
);
1800 spin_unlock_irq(&cm_id_priv
->lock
);
1804 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1808 cm_deref_id(cm_id_priv
);
1810 cm_destroy_id(&cm_id_priv
->id
, ret
);
1813 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1814 struct cm_id_private
*cm_id_priv
,
1815 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1816 const void *private_data
, u8 private_data_len
)
1818 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1819 IBA_SET(CM_MRA_MESSAGE_MRAED
, mra_msg
, msg_mraed
);
1820 IBA_SET(CM_MRA_LOCAL_COMM_ID
, mra_msg
,
1821 be32_to_cpu(cm_id_priv
->id
.local_id
));
1822 IBA_SET(CM_MRA_REMOTE_COMM_ID
, mra_msg
,
1823 be32_to_cpu(cm_id_priv
->id
.remote_id
));
1824 IBA_SET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
, service_timeout
);
1826 if (private_data
&& private_data_len
)
1827 IBA_SET_MEM(CM_MRA_PRIVATE_DATA
, mra_msg
, private_data
,
1831 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1832 struct cm_id_private
*cm_id_priv
,
1833 enum ib_cm_rej_reason reason
, void *ari
,
1834 u8 ari_length
, const void *private_data
,
1835 u8 private_data_len
, enum ib_cm_state state
)
1837 lockdep_assert_held(&cm_id_priv
->lock
);
1839 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1840 IBA_SET(CM_REJ_REMOTE_COMM_ID
, rej_msg
,
1841 be32_to_cpu(cm_id_priv
->id
.remote_id
));
1844 case IB_CM_REQ_RCVD
:
1845 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
, be32_to_cpu(0));
1846 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REQ
);
1848 case IB_CM_MRA_REQ_SENT
:
1849 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1850 be32_to_cpu(cm_id_priv
->id
.local_id
));
1851 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REQ
);
1853 case IB_CM_REP_RCVD
:
1854 case IB_CM_MRA_REP_SENT
:
1855 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1856 be32_to_cpu(cm_id_priv
->id
.local_id
));
1857 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REP
);
1860 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1861 be32_to_cpu(cm_id_priv
->id
.local_id
));
1862 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
,
1863 CM_MSG_RESPONSE_OTHER
);
1867 IBA_SET(CM_REJ_REASON
, rej_msg
, reason
);
1868 if (ari
&& ari_length
) {
1869 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
, ari_length
);
1870 IBA_SET_MEM(CM_REJ_ARI
, rej_msg
, ari
, ari_length
);
1873 if (private_data
&& private_data_len
)
1874 IBA_SET_MEM(CM_REJ_PRIVATE_DATA
, rej_msg
, private_data
,
1878 static void cm_dup_req_handler(struct cm_work
*work
,
1879 struct cm_id_private
*cm_id_priv
)
1881 struct ib_mad_send_buf
*msg
= NULL
;
1884 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1885 counter
[CM_REQ_COUNTER
]);
1887 /* Quick state check to discard duplicate REQs. */
1888 spin_lock_irq(&cm_id_priv
->lock
);
1889 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
) {
1890 spin_unlock_irq(&cm_id_priv
->lock
);
1893 spin_unlock_irq(&cm_id_priv
->lock
);
1895 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1899 spin_lock_irq(&cm_id_priv
->lock
);
1900 switch (cm_id_priv
->id
.state
) {
1901 case IB_CM_MRA_REQ_SENT
:
1902 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1903 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1904 cm_id_priv
->private_data
,
1905 cm_id_priv
->private_data_len
);
1907 case IB_CM_TIMEWAIT
:
1908 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
,
1909 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0,
1915 spin_unlock_irq(&cm_id_priv
->lock
);
1917 ret
= ib_post_send_mad(msg
, NULL
);
1922 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1923 free
: cm_free_msg(msg
);
1926 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1927 struct cm_id_private
*cm_id_priv
)
1929 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1930 struct cm_timewait_info
*timewait_info
;
1931 struct cm_req_msg
*req_msg
;
1932 struct ib_cm_id
*cm_id
;
1934 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1936 /* Check for possible duplicate REQ. */
1937 spin_lock_irq(&cm
.lock
);
1938 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1939 if (timewait_info
) {
1940 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
1941 timewait_info
->work
.remote_id
);
1942 spin_unlock_irq(&cm
.lock
);
1943 if (cur_cm_id_priv
) {
1944 cm_dup_req_handler(work
, cur_cm_id_priv
);
1945 cm_deref_id(cur_cm_id_priv
);
1950 /* Check for stale connections. */
1951 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1952 if (timewait_info
) {
1953 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1954 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
1955 timewait_info
->work
.remote_id
);
1957 spin_unlock_irq(&cm
.lock
);
1958 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1959 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1961 if (cur_cm_id_priv
) {
1962 cm_id
= &cur_cm_id_priv
->id
;
1963 ib_send_cm_dreq(cm_id
, NULL
, 0);
1964 cm_deref_id(cur_cm_id_priv
);
1969 /* Find matching listen request. */
1970 listen_cm_id_priv
= cm_find_listen(
1971 cm_id_priv
->id
.device
,
1972 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
)));
1973 if (!listen_cm_id_priv
) {
1974 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1975 spin_unlock_irq(&cm
.lock
);
1976 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1977 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1981 refcount_inc(&listen_cm_id_priv
->refcount
);
1982 spin_unlock_irq(&cm
.lock
);
1983 return listen_cm_id_priv
;
1987 * Work-around for inter-subnet connections. If the LIDs are permissive,
1988 * we need to override the LID/SL data in the REQ with the LID information
1989 * in the work completion.
1991 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1993 if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL
, req_msg
)) {
1994 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID
,
1995 req_msg
)) == IB_LID_PERMISSIVE
) {
1996 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1997 be16_to_cpu(ib_lid_be16(wc
->slid
)));
1998 IBA_SET(CM_REQ_PRIMARY_SL
, req_msg
, wc
->sl
);
2001 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID
,
2002 req_msg
)) == IB_LID_PERMISSIVE
)
2003 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
2004 wc
->dlid_path_bits
);
2007 if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL
, req_msg
)) {
2008 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
,
2009 req_msg
)) == IB_LID_PERMISSIVE
) {
2010 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
2011 be16_to_cpu(ib_lid_be16(wc
->slid
)));
2012 IBA_SET(CM_REQ_ALTERNATE_SL
, req_msg
, wc
->sl
);
2015 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
,
2016 req_msg
)) == IB_LID_PERMISSIVE
)
2017 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
2018 wc
->dlid_path_bits
);
2022 static int cm_req_handler(struct cm_work
*work
)
2024 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
2025 struct cm_req_msg
*req_msg
;
2026 const struct ib_global_route
*grh
;
2027 const struct ib_gid_attr
*gid_attr
;
2030 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2033 cm_alloc_id_priv(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
2034 if (IS_ERR(cm_id_priv
))
2035 return PTR_ERR(cm_id_priv
);
2037 cm_id_priv
->id
.remote_id
=
2038 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID
, req_msg
));
2039 cm_id_priv
->id
.service_id
=
2040 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
2041 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
2042 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
2043 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
2044 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
));
2045 cm_id_priv
->max_cm_retries
= IBA_GET(CM_REQ_MAX_CM_RETRIES
, req_msg
);
2046 cm_id_priv
->remote_qpn
=
2047 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
));
2048 cm_id_priv
->initiator_depth
=
2049 IBA_GET(CM_REQ_RESPONDER_RESOURCES
, req_msg
);
2050 cm_id_priv
->responder_resources
=
2051 IBA_GET(CM_REQ_INITIATOR_DEPTH
, req_msg
);
2052 cm_id_priv
->path_mtu
= IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
2053 cm_id_priv
->pkey
= cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
2054 cm_id_priv
->sq_psn
= cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN
, req_msg
));
2055 cm_id_priv
->retry_count
= IBA_GET(CM_REQ_RETRY_COUNT
, req_msg
);
2056 cm_id_priv
->rnr_retry_count
= IBA_GET(CM_REQ_RNR_RETRY_COUNT
, req_msg
);
2057 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
2059 ret
= cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2060 work
->mad_recv_wc
->recv_buf
.grh
,
2064 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
2066 if (IS_ERR(cm_id_priv
->timewait_info
)) {
2067 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
2070 cm_id_priv
->timewait_info
->work
.remote_id
= cm_id_priv
->id
.remote_id
;
2071 cm_id_priv
->timewait_info
->remote_ca_guid
=
2072 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID
, req_msg
));
2073 cm_id_priv
->timewait_info
->remote_qpn
= cm_id_priv
->remote_qpn
;
2076 * Note that the ID pointer is not in the xarray at this point,
2077 * so this set is only visible to the local thread.
2079 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
2081 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
2082 if (!listen_cm_id_priv
) {
2083 pr_debug("%s: local_id %d, no listen_cm_id_priv\n", __func__
,
2084 be32_to_cpu(cm_id_priv
->id
.local_id
));
2085 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2090 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
2092 memset(&work
->path
[0], 0, sizeof(work
->path
[0]));
2093 if (cm_req_has_alt_path(req_msg
))
2094 memset(&work
->path
[1], 0, sizeof(work
->path
[1]));
2095 grh
= rdma_ah_read_grh(&cm_id_priv
->av
.ah_attr
);
2096 gid_attr
= grh
->sgid_attr
;
2099 rdma_protocol_roce(work
->port
->cm_dev
->ib_device
,
2100 work
->port
->port_num
)) {
2101 work
->path
[0].rec_type
=
2102 sa_conv_gid_to_pathrec_type(gid_attr
->gid_type
);
2104 cm_path_set_rec_type(
2105 work
->port
->cm_dev
->ib_device
, work
->port
->port_num
,
2107 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
,
2110 if (cm_req_has_alt_path(req_msg
))
2111 work
->path
[1].rec_type
= work
->path
[0].rec_type
;
2112 cm_format_paths_from_req(req_msg
, &work
->path
[0],
2114 if (cm_id_priv
->av
.ah_attr
.type
== RDMA_AH_ATTR_TYPE_ROCE
)
2115 sa_path_set_dmac(&work
->path
[0],
2116 cm_id_priv
->av
.ah_attr
.roce
.dmac
);
2117 work
->path
[0].hop_limit
= grh
->hop_limit
;
2118 ret
= cm_init_av_by_path(&work
->path
[0], gid_attr
, &cm_id_priv
->av
,
2123 err
= rdma_query_gid(work
->port
->cm_dev
->ib_device
,
2124 work
->port
->port_num
, 0,
2125 &work
->path
[0].sgid
);
2127 ib_send_cm_rej(&cm_id_priv
->id
, IB_CM_REJ_INVALID_GID
,
2130 ib_send_cm_rej(&cm_id_priv
->id
, IB_CM_REJ_INVALID_GID
,
2131 &work
->path
[0].sgid
,
2132 sizeof(work
->path
[0].sgid
),
2136 if (cm_req_has_alt_path(req_msg
)) {
2137 ret
= cm_init_av_by_path(&work
->path
[1], NULL
,
2138 &cm_id_priv
->alt_av
, cm_id_priv
);
2140 ib_send_cm_rej(&cm_id_priv
->id
,
2141 IB_CM_REJ_INVALID_ALT_GID
,
2142 &work
->path
[0].sgid
,
2143 sizeof(work
->path
[0].sgid
), NULL
, 0);
2148 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
2149 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
2150 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
2152 /* Now MAD handlers can see the new ID */
2153 spin_lock_irq(&cm_id_priv
->lock
);
2154 cm_finalize_id(cm_id_priv
);
2156 /* Refcount belongs to the event, pairs with cm_process_work() */
2157 refcount_inc(&cm_id_priv
->refcount
);
2158 atomic_inc(&cm_id_priv
->work_count
);
2159 spin_unlock_irq(&cm_id_priv
->lock
);
2160 cm_process_work(cm_id_priv
, work
);
2162 * Since this ID was just created and was not made visible to other MAD
2163 * handlers until the cm_finalize_id() above we know that the
2164 * cm_process_work() will deliver the event and the listen_cm_id
2165 * embedded in the event can be derefed here.
2167 cm_deref_id(listen_cm_id_priv
);
2171 cm_deref_id(listen_cm_id_priv
);
2173 ib_destroy_cm_id(&cm_id_priv
->id
);
2177 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
2178 struct cm_id_private
*cm_id_priv
,
2179 struct ib_cm_rep_param
*param
)
2181 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
2182 IBA_SET(CM_REP_LOCAL_COMM_ID
, rep_msg
,
2183 be32_to_cpu(cm_id_priv
->id
.local_id
));
2184 IBA_SET(CM_REP_REMOTE_COMM_ID
, rep_msg
,
2185 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2186 IBA_SET(CM_REP_STARTING_PSN
, rep_msg
, param
->starting_psn
);
2187 IBA_SET(CM_REP_RESPONDER_RESOURCES
, rep_msg
,
2188 param
->responder_resources
);
2189 IBA_SET(CM_REP_TARGET_ACK_DELAY
, rep_msg
,
2190 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
2191 IBA_SET(CM_REP_FAILOVER_ACCEPTED
, rep_msg
, param
->failover_accepted
);
2192 IBA_SET(CM_REP_RNR_RETRY_COUNT
, rep_msg
, param
->rnr_retry_count
);
2193 IBA_SET(CM_REP_LOCAL_CA_GUID
, rep_msg
,
2194 be64_to_cpu(cm_id_priv
->id
.device
->node_guid
));
2196 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
2197 IBA_SET(CM_REP_INITIATOR_DEPTH
, rep_msg
,
2198 param
->initiator_depth
);
2199 IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL
, rep_msg
,
2200 param
->flow_control
);
2201 IBA_SET(CM_REP_SRQ
, rep_msg
, param
->srq
);
2202 IBA_SET(CM_REP_LOCAL_QPN
, rep_msg
, param
->qp_num
);
2204 IBA_SET(CM_REP_SRQ
, rep_msg
, 1);
2205 IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER
, rep_msg
, param
->qp_num
);
2208 if (param
->private_data
&& param
->private_data_len
)
2209 IBA_SET_MEM(CM_REP_PRIVATE_DATA
, rep_msg
, param
->private_data
,
2210 param
->private_data_len
);
2213 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
2214 struct ib_cm_rep_param
*param
)
2216 struct cm_id_private
*cm_id_priv
;
2217 struct ib_mad_send_buf
*msg
;
2218 struct cm_rep_msg
*rep_msg
;
2219 unsigned long flags
;
2222 if (param
->private_data
&&
2223 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
2226 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2227 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2228 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
2229 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
2230 pr_debug("%s: local_comm_id %d, cm_id->state: %d\n", __func__
,
2231 be32_to_cpu(cm_id_priv
->id
.local_id
), cm_id
->state
);
2236 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2240 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
2241 cm_format_rep(rep_msg
, cm_id_priv
, param
);
2242 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2243 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
2245 ret
= ib_post_send_mad(msg
, NULL
);
2247 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2252 cm_id
->state
= IB_CM_REP_SENT
;
2253 cm_id_priv
->msg
= msg
;
2254 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
2255 cm_id_priv
->responder_resources
= param
->responder_resources
;
2256 cm_id_priv
->rq_psn
= cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN
, rep_msg
));
2257 WARN_ONCE(param
->qp_num
& 0xFF000000,
2258 "IBTA declares QPN to be 24 bits, but it is 0x%X\n",
2260 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
2262 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2265 EXPORT_SYMBOL(ib_send_cm_rep
);
2267 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
2268 struct cm_id_private
*cm_id_priv
,
2269 const void *private_data
,
2270 u8 private_data_len
)
2272 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
2273 IBA_SET(CM_RTU_LOCAL_COMM_ID
, rtu_msg
,
2274 be32_to_cpu(cm_id_priv
->id
.local_id
));
2275 IBA_SET(CM_RTU_REMOTE_COMM_ID
, rtu_msg
,
2276 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2278 if (private_data
&& private_data_len
)
2279 IBA_SET_MEM(CM_RTU_PRIVATE_DATA
, rtu_msg
, private_data
,
2283 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
2284 const void *private_data
,
2285 u8 private_data_len
)
2287 struct cm_id_private
*cm_id_priv
;
2288 struct ib_mad_send_buf
*msg
;
2289 unsigned long flags
;
2293 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
2296 data
= cm_copy_private_data(private_data
, private_data_len
);
2298 return PTR_ERR(data
);
2300 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2301 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2302 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
2303 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
2304 pr_debug("%s: local_id %d, cm_id->state %d\n", __func__
,
2305 be32_to_cpu(cm_id
->local_id
), cm_id
->state
);
2310 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2314 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
2315 private_data
, private_data_len
);
2317 ret
= ib_post_send_mad(msg
, NULL
);
2319 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2325 cm_id
->state
= IB_CM_ESTABLISHED
;
2326 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2327 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2330 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2334 EXPORT_SYMBOL(ib_send_cm_rtu
);
2336 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
2338 struct cm_rep_msg
*rep_msg
;
2339 struct ib_cm_rep_event_param
*param
;
2341 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2342 param
= &work
->cm_event
.param
.rep_rcvd
;
2343 param
->remote_ca_guid
=
2344 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID
, rep_msg
));
2345 param
->remote_qkey
= IBA_GET(CM_REP_LOCAL_Q_KEY
, rep_msg
);
2346 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
2347 param
->starting_psn
= IBA_GET(CM_REP_STARTING_PSN
, rep_msg
);
2348 param
->responder_resources
= IBA_GET(CM_REP_INITIATOR_DEPTH
, rep_msg
);
2349 param
->initiator_depth
= IBA_GET(CM_REP_RESPONDER_RESOURCES
, rep_msg
);
2350 param
->target_ack_delay
= IBA_GET(CM_REP_TARGET_ACK_DELAY
, rep_msg
);
2351 param
->failover_accepted
= IBA_GET(CM_REP_FAILOVER_ACCEPTED
, rep_msg
);
2352 param
->flow_control
= IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL
, rep_msg
);
2353 param
->rnr_retry_count
= IBA_GET(CM_REP_RNR_RETRY_COUNT
, rep_msg
);
2354 param
->srq
= IBA_GET(CM_REP_SRQ
, rep_msg
);
2355 work
->cm_event
.private_data
=
2356 IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA
, rep_msg
);
2359 static void cm_dup_rep_handler(struct cm_work
*work
)
2361 struct cm_id_private
*cm_id_priv
;
2362 struct cm_rep_msg
*rep_msg
;
2363 struct ib_mad_send_buf
*msg
= NULL
;
2366 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
2367 cm_id_priv
= cm_acquire_id(
2368 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
)),
2369 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
)));
2373 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2374 counter
[CM_REP_COUNTER
]);
2375 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
2379 spin_lock_irq(&cm_id_priv
->lock
);
2380 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
2381 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
2382 cm_id_priv
->private_data
,
2383 cm_id_priv
->private_data_len
);
2384 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
2385 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2386 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
2387 cm_id_priv
->private_data
,
2388 cm_id_priv
->private_data_len
);
2391 spin_unlock_irq(&cm_id_priv
->lock
);
2393 ret
= ib_post_send_mad(msg
, NULL
);
2398 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2399 free
: cm_free_msg(msg
);
2400 deref
: cm_deref_id(cm_id_priv
);
2403 static int cm_rep_handler(struct cm_work
*work
)
2405 struct cm_id_private
*cm_id_priv
;
2406 struct cm_rep_msg
*rep_msg
;
2408 struct cm_id_private
*cur_cm_id_priv
;
2409 struct ib_cm_id
*cm_id
;
2410 struct cm_timewait_info
*timewait_info
;
2412 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2413 cm_id_priv
= cm_acquire_id(
2414 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
)), 0);
2416 cm_dup_rep_handler(work
);
2417 pr_debug("%s: remote_comm_id %d, no cm_id_priv\n", __func__
,
2418 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2422 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
2424 spin_lock_irq(&cm_id_priv
->lock
);
2425 switch (cm_id_priv
->id
.state
) {
2426 case IB_CM_REQ_SENT
:
2427 case IB_CM_MRA_REQ_RCVD
:
2432 "%s: cm_id_priv->id.state: %d, local_comm_id %d, remote_comm_id %d\n",
2433 __func__
, cm_id_priv
->id
.state
,
2434 IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
),
2435 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2436 spin_unlock_irq(&cm_id_priv
->lock
);
2440 cm_id_priv
->timewait_info
->work
.remote_id
=
2441 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
));
2442 cm_id_priv
->timewait_info
->remote_ca_guid
=
2443 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID
, rep_msg
));
2444 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2446 spin_lock(&cm
.lock
);
2447 /* Check for duplicate REP. */
2448 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
2449 spin_unlock(&cm
.lock
);
2450 spin_unlock_irq(&cm_id_priv
->lock
);
2452 pr_debug("%s: Failed to insert remote id %d\n", __func__
,
2453 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2456 /* Check for a stale connection. */
2457 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
2458 if (timewait_info
) {
2459 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
2460 &cm
.remote_id_table
);
2461 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
2462 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2463 timewait_info
->work
.remote_id
);
2465 spin_unlock(&cm
.lock
);
2466 spin_unlock_irq(&cm_id_priv
->lock
);
2467 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
2468 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
2472 "%s: Stale connection. local_comm_id %d, remote_comm_id %d\n",
2473 __func__
, IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
),
2474 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2476 if (cur_cm_id_priv
) {
2477 cm_id
= &cur_cm_id_priv
->id
;
2478 ib_send_cm_dreq(cm_id
, NULL
, 0);
2479 cm_deref_id(cur_cm_id_priv
);
2484 spin_unlock(&cm
.lock
);
2486 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
2487 cm_id_priv
->id
.remote_id
=
2488 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
));
2489 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2490 cm_id_priv
->initiator_depth
=
2491 IBA_GET(CM_REP_RESPONDER_RESOURCES
, rep_msg
);
2492 cm_id_priv
->responder_resources
=
2493 IBA_GET(CM_REP_INITIATOR_DEPTH
, rep_msg
);
2494 cm_id_priv
->sq_psn
= cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN
, rep_msg
));
2495 cm_id_priv
->rnr_retry_count
= IBA_GET(CM_REP_RNR_RETRY_COUNT
, rep_msg
);
2496 cm_id_priv
->target_ack_delay
=
2497 IBA_GET(CM_REP_TARGET_ACK_DELAY
, rep_msg
);
2498 cm_id_priv
->av
.timeout
=
2499 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2500 cm_id_priv
->av
.timeout
- 1);
2501 cm_id_priv
->alt_av
.timeout
=
2502 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2503 cm_id_priv
->alt_av
.timeout
- 1);
2505 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2506 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2508 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2509 spin_unlock_irq(&cm_id_priv
->lock
);
2512 cm_process_work(cm_id_priv
, work
);
2514 cm_deref_id(cm_id_priv
);
2518 cm_deref_id(cm_id_priv
);
2522 static int cm_establish_handler(struct cm_work
*work
)
2524 struct cm_id_private
*cm_id_priv
;
2527 /* See comment in cm_establish about lookup. */
2528 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
2532 spin_lock_irq(&cm_id_priv
->lock
);
2533 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2534 spin_unlock_irq(&cm_id_priv
->lock
);
2538 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2539 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2541 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2542 spin_unlock_irq(&cm_id_priv
->lock
);
2545 cm_process_work(cm_id_priv
, work
);
2547 cm_deref_id(cm_id_priv
);
2550 cm_deref_id(cm_id_priv
);
2554 static int cm_rtu_handler(struct cm_work
*work
)
2556 struct cm_id_private
*cm_id_priv
;
2557 struct cm_rtu_msg
*rtu_msg
;
2560 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2561 cm_id_priv
= cm_acquire_id(
2562 cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID
, rtu_msg
)),
2563 cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID
, rtu_msg
)));
2567 work
->cm_event
.private_data
=
2568 IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA
, rtu_msg
);
2570 spin_lock_irq(&cm_id_priv
->lock
);
2571 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
2572 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
2573 spin_unlock_irq(&cm_id_priv
->lock
);
2574 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2575 counter
[CM_RTU_COUNTER
]);
2578 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
2580 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2581 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2583 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2584 spin_unlock_irq(&cm_id_priv
->lock
);
2587 cm_process_work(cm_id_priv
, work
);
2589 cm_deref_id(cm_id_priv
);
2592 cm_deref_id(cm_id_priv
);
2596 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
2597 struct cm_id_private
*cm_id_priv
,
2598 const void *private_data
,
2599 u8 private_data_len
)
2601 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
2602 cm_form_tid(cm_id_priv
));
2603 IBA_SET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
,
2604 be32_to_cpu(cm_id_priv
->id
.local_id
));
2605 IBA_SET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
,
2606 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2607 IBA_SET(CM_DREQ_REMOTE_QPN_EECN
, dreq_msg
,
2608 be32_to_cpu(cm_id_priv
->remote_qpn
));
2610 if (private_data
&& private_data_len
)
2611 IBA_SET_MEM(CM_DREQ_PRIVATE_DATA
, dreq_msg
, private_data
,
2615 static int cm_send_dreq_locked(struct cm_id_private
*cm_id_priv
,
2616 const void *private_data
, u8 private_data_len
)
2618 struct ib_mad_send_buf
*msg
;
2621 lockdep_assert_held(&cm_id_priv
->lock
);
2623 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2626 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2627 pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__
,
2628 be32_to_cpu(cm_id_priv
->id
.local_id
),
2629 cm_id_priv
->id
.state
);
2633 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2634 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2635 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2637 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2639 cm_enter_timewait(cm_id_priv
);
2643 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2644 private_data
, private_data_len
);
2645 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2646 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2648 ret
= ib_post_send_mad(msg
, NULL
);
2650 cm_enter_timewait(cm_id_priv
);
2655 cm_id_priv
->id
.state
= IB_CM_DREQ_SENT
;
2656 cm_id_priv
->msg
= msg
;
2660 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
, const void *private_data
,
2661 u8 private_data_len
)
2663 struct cm_id_private
*cm_id_priv
=
2664 container_of(cm_id
, struct cm_id_private
, id
);
2665 unsigned long flags
;
2668 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2669 ret
= cm_send_dreq_locked(cm_id_priv
, private_data
, private_data_len
);
2670 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2673 EXPORT_SYMBOL(ib_send_cm_dreq
);
2675 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2676 struct cm_id_private
*cm_id_priv
,
2677 const void *private_data
,
2678 u8 private_data_len
)
2680 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2681 IBA_SET(CM_DREP_LOCAL_COMM_ID
, drep_msg
,
2682 be32_to_cpu(cm_id_priv
->id
.local_id
));
2683 IBA_SET(CM_DREP_REMOTE_COMM_ID
, drep_msg
,
2684 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2686 if (private_data
&& private_data_len
)
2687 IBA_SET_MEM(CM_DREP_PRIVATE_DATA
, drep_msg
, private_data
,
2691 static int cm_send_drep_locked(struct cm_id_private
*cm_id_priv
,
2692 void *private_data
, u8 private_data_len
)
2694 struct ib_mad_send_buf
*msg
;
2697 lockdep_assert_held(&cm_id_priv
->lock
);
2699 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2702 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2704 "%s: local_id %d, cm_idcm_id->state(%d) != IB_CM_DREQ_RCVD\n",
2705 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
2706 cm_id_priv
->id
.state
);
2707 kfree(private_data
);
2711 cm_set_private_data(cm_id_priv
, private_data
, private_data_len
);
2712 cm_enter_timewait(cm_id_priv
);
2714 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2718 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2719 private_data
, private_data_len
);
2721 ret
= ib_post_send_mad(msg
, NULL
);
2729 int ib_send_cm_drep(struct ib_cm_id
*cm_id
, const void *private_data
,
2730 u8 private_data_len
)
2732 struct cm_id_private
*cm_id_priv
=
2733 container_of(cm_id
, struct cm_id_private
, id
);
2734 unsigned long flags
;
2738 data
= cm_copy_private_data(private_data
, private_data_len
);
2740 return PTR_ERR(data
);
2742 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2743 ret
= cm_send_drep_locked(cm_id_priv
, data
, private_data_len
);
2744 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2747 EXPORT_SYMBOL(ib_send_cm_drep
);
2749 static int cm_issue_drep(struct cm_port
*port
,
2750 struct ib_mad_recv_wc
*mad_recv_wc
)
2752 struct ib_mad_send_buf
*msg
= NULL
;
2753 struct cm_dreq_msg
*dreq_msg
;
2754 struct cm_drep_msg
*drep_msg
;
2757 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2761 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2762 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2764 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2765 IBA_SET(CM_DREP_REMOTE_COMM_ID
, drep_msg
,
2766 IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
));
2767 IBA_SET(CM_DREP_LOCAL_COMM_ID
, drep_msg
,
2768 IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
));
2770 ret
= ib_post_send_mad(msg
, NULL
);
2777 static int cm_dreq_handler(struct cm_work
*work
)
2779 struct cm_id_private
*cm_id_priv
;
2780 struct cm_dreq_msg
*dreq_msg
;
2781 struct ib_mad_send_buf
*msg
= NULL
;
2784 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2785 cm_id_priv
= cm_acquire_id(
2786 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
)),
2787 cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
)));
2789 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2790 counter
[CM_DREQ_COUNTER
]);
2791 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2793 "%s: no cm_id_priv, local_comm_id %d, remote_comm_id %d\n",
2794 __func__
, IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
),
2795 IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
));
2799 work
->cm_event
.private_data
=
2800 IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA
, dreq_msg
);
2802 spin_lock_irq(&cm_id_priv
->lock
);
2803 if (cm_id_priv
->local_qpn
!=
2804 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN
, dreq_msg
)))
2807 switch (cm_id_priv
->id
.state
) {
2808 case IB_CM_REP_SENT
:
2809 case IB_CM_DREQ_SENT
:
2810 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2812 case IB_CM_ESTABLISHED
:
2813 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2814 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2815 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2817 case IB_CM_MRA_REP_RCVD
:
2819 case IB_CM_TIMEWAIT
:
2820 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2821 counter
[CM_DREQ_COUNTER
]);
2822 msg
= cm_alloc_response_msg_no_ah(work
->port
, work
->mad_recv_wc
);
2826 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2827 cm_id_priv
->private_data
,
2828 cm_id_priv
->private_data_len
);
2829 spin_unlock_irq(&cm_id_priv
->lock
);
2831 if (cm_create_response_msg_ah(work
->port
, work
->mad_recv_wc
, msg
) ||
2832 ib_post_send_mad(msg
, NULL
))
2835 case IB_CM_DREQ_RCVD
:
2836 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2837 counter
[CM_DREQ_COUNTER
]);
2840 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
2841 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
2842 cm_id_priv
->id
.state
);
2845 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2846 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2847 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2849 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2850 spin_unlock_irq(&cm_id_priv
->lock
);
2853 cm_process_work(cm_id_priv
, work
);
2855 cm_deref_id(cm_id_priv
);
2858 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2859 deref
: cm_deref_id(cm_id_priv
);
2863 static int cm_drep_handler(struct cm_work
*work
)
2865 struct cm_id_private
*cm_id_priv
;
2866 struct cm_drep_msg
*drep_msg
;
2869 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2870 cm_id_priv
= cm_acquire_id(
2871 cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID
, drep_msg
)),
2872 cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID
, drep_msg
)));
2876 work
->cm_event
.private_data
=
2877 IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA
, drep_msg
);
2879 spin_lock_irq(&cm_id_priv
->lock
);
2880 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2881 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2882 spin_unlock_irq(&cm_id_priv
->lock
);
2885 cm_enter_timewait(cm_id_priv
);
2887 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2888 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2890 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2891 spin_unlock_irq(&cm_id_priv
->lock
);
2894 cm_process_work(cm_id_priv
, work
);
2896 cm_deref_id(cm_id_priv
);
2899 cm_deref_id(cm_id_priv
);
2903 static int cm_send_rej_locked(struct cm_id_private
*cm_id_priv
,
2904 enum ib_cm_rej_reason reason
, void *ari
,
2905 u8 ari_length
, const void *private_data
,
2906 u8 private_data_len
)
2908 enum ib_cm_state state
= cm_id_priv
->id
.state
;
2909 struct ib_mad_send_buf
*msg
;
2912 lockdep_assert_held(&cm_id_priv
->lock
);
2914 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2915 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2919 case IB_CM_REQ_SENT
:
2920 case IB_CM_MRA_REQ_RCVD
:
2921 case IB_CM_REQ_RCVD
:
2922 case IB_CM_MRA_REQ_SENT
:
2923 case IB_CM_REP_RCVD
:
2924 case IB_CM_MRA_REP_SENT
:
2925 cm_reset_to_idle(cm_id_priv
);
2926 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2929 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
, reason
,
2930 ari
, ari_length
, private_data
, private_data_len
,
2933 case IB_CM_REP_SENT
:
2934 case IB_CM_MRA_REP_RCVD
:
2935 cm_enter_timewait(cm_id_priv
);
2936 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2939 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
, reason
,
2940 ari
, ari_length
, private_data
, private_data_len
,
2944 pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__
,
2945 be32_to_cpu(cm_id_priv
->id
.local_id
),
2946 cm_id_priv
->id
.state
);
2950 ret
= ib_post_send_mad(msg
, NULL
);
2959 int ib_send_cm_rej(struct ib_cm_id
*cm_id
, enum ib_cm_rej_reason reason
,
2960 void *ari
, u8 ari_length
, const void *private_data
,
2961 u8 private_data_len
)
2963 struct cm_id_private
*cm_id_priv
=
2964 container_of(cm_id
, struct cm_id_private
, id
);
2965 unsigned long flags
;
2968 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2969 ret
= cm_send_rej_locked(cm_id_priv
, reason
, ari
, ari_length
,
2970 private_data
, private_data_len
);
2971 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2974 EXPORT_SYMBOL(ib_send_cm_rej
);
2976 static void cm_format_rej_event(struct cm_work
*work
)
2978 struct cm_rej_msg
*rej_msg
;
2979 struct ib_cm_rej_event_param
*param
;
2981 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2982 param
= &work
->cm_event
.param
.rej_rcvd
;
2983 param
->ari
= IBA_GET_MEM_PTR(CM_REJ_ARI
, rej_msg
);
2984 param
->ari_length
= IBA_GET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
);
2985 param
->reason
= IBA_GET(CM_REJ_REASON
, rej_msg
);
2986 work
->cm_event
.private_data
=
2987 IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA
, rej_msg
);
2990 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2992 struct cm_timewait_info
*timewait_info
;
2993 struct cm_id_private
*cm_id_priv
;
2996 remote_id
= cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID
, rej_msg
));
2998 if (IBA_GET(CM_REJ_REASON
, rej_msg
) == IB_CM_REJ_TIMEOUT
) {
2999 spin_lock_irq(&cm
.lock
);
3000 timewait_info
= cm_find_remote_id(
3001 *((__be64
*)IBA_GET_MEM_PTR(CM_REJ_ARI
, rej_msg
)),
3003 if (!timewait_info
) {
3004 spin_unlock_irq(&cm
.lock
);
3008 cm_acquire_id(timewait_info
->work
.local_id
, remote_id
);
3009 spin_unlock_irq(&cm
.lock
);
3010 } else if (IBA_GET(CM_REJ_MESSAGE_REJECTED
, rej_msg
) ==
3011 CM_MSG_RESPONSE_REQ
)
3012 cm_id_priv
= cm_acquire_id(
3013 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID
, rej_msg
)),
3016 cm_id_priv
= cm_acquire_id(
3017 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID
, rej_msg
)),
3023 static int cm_rej_handler(struct cm_work
*work
)
3025 struct cm_id_private
*cm_id_priv
;
3026 struct cm_rej_msg
*rej_msg
;
3029 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3030 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
3034 cm_format_rej_event(work
);
3036 spin_lock_irq(&cm_id_priv
->lock
);
3037 switch (cm_id_priv
->id
.state
) {
3038 case IB_CM_REQ_SENT
:
3039 case IB_CM_MRA_REQ_RCVD
:
3040 case IB_CM_REP_SENT
:
3041 case IB_CM_MRA_REP_RCVD
:
3042 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3044 case IB_CM_REQ_RCVD
:
3045 case IB_CM_MRA_REQ_SENT
:
3046 if (IBA_GET(CM_REJ_REASON
, rej_msg
) == IB_CM_REJ_STALE_CONN
)
3047 cm_enter_timewait(cm_id_priv
);
3049 cm_reset_to_idle(cm_id_priv
);
3051 case IB_CM_DREQ_SENT
:
3052 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3054 case IB_CM_REP_RCVD
:
3055 case IB_CM_MRA_REP_SENT
:
3056 cm_enter_timewait(cm_id_priv
);
3058 case IB_CM_ESTABLISHED
:
3059 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
3060 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
3061 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
3062 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
3064 cm_enter_timewait(cm_id_priv
);
3069 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
3070 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
3071 cm_id_priv
->id
.state
);
3072 spin_unlock_irq(&cm_id_priv
->lock
);
3077 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3079 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3080 spin_unlock_irq(&cm_id_priv
->lock
);
3083 cm_process_work(cm_id_priv
, work
);
3085 cm_deref_id(cm_id_priv
);
3088 cm_deref_id(cm_id_priv
);
3092 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
3094 const void *private_data
,
3095 u8 private_data_len
)
3097 struct cm_id_private
*cm_id_priv
;
3098 struct ib_mad_send_buf
*msg
;
3099 enum ib_cm_state cm_state
;
3100 enum ib_cm_lap_state lap_state
;
3101 enum cm_msg_response msg_response
;
3103 unsigned long flags
;
3106 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
3109 data
= cm_copy_private_data(private_data
, private_data_len
);
3111 return PTR_ERR(data
);
3113 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3115 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3116 switch(cm_id_priv
->id
.state
) {
3117 case IB_CM_REQ_RCVD
:
3118 cm_state
= IB_CM_MRA_REQ_SENT
;
3119 lap_state
= cm_id
->lap_state
;
3120 msg_response
= CM_MSG_RESPONSE_REQ
;
3122 case IB_CM_REP_RCVD
:
3123 cm_state
= IB_CM_MRA_REP_SENT
;
3124 lap_state
= cm_id
->lap_state
;
3125 msg_response
= CM_MSG_RESPONSE_REP
;
3127 case IB_CM_ESTABLISHED
:
3128 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
3129 cm_state
= cm_id
->state
;
3130 lap_state
= IB_CM_MRA_LAP_SENT
;
3131 msg_response
= CM_MSG_RESPONSE_OTHER
;
3136 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
3137 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
3138 cm_id_priv
->id
.state
);
3143 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
3144 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3148 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
3149 msg_response
, service_timeout
,
3150 private_data
, private_data_len
);
3151 ret
= ib_post_send_mad(msg
, NULL
);
3156 cm_id
->state
= cm_state
;
3157 cm_id
->lap_state
= lap_state
;
3158 cm_id_priv
->service_timeout
= service_timeout
;
3159 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
3160 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3163 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3167 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3172 EXPORT_SYMBOL(ib_send_cm_mra
);
3174 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
3176 switch (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
)) {
3177 case CM_MSG_RESPONSE_REQ
:
3178 return cm_acquire_id(
3179 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID
, mra_msg
)),
3181 case CM_MSG_RESPONSE_REP
:
3182 case CM_MSG_RESPONSE_OTHER
:
3183 return cm_acquire_id(
3184 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID
, mra_msg
)),
3185 cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID
, mra_msg
)));
3191 static int cm_mra_handler(struct cm_work
*work
)
3193 struct cm_id_private
*cm_id_priv
;
3194 struct cm_mra_msg
*mra_msg
;
3197 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3198 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
3202 work
->cm_event
.private_data
=
3203 IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA
, mra_msg
);
3204 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
3205 IBA_GET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
);
3206 timeout
= cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
)) +
3207 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
3209 spin_lock_irq(&cm_id_priv
->lock
);
3210 switch (cm_id_priv
->id
.state
) {
3211 case IB_CM_REQ_SENT
:
3212 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3213 CM_MSG_RESPONSE_REQ
||
3214 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
3215 cm_id_priv
->msg
, timeout
))
3217 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
3219 case IB_CM_REP_SENT
:
3220 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3221 CM_MSG_RESPONSE_REP
||
3222 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
3223 cm_id_priv
->msg
, timeout
))
3225 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
3227 case IB_CM_ESTABLISHED
:
3228 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3229 CM_MSG_RESPONSE_OTHER
||
3230 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
3231 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
3232 cm_id_priv
->msg
, timeout
)) {
3233 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
3234 atomic_long_inc(&work
->port
->
3235 counter_group
[CM_RECV_DUPLICATES
].
3236 counter
[CM_MRA_COUNTER
]);
3239 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
3241 case IB_CM_MRA_REQ_RCVD
:
3242 case IB_CM_MRA_REP_RCVD
:
3243 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3244 counter
[CM_MRA_COUNTER
]);
3247 pr_debug("%s local_id %d, cm_id_priv->id.state: %d\n",
3248 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
3249 cm_id_priv
->id
.state
);
3253 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
3254 cm_id_priv
->id
.state
;
3255 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3257 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3258 spin_unlock_irq(&cm_id_priv
->lock
);
3261 cm_process_work(cm_id_priv
, work
);
3263 cm_deref_id(cm_id_priv
);
3266 spin_unlock_irq(&cm_id_priv
->lock
);
3267 cm_deref_id(cm_id_priv
);
3271 static void cm_format_path_lid_from_lap(struct cm_lap_msg
*lap_msg
,
3272 struct sa_path_rec
*path
)
3276 if (path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
3277 sa_path_set_dlid(path
, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID
,
3279 sa_path_set_slid(path
, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID
,
3282 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3283 CM_LAP_ALTERNATE_LOCAL_PORT_GID
, lap_msg
));
3284 sa_path_set_dlid(path
, lid
);
3286 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3287 CM_LAP_ALTERNATE_REMOTE_PORT_GID
, lap_msg
));
3288 sa_path_set_slid(path
, lid
);
3292 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
3293 struct sa_path_rec
*path
,
3294 struct cm_lap_msg
*lap_msg
)
3296 path
->dgid
= *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID
, lap_msg
);
3298 *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID
, lap_msg
);
3300 cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL
, lap_msg
));
3301 path
->hop_limit
= IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT
, lap_msg
);
3302 path
->traffic_class
= IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS
, lap_msg
);
3303 path
->reversible
= 1;
3304 path
->pkey
= cm_id_priv
->pkey
;
3305 path
->sl
= IBA_GET(CM_LAP_ALTERNATE_SL
, lap_msg
);
3306 path
->mtu_selector
= IB_SA_EQ
;
3307 path
->mtu
= cm_id_priv
->path_mtu
;
3308 path
->rate_selector
= IB_SA_EQ
;
3309 path
->rate
= IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE
, lap_msg
);
3310 path
->packet_life_time_selector
= IB_SA_EQ
;
3311 path
->packet_life_time
=
3312 IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT
, lap_msg
);
3313 path
->packet_life_time
-= (path
->packet_life_time
> 0);
3314 cm_format_path_lid_from_lap(lap_msg
, path
);
3317 static int cm_lap_handler(struct cm_work
*work
)
3319 struct cm_id_private
*cm_id_priv
;
3320 struct cm_lap_msg
*lap_msg
;
3321 struct ib_cm_lap_event_param
*param
;
3322 struct ib_mad_send_buf
*msg
= NULL
;
3325 /* Currently Alternate path messages are not supported for
3328 if (rdma_protocol_roce(work
->port
->cm_dev
->ib_device
,
3329 work
->port
->port_num
))
3332 /* todo: verify LAP request and send reject APR if invalid. */
3333 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3334 cm_id_priv
= cm_acquire_id(
3335 cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID
, lap_msg
)),
3336 cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID
, lap_msg
)));
3340 param
= &work
->cm_event
.param
.lap_rcvd
;
3341 memset(&work
->path
[0], 0, sizeof(work
->path
[1]));
3342 cm_path_set_rec_type(work
->port
->cm_dev
->ib_device
,
3343 work
->port
->port_num
, &work
->path
[0],
3344 IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID
,
3346 param
->alternate_path
= &work
->path
[0];
3347 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
3348 work
->cm_event
.private_data
=
3349 IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA
, lap_msg
);
3351 spin_lock_irq(&cm_id_priv
->lock
);
3352 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
3355 switch (cm_id_priv
->id
.lap_state
) {
3356 case IB_CM_LAP_UNINIT
:
3357 case IB_CM_LAP_IDLE
:
3359 case IB_CM_MRA_LAP_SENT
:
3360 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3361 counter
[CM_LAP_COUNTER
]);
3362 msg
= cm_alloc_response_msg_no_ah(work
->port
, work
->mad_recv_wc
);
3366 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
3367 CM_MSG_RESPONSE_OTHER
,
3368 cm_id_priv
->service_timeout
,
3369 cm_id_priv
->private_data
,
3370 cm_id_priv
->private_data_len
);
3371 spin_unlock_irq(&cm_id_priv
->lock
);
3373 if (cm_create_response_msg_ah(work
->port
, work
->mad_recv_wc
, msg
) ||
3374 ib_post_send_mad(msg
, NULL
))
3377 case IB_CM_LAP_RCVD
:
3378 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3379 counter
[CM_LAP_COUNTER
]);
3385 ret
= cm_init_av_for_lap(work
->port
, work
->mad_recv_wc
->wc
,
3386 work
->mad_recv_wc
->recv_buf
.grh
,
3391 ret
= cm_init_av_by_path(param
->alternate_path
, NULL
,
3392 &cm_id_priv
->alt_av
, cm_id_priv
);
3396 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
3397 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
3398 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3400 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3401 spin_unlock_irq(&cm_id_priv
->lock
);
3404 cm_process_work(cm_id_priv
, work
);
3406 cm_deref_id(cm_id_priv
);
3409 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
3410 deref
: cm_deref_id(cm_id_priv
);
3414 static int cm_apr_handler(struct cm_work
*work
)
3416 struct cm_id_private
*cm_id_priv
;
3417 struct cm_apr_msg
*apr_msg
;
3420 /* Currently Alternate path messages are not supported for
3423 if (rdma_protocol_roce(work
->port
->cm_dev
->ib_device
,
3424 work
->port
->port_num
))
3427 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3428 cm_id_priv
= cm_acquire_id(
3429 cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID
, apr_msg
)),
3430 cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID
, apr_msg
)));
3432 return -EINVAL
; /* Unmatched reply. */
3434 work
->cm_event
.param
.apr_rcvd
.ap_status
=
3435 IBA_GET(CM_APR_AR_STATUS
, apr_msg
);
3436 work
->cm_event
.param
.apr_rcvd
.apr_info
=
3437 IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION
, apr_msg
);
3438 work
->cm_event
.param
.apr_rcvd
.info_len
=
3439 IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH
, apr_msg
);
3440 work
->cm_event
.private_data
=
3441 IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA
, apr_msg
);
3443 spin_lock_irq(&cm_id_priv
->lock
);
3444 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
3445 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
3446 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
3447 spin_unlock_irq(&cm_id_priv
->lock
);
3450 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
3451 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3452 cm_id_priv
->msg
= NULL
;
3454 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3456 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3457 spin_unlock_irq(&cm_id_priv
->lock
);
3460 cm_process_work(cm_id_priv
, work
);
3462 cm_deref_id(cm_id_priv
);
3465 cm_deref_id(cm_id_priv
);
3469 static int cm_timewait_handler(struct cm_work
*work
)
3471 struct cm_timewait_info
*timewait_info
;
3472 struct cm_id_private
*cm_id_priv
;
3475 timewait_info
= container_of(work
, struct cm_timewait_info
, work
);
3476 spin_lock_irq(&cm
.lock
);
3477 list_del(&timewait_info
->list
);
3478 spin_unlock_irq(&cm
.lock
);
3480 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
3481 timewait_info
->work
.remote_id
);
3485 spin_lock_irq(&cm_id_priv
->lock
);
3486 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
3487 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
3488 spin_unlock_irq(&cm_id_priv
->lock
);
3491 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3492 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3494 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3495 spin_unlock_irq(&cm_id_priv
->lock
);
3498 cm_process_work(cm_id_priv
, work
);
3500 cm_deref_id(cm_id_priv
);
3503 cm_deref_id(cm_id_priv
);
3507 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
3508 struct cm_id_private
*cm_id_priv
,
3509 struct ib_cm_sidr_req_param
*param
)
3511 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
3512 cm_form_tid(cm_id_priv
));
3513 IBA_SET(CM_SIDR_REQ_REQUESTID
, sidr_req_msg
,
3514 be32_to_cpu(cm_id_priv
->id
.local_id
));
3515 IBA_SET(CM_SIDR_REQ_PARTITION_KEY
, sidr_req_msg
,
3516 be16_to_cpu(param
->path
->pkey
));
3517 IBA_SET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
,
3518 be64_to_cpu(param
->service_id
));
3520 if (param
->private_data
&& param
->private_data_len
)
3521 IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA
, sidr_req_msg
,
3522 param
->private_data
, param
->private_data_len
);
3525 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
3526 struct ib_cm_sidr_req_param
*param
)
3528 struct cm_id_private
*cm_id_priv
;
3529 struct ib_mad_send_buf
*msg
;
3530 unsigned long flags
;
3533 if (!param
->path
|| (param
->private_data
&&
3534 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
3537 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3538 ret
= cm_init_av_by_path(param
->path
, param
->sgid_attr
,
3544 cm_id
->service_id
= param
->service_id
;
3545 cm_id
->service_mask
= ~cpu_to_be64(0);
3546 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
3547 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
3548 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3552 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
3554 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
3555 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
3557 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3558 if (cm_id
->state
== IB_CM_IDLE
)
3559 ret
= ib_post_send_mad(msg
, NULL
);
3564 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3568 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
3569 cm_id_priv
->msg
= msg
;
3570 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3574 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
3576 static void cm_format_sidr_req_event(struct cm_work
*work
,
3577 const struct cm_id_private
*rx_cm_id
,
3578 struct ib_cm_id
*listen_id
)
3580 struct cm_sidr_req_msg
*sidr_req_msg
;
3581 struct ib_cm_sidr_req_event_param
*param
;
3583 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3584 work
->mad_recv_wc
->recv_buf
.mad
;
3585 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
3586 param
->pkey
= IBA_GET(CM_SIDR_REQ_PARTITION_KEY
, sidr_req_msg
);
3587 param
->listen_id
= listen_id
;
3589 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
));
3590 param
->bth_pkey
= cm_get_bth_pkey(work
);
3591 param
->port
= work
->port
->port_num
;
3592 param
->sgid_attr
= rx_cm_id
->av
.ah_attr
.grh
.sgid_attr
;
3593 work
->cm_event
.private_data
=
3594 IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA
, sidr_req_msg
);
3597 static int cm_sidr_req_handler(struct cm_work
*work
)
3599 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
3600 struct cm_sidr_req_msg
*sidr_req_msg
;
3605 cm_alloc_id_priv(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3606 if (IS_ERR(cm_id_priv
))
3607 return PTR_ERR(cm_id_priv
);
3609 /* Record SGID/SLID and request ID for lookup. */
3610 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3611 work
->mad_recv_wc
->recv_buf
.mad
;
3613 cm_id_priv
->id
.remote_id
=
3614 cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID
, sidr_req_msg
));
3615 cm_id_priv
->id
.service_id
=
3616 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
));
3617 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3618 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3620 wc
= work
->mad_recv_wc
->wc
;
3621 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3622 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3623 ret
= cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3624 work
->mad_recv_wc
->recv_buf
.grh
,
3629 spin_lock_irq(&cm
.lock
);
3630 listen_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3631 if (listen_cm_id_priv
) {
3632 spin_unlock_irq(&cm
.lock
);
3633 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3634 counter
[CM_SIDR_REQ_COUNTER
]);
3635 goto out
; /* Duplicate message. */
3637 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3638 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
3639 cm_id_priv
->id
.service_id
);
3640 if (!listen_cm_id_priv
) {
3641 spin_unlock_irq(&cm
.lock
);
3642 ib_send_cm_sidr_rep(&cm_id_priv
->id
,
3643 &(struct ib_cm_sidr_rep_param
){
3644 .status
= IB_SIDR_UNSUPPORTED
});
3645 goto out
; /* No match. */
3647 refcount_inc(&listen_cm_id_priv
->refcount
);
3648 spin_unlock_irq(&cm
.lock
);
3650 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
3651 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
3654 * A SIDR ID does not need to be in the xarray since it does not receive
3655 * mads, is not placed in the remote_id or remote_qpn rbtree, and does
3656 * not enter timewait.
3659 cm_format_sidr_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
3660 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
3663 * A pointer to the listen_cm_id is held in the event, so this deref
3664 * must be after the event is delivered above.
3666 cm_deref_id(listen_cm_id_priv
);
3668 cm_destroy_id(&cm_id_priv
->id
, ret
);
3671 ib_destroy_cm_id(&cm_id_priv
->id
);
3675 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3676 struct cm_id_private
*cm_id_priv
,
3677 struct ib_cm_sidr_rep_param
*param
)
3679 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3681 IBA_SET(CM_SIDR_REP_REQUESTID
, sidr_rep_msg
,
3682 be32_to_cpu(cm_id_priv
->id
.remote_id
));
3683 IBA_SET(CM_SIDR_REP_STATUS
, sidr_rep_msg
, param
->status
);
3684 IBA_SET(CM_SIDR_REP_QPN
, sidr_rep_msg
, param
->qp_num
);
3685 IBA_SET(CM_SIDR_REP_SERVICEID
, sidr_rep_msg
,
3686 be64_to_cpu(cm_id_priv
->id
.service_id
));
3687 IBA_SET(CM_SIDR_REP_Q_KEY
, sidr_rep_msg
, param
->qkey
);
3689 if (param
->info
&& param
->info_length
)
3690 IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION
, sidr_rep_msg
,
3691 param
->info
, param
->info_length
);
3693 if (param
->private_data
&& param
->private_data_len
)
3694 IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA
, sidr_rep_msg
,
3695 param
->private_data
, param
->private_data_len
);
3698 static int cm_send_sidr_rep_locked(struct cm_id_private
*cm_id_priv
,
3699 struct ib_cm_sidr_rep_param
*param
)
3701 struct ib_mad_send_buf
*msg
;
3704 lockdep_assert_held(&cm_id_priv
->lock
);
3706 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3707 (param
->private_data
&&
3708 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3711 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_RCVD
)
3714 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3718 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3720 ret
= ib_post_send_mad(msg
, NULL
);
3725 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3726 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
)) {
3727 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3728 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
3733 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3734 struct ib_cm_sidr_rep_param
*param
)
3736 struct cm_id_private
*cm_id_priv
=
3737 container_of(cm_id
, struct cm_id_private
, id
);
3738 unsigned long flags
;
3741 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3742 ret
= cm_send_sidr_rep_locked(cm_id_priv
, param
);
3743 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3746 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3748 static void cm_format_sidr_rep_event(struct cm_work
*work
,
3749 const struct cm_id_private
*cm_id_priv
)
3751 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3752 struct ib_cm_sidr_rep_event_param
*param
;
3754 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3755 work
->mad_recv_wc
->recv_buf
.mad
;
3756 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3757 param
->status
= IBA_GET(CM_SIDR_REP_STATUS
, sidr_rep_msg
);
3758 param
->qkey
= IBA_GET(CM_SIDR_REP_Q_KEY
, sidr_rep_msg
);
3759 param
->qpn
= IBA_GET(CM_SIDR_REP_QPN
, sidr_rep_msg
);
3760 param
->info
= IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION
,
3762 param
->info_len
= IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH
,
3764 param
->sgid_attr
= cm_id_priv
->av
.ah_attr
.grh
.sgid_attr
;
3765 work
->cm_event
.private_data
=
3766 IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA
, sidr_rep_msg
);
3769 static int cm_sidr_rep_handler(struct cm_work
*work
)
3771 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3772 struct cm_id_private
*cm_id_priv
;
3774 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3775 work
->mad_recv_wc
->recv_buf
.mad
;
3776 cm_id_priv
= cm_acquire_id(
3777 cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID
, sidr_rep_msg
)), 0);
3779 return -EINVAL
; /* Unmatched reply. */
3781 spin_lock_irq(&cm_id_priv
->lock
);
3782 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3783 spin_unlock_irq(&cm_id_priv
->lock
);
3786 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3787 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3788 spin_unlock_irq(&cm_id_priv
->lock
);
3790 cm_format_sidr_rep_event(work
, cm_id_priv
);
3791 cm_process_work(cm_id_priv
, work
);
3794 cm_deref_id(cm_id_priv
);
3798 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3799 enum ib_wc_status wc_status
)
3801 struct cm_id_private
*cm_id_priv
;
3802 struct ib_cm_event cm_event
;
3803 enum ib_cm_state state
;
3806 memset(&cm_event
, 0, sizeof cm_event
);
3807 cm_id_priv
= msg
->context
[0];
3809 /* Discard old sends or ones without a response. */
3810 spin_lock_irq(&cm_id_priv
->lock
);
3811 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3812 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3815 pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
3816 state
, ib_wc_status_msg(wc_status
));
3818 case IB_CM_REQ_SENT
:
3819 case IB_CM_MRA_REQ_RCVD
:
3820 cm_reset_to_idle(cm_id_priv
);
3821 cm_event
.event
= IB_CM_REQ_ERROR
;
3823 case IB_CM_REP_SENT
:
3824 case IB_CM_MRA_REP_RCVD
:
3825 cm_reset_to_idle(cm_id_priv
);
3826 cm_event
.event
= IB_CM_REP_ERROR
;
3828 case IB_CM_DREQ_SENT
:
3829 cm_enter_timewait(cm_id_priv
);
3830 cm_event
.event
= IB_CM_DREQ_ERROR
;
3832 case IB_CM_SIDR_REQ_SENT
:
3833 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3834 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3839 spin_unlock_irq(&cm_id_priv
->lock
);
3840 cm_event
.param
.send_status
= wc_status
;
3842 /* No other events can occur on the cm_id at this point. */
3843 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3846 ib_destroy_cm_id(&cm_id_priv
->id
);
3849 spin_unlock_irq(&cm_id_priv
->lock
);
3853 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3854 struct ib_mad_send_wc
*mad_send_wc
)
3856 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3857 struct cm_port
*port
;
3860 port
= mad_agent
->context
;
3861 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3862 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3865 * If the send was in response to a received message (context[0] is not
3866 * set to a cm_id), and is not a REJ, then it is a send that was
3869 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3872 atomic_long_add(1 + msg
->retries
,
3873 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3875 atomic_long_add(msg
->retries
,
3876 &port
->counter_group
[CM_XMIT_RETRIES
].
3877 counter
[attr_index
]);
3879 switch (mad_send_wc
->status
) {
3881 case IB_WC_WR_FLUSH_ERR
:
3885 if (msg
->context
[0] && msg
->context
[1])
3886 cm_process_send_error(msg
, mad_send_wc
->status
);
3893 static void cm_work_handler(struct work_struct
*_work
)
3895 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3898 switch (work
->cm_event
.event
) {
3899 case IB_CM_REQ_RECEIVED
:
3900 ret
= cm_req_handler(work
);
3902 case IB_CM_MRA_RECEIVED
:
3903 ret
= cm_mra_handler(work
);
3905 case IB_CM_REJ_RECEIVED
:
3906 ret
= cm_rej_handler(work
);
3908 case IB_CM_REP_RECEIVED
:
3909 ret
= cm_rep_handler(work
);
3911 case IB_CM_RTU_RECEIVED
:
3912 ret
= cm_rtu_handler(work
);
3914 case IB_CM_USER_ESTABLISHED
:
3915 ret
= cm_establish_handler(work
);
3917 case IB_CM_DREQ_RECEIVED
:
3918 ret
= cm_dreq_handler(work
);
3920 case IB_CM_DREP_RECEIVED
:
3921 ret
= cm_drep_handler(work
);
3923 case IB_CM_SIDR_REQ_RECEIVED
:
3924 ret
= cm_sidr_req_handler(work
);
3926 case IB_CM_SIDR_REP_RECEIVED
:
3927 ret
= cm_sidr_rep_handler(work
);
3929 case IB_CM_LAP_RECEIVED
:
3930 ret
= cm_lap_handler(work
);
3932 case IB_CM_APR_RECEIVED
:
3933 ret
= cm_apr_handler(work
);
3935 case IB_CM_TIMEWAIT_EXIT
:
3936 ret
= cm_timewait_handler(work
);
3939 pr_debug("cm_event.event: 0x%x\n", work
->cm_event
.event
);
3947 static int cm_establish(struct ib_cm_id
*cm_id
)
3949 struct cm_id_private
*cm_id_priv
;
3950 struct cm_work
*work
;
3951 unsigned long flags
;
3953 struct cm_device
*cm_dev
;
3955 cm_dev
= ib_get_client_data(cm_id
->device
, &cm_client
);
3959 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3963 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3964 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3965 switch (cm_id
->state
)
3967 case IB_CM_REP_SENT
:
3968 case IB_CM_MRA_REP_RCVD
:
3969 cm_id
->state
= IB_CM_ESTABLISHED
;
3971 case IB_CM_ESTABLISHED
:
3975 pr_debug("%s: local_id %d, cm_id->state: %d\n", __func__
,
3976 be32_to_cpu(cm_id
->local_id
), cm_id
->state
);
3980 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3988 * The CM worker thread may try to destroy the cm_id before it
3989 * can execute this work item. To prevent potential deadlock,
3990 * we need to find the cm_id once we're in the context of the
3991 * worker thread, rather than holding a reference on it.
3993 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3994 work
->local_id
= cm_id
->local_id
;
3995 work
->remote_id
= cm_id
->remote_id
;
3996 work
->mad_recv_wc
= NULL
;
3997 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3999 /* Check if the device started its remove_one */
4000 spin_lock_irqsave(&cm
.lock
, flags
);
4001 if (!cm_dev
->going_down
) {
4002 queue_delayed_work(cm
.wq
, &work
->work
, 0);
4007 spin_unlock_irqrestore(&cm
.lock
, flags
);
4013 static int cm_migrate(struct ib_cm_id
*cm_id
)
4015 struct cm_id_private
*cm_id_priv
;
4016 struct cm_av tmp_av
;
4017 unsigned long flags
;
4018 int tmp_send_port_not_ready
;
4021 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
4022 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4023 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
4024 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
4025 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
4026 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
4027 /* Swap address vector */
4028 tmp_av
= cm_id_priv
->av
;
4029 cm_id_priv
->av
= cm_id_priv
->alt_av
;
4030 cm_id_priv
->alt_av
= tmp_av
;
4031 /* Swap port send ready state */
4032 tmp_send_port_not_ready
= cm_id_priv
->prim_send_port_not_ready
;
4033 cm_id_priv
->prim_send_port_not_ready
= cm_id_priv
->altr_send_port_not_ready
;
4034 cm_id_priv
->altr_send_port_not_ready
= tmp_send_port_not_ready
;
4037 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4042 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
4047 case IB_EVENT_COMM_EST
:
4048 ret
= cm_establish(cm_id
);
4050 case IB_EVENT_PATH_MIG
:
4051 ret
= cm_migrate(cm_id
);
4058 EXPORT_SYMBOL(ib_cm_notify
);
4060 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
4061 struct ib_mad_send_buf
*send_buf
,
4062 struct ib_mad_recv_wc
*mad_recv_wc
)
4064 struct cm_port
*port
= mad_agent
->context
;
4065 struct cm_work
*work
;
4066 enum ib_cm_event_type event
;
4067 bool alt_path
= false;
4072 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
4073 case CM_REQ_ATTR_ID
:
4074 alt_path
= cm_req_has_alt_path((struct cm_req_msg
*)
4075 mad_recv_wc
->recv_buf
.mad
);
4076 paths
= 1 + (alt_path
!= 0);
4077 event
= IB_CM_REQ_RECEIVED
;
4079 case CM_MRA_ATTR_ID
:
4080 event
= IB_CM_MRA_RECEIVED
;
4082 case CM_REJ_ATTR_ID
:
4083 event
= IB_CM_REJ_RECEIVED
;
4085 case CM_REP_ATTR_ID
:
4086 event
= IB_CM_REP_RECEIVED
;
4088 case CM_RTU_ATTR_ID
:
4089 event
= IB_CM_RTU_RECEIVED
;
4091 case CM_DREQ_ATTR_ID
:
4092 event
= IB_CM_DREQ_RECEIVED
;
4094 case CM_DREP_ATTR_ID
:
4095 event
= IB_CM_DREP_RECEIVED
;
4097 case CM_SIDR_REQ_ATTR_ID
:
4098 event
= IB_CM_SIDR_REQ_RECEIVED
;
4100 case CM_SIDR_REP_ATTR_ID
:
4101 event
= IB_CM_SIDR_REP_RECEIVED
;
4103 case CM_LAP_ATTR_ID
:
4105 event
= IB_CM_LAP_RECEIVED
;
4107 case CM_APR_ATTR_ID
:
4108 event
= IB_CM_APR_RECEIVED
;
4111 ib_free_recv_mad(mad_recv_wc
);
4115 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
4116 atomic_long_inc(&port
->counter_group
[CM_RECV
].
4117 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
4119 work
= kmalloc(struct_size(work
, path
, paths
), GFP_KERNEL
);
4121 ib_free_recv_mad(mad_recv_wc
);
4125 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
4126 work
->cm_event
.event
= event
;
4127 work
->mad_recv_wc
= mad_recv_wc
;
4130 /* Check if the device started its remove_one */
4131 spin_lock_irq(&cm
.lock
);
4132 if (!port
->cm_dev
->going_down
)
4133 queue_delayed_work(cm
.wq
, &work
->work
, 0);
4136 spin_unlock_irq(&cm
.lock
);
4140 ib_free_recv_mad(mad_recv_wc
);
4144 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
4145 struct ib_qp_attr
*qp_attr
,
4148 unsigned long flags
;
4151 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4152 switch (cm_id_priv
->id
.state
) {
4153 case IB_CM_REQ_SENT
:
4154 case IB_CM_MRA_REQ_RCVD
:
4155 case IB_CM_REQ_RCVD
:
4156 case IB_CM_MRA_REQ_SENT
:
4157 case IB_CM_REP_RCVD
:
4158 case IB_CM_MRA_REP_SENT
:
4159 case IB_CM_REP_SENT
:
4160 case IB_CM_MRA_REP_RCVD
:
4161 case IB_CM_ESTABLISHED
:
4162 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
4163 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
4164 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
4165 if (cm_id_priv
->responder_resources
)
4166 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
4167 IB_ACCESS_REMOTE_ATOMIC
;
4168 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
4169 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
4173 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
4174 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
4175 cm_id_priv
->id
.state
);
4179 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4183 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
4184 struct ib_qp_attr
*qp_attr
,
4187 unsigned long flags
;
4190 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4191 switch (cm_id_priv
->id
.state
) {
4192 case IB_CM_REQ_RCVD
:
4193 case IB_CM_MRA_REQ_SENT
:
4194 case IB_CM_REP_RCVD
:
4195 case IB_CM_MRA_REP_SENT
:
4196 case IB_CM_REP_SENT
:
4197 case IB_CM_MRA_REP_RCVD
:
4198 case IB_CM_ESTABLISHED
:
4199 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
4200 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
4201 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
4202 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
4203 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
4204 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
4205 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
4206 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
4207 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
4208 IB_QP_MIN_RNR_TIMER
;
4209 qp_attr
->max_dest_rd_atomic
=
4210 cm_id_priv
->responder_resources
;
4211 qp_attr
->min_rnr_timer
= 0;
4213 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
)) {
4214 *qp_attr_mask
|= IB_QP_ALT_PATH
;
4215 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
4216 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
4217 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
4218 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
4223 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
4224 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
4225 cm_id_priv
->id
.state
);
4229 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4233 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
4234 struct ib_qp_attr
*qp_attr
,
4237 unsigned long flags
;
4240 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4241 switch (cm_id_priv
->id
.state
) {
4242 /* Allow transition to RTS before sending REP */
4243 case IB_CM_REQ_RCVD
:
4244 case IB_CM_MRA_REQ_SENT
:
4246 case IB_CM_REP_RCVD
:
4247 case IB_CM_MRA_REP_SENT
:
4248 case IB_CM_REP_SENT
:
4249 case IB_CM_MRA_REP_RCVD
:
4250 case IB_CM_ESTABLISHED
:
4251 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
4252 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
4253 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
4254 switch (cm_id_priv
->qp_type
) {
4256 case IB_QPT_XRC_INI
:
4257 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
4258 IB_QP_MAX_QP_RD_ATOMIC
;
4259 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
4260 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
4261 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
4263 case IB_QPT_XRC_TGT
:
4264 *qp_attr_mask
|= IB_QP_TIMEOUT
;
4265 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
4270 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
)) {
4271 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
4272 qp_attr
->path_mig_state
= IB_MIG_REARM
;
4275 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
4276 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
4277 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
4278 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
4279 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
4280 qp_attr
->path_mig_state
= IB_MIG_REARM
;
4285 pr_debug("%s: local_id %d, cm_id_priv->id.state: %d\n",
4286 __func__
, be32_to_cpu(cm_id_priv
->id
.local_id
),
4287 cm_id_priv
->id
.state
);
4291 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4295 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
4296 struct ib_qp_attr
*qp_attr
,
4299 struct cm_id_private
*cm_id_priv
;
4302 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
4303 switch (qp_attr
->qp_state
) {
4305 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4308 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4311 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4319 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
4321 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
4324 struct cm_counter_group
*group
;
4325 struct cm_counter_attribute
*cm_attr
;
4327 group
= container_of(obj
, struct cm_counter_group
, obj
);
4328 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
4330 return sprintf(buf
, "%ld\n",
4331 atomic_long_read(&group
->counter
[cm_attr
->index
]));
4334 static const struct sysfs_ops cm_counter_ops
= {
4335 .show
= cm_show_counter
4338 static struct kobj_type cm_counter_obj_type
= {
4339 .sysfs_ops
= &cm_counter_ops
,
4340 .default_attrs
= cm_counter_default_attrs
4343 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
4347 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
4350 struct class cm_class
= {
4351 .owner
= THIS_MODULE
,
4352 .name
= "infiniband_cm",
4353 .devnode
= cm_devnode
,
4355 EXPORT_SYMBOL(cm_class
);
4357 static int cm_create_port_fs(struct cm_port
*port
)
4361 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
4362 ret
= ib_port_register_module_stat(port
->cm_dev
->ib_device
,
4364 &port
->counter_group
[i
].obj
,
4365 &cm_counter_obj_type
,
4366 counter_group_names
[i
]);
4375 ib_port_unregister_module_stat(&port
->counter_group
[i
].obj
);
4380 static void cm_remove_port_fs(struct cm_port
*port
)
4384 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
4385 ib_port_unregister_module_stat(&port
->counter_group
[i
].obj
);
4389 static void cm_add_one(struct ib_device
*ib_device
)
4391 struct cm_device
*cm_dev
;
4392 struct cm_port
*port
;
4393 struct ib_mad_reg_req reg_req
= {
4394 .mgmt_class
= IB_MGMT_CLASS_CM
,
4395 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
4397 struct ib_port_modify port_modify
= {
4398 .set_port_cap_mask
= IB_PORT_CM_SUP
4400 unsigned long flags
;
4405 cm_dev
= kzalloc(struct_size(cm_dev
, port
, ib_device
->phys_port_cnt
),
4410 cm_dev
->ib_device
= ib_device
;
4411 cm_dev
->ack_delay
= ib_device
->attrs
.local_ca_ack_delay
;
4412 cm_dev
->going_down
= 0;
4414 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
4415 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
4416 if (!rdma_cap_ib_cm(ib_device
, i
))
4419 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
4423 cm_dev
->port
[i
-1] = port
;
4424 port
->cm_dev
= cm_dev
;
4427 INIT_LIST_HEAD(&port
->cm_priv_prim_list
);
4428 INIT_LIST_HEAD(&port
->cm_priv_altr_list
);
4430 ret
= cm_create_port_fs(port
);
4434 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
4442 if (IS_ERR(port
->mad_agent
))
4445 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
4455 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
4457 write_lock_irqsave(&cm
.device_lock
, flags
);
4458 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
4459 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4463 ib_unregister_mad_agent(port
->mad_agent
);
4465 cm_remove_port_fs(port
);
4467 port_modify
.set_port_cap_mask
= 0;
4468 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
4471 if (!rdma_cap_ib_cm(ib_device
, i
))
4474 port
= cm_dev
->port
[i
-1];
4475 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4476 ib_unregister_mad_agent(port
->mad_agent
);
4477 cm_remove_port_fs(port
);
4484 static void cm_remove_one(struct ib_device
*ib_device
, void *client_data
)
4486 struct cm_device
*cm_dev
= client_data
;
4487 struct cm_port
*port
;
4488 struct cm_id_private
*cm_id_priv
;
4489 struct ib_mad_agent
*cur_mad_agent
;
4490 struct ib_port_modify port_modify
= {
4491 .clr_port_cap_mask
= IB_PORT_CM_SUP
4493 unsigned long flags
;
4499 write_lock_irqsave(&cm
.device_lock
, flags
);
4500 list_del(&cm_dev
->list
);
4501 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4503 spin_lock_irq(&cm
.lock
);
4504 cm_dev
->going_down
= 1;
4505 spin_unlock_irq(&cm
.lock
);
4507 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
4508 if (!rdma_cap_ib_cm(ib_device
, i
))
4511 port
= cm_dev
->port
[i
-1];
4512 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4513 /* Mark all the cm_id's as not valid */
4514 spin_lock_irq(&cm
.lock
);
4515 list_for_each_entry(cm_id_priv
, &port
->cm_priv_altr_list
, altr_list
)
4516 cm_id_priv
->altr_send_port_not_ready
= 1;
4517 list_for_each_entry(cm_id_priv
, &port
->cm_priv_prim_list
, prim_list
)
4518 cm_id_priv
->prim_send_port_not_ready
= 1;
4519 spin_unlock_irq(&cm
.lock
);
4521 * We flush the queue here after the going_down set, this
4522 * verify that no new works will be queued in the recv handler,
4523 * after that we can call the unregister_mad_agent
4525 flush_workqueue(cm
.wq
);
4526 spin_lock_irq(&cm
.state_lock
);
4527 cur_mad_agent
= port
->mad_agent
;
4528 port
->mad_agent
= NULL
;
4529 spin_unlock_irq(&cm
.state_lock
);
4530 ib_unregister_mad_agent(cur_mad_agent
);
4531 cm_remove_port_fs(port
);
4538 static int __init
ib_cm_init(void)
4542 INIT_LIST_HEAD(&cm
.device_list
);
4543 rwlock_init(&cm
.device_lock
);
4544 spin_lock_init(&cm
.lock
);
4545 spin_lock_init(&cm
.state_lock
);
4546 cm
.listen_service_table
= RB_ROOT
;
4547 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
4548 cm
.remote_id_table
= RB_ROOT
;
4549 cm
.remote_qp_table
= RB_ROOT
;
4550 cm
.remote_sidr_table
= RB_ROOT
;
4551 xa_init_flags(&cm
.local_id_table
, XA_FLAGS_ALLOC
| XA_FLAGS_LOCK_IRQ
);
4552 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
4553 INIT_LIST_HEAD(&cm
.timewait_list
);
4555 ret
= class_register(&cm_class
);
4561 cm
.wq
= alloc_workqueue("ib_cm", 0, 1);
4567 ret
= ib_register_client(&cm_client
);
4573 destroy_workqueue(cm
.wq
);
4575 class_unregister(&cm_class
);
4580 static void __exit
ib_cm_cleanup(void)
4582 struct cm_timewait_info
*timewait_info
, *tmp
;
4584 spin_lock_irq(&cm
.lock
);
4585 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
4586 cancel_delayed_work(&timewait_info
->work
.work
);
4587 spin_unlock_irq(&cm
.lock
);
4589 ib_unregister_client(&cm_client
);
4590 destroy_workqueue(cm
.wq
);
4592 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
4593 list_del(&timewait_info
->list
);
4594 kfree(timewait_info
);
4597 class_unregister(&cm_class
);
4598 WARN_ON(!xa_empty(&cm
.local_id_table
));
4601 module_init(ib_cm_init
);
4602 module_exit(ib_cm_cleanup
);