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/err.h>
40 #include <linux/idr.h>
41 #include <linux/interrupt.h>
42 #include <linux/random.h>
43 #include <linux/rbtree.h>
44 #include <linux/spinlock.h>
45 #include <linux/slab.h>
46 #include <linux/sysfs.h>
47 #include <linux/workqueue.h>
48 #include <linux/kdev_t.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_cm.h>
54 MODULE_AUTHOR("Sean Hefty");
55 MODULE_DESCRIPTION("InfiniBand CM");
56 MODULE_LICENSE("Dual BSD/GPL");
58 static void cm_add_one(struct ib_device
*device
);
59 static void cm_remove_one(struct ib_device
*device
);
61 static struct ib_client cm_client
= {
64 .remove
= cm_remove_one
69 struct list_head device_list
;
71 struct rb_root listen_service_table
;
72 u64 listen_service_id
;
73 /* struct rb_root peer_service_table; todo: fix peer to peer */
74 struct rb_root remote_qp_table
;
75 struct rb_root remote_id_table
;
76 struct rb_root remote_sidr_table
;
77 struct idr local_id_table
;
78 __be32 random_id_operand
;
79 struct list_head timewait_list
;
80 struct workqueue_struct
*wq
;
83 /* Counter indexes ordered by attribute ID */
97 CM_ATTR_ID_OFFSET
= 0x0010,
108 static char const counter_group_names
[CM_COUNTER_GROUPS
]
109 [sizeof("cm_rx_duplicates")] = {
110 "cm_tx_msgs", "cm_tx_retries",
111 "cm_rx_msgs", "cm_rx_duplicates"
114 struct cm_counter_group
{
116 atomic_long_t counter
[CM_ATTR_COUNT
];
119 struct cm_counter_attribute
{
120 struct attribute attr
;
124 #define CM_COUNTER_ATTR(_name, _index) \
125 struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444 }, \
130 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
131 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
132 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
133 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
134 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
135 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
136 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
137 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
138 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
139 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
140 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
142 static struct attribute
*cm_counter_default_attrs
[] = {
143 &cm_req_counter_attr
.attr
,
144 &cm_mra_counter_attr
.attr
,
145 &cm_rej_counter_attr
.attr
,
146 &cm_rep_counter_attr
.attr
,
147 &cm_rtu_counter_attr
.attr
,
148 &cm_dreq_counter_attr
.attr
,
149 &cm_drep_counter_attr
.attr
,
150 &cm_sidr_req_counter_attr
.attr
,
151 &cm_sidr_rep_counter_attr
.attr
,
152 &cm_lap_counter_attr
.attr
,
153 &cm_apr_counter_attr
.attr
,
158 struct cm_device
*cm_dev
;
159 struct ib_mad_agent
*mad_agent
;
160 struct kobject port_obj
;
162 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
166 struct list_head list
;
167 struct ib_device
*ib_device
;
168 struct device
*device
;
170 struct cm_port
*port
[0];
174 struct cm_port
*port
;
176 struct ib_ah_attr ah_attr
;
182 struct delayed_work work
;
183 struct list_head list
;
184 struct cm_port
*port
;
185 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
186 __be32 local_id
; /* Established / timewait */
188 struct ib_cm_event cm_event
;
189 struct ib_sa_path_rec path
[0];
192 struct cm_timewait_info
{
193 struct cm_work work
; /* Must be first. */
194 struct list_head list
;
195 struct rb_node remote_qp_node
;
196 struct rb_node remote_id_node
;
197 __be64 remote_ca_guid
;
199 u8 inserted_remote_qp
;
200 u8 inserted_remote_id
;
203 struct cm_id_private
{
206 struct rb_node service_node
;
207 struct rb_node sidr_id_node
;
208 spinlock_t lock
; /* Do not acquire inside cm.lock */
209 struct completion comp
;
212 struct ib_mad_send_buf
*msg
;
213 struct cm_timewait_info
*timewait_info
;
214 /* todo: use alternate port on send failure */
217 struct ib_cm_compare_data
*compare_data
;
223 enum ib_qp_type qp_type
;
227 enum ib_mtu path_mtu
;
232 u8 responder_resources
;
239 struct list_head work_list
;
243 static void cm_work_handler(struct work_struct
*work
);
245 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
247 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
248 complete(&cm_id_priv
->comp
);
251 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
252 struct ib_mad_send_buf
**msg
)
254 struct ib_mad_agent
*mad_agent
;
255 struct ib_mad_send_buf
*m
;
258 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
259 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
263 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
264 cm_id_priv
->av
.pkey_index
,
265 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
272 /* Timeout set by caller if response is expected. */
274 m
->retries
= cm_id_priv
->max_cm_retries
;
276 atomic_inc(&cm_id_priv
->refcount
);
277 m
->context
[0] = cm_id_priv
;
282 static int cm_alloc_response_msg(struct cm_port
*port
,
283 struct ib_mad_recv_wc
*mad_recv_wc
,
284 struct ib_mad_send_buf
**msg
)
286 struct ib_mad_send_buf
*m
;
289 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
290 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
294 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
295 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
306 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
308 ib_destroy_ah(msg
->ah
);
310 cm_deref_id(msg
->context
[0]);
311 ib_free_send_mad(msg
);
314 static void * cm_copy_private_data(const void *private_data
,
319 if (!private_data
|| !private_data_len
)
322 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
324 return ERR_PTR(-ENOMEM
);
329 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
330 void *private_data
, u8 private_data_len
)
332 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
333 kfree(cm_id_priv
->private_data
);
335 cm_id_priv
->private_data
= private_data
;
336 cm_id_priv
->private_data_len
= private_data_len
;
339 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
340 struct ib_grh
*grh
, struct cm_av
*av
)
343 av
->pkey_index
= wc
->pkey_index
;
344 ib_init_ah_from_wc(port
->cm_dev
->ib_device
, port
->port_num
, wc
,
348 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
350 struct cm_device
*cm_dev
;
351 struct cm_port
*port
= NULL
;
356 read_lock_irqsave(&cm
.device_lock
, flags
);
357 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
358 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
360 port
= cm_dev
->port
[p
-1];
364 read_unlock_irqrestore(&cm
.device_lock
, flags
);
369 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
370 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
375 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
377 av
->timeout
= path
->packet_life_time
+ 1;
381 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
388 spin_lock_irqsave(&cm
.lock
, flags
);
389 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
,
392 next_id
= ((unsigned) id
+ 1) & MAX_ID_MASK
;
393 spin_unlock_irqrestore(&cm
.lock
, flags
);
394 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
396 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
400 static void cm_free_id(__be32 local_id
)
402 spin_lock_irq(&cm
.lock
);
403 idr_remove(&cm
.local_id_table
,
404 (__force
int) (local_id
^ cm
.random_id_operand
));
405 spin_unlock_irq(&cm
.lock
);
408 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
410 struct cm_id_private
*cm_id_priv
;
412 cm_id_priv
= idr_find(&cm
.local_id_table
,
413 (__force
int) (local_id
^ cm
.random_id_operand
));
415 if (cm_id_priv
->id
.remote_id
== remote_id
)
416 atomic_inc(&cm_id_priv
->refcount
);
424 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
426 struct cm_id_private
*cm_id_priv
;
428 spin_lock_irq(&cm
.lock
);
429 cm_id_priv
= cm_get_id(local_id
, remote_id
);
430 spin_unlock_irq(&cm
.lock
);
435 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
439 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
440 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
441 ((unsigned long *) mask
)[i
];
444 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
445 struct ib_cm_compare_data
*dst_data
)
447 u8 src
[IB_CM_COMPARE_SIZE
];
448 u8 dst
[IB_CM_COMPARE_SIZE
];
450 if (!src_data
|| !dst_data
)
453 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
454 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
455 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
458 static int cm_compare_private_data(u8
*private_data
,
459 struct ib_cm_compare_data
*dst_data
)
461 u8 src
[IB_CM_COMPARE_SIZE
];
466 cm_mask_copy(src
, private_data
, dst_data
->mask
);
467 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
471 * Trivial helpers to strip endian annotation and compare; the
472 * endianness doesn't actually matter since we just need a stable
473 * order for the RB tree.
475 static int be32_lt(__be32 a
, __be32 b
)
477 return (__force u32
) a
< (__force u32
) b
;
480 static int be32_gt(__be32 a
, __be32 b
)
482 return (__force u32
) a
> (__force u32
) b
;
485 static int be64_lt(__be64 a
, __be64 b
)
487 return (__force u64
) a
< (__force u64
) b
;
490 static int be64_gt(__be64 a
, __be64 b
)
492 return (__force u64
) a
> (__force u64
) b
;
495 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
497 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
498 struct rb_node
*parent
= NULL
;
499 struct cm_id_private
*cur_cm_id_priv
;
500 __be64 service_id
= cm_id_priv
->id
.service_id
;
501 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
506 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
508 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
509 cur_cm_id_priv
->compare_data
);
510 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
511 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
512 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
514 return cur_cm_id_priv
;
516 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
517 link
= &(*link
)->rb_left
;
518 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
519 link
= &(*link
)->rb_right
;
520 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
521 link
= &(*link
)->rb_left
;
522 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
523 link
= &(*link
)->rb_right
;
524 else if (data_cmp
< 0)
525 link
= &(*link
)->rb_left
;
527 link
= &(*link
)->rb_right
;
529 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
530 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
534 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
538 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
539 struct cm_id_private
*cm_id_priv
;
543 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
544 data_cmp
= cm_compare_private_data(private_data
,
545 cm_id_priv
->compare_data
);
546 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
547 cm_id_priv
->id
.service_id
&&
548 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
551 if (device
< cm_id_priv
->id
.device
)
552 node
= node
->rb_left
;
553 else if (device
> cm_id_priv
->id
.device
)
554 node
= node
->rb_right
;
555 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
556 node
= node
->rb_left
;
557 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
558 node
= node
->rb_right
;
559 else if (data_cmp
< 0)
560 node
= node
->rb_left
;
562 node
= node
->rb_right
;
567 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
570 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
571 struct rb_node
*parent
= NULL
;
572 struct cm_timewait_info
*cur_timewait_info
;
573 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
574 __be32 remote_id
= timewait_info
->work
.remote_id
;
578 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
580 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
581 link
= &(*link
)->rb_left
;
582 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
583 link
= &(*link
)->rb_right
;
584 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
585 link
= &(*link
)->rb_left
;
586 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
587 link
= &(*link
)->rb_right
;
589 return cur_timewait_info
;
591 timewait_info
->inserted_remote_id
= 1;
592 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
593 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
597 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
600 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
601 struct cm_timewait_info
*timewait_info
;
604 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
606 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
607 node
= node
->rb_left
;
608 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
609 node
= node
->rb_right
;
610 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
611 node
= node
->rb_left
;
612 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
613 node
= node
->rb_right
;
615 return timewait_info
;
620 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
623 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
624 struct rb_node
*parent
= NULL
;
625 struct cm_timewait_info
*cur_timewait_info
;
626 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
627 __be32 remote_qpn
= timewait_info
->remote_qpn
;
631 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
633 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
634 link
= &(*link
)->rb_left
;
635 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
636 link
= &(*link
)->rb_right
;
637 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
638 link
= &(*link
)->rb_left
;
639 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
640 link
= &(*link
)->rb_right
;
642 return cur_timewait_info
;
644 timewait_info
->inserted_remote_qp
= 1;
645 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
646 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
650 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
653 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
654 struct rb_node
*parent
= NULL
;
655 struct cm_id_private
*cur_cm_id_priv
;
656 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
657 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
661 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
663 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
664 link
= &(*link
)->rb_left
;
665 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
666 link
= &(*link
)->rb_right
;
669 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
672 link
= &(*link
)->rb_left
;
674 link
= &(*link
)->rb_right
;
676 return cur_cm_id_priv
;
679 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
680 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
684 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
685 enum ib_cm_sidr_status status
)
687 struct ib_cm_sidr_rep_param param
;
689 memset(¶m
, 0, sizeof param
);
690 param
.status
= status
;
691 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
694 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
695 ib_cm_handler cm_handler
,
698 struct cm_id_private
*cm_id_priv
;
701 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
703 return ERR_PTR(-ENOMEM
);
705 cm_id_priv
->id
.state
= IB_CM_IDLE
;
706 cm_id_priv
->id
.device
= device
;
707 cm_id_priv
->id
.cm_handler
= cm_handler
;
708 cm_id_priv
->id
.context
= context
;
709 cm_id_priv
->id
.remote_cm_qpn
= 1;
710 ret
= cm_alloc_id(cm_id_priv
);
714 spin_lock_init(&cm_id_priv
->lock
);
715 init_completion(&cm_id_priv
->comp
);
716 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
717 atomic_set(&cm_id_priv
->work_count
, -1);
718 atomic_set(&cm_id_priv
->refcount
, 1);
719 return &cm_id_priv
->id
;
723 return ERR_PTR(-ENOMEM
);
725 EXPORT_SYMBOL(ib_create_cm_id
);
727 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
729 struct cm_work
*work
;
731 if (list_empty(&cm_id_priv
->work_list
))
734 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
735 list_del(&work
->list
);
739 static void cm_free_work(struct cm_work
*work
)
741 if (work
->mad_recv_wc
)
742 ib_free_recv_mad(work
->mad_recv_wc
);
746 static inline int cm_convert_to_ms(int iba_time
)
748 /* approximate conversion to ms from 4.096us x 2^iba_time */
749 return 1 << max(iba_time
- 8, 0);
753 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
754 * Because of how ack_timeout is stored, adding one doubles the timeout.
755 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
756 * increment it (round up) only if the other is within 50%.
758 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
760 int ack_timeout
= packet_life_time
+ 1;
762 if (ack_timeout
>= ca_ack_delay
)
763 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
765 ack_timeout
= ca_ack_delay
+
766 (ack_timeout
>= (ca_ack_delay
- 1));
768 return min(31, ack_timeout
);
771 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
773 if (timewait_info
->inserted_remote_id
) {
774 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
775 timewait_info
->inserted_remote_id
= 0;
778 if (timewait_info
->inserted_remote_qp
) {
779 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
780 timewait_info
->inserted_remote_qp
= 0;
784 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
786 struct cm_timewait_info
*timewait_info
;
788 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
790 return ERR_PTR(-ENOMEM
);
792 timewait_info
->work
.local_id
= local_id
;
793 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
794 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
795 return timewait_info
;
798 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
803 spin_lock_irqsave(&cm
.lock
, flags
);
804 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
805 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
806 spin_unlock_irqrestore(&cm
.lock
, flags
);
809 * The cm_id could be destroyed by the user before we exit timewait.
810 * To protect against this, we search for the cm_id after exiting
811 * timewait before notifying the user that we've exited timewait.
813 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
814 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
815 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
816 msecs_to_jiffies(wait_time
));
817 cm_id_priv
->timewait_info
= NULL
;
820 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
824 cm_id_priv
->id
.state
= IB_CM_IDLE
;
825 if (cm_id_priv
->timewait_info
) {
826 spin_lock_irqsave(&cm
.lock
, flags
);
827 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
828 spin_unlock_irqrestore(&cm
.lock
, flags
);
829 kfree(cm_id_priv
->timewait_info
);
830 cm_id_priv
->timewait_info
= NULL
;
834 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
836 struct cm_id_private
*cm_id_priv
;
837 struct cm_work
*work
;
839 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
841 spin_lock_irq(&cm_id_priv
->lock
);
842 switch (cm_id
->state
) {
844 cm_id
->state
= IB_CM_IDLE
;
845 spin_unlock_irq(&cm_id_priv
->lock
);
846 spin_lock_irq(&cm
.lock
);
847 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
848 spin_unlock_irq(&cm
.lock
);
850 case IB_CM_SIDR_REQ_SENT
:
851 cm_id
->state
= IB_CM_IDLE
;
852 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
853 spin_unlock_irq(&cm_id_priv
->lock
);
855 case IB_CM_SIDR_REQ_RCVD
:
856 spin_unlock_irq(&cm_id_priv
->lock
);
857 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
860 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
861 spin_unlock_irq(&cm_id_priv
->lock
);
862 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
863 &cm_id_priv
->id
.device
->node_guid
,
864 sizeof cm_id_priv
->id
.device
->node_guid
,
868 if (err
== -ENOMEM
) {
869 /* Do not reject to allow future retries. */
870 cm_reset_to_idle(cm_id_priv
);
871 spin_unlock_irq(&cm_id_priv
->lock
);
873 spin_unlock_irq(&cm_id_priv
->lock
);
874 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
878 case IB_CM_MRA_REQ_RCVD
:
880 case IB_CM_MRA_REP_RCVD
:
881 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
883 case IB_CM_MRA_REQ_SENT
:
885 case IB_CM_MRA_REP_SENT
:
886 spin_unlock_irq(&cm_id_priv
->lock
);
887 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
890 case IB_CM_ESTABLISHED
:
891 spin_unlock_irq(&cm_id_priv
->lock
);
892 ib_send_cm_dreq(cm_id
, NULL
, 0);
894 case IB_CM_DREQ_SENT
:
895 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
896 cm_enter_timewait(cm_id_priv
);
897 spin_unlock_irq(&cm_id_priv
->lock
);
899 case IB_CM_DREQ_RCVD
:
900 spin_unlock_irq(&cm_id_priv
->lock
);
901 ib_send_cm_drep(cm_id
, NULL
, 0);
904 spin_unlock_irq(&cm_id_priv
->lock
);
908 cm_free_id(cm_id
->local_id
);
909 cm_deref_id(cm_id_priv
);
910 wait_for_completion(&cm_id_priv
->comp
);
911 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
913 kfree(cm_id_priv
->compare_data
);
914 kfree(cm_id_priv
->private_data
);
918 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
920 cm_destroy_id(cm_id
, 0);
922 EXPORT_SYMBOL(ib_destroy_cm_id
);
924 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
925 struct ib_cm_compare_data
*compare_data
)
927 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
931 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
932 service_id
&= service_mask
;
933 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
934 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
937 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
938 if (cm_id
->state
!= IB_CM_IDLE
)
942 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
944 if (!cm_id_priv
->compare_data
)
946 cm_mask_copy(cm_id_priv
->compare_data
->data
,
947 compare_data
->data
, compare_data
->mask
);
948 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
952 cm_id
->state
= IB_CM_LISTEN
;
954 spin_lock_irqsave(&cm
.lock
, flags
);
955 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
956 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
957 cm_id
->service_mask
= ~cpu_to_be64(0);
959 cm_id
->service_id
= service_id
;
960 cm_id
->service_mask
= service_mask
;
962 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
963 spin_unlock_irqrestore(&cm
.lock
, flags
);
965 if (cur_cm_id_priv
) {
966 cm_id
->state
= IB_CM_IDLE
;
967 kfree(cm_id_priv
->compare_data
);
968 cm_id_priv
->compare_data
= NULL
;
973 EXPORT_SYMBOL(ib_cm_listen
);
975 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
976 enum cm_msg_sequence msg_seq
)
980 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
981 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
983 return cpu_to_be64(hi_tid
| low_tid
);
986 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
987 __be16 attr_id
, __be64 tid
)
989 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
990 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
991 hdr
->class_version
= IB_CM_CLASS_VERSION
;
992 hdr
->method
= IB_MGMT_METHOD_SEND
;
993 hdr
->attr_id
= attr_id
;
997 static void cm_format_req(struct cm_req_msg
*req_msg
,
998 struct cm_id_private
*cm_id_priv
,
999 struct ib_cm_req_param
*param
)
1001 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1002 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1004 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1005 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1007 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1008 req_msg
->service_id
= param
->service_id
;
1009 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1010 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1011 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
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 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1021 req_msg
->pkey
= param
->primary_path
->pkey
;
1022 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1023 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1024 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1025 cm_req_set_srq(req_msg
, param
->srq
);
1027 if (pri_path
->hop_limit
<= 1) {
1028 req_msg
->primary_local_lid
= pri_path
->slid
;
1029 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1031 /* Work-around until there's a way to obtain remote LID info */
1032 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1033 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1035 req_msg
->primary_local_gid
= pri_path
->sgid
;
1036 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1037 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1038 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1039 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1040 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1041 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1042 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1043 cm_req_set_primary_local_ack_timeout(req_msg
,
1044 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1045 pri_path
->packet_life_time
));
1048 if (alt_path
->hop_limit
<= 1) {
1049 req_msg
->alt_local_lid
= alt_path
->slid
;
1050 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1052 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1053 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1055 req_msg
->alt_local_gid
= alt_path
->sgid
;
1056 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1057 cm_req_set_alt_flow_label(req_msg
,
1058 alt_path
->flow_label
);
1059 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1060 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1061 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1062 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1063 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1064 cm_req_set_alt_local_ack_timeout(req_msg
,
1065 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1066 alt_path
->packet_life_time
));
1069 if (param
->private_data
&& param
->private_data_len
)
1070 memcpy(req_msg
->private_data
, param
->private_data
,
1071 param
->private_data_len
);
1074 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1076 /* peer-to-peer not supported */
1077 if (param
->peer_to_peer
)
1080 if (!param
->primary_path
)
1083 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
1086 if (param
->private_data
&&
1087 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1090 if (param
->alternate_path
&&
1091 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1092 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1098 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1099 struct ib_cm_req_param
*param
)
1101 struct cm_id_private
*cm_id_priv
;
1102 struct cm_req_msg
*req_msg
;
1103 unsigned long flags
;
1106 ret
= cm_validate_req_param(param
);
1110 /* Verify that we're not in timewait. */
1111 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1112 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1113 if (cm_id
->state
!= IB_CM_IDLE
) {
1114 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1118 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1120 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1122 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1123 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1127 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1130 if (param
->alternate_path
) {
1131 ret
= cm_init_av_by_path(param
->alternate_path
,
1132 &cm_id_priv
->alt_av
);
1136 cm_id
->service_id
= param
->service_id
;
1137 cm_id
->service_mask
= ~cpu_to_be64(0);
1138 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1139 param
->primary_path
->packet_life_time
) * 2 +
1141 param
->remote_cm_response_timeout
);
1142 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1143 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1144 cm_id_priv
->responder_resources
= param
->responder_resources
;
1145 cm_id_priv
->retry_count
= param
->retry_count
;
1146 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1147 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1148 cm_id_priv
->qp_type
= param
->qp_type
;
1150 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1154 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1155 cm_format_req(req_msg
, cm_id_priv
, param
);
1156 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1157 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1158 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1160 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1161 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1163 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1164 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1166 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1169 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1170 cm_id
->state
= IB_CM_REQ_SENT
;
1171 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1174 error2
: cm_free_msg(cm_id_priv
->msg
);
1175 error1
: kfree(cm_id_priv
->timewait_info
);
1178 EXPORT_SYMBOL(ib_send_cm_req
);
1180 static int cm_issue_rej(struct cm_port
*port
,
1181 struct ib_mad_recv_wc
*mad_recv_wc
,
1182 enum ib_cm_rej_reason reason
,
1183 enum cm_msg_response msg_rejected
,
1184 void *ari
, u8 ari_length
)
1186 struct ib_mad_send_buf
*msg
= NULL
;
1187 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1190 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1194 /* We just need common CM header information. Cast to any message. */
1195 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1196 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1198 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1199 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1200 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1201 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1202 rej_msg
->reason
= cpu_to_be16(reason
);
1204 if (ari
&& ari_length
) {
1205 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1206 memcpy(rej_msg
->ari
, ari
, ari_length
);
1209 ret
= ib_post_send_mad(msg
, NULL
);
1216 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1217 __be32 local_qpn
, __be32 remote_qpn
)
1219 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1220 ((local_ca_guid
== remote_ca_guid
) &&
1221 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1224 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1225 struct ib_sa_path_rec
*primary_path
,
1226 struct ib_sa_path_rec
*alt_path
)
1228 memset(primary_path
, 0, sizeof *primary_path
);
1229 primary_path
->dgid
= req_msg
->primary_local_gid
;
1230 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1231 primary_path
->dlid
= req_msg
->primary_local_lid
;
1232 primary_path
->slid
= req_msg
->primary_remote_lid
;
1233 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1234 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1235 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1236 primary_path
->reversible
= 1;
1237 primary_path
->pkey
= req_msg
->pkey
;
1238 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1239 primary_path
->mtu_selector
= IB_SA_EQ
;
1240 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1241 primary_path
->rate_selector
= IB_SA_EQ
;
1242 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1243 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1244 primary_path
->packet_life_time
=
1245 cm_req_get_primary_local_ack_timeout(req_msg
);
1246 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1248 if (req_msg
->alt_local_lid
) {
1249 memset(alt_path
, 0, sizeof *alt_path
);
1250 alt_path
->dgid
= req_msg
->alt_local_gid
;
1251 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1252 alt_path
->dlid
= req_msg
->alt_local_lid
;
1253 alt_path
->slid
= req_msg
->alt_remote_lid
;
1254 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1255 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1256 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1257 alt_path
->reversible
= 1;
1258 alt_path
->pkey
= req_msg
->pkey
;
1259 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1260 alt_path
->mtu_selector
= IB_SA_EQ
;
1261 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1262 alt_path
->rate_selector
= IB_SA_EQ
;
1263 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1264 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1265 alt_path
->packet_life_time
=
1266 cm_req_get_alt_local_ack_timeout(req_msg
);
1267 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1271 static void cm_format_req_event(struct cm_work
*work
,
1272 struct cm_id_private
*cm_id_priv
,
1273 struct ib_cm_id
*listen_id
)
1275 struct cm_req_msg
*req_msg
;
1276 struct ib_cm_req_event_param
*param
;
1278 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1279 param
= &work
->cm_event
.param
.req_rcvd
;
1280 param
->listen_id
= listen_id
;
1281 param
->port
= cm_id_priv
->av
.port
->port_num
;
1282 param
->primary_path
= &work
->path
[0];
1283 if (req_msg
->alt_local_lid
)
1284 param
->alternate_path
= &work
->path
[1];
1286 param
->alternate_path
= NULL
;
1287 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1288 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1289 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1290 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1291 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1292 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1293 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1294 param
->local_cm_response_timeout
=
1295 cm_req_get_remote_resp_timeout(req_msg
);
1296 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1297 param
->remote_cm_response_timeout
=
1298 cm_req_get_local_resp_timeout(req_msg
);
1299 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1300 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1301 param
->srq
= cm_req_get_srq(req_msg
);
1302 work
->cm_event
.private_data
= &req_msg
->private_data
;
1305 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1306 struct cm_work
*work
)
1310 /* We will typically only have the current event to report. */
1311 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1314 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1315 spin_lock_irq(&cm_id_priv
->lock
);
1316 work
= cm_dequeue_work(cm_id_priv
);
1317 spin_unlock_irq(&cm_id_priv
->lock
);
1319 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1323 cm_deref_id(cm_id_priv
);
1325 cm_destroy_id(&cm_id_priv
->id
, ret
);
1328 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1329 struct cm_id_private
*cm_id_priv
,
1330 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1331 const void *private_data
, u8 private_data_len
)
1333 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1334 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1335 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1336 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1337 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1339 if (private_data
&& private_data_len
)
1340 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1343 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1344 struct cm_id_private
*cm_id_priv
,
1345 enum ib_cm_rej_reason reason
,
1348 const void *private_data
,
1349 u8 private_data_len
)
1351 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1352 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1354 switch(cm_id_priv
->id
.state
) {
1355 case IB_CM_REQ_RCVD
:
1356 rej_msg
->local_comm_id
= 0;
1357 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1359 case IB_CM_MRA_REQ_SENT
:
1360 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1361 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1363 case IB_CM_REP_RCVD
:
1364 case IB_CM_MRA_REP_SENT
:
1365 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1366 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1369 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1370 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1374 rej_msg
->reason
= cpu_to_be16(reason
);
1375 if (ari
&& ari_length
) {
1376 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1377 memcpy(rej_msg
->ari
, ari
, ari_length
);
1380 if (private_data
&& private_data_len
)
1381 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1384 static void cm_dup_req_handler(struct cm_work
*work
,
1385 struct cm_id_private
*cm_id_priv
)
1387 struct ib_mad_send_buf
*msg
= NULL
;
1390 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1391 counter
[CM_REQ_COUNTER
]);
1393 /* Quick state check to discard duplicate REQs. */
1394 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1397 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1401 spin_lock_irq(&cm_id_priv
->lock
);
1402 switch (cm_id_priv
->id
.state
) {
1403 case IB_CM_MRA_REQ_SENT
:
1404 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1405 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1406 cm_id_priv
->private_data
,
1407 cm_id_priv
->private_data_len
);
1409 case IB_CM_TIMEWAIT
:
1410 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1411 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1416 spin_unlock_irq(&cm_id_priv
->lock
);
1418 ret
= ib_post_send_mad(msg
, NULL
);
1423 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1424 free
: cm_free_msg(msg
);
1427 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1428 struct cm_id_private
*cm_id_priv
)
1430 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1431 struct cm_timewait_info
*timewait_info
;
1432 struct cm_req_msg
*req_msg
;
1434 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1436 /* Check for possible duplicate REQ. */
1437 spin_lock_irq(&cm
.lock
);
1438 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1439 if (timewait_info
) {
1440 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1441 timewait_info
->work
.remote_id
);
1442 spin_unlock_irq(&cm
.lock
);
1443 if (cur_cm_id_priv
) {
1444 cm_dup_req_handler(work
, cur_cm_id_priv
);
1445 cm_deref_id(cur_cm_id_priv
);
1450 /* Check for stale connections. */
1451 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1452 if (timewait_info
) {
1453 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1454 spin_unlock_irq(&cm
.lock
);
1455 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1456 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1461 /* Find matching listen request. */
1462 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1463 req_msg
->service_id
,
1464 req_msg
->private_data
);
1465 if (!listen_cm_id_priv
) {
1466 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1467 spin_unlock_irq(&cm
.lock
);
1468 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1469 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1473 atomic_inc(&listen_cm_id_priv
->refcount
);
1474 atomic_inc(&cm_id_priv
->refcount
);
1475 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1476 atomic_inc(&cm_id_priv
->work_count
);
1477 spin_unlock_irq(&cm
.lock
);
1479 return listen_cm_id_priv
;
1483 * Work-around for inter-subnet connections. If the LIDs are permissive,
1484 * we need to override the LID/SL data in the REQ with the LID information
1485 * in the work completion.
1487 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1489 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1490 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1491 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1492 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1495 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1496 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1499 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1500 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1501 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1502 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1505 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1506 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1510 static int cm_req_handler(struct cm_work
*work
)
1512 struct ib_cm_id
*cm_id
;
1513 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1514 struct cm_req_msg
*req_msg
;
1517 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1519 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1521 return PTR_ERR(cm_id
);
1523 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1524 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1525 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1526 work
->mad_recv_wc
->recv_buf
.grh
,
1528 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1530 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1531 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1534 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1535 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1536 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1538 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1539 if (!listen_cm_id_priv
) {
1541 kfree(cm_id_priv
->timewait_info
);
1545 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1546 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1547 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1548 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1550 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1551 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1552 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1554 ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1555 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1556 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1557 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1561 if (req_msg
->alt_local_lid
) {
1562 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1564 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1565 &work
->path
[0].sgid
,
1566 sizeof work
->path
[0].sgid
, NULL
, 0);
1570 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1571 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1572 cm_req_get_local_resp_timeout(req_msg
));
1573 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1574 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1575 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1576 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1577 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1578 cm_id_priv
->pkey
= req_msg
->pkey
;
1579 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1580 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1581 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1582 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1584 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1585 cm_process_work(cm_id_priv
, work
);
1586 cm_deref_id(listen_cm_id_priv
);
1590 atomic_dec(&cm_id_priv
->refcount
);
1591 cm_deref_id(listen_cm_id_priv
);
1593 ib_destroy_cm_id(cm_id
);
1597 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1598 struct cm_id_private
*cm_id_priv
,
1599 struct ib_cm_rep_param
*param
)
1601 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1602 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1603 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1604 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1605 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1606 rep_msg
->resp_resources
= param
->responder_resources
;
1607 rep_msg
->initiator_depth
= param
->initiator_depth
;
1608 cm_rep_set_target_ack_delay(rep_msg
,
1609 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1610 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1611 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1612 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1613 cm_rep_set_srq(rep_msg
, param
->srq
);
1614 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1616 if (param
->private_data
&& param
->private_data_len
)
1617 memcpy(rep_msg
->private_data
, param
->private_data
,
1618 param
->private_data_len
);
1621 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1622 struct ib_cm_rep_param
*param
)
1624 struct cm_id_private
*cm_id_priv
;
1625 struct ib_mad_send_buf
*msg
;
1626 struct cm_rep_msg
*rep_msg
;
1627 unsigned long flags
;
1630 if (param
->private_data
&&
1631 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1634 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1635 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1636 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1637 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1642 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1646 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1647 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1648 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1649 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1651 ret
= ib_post_send_mad(msg
, NULL
);
1653 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1658 cm_id
->state
= IB_CM_REP_SENT
;
1659 cm_id_priv
->msg
= msg
;
1660 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1661 cm_id_priv
->responder_resources
= param
->responder_resources
;
1662 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1663 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1665 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1668 EXPORT_SYMBOL(ib_send_cm_rep
);
1670 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1671 struct cm_id_private
*cm_id_priv
,
1672 const void *private_data
,
1673 u8 private_data_len
)
1675 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1676 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1677 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1679 if (private_data
&& private_data_len
)
1680 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1683 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1684 const void *private_data
,
1685 u8 private_data_len
)
1687 struct cm_id_private
*cm_id_priv
;
1688 struct ib_mad_send_buf
*msg
;
1689 unsigned long flags
;
1693 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1696 data
= cm_copy_private_data(private_data
, private_data_len
);
1698 return PTR_ERR(data
);
1700 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1701 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1702 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1703 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1708 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1712 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1713 private_data
, private_data_len
);
1715 ret
= ib_post_send_mad(msg
, NULL
);
1717 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1723 cm_id
->state
= IB_CM_ESTABLISHED
;
1724 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1725 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1728 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1732 EXPORT_SYMBOL(ib_send_cm_rtu
);
1734 static void cm_format_rep_event(struct cm_work
*work
)
1736 struct cm_rep_msg
*rep_msg
;
1737 struct ib_cm_rep_event_param
*param
;
1739 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1740 param
= &work
->cm_event
.param
.rep_rcvd
;
1741 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1742 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1743 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1744 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1745 param
->responder_resources
= rep_msg
->initiator_depth
;
1746 param
->initiator_depth
= rep_msg
->resp_resources
;
1747 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1748 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1749 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1750 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1751 param
->srq
= cm_rep_get_srq(rep_msg
);
1752 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1755 static void cm_dup_rep_handler(struct cm_work
*work
)
1757 struct cm_id_private
*cm_id_priv
;
1758 struct cm_rep_msg
*rep_msg
;
1759 struct ib_mad_send_buf
*msg
= NULL
;
1762 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1763 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1764 rep_msg
->local_comm_id
);
1768 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1769 counter
[CM_REP_COUNTER
]);
1770 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1774 spin_lock_irq(&cm_id_priv
->lock
);
1775 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1776 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1777 cm_id_priv
->private_data
,
1778 cm_id_priv
->private_data_len
);
1779 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1780 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1781 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1782 cm_id_priv
->private_data
,
1783 cm_id_priv
->private_data_len
);
1786 spin_unlock_irq(&cm_id_priv
->lock
);
1788 ret
= ib_post_send_mad(msg
, NULL
);
1793 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1794 free
: cm_free_msg(msg
);
1795 deref
: cm_deref_id(cm_id_priv
);
1798 static int cm_rep_handler(struct cm_work
*work
)
1800 struct cm_id_private
*cm_id_priv
;
1801 struct cm_rep_msg
*rep_msg
;
1804 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1805 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1807 cm_dup_rep_handler(work
);
1811 cm_format_rep_event(work
);
1813 spin_lock_irq(&cm_id_priv
->lock
);
1814 switch (cm_id_priv
->id
.state
) {
1815 case IB_CM_REQ_SENT
:
1816 case IB_CM_MRA_REQ_RCVD
:
1819 spin_unlock_irq(&cm_id_priv
->lock
);
1824 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1825 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1826 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1828 spin_lock(&cm
.lock
);
1829 /* Check for duplicate REP. */
1830 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1831 spin_unlock(&cm
.lock
);
1832 spin_unlock_irq(&cm_id_priv
->lock
);
1836 /* Check for a stale connection. */
1837 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1838 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1839 &cm
.remote_id_table
);
1840 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1841 spin_unlock(&cm
.lock
);
1842 spin_unlock_irq(&cm_id_priv
->lock
);
1843 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1844 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1849 spin_unlock(&cm
.lock
);
1851 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1852 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1853 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1854 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1855 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1856 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1857 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1858 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1859 cm_id_priv
->av
.timeout
=
1860 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1861 cm_id_priv
->av
.timeout
- 1);
1862 cm_id_priv
->alt_av
.timeout
=
1863 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1864 cm_id_priv
->alt_av
.timeout
- 1);
1866 /* todo: handle peer_to_peer */
1868 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1869 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1871 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1872 spin_unlock_irq(&cm_id_priv
->lock
);
1875 cm_process_work(cm_id_priv
, work
);
1877 cm_deref_id(cm_id_priv
);
1881 cm_deref_id(cm_id_priv
);
1885 static int cm_establish_handler(struct cm_work
*work
)
1887 struct cm_id_private
*cm_id_priv
;
1890 /* See comment in cm_establish about lookup. */
1891 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1895 spin_lock_irq(&cm_id_priv
->lock
);
1896 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1897 spin_unlock_irq(&cm_id_priv
->lock
);
1901 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1902 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1904 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1905 spin_unlock_irq(&cm_id_priv
->lock
);
1908 cm_process_work(cm_id_priv
, work
);
1910 cm_deref_id(cm_id_priv
);
1913 cm_deref_id(cm_id_priv
);
1917 static int cm_rtu_handler(struct cm_work
*work
)
1919 struct cm_id_private
*cm_id_priv
;
1920 struct cm_rtu_msg
*rtu_msg
;
1923 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1924 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1925 rtu_msg
->local_comm_id
);
1929 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1931 spin_lock_irq(&cm_id_priv
->lock
);
1932 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1933 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1934 spin_unlock_irq(&cm_id_priv
->lock
);
1935 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1936 counter
[CM_RTU_COUNTER
]);
1939 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1941 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1942 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1944 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1945 spin_unlock_irq(&cm_id_priv
->lock
);
1948 cm_process_work(cm_id_priv
, work
);
1950 cm_deref_id(cm_id_priv
);
1953 cm_deref_id(cm_id_priv
);
1957 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1958 struct cm_id_private
*cm_id_priv
,
1959 const void *private_data
,
1960 u8 private_data_len
)
1962 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1963 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1964 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1965 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1966 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1968 if (private_data
&& private_data_len
)
1969 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1972 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1973 const void *private_data
,
1974 u8 private_data_len
)
1976 struct cm_id_private
*cm_id_priv
;
1977 struct ib_mad_send_buf
*msg
;
1978 unsigned long flags
;
1981 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1984 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1985 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1986 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1991 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
1992 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
1993 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1995 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1997 cm_enter_timewait(cm_id_priv
);
2001 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2002 private_data
, private_data_len
);
2003 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2004 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2006 ret
= ib_post_send_mad(msg
, NULL
);
2008 cm_enter_timewait(cm_id_priv
);
2009 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2014 cm_id
->state
= IB_CM_DREQ_SENT
;
2015 cm_id_priv
->msg
= msg
;
2016 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2019 EXPORT_SYMBOL(ib_send_cm_dreq
);
2021 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2022 struct cm_id_private
*cm_id_priv
,
2023 const void *private_data
,
2024 u8 private_data_len
)
2026 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2027 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2028 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2030 if (private_data
&& private_data_len
)
2031 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2034 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2035 const void *private_data
,
2036 u8 private_data_len
)
2038 struct cm_id_private
*cm_id_priv
;
2039 struct ib_mad_send_buf
*msg
;
2040 unsigned long flags
;
2044 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2047 data
= cm_copy_private_data(private_data
, private_data_len
);
2049 return PTR_ERR(data
);
2051 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2052 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2053 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2054 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2059 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2060 cm_enter_timewait(cm_id_priv
);
2062 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2066 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2067 private_data
, private_data_len
);
2069 ret
= ib_post_send_mad(msg
, NULL
);
2071 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2076 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2079 EXPORT_SYMBOL(ib_send_cm_drep
);
2081 static int cm_issue_drep(struct cm_port
*port
,
2082 struct ib_mad_recv_wc
*mad_recv_wc
)
2084 struct ib_mad_send_buf
*msg
= NULL
;
2085 struct cm_dreq_msg
*dreq_msg
;
2086 struct cm_drep_msg
*drep_msg
;
2089 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2093 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2094 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2096 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2097 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2098 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2100 ret
= ib_post_send_mad(msg
, NULL
);
2107 static int cm_dreq_handler(struct cm_work
*work
)
2109 struct cm_id_private
*cm_id_priv
;
2110 struct cm_dreq_msg
*dreq_msg
;
2111 struct ib_mad_send_buf
*msg
= NULL
;
2114 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2115 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2116 dreq_msg
->local_comm_id
);
2118 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2119 counter
[CM_DREQ_COUNTER
]);
2120 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2124 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2126 spin_lock_irq(&cm_id_priv
->lock
);
2127 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2130 switch (cm_id_priv
->id
.state
) {
2131 case IB_CM_REP_SENT
:
2132 case IB_CM_DREQ_SENT
:
2133 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2135 case IB_CM_ESTABLISHED
:
2136 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2137 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2138 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2140 case IB_CM_MRA_REP_RCVD
:
2142 case IB_CM_TIMEWAIT
:
2143 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2144 counter
[CM_DREQ_COUNTER
]);
2145 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2148 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2149 cm_id_priv
->private_data
,
2150 cm_id_priv
->private_data_len
);
2151 spin_unlock_irq(&cm_id_priv
->lock
);
2153 if (ib_post_send_mad(msg
, NULL
))
2156 case IB_CM_DREQ_RCVD
:
2157 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2158 counter
[CM_DREQ_COUNTER
]);
2163 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2164 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2165 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2167 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2168 spin_unlock_irq(&cm_id_priv
->lock
);
2171 cm_process_work(cm_id_priv
, work
);
2173 cm_deref_id(cm_id_priv
);
2176 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2177 deref
: cm_deref_id(cm_id_priv
);
2181 static int cm_drep_handler(struct cm_work
*work
)
2183 struct cm_id_private
*cm_id_priv
;
2184 struct cm_drep_msg
*drep_msg
;
2187 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2188 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2189 drep_msg
->local_comm_id
);
2193 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2195 spin_lock_irq(&cm_id_priv
->lock
);
2196 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2197 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2198 spin_unlock_irq(&cm_id_priv
->lock
);
2201 cm_enter_timewait(cm_id_priv
);
2203 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2204 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2206 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2207 spin_unlock_irq(&cm_id_priv
->lock
);
2210 cm_process_work(cm_id_priv
, work
);
2212 cm_deref_id(cm_id_priv
);
2215 cm_deref_id(cm_id_priv
);
2219 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2220 enum ib_cm_rej_reason reason
,
2223 const void *private_data
,
2224 u8 private_data_len
)
2226 struct cm_id_private
*cm_id_priv
;
2227 struct ib_mad_send_buf
*msg
;
2228 unsigned long flags
;
2231 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2232 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2235 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2237 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2238 switch (cm_id
->state
) {
2239 case IB_CM_REQ_SENT
:
2240 case IB_CM_MRA_REQ_RCVD
:
2241 case IB_CM_REQ_RCVD
:
2242 case IB_CM_MRA_REQ_SENT
:
2243 case IB_CM_REP_RCVD
:
2244 case IB_CM_MRA_REP_SENT
:
2245 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2247 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2248 cm_id_priv
, reason
, ari
, ari_length
,
2249 private_data
, private_data_len
);
2251 cm_reset_to_idle(cm_id_priv
);
2253 case IB_CM_REP_SENT
:
2254 case IB_CM_MRA_REP_RCVD
:
2255 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2257 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2258 cm_id_priv
, reason
, ari
, ari_length
,
2259 private_data
, private_data_len
);
2261 cm_enter_timewait(cm_id_priv
);
2271 ret
= ib_post_send_mad(msg
, NULL
);
2275 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2278 EXPORT_SYMBOL(ib_send_cm_rej
);
2280 static void cm_format_rej_event(struct cm_work
*work
)
2282 struct cm_rej_msg
*rej_msg
;
2283 struct ib_cm_rej_event_param
*param
;
2285 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2286 param
= &work
->cm_event
.param
.rej_rcvd
;
2287 param
->ari
= rej_msg
->ari
;
2288 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2289 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2290 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2293 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2295 struct cm_timewait_info
*timewait_info
;
2296 struct cm_id_private
*cm_id_priv
;
2299 remote_id
= rej_msg
->local_comm_id
;
2301 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2302 spin_lock_irq(&cm
.lock
);
2303 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2305 if (!timewait_info
) {
2306 spin_unlock_irq(&cm
.lock
);
2309 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2310 (timewait_info
->work
.local_id
^
2311 cm
.random_id_operand
));
2313 if (cm_id_priv
->id
.remote_id
== remote_id
)
2314 atomic_inc(&cm_id_priv
->refcount
);
2318 spin_unlock_irq(&cm
.lock
);
2319 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2320 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2322 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2327 static int cm_rej_handler(struct cm_work
*work
)
2329 struct cm_id_private
*cm_id_priv
;
2330 struct cm_rej_msg
*rej_msg
;
2333 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2334 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2338 cm_format_rej_event(work
);
2340 spin_lock_irq(&cm_id_priv
->lock
);
2341 switch (cm_id_priv
->id
.state
) {
2342 case IB_CM_REQ_SENT
:
2343 case IB_CM_MRA_REQ_RCVD
:
2344 case IB_CM_REP_SENT
:
2345 case IB_CM_MRA_REP_RCVD
:
2346 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2348 case IB_CM_REQ_RCVD
:
2349 case IB_CM_MRA_REQ_SENT
:
2350 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2351 cm_enter_timewait(cm_id_priv
);
2353 cm_reset_to_idle(cm_id_priv
);
2355 case IB_CM_DREQ_SENT
:
2356 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2358 case IB_CM_REP_RCVD
:
2359 case IB_CM_MRA_REP_SENT
:
2360 cm_enter_timewait(cm_id_priv
);
2362 case IB_CM_ESTABLISHED
:
2363 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2364 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2365 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2366 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2368 cm_enter_timewait(cm_id_priv
);
2373 spin_unlock_irq(&cm_id_priv
->lock
);
2378 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2380 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2381 spin_unlock_irq(&cm_id_priv
->lock
);
2384 cm_process_work(cm_id_priv
, work
);
2386 cm_deref_id(cm_id_priv
);
2389 cm_deref_id(cm_id_priv
);
2393 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2395 const void *private_data
,
2396 u8 private_data_len
)
2398 struct cm_id_private
*cm_id_priv
;
2399 struct ib_mad_send_buf
*msg
;
2400 enum ib_cm_state cm_state
;
2401 enum ib_cm_lap_state lap_state
;
2402 enum cm_msg_response msg_response
;
2404 unsigned long flags
;
2407 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2410 data
= cm_copy_private_data(private_data
, private_data_len
);
2412 return PTR_ERR(data
);
2414 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2416 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2417 switch(cm_id_priv
->id
.state
) {
2418 case IB_CM_REQ_RCVD
:
2419 cm_state
= IB_CM_MRA_REQ_SENT
;
2420 lap_state
= cm_id
->lap_state
;
2421 msg_response
= CM_MSG_RESPONSE_REQ
;
2423 case IB_CM_REP_RCVD
:
2424 cm_state
= IB_CM_MRA_REP_SENT
;
2425 lap_state
= cm_id
->lap_state
;
2426 msg_response
= CM_MSG_RESPONSE_REP
;
2428 case IB_CM_ESTABLISHED
:
2429 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2430 cm_state
= cm_id
->state
;
2431 lap_state
= IB_CM_MRA_LAP_SENT
;
2432 msg_response
= CM_MSG_RESPONSE_OTHER
;
2440 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2441 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2445 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2446 msg_response
, service_timeout
,
2447 private_data
, private_data_len
);
2448 ret
= ib_post_send_mad(msg
, NULL
);
2453 cm_id
->state
= cm_state
;
2454 cm_id
->lap_state
= lap_state
;
2455 cm_id_priv
->service_timeout
= service_timeout
;
2456 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2457 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2460 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2464 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2469 EXPORT_SYMBOL(ib_send_cm_mra
);
2471 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2473 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2474 case CM_MSG_RESPONSE_REQ
:
2475 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2476 case CM_MSG_RESPONSE_REP
:
2477 case CM_MSG_RESPONSE_OTHER
:
2478 return cm_acquire_id(mra_msg
->remote_comm_id
,
2479 mra_msg
->local_comm_id
);
2485 static int cm_mra_handler(struct cm_work
*work
)
2487 struct cm_id_private
*cm_id_priv
;
2488 struct cm_mra_msg
*mra_msg
;
2491 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2492 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2496 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2497 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2498 cm_mra_get_service_timeout(mra_msg
);
2499 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2500 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2502 spin_lock_irq(&cm_id_priv
->lock
);
2503 switch (cm_id_priv
->id
.state
) {
2504 case IB_CM_REQ_SENT
:
2505 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2506 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2507 cm_id_priv
->msg
, timeout
))
2509 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2511 case IB_CM_REP_SENT
:
2512 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2513 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2514 cm_id_priv
->msg
, timeout
))
2516 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2518 case IB_CM_ESTABLISHED
:
2519 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2520 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2521 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2522 cm_id_priv
->msg
, timeout
)) {
2523 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2524 atomic_long_inc(&work
->port
->
2525 counter_group
[CM_RECV_DUPLICATES
].
2526 counter
[CM_MRA_COUNTER
]);
2529 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2531 case IB_CM_MRA_REQ_RCVD
:
2532 case IB_CM_MRA_REP_RCVD
:
2533 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2534 counter
[CM_MRA_COUNTER
]);
2540 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2541 cm_id_priv
->id
.state
;
2542 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2544 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2545 spin_unlock_irq(&cm_id_priv
->lock
);
2548 cm_process_work(cm_id_priv
, work
);
2550 cm_deref_id(cm_id_priv
);
2553 spin_unlock_irq(&cm_id_priv
->lock
);
2554 cm_deref_id(cm_id_priv
);
2558 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2559 struct cm_id_private
*cm_id_priv
,
2560 struct ib_sa_path_rec
*alternate_path
,
2561 const void *private_data
,
2562 u8 private_data_len
)
2564 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2565 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2566 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2567 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2568 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2569 /* todo: need remote CM response timeout */
2570 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2571 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2572 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2573 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2574 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2575 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2576 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2577 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2578 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2579 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2580 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2581 cm_lap_set_local_ack_timeout(lap_msg
,
2582 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2583 alternate_path
->packet_life_time
));
2585 if (private_data
&& private_data_len
)
2586 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2589 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2590 struct ib_sa_path_rec
*alternate_path
,
2591 const void *private_data
,
2592 u8 private_data_len
)
2594 struct cm_id_private
*cm_id_priv
;
2595 struct ib_mad_send_buf
*msg
;
2596 unsigned long flags
;
2599 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2602 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2603 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2604 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2605 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2606 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2611 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2614 cm_id_priv
->alt_av
.timeout
=
2615 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2616 cm_id_priv
->alt_av
.timeout
- 1);
2618 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2622 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2623 alternate_path
, private_data
, private_data_len
);
2624 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2625 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2627 ret
= ib_post_send_mad(msg
, NULL
);
2629 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2634 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2635 cm_id_priv
->msg
= msg
;
2637 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2640 EXPORT_SYMBOL(ib_send_cm_lap
);
2642 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2643 struct ib_sa_path_rec
*path
,
2644 struct cm_lap_msg
*lap_msg
)
2646 memset(path
, 0, sizeof *path
);
2647 path
->dgid
= lap_msg
->alt_local_gid
;
2648 path
->sgid
= lap_msg
->alt_remote_gid
;
2649 path
->dlid
= lap_msg
->alt_local_lid
;
2650 path
->slid
= lap_msg
->alt_remote_lid
;
2651 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2652 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2653 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2654 path
->reversible
= 1;
2655 path
->pkey
= cm_id_priv
->pkey
;
2656 path
->sl
= cm_lap_get_sl(lap_msg
);
2657 path
->mtu_selector
= IB_SA_EQ
;
2658 path
->mtu
= cm_id_priv
->path_mtu
;
2659 path
->rate_selector
= IB_SA_EQ
;
2660 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2661 path
->packet_life_time_selector
= IB_SA_EQ
;
2662 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2663 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2666 static int cm_lap_handler(struct cm_work
*work
)
2668 struct cm_id_private
*cm_id_priv
;
2669 struct cm_lap_msg
*lap_msg
;
2670 struct ib_cm_lap_event_param
*param
;
2671 struct ib_mad_send_buf
*msg
= NULL
;
2674 /* todo: verify LAP request and send reject APR if invalid. */
2675 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2676 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2677 lap_msg
->local_comm_id
);
2681 param
= &work
->cm_event
.param
.lap_rcvd
;
2682 param
->alternate_path
= &work
->path
[0];
2683 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2684 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2686 spin_lock_irq(&cm_id_priv
->lock
);
2687 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2690 switch (cm_id_priv
->id
.lap_state
) {
2691 case IB_CM_LAP_UNINIT
:
2692 case IB_CM_LAP_IDLE
:
2694 case IB_CM_MRA_LAP_SENT
:
2695 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2696 counter
[CM_LAP_COUNTER
]);
2697 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2700 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2701 CM_MSG_RESPONSE_OTHER
,
2702 cm_id_priv
->service_timeout
,
2703 cm_id_priv
->private_data
,
2704 cm_id_priv
->private_data_len
);
2705 spin_unlock_irq(&cm_id_priv
->lock
);
2707 if (ib_post_send_mad(msg
, NULL
))
2710 case IB_CM_LAP_RCVD
:
2711 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2712 counter
[CM_LAP_COUNTER
]);
2718 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2719 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2720 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2721 work
->mad_recv_wc
->recv_buf
.grh
,
2723 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2724 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2726 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2727 spin_unlock_irq(&cm_id_priv
->lock
);
2730 cm_process_work(cm_id_priv
, work
);
2732 cm_deref_id(cm_id_priv
);
2735 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2736 deref
: cm_deref_id(cm_id_priv
);
2740 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2741 struct cm_id_private
*cm_id_priv
,
2742 enum ib_cm_apr_status status
,
2745 const void *private_data
,
2746 u8 private_data_len
)
2748 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2749 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2750 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2751 apr_msg
->ap_status
= (u8
) status
;
2753 if (info
&& info_length
) {
2754 apr_msg
->info_length
= info_length
;
2755 memcpy(apr_msg
->info
, info
, info_length
);
2758 if (private_data
&& private_data_len
)
2759 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2762 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2763 enum ib_cm_apr_status status
,
2766 const void *private_data
,
2767 u8 private_data_len
)
2769 struct cm_id_private
*cm_id_priv
;
2770 struct ib_mad_send_buf
*msg
;
2771 unsigned long flags
;
2774 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2775 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2778 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2779 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2780 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2781 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2782 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2787 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2791 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2792 info
, info_length
, private_data
, private_data_len
);
2793 ret
= ib_post_send_mad(msg
, NULL
);
2795 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2800 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2801 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2804 EXPORT_SYMBOL(ib_send_cm_apr
);
2806 static int cm_apr_handler(struct cm_work
*work
)
2808 struct cm_id_private
*cm_id_priv
;
2809 struct cm_apr_msg
*apr_msg
;
2812 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2813 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2814 apr_msg
->local_comm_id
);
2816 return -EINVAL
; /* Unmatched reply. */
2818 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2819 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2820 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2821 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2823 spin_lock_irq(&cm_id_priv
->lock
);
2824 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2825 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2826 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2827 spin_unlock_irq(&cm_id_priv
->lock
);
2830 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2831 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2832 cm_id_priv
->msg
= NULL
;
2834 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2836 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2837 spin_unlock_irq(&cm_id_priv
->lock
);
2840 cm_process_work(cm_id_priv
, work
);
2842 cm_deref_id(cm_id_priv
);
2845 cm_deref_id(cm_id_priv
);
2849 static int cm_timewait_handler(struct cm_work
*work
)
2851 struct cm_timewait_info
*timewait_info
;
2852 struct cm_id_private
*cm_id_priv
;
2855 timewait_info
= (struct cm_timewait_info
*)work
;
2856 spin_lock_irq(&cm
.lock
);
2857 list_del(&timewait_info
->list
);
2858 spin_unlock_irq(&cm
.lock
);
2860 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2861 timewait_info
->work
.remote_id
);
2865 spin_lock_irq(&cm_id_priv
->lock
);
2866 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2867 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2868 spin_unlock_irq(&cm_id_priv
->lock
);
2871 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2872 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2874 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2875 spin_unlock_irq(&cm_id_priv
->lock
);
2878 cm_process_work(cm_id_priv
, work
);
2880 cm_deref_id(cm_id_priv
);
2883 cm_deref_id(cm_id_priv
);
2887 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2888 struct cm_id_private
*cm_id_priv
,
2889 struct ib_cm_sidr_req_param
*param
)
2891 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2892 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2893 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2894 sidr_req_msg
->pkey
= param
->path
->pkey
;
2895 sidr_req_msg
->service_id
= param
->service_id
;
2897 if (param
->private_data
&& param
->private_data_len
)
2898 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2899 param
->private_data_len
);
2902 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2903 struct ib_cm_sidr_req_param
*param
)
2905 struct cm_id_private
*cm_id_priv
;
2906 struct ib_mad_send_buf
*msg
;
2907 unsigned long flags
;
2910 if (!param
->path
|| (param
->private_data
&&
2911 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2914 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2915 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2919 cm_id
->service_id
= param
->service_id
;
2920 cm_id
->service_mask
= ~cpu_to_be64(0);
2921 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2922 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2923 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2927 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2929 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2930 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2932 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2933 if (cm_id
->state
== IB_CM_IDLE
)
2934 ret
= ib_post_send_mad(msg
, NULL
);
2939 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2943 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2944 cm_id_priv
->msg
= msg
;
2945 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2949 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2951 static void cm_format_sidr_req_event(struct cm_work
*work
,
2952 struct ib_cm_id
*listen_id
)
2954 struct cm_sidr_req_msg
*sidr_req_msg
;
2955 struct ib_cm_sidr_req_event_param
*param
;
2957 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2958 work
->mad_recv_wc
->recv_buf
.mad
;
2959 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2960 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2961 param
->listen_id
= listen_id
;
2962 param
->port
= work
->port
->port_num
;
2963 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2966 static int cm_sidr_req_handler(struct cm_work
*work
)
2968 struct ib_cm_id
*cm_id
;
2969 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2970 struct cm_sidr_req_msg
*sidr_req_msg
;
2973 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
2975 return PTR_ERR(cm_id
);
2976 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2978 /* Record SGID/SLID and request ID for lookup. */
2979 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2980 work
->mad_recv_wc
->recv_buf
.mad
;
2981 wc
= work
->mad_recv_wc
->wc
;
2982 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2983 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2984 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2985 work
->mad_recv_wc
->recv_buf
.grh
,
2987 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2988 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2989 atomic_inc(&cm_id_priv
->work_count
);
2991 spin_lock_irq(&cm
.lock
);
2992 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2993 if (cur_cm_id_priv
) {
2994 spin_unlock_irq(&cm
.lock
);
2995 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2996 counter
[CM_SIDR_REQ_COUNTER
]);
2997 goto out
; /* Duplicate message. */
2999 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3000 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3001 sidr_req_msg
->service_id
,
3002 sidr_req_msg
->private_data
);
3003 if (!cur_cm_id_priv
) {
3004 spin_unlock_irq(&cm
.lock
);
3005 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3006 goto out
; /* No match. */
3008 atomic_inc(&cur_cm_id_priv
->refcount
);
3009 atomic_inc(&cm_id_priv
->refcount
);
3010 spin_unlock_irq(&cm
.lock
);
3012 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3013 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3014 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3015 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3017 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3018 cm_process_work(cm_id_priv
, work
);
3019 cm_deref_id(cur_cm_id_priv
);
3022 ib_destroy_cm_id(&cm_id_priv
->id
);
3026 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3027 struct cm_id_private
*cm_id_priv
,
3028 struct ib_cm_sidr_rep_param
*param
)
3030 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3032 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3033 sidr_rep_msg
->status
= param
->status
;
3034 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3035 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3036 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3038 if (param
->info
&& param
->info_length
)
3039 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3041 if (param
->private_data
&& param
->private_data_len
)
3042 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3043 param
->private_data_len
);
3046 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3047 struct ib_cm_sidr_rep_param
*param
)
3049 struct cm_id_private
*cm_id_priv
;
3050 struct ib_mad_send_buf
*msg
;
3051 unsigned long flags
;
3054 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3055 (param
->private_data
&&
3056 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3059 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3060 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3061 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3066 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3070 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3072 ret
= ib_post_send_mad(msg
, NULL
);
3074 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3078 cm_id
->state
= IB_CM_IDLE
;
3079 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3081 spin_lock_irqsave(&cm
.lock
, flags
);
3082 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3083 spin_unlock_irqrestore(&cm
.lock
, flags
);
3086 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3089 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3091 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3093 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3094 struct ib_cm_sidr_rep_event_param
*param
;
3096 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3097 work
->mad_recv_wc
->recv_buf
.mad
;
3098 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3099 param
->status
= sidr_rep_msg
->status
;
3100 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3101 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3102 param
->info
= &sidr_rep_msg
->info
;
3103 param
->info_len
= sidr_rep_msg
->info_length
;
3104 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3107 static int cm_sidr_rep_handler(struct cm_work
*work
)
3109 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3110 struct cm_id_private
*cm_id_priv
;
3112 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3113 work
->mad_recv_wc
->recv_buf
.mad
;
3114 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3116 return -EINVAL
; /* Unmatched reply. */
3118 spin_lock_irq(&cm_id_priv
->lock
);
3119 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3120 spin_unlock_irq(&cm_id_priv
->lock
);
3123 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3124 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3125 spin_unlock_irq(&cm_id_priv
->lock
);
3127 cm_format_sidr_rep_event(work
);
3128 cm_process_work(cm_id_priv
, work
);
3131 cm_deref_id(cm_id_priv
);
3135 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3136 enum ib_wc_status wc_status
)
3138 struct cm_id_private
*cm_id_priv
;
3139 struct ib_cm_event cm_event
;
3140 enum ib_cm_state state
;
3143 memset(&cm_event
, 0, sizeof cm_event
);
3144 cm_id_priv
= msg
->context
[0];
3146 /* Discard old sends or ones without a response. */
3147 spin_lock_irq(&cm_id_priv
->lock
);
3148 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3149 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3153 case IB_CM_REQ_SENT
:
3154 case IB_CM_MRA_REQ_RCVD
:
3155 cm_reset_to_idle(cm_id_priv
);
3156 cm_event
.event
= IB_CM_REQ_ERROR
;
3158 case IB_CM_REP_SENT
:
3159 case IB_CM_MRA_REP_RCVD
:
3160 cm_reset_to_idle(cm_id_priv
);
3161 cm_event
.event
= IB_CM_REP_ERROR
;
3163 case IB_CM_DREQ_SENT
:
3164 cm_enter_timewait(cm_id_priv
);
3165 cm_event
.event
= IB_CM_DREQ_ERROR
;
3167 case IB_CM_SIDR_REQ_SENT
:
3168 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3169 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3174 spin_unlock_irq(&cm_id_priv
->lock
);
3175 cm_event
.param
.send_status
= wc_status
;
3177 /* No other events can occur on the cm_id at this point. */
3178 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3181 ib_destroy_cm_id(&cm_id_priv
->id
);
3184 spin_unlock_irq(&cm_id_priv
->lock
);
3188 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3189 struct ib_mad_send_wc
*mad_send_wc
)
3191 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3192 struct cm_port
*port
;
3195 port
= mad_agent
->context
;
3196 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3197 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3200 * If the send was in response to a received message (context[0] is not
3201 * set to a cm_id), and is not a REJ, then it is a send that was
3204 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3207 atomic_long_add(1 + msg
->retries
,
3208 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3210 atomic_long_add(msg
->retries
,
3211 &port
->counter_group
[CM_XMIT_RETRIES
].
3212 counter
[attr_index
]);
3214 switch (mad_send_wc
->status
) {
3216 case IB_WC_WR_FLUSH_ERR
:
3220 if (msg
->context
[0] && msg
->context
[1])
3221 cm_process_send_error(msg
, mad_send_wc
->status
);
3228 static void cm_work_handler(struct work_struct
*_work
)
3230 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3233 switch (work
->cm_event
.event
) {
3234 case IB_CM_REQ_RECEIVED
:
3235 ret
= cm_req_handler(work
);
3237 case IB_CM_MRA_RECEIVED
:
3238 ret
= cm_mra_handler(work
);
3240 case IB_CM_REJ_RECEIVED
:
3241 ret
= cm_rej_handler(work
);
3243 case IB_CM_REP_RECEIVED
:
3244 ret
= cm_rep_handler(work
);
3246 case IB_CM_RTU_RECEIVED
:
3247 ret
= cm_rtu_handler(work
);
3249 case IB_CM_USER_ESTABLISHED
:
3250 ret
= cm_establish_handler(work
);
3252 case IB_CM_DREQ_RECEIVED
:
3253 ret
= cm_dreq_handler(work
);
3255 case IB_CM_DREP_RECEIVED
:
3256 ret
= cm_drep_handler(work
);
3258 case IB_CM_SIDR_REQ_RECEIVED
:
3259 ret
= cm_sidr_req_handler(work
);
3261 case IB_CM_SIDR_REP_RECEIVED
:
3262 ret
= cm_sidr_rep_handler(work
);
3264 case IB_CM_LAP_RECEIVED
:
3265 ret
= cm_lap_handler(work
);
3267 case IB_CM_APR_RECEIVED
:
3268 ret
= cm_apr_handler(work
);
3270 case IB_CM_TIMEWAIT_EXIT
:
3271 ret
= cm_timewait_handler(work
);
3281 static int cm_establish(struct ib_cm_id
*cm_id
)
3283 struct cm_id_private
*cm_id_priv
;
3284 struct cm_work
*work
;
3285 unsigned long flags
;
3288 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3292 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3293 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3294 switch (cm_id
->state
)
3296 case IB_CM_REP_SENT
:
3297 case IB_CM_MRA_REP_RCVD
:
3298 cm_id
->state
= IB_CM_ESTABLISHED
;
3300 case IB_CM_ESTABLISHED
:
3307 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3315 * The CM worker thread may try to destroy the cm_id before it
3316 * can execute this work item. To prevent potential deadlock,
3317 * we need to find the cm_id once we're in the context of the
3318 * worker thread, rather than holding a reference on it.
3320 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3321 work
->local_id
= cm_id
->local_id
;
3322 work
->remote_id
= cm_id
->remote_id
;
3323 work
->mad_recv_wc
= NULL
;
3324 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3325 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3330 static int cm_migrate(struct ib_cm_id
*cm_id
)
3332 struct cm_id_private
*cm_id_priv
;
3333 unsigned long flags
;
3336 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3337 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3338 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3339 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3340 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3341 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3342 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3345 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3350 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3355 case IB_EVENT_COMM_EST
:
3356 ret
= cm_establish(cm_id
);
3358 case IB_EVENT_PATH_MIG
:
3359 ret
= cm_migrate(cm_id
);
3366 EXPORT_SYMBOL(ib_cm_notify
);
3368 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3369 struct ib_mad_recv_wc
*mad_recv_wc
)
3371 struct cm_port
*port
= mad_agent
->context
;
3372 struct cm_work
*work
;
3373 enum ib_cm_event_type event
;
3377 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3378 case CM_REQ_ATTR_ID
:
3379 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3380 alt_local_lid
!= 0);
3381 event
= IB_CM_REQ_RECEIVED
;
3383 case CM_MRA_ATTR_ID
:
3384 event
= IB_CM_MRA_RECEIVED
;
3386 case CM_REJ_ATTR_ID
:
3387 event
= IB_CM_REJ_RECEIVED
;
3389 case CM_REP_ATTR_ID
:
3390 event
= IB_CM_REP_RECEIVED
;
3392 case CM_RTU_ATTR_ID
:
3393 event
= IB_CM_RTU_RECEIVED
;
3395 case CM_DREQ_ATTR_ID
:
3396 event
= IB_CM_DREQ_RECEIVED
;
3398 case CM_DREP_ATTR_ID
:
3399 event
= IB_CM_DREP_RECEIVED
;
3401 case CM_SIDR_REQ_ATTR_ID
:
3402 event
= IB_CM_SIDR_REQ_RECEIVED
;
3404 case CM_SIDR_REP_ATTR_ID
:
3405 event
= IB_CM_SIDR_REP_RECEIVED
;
3407 case CM_LAP_ATTR_ID
:
3409 event
= IB_CM_LAP_RECEIVED
;
3411 case CM_APR_ATTR_ID
:
3412 event
= IB_CM_APR_RECEIVED
;
3415 ib_free_recv_mad(mad_recv_wc
);
3419 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3420 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3421 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3423 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3426 ib_free_recv_mad(mad_recv_wc
);
3430 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3431 work
->cm_event
.event
= event
;
3432 work
->mad_recv_wc
= mad_recv_wc
;
3434 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3437 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3438 struct ib_qp_attr
*qp_attr
,
3441 unsigned long flags
;
3444 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3445 switch (cm_id_priv
->id
.state
) {
3446 case IB_CM_REQ_SENT
:
3447 case IB_CM_MRA_REQ_RCVD
:
3448 case IB_CM_REQ_RCVD
:
3449 case IB_CM_MRA_REQ_SENT
:
3450 case IB_CM_REP_RCVD
:
3451 case IB_CM_MRA_REP_SENT
:
3452 case IB_CM_REP_SENT
:
3453 case IB_CM_MRA_REP_RCVD
:
3454 case IB_CM_ESTABLISHED
:
3455 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3456 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3457 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3458 if (cm_id_priv
->responder_resources
)
3459 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3460 IB_ACCESS_REMOTE_ATOMIC
;
3461 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3462 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3469 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3473 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3474 struct ib_qp_attr
*qp_attr
,
3477 unsigned long flags
;
3480 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3481 switch (cm_id_priv
->id
.state
) {
3482 case IB_CM_REQ_RCVD
:
3483 case IB_CM_MRA_REQ_SENT
:
3484 case IB_CM_REP_RCVD
:
3485 case IB_CM_MRA_REP_SENT
:
3486 case IB_CM_REP_SENT
:
3487 case IB_CM_MRA_REP_RCVD
:
3488 case IB_CM_ESTABLISHED
:
3489 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3490 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3491 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3492 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3493 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3494 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3495 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3496 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3497 IB_QP_MIN_RNR_TIMER
;
3498 qp_attr
->max_dest_rd_atomic
=
3499 cm_id_priv
->responder_resources
;
3500 qp_attr
->min_rnr_timer
= 0;
3502 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3503 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3504 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3505 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3506 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3507 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3515 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3519 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3520 struct ib_qp_attr
*qp_attr
,
3523 unsigned long flags
;
3526 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3527 switch (cm_id_priv
->id
.state
) {
3528 /* Allow transition to RTS before sending REP */
3529 case IB_CM_REQ_RCVD
:
3530 case IB_CM_MRA_REQ_SENT
:
3532 case IB_CM_REP_RCVD
:
3533 case IB_CM_MRA_REP_SENT
:
3534 case IB_CM_REP_SENT
:
3535 case IB_CM_MRA_REP_RCVD
:
3536 case IB_CM_ESTABLISHED
:
3537 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3538 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3539 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3540 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3541 *qp_attr_mask
|= IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3543 IB_QP_MAX_QP_RD_ATOMIC
;
3544 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3545 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3546 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3547 qp_attr
->max_rd_atomic
=
3548 cm_id_priv
->initiator_depth
;
3550 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3551 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3552 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3555 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3556 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3557 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3558 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3559 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3560 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3568 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3572 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3573 struct ib_qp_attr
*qp_attr
,
3576 struct cm_id_private
*cm_id_priv
;
3579 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3580 switch (qp_attr
->qp_state
) {
3582 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3585 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3588 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3596 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3598 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3600 struct ib_device_attr attr
;
3602 if (ib_query_device(cm_dev
->ib_device
, &attr
))
3603 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3605 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3608 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3611 struct cm_counter_group
*group
;
3612 struct cm_counter_attribute
*cm_attr
;
3614 group
= container_of(obj
, struct cm_counter_group
, obj
);
3615 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3617 return sprintf(buf
, "%ld\n",
3618 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3621 static const struct sysfs_ops cm_counter_ops
= {
3622 .show
= cm_show_counter
3625 static struct kobj_type cm_counter_obj_type
= {
3626 .sysfs_ops
= &cm_counter_ops
,
3627 .default_attrs
= cm_counter_default_attrs
3630 static void cm_release_port_obj(struct kobject
*obj
)
3632 struct cm_port
*cm_port
;
3634 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3638 static struct kobj_type cm_port_obj_type
= {
3639 .release
= cm_release_port_obj
3642 struct class cm_class
= {
3643 .name
= "infiniband_cm",
3645 EXPORT_SYMBOL(cm_class
);
3647 static int cm_create_port_fs(struct cm_port
*port
)
3651 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3652 &port
->cm_dev
->device
->kobj
,
3653 "%d", port
->port_num
);
3659 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3660 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3661 &cm_counter_obj_type
,
3663 "%s", counter_group_names
[i
]);
3672 kobject_put(&port
->counter_group
[i
].obj
);
3673 kobject_put(&port
->port_obj
);
3678 static void cm_remove_port_fs(struct cm_port
*port
)
3682 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3683 kobject_put(&port
->counter_group
[i
].obj
);
3685 kobject_put(&port
->port_obj
);
3688 static void cm_add_one(struct ib_device
*ib_device
)
3690 struct cm_device
*cm_dev
;
3691 struct cm_port
*port
;
3692 struct ib_mad_reg_req reg_req
= {
3693 .mgmt_class
= IB_MGMT_CLASS_CM
,
3694 .mgmt_class_version
= IB_CM_CLASS_VERSION
3696 struct ib_port_modify port_modify
= {
3697 .set_port_cap_mask
= IB_PORT_CM_SUP
3699 unsigned long flags
;
3703 if (rdma_node_get_transport(ib_device
->node_type
) != RDMA_TRANSPORT_IB
)
3706 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3707 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3711 cm_dev
->ib_device
= ib_device
;
3712 cm_get_ack_delay(cm_dev
);
3714 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3716 "%s", ib_device
->name
);
3717 if (IS_ERR(cm_dev
->device
)) {
3722 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3723 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3724 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3728 cm_dev
->port
[i
-1] = port
;
3729 port
->cm_dev
= cm_dev
;
3732 ret
= cm_create_port_fs(port
);
3736 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3743 if (IS_ERR(port
->mad_agent
))
3746 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3750 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3752 write_lock_irqsave(&cm
.device_lock
, flags
);
3753 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3754 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3758 ib_unregister_mad_agent(port
->mad_agent
);
3760 cm_remove_port_fs(port
);
3762 port_modify
.set_port_cap_mask
= 0;
3763 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3765 port
= cm_dev
->port
[i
-1];
3766 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3767 ib_unregister_mad_agent(port
->mad_agent
);
3768 cm_remove_port_fs(port
);
3770 device_unregister(cm_dev
->device
);
3774 static void cm_remove_one(struct ib_device
*ib_device
)
3776 struct cm_device
*cm_dev
;
3777 struct cm_port
*port
;
3778 struct ib_port_modify port_modify
= {
3779 .clr_port_cap_mask
= IB_PORT_CM_SUP
3781 unsigned long flags
;
3784 cm_dev
= ib_get_client_data(ib_device
, &cm_client
);
3788 write_lock_irqsave(&cm
.device_lock
, flags
);
3789 list_del(&cm_dev
->list
);
3790 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3792 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3793 port
= cm_dev
->port
[i
-1];
3794 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3795 ib_unregister_mad_agent(port
->mad_agent
);
3796 flush_workqueue(cm
.wq
);
3797 cm_remove_port_fs(port
);
3799 device_unregister(cm_dev
->device
);
3803 static int __init
ib_cm_init(void)
3807 memset(&cm
, 0, sizeof cm
);
3808 INIT_LIST_HEAD(&cm
.device_list
);
3809 rwlock_init(&cm
.device_lock
);
3810 spin_lock_init(&cm
.lock
);
3811 cm
.listen_service_table
= RB_ROOT
;
3812 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3813 cm
.remote_id_table
= RB_ROOT
;
3814 cm
.remote_qp_table
= RB_ROOT
;
3815 cm
.remote_sidr_table
= RB_ROOT
;
3816 idr_init(&cm
.local_id_table
);
3817 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3818 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3819 INIT_LIST_HEAD(&cm
.timewait_list
);
3821 ret
= class_register(&cm_class
);
3825 cm
.wq
= create_workqueue("ib_cm");
3831 ret
= ib_register_client(&cm_client
);
3837 destroy_workqueue(cm
.wq
);
3839 class_unregister(&cm_class
);
3843 static void __exit
ib_cm_cleanup(void)
3845 struct cm_timewait_info
*timewait_info
, *tmp
;
3847 spin_lock_irq(&cm
.lock
);
3848 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3849 cancel_delayed_work(&timewait_info
->work
.work
);
3850 spin_unlock_irq(&cm
.lock
);
3852 ib_unregister_client(&cm_client
);
3853 destroy_workqueue(cm
.wq
);
3855 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3856 list_del(&timewait_info
->list
);
3857 kfree(timewait_info
);
3860 class_unregister(&cm_class
);
3861 idr_destroy(&cm
.local_id_table
);
3864 module_init(ib_cm_init
);
3865 module_exit(ib_cm_cleanup
);