2 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/completion.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
50 #include <linux/etherdevice.h>
52 #include <rdma/ib_cache.h>
53 #include <rdma/ib_cm.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("InfiniBand CM");
58 MODULE_LICENSE("Dual BSD/GPL");
60 static void cm_add_one(struct ib_device
*device
);
61 static void cm_remove_one(struct ib_device
*device
);
63 static struct ib_client cm_client
= {
66 .remove
= cm_remove_one
71 struct list_head device_list
;
73 struct rb_root listen_service_table
;
74 u64 listen_service_id
;
75 /* struct rb_root peer_service_table; todo: fix peer to peer */
76 struct rb_root remote_qp_table
;
77 struct rb_root remote_id_table
;
78 struct rb_root remote_sidr_table
;
79 struct idr local_id_table
;
80 __be32 random_id_operand
;
81 struct list_head timewait_list
;
82 struct workqueue_struct
*wq
;
85 /* Counter indexes ordered by attribute ID */
99 CM_ATTR_ID_OFFSET
= 0x0010,
110 static char const counter_group_names
[CM_COUNTER_GROUPS
]
111 [sizeof("cm_rx_duplicates")] = {
112 "cm_tx_msgs", "cm_tx_retries",
113 "cm_rx_msgs", "cm_rx_duplicates"
116 struct cm_counter_group
{
118 atomic_long_t counter
[CM_ATTR_COUNT
];
121 struct cm_counter_attribute
{
122 struct attribute attr
;
126 #define CM_COUNTER_ATTR(_name, _index) \
127 struct cm_counter_attribute cm_##_name##_counter_attr = { \
128 .attr = { .name = __stringify(_name), .mode = 0444 }, \
132 static CM_COUNTER_ATTR(req
, CM_REQ_COUNTER
);
133 static CM_COUNTER_ATTR(mra
, CM_MRA_COUNTER
);
134 static CM_COUNTER_ATTR(rej
, CM_REJ_COUNTER
);
135 static CM_COUNTER_ATTR(rep
, CM_REP_COUNTER
);
136 static CM_COUNTER_ATTR(rtu
, CM_RTU_COUNTER
);
137 static CM_COUNTER_ATTR(dreq
, CM_DREQ_COUNTER
);
138 static CM_COUNTER_ATTR(drep
, CM_DREP_COUNTER
);
139 static CM_COUNTER_ATTR(sidr_req
, CM_SIDR_REQ_COUNTER
);
140 static CM_COUNTER_ATTR(sidr_rep
, CM_SIDR_REP_COUNTER
);
141 static CM_COUNTER_ATTR(lap
, CM_LAP_COUNTER
);
142 static CM_COUNTER_ATTR(apr
, CM_APR_COUNTER
);
144 static struct attribute
*cm_counter_default_attrs
[] = {
145 &cm_req_counter_attr
.attr
,
146 &cm_mra_counter_attr
.attr
,
147 &cm_rej_counter_attr
.attr
,
148 &cm_rep_counter_attr
.attr
,
149 &cm_rtu_counter_attr
.attr
,
150 &cm_dreq_counter_attr
.attr
,
151 &cm_drep_counter_attr
.attr
,
152 &cm_sidr_req_counter_attr
.attr
,
153 &cm_sidr_rep_counter_attr
.attr
,
154 &cm_lap_counter_attr
.attr
,
155 &cm_apr_counter_attr
.attr
,
160 struct cm_device
*cm_dev
;
161 struct ib_mad_agent
*mad_agent
;
162 struct kobject port_obj
;
164 struct cm_counter_group counter_group
[CM_COUNTER_GROUPS
];
168 struct list_head list
;
169 struct ib_device
*ib_device
;
170 struct device
*device
;
172 struct cm_port
*port
[0];
176 struct cm_port
*port
;
178 struct ib_ah_attr ah_attr
;
186 struct delayed_work work
;
187 struct list_head list
;
188 struct cm_port
*port
;
189 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
190 __be32 local_id
; /* Established / timewait */
192 struct ib_cm_event cm_event
;
193 struct ib_sa_path_rec path
[0];
196 struct cm_timewait_info
{
197 struct cm_work work
; /* Must be first. */
198 struct list_head list
;
199 struct rb_node remote_qp_node
;
200 struct rb_node remote_id_node
;
201 __be64 remote_ca_guid
;
203 u8 inserted_remote_qp
;
204 u8 inserted_remote_id
;
207 struct cm_id_private
{
210 struct rb_node service_node
;
211 struct rb_node sidr_id_node
;
212 spinlock_t lock
; /* Do not acquire inside cm.lock */
213 struct completion comp
;
216 struct ib_mad_send_buf
*msg
;
217 struct cm_timewait_info
*timewait_info
;
218 /* todo: use alternate port on send failure */
221 struct ib_cm_compare_data
*compare_data
;
227 enum ib_qp_type qp_type
;
231 enum ib_mtu path_mtu
;
236 u8 responder_resources
;
243 struct list_head work_list
;
247 static void cm_work_handler(struct work_struct
*work
);
249 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
251 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
252 complete(&cm_id_priv
->comp
);
255 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
256 struct ib_mad_send_buf
**msg
)
258 struct ib_mad_agent
*mad_agent
;
259 struct ib_mad_send_buf
*m
;
262 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
263 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
267 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
268 cm_id_priv
->av
.pkey_index
,
269 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
276 /* Timeout set by caller if response is expected. */
278 m
->retries
= cm_id_priv
->max_cm_retries
;
280 atomic_inc(&cm_id_priv
->refcount
);
281 m
->context
[0] = cm_id_priv
;
286 static int cm_alloc_response_msg(struct cm_port
*port
,
287 struct ib_mad_recv_wc
*mad_recv_wc
,
288 struct ib_mad_send_buf
**msg
)
290 struct ib_mad_send_buf
*m
;
293 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
294 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
298 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
299 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
310 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
312 ib_destroy_ah(msg
->ah
);
314 cm_deref_id(msg
->context
[0]);
315 ib_free_send_mad(msg
);
318 static void * cm_copy_private_data(const void *private_data
,
323 if (!private_data
|| !private_data_len
)
326 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
328 return ERR_PTR(-ENOMEM
);
333 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
334 void *private_data
, u8 private_data_len
)
336 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
337 kfree(cm_id_priv
->private_data
);
339 cm_id_priv
->private_data
= private_data
;
340 cm_id_priv
->private_data_len
= private_data_len
;
343 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
344 struct ib_grh
*grh
, struct cm_av
*av
)
347 av
->pkey_index
= wc
->pkey_index
;
348 ib_init_ah_from_wc(port
->cm_dev
->ib_device
, port
->port_num
, wc
,
352 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
354 struct cm_device
*cm_dev
;
355 struct cm_port
*port
= NULL
;
360 read_lock_irqsave(&cm
.device_lock
, flags
);
361 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
362 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
364 port
= cm_dev
->port
[p
-1];
368 read_unlock_irqrestore(&cm
.device_lock
, flags
);
373 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
374 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
379 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
381 av
->timeout
= path
->packet_life_time
+ 1;
382 memcpy(av
->smac
, path
->smac
, sizeof(av
->smac
));
388 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
393 idr_preload(GFP_KERNEL
);
394 spin_lock_irqsave(&cm
.lock
, flags
);
396 id
= idr_alloc_cyclic(&cm
.local_id_table
, cm_id_priv
, 0, 0, GFP_NOWAIT
);
398 spin_unlock_irqrestore(&cm
.lock
, flags
);
401 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
402 return id
< 0 ? id
: 0;
405 static void cm_free_id(__be32 local_id
)
407 spin_lock_irq(&cm
.lock
);
408 idr_remove(&cm
.local_id_table
,
409 (__force
int) (local_id
^ cm
.random_id_operand
));
410 spin_unlock_irq(&cm
.lock
);
413 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
415 struct cm_id_private
*cm_id_priv
;
417 cm_id_priv
= idr_find(&cm
.local_id_table
,
418 (__force
int) (local_id
^ cm
.random_id_operand
));
420 if (cm_id_priv
->id
.remote_id
== remote_id
)
421 atomic_inc(&cm_id_priv
->refcount
);
429 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
431 struct cm_id_private
*cm_id_priv
;
433 spin_lock_irq(&cm
.lock
);
434 cm_id_priv
= cm_get_id(local_id
, remote_id
);
435 spin_unlock_irq(&cm
.lock
);
440 static void cm_mask_copy(u32
*dst
, const u32
*src
, const u32
*mask
)
444 for (i
= 0; i
< IB_CM_COMPARE_SIZE
; i
++)
445 dst
[i
] = src
[i
] & mask
[i
];
448 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
449 struct ib_cm_compare_data
*dst_data
)
451 u32 src
[IB_CM_COMPARE_SIZE
];
452 u32 dst
[IB_CM_COMPARE_SIZE
];
454 if (!src_data
|| !dst_data
)
457 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
458 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
459 return memcmp(src
, dst
, sizeof(src
));
462 static int cm_compare_private_data(u32
*private_data
,
463 struct ib_cm_compare_data
*dst_data
)
465 u32 src
[IB_CM_COMPARE_SIZE
];
470 cm_mask_copy(src
, private_data
, dst_data
->mask
);
471 return memcmp(src
, dst_data
->data
, sizeof(src
));
475 * Trivial helpers to strip endian annotation and compare; the
476 * endianness doesn't actually matter since we just need a stable
477 * order for the RB tree.
479 static int be32_lt(__be32 a
, __be32 b
)
481 return (__force u32
) a
< (__force u32
) b
;
484 static int be32_gt(__be32 a
, __be32 b
)
486 return (__force u32
) a
> (__force u32
) b
;
489 static int be64_lt(__be64 a
, __be64 b
)
491 return (__force u64
) a
< (__force u64
) b
;
494 static int be64_gt(__be64 a
, __be64 b
)
496 return (__force u64
) a
> (__force u64
) b
;
499 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
501 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
502 struct rb_node
*parent
= NULL
;
503 struct cm_id_private
*cur_cm_id_priv
;
504 __be64 service_id
= cm_id_priv
->id
.service_id
;
505 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
510 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
512 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
513 cur_cm_id_priv
->compare_data
);
514 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
515 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
516 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
518 return cur_cm_id_priv
;
520 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
521 link
= &(*link
)->rb_left
;
522 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
523 link
= &(*link
)->rb_right
;
524 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
525 link
= &(*link
)->rb_left
;
526 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
527 link
= &(*link
)->rb_right
;
528 else if (data_cmp
< 0)
529 link
= &(*link
)->rb_left
;
531 link
= &(*link
)->rb_right
;
533 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
534 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
538 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
542 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
543 struct cm_id_private
*cm_id_priv
;
547 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
548 data_cmp
= cm_compare_private_data(private_data
,
549 cm_id_priv
->compare_data
);
550 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
551 cm_id_priv
->id
.service_id
&&
552 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
555 if (device
< cm_id_priv
->id
.device
)
556 node
= node
->rb_left
;
557 else if (device
> cm_id_priv
->id
.device
)
558 node
= node
->rb_right
;
559 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
560 node
= node
->rb_left
;
561 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
562 node
= node
->rb_right
;
563 else if (data_cmp
< 0)
564 node
= node
->rb_left
;
566 node
= node
->rb_right
;
571 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
574 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
575 struct rb_node
*parent
= NULL
;
576 struct cm_timewait_info
*cur_timewait_info
;
577 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
578 __be32 remote_id
= timewait_info
->work
.remote_id
;
582 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
584 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
585 link
= &(*link
)->rb_left
;
586 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
587 link
= &(*link
)->rb_right
;
588 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
589 link
= &(*link
)->rb_left
;
590 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
591 link
= &(*link
)->rb_right
;
593 return cur_timewait_info
;
595 timewait_info
->inserted_remote_id
= 1;
596 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
597 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
601 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
604 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
605 struct cm_timewait_info
*timewait_info
;
608 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
610 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
611 node
= node
->rb_left
;
612 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
613 node
= node
->rb_right
;
614 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
615 node
= node
->rb_left
;
616 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
617 node
= node
->rb_right
;
619 return timewait_info
;
624 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
627 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
628 struct rb_node
*parent
= NULL
;
629 struct cm_timewait_info
*cur_timewait_info
;
630 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
631 __be32 remote_qpn
= timewait_info
->remote_qpn
;
635 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
637 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
638 link
= &(*link
)->rb_left
;
639 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
640 link
= &(*link
)->rb_right
;
641 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
642 link
= &(*link
)->rb_left
;
643 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
644 link
= &(*link
)->rb_right
;
646 return cur_timewait_info
;
648 timewait_info
->inserted_remote_qp
= 1;
649 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
650 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
654 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
657 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
658 struct rb_node
*parent
= NULL
;
659 struct cm_id_private
*cur_cm_id_priv
;
660 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
661 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
665 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
667 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
668 link
= &(*link
)->rb_left
;
669 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
670 link
= &(*link
)->rb_right
;
673 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
676 link
= &(*link
)->rb_left
;
678 link
= &(*link
)->rb_right
;
680 return cur_cm_id_priv
;
683 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
684 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
688 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
689 enum ib_cm_sidr_status status
)
691 struct ib_cm_sidr_rep_param param
;
693 memset(¶m
, 0, sizeof param
);
694 param
.status
= status
;
695 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
698 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
699 ib_cm_handler cm_handler
,
702 struct cm_id_private
*cm_id_priv
;
705 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
707 return ERR_PTR(-ENOMEM
);
709 cm_id_priv
->id
.state
= IB_CM_IDLE
;
710 cm_id_priv
->id
.device
= device
;
711 cm_id_priv
->id
.cm_handler
= cm_handler
;
712 cm_id_priv
->id
.context
= context
;
713 cm_id_priv
->id
.remote_cm_qpn
= 1;
714 ret
= cm_alloc_id(cm_id_priv
);
718 spin_lock_init(&cm_id_priv
->lock
);
719 init_completion(&cm_id_priv
->comp
);
720 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
721 atomic_set(&cm_id_priv
->work_count
, -1);
722 atomic_set(&cm_id_priv
->refcount
, 1);
723 return &cm_id_priv
->id
;
727 return ERR_PTR(-ENOMEM
);
729 EXPORT_SYMBOL(ib_create_cm_id
);
731 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
733 struct cm_work
*work
;
735 if (list_empty(&cm_id_priv
->work_list
))
738 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
739 list_del(&work
->list
);
743 static void cm_free_work(struct cm_work
*work
)
745 if (work
->mad_recv_wc
)
746 ib_free_recv_mad(work
->mad_recv_wc
);
750 static inline int cm_convert_to_ms(int iba_time
)
752 /* approximate conversion to ms from 4.096us x 2^iba_time */
753 return 1 << max(iba_time
- 8, 0);
757 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
758 * Because of how ack_timeout is stored, adding one doubles the timeout.
759 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
760 * increment it (round up) only if the other is within 50%.
762 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
764 int ack_timeout
= packet_life_time
+ 1;
766 if (ack_timeout
>= ca_ack_delay
)
767 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
769 ack_timeout
= ca_ack_delay
+
770 (ack_timeout
>= (ca_ack_delay
- 1));
772 return min(31, ack_timeout
);
775 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
777 if (timewait_info
->inserted_remote_id
) {
778 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
779 timewait_info
->inserted_remote_id
= 0;
782 if (timewait_info
->inserted_remote_qp
) {
783 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
784 timewait_info
->inserted_remote_qp
= 0;
788 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
790 struct cm_timewait_info
*timewait_info
;
792 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
794 return ERR_PTR(-ENOMEM
);
796 timewait_info
->work
.local_id
= local_id
;
797 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
798 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
799 return timewait_info
;
802 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
807 spin_lock_irqsave(&cm
.lock
, flags
);
808 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
809 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
810 spin_unlock_irqrestore(&cm
.lock
, flags
);
813 * The cm_id could be destroyed by the user before we exit timewait.
814 * To protect against this, we search for the cm_id after exiting
815 * timewait before notifying the user that we've exited timewait.
817 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
818 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
819 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
820 msecs_to_jiffies(wait_time
));
821 cm_id_priv
->timewait_info
= NULL
;
824 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
828 cm_id_priv
->id
.state
= IB_CM_IDLE
;
829 if (cm_id_priv
->timewait_info
) {
830 spin_lock_irqsave(&cm
.lock
, flags
);
831 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
832 spin_unlock_irqrestore(&cm
.lock
, flags
);
833 kfree(cm_id_priv
->timewait_info
);
834 cm_id_priv
->timewait_info
= NULL
;
838 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
840 struct cm_id_private
*cm_id_priv
;
841 struct cm_work
*work
;
843 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
845 spin_lock_irq(&cm_id_priv
->lock
);
846 switch (cm_id
->state
) {
848 cm_id
->state
= IB_CM_IDLE
;
849 spin_unlock_irq(&cm_id_priv
->lock
);
850 spin_lock_irq(&cm
.lock
);
851 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
852 spin_unlock_irq(&cm
.lock
);
854 case IB_CM_SIDR_REQ_SENT
:
855 cm_id
->state
= IB_CM_IDLE
;
856 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
857 spin_unlock_irq(&cm_id_priv
->lock
);
859 case IB_CM_SIDR_REQ_RCVD
:
860 spin_unlock_irq(&cm_id_priv
->lock
);
861 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
864 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
865 spin_unlock_irq(&cm_id_priv
->lock
);
866 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
867 &cm_id_priv
->id
.device
->node_guid
,
868 sizeof cm_id_priv
->id
.device
->node_guid
,
872 if (err
== -ENOMEM
) {
873 /* Do not reject to allow future retries. */
874 cm_reset_to_idle(cm_id_priv
);
875 spin_unlock_irq(&cm_id_priv
->lock
);
877 spin_unlock_irq(&cm_id_priv
->lock
);
878 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
882 case IB_CM_MRA_REQ_RCVD
:
884 case IB_CM_MRA_REP_RCVD
:
885 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
887 case IB_CM_MRA_REQ_SENT
:
889 case IB_CM_MRA_REP_SENT
:
890 spin_unlock_irq(&cm_id_priv
->lock
);
891 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
894 case IB_CM_ESTABLISHED
:
895 spin_unlock_irq(&cm_id_priv
->lock
);
896 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
898 ib_send_cm_dreq(cm_id
, NULL
, 0);
900 case IB_CM_DREQ_SENT
:
901 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
902 cm_enter_timewait(cm_id_priv
);
903 spin_unlock_irq(&cm_id_priv
->lock
);
905 case IB_CM_DREQ_RCVD
:
906 spin_unlock_irq(&cm_id_priv
->lock
);
907 ib_send_cm_drep(cm_id
, NULL
, 0);
910 spin_unlock_irq(&cm_id_priv
->lock
);
914 cm_free_id(cm_id
->local_id
);
915 cm_deref_id(cm_id_priv
);
916 wait_for_completion(&cm_id_priv
->comp
);
917 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
919 kfree(cm_id_priv
->compare_data
);
920 kfree(cm_id_priv
->private_data
);
924 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
926 cm_destroy_id(cm_id
, 0);
928 EXPORT_SYMBOL(ib_destroy_cm_id
);
930 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
931 struct ib_cm_compare_data
*compare_data
)
933 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
937 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
938 service_id
&= service_mask
;
939 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
940 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
943 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
944 if (cm_id
->state
!= IB_CM_IDLE
)
948 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
950 if (!cm_id_priv
->compare_data
)
952 cm_mask_copy(cm_id_priv
->compare_data
->data
,
953 compare_data
->data
, compare_data
->mask
);
954 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
955 sizeof(compare_data
->mask
));
958 cm_id
->state
= IB_CM_LISTEN
;
960 spin_lock_irqsave(&cm
.lock
, flags
);
961 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
962 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
963 cm_id
->service_mask
= ~cpu_to_be64(0);
965 cm_id
->service_id
= service_id
;
966 cm_id
->service_mask
= service_mask
;
968 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
969 spin_unlock_irqrestore(&cm
.lock
, flags
);
971 if (cur_cm_id_priv
) {
972 cm_id
->state
= IB_CM_IDLE
;
973 kfree(cm_id_priv
->compare_data
);
974 cm_id_priv
->compare_data
= NULL
;
979 EXPORT_SYMBOL(ib_cm_listen
);
981 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
982 enum cm_msg_sequence msg_seq
)
986 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
987 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
989 return cpu_to_be64(hi_tid
| low_tid
);
992 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
993 __be16 attr_id
, __be64 tid
)
995 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
996 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
997 hdr
->class_version
= IB_CM_CLASS_VERSION
;
998 hdr
->method
= IB_MGMT_METHOD_SEND
;
999 hdr
->attr_id
= attr_id
;
1003 static void cm_format_req(struct cm_req_msg
*req_msg
,
1004 struct cm_id_private
*cm_id_priv
,
1005 struct ib_cm_req_param
*param
)
1007 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1008 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1010 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1011 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1013 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1014 req_msg
->service_id
= param
->service_id
;
1015 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1016 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1017 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1018 cm_req_set_remote_resp_timeout(req_msg
,
1019 param
->remote_cm_response_timeout
);
1020 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1021 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1022 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1023 cm_req_set_local_resp_timeout(req_msg
,
1024 param
->local_cm_response_timeout
);
1025 req_msg
->pkey
= param
->primary_path
->pkey
;
1026 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1027 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1029 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1030 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1031 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1032 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1033 cm_req_set_srq(req_msg
, param
->srq
);
1036 if (pri_path
->hop_limit
<= 1) {
1037 req_msg
->primary_local_lid
= pri_path
->slid
;
1038 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1040 /* Work-around until there's a way to obtain remote LID info */
1041 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1042 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1044 req_msg
->primary_local_gid
= pri_path
->sgid
;
1045 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1046 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1047 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1048 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1049 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1050 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1051 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1052 cm_req_set_primary_local_ack_timeout(req_msg
,
1053 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1054 pri_path
->packet_life_time
));
1057 if (alt_path
->hop_limit
<= 1) {
1058 req_msg
->alt_local_lid
= alt_path
->slid
;
1059 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1061 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1062 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1064 req_msg
->alt_local_gid
= alt_path
->sgid
;
1065 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1066 cm_req_set_alt_flow_label(req_msg
,
1067 alt_path
->flow_label
);
1068 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1069 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1070 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1071 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1072 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1073 cm_req_set_alt_local_ack_timeout(req_msg
,
1074 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1075 alt_path
->packet_life_time
));
1078 if (param
->private_data
&& param
->private_data_len
)
1079 memcpy(req_msg
->private_data
, param
->private_data
,
1080 param
->private_data_len
);
1083 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1085 /* peer-to-peer not supported */
1086 if (param
->peer_to_peer
)
1089 if (!param
->primary_path
)
1092 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1093 param
->qp_type
!= IB_QPT_XRC_INI
)
1096 if (param
->private_data
&&
1097 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1100 if (param
->alternate_path
&&
1101 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1102 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1108 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1109 struct ib_cm_req_param
*param
)
1111 struct cm_id_private
*cm_id_priv
;
1112 struct cm_req_msg
*req_msg
;
1113 unsigned long flags
;
1116 ret
= cm_validate_req_param(param
);
1120 /* Verify that we're not in timewait. */
1121 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1122 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1123 if (cm_id
->state
!= IB_CM_IDLE
) {
1124 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1128 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1130 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1132 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1133 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1137 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1140 if (param
->alternate_path
) {
1141 ret
= cm_init_av_by_path(param
->alternate_path
,
1142 &cm_id_priv
->alt_av
);
1146 cm_id
->service_id
= param
->service_id
;
1147 cm_id
->service_mask
= ~cpu_to_be64(0);
1148 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1149 param
->primary_path
->packet_life_time
) * 2 +
1151 param
->remote_cm_response_timeout
);
1152 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1153 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1154 cm_id_priv
->responder_resources
= param
->responder_resources
;
1155 cm_id_priv
->retry_count
= param
->retry_count
;
1156 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1157 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1158 cm_id_priv
->qp_type
= param
->qp_type
;
1160 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1164 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1165 cm_format_req(req_msg
, cm_id_priv
, param
);
1166 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1167 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1168 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1170 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1171 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1173 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1174 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1176 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1179 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1180 cm_id
->state
= IB_CM_REQ_SENT
;
1181 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1184 error2
: cm_free_msg(cm_id_priv
->msg
);
1185 error1
: kfree(cm_id_priv
->timewait_info
);
1188 EXPORT_SYMBOL(ib_send_cm_req
);
1190 static int cm_issue_rej(struct cm_port
*port
,
1191 struct ib_mad_recv_wc
*mad_recv_wc
,
1192 enum ib_cm_rej_reason reason
,
1193 enum cm_msg_response msg_rejected
,
1194 void *ari
, u8 ari_length
)
1196 struct ib_mad_send_buf
*msg
= NULL
;
1197 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1200 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1204 /* We just need common CM header information. Cast to any message. */
1205 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1206 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1208 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1209 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1210 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1211 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1212 rej_msg
->reason
= cpu_to_be16(reason
);
1214 if (ari
&& ari_length
) {
1215 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1216 memcpy(rej_msg
->ari
, ari
, ari_length
);
1219 ret
= ib_post_send_mad(msg
, NULL
);
1226 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1227 __be32 local_qpn
, __be32 remote_qpn
)
1229 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1230 ((local_ca_guid
== remote_ca_guid
) &&
1231 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1234 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1235 struct ib_sa_path_rec
*primary_path
,
1236 struct ib_sa_path_rec
*alt_path
)
1238 memset(primary_path
, 0, sizeof *primary_path
);
1239 primary_path
->dgid
= req_msg
->primary_local_gid
;
1240 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1241 primary_path
->dlid
= req_msg
->primary_local_lid
;
1242 primary_path
->slid
= req_msg
->primary_remote_lid
;
1243 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1244 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1245 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1246 primary_path
->reversible
= 1;
1247 primary_path
->pkey
= req_msg
->pkey
;
1248 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1249 primary_path
->mtu_selector
= IB_SA_EQ
;
1250 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1251 primary_path
->rate_selector
= IB_SA_EQ
;
1252 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1253 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1254 primary_path
->packet_life_time
=
1255 cm_req_get_primary_local_ack_timeout(req_msg
);
1256 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1258 if (req_msg
->alt_local_lid
) {
1259 memset(alt_path
, 0, sizeof *alt_path
);
1260 alt_path
->dgid
= req_msg
->alt_local_gid
;
1261 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1262 alt_path
->dlid
= req_msg
->alt_local_lid
;
1263 alt_path
->slid
= req_msg
->alt_remote_lid
;
1264 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1265 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1266 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1267 alt_path
->reversible
= 1;
1268 alt_path
->pkey
= req_msg
->pkey
;
1269 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1270 alt_path
->mtu_selector
= IB_SA_EQ
;
1271 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1272 alt_path
->rate_selector
= IB_SA_EQ
;
1273 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1274 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1275 alt_path
->packet_life_time
=
1276 cm_req_get_alt_local_ack_timeout(req_msg
);
1277 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1281 static void cm_format_req_event(struct cm_work
*work
,
1282 struct cm_id_private
*cm_id_priv
,
1283 struct ib_cm_id
*listen_id
)
1285 struct cm_req_msg
*req_msg
;
1286 struct ib_cm_req_event_param
*param
;
1288 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1289 param
= &work
->cm_event
.param
.req_rcvd
;
1290 param
->listen_id
= listen_id
;
1291 param
->port
= cm_id_priv
->av
.port
->port_num
;
1292 param
->primary_path
= &work
->path
[0];
1293 if (req_msg
->alt_local_lid
)
1294 param
->alternate_path
= &work
->path
[1];
1296 param
->alternate_path
= NULL
;
1297 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1298 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1299 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1300 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1301 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1302 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1303 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1304 param
->local_cm_response_timeout
=
1305 cm_req_get_remote_resp_timeout(req_msg
);
1306 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1307 param
->remote_cm_response_timeout
=
1308 cm_req_get_local_resp_timeout(req_msg
);
1309 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1310 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1311 param
->srq
= cm_req_get_srq(req_msg
);
1312 work
->cm_event
.private_data
= &req_msg
->private_data
;
1315 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1316 struct cm_work
*work
)
1320 /* We will typically only have the current event to report. */
1321 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1324 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1325 spin_lock_irq(&cm_id_priv
->lock
);
1326 work
= cm_dequeue_work(cm_id_priv
);
1327 spin_unlock_irq(&cm_id_priv
->lock
);
1329 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1333 cm_deref_id(cm_id_priv
);
1335 cm_destroy_id(&cm_id_priv
->id
, ret
);
1338 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1339 struct cm_id_private
*cm_id_priv
,
1340 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1341 const void *private_data
, u8 private_data_len
)
1343 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1344 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1345 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1346 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1347 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1349 if (private_data
&& private_data_len
)
1350 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1353 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1354 struct cm_id_private
*cm_id_priv
,
1355 enum ib_cm_rej_reason reason
,
1358 const void *private_data
,
1359 u8 private_data_len
)
1361 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1362 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1364 switch(cm_id_priv
->id
.state
) {
1365 case IB_CM_REQ_RCVD
:
1366 rej_msg
->local_comm_id
= 0;
1367 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1369 case IB_CM_MRA_REQ_SENT
:
1370 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1371 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1373 case IB_CM_REP_RCVD
:
1374 case IB_CM_MRA_REP_SENT
:
1375 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1376 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1379 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1380 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1384 rej_msg
->reason
= cpu_to_be16(reason
);
1385 if (ari
&& ari_length
) {
1386 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1387 memcpy(rej_msg
->ari
, ari
, ari_length
);
1390 if (private_data
&& private_data_len
)
1391 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1394 static void cm_dup_req_handler(struct cm_work
*work
,
1395 struct cm_id_private
*cm_id_priv
)
1397 struct ib_mad_send_buf
*msg
= NULL
;
1400 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1401 counter
[CM_REQ_COUNTER
]);
1403 /* Quick state check to discard duplicate REQs. */
1404 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1407 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1411 spin_lock_irq(&cm_id_priv
->lock
);
1412 switch (cm_id_priv
->id
.state
) {
1413 case IB_CM_MRA_REQ_SENT
:
1414 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1415 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1416 cm_id_priv
->private_data
,
1417 cm_id_priv
->private_data_len
);
1419 case IB_CM_TIMEWAIT
:
1420 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1421 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1426 spin_unlock_irq(&cm_id_priv
->lock
);
1428 ret
= ib_post_send_mad(msg
, NULL
);
1433 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1434 free
: cm_free_msg(msg
);
1437 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1438 struct cm_id_private
*cm_id_priv
)
1440 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1441 struct cm_timewait_info
*timewait_info
;
1442 struct cm_req_msg
*req_msg
;
1444 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1446 /* Check for possible duplicate REQ. */
1447 spin_lock_irq(&cm
.lock
);
1448 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1449 if (timewait_info
) {
1450 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1451 timewait_info
->work
.remote_id
);
1452 spin_unlock_irq(&cm
.lock
);
1453 if (cur_cm_id_priv
) {
1454 cm_dup_req_handler(work
, cur_cm_id_priv
);
1455 cm_deref_id(cur_cm_id_priv
);
1460 /* Check for stale connections. */
1461 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1462 if (timewait_info
) {
1463 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1464 spin_unlock_irq(&cm
.lock
);
1465 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1466 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1471 /* Find matching listen request. */
1472 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1473 req_msg
->service_id
,
1474 req_msg
->private_data
);
1475 if (!listen_cm_id_priv
) {
1476 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1477 spin_unlock_irq(&cm
.lock
);
1478 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1479 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1483 atomic_inc(&listen_cm_id_priv
->refcount
);
1484 atomic_inc(&cm_id_priv
->refcount
);
1485 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1486 atomic_inc(&cm_id_priv
->work_count
);
1487 spin_unlock_irq(&cm
.lock
);
1489 return listen_cm_id_priv
;
1493 * Work-around for inter-subnet connections. If the LIDs are permissive,
1494 * we need to override the LID/SL data in the REQ with the LID information
1495 * in the work completion.
1497 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1499 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1500 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1501 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1502 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1505 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1506 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1509 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1510 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1511 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1512 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1515 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1516 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1520 static int cm_req_handler(struct cm_work
*work
)
1522 struct ib_cm_id
*cm_id
;
1523 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1524 struct cm_req_msg
*req_msg
;
1527 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1529 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1531 return PTR_ERR(cm_id
);
1533 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1534 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1535 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1536 work
->mad_recv_wc
->recv_buf
.grh
,
1538 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1540 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1541 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1544 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1545 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1546 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1548 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1549 if (!listen_cm_id_priv
) {
1551 kfree(cm_id_priv
->timewait_info
);
1555 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1556 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1557 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1558 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1560 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1561 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1563 memcpy(work
->path
[0].dmac
, cm_id_priv
->av
.ah_attr
.dmac
, ETH_ALEN
);
1564 work
->path
[0].vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
1565 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1567 ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1568 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1569 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1570 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1574 if (req_msg
->alt_local_lid
) {
1575 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1577 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1578 &work
->path
[0].sgid
,
1579 sizeof work
->path
[0].sgid
, NULL
, 0);
1583 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1584 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1585 cm_req_get_local_resp_timeout(req_msg
));
1586 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1587 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1588 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1589 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1590 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1591 cm_id_priv
->pkey
= req_msg
->pkey
;
1592 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1593 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1594 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1595 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1597 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1598 cm_process_work(cm_id_priv
, work
);
1599 cm_deref_id(listen_cm_id_priv
);
1603 atomic_dec(&cm_id_priv
->refcount
);
1604 cm_deref_id(listen_cm_id_priv
);
1606 ib_destroy_cm_id(cm_id
);
1610 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1611 struct cm_id_private
*cm_id_priv
,
1612 struct ib_cm_rep_param
*param
)
1614 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1615 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1616 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1617 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1618 rep_msg
->resp_resources
= param
->responder_resources
;
1619 cm_rep_set_target_ack_delay(rep_msg
,
1620 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1621 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1622 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1623 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1625 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1626 rep_msg
->initiator_depth
= param
->initiator_depth
;
1627 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1628 cm_rep_set_srq(rep_msg
, param
->srq
);
1629 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1631 cm_rep_set_srq(rep_msg
, 1);
1632 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1635 if (param
->private_data
&& param
->private_data_len
)
1636 memcpy(rep_msg
->private_data
, param
->private_data
,
1637 param
->private_data_len
);
1640 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1641 struct ib_cm_rep_param
*param
)
1643 struct cm_id_private
*cm_id_priv
;
1644 struct ib_mad_send_buf
*msg
;
1645 struct cm_rep_msg
*rep_msg
;
1646 unsigned long flags
;
1649 if (param
->private_data
&&
1650 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1653 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1654 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1655 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1656 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1661 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1665 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1666 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1667 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1668 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1670 ret
= ib_post_send_mad(msg
, NULL
);
1672 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1677 cm_id
->state
= IB_CM_REP_SENT
;
1678 cm_id_priv
->msg
= msg
;
1679 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1680 cm_id_priv
->responder_resources
= param
->responder_resources
;
1681 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1682 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1684 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1687 EXPORT_SYMBOL(ib_send_cm_rep
);
1689 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1690 struct cm_id_private
*cm_id_priv
,
1691 const void *private_data
,
1692 u8 private_data_len
)
1694 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1695 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1696 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1698 if (private_data
&& private_data_len
)
1699 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1702 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1703 const void *private_data
,
1704 u8 private_data_len
)
1706 struct cm_id_private
*cm_id_priv
;
1707 struct ib_mad_send_buf
*msg
;
1708 unsigned long flags
;
1712 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1715 data
= cm_copy_private_data(private_data
, private_data_len
);
1717 return PTR_ERR(data
);
1719 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1720 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1721 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1722 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1727 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1731 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1732 private_data
, private_data_len
);
1734 ret
= ib_post_send_mad(msg
, NULL
);
1736 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1742 cm_id
->state
= IB_CM_ESTABLISHED
;
1743 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1744 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1747 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1751 EXPORT_SYMBOL(ib_send_cm_rtu
);
1753 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
1755 struct cm_rep_msg
*rep_msg
;
1756 struct ib_cm_rep_event_param
*param
;
1758 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1759 param
= &work
->cm_event
.param
.rep_rcvd
;
1760 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1761 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1762 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
1763 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1764 param
->responder_resources
= rep_msg
->initiator_depth
;
1765 param
->initiator_depth
= rep_msg
->resp_resources
;
1766 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1767 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1768 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1769 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1770 param
->srq
= cm_rep_get_srq(rep_msg
);
1771 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1774 static void cm_dup_rep_handler(struct cm_work
*work
)
1776 struct cm_id_private
*cm_id_priv
;
1777 struct cm_rep_msg
*rep_msg
;
1778 struct ib_mad_send_buf
*msg
= NULL
;
1781 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1782 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1783 rep_msg
->local_comm_id
);
1787 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1788 counter
[CM_REP_COUNTER
]);
1789 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1793 spin_lock_irq(&cm_id_priv
->lock
);
1794 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1795 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1796 cm_id_priv
->private_data
,
1797 cm_id_priv
->private_data_len
);
1798 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1799 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1800 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1801 cm_id_priv
->private_data
,
1802 cm_id_priv
->private_data_len
);
1805 spin_unlock_irq(&cm_id_priv
->lock
);
1807 ret
= ib_post_send_mad(msg
, NULL
);
1812 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1813 free
: cm_free_msg(msg
);
1814 deref
: cm_deref_id(cm_id_priv
);
1817 static int cm_rep_handler(struct cm_work
*work
)
1819 struct cm_id_private
*cm_id_priv
;
1820 struct cm_rep_msg
*rep_msg
;
1823 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1824 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1826 cm_dup_rep_handler(work
);
1830 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
1832 spin_lock_irq(&cm_id_priv
->lock
);
1833 switch (cm_id_priv
->id
.state
) {
1834 case IB_CM_REQ_SENT
:
1835 case IB_CM_MRA_REQ_RCVD
:
1838 spin_unlock_irq(&cm_id_priv
->lock
);
1843 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1844 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1845 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1847 spin_lock(&cm
.lock
);
1848 /* Check for duplicate REP. */
1849 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1850 spin_unlock(&cm
.lock
);
1851 spin_unlock_irq(&cm_id_priv
->lock
);
1855 /* Check for a stale connection. */
1856 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1857 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1858 &cm
.remote_id_table
);
1859 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1860 spin_unlock(&cm
.lock
);
1861 spin_unlock_irq(&cm_id_priv
->lock
);
1862 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1863 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1868 spin_unlock(&cm
.lock
);
1870 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1871 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1872 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1873 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1874 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1875 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1876 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1877 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1878 cm_id_priv
->av
.timeout
=
1879 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1880 cm_id_priv
->av
.timeout
- 1);
1881 cm_id_priv
->alt_av
.timeout
=
1882 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1883 cm_id_priv
->alt_av
.timeout
- 1);
1885 /* todo: handle peer_to_peer */
1887 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1888 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1890 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1891 spin_unlock_irq(&cm_id_priv
->lock
);
1894 cm_process_work(cm_id_priv
, work
);
1896 cm_deref_id(cm_id_priv
);
1900 cm_deref_id(cm_id_priv
);
1904 static int cm_establish_handler(struct cm_work
*work
)
1906 struct cm_id_private
*cm_id_priv
;
1909 /* See comment in cm_establish about lookup. */
1910 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1914 spin_lock_irq(&cm_id_priv
->lock
);
1915 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1916 spin_unlock_irq(&cm_id_priv
->lock
);
1920 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1921 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1923 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1924 spin_unlock_irq(&cm_id_priv
->lock
);
1927 cm_process_work(cm_id_priv
, work
);
1929 cm_deref_id(cm_id_priv
);
1932 cm_deref_id(cm_id_priv
);
1936 static int cm_rtu_handler(struct cm_work
*work
)
1938 struct cm_id_private
*cm_id_priv
;
1939 struct cm_rtu_msg
*rtu_msg
;
1942 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1943 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1944 rtu_msg
->local_comm_id
);
1948 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1950 spin_lock_irq(&cm_id_priv
->lock
);
1951 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1952 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1953 spin_unlock_irq(&cm_id_priv
->lock
);
1954 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1955 counter
[CM_RTU_COUNTER
]);
1958 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1960 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1961 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1963 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1964 spin_unlock_irq(&cm_id_priv
->lock
);
1967 cm_process_work(cm_id_priv
, work
);
1969 cm_deref_id(cm_id_priv
);
1972 cm_deref_id(cm_id_priv
);
1976 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1977 struct cm_id_private
*cm_id_priv
,
1978 const void *private_data
,
1979 u8 private_data_len
)
1981 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1982 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1983 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1984 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1985 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1987 if (private_data
&& private_data_len
)
1988 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1991 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1992 const void *private_data
,
1993 u8 private_data_len
)
1995 struct cm_id_private
*cm_id_priv
;
1996 struct ib_mad_send_buf
*msg
;
1997 unsigned long flags
;
2000 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2003 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2004 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2005 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2010 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2011 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2012 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2014 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2016 cm_enter_timewait(cm_id_priv
);
2020 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2021 private_data
, private_data_len
);
2022 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2023 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2025 ret
= ib_post_send_mad(msg
, NULL
);
2027 cm_enter_timewait(cm_id_priv
);
2028 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2033 cm_id
->state
= IB_CM_DREQ_SENT
;
2034 cm_id_priv
->msg
= msg
;
2035 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2038 EXPORT_SYMBOL(ib_send_cm_dreq
);
2040 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2041 struct cm_id_private
*cm_id_priv
,
2042 const void *private_data
,
2043 u8 private_data_len
)
2045 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2046 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2047 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2049 if (private_data
&& private_data_len
)
2050 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2053 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2054 const void *private_data
,
2055 u8 private_data_len
)
2057 struct cm_id_private
*cm_id_priv
;
2058 struct ib_mad_send_buf
*msg
;
2059 unsigned long flags
;
2063 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2066 data
= cm_copy_private_data(private_data
, private_data_len
);
2068 return PTR_ERR(data
);
2070 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2071 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2072 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2073 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2078 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2079 cm_enter_timewait(cm_id_priv
);
2081 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2085 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2086 private_data
, private_data_len
);
2088 ret
= ib_post_send_mad(msg
, NULL
);
2090 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2095 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2098 EXPORT_SYMBOL(ib_send_cm_drep
);
2100 static int cm_issue_drep(struct cm_port
*port
,
2101 struct ib_mad_recv_wc
*mad_recv_wc
)
2103 struct ib_mad_send_buf
*msg
= NULL
;
2104 struct cm_dreq_msg
*dreq_msg
;
2105 struct cm_drep_msg
*drep_msg
;
2108 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2112 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2113 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2115 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2116 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2117 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2119 ret
= ib_post_send_mad(msg
, NULL
);
2126 static int cm_dreq_handler(struct cm_work
*work
)
2128 struct cm_id_private
*cm_id_priv
;
2129 struct cm_dreq_msg
*dreq_msg
;
2130 struct ib_mad_send_buf
*msg
= NULL
;
2133 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2134 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2135 dreq_msg
->local_comm_id
);
2137 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2138 counter
[CM_DREQ_COUNTER
]);
2139 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2143 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2145 spin_lock_irq(&cm_id_priv
->lock
);
2146 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2149 switch (cm_id_priv
->id
.state
) {
2150 case IB_CM_REP_SENT
:
2151 case IB_CM_DREQ_SENT
:
2152 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2154 case IB_CM_ESTABLISHED
:
2155 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2156 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2157 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2159 case IB_CM_MRA_REP_RCVD
:
2161 case IB_CM_TIMEWAIT
:
2162 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2163 counter
[CM_DREQ_COUNTER
]);
2164 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2167 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2168 cm_id_priv
->private_data
,
2169 cm_id_priv
->private_data_len
);
2170 spin_unlock_irq(&cm_id_priv
->lock
);
2172 if (ib_post_send_mad(msg
, NULL
))
2175 case IB_CM_DREQ_RCVD
:
2176 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2177 counter
[CM_DREQ_COUNTER
]);
2182 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2183 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2184 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2186 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2187 spin_unlock_irq(&cm_id_priv
->lock
);
2190 cm_process_work(cm_id_priv
, work
);
2192 cm_deref_id(cm_id_priv
);
2195 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2196 deref
: cm_deref_id(cm_id_priv
);
2200 static int cm_drep_handler(struct cm_work
*work
)
2202 struct cm_id_private
*cm_id_priv
;
2203 struct cm_drep_msg
*drep_msg
;
2206 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2207 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2208 drep_msg
->local_comm_id
);
2212 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2214 spin_lock_irq(&cm_id_priv
->lock
);
2215 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2216 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2217 spin_unlock_irq(&cm_id_priv
->lock
);
2220 cm_enter_timewait(cm_id_priv
);
2222 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2223 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2225 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2226 spin_unlock_irq(&cm_id_priv
->lock
);
2229 cm_process_work(cm_id_priv
, work
);
2231 cm_deref_id(cm_id_priv
);
2234 cm_deref_id(cm_id_priv
);
2238 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2239 enum ib_cm_rej_reason reason
,
2242 const void *private_data
,
2243 u8 private_data_len
)
2245 struct cm_id_private
*cm_id_priv
;
2246 struct ib_mad_send_buf
*msg
;
2247 unsigned long flags
;
2250 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2251 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2254 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2256 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2257 switch (cm_id
->state
) {
2258 case IB_CM_REQ_SENT
:
2259 case IB_CM_MRA_REQ_RCVD
:
2260 case IB_CM_REQ_RCVD
:
2261 case IB_CM_MRA_REQ_SENT
:
2262 case IB_CM_REP_RCVD
:
2263 case IB_CM_MRA_REP_SENT
:
2264 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2266 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2267 cm_id_priv
, reason
, ari
, ari_length
,
2268 private_data
, private_data_len
);
2270 cm_reset_to_idle(cm_id_priv
);
2272 case IB_CM_REP_SENT
:
2273 case IB_CM_MRA_REP_RCVD
:
2274 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2276 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2277 cm_id_priv
, reason
, ari
, ari_length
,
2278 private_data
, private_data_len
);
2280 cm_enter_timewait(cm_id_priv
);
2290 ret
= ib_post_send_mad(msg
, NULL
);
2294 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2297 EXPORT_SYMBOL(ib_send_cm_rej
);
2299 static void cm_format_rej_event(struct cm_work
*work
)
2301 struct cm_rej_msg
*rej_msg
;
2302 struct ib_cm_rej_event_param
*param
;
2304 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2305 param
= &work
->cm_event
.param
.rej_rcvd
;
2306 param
->ari
= rej_msg
->ari
;
2307 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2308 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2309 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2312 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2314 struct cm_timewait_info
*timewait_info
;
2315 struct cm_id_private
*cm_id_priv
;
2318 remote_id
= rej_msg
->local_comm_id
;
2320 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2321 spin_lock_irq(&cm
.lock
);
2322 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2324 if (!timewait_info
) {
2325 spin_unlock_irq(&cm
.lock
);
2328 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2329 (timewait_info
->work
.local_id
^
2330 cm
.random_id_operand
));
2332 if (cm_id_priv
->id
.remote_id
== remote_id
)
2333 atomic_inc(&cm_id_priv
->refcount
);
2337 spin_unlock_irq(&cm
.lock
);
2338 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2339 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2341 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2346 static int cm_rej_handler(struct cm_work
*work
)
2348 struct cm_id_private
*cm_id_priv
;
2349 struct cm_rej_msg
*rej_msg
;
2352 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2353 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2357 cm_format_rej_event(work
);
2359 spin_lock_irq(&cm_id_priv
->lock
);
2360 switch (cm_id_priv
->id
.state
) {
2361 case IB_CM_REQ_SENT
:
2362 case IB_CM_MRA_REQ_RCVD
:
2363 case IB_CM_REP_SENT
:
2364 case IB_CM_MRA_REP_RCVD
:
2365 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2367 case IB_CM_REQ_RCVD
:
2368 case IB_CM_MRA_REQ_SENT
:
2369 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2370 cm_enter_timewait(cm_id_priv
);
2372 cm_reset_to_idle(cm_id_priv
);
2374 case IB_CM_DREQ_SENT
:
2375 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2377 case IB_CM_REP_RCVD
:
2378 case IB_CM_MRA_REP_SENT
:
2379 cm_enter_timewait(cm_id_priv
);
2381 case IB_CM_ESTABLISHED
:
2382 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2383 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2384 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2385 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2387 cm_enter_timewait(cm_id_priv
);
2392 spin_unlock_irq(&cm_id_priv
->lock
);
2397 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2399 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2400 spin_unlock_irq(&cm_id_priv
->lock
);
2403 cm_process_work(cm_id_priv
, work
);
2405 cm_deref_id(cm_id_priv
);
2408 cm_deref_id(cm_id_priv
);
2412 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2414 const void *private_data
,
2415 u8 private_data_len
)
2417 struct cm_id_private
*cm_id_priv
;
2418 struct ib_mad_send_buf
*msg
;
2419 enum ib_cm_state cm_state
;
2420 enum ib_cm_lap_state lap_state
;
2421 enum cm_msg_response msg_response
;
2423 unsigned long flags
;
2426 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2429 data
= cm_copy_private_data(private_data
, private_data_len
);
2431 return PTR_ERR(data
);
2433 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2435 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2436 switch(cm_id_priv
->id
.state
) {
2437 case IB_CM_REQ_RCVD
:
2438 cm_state
= IB_CM_MRA_REQ_SENT
;
2439 lap_state
= cm_id
->lap_state
;
2440 msg_response
= CM_MSG_RESPONSE_REQ
;
2442 case IB_CM_REP_RCVD
:
2443 cm_state
= IB_CM_MRA_REP_SENT
;
2444 lap_state
= cm_id
->lap_state
;
2445 msg_response
= CM_MSG_RESPONSE_REP
;
2447 case IB_CM_ESTABLISHED
:
2448 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2449 cm_state
= cm_id
->state
;
2450 lap_state
= IB_CM_MRA_LAP_SENT
;
2451 msg_response
= CM_MSG_RESPONSE_OTHER
;
2459 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2460 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2464 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2465 msg_response
, service_timeout
,
2466 private_data
, private_data_len
);
2467 ret
= ib_post_send_mad(msg
, NULL
);
2472 cm_id
->state
= cm_state
;
2473 cm_id
->lap_state
= lap_state
;
2474 cm_id_priv
->service_timeout
= service_timeout
;
2475 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2476 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2479 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2483 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2488 EXPORT_SYMBOL(ib_send_cm_mra
);
2490 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2492 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2493 case CM_MSG_RESPONSE_REQ
:
2494 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2495 case CM_MSG_RESPONSE_REP
:
2496 case CM_MSG_RESPONSE_OTHER
:
2497 return cm_acquire_id(mra_msg
->remote_comm_id
,
2498 mra_msg
->local_comm_id
);
2504 static int cm_mra_handler(struct cm_work
*work
)
2506 struct cm_id_private
*cm_id_priv
;
2507 struct cm_mra_msg
*mra_msg
;
2510 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2511 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2515 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2516 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2517 cm_mra_get_service_timeout(mra_msg
);
2518 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2519 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2521 spin_lock_irq(&cm_id_priv
->lock
);
2522 switch (cm_id_priv
->id
.state
) {
2523 case IB_CM_REQ_SENT
:
2524 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2525 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2526 cm_id_priv
->msg
, timeout
))
2528 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2530 case IB_CM_REP_SENT
:
2531 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2532 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2533 cm_id_priv
->msg
, timeout
))
2535 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2537 case IB_CM_ESTABLISHED
:
2538 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2539 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2540 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2541 cm_id_priv
->msg
, timeout
)) {
2542 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2543 atomic_long_inc(&work
->port
->
2544 counter_group
[CM_RECV_DUPLICATES
].
2545 counter
[CM_MRA_COUNTER
]);
2548 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2550 case IB_CM_MRA_REQ_RCVD
:
2551 case IB_CM_MRA_REP_RCVD
:
2552 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2553 counter
[CM_MRA_COUNTER
]);
2559 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2560 cm_id_priv
->id
.state
;
2561 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2563 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2564 spin_unlock_irq(&cm_id_priv
->lock
);
2567 cm_process_work(cm_id_priv
, work
);
2569 cm_deref_id(cm_id_priv
);
2572 spin_unlock_irq(&cm_id_priv
->lock
);
2573 cm_deref_id(cm_id_priv
);
2577 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2578 struct cm_id_private
*cm_id_priv
,
2579 struct ib_sa_path_rec
*alternate_path
,
2580 const void *private_data
,
2581 u8 private_data_len
)
2583 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2584 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2585 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2586 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2587 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2588 /* todo: need remote CM response timeout */
2589 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2590 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2591 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2592 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2593 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2594 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2595 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2596 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2597 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2598 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2599 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2600 cm_lap_set_local_ack_timeout(lap_msg
,
2601 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2602 alternate_path
->packet_life_time
));
2604 if (private_data
&& private_data_len
)
2605 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2608 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2609 struct ib_sa_path_rec
*alternate_path
,
2610 const void *private_data
,
2611 u8 private_data_len
)
2613 struct cm_id_private
*cm_id_priv
;
2614 struct ib_mad_send_buf
*msg
;
2615 unsigned long flags
;
2618 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2621 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2622 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2623 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2624 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2625 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2630 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2633 cm_id_priv
->alt_av
.timeout
=
2634 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2635 cm_id_priv
->alt_av
.timeout
- 1);
2637 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2641 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2642 alternate_path
, private_data
, private_data_len
);
2643 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2644 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2646 ret
= ib_post_send_mad(msg
, NULL
);
2648 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2653 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2654 cm_id_priv
->msg
= msg
;
2656 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2659 EXPORT_SYMBOL(ib_send_cm_lap
);
2661 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2662 struct ib_sa_path_rec
*path
,
2663 struct cm_lap_msg
*lap_msg
)
2665 memset(path
, 0, sizeof *path
);
2666 path
->dgid
= lap_msg
->alt_local_gid
;
2667 path
->sgid
= lap_msg
->alt_remote_gid
;
2668 path
->dlid
= lap_msg
->alt_local_lid
;
2669 path
->slid
= lap_msg
->alt_remote_lid
;
2670 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2671 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2672 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2673 path
->reversible
= 1;
2674 path
->pkey
= cm_id_priv
->pkey
;
2675 path
->sl
= cm_lap_get_sl(lap_msg
);
2676 path
->mtu_selector
= IB_SA_EQ
;
2677 path
->mtu
= cm_id_priv
->path_mtu
;
2678 path
->rate_selector
= IB_SA_EQ
;
2679 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2680 path
->packet_life_time_selector
= IB_SA_EQ
;
2681 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2682 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2685 static int cm_lap_handler(struct cm_work
*work
)
2687 struct cm_id_private
*cm_id_priv
;
2688 struct cm_lap_msg
*lap_msg
;
2689 struct ib_cm_lap_event_param
*param
;
2690 struct ib_mad_send_buf
*msg
= NULL
;
2693 /* todo: verify LAP request and send reject APR if invalid. */
2694 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2695 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2696 lap_msg
->local_comm_id
);
2700 param
= &work
->cm_event
.param
.lap_rcvd
;
2701 param
->alternate_path
= &work
->path
[0];
2702 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2703 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2705 spin_lock_irq(&cm_id_priv
->lock
);
2706 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2709 switch (cm_id_priv
->id
.lap_state
) {
2710 case IB_CM_LAP_UNINIT
:
2711 case IB_CM_LAP_IDLE
:
2713 case IB_CM_MRA_LAP_SENT
:
2714 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2715 counter
[CM_LAP_COUNTER
]);
2716 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2719 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2720 CM_MSG_RESPONSE_OTHER
,
2721 cm_id_priv
->service_timeout
,
2722 cm_id_priv
->private_data
,
2723 cm_id_priv
->private_data_len
);
2724 spin_unlock_irq(&cm_id_priv
->lock
);
2726 if (ib_post_send_mad(msg
, NULL
))
2729 case IB_CM_LAP_RCVD
:
2730 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2731 counter
[CM_LAP_COUNTER
]);
2737 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2738 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2739 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2740 work
->mad_recv_wc
->recv_buf
.grh
,
2742 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2743 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2745 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2746 spin_unlock_irq(&cm_id_priv
->lock
);
2749 cm_process_work(cm_id_priv
, work
);
2751 cm_deref_id(cm_id_priv
);
2754 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2755 deref
: cm_deref_id(cm_id_priv
);
2759 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2760 struct cm_id_private
*cm_id_priv
,
2761 enum ib_cm_apr_status status
,
2764 const void *private_data
,
2765 u8 private_data_len
)
2767 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2768 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2769 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2770 apr_msg
->ap_status
= (u8
) status
;
2772 if (info
&& info_length
) {
2773 apr_msg
->info_length
= info_length
;
2774 memcpy(apr_msg
->info
, info
, info_length
);
2777 if (private_data
&& private_data_len
)
2778 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2781 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2782 enum ib_cm_apr_status status
,
2785 const void *private_data
,
2786 u8 private_data_len
)
2788 struct cm_id_private
*cm_id_priv
;
2789 struct ib_mad_send_buf
*msg
;
2790 unsigned long flags
;
2793 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2794 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2797 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2798 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2799 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2800 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2801 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2806 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2810 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2811 info
, info_length
, private_data
, private_data_len
);
2812 ret
= ib_post_send_mad(msg
, NULL
);
2814 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2819 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2820 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2823 EXPORT_SYMBOL(ib_send_cm_apr
);
2825 static int cm_apr_handler(struct cm_work
*work
)
2827 struct cm_id_private
*cm_id_priv
;
2828 struct cm_apr_msg
*apr_msg
;
2831 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2832 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2833 apr_msg
->local_comm_id
);
2835 return -EINVAL
; /* Unmatched reply. */
2837 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2838 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2839 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2840 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2842 spin_lock_irq(&cm_id_priv
->lock
);
2843 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2844 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2845 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2846 spin_unlock_irq(&cm_id_priv
->lock
);
2849 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2850 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2851 cm_id_priv
->msg
= NULL
;
2853 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2855 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2856 spin_unlock_irq(&cm_id_priv
->lock
);
2859 cm_process_work(cm_id_priv
, work
);
2861 cm_deref_id(cm_id_priv
);
2864 cm_deref_id(cm_id_priv
);
2868 static int cm_timewait_handler(struct cm_work
*work
)
2870 struct cm_timewait_info
*timewait_info
;
2871 struct cm_id_private
*cm_id_priv
;
2874 timewait_info
= (struct cm_timewait_info
*)work
;
2875 spin_lock_irq(&cm
.lock
);
2876 list_del(&timewait_info
->list
);
2877 spin_unlock_irq(&cm
.lock
);
2879 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2880 timewait_info
->work
.remote_id
);
2884 spin_lock_irq(&cm_id_priv
->lock
);
2885 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2886 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2887 spin_unlock_irq(&cm_id_priv
->lock
);
2890 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2891 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2893 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2894 spin_unlock_irq(&cm_id_priv
->lock
);
2897 cm_process_work(cm_id_priv
, work
);
2899 cm_deref_id(cm_id_priv
);
2902 cm_deref_id(cm_id_priv
);
2906 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2907 struct cm_id_private
*cm_id_priv
,
2908 struct ib_cm_sidr_req_param
*param
)
2910 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2911 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2912 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2913 sidr_req_msg
->pkey
= param
->path
->pkey
;
2914 sidr_req_msg
->service_id
= param
->service_id
;
2916 if (param
->private_data
&& param
->private_data_len
)
2917 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2918 param
->private_data_len
);
2921 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2922 struct ib_cm_sidr_req_param
*param
)
2924 struct cm_id_private
*cm_id_priv
;
2925 struct ib_mad_send_buf
*msg
;
2926 unsigned long flags
;
2929 if (!param
->path
|| (param
->private_data
&&
2930 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2933 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2934 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2938 cm_id
->service_id
= param
->service_id
;
2939 cm_id
->service_mask
= ~cpu_to_be64(0);
2940 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2941 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2942 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2946 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2948 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2949 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2951 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2952 if (cm_id
->state
== IB_CM_IDLE
)
2953 ret
= ib_post_send_mad(msg
, NULL
);
2958 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2962 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2963 cm_id_priv
->msg
= msg
;
2964 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2968 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2970 static void cm_format_sidr_req_event(struct cm_work
*work
,
2971 struct ib_cm_id
*listen_id
)
2973 struct cm_sidr_req_msg
*sidr_req_msg
;
2974 struct ib_cm_sidr_req_event_param
*param
;
2976 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2977 work
->mad_recv_wc
->recv_buf
.mad
;
2978 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2979 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2980 param
->listen_id
= listen_id
;
2981 param
->port
= work
->port
->port_num
;
2982 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2985 static int cm_sidr_req_handler(struct cm_work
*work
)
2987 struct ib_cm_id
*cm_id
;
2988 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2989 struct cm_sidr_req_msg
*sidr_req_msg
;
2992 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
2994 return PTR_ERR(cm_id
);
2995 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2997 /* Record SGID/SLID and request ID for lookup. */
2998 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2999 work
->mad_recv_wc
->recv_buf
.mad
;
3000 wc
= work
->mad_recv_wc
->wc
;
3001 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3002 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3003 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3004 work
->mad_recv_wc
->recv_buf
.grh
,
3006 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
3007 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3008 atomic_inc(&cm_id_priv
->work_count
);
3010 spin_lock_irq(&cm
.lock
);
3011 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3012 if (cur_cm_id_priv
) {
3013 spin_unlock_irq(&cm
.lock
);
3014 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3015 counter
[CM_SIDR_REQ_COUNTER
]);
3016 goto out
; /* Duplicate message. */
3018 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3019 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3020 sidr_req_msg
->service_id
,
3021 sidr_req_msg
->private_data
);
3022 if (!cur_cm_id_priv
) {
3023 spin_unlock_irq(&cm
.lock
);
3024 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3025 goto out
; /* No match. */
3027 atomic_inc(&cur_cm_id_priv
->refcount
);
3028 atomic_inc(&cm_id_priv
->refcount
);
3029 spin_unlock_irq(&cm
.lock
);
3031 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3032 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3033 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3034 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3036 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3037 cm_process_work(cm_id_priv
, work
);
3038 cm_deref_id(cur_cm_id_priv
);
3041 ib_destroy_cm_id(&cm_id_priv
->id
);
3045 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3046 struct cm_id_private
*cm_id_priv
,
3047 struct ib_cm_sidr_rep_param
*param
)
3049 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3051 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3052 sidr_rep_msg
->status
= param
->status
;
3053 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3054 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3055 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3057 if (param
->info
&& param
->info_length
)
3058 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3060 if (param
->private_data
&& param
->private_data_len
)
3061 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3062 param
->private_data_len
);
3065 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3066 struct ib_cm_sidr_rep_param
*param
)
3068 struct cm_id_private
*cm_id_priv
;
3069 struct ib_mad_send_buf
*msg
;
3070 unsigned long flags
;
3073 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3074 (param
->private_data
&&
3075 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3078 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3079 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3080 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3085 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3089 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3091 ret
= ib_post_send_mad(msg
, NULL
);
3093 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3097 cm_id
->state
= IB_CM_IDLE
;
3098 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3100 spin_lock_irqsave(&cm
.lock
, flags
);
3101 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3102 spin_unlock_irqrestore(&cm
.lock
, flags
);
3105 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3108 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3110 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3112 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3113 struct ib_cm_sidr_rep_event_param
*param
;
3115 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3116 work
->mad_recv_wc
->recv_buf
.mad
;
3117 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3118 param
->status
= sidr_rep_msg
->status
;
3119 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3120 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3121 param
->info
= &sidr_rep_msg
->info
;
3122 param
->info_len
= sidr_rep_msg
->info_length
;
3123 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3126 static int cm_sidr_rep_handler(struct cm_work
*work
)
3128 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3129 struct cm_id_private
*cm_id_priv
;
3131 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3132 work
->mad_recv_wc
->recv_buf
.mad
;
3133 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3135 return -EINVAL
; /* Unmatched reply. */
3137 spin_lock_irq(&cm_id_priv
->lock
);
3138 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3139 spin_unlock_irq(&cm_id_priv
->lock
);
3142 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3143 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3144 spin_unlock_irq(&cm_id_priv
->lock
);
3146 cm_format_sidr_rep_event(work
);
3147 cm_process_work(cm_id_priv
, work
);
3150 cm_deref_id(cm_id_priv
);
3154 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3155 enum ib_wc_status wc_status
)
3157 struct cm_id_private
*cm_id_priv
;
3158 struct ib_cm_event cm_event
;
3159 enum ib_cm_state state
;
3162 memset(&cm_event
, 0, sizeof cm_event
);
3163 cm_id_priv
= msg
->context
[0];
3165 /* Discard old sends or ones without a response. */
3166 spin_lock_irq(&cm_id_priv
->lock
);
3167 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3168 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3172 case IB_CM_REQ_SENT
:
3173 case IB_CM_MRA_REQ_RCVD
:
3174 cm_reset_to_idle(cm_id_priv
);
3175 cm_event
.event
= IB_CM_REQ_ERROR
;
3177 case IB_CM_REP_SENT
:
3178 case IB_CM_MRA_REP_RCVD
:
3179 cm_reset_to_idle(cm_id_priv
);
3180 cm_event
.event
= IB_CM_REP_ERROR
;
3182 case IB_CM_DREQ_SENT
:
3183 cm_enter_timewait(cm_id_priv
);
3184 cm_event
.event
= IB_CM_DREQ_ERROR
;
3186 case IB_CM_SIDR_REQ_SENT
:
3187 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3188 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3193 spin_unlock_irq(&cm_id_priv
->lock
);
3194 cm_event
.param
.send_status
= wc_status
;
3196 /* No other events can occur on the cm_id at this point. */
3197 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3200 ib_destroy_cm_id(&cm_id_priv
->id
);
3203 spin_unlock_irq(&cm_id_priv
->lock
);
3207 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3208 struct ib_mad_send_wc
*mad_send_wc
)
3210 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3211 struct cm_port
*port
;
3214 port
= mad_agent
->context
;
3215 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3216 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3219 * If the send was in response to a received message (context[0] is not
3220 * set to a cm_id), and is not a REJ, then it is a send that was
3223 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3226 atomic_long_add(1 + msg
->retries
,
3227 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3229 atomic_long_add(msg
->retries
,
3230 &port
->counter_group
[CM_XMIT_RETRIES
].
3231 counter
[attr_index
]);
3233 switch (mad_send_wc
->status
) {
3235 case IB_WC_WR_FLUSH_ERR
:
3239 if (msg
->context
[0] && msg
->context
[1])
3240 cm_process_send_error(msg
, mad_send_wc
->status
);
3247 static void cm_work_handler(struct work_struct
*_work
)
3249 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3252 switch (work
->cm_event
.event
) {
3253 case IB_CM_REQ_RECEIVED
:
3254 ret
= cm_req_handler(work
);
3256 case IB_CM_MRA_RECEIVED
:
3257 ret
= cm_mra_handler(work
);
3259 case IB_CM_REJ_RECEIVED
:
3260 ret
= cm_rej_handler(work
);
3262 case IB_CM_REP_RECEIVED
:
3263 ret
= cm_rep_handler(work
);
3265 case IB_CM_RTU_RECEIVED
:
3266 ret
= cm_rtu_handler(work
);
3268 case IB_CM_USER_ESTABLISHED
:
3269 ret
= cm_establish_handler(work
);
3271 case IB_CM_DREQ_RECEIVED
:
3272 ret
= cm_dreq_handler(work
);
3274 case IB_CM_DREP_RECEIVED
:
3275 ret
= cm_drep_handler(work
);
3277 case IB_CM_SIDR_REQ_RECEIVED
:
3278 ret
= cm_sidr_req_handler(work
);
3280 case IB_CM_SIDR_REP_RECEIVED
:
3281 ret
= cm_sidr_rep_handler(work
);
3283 case IB_CM_LAP_RECEIVED
:
3284 ret
= cm_lap_handler(work
);
3286 case IB_CM_APR_RECEIVED
:
3287 ret
= cm_apr_handler(work
);
3289 case IB_CM_TIMEWAIT_EXIT
:
3290 ret
= cm_timewait_handler(work
);
3300 static int cm_establish(struct ib_cm_id
*cm_id
)
3302 struct cm_id_private
*cm_id_priv
;
3303 struct cm_work
*work
;
3304 unsigned long flags
;
3307 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3311 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3312 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3313 switch (cm_id
->state
)
3315 case IB_CM_REP_SENT
:
3316 case IB_CM_MRA_REP_RCVD
:
3317 cm_id
->state
= IB_CM_ESTABLISHED
;
3319 case IB_CM_ESTABLISHED
:
3326 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3334 * The CM worker thread may try to destroy the cm_id before it
3335 * can execute this work item. To prevent potential deadlock,
3336 * we need to find the cm_id once we're in the context of the
3337 * worker thread, rather than holding a reference on it.
3339 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3340 work
->local_id
= cm_id
->local_id
;
3341 work
->remote_id
= cm_id
->remote_id
;
3342 work
->mad_recv_wc
= NULL
;
3343 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3344 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3349 static int cm_migrate(struct ib_cm_id
*cm_id
)
3351 struct cm_id_private
*cm_id_priv
;
3352 unsigned long flags
;
3355 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3356 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3357 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3358 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3359 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3360 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3361 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3364 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3369 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3374 case IB_EVENT_COMM_EST
:
3375 ret
= cm_establish(cm_id
);
3377 case IB_EVENT_PATH_MIG
:
3378 ret
= cm_migrate(cm_id
);
3385 EXPORT_SYMBOL(ib_cm_notify
);
3387 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3388 struct ib_mad_recv_wc
*mad_recv_wc
)
3390 struct cm_port
*port
= mad_agent
->context
;
3391 struct cm_work
*work
;
3392 enum ib_cm_event_type event
;
3396 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3397 case CM_REQ_ATTR_ID
:
3398 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3399 alt_local_lid
!= 0);
3400 event
= IB_CM_REQ_RECEIVED
;
3402 case CM_MRA_ATTR_ID
:
3403 event
= IB_CM_MRA_RECEIVED
;
3405 case CM_REJ_ATTR_ID
:
3406 event
= IB_CM_REJ_RECEIVED
;
3408 case CM_REP_ATTR_ID
:
3409 event
= IB_CM_REP_RECEIVED
;
3411 case CM_RTU_ATTR_ID
:
3412 event
= IB_CM_RTU_RECEIVED
;
3414 case CM_DREQ_ATTR_ID
:
3415 event
= IB_CM_DREQ_RECEIVED
;
3417 case CM_DREP_ATTR_ID
:
3418 event
= IB_CM_DREP_RECEIVED
;
3420 case CM_SIDR_REQ_ATTR_ID
:
3421 event
= IB_CM_SIDR_REQ_RECEIVED
;
3423 case CM_SIDR_REP_ATTR_ID
:
3424 event
= IB_CM_SIDR_REP_RECEIVED
;
3426 case CM_LAP_ATTR_ID
:
3428 event
= IB_CM_LAP_RECEIVED
;
3430 case CM_APR_ATTR_ID
:
3431 event
= IB_CM_APR_RECEIVED
;
3434 ib_free_recv_mad(mad_recv_wc
);
3438 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3439 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3440 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3442 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3445 ib_free_recv_mad(mad_recv_wc
);
3449 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3450 work
->cm_event
.event
= event
;
3451 work
->mad_recv_wc
= mad_recv_wc
;
3453 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3456 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3457 struct ib_qp_attr
*qp_attr
,
3460 unsigned long flags
;
3463 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3464 switch (cm_id_priv
->id
.state
) {
3465 case IB_CM_REQ_SENT
:
3466 case IB_CM_MRA_REQ_RCVD
:
3467 case IB_CM_REQ_RCVD
:
3468 case IB_CM_MRA_REQ_SENT
:
3469 case IB_CM_REP_RCVD
:
3470 case IB_CM_MRA_REP_SENT
:
3471 case IB_CM_REP_SENT
:
3472 case IB_CM_MRA_REP_RCVD
:
3473 case IB_CM_ESTABLISHED
:
3474 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3475 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3476 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3477 if (cm_id_priv
->responder_resources
)
3478 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3479 IB_ACCESS_REMOTE_ATOMIC
;
3480 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3481 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3488 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3492 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3493 struct ib_qp_attr
*qp_attr
,
3496 unsigned long flags
;
3499 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3500 switch (cm_id_priv
->id
.state
) {
3501 case IB_CM_REQ_RCVD
:
3502 case IB_CM_MRA_REQ_SENT
:
3503 case IB_CM_REP_RCVD
:
3504 case IB_CM_MRA_REP_SENT
:
3505 case IB_CM_REP_SENT
:
3506 case IB_CM_MRA_REP_RCVD
:
3507 case IB_CM_ESTABLISHED
:
3508 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3509 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3510 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3511 if (!cm_id_priv
->av
.valid
) {
3512 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3515 if (cm_id_priv
->av
.ah_attr
.vlan_id
!= 0xffff) {
3516 qp_attr
->vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
3517 *qp_attr_mask
|= IB_QP_VID
;
3519 if (!is_zero_ether_addr(cm_id_priv
->av
.smac
)) {
3520 memcpy(qp_attr
->smac
, cm_id_priv
->av
.smac
,
3521 sizeof(qp_attr
->smac
));
3522 *qp_attr_mask
|= IB_QP_SMAC
;
3524 if (cm_id_priv
->alt_av
.valid
) {
3525 if (cm_id_priv
->alt_av
.ah_attr
.vlan_id
!= 0xffff) {
3526 qp_attr
->alt_vlan_id
=
3527 cm_id_priv
->alt_av
.ah_attr
.vlan_id
;
3528 *qp_attr_mask
|= IB_QP_ALT_VID
;
3530 if (!is_zero_ether_addr(cm_id_priv
->alt_av
.smac
)) {
3531 memcpy(qp_attr
->alt_smac
,
3532 cm_id_priv
->alt_av
.smac
,
3533 sizeof(qp_attr
->alt_smac
));
3534 *qp_attr_mask
|= IB_QP_ALT_SMAC
;
3537 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3538 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3539 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3540 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3541 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3542 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3543 IB_QP_MIN_RNR_TIMER
;
3544 qp_attr
->max_dest_rd_atomic
=
3545 cm_id_priv
->responder_resources
;
3546 qp_attr
->min_rnr_timer
= 0;
3548 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3549 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3550 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3551 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3552 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3553 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3561 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3565 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3566 struct ib_qp_attr
*qp_attr
,
3569 unsigned long flags
;
3572 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3573 switch (cm_id_priv
->id
.state
) {
3574 /* Allow transition to RTS before sending REP */
3575 case IB_CM_REQ_RCVD
:
3576 case IB_CM_MRA_REQ_SENT
:
3578 case IB_CM_REP_RCVD
:
3579 case IB_CM_MRA_REP_SENT
:
3580 case IB_CM_REP_SENT
:
3581 case IB_CM_MRA_REP_RCVD
:
3582 case IB_CM_ESTABLISHED
:
3583 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3584 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3585 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3586 switch (cm_id_priv
->qp_type
) {
3588 case IB_QPT_XRC_INI
:
3589 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3590 IB_QP_MAX_QP_RD_ATOMIC
;
3591 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3592 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3593 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3595 case IB_QPT_XRC_TGT
:
3596 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3597 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3602 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3603 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3604 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3607 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3608 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3609 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3610 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3611 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3612 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3620 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3624 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3625 struct ib_qp_attr
*qp_attr
,
3628 struct cm_id_private
*cm_id_priv
;
3631 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3632 switch (qp_attr
->qp_state
) {
3634 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3637 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3640 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3648 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3650 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3652 struct ib_device_attr attr
;
3654 if (ib_query_device(cm_dev
->ib_device
, &attr
))
3655 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3657 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3660 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3663 struct cm_counter_group
*group
;
3664 struct cm_counter_attribute
*cm_attr
;
3666 group
= container_of(obj
, struct cm_counter_group
, obj
);
3667 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3669 return sprintf(buf
, "%ld\n",
3670 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3673 static const struct sysfs_ops cm_counter_ops
= {
3674 .show
= cm_show_counter
3677 static struct kobj_type cm_counter_obj_type
= {
3678 .sysfs_ops
= &cm_counter_ops
,
3679 .default_attrs
= cm_counter_default_attrs
3682 static void cm_release_port_obj(struct kobject
*obj
)
3684 struct cm_port
*cm_port
;
3686 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3690 static struct kobj_type cm_port_obj_type
= {
3691 .release
= cm_release_port_obj
3694 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3698 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3701 struct class cm_class
= {
3702 .owner
= THIS_MODULE
,
3703 .name
= "infiniband_cm",
3704 .devnode
= cm_devnode
,
3706 EXPORT_SYMBOL(cm_class
);
3708 static int cm_create_port_fs(struct cm_port
*port
)
3712 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3713 &port
->cm_dev
->device
->kobj
,
3714 "%d", port
->port_num
);
3720 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3721 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3722 &cm_counter_obj_type
,
3724 "%s", counter_group_names
[i
]);
3733 kobject_put(&port
->counter_group
[i
].obj
);
3734 kobject_put(&port
->port_obj
);
3739 static void cm_remove_port_fs(struct cm_port
*port
)
3743 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3744 kobject_put(&port
->counter_group
[i
].obj
);
3746 kobject_put(&port
->port_obj
);
3749 static void cm_add_one(struct ib_device
*ib_device
)
3751 struct cm_device
*cm_dev
;
3752 struct cm_port
*port
;
3753 struct ib_mad_reg_req reg_req
= {
3754 .mgmt_class
= IB_MGMT_CLASS_CM
,
3755 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
3757 struct ib_port_modify port_modify
= {
3758 .set_port_cap_mask
= IB_PORT_CM_SUP
3760 unsigned long flags
;
3765 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3766 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3770 cm_dev
->ib_device
= ib_device
;
3771 cm_get_ack_delay(cm_dev
);
3773 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3775 "%s", ib_device
->name
);
3776 if (IS_ERR(cm_dev
->device
)) {
3781 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3782 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3783 if (!rdma_cap_ib_cm(ib_device
, i
))
3786 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3790 cm_dev
->port
[i
-1] = port
;
3791 port
->cm_dev
= cm_dev
;
3794 ret
= cm_create_port_fs(port
);
3798 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3806 if (IS_ERR(port
->mad_agent
))
3809 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3819 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3821 write_lock_irqsave(&cm
.device_lock
, flags
);
3822 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3823 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3827 ib_unregister_mad_agent(port
->mad_agent
);
3829 cm_remove_port_fs(port
);
3831 port_modify
.set_port_cap_mask
= 0;
3832 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3834 if (!rdma_cap_ib_cm(ib_device
, i
))
3837 port
= cm_dev
->port
[i
-1];
3838 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3839 ib_unregister_mad_agent(port
->mad_agent
);
3840 cm_remove_port_fs(port
);
3843 device_unregister(cm_dev
->device
);
3847 static void cm_remove_one(struct ib_device
*ib_device
)
3849 struct cm_device
*cm_dev
;
3850 struct cm_port
*port
;
3851 struct ib_port_modify port_modify
= {
3852 .clr_port_cap_mask
= IB_PORT_CM_SUP
3854 unsigned long flags
;
3857 cm_dev
= ib_get_client_data(ib_device
, &cm_client
);
3861 write_lock_irqsave(&cm
.device_lock
, flags
);
3862 list_del(&cm_dev
->list
);
3863 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3865 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3866 if (!rdma_cap_ib_cm(ib_device
, i
))
3869 port
= cm_dev
->port
[i
-1];
3870 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3871 ib_unregister_mad_agent(port
->mad_agent
);
3872 flush_workqueue(cm
.wq
);
3873 cm_remove_port_fs(port
);
3875 device_unregister(cm_dev
->device
);
3879 static int __init
ib_cm_init(void)
3883 memset(&cm
, 0, sizeof cm
);
3884 INIT_LIST_HEAD(&cm
.device_list
);
3885 rwlock_init(&cm
.device_lock
);
3886 spin_lock_init(&cm
.lock
);
3887 cm
.listen_service_table
= RB_ROOT
;
3888 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3889 cm
.remote_id_table
= RB_ROOT
;
3890 cm
.remote_qp_table
= RB_ROOT
;
3891 cm
.remote_sidr_table
= RB_ROOT
;
3892 idr_init(&cm
.local_id_table
);
3893 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3894 INIT_LIST_HEAD(&cm
.timewait_list
);
3896 ret
= class_register(&cm_class
);
3902 cm
.wq
= create_workqueue("ib_cm");
3908 ret
= ib_register_client(&cm_client
);
3914 destroy_workqueue(cm
.wq
);
3916 class_unregister(&cm_class
);
3918 idr_destroy(&cm
.local_id_table
);
3922 static void __exit
ib_cm_cleanup(void)
3924 struct cm_timewait_info
*timewait_info
, *tmp
;
3926 spin_lock_irq(&cm
.lock
);
3927 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3928 cancel_delayed_work(&timewait_info
->work
.work
);
3929 spin_unlock_irq(&cm
.lock
);
3931 ib_unregister_client(&cm_client
);
3932 destroy_workqueue(cm
.wq
);
3934 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3935 list_del(&timewait_info
->list
);
3936 kfree(timewait_info
);
3939 class_unregister(&cm_class
);
3940 idr_destroy(&cm
.local_id_table
);
3943 module_init(ib_cm_init
);
3944 module_exit(ib_cm_cleanup
);