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>
28 #include <rdma/ib_sysfs.h>
30 #include "core_priv.h"
33 MODULE_AUTHOR("Sean Hefty");
34 MODULE_DESCRIPTION("InfiniBand CM");
35 MODULE_LICENSE("Dual BSD/GPL");
37 #define CM_DESTROY_ID_WAIT_TIMEOUT 10000 /* msecs */
38 #define CM_DIRECT_RETRY_CTX ((void *) 1UL)
40 static const char * const ibcm_rej_reason_strs
[] = {
41 [IB_CM_REJ_NO_QP
] = "no QP",
42 [IB_CM_REJ_NO_EEC
] = "no EEC",
43 [IB_CM_REJ_NO_RESOURCES
] = "no resources",
44 [IB_CM_REJ_TIMEOUT
] = "timeout",
45 [IB_CM_REJ_UNSUPPORTED
] = "unsupported",
46 [IB_CM_REJ_INVALID_COMM_ID
] = "invalid comm ID",
47 [IB_CM_REJ_INVALID_COMM_INSTANCE
] = "invalid comm instance",
48 [IB_CM_REJ_INVALID_SERVICE_ID
] = "invalid service ID",
49 [IB_CM_REJ_INVALID_TRANSPORT_TYPE
] = "invalid transport type",
50 [IB_CM_REJ_STALE_CONN
] = "stale conn",
51 [IB_CM_REJ_RDC_NOT_EXIST
] = "RDC not exist",
52 [IB_CM_REJ_INVALID_GID
] = "invalid GID",
53 [IB_CM_REJ_INVALID_LID
] = "invalid LID",
54 [IB_CM_REJ_INVALID_SL
] = "invalid SL",
55 [IB_CM_REJ_INVALID_TRAFFIC_CLASS
] = "invalid traffic class",
56 [IB_CM_REJ_INVALID_HOP_LIMIT
] = "invalid hop limit",
57 [IB_CM_REJ_INVALID_PACKET_RATE
] = "invalid packet rate",
58 [IB_CM_REJ_INVALID_ALT_GID
] = "invalid alt GID",
59 [IB_CM_REJ_INVALID_ALT_LID
] = "invalid alt LID",
60 [IB_CM_REJ_INVALID_ALT_SL
] = "invalid alt SL",
61 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS
] = "invalid alt traffic class",
62 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT
] = "invalid alt hop limit",
63 [IB_CM_REJ_INVALID_ALT_PACKET_RATE
] = "invalid alt packet rate",
64 [IB_CM_REJ_PORT_CM_REDIRECT
] = "port CM redirect",
65 [IB_CM_REJ_PORT_REDIRECT
] = "port redirect",
66 [IB_CM_REJ_INVALID_MTU
] = "invalid MTU",
67 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES
] = "insufficient resp resources",
68 [IB_CM_REJ_CONSUMER_DEFINED
] = "consumer defined",
69 [IB_CM_REJ_INVALID_RNR_RETRY
] = "invalid RNR retry",
70 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
] = "duplicate local comm ID",
71 [IB_CM_REJ_INVALID_CLASS_VERSION
] = "invalid class version",
72 [IB_CM_REJ_INVALID_FLOW_LABEL
] = "invalid flow label",
73 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL
] = "invalid alt flow label",
74 [IB_CM_REJ_VENDOR_OPTION_NOT_SUPPORTED
] =
75 "vendor option is not supported",
78 const char *__attribute_const__
ibcm_reject_msg(int reason
)
80 size_t index
= reason
;
82 if (index
< ARRAY_SIZE(ibcm_rej_reason_strs
) &&
83 ibcm_rej_reason_strs
[index
])
84 return ibcm_rej_reason_strs
[index
];
86 return "unrecognized reason";
88 EXPORT_SYMBOL(ibcm_reject_msg
);
92 static int cm_add_one(struct ib_device
*device
);
93 static void cm_remove_one(struct ib_device
*device
, void *client_data
);
94 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
95 struct cm_work
*work
);
96 static int cm_send_sidr_rep_locked(struct cm_id_private
*cm_id_priv
,
97 struct ib_cm_sidr_rep_param
*param
);
98 static void cm_issue_dreq(struct cm_id_private
*cm_id_priv
);
99 static int cm_send_drep_locked(struct cm_id_private
*cm_id_priv
,
100 void *private_data
, u8 private_data_len
);
101 static int cm_send_rej_locked(struct cm_id_private
*cm_id_priv
,
102 enum ib_cm_rej_reason reason
, void *ari
,
103 u8 ari_length
, const void *private_data
,
104 u8 private_data_len
);
106 static struct ib_client cm_client
= {
109 .remove
= cm_remove_one
112 static struct ib_cm
{
114 struct list_head device_list
;
115 rwlock_t device_lock
;
116 struct rb_root listen_service_table
;
117 u64 listen_service_id
;
118 /* struct rb_root peer_service_table; todo: fix peer to peer */
119 struct rb_root remote_qp_table
;
120 struct rb_root remote_id_table
;
121 struct rb_root remote_sidr_table
;
122 struct xarray local_id_table
;
124 __be32 random_id_operand
;
125 struct list_head timewait_list
;
126 struct workqueue_struct
*wq
;
129 /* Counter indexes ordered by attribute ID */
143 CM_ATTR_ID_OFFSET
= 0x0010,
154 struct cm_counter_attribute
{
155 struct ib_port_attribute attr
;
156 unsigned short group
;
157 unsigned short index
;
161 struct cm_device
*cm_dev
;
162 struct ib_mad_agent
*mad_agent
;
164 atomic_long_t counters
[CM_COUNTER_GROUPS
][CM_ATTR_COUNT
];
169 struct list_head list
;
170 spinlock_t mad_agent_lock
;
171 struct ib_device
*ib_device
;
174 struct cm_port
*port
[];
178 struct cm_port
*port
;
179 struct rdma_ah_attr ah_attr
;
186 struct delayed_work work
;
187 struct list_head list
;
188 struct cm_port
*port
;
189 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
190 __be32 local_id
; /* Established / timewait */
192 struct ib_cm_event cm_event
;
193 struct sa_path_rec path
[];
196 struct cm_timewait_info
{
198 struct list_head list
;
199 struct rb_node remote_qp_node
;
200 struct rb_node remote_id_node
;
201 __be64 remote_ca_guid
;
203 u8 inserted_remote_qp
;
204 u8 inserted_remote_id
;
207 struct cm_id_private
{
210 struct rb_node service_node
;
211 struct rb_node sidr_id_node
;
213 spinlock_t lock
; /* Do not acquire inside cm.lock */
214 struct completion comp
;
216 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
217 * Protected by the cm.lock spinlock.
219 int listen_sharecount
;
222 struct ib_mad_send_buf
*msg
;
223 struct cm_timewait_info
*timewait_info
;
224 /* todo: use alternate port on send failure */
232 enum ib_qp_type qp_type
;
236 enum ib_mtu path_mtu
;
240 u8 responder_resources
;
247 struct list_head work_list
;
250 struct rdma_ucm_ece ece
;
253 static void cm_dev_release(struct kref
*kref
)
255 struct cm_device
*cm_dev
= container_of(kref
, struct cm_device
, kref
);
258 rdma_for_each_port(cm_dev
->ib_device
, i
)
259 kfree(cm_dev
->port
[i
- 1]);
264 static void cm_device_put(struct cm_device
*cm_dev
)
266 kref_put(&cm_dev
->kref
, cm_dev_release
);
269 static void cm_work_handler(struct work_struct
*work
);
271 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
273 if (refcount_dec_and_test(&cm_id_priv
->refcount
))
274 complete(&cm_id_priv
->comp
);
277 static struct ib_mad_send_buf
*cm_alloc_msg(struct cm_id_private
*cm_id_priv
)
279 struct ib_mad_agent
*mad_agent
;
280 struct ib_mad_send_buf
*m
;
283 lockdep_assert_held(&cm_id_priv
->lock
);
285 if (!cm_id_priv
->av
.port
)
286 return ERR_PTR(-EINVAL
);
288 spin_lock(&cm_id_priv
->av
.port
->cm_dev
->mad_agent_lock
);
289 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
291 m
= ERR_PTR(-EINVAL
);
295 ah
= rdma_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
, 0);
301 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
302 cm_id_priv
->av
.pkey_index
,
303 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
305 IB_MGMT_BASE_VERSION
);
307 rdma_destroy_ah(ah
, 0);
314 spin_unlock(&cm_id_priv
->av
.port
->cm_dev
->mad_agent_lock
);
318 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
321 rdma_destroy_ah(msg
->ah
, 0);
322 ib_free_send_mad(msg
);
325 static struct ib_mad_send_buf
*
326 cm_alloc_priv_msg(struct cm_id_private
*cm_id_priv
, enum ib_cm_state state
)
328 struct ib_mad_send_buf
*msg
;
330 lockdep_assert_held(&cm_id_priv
->lock
);
332 msg
= cm_alloc_msg(cm_id_priv
);
336 cm_id_priv
->msg
= msg
;
337 refcount_inc(&cm_id_priv
->refcount
);
338 msg
->context
[0] = cm_id_priv
;
339 msg
->context
[1] = (void *) (unsigned long) state
;
341 msg
->retries
= cm_id_priv
->max_cm_retries
;
342 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
347 static void cm_free_priv_msg(struct ib_mad_send_buf
*msg
)
349 struct cm_id_private
*cm_id_priv
= msg
->context
[0];
351 lockdep_assert_held(&cm_id_priv
->lock
);
353 if (!WARN_ON(cm_id_priv
->msg
!= msg
))
354 cm_id_priv
->msg
= NULL
;
357 rdma_destroy_ah(msg
->ah
, 0);
358 cm_deref_id(cm_id_priv
);
359 ib_free_send_mad(msg
);
362 static struct ib_mad_send_buf
*
363 cm_alloc_response_msg_no_ah(struct cm_port
*port
,
364 struct ib_mad_recv_wc
*mad_recv_wc
,
367 struct ib_mad_send_buf
*m
;
369 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
370 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
371 GFP_ATOMIC
, IB_MGMT_BASE_VERSION
);
373 m
->context
[0] = direct_retry
? CM_DIRECT_RETRY_CTX
: NULL
;
378 static int cm_create_response_msg_ah(struct cm_port
*port
,
379 struct ib_mad_recv_wc
*mad_recv_wc
,
380 struct ib_mad_send_buf
*msg
)
384 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
385 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
393 static int cm_alloc_response_msg(struct cm_port
*port
,
394 struct ib_mad_recv_wc
*mad_recv_wc
,
396 struct ib_mad_send_buf
**msg
)
398 struct ib_mad_send_buf
*m
;
401 m
= cm_alloc_response_msg_no_ah(port
, mad_recv_wc
, direct_retry
);
405 ret
= cm_create_response_msg_ah(port
, mad_recv_wc
, m
);
415 static void *cm_copy_private_data(const void *private_data
, u8 private_data_len
)
419 if (!private_data
|| !private_data_len
)
422 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
424 return ERR_PTR(-ENOMEM
);
429 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
430 void *private_data
, u8 private_data_len
)
432 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
433 kfree(cm_id_priv
->private_data
);
435 cm_id_priv
->private_data
= private_data
;
436 cm_id_priv
->private_data_len
= private_data_len
;
439 static void cm_set_av_port(struct cm_av
*av
, struct cm_port
*port
)
441 struct cm_port
*old_port
= av
->port
;
443 if (old_port
== port
)
448 cm_device_put(old_port
->cm_dev
);
450 kref_get(&port
->cm_dev
->kref
);
453 static void cm_init_av_for_lap(struct cm_port
*port
, struct ib_wc
*wc
,
454 struct rdma_ah_attr
*ah_attr
, struct cm_av
*av
)
456 cm_set_av_port(av
, port
);
457 av
->pkey_index
= wc
->pkey_index
;
458 rdma_move_ah_attr(&av
->ah_attr
, ah_attr
);
461 static int cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
462 struct ib_grh
*grh
, struct cm_av
*av
)
464 cm_set_av_port(av
, port
);
465 av
->pkey_index
= wc
->pkey_index
;
466 return ib_init_ah_attr_from_wc(port
->cm_dev
->ib_device
,
471 static struct cm_port
*
472 get_cm_port_from_path(struct sa_path_rec
*path
, const struct ib_gid_attr
*attr
)
474 struct cm_device
*cm_dev
;
475 struct cm_port
*port
= NULL
;
479 read_lock_irqsave(&cm
.device_lock
, flags
);
480 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
481 if (cm_dev
->ib_device
== attr
->device
) {
482 port
= cm_dev
->port
[attr
->port_num
- 1];
486 read_unlock_irqrestore(&cm
.device_lock
, flags
);
488 /* SGID attribute can be NULL in following
490 * (a) Alternative path
491 * (b) IB link layer without GRH
492 * (c) LAP send messages
494 read_lock_irqsave(&cm
.device_lock
, flags
);
495 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
496 attr
= rdma_find_gid(cm_dev
->ib_device
,
498 sa_conv_pathrec_to_gid_type(path
),
501 port
= cm_dev
->port
[attr
->port_num
- 1];
505 read_unlock_irqrestore(&cm
.device_lock
, flags
);
507 rdma_put_gid_attr(attr
);
512 static int cm_init_av_by_path(struct sa_path_rec
*path
,
513 const struct ib_gid_attr
*sgid_attr
,
516 struct rdma_ah_attr new_ah_attr
;
517 struct cm_device
*cm_dev
;
518 struct cm_port
*port
;
521 port
= get_cm_port_from_path(path
, sgid_attr
);
524 cm_dev
= port
->cm_dev
;
526 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
527 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
531 cm_set_av_port(av
, port
);
534 * av->ah_attr might be initialized based on wc or during
535 * request processing time which might have reference to sgid_attr.
536 * So initialize a new ah_attr on stack.
537 * If initialization fails, old ah_attr is used for sending any
538 * responses. If initialization is successful, than new ah_attr
539 * is used by overwriting the old one. So that right ah_attr
540 * can be used to return an error response.
542 ret
= ib_init_ah_attr_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
543 &new_ah_attr
, sgid_attr
);
547 av
->timeout
= path
->packet_life_time
+ 1;
548 rdma_move_ah_attr(&av
->ah_attr
, &new_ah_attr
);
552 /* Move av created by cm_init_av_by_path(), so av.dgid is not moved */
553 static void cm_move_av_from_path(struct cm_av
*dest
, struct cm_av
*src
)
555 cm_set_av_port(dest
, src
->port
);
556 cm_set_av_port(src
, NULL
);
557 dest
->pkey_index
= src
->pkey_index
;
558 rdma_move_ah_attr(&dest
->ah_attr
, &src
->ah_attr
);
559 dest
->timeout
= src
->timeout
;
562 static void cm_destroy_av(struct cm_av
*av
)
564 rdma_destroy_ah_attr(&av
->ah_attr
);
565 cm_set_av_port(av
, NULL
);
568 static u32
cm_local_id(__be32 local_id
)
570 return (__force u32
) (local_id
^ cm
.random_id_operand
);
573 static struct cm_id_private
*cm_acquire_id(__be32 local_id
, __be32 remote_id
)
575 struct cm_id_private
*cm_id_priv
;
578 cm_id_priv
= xa_load(&cm
.local_id_table
, cm_local_id(local_id
));
579 if (!cm_id_priv
|| cm_id_priv
->id
.remote_id
!= remote_id
||
580 !refcount_inc_not_zero(&cm_id_priv
->refcount
))
588 * Trivial helpers to strip endian annotation and compare; the
589 * endianness doesn't actually matter since we just need a stable
590 * order for the RB tree.
592 static int be32_lt(__be32 a
, __be32 b
)
594 return (__force u32
) a
< (__force u32
) b
;
597 static int be32_gt(__be32 a
, __be32 b
)
599 return (__force u32
) a
> (__force u32
) b
;
602 static int be64_lt(__be64 a
, __be64 b
)
604 return (__force u64
) a
< (__force u64
) b
;
607 static int be64_gt(__be64 a
, __be64 b
)
609 return (__force u64
) a
> (__force u64
) b
;
613 * Inserts a new cm_id_priv into the listen_service_table. Returns cm_id_priv
614 * if the new ID was inserted, NULL if it could not be inserted due to a
615 * collision, or the existing cm_id_priv ready for shared usage.
617 static struct cm_id_private
*cm_insert_listen(struct cm_id_private
*cm_id_priv
,
618 ib_cm_handler shared_handler
)
620 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
621 struct rb_node
*parent
= NULL
;
622 struct cm_id_private
*cur_cm_id_priv
;
623 __be64 service_id
= cm_id_priv
->id
.service_id
;
626 spin_lock_irqsave(&cm
.lock
, flags
);
629 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
632 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
633 link
= &(*link
)->rb_left
;
634 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
635 link
= &(*link
)->rb_right
;
636 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
637 link
= &(*link
)->rb_left
;
638 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
639 link
= &(*link
)->rb_right
;
642 * Sharing an ib_cm_id with different handlers is not
645 if (cur_cm_id_priv
->id
.cm_handler
!= shared_handler
||
646 cur_cm_id_priv
->id
.context
||
647 WARN_ON(!cur_cm_id_priv
->id
.cm_handler
)) {
648 spin_unlock_irqrestore(&cm
.lock
, flags
);
651 refcount_inc(&cur_cm_id_priv
->refcount
);
652 cur_cm_id_priv
->listen_sharecount
++;
653 spin_unlock_irqrestore(&cm
.lock
, flags
);
654 return cur_cm_id_priv
;
657 cm_id_priv
->listen_sharecount
++;
658 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
659 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
660 spin_unlock_irqrestore(&cm
.lock
, flags
);
664 static struct cm_id_private
*cm_find_listen(struct ib_device
*device
,
667 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
668 struct cm_id_private
*cm_id_priv
;
671 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
673 if (device
< cm_id_priv
->id
.device
)
674 node
= node
->rb_left
;
675 else if (device
> cm_id_priv
->id
.device
)
676 node
= node
->rb_right
;
677 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
678 node
= node
->rb_left
;
679 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
680 node
= node
->rb_right
;
682 refcount_inc(&cm_id_priv
->refcount
);
689 static struct cm_timewait_info
*
690 cm_insert_remote_id(struct cm_timewait_info
*timewait_info
)
692 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
693 struct rb_node
*parent
= NULL
;
694 struct cm_timewait_info
*cur_timewait_info
;
695 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
696 __be32 remote_id
= timewait_info
->work
.remote_id
;
700 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
702 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
703 link
= &(*link
)->rb_left
;
704 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
705 link
= &(*link
)->rb_right
;
706 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
707 link
= &(*link
)->rb_left
;
708 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
709 link
= &(*link
)->rb_right
;
711 return cur_timewait_info
;
713 timewait_info
->inserted_remote_id
= 1;
714 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
715 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
719 static struct cm_id_private
*cm_find_remote_id(__be64 remote_ca_guid
,
722 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
723 struct cm_timewait_info
*timewait_info
;
724 struct cm_id_private
*res
= NULL
;
726 spin_lock_irq(&cm
.lock
);
728 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
730 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
731 node
= node
->rb_left
;
732 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
733 node
= node
->rb_right
;
734 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
735 node
= node
->rb_left
;
736 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
737 node
= node
->rb_right
;
739 res
= cm_acquire_id(timewait_info
->work
.local_id
,
740 timewait_info
->work
.remote_id
);
744 spin_unlock_irq(&cm
.lock
);
748 static struct cm_timewait_info
*
749 cm_insert_remote_qpn(struct cm_timewait_info
*timewait_info
)
751 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
752 struct rb_node
*parent
= NULL
;
753 struct cm_timewait_info
*cur_timewait_info
;
754 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
755 __be32 remote_qpn
= timewait_info
->remote_qpn
;
759 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
761 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
762 link
= &(*link
)->rb_left
;
763 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
764 link
= &(*link
)->rb_right
;
765 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
766 link
= &(*link
)->rb_left
;
767 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
768 link
= &(*link
)->rb_right
;
770 return cur_timewait_info
;
772 timewait_info
->inserted_remote_qp
= 1;
773 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
774 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
778 static struct cm_id_private
*
779 cm_insert_remote_sidr(struct cm_id_private
*cm_id_priv
)
781 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
782 struct rb_node
*parent
= NULL
;
783 struct cm_id_private
*cur_cm_id_priv
;
784 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
788 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
790 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
791 link
= &(*link
)->rb_left
;
792 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
793 link
= &(*link
)->rb_right
;
795 if (cur_cm_id_priv
->sidr_slid
< cm_id_priv
->sidr_slid
)
796 link
= &(*link
)->rb_left
;
797 else if (cur_cm_id_priv
->sidr_slid
> cm_id_priv
->sidr_slid
)
798 link
= &(*link
)->rb_right
;
800 return cur_cm_id_priv
;
803 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
804 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
808 static struct cm_id_private
*cm_alloc_id_priv(struct ib_device
*device
,
809 ib_cm_handler cm_handler
,
812 struct cm_id_private
*cm_id_priv
;
816 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
818 return ERR_PTR(-ENOMEM
);
820 cm_id_priv
->id
.state
= IB_CM_IDLE
;
821 cm_id_priv
->id
.device
= device
;
822 cm_id_priv
->id
.cm_handler
= cm_handler
;
823 cm_id_priv
->id
.context
= context
;
824 cm_id_priv
->id
.remote_cm_qpn
= 1;
826 RB_CLEAR_NODE(&cm_id_priv
->service_node
);
827 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
828 spin_lock_init(&cm_id_priv
->lock
);
829 init_completion(&cm_id_priv
->comp
);
830 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
831 atomic_set(&cm_id_priv
->work_count
, -1);
832 refcount_set(&cm_id_priv
->refcount
, 1);
834 ret
= xa_alloc_cyclic(&cm
.local_id_table
, &id
, NULL
, xa_limit_32b
,
835 &cm
.local_id_next
, GFP_KERNEL
);
838 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
848 * Make the ID visible to the MAD handlers and other threads that use the
851 static void cm_finalize_id(struct cm_id_private
*cm_id_priv
)
853 xa_store(&cm
.local_id_table
, cm_local_id(cm_id_priv
->id
.local_id
),
854 cm_id_priv
, GFP_ATOMIC
);
857 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
858 ib_cm_handler cm_handler
,
861 struct cm_id_private
*cm_id_priv
;
863 cm_id_priv
= cm_alloc_id_priv(device
, cm_handler
, context
);
864 if (IS_ERR(cm_id_priv
))
865 return ERR_CAST(cm_id_priv
);
867 cm_finalize_id(cm_id_priv
);
868 return &cm_id_priv
->id
;
870 EXPORT_SYMBOL(ib_create_cm_id
);
872 static struct cm_work
*cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
874 struct cm_work
*work
;
876 if (list_empty(&cm_id_priv
->work_list
))
879 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
880 list_del(&work
->list
);
884 static void cm_free_work(struct cm_work
*work
)
886 if (work
->mad_recv_wc
)
887 ib_free_recv_mad(work
->mad_recv_wc
);
891 static void cm_queue_work_unlock(struct cm_id_private
*cm_id_priv
,
892 struct cm_work
*work
)
893 __releases(&cm_id_priv
->lock
)
898 * To deliver the event to the user callback we have the drop the
899 * spinlock, however, we need to ensure that the user callback is single
900 * threaded and receives events in the temporal order. If there are
901 * already events being processed then thread new events onto a list,
902 * the thread currently processing will pick them up.
904 immediate
= atomic_inc_and_test(&cm_id_priv
->work_count
);
906 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
908 * This routine always consumes incoming reference. Once queued
909 * to the work_list then a reference is held by the thread
910 * currently running cm_process_work() and this reference is not
913 cm_deref_id(cm_id_priv
);
915 spin_unlock_irq(&cm_id_priv
->lock
);
918 cm_process_work(cm_id_priv
, work
);
921 static inline int cm_convert_to_ms(int iba_time
)
923 /* approximate conversion to ms from 4.096us x 2^iba_time */
924 return 1 << max(iba_time
- 8, 0);
928 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
929 * Because of how ack_timeout is stored, adding one doubles the timeout.
930 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
931 * increment it (round up) only if the other is within 50%.
933 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
935 int ack_timeout
= packet_life_time
+ 1;
937 if (ack_timeout
>= ca_ack_delay
)
938 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
940 ack_timeout
= ca_ack_delay
+
941 (ack_timeout
>= (ca_ack_delay
- 1));
943 return min(31, ack_timeout
);
946 static void cm_remove_remote(struct cm_id_private
*cm_id_priv
)
948 struct cm_timewait_info
*timewait_info
= cm_id_priv
->timewait_info
;
950 if (timewait_info
->inserted_remote_id
) {
951 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
952 timewait_info
->inserted_remote_id
= 0;
955 if (timewait_info
->inserted_remote_qp
) {
956 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
957 timewait_info
->inserted_remote_qp
= 0;
961 static struct cm_timewait_info
*cm_create_timewait_info(__be32 local_id
)
963 struct cm_timewait_info
*timewait_info
;
965 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
967 return ERR_PTR(-ENOMEM
);
969 timewait_info
->work
.local_id
= local_id
;
970 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
971 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
972 return timewait_info
;
975 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
979 struct cm_device
*cm_dev
;
981 lockdep_assert_held(&cm_id_priv
->lock
);
983 cm_dev
= ib_get_client_data(cm_id_priv
->id
.device
, &cm_client
);
987 spin_lock_irqsave(&cm
.lock
, flags
);
988 cm_remove_remote(cm_id_priv
);
989 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
990 spin_unlock_irqrestore(&cm
.lock
, flags
);
993 * The cm_id could be destroyed by the user before we exit timewait.
994 * To protect against this, we search for the cm_id after exiting
995 * timewait before notifying the user that we've exited timewait.
997 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
998 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
1000 /* Check if the device started its remove_one */
1001 spin_lock_irqsave(&cm
.lock
, flags
);
1002 if (!cm_dev
->going_down
)
1003 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
1004 msecs_to_jiffies(wait_time
));
1005 spin_unlock_irqrestore(&cm
.lock
, flags
);
1008 * The timewait_info is converted into a work and gets freed during
1009 * cm_free_work() in cm_timewait_handler().
1011 BUILD_BUG_ON(offsetof(struct cm_timewait_info
, work
) != 0);
1012 cm_id_priv
->timewait_info
= NULL
;
1015 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
1017 unsigned long flags
;
1019 lockdep_assert_held(&cm_id_priv
->lock
);
1021 cm_id_priv
->id
.state
= IB_CM_IDLE
;
1022 if (cm_id_priv
->timewait_info
) {
1023 spin_lock_irqsave(&cm
.lock
, flags
);
1024 cm_remove_remote(cm_id_priv
);
1025 spin_unlock_irqrestore(&cm
.lock
, flags
);
1026 kfree(cm_id_priv
->timewait_info
);
1027 cm_id_priv
->timewait_info
= NULL
;
1031 static noinline
void cm_destroy_id_wait_timeout(struct ib_cm_id
*cm_id
,
1032 enum ib_cm_state old_state
)
1034 struct cm_id_private
*cm_id_priv
;
1036 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1037 pr_err("%s: cm_id=%p timed out. state %d -> %d, refcnt=%d\n", __func__
,
1038 cm_id
, old_state
, cm_id
->state
, refcount_read(&cm_id_priv
->refcount
));
1041 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
1043 struct cm_id_private
*cm_id_priv
;
1044 enum ib_cm_state old_state
;
1045 struct cm_work
*work
;
1048 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1049 spin_lock_irq(&cm_id_priv
->lock
);
1050 old_state
= cm_id
->state
;
1052 switch (cm_id
->state
) {
1054 spin_lock(&cm
.lock
);
1055 if (--cm_id_priv
->listen_sharecount
> 0) {
1056 /* The id is still shared. */
1057 WARN_ON(refcount_read(&cm_id_priv
->refcount
) == 1);
1058 spin_unlock(&cm
.lock
);
1059 spin_unlock_irq(&cm_id_priv
->lock
);
1060 cm_deref_id(cm_id_priv
);
1063 cm_id
->state
= IB_CM_IDLE
;
1064 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
1065 RB_CLEAR_NODE(&cm_id_priv
->service_node
);
1066 spin_unlock(&cm
.lock
);
1068 case IB_CM_SIDR_REQ_SENT
:
1069 cm_id
->state
= IB_CM_IDLE
;
1070 ib_cancel_mad(cm_id_priv
->msg
);
1072 case IB_CM_SIDR_REQ_RCVD
:
1073 cm_send_sidr_rep_locked(cm_id_priv
,
1074 &(struct ib_cm_sidr_rep_param
){
1075 .status
= IB_SIDR_REJECT
});
1076 /* cm_send_sidr_rep_locked will not move to IDLE if it fails */
1077 cm_id
->state
= IB_CM_IDLE
;
1079 case IB_CM_REQ_SENT
:
1080 case IB_CM_MRA_REQ_RCVD
:
1081 ib_cancel_mad(cm_id_priv
->msg
);
1082 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_TIMEOUT
,
1083 &cm_id_priv
->id
.device
->node_guid
,
1084 sizeof(cm_id_priv
->id
.device
->node_guid
),
1087 case IB_CM_REQ_RCVD
:
1088 if (err
== -ENOMEM
) {
1089 /* Do not reject to allow future retries. */
1090 cm_reset_to_idle(cm_id_priv
);
1092 cm_send_rej_locked(cm_id_priv
,
1093 IB_CM_REJ_CONSUMER_DEFINED
, NULL
, 0,
1097 case IB_CM_REP_SENT
:
1098 case IB_CM_MRA_REP_RCVD
:
1099 ib_cancel_mad(cm_id_priv
->msg
);
1100 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
1103 case IB_CM_MRA_REQ_SENT
:
1104 case IB_CM_REP_RCVD
:
1105 case IB_CM_MRA_REP_SENT
:
1106 cm_send_rej_locked(cm_id_priv
, IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
1109 case IB_CM_ESTABLISHED
:
1110 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
1111 cm_id
->state
= IB_CM_IDLE
;
1114 cm_issue_dreq(cm_id_priv
);
1115 cm_enter_timewait(cm_id_priv
);
1117 case IB_CM_DREQ_SENT
:
1118 ib_cancel_mad(cm_id_priv
->msg
);
1119 cm_enter_timewait(cm_id_priv
);
1121 case IB_CM_DREQ_RCVD
:
1122 cm_send_drep_locked(cm_id_priv
, NULL
, 0);
1123 WARN_ON(cm_id
->state
!= IB_CM_TIMEWAIT
);
1125 case IB_CM_TIMEWAIT
:
1127 * The cm_acquire_id in cm_timewait_handler will stop working
1128 * once we do xa_erase below, so just move to idle here for
1131 cm_id
->state
= IB_CM_IDLE
;
1136 WARN_ON(cm_id
->state
!= IB_CM_IDLE
);
1138 spin_lock(&cm
.lock
);
1139 /* Required for cleanup paths related cm_req_handler() */
1140 if (cm_id_priv
->timewait_info
) {
1141 cm_remove_remote(cm_id_priv
);
1142 kfree(cm_id_priv
->timewait_info
);
1143 cm_id_priv
->timewait_info
= NULL
;
1146 WARN_ON(cm_id_priv
->listen_sharecount
);
1147 WARN_ON(!RB_EMPTY_NODE(&cm_id_priv
->service_node
));
1148 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
))
1149 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
1150 spin_unlock(&cm
.lock
);
1151 spin_unlock_irq(&cm_id_priv
->lock
);
1153 xa_erase(&cm
.local_id_table
, cm_local_id(cm_id
->local_id
));
1154 cm_deref_id(cm_id_priv
);
1156 ret
= wait_for_completion_timeout(&cm_id_priv
->comp
,
1158 CM_DESTROY_ID_WAIT_TIMEOUT
));
1159 if (!ret
) /* timeout happened */
1160 cm_destroy_id_wait_timeout(cm_id
, old_state
);
1163 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
1166 cm_destroy_av(&cm_id_priv
->av
);
1167 cm_destroy_av(&cm_id_priv
->alt_av
);
1168 kfree(cm_id_priv
->private_data
);
1169 kfree_rcu(cm_id_priv
, rcu
);
1172 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
1174 cm_destroy_id(cm_id
, 0);
1176 EXPORT_SYMBOL(ib_destroy_cm_id
);
1178 static int cm_init_listen(struct cm_id_private
*cm_id_priv
, __be64 service_id
)
1180 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
1181 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
1184 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
)
1185 cm_id_priv
->id
.service_id
= cpu_to_be64(cm
.listen_service_id
++);
1187 cm_id_priv
->id
.service_id
= service_id
;
1193 * ib_cm_listen - Initiates listening on the specified service ID for
1194 * connection and service ID resolution requests.
1195 * @cm_id: Connection identifier associated with the listen request.
1196 * @service_id: Service identifier matched against incoming connection
1197 * and service ID resolution requests. The service ID should be specified
1198 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1199 * assign a service ID to the caller.
1201 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
)
1203 struct cm_id_private
*cm_id_priv
=
1204 container_of(cm_id
, struct cm_id_private
, id
);
1205 unsigned long flags
;
1208 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1209 if (cm_id_priv
->id
.state
!= IB_CM_IDLE
) {
1214 ret
= cm_init_listen(cm_id_priv
, service_id
);
1218 if (!cm_insert_listen(cm_id_priv
, NULL
)) {
1223 cm_id_priv
->id
.state
= IB_CM_LISTEN
;
1227 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1230 EXPORT_SYMBOL(ib_cm_listen
);
1233 * ib_cm_insert_listen - Create a new listening ib_cm_id and listen on
1234 * the given service ID.
1236 * If there's an existing ID listening on that same device and service ID,
1239 * @device: Device associated with the cm_id. All related communication will
1240 * be associated with the specified device.
1241 * @cm_handler: Callback invoked to notify the user of CM events.
1242 * @service_id: Service identifier matched against incoming connection
1243 * and service ID resolution requests. The service ID should be specified
1244 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1245 * assign a service ID to the caller.
1247 * Callers should call ib_destroy_cm_id when done with the listener ID.
1249 struct ib_cm_id
*ib_cm_insert_listen(struct ib_device
*device
,
1250 ib_cm_handler cm_handler
,
1253 struct cm_id_private
*listen_id_priv
;
1254 struct cm_id_private
*cm_id_priv
;
1257 /* Create an ID in advance, since the creation may sleep */
1258 cm_id_priv
= cm_alloc_id_priv(device
, cm_handler
, NULL
);
1259 if (IS_ERR(cm_id_priv
))
1260 return ERR_CAST(cm_id_priv
);
1262 err
= cm_init_listen(cm_id_priv
, service_id
);
1264 ib_destroy_cm_id(&cm_id_priv
->id
);
1265 return ERR_PTR(err
);
1268 spin_lock_irq(&cm_id_priv
->lock
);
1269 listen_id_priv
= cm_insert_listen(cm_id_priv
, cm_handler
);
1270 if (listen_id_priv
!= cm_id_priv
) {
1271 spin_unlock_irq(&cm_id_priv
->lock
);
1272 ib_destroy_cm_id(&cm_id_priv
->id
);
1273 if (!listen_id_priv
)
1274 return ERR_PTR(-EINVAL
);
1275 return &listen_id_priv
->id
;
1277 cm_id_priv
->id
.state
= IB_CM_LISTEN
;
1278 spin_unlock_irq(&cm_id_priv
->lock
);
1281 * A listen ID does not need to be in the xarray since it does not
1282 * receive mads, is not placed in the remote_id or remote_qpn rbtree,
1283 * and does not enter timewait.
1286 return &cm_id_priv
->id
;
1288 EXPORT_SYMBOL(ib_cm_insert_listen
);
1290 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
)
1292 u64 hi_tid
= 0, low_tid
;
1294 lockdep_assert_held(&cm_id_priv
->lock
);
1296 low_tid
= (u64
)cm_id_priv
->id
.local_id
;
1297 if (!cm_id_priv
->av
.port
)
1298 return cpu_to_be64(low_tid
);
1300 spin_lock(&cm_id_priv
->av
.port
->cm_dev
->mad_agent_lock
);
1301 if (cm_id_priv
->av
.port
->mad_agent
)
1302 hi_tid
= ((u64
)cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
1303 spin_unlock(&cm_id_priv
->av
.port
->cm_dev
->mad_agent_lock
);
1304 return cpu_to_be64(hi_tid
| low_tid
);
1307 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
1308 __be16 attr_id
, __be64 tid
)
1310 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1311 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1312 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1313 hdr
->method
= IB_MGMT_METHOD_SEND
;
1314 hdr
->attr_id
= attr_id
;
1318 static void cm_format_mad_ece_hdr(struct ib_mad_hdr
*hdr
, __be16 attr_id
,
1319 __be64 tid
, u32 attr_mod
)
1321 cm_format_mad_hdr(hdr
, attr_id
, tid
);
1322 hdr
->attr_mod
= cpu_to_be32(attr_mod
);
1325 static void cm_format_req(struct cm_req_msg
*req_msg
,
1326 struct cm_id_private
*cm_id_priv
,
1327 struct ib_cm_req_param
*param
)
1329 struct sa_path_rec
*pri_path
= param
->primary_path
;
1330 struct sa_path_rec
*alt_path
= param
->alternate_path
;
1331 bool pri_ext
= false;
1334 if (pri_path
->rec_type
== SA_PATH_REC_TYPE_OPA
)
1335 pri_ext
= opa_is_extended_lid(pri_path
->opa
.dlid
,
1336 pri_path
->opa
.slid
);
1338 cm_format_mad_ece_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1339 cm_form_tid(cm_id_priv
), param
->ece
.attr_mod
);
1341 IBA_SET(CM_REQ_LOCAL_COMM_ID
, req_msg
,
1342 be32_to_cpu(cm_id_priv
->id
.local_id
));
1343 IBA_SET(CM_REQ_SERVICE_ID
, req_msg
, be64_to_cpu(param
->service_id
));
1344 IBA_SET(CM_REQ_LOCAL_CA_GUID
, req_msg
,
1345 be64_to_cpu(cm_id_priv
->id
.device
->node_guid
));
1346 IBA_SET(CM_REQ_LOCAL_QPN
, req_msg
, param
->qp_num
);
1347 IBA_SET(CM_REQ_INITIATOR_DEPTH
, req_msg
, param
->initiator_depth
);
1348 IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT
, req_msg
,
1349 param
->remote_cm_response_timeout
);
1350 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1351 IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL
, req_msg
, param
->flow_control
);
1352 IBA_SET(CM_REQ_STARTING_PSN
, req_msg
, param
->starting_psn
);
1353 IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
,
1354 param
->local_cm_response_timeout
);
1355 IBA_SET(CM_REQ_PARTITION_KEY
, req_msg
,
1356 be16_to_cpu(param
->primary_path
->pkey
));
1357 IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
,
1358 param
->primary_path
->mtu
);
1359 IBA_SET(CM_REQ_MAX_CM_RETRIES
, req_msg
, param
->max_cm_retries
);
1361 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1362 IBA_SET(CM_REQ_RESPONDER_RESOURCES
, req_msg
,
1363 param
->responder_resources
);
1364 IBA_SET(CM_REQ_RETRY_COUNT
, req_msg
, param
->retry_count
);
1365 IBA_SET(CM_REQ_RNR_RETRY_COUNT
, req_msg
,
1366 param
->rnr_retry_count
);
1367 IBA_SET(CM_REQ_SRQ
, req_msg
, param
->srq
);
1370 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
) =
1372 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
) =
1375 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
)
1376 ->global
.interface_id
=
1377 OPA_MAKE_ID(be32_to_cpu(pri_path
->opa
.slid
));
1378 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
)
1379 ->global
.interface_id
=
1380 OPA_MAKE_ID(be32_to_cpu(pri_path
->opa
.dlid
));
1382 if (pri_path
->hop_limit
<= 1) {
1383 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1384 be16_to_cpu(pri_ext
? 0 :
1385 htons(ntohl(sa_path_get_slid(
1387 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
1388 be16_to_cpu(pri_ext
? 0 :
1389 htons(ntohl(sa_path_get_dlid(
1393 if (param
->primary_path_inbound
) {
1394 lid
= param
->primary_path_inbound
->ib
.dlid
;
1395 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1398 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
1399 be16_to_cpu(IB_LID_PERMISSIVE
));
1401 /* Work-around until there's a way to obtain remote LID info */
1402 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
1403 be16_to_cpu(IB_LID_PERMISSIVE
));
1405 IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL
, req_msg
,
1406 be32_to_cpu(pri_path
->flow_label
));
1407 IBA_SET(CM_REQ_PRIMARY_PACKET_RATE
, req_msg
, pri_path
->rate
);
1408 IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS
, req_msg
, pri_path
->traffic_class
);
1409 IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT
, req_msg
, pri_path
->hop_limit
);
1410 IBA_SET(CM_REQ_PRIMARY_SL
, req_msg
, pri_path
->sl
);
1411 IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL
, req_msg
,
1412 (pri_path
->hop_limit
<= 1));
1413 IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT
, req_msg
,
1414 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1415 pri_path
->packet_life_time
));
1418 bool alt_ext
= false;
1420 if (alt_path
->rec_type
== SA_PATH_REC_TYPE_OPA
)
1421 alt_ext
= opa_is_extended_lid(alt_path
->opa
.dlid
,
1422 alt_path
->opa
.slid
);
1424 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
) =
1426 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
) =
1429 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
,
1431 ->global
.interface_id
=
1432 OPA_MAKE_ID(be32_to_cpu(alt_path
->opa
.slid
));
1433 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID
,
1435 ->global
.interface_id
=
1436 OPA_MAKE_ID(be32_to_cpu(alt_path
->opa
.dlid
));
1438 if (alt_path
->hop_limit
<= 1) {
1439 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
1442 htons(ntohl(sa_path_get_slid(
1444 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
1447 htons(ntohl(sa_path_get_dlid(
1450 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
1451 be16_to_cpu(IB_LID_PERMISSIVE
));
1452 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
1453 be16_to_cpu(IB_LID_PERMISSIVE
));
1455 IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL
, req_msg
,
1456 be32_to_cpu(alt_path
->flow_label
));
1457 IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE
, req_msg
, alt_path
->rate
);
1458 IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS
, req_msg
,
1459 alt_path
->traffic_class
);
1460 IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT
, req_msg
,
1461 alt_path
->hop_limit
);
1462 IBA_SET(CM_REQ_ALTERNATE_SL
, req_msg
, alt_path
->sl
);
1463 IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL
, req_msg
,
1464 (alt_path
->hop_limit
<= 1));
1465 IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT
, req_msg
,
1466 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1467 alt_path
->packet_life_time
));
1469 IBA_SET(CM_REQ_VENDOR_ID
, req_msg
, param
->ece
.vendor_id
);
1471 if (param
->private_data
&& param
->private_data_len
)
1472 IBA_SET_MEM(CM_REQ_PRIVATE_DATA
, req_msg
, param
->private_data
,
1473 param
->private_data_len
);
1476 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1478 if (!param
->primary_path
)
1481 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1482 param
->qp_type
!= IB_QPT_XRC_INI
)
1485 if (param
->private_data
&&
1486 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1489 if (param
->alternate_path
&&
1490 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1491 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1497 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1498 struct ib_cm_req_param
*param
)
1500 struct cm_av av
= {}, alt_av
= {};
1501 struct cm_id_private
*cm_id_priv
;
1502 struct ib_mad_send_buf
*msg
;
1503 struct cm_req_msg
*req_msg
;
1504 unsigned long flags
;
1507 ret
= cm_validate_req_param(param
);
1511 /* Verify that we're not in timewait. */
1512 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1513 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1514 if (cm_id
->state
!= IB_CM_IDLE
|| WARN_ON(cm_id_priv
->timewait_info
)) {
1515 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1518 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1520 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1522 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1523 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1524 cm_id_priv
->timewait_info
= NULL
;
1528 ret
= cm_init_av_by_path(param
->primary_path
,
1529 param
->ppath_sgid_attr
, &av
);
1532 if (param
->alternate_path
) {
1533 ret
= cm_init_av_by_path(param
->alternate_path
, NULL
,
1540 cm_id
->service_id
= param
->service_id
;
1541 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1542 param
->primary_path
->packet_life_time
) * 2 +
1544 param
->remote_cm_response_timeout
);
1545 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1546 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1547 cm_id_priv
->responder_resources
= param
->responder_resources
;
1548 cm_id_priv
->retry_count
= param
->retry_count
;
1549 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1550 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1551 cm_id_priv
->qp_type
= param
->qp_type
;
1553 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1555 cm_move_av_from_path(&cm_id_priv
->av
, &av
);
1556 if (param
->primary_path_outbound
)
1557 cm_id_priv
->av
.dlid_datapath
=
1558 be16_to_cpu(param
->primary_path_outbound
->ib
.dlid
);
1560 if (param
->alternate_path
)
1561 cm_move_av_from_path(&cm_id_priv
->alt_av
, &alt_av
);
1563 msg
= cm_alloc_priv_msg(cm_id_priv
, IB_CM_REQ_SENT
);
1569 req_msg
= (struct cm_req_msg
*)msg
->mad
;
1570 cm_format_req(req_msg
, cm_id_priv
, param
);
1571 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1573 cm_id_priv
->local_qpn
= cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
));
1574 cm_id_priv
->rq_psn
= cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN
, req_msg
));
1576 trace_icm_send_req(&cm_id_priv
->id
);
1577 ret
= ib_post_send_mad(msg
, NULL
);
1580 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1581 cm_id
->state
= IB_CM_REQ_SENT
;
1582 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1585 cm_free_priv_msg(msg
);
1587 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1590 EXPORT_SYMBOL(ib_send_cm_req
);
1592 static int cm_issue_rej(struct cm_port
*port
,
1593 struct ib_mad_recv_wc
*mad_recv_wc
,
1594 enum ib_cm_rej_reason reason
,
1595 enum cm_msg_response msg_rejected
,
1596 void *ari
, u8 ari_length
)
1598 struct ib_mad_send_buf
*msg
= NULL
;
1599 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1602 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, false, &msg
);
1606 /* We just need common CM header information. Cast to any message. */
1607 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1608 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1610 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1611 IBA_SET(CM_REJ_REMOTE_COMM_ID
, rej_msg
,
1612 IBA_GET(CM_REJ_LOCAL_COMM_ID
, rcv_msg
));
1613 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1614 IBA_GET(CM_REJ_REMOTE_COMM_ID
, rcv_msg
));
1615 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, msg_rejected
);
1616 IBA_SET(CM_REJ_REASON
, rej_msg
, reason
);
1618 if (ari
&& ari_length
) {
1619 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
, ari_length
);
1620 IBA_SET_MEM(CM_REJ_ARI
, rej_msg
, ari
, ari_length
);
1623 trace_icm_issue_rej(
1624 IBA_GET(CM_REJ_LOCAL_COMM_ID
, rcv_msg
),
1625 IBA_GET(CM_REJ_REMOTE_COMM_ID
, rcv_msg
));
1626 ret
= ib_post_send_mad(msg
, NULL
);
1633 static bool cm_req_has_alt_path(struct cm_req_msg
*req_msg
)
1635 return ((cpu_to_be16(
1636 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
))) ||
1637 (ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID
,
1641 static void cm_path_set_rec_type(struct ib_device
*ib_device
, u32 port_num
,
1642 struct sa_path_rec
*path
, union ib_gid
*gid
)
1644 if (ib_is_opa_gid(gid
) && rdma_cap_opa_ah(ib_device
, port_num
))
1645 path
->rec_type
= SA_PATH_REC_TYPE_OPA
;
1647 path
->rec_type
= SA_PATH_REC_TYPE_IB
;
1650 static void cm_format_path_lid_from_req(struct cm_req_msg
*req_msg
,
1651 struct sa_path_rec
*primary_path
,
1652 struct sa_path_rec
*alt_path
,
1657 if (primary_path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
1658 sa_path_set_dlid(primary_path
, wc
->slid
);
1659 sa_path_set_slid(primary_path
,
1660 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID
,
1663 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1664 CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
));
1665 sa_path_set_dlid(primary_path
, lid
);
1667 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1668 CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
));
1669 sa_path_set_slid(primary_path
, lid
);
1672 if (!cm_req_has_alt_path(req_msg
))
1675 if (alt_path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
1676 sa_path_set_dlid(alt_path
,
1677 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
,
1679 sa_path_set_slid(alt_path
,
1680 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
,
1683 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1684 CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
));
1685 sa_path_set_dlid(alt_path
, lid
);
1687 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1688 CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
));
1689 sa_path_set_slid(alt_path
, lid
);
1693 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1694 struct sa_path_rec
*primary_path
,
1695 struct sa_path_rec
*alt_path
,
1698 primary_path
->dgid
=
1699 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
, req_msg
);
1700 primary_path
->sgid
=
1701 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID
, req_msg
);
1702 primary_path
->flow_label
=
1703 cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL
, req_msg
));
1704 primary_path
->hop_limit
= IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT
, req_msg
);
1705 primary_path
->traffic_class
=
1706 IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS
, req_msg
);
1707 primary_path
->reversible
= 1;
1708 primary_path
->pkey
=
1709 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
1710 primary_path
->sl
= IBA_GET(CM_REQ_PRIMARY_SL
, req_msg
);
1711 primary_path
->mtu_selector
= IB_SA_EQ
;
1712 primary_path
->mtu
= IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
1713 primary_path
->rate_selector
= IB_SA_EQ
;
1714 primary_path
->rate
= IBA_GET(CM_REQ_PRIMARY_PACKET_RATE
, req_msg
);
1715 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1716 primary_path
->packet_life_time
=
1717 IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT
, req_msg
);
1718 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1719 primary_path
->service_id
=
1720 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
1721 if (sa_path_is_roce(primary_path
))
1722 primary_path
->roce
.route_resolved
= false;
1724 if (cm_req_has_alt_path(req_msg
)) {
1725 alt_path
->dgid
= *IBA_GET_MEM_PTR(
1726 CM_REQ_ALTERNATE_LOCAL_PORT_GID
, req_msg
);
1727 alt_path
->sgid
= *IBA_GET_MEM_PTR(
1728 CM_REQ_ALTERNATE_REMOTE_PORT_GID
, req_msg
);
1729 alt_path
->flow_label
= cpu_to_be32(
1730 IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL
, req_msg
));
1731 alt_path
->hop_limit
=
1732 IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT
, req_msg
);
1733 alt_path
->traffic_class
=
1734 IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS
, req_msg
);
1735 alt_path
->reversible
= 1;
1737 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
1738 alt_path
->sl
= IBA_GET(CM_REQ_ALTERNATE_SL
, req_msg
);
1739 alt_path
->mtu_selector
= IB_SA_EQ
;
1741 IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
1742 alt_path
->rate_selector
= IB_SA_EQ
;
1743 alt_path
->rate
= IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE
, req_msg
);
1744 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1745 alt_path
->packet_life_time
=
1746 IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT
, req_msg
);
1747 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1748 alt_path
->service_id
=
1749 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
1751 if (sa_path_is_roce(alt_path
))
1752 alt_path
->roce
.route_resolved
= false;
1754 cm_format_path_lid_from_req(req_msg
, primary_path
, alt_path
, wc
);
1757 static u16
cm_get_bth_pkey(struct cm_work
*work
)
1759 struct ib_device
*ib_dev
= work
->port
->cm_dev
->ib_device
;
1760 u32 port_num
= work
->port
->port_num
;
1761 u16 pkey_index
= work
->mad_recv_wc
->wc
->pkey_index
;
1765 ret
= ib_get_cached_pkey(ib_dev
, port_num
, pkey_index
, &pkey
);
1767 dev_warn_ratelimited(&ib_dev
->dev
, "ib_cm: Couldn't retrieve pkey for incoming request (port %u, pkey index %u). %d\n",
1768 port_num
, pkey_index
, ret
);
1776 * cm_opa_to_ib_sgid - Convert OPA SGID to IB SGID
1777 * ULPs (such as IPoIB) do not understand OPA GIDs and will
1778 * reject them as the local_gid will not match the sgid. Therefore,
1779 * change the pathrec's SGID to an IB SGID.
1781 * @work: Work completion
1782 * @path: Path record
1784 static void cm_opa_to_ib_sgid(struct cm_work
*work
,
1785 struct sa_path_rec
*path
)
1787 struct ib_device
*dev
= work
->port
->cm_dev
->ib_device
;
1788 u32 port_num
= work
->port
->port_num
;
1790 if (rdma_cap_opa_ah(dev
, port_num
) &&
1791 (ib_is_opa_gid(&path
->sgid
))) {
1794 if (rdma_query_gid(dev
, port_num
, 0, &sgid
)) {
1796 "Error updating sgid in CM request\n");
1804 static void cm_format_req_event(struct cm_work
*work
,
1805 struct cm_id_private
*cm_id_priv
,
1806 struct ib_cm_id
*listen_id
)
1808 struct cm_req_msg
*req_msg
;
1809 struct ib_cm_req_event_param
*param
;
1811 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1812 param
= &work
->cm_event
.param
.req_rcvd
;
1813 param
->listen_id
= listen_id
;
1814 param
->bth_pkey
= cm_get_bth_pkey(work
);
1815 param
->port
= cm_id_priv
->av
.port
->port_num
;
1816 param
->primary_path
= &work
->path
[0];
1817 cm_opa_to_ib_sgid(work
, param
->primary_path
);
1818 if (cm_req_has_alt_path(req_msg
)) {
1819 param
->alternate_path
= &work
->path
[1];
1820 cm_opa_to_ib_sgid(work
, param
->alternate_path
);
1822 param
->alternate_path
= NULL
;
1824 param
->remote_ca_guid
=
1825 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID
, req_msg
));
1826 param
->remote_qkey
= IBA_GET(CM_REQ_LOCAL_Q_KEY
, req_msg
);
1827 param
->remote_qpn
= IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
);
1828 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1829 param
->starting_psn
= IBA_GET(CM_REQ_STARTING_PSN
, req_msg
);
1830 param
->responder_resources
= IBA_GET(CM_REQ_INITIATOR_DEPTH
, req_msg
);
1831 param
->initiator_depth
= IBA_GET(CM_REQ_RESPONDER_RESOURCES
, req_msg
);
1832 param
->local_cm_response_timeout
=
1833 IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT
, req_msg
);
1834 param
->flow_control
= IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL
, req_msg
);
1835 param
->remote_cm_response_timeout
=
1836 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
);
1837 param
->retry_count
= IBA_GET(CM_REQ_RETRY_COUNT
, req_msg
);
1838 param
->rnr_retry_count
= IBA_GET(CM_REQ_RNR_RETRY_COUNT
, req_msg
);
1839 param
->srq
= IBA_GET(CM_REQ_SRQ
, req_msg
);
1840 param
->ppath_sgid_attr
= cm_id_priv
->av
.ah_attr
.grh
.sgid_attr
;
1841 param
->ece
.vendor_id
= IBA_GET(CM_REQ_VENDOR_ID
, req_msg
);
1842 param
->ece
.attr_mod
= be32_to_cpu(req_msg
->hdr
.attr_mod
);
1844 work
->cm_event
.private_data
=
1845 IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA
, req_msg
);
1848 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1849 struct cm_work
*work
)
1853 /* We will typically only have the current event to report. */
1854 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1857 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1858 spin_lock_irq(&cm_id_priv
->lock
);
1859 work
= cm_dequeue_work(cm_id_priv
);
1860 spin_unlock_irq(&cm_id_priv
->lock
);
1864 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1868 cm_deref_id(cm_id_priv
);
1870 cm_destroy_id(&cm_id_priv
->id
, ret
);
1873 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1874 struct cm_id_private
*cm_id_priv
,
1875 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1876 const void *private_data
, u8 private_data_len
)
1878 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1879 IBA_SET(CM_MRA_MESSAGE_MRAED
, mra_msg
, msg_mraed
);
1880 IBA_SET(CM_MRA_LOCAL_COMM_ID
, mra_msg
,
1881 be32_to_cpu(cm_id_priv
->id
.local_id
));
1882 IBA_SET(CM_MRA_REMOTE_COMM_ID
, mra_msg
,
1883 be32_to_cpu(cm_id_priv
->id
.remote_id
));
1884 IBA_SET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
, service_timeout
);
1886 if (private_data
&& private_data_len
)
1887 IBA_SET_MEM(CM_MRA_PRIVATE_DATA
, mra_msg
, private_data
,
1891 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1892 struct cm_id_private
*cm_id_priv
,
1893 enum ib_cm_rej_reason reason
, void *ari
,
1894 u8 ari_length
, const void *private_data
,
1895 u8 private_data_len
, enum ib_cm_state state
)
1897 lockdep_assert_held(&cm_id_priv
->lock
);
1899 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1900 IBA_SET(CM_REJ_REMOTE_COMM_ID
, rej_msg
,
1901 be32_to_cpu(cm_id_priv
->id
.remote_id
));
1904 case IB_CM_REQ_RCVD
:
1905 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
, be32_to_cpu(0));
1906 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REQ
);
1908 case IB_CM_MRA_REQ_SENT
:
1909 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1910 be32_to_cpu(cm_id_priv
->id
.local_id
));
1911 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REQ
);
1913 case IB_CM_REP_RCVD
:
1914 case IB_CM_MRA_REP_SENT
:
1915 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1916 be32_to_cpu(cm_id_priv
->id
.local_id
));
1917 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
, CM_MSG_RESPONSE_REP
);
1920 IBA_SET(CM_REJ_LOCAL_COMM_ID
, rej_msg
,
1921 be32_to_cpu(cm_id_priv
->id
.local_id
));
1922 IBA_SET(CM_REJ_MESSAGE_REJECTED
, rej_msg
,
1923 CM_MSG_RESPONSE_OTHER
);
1927 IBA_SET(CM_REJ_REASON
, rej_msg
, reason
);
1928 if (ari
&& ari_length
) {
1929 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
, ari_length
);
1930 IBA_SET_MEM(CM_REJ_ARI
, rej_msg
, ari
, ari_length
);
1933 if (private_data
&& private_data_len
)
1934 IBA_SET_MEM(CM_REJ_PRIVATE_DATA
, rej_msg
, private_data
,
1938 static void cm_dup_req_handler(struct cm_work
*work
,
1939 struct cm_id_private
*cm_id_priv
)
1941 struct ib_mad_send_buf
*msg
= NULL
;
1945 &work
->port
->counters
[CM_RECV_DUPLICATES
][CM_REQ_COUNTER
]);
1947 /* Quick state check to discard duplicate REQs. */
1948 spin_lock_irq(&cm_id_priv
->lock
);
1949 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
) {
1950 spin_unlock_irq(&cm_id_priv
->lock
);
1953 spin_unlock_irq(&cm_id_priv
->lock
);
1955 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, true, &msg
);
1959 spin_lock_irq(&cm_id_priv
->lock
);
1960 switch (cm_id_priv
->id
.state
) {
1961 case IB_CM_MRA_REQ_SENT
:
1962 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1963 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1964 cm_id_priv
->private_data
,
1965 cm_id_priv
->private_data_len
);
1967 case IB_CM_TIMEWAIT
:
1968 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
,
1969 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0,
1975 spin_unlock_irq(&cm_id_priv
->lock
);
1977 trace_icm_send_dup_req(&cm_id_priv
->id
);
1978 ret
= ib_post_send_mad(msg
, NULL
);
1983 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1984 free
: cm_free_msg(msg
);
1987 static struct cm_id_private
*cm_match_req(struct cm_work
*work
,
1988 struct cm_id_private
*cm_id_priv
)
1990 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1991 struct cm_timewait_info
*timewait_info
;
1992 struct cm_req_msg
*req_msg
;
1994 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1996 /* Check for possible duplicate REQ. */
1997 spin_lock_irq(&cm
.lock
);
1998 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1999 if (timewait_info
) {
2000 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2001 timewait_info
->work
.remote_id
);
2002 spin_unlock_irq(&cm
.lock
);
2003 if (cur_cm_id_priv
) {
2004 cm_dup_req_handler(work
, cur_cm_id_priv
);
2005 cm_deref_id(cur_cm_id_priv
);
2010 /* Check for stale connections. */
2011 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
2012 if (timewait_info
) {
2013 cm_remove_remote(cm_id_priv
);
2014 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2015 timewait_info
->work
.remote_id
);
2017 spin_unlock_irq(&cm
.lock
);
2018 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
2019 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
2021 if (cur_cm_id_priv
) {
2022 ib_send_cm_dreq(&cur_cm_id_priv
->id
, NULL
, 0);
2023 cm_deref_id(cur_cm_id_priv
);
2028 /* Find matching listen request. */
2029 listen_cm_id_priv
= cm_find_listen(
2030 cm_id_priv
->id
.device
,
2031 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
)));
2032 if (!listen_cm_id_priv
) {
2033 cm_remove_remote(cm_id_priv
);
2034 spin_unlock_irq(&cm
.lock
);
2035 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
2036 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
2040 spin_unlock_irq(&cm
.lock
);
2041 return listen_cm_id_priv
;
2045 * Work-around for inter-subnet connections. If the LIDs are permissive,
2046 * we need to override the LID/SL data in the REQ with the LID information
2047 * in the work completion.
2049 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
2051 if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL
, req_msg
)) {
2052 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID
,
2053 req_msg
)) == IB_LID_PERMISSIVE
) {
2054 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
,
2055 be16_to_cpu(ib_lid_be16(wc
->slid
)));
2056 IBA_SET(CM_REQ_PRIMARY_SL
, req_msg
, wc
->sl
);
2059 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID
,
2060 req_msg
)) == IB_LID_PERMISSIVE
)
2061 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID
, req_msg
,
2062 wc
->dlid_path_bits
);
2065 if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL
, req_msg
)) {
2066 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
,
2067 req_msg
)) == IB_LID_PERMISSIVE
) {
2068 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID
, req_msg
,
2069 be16_to_cpu(ib_lid_be16(wc
->slid
)));
2070 IBA_SET(CM_REQ_ALTERNATE_SL
, req_msg
, wc
->sl
);
2073 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
,
2074 req_msg
)) == IB_LID_PERMISSIVE
)
2075 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID
, req_msg
,
2076 wc
->dlid_path_bits
);
2080 static int cm_req_handler(struct cm_work
*work
)
2082 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
2083 struct cm_req_msg
*req_msg
;
2084 const struct ib_global_route
*grh
;
2085 const struct ib_gid_attr
*gid_attr
;
2088 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2091 cm_alloc_id_priv(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
2092 if (IS_ERR(cm_id_priv
))
2093 return PTR_ERR(cm_id_priv
);
2095 cm_id_priv
->id
.remote_id
=
2096 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID
, req_msg
));
2097 cm_id_priv
->id
.service_id
=
2098 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID
, req_msg
));
2099 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
2100 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
2101 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT
, req_msg
));
2102 cm_id_priv
->max_cm_retries
= IBA_GET(CM_REQ_MAX_CM_RETRIES
, req_msg
);
2103 cm_id_priv
->remote_qpn
=
2104 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN
, req_msg
));
2105 cm_id_priv
->initiator_depth
=
2106 IBA_GET(CM_REQ_RESPONDER_RESOURCES
, req_msg
);
2107 cm_id_priv
->responder_resources
=
2108 IBA_GET(CM_REQ_INITIATOR_DEPTH
, req_msg
);
2109 cm_id_priv
->path_mtu
= IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU
, req_msg
);
2110 cm_id_priv
->pkey
= cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY
, req_msg
));
2111 cm_id_priv
->sq_psn
= cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN
, req_msg
));
2112 cm_id_priv
->retry_count
= IBA_GET(CM_REQ_RETRY_COUNT
, req_msg
);
2113 cm_id_priv
->rnr_retry_count
= IBA_GET(CM_REQ_RNR_RETRY_COUNT
, req_msg
);
2114 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
2116 ret
= cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2117 work
->mad_recv_wc
->recv_buf
.grh
,
2121 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
2123 if (IS_ERR(cm_id_priv
->timewait_info
)) {
2124 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
2125 cm_id_priv
->timewait_info
= NULL
;
2128 cm_id_priv
->timewait_info
->work
.remote_id
= cm_id_priv
->id
.remote_id
;
2129 cm_id_priv
->timewait_info
->remote_ca_guid
=
2130 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID
, req_msg
));
2131 cm_id_priv
->timewait_info
->remote_qpn
= cm_id_priv
->remote_qpn
;
2134 * Note that the ID pointer is not in the xarray at this point,
2135 * so this set is only visible to the local thread.
2137 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
2139 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
2140 if (!listen_cm_id_priv
) {
2141 trace_icm_no_listener_err(&cm_id_priv
->id
);
2142 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2147 memset(&work
->path
[0], 0, sizeof(work
->path
[0]));
2148 if (cm_req_has_alt_path(req_msg
))
2149 memset(&work
->path
[1], 0, sizeof(work
->path
[1]));
2150 grh
= rdma_ah_read_grh(&cm_id_priv
->av
.ah_attr
);
2151 gid_attr
= grh
->sgid_attr
;
2153 if (cm_id_priv
->av
.ah_attr
.type
== RDMA_AH_ATTR_TYPE_ROCE
) {
2154 work
->path
[0].rec_type
=
2155 sa_conv_gid_to_pathrec_type(gid_attr
->gid_type
);
2157 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
2158 cm_path_set_rec_type(
2159 work
->port
->cm_dev
->ib_device
, work
->port
->port_num
,
2161 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID
,
2164 if (cm_req_has_alt_path(req_msg
))
2165 work
->path
[1].rec_type
= work
->path
[0].rec_type
;
2166 cm_format_paths_from_req(req_msg
, &work
->path
[0],
2167 &work
->path
[1], work
->mad_recv_wc
->wc
);
2168 if (cm_id_priv
->av
.ah_attr
.type
== RDMA_AH_ATTR_TYPE_ROCE
)
2169 sa_path_set_dmac(&work
->path
[0],
2170 cm_id_priv
->av
.ah_attr
.roce
.dmac
);
2171 work
->path
[0].hop_limit
= grh
->hop_limit
;
2173 /* This destroy call is needed to pair with cm_init_av_for_response */
2174 cm_destroy_av(&cm_id_priv
->av
);
2175 ret
= cm_init_av_by_path(&work
->path
[0], gid_attr
, &cm_id_priv
->av
);
2179 err
= rdma_query_gid(work
->port
->cm_dev
->ib_device
,
2180 work
->port
->port_num
, 0,
2181 &work
->path
[0].sgid
);
2183 ib_send_cm_rej(&cm_id_priv
->id
, IB_CM_REJ_INVALID_GID
,
2186 ib_send_cm_rej(&cm_id_priv
->id
, IB_CM_REJ_INVALID_GID
,
2187 &work
->path
[0].sgid
,
2188 sizeof(work
->path
[0].sgid
),
2192 if (cm_id_priv
->av
.ah_attr
.type
== RDMA_AH_ATTR_TYPE_IB
)
2193 cm_id_priv
->av
.dlid_datapath
=
2194 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID
, req_msg
);
2196 if (cm_req_has_alt_path(req_msg
)) {
2197 ret
= cm_init_av_by_path(&work
->path
[1], NULL
,
2198 &cm_id_priv
->alt_av
);
2200 ib_send_cm_rej(&cm_id_priv
->id
,
2201 IB_CM_REJ_INVALID_ALT_GID
,
2202 &work
->path
[0].sgid
,
2203 sizeof(work
->path
[0].sgid
), NULL
, 0);
2208 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
2209 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
2210 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
2212 /* Now MAD handlers can see the new ID */
2213 spin_lock_irq(&cm_id_priv
->lock
);
2214 cm_finalize_id(cm_id_priv
);
2216 /* Refcount belongs to the event, pairs with cm_process_work() */
2217 refcount_inc(&cm_id_priv
->refcount
);
2218 cm_queue_work_unlock(cm_id_priv
, work
);
2220 * Since this ID was just created and was not made visible to other MAD
2221 * handlers until the cm_finalize_id() above we know that the
2222 * cm_process_work() will deliver the event and the listen_cm_id
2223 * embedded in the event can be derefed here.
2225 cm_deref_id(listen_cm_id_priv
);
2229 cm_deref_id(listen_cm_id_priv
);
2231 ib_destroy_cm_id(&cm_id_priv
->id
);
2235 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
2236 struct cm_id_private
*cm_id_priv
,
2237 struct ib_cm_rep_param
*param
)
2239 cm_format_mad_ece_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
,
2240 param
->ece
.attr_mod
);
2241 IBA_SET(CM_REP_LOCAL_COMM_ID
, rep_msg
,
2242 be32_to_cpu(cm_id_priv
->id
.local_id
));
2243 IBA_SET(CM_REP_REMOTE_COMM_ID
, rep_msg
,
2244 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2245 IBA_SET(CM_REP_STARTING_PSN
, rep_msg
, param
->starting_psn
);
2246 IBA_SET(CM_REP_RESPONDER_RESOURCES
, rep_msg
,
2247 param
->responder_resources
);
2248 IBA_SET(CM_REP_TARGET_ACK_DELAY
, rep_msg
,
2249 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
2250 IBA_SET(CM_REP_FAILOVER_ACCEPTED
, rep_msg
, param
->failover_accepted
);
2251 IBA_SET(CM_REP_RNR_RETRY_COUNT
, rep_msg
, param
->rnr_retry_count
);
2252 IBA_SET(CM_REP_LOCAL_CA_GUID
, rep_msg
,
2253 be64_to_cpu(cm_id_priv
->id
.device
->node_guid
));
2255 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
2256 IBA_SET(CM_REP_INITIATOR_DEPTH
, rep_msg
,
2257 param
->initiator_depth
);
2258 IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL
, rep_msg
,
2259 param
->flow_control
);
2260 IBA_SET(CM_REP_SRQ
, rep_msg
, param
->srq
);
2261 IBA_SET(CM_REP_LOCAL_QPN
, rep_msg
, param
->qp_num
);
2263 IBA_SET(CM_REP_SRQ
, rep_msg
, 1);
2264 IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER
, rep_msg
, param
->qp_num
);
2267 IBA_SET(CM_REP_VENDOR_ID_L
, rep_msg
, param
->ece
.vendor_id
);
2268 IBA_SET(CM_REP_VENDOR_ID_M
, rep_msg
, param
->ece
.vendor_id
>> 8);
2269 IBA_SET(CM_REP_VENDOR_ID_H
, rep_msg
, param
->ece
.vendor_id
>> 16);
2271 if (param
->private_data
&& param
->private_data_len
)
2272 IBA_SET_MEM(CM_REP_PRIVATE_DATA
, rep_msg
, param
->private_data
,
2273 param
->private_data_len
);
2276 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
2277 struct ib_cm_rep_param
*param
)
2279 struct cm_id_private
*cm_id_priv
;
2280 struct ib_mad_send_buf
*msg
;
2281 struct cm_rep_msg
*rep_msg
;
2282 unsigned long flags
;
2285 if (param
->private_data
&&
2286 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
2289 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2290 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2291 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
2292 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
2293 trace_icm_send_rep_err(cm_id_priv
->id
.local_id
, cm_id
->state
);
2298 msg
= cm_alloc_priv_msg(cm_id_priv
, IB_CM_REP_SENT
);
2304 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
2305 cm_format_rep(rep_msg
, cm_id_priv
, param
);
2307 trace_icm_send_rep(cm_id
);
2308 ret
= ib_post_send_mad(msg
, NULL
);
2312 cm_id
->state
= IB_CM_REP_SENT
;
2313 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
2314 cm_id_priv
->responder_resources
= param
->responder_resources
;
2315 cm_id_priv
->rq_psn
= cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN
, rep_msg
));
2316 WARN_ONCE(param
->qp_num
& 0xFF000000,
2317 "IBTA declares QPN to be 24 bits, but it is 0x%X\n",
2319 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
2320 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2324 cm_free_priv_msg(msg
);
2326 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2329 EXPORT_SYMBOL(ib_send_cm_rep
);
2331 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
2332 struct cm_id_private
*cm_id_priv
,
2333 const void *private_data
,
2334 u8 private_data_len
)
2336 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
2337 IBA_SET(CM_RTU_LOCAL_COMM_ID
, rtu_msg
,
2338 be32_to_cpu(cm_id_priv
->id
.local_id
));
2339 IBA_SET(CM_RTU_REMOTE_COMM_ID
, rtu_msg
,
2340 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2342 if (private_data
&& private_data_len
)
2343 IBA_SET_MEM(CM_RTU_PRIVATE_DATA
, rtu_msg
, private_data
,
2347 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
2348 const void *private_data
,
2349 u8 private_data_len
)
2351 struct cm_id_private
*cm_id_priv
;
2352 struct ib_mad_send_buf
*msg
;
2353 unsigned long flags
;
2357 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
2360 data
= cm_copy_private_data(private_data
, private_data_len
);
2362 return PTR_ERR(data
);
2364 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2365 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2366 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
2367 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
2368 trace_icm_send_cm_rtu_err(cm_id
);
2373 msg
= cm_alloc_msg(cm_id_priv
);
2379 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
2380 private_data
, private_data_len
);
2382 trace_icm_send_rtu(cm_id
);
2383 ret
= ib_post_send_mad(msg
, NULL
);
2385 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2391 cm_id
->state
= IB_CM_ESTABLISHED
;
2392 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2393 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2396 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2400 EXPORT_SYMBOL(ib_send_cm_rtu
);
2402 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
2404 struct cm_rep_msg
*rep_msg
;
2405 struct ib_cm_rep_event_param
*param
;
2407 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2408 param
= &work
->cm_event
.param
.rep_rcvd
;
2409 param
->remote_ca_guid
=
2410 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID
, rep_msg
));
2411 param
->remote_qkey
= IBA_GET(CM_REP_LOCAL_Q_KEY
, rep_msg
);
2412 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
2413 param
->starting_psn
= IBA_GET(CM_REP_STARTING_PSN
, rep_msg
);
2414 param
->responder_resources
= IBA_GET(CM_REP_INITIATOR_DEPTH
, rep_msg
);
2415 param
->initiator_depth
= IBA_GET(CM_REP_RESPONDER_RESOURCES
, rep_msg
);
2416 param
->target_ack_delay
= IBA_GET(CM_REP_TARGET_ACK_DELAY
, rep_msg
);
2417 param
->failover_accepted
= IBA_GET(CM_REP_FAILOVER_ACCEPTED
, rep_msg
);
2418 param
->flow_control
= IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL
, rep_msg
);
2419 param
->rnr_retry_count
= IBA_GET(CM_REP_RNR_RETRY_COUNT
, rep_msg
);
2420 param
->srq
= IBA_GET(CM_REP_SRQ
, rep_msg
);
2421 param
->ece
.vendor_id
= IBA_GET(CM_REP_VENDOR_ID_H
, rep_msg
) << 16;
2422 param
->ece
.vendor_id
|= IBA_GET(CM_REP_VENDOR_ID_M
, rep_msg
) << 8;
2423 param
->ece
.vendor_id
|= IBA_GET(CM_REP_VENDOR_ID_L
, rep_msg
);
2424 param
->ece
.attr_mod
= be32_to_cpu(rep_msg
->hdr
.attr_mod
);
2426 work
->cm_event
.private_data
=
2427 IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA
, rep_msg
);
2430 static void cm_dup_rep_handler(struct cm_work
*work
)
2432 struct cm_id_private
*cm_id_priv
;
2433 struct cm_rep_msg
*rep_msg
;
2434 struct ib_mad_send_buf
*msg
= NULL
;
2437 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
2438 cm_id_priv
= cm_acquire_id(
2439 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
)),
2440 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
)));
2445 &work
->port
->counters
[CM_RECV_DUPLICATES
][CM_REP_COUNTER
]);
2446 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, true, &msg
);
2450 spin_lock_irq(&cm_id_priv
->lock
);
2451 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
2452 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
2453 cm_id_priv
->private_data
,
2454 cm_id_priv
->private_data_len
);
2455 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
2456 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2457 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
2458 cm_id_priv
->private_data
,
2459 cm_id_priv
->private_data_len
);
2462 spin_unlock_irq(&cm_id_priv
->lock
);
2464 trace_icm_send_dup_rep(&cm_id_priv
->id
);
2465 ret
= ib_post_send_mad(msg
, NULL
);
2470 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2471 free
: cm_free_msg(msg
);
2472 deref
: cm_deref_id(cm_id_priv
);
2475 static int cm_rep_handler(struct cm_work
*work
)
2477 struct cm_id_private
*cm_id_priv
;
2478 struct cm_rep_msg
*rep_msg
;
2480 struct cm_id_private
*cur_cm_id_priv
;
2481 struct cm_timewait_info
*timewait_info
;
2483 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2484 cm_id_priv
= cm_acquire_id(
2485 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
)), 0);
2487 cm_dup_rep_handler(work
);
2488 trace_icm_remote_no_priv_err(
2489 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2493 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
2495 spin_lock_irq(&cm_id_priv
->lock
);
2496 switch (cm_id_priv
->id
.state
) {
2497 case IB_CM_REQ_SENT
:
2498 case IB_CM_MRA_REQ_RCVD
:
2502 trace_icm_rep_unknown_err(
2503 IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
),
2504 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
),
2505 cm_id_priv
->id
.state
);
2506 spin_unlock_irq(&cm_id_priv
->lock
);
2510 cm_id_priv
->timewait_info
->work
.remote_id
=
2511 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
));
2512 cm_id_priv
->timewait_info
->remote_ca_guid
=
2513 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID
, rep_msg
));
2514 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2516 spin_lock(&cm
.lock
);
2517 /* Check for duplicate REP. */
2518 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
2519 spin_unlock(&cm
.lock
);
2520 spin_unlock_irq(&cm_id_priv
->lock
);
2522 trace_icm_insert_failed_err(
2523 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2526 /* Check for a stale connection. */
2527 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
2528 if (timewait_info
) {
2529 cm_remove_remote(cm_id_priv
);
2530 cur_cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2531 timewait_info
->work
.remote_id
);
2533 spin_unlock(&cm
.lock
);
2534 spin_unlock_irq(&cm_id_priv
->lock
);
2535 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
2536 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
2539 trace_icm_staleconn_err(
2540 IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
),
2541 IBA_GET(CM_REP_REMOTE_COMM_ID
, rep_msg
));
2543 if (cur_cm_id_priv
) {
2544 ib_send_cm_dreq(&cur_cm_id_priv
->id
, NULL
, 0);
2545 cm_deref_id(cur_cm_id_priv
);
2550 spin_unlock(&cm
.lock
);
2552 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
2553 cm_id_priv
->id
.remote_id
=
2554 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID
, rep_msg
));
2555 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2556 cm_id_priv
->initiator_depth
=
2557 IBA_GET(CM_REP_RESPONDER_RESOURCES
, rep_msg
);
2558 cm_id_priv
->responder_resources
=
2559 IBA_GET(CM_REP_INITIATOR_DEPTH
, rep_msg
);
2560 cm_id_priv
->sq_psn
= cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN
, rep_msg
));
2561 cm_id_priv
->rnr_retry_count
= IBA_GET(CM_REP_RNR_RETRY_COUNT
, rep_msg
);
2562 cm_id_priv
->target_ack_delay
=
2563 IBA_GET(CM_REP_TARGET_ACK_DELAY
, rep_msg
);
2564 cm_id_priv
->av
.timeout
=
2565 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2566 cm_id_priv
->av
.timeout
- 1);
2567 cm_id_priv
->alt_av
.timeout
=
2568 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2569 cm_id_priv
->alt_av
.timeout
- 1);
2571 ib_cancel_mad(cm_id_priv
->msg
);
2572 cm_queue_work_unlock(cm_id_priv
, work
);
2576 cm_deref_id(cm_id_priv
);
2580 static int cm_establish_handler(struct cm_work
*work
)
2582 struct cm_id_private
*cm_id_priv
;
2584 /* See comment in cm_establish about lookup. */
2585 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
2589 spin_lock_irq(&cm_id_priv
->lock
);
2590 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2591 spin_unlock_irq(&cm_id_priv
->lock
);
2595 ib_cancel_mad(cm_id_priv
->msg
);
2596 cm_queue_work_unlock(cm_id_priv
, work
);
2599 cm_deref_id(cm_id_priv
);
2603 static int cm_rtu_handler(struct cm_work
*work
)
2605 struct cm_id_private
*cm_id_priv
;
2606 struct cm_rtu_msg
*rtu_msg
;
2608 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2609 cm_id_priv
= cm_acquire_id(
2610 cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID
, rtu_msg
)),
2611 cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID
, rtu_msg
)));
2615 work
->cm_event
.private_data
=
2616 IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA
, rtu_msg
);
2618 spin_lock_irq(&cm_id_priv
->lock
);
2619 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
2620 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
2621 spin_unlock_irq(&cm_id_priv
->lock
);
2622 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
2626 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
2628 ib_cancel_mad(cm_id_priv
->msg
);
2629 cm_queue_work_unlock(cm_id_priv
, work
);
2632 cm_deref_id(cm_id_priv
);
2636 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
2637 struct cm_id_private
*cm_id_priv
,
2638 const void *private_data
,
2639 u8 private_data_len
)
2641 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
2642 cm_form_tid(cm_id_priv
));
2643 IBA_SET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
,
2644 be32_to_cpu(cm_id_priv
->id
.local_id
));
2645 IBA_SET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
,
2646 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2647 IBA_SET(CM_DREQ_REMOTE_QPN_EECN
, dreq_msg
,
2648 be32_to_cpu(cm_id_priv
->remote_qpn
));
2650 if (private_data
&& private_data_len
)
2651 IBA_SET_MEM(CM_DREQ_PRIVATE_DATA
, dreq_msg
, private_data
,
2655 static void cm_issue_dreq(struct cm_id_private
*cm_id_priv
)
2657 struct ib_mad_send_buf
*msg
;
2660 lockdep_assert_held(&cm_id_priv
->lock
);
2662 msg
= cm_alloc_msg(cm_id_priv
);
2666 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
, NULL
, 0);
2668 trace_icm_send_dreq(&cm_id_priv
->id
);
2669 ret
= ib_post_send_mad(msg
, NULL
);
2674 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
, const void *private_data
,
2675 u8 private_data_len
)
2677 struct cm_id_private
*cm_id_priv
=
2678 container_of(cm_id
, struct cm_id_private
, id
);
2679 struct ib_mad_send_buf
*msg
;
2680 unsigned long flags
;
2683 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2686 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2687 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2688 trace_icm_dreq_skipped(&cm_id_priv
->id
);
2693 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2694 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2695 ib_cancel_mad(cm_id_priv
->msg
);
2697 msg
= cm_alloc_priv_msg(cm_id_priv
, IB_CM_DREQ_SENT
);
2699 cm_enter_timewait(cm_id_priv
);
2704 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2705 private_data
, private_data_len
);
2707 trace_icm_send_dreq(&cm_id_priv
->id
);
2708 ret
= ib_post_send_mad(msg
, NULL
);
2710 cm_enter_timewait(cm_id_priv
);
2711 cm_free_priv_msg(msg
);
2715 cm_id_priv
->id
.state
= IB_CM_DREQ_SENT
;
2717 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2720 EXPORT_SYMBOL(ib_send_cm_dreq
);
2722 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2723 struct cm_id_private
*cm_id_priv
,
2724 const void *private_data
,
2725 u8 private_data_len
)
2727 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2728 IBA_SET(CM_DREP_LOCAL_COMM_ID
, drep_msg
,
2729 be32_to_cpu(cm_id_priv
->id
.local_id
));
2730 IBA_SET(CM_DREP_REMOTE_COMM_ID
, drep_msg
,
2731 be32_to_cpu(cm_id_priv
->id
.remote_id
));
2733 if (private_data
&& private_data_len
)
2734 IBA_SET_MEM(CM_DREP_PRIVATE_DATA
, drep_msg
, private_data
,
2738 static int cm_send_drep_locked(struct cm_id_private
*cm_id_priv
,
2739 void *private_data
, u8 private_data_len
)
2741 struct ib_mad_send_buf
*msg
;
2744 lockdep_assert_held(&cm_id_priv
->lock
);
2746 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2749 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2750 trace_icm_send_drep_err(&cm_id_priv
->id
);
2751 kfree(private_data
);
2755 cm_set_private_data(cm_id_priv
, private_data
, private_data_len
);
2756 cm_enter_timewait(cm_id_priv
);
2758 msg
= cm_alloc_msg(cm_id_priv
);
2760 return PTR_ERR(msg
);
2762 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2763 private_data
, private_data_len
);
2765 trace_icm_send_drep(&cm_id_priv
->id
);
2766 ret
= ib_post_send_mad(msg
, NULL
);
2774 int ib_send_cm_drep(struct ib_cm_id
*cm_id
, const void *private_data
,
2775 u8 private_data_len
)
2777 struct cm_id_private
*cm_id_priv
=
2778 container_of(cm_id
, struct cm_id_private
, id
);
2779 unsigned long flags
;
2783 data
= cm_copy_private_data(private_data
, private_data_len
);
2785 return PTR_ERR(data
);
2787 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2788 ret
= cm_send_drep_locked(cm_id_priv
, data
, private_data_len
);
2789 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2792 EXPORT_SYMBOL(ib_send_cm_drep
);
2794 static int cm_issue_drep(struct cm_port
*port
,
2795 struct ib_mad_recv_wc
*mad_recv_wc
)
2797 struct ib_mad_send_buf
*msg
= NULL
;
2798 struct cm_dreq_msg
*dreq_msg
;
2799 struct cm_drep_msg
*drep_msg
;
2802 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, true, &msg
);
2806 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2807 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2809 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2810 IBA_SET(CM_DREP_REMOTE_COMM_ID
, drep_msg
,
2811 IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
));
2812 IBA_SET(CM_DREP_LOCAL_COMM_ID
, drep_msg
,
2813 IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
));
2815 trace_icm_issue_drep(
2816 IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
),
2817 IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
));
2818 ret
= ib_post_send_mad(msg
, NULL
);
2825 static int cm_dreq_handler(struct cm_work
*work
)
2827 struct cm_id_private
*cm_id_priv
;
2828 struct cm_dreq_msg
*dreq_msg
;
2829 struct ib_mad_send_buf
*msg
= NULL
;
2831 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2832 cm_id_priv
= cm_acquire_id(
2833 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
)),
2834 cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
)));
2836 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
2838 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2839 trace_icm_no_priv_err(
2840 IBA_GET(CM_DREQ_LOCAL_COMM_ID
, dreq_msg
),
2841 IBA_GET(CM_DREQ_REMOTE_COMM_ID
, dreq_msg
));
2845 work
->cm_event
.private_data
=
2846 IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA
, dreq_msg
);
2848 spin_lock_irq(&cm_id_priv
->lock
);
2849 if (cm_id_priv
->local_qpn
!=
2850 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN
, dreq_msg
)))
2853 switch (cm_id_priv
->id
.state
) {
2854 case IB_CM_REP_SENT
:
2855 case IB_CM_DREQ_SENT
:
2856 case IB_CM_MRA_REP_RCVD
:
2857 ib_cancel_mad(cm_id_priv
->msg
);
2859 case IB_CM_ESTABLISHED
:
2860 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2861 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2862 ib_cancel_mad(cm_id_priv
->msg
);
2864 case IB_CM_TIMEWAIT
:
2865 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
2867 msg
= cm_alloc_response_msg_no_ah(work
->port
, work
->mad_recv_wc
,
2872 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2873 cm_id_priv
->private_data
,
2874 cm_id_priv
->private_data_len
);
2875 spin_unlock_irq(&cm_id_priv
->lock
);
2877 if (cm_create_response_msg_ah(work
->port
, work
->mad_recv_wc
, msg
) ||
2878 ib_post_send_mad(msg
, NULL
))
2881 case IB_CM_DREQ_RCVD
:
2882 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
2886 trace_icm_dreq_unknown_err(&cm_id_priv
->id
);
2889 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2890 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2891 cm_queue_work_unlock(cm_id_priv
, work
);
2894 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2895 deref
: cm_deref_id(cm_id_priv
);
2899 static int cm_drep_handler(struct cm_work
*work
)
2901 struct cm_id_private
*cm_id_priv
;
2902 struct cm_drep_msg
*drep_msg
;
2904 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2905 cm_id_priv
= cm_acquire_id(
2906 cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID
, drep_msg
)),
2907 cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID
, drep_msg
)));
2911 work
->cm_event
.private_data
=
2912 IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA
, drep_msg
);
2914 spin_lock_irq(&cm_id_priv
->lock
);
2915 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2916 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2917 spin_unlock_irq(&cm_id_priv
->lock
);
2920 cm_enter_timewait(cm_id_priv
);
2922 ib_cancel_mad(cm_id_priv
->msg
);
2923 cm_queue_work_unlock(cm_id_priv
, work
);
2926 cm_deref_id(cm_id_priv
);
2930 static int cm_send_rej_locked(struct cm_id_private
*cm_id_priv
,
2931 enum ib_cm_rej_reason reason
, void *ari
,
2932 u8 ari_length
, const void *private_data
,
2933 u8 private_data_len
)
2935 enum ib_cm_state state
= cm_id_priv
->id
.state
;
2936 struct ib_mad_send_buf
*msg
;
2939 lockdep_assert_held(&cm_id_priv
->lock
);
2941 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2942 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2945 trace_icm_send_rej(&cm_id_priv
->id
, reason
);
2948 case IB_CM_REQ_SENT
:
2949 case IB_CM_MRA_REQ_RCVD
:
2950 case IB_CM_REQ_RCVD
:
2951 case IB_CM_MRA_REQ_SENT
:
2952 case IB_CM_REP_RCVD
:
2953 case IB_CM_MRA_REP_SENT
:
2954 cm_reset_to_idle(cm_id_priv
);
2955 msg
= cm_alloc_msg(cm_id_priv
);
2957 return PTR_ERR(msg
);
2958 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
, reason
,
2959 ari
, ari_length
, private_data
, private_data_len
,
2962 case IB_CM_REP_SENT
:
2963 case IB_CM_MRA_REP_RCVD
:
2964 cm_enter_timewait(cm_id_priv
);
2965 msg
= cm_alloc_msg(cm_id_priv
);
2967 return PTR_ERR(msg
);
2968 cm_format_rej((struct cm_rej_msg
*)msg
->mad
, cm_id_priv
, reason
,
2969 ari
, ari_length
, private_data
, private_data_len
,
2973 trace_icm_send_unknown_rej_err(&cm_id_priv
->id
);
2977 ret
= ib_post_send_mad(msg
, NULL
);
2986 int ib_send_cm_rej(struct ib_cm_id
*cm_id
, enum ib_cm_rej_reason reason
,
2987 void *ari
, u8 ari_length
, const void *private_data
,
2988 u8 private_data_len
)
2990 struct cm_id_private
*cm_id_priv
=
2991 container_of(cm_id
, struct cm_id_private
, id
);
2992 unsigned long flags
;
2995 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2996 ret
= cm_send_rej_locked(cm_id_priv
, reason
, ari
, ari_length
,
2997 private_data
, private_data_len
);
2998 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3001 EXPORT_SYMBOL(ib_send_cm_rej
);
3003 static void cm_format_rej_event(struct cm_work
*work
)
3005 struct cm_rej_msg
*rej_msg
;
3006 struct ib_cm_rej_event_param
*param
;
3008 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3009 param
= &work
->cm_event
.param
.rej_rcvd
;
3010 param
->ari
= IBA_GET_MEM_PTR(CM_REJ_ARI
, rej_msg
);
3011 param
->ari_length
= IBA_GET(CM_REJ_REJECTED_INFO_LENGTH
, rej_msg
);
3012 param
->reason
= IBA_GET(CM_REJ_REASON
, rej_msg
);
3013 work
->cm_event
.private_data
=
3014 IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA
, rej_msg
);
3017 static struct cm_id_private
*cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
3019 struct cm_id_private
*cm_id_priv
;
3022 remote_id
= cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID
, rej_msg
));
3024 if (IBA_GET(CM_REJ_REASON
, rej_msg
) == IB_CM_REJ_TIMEOUT
) {
3025 cm_id_priv
= cm_find_remote_id(
3026 *((__be64
*)IBA_GET_MEM_PTR(CM_REJ_ARI
, rej_msg
)),
3028 } else if (IBA_GET(CM_REJ_MESSAGE_REJECTED
, rej_msg
) ==
3029 CM_MSG_RESPONSE_REQ
)
3030 cm_id_priv
= cm_acquire_id(
3031 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID
, rej_msg
)),
3034 cm_id_priv
= cm_acquire_id(
3035 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID
, rej_msg
)),
3041 static int cm_rej_handler(struct cm_work
*work
)
3043 struct cm_id_private
*cm_id_priv
;
3044 struct cm_rej_msg
*rej_msg
;
3046 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3047 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
3051 cm_format_rej_event(work
);
3053 spin_lock_irq(&cm_id_priv
->lock
);
3054 switch (cm_id_priv
->id
.state
) {
3055 case IB_CM_REQ_SENT
:
3056 case IB_CM_MRA_REQ_RCVD
:
3057 case IB_CM_REP_SENT
:
3058 case IB_CM_MRA_REP_RCVD
:
3059 ib_cancel_mad(cm_id_priv
->msg
);
3061 case IB_CM_REQ_RCVD
:
3062 case IB_CM_MRA_REQ_SENT
:
3063 if (IBA_GET(CM_REJ_REASON
, rej_msg
) == IB_CM_REJ_STALE_CONN
)
3064 cm_enter_timewait(cm_id_priv
);
3066 cm_reset_to_idle(cm_id_priv
);
3068 case IB_CM_DREQ_SENT
:
3069 ib_cancel_mad(cm_id_priv
->msg
);
3071 case IB_CM_REP_RCVD
:
3072 case IB_CM_MRA_REP_SENT
:
3073 cm_enter_timewait(cm_id_priv
);
3075 case IB_CM_ESTABLISHED
:
3076 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
3077 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
3078 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
3079 ib_cancel_mad(cm_id_priv
->msg
);
3080 cm_enter_timewait(cm_id_priv
);
3085 trace_icm_rej_unknown_err(&cm_id_priv
->id
);
3086 spin_unlock_irq(&cm_id_priv
->lock
);
3090 cm_queue_work_unlock(cm_id_priv
, work
);
3093 cm_deref_id(cm_id_priv
);
3097 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
3099 const void *private_data
,
3100 u8 private_data_len
)
3102 struct cm_id_private
*cm_id_priv
;
3103 struct ib_mad_send_buf
*msg
;
3104 enum ib_cm_state cm_state
;
3105 enum ib_cm_lap_state lap_state
;
3106 enum cm_msg_response msg_response
;
3108 unsigned long flags
;
3111 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
3114 data
= cm_copy_private_data(private_data
, private_data_len
);
3116 return PTR_ERR(data
);
3118 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3120 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3121 switch (cm_id_priv
->id
.state
) {
3122 case IB_CM_REQ_RCVD
:
3123 cm_state
= IB_CM_MRA_REQ_SENT
;
3124 lap_state
= cm_id
->lap_state
;
3125 msg_response
= CM_MSG_RESPONSE_REQ
;
3127 case IB_CM_REP_RCVD
:
3128 cm_state
= IB_CM_MRA_REP_SENT
;
3129 lap_state
= cm_id
->lap_state
;
3130 msg_response
= CM_MSG_RESPONSE_REP
;
3132 case IB_CM_ESTABLISHED
:
3133 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
3134 cm_state
= cm_id
->state
;
3135 lap_state
= IB_CM_MRA_LAP_SENT
;
3136 msg_response
= CM_MSG_RESPONSE_OTHER
;
3141 trace_icm_send_mra_unknown_err(&cm_id_priv
->id
);
3146 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
3147 msg
= cm_alloc_msg(cm_id_priv
);
3153 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
3154 msg_response
, service_timeout
,
3155 private_data
, private_data_len
);
3156 trace_icm_send_mra(cm_id
);
3157 ret
= ib_post_send_mad(msg
, NULL
);
3159 goto error_free_msg
;
3162 cm_id
->state
= cm_state
;
3163 cm_id
->lap_state
= lap_state
;
3164 cm_id_priv
->service_timeout
= service_timeout
;
3165 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
3166 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3172 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3176 EXPORT_SYMBOL(ib_send_cm_mra
);
3178 static struct cm_id_private
*cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
3180 switch (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
)) {
3181 case CM_MSG_RESPONSE_REQ
:
3182 return cm_acquire_id(
3183 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID
, mra_msg
)),
3185 case CM_MSG_RESPONSE_REP
:
3186 case CM_MSG_RESPONSE_OTHER
:
3187 return cm_acquire_id(
3188 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID
, mra_msg
)),
3189 cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID
, mra_msg
)));
3195 static int cm_mra_handler(struct cm_work
*work
)
3197 struct cm_id_private
*cm_id_priv
;
3198 struct cm_mra_msg
*mra_msg
;
3201 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3202 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
3206 work
->cm_event
.private_data
=
3207 IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA
, mra_msg
);
3208 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
3209 IBA_GET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
);
3210 timeout
= cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT
, mra_msg
)) +
3211 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
3213 spin_lock_irq(&cm_id_priv
->lock
);
3214 switch (cm_id_priv
->id
.state
) {
3215 case IB_CM_REQ_SENT
:
3216 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3217 CM_MSG_RESPONSE_REQ
||
3218 ib_modify_mad(cm_id_priv
->msg
, timeout
))
3220 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
3222 case IB_CM_REP_SENT
:
3223 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3224 CM_MSG_RESPONSE_REP
||
3225 ib_modify_mad(cm_id_priv
->msg
, timeout
))
3227 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
3229 case IB_CM_ESTABLISHED
:
3230 if (IBA_GET(CM_MRA_MESSAGE_MRAED
, mra_msg
) !=
3231 CM_MSG_RESPONSE_OTHER
||
3232 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
3233 ib_modify_mad(cm_id_priv
->msg
, timeout
)) {
3234 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
3236 &work
->port
->counters
[CM_RECV_DUPLICATES
]
3240 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
3242 case IB_CM_MRA_REQ_RCVD
:
3243 case IB_CM_MRA_REP_RCVD
:
3244 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
3248 trace_icm_mra_unknown_err(&cm_id_priv
->id
);
3252 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
3253 cm_id_priv
->id
.state
;
3254 cm_queue_work_unlock(cm_id_priv
, work
);
3257 spin_unlock_irq(&cm_id_priv
->lock
);
3258 cm_deref_id(cm_id_priv
);
3262 static void cm_format_path_lid_from_lap(struct cm_lap_msg
*lap_msg
,
3263 struct sa_path_rec
*path
)
3267 if (path
->rec_type
!= SA_PATH_REC_TYPE_OPA
) {
3268 sa_path_set_dlid(path
, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID
,
3270 sa_path_set_slid(path
, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID
,
3273 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3274 CM_LAP_ALTERNATE_LOCAL_PORT_GID
, lap_msg
));
3275 sa_path_set_dlid(path
, lid
);
3277 lid
= opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3278 CM_LAP_ALTERNATE_REMOTE_PORT_GID
, lap_msg
));
3279 sa_path_set_slid(path
, lid
);
3283 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
3284 struct sa_path_rec
*path
,
3285 struct cm_lap_msg
*lap_msg
)
3287 path
->dgid
= *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID
, lap_msg
);
3289 *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID
, lap_msg
);
3291 cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL
, lap_msg
));
3292 path
->hop_limit
= IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT
, lap_msg
);
3293 path
->traffic_class
= IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS
, lap_msg
);
3294 path
->reversible
= 1;
3295 path
->pkey
= cm_id_priv
->pkey
;
3296 path
->sl
= IBA_GET(CM_LAP_ALTERNATE_SL
, lap_msg
);
3297 path
->mtu_selector
= IB_SA_EQ
;
3298 path
->mtu
= cm_id_priv
->path_mtu
;
3299 path
->rate_selector
= IB_SA_EQ
;
3300 path
->rate
= IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE
, lap_msg
);
3301 path
->packet_life_time_selector
= IB_SA_EQ
;
3302 path
->packet_life_time
=
3303 IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT
, lap_msg
);
3304 path
->packet_life_time
-= (path
->packet_life_time
> 0);
3305 cm_format_path_lid_from_lap(lap_msg
, path
);
3308 static int cm_lap_handler(struct cm_work
*work
)
3310 struct cm_id_private
*cm_id_priv
;
3311 struct cm_lap_msg
*lap_msg
;
3312 struct ib_cm_lap_event_param
*param
;
3313 struct ib_mad_send_buf
*msg
= NULL
;
3314 struct rdma_ah_attr ah_attr
;
3315 struct cm_av alt_av
= {};
3318 /* Currently Alternate path messages are not supported for
3321 if (rdma_protocol_roce(work
->port
->cm_dev
->ib_device
,
3322 work
->port
->port_num
))
3325 /* todo: verify LAP request and send reject APR if invalid. */
3326 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3327 cm_id_priv
= cm_acquire_id(
3328 cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID
, lap_msg
)),
3329 cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID
, lap_msg
)));
3333 param
= &work
->cm_event
.param
.lap_rcvd
;
3334 memset(&work
->path
[0], 0, sizeof(work
->path
[1]));
3335 cm_path_set_rec_type(work
->port
->cm_dev
->ib_device
,
3336 work
->port
->port_num
, &work
->path
[0],
3337 IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID
,
3339 param
->alternate_path
= &work
->path
[0];
3340 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
3341 work
->cm_event
.private_data
=
3342 IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA
, lap_msg
);
3344 ret
= ib_init_ah_attr_from_wc(work
->port
->cm_dev
->ib_device
,
3345 work
->port
->port_num
,
3346 work
->mad_recv_wc
->wc
,
3347 work
->mad_recv_wc
->recv_buf
.grh
,
3352 ret
= cm_init_av_by_path(param
->alternate_path
, NULL
, &alt_av
);
3354 rdma_destroy_ah_attr(&ah_attr
);
3358 spin_lock_irq(&cm_id_priv
->lock
);
3359 cm_init_av_for_lap(work
->port
, work
->mad_recv_wc
->wc
,
3360 &ah_attr
, &cm_id_priv
->av
);
3361 cm_move_av_from_path(&cm_id_priv
->alt_av
, &alt_av
);
3363 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
3366 switch (cm_id_priv
->id
.lap_state
) {
3367 case IB_CM_LAP_UNINIT
:
3368 case IB_CM_LAP_IDLE
:
3370 case IB_CM_MRA_LAP_SENT
:
3371 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
3373 msg
= cm_alloc_response_msg_no_ah(work
->port
, work
->mad_recv_wc
,
3378 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
3379 CM_MSG_RESPONSE_OTHER
,
3380 cm_id_priv
->service_timeout
,
3381 cm_id_priv
->private_data
,
3382 cm_id_priv
->private_data_len
);
3383 spin_unlock_irq(&cm_id_priv
->lock
);
3385 if (cm_create_response_msg_ah(work
->port
, work
->mad_recv_wc
, msg
) ||
3386 ib_post_send_mad(msg
, NULL
))
3389 case IB_CM_LAP_RCVD
:
3390 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
3397 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
3398 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
3399 cm_queue_work_unlock(cm_id_priv
, work
);
3402 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
3403 deref
: cm_deref_id(cm_id_priv
);
3407 static int cm_apr_handler(struct cm_work
*work
)
3409 struct cm_id_private
*cm_id_priv
;
3410 struct cm_apr_msg
*apr_msg
;
3412 /* Currently Alternate path messages are not supported for
3415 if (rdma_protocol_roce(work
->port
->cm_dev
->ib_device
,
3416 work
->port
->port_num
))
3419 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3420 cm_id_priv
= cm_acquire_id(
3421 cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID
, apr_msg
)),
3422 cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID
, apr_msg
)));
3424 return -EINVAL
; /* Unmatched reply. */
3426 work
->cm_event
.param
.apr_rcvd
.ap_status
=
3427 IBA_GET(CM_APR_AR_STATUS
, apr_msg
);
3428 work
->cm_event
.param
.apr_rcvd
.apr_info
=
3429 IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION
, apr_msg
);
3430 work
->cm_event
.param
.apr_rcvd
.info_len
=
3431 IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH
, apr_msg
);
3432 work
->cm_event
.private_data
=
3433 IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA
, apr_msg
);
3435 spin_lock_irq(&cm_id_priv
->lock
);
3436 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
3437 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
3438 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
3439 spin_unlock_irq(&cm_id_priv
->lock
);
3442 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
3443 ib_cancel_mad(cm_id_priv
->msg
);
3444 cm_queue_work_unlock(cm_id_priv
, work
);
3447 cm_deref_id(cm_id_priv
);
3451 static int cm_timewait_handler(struct cm_work
*work
)
3453 struct cm_timewait_info
*timewait_info
;
3454 struct cm_id_private
*cm_id_priv
;
3456 timewait_info
= container_of(work
, struct cm_timewait_info
, work
);
3457 spin_lock_irq(&cm
.lock
);
3458 list_del(&timewait_info
->list
);
3459 spin_unlock_irq(&cm
.lock
);
3461 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
3462 timewait_info
->work
.remote_id
);
3466 spin_lock_irq(&cm_id_priv
->lock
);
3467 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
3468 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
3469 spin_unlock_irq(&cm_id_priv
->lock
);
3472 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3473 cm_queue_work_unlock(cm_id_priv
, work
);
3476 cm_deref_id(cm_id_priv
);
3480 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
3481 struct cm_id_private
*cm_id_priv
,
3482 struct ib_cm_sidr_req_param
*param
)
3484 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
3485 cm_form_tid(cm_id_priv
));
3486 IBA_SET(CM_SIDR_REQ_REQUESTID
, sidr_req_msg
,
3487 be32_to_cpu(cm_id_priv
->id
.local_id
));
3488 IBA_SET(CM_SIDR_REQ_PARTITION_KEY
, sidr_req_msg
,
3489 be16_to_cpu(param
->path
->pkey
));
3490 IBA_SET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
,
3491 be64_to_cpu(param
->service_id
));
3493 if (param
->private_data
&& param
->private_data_len
)
3494 IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA
, sidr_req_msg
,
3495 param
->private_data
, param
->private_data_len
);
3498 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
3499 struct ib_cm_sidr_req_param
*param
)
3501 struct cm_id_private
*cm_id_priv
;
3502 struct ib_mad_send_buf
*msg
;
3503 struct cm_av av
= {};
3504 unsigned long flags
;
3507 if (!param
->path
|| (param
->private_data
&&
3508 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
3511 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3512 ret
= cm_init_av_by_path(param
->path
, param
->sgid_attr
, &av
);
3516 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3517 cm_move_av_from_path(&cm_id_priv
->av
, &av
);
3518 cm_id
->service_id
= param
->service_id
;
3519 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
3520 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
3521 if (cm_id
->state
!= IB_CM_IDLE
) {
3526 msg
= cm_alloc_priv_msg(cm_id_priv
, IB_CM_SIDR_REQ_SENT
);
3532 cm_format_sidr_req((struct cm_sidr_req_msg
*)msg
->mad
, cm_id_priv
,
3535 trace_icm_send_sidr_req(&cm_id_priv
->id
);
3536 ret
= ib_post_send_mad(msg
, NULL
);
3539 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
3540 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3543 cm_free_priv_msg(msg
);
3545 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3548 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
3550 static void cm_format_sidr_req_event(struct cm_work
*work
,
3551 const struct cm_id_private
*rx_cm_id
,
3552 struct ib_cm_id
*listen_id
)
3554 struct cm_sidr_req_msg
*sidr_req_msg
;
3555 struct ib_cm_sidr_req_event_param
*param
;
3557 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3558 work
->mad_recv_wc
->recv_buf
.mad
;
3559 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
3560 param
->pkey
= IBA_GET(CM_SIDR_REQ_PARTITION_KEY
, sidr_req_msg
);
3561 param
->listen_id
= listen_id
;
3563 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
));
3564 param
->bth_pkey
= cm_get_bth_pkey(work
);
3565 param
->port
= work
->port
->port_num
;
3566 param
->sgid_attr
= rx_cm_id
->av
.ah_attr
.grh
.sgid_attr
;
3567 work
->cm_event
.private_data
=
3568 IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA
, sidr_req_msg
);
3571 static int cm_sidr_req_handler(struct cm_work
*work
)
3573 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
3574 struct cm_sidr_req_msg
*sidr_req_msg
;
3579 cm_alloc_id_priv(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3580 if (IS_ERR(cm_id_priv
))
3581 return PTR_ERR(cm_id_priv
);
3583 /* Record SGID/SLID and request ID for lookup. */
3584 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3585 work
->mad_recv_wc
->recv_buf
.mad
;
3587 cm_id_priv
->id
.remote_id
=
3588 cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID
, sidr_req_msg
));
3589 cm_id_priv
->id
.service_id
=
3590 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID
, sidr_req_msg
));
3591 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3593 wc
= work
->mad_recv_wc
->wc
;
3594 cm_id_priv
->sidr_slid
= wc
->slid
;
3595 ret
= cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3596 work
->mad_recv_wc
->recv_buf
.grh
,
3601 spin_lock_irq(&cm
.lock
);
3602 listen_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3603 if (listen_cm_id_priv
) {
3604 spin_unlock_irq(&cm
.lock
);
3605 atomic_long_inc(&work
->port
->counters
[CM_RECV_DUPLICATES
]
3606 [CM_SIDR_REQ_COUNTER
]);
3607 goto out
; /* Duplicate message. */
3609 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3610 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
3611 cm_id_priv
->id
.service_id
);
3612 if (!listen_cm_id_priv
) {
3613 spin_unlock_irq(&cm
.lock
);
3614 ib_send_cm_sidr_rep(&cm_id_priv
->id
,
3615 &(struct ib_cm_sidr_rep_param
){
3616 .status
= IB_SIDR_UNSUPPORTED
});
3617 goto out
; /* No match. */
3619 spin_unlock_irq(&cm
.lock
);
3621 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
3622 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
3625 * A SIDR ID does not need to be in the xarray since it does not receive
3626 * mads, is not placed in the remote_id or remote_qpn rbtree, and does
3627 * not enter timewait.
3630 cm_format_sidr_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
3631 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
3634 * A pointer to the listen_cm_id is held in the event, so this deref
3635 * must be after the event is delivered above.
3637 cm_deref_id(listen_cm_id_priv
);
3639 cm_destroy_id(&cm_id_priv
->id
, ret
);
3642 ib_destroy_cm_id(&cm_id_priv
->id
);
3646 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3647 struct cm_id_private
*cm_id_priv
,
3648 struct ib_cm_sidr_rep_param
*param
)
3650 cm_format_mad_ece_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3651 cm_id_priv
->tid
, param
->ece
.attr_mod
);
3652 IBA_SET(CM_SIDR_REP_REQUESTID
, sidr_rep_msg
,
3653 be32_to_cpu(cm_id_priv
->id
.remote_id
));
3654 IBA_SET(CM_SIDR_REP_STATUS
, sidr_rep_msg
, param
->status
);
3655 IBA_SET(CM_SIDR_REP_QPN
, sidr_rep_msg
, param
->qp_num
);
3656 IBA_SET(CM_SIDR_REP_SERVICEID
, sidr_rep_msg
,
3657 be64_to_cpu(cm_id_priv
->id
.service_id
));
3658 IBA_SET(CM_SIDR_REP_Q_KEY
, sidr_rep_msg
, param
->qkey
);
3659 IBA_SET(CM_SIDR_REP_VENDOR_ID_L
, sidr_rep_msg
,
3660 param
->ece
.vendor_id
& 0xFF);
3661 IBA_SET(CM_SIDR_REP_VENDOR_ID_H
, sidr_rep_msg
,
3662 (param
->ece
.vendor_id
>> 8) & 0xFF);
3664 if (param
->info
&& param
->info_length
)
3665 IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION
, sidr_rep_msg
,
3666 param
->info
, param
->info_length
);
3668 if (param
->private_data
&& param
->private_data_len
)
3669 IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA
, sidr_rep_msg
,
3670 param
->private_data
, param
->private_data_len
);
3673 static int cm_send_sidr_rep_locked(struct cm_id_private
*cm_id_priv
,
3674 struct ib_cm_sidr_rep_param
*param
)
3676 struct ib_mad_send_buf
*msg
;
3677 unsigned long flags
;
3680 lockdep_assert_held(&cm_id_priv
->lock
);
3682 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3683 (param
->private_data
&&
3684 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3687 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_RCVD
)
3690 msg
= cm_alloc_msg(cm_id_priv
);
3692 return PTR_ERR(msg
);
3694 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3696 trace_icm_send_sidr_rep(&cm_id_priv
->id
);
3697 ret
= ib_post_send_mad(msg
, NULL
);
3702 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3703 spin_lock_irqsave(&cm
.lock
, flags
);
3704 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
)) {
3705 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3706 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
3708 spin_unlock_irqrestore(&cm
.lock
, flags
);
3712 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3713 struct ib_cm_sidr_rep_param
*param
)
3715 struct cm_id_private
*cm_id_priv
=
3716 container_of(cm_id
, struct cm_id_private
, id
);
3717 unsigned long flags
;
3720 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3721 ret
= cm_send_sidr_rep_locked(cm_id_priv
, param
);
3722 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3725 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3727 static void cm_format_sidr_rep_event(struct cm_work
*work
,
3728 const struct cm_id_private
*cm_id_priv
)
3730 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3731 struct ib_cm_sidr_rep_event_param
*param
;
3733 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3734 work
->mad_recv_wc
->recv_buf
.mad
;
3735 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3736 param
->status
= IBA_GET(CM_SIDR_REP_STATUS
, sidr_rep_msg
);
3737 param
->qkey
= IBA_GET(CM_SIDR_REP_Q_KEY
, sidr_rep_msg
);
3738 param
->qpn
= IBA_GET(CM_SIDR_REP_QPN
, sidr_rep_msg
);
3739 param
->info
= IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION
,
3741 param
->info_len
= IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH
,
3743 param
->sgid_attr
= cm_id_priv
->av
.ah_attr
.grh
.sgid_attr
;
3744 work
->cm_event
.private_data
=
3745 IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA
, sidr_rep_msg
);
3748 static int cm_sidr_rep_handler(struct cm_work
*work
)
3750 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3751 struct cm_id_private
*cm_id_priv
;
3753 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3754 work
->mad_recv_wc
->recv_buf
.mad
;
3755 cm_id_priv
= cm_acquire_id(
3756 cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID
, sidr_rep_msg
)), 0);
3758 return -EINVAL
; /* Unmatched reply. */
3760 spin_lock_irq(&cm_id_priv
->lock
);
3761 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3762 spin_unlock_irq(&cm_id_priv
->lock
);
3765 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3766 ib_cancel_mad(cm_id_priv
->msg
);
3767 spin_unlock_irq(&cm_id_priv
->lock
);
3769 cm_format_sidr_rep_event(work
, cm_id_priv
);
3770 cm_process_work(cm_id_priv
, work
);
3773 cm_deref_id(cm_id_priv
);
3777 static void cm_process_send_error(struct cm_id_private
*cm_id_priv
,
3778 struct ib_mad_send_buf
*msg
,
3779 enum ib_wc_status wc_status
)
3781 enum ib_cm_state state
= (unsigned long) msg
->context
[1];
3782 struct ib_cm_event cm_event
= {};
3785 /* Discard old sends. */
3786 spin_lock_irq(&cm_id_priv
->lock
);
3787 if (msg
!= cm_id_priv
->msg
) {
3788 spin_unlock_irq(&cm_id_priv
->lock
);
3789 cm_free_priv_msg(msg
);
3792 cm_free_priv_msg(msg
);
3794 if (state
!= cm_id_priv
->id
.state
|| wc_status
== IB_WC_SUCCESS
||
3795 wc_status
== IB_WC_WR_FLUSH_ERR
)
3798 trace_icm_mad_send_err(state
, wc_status
);
3800 case IB_CM_REQ_SENT
:
3801 case IB_CM_MRA_REQ_RCVD
:
3802 cm_reset_to_idle(cm_id_priv
);
3803 cm_event
.event
= IB_CM_REQ_ERROR
;
3805 case IB_CM_REP_SENT
:
3806 case IB_CM_MRA_REP_RCVD
:
3807 cm_reset_to_idle(cm_id_priv
);
3808 cm_event
.event
= IB_CM_REP_ERROR
;
3810 case IB_CM_DREQ_SENT
:
3811 cm_enter_timewait(cm_id_priv
);
3812 cm_event
.event
= IB_CM_DREQ_ERROR
;
3814 case IB_CM_SIDR_REQ_SENT
:
3815 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3816 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3821 spin_unlock_irq(&cm_id_priv
->lock
);
3822 cm_event
.param
.send_status
= wc_status
;
3824 /* No other events can occur on the cm_id at this point. */
3825 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3827 ib_destroy_cm_id(&cm_id_priv
->id
);
3830 spin_unlock_irq(&cm_id_priv
->lock
);
3833 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3834 struct ib_mad_send_wc
*mad_send_wc
)
3836 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3837 struct cm_id_private
*cm_id_priv
;
3838 struct cm_port
*port
;
3841 port
= mad_agent
->context
;
3842 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3843 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3845 if (msg
->context
[0] == CM_DIRECT_RETRY_CTX
) {
3849 cm_id_priv
= msg
->context
[0];
3852 atomic_long_add(1 + msg
->retries
, &port
->counters
[CM_XMIT
][attr_index
]);
3854 atomic_long_add(msg
->retries
,
3855 &port
->counters
[CM_XMIT_RETRIES
][attr_index
]);
3858 cm_process_send_error(cm_id_priv
, msg
, mad_send_wc
->status
);
3863 static void cm_work_handler(struct work_struct
*_work
)
3865 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3868 switch (work
->cm_event
.event
) {
3869 case IB_CM_REQ_RECEIVED
:
3870 ret
= cm_req_handler(work
);
3872 case IB_CM_MRA_RECEIVED
:
3873 ret
= cm_mra_handler(work
);
3875 case IB_CM_REJ_RECEIVED
:
3876 ret
= cm_rej_handler(work
);
3878 case IB_CM_REP_RECEIVED
:
3879 ret
= cm_rep_handler(work
);
3881 case IB_CM_RTU_RECEIVED
:
3882 ret
= cm_rtu_handler(work
);
3884 case IB_CM_USER_ESTABLISHED
:
3885 ret
= cm_establish_handler(work
);
3887 case IB_CM_DREQ_RECEIVED
:
3888 ret
= cm_dreq_handler(work
);
3890 case IB_CM_DREP_RECEIVED
:
3891 ret
= cm_drep_handler(work
);
3893 case IB_CM_SIDR_REQ_RECEIVED
:
3894 ret
= cm_sidr_req_handler(work
);
3896 case IB_CM_SIDR_REP_RECEIVED
:
3897 ret
= cm_sidr_rep_handler(work
);
3899 case IB_CM_LAP_RECEIVED
:
3900 ret
= cm_lap_handler(work
);
3902 case IB_CM_APR_RECEIVED
:
3903 ret
= cm_apr_handler(work
);
3905 case IB_CM_TIMEWAIT_EXIT
:
3906 ret
= cm_timewait_handler(work
);
3909 trace_icm_handler_err(work
->cm_event
.event
);
3917 static int cm_establish(struct ib_cm_id
*cm_id
)
3919 struct cm_id_private
*cm_id_priv
;
3920 struct cm_work
*work
;
3921 unsigned long flags
;
3923 struct cm_device
*cm_dev
;
3925 cm_dev
= ib_get_client_data(cm_id
->device
, &cm_client
);
3929 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3933 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3934 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3935 switch (cm_id
->state
) {
3936 case IB_CM_REP_SENT
:
3937 case IB_CM_MRA_REP_RCVD
:
3938 cm_id
->state
= IB_CM_ESTABLISHED
;
3940 case IB_CM_ESTABLISHED
:
3944 trace_icm_establish_err(cm_id
);
3948 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3956 * The CM worker thread may try to destroy the cm_id before it
3957 * can execute this work item. To prevent potential deadlock,
3958 * we need to find the cm_id once we're in the context of the
3959 * worker thread, rather than holding a reference on it.
3961 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3962 work
->local_id
= cm_id
->local_id
;
3963 work
->remote_id
= cm_id
->remote_id
;
3964 work
->mad_recv_wc
= NULL
;
3965 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3967 /* Check if the device started its remove_one */
3968 spin_lock_irqsave(&cm
.lock
, flags
);
3969 if (!cm_dev
->going_down
) {
3970 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3975 spin_unlock_irqrestore(&cm
.lock
, flags
);
3981 static int cm_migrate(struct ib_cm_id
*cm_id
)
3983 struct cm_id_private
*cm_id_priv
;
3984 unsigned long flags
;
3987 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3988 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3989 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3990 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3991 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3992 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3993 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3996 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4001 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
4006 case IB_EVENT_COMM_EST
:
4007 ret
= cm_establish(cm_id
);
4009 case IB_EVENT_PATH_MIG
:
4010 ret
= cm_migrate(cm_id
);
4017 EXPORT_SYMBOL(ib_cm_notify
);
4019 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
4020 struct ib_mad_send_buf
*send_buf
,
4021 struct ib_mad_recv_wc
*mad_recv_wc
)
4023 struct cm_port
*port
= mad_agent
->context
;
4024 struct cm_work
*work
;
4025 enum ib_cm_event_type event
;
4026 bool alt_path
= false;
4031 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
4032 case CM_REQ_ATTR_ID
:
4033 alt_path
= cm_req_has_alt_path((struct cm_req_msg
*)
4034 mad_recv_wc
->recv_buf
.mad
);
4035 paths
= 1 + (alt_path
!= 0);
4036 event
= IB_CM_REQ_RECEIVED
;
4038 case CM_MRA_ATTR_ID
:
4039 event
= IB_CM_MRA_RECEIVED
;
4041 case CM_REJ_ATTR_ID
:
4042 event
= IB_CM_REJ_RECEIVED
;
4044 case CM_REP_ATTR_ID
:
4045 event
= IB_CM_REP_RECEIVED
;
4047 case CM_RTU_ATTR_ID
:
4048 event
= IB_CM_RTU_RECEIVED
;
4050 case CM_DREQ_ATTR_ID
:
4051 event
= IB_CM_DREQ_RECEIVED
;
4053 case CM_DREP_ATTR_ID
:
4054 event
= IB_CM_DREP_RECEIVED
;
4056 case CM_SIDR_REQ_ATTR_ID
:
4057 event
= IB_CM_SIDR_REQ_RECEIVED
;
4059 case CM_SIDR_REP_ATTR_ID
:
4060 event
= IB_CM_SIDR_REP_RECEIVED
;
4062 case CM_LAP_ATTR_ID
:
4064 event
= IB_CM_LAP_RECEIVED
;
4066 case CM_APR_ATTR_ID
:
4067 event
= IB_CM_APR_RECEIVED
;
4070 ib_free_recv_mad(mad_recv_wc
);
4074 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
4075 atomic_long_inc(&port
->counters
[CM_RECV
][attr_id
- CM_ATTR_ID_OFFSET
]);
4077 work
= kmalloc(struct_size(work
, path
, paths
), GFP_KERNEL
);
4079 ib_free_recv_mad(mad_recv_wc
);
4083 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
4084 work
->cm_event
.event
= event
;
4085 work
->mad_recv_wc
= mad_recv_wc
;
4088 /* Check if the device started its remove_one */
4089 spin_lock_irq(&cm
.lock
);
4090 if (!port
->cm_dev
->going_down
)
4091 queue_delayed_work(cm
.wq
, &work
->work
, 0);
4094 spin_unlock_irq(&cm
.lock
);
4098 ib_free_recv_mad(mad_recv_wc
);
4102 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
4103 struct ib_qp_attr
*qp_attr
,
4106 unsigned long flags
;
4109 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4110 switch (cm_id_priv
->id
.state
) {
4111 case IB_CM_REQ_SENT
:
4112 case IB_CM_MRA_REQ_RCVD
:
4113 case IB_CM_REQ_RCVD
:
4114 case IB_CM_MRA_REQ_SENT
:
4115 case IB_CM_REP_RCVD
:
4116 case IB_CM_MRA_REP_SENT
:
4117 case IB_CM_REP_SENT
:
4118 case IB_CM_MRA_REP_RCVD
:
4119 case IB_CM_ESTABLISHED
:
4120 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
4121 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
4122 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
4123 if (cm_id_priv
->responder_resources
) {
4124 struct ib_device
*ib_dev
= cm_id_priv
->id
.device
;
4125 u64 support_flush
= ib_dev
->attrs
.device_cap_flags
&
4126 (IB_DEVICE_FLUSH_GLOBAL
| IB_DEVICE_FLUSH_PERSISTENT
);
4127 u32 flushable
= support_flush
?
4128 (IB_ACCESS_FLUSH_GLOBAL
|
4129 IB_ACCESS_FLUSH_PERSISTENT
) : 0;
4131 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
4132 IB_ACCESS_REMOTE_ATOMIC
|
4135 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
4136 if (cm_id_priv
->av
.port
)
4137 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
4141 trace_icm_qp_init_err(&cm_id_priv
->id
);
4145 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4149 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
4150 struct ib_qp_attr
*qp_attr
,
4153 unsigned long flags
;
4156 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4157 switch (cm_id_priv
->id
.state
) {
4158 case IB_CM_REQ_RCVD
:
4159 case IB_CM_MRA_REQ_SENT
:
4160 case IB_CM_REP_RCVD
:
4161 case IB_CM_MRA_REP_SENT
:
4162 case IB_CM_REP_SENT
:
4163 case IB_CM_MRA_REP_RCVD
:
4164 case IB_CM_ESTABLISHED
:
4165 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
4166 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
4167 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
4168 if ((qp_attr
->ah_attr
.type
== RDMA_AH_ATTR_TYPE_IB
) &&
4169 cm_id_priv
->av
.dlid_datapath
&&
4170 (cm_id_priv
->av
.dlid_datapath
!= 0xffff))
4171 qp_attr
->ah_attr
.ib
.dlid
= cm_id_priv
->av
.dlid_datapath
;
4172 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
4173 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
4174 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
4175 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
4176 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
4177 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
4178 IB_QP_MIN_RNR_TIMER
;
4179 qp_attr
->max_dest_rd_atomic
=
4180 cm_id_priv
->responder_resources
;
4181 qp_attr
->min_rnr_timer
= 0;
4183 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
) &&
4184 cm_id_priv
->alt_av
.port
) {
4185 *qp_attr_mask
|= IB_QP_ALT_PATH
;
4186 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
4187 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
4188 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
4189 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
4194 trace_icm_qp_rtr_err(&cm_id_priv
->id
);
4198 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4202 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
4203 struct ib_qp_attr
*qp_attr
,
4206 unsigned long flags
;
4209 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
4210 switch (cm_id_priv
->id
.state
) {
4211 /* Allow transition to RTS before sending REP */
4212 case IB_CM_REQ_RCVD
:
4213 case IB_CM_MRA_REQ_SENT
:
4215 case IB_CM_REP_RCVD
:
4216 case IB_CM_MRA_REP_SENT
:
4217 case IB_CM_REP_SENT
:
4218 case IB_CM_MRA_REP_RCVD
:
4219 case IB_CM_ESTABLISHED
:
4220 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
4221 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
4222 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
4223 switch (cm_id_priv
->qp_type
) {
4225 case IB_QPT_XRC_INI
:
4226 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
4227 IB_QP_MAX_QP_RD_ATOMIC
;
4228 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
4229 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
4230 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
4232 case IB_QPT_XRC_TGT
:
4233 *qp_attr_mask
|= IB_QP_TIMEOUT
;
4234 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
4239 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
)) {
4240 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
4241 qp_attr
->path_mig_state
= IB_MIG_REARM
;
4244 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
4245 if (cm_id_priv
->alt_av
.port
)
4246 qp_attr
->alt_port_num
=
4247 cm_id_priv
->alt_av
.port
->port_num
;
4248 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
4249 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
4250 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
4251 qp_attr
->path_mig_state
= IB_MIG_REARM
;
4256 trace_icm_qp_rts_err(&cm_id_priv
->id
);
4260 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
4264 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
4265 struct ib_qp_attr
*qp_attr
,
4268 struct cm_id_private
*cm_id_priv
;
4271 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
4272 switch (qp_attr
->qp_state
) {
4274 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4277 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4280 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
4288 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
4290 static ssize_t
cm_show_counter(struct ib_device
*ibdev
, u32 port_num
,
4291 struct ib_port_attribute
*attr
, char *buf
)
4293 struct cm_counter_attribute
*cm_attr
=
4294 container_of(attr
, struct cm_counter_attribute
, attr
);
4295 struct cm_device
*cm_dev
= ib_get_client_data(ibdev
, &cm_client
);
4297 if (WARN_ON(!cm_dev
))
4303 &cm_dev
->port
[port_num
- 1]
4304 ->counters
[cm_attr
->group
][cm_attr
->index
]));
4307 #define CM_COUNTER_ATTR(_name, _group, _index) \
4309 .attr = __ATTR(_name, 0444, cm_show_counter, NULL), \
4310 .group = _group, .index = _index \
4313 #define CM_COUNTER_GROUP(_group, _name) \
4314 static struct cm_counter_attribute cm_counter_attr_##_group[] = { \
4315 CM_COUNTER_ATTR(req, _group, CM_REQ_COUNTER), \
4316 CM_COUNTER_ATTR(mra, _group, CM_MRA_COUNTER), \
4317 CM_COUNTER_ATTR(rej, _group, CM_REJ_COUNTER), \
4318 CM_COUNTER_ATTR(rep, _group, CM_REP_COUNTER), \
4319 CM_COUNTER_ATTR(rtu, _group, CM_RTU_COUNTER), \
4320 CM_COUNTER_ATTR(dreq, _group, CM_DREQ_COUNTER), \
4321 CM_COUNTER_ATTR(drep, _group, CM_DREP_COUNTER), \
4322 CM_COUNTER_ATTR(sidr_req, _group, CM_SIDR_REQ_COUNTER), \
4323 CM_COUNTER_ATTR(sidr_rep, _group, CM_SIDR_REP_COUNTER), \
4324 CM_COUNTER_ATTR(lap, _group, CM_LAP_COUNTER), \
4325 CM_COUNTER_ATTR(apr, _group, CM_APR_COUNTER), \
4327 static struct attribute *cm_counter_attrs_##_group[] = { \
4328 &cm_counter_attr_##_group[0].attr.attr, \
4329 &cm_counter_attr_##_group[1].attr.attr, \
4330 &cm_counter_attr_##_group[2].attr.attr, \
4331 &cm_counter_attr_##_group[3].attr.attr, \
4332 &cm_counter_attr_##_group[4].attr.attr, \
4333 &cm_counter_attr_##_group[5].attr.attr, \
4334 &cm_counter_attr_##_group[6].attr.attr, \
4335 &cm_counter_attr_##_group[7].attr.attr, \
4336 &cm_counter_attr_##_group[8].attr.attr, \
4337 &cm_counter_attr_##_group[9].attr.attr, \
4338 &cm_counter_attr_##_group[10].attr.attr, \
4341 static const struct attribute_group cm_counter_group_##_group = { \
4343 .attrs = cm_counter_attrs_##_group, \
4346 CM_COUNTER_GROUP(CM_XMIT
, "cm_tx_msgs")
4347 CM_COUNTER_GROUP(CM_XMIT_RETRIES
, "cm_tx_retries")
4348 CM_COUNTER_GROUP(CM_RECV
, "cm_rx_msgs")
4349 CM_COUNTER_GROUP(CM_RECV_DUPLICATES
, "cm_rx_duplicates")
4351 static const struct attribute_group
*cm_counter_groups
[] = {
4352 &cm_counter_group_CM_XMIT
,
4353 &cm_counter_group_CM_XMIT_RETRIES
,
4354 &cm_counter_group_CM_RECV
,
4355 &cm_counter_group_CM_RECV_DUPLICATES
,
4359 static int cm_add_one(struct ib_device
*ib_device
)
4361 struct cm_device
*cm_dev
;
4362 struct cm_port
*port
;
4363 struct ib_mad_reg_req reg_req
= {
4364 .mgmt_class
= IB_MGMT_CLASS_CM
,
4365 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
4367 struct ib_port_modify port_modify
= {
4368 .set_port_cap_mask
= IB_PORT_CM_SUP
4370 unsigned long flags
;
4375 cm_dev
= kzalloc(struct_size(cm_dev
, port
, ib_device
->phys_port_cnt
),
4380 kref_init(&cm_dev
->kref
);
4381 spin_lock_init(&cm_dev
->mad_agent_lock
);
4382 cm_dev
->ib_device
= ib_device
;
4383 cm_dev
->ack_delay
= ib_device
->attrs
.local_ca_ack_delay
;
4384 cm_dev
->going_down
= 0;
4386 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
4388 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
4389 rdma_for_each_port (ib_device
, i
) {
4390 if (!rdma_cap_ib_cm(ib_device
, i
))
4393 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
4399 cm_dev
->port
[i
-1] = port
;
4400 port
->cm_dev
= cm_dev
;
4403 ret
= ib_port_register_client_groups(ib_device
, i
,
4408 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
4416 if (IS_ERR(port
->mad_agent
)) {
4417 ret
= PTR_ERR(port
->mad_agent
);
4421 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
4433 write_lock_irqsave(&cm
.device_lock
, flags
);
4434 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
4435 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4439 ib_unregister_mad_agent(port
->mad_agent
);
4441 ib_port_unregister_client_groups(ib_device
, i
, cm_counter_groups
);
4443 port_modify
.set_port_cap_mask
= 0;
4444 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
4446 if (!rdma_cap_ib_cm(ib_device
, i
))
4449 port
= cm_dev
->port
[i
-1];
4450 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4451 ib_unregister_mad_agent(port
->mad_agent
);
4452 ib_port_unregister_client_groups(ib_device
, i
,
4456 cm_device_put(cm_dev
);
4460 static void cm_remove_one(struct ib_device
*ib_device
, void *client_data
)
4462 struct cm_device
*cm_dev
= client_data
;
4463 struct cm_port
*port
;
4464 struct ib_port_modify port_modify
= {
4465 .clr_port_cap_mask
= IB_PORT_CM_SUP
4467 unsigned long flags
;
4470 write_lock_irqsave(&cm
.device_lock
, flags
);
4471 list_del(&cm_dev
->list
);
4472 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4474 spin_lock_irq(&cm
.lock
);
4475 cm_dev
->going_down
= 1;
4476 spin_unlock_irq(&cm
.lock
);
4478 rdma_for_each_port (ib_device
, i
) {
4479 struct ib_mad_agent
*mad_agent
;
4481 if (!rdma_cap_ib_cm(ib_device
, i
))
4484 port
= cm_dev
->port
[i
-1];
4485 mad_agent
= port
->mad_agent
;
4486 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4488 * We flush the queue here after the going_down set, this
4489 * verify that no new works will be queued in the recv handler,
4490 * after that we can call the unregister_mad_agent
4492 flush_workqueue(cm
.wq
);
4494 * The above ensures no call paths from the work are running,
4495 * the remaining paths all take the mad_agent_lock.
4497 spin_lock(&cm_dev
->mad_agent_lock
);
4498 port
->mad_agent
= NULL
;
4499 spin_unlock(&cm_dev
->mad_agent_lock
);
4500 ib_unregister_mad_agent(mad_agent
);
4501 ib_port_unregister_client_groups(ib_device
, i
,
4505 cm_device_put(cm_dev
);
4508 static int __init
ib_cm_init(void)
4512 INIT_LIST_HEAD(&cm
.device_list
);
4513 rwlock_init(&cm
.device_lock
);
4514 spin_lock_init(&cm
.lock
);
4515 cm
.listen_service_table
= RB_ROOT
;
4516 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
4517 cm
.remote_id_table
= RB_ROOT
;
4518 cm
.remote_qp_table
= RB_ROOT
;
4519 cm
.remote_sidr_table
= RB_ROOT
;
4520 xa_init_flags(&cm
.local_id_table
, XA_FLAGS_ALLOC
);
4521 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
4522 INIT_LIST_HEAD(&cm
.timewait_list
);
4524 cm
.wq
= alloc_workqueue("ib_cm", 0, 1);
4530 ret
= ib_register_client(&cm_client
);
4536 destroy_workqueue(cm
.wq
);
4541 static void __exit
ib_cm_cleanup(void)
4543 struct cm_timewait_info
*timewait_info
, *tmp
;
4545 spin_lock_irq(&cm
.lock
);
4546 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
4547 cancel_delayed_work(&timewait_info
->work
.work
);
4548 spin_unlock_irq(&cm
.lock
);
4550 ib_unregister_client(&cm_client
);
4551 destroy_workqueue(cm
.wq
);
4553 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
4554 list_del(&timewait_info
->list
);
4555 kfree(timewait_info
);
4558 WARN_ON(!xa_empty(&cm
.local_id_table
));
4561 module_init(ib_cm_init
);
4562 module_exit(ib_cm_cleanup
);