2 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/completion.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_cm.h>
55 MODULE_AUTHOR("Sean Hefty");
56 MODULE_DESCRIPTION("InfiniBand CM");
57 MODULE_LICENSE("Dual BSD/GPL");
59 static void cm_add_one(struct ib_device
*device
);
60 static void cm_remove_one(struct ib_device
*device
);
62 static struct ib_client cm_client
= {
65 .remove
= cm_remove_one
70 struct list_head device_list
;
72 struct rb_root listen_service_table
;
73 u64 listen_service_id
;
74 /* struct rb_root peer_service_table; todo: fix peer to peer */
75 struct rb_root remote_qp_table
;
76 struct rb_root remote_id_table
;
77 struct rb_root remote_sidr_table
;
78 struct idr local_id_table
;
79 __be32 random_id_operand
;
80 struct list_head timewait_list
;
81 struct workqueue_struct
*wq
;
84 /* Counter indexes ordered by attribute ID */
98 CM_ATTR_ID_OFFSET
= 0x0010,
109 static char const counter_group_names
[CM_COUNTER_GROUPS
]
110 [sizeof("cm_rx_duplicates")] = {
111 "cm_tx_msgs", "cm_tx_retries",
112 "cm_rx_msgs", "cm_rx_duplicates"
115 struct cm_counter_group
{
117 atomic_long_t counter
[CM_ATTR_COUNT
];
120 struct cm_counter_attribute
{
121 struct attribute attr
;
125 #define CM_COUNTER_ATTR(_name, _index) \
126 struct cm_counter_attribute cm_##_name##_counter_attr = { \
127 .attr = { .name = __stringify(_name), .mode = 0444 }, \
131 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
132 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
133 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
134 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
135 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
136 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
137 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
138 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
139 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
140 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
141 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
143 static struct attribute
*cm_counter_default_attrs
[] = {
144 &cm_req_counter_attr
.attr
,
145 &cm_mra_counter_attr
.attr
,
146 &cm_rej_counter_attr
.attr
,
147 &cm_rep_counter_attr
.attr
,
148 &cm_rtu_counter_attr
.attr
,
149 &cm_dreq_counter_attr
.attr
,
150 &cm_drep_counter_attr
.attr
,
151 &cm_sidr_req_counter_attr
.attr
,
152 &cm_sidr_rep_counter_attr
.attr
,
153 &cm_lap_counter_attr
.attr
,
154 &cm_apr_counter_attr
.attr
,
159 struct cm_device
*cm_dev
;
160 struct ib_mad_agent
*mad_agent
;
161 struct kobject port_obj
;
163 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
167 struct list_head list
;
168 struct ib_device
*ib_device
;
169 struct device
*device
;
171 struct cm_port
*port
[0];
175 struct cm_port
*port
;
177 struct ib_ah_attr ah_attr
;
183 struct delayed_work work
;
184 struct list_head list
;
185 struct cm_port
*port
;
186 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
187 __be32 local_id
; /* Established / timewait */
189 struct ib_cm_event cm_event
;
190 struct ib_sa_path_rec path
[0];
193 struct cm_timewait_info
{
194 struct cm_work work
; /* Must be first. */
195 struct list_head list
;
196 struct rb_node remote_qp_node
;
197 struct rb_node remote_id_node
;
198 __be64 remote_ca_guid
;
200 u8 inserted_remote_qp
;
201 u8 inserted_remote_id
;
204 struct cm_id_private
{
207 struct rb_node service_node
;
208 struct rb_node sidr_id_node
;
209 spinlock_t lock
; /* Do not acquire inside cm.lock */
210 struct completion comp
;
213 struct ib_mad_send_buf
*msg
;
214 struct cm_timewait_info
*timewait_info
;
215 /* todo: use alternate port on send failure */
218 struct ib_cm_compare_data
*compare_data
;
224 enum ib_qp_type qp_type
;
228 enum ib_mtu path_mtu
;
233 u8 responder_resources
;
240 struct list_head work_list
;
244 static void cm_work_handler(struct work_struct
*work
);
246 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
248 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
249 complete(&cm_id_priv
->comp
);
252 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
253 struct ib_mad_send_buf
**msg
)
255 struct ib_mad_agent
*mad_agent
;
256 struct ib_mad_send_buf
*m
;
259 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
260 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
264 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
265 cm_id_priv
->av
.pkey_index
,
266 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
273 /* Timeout set by caller if response is expected. */
275 m
->retries
= cm_id_priv
->max_cm_retries
;
277 atomic_inc(&cm_id_priv
->refcount
);
278 m
->context
[0] = cm_id_priv
;
283 static int cm_alloc_response_msg(struct cm_port
*port
,
284 struct ib_mad_recv_wc
*mad_recv_wc
,
285 struct ib_mad_send_buf
**msg
)
287 struct ib_mad_send_buf
*m
;
290 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
291 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
295 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
296 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
307 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
309 ib_destroy_ah(msg
->ah
);
311 cm_deref_id(msg
->context
[0]);
312 ib_free_send_mad(msg
);
315 static void * cm_copy_private_data(const void *private_data
,
320 if (!private_data
|| !private_data_len
)
323 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
325 return ERR_PTR(-ENOMEM
);
330 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
331 void *private_data
, u8 private_data_len
)
333 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
334 kfree(cm_id_priv
->private_data
);
336 cm_id_priv
->private_data
= private_data
;
337 cm_id_priv
->private_data_len
= private_data_len
;
340 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
341 struct ib_grh
*grh
, struct cm_av
*av
)
344 av
->pkey_index
= wc
->pkey_index
;
345 ib_init_ah_from_wc(port
->cm_dev
->ib_device
, port
->port_num
, wc
,
349 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
351 struct cm_device
*cm_dev
;
352 struct cm_port
*port
= NULL
;
357 read_lock_irqsave(&cm
.device_lock
, flags
);
358 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
359 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
361 port
= cm_dev
->port
[p
-1];
365 read_unlock_irqrestore(&cm
.device_lock
, flags
);
370 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
371 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
376 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
378 av
->timeout
= path
->packet_life_time
+ 1;
382 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
387 idr_preload(GFP_KERNEL
);
388 spin_lock_irqsave(&cm
.lock
, flags
);
390 id
= idr_alloc_cyclic(&cm
.local_id_table
, cm_id_priv
, 0, 0, GFP_NOWAIT
);
392 spin_unlock_irqrestore(&cm
.lock
, flags
);
395 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
396 return id
< 0 ? id
: 0;
399 static void cm_free_id(__be32 local_id
)
401 spin_lock_irq(&cm
.lock
);
402 idr_remove(&cm
.local_id_table
,
403 (__force
int) (local_id
^ cm
.random_id_operand
));
404 spin_unlock_irq(&cm
.lock
);
407 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
409 struct cm_id_private
*cm_id_priv
;
411 cm_id_priv
= idr_find(&cm
.local_id_table
,
412 (__force
int) (local_id
^ cm
.random_id_operand
));
414 if (cm_id_priv
->id
.remote_id
== remote_id
)
415 atomic_inc(&cm_id_priv
->refcount
);
423 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
425 struct cm_id_private
*cm_id_priv
;
427 spin_lock_irq(&cm
.lock
);
428 cm_id_priv
= cm_get_id(local_id
, remote_id
);
429 spin_unlock_irq(&cm
.lock
);
434 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
438 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
439 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
440 ((unsigned long *) mask
)[i
];
443 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
444 struct ib_cm_compare_data
*dst_data
)
446 u8 src
[IB_CM_COMPARE_SIZE
];
447 u8 dst
[IB_CM_COMPARE_SIZE
];
449 if (!src_data
|| !dst_data
)
452 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
453 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
454 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
457 static int cm_compare_private_data(u8
*private_data
,
458 struct ib_cm_compare_data
*dst_data
)
460 u8 src
[IB_CM_COMPARE_SIZE
];
465 cm_mask_copy(src
, private_data
, dst_data
->mask
);
466 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
470 * Trivial helpers to strip endian annotation and compare; the
471 * endianness doesn't actually matter since we just need a stable
472 * order for the RB tree.
474 static int be32_lt(__be32 a
, __be32 b
)
476 return (__force u32
) a
< (__force u32
) b
;
479 static int be32_gt(__be32 a
, __be32 b
)
481 return (__force u32
) a
> (__force u32
) b
;
484 static int be64_lt(__be64 a
, __be64 b
)
486 return (__force u64
) a
< (__force u64
) b
;
489 static int be64_gt(__be64 a
, __be64 b
)
491 return (__force u64
) a
> (__force u64
) b
;
494 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
496 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
497 struct rb_node
*parent
= NULL
;
498 struct cm_id_private
*cur_cm_id_priv
;
499 __be64 service_id
= cm_id_priv
->id
.service_id
;
500 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
505 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
507 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
508 cur_cm_id_priv
->compare_data
);
509 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
510 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
511 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
513 return cur_cm_id_priv
;
515 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
516 link
= &(*link
)->rb_left
;
517 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
518 link
= &(*link
)->rb_right
;
519 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
520 link
= &(*link
)->rb_left
;
521 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
522 link
= &(*link
)->rb_right
;
523 else if (data_cmp
< 0)
524 link
= &(*link
)->rb_left
;
526 link
= &(*link
)->rb_right
;
528 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
529 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
533 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
537 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
538 struct cm_id_private
*cm_id_priv
;
542 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
543 data_cmp
= cm_compare_private_data(private_data
,
544 cm_id_priv
->compare_data
);
545 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
546 cm_id_priv
->id
.service_id
&&
547 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
550 if (device
< cm_id_priv
->id
.device
)
551 node
= node
->rb_left
;
552 else if (device
> cm_id_priv
->id
.device
)
553 node
= node
->rb_right
;
554 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
555 node
= node
->rb_left
;
556 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
557 node
= node
->rb_right
;
558 else if (data_cmp
< 0)
559 node
= node
->rb_left
;
561 node
= node
->rb_right
;
566 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
569 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
570 struct rb_node
*parent
= NULL
;
571 struct cm_timewait_info
*cur_timewait_info
;
572 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
573 __be32 remote_id
= timewait_info
->work
.remote_id
;
577 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
579 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
580 link
= &(*link
)->rb_left
;
581 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
582 link
= &(*link
)->rb_right
;
583 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
584 link
= &(*link
)->rb_left
;
585 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
586 link
= &(*link
)->rb_right
;
588 return cur_timewait_info
;
590 timewait_info
->inserted_remote_id
= 1;
591 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
592 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
596 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
599 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
600 struct cm_timewait_info
*timewait_info
;
603 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
605 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
606 node
= node
->rb_left
;
607 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
608 node
= node
->rb_right
;
609 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
610 node
= node
->rb_left
;
611 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
612 node
= node
->rb_right
;
614 return timewait_info
;
619 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
622 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
623 struct rb_node
*parent
= NULL
;
624 struct cm_timewait_info
*cur_timewait_info
;
625 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
626 __be32 remote_qpn
= timewait_info
->remote_qpn
;
630 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
632 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
633 link
= &(*link
)->rb_left
;
634 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
635 link
= &(*link
)->rb_right
;
636 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
637 link
= &(*link
)->rb_left
;
638 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
639 link
= &(*link
)->rb_right
;
641 return cur_timewait_info
;
643 timewait_info
->inserted_remote_qp
= 1;
644 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
645 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
649 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
652 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
653 struct rb_node
*parent
= NULL
;
654 struct cm_id_private
*cur_cm_id_priv
;
655 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
656 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
660 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
662 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
663 link
= &(*link
)->rb_left
;
664 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
665 link
= &(*link
)->rb_right
;
668 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
671 link
= &(*link
)->rb_left
;
673 link
= &(*link
)->rb_right
;
675 return cur_cm_id_priv
;
678 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
679 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
683 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
684 enum ib_cm_sidr_status status
)
686 struct ib_cm_sidr_rep_param param
;
688 memset(¶m
, 0, sizeof param
);
689 param
.status
= status
;
690 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
693 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
694 ib_cm_handler cm_handler
,
697 struct cm_id_private
*cm_id_priv
;
700 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
702 return ERR_PTR(-ENOMEM
);
704 cm_id_priv
->id
.state
= IB_CM_IDLE
;
705 cm_id_priv
->id
.device
= device
;
706 cm_id_priv
->id
.cm_handler
= cm_handler
;
707 cm_id_priv
->id
.context
= context
;
708 cm_id_priv
->id
.remote_cm_qpn
= 1;
709 ret
= cm_alloc_id(cm_id_priv
);
713 spin_lock_init(&cm_id_priv
->lock
);
714 init_completion(&cm_id_priv
->comp
);
715 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
716 atomic_set(&cm_id_priv
->work_count
, -1);
717 atomic_set(&cm_id_priv
->refcount
, 1);
718 return &cm_id_priv
->id
;
722 return ERR_PTR(-ENOMEM
);
724 EXPORT_SYMBOL(ib_create_cm_id
);
726 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
728 struct cm_work
*work
;
730 if (list_empty(&cm_id_priv
->work_list
))
733 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
734 list_del(&work
->list
);
738 static void cm_free_work(struct cm_work
*work
)
740 if (work
->mad_recv_wc
)
741 ib_free_recv_mad(work
->mad_recv_wc
);
745 static inline int cm_convert_to_ms(int iba_time
)
747 /* approximate conversion to ms from 4.096us x 2^iba_time */
748 return 1 << max(iba_time
- 8, 0);
752 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
753 * Because of how ack_timeout is stored, adding one doubles the timeout.
754 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
755 * increment it (round up) only if the other is within 50%.
757 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
759 int ack_timeout
= packet_life_time
+ 1;
761 if (ack_timeout
>= ca_ack_delay
)
762 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
764 ack_timeout
= ca_ack_delay
+
765 (ack_timeout
>= (ca_ack_delay
- 1));
767 return min(31, ack_timeout
);
770 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
772 if (timewait_info
->inserted_remote_id
) {
773 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
774 timewait_info
->inserted_remote_id
= 0;
777 if (timewait_info
->inserted_remote_qp
) {
778 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
779 timewait_info
->inserted_remote_qp
= 0;
783 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
785 struct cm_timewait_info
*timewait_info
;
787 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
789 return ERR_PTR(-ENOMEM
);
791 timewait_info
->work
.local_id
= local_id
;
792 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
793 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
794 return timewait_info
;
797 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
802 spin_lock_irqsave(&cm
.lock
, flags
);
803 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
804 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
805 spin_unlock_irqrestore(&cm
.lock
, flags
);
808 * The cm_id could be destroyed by the user before we exit timewait.
809 * To protect against this, we search for the cm_id after exiting
810 * timewait before notifying the user that we've exited timewait.
812 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
813 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
814 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
815 msecs_to_jiffies(wait_time
));
816 cm_id_priv
->timewait_info
= NULL
;
819 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
823 cm_id_priv
->id
.state
= IB_CM_IDLE
;
824 if (cm_id_priv
->timewait_info
) {
825 spin_lock_irqsave(&cm
.lock
, flags
);
826 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
827 spin_unlock_irqrestore(&cm
.lock
, flags
);
828 kfree(cm_id_priv
->timewait_info
);
829 cm_id_priv
->timewait_info
= NULL
;
833 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
835 struct cm_id_private
*cm_id_priv
;
836 struct cm_work
*work
;
838 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
840 spin_lock_irq(&cm_id_priv
->lock
);
841 switch (cm_id
->state
) {
843 cm_id
->state
= IB_CM_IDLE
;
844 spin_unlock_irq(&cm_id_priv
->lock
);
845 spin_lock_irq(&cm
.lock
);
846 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
847 spin_unlock_irq(&cm
.lock
);
849 case IB_CM_SIDR_REQ_SENT
:
850 cm_id
->state
= IB_CM_IDLE
;
851 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
852 spin_unlock_irq(&cm_id_priv
->lock
);
854 case IB_CM_SIDR_REQ_RCVD
:
855 spin_unlock_irq(&cm_id_priv
->lock
);
856 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
859 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
860 spin_unlock_irq(&cm_id_priv
->lock
);
861 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
862 &cm_id_priv
->id
.device
->node_guid
,
863 sizeof cm_id_priv
->id
.device
->node_guid
,
867 if (err
== -ENOMEM
) {
868 /* Do not reject to allow future retries. */
869 cm_reset_to_idle(cm_id_priv
);
870 spin_unlock_irq(&cm_id_priv
->lock
);
872 spin_unlock_irq(&cm_id_priv
->lock
);
873 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
877 case IB_CM_MRA_REQ_RCVD
:
879 case IB_CM_MRA_REP_RCVD
:
880 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
882 case IB_CM_MRA_REQ_SENT
:
884 case IB_CM_MRA_REP_SENT
:
885 spin_unlock_irq(&cm_id_priv
->lock
);
886 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
889 case IB_CM_ESTABLISHED
:
890 spin_unlock_irq(&cm_id_priv
->lock
);
891 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
893 ib_send_cm_dreq(cm_id
, NULL
, 0);
895 case IB_CM_DREQ_SENT
:
896 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
897 cm_enter_timewait(cm_id_priv
);
898 spin_unlock_irq(&cm_id_priv
->lock
);
900 case IB_CM_DREQ_RCVD
:
901 spin_unlock_irq(&cm_id_priv
->lock
);
902 ib_send_cm_drep(cm_id
, NULL
, 0);
905 spin_unlock_irq(&cm_id_priv
->lock
);
909 cm_free_id(cm_id
->local_id
);
910 cm_deref_id(cm_id_priv
);
911 wait_for_completion(&cm_id_priv
->comp
);
912 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
914 kfree(cm_id_priv
->compare_data
);
915 kfree(cm_id_priv
->private_data
);
919 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
921 cm_destroy_id(cm_id
, 0);
923 EXPORT_SYMBOL(ib_destroy_cm_id
);
925 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
926 struct ib_cm_compare_data
*compare_data
)
928 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
932 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
933 service_id
&= service_mask
;
934 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
935 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
938 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
939 if (cm_id
->state
!= IB_CM_IDLE
)
943 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
945 if (!cm_id_priv
->compare_data
)
947 cm_mask_copy(cm_id_priv
->compare_data
->data
,
948 compare_data
->data
, compare_data
->mask
);
949 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
953 cm_id
->state
= IB_CM_LISTEN
;
955 spin_lock_irqsave(&cm
.lock
, flags
);
956 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
957 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
958 cm_id
->service_mask
= ~cpu_to_be64(0);
960 cm_id
->service_id
= service_id
;
961 cm_id
->service_mask
= service_mask
;
963 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
964 spin_unlock_irqrestore(&cm
.lock
, flags
);
966 if (cur_cm_id_priv
) {
967 cm_id
->state
= IB_CM_IDLE
;
968 kfree(cm_id_priv
->compare_data
);
969 cm_id_priv
->compare_data
= NULL
;
974 EXPORT_SYMBOL(ib_cm_listen
);
976 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
977 enum cm_msg_sequence msg_seq
)
981 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
982 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
984 return cpu_to_be64(hi_tid
| low_tid
);
987 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
988 __be16 attr_id
, __be64 tid
)
990 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
991 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
992 hdr
->class_version
= IB_CM_CLASS_VERSION
;
993 hdr
->method
= IB_MGMT_METHOD_SEND
;
994 hdr
->attr_id
= attr_id
;
998 static void cm_format_req(struct cm_req_msg
*req_msg
,
999 struct cm_id_private
*cm_id_priv
,
1000 struct ib_cm_req_param
*param
)
1002 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1003 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1005 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1006 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1008 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1009 req_msg
->service_id
= param
->service_id
;
1010 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1011 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1012 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1013 cm_req_set_remote_resp_timeout(req_msg
,
1014 param
->remote_cm_response_timeout
);
1015 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1016 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1017 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1018 cm_req_set_local_resp_timeout(req_msg
,
1019 param
->local_cm_response_timeout
);
1020 req_msg
->pkey
= param
->primary_path
->pkey
;
1021 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1022 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1024 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1025 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1026 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1027 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1028 cm_req_set_srq(req_msg
, param
->srq
);
1031 if (pri_path
->hop_limit
<= 1) {
1032 req_msg
->primary_local_lid
= pri_path
->slid
;
1033 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1035 /* Work-around until there's a way to obtain remote LID info */
1036 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1037 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1039 req_msg
->primary_local_gid
= pri_path
->sgid
;
1040 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1041 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1042 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1043 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1044 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1045 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1046 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1047 cm_req_set_primary_local_ack_timeout(req_msg
,
1048 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1049 pri_path
->packet_life_time
));
1052 if (alt_path
->hop_limit
<= 1) {
1053 req_msg
->alt_local_lid
= alt_path
->slid
;
1054 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1056 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1057 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1059 req_msg
->alt_local_gid
= alt_path
->sgid
;
1060 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1061 cm_req_set_alt_flow_label(req_msg
,
1062 alt_path
->flow_label
);
1063 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1064 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1065 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1066 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1067 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1068 cm_req_set_alt_local_ack_timeout(req_msg
,
1069 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1070 alt_path
->packet_life_time
));
1073 if (param
->private_data
&& param
->private_data_len
)
1074 memcpy(req_msg
->private_data
, param
->private_data
,
1075 param
->private_data_len
);
1078 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1080 /* peer-to-peer not supported */
1081 if (param
->peer_to_peer
)
1084 if (!param
->primary_path
)
1087 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1088 param
->qp_type
!= IB_QPT_XRC_INI
)
1091 if (param
->private_data
&&
1092 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1095 if (param
->alternate_path
&&
1096 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1097 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1103 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1104 struct ib_cm_req_param
*param
)
1106 struct cm_id_private
*cm_id_priv
;
1107 struct cm_req_msg
*req_msg
;
1108 unsigned long flags
;
1111 ret
= cm_validate_req_param(param
);
1115 /* Verify that we're not in timewait. */
1116 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1117 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1118 if (cm_id
->state
!= IB_CM_IDLE
) {
1119 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1123 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1125 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1127 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1128 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1132 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1135 if (param
->alternate_path
) {
1136 ret
= cm_init_av_by_path(param
->alternate_path
,
1137 &cm_id_priv
->alt_av
);
1141 cm_id
->service_id
= param
->service_id
;
1142 cm_id
->service_mask
= ~cpu_to_be64(0);
1143 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1144 param
->primary_path
->packet_life_time
) * 2 +
1146 param
->remote_cm_response_timeout
);
1147 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1148 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1149 cm_id_priv
->responder_resources
= param
->responder_resources
;
1150 cm_id_priv
->retry_count
= param
->retry_count
;
1151 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1152 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1153 cm_id_priv
->qp_type
= param
->qp_type
;
1155 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1159 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1160 cm_format_req(req_msg
, cm_id_priv
, param
);
1161 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1162 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1163 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1165 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1166 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1168 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1169 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1171 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1174 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1175 cm_id
->state
= IB_CM_REQ_SENT
;
1176 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1179 error2
: cm_free_msg(cm_id_priv
->msg
);
1180 error1
: kfree(cm_id_priv
->timewait_info
);
1183 EXPORT_SYMBOL(ib_send_cm_req
);
1185 static int cm_issue_rej(struct cm_port
*port
,
1186 struct ib_mad_recv_wc
*mad_recv_wc
,
1187 enum ib_cm_rej_reason reason
,
1188 enum cm_msg_response msg_rejected
,
1189 void *ari
, u8 ari_length
)
1191 struct ib_mad_send_buf
*msg
= NULL
;
1192 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1195 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1199 /* We just need common CM header information. Cast to any message. */
1200 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1201 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1203 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1204 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1205 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1206 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1207 rej_msg
->reason
= cpu_to_be16(reason
);
1209 if (ari
&& ari_length
) {
1210 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1211 memcpy(rej_msg
->ari
, ari
, ari_length
);
1214 ret
= ib_post_send_mad(msg
, NULL
);
1221 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1222 __be32 local_qpn
, __be32 remote_qpn
)
1224 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1225 ((local_ca_guid
== remote_ca_guid
) &&
1226 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1229 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1230 struct ib_sa_path_rec
*primary_path
,
1231 struct ib_sa_path_rec
*alt_path
)
1233 memset(primary_path
, 0, sizeof *primary_path
);
1234 primary_path
->dgid
= req_msg
->primary_local_gid
;
1235 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1236 primary_path
->dlid
= req_msg
->primary_local_lid
;
1237 primary_path
->slid
= req_msg
->primary_remote_lid
;
1238 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1239 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1240 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1241 primary_path
->reversible
= 1;
1242 primary_path
->pkey
= req_msg
->pkey
;
1243 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1244 primary_path
->mtu_selector
= IB_SA_EQ
;
1245 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1246 primary_path
->rate_selector
= IB_SA_EQ
;
1247 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1248 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1249 primary_path
->packet_life_time
=
1250 cm_req_get_primary_local_ack_timeout(req_msg
);
1251 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1253 if (req_msg
->alt_local_lid
) {
1254 memset(alt_path
, 0, sizeof *alt_path
);
1255 alt_path
->dgid
= req_msg
->alt_local_gid
;
1256 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1257 alt_path
->dlid
= req_msg
->alt_local_lid
;
1258 alt_path
->slid
= req_msg
->alt_remote_lid
;
1259 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1260 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1261 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1262 alt_path
->reversible
= 1;
1263 alt_path
->pkey
= req_msg
->pkey
;
1264 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1265 alt_path
->mtu_selector
= IB_SA_EQ
;
1266 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1267 alt_path
->rate_selector
= IB_SA_EQ
;
1268 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1269 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1270 alt_path
->packet_life_time
=
1271 cm_req_get_alt_local_ack_timeout(req_msg
);
1272 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1276 static void cm_format_req_event(struct cm_work
*work
,
1277 struct cm_id_private
*cm_id_priv
,
1278 struct ib_cm_id
*listen_id
)
1280 struct cm_req_msg
*req_msg
;
1281 struct ib_cm_req_event_param
*param
;
1283 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1284 param
= &work
->cm_event
.param
.req_rcvd
;
1285 param
->listen_id
= listen_id
;
1286 param
->port
= cm_id_priv
->av
.port
->port_num
;
1287 param
->primary_path
= &work
->path
[0];
1288 if (req_msg
->alt_local_lid
)
1289 param
->alternate_path
= &work
->path
[1];
1291 param
->alternate_path
= NULL
;
1292 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1293 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1294 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1295 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1296 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1297 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1298 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1299 param
->local_cm_response_timeout
=
1300 cm_req_get_remote_resp_timeout(req_msg
);
1301 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1302 param
->remote_cm_response_timeout
=
1303 cm_req_get_local_resp_timeout(req_msg
);
1304 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1305 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1306 param
->srq
= cm_req_get_srq(req_msg
);
1307 work
->cm_event
.private_data
= &req_msg
->private_data
;
1310 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1311 struct cm_work
*work
)
1315 /* We will typically only have the current event to report. */
1316 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1319 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1320 spin_lock_irq(&cm_id_priv
->lock
);
1321 work
= cm_dequeue_work(cm_id_priv
);
1322 spin_unlock_irq(&cm_id_priv
->lock
);
1324 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1328 cm_deref_id(cm_id_priv
);
1330 cm_destroy_id(&cm_id_priv
->id
, ret
);
1333 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1334 struct cm_id_private
*cm_id_priv
,
1335 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1336 const void *private_data
, u8 private_data_len
)
1338 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1339 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1340 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1341 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1342 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1344 if (private_data
&& private_data_len
)
1345 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1348 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1349 struct cm_id_private
*cm_id_priv
,
1350 enum ib_cm_rej_reason reason
,
1353 const void *private_data
,
1354 u8 private_data_len
)
1356 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1357 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1359 switch(cm_id_priv
->id
.state
) {
1360 case IB_CM_REQ_RCVD
:
1361 rej_msg
->local_comm_id
= 0;
1362 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1364 case IB_CM_MRA_REQ_SENT
:
1365 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1366 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1368 case IB_CM_REP_RCVD
:
1369 case IB_CM_MRA_REP_SENT
:
1370 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1371 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1374 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1375 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1379 rej_msg
->reason
= cpu_to_be16(reason
);
1380 if (ari
&& ari_length
) {
1381 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1382 memcpy(rej_msg
->ari
, ari
, ari_length
);
1385 if (private_data
&& private_data_len
)
1386 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1389 static void cm_dup_req_handler(struct cm_work
*work
,
1390 struct cm_id_private
*cm_id_priv
)
1392 struct ib_mad_send_buf
*msg
= NULL
;
1395 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1396 counter
[CM_REQ_COUNTER
]);
1398 /* Quick state check to discard duplicate REQs. */
1399 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1402 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1406 spin_lock_irq(&cm_id_priv
->lock
);
1407 switch (cm_id_priv
->id
.state
) {
1408 case IB_CM_MRA_REQ_SENT
:
1409 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1410 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1411 cm_id_priv
->private_data
,
1412 cm_id_priv
->private_data_len
);
1414 case IB_CM_TIMEWAIT
:
1415 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1416 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1421 spin_unlock_irq(&cm_id_priv
->lock
);
1423 ret
= ib_post_send_mad(msg
, NULL
);
1428 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1429 free
: cm_free_msg(msg
);
1432 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1433 struct cm_id_private
*cm_id_priv
)
1435 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1436 struct cm_timewait_info
*timewait_info
;
1437 struct cm_req_msg
*req_msg
;
1439 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1441 /* Check for possible duplicate REQ. */
1442 spin_lock_irq(&cm
.lock
);
1443 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1444 if (timewait_info
) {
1445 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1446 timewait_info
->work
.remote_id
);
1447 spin_unlock_irq(&cm
.lock
);
1448 if (cur_cm_id_priv
) {
1449 cm_dup_req_handler(work
, cur_cm_id_priv
);
1450 cm_deref_id(cur_cm_id_priv
);
1455 /* Check for stale connections. */
1456 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1457 if (timewait_info
) {
1458 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1459 spin_unlock_irq(&cm
.lock
);
1460 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1461 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1466 /* Find matching listen request. */
1467 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1468 req_msg
->service_id
,
1469 req_msg
->private_data
);
1470 if (!listen_cm_id_priv
) {
1471 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1472 spin_unlock_irq(&cm
.lock
);
1473 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1474 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1478 atomic_inc(&listen_cm_id_priv
->refcount
);
1479 atomic_inc(&cm_id_priv
->refcount
);
1480 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1481 atomic_inc(&cm_id_priv
->work_count
);
1482 spin_unlock_irq(&cm
.lock
);
1484 return listen_cm_id_priv
;
1488 * Work-around for inter-subnet connections. If the LIDs are permissive,
1489 * we need to override the LID/SL data in the REQ with the LID information
1490 * in the work completion.
1492 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1494 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1495 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1496 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1497 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1500 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1501 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1504 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1505 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1506 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1507 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1510 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1511 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1515 static int cm_req_handler(struct cm_work
*work
)
1517 struct ib_cm_id
*cm_id
;
1518 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1519 struct cm_req_msg
*req_msg
;
1522 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1524 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1526 return PTR_ERR(cm_id
);
1528 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1529 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1530 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1531 work
->mad_recv_wc
->recv_buf
.grh
,
1533 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1535 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1536 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1539 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1540 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1541 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1543 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1544 if (!listen_cm_id_priv
) {
1546 kfree(cm_id_priv
->timewait_info
);
1550 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1551 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1552 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1553 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1555 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1556 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1557 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1559 ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1560 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1561 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1562 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1566 if (req_msg
->alt_local_lid
) {
1567 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1569 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1570 &work
->path
[0].sgid
,
1571 sizeof work
->path
[0].sgid
, NULL
, 0);
1575 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1576 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1577 cm_req_get_local_resp_timeout(req_msg
));
1578 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1579 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1580 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1581 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1582 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1583 cm_id_priv
->pkey
= req_msg
->pkey
;
1584 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1585 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1586 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1587 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1589 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1590 cm_process_work(cm_id_priv
, work
);
1591 cm_deref_id(listen_cm_id_priv
);
1595 atomic_dec(&cm_id_priv
->refcount
);
1596 cm_deref_id(listen_cm_id_priv
);
1598 ib_destroy_cm_id(cm_id
);
1602 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1603 struct cm_id_private
*cm_id_priv
,
1604 struct ib_cm_rep_param
*param
)
1606 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1607 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1608 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1609 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1610 rep_msg
->resp_resources
= param
->responder_resources
;
1611 cm_rep_set_target_ack_delay(rep_msg
,
1612 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1613 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1614 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1615 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1617 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1618 rep_msg
->initiator_depth
= param
->initiator_depth
;
1619 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1620 cm_rep_set_srq(rep_msg
, param
->srq
);
1621 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1623 cm_rep_set_srq(rep_msg
, 1);
1624 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1627 if (param
->private_data
&& param
->private_data_len
)
1628 memcpy(rep_msg
->private_data
, param
->private_data
,
1629 param
->private_data_len
);
1632 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1633 struct ib_cm_rep_param
*param
)
1635 struct cm_id_private
*cm_id_priv
;
1636 struct ib_mad_send_buf
*msg
;
1637 struct cm_rep_msg
*rep_msg
;
1638 unsigned long flags
;
1641 if (param
->private_data
&&
1642 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1645 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1646 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1647 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1648 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1653 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1657 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1658 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1659 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1660 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1662 ret
= ib_post_send_mad(msg
, NULL
);
1664 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1669 cm_id
->state
= IB_CM_REP_SENT
;
1670 cm_id_priv
->msg
= msg
;
1671 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1672 cm_id_priv
->responder_resources
= param
->responder_resources
;
1673 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1674 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1676 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1679 EXPORT_SYMBOL(ib_send_cm_rep
);
1681 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1682 struct cm_id_private
*cm_id_priv
,
1683 const void *private_data
,
1684 u8 private_data_len
)
1686 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1687 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1688 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1690 if (private_data
&& private_data_len
)
1691 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1694 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1695 const void *private_data
,
1696 u8 private_data_len
)
1698 struct cm_id_private
*cm_id_priv
;
1699 struct ib_mad_send_buf
*msg
;
1700 unsigned long flags
;
1704 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1707 data
= cm_copy_private_data(private_data
, private_data_len
);
1709 return PTR_ERR(data
);
1711 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1712 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1713 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1714 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1719 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1723 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1724 private_data
, private_data_len
);
1726 ret
= ib_post_send_mad(msg
, NULL
);
1728 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1734 cm_id
->state
= IB_CM_ESTABLISHED
;
1735 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1736 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1739 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1743 EXPORT_SYMBOL(ib_send_cm_rtu
);
1745 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
1747 struct cm_rep_msg
*rep_msg
;
1748 struct ib_cm_rep_event_param
*param
;
1750 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1751 param
= &work
->cm_event
.param
.rep_rcvd
;
1752 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1753 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1754 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
1755 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1756 param
->responder_resources
= rep_msg
->initiator_depth
;
1757 param
->initiator_depth
= rep_msg
->resp_resources
;
1758 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1759 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1760 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1761 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1762 param
->srq
= cm_rep_get_srq(rep_msg
);
1763 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1766 static void cm_dup_rep_handler(struct cm_work
*work
)
1768 struct cm_id_private
*cm_id_priv
;
1769 struct cm_rep_msg
*rep_msg
;
1770 struct ib_mad_send_buf
*msg
= NULL
;
1773 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1774 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1775 rep_msg
->local_comm_id
);
1779 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1780 counter
[CM_REP_COUNTER
]);
1781 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1785 spin_lock_irq(&cm_id_priv
->lock
);
1786 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1787 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1788 cm_id_priv
->private_data
,
1789 cm_id_priv
->private_data_len
);
1790 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1791 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1792 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1793 cm_id_priv
->private_data
,
1794 cm_id_priv
->private_data_len
);
1797 spin_unlock_irq(&cm_id_priv
->lock
);
1799 ret
= ib_post_send_mad(msg
, NULL
);
1804 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1805 free
: cm_free_msg(msg
);
1806 deref
: cm_deref_id(cm_id_priv
);
1809 static int cm_rep_handler(struct cm_work
*work
)
1811 struct cm_id_private
*cm_id_priv
;
1812 struct cm_rep_msg
*rep_msg
;
1815 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1816 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1818 cm_dup_rep_handler(work
);
1822 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
1824 spin_lock_irq(&cm_id_priv
->lock
);
1825 switch (cm_id_priv
->id
.state
) {
1826 case IB_CM_REQ_SENT
:
1827 case IB_CM_MRA_REQ_RCVD
:
1830 spin_unlock_irq(&cm_id_priv
->lock
);
1835 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1836 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1837 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1839 spin_lock(&cm
.lock
);
1840 /* Check for duplicate REP. */
1841 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1842 spin_unlock(&cm
.lock
);
1843 spin_unlock_irq(&cm_id_priv
->lock
);
1847 /* Check for a stale connection. */
1848 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1849 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1850 &cm
.remote_id_table
);
1851 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1852 spin_unlock(&cm
.lock
);
1853 spin_unlock_irq(&cm_id_priv
->lock
);
1854 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1855 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1860 spin_unlock(&cm
.lock
);
1862 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1863 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1864 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1865 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1866 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1867 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1868 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1869 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1870 cm_id_priv
->av
.timeout
=
1871 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1872 cm_id_priv
->av
.timeout
- 1);
1873 cm_id_priv
->alt_av
.timeout
=
1874 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1875 cm_id_priv
->alt_av
.timeout
- 1);
1877 /* todo: handle peer_to_peer */
1879 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1880 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1882 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1883 spin_unlock_irq(&cm_id_priv
->lock
);
1886 cm_process_work(cm_id_priv
, work
);
1888 cm_deref_id(cm_id_priv
);
1892 cm_deref_id(cm_id_priv
);
1896 static int cm_establish_handler(struct cm_work
*work
)
1898 struct cm_id_private
*cm_id_priv
;
1901 /* See comment in cm_establish about lookup. */
1902 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1906 spin_lock_irq(&cm_id_priv
->lock
);
1907 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1908 spin_unlock_irq(&cm_id_priv
->lock
);
1912 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1913 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1915 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1916 spin_unlock_irq(&cm_id_priv
->lock
);
1919 cm_process_work(cm_id_priv
, work
);
1921 cm_deref_id(cm_id_priv
);
1924 cm_deref_id(cm_id_priv
);
1928 static int cm_rtu_handler(struct cm_work
*work
)
1930 struct cm_id_private
*cm_id_priv
;
1931 struct cm_rtu_msg
*rtu_msg
;
1934 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1935 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1936 rtu_msg
->local_comm_id
);
1940 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1942 spin_lock_irq(&cm_id_priv
->lock
);
1943 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1944 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1945 spin_unlock_irq(&cm_id_priv
->lock
);
1946 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1947 counter
[CM_RTU_COUNTER
]);
1950 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1952 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1953 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1955 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1956 spin_unlock_irq(&cm_id_priv
->lock
);
1959 cm_process_work(cm_id_priv
, work
);
1961 cm_deref_id(cm_id_priv
);
1964 cm_deref_id(cm_id_priv
);
1968 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1969 struct cm_id_private
*cm_id_priv
,
1970 const void *private_data
,
1971 u8 private_data_len
)
1973 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1974 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1975 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1976 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1977 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1979 if (private_data
&& private_data_len
)
1980 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1983 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1984 const void *private_data
,
1985 u8 private_data_len
)
1987 struct cm_id_private
*cm_id_priv
;
1988 struct ib_mad_send_buf
*msg
;
1989 unsigned long flags
;
1992 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1995 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1996 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1997 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2002 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2003 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2004 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2006 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2008 cm_enter_timewait(cm_id_priv
);
2012 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2013 private_data
, private_data_len
);
2014 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2015 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2017 ret
= ib_post_send_mad(msg
, NULL
);
2019 cm_enter_timewait(cm_id_priv
);
2020 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2025 cm_id
->state
= IB_CM_DREQ_SENT
;
2026 cm_id_priv
->msg
= msg
;
2027 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2030 EXPORT_SYMBOL(ib_send_cm_dreq
);
2032 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2033 struct cm_id_private
*cm_id_priv
,
2034 const void *private_data
,
2035 u8 private_data_len
)
2037 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2038 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2039 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2041 if (private_data
&& private_data_len
)
2042 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2045 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2046 const void *private_data
,
2047 u8 private_data_len
)
2049 struct cm_id_private
*cm_id_priv
;
2050 struct ib_mad_send_buf
*msg
;
2051 unsigned long flags
;
2055 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2058 data
= cm_copy_private_data(private_data
, private_data_len
);
2060 return PTR_ERR(data
);
2062 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2063 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2064 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2065 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2070 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2071 cm_enter_timewait(cm_id_priv
);
2073 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2077 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2078 private_data
, private_data_len
);
2080 ret
= ib_post_send_mad(msg
, NULL
);
2082 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2087 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2090 EXPORT_SYMBOL(ib_send_cm_drep
);
2092 static int cm_issue_drep(struct cm_port
*port
,
2093 struct ib_mad_recv_wc
*mad_recv_wc
)
2095 struct ib_mad_send_buf
*msg
= NULL
;
2096 struct cm_dreq_msg
*dreq_msg
;
2097 struct cm_drep_msg
*drep_msg
;
2100 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2104 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2105 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2107 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2108 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2109 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2111 ret
= ib_post_send_mad(msg
, NULL
);
2118 static int cm_dreq_handler(struct cm_work
*work
)
2120 struct cm_id_private
*cm_id_priv
;
2121 struct cm_dreq_msg
*dreq_msg
;
2122 struct ib_mad_send_buf
*msg
= NULL
;
2125 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2126 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2127 dreq_msg
->local_comm_id
);
2129 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2130 counter
[CM_DREQ_COUNTER
]);
2131 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2135 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2137 spin_lock_irq(&cm_id_priv
->lock
);
2138 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2141 switch (cm_id_priv
->id
.state
) {
2142 case IB_CM_REP_SENT
:
2143 case IB_CM_DREQ_SENT
:
2144 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2146 case IB_CM_ESTABLISHED
:
2147 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2148 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2149 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2151 case IB_CM_MRA_REP_RCVD
:
2153 case IB_CM_TIMEWAIT
:
2154 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2155 counter
[CM_DREQ_COUNTER
]);
2156 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2159 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2160 cm_id_priv
->private_data
,
2161 cm_id_priv
->private_data_len
);
2162 spin_unlock_irq(&cm_id_priv
->lock
);
2164 if (ib_post_send_mad(msg
, NULL
))
2167 case IB_CM_DREQ_RCVD
:
2168 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2169 counter
[CM_DREQ_COUNTER
]);
2174 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2175 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2176 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2178 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2179 spin_unlock_irq(&cm_id_priv
->lock
);
2182 cm_process_work(cm_id_priv
, work
);
2184 cm_deref_id(cm_id_priv
);
2187 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2188 deref
: cm_deref_id(cm_id_priv
);
2192 static int cm_drep_handler(struct cm_work
*work
)
2194 struct cm_id_private
*cm_id_priv
;
2195 struct cm_drep_msg
*drep_msg
;
2198 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2199 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2200 drep_msg
->local_comm_id
);
2204 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2206 spin_lock_irq(&cm_id_priv
->lock
);
2207 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2208 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2209 spin_unlock_irq(&cm_id_priv
->lock
);
2212 cm_enter_timewait(cm_id_priv
);
2214 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2215 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2217 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2218 spin_unlock_irq(&cm_id_priv
->lock
);
2221 cm_process_work(cm_id_priv
, work
);
2223 cm_deref_id(cm_id_priv
);
2226 cm_deref_id(cm_id_priv
);
2230 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2231 enum ib_cm_rej_reason reason
,
2234 const void *private_data
,
2235 u8 private_data_len
)
2237 struct cm_id_private
*cm_id_priv
;
2238 struct ib_mad_send_buf
*msg
;
2239 unsigned long flags
;
2242 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2243 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2246 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2248 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2249 switch (cm_id
->state
) {
2250 case IB_CM_REQ_SENT
:
2251 case IB_CM_MRA_REQ_RCVD
:
2252 case IB_CM_REQ_RCVD
:
2253 case IB_CM_MRA_REQ_SENT
:
2254 case IB_CM_REP_RCVD
:
2255 case IB_CM_MRA_REP_SENT
:
2256 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2258 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2259 cm_id_priv
, reason
, ari
, ari_length
,
2260 private_data
, private_data_len
);
2262 cm_reset_to_idle(cm_id_priv
);
2264 case IB_CM_REP_SENT
:
2265 case IB_CM_MRA_REP_RCVD
:
2266 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2268 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2269 cm_id_priv
, reason
, ari
, ari_length
,
2270 private_data
, private_data_len
);
2272 cm_enter_timewait(cm_id_priv
);
2282 ret
= ib_post_send_mad(msg
, NULL
);
2286 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2289 EXPORT_SYMBOL(ib_send_cm_rej
);
2291 static void cm_format_rej_event(struct cm_work
*work
)
2293 struct cm_rej_msg
*rej_msg
;
2294 struct ib_cm_rej_event_param
*param
;
2296 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2297 param
= &work
->cm_event
.param
.rej_rcvd
;
2298 param
->ari
= rej_msg
->ari
;
2299 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2300 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2301 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2304 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2306 struct cm_timewait_info
*timewait_info
;
2307 struct cm_id_private
*cm_id_priv
;
2310 remote_id
= rej_msg
->local_comm_id
;
2312 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2313 spin_lock_irq(&cm
.lock
);
2314 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2316 if (!timewait_info
) {
2317 spin_unlock_irq(&cm
.lock
);
2320 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2321 (timewait_info
->work
.local_id
^
2322 cm
.random_id_operand
));
2324 if (cm_id_priv
->id
.remote_id
== remote_id
)
2325 atomic_inc(&cm_id_priv
->refcount
);
2329 spin_unlock_irq(&cm
.lock
);
2330 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2331 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2333 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2338 static int cm_rej_handler(struct cm_work
*work
)
2340 struct cm_id_private
*cm_id_priv
;
2341 struct cm_rej_msg
*rej_msg
;
2344 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2345 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2349 cm_format_rej_event(work
);
2351 spin_lock_irq(&cm_id_priv
->lock
);
2352 switch (cm_id_priv
->id
.state
) {
2353 case IB_CM_REQ_SENT
:
2354 case IB_CM_MRA_REQ_RCVD
:
2355 case IB_CM_REP_SENT
:
2356 case IB_CM_MRA_REP_RCVD
:
2357 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2359 case IB_CM_REQ_RCVD
:
2360 case IB_CM_MRA_REQ_SENT
:
2361 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2362 cm_enter_timewait(cm_id_priv
);
2364 cm_reset_to_idle(cm_id_priv
);
2366 case IB_CM_DREQ_SENT
:
2367 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2369 case IB_CM_REP_RCVD
:
2370 case IB_CM_MRA_REP_SENT
:
2371 cm_enter_timewait(cm_id_priv
);
2373 case IB_CM_ESTABLISHED
:
2374 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2375 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2376 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2377 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2379 cm_enter_timewait(cm_id_priv
);
2384 spin_unlock_irq(&cm_id_priv
->lock
);
2389 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2391 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2392 spin_unlock_irq(&cm_id_priv
->lock
);
2395 cm_process_work(cm_id_priv
, work
);
2397 cm_deref_id(cm_id_priv
);
2400 cm_deref_id(cm_id_priv
);
2404 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2406 const void *private_data
,
2407 u8 private_data_len
)
2409 struct cm_id_private
*cm_id_priv
;
2410 struct ib_mad_send_buf
*msg
;
2411 enum ib_cm_state cm_state
;
2412 enum ib_cm_lap_state lap_state
;
2413 enum cm_msg_response msg_response
;
2415 unsigned long flags
;
2418 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2421 data
= cm_copy_private_data(private_data
, private_data_len
);
2423 return PTR_ERR(data
);
2425 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2427 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2428 switch(cm_id_priv
->id
.state
) {
2429 case IB_CM_REQ_RCVD
:
2430 cm_state
= IB_CM_MRA_REQ_SENT
;
2431 lap_state
= cm_id
->lap_state
;
2432 msg_response
= CM_MSG_RESPONSE_REQ
;
2434 case IB_CM_REP_RCVD
:
2435 cm_state
= IB_CM_MRA_REP_SENT
;
2436 lap_state
= cm_id
->lap_state
;
2437 msg_response
= CM_MSG_RESPONSE_REP
;
2439 case IB_CM_ESTABLISHED
:
2440 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2441 cm_state
= cm_id
->state
;
2442 lap_state
= IB_CM_MRA_LAP_SENT
;
2443 msg_response
= CM_MSG_RESPONSE_OTHER
;
2451 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2452 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2456 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2457 msg_response
, service_timeout
,
2458 private_data
, private_data_len
);
2459 ret
= ib_post_send_mad(msg
, NULL
);
2464 cm_id
->state
= cm_state
;
2465 cm_id
->lap_state
= lap_state
;
2466 cm_id_priv
->service_timeout
= service_timeout
;
2467 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2468 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2471 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2475 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2480 EXPORT_SYMBOL(ib_send_cm_mra
);
2482 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2484 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2485 case CM_MSG_RESPONSE_REQ
:
2486 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2487 case CM_MSG_RESPONSE_REP
:
2488 case CM_MSG_RESPONSE_OTHER
:
2489 return cm_acquire_id(mra_msg
->remote_comm_id
,
2490 mra_msg
->local_comm_id
);
2496 static int cm_mra_handler(struct cm_work
*work
)
2498 struct cm_id_private
*cm_id_priv
;
2499 struct cm_mra_msg
*mra_msg
;
2502 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2503 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2507 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2508 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2509 cm_mra_get_service_timeout(mra_msg
);
2510 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2511 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2513 spin_lock_irq(&cm_id_priv
->lock
);
2514 switch (cm_id_priv
->id
.state
) {
2515 case IB_CM_REQ_SENT
:
2516 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2517 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2518 cm_id_priv
->msg
, timeout
))
2520 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2522 case IB_CM_REP_SENT
:
2523 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2524 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2525 cm_id_priv
->msg
, timeout
))
2527 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2529 case IB_CM_ESTABLISHED
:
2530 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2531 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2532 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2533 cm_id_priv
->msg
, timeout
)) {
2534 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2535 atomic_long_inc(&work
->port
->
2536 counter_group
[CM_RECV_DUPLICATES
].
2537 counter
[CM_MRA_COUNTER
]);
2540 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2542 case IB_CM_MRA_REQ_RCVD
:
2543 case IB_CM_MRA_REP_RCVD
:
2544 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2545 counter
[CM_MRA_COUNTER
]);
2551 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2552 cm_id_priv
->id
.state
;
2553 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2555 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2556 spin_unlock_irq(&cm_id_priv
->lock
);
2559 cm_process_work(cm_id_priv
, work
);
2561 cm_deref_id(cm_id_priv
);
2564 spin_unlock_irq(&cm_id_priv
->lock
);
2565 cm_deref_id(cm_id_priv
);
2569 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2570 struct cm_id_private
*cm_id_priv
,
2571 struct ib_sa_path_rec
*alternate_path
,
2572 const void *private_data
,
2573 u8 private_data_len
)
2575 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2576 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2577 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2578 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2579 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2580 /* todo: need remote CM response timeout */
2581 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2582 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2583 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2584 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2585 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2586 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2587 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2588 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2589 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2590 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2591 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2592 cm_lap_set_local_ack_timeout(lap_msg
,
2593 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2594 alternate_path
->packet_life_time
));
2596 if (private_data
&& private_data_len
)
2597 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2600 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2601 struct ib_sa_path_rec
*alternate_path
,
2602 const void *private_data
,
2603 u8 private_data_len
)
2605 struct cm_id_private
*cm_id_priv
;
2606 struct ib_mad_send_buf
*msg
;
2607 unsigned long flags
;
2610 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2613 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2614 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2615 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2616 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2617 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2622 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2625 cm_id_priv
->alt_av
.timeout
=
2626 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2627 cm_id_priv
->alt_av
.timeout
- 1);
2629 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2633 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2634 alternate_path
, private_data
, private_data_len
);
2635 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2636 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2638 ret
= ib_post_send_mad(msg
, NULL
);
2640 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2645 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2646 cm_id_priv
->msg
= msg
;
2648 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2651 EXPORT_SYMBOL(ib_send_cm_lap
);
2653 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2654 struct ib_sa_path_rec
*path
,
2655 struct cm_lap_msg
*lap_msg
)
2657 memset(path
, 0, sizeof *path
);
2658 path
->dgid
= lap_msg
->alt_local_gid
;
2659 path
->sgid
= lap_msg
->alt_remote_gid
;
2660 path
->dlid
= lap_msg
->alt_local_lid
;
2661 path
->slid
= lap_msg
->alt_remote_lid
;
2662 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2663 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2664 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2665 path
->reversible
= 1;
2666 path
->pkey
= cm_id_priv
->pkey
;
2667 path
->sl
= cm_lap_get_sl(lap_msg
);
2668 path
->mtu_selector
= IB_SA_EQ
;
2669 path
->mtu
= cm_id_priv
->path_mtu
;
2670 path
->rate_selector
= IB_SA_EQ
;
2671 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2672 path
->packet_life_time_selector
= IB_SA_EQ
;
2673 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2674 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2677 static int cm_lap_handler(struct cm_work
*work
)
2679 struct cm_id_private
*cm_id_priv
;
2680 struct cm_lap_msg
*lap_msg
;
2681 struct ib_cm_lap_event_param
*param
;
2682 struct ib_mad_send_buf
*msg
= NULL
;
2685 /* todo: verify LAP request and send reject APR if invalid. */
2686 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2687 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2688 lap_msg
->local_comm_id
);
2692 param
= &work
->cm_event
.param
.lap_rcvd
;
2693 param
->alternate_path
= &work
->path
[0];
2694 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2695 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2697 spin_lock_irq(&cm_id_priv
->lock
);
2698 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2701 switch (cm_id_priv
->id
.lap_state
) {
2702 case IB_CM_LAP_UNINIT
:
2703 case IB_CM_LAP_IDLE
:
2705 case IB_CM_MRA_LAP_SENT
:
2706 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2707 counter
[CM_LAP_COUNTER
]);
2708 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2711 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2712 CM_MSG_RESPONSE_OTHER
,
2713 cm_id_priv
->service_timeout
,
2714 cm_id_priv
->private_data
,
2715 cm_id_priv
->private_data_len
);
2716 spin_unlock_irq(&cm_id_priv
->lock
);
2718 if (ib_post_send_mad(msg
, NULL
))
2721 case IB_CM_LAP_RCVD
:
2722 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2723 counter
[CM_LAP_COUNTER
]);
2729 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2730 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2731 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2732 work
->mad_recv_wc
->recv_buf
.grh
,
2734 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2735 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2737 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2738 spin_unlock_irq(&cm_id_priv
->lock
);
2741 cm_process_work(cm_id_priv
, work
);
2743 cm_deref_id(cm_id_priv
);
2746 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2747 deref
: cm_deref_id(cm_id_priv
);
2751 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2752 struct cm_id_private
*cm_id_priv
,
2753 enum ib_cm_apr_status status
,
2756 const void *private_data
,
2757 u8 private_data_len
)
2759 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2760 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2761 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2762 apr_msg
->ap_status
= (u8
) status
;
2764 if (info
&& info_length
) {
2765 apr_msg
->info_length
= info_length
;
2766 memcpy(apr_msg
->info
, info
, info_length
);
2769 if (private_data
&& private_data_len
)
2770 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2773 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2774 enum ib_cm_apr_status status
,
2777 const void *private_data
,
2778 u8 private_data_len
)
2780 struct cm_id_private
*cm_id_priv
;
2781 struct ib_mad_send_buf
*msg
;
2782 unsigned long flags
;
2785 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2786 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2789 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2790 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2791 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2792 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2793 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2798 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2802 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2803 info
, info_length
, private_data
, private_data_len
);
2804 ret
= ib_post_send_mad(msg
, NULL
);
2806 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2811 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2812 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2815 EXPORT_SYMBOL(ib_send_cm_apr
);
2817 static int cm_apr_handler(struct cm_work
*work
)
2819 struct cm_id_private
*cm_id_priv
;
2820 struct cm_apr_msg
*apr_msg
;
2823 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2824 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2825 apr_msg
->local_comm_id
);
2827 return -EINVAL
; /* Unmatched reply. */
2829 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2830 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2831 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2832 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2834 spin_lock_irq(&cm_id_priv
->lock
);
2835 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2836 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2837 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2838 spin_unlock_irq(&cm_id_priv
->lock
);
2841 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2842 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2843 cm_id_priv
->msg
= NULL
;
2845 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2847 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2848 spin_unlock_irq(&cm_id_priv
->lock
);
2851 cm_process_work(cm_id_priv
, work
);
2853 cm_deref_id(cm_id_priv
);
2856 cm_deref_id(cm_id_priv
);
2860 static int cm_timewait_handler(struct cm_work
*work
)
2862 struct cm_timewait_info
*timewait_info
;
2863 struct cm_id_private
*cm_id_priv
;
2866 timewait_info
= (struct cm_timewait_info
*)work
;
2867 spin_lock_irq(&cm
.lock
);
2868 list_del(&timewait_info
->list
);
2869 spin_unlock_irq(&cm
.lock
);
2871 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2872 timewait_info
->work
.remote_id
);
2876 spin_lock_irq(&cm_id_priv
->lock
);
2877 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2878 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2879 spin_unlock_irq(&cm_id_priv
->lock
);
2882 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2883 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2885 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2886 spin_unlock_irq(&cm_id_priv
->lock
);
2889 cm_process_work(cm_id_priv
, work
);
2891 cm_deref_id(cm_id_priv
);
2894 cm_deref_id(cm_id_priv
);
2898 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2899 struct cm_id_private
*cm_id_priv
,
2900 struct ib_cm_sidr_req_param
*param
)
2902 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2903 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2904 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2905 sidr_req_msg
->pkey
= param
->path
->pkey
;
2906 sidr_req_msg
->service_id
= param
->service_id
;
2908 if (param
->private_data
&& param
->private_data_len
)
2909 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2910 param
->private_data_len
);
2913 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2914 struct ib_cm_sidr_req_param
*param
)
2916 struct cm_id_private
*cm_id_priv
;
2917 struct ib_mad_send_buf
*msg
;
2918 unsigned long flags
;
2921 if (!param
->path
|| (param
->private_data
&&
2922 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2925 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2926 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2930 cm_id
->service_id
= param
->service_id
;
2931 cm_id
->service_mask
= ~cpu_to_be64(0);
2932 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2933 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2934 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2938 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2940 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2941 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2943 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2944 if (cm_id
->state
== IB_CM_IDLE
)
2945 ret
= ib_post_send_mad(msg
, NULL
);
2950 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2954 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2955 cm_id_priv
->msg
= msg
;
2956 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2960 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2962 static void cm_format_sidr_req_event(struct cm_work
*work
,
2963 struct ib_cm_id
*listen_id
)
2965 struct cm_sidr_req_msg
*sidr_req_msg
;
2966 struct ib_cm_sidr_req_event_param
*param
;
2968 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2969 work
->mad_recv_wc
->recv_buf
.mad
;
2970 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2971 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2972 param
->listen_id
= listen_id
;
2973 param
->port
= work
->port
->port_num
;
2974 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2977 static int cm_sidr_req_handler(struct cm_work
*work
)
2979 struct ib_cm_id
*cm_id
;
2980 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2981 struct cm_sidr_req_msg
*sidr_req_msg
;
2984 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
2986 return PTR_ERR(cm_id
);
2987 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2989 /* Record SGID/SLID and request ID for lookup. */
2990 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2991 work
->mad_recv_wc
->recv_buf
.mad
;
2992 wc
= work
->mad_recv_wc
->wc
;
2993 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2994 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2995 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2996 work
->mad_recv_wc
->recv_buf
.grh
,
2998 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2999 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3000 atomic_inc(&cm_id_priv
->work_count
);
3002 spin_lock_irq(&cm
.lock
);
3003 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3004 if (cur_cm_id_priv
) {
3005 spin_unlock_irq(&cm
.lock
);
3006 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3007 counter
[CM_SIDR_REQ_COUNTER
]);
3008 goto out
; /* Duplicate message. */
3010 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3011 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3012 sidr_req_msg
->service_id
,
3013 sidr_req_msg
->private_data
);
3014 if (!cur_cm_id_priv
) {
3015 spin_unlock_irq(&cm
.lock
);
3016 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3017 goto out
; /* No match. */
3019 atomic_inc(&cur_cm_id_priv
->refcount
);
3020 atomic_inc(&cm_id_priv
->refcount
);
3021 spin_unlock_irq(&cm
.lock
);
3023 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3024 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3025 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3026 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3028 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3029 cm_process_work(cm_id_priv
, work
);
3030 cm_deref_id(cur_cm_id_priv
);
3033 ib_destroy_cm_id(&cm_id_priv
->id
);
3037 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3038 struct cm_id_private
*cm_id_priv
,
3039 struct ib_cm_sidr_rep_param
*param
)
3041 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3043 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3044 sidr_rep_msg
->status
= param
->status
;
3045 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3046 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3047 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3049 if (param
->info
&& param
->info_length
)
3050 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3052 if (param
->private_data
&& param
->private_data_len
)
3053 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3054 param
->private_data_len
);
3057 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3058 struct ib_cm_sidr_rep_param
*param
)
3060 struct cm_id_private
*cm_id_priv
;
3061 struct ib_mad_send_buf
*msg
;
3062 unsigned long flags
;
3065 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3066 (param
->private_data
&&
3067 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3070 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3071 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3072 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3077 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3081 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3083 ret
= ib_post_send_mad(msg
, NULL
);
3085 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3089 cm_id
->state
= IB_CM_IDLE
;
3090 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3092 spin_lock_irqsave(&cm
.lock
, flags
);
3093 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3094 spin_unlock_irqrestore(&cm
.lock
, flags
);
3097 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3100 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3102 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3104 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3105 struct ib_cm_sidr_rep_event_param
*param
;
3107 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3108 work
->mad_recv_wc
->recv_buf
.mad
;
3109 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3110 param
->status
= sidr_rep_msg
->status
;
3111 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3112 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3113 param
->info
= &sidr_rep_msg
->info
;
3114 param
->info_len
= sidr_rep_msg
->info_length
;
3115 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3118 static int cm_sidr_rep_handler(struct cm_work
*work
)
3120 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3121 struct cm_id_private
*cm_id_priv
;
3123 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3124 work
->mad_recv_wc
->recv_buf
.mad
;
3125 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3127 return -EINVAL
; /* Unmatched reply. */
3129 spin_lock_irq(&cm_id_priv
->lock
);
3130 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3131 spin_unlock_irq(&cm_id_priv
->lock
);
3134 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3135 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3136 spin_unlock_irq(&cm_id_priv
->lock
);
3138 cm_format_sidr_rep_event(work
);
3139 cm_process_work(cm_id_priv
, work
);
3142 cm_deref_id(cm_id_priv
);
3146 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3147 enum ib_wc_status wc_status
)
3149 struct cm_id_private
*cm_id_priv
;
3150 struct ib_cm_event cm_event
;
3151 enum ib_cm_state state
;
3154 memset(&cm_event
, 0, sizeof cm_event
);
3155 cm_id_priv
= msg
->context
[0];
3157 /* Discard old sends or ones without a response. */
3158 spin_lock_irq(&cm_id_priv
->lock
);
3159 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3160 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3164 case IB_CM_REQ_SENT
:
3165 case IB_CM_MRA_REQ_RCVD
:
3166 cm_reset_to_idle(cm_id_priv
);
3167 cm_event
.event
= IB_CM_REQ_ERROR
;
3169 case IB_CM_REP_SENT
:
3170 case IB_CM_MRA_REP_RCVD
:
3171 cm_reset_to_idle(cm_id_priv
);
3172 cm_event
.event
= IB_CM_REP_ERROR
;
3174 case IB_CM_DREQ_SENT
:
3175 cm_enter_timewait(cm_id_priv
);
3176 cm_event
.event
= IB_CM_DREQ_ERROR
;
3178 case IB_CM_SIDR_REQ_SENT
:
3179 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3180 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3185 spin_unlock_irq(&cm_id_priv
->lock
);
3186 cm_event
.param
.send_status
= wc_status
;
3188 /* No other events can occur on the cm_id at this point. */
3189 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3192 ib_destroy_cm_id(&cm_id_priv
->id
);
3195 spin_unlock_irq(&cm_id_priv
->lock
);
3199 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3200 struct ib_mad_send_wc
*mad_send_wc
)
3202 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3203 struct cm_port
*port
;
3206 port
= mad_agent
->context
;
3207 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3208 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3211 * If the send was in response to a received message (context[0] is not
3212 * set to a cm_id), and is not a REJ, then it is a send that was
3215 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3218 atomic_long_add(1 + msg
->retries
,
3219 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3221 atomic_long_add(msg
->retries
,
3222 &port
->counter_group
[CM_XMIT_RETRIES
].
3223 counter
[attr_index
]);
3225 switch (mad_send_wc
->status
) {
3227 case IB_WC_WR_FLUSH_ERR
:
3231 if (msg
->context
[0] && msg
->context
[1])
3232 cm_process_send_error(msg
, mad_send_wc
->status
);
3239 static void cm_work_handler(struct work_struct
*_work
)
3241 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3244 switch (work
->cm_event
.event
) {
3245 case IB_CM_REQ_RECEIVED
:
3246 ret
= cm_req_handler(work
);
3248 case IB_CM_MRA_RECEIVED
:
3249 ret
= cm_mra_handler(work
);
3251 case IB_CM_REJ_RECEIVED
:
3252 ret
= cm_rej_handler(work
);
3254 case IB_CM_REP_RECEIVED
:
3255 ret
= cm_rep_handler(work
);
3257 case IB_CM_RTU_RECEIVED
:
3258 ret
= cm_rtu_handler(work
);
3260 case IB_CM_USER_ESTABLISHED
:
3261 ret
= cm_establish_handler(work
);
3263 case IB_CM_DREQ_RECEIVED
:
3264 ret
= cm_dreq_handler(work
);
3266 case IB_CM_DREP_RECEIVED
:
3267 ret
= cm_drep_handler(work
);
3269 case IB_CM_SIDR_REQ_RECEIVED
:
3270 ret
= cm_sidr_req_handler(work
);
3272 case IB_CM_SIDR_REP_RECEIVED
:
3273 ret
= cm_sidr_rep_handler(work
);
3275 case IB_CM_LAP_RECEIVED
:
3276 ret
= cm_lap_handler(work
);
3278 case IB_CM_APR_RECEIVED
:
3279 ret
= cm_apr_handler(work
);
3281 case IB_CM_TIMEWAIT_EXIT
:
3282 ret
= cm_timewait_handler(work
);
3292 static int cm_establish(struct ib_cm_id
*cm_id
)
3294 struct cm_id_private
*cm_id_priv
;
3295 struct cm_work
*work
;
3296 unsigned long flags
;
3299 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3303 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3304 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3305 switch (cm_id
->state
)
3307 case IB_CM_REP_SENT
:
3308 case IB_CM_MRA_REP_RCVD
:
3309 cm_id
->state
= IB_CM_ESTABLISHED
;
3311 case IB_CM_ESTABLISHED
:
3318 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3326 * The CM worker thread may try to destroy the cm_id before it
3327 * can execute this work item. To prevent potential deadlock,
3328 * we need to find the cm_id once we're in the context of the
3329 * worker thread, rather than holding a reference on it.
3331 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3332 work
->local_id
= cm_id
->local_id
;
3333 work
->remote_id
= cm_id
->remote_id
;
3334 work
->mad_recv_wc
= NULL
;
3335 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3336 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3341 static int cm_migrate(struct ib_cm_id
*cm_id
)
3343 struct cm_id_private
*cm_id_priv
;
3344 unsigned long flags
;
3347 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3348 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3349 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3350 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3351 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3352 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3353 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3356 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3361 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3366 case IB_EVENT_COMM_EST
:
3367 ret
= cm_establish(cm_id
);
3369 case IB_EVENT_PATH_MIG
:
3370 ret
= cm_migrate(cm_id
);
3377 EXPORT_SYMBOL(ib_cm_notify
);
3379 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3380 struct ib_mad_recv_wc
*mad_recv_wc
)
3382 struct cm_port
*port
= mad_agent
->context
;
3383 struct cm_work
*work
;
3384 enum ib_cm_event_type event
;
3388 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3389 case CM_REQ_ATTR_ID
:
3390 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3391 alt_local_lid
!= 0);
3392 event
= IB_CM_REQ_RECEIVED
;
3394 case CM_MRA_ATTR_ID
:
3395 event
= IB_CM_MRA_RECEIVED
;
3397 case CM_REJ_ATTR_ID
:
3398 event
= IB_CM_REJ_RECEIVED
;
3400 case CM_REP_ATTR_ID
:
3401 event
= IB_CM_REP_RECEIVED
;
3403 case CM_RTU_ATTR_ID
:
3404 event
= IB_CM_RTU_RECEIVED
;
3406 case CM_DREQ_ATTR_ID
:
3407 event
= IB_CM_DREQ_RECEIVED
;
3409 case CM_DREP_ATTR_ID
:
3410 event
= IB_CM_DREP_RECEIVED
;
3412 case CM_SIDR_REQ_ATTR_ID
:
3413 event
= IB_CM_SIDR_REQ_RECEIVED
;
3415 case CM_SIDR_REP_ATTR_ID
:
3416 event
= IB_CM_SIDR_REP_RECEIVED
;
3418 case CM_LAP_ATTR_ID
:
3420 event
= IB_CM_LAP_RECEIVED
;
3422 case CM_APR_ATTR_ID
:
3423 event
= IB_CM_APR_RECEIVED
;
3426 ib_free_recv_mad(mad_recv_wc
);
3430 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3431 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3432 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3434 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3437 ib_free_recv_mad(mad_recv_wc
);
3441 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3442 work
->cm_event
.event
= event
;
3443 work
->mad_recv_wc
= mad_recv_wc
;
3445 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3448 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3449 struct ib_qp_attr
*qp_attr
,
3452 unsigned long flags
;
3455 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3456 switch (cm_id_priv
->id
.state
) {
3457 case IB_CM_REQ_SENT
:
3458 case IB_CM_MRA_REQ_RCVD
:
3459 case IB_CM_REQ_RCVD
:
3460 case IB_CM_MRA_REQ_SENT
:
3461 case IB_CM_REP_RCVD
:
3462 case IB_CM_MRA_REP_SENT
:
3463 case IB_CM_REP_SENT
:
3464 case IB_CM_MRA_REP_RCVD
:
3465 case IB_CM_ESTABLISHED
:
3466 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3467 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3468 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3469 if (cm_id_priv
->responder_resources
)
3470 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3471 IB_ACCESS_REMOTE_ATOMIC
;
3472 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3473 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3480 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3484 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3485 struct ib_qp_attr
*qp_attr
,
3488 unsigned long flags
;
3491 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3492 switch (cm_id_priv
->id
.state
) {
3493 case IB_CM_REQ_RCVD
:
3494 case IB_CM_MRA_REQ_SENT
:
3495 case IB_CM_REP_RCVD
:
3496 case IB_CM_MRA_REP_SENT
:
3497 case IB_CM_REP_SENT
:
3498 case IB_CM_MRA_REP_RCVD
:
3499 case IB_CM_ESTABLISHED
:
3500 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3501 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3502 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3503 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3504 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3505 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3506 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3507 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3508 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3509 IB_QP_MIN_RNR_TIMER
;
3510 qp_attr
->max_dest_rd_atomic
=
3511 cm_id_priv
->responder_resources
;
3512 qp_attr
->min_rnr_timer
= 0;
3514 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3515 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3516 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3517 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3518 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3519 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3527 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3531 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3532 struct ib_qp_attr
*qp_attr
,
3535 unsigned long flags
;
3538 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3539 switch (cm_id_priv
->id
.state
) {
3540 /* Allow transition to RTS before sending REP */
3541 case IB_CM_REQ_RCVD
:
3542 case IB_CM_MRA_REQ_SENT
:
3544 case IB_CM_REP_RCVD
:
3545 case IB_CM_MRA_REP_SENT
:
3546 case IB_CM_REP_SENT
:
3547 case IB_CM_MRA_REP_RCVD
:
3548 case IB_CM_ESTABLISHED
:
3549 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3550 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3551 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3552 switch (cm_id_priv
->qp_type
) {
3554 case IB_QPT_XRC_INI
:
3555 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3556 IB_QP_MAX_QP_RD_ATOMIC
;
3557 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3558 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3559 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3561 case IB_QPT_XRC_TGT
:
3562 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3563 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3568 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3569 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3570 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3573 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3574 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3575 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3576 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3577 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3578 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3586 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3590 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3591 struct ib_qp_attr
*qp_attr
,
3594 struct cm_id_private
*cm_id_priv
;
3597 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3598 switch (qp_attr
->qp_state
) {
3600 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3603 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3606 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3614 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3616 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3618 struct ib_device_attr attr
;
3620 if (ib_query_device(cm_dev
->ib_device
, &attr
))
3621 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3623 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3626 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3629 struct cm_counter_group
*group
;
3630 struct cm_counter_attribute
*cm_attr
;
3632 group
= container_of(obj
, struct cm_counter_group
, obj
);
3633 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3635 return sprintf(buf
, "%ld\n",
3636 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3639 static const struct sysfs_ops cm_counter_ops
= {
3640 .show
= cm_show_counter
3643 static struct kobj_type cm_counter_obj_type
= {
3644 .sysfs_ops
= &cm_counter_ops
,
3645 .default_attrs
= cm_counter_default_attrs
3648 static void cm_release_port_obj(struct kobject
*obj
)
3650 struct cm_port
*cm_port
;
3652 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3656 static struct kobj_type cm_port_obj_type
= {
3657 .release
= cm_release_port_obj
3660 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3664 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3667 struct class cm_class
= {
3668 .owner
= THIS_MODULE
,
3669 .name
= "infiniband_cm",
3670 .devnode
= cm_devnode
,
3672 EXPORT_SYMBOL(cm_class
);
3674 static int cm_create_port_fs(struct cm_port
*port
)
3678 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3679 &port
->cm_dev
->device
->kobj
,
3680 "%d", port
->port_num
);
3686 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3687 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3688 &cm_counter_obj_type
,
3690 "%s", counter_group_names
[i
]);
3699 kobject_put(&port
->counter_group
[i
].obj
);
3700 kobject_put(&port
->port_obj
);
3705 static void cm_remove_port_fs(struct cm_port
*port
)
3709 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3710 kobject_put(&port
->counter_group
[i
].obj
);
3712 kobject_put(&port
->port_obj
);
3715 static void cm_add_one(struct ib_device
*ib_device
)
3717 struct cm_device
*cm_dev
;
3718 struct cm_port
*port
;
3719 struct ib_mad_reg_req reg_req
= {
3720 .mgmt_class
= IB_MGMT_CLASS_CM
,
3721 .mgmt_class_version
= IB_CM_CLASS_VERSION
3723 struct ib_port_modify port_modify
= {
3724 .set_port_cap_mask
= IB_PORT_CM_SUP
3726 unsigned long flags
;
3730 if (rdma_node_get_transport(ib_device
->node_type
) != RDMA_TRANSPORT_IB
)
3733 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3734 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3738 cm_dev
->ib_device
= ib_device
;
3739 cm_get_ack_delay(cm_dev
);
3741 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3743 "%s", ib_device
->name
);
3744 if (IS_ERR(cm_dev
->device
)) {
3749 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3750 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3751 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3755 cm_dev
->port
[i
-1] = port
;
3756 port
->cm_dev
= cm_dev
;
3759 ret
= cm_create_port_fs(port
);
3763 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3770 if (IS_ERR(port
->mad_agent
))
3773 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3777 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3779 write_lock_irqsave(&cm
.device_lock
, flags
);
3780 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3781 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3785 ib_unregister_mad_agent(port
->mad_agent
);
3787 cm_remove_port_fs(port
);
3789 port_modify
.set_port_cap_mask
= 0;
3790 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3792 port
= cm_dev
->port
[i
-1];
3793 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3794 ib_unregister_mad_agent(port
->mad_agent
);
3795 cm_remove_port_fs(port
);
3797 device_unregister(cm_dev
->device
);
3801 static void cm_remove_one(struct ib_device
*ib_device
)
3803 struct cm_device
*cm_dev
;
3804 struct cm_port
*port
;
3805 struct ib_port_modify port_modify
= {
3806 .clr_port_cap_mask
= IB_PORT_CM_SUP
3808 unsigned long flags
;
3811 cm_dev
= ib_get_client_data(ib_device
, &cm_client
);
3815 write_lock_irqsave(&cm
.device_lock
, flags
);
3816 list_del(&cm_dev
->list
);
3817 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3819 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3820 port
= cm_dev
->port
[i
-1];
3821 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3822 ib_unregister_mad_agent(port
->mad_agent
);
3823 flush_workqueue(cm
.wq
);
3824 cm_remove_port_fs(port
);
3826 device_unregister(cm_dev
->device
);
3830 static int __init
ib_cm_init(void)
3834 memset(&cm
, 0, sizeof cm
);
3835 INIT_LIST_HEAD(&cm
.device_list
);
3836 rwlock_init(&cm
.device_lock
);
3837 spin_lock_init(&cm
.lock
);
3838 cm
.listen_service_table
= RB_ROOT
;
3839 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3840 cm
.remote_id_table
= RB_ROOT
;
3841 cm
.remote_qp_table
= RB_ROOT
;
3842 cm
.remote_sidr_table
= RB_ROOT
;
3843 idr_init(&cm
.local_id_table
);
3844 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3845 INIT_LIST_HEAD(&cm
.timewait_list
);
3847 ret
= class_register(&cm_class
);
3853 cm
.wq
= create_workqueue("ib_cm");
3859 ret
= ib_register_client(&cm_client
);
3865 destroy_workqueue(cm
.wq
);
3867 class_unregister(&cm_class
);
3869 idr_destroy(&cm
.local_id_table
);
3873 static void __exit
ib_cm_cleanup(void)
3875 struct cm_timewait_info
*timewait_info
, *tmp
;
3877 spin_lock_irq(&cm
.lock
);
3878 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3879 cancel_delayed_work(&timewait_info
->work
.work
);
3880 spin_unlock_irq(&cm
.lock
);
3882 ib_unregister_client(&cm_client
);
3883 destroy_workqueue(cm
.wq
);
3885 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3886 list_del(&timewait_info
->list
);
3887 kfree(timewait_info
);
3890 class_unregister(&cm_class
);
3891 idr_destroy(&cm
.local_id_table
);
3894 module_init(ib_cm_init
);
3895 module_exit(ib_cm_cleanup
);