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
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/device.h>
41 #include <linux/err.h>
42 #include <linux/idr.h>
43 #include <linux/interrupt.h>
44 #include <linux/random.h>
45 #include <linux/rbtree.h>
46 #include <linux/spinlock.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_cm.h>
54 MODULE_AUTHOR("Sean Hefty");
55 MODULE_DESCRIPTION("InfiniBand CM");
56 MODULE_LICENSE("Dual BSD/GPL");
58 static void cm_add_one(struct ib_device
*device
);
59 static void cm_remove_one(struct ib_device
*device
);
61 static struct ib_client cm_client
= {
64 .remove
= cm_remove_one
69 struct list_head device_list
;
71 struct rb_root listen_service_table
;
72 u64 listen_service_id
;
73 /* struct rb_root peer_service_table; todo: fix peer to peer */
74 struct rb_root remote_qp_table
;
75 struct rb_root remote_id_table
;
76 struct rb_root remote_sidr_table
;
77 struct idr local_id_table
;
78 __be32 random_id_operand
;
79 struct list_head timewait_list
;
80 struct workqueue_struct
*wq
;
83 /* Counter indexes ordered by attribute ID */
97 CM_ATTR_ID_OFFSET
= 0x0010,
108 static char const counter_group_names
[CM_COUNTER_GROUPS
]
109 [sizeof("cm_rx_duplicates")] = {
110 "cm_tx_msgs", "cm_tx_retries",
111 "cm_rx_msgs", "cm_rx_duplicates"
114 struct cm_counter_group
{
116 atomic_long_t counter
[CM_ATTR_COUNT
];
119 struct cm_counter_attribute
{
120 struct attribute attr
;
124 #define CM_COUNTER_ATTR(_name, _index) \
125 struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \
130 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
131 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
132 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
133 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
134 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
135 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
136 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
137 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
138 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
139 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
140 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
142 static struct attribute
*cm_counter_default_attrs
[] = {
143 &cm_req_counter_attr
.attr
,
144 &cm_mra_counter_attr
.attr
,
145 &cm_rej_counter_attr
.attr
,
146 &cm_rep_counter_attr
.attr
,
147 &cm_rtu_counter_attr
.attr
,
148 &cm_dreq_counter_attr
.attr
,
149 &cm_drep_counter_attr
.attr
,
150 &cm_sidr_req_counter_attr
.attr
,
151 &cm_sidr_rep_counter_attr
.attr
,
152 &cm_lap_counter_attr
.attr
,
153 &cm_apr_counter_attr
.attr
,
158 struct cm_device
*cm_dev
;
159 struct ib_mad_agent
*mad_agent
;
160 struct kobject port_obj
;
162 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
166 struct list_head list
;
167 struct ib_device
*device
;
168 struct kobject dev_obj
;
170 struct cm_port
*port
[0];
174 struct cm_port
*port
;
176 struct ib_ah_attr ah_attr
;
182 struct delayed_work work
;
183 struct list_head list
;
184 struct cm_port
*port
;
185 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
186 __be32 local_id
; /* Established / timewait */
188 struct ib_cm_event cm_event
;
189 struct ib_sa_path_rec path
[0];
192 struct cm_timewait_info
{
193 struct cm_work work
; /* Must be first. */
194 struct list_head list
;
195 struct rb_node remote_qp_node
;
196 struct rb_node remote_id_node
;
197 __be64 remote_ca_guid
;
199 u8 inserted_remote_qp
;
200 u8 inserted_remote_id
;
203 struct cm_id_private
{
206 struct rb_node service_node
;
207 struct rb_node sidr_id_node
;
208 spinlock_t lock
; /* Do not acquire inside cm.lock */
209 struct completion comp
;
212 struct ib_mad_send_buf
*msg
;
213 struct cm_timewait_info
*timewait_info
;
214 /* todo: use alternate port on send failure */
217 struct ib_cm_compare_data
*compare_data
;
223 enum ib_qp_type qp_type
;
227 enum ib_mtu path_mtu
;
232 u8 responder_resources
;
239 struct list_head work_list
;
243 static void cm_work_handler(struct work_struct
*work
);
245 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
247 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
248 complete(&cm_id_priv
->comp
);
251 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
252 struct ib_mad_send_buf
**msg
)
254 struct ib_mad_agent
*mad_agent
;
255 struct ib_mad_send_buf
*m
;
258 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
259 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
263 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
264 cm_id_priv
->av
.pkey_index
,
265 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
272 /* Timeout set by caller if response is expected. */
274 m
->retries
= cm_id_priv
->max_cm_retries
;
276 atomic_inc(&cm_id_priv
->refcount
);
277 m
->context
[0] = cm_id_priv
;
282 static int cm_alloc_response_msg(struct cm_port
*port
,
283 struct ib_mad_recv_wc
*mad_recv_wc
,
284 struct ib_mad_send_buf
**msg
)
286 struct ib_mad_send_buf
*m
;
289 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
290 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
294 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
295 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
306 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
308 ib_destroy_ah(msg
->ah
);
310 cm_deref_id(msg
->context
[0]);
311 ib_free_send_mad(msg
);
314 static void * cm_copy_private_data(const void *private_data
,
319 if (!private_data
|| !private_data_len
)
322 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
324 return ERR_PTR(-ENOMEM
);
329 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
330 void *private_data
, u8 private_data_len
)
332 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
333 kfree(cm_id_priv
->private_data
);
335 cm_id_priv
->private_data
= private_data
;
336 cm_id_priv
->private_data_len
= private_data_len
;
339 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
340 struct ib_grh
*grh
, struct cm_av
*av
)
343 av
->pkey_index
= wc
->pkey_index
;
344 ib_init_ah_from_wc(port
->cm_dev
->device
, port
->port_num
, wc
,
348 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
350 struct cm_device
*cm_dev
;
351 struct cm_port
*port
= NULL
;
356 read_lock_irqsave(&cm
.device_lock
, flags
);
357 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
358 if (!ib_find_cached_gid(cm_dev
->device
, &path
->sgid
,
360 port
= cm_dev
->port
[p
-1];
364 read_unlock_irqrestore(&cm
.device_lock
, flags
);
369 ret
= ib_find_cached_pkey(cm_dev
->device
, port
->port_num
,
370 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
375 ib_init_ah_from_path(cm_dev
->device
, port
->port_num
, path
,
377 av
->timeout
= path
->packet_life_time
+ 1;
381 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
388 spin_lock_irqsave(&cm
.lock
, flags
);
389 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
,
392 next_id
= ((unsigned) id
+ 1) & MAX_ID_MASK
;
393 spin_unlock_irqrestore(&cm
.lock
, flags
);
394 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
396 cm_id_priv
->id
.local_id
= (__force __be32
) (id
^ cm
.random_id_operand
);
400 static void cm_free_id(__be32 local_id
)
402 spin_lock_irq(&cm
.lock
);
403 idr_remove(&cm
.local_id_table
,
404 (__force
int) (local_id
^ cm
.random_id_operand
));
405 spin_unlock_irq(&cm
.lock
);
408 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
410 struct cm_id_private
*cm_id_priv
;
412 cm_id_priv
= idr_find(&cm
.local_id_table
,
413 (__force
int) (local_id
^ cm
.random_id_operand
));
415 if (cm_id_priv
->id
.remote_id
== remote_id
)
416 atomic_inc(&cm_id_priv
->refcount
);
424 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
426 struct cm_id_private
*cm_id_priv
;
428 spin_lock_irq(&cm
.lock
);
429 cm_id_priv
= cm_get_id(local_id
, remote_id
);
430 spin_unlock_irq(&cm
.lock
);
435 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
439 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
440 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
441 ((unsigned long *) mask
)[i
];
444 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
445 struct ib_cm_compare_data
*dst_data
)
447 u8 src
[IB_CM_COMPARE_SIZE
];
448 u8 dst
[IB_CM_COMPARE_SIZE
];
450 if (!src_data
|| !dst_data
)
453 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
454 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
455 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
458 static int cm_compare_private_data(u8
*private_data
,
459 struct ib_cm_compare_data
*dst_data
)
461 u8 src
[IB_CM_COMPARE_SIZE
];
466 cm_mask_copy(src
, private_data
, dst_data
->mask
);
467 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
470 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
472 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
473 struct rb_node
*parent
= NULL
;
474 struct cm_id_private
*cur_cm_id_priv
;
475 __be64 service_id
= cm_id_priv
->id
.service_id
;
476 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
481 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
483 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
484 cur_cm_id_priv
->compare_data
);
485 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
486 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
487 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
489 return cur_cm_id_priv
;
491 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
492 link
= &(*link
)->rb_left
;
493 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
494 link
= &(*link
)->rb_right
;
495 else if (service_id
< cur_cm_id_priv
->id
.service_id
)
496 link
= &(*link
)->rb_left
;
497 else if (service_id
> cur_cm_id_priv
->id
.service_id
)
498 link
= &(*link
)->rb_right
;
499 else if (data_cmp
< 0)
500 link
= &(*link
)->rb_left
;
502 link
= &(*link
)->rb_right
;
504 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
505 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
509 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
513 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
514 struct cm_id_private
*cm_id_priv
;
518 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
519 data_cmp
= cm_compare_private_data(private_data
,
520 cm_id_priv
->compare_data
);
521 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
522 cm_id_priv
->id
.service_id
&&
523 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
526 if (device
< cm_id_priv
->id
.device
)
527 node
= node
->rb_left
;
528 else if (device
> cm_id_priv
->id
.device
)
529 node
= node
->rb_right
;
530 else if (service_id
< cm_id_priv
->id
.service_id
)
531 node
= node
->rb_left
;
532 else if (service_id
> cm_id_priv
->id
.service_id
)
533 node
= node
->rb_right
;
534 else if (data_cmp
< 0)
535 node
= node
->rb_left
;
537 node
= node
->rb_right
;
542 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
545 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
546 struct rb_node
*parent
= NULL
;
547 struct cm_timewait_info
*cur_timewait_info
;
548 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
549 __be32 remote_id
= timewait_info
->work
.remote_id
;
553 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
555 if (remote_id
< cur_timewait_info
->work
.remote_id
)
556 link
= &(*link
)->rb_left
;
557 else if (remote_id
> cur_timewait_info
->work
.remote_id
)
558 link
= &(*link
)->rb_right
;
559 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
560 link
= &(*link
)->rb_left
;
561 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
562 link
= &(*link
)->rb_right
;
564 return cur_timewait_info
;
566 timewait_info
->inserted_remote_id
= 1;
567 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
568 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
572 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
575 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
576 struct cm_timewait_info
*timewait_info
;
579 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
581 if (remote_id
< timewait_info
->work
.remote_id
)
582 node
= node
->rb_left
;
583 else if (remote_id
> timewait_info
->work
.remote_id
)
584 node
= node
->rb_right
;
585 else if (remote_ca_guid
< timewait_info
->remote_ca_guid
)
586 node
= node
->rb_left
;
587 else if (remote_ca_guid
> timewait_info
->remote_ca_guid
)
588 node
= node
->rb_right
;
590 return timewait_info
;
595 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
598 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
599 struct rb_node
*parent
= NULL
;
600 struct cm_timewait_info
*cur_timewait_info
;
601 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
602 __be32 remote_qpn
= timewait_info
->remote_qpn
;
606 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
608 if (remote_qpn
< cur_timewait_info
->remote_qpn
)
609 link
= &(*link
)->rb_left
;
610 else if (remote_qpn
> cur_timewait_info
->remote_qpn
)
611 link
= &(*link
)->rb_right
;
612 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
613 link
= &(*link
)->rb_left
;
614 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
615 link
= &(*link
)->rb_right
;
617 return cur_timewait_info
;
619 timewait_info
->inserted_remote_qp
= 1;
620 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
621 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
625 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
628 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
629 struct rb_node
*parent
= NULL
;
630 struct cm_id_private
*cur_cm_id_priv
;
631 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
632 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
636 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
638 if (remote_id
< cur_cm_id_priv
->id
.remote_id
)
639 link
= &(*link
)->rb_left
;
640 else if (remote_id
> cur_cm_id_priv
->id
.remote_id
)
641 link
= &(*link
)->rb_right
;
644 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
647 link
= &(*link
)->rb_left
;
649 link
= &(*link
)->rb_right
;
651 return cur_cm_id_priv
;
654 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
655 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
659 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
660 enum ib_cm_sidr_status status
)
662 struct ib_cm_sidr_rep_param param
;
664 memset(¶m
, 0, sizeof param
);
665 param
.status
= status
;
666 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
669 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
670 ib_cm_handler cm_handler
,
673 struct cm_id_private
*cm_id_priv
;
676 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
678 return ERR_PTR(-ENOMEM
);
680 cm_id_priv
->id
.state
= IB_CM_IDLE
;
681 cm_id_priv
->id
.device
= device
;
682 cm_id_priv
->id
.cm_handler
= cm_handler
;
683 cm_id_priv
->id
.context
= context
;
684 cm_id_priv
->id
.remote_cm_qpn
= 1;
685 ret
= cm_alloc_id(cm_id_priv
);
689 spin_lock_init(&cm_id_priv
->lock
);
690 init_completion(&cm_id_priv
->comp
);
691 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
692 atomic_set(&cm_id_priv
->work_count
, -1);
693 atomic_set(&cm_id_priv
->refcount
, 1);
694 return &cm_id_priv
->id
;
698 return ERR_PTR(-ENOMEM
);
700 EXPORT_SYMBOL(ib_create_cm_id
);
702 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
704 struct cm_work
*work
;
706 if (list_empty(&cm_id_priv
->work_list
))
709 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
710 list_del(&work
->list
);
714 static void cm_free_work(struct cm_work
*work
)
716 if (work
->mad_recv_wc
)
717 ib_free_recv_mad(work
->mad_recv_wc
);
721 static inline int cm_convert_to_ms(int iba_time
)
723 /* approximate conversion to ms from 4.096us x 2^iba_time */
724 return 1 << max(iba_time
- 8, 0);
728 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
729 * Because of how ack_timeout is stored, adding one doubles the timeout.
730 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
731 * increment it (round up) only if the other is within 50%.
733 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
735 int ack_timeout
= packet_life_time
+ 1;
737 if (ack_timeout
>= ca_ack_delay
)
738 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
740 ack_timeout
= ca_ack_delay
+
741 (ack_timeout
>= (ca_ack_delay
- 1));
743 return min(31, ack_timeout
);
746 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
748 if (timewait_info
->inserted_remote_id
) {
749 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
750 timewait_info
->inserted_remote_id
= 0;
753 if (timewait_info
->inserted_remote_qp
) {
754 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
755 timewait_info
->inserted_remote_qp
= 0;
759 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
761 struct cm_timewait_info
*timewait_info
;
763 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
765 return ERR_PTR(-ENOMEM
);
767 timewait_info
->work
.local_id
= local_id
;
768 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
769 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
770 return timewait_info
;
773 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
778 spin_lock_irqsave(&cm
.lock
, flags
);
779 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
780 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
781 spin_unlock_irqrestore(&cm
.lock
, flags
);
784 * The cm_id could be destroyed by the user before we exit timewait.
785 * To protect against this, we search for the cm_id after exiting
786 * timewait before notifying the user that we've exited timewait.
788 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
789 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
790 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
791 msecs_to_jiffies(wait_time
));
792 cm_id_priv
->timewait_info
= NULL
;
795 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
799 cm_id_priv
->id
.state
= IB_CM_IDLE
;
800 if (cm_id_priv
->timewait_info
) {
801 spin_lock_irqsave(&cm
.lock
, flags
);
802 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
803 spin_unlock_irqrestore(&cm
.lock
, flags
);
804 kfree(cm_id_priv
->timewait_info
);
805 cm_id_priv
->timewait_info
= NULL
;
809 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
811 struct cm_id_private
*cm_id_priv
;
812 struct cm_work
*work
;
814 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
816 spin_lock_irq(&cm_id_priv
->lock
);
817 switch (cm_id
->state
) {
819 cm_id
->state
= IB_CM_IDLE
;
820 spin_unlock_irq(&cm_id_priv
->lock
);
821 spin_lock_irq(&cm
.lock
);
822 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
823 spin_unlock_irq(&cm
.lock
);
825 case IB_CM_SIDR_REQ_SENT
:
826 cm_id
->state
= IB_CM_IDLE
;
827 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
828 spin_unlock_irq(&cm_id_priv
->lock
);
830 case IB_CM_SIDR_REQ_RCVD
:
831 spin_unlock_irq(&cm_id_priv
->lock
);
832 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
835 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
836 spin_unlock_irq(&cm_id_priv
->lock
);
837 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
838 &cm_id_priv
->id
.device
->node_guid
,
839 sizeof cm_id_priv
->id
.device
->node_guid
,
843 if (err
== -ENOMEM
) {
844 /* Do not reject to allow future retries. */
845 cm_reset_to_idle(cm_id_priv
);
846 spin_unlock_irq(&cm_id_priv
->lock
);
848 spin_unlock_irq(&cm_id_priv
->lock
);
849 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
853 case IB_CM_MRA_REQ_RCVD
:
855 case IB_CM_MRA_REP_RCVD
:
856 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
858 case IB_CM_MRA_REQ_SENT
:
860 case IB_CM_MRA_REP_SENT
:
861 spin_unlock_irq(&cm_id_priv
->lock
);
862 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
865 case IB_CM_ESTABLISHED
:
866 spin_unlock_irq(&cm_id_priv
->lock
);
867 ib_send_cm_dreq(cm_id
, NULL
, 0);
869 case IB_CM_DREQ_SENT
:
870 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
871 cm_enter_timewait(cm_id_priv
);
872 spin_unlock_irq(&cm_id_priv
->lock
);
874 case IB_CM_DREQ_RCVD
:
875 spin_unlock_irq(&cm_id_priv
->lock
);
876 ib_send_cm_drep(cm_id
, NULL
, 0);
879 spin_unlock_irq(&cm_id_priv
->lock
);
883 cm_free_id(cm_id
->local_id
);
884 cm_deref_id(cm_id_priv
);
885 wait_for_completion(&cm_id_priv
->comp
);
886 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
888 kfree(cm_id_priv
->compare_data
);
889 kfree(cm_id_priv
->private_data
);
893 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
895 cm_destroy_id(cm_id
, 0);
897 EXPORT_SYMBOL(ib_destroy_cm_id
);
899 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
900 struct ib_cm_compare_data
*compare_data
)
902 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
906 service_mask
= service_mask
? service_mask
:
907 __constant_cpu_to_be64(~0ULL);
908 service_id
&= service_mask
;
909 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
910 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
913 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
914 if (cm_id
->state
!= IB_CM_IDLE
)
918 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
920 if (!cm_id_priv
->compare_data
)
922 cm_mask_copy(cm_id_priv
->compare_data
->data
,
923 compare_data
->data
, compare_data
->mask
);
924 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
928 cm_id
->state
= IB_CM_LISTEN
;
930 spin_lock_irqsave(&cm
.lock
, flags
);
931 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
932 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
933 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
935 cm_id
->service_id
= service_id
;
936 cm_id
->service_mask
= service_mask
;
938 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
939 spin_unlock_irqrestore(&cm
.lock
, flags
);
941 if (cur_cm_id_priv
) {
942 cm_id
->state
= IB_CM_IDLE
;
943 kfree(cm_id_priv
->compare_data
);
944 cm_id_priv
->compare_data
= NULL
;
949 EXPORT_SYMBOL(ib_cm_listen
);
951 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
952 enum cm_msg_sequence msg_seq
)
956 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
957 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
959 return cpu_to_be64(hi_tid
| low_tid
);
962 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
963 __be16 attr_id
, __be64 tid
)
965 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
966 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
967 hdr
->class_version
= IB_CM_CLASS_VERSION
;
968 hdr
->method
= IB_MGMT_METHOD_SEND
;
969 hdr
->attr_id
= attr_id
;
973 static void cm_format_req(struct cm_req_msg
*req_msg
,
974 struct cm_id_private
*cm_id_priv
,
975 struct ib_cm_req_param
*param
)
977 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
978 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
980 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
981 req_msg
->service_id
= param
->service_id
;
982 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
983 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
984 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
985 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
986 cm_req_set_remote_resp_timeout(req_msg
,
987 param
->remote_cm_response_timeout
);
988 cm_req_set_qp_type(req_msg
, param
->qp_type
);
989 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
990 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
991 cm_req_set_local_resp_timeout(req_msg
,
992 param
->local_cm_response_timeout
);
993 cm_req_set_retry_count(req_msg
, param
->retry_count
);
994 req_msg
->pkey
= param
->primary_path
->pkey
;
995 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
996 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
997 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
998 cm_req_set_srq(req_msg
, param
->srq
);
1000 req_msg
->primary_local_lid
= param
->primary_path
->slid
;
1001 req_msg
->primary_remote_lid
= param
->primary_path
->dlid
;
1002 req_msg
->primary_local_gid
= param
->primary_path
->sgid
;
1003 req_msg
->primary_remote_gid
= param
->primary_path
->dgid
;
1004 cm_req_set_primary_flow_label(req_msg
, param
->primary_path
->flow_label
);
1005 cm_req_set_primary_packet_rate(req_msg
, param
->primary_path
->rate
);
1006 req_msg
->primary_traffic_class
= param
->primary_path
->traffic_class
;
1007 req_msg
->primary_hop_limit
= param
->primary_path
->hop_limit
;
1008 cm_req_set_primary_sl(req_msg
, param
->primary_path
->sl
);
1009 cm_req_set_primary_subnet_local(req_msg
, 1); /* local only... */
1010 cm_req_set_primary_local_ack_timeout(req_msg
,
1011 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1012 param
->primary_path
->packet_life_time
));
1014 if (param
->alternate_path
) {
1015 req_msg
->alt_local_lid
= param
->alternate_path
->slid
;
1016 req_msg
->alt_remote_lid
= param
->alternate_path
->dlid
;
1017 req_msg
->alt_local_gid
= param
->alternate_path
->sgid
;
1018 req_msg
->alt_remote_gid
= param
->alternate_path
->dgid
;
1019 cm_req_set_alt_flow_label(req_msg
,
1020 param
->alternate_path
->flow_label
);
1021 cm_req_set_alt_packet_rate(req_msg
, param
->alternate_path
->rate
);
1022 req_msg
->alt_traffic_class
= param
->alternate_path
->traffic_class
;
1023 req_msg
->alt_hop_limit
= param
->alternate_path
->hop_limit
;
1024 cm_req_set_alt_sl(req_msg
, param
->alternate_path
->sl
);
1025 cm_req_set_alt_subnet_local(req_msg
, 1); /* local only... */
1026 cm_req_set_alt_local_ack_timeout(req_msg
,
1027 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1028 param
->alternate_path
->packet_life_time
));
1031 if (param
->private_data
&& param
->private_data_len
)
1032 memcpy(req_msg
->private_data
, param
->private_data
,
1033 param
->private_data_len
);
1036 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1038 /* peer-to-peer not supported */
1039 if (param
->peer_to_peer
)
1042 if (!param
->primary_path
)
1045 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
1048 if (param
->private_data
&&
1049 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1052 if (param
->alternate_path
&&
1053 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1054 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1060 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1061 struct ib_cm_req_param
*param
)
1063 struct cm_id_private
*cm_id_priv
;
1064 struct cm_req_msg
*req_msg
;
1065 unsigned long flags
;
1068 ret
= cm_validate_req_param(param
);
1072 /* Verify that we're not in timewait. */
1073 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1074 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1075 if (cm_id
->state
!= IB_CM_IDLE
) {
1076 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1080 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1082 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1084 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1085 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1089 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1092 if (param
->alternate_path
) {
1093 ret
= cm_init_av_by_path(param
->alternate_path
,
1094 &cm_id_priv
->alt_av
);
1098 cm_id
->service_id
= param
->service_id
;
1099 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
1100 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1101 param
->primary_path
->packet_life_time
) * 2 +
1103 param
->remote_cm_response_timeout
);
1104 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1105 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1106 cm_id_priv
->responder_resources
= param
->responder_resources
;
1107 cm_id_priv
->retry_count
= param
->retry_count
;
1108 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1109 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1110 cm_id_priv
->qp_type
= param
->qp_type
;
1112 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1116 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1117 cm_format_req(req_msg
, cm_id_priv
, param
);
1118 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1119 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1120 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1122 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1123 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1125 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1126 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1128 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1131 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1132 cm_id
->state
= IB_CM_REQ_SENT
;
1133 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1136 error2
: cm_free_msg(cm_id_priv
->msg
);
1137 error1
: kfree(cm_id_priv
->timewait_info
);
1140 EXPORT_SYMBOL(ib_send_cm_req
);
1142 static int cm_issue_rej(struct cm_port
*port
,
1143 struct ib_mad_recv_wc
*mad_recv_wc
,
1144 enum ib_cm_rej_reason reason
,
1145 enum cm_msg_response msg_rejected
,
1146 void *ari
, u8 ari_length
)
1148 struct ib_mad_send_buf
*msg
= NULL
;
1149 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1152 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1156 /* We just need common CM header information. Cast to any message. */
1157 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1158 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1160 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1161 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1162 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1163 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1164 rej_msg
->reason
= cpu_to_be16(reason
);
1166 if (ari
&& ari_length
) {
1167 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1168 memcpy(rej_msg
->ari
, ari
, ari_length
);
1171 ret
= ib_post_send_mad(msg
, NULL
);
1178 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1179 __be32 local_qpn
, __be32 remote_qpn
)
1181 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1182 ((local_ca_guid
== remote_ca_guid
) &&
1183 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1186 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1187 struct ib_sa_path_rec
*primary_path
,
1188 struct ib_sa_path_rec
*alt_path
)
1190 memset(primary_path
, 0, sizeof *primary_path
);
1191 primary_path
->dgid
= req_msg
->primary_local_gid
;
1192 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1193 primary_path
->dlid
= req_msg
->primary_local_lid
;
1194 primary_path
->slid
= req_msg
->primary_remote_lid
;
1195 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1196 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1197 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1198 primary_path
->reversible
= 1;
1199 primary_path
->pkey
= req_msg
->pkey
;
1200 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1201 primary_path
->mtu_selector
= IB_SA_EQ
;
1202 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1203 primary_path
->rate_selector
= IB_SA_EQ
;
1204 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1205 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1206 primary_path
->packet_life_time
=
1207 cm_req_get_primary_local_ack_timeout(req_msg
);
1208 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1210 if (req_msg
->alt_local_lid
) {
1211 memset(alt_path
, 0, sizeof *alt_path
);
1212 alt_path
->dgid
= req_msg
->alt_local_gid
;
1213 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1214 alt_path
->dlid
= req_msg
->alt_local_lid
;
1215 alt_path
->slid
= req_msg
->alt_remote_lid
;
1216 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1217 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1218 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1219 alt_path
->reversible
= 1;
1220 alt_path
->pkey
= req_msg
->pkey
;
1221 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1222 alt_path
->mtu_selector
= IB_SA_EQ
;
1223 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1224 alt_path
->rate_selector
= IB_SA_EQ
;
1225 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1226 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1227 alt_path
->packet_life_time
=
1228 cm_req_get_alt_local_ack_timeout(req_msg
);
1229 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1233 static void cm_format_req_event(struct cm_work
*work
,
1234 struct cm_id_private
*cm_id_priv
,
1235 struct ib_cm_id
*listen_id
)
1237 struct cm_req_msg
*req_msg
;
1238 struct ib_cm_req_event_param
*param
;
1240 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1241 param
= &work
->cm_event
.param
.req_rcvd
;
1242 param
->listen_id
= listen_id
;
1243 param
->port
= cm_id_priv
->av
.port
->port_num
;
1244 param
->primary_path
= &work
->path
[0];
1245 if (req_msg
->alt_local_lid
)
1246 param
->alternate_path
= &work
->path
[1];
1248 param
->alternate_path
= NULL
;
1249 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1250 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1251 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1252 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1253 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1254 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1255 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1256 param
->local_cm_response_timeout
=
1257 cm_req_get_remote_resp_timeout(req_msg
);
1258 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1259 param
->remote_cm_response_timeout
=
1260 cm_req_get_local_resp_timeout(req_msg
);
1261 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1262 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1263 param
->srq
= cm_req_get_srq(req_msg
);
1264 work
->cm_event
.private_data
= &req_msg
->private_data
;
1267 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1268 struct cm_work
*work
)
1272 /* We will typically only have the current event to report. */
1273 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1276 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1277 spin_lock_irq(&cm_id_priv
->lock
);
1278 work
= cm_dequeue_work(cm_id_priv
);
1279 spin_unlock_irq(&cm_id_priv
->lock
);
1281 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1285 cm_deref_id(cm_id_priv
);
1287 cm_destroy_id(&cm_id_priv
->id
, ret
);
1290 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1291 struct cm_id_private
*cm_id_priv
,
1292 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1293 const void *private_data
, u8 private_data_len
)
1295 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1296 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1297 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1298 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1299 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1301 if (private_data
&& private_data_len
)
1302 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1305 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1306 struct cm_id_private
*cm_id_priv
,
1307 enum ib_cm_rej_reason reason
,
1310 const void *private_data
,
1311 u8 private_data_len
)
1313 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1314 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1316 switch(cm_id_priv
->id
.state
) {
1317 case IB_CM_REQ_RCVD
:
1318 rej_msg
->local_comm_id
= 0;
1319 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1321 case IB_CM_MRA_REQ_SENT
:
1322 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1323 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1325 case IB_CM_REP_RCVD
:
1326 case IB_CM_MRA_REP_SENT
:
1327 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1328 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1331 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1332 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1336 rej_msg
->reason
= cpu_to_be16(reason
);
1337 if (ari
&& ari_length
) {
1338 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1339 memcpy(rej_msg
->ari
, ari
, ari_length
);
1342 if (private_data
&& private_data_len
)
1343 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1346 static void cm_dup_req_handler(struct cm_work
*work
,
1347 struct cm_id_private
*cm_id_priv
)
1349 struct ib_mad_send_buf
*msg
= NULL
;
1352 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1353 counter
[CM_REQ_COUNTER
]);
1355 /* Quick state check to discard duplicate REQs. */
1356 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1359 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1363 spin_lock_irq(&cm_id_priv
->lock
);
1364 switch (cm_id_priv
->id
.state
) {
1365 case IB_CM_MRA_REQ_SENT
:
1366 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1367 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1368 cm_id_priv
->private_data
,
1369 cm_id_priv
->private_data_len
);
1371 case IB_CM_TIMEWAIT
:
1372 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1373 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1378 spin_unlock_irq(&cm_id_priv
->lock
);
1380 ret
= ib_post_send_mad(msg
, NULL
);
1385 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1386 free
: cm_free_msg(msg
);
1389 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1390 struct cm_id_private
*cm_id_priv
)
1392 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1393 struct cm_timewait_info
*timewait_info
;
1394 struct cm_req_msg
*req_msg
;
1396 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1398 /* Check for possible duplicate REQ. */
1399 spin_lock_irq(&cm
.lock
);
1400 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1401 if (timewait_info
) {
1402 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1403 timewait_info
->work
.remote_id
);
1404 spin_unlock_irq(&cm
.lock
);
1405 if (cur_cm_id_priv
) {
1406 cm_dup_req_handler(work
, cur_cm_id_priv
);
1407 cm_deref_id(cur_cm_id_priv
);
1412 /* Check for stale connections. */
1413 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1414 if (timewait_info
) {
1415 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1416 spin_unlock_irq(&cm
.lock
);
1417 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1418 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1423 /* Find matching listen request. */
1424 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1425 req_msg
->service_id
,
1426 req_msg
->private_data
);
1427 if (!listen_cm_id_priv
) {
1428 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1429 spin_unlock_irq(&cm
.lock
);
1430 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1431 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1435 atomic_inc(&listen_cm_id_priv
->refcount
);
1436 atomic_inc(&cm_id_priv
->refcount
);
1437 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1438 atomic_inc(&cm_id_priv
->work_count
);
1439 spin_unlock_irq(&cm
.lock
);
1441 return listen_cm_id_priv
;
1444 static int cm_req_handler(struct cm_work
*work
)
1446 struct ib_cm_id
*cm_id
;
1447 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1448 struct cm_req_msg
*req_msg
;
1451 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1453 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
1455 return PTR_ERR(cm_id
);
1457 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1458 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1459 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1460 work
->mad_recv_wc
->recv_buf
.grh
,
1462 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1464 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1465 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1468 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1469 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1470 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1472 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1473 if (!listen_cm_id_priv
) {
1475 kfree(cm_id_priv
->timewait_info
);
1479 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1480 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1481 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1482 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
1484 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1485 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1487 ib_get_cached_gid(work
->port
->cm_dev
->device
,
1488 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1489 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1490 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1494 if (req_msg
->alt_local_lid
) {
1495 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1497 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1498 &work
->path
[0].sgid
,
1499 sizeof work
->path
[0].sgid
, NULL
, 0);
1503 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1504 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1505 cm_req_get_local_resp_timeout(req_msg
));
1506 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1507 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1508 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1509 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1510 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1511 cm_id_priv
->pkey
= req_msg
->pkey
;
1512 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1513 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1514 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1515 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1517 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1518 cm_process_work(cm_id_priv
, work
);
1519 cm_deref_id(listen_cm_id_priv
);
1523 atomic_dec(&cm_id_priv
->refcount
);
1524 cm_deref_id(listen_cm_id_priv
);
1526 ib_destroy_cm_id(cm_id
);
1530 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1531 struct cm_id_private
*cm_id_priv
,
1532 struct ib_cm_rep_param
*param
)
1534 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1535 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1536 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1537 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1538 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1539 rep_msg
->resp_resources
= param
->responder_resources
;
1540 rep_msg
->initiator_depth
= param
->initiator_depth
;
1541 cm_rep_set_target_ack_delay(rep_msg
,
1542 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1543 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1544 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1545 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1546 cm_rep_set_srq(rep_msg
, param
->srq
);
1547 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1549 if (param
->private_data
&& param
->private_data_len
)
1550 memcpy(rep_msg
->private_data
, param
->private_data
,
1551 param
->private_data_len
);
1554 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1555 struct ib_cm_rep_param
*param
)
1557 struct cm_id_private
*cm_id_priv
;
1558 struct ib_mad_send_buf
*msg
;
1559 struct cm_rep_msg
*rep_msg
;
1560 unsigned long flags
;
1563 if (param
->private_data
&&
1564 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1567 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1568 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1569 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1570 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1575 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1579 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1580 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1581 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1582 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1584 ret
= ib_post_send_mad(msg
, NULL
);
1586 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1591 cm_id
->state
= IB_CM_REP_SENT
;
1592 cm_id_priv
->msg
= msg
;
1593 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1594 cm_id_priv
->responder_resources
= param
->responder_resources
;
1595 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1596 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1598 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1601 EXPORT_SYMBOL(ib_send_cm_rep
);
1603 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1604 struct cm_id_private
*cm_id_priv
,
1605 const void *private_data
,
1606 u8 private_data_len
)
1608 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1609 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1610 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1612 if (private_data
&& private_data_len
)
1613 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1616 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1617 const void *private_data
,
1618 u8 private_data_len
)
1620 struct cm_id_private
*cm_id_priv
;
1621 struct ib_mad_send_buf
*msg
;
1622 unsigned long flags
;
1626 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1629 data
= cm_copy_private_data(private_data
, private_data_len
);
1631 return PTR_ERR(data
);
1633 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1634 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1635 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1636 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1641 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1645 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1646 private_data
, private_data_len
);
1648 ret
= ib_post_send_mad(msg
, NULL
);
1650 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1656 cm_id
->state
= IB_CM_ESTABLISHED
;
1657 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1658 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1661 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1665 EXPORT_SYMBOL(ib_send_cm_rtu
);
1667 static void cm_format_rep_event(struct cm_work
*work
)
1669 struct cm_rep_msg
*rep_msg
;
1670 struct ib_cm_rep_event_param
*param
;
1672 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1673 param
= &work
->cm_event
.param
.rep_rcvd
;
1674 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1675 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1676 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1677 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1678 param
->responder_resources
= rep_msg
->initiator_depth
;
1679 param
->initiator_depth
= rep_msg
->resp_resources
;
1680 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1681 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1682 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1683 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1684 param
->srq
= cm_rep_get_srq(rep_msg
);
1685 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1688 static void cm_dup_rep_handler(struct cm_work
*work
)
1690 struct cm_id_private
*cm_id_priv
;
1691 struct cm_rep_msg
*rep_msg
;
1692 struct ib_mad_send_buf
*msg
= NULL
;
1695 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1696 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1697 rep_msg
->local_comm_id
);
1701 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1702 counter
[CM_REP_COUNTER
]);
1703 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1707 spin_lock_irq(&cm_id_priv
->lock
);
1708 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1709 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1710 cm_id_priv
->private_data
,
1711 cm_id_priv
->private_data_len
);
1712 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1713 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1714 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1715 cm_id_priv
->private_data
,
1716 cm_id_priv
->private_data_len
);
1719 spin_unlock_irq(&cm_id_priv
->lock
);
1721 ret
= ib_post_send_mad(msg
, NULL
);
1726 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1727 free
: cm_free_msg(msg
);
1728 deref
: cm_deref_id(cm_id_priv
);
1731 static int cm_rep_handler(struct cm_work
*work
)
1733 struct cm_id_private
*cm_id_priv
;
1734 struct cm_rep_msg
*rep_msg
;
1737 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1738 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1740 cm_dup_rep_handler(work
);
1744 cm_format_rep_event(work
);
1746 spin_lock_irq(&cm_id_priv
->lock
);
1747 switch (cm_id_priv
->id
.state
) {
1748 case IB_CM_REQ_SENT
:
1749 case IB_CM_MRA_REQ_RCVD
:
1752 spin_unlock_irq(&cm_id_priv
->lock
);
1757 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1758 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1759 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1761 spin_lock(&cm
.lock
);
1762 /* Check for duplicate REP. */
1763 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1764 spin_unlock(&cm
.lock
);
1765 spin_unlock_irq(&cm_id_priv
->lock
);
1769 /* Check for a stale connection. */
1770 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1771 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1772 &cm
.remote_id_table
);
1773 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1774 spin_unlock(&cm
.lock
);
1775 spin_unlock_irq(&cm_id_priv
->lock
);
1776 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1777 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1782 spin_unlock(&cm
.lock
);
1784 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1785 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1786 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1787 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1788 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1789 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1790 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1791 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1792 cm_id_priv
->av
.timeout
=
1793 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1794 cm_id_priv
->av
.timeout
- 1);
1795 cm_id_priv
->alt_av
.timeout
=
1796 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1797 cm_id_priv
->alt_av
.timeout
- 1);
1799 /* todo: handle peer_to_peer */
1801 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1802 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1804 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1805 spin_unlock_irq(&cm_id_priv
->lock
);
1808 cm_process_work(cm_id_priv
, work
);
1810 cm_deref_id(cm_id_priv
);
1814 cm_deref_id(cm_id_priv
);
1818 static int cm_establish_handler(struct cm_work
*work
)
1820 struct cm_id_private
*cm_id_priv
;
1823 /* See comment in cm_establish about lookup. */
1824 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1828 spin_lock_irq(&cm_id_priv
->lock
);
1829 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1830 spin_unlock_irq(&cm_id_priv
->lock
);
1834 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1835 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1837 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1838 spin_unlock_irq(&cm_id_priv
->lock
);
1841 cm_process_work(cm_id_priv
, work
);
1843 cm_deref_id(cm_id_priv
);
1846 cm_deref_id(cm_id_priv
);
1850 static int cm_rtu_handler(struct cm_work
*work
)
1852 struct cm_id_private
*cm_id_priv
;
1853 struct cm_rtu_msg
*rtu_msg
;
1856 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1857 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1858 rtu_msg
->local_comm_id
);
1862 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1864 spin_lock_irq(&cm_id_priv
->lock
);
1865 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1866 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1867 spin_unlock_irq(&cm_id_priv
->lock
);
1868 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1869 counter
[CM_RTU_COUNTER
]);
1872 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1874 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1875 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1877 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1878 spin_unlock_irq(&cm_id_priv
->lock
);
1881 cm_process_work(cm_id_priv
, work
);
1883 cm_deref_id(cm_id_priv
);
1886 cm_deref_id(cm_id_priv
);
1890 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1891 struct cm_id_private
*cm_id_priv
,
1892 const void *private_data
,
1893 u8 private_data_len
)
1895 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1896 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1897 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1898 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1899 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1901 if (private_data
&& private_data_len
)
1902 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1905 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1906 const void *private_data
,
1907 u8 private_data_len
)
1909 struct cm_id_private
*cm_id_priv
;
1910 struct ib_mad_send_buf
*msg
;
1911 unsigned long flags
;
1914 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1917 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1918 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1919 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1924 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1926 cm_enter_timewait(cm_id_priv
);
1930 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
1931 private_data
, private_data_len
);
1932 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1933 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
1935 ret
= ib_post_send_mad(msg
, NULL
);
1937 cm_enter_timewait(cm_id_priv
);
1938 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1943 cm_id
->state
= IB_CM_DREQ_SENT
;
1944 cm_id_priv
->msg
= msg
;
1945 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1948 EXPORT_SYMBOL(ib_send_cm_dreq
);
1950 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
1951 struct cm_id_private
*cm_id_priv
,
1952 const void *private_data
,
1953 u8 private_data_len
)
1955 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
1956 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1957 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1959 if (private_data
&& private_data_len
)
1960 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
1963 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
1964 const void *private_data
,
1965 u8 private_data_len
)
1967 struct cm_id_private
*cm_id_priv
;
1968 struct ib_mad_send_buf
*msg
;
1969 unsigned long flags
;
1973 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
1976 data
= cm_copy_private_data(private_data
, private_data_len
);
1978 return PTR_ERR(data
);
1980 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1981 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1982 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
1983 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1988 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1989 cm_enter_timewait(cm_id_priv
);
1991 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1995 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1996 private_data
, private_data_len
);
1998 ret
= ib_post_send_mad(msg
, NULL
);
2000 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2005 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2008 EXPORT_SYMBOL(ib_send_cm_drep
);
2010 static int cm_issue_drep(struct cm_port
*port
,
2011 struct ib_mad_recv_wc
*mad_recv_wc
)
2013 struct ib_mad_send_buf
*msg
= NULL
;
2014 struct cm_dreq_msg
*dreq_msg
;
2015 struct cm_drep_msg
*drep_msg
;
2018 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2022 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2023 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2025 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2026 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2027 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2029 ret
= ib_post_send_mad(msg
, NULL
);
2036 static int cm_dreq_handler(struct cm_work
*work
)
2038 struct cm_id_private
*cm_id_priv
;
2039 struct cm_dreq_msg
*dreq_msg
;
2040 struct ib_mad_send_buf
*msg
= NULL
;
2043 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2044 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2045 dreq_msg
->local_comm_id
);
2047 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2048 counter
[CM_DREQ_COUNTER
]);
2049 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2053 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2055 spin_lock_irq(&cm_id_priv
->lock
);
2056 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2059 switch (cm_id_priv
->id
.state
) {
2060 case IB_CM_REP_SENT
:
2061 case IB_CM_DREQ_SENT
:
2062 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2064 case IB_CM_ESTABLISHED
:
2065 case IB_CM_MRA_REP_RCVD
:
2067 case IB_CM_TIMEWAIT
:
2068 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2069 counter
[CM_DREQ_COUNTER
]);
2070 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2073 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2074 cm_id_priv
->private_data
,
2075 cm_id_priv
->private_data_len
);
2076 spin_unlock_irq(&cm_id_priv
->lock
);
2078 if (ib_post_send_mad(msg
, NULL
))
2081 case IB_CM_DREQ_RCVD
:
2082 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2083 counter
[CM_DREQ_COUNTER
]);
2088 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2089 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2090 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2092 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2093 spin_unlock_irq(&cm_id_priv
->lock
);
2096 cm_process_work(cm_id_priv
, work
);
2098 cm_deref_id(cm_id_priv
);
2101 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2102 deref
: cm_deref_id(cm_id_priv
);
2106 static int cm_drep_handler(struct cm_work
*work
)
2108 struct cm_id_private
*cm_id_priv
;
2109 struct cm_drep_msg
*drep_msg
;
2112 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2113 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2114 drep_msg
->local_comm_id
);
2118 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2120 spin_lock_irq(&cm_id_priv
->lock
);
2121 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2122 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2123 spin_unlock_irq(&cm_id_priv
->lock
);
2126 cm_enter_timewait(cm_id_priv
);
2128 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2129 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2131 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2132 spin_unlock_irq(&cm_id_priv
->lock
);
2135 cm_process_work(cm_id_priv
, work
);
2137 cm_deref_id(cm_id_priv
);
2140 cm_deref_id(cm_id_priv
);
2144 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2145 enum ib_cm_rej_reason reason
,
2148 const void *private_data
,
2149 u8 private_data_len
)
2151 struct cm_id_private
*cm_id_priv
;
2152 struct ib_mad_send_buf
*msg
;
2153 unsigned long flags
;
2156 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2157 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2160 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2162 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2163 switch (cm_id
->state
) {
2164 case IB_CM_REQ_SENT
:
2165 case IB_CM_MRA_REQ_RCVD
:
2166 case IB_CM_REQ_RCVD
:
2167 case IB_CM_MRA_REQ_SENT
:
2168 case IB_CM_REP_RCVD
:
2169 case IB_CM_MRA_REP_SENT
:
2170 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2172 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2173 cm_id_priv
, reason
, ari
, ari_length
,
2174 private_data
, private_data_len
);
2176 cm_reset_to_idle(cm_id_priv
);
2178 case IB_CM_REP_SENT
:
2179 case IB_CM_MRA_REP_RCVD
:
2180 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2182 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2183 cm_id_priv
, reason
, ari
, ari_length
,
2184 private_data
, private_data_len
);
2186 cm_enter_timewait(cm_id_priv
);
2196 ret
= ib_post_send_mad(msg
, NULL
);
2200 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2203 EXPORT_SYMBOL(ib_send_cm_rej
);
2205 static void cm_format_rej_event(struct cm_work
*work
)
2207 struct cm_rej_msg
*rej_msg
;
2208 struct ib_cm_rej_event_param
*param
;
2210 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2211 param
= &work
->cm_event
.param
.rej_rcvd
;
2212 param
->ari
= rej_msg
->ari
;
2213 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2214 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2215 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2218 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2220 struct cm_timewait_info
*timewait_info
;
2221 struct cm_id_private
*cm_id_priv
;
2224 remote_id
= rej_msg
->local_comm_id
;
2226 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2227 spin_lock_irq(&cm
.lock
);
2228 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2230 if (!timewait_info
) {
2231 spin_unlock_irq(&cm
.lock
);
2234 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2235 (timewait_info
->work
.local_id
^
2236 cm
.random_id_operand
));
2238 if (cm_id_priv
->id
.remote_id
== remote_id
)
2239 atomic_inc(&cm_id_priv
->refcount
);
2243 spin_unlock_irq(&cm
.lock
);
2244 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2245 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2247 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2252 static int cm_rej_handler(struct cm_work
*work
)
2254 struct cm_id_private
*cm_id_priv
;
2255 struct cm_rej_msg
*rej_msg
;
2258 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2259 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2263 cm_format_rej_event(work
);
2265 spin_lock_irq(&cm_id_priv
->lock
);
2266 switch (cm_id_priv
->id
.state
) {
2267 case IB_CM_REQ_SENT
:
2268 case IB_CM_MRA_REQ_RCVD
:
2269 case IB_CM_REP_SENT
:
2270 case IB_CM_MRA_REP_RCVD
:
2271 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2273 case IB_CM_REQ_RCVD
:
2274 case IB_CM_MRA_REQ_SENT
:
2275 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2276 cm_enter_timewait(cm_id_priv
);
2278 cm_reset_to_idle(cm_id_priv
);
2280 case IB_CM_DREQ_SENT
:
2281 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2283 case IB_CM_REP_RCVD
:
2284 case IB_CM_MRA_REP_SENT
:
2285 case IB_CM_ESTABLISHED
:
2286 cm_enter_timewait(cm_id_priv
);
2289 spin_unlock_irq(&cm_id_priv
->lock
);
2294 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2296 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2297 spin_unlock_irq(&cm_id_priv
->lock
);
2300 cm_process_work(cm_id_priv
, work
);
2302 cm_deref_id(cm_id_priv
);
2305 cm_deref_id(cm_id_priv
);
2309 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2311 const void *private_data
,
2312 u8 private_data_len
)
2314 struct cm_id_private
*cm_id_priv
;
2315 struct ib_mad_send_buf
*msg
;
2316 enum ib_cm_state cm_state
;
2317 enum ib_cm_lap_state lap_state
;
2318 enum cm_msg_response msg_response
;
2320 unsigned long flags
;
2323 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2326 data
= cm_copy_private_data(private_data
, private_data_len
);
2328 return PTR_ERR(data
);
2330 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2332 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2333 switch(cm_id_priv
->id
.state
) {
2334 case IB_CM_REQ_RCVD
:
2335 cm_state
= IB_CM_MRA_REQ_SENT
;
2336 lap_state
= cm_id
->lap_state
;
2337 msg_response
= CM_MSG_RESPONSE_REQ
;
2339 case IB_CM_REP_RCVD
:
2340 cm_state
= IB_CM_MRA_REP_SENT
;
2341 lap_state
= cm_id
->lap_state
;
2342 msg_response
= CM_MSG_RESPONSE_REP
;
2344 case IB_CM_ESTABLISHED
:
2345 cm_state
= cm_id
->state
;
2346 lap_state
= IB_CM_MRA_LAP_SENT
;
2347 msg_response
= CM_MSG_RESPONSE_OTHER
;
2354 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2355 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2359 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2360 msg_response
, service_timeout
,
2361 private_data
, private_data_len
);
2362 ret
= ib_post_send_mad(msg
, NULL
);
2367 cm_id
->state
= cm_state
;
2368 cm_id
->lap_state
= lap_state
;
2369 cm_id_priv
->service_timeout
= service_timeout
;
2370 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2371 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2374 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2378 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2383 EXPORT_SYMBOL(ib_send_cm_mra
);
2385 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2387 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2388 case CM_MSG_RESPONSE_REQ
:
2389 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2390 case CM_MSG_RESPONSE_REP
:
2391 case CM_MSG_RESPONSE_OTHER
:
2392 return cm_acquire_id(mra_msg
->remote_comm_id
,
2393 mra_msg
->local_comm_id
);
2399 static int cm_mra_handler(struct cm_work
*work
)
2401 struct cm_id_private
*cm_id_priv
;
2402 struct cm_mra_msg
*mra_msg
;
2405 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2406 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2410 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2411 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2412 cm_mra_get_service_timeout(mra_msg
);
2413 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2414 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2416 spin_lock_irq(&cm_id_priv
->lock
);
2417 switch (cm_id_priv
->id
.state
) {
2418 case IB_CM_REQ_SENT
:
2419 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2420 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2421 cm_id_priv
->msg
, timeout
))
2423 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2425 case IB_CM_REP_SENT
:
2426 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2427 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2428 cm_id_priv
->msg
, timeout
))
2430 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2432 case IB_CM_ESTABLISHED
:
2433 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2434 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2435 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2436 cm_id_priv
->msg
, timeout
)) {
2437 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2438 atomic_long_inc(&work
->port
->
2439 counter_group
[CM_RECV_DUPLICATES
].
2440 counter
[CM_MRA_COUNTER
]);
2443 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2445 case IB_CM_MRA_REQ_RCVD
:
2446 case IB_CM_MRA_REP_RCVD
:
2447 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2448 counter
[CM_MRA_COUNTER
]);
2454 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2455 cm_id_priv
->id
.state
;
2456 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2458 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2459 spin_unlock_irq(&cm_id_priv
->lock
);
2462 cm_process_work(cm_id_priv
, work
);
2464 cm_deref_id(cm_id_priv
);
2467 spin_unlock_irq(&cm_id_priv
->lock
);
2468 cm_deref_id(cm_id_priv
);
2472 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2473 struct cm_id_private
*cm_id_priv
,
2474 struct ib_sa_path_rec
*alternate_path
,
2475 const void *private_data
,
2476 u8 private_data_len
)
2478 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2479 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2480 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2481 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2482 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2483 /* todo: need remote CM response timeout */
2484 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2485 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2486 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2487 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2488 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2489 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2490 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2491 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2492 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2493 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2494 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2495 cm_lap_set_local_ack_timeout(lap_msg
,
2496 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2497 alternate_path
->packet_life_time
));
2499 if (private_data
&& private_data_len
)
2500 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2503 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2504 struct ib_sa_path_rec
*alternate_path
,
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_LAP_PRIVATE_DATA_SIZE
)
2516 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2517 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2518 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2519 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2520 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2525 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2528 cm_id_priv
->alt_av
.timeout
=
2529 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2530 cm_id_priv
->alt_av
.timeout
- 1);
2532 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2536 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2537 alternate_path
, private_data
, private_data_len
);
2538 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2539 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2541 ret
= ib_post_send_mad(msg
, NULL
);
2543 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2548 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2549 cm_id_priv
->msg
= msg
;
2551 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2554 EXPORT_SYMBOL(ib_send_cm_lap
);
2556 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2557 struct ib_sa_path_rec
*path
,
2558 struct cm_lap_msg
*lap_msg
)
2560 memset(path
, 0, sizeof *path
);
2561 path
->dgid
= lap_msg
->alt_local_gid
;
2562 path
->sgid
= lap_msg
->alt_remote_gid
;
2563 path
->dlid
= lap_msg
->alt_local_lid
;
2564 path
->slid
= lap_msg
->alt_remote_lid
;
2565 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2566 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2567 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2568 path
->reversible
= 1;
2569 path
->pkey
= cm_id_priv
->pkey
;
2570 path
->sl
= cm_lap_get_sl(lap_msg
);
2571 path
->mtu_selector
= IB_SA_EQ
;
2572 path
->mtu
= cm_id_priv
->path_mtu
;
2573 path
->rate_selector
= IB_SA_EQ
;
2574 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2575 path
->packet_life_time_selector
= IB_SA_EQ
;
2576 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2577 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2580 static int cm_lap_handler(struct cm_work
*work
)
2582 struct cm_id_private
*cm_id_priv
;
2583 struct cm_lap_msg
*lap_msg
;
2584 struct ib_cm_lap_event_param
*param
;
2585 struct ib_mad_send_buf
*msg
= NULL
;
2588 /* todo: verify LAP request and send reject APR if invalid. */
2589 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2590 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2591 lap_msg
->local_comm_id
);
2595 param
= &work
->cm_event
.param
.lap_rcvd
;
2596 param
->alternate_path
= &work
->path
[0];
2597 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2598 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2600 spin_lock_irq(&cm_id_priv
->lock
);
2601 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2604 switch (cm_id_priv
->id
.lap_state
) {
2605 case IB_CM_LAP_UNINIT
:
2606 case IB_CM_LAP_IDLE
:
2608 case IB_CM_MRA_LAP_SENT
:
2609 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2610 counter
[CM_LAP_COUNTER
]);
2611 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2614 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2615 CM_MSG_RESPONSE_OTHER
,
2616 cm_id_priv
->service_timeout
,
2617 cm_id_priv
->private_data
,
2618 cm_id_priv
->private_data_len
);
2619 spin_unlock_irq(&cm_id_priv
->lock
);
2621 if (ib_post_send_mad(msg
, NULL
))
2624 case IB_CM_LAP_RCVD
:
2625 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2626 counter
[CM_LAP_COUNTER
]);
2632 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2633 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2634 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2635 work
->mad_recv_wc
->recv_buf
.grh
,
2637 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2638 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2640 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2641 spin_unlock_irq(&cm_id_priv
->lock
);
2644 cm_process_work(cm_id_priv
, work
);
2646 cm_deref_id(cm_id_priv
);
2649 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2650 deref
: cm_deref_id(cm_id_priv
);
2654 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2655 struct cm_id_private
*cm_id_priv
,
2656 enum ib_cm_apr_status status
,
2659 const void *private_data
,
2660 u8 private_data_len
)
2662 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2663 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2664 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2665 apr_msg
->ap_status
= (u8
) status
;
2667 if (info
&& info_length
) {
2668 apr_msg
->info_length
= info_length
;
2669 memcpy(apr_msg
->info
, info
, info_length
);
2672 if (private_data
&& private_data_len
)
2673 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2676 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2677 enum ib_cm_apr_status status
,
2680 const void *private_data
,
2681 u8 private_data_len
)
2683 struct cm_id_private
*cm_id_priv
;
2684 struct ib_mad_send_buf
*msg
;
2685 unsigned long flags
;
2688 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2689 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2692 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2693 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2694 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2695 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2696 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2701 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2705 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2706 info
, info_length
, private_data
, private_data_len
);
2707 ret
= ib_post_send_mad(msg
, NULL
);
2709 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2714 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2715 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2718 EXPORT_SYMBOL(ib_send_cm_apr
);
2720 static int cm_apr_handler(struct cm_work
*work
)
2722 struct cm_id_private
*cm_id_priv
;
2723 struct cm_apr_msg
*apr_msg
;
2726 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2727 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2728 apr_msg
->local_comm_id
);
2730 return -EINVAL
; /* Unmatched reply. */
2732 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2733 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2734 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2735 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2737 spin_lock_irq(&cm_id_priv
->lock
);
2738 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2739 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2740 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2741 spin_unlock_irq(&cm_id_priv
->lock
);
2744 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2745 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2746 cm_id_priv
->msg
= NULL
;
2748 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2750 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2751 spin_unlock_irq(&cm_id_priv
->lock
);
2754 cm_process_work(cm_id_priv
, work
);
2756 cm_deref_id(cm_id_priv
);
2759 cm_deref_id(cm_id_priv
);
2763 static int cm_timewait_handler(struct cm_work
*work
)
2765 struct cm_timewait_info
*timewait_info
;
2766 struct cm_id_private
*cm_id_priv
;
2769 timewait_info
= (struct cm_timewait_info
*)work
;
2770 spin_lock_irq(&cm
.lock
);
2771 list_del(&timewait_info
->list
);
2772 spin_unlock_irq(&cm
.lock
);
2774 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2775 timewait_info
->work
.remote_id
);
2779 spin_lock_irq(&cm_id_priv
->lock
);
2780 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2781 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2782 spin_unlock_irq(&cm_id_priv
->lock
);
2785 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2786 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2788 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2789 spin_unlock_irq(&cm_id_priv
->lock
);
2792 cm_process_work(cm_id_priv
, work
);
2794 cm_deref_id(cm_id_priv
);
2797 cm_deref_id(cm_id_priv
);
2801 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2802 struct cm_id_private
*cm_id_priv
,
2803 struct ib_cm_sidr_req_param
*param
)
2805 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2806 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2807 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2808 sidr_req_msg
->pkey
= cpu_to_be16(param
->path
->pkey
);
2809 sidr_req_msg
->service_id
= param
->service_id
;
2811 if (param
->private_data
&& param
->private_data_len
)
2812 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2813 param
->private_data_len
);
2816 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2817 struct ib_cm_sidr_req_param
*param
)
2819 struct cm_id_private
*cm_id_priv
;
2820 struct ib_mad_send_buf
*msg
;
2821 unsigned long flags
;
2824 if (!param
->path
|| (param
->private_data
&&
2825 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2828 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2829 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2833 cm_id
->service_id
= param
->service_id
;
2834 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
2835 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2836 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2837 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2841 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2843 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2844 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2846 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2847 if (cm_id
->state
== IB_CM_IDLE
)
2848 ret
= ib_post_send_mad(msg
, NULL
);
2853 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2857 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2858 cm_id_priv
->msg
= msg
;
2859 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2863 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2865 static void cm_format_sidr_req_event(struct cm_work
*work
,
2866 struct ib_cm_id
*listen_id
)
2868 struct cm_sidr_req_msg
*sidr_req_msg
;
2869 struct ib_cm_sidr_req_event_param
*param
;
2871 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2872 work
->mad_recv_wc
->recv_buf
.mad
;
2873 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2874 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2875 param
->listen_id
= listen_id
;
2876 param
->port
= work
->port
->port_num
;
2877 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2880 static int cm_sidr_req_handler(struct cm_work
*work
)
2882 struct ib_cm_id
*cm_id
;
2883 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2884 struct cm_sidr_req_msg
*sidr_req_msg
;
2887 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
2889 return PTR_ERR(cm_id
);
2890 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2892 /* Record SGID/SLID and request ID for lookup. */
2893 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2894 work
->mad_recv_wc
->recv_buf
.mad
;
2895 wc
= work
->mad_recv_wc
->wc
;
2896 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2897 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2898 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2899 work
->mad_recv_wc
->recv_buf
.grh
,
2901 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2902 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2903 atomic_inc(&cm_id_priv
->work_count
);
2905 spin_lock_irq(&cm
.lock
);
2906 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2907 if (cur_cm_id_priv
) {
2908 spin_unlock_irq(&cm
.lock
);
2909 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2910 counter
[CM_SIDR_REQ_COUNTER
]);
2911 goto out
; /* Duplicate message. */
2913 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
2914 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
2915 sidr_req_msg
->service_id
,
2916 sidr_req_msg
->private_data
);
2917 if (!cur_cm_id_priv
) {
2918 spin_unlock_irq(&cm
.lock
);
2919 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
2920 goto out
; /* No match. */
2922 atomic_inc(&cur_cm_id_priv
->refcount
);
2923 spin_unlock_irq(&cm
.lock
);
2925 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
2926 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
2927 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
2928 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
2930 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
2931 cm_process_work(cm_id_priv
, work
);
2932 cm_deref_id(cur_cm_id_priv
);
2935 ib_destroy_cm_id(&cm_id_priv
->id
);
2939 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
2940 struct cm_id_private
*cm_id_priv
,
2941 struct ib_cm_sidr_rep_param
*param
)
2943 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
2945 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
2946 sidr_rep_msg
->status
= param
->status
;
2947 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
2948 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
2949 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
2951 if (param
->info
&& param
->info_length
)
2952 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
2954 if (param
->private_data
&& param
->private_data_len
)
2955 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
2956 param
->private_data_len
);
2959 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
2960 struct ib_cm_sidr_rep_param
*param
)
2962 struct cm_id_private
*cm_id_priv
;
2963 struct ib_mad_send_buf
*msg
;
2964 unsigned long flags
;
2967 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
2968 (param
->private_data
&&
2969 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
2972 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2973 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2974 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
2979 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2983 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
2985 ret
= ib_post_send_mad(msg
, NULL
);
2987 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2991 cm_id
->state
= IB_CM_IDLE
;
2992 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2994 spin_lock_irqsave(&cm
.lock
, flags
);
2995 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2996 spin_unlock_irqrestore(&cm
.lock
, flags
);
2999 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3002 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3004 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3006 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3007 struct ib_cm_sidr_rep_event_param
*param
;
3009 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3010 work
->mad_recv_wc
->recv_buf
.mad
;
3011 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3012 param
->status
= sidr_rep_msg
->status
;
3013 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3014 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3015 param
->info
= &sidr_rep_msg
->info
;
3016 param
->info_len
= sidr_rep_msg
->info_length
;
3017 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3020 static int cm_sidr_rep_handler(struct cm_work
*work
)
3022 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3023 struct cm_id_private
*cm_id_priv
;
3025 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3026 work
->mad_recv_wc
->recv_buf
.mad
;
3027 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3029 return -EINVAL
; /* Unmatched reply. */
3031 spin_lock_irq(&cm_id_priv
->lock
);
3032 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3033 spin_unlock_irq(&cm_id_priv
->lock
);
3036 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3037 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3038 spin_unlock_irq(&cm_id_priv
->lock
);
3040 cm_format_sidr_rep_event(work
);
3041 cm_process_work(cm_id_priv
, work
);
3044 cm_deref_id(cm_id_priv
);
3048 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3049 enum ib_wc_status wc_status
)
3051 struct cm_id_private
*cm_id_priv
;
3052 struct ib_cm_event cm_event
;
3053 enum ib_cm_state state
;
3056 memset(&cm_event
, 0, sizeof cm_event
);
3057 cm_id_priv
= msg
->context
[0];
3059 /* Discard old sends or ones without a response. */
3060 spin_lock_irq(&cm_id_priv
->lock
);
3061 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3062 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3066 case IB_CM_REQ_SENT
:
3067 case IB_CM_MRA_REQ_RCVD
:
3068 cm_reset_to_idle(cm_id_priv
);
3069 cm_event
.event
= IB_CM_REQ_ERROR
;
3071 case IB_CM_REP_SENT
:
3072 case IB_CM_MRA_REP_RCVD
:
3073 cm_reset_to_idle(cm_id_priv
);
3074 cm_event
.event
= IB_CM_REP_ERROR
;
3076 case IB_CM_DREQ_SENT
:
3077 cm_enter_timewait(cm_id_priv
);
3078 cm_event
.event
= IB_CM_DREQ_ERROR
;
3080 case IB_CM_SIDR_REQ_SENT
:
3081 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3082 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3087 spin_unlock_irq(&cm_id_priv
->lock
);
3088 cm_event
.param
.send_status
= wc_status
;
3090 /* No other events can occur on the cm_id at this point. */
3091 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3094 ib_destroy_cm_id(&cm_id_priv
->id
);
3097 spin_unlock_irq(&cm_id_priv
->lock
);
3101 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3102 struct ib_mad_send_wc
*mad_send_wc
)
3104 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3105 struct cm_port
*port
;
3108 port
= mad_agent
->context
;
3109 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3110 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3113 * If the send was in response to a received message (context[0] is not
3114 * set to a cm_id), and is not a REJ, then it is a send that was
3117 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3120 atomic_long_add(1 + msg
->retries
,
3121 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3123 atomic_long_add(msg
->retries
,
3124 &port
->counter_group
[CM_XMIT_RETRIES
].
3125 counter
[attr_index
]);
3127 switch (mad_send_wc
->status
) {
3129 case IB_WC_WR_FLUSH_ERR
:
3133 if (msg
->context
[0] && msg
->context
[1])
3134 cm_process_send_error(msg
, mad_send_wc
->status
);
3141 static void cm_work_handler(struct work_struct
*_work
)
3143 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3146 switch (work
->cm_event
.event
) {
3147 case IB_CM_REQ_RECEIVED
:
3148 ret
= cm_req_handler(work
);
3150 case IB_CM_MRA_RECEIVED
:
3151 ret
= cm_mra_handler(work
);
3153 case IB_CM_REJ_RECEIVED
:
3154 ret
= cm_rej_handler(work
);
3156 case IB_CM_REP_RECEIVED
:
3157 ret
= cm_rep_handler(work
);
3159 case IB_CM_RTU_RECEIVED
:
3160 ret
= cm_rtu_handler(work
);
3162 case IB_CM_USER_ESTABLISHED
:
3163 ret
= cm_establish_handler(work
);
3165 case IB_CM_DREQ_RECEIVED
:
3166 ret
= cm_dreq_handler(work
);
3168 case IB_CM_DREP_RECEIVED
:
3169 ret
= cm_drep_handler(work
);
3171 case IB_CM_SIDR_REQ_RECEIVED
:
3172 ret
= cm_sidr_req_handler(work
);
3174 case IB_CM_SIDR_REP_RECEIVED
:
3175 ret
= cm_sidr_rep_handler(work
);
3177 case IB_CM_LAP_RECEIVED
:
3178 ret
= cm_lap_handler(work
);
3180 case IB_CM_APR_RECEIVED
:
3181 ret
= cm_apr_handler(work
);
3183 case IB_CM_TIMEWAIT_EXIT
:
3184 ret
= cm_timewait_handler(work
);
3194 static int cm_establish(struct ib_cm_id
*cm_id
)
3196 struct cm_id_private
*cm_id_priv
;
3197 struct cm_work
*work
;
3198 unsigned long flags
;
3201 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3205 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3206 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3207 switch (cm_id
->state
)
3209 case IB_CM_REP_SENT
:
3210 case IB_CM_MRA_REP_RCVD
:
3211 cm_id
->state
= IB_CM_ESTABLISHED
;
3213 case IB_CM_ESTABLISHED
:
3220 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3228 * The CM worker thread may try to destroy the cm_id before it
3229 * can execute this work item. To prevent potential deadlock,
3230 * we need to find the cm_id once we're in the context of the
3231 * worker thread, rather than holding a reference on it.
3233 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3234 work
->local_id
= cm_id
->local_id
;
3235 work
->remote_id
= cm_id
->remote_id
;
3236 work
->mad_recv_wc
= NULL
;
3237 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3238 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3243 static int cm_migrate(struct ib_cm_id
*cm_id
)
3245 struct cm_id_private
*cm_id_priv
;
3246 unsigned long flags
;
3249 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3250 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3251 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3252 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3253 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3254 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3255 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3258 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3263 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3268 case IB_EVENT_COMM_EST
:
3269 ret
= cm_establish(cm_id
);
3271 case IB_EVENT_PATH_MIG
:
3272 ret
= cm_migrate(cm_id
);
3279 EXPORT_SYMBOL(ib_cm_notify
);
3281 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3282 struct ib_mad_recv_wc
*mad_recv_wc
)
3284 struct cm_port
*port
= mad_agent
->context
;
3285 struct cm_work
*work
;
3286 enum ib_cm_event_type event
;
3290 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3291 case CM_REQ_ATTR_ID
:
3292 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3293 alt_local_lid
!= 0);
3294 event
= IB_CM_REQ_RECEIVED
;
3296 case CM_MRA_ATTR_ID
:
3297 event
= IB_CM_MRA_RECEIVED
;
3299 case CM_REJ_ATTR_ID
:
3300 event
= IB_CM_REJ_RECEIVED
;
3302 case CM_REP_ATTR_ID
:
3303 event
= IB_CM_REP_RECEIVED
;
3305 case CM_RTU_ATTR_ID
:
3306 event
= IB_CM_RTU_RECEIVED
;
3308 case CM_DREQ_ATTR_ID
:
3309 event
= IB_CM_DREQ_RECEIVED
;
3311 case CM_DREP_ATTR_ID
:
3312 event
= IB_CM_DREP_RECEIVED
;
3314 case CM_SIDR_REQ_ATTR_ID
:
3315 event
= IB_CM_SIDR_REQ_RECEIVED
;
3317 case CM_SIDR_REP_ATTR_ID
:
3318 event
= IB_CM_SIDR_REP_RECEIVED
;
3320 case CM_LAP_ATTR_ID
:
3322 event
= IB_CM_LAP_RECEIVED
;
3324 case CM_APR_ATTR_ID
:
3325 event
= IB_CM_APR_RECEIVED
;
3328 ib_free_recv_mad(mad_recv_wc
);
3332 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3333 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3334 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3336 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3339 ib_free_recv_mad(mad_recv_wc
);
3343 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3344 work
->cm_event
.event
= event
;
3345 work
->mad_recv_wc
= mad_recv_wc
;
3347 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3350 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3351 struct ib_qp_attr
*qp_attr
,
3354 unsigned long flags
;
3357 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3358 switch (cm_id_priv
->id
.state
) {
3359 case IB_CM_REQ_SENT
:
3360 case IB_CM_MRA_REQ_RCVD
:
3361 case IB_CM_REQ_RCVD
:
3362 case IB_CM_MRA_REQ_SENT
:
3363 case IB_CM_REP_RCVD
:
3364 case IB_CM_MRA_REP_SENT
:
3365 case IB_CM_REP_SENT
:
3366 case IB_CM_MRA_REP_RCVD
:
3367 case IB_CM_ESTABLISHED
:
3368 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3369 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3370 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3371 if (cm_id_priv
->responder_resources
)
3372 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3373 IB_ACCESS_REMOTE_ATOMIC
;
3374 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3375 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3382 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3386 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3387 struct ib_qp_attr
*qp_attr
,
3390 unsigned long flags
;
3393 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3394 switch (cm_id_priv
->id
.state
) {
3395 case IB_CM_REQ_RCVD
:
3396 case IB_CM_MRA_REQ_SENT
:
3397 case IB_CM_REP_RCVD
:
3398 case IB_CM_MRA_REP_SENT
:
3399 case IB_CM_REP_SENT
:
3400 case IB_CM_MRA_REP_RCVD
:
3401 case IB_CM_ESTABLISHED
:
3402 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3403 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3404 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3405 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3406 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3407 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3408 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3409 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3410 IB_QP_MIN_RNR_TIMER
;
3411 qp_attr
->max_dest_rd_atomic
=
3412 cm_id_priv
->responder_resources
;
3413 qp_attr
->min_rnr_timer
= 0;
3415 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3416 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3417 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3418 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3419 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3420 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3428 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3432 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3433 struct ib_qp_attr
*qp_attr
,
3436 unsigned long flags
;
3439 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3440 switch (cm_id_priv
->id
.state
) {
3441 /* Allow transition to RTS before sending REP */
3442 case IB_CM_REQ_RCVD
:
3443 case IB_CM_MRA_REQ_SENT
:
3445 case IB_CM_REP_RCVD
:
3446 case IB_CM_MRA_REP_SENT
:
3447 case IB_CM_REP_SENT
:
3448 case IB_CM_MRA_REP_RCVD
:
3449 case IB_CM_ESTABLISHED
:
3450 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3451 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3452 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3453 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3454 *qp_attr_mask
|= IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3456 IB_QP_MAX_QP_RD_ATOMIC
;
3457 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3458 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3459 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3460 qp_attr
->max_rd_atomic
=
3461 cm_id_priv
->initiator_depth
;
3463 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3464 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3465 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3468 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3469 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3470 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3471 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3472 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3473 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3481 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3485 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3486 struct ib_qp_attr
*qp_attr
,
3489 struct cm_id_private
*cm_id_priv
;
3492 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3493 switch (qp_attr
->qp_state
) {
3495 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3498 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3501 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3509 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3511 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3513 struct ib_device_attr attr
;
3515 if (ib_query_device(cm_dev
->device
, &attr
))
3516 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3518 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3521 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3524 struct cm_counter_group
*group
;
3525 struct cm_counter_attribute
*cm_attr
;
3527 group
= container_of(obj
, struct cm_counter_group
, obj
);
3528 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3530 return sprintf(buf
, "%ld\n",
3531 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3534 static struct sysfs_ops cm_counter_ops
= {
3535 .show
= cm_show_counter
3538 static struct kobj_type cm_counter_obj_type
= {
3539 .sysfs_ops
= &cm_counter_ops
,
3540 .default_attrs
= cm_counter_default_attrs
3543 static void cm_release_port_obj(struct kobject
*obj
)
3545 struct cm_port
*cm_port
;
3547 printk(KERN_ERR
"free cm port\n");
3549 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3553 static struct kobj_type cm_port_obj_type
= {
3554 .release
= cm_release_port_obj
3557 static void cm_release_dev_obj(struct kobject
*obj
)
3559 struct cm_device
*cm_dev
;
3561 printk(KERN_ERR
"free cm dev\n");
3563 cm_dev
= container_of(obj
, struct cm_device
, dev_obj
);
3567 static struct kobj_type cm_dev_obj_type
= {
3568 .release
= cm_release_dev_obj
3571 struct class cm_class
= {
3572 .name
= "infiniband_cm",
3574 EXPORT_SYMBOL(cm_class
);
3576 static void cm_remove_fs_obj(struct kobject
*obj
)
3578 kobject_put(obj
->parent
);
3582 static int cm_create_port_fs(struct cm_port
*port
)
3586 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3587 kobject_get(&port
->cm_dev
->dev_obj
),
3588 "%d", port
->port_num
);
3594 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3595 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3596 &cm_counter_obj_type
,
3597 kobject_get(&port
->port_obj
),
3598 "%s", counter_group_names
[i
]);
3607 cm_remove_fs_obj(&port
->counter_group
[i
].obj
);
3608 cm_remove_fs_obj(&port
->port_obj
);
3613 static void cm_remove_port_fs(struct cm_port
*port
)
3617 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3618 cm_remove_fs_obj(&port
->counter_group
[i
].obj
);
3620 cm_remove_fs_obj(&port
->port_obj
);
3623 static void cm_add_one(struct ib_device
*device
)
3625 struct cm_device
*cm_dev
;
3626 struct cm_port
*port
;
3627 struct ib_mad_reg_req reg_req
= {
3628 .mgmt_class
= IB_MGMT_CLASS_CM
,
3629 .mgmt_class_version
= IB_CM_CLASS_VERSION
3631 struct ib_port_modify port_modify
= {
3632 .set_port_cap_mask
= IB_PORT_CM_SUP
3634 unsigned long flags
;
3638 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
3641 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3642 device
->phys_port_cnt
, GFP_KERNEL
);
3646 cm_dev
->device
= device
;
3647 cm_get_ack_delay(cm_dev
);
3649 ret
= kobject_init_and_add(&cm_dev
->dev_obj
, &cm_dev_obj_type
,
3650 &cm_class
.subsys
.kobj
, "%s", device
->name
);
3656 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3657 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3658 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3662 cm_dev
->port
[i
-1] = port
;
3663 port
->cm_dev
= cm_dev
;
3666 ret
= cm_create_port_fs(port
);
3670 port
->mad_agent
= ib_register_mad_agent(device
, i
,
3677 if (IS_ERR(port
->mad_agent
))
3680 ret
= ib_modify_port(device
, i
, 0, &port_modify
);
3684 ib_set_client_data(device
, &cm_client
, cm_dev
);
3686 write_lock_irqsave(&cm
.device_lock
, flags
);
3687 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3688 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3692 ib_unregister_mad_agent(port
->mad_agent
);
3694 cm_remove_port_fs(port
);
3696 port_modify
.set_port_cap_mask
= 0;
3697 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3699 port
= cm_dev
->port
[i
-1];
3700 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3701 ib_unregister_mad_agent(port
->mad_agent
);
3702 cm_remove_port_fs(port
);
3704 cm_remove_fs_obj(&cm_dev
->dev_obj
);
3707 static void cm_remove_one(struct ib_device
*device
)
3709 struct cm_device
*cm_dev
;
3710 struct cm_port
*port
;
3711 struct ib_port_modify port_modify
= {
3712 .clr_port_cap_mask
= IB_PORT_CM_SUP
3714 unsigned long flags
;
3717 cm_dev
= ib_get_client_data(device
, &cm_client
);
3721 write_lock_irqsave(&cm
.device_lock
, flags
);
3722 list_del(&cm_dev
->list
);
3723 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3725 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3726 port
= cm_dev
->port
[i
-1];
3727 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3728 ib_unregister_mad_agent(port
->mad_agent
);
3729 cm_remove_port_fs(port
);
3731 cm_remove_fs_obj(&cm_dev
->dev_obj
);
3734 static int __init
ib_cm_init(void)
3738 memset(&cm
, 0, sizeof cm
);
3739 INIT_LIST_HEAD(&cm
.device_list
);
3740 rwlock_init(&cm
.device_lock
);
3741 spin_lock_init(&cm
.lock
);
3742 cm
.listen_service_table
= RB_ROOT
;
3743 cm
.listen_service_id
= __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3744 cm
.remote_id_table
= RB_ROOT
;
3745 cm
.remote_qp_table
= RB_ROOT
;
3746 cm
.remote_sidr_table
= RB_ROOT
;
3747 idr_init(&cm
.local_id_table
);
3748 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3749 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3750 INIT_LIST_HEAD(&cm
.timewait_list
);
3752 ret
= class_register(&cm_class
);
3756 cm
.wq
= create_workqueue("ib_cm");
3762 ret
= ib_register_client(&cm_client
);
3768 destroy_workqueue(cm
.wq
);
3770 class_unregister(&cm_class
);
3774 static void __exit
ib_cm_cleanup(void)
3776 struct cm_timewait_info
*timewait_info
, *tmp
;
3778 spin_lock_irq(&cm
.lock
);
3779 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3780 cancel_delayed_work(&timewait_info
->work
.work
);
3781 spin_unlock_irq(&cm
.lock
);
3783 destroy_workqueue(cm
.wq
);
3785 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3786 list_del(&timewait_info
->list
);
3787 kfree(timewait_info
);
3790 ib_unregister_client(&cm_client
);
3791 class_unregister(&cm_class
);
3792 idr_destroy(&cm
.local_id_table
);
3795 module_init(ib_cm_init
);
3796 module_exit(ib_cm_cleanup
);