2 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/completion.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
50 #include <linux/etherdevice.h>
52 #include <rdma/ib_cache.h>
53 #include <rdma/ib_cm.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("InfiniBand CM");
58 MODULE_LICENSE("Dual BSD/GPL");
60 static const char * const ibcm_rej_reason_strs
[] = {
61 [IB_CM_REJ_NO_QP
] = "no QP",
62 [IB_CM_REJ_NO_EEC
] = "no EEC",
63 [IB_CM_REJ_NO_RESOURCES
] = "no resources",
64 [IB_CM_REJ_TIMEOUT
] = "timeout",
65 [IB_CM_REJ_UNSUPPORTED
] = "unsupported",
66 [IB_CM_REJ_INVALID_COMM_ID
] = "invalid comm ID",
67 [IB_CM_REJ_INVALID_COMM_INSTANCE
] = "invalid comm instance",
68 [IB_CM_REJ_INVALID_SERVICE_ID
] = "invalid service ID",
69 [IB_CM_REJ_INVALID_TRANSPORT_TYPE
] = "invalid transport type",
70 [IB_CM_REJ_STALE_CONN
] = "stale conn",
71 [IB_CM_REJ_RDC_NOT_EXIST
] = "RDC not exist",
72 [IB_CM_REJ_INVALID_GID
] = "invalid GID",
73 [IB_CM_REJ_INVALID_LID
] = "invalid LID",
74 [IB_CM_REJ_INVALID_SL
] = "invalid SL",
75 [IB_CM_REJ_INVALID_TRAFFIC_CLASS
] = "invalid traffic class",
76 [IB_CM_REJ_INVALID_HOP_LIMIT
] = "invalid hop limit",
77 [IB_CM_REJ_INVALID_PACKET_RATE
] = "invalid packet rate",
78 [IB_CM_REJ_INVALID_ALT_GID
] = "invalid alt GID",
79 [IB_CM_REJ_INVALID_ALT_LID
] = "invalid alt LID",
80 [IB_CM_REJ_INVALID_ALT_SL
] = "invalid alt SL",
81 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS
] = "invalid alt traffic class",
82 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT
] = "invalid alt hop limit",
83 [IB_CM_REJ_INVALID_ALT_PACKET_RATE
] = "invalid alt packet rate",
84 [IB_CM_REJ_PORT_CM_REDIRECT
] = "port CM redirect",
85 [IB_CM_REJ_PORT_REDIRECT
] = "port redirect",
86 [IB_CM_REJ_INVALID_MTU
] = "invalid MTU",
87 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES
] = "insufficient resp resources",
88 [IB_CM_REJ_CONSUMER_DEFINED
] = "consumer defined",
89 [IB_CM_REJ_INVALID_RNR_RETRY
] = "invalid RNR retry",
90 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
] = "duplicate local comm ID",
91 [IB_CM_REJ_INVALID_CLASS_VERSION
] = "invalid class version",
92 [IB_CM_REJ_INVALID_FLOW_LABEL
] = "invalid flow label",
93 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL
] = "invalid alt flow label",
96 const char *__attribute_const__
ibcm_reject_msg(int reason
)
98 size_t index
= reason
;
100 if (index
< ARRAY_SIZE(ibcm_rej_reason_strs
) &&
101 ibcm_rej_reason_strs
[index
])
102 return ibcm_rej_reason_strs
[index
];
104 return "unrecognized reason";
106 EXPORT_SYMBOL(ibcm_reject_msg
);
108 static void cm_add_one(struct ib_device
*device
);
109 static void cm_remove_one(struct ib_device
*device
, void *client_data
);
111 static struct ib_client cm_client
= {
114 .remove
= cm_remove_one
117 static struct ib_cm
{
119 struct list_head device_list
;
120 rwlock_t device_lock
;
121 struct rb_root listen_service_table
;
122 u64 listen_service_id
;
123 /* struct rb_root peer_service_table; todo: fix peer to peer */
124 struct rb_root remote_qp_table
;
125 struct rb_root remote_id_table
;
126 struct rb_root remote_sidr_table
;
127 struct idr local_id_table
;
128 __be32 random_id_operand
;
129 struct list_head timewait_list
;
130 struct workqueue_struct
*wq
;
131 /* Sync on cm change port state */
132 spinlock_t state_lock
;
135 /* Counter indexes ordered by attribute ID */
149 CM_ATTR_ID_OFFSET
= 0x0010,
160 static char const counter_group_names
[CM_COUNTER_GROUPS
]
161 [sizeof("cm_rx_duplicates")] = {
162 "cm_tx_msgs", "cm_tx_retries",
163 "cm_rx_msgs", "cm_rx_duplicates"
166 struct cm_counter_group
{
168 atomic_long_t counter
[CM_ATTR_COUNT
];
171 struct cm_counter_attribute
{
172 struct attribute attr
;
176 #define CM_COUNTER_ATTR(_name, _index) \
177 struct cm_counter_attribute cm_##_name##_counter_attr = { \
178 .attr = { .name = __stringify(_name), .mode = 0444 }, \
182 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
183 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
184 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
185 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
186 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
187 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
188 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
189 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
190 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
191 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
192 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
194 static struct attribute
*cm_counter_default_attrs
[] = {
195 &cm_req_counter_attr
.attr
,
196 &cm_mra_counter_attr
.attr
,
197 &cm_rej_counter_attr
.attr
,
198 &cm_rep_counter_attr
.attr
,
199 &cm_rtu_counter_attr
.attr
,
200 &cm_dreq_counter_attr
.attr
,
201 &cm_drep_counter_attr
.attr
,
202 &cm_sidr_req_counter_attr
.attr
,
203 &cm_sidr_rep_counter_attr
.attr
,
204 &cm_lap_counter_attr
.attr
,
205 &cm_apr_counter_attr
.attr
,
210 struct cm_device
*cm_dev
;
211 struct ib_mad_agent
*mad_agent
;
212 struct kobject port_obj
;
214 struct list_head cm_priv_prim_list
;
215 struct list_head cm_priv_altr_list
;
216 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
220 struct list_head list
;
221 struct ib_device
*ib_device
;
222 struct device
*device
;
225 struct cm_port
*port
[0];
229 struct cm_port
*port
;
231 struct rdma_ah_attr ah_attr
;
237 struct delayed_work work
;
238 struct list_head list
;
239 struct cm_port
*port
;
240 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
241 __be32 local_id
; /* Established / timewait */
243 struct ib_cm_event cm_event
;
244 struct sa_path_rec path
[0];
247 struct cm_timewait_info
{
248 struct cm_work work
; /* Must be first. */
249 struct list_head list
;
250 struct rb_node remote_qp_node
;
251 struct rb_node remote_id_node
;
252 __be64 remote_ca_guid
;
254 u8 inserted_remote_qp
;
255 u8 inserted_remote_id
;
258 struct cm_id_private
{
261 struct rb_node service_node
;
262 struct rb_node sidr_id_node
;
263 spinlock_t lock
; /* Do not acquire inside cm.lock */
264 struct completion comp
;
266 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
267 * Protected by the cm.lock spinlock. */
268 int listen_sharecount
;
270 struct ib_mad_send_buf
*msg
;
271 struct cm_timewait_info
*timewait_info
;
272 /* todo: use alternate port on send failure */
280 enum ib_qp_type qp_type
;
284 enum ib_mtu path_mtu
;
289 u8 responder_resources
;
296 struct list_head prim_list
;
297 struct list_head altr_list
;
298 /* Indicates that the send port mad is registered and av is set */
299 int prim_send_port_not_ready
;
300 int altr_send_port_not_ready
;
302 struct list_head work_list
;
306 static void cm_work_handler(struct work_struct
*work
);
308 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
310 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
311 complete(&cm_id_priv
->comp
);
314 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
315 struct ib_mad_send_buf
**msg
)
317 struct ib_mad_agent
*mad_agent
;
318 struct ib_mad_send_buf
*m
;
321 unsigned long flags
, flags2
;
324 /* don't let the port to be released till the agent is down */
325 spin_lock_irqsave(&cm
.state_lock
, flags2
);
326 spin_lock_irqsave(&cm
.lock
, flags
);
327 if (!cm_id_priv
->prim_send_port_not_ready
)
328 av
= &cm_id_priv
->av
;
329 else if (!cm_id_priv
->altr_send_port_not_ready
&&
330 (cm_id_priv
->alt_av
.port
))
331 av
= &cm_id_priv
->alt_av
;
333 pr_info("%s: not valid CM id\n", __func__
);
335 spin_unlock_irqrestore(&cm
.lock
, flags
);
338 spin_unlock_irqrestore(&cm
.lock
, flags
);
339 /* Make sure the port haven't released the mad yet */
340 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
342 pr_info("%s: not a valid MAD agent\n", __func__
);
346 ah
= rdma_create_ah(mad_agent
->qp
->pd
, &av
->ah_attr
);
352 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
354 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
356 IB_MGMT_BASE_VERSION
);
363 /* Timeout set by caller if response is expected. */
365 m
->retries
= cm_id_priv
->max_cm_retries
;
367 atomic_inc(&cm_id_priv
->refcount
);
368 m
->context
[0] = cm_id_priv
;
372 spin_unlock_irqrestore(&cm
.state_lock
, flags2
);
376 static int cm_alloc_response_msg(struct cm_port
*port
,
377 struct ib_mad_recv_wc
*mad_recv_wc
,
378 struct ib_mad_send_buf
**msg
)
380 struct ib_mad_send_buf
*m
;
383 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
384 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
388 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
389 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
391 IB_MGMT_BASE_VERSION
);
401 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
403 rdma_destroy_ah(msg
->ah
);
405 cm_deref_id(msg
->context
[0]);
406 ib_free_send_mad(msg
);
409 static void * cm_copy_private_data(const void *private_data
,
414 if (!private_data
|| !private_data_len
)
417 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
419 return ERR_PTR(-ENOMEM
);
424 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
425 void *private_data
, u8 private_data_len
)
427 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
428 kfree(cm_id_priv
->private_data
);
430 cm_id_priv
->private_data
= private_data
;
431 cm_id_priv
->private_data_len
= private_data_len
;
434 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
435 struct ib_grh
*grh
, struct cm_av
*av
)
438 av
->pkey_index
= wc
->pkey_index
;
439 ib_init_ah_from_wc(port
->cm_dev
->ib_device
, port
->port_num
, wc
,
443 static int cm_init_av_by_path(struct sa_path_rec
*path
, struct cm_av
*av
,
444 struct cm_id_private
*cm_id_priv
)
446 struct cm_device
*cm_dev
;
447 struct cm_port
*port
= NULL
;
451 struct net_device
*ndev
= ib_get_ndev_from_path(path
);
453 read_lock_irqsave(&cm
.device_lock
, flags
);
454 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
455 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
456 sa_conv_pathrec_to_gid_type(path
),
458 port
= cm_dev
->port
[p
-1];
462 read_unlock_irqrestore(&cm
.device_lock
, flags
);
470 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
471 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
476 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
478 av
->timeout
= path
->packet_life_time
+ 1;
480 spin_lock_irqsave(&cm
.lock
, flags
);
481 if (&cm_id_priv
->av
== av
)
482 list_add_tail(&cm_id_priv
->prim_list
, &port
->cm_priv_prim_list
);
483 else if (&cm_id_priv
->alt_av
== av
)
484 list_add_tail(&cm_id_priv
->altr_list
, &port
->cm_priv_altr_list
);
488 spin_unlock_irqrestore(&cm
.lock
, flags
);
493 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
498 idr_preload(GFP_KERNEL
);
499 spin_lock_irqsave(&cm
.lock
, flags
);
501 id
= idr_alloc_cyclic(&cm
.local_id_table
, cm_id_priv
, 0, 0, GFP_NOWAIT
);
503 spin_unlock_irqrestore(&cm
.lock
, flags
);
506 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
507 return id
< 0 ? id
: 0;
510 static void cm_free_id(__be32 local_id
)
512 spin_lock_irq(&cm
.lock
);
513 idr_remove(&cm
.local_id_table
,
514 (__force
int) (local_id
^ cm
.random_id_operand
));
515 spin_unlock_irq(&cm
.lock
);
518 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
520 struct cm_id_private
*cm_id_priv
;
522 cm_id_priv
= idr_find(&cm
.local_id_table
,
523 (__force
int) (local_id
^ cm
.random_id_operand
));
525 if (cm_id_priv
->id
.remote_id
== remote_id
)
526 atomic_inc(&cm_id_priv
->refcount
);
534 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
536 struct cm_id_private
*cm_id_priv
;
538 spin_lock_irq(&cm
.lock
);
539 cm_id_priv
= cm_get_id(local_id
, remote_id
);
540 spin_unlock_irq(&cm
.lock
);
546 * Trivial helpers to strip endian annotation and compare; the
547 * endianness doesn't actually matter since we just need a stable
548 * order for the RB tree.
550 static int be32_lt(__be32 a
, __be32 b
)
552 return (__force u32
) a
< (__force u32
) b
;
555 static int be32_gt(__be32 a
, __be32 b
)
557 return (__force u32
) a
> (__force u32
) b
;
560 static int be64_lt(__be64 a
, __be64 b
)
562 return (__force u64
) a
< (__force u64
) b
;
565 static int be64_gt(__be64 a
, __be64 b
)
567 return (__force u64
) a
> (__force u64
) b
;
570 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
572 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
573 struct rb_node
*parent
= NULL
;
574 struct cm_id_private
*cur_cm_id_priv
;
575 __be64 service_id
= cm_id_priv
->id
.service_id
;
576 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
580 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
582 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
583 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
584 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
))
585 return cur_cm_id_priv
;
587 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
588 link
= &(*link
)->rb_left
;
589 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
590 link
= &(*link
)->rb_right
;
591 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
592 link
= &(*link
)->rb_left
;
593 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
594 link
= &(*link
)->rb_right
;
596 link
= &(*link
)->rb_right
;
598 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
599 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
603 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
606 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
607 struct cm_id_private
*cm_id_priv
;
610 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
611 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
612 cm_id_priv
->id
.service_id
&&
613 (cm_id_priv
->id
.device
== device
))
616 if (device
< cm_id_priv
->id
.device
)
617 node
= node
->rb_left
;
618 else if (device
> cm_id_priv
->id
.device
)
619 node
= node
->rb_right
;
620 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
621 node
= node
->rb_left
;
622 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
623 node
= node
->rb_right
;
625 node
= node
->rb_right
;
630 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
633 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
634 struct rb_node
*parent
= NULL
;
635 struct cm_timewait_info
*cur_timewait_info
;
636 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
637 __be32 remote_id
= timewait_info
->work
.remote_id
;
641 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
643 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
644 link
= &(*link
)->rb_left
;
645 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
646 link
= &(*link
)->rb_right
;
647 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
648 link
= &(*link
)->rb_left
;
649 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
650 link
= &(*link
)->rb_right
;
652 return cur_timewait_info
;
654 timewait_info
->inserted_remote_id
= 1;
655 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
656 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
660 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
663 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
664 struct cm_timewait_info
*timewait_info
;
667 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
669 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
670 node
= node
->rb_left
;
671 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
672 node
= node
->rb_right
;
673 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
674 node
= node
->rb_left
;
675 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
676 node
= node
->rb_right
;
678 return timewait_info
;
683 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
686 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
687 struct rb_node
*parent
= NULL
;
688 struct cm_timewait_info
*cur_timewait_info
;
689 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
690 __be32 remote_qpn
= timewait_info
->remote_qpn
;
694 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
696 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
697 link
= &(*link
)->rb_left
;
698 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
699 link
= &(*link
)->rb_right
;
700 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
701 link
= &(*link
)->rb_left
;
702 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
703 link
= &(*link
)->rb_right
;
705 return cur_timewait_info
;
707 timewait_info
->inserted_remote_qp
= 1;
708 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
709 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
713 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
716 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
717 struct rb_node
*parent
= NULL
;
718 struct cm_id_private
*cur_cm_id_priv
;
719 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
720 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
724 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
726 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
727 link
= &(*link
)->rb_left
;
728 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
729 link
= &(*link
)->rb_right
;
732 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
735 link
= &(*link
)->rb_left
;
737 link
= &(*link
)->rb_right
;
739 return cur_cm_id_priv
;
742 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
743 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
747 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
748 enum ib_cm_sidr_status status
)
750 struct ib_cm_sidr_rep_param param
;
752 memset(¶m
, 0, sizeof param
);
753 param
.status
= status
;
754 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
757 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
758 ib_cm_handler cm_handler
,
761 struct cm_id_private
*cm_id_priv
;
764 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
766 return ERR_PTR(-ENOMEM
);
768 cm_id_priv
->id
.state
= IB_CM_IDLE
;
769 cm_id_priv
->id
.device
= device
;
770 cm_id_priv
->id
.cm_handler
= cm_handler
;
771 cm_id_priv
->id
.context
= context
;
772 cm_id_priv
->id
.remote_cm_qpn
= 1;
773 ret
= cm_alloc_id(cm_id_priv
);
777 spin_lock_init(&cm_id_priv
->lock
);
778 init_completion(&cm_id_priv
->comp
);
779 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
780 INIT_LIST_HEAD(&cm_id_priv
->prim_list
);
781 INIT_LIST_HEAD(&cm_id_priv
->altr_list
);
782 atomic_set(&cm_id_priv
->work_count
, -1);
783 atomic_set(&cm_id_priv
->refcount
, 1);
784 return &cm_id_priv
->id
;
788 return ERR_PTR(-ENOMEM
);
790 EXPORT_SYMBOL(ib_create_cm_id
);
792 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
794 struct cm_work
*work
;
796 if (list_empty(&cm_id_priv
->work_list
))
799 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
800 list_del(&work
->list
);
804 static void cm_free_work(struct cm_work
*work
)
806 if (work
->mad_recv_wc
)
807 ib_free_recv_mad(work
->mad_recv_wc
);
811 static inline int cm_convert_to_ms(int iba_time
)
813 /* approximate conversion to ms from 4.096us x 2^iba_time */
814 return 1 << max(iba_time
- 8, 0);
818 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
819 * Because of how ack_timeout is stored, adding one doubles the timeout.
820 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
821 * increment it (round up) only if the other is within 50%.
823 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
825 int ack_timeout
= packet_life_time
+ 1;
827 if (ack_timeout
>= ca_ack_delay
)
828 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
830 ack_timeout
= ca_ack_delay
+
831 (ack_timeout
>= (ca_ack_delay
- 1));
833 return min(31, ack_timeout
);
836 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
838 if (timewait_info
->inserted_remote_id
) {
839 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
840 timewait_info
->inserted_remote_id
= 0;
843 if (timewait_info
->inserted_remote_qp
) {
844 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
845 timewait_info
->inserted_remote_qp
= 0;
849 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
851 struct cm_timewait_info
*timewait_info
;
853 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
855 return ERR_PTR(-ENOMEM
);
857 timewait_info
->work
.local_id
= local_id
;
858 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
859 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
860 return timewait_info
;
863 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
867 struct cm_device
*cm_dev
;
869 cm_dev
= ib_get_client_data(cm_id_priv
->id
.device
, &cm_client
);
873 spin_lock_irqsave(&cm
.lock
, flags
);
874 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
875 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
876 spin_unlock_irqrestore(&cm
.lock
, flags
);
879 * The cm_id could be destroyed by the user before we exit timewait.
880 * To protect against this, we search for the cm_id after exiting
881 * timewait before notifying the user that we've exited timewait.
883 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
884 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
886 /* Check if the device started its remove_one */
887 spin_lock_irqsave(&cm
.lock
, flags
);
888 if (!cm_dev
->going_down
)
889 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
890 msecs_to_jiffies(wait_time
));
891 spin_unlock_irqrestore(&cm
.lock
, flags
);
893 cm_id_priv
->timewait_info
= NULL
;
896 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
900 cm_id_priv
->id
.state
= IB_CM_IDLE
;
901 if (cm_id_priv
->timewait_info
) {
902 spin_lock_irqsave(&cm
.lock
, flags
);
903 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
904 spin_unlock_irqrestore(&cm
.lock
, flags
);
905 kfree(cm_id_priv
->timewait_info
);
906 cm_id_priv
->timewait_info
= NULL
;
910 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
912 struct cm_id_private
*cm_id_priv
;
913 struct cm_work
*work
;
915 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
917 spin_lock_irq(&cm_id_priv
->lock
);
918 switch (cm_id
->state
) {
920 spin_unlock_irq(&cm_id_priv
->lock
);
922 spin_lock_irq(&cm
.lock
);
923 if (--cm_id_priv
->listen_sharecount
> 0) {
924 /* The id is still shared. */
925 cm_deref_id(cm_id_priv
);
926 spin_unlock_irq(&cm
.lock
);
929 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
930 spin_unlock_irq(&cm
.lock
);
932 case IB_CM_SIDR_REQ_SENT
:
933 cm_id
->state
= IB_CM_IDLE
;
934 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
935 spin_unlock_irq(&cm_id_priv
->lock
);
937 case IB_CM_SIDR_REQ_RCVD
:
938 spin_unlock_irq(&cm_id_priv
->lock
);
939 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
940 spin_lock_irq(&cm
.lock
);
941 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
))
942 rb_erase(&cm_id_priv
->sidr_id_node
,
943 &cm
.remote_sidr_table
);
944 spin_unlock_irq(&cm
.lock
);
947 case IB_CM_MRA_REQ_RCVD
:
948 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
949 spin_unlock_irq(&cm_id_priv
->lock
);
950 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
951 &cm_id_priv
->id
.device
->node_guid
,
952 sizeof cm_id_priv
->id
.device
->node_guid
,
956 if (err
== -ENOMEM
) {
957 /* Do not reject to allow future retries. */
958 cm_reset_to_idle(cm_id_priv
);
959 spin_unlock_irq(&cm_id_priv
->lock
);
961 spin_unlock_irq(&cm_id_priv
->lock
);
962 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
967 case IB_CM_MRA_REP_RCVD
:
968 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
970 case IB_CM_MRA_REQ_SENT
:
972 case IB_CM_MRA_REP_SENT
:
973 spin_unlock_irq(&cm_id_priv
->lock
);
974 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
977 case IB_CM_ESTABLISHED
:
978 spin_unlock_irq(&cm_id_priv
->lock
);
979 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
981 ib_send_cm_dreq(cm_id
, NULL
, 0);
983 case IB_CM_DREQ_SENT
:
984 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
985 cm_enter_timewait(cm_id_priv
);
986 spin_unlock_irq(&cm_id_priv
->lock
);
988 case IB_CM_DREQ_RCVD
:
989 spin_unlock_irq(&cm_id_priv
->lock
);
990 ib_send_cm_drep(cm_id
, NULL
, 0);
993 spin_unlock_irq(&cm_id_priv
->lock
);
997 spin_lock_irq(&cm
.lock
);
998 if (!list_empty(&cm_id_priv
->altr_list
) &&
999 (!cm_id_priv
->altr_send_port_not_ready
))
1000 list_del(&cm_id_priv
->altr_list
);
1001 if (!list_empty(&cm_id_priv
->prim_list
) &&
1002 (!cm_id_priv
->prim_send_port_not_ready
))
1003 list_del(&cm_id_priv
->prim_list
);
1004 spin_unlock_irq(&cm
.lock
);
1006 cm_free_id(cm_id
->local_id
);
1007 cm_deref_id(cm_id_priv
);
1008 wait_for_completion(&cm_id_priv
->comp
);
1009 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
1011 kfree(cm_id_priv
->private_data
);
1015 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
1017 cm_destroy_id(cm_id
, 0);
1019 EXPORT_SYMBOL(ib_destroy_cm_id
);
1022 * __ib_cm_listen - Initiates listening on the specified service ID for
1023 * connection and service ID resolution requests.
1024 * @cm_id: Connection identifier associated with the listen request.
1025 * @service_id: Service identifier matched against incoming connection
1026 * and service ID resolution requests. The service ID should be specified
1027 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1028 * assign a service ID to the caller.
1029 * @service_mask: Mask applied to service ID used to listen across a
1030 * range of service IDs. If set to 0, the service ID is matched
1031 * exactly. This parameter is ignored if %service_id is set to
1032 * IB_CM_ASSIGN_SERVICE_ID.
1034 static int __ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
,
1035 __be64 service_mask
)
1037 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
1040 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
1041 service_id
&= service_mask
;
1042 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
1043 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
1046 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1047 if (cm_id
->state
!= IB_CM_IDLE
)
1050 cm_id
->state
= IB_CM_LISTEN
;
1051 ++cm_id_priv
->listen_sharecount
;
1053 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
1054 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
1055 cm_id
->service_mask
= ~cpu_to_be64(0);
1057 cm_id
->service_id
= service_id
;
1058 cm_id
->service_mask
= service_mask
;
1060 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
1062 if (cur_cm_id_priv
) {
1063 cm_id
->state
= IB_CM_IDLE
;
1064 --cm_id_priv
->listen_sharecount
;
1070 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
)
1072 unsigned long flags
;
1075 spin_lock_irqsave(&cm
.lock
, flags
);
1076 ret
= __ib_cm_listen(cm_id
, service_id
, service_mask
);
1077 spin_unlock_irqrestore(&cm
.lock
, flags
);
1081 EXPORT_SYMBOL(ib_cm_listen
);
1084 * Create a new listening ib_cm_id and listen on the given service ID.
1086 * If there's an existing ID listening on that same device and service ID,
1089 * @device: Device associated with the cm_id. All related communication will
1090 * be associated with the specified device.
1091 * @cm_handler: Callback invoked to notify the user of CM events.
1092 * @service_id: Service identifier matched against incoming connection
1093 * and service ID resolution requests. The service ID should be specified
1094 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1095 * assign a service ID to the caller.
1097 * Callers should call ib_destroy_cm_id when done with the listener ID.
1099 struct ib_cm_id
*ib_cm_insert_listen(struct ib_device
*device
,
1100 ib_cm_handler cm_handler
,
1103 struct cm_id_private
*cm_id_priv
;
1104 struct ib_cm_id
*cm_id
;
1105 unsigned long flags
;
1108 /* Create an ID in advance, since the creation may sleep */
1109 cm_id
= ib_create_cm_id(device
, cm_handler
, NULL
);
1113 spin_lock_irqsave(&cm
.lock
, flags
);
1115 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
)
1118 /* Find an existing ID */
1119 cm_id_priv
= cm_find_listen(device
, service_id
);
1121 if (cm_id
->cm_handler
!= cm_handler
|| cm_id
->context
) {
1122 /* Sharing an ib_cm_id with different handlers is not
1124 spin_unlock_irqrestore(&cm
.lock
, flags
);
1125 return ERR_PTR(-EINVAL
);
1127 atomic_inc(&cm_id_priv
->refcount
);
1128 ++cm_id_priv
->listen_sharecount
;
1129 spin_unlock_irqrestore(&cm
.lock
, flags
);
1131 ib_destroy_cm_id(cm_id
);
1132 cm_id
= &cm_id_priv
->id
;
1137 /* Use newly created ID */
1138 err
= __ib_cm_listen(cm_id
, service_id
, 0);
1140 spin_unlock_irqrestore(&cm
.lock
, flags
);
1143 ib_destroy_cm_id(cm_id
);
1144 return ERR_PTR(err
);
1148 EXPORT_SYMBOL(ib_cm_insert_listen
);
1150 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
1151 enum cm_msg_sequence msg_seq
)
1153 u64 hi_tid
, low_tid
;
1155 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
1156 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
1158 return cpu_to_be64(hi_tid
| low_tid
);
1161 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
1162 __be16 attr_id
, __be64 tid
)
1164 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1165 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1166 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1167 hdr
->method
= IB_MGMT_METHOD_SEND
;
1168 hdr
->attr_id
= attr_id
;
1172 static void cm_format_req(struct cm_req_msg
*req_msg
,
1173 struct cm_id_private
*cm_id_priv
,
1174 struct ib_cm_req_param
*param
)
1176 struct sa_path_rec
*pri_path
= param
->primary_path
;
1177 struct sa_path_rec
*alt_path
= param
->alternate_path
;
1179 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1180 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1182 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1183 req_msg
->service_id
= param
->service_id
;
1184 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1185 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1186 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1187 cm_req_set_remote_resp_timeout(req_msg
,
1188 param
->remote_cm_response_timeout
);
1189 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1190 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1191 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1192 cm_req_set_local_resp_timeout(req_msg
,
1193 param
->local_cm_response_timeout
);
1194 req_msg
->pkey
= param
->primary_path
->pkey
;
1195 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1196 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1198 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1199 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1200 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1201 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1202 cm_req_set_srq(req_msg
, param
->srq
);
1205 if (pri_path
->hop_limit
<= 1) {
1206 req_msg
->primary_local_lid
=
1207 htons(ntohl(sa_path_get_slid(pri_path
)));
1208 req_msg
->primary_remote_lid
=
1209 htons(ntohl(sa_path_get_dlid(pri_path
)));
1211 /* Work-around until there's a way to obtain remote LID info */
1212 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1213 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1215 req_msg
->primary_local_gid
= pri_path
->sgid
;
1216 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1217 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1218 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1219 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1220 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1221 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1222 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1223 cm_req_set_primary_local_ack_timeout(req_msg
,
1224 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1225 pri_path
->packet_life_time
));
1228 if (alt_path
->hop_limit
<= 1) {
1229 req_msg
->alt_local_lid
=
1230 htons(ntohl(sa_path_get_slid(alt_path
)));
1231 req_msg
->alt_remote_lid
=
1232 htons(ntohl(sa_path_get_dlid(alt_path
)));
1234 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1235 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1237 req_msg
->alt_local_gid
= alt_path
->sgid
;
1238 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1239 cm_req_set_alt_flow_label(req_msg
,
1240 alt_path
->flow_label
);
1241 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1242 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1243 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1244 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1245 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1246 cm_req_set_alt_local_ack_timeout(req_msg
,
1247 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1248 alt_path
->packet_life_time
));
1251 if (param
->private_data
&& param
->private_data_len
)
1252 memcpy(req_msg
->private_data
, param
->private_data
,
1253 param
->private_data_len
);
1256 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1258 /* peer-to-peer not supported */
1259 if (param
->peer_to_peer
)
1262 if (!param
->primary_path
)
1265 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1266 param
->qp_type
!= IB_QPT_XRC_INI
)
1269 if (param
->private_data
&&
1270 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1273 if (param
->alternate_path
&&
1274 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1275 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1281 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1282 struct ib_cm_req_param
*param
)
1284 struct cm_id_private
*cm_id_priv
;
1285 struct cm_req_msg
*req_msg
;
1286 unsigned long flags
;
1289 ret
= cm_validate_req_param(param
);
1293 /* Verify that we're not in timewait. */
1294 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1295 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1296 if (cm_id
->state
!= IB_CM_IDLE
) {
1297 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1301 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1303 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1305 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1306 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1310 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
,
1314 if (param
->alternate_path
) {
1315 ret
= cm_init_av_by_path(param
->alternate_path
,
1316 &cm_id_priv
->alt_av
, cm_id_priv
);
1320 cm_id
->service_id
= param
->service_id
;
1321 cm_id
->service_mask
= ~cpu_to_be64(0);
1322 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1323 param
->primary_path
->packet_life_time
) * 2 +
1325 param
->remote_cm_response_timeout
);
1326 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1327 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1328 cm_id_priv
->responder_resources
= param
->responder_resources
;
1329 cm_id_priv
->retry_count
= param
->retry_count
;
1330 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1331 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1332 cm_id_priv
->qp_type
= param
->qp_type
;
1334 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1338 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1339 cm_format_req(req_msg
, cm_id_priv
, param
);
1340 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1341 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1342 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1344 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1345 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1347 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1348 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1350 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1353 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1354 cm_id
->state
= IB_CM_REQ_SENT
;
1355 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1358 error2
: cm_free_msg(cm_id_priv
->msg
);
1359 error1
: kfree(cm_id_priv
->timewait_info
);
1362 EXPORT_SYMBOL(ib_send_cm_req
);
1364 static int cm_issue_rej(struct cm_port
*port
,
1365 struct ib_mad_recv_wc
*mad_recv_wc
,
1366 enum ib_cm_rej_reason reason
,
1367 enum cm_msg_response msg_rejected
,
1368 void *ari
, u8 ari_length
)
1370 struct ib_mad_send_buf
*msg
= NULL
;
1371 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1374 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1378 /* We just need common CM header information. Cast to any message. */
1379 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1380 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1382 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1383 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1384 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1385 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1386 rej_msg
->reason
= cpu_to_be16(reason
);
1388 if (ari
&& ari_length
) {
1389 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1390 memcpy(rej_msg
->ari
, ari
, ari_length
);
1393 ret
= ib_post_send_mad(msg
, NULL
);
1400 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1401 __be32 local_qpn
, __be32 remote_qpn
)
1403 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1404 ((local_ca_guid
== remote_ca_guid
) &&
1405 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1408 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1409 struct sa_path_rec
*primary_path
,
1410 struct sa_path_rec
*alt_path
)
1412 primary_path
->dgid
= req_msg
->primary_local_gid
;
1413 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1414 sa_path_set_dlid(primary_path
,
1415 htonl(ntohs(req_msg
->primary_local_lid
)));
1416 sa_path_set_slid(primary_path
,
1417 htonl(ntohs(req_msg
->primary_remote_lid
)));
1418 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1419 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1420 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1421 primary_path
->reversible
= 1;
1422 primary_path
->pkey
= req_msg
->pkey
;
1423 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1424 primary_path
->mtu_selector
= IB_SA_EQ
;
1425 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1426 primary_path
->rate_selector
= IB_SA_EQ
;
1427 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1428 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1429 primary_path
->packet_life_time
=
1430 cm_req_get_primary_local_ack_timeout(req_msg
);
1431 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1432 primary_path
->service_id
= req_msg
->service_id
;
1434 if (req_msg
->alt_local_lid
) {
1435 alt_path
->dgid
= req_msg
->alt_local_gid
;
1436 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1437 sa_path_set_dlid(alt_path
,
1438 htonl(ntohs(req_msg
->alt_local_lid
)));
1439 sa_path_set_slid(alt_path
,
1440 htonl(ntohs(req_msg
->alt_remote_lid
)));
1441 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1442 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1443 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1444 alt_path
->reversible
= 1;
1445 alt_path
->pkey
= req_msg
->pkey
;
1446 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1447 alt_path
->mtu_selector
= IB_SA_EQ
;
1448 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1449 alt_path
->rate_selector
= IB_SA_EQ
;
1450 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1451 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1452 alt_path
->packet_life_time
=
1453 cm_req_get_alt_local_ack_timeout(req_msg
);
1454 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1455 alt_path
->service_id
= req_msg
->service_id
;
1459 static u16
cm_get_bth_pkey(struct cm_work
*work
)
1461 struct ib_device
*ib_dev
= work
->port
->cm_dev
->ib_device
;
1462 u8 port_num
= work
->port
->port_num
;
1463 u16 pkey_index
= work
->mad_recv_wc
->wc
->pkey_index
;
1467 ret
= ib_get_cached_pkey(ib_dev
, port_num
, pkey_index
, &pkey
);
1469 dev_warn_ratelimited(&ib_dev
->dev
, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1470 port_num
, pkey_index
, ret
);
1477 static void cm_format_req_event(struct cm_work
*work
,
1478 struct cm_id_private
*cm_id_priv
,
1479 struct ib_cm_id
*listen_id
)
1481 struct cm_req_msg
*req_msg
;
1482 struct ib_cm_req_event_param
*param
;
1484 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1485 param
= &work
->cm_event
.param
.req_rcvd
;
1486 param
->listen_id
= listen_id
;
1487 param
->bth_pkey
= cm_get_bth_pkey(work
);
1488 param
->port
= cm_id_priv
->av
.port
->port_num
;
1489 param
->primary_path
= &work
->path
[0];
1490 if (req_msg
->alt_local_lid
)
1491 param
->alternate_path
= &work
->path
[1];
1493 param
->alternate_path
= NULL
;
1494 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1495 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1496 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1497 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1498 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1499 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1500 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1501 param
->local_cm_response_timeout
=
1502 cm_req_get_remote_resp_timeout(req_msg
);
1503 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1504 param
->remote_cm_response_timeout
=
1505 cm_req_get_local_resp_timeout(req_msg
);
1506 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1507 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1508 param
->srq
= cm_req_get_srq(req_msg
);
1509 work
->cm_event
.private_data
= &req_msg
->private_data
;
1512 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1513 struct cm_work
*work
)
1517 /* We will typically only have the current event to report. */
1518 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1521 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1522 spin_lock_irq(&cm_id_priv
->lock
);
1523 work
= cm_dequeue_work(cm_id_priv
);
1524 spin_unlock_irq(&cm_id_priv
->lock
);
1526 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1530 cm_deref_id(cm_id_priv
);
1532 cm_destroy_id(&cm_id_priv
->id
, ret
);
1535 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1536 struct cm_id_private
*cm_id_priv
,
1537 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1538 const void *private_data
, u8 private_data_len
)
1540 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1541 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1542 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1543 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1544 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1546 if (private_data
&& private_data_len
)
1547 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1550 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1551 struct cm_id_private
*cm_id_priv
,
1552 enum ib_cm_rej_reason reason
,
1555 const void *private_data
,
1556 u8 private_data_len
)
1558 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1559 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1561 switch(cm_id_priv
->id
.state
) {
1562 case IB_CM_REQ_RCVD
:
1563 rej_msg
->local_comm_id
= 0;
1564 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1566 case IB_CM_MRA_REQ_SENT
:
1567 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1568 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1570 case IB_CM_REP_RCVD
:
1571 case IB_CM_MRA_REP_SENT
:
1572 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1573 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1576 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1577 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1581 rej_msg
->reason
= cpu_to_be16(reason
);
1582 if (ari
&& ari_length
) {
1583 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1584 memcpy(rej_msg
->ari
, ari
, ari_length
);
1587 if (private_data
&& private_data_len
)
1588 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1591 static void cm_dup_req_handler(struct cm_work
*work
,
1592 struct cm_id_private
*cm_id_priv
)
1594 struct ib_mad_send_buf
*msg
= NULL
;
1597 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1598 counter
[CM_REQ_COUNTER
]);
1600 /* Quick state check to discard duplicate REQs. */
1601 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1604 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1608 spin_lock_irq(&cm_id_priv
->lock
);
1609 switch (cm_id_priv
->id
.state
) {
1610 case IB_CM_MRA_REQ_SENT
:
1611 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1612 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1613 cm_id_priv
->private_data
,
1614 cm_id_priv
->private_data_len
);
1616 case IB_CM_TIMEWAIT
:
1617 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1618 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1623 spin_unlock_irq(&cm_id_priv
->lock
);
1625 ret
= ib_post_send_mad(msg
, NULL
);
1630 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1631 free
: cm_free_msg(msg
);
1634 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1635 struct cm_id_private
*cm_id_priv
)
1637 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1638 struct cm_timewait_info
*timewait_info
;
1639 struct cm_req_msg
*req_msg
;
1640 struct ib_cm_id
*cm_id
;
1642 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1644 /* Check for possible duplicate REQ. */
1645 spin_lock_irq(&cm
.lock
);
1646 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1647 if (timewait_info
) {
1648 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1649 timewait_info
->work
.remote_id
);
1650 spin_unlock_irq(&cm
.lock
);
1651 if (cur_cm_id_priv
) {
1652 cm_dup_req_handler(work
, cur_cm_id_priv
);
1653 cm_deref_id(cur_cm_id_priv
);
1658 /* Check for stale connections. */
1659 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1660 if (timewait_info
) {
1661 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1662 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1663 timewait_info
->work
.remote_id
);
1665 spin_unlock_irq(&cm
.lock
);
1666 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1667 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1669 if (cur_cm_id_priv
) {
1670 cm_id
= &cur_cm_id_priv
->id
;
1671 ib_send_cm_dreq(cm_id
, NULL
, 0);
1672 cm_deref_id(cur_cm_id_priv
);
1677 /* Find matching listen request. */
1678 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1679 req_msg
->service_id
);
1680 if (!listen_cm_id_priv
) {
1681 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1682 spin_unlock_irq(&cm
.lock
);
1683 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1684 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1688 atomic_inc(&listen_cm_id_priv
->refcount
);
1689 atomic_inc(&cm_id_priv
->refcount
);
1690 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1691 atomic_inc(&cm_id_priv
->work_count
);
1692 spin_unlock_irq(&cm
.lock
);
1694 return listen_cm_id_priv
;
1698 * Work-around for inter-subnet connections. If the LIDs are permissive,
1699 * we need to override the LID/SL data in the REQ with the LID information
1700 * in the work completion.
1702 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1704 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1705 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1706 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1707 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1710 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1711 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1714 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1715 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1716 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1717 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1720 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1721 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1725 static int cm_req_handler(struct cm_work
*work
)
1727 struct ib_cm_id
*cm_id
;
1728 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1729 struct cm_req_msg
*req_msg
;
1731 struct ib_gid_attr gid_attr
;
1732 const struct ib_global_route
*grh
;
1735 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1737 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1739 return PTR_ERR(cm_id
);
1741 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1742 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1743 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1744 work
->mad_recv_wc
->recv_buf
.grh
,
1746 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1748 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1749 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1752 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1753 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1754 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1756 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1757 if (!listen_cm_id_priv
) {
1759 kfree(cm_id_priv
->timewait_info
);
1763 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1764 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1765 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1766 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1768 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1770 memset(&work
->path
[0], 0, sizeof(work
->path
[0]));
1771 memset(&work
->path
[1], 0, sizeof(work
->path
[1]));
1772 grh
= rdma_ah_read_grh(&cm_id_priv
->av
.ah_attr
);
1773 ret
= ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1774 work
->port
->port_num
,
1778 if (gid_attr
.ndev
) {
1779 work
->path
[0].rec_type
=
1780 sa_conv_gid_to_pathrec_type(gid_attr
.gid_type
);
1781 sa_path_set_ifindex(&work
->path
[0],
1782 gid_attr
.ndev
->ifindex
);
1783 sa_path_set_ndev(&work
->path
[0],
1784 dev_net(gid_attr
.ndev
));
1785 dev_put(gid_attr
.ndev
);
1787 work
->path
[0].rec_type
= SA_PATH_REC_TYPE_IB
;
1789 if (req_msg
->alt_local_lid
)
1790 work
->path
[1].rec_type
= work
->path
[0].rec_type
;
1791 cm_format_paths_from_req(req_msg
, &work
->path
[0],
1793 if (cm_id_priv
->av
.ah_attr
.type
== RDMA_AH_ATTR_TYPE_ROCE
)
1794 sa_path_set_dmac(&work
->path
[0],
1795 cm_id_priv
->av
.ah_attr
.roce
.dmac
);
1796 work
->path
[0].hop_limit
= grh
->hop_limit
;
1797 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
,
1801 int err
= ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1802 work
->port
->port_num
, 0,
1803 &work
->path
[0].sgid
,
1805 if (!err
&& gid_attr
.ndev
) {
1806 work
->path
[0].rec_type
=
1807 sa_conv_gid_to_pathrec_type(gid_attr
.gid_type
);
1808 sa_path_set_ifindex(&work
->path
[0],
1809 gid_attr
.ndev
->ifindex
);
1810 sa_path_set_ndev(&work
->path
[0],
1811 dev_net(gid_attr
.ndev
));
1812 dev_put(gid_attr
.ndev
);
1814 work
->path
[0].rec_type
= SA_PATH_REC_TYPE_IB
;
1816 if (req_msg
->alt_local_lid
)
1817 work
->path
[1].rec_type
= work
->path
[0].rec_type
;
1818 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1819 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1823 if (req_msg
->alt_local_lid
) {
1824 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
,
1827 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1828 &work
->path
[0].sgid
,
1829 sizeof work
->path
[0].sgid
, NULL
, 0);
1833 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1834 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1835 cm_req_get_local_resp_timeout(req_msg
));
1836 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1837 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1838 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1839 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1840 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1841 cm_id_priv
->pkey
= req_msg
->pkey
;
1842 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1843 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1844 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1845 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1847 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1848 cm_process_work(cm_id_priv
, work
);
1849 cm_deref_id(listen_cm_id_priv
);
1853 atomic_dec(&cm_id_priv
->refcount
);
1854 cm_deref_id(listen_cm_id_priv
);
1856 ib_destroy_cm_id(cm_id
);
1860 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1861 struct cm_id_private
*cm_id_priv
,
1862 struct ib_cm_rep_param
*param
)
1864 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1865 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1866 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1867 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1868 rep_msg
->resp_resources
= param
->responder_resources
;
1869 cm_rep_set_target_ack_delay(rep_msg
,
1870 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1871 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1872 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1873 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1875 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1876 rep_msg
->initiator_depth
= param
->initiator_depth
;
1877 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1878 cm_rep_set_srq(rep_msg
, param
->srq
);
1879 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1881 cm_rep_set_srq(rep_msg
, 1);
1882 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1885 if (param
->private_data
&& param
->private_data_len
)
1886 memcpy(rep_msg
->private_data
, param
->private_data
,
1887 param
->private_data_len
);
1890 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1891 struct ib_cm_rep_param
*param
)
1893 struct cm_id_private
*cm_id_priv
;
1894 struct ib_mad_send_buf
*msg
;
1895 struct cm_rep_msg
*rep_msg
;
1896 unsigned long flags
;
1899 if (param
->private_data
&&
1900 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1903 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1904 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1905 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1906 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1911 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1915 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1916 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1917 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1918 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1920 ret
= ib_post_send_mad(msg
, NULL
);
1922 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1927 cm_id
->state
= IB_CM_REP_SENT
;
1928 cm_id_priv
->msg
= msg
;
1929 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1930 cm_id_priv
->responder_resources
= param
->responder_resources
;
1931 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1932 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1934 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1937 EXPORT_SYMBOL(ib_send_cm_rep
);
1939 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1940 struct cm_id_private
*cm_id_priv
,
1941 const void *private_data
,
1942 u8 private_data_len
)
1944 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1945 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1946 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1948 if (private_data
&& private_data_len
)
1949 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1952 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1953 const void *private_data
,
1954 u8 private_data_len
)
1956 struct cm_id_private
*cm_id_priv
;
1957 struct ib_mad_send_buf
*msg
;
1958 unsigned long flags
;
1962 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1965 data
= cm_copy_private_data(private_data
, private_data_len
);
1967 return PTR_ERR(data
);
1969 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1970 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1971 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1972 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1977 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1981 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1982 private_data
, private_data_len
);
1984 ret
= ib_post_send_mad(msg
, NULL
);
1986 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1992 cm_id
->state
= IB_CM_ESTABLISHED
;
1993 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1994 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1997 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2001 EXPORT_SYMBOL(ib_send_cm_rtu
);
2003 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
2005 struct cm_rep_msg
*rep_msg
;
2006 struct ib_cm_rep_event_param
*param
;
2008 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2009 param
= &work
->cm_event
.param
.rep_rcvd
;
2010 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
2011 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
2012 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
2013 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
2014 param
->responder_resources
= rep_msg
->initiator_depth
;
2015 param
->initiator_depth
= rep_msg
->resp_resources
;
2016 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
2017 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
2018 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
2019 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
2020 param
->srq
= cm_rep_get_srq(rep_msg
);
2021 work
->cm_event
.private_data
= &rep_msg
->private_data
;
2024 static void cm_dup_rep_handler(struct cm_work
*work
)
2026 struct cm_id_private
*cm_id_priv
;
2027 struct cm_rep_msg
*rep_msg
;
2028 struct ib_mad_send_buf
*msg
= NULL
;
2031 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
2032 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
2033 rep_msg
->local_comm_id
);
2037 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2038 counter
[CM_REP_COUNTER
]);
2039 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
2043 spin_lock_irq(&cm_id_priv
->lock
);
2044 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
2045 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
2046 cm_id_priv
->private_data
,
2047 cm_id_priv
->private_data_len
);
2048 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
2049 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2050 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
2051 cm_id_priv
->private_data
,
2052 cm_id_priv
->private_data_len
);
2055 spin_unlock_irq(&cm_id_priv
->lock
);
2057 ret
= ib_post_send_mad(msg
, NULL
);
2062 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2063 free
: cm_free_msg(msg
);
2064 deref
: cm_deref_id(cm_id_priv
);
2067 static int cm_rep_handler(struct cm_work
*work
)
2069 struct cm_id_private
*cm_id_priv
;
2070 struct cm_rep_msg
*rep_msg
;
2072 struct cm_id_private
*cur_cm_id_priv
;
2073 struct ib_cm_id
*cm_id
;
2074 struct cm_timewait_info
*timewait_info
;
2076 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2077 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
2079 cm_dup_rep_handler(work
);
2083 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
2085 spin_lock_irq(&cm_id_priv
->lock
);
2086 switch (cm_id_priv
->id
.state
) {
2087 case IB_CM_REQ_SENT
:
2088 case IB_CM_MRA_REQ_RCVD
:
2091 spin_unlock_irq(&cm_id_priv
->lock
);
2096 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
2097 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
2098 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2100 spin_lock(&cm
.lock
);
2101 /* Check for duplicate REP. */
2102 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
2103 spin_unlock(&cm
.lock
);
2104 spin_unlock_irq(&cm_id_priv
->lock
);
2108 /* Check for a stale connection. */
2109 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
2110 if (timewait_info
) {
2111 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
2112 &cm
.remote_id_table
);
2113 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
2114 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
2115 timewait_info
->work
.remote_id
);
2117 spin_unlock(&cm
.lock
);
2118 spin_unlock_irq(&cm_id_priv
->lock
);
2119 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
2120 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
2123 if (cur_cm_id_priv
) {
2124 cm_id
= &cur_cm_id_priv
->id
;
2125 ib_send_cm_dreq(cm_id
, NULL
, 0);
2126 cm_deref_id(cur_cm_id_priv
);
2131 spin_unlock(&cm
.lock
);
2133 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
2134 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
2135 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
2136 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
2137 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
2138 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
2139 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
2140 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
2141 cm_id_priv
->av
.timeout
=
2142 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2143 cm_id_priv
->av
.timeout
- 1);
2144 cm_id_priv
->alt_av
.timeout
=
2145 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2146 cm_id_priv
->alt_av
.timeout
- 1);
2148 /* todo: handle peer_to_peer */
2150 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2151 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2153 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2154 spin_unlock_irq(&cm_id_priv
->lock
);
2157 cm_process_work(cm_id_priv
, work
);
2159 cm_deref_id(cm_id_priv
);
2163 cm_deref_id(cm_id_priv
);
2167 static int cm_establish_handler(struct cm_work
*work
)
2169 struct cm_id_private
*cm_id_priv
;
2172 /* See comment in cm_establish about lookup. */
2173 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
2177 spin_lock_irq(&cm_id_priv
->lock
);
2178 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2179 spin_unlock_irq(&cm_id_priv
->lock
);
2183 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2184 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2186 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2187 spin_unlock_irq(&cm_id_priv
->lock
);
2190 cm_process_work(cm_id_priv
, work
);
2192 cm_deref_id(cm_id_priv
);
2195 cm_deref_id(cm_id_priv
);
2199 static int cm_rtu_handler(struct cm_work
*work
)
2201 struct cm_id_private
*cm_id_priv
;
2202 struct cm_rtu_msg
*rtu_msg
;
2205 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2206 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
2207 rtu_msg
->local_comm_id
);
2211 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
2213 spin_lock_irq(&cm_id_priv
->lock
);
2214 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
2215 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
2216 spin_unlock_irq(&cm_id_priv
->lock
);
2217 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2218 counter
[CM_RTU_COUNTER
]);
2221 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
2223 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2224 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2226 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2227 spin_unlock_irq(&cm_id_priv
->lock
);
2230 cm_process_work(cm_id_priv
, work
);
2232 cm_deref_id(cm_id_priv
);
2235 cm_deref_id(cm_id_priv
);
2239 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
2240 struct cm_id_private
*cm_id_priv
,
2241 const void *private_data
,
2242 u8 private_data_len
)
2244 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
2245 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
2246 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2247 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2248 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
2250 if (private_data
&& private_data_len
)
2251 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
2254 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
2255 const void *private_data
,
2256 u8 private_data_len
)
2258 struct cm_id_private
*cm_id_priv
;
2259 struct ib_mad_send_buf
*msg
;
2260 unsigned long flags
;
2263 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2266 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2267 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2268 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2273 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2274 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2275 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2277 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2279 cm_enter_timewait(cm_id_priv
);
2283 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2284 private_data
, private_data_len
);
2285 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2286 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2288 ret
= ib_post_send_mad(msg
, NULL
);
2290 cm_enter_timewait(cm_id_priv
);
2291 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2296 cm_id
->state
= IB_CM_DREQ_SENT
;
2297 cm_id_priv
->msg
= msg
;
2298 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2301 EXPORT_SYMBOL(ib_send_cm_dreq
);
2303 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2304 struct cm_id_private
*cm_id_priv
,
2305 const void *private_data
,
2306 u8 private_data_len
)
2308 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2309 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2310 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2312 if (private_data
&& private_data_len
)
2313 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2316 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2317 const void *private_data
,
2318 u8 private_data_len
)
2320 struct cm_id_private
*cm_id_priv
;
2321 struct ib_mad_send_buf
*msg
;
2322 unsigned long flags
;
2326 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2329 data
= cm_copy_private_data(private_data
, private_data_len
);
2331 return PTR_ERR(data
);
2333 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2334 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2335 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2336 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2341 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2342 cm_enter_timewait(cm_id_priv
);
2344 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2348 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2349 private_data
, private_data_len
);
2351 ret
= ib_post_send_mad(msg
, NULL
);
2353 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2358 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2361 EXPORT_SYMBOL(ib_send_cm_drep
);
2363 static int cm_issue_drep(struct cm_port
*port
,
2364 struct ib_mad_recv_wc
*mad_recv_wc
)
2366 struct ib_mad_send_buf
*msg
= NULL
;
2367 struct cm_dreq_msg
*dreq_msg
;
2368 struct cm_drep_msg
*drep_msg
;
2371 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2375 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2376 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2378 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2379 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2380 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2382 ret
= ib_post_send_mad(msg
, NULL
);
2389 static int cm_dreq_handler(struct cm_work
*work
)
2391 struct cm_id_private
*cm_id_priv
;
2392 struct cm_dreq_msg
*dreq_msg
;
2393 struct ib_mad_send_buf
*msg
= NULL
;
2396 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2397 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2398 dreq_msg
->local_comm_id
);
2400 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2401 counter
[CM_DREQ_COUNTER
]);
2402 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2406 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2408 spin_lock_irq(&cm_id_priv
->lock
);
2409 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2412 switch (cm_id_priv
->id
.state
) {
2413 case IB_CM_REP_SENT
:
2414 case IB_CM_DREQ_SENT
:
2415 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2417 case IB_CM_ESTABLISHED
:
2418 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2419 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2420 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2422 case IB_CM_MRA_REP_RCVD
:
2424 case IB_CM_TIMEWAIT
:
2425 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2426 counter
[CM_DREQ_COUNTER
]);
2427 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2430 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2431 cm_id_priv
->private_data
,
2432 cm_id_priv
->private_data_len
);
2433 spin_unlock_irq(&cm_id_priv
->lock
);
2435 if (ib_post_send_mad(msg
, NULL
))
2438 case IB_CM_DREQ_RCVD
:
2439 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2440 counter
[CM_DREQ_COUNTER
]);
2445 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2446 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2447 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2449 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2450 spin_unlock_irq(&cm_id_priv
->lock
);
2453 cm_process_work(cm_id_priv
, work
);
2455 cm_deref_id(cm_id_priv
);
2458 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2459 deref
: cm_deref_id(cm_id_priv
);
2463 static int cm_drep_handler(struct cm_work
*work
)
2465 struct cm_id_private
*cm_id_priv
;
2466 struct cm_drep_msg
*drep_msg
;
2469 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2470 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2471 drep_msg
->local_comm_id
);
2475 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2477 spin_lock_irq(&cm_id_priv
->lock
);
2478 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2479 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2480 spin_unlock_irq(&cm_id_priv
->lock
);
2483 cm_enter_timewait(cm_id_priv
);
2485 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2486 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2488 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2489 spin_unlock_irq(&cm_id_priv
->lock
);
2492 cm_process_work(cm_id_priv
, work
);
2494 cm_deref_id(cm_id_priv
);
2497 cm_deref_id(cm_id_priv
);
2501 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2502 enum ib_cm_rej_reason reason
,
2505 const void *private_data
,
2506 u8 private_data_len
)
2508 struct cm_id_private
*cm_id_priv
;
2509 struct ib_mad_send_buf
*msg
;
2510 unsigned long flags
;
2513 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2514 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2517 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2519 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2520 switch (cm_id
->state
) {
2521 case IB_CM_REQ_SENT
:
2522 case IB_CM_MRA_REQ_RCVD
:
2523 case IB_CM_REQ_RCVD
:
2524 case IB_CM_MRA_REQ_SENT
:
2525 case IB_CM_REP_RCVD
:
2526 case IB_CM_MRA_REP_SENT
:
2527 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2529 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2530 cm_id_priv
, reason
, ari
, ari_length
,
2531 private_data
, private_data_len
);
2533 cm_reset_to_idle(cm_id_priv
);
2535 case IB_CM_REP_SENT
:
2536 case IB_CM_MRA_REP_RCVD
:
2537 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2539 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2540 cm_id_priv
, reason
, ari
, ari_length
,
2541 private_data
, private_data_len
);
2543 cm_enter_timewait(cm_id_priv
);
2553 ret
= ib_post_send_mad(msg
, NULL
);
2557 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2560 EXPORT_SYMBOL(ib_send_cm_rej
);
2562 static void cm_format_rej_event(struct cm_work
*work
)
2564 struct cm_rej_msg
*rej_msg
;
2565 struct ib_cm_rej_event_param
*param
;
2567 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2568 param
= &work
->cm_event
.param
.rej_rcvd
;
2569 param
->ari
= rej_msg
->ari
;
2570 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2571 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2572 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2575 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2577 struct cm_timewait_info
*timewait_info
;
2578 struct cm_id_private
*cm_id_priv
;
2581 remote_id
= rej_msg
->local_comm_id
;
2583 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2584 spin_lock_irq(&cm
.lock
);
2585 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2587 if (!timewait_info
) {
2588 spin_unlock_irq(&cm
.lock
);
2591 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2592 (timewait_info
->work
.local_id
^
2593 cm
.random_id_operand
));
2595 if (cm_id_priv
->id
.remote_id
== remote_id
)
2596 atomic_inc(&cm_id_priv
->refcount
);
2600 spin_unlock_irq(&cm
.lock
);
2601 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2602 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2604 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2609 static int cm_rej_handler(struct cm_work
*work
)
2611 struct cm_id_private
*cm_id_priv
;
2612 struct cm_rej_msg
*rej_msg
;
2615 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2616 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2620 cm_format_rej_event(work
);
2622 spin_lock_irq(&cm_id_priv
->lock
);
2623 switch (cm_id_priv
->id
.state
) {
2624 case IB_CM_REQ_SENT
:
2625 case IB_CM_MRA_REQ_RCVD
:
2626 case IB_CM_REP_SENT
:
2627 case IB_CM_MRA_REP_RCVD
:
2628 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2630 case IB_CM_REQ_RCVD
:
2631 case IB_CM_MRA_REQ_SENT
:
2632 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2633 cm_enter_timewait(cm_id_priv
);
2635 cm_reset_to_idle(cm_id_priv
);
2637 case IB_CM_DREQ_SENT
:
2638 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2640 case IB_CM_REP_RCVD
:
2641 case IB_CM_MRA_REP_SENT
:
2642 cm_enter_timewait(cm_id_priv
);
2644 case IB_CM_ESTABLISHED
:
2645 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2646 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2647 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2648 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2650 cm_enter_timewait(cm_id_priv
);
2655 spin_unlock_irq(&cm_id_priv
->lock
);
2660 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2662 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2663 spin_unlock_irq(&cm_id_priv
->lock
);
2666 cm_process_work(cm_id_priv
, work
);
2668 cm_deref_id(cm_id_priv
);
2671 cm_deref_id(cm_id_priv
);
2675 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2677 const void *private_data
,
2678 u8 private_data_len
)
2680 struct cm_id_private
*cm_id_priv
;
2681 struct ib_mad_send_buf
*msg
;
2682 enum ib_cm_state cm_state
;
2683 enum ib_cm_lap_state lap_state
;
2684 enum cm_msg_response msg_response
;
2686 unsigned long flags
;
2689 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2692 data
= cm_copy_private_data(private_data
, private_data_len
);
2694 return PTR_ERR(data
);
2696 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2698 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2699 switch(cm_id_priv
->id
.state
) {
2700 case IB_CM_REQ_RCVD
:
2701 cm_state
= IB_CM_MRA_REQ_SENT
;
2702 lap_state
= cm_id
->lap_state
;
2703 msg_response
= CM_MSG_RESPONSE_REQ
;
2705 case IB_CM_REP_RCVD
:
2706 cm_state
= IB_CM_MRA_REP_SENT
;
2707 lap_state
= cm_id
->lap_state
;
2708 msg_response
= CM_MSG_RESPONSE_REP
;
2710 case IB_CM_ESTABLISHED
:
2711 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2712 cm_state
= cm_id
->state
;
2713 lap_state
= IB_CM_MRA_LAP_SENT
;
2714 msg_response
= CM_MSG_RESPONSE_OTHER
;
2722 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2723 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2727 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2728 msg_response
, service_timeout
,
2729 private_data
, private_data_len
);
2730 ret
= ib_post_send_mad(msg
, NULL
);
2735 cm_id
->state
= cm_state
;
2736 cm_id
->lap_state
= lap_state
;
2737 cm_id_priv
->service_timeout
= service_timeout
;
2738 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2739 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2742 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2746 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2751 EXPORT_SYMBOL(ib_send_cm_mra
);
2753 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2755 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2756 case CM_MSG_RESPONSE_REQ
:
2757 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2758 case CM_MSG_RESPONSE_REP
:
2759 case CM_MSG_RESPONSE_OTHER
:
2760 return cm_acquire_id(mra_msg
->remote_comm_id
,
2761 mra_msg
->local_comm_id
);
2767 static int cm_mra_handler(struct cm_work
*work
)
2769 struct cm_id_private
*cm_id_priv
;
2770 struct cm_mra_msg
*mra_msg
;
2773 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2774 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2778 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2779 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2780 cm_mra_get_service_timeout(mra_msg
);
2781 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2782 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2784 spin_lock_irq(&cm_id_priv
->lock
);
2785 switch (cm_id_priv
->id
.state
) {
2786 case IB_CM_REQ_SENT
:
2787 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2788 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2789 cm_id_priv
->msg
, timeout
))
2791 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2793 case IB_CM_REP_SENT
:
2794 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2795 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2796 cm_id_priv
->msg
, timeout
))
2798 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2800 case IB_CM_ESTABLISHED
:
2801 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2802 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2803 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2804 cm_id_priv
->msg
, timeout
)) {
2805 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2806 atomic_long_inc(&work
->port
->
2807 counter_group
[CM_RECV_DUPLICATES
].
2808 counter
[CM_MRA_COUNTER
]);
2811 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2813 case IB_CM_MRA_REQ_RCVD
:
2814 case IB_CM_MRA_REP_RCVD
:
2815 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2816 counter
[CM_MRA_COUNTER
]);
2822 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2823 cm_id_priv
->id
.state
;
2824 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2826 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2827 spin_unlock_irq(&cm_id_priv
->lock
);
2830 cm_process_work(cm_id_priv
, work
);
2832 cm_deref_id(cm_id_priv
);
2835 spin_unlock_irq(&cm_id_priv
->lock
);
2836 cm_deref_id(cm_id_priv
);
2840 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2841 struct cm_id_private
*cm_id_priv
,
2842 struct sa_path_rec
*alternate_path
,
2843 const void *private_data
,
2844 u8 private_data_len
)
2846 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2847 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2848 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2849 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2850 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2851 /* todo: need remote CM response timeout */
2852 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2853 lap_msg
->alt_local_lid
=
2854 htons(ntohl(sa_path_get_slid(alternate_path
)));
2855 lap_msg
->alt_remote_lid
=
2856 htons(ntohl(sa_path_get_dlid(alternate_path
)));
2857 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2858 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2859 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2860 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2861 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2862 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2863 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2864 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2865 cm_lap_set_local_ack_timeout(lap_msg
,
2866 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2867 alternate_path
->packet_life_time
));
2869 if (private_data
&& private_data_len
)
2870 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2873 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2874 struct sa_path_rec
*alternate_path
,
2875 const void *private_data
,
2876 u8 private_data_len
)
2878 struct cm_id_private
*cm_id_priv
;
2879 struct ib_mad_send_buf
*msg
;
2880 unsigned long flags
;
2883 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2886 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2887 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2888 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2889 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2890 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2895 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
,
2899 cm_id_priv
->alt_av
.timeout
=
2900 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2901 cm_id_priv
->alt_av
.timeout
- 1);
2903 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2907 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2908 alternate_path
, private_data
, private_data_len
);
2909 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2910 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2912 ret
= ib_post_send_mad(msg
, NULL
);
2914 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2919 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2920 cm_id_priv
->msg
= msg
;
2922 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2925 EXPORT_SYMBOL(ib_send_cm_lap
);
2927 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2928 struct sa_path_rec
*path
,
2929 struct cm_lap_msg
*lap_msg
)
2931 memset(path
, 0, sizeof *path
);
2932 path
->rec_type
= SA_PATH_REC_TYPE_IB
;
2933 path
->dgid
= lap_msg
->alt_local_gid
;
2934 path
->sgid
= lap_msg
->alt_remote_gid
;
2935 sa_path_set_dlid(path
, htonl(ntohs(lap_msg
->alt_local_lid
)));
2936 sa_path_set_slid(path
, htonl(ntohs(lap_msg
->alt_remote_lid
)));
2937 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2938 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2939 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2940 path
->reversible
= 1;
2941 path
->pkey
= cm_id_priv
->pkey
;
2942 path
->sl
= cm_lap_get_sl(lap_msg
);
2943 path
->mtu_selector
= IB_SA_EQ
;
2944 path
->mtu
= cm_id_priv
->path_mtu
;
2945 path
->rate_selector
= IB_SA_EQ
;
2946 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2947 path
->packet_life_time_selector
= IB_SA_EQ
;
2948 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2949 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2952 static int cm_lap_handler(struct cm_work
*work
)
2954 struct cm_id_private
*cm_id_priv
;
2955 struct cm_lap_msg
*lap_msg
;
2956 struct ib_cm_lap_event_param
*param
;
2957 struct ib_mad_send_buf
*msg
= NULL
;
2960 /* todo: verify LAP request and send reject APR if invalid. */
2961 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2962 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2963 lap_msg
->local_comm_id
);
2967 param
= &work
->cm_event
.param
.lap_rcvd
;
2968 param
->alternate_path
= &work
->path
[0];
2969 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2970 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2972 spin_lock_irq(&cm_id_priv
->lock
);
2973 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2976 switch (cm_id_priv
->id
.lap_state
) {
2977 case IB_CM_LAP_UNINIT
:
2978 case IB_CM_LAP_IDLE
:
2980 case IB_CM_MRA_LAP_SENT
:
2981 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2982 counter
[CM_LAP_COUNTER
]);
2983 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2986 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2987 CM_MSG_RESPONSE_OTHER
,
2988 cm_id_priv
->service_timeout
,
2989 cm_id_priv
->private_data
,
2990 cm_id_priv
->private_data_len
);
2991 spin_unlock_irq(&cm_id_priv
->lock
);
2993 if (ib_post_send_mad(msg
, NULL
))
2996 case IB_CM_LAP_RCVD
:
2997 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2998 counter
[CM_LAP_COUNTER
]);
3004 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
3005 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
3006 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3007 work
->mad_recv_wc
->recv_buf
.grh
,
3009 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
,
3011 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3013 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3014 spin_unlock_irq(&cm_id_priv
->lock
);
3017 cm_process_work(cm_id_priv
, work
);
3019 cm_deref_id(cm_id_priv
);
3022 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
3023 deref
: cm_deref_id(cm_id_priv
);
3027 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
3028 struct cm_id_private
*cm_id_priv
,
3029 enum ib_cm_apr_status status
,
3032 const void *private_data
,
3033 u8 private_data_len
)
3035 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
3036 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
3037 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
3038 apr_msg
->ap_status
= (u8
) status
;
3040 if (info
&& info_length
) {
3041 apr_msg
->info_length
= info_length
;
3042 memcpy(apr_msg
->info
, info
, info_length
);
3045 if (private_data
&& private_data_len
)
3046 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
3049 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
3050 enum ib_cm_apr_status status
,
3053 const void *private_data
,
3054 u8 private_data_len
)
3056 struct cm_id_private
*cm_id_priv
;
3057 struct ib_mad_send_buf
*msg
;
3058 unsigned long flags
;
3061 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
3062 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
3065 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3066 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3067 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
3068 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
3069 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
3074 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3078 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
3079 info
, info_length
, private_data
, private_data_len
);
3080 ret
= ib_post_send_mad(msg
, NULL
);
3082 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3087 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3088 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3091 EXPORT_SYMBOL(ib_send_cm_apr
);
3093 static int cm_apr_handler(struct cm_work
*work
)
3095 struct cm_id_private
*cm_id_priv
;
3096 struct cm_apr_msg
*apr_msg
;
3099 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
3100 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
3101 apr_msg
->local_comm_id
);
3103 return -EINVAL
; /* Unmatched reply. */
3105 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
3106 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
3107 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
3108 work
->cm_event
.private_data
= &apr_msg
->private_data
;
3110 spin_lock_irq(&cm_id_priv
->lock
);
3111 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
3112 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
3113 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
3114 spin_unlock_irq(&cm_id_priv
->lock
);
3117 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
3118 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3119 cm_id_priv
->msg
= NULL
;
3121 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3123 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3124 spin_unlock_irq(&cm_id_priv
->lock
);
3127 cm_process_work(cm_id_priv
, work
);
3129 cm_deref_id(cm_id_priv
);
3132 cm_deref_id(cm_id_priv
);
3136 static int cm_timewait_handler(struct cm_work
*work
)
3138 struct cm_timewait_info
*timewait_info
;
3139 struct cm_id_private
*cm_id_priv
;
3142 timewait_info
= (struct cm_timewait_info
*)work
;
3143 spin_lock_irq(&cm
.lock
);
3144 list_del(&timewait_info
->list
);
3145 spin_unlock_irq(&cm
.lock
);
3147 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
3148 timewait_info
->work
.remote_id
);
3152 spin_lock_irq(&cm_id_priv
->lock
);
3153 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
3154 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
3155 spin_unlock_irq(&cm_id_priv
->lock
);
3158 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3159 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3161 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3162 spin_unlock_irq(&cm_id_priv
->lock
);
3165 cm_process_work(cm_id_priv
, work
);
3167 cm_deref_id(cm_id_priv
);
3170 cm_deref_id(cm_id_priv
);
3174 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
3175 struct cm_id_private
*cm_id_priv
,
3176 struct ib_cm_sidr_req_param
*param
)
3178 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
3179 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
3180 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
3181 sidr_req_msg
->pkey
= param
->path
->pkey
;
3182 sidr_req_msg
->service_id
= param
->service_id
;
3184 if (param
->private_data
&& param
->private_data_len
)
3185 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
3186 param
->private_data_len
);
3189 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
3190 struct ib_cm_sidr_req_param
*param
)
3192 struct cm_id_private
*cm_id_priv
;
3193 struct ib_mad_send_buf
*msg
;
3194 unsigned long flags
;
3197 if (!param
->path
|| (param
->private_data
&&
3198 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
3201 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3202 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
, cm_id_priv
);
3206 cm_id
->service_id
= param
->service_id
;
3207 cm_id
->service_mask
= ~cpu_to_be64(0);
3208 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
3209 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
3210 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3214 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
3216 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
3217 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
3219 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3220 if (cm_id
->state
== IB_CM_IDLE
)
3221 ret
= ib_post_send_mad(msg
, NULL
);
3226 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3230 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
3231 cm_id_priv
->msg
= msg
;
3232 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3236 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
3238 static void cm_format_sidr_req_event(struct cm_work
*work
,
3239 struct ib_cm_id
*listen_id
)
3241 struct cm_sidr_req_msg
*sidr_req_msg
;
3242 struct ib_cm_sidr_req_event_param
*param
;
3244 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3245 work
->mad_recv_wc
->recv_buf
.mad
;
3246 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
3247 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
3248 param
->listen_id
= listen_id
;
3249 param
->service_id
= sidr_req_msg
->service_id
;
3250 param
->bth_pkey
= cm_get_bth_pkey(work
);
3251 param
->port
= work
->port
->port_num
;
3252 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
3255 static int cm_sidr_req_handler(struct cm_work
*work
)
3257 struct ib_cm_id
*cm_id
;
3258 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
3259 struct cm_sidr_req_msg
*sidr_req_msg
;
3262 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3264 return PTR_ERR(cm_id
);
3265 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3267 /* Record SGID/SLID and request ID for lookup. */
3268 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3269 work
->mad_recv_wc
->recv_buf
.mad
;
3270 wc
= work
->mad_recv_wc
->wc
;
3271 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3272 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3273 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3274 work
->mad_recv_wc
->recv_buf
.grh
,
3276 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
3277 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3278 atomic_inc(&cm_id_priv
->work_count
);
3280 spin_lock_irq(&cm
.lock
);
3281 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3282 if (cur_cm_id_priv
) {
3283 spin_unlock_irq(&cm
.lock
);
3284 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3285 counter
[CM_SIDR_REQ_COUNTER
]);
3286 goto out
; /* Duplicate message. */
3288 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3289 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3290 sidr_req_msg
->service_id
);
3291 if (!cur_cm_id_priv
) {
3292 spin_unlock_irq(&cm
.lock
);
3293 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3294 goto out
; /* No match. */
3296 atomic_inc(&cur_cm_id_priv
->refcount
);
3297 atomic_inc(&cm_id_priv
->refcount
);
3298 spin_unlock_irq(&cm
.lock
);
3300 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3301 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3302 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3303 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3305 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3306 cm_process_work(cm_id_priv
, work
);
3307 cm_deref_id(cur_cm_id_priv
);
3310 ib_destroy_cm_id(&cm_id_priv
->id
);
3314 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3315 struct cm_id_private
*cm_id_priv
,
3316 struct ib_cm_sidr_rep_param
*param
)
3318 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3320 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3321 sidr_rep_msg
->status
= param
->status
;
3322 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3323 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3324 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3326 if (param
->info
&& param
->info_length
)
3327 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3329 if (param
->private_data
&& param
->private_data_len
)
3330 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3331 param
->private_data_len
);
3334 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3335 struct ib_cm_sidr_rep_param
*param
)
3337 struct cm_id_private
*cm_id_priv
;
3338 struct ib_mad_send_buf
*msg
;
3339 unsigned long flags
;
3342 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3343 (param
->private_data
&&
3344 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3347 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3348 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3349 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3354 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3358 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3360 ret
= ib_post_send_mad(msg
, NULL
);
3362 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3366 cm_id
->state
= IB_CM_IDLE
;
3367 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3369 spin_lock_irqsave(&cm
.lock
, flags
);
3370 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
)) {
3371 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3372 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
3374 spin_unlock_irqrestore(&cm
.lock
, flags
);
3377 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3380 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3382 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3384 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3385 struct ib_cm_sidr_rep_event_param
*param
;
3387 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3388 work
->mad_recv_wc
->recv_buf
.mad
;
3389 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3390 param
->status
= sidr_rep_msg
->status
;
3391 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3392 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3393 param
->info
= &sidr_rep_msg
->info
;
3394 param
->info_len
= sidr_rep_msg
->info_length
;
3395 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3398 static int cm_sidr_rep_handler(struct cm_work
*work
)
3400 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3401 struct cm_id_private
*cm_id_priv
;
3403 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3404 work
->mad_recv_wc
->recv_buf
.mad
;
3405 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3407 return -EINVAL
; /* Unmatched reply. */
3409 spin_lock_irq(&cm_id_priv
->lock
);
3410 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3411 spin_unlock_irq(&cm_id_priv
->lock
);
3414 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3415 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3416 spin_unlock_irq(&cm_id_priv
->lock
);
3418 cm_format_sidr_rep_event(work
);
3419 cm_process_work(cm_id_priv
, work
);
3422 cm_deref_id(cm_id_priv
);
3426 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3427 enum ib_wc_status wc_status
)
3429 struct cm_id_private
*cm_id_priv
;
3430 struct ib_cm_event cm_event
;
3431 enum ib_cm_state state
;
3434 memset(&cm_event
, 0, sizeof cm_event
);
3435 cm_id_priv
= msg
->context
[0];
3437 /* Discard old sends or ones without a response. */
3438 spin_lock_irq(&cm_id_priv
->lock
);
3439 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3440 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3443 pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
3444 state
, ib_wc_status_msg(wc_status
));
3446 case IB_CM_REQ_SENT
:
3447 case IB_CM_MRA_REQ_RCVD
:
3448 cm_reset_to_idle(cm_id_priv
);
3449 cm_event
.event
= IB_CM_REQ_ERROR
;
3451 case IB_CM_REP_SENT
:
3452 case IB_CM_MRA_REP_RCVD
:
3453 cm_reset_to_idle(cm_id_priv
);
3454 cm_event
.event
= IB_CM_REP_ERROR
;
3456 case IB_CM_DREQ_SENT
:
3457 cm_enter_timewait(cm_id_priv
);
3458 cm_event
.event
= IB_CM_DREQ_ERROR
;
3460 case IB_CM_SIDR_REQ_SENT
:
3461 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3462 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3467 spin_unlock_irq(&cm_id_priv
->lock
);
3468 cm_event
.param
.send_status
= wc_status
;
3470 /* No other events can occur on the cm_id at this point. */
3471 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3474 ib_destroy_cm_id(&cm_id_priv
->id
);
3477 spin_unlock_irq(&cm_id_priv
->lock
);
3481 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3482 struct ib_mad_send_wc
*mad_send_wc
)
3484 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3485 struct cm_port
*port
;
3488 port
= mad_agent
->context
;
3489 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3490 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3493 * If the send was in response to a received message (context[0] is not
3494 * set to a cm_id), and is not a REJ, then it is a send that was
3497 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3500 atomic_long_add(1 + msg
->retries
,
3501 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3503 atomic_long_add(msg
->retries
,
3504 &port
->counter_group
[CM_XMIT_RETRIES
].
3505 counter
[attr_index
]);
3507 switch (mad_send_wc
->status
) {
3509 case IB_WC_WR_FLUSH_ERR
:
3513 if (msg
->context
[0] && msg
->context
[1])
3514 cm_process_send_error(msg
, mad_send_wc
->status
);
3521 static void cm_work_handler(struct work_struct
*_work
)
3523 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3526 switch (work
->cm_event
.event
) {
3527 case IB_CM_REQ_RECEIVED
:
3528 ret
= cm_req_handler(work
);
3530 case IB_CM_MRA_RECEIVED
:
3531 ret
= cm_mra_handler(work
);
3533 case IB_CM_REJ_RECEIVED
:
3534 ret
= cm_rej_handler(work
);
3536 case IB_CM_REP_RECEIVED
:
3537 ret
= cm_rep_handler(work
);
3539 case IB_CM_RTU_RECEIVED
:
3540 ret
= cm_rtu_handler(work
);
3542 case IB_CM_USER_ESTABLISHED
:
3543 ret
= cm_establish_handler(work
);
3545 case IB_CM_DREQ_RECEIVED
:
3546 ret
= cm_dreq_handler(work
);
3548 case IB_CM_DREP_RECEIVED
:
3549 ret
= cm_drep_handler(work
);
3551 case IB_CM_SIDR_REQ_RECEIVED
:
3552 ret
= cm_sidr_req_handler(work
);
3554 case IB_CM_SIDR_REP_RECEIVED
:
3555 ret
= cm_sidr_rep_handler(work
);
3557 case IB_CM_LAP_RECEIVED
:
3558 ret
= cm_lap_handler(work
);
3560 case IB_CM_APR_RECEIVED
:
3561 ret
= cm_apr_handler(work
);
3563 case IB_CM_TIMEWAIT_EXIT
:
3564 ret
= cm_timewait_handler(work
);
3574 static int cm_establish(struct ib_cm_id
*cm_id
)
3576 struct cm_id_private
*cm_id_priv
;
3577 struct cm_work
*work
;
3578 unsigned long flags
;
3580 struct cm_device
*cm_dev
;
3582 cm_dev
= ib_get_client_data(cm_id
->device
, &cm_client
);
3586 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3590 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3591 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3592 switch (cm_id
->state
)
3594 case IB_CM_REP_SENT
:
3595 case IB_CM_MRA_REP_RCVD
:
3596 cm_id
->state
= IB_CM_ESTABLISHED
;
3598 case IB_CM_ESTABLISHED
:
3605 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3613 * The CM worker thread may try to destroy the cm_id before it
3614 * can execute this work item. To prevent potential deadlock,
3615 * we need to find the cm_id once we're in the context of the
3616 * worker thread, rather than holding a reference on it.
3618 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3619 work
->local_id
= cm_id
->local_id
;
3620 work
->remote_id
= cm_id
->remote_id
;
3621 work
->mad_recv_wc
= NULL
;
3622 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3624 /* Check if the device started its remove_one */
3625 spin_lock_irqsave(&cm
.lock
, flags
);
3626 if (!cm_dev
->going_down
) {
3627 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3632 spin_unlock_irqrestore(&cm
.lock
, flags
);
3638 static int cm_migrate(struct ib_cm_id
*cm_id
)
3640 struct cm_id_private
*cm_id_priv
;
3641 struct cm_av tmp_av
;
3642 unsigned long flags
;
3643 int tmp_send_port_not_ready
;
3646 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3647 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3648 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3649 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3650 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3651 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3652 /* Swap address vector */
3653 tmp_av
= cm_id_priv
->av
;
3654 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3655 cm_id_priv
->alt_av
= tmp_av
;
3656 /* Swap port send ready state */
3657 tmp_send_port_not_ready
= cm_id_priv
->prim_send_port_not_ready
;
3658 cm_id_priv
->prim_send_port_not_ready
= cm_id_priv
->altr_send_port_not_ready
;
3659 cm_id_priv
->altr_send_port_not_ready
= tmp_send_port_not_ready
;
3662 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3667 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3672 case IB_EVENT_COMM_EST
:
3673 ret
= cm_establish(cm_id
);
3675 case IB_EVENT_PATH_MIG
:
3676 ret
= cm_migrate(cm_id
);
3683 EXPORT_SYMBOL(ib_cm_notify
);
3685 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3686 struct ib_mad_send_buf
*send_buf
,
3687 struct ib_mad_recv_wc
*mad_recv_wc
)
3689 struct cm_port
*port
= mad_agent
->context
;
3690 struct cm_work
*work
;
3691 enum ib_cm_event_type event
;
3696 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3697 case CM_REQ_ATTR_ID
:
3698 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3699 alt_local_lid
!= 0);
3700 event
= IB_CM_REQ_RECEIVED
;
3702 case CM_MRA_ATTR_ID
:
3703 event
= IB_CM_MRA_RECEIVED
;
3705 case CM_REJ_ATTR_ID
:
3706 event
= IB_CM_REJ_RECEIVED
;
3708 case CM_REP_ATTR_ID
:
3709 event
= IB_CM_REP_RECEIVED
;
3711 case CM_RTU_ATTR_ID
:
3712 event
= IB_CM_RTU_RECEIVED
;
3714 case CM_DREQ_ATTR_ID
:
3715 event
= IB_CM_DREQ_RECEIVED
;
3717 case CM_DREP_ATTR_ID
:
3718 event
= IB_CM_DREP_RECEIVED
;
3720 case CM_SIDR_REQ_ATTR_ID
:
3721 event
= IB_CM_SIDR_REQ_RECEIVED
;
3723 case CM_SIDR_REP_ATTR_ID
:
3724 event
= IB_CM_SIDR_REP_RECEIVED
;
3726 case CM_LAP_ATTR_ID
:
3728 event
= IB_CM_LAP_RECEIVED
;
3730 case CM_APR_ATTR_ID
:
3731 event
= IB_CM_APR_RECEIVED
;
3734 ib_free_recv_mad(mad_recv_wc
);
3738 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3739 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3740 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3742 work
= kmalloc(sizeof(*work
) + sizeof(struct sa_path_rec
) * paths
,
3745 ib_free_recv_mad(mad_recv_wc
);
3749 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3750 work
->cm_event
.event
= event
;
3751 work
->mad_recv_wc
= mad_recv_wc
;
3754 /* Check if the device started its remove_one */
3755 spin_lock_irq(&cm
.lock
);
3756 if (!port
->cm_dev
->going_down
)
3757 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3760 spin_unlock_irq(&cm
.lock
);
3764 ib_free_recv_mad(mad_recv_wc
);
3768 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3769 struct ib_qp_attr
*qp_attr
,
3772 unsigned long flags
;
3775 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3776 switch (cm_id_priv
->id
.state
) {
3777 case IB_CM_REQ_SENT
:
3778 case IB_CM_MRA_REQ_RCVD
:
3779 case IB_CM_REQ_RCVD
:
3780 case IB_CM_MRA_REQ_SENT
:
3781 case IB_CM_REP_RCVD
:
3782 case IB_CM_MRA_REP_SENT
:
3783 case IB_CM_REP_SENT
:
3784 case IB_CM_MRA_REP_RCVD
:
3785 case IB_CM_ESTABLISHED
:
3786 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3787 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3788 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3789 if (cm_id_priv
->responder_resources
)
3790 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3791 IB_ACCESS_REMOTE_ATOMIC
;
3792 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3793 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3800 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3804 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3805 struct ib_qp_attr
*qp_attr
,
3808 unsigned long flags
;
3811 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3812 switch (cm_id_priv
->id
.state
) {
3813 case IB_CM_REQ_RCVD
:
3814 case IB_CM_MRA_REQ_SENT
:
3815 case IB_CM_REP_RCVD
:
3816 case IB_CM_MRA_REP_SENT
:
3817 case IB_CM_REP_SENT
:
3818 case IB_CM_MRA_REP_RCVD
:
3819 case IB_CM_ESTABLISHED
:
3820 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3821 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3822 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3823 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3824 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3825 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3826 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3827 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3828 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3829 IB_QP_MIN_RNR_TIMER
;
3830 qp_attr
->max_dest_rd_atomic
=
3831 cm_id_priv
->responder_resources
;
3832 qp_attr
->min_rnr_timer
= 0;
3834 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
)) {
3835 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3836 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3837 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3838 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3839 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3847 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3851 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3852 struct ib_qp_attr
*qp_attr
,
3855 unsigned long flags
;
3858 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3859 switch (cm_id_priv
->id
.state
) {
3860 /* Allow transition to RTS before sending REP */
3861 case IB_CM_REQ_RCVD
:
3862 case IB_CM_MRA_REQ_SENT
:
3864 case IB_CM_REP_RCVD
:
3865 case IB_CM_MRA_REP_SENT
:
3866 case IB_CM_REP_SENT
:
3867 case IB_CM_MRA_REP_RCVD
:
3868 case IB_CM_ESTABLISHED
:
3869 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3870 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3871 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3872 switch (cm_id_priv
->qp_type
) {
3874 case IB_QPT_XRC_INI
:
3875 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3876 IB_QP_MAX_QP_RD_ATOMIC
;
3877 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3878 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3879 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3881 case IB_QPT_XRC_TGT
:
3882 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3883 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3888 if (rdma_ah_get_dlid(&cm_id_priv
->alt_av
.ah_attr
)) {
3889 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3890 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3893 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3894 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3895 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3896 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3897 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3898 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3906 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3910 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3911 struct ib_qp_attr
*qp_attr
,
3914 struct cm_id_private
*cm_id_priv
;
3917 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3918 switch (qp_attr
->qp_state
) {
3920 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3923 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3926 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3934 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3936 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3939 struct cm_counter_group
*group
;
3940 struct cm_counter_attribute
*cm_attr
;
3942 group
= container_of(obj
, struct cm_counter_group
, obj
);
3943 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3945 return sprintf(buf
, "%ld\n",
3946 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3949 static const struct sysfs_ops cm_counter_ops
= {
3950 .show
= cm_show_counter
3953 static struct kobj_type cm_counter_obj_type
= {
3954 .sysfs_ops
= &cm_counter_ops
,
3955 .default_attrs
= cm_counter_default_attrs
3958 static void cm_release_port_obj(struct kobject
*obj
)
3960 struct cm_port
*cm_port
;
3962 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3966 static struct kobj_type cm_port_obj_type
= {
3967 .release
= cm_release_port_obj
3970 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3974 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3977 struct class cm_class
= {
3978 .owner
= THIS_MODULE
,
3979 .name
= "infiniband_cm",
3980 .devnode
= cm_devnode
,
3982 EXPORT_SYMBOL(cm_class
);
3984 static int cm_create_port_fs(struct cm_port
*port
)
3988 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3989 &port
->cm_dev
->device
->kobj
,
3990 "%d", port
->port_num
);
3996 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3997 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3998 &cm_counter_obj_type
,
4000 "%s", counter_group_names
[i
]);
4009 kobject_put(&port
->counter_group
[i
].obj
);
4010 kobject_put(&port
->port_obj
);
4015 static void cm_remove_port_fs(struct cm_port
*port
)
4019 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
4020 kobject_put(&port
->counter_group
[i
].obj
);
4022 kobject_put(&port
->port_obj
);
4025 static void cm_add_one(struct ib_device
*ib_device
)
4027 struct cm_device
*cm_dev
;
4028 struct cm_port
*port
;
4029 struct ib_mad_reg_req reg_req
= {
4030 .mgmt_class
= IB_MGMT_CLASS_CM
,
4031 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
4033 struct ib_port_modify port_modify
= {
4034 .set_port_cap_mask
= IB_PORT_CM_SUP
4036 unsigned long flags
;
4041 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
4042 ib_device
->phys_port_cnt
, GFP_KERNEL
);
4046 cm_dev
->ib_device
= ib_device
;
4047 cm_dev
->ack_delay
= ib_device
->attrs
.local_ca_ack_delay
;
4048 cm_dev
->going_down
= 0;
4049 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
4051 "%s", ib_device
->name
);
4052 if (IS_ERR(cm_dev
->device
)) {
4057 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
4058 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
4059 if (!rdma_cap_ib_cm(ib_device
, i
))
4062 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
4066 cm_dev
->port
[i
-1] = port
;
4067 port
->cm_dev
= cm_dev
;
4070 INIT_LIST_HEAD(&port
->cm_priv_prim_list
);
4071 INIT_LIST_HEAD(&port
->cm_priv_altr_list
);
4073 ret
= cm_create_port_fs(port
);
4077 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
4085 if (IS_ERR(port
->mad_agent
))
4088 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
4098 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
4100 write_lock_irqsave(&cm
.device_lock
, flags
);
4101 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
4102 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4106 ib_unregister_mad_agent(port
->mad_agent
);
4108 cm_remove_port_fs(port
);
4110 port_modify
.set_port_cap_mask
= 0;
4111 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
4113 if (!rdma_cap_ib_cm(ib_device
, i
))
4116 port
= cm_dev
->port
[i
-1];
4117 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4118 ib_unregister_mad_agent(port
->mad_agent
);
4119 cm_remove_port_fs(port
);
4122 device_unregister(cm_dev
->device
);
4126 static void cm_remove_one(struct ib_device
*ib_device
, void *client_data
)
4128 struct cm_device
*cm_dev
= client_data
;
4129 struct cm_port
*port
;
4130 struct cm_id_private
*cm_id_priv
;
4131 struct ib_mad_agent
*cur_mad_agent
;
4132 struct ib_port_modify port_modify
= {
4133 .clr_port_cap_mask
= IB_PORT_CM_SUP
4135 unsigned long flags
;
4141 write_lock_irqsave(&cm
.device_lock
, flags
);
4142 list_del(&cm_dev
->list
);
4143 write_unlock_irqrestore(&cm
.device_lock
, flags
);
4145 spin_lock_irq(&cm
.lock
);
4146 cm_dev
->going_down
= 1;
4147 spin_unlock_irq(&cm
.lock
);
4149 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
4150 if (!rdma_cap_ib_cm(ib_device
, i
))
4153 port
= cm_dev
->port
[i
-1];
4154 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
4155 /* Mark all the cm_id's as not valid */
4156 spin_lock_irq(&cm
.lock
);
4157 list_for_each_entry(cm_id_priv
, &port
->cm_priv_altr_list
, altr_list
)
4158 cm_id_priv
->altr_send_port_not_ready
= 1;
4159 list_for_each_entry(cm_id_priv
, &port
->cm_priv_prim_list
, prim_list
)
4160 cm_id_priv
->prim_send_port_not_ready
= 1;
4161 spin_unlock_irq(&cm
.lock
);
4163 * We flush the queue here after the going_down set, this
4164 * verify that no new works will be queued in the recv handler,
4165 * after that we can call the unregister_mad_agent
4167 flush_workqueue(cm
.wq
);
4168 spin_lock_irq(&cm
.state_lock
);
4169 cur_mad_agent
= port
->mad_agent
;
4170 port
->mad_agent
= NULL
;
4171 spin_unlock_irq(&cm
.state_lock
);
4172 ib_unregister_mad_agent(cur_mad_agent
);
4173 cm_remove_port_fs(port
);
4176 device_unregister(cm_dev
->device
);
4180 static int __init
ib_cm_init(void)
4184 memset(&cm
, 0, sizeof cm
);
4185 INIT_LIST_HEAD(&cm
.device_list
);
4186 rwlock_init(&cm
.device_lock
);
4187 spin_lock_init(&cm
.lock
);
4188 spin_lock_init(&cm
.state_lock
);
4189 cm
.listen_service_table
= RB_ROOT
;
4190 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
4191 cm
.remote_id_table
= RB_ROOT
;
4192 cm
.remote_qp_table
= RB_ROOT
;
4193 cm
.remote_sidr_table
= RB_ROOT
;
4194 idr_init(&cm
.local_id_table
);
4195 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
4196 INIT_LIST_HEAD(&cm
.timewait_list
);
4198 ret
= class_register(&cm_class
);
4204 cm
.wq
= create_workqueue("ib_cm");
4210 ret
= ib_register_client(&cm_client
);
4216 destroy_workqueue(cm
.wq
);
4218 class_unregister(&cm_class
);
4220 idr_destroy(&cm
.local_id_table
);
4224 static void __exit
ib_cm_cleanup(void)
4226 struct cm_timewait_info
*timewait_info
, *tmp
;
4228 spin_lock_irq(&cm
.lock
);
4229 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
4230 cancel_delayed_work(&timewait_info
->work
.work
);
4231 spin_unlock_irq(&cm
.lock
);
4233 ib_unregister_client(&cm_client
);
4234 destroy_workqueue(cm
.wq
);
4236 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
4237 list_del(&timewait_info
->list
);
4238 kfree(timewait_info
);
4241 class_unregister(&cm_class
);
4242 idr_destroy(&cm
.local_id_table
);
4245 module_init(ib_cm_init
);
4246 module_exit(ib_cm_cleanup
);