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(u8
*dst
, u8
*src
, u8
*mask
)
444 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
445 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
446 ((unsigned long *) mask
)[i
];
449 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
450 struct ib_cm_compare_data
*dst_data
)
452 u8 src
[IB_CM_COMPARE_SIZE
];
453 u8 dst
[IB_CM_COMPARE_SIZE
];
455 if (!src_data
|| !dst_data
)
458 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
459 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
460 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
463 static int cm_compare_private_data(u8
*private_data
,
464 struct ib_cm_compare_data
*dst_data
)
466 u8 src
[IB_CM_COMPARE_SIZE
];
471 cm_mask_copy(src
, private_data
, dst_data
->mask
);
472 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
476 * Trivial helpers to strip endian annotation and compare; the
477 * endianness doesn't actually matter since we just need a stable
478 * order for the RB tree.
480 static int be32_lt(__be32 a
, __be32 b
)
482 return (__force u32
) a
< (__force u32
) b
;
485 static int be32_gt(__be32 a
, __be32 b
)
487 return (__force u32
) a
> (__force u32
) b
;
490 static int be64_lt(__be64 a
, __be64 b
)
492 return (__force u64
) a
< (__force u64
) b
;
495 static int be64_gt(__be64 a
, __be64 b
)
497 return (__force u64
) a
> (__force u64
) b
;
500 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
502 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
503 struct rb_node
*parent
= NULL
;
504 struct cm_id_private
*cur_cm_id_priv
;
505 __be64 service_id
= cm_id_priv
->id
.service_id
;
506 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
511 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
513 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
514 cur_cm_id_priv
->compare_data
);
515 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
516 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
517 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
519 return cur_cm_id_priv
;
521 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
522 link
= &(*link
)->rb_left
;
523 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
524 link
= &(*link
)->rb_right
;
525 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
526 link
= &(*link
)->rb_left
;
527 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
528 link
= &(*link
)->rb_right
;
529 else if (data_cmp
< 0)
530 link
= &(*link
)->rb_left
;
532 link
= &(*link
)->rb_right
;
534 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
535 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
539 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
543 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
544 struct cm_id_private
*cm_id_priv
;
548 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
549 data_cmp
= cm_compare_private_data(private_data
,
550 cm_id_priv
->compare_data
);
551 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
552 cm_id_priv
->id
.service_id
&&
553 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
556 if (device
< cm_id_priv
->id
.device
)
557 node
= node
->rb_left
;
558 else if (device
> cm_id_priv
->id
.device
)
559 node
= node
->rb_right
;
560 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
561 node
= node
->rb_left
;
562 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
563 node
= node
->rb_right
;
564 else if (data_cmp
< 0)
565 node
= node
->rb_left
;
567 node
= node
->rb_right
;
572 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
575 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
576 struct rb_node
*parent
= NULL
;
577 struct cm_timewait_info
*cur_timewait_info
;
578 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
579 __be32 remote_id
= timewait_info
->work
.remote_id
;
583 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
585 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
586 link
= &(*link
)->rb_left
;
587 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
588 link
= &(*link
)->rb_right
;
589 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
590 link
= &(*link
)->rb_left
;
591 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
592 link
= &(*link
)->rb_right
;
594 return cur_timewait_info
;
596 timewait_info
->inserted_remote_id
= 1;
597 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
598 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
602 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
605 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
606 struct cm_timewait_info
*timewait_info
;
609 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
611 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
612 node
= node
->rb_left
;
613 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
614 node
= node
->rb_right
;
615 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
616 node
= node
->rb_left
;
617 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
618 node
= node
->rb_right
;
620 return timewait_info
;
625 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
628 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
629 struct rb_node
*parent
= NULL
;
630 struct cm_timewait_info
*cur_timewait_info
;
631 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
632 __be32 remote_qpn
= timewait_info
->remote_qpn
;
636 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
638 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
639 link
= &(*link
)->rb_left
;
640 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
641 link
= &(*link
)->rb_right
;
642 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
643 link
= &(*link
)->rb_left
;
644 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
645 link
= &(*link
)->rb_right
;
647 return cur_timewait_info
;
649 timewait_info
->inserted_remote_qp
= 1;
650 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
651 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
655 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
658 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
659 struct rb_node
*parent
= NULL
;
660 struct cm_id_private
*cur_cm_id_priv
;
661 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
662 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
666 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
668 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
669 link
= &(*link
)->rb_left
;
670 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
671 link
= &(*link
)->rb_right
;
674 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
677 link
= &(*link
)->rb_left
;
679 link
= &(*link
)->rb_right
;
681 return cur_cm_id_priv
;
684 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
685 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
689 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
690 enum ib_cm_sidr_status status
)
692 struct ib_cm_sidr_rep_param param
;
694 memset(¶m
, 0, sizeof param
);
695 param
.status
= status
;
696 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
699 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
700 ib_cm_handler cm_handler
,
703 struct cm_id_private
*cm_id_priv
;
706 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
708 return ERR_PTR(-ENOMEM
);
710 cm_id_priv
->id
.state
= IB_CM_IDLE
;
711 cm_id_priv
->id
.device
= device
;
712 cm_id_priv
->id
.cm_handler
= cm_handler
;
713 cm_id_priv
->id
.context
= context
;
714 cm_id_priv
->id
.remote_cm_qpn
= 1;
715 ret
= cm_alloc_id(cm_id_priv
);
719 spin_lock_init(&cm_id_priv
->lock
);
720 init_completion(&cm_id_priv
->comp
);
721 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
722 atomic_set(&cm_id_priv
->work_count
, -1);
723 atomic_set(&cm_id_priv
->refcount
, 1);
724 return &cm_id_priv
->id
;
728 return ERR_PTR(-ENOMEM
);
730 EXPORT_SYMBOL(ib_create_cm_id
);
732 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
734 struct cm_work
*work
;
736 if (list_empty(&cm_id_priv
->work_list
))
739 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
740 list_del(&work
->list
);
744 static void cm_free_work(struct cm_work
*work
)
746 if (work
->mad_recv_wc
)
747 ib_free_recv_mad(work
->mad_recv_wc
);
751 static inline int cm_convert_to_ms(int iba_time
)
753 /* approximate conversion to ms from 4.096us x 2^iba_time */
754 return 1 << max(iba_time
- 8, 0);
758 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
759 * Because of how ack_timeout is stored, adding one doubles the timeout.
760 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
761 * increment it (round up) only if the other is within 50%.
763 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
765 int ack_timeout
= packet_life_time
+ 1;
767 if (ack_timeout
>= ca_ack_delay
)
768 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
770 ack_timeout
= ca_ack_delay
+
771 (ack_timeout
>= (ca_ack_delay
- 1));
773 return min(31, ack_timeout
);
776 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
778 if (timewait_info
->inserted_remote_id
) {
779 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
780 timewait_info
->inserted_remote_id
= 0;
783 if (timewait_info
->inserted_remote_qp
) {
784 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
785 timewait_info
->inserted_remote_qp
= 0;
789 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
791 struct cm_timewait_info
*timewait_info
;
793 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
795 return ERR_PTR(-ENOMEM
);
797 timewait_info
->work
.local_id
= local_id
;
798 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
799 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
800 return timewait_info
;
803 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
808 spin_lock_irqsave(&cm
.lock
, flags
);
809 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
810 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
811 spin_unlock_irqrestore(&cm
.lock
, flags
);
814 * The cm_id could be destroyed by the user before we exit timewait.
815 * To protect against this, we search for the cm_id after exiting
816 * timewait before notifying the user that we've exited timewait.
818 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
819 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
820 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
821 msecs_to_jiffies(wait_time
));
822 cm_id_priv
->timewait_info
= NULL
;
825 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
829 cm_id_priv
->id
.state
= IB_CM_IDLE
;
830 if (cm_id_priv
->timewait_info
) {
831 spin_lock_irqsave(&cm
.lock
, flags
);
832 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
833 spin_unlock_irqrestore(&cm
.lock
, flags
);
834 kfree(cm_id_priv
->timewait_info
);
835 cm_id_priv
->timewait_info
= NULL
;
839 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
841 struct cm_id_private
*cm_id_priv
;
842 struct cm_work
*work
;
844 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
846 spin_lock_irq(&cm_id_priv
->lock
);
847 switch (cm_id
->state
) {
849 cm_id
->state
= IB_CM_IDLE
;
850 spin_unlock_irq(&cm_id_priv
->lock
);
851 spin_lock_irq(&cm
.lock
);
852 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
853 spin_unlock_irq(&cm
.lock
);
855 case IB_CM_SIDR_REQ_SENT
:
856 cm_id
->state
= IB_CM_IDLE
;
857 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
858 spin_unlock_irq(&cm_id_priv
->lock
);
860 case IB_CM_SIDR_REQ_RCVD
:
861 spin_unlock_irq(&cm_id_priv
->lock
);
862 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
863 spin_lock_irq(&cm
.lock
);
864 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
))
865 rb_erase(&cm_id_priv
->sidr_id_node
,
866 &cm
.remote_sidr_table
);
867 spin_unlock_irq(&cm
.lock
);
870 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
871 spin_unlock_irq(&cm_id_priv
->lock
);
872 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
873 &cm_id_priv
->id
.device
->node_guid
,
874 sizeof cm_id_priv
->id
.device
->node_guid
,
878 if (err
== -ENOMEM
) {
879 /* Do not reject to allow future retries. */
880 cm_reset_to_idle(cm_id_priv
);
881 spin_unlock_irq(&cm_id_priv
->lock
);
883 spin_unlock_irq(&cm_id_priv
->lock
);
884 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
888 case IB_CM_MRA_REQ_RCVD
:
890 case IB_CM_MRA_REP_RCVD
:
891 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
893 case IB_CM_MRA_REQ_SENT
:
895 case IB_CM_MRA_REP_SENT
:
896 spin_unlock_irq(&cm_id_priv
->lock
);
897 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
900 case IB_CM_ESTABLISHED
:
901 spin_unlock_irq(&cm_id_priv
->lock
);
902 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
904 ib_send_cm_dreq(cm_id
, NULL
, 0);
906 case IB_CM_DREQ_SENT
:
907 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
908 cm_enter_timewait(cm_id_priv
);
909 spin_unlock_irq(&cm_id_priv
->lock
);
911 case IB_CM_DREQ_RCVD
:
912 spin_unlock_irq(&cm_id_priv
->lock
);
913 ib_send_cm_drep(cm_id
, NULL
, 0);
916 spin_unlock_irq(&cm_id_priv
->lock
);
920 cm_free_id(cm_id
->local_id
);
921 cm_deref_id(cm_id_priv
);
922 wait_for_completion(&cm_id_priv
->comp
);
923 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
925 kfree(cm_id_priv
->compare_data
);
926 kfree(cm_id_priv
->private_data
);
930 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
932 cm_destroy_id(cm_id
, 0);
934 EXPORT_SYMBOL(ib_destroy_cm_id
);
936 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
937 struct ib_cm_compare_data
*compare_data
)
939 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
943 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
944 service_id
&= service_mask
;
945 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
946 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
949 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
950 if (cm_id
->state
!= IB_CM_IDLE
)
954 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
956 if (!cm_id_priv
->compare_data
)
958 cm_mask_copy(cm_id_priv
->compare_data
->data
,
959 compare_data
->data
, compare_data
->mask
);
960 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
964 cm_id
->state
= IB_CM_LISTEN
;
966 spin_lock_irqsave(&cm
.lock
, flags
);
967 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
968 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
969 cm_id
->service_mask
= ~cpu_to_be64(0);
971 cm_id
->service_id
= service_id
;
972 cm_id
->service_mask
= service_mask
;
974 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
975 spin_unlock_irqrestore(&cm
.lock
, flags
);
977 if (cur_cm_id_priv
) {
978 cm_id
->state
= IB_CM_IDLE
;
979 kfree(cm_id_priv
->compare_data
);
980 cm_id_priv
->compare_data
= NULL
;
985 EXPORT_SYMBOL(ib_cm_listen
);
987 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
988 enum cm_msg_sequence msg_seq
)
992 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
993 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
995 return cpu_to_be64(hi_tid
| low_tid
);
998 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
999 __be16 attr_id
, __be64 tid
)
1001 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1002 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1003 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1004 hdr
->method
= IB_MGMT_METHOD_SEND
;
1005 hdr
->attr_id
= attr_id
;
1009 static void cm_format_req(struct cm_req_msg
*req_msg
,
1010 struct cm_id_private
*cm_id_priv
,
1011 struct ib_cm_req_param
*param
)
1013 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1014 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1016 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1017 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1019 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1020 req_msg
->service_id
= param
->service_id
;
1021 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1022 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1023 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1024 cm_req_set_remote_resp_timeout(req_msg
,
1025 param
->remote_cm_response_timeout
);
1026 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1027 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1028 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1029 cm_req_set_local_resp_timeout(req_msg
,
1030 param
->local_cm_response_timeout
);
1031 req_msg
->pkey
= param
->primary_path
->pkey
;
1032 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1033 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1035 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1036 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1037 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1038 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1039 cm_req_set_srq(req_msg
, param
->srq
);
1042 if (pri_path
->hop_limit
<= 1) {
1043 req_msg
->primary_local_lid
= pri_path
->slid
;
1044 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1046 /* Work-around until there's a way to obtain remote LID info */
1047 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1048 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1050 req_msg
->primary_local_gid
= pri_path
->sgid
;
1051 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1052 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1053 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1054 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1055 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1056 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1057 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1058 cm_req_set_primary_local_ack_timeout(req_msg
,
1059 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1060 pri_path
->packet_life_time
));
1063 if (alt_path
->hop_limit
<= 1) {
1064 req_msg
->alt_local_lid
= alt_path
->slid
;
1065 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1067 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1068 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1070 req_msg
->alt_local_gid
= alt_path
->sgid
;
1071 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1072 cm_req_set_alt_flow_label(req_msg
,
1073 alt_path
->flow_label
);
1074 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1075 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1076 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1077 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1078 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1079 cm_req_set_alt_local_ack_timeout(req_msg
,
1080 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1081 alt_path
->packet_life_time
));
1084 if (param
->private_data
&& param
->private_data_len
)
1085 memcpy(req_msg
->private_data
, param
->private_data
,
1086 param
->private_data_len
);
1089 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1091 /* peer-to-peer not supported */
1092 if (param
->peer_to_peer
)
1095 if (!param
->primary_path
)
1098 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1099 param
->qp_type
!= IB_QPT_XRC_INI
)
1102 if (param
->private_data
&&
1103 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1106 if (param
->alternate_path
&&
1107 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1108 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1114 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1115 struct ib_cm_req_param
*param
)
1117 struct cm_id_private
*cm_id_priv
;
1118 struct cm_req_msg
*req_msg
;
1119 unsigned long flags
;
1122 ret
= cm_validate_req_param(param
);
1126 /* Verify that we're not in timewait. */
1127 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1128 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1129 if (cm_id
->state
!= IB_CM_IDLE
) {
1130 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1134 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1136 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1138 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1139 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1143 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1146 if (param
->alternate_path
) {
1147 ret
= cm_init_av_by_path(param
->alternate_path
,
1148 &cm_id_priv
->alt_av
);
1152 cm_id
->service_id
= param
->service_id
;
1153 cm_id
->service_mask
= ~cpu_to_be64(0);
1154 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1155 param
->primary_path
->packet_life_time
) * 2 +
1157 param
->remote_cm_response_timeout
);
1158 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1159 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1160 cm_id_priv
->responder_resources
= param
->responder_resources
;
1161 cm_id_priv
->retry_count
= param
->retry_count
;
1162 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1163 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1164 cm_id_priv
->qp_type
= param
->qp_type
;
1166 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1170 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1171 cm_format_req(req_msg
, cm_id_priv
, param
);
1172 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1173 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1174 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1176 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1177 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1179 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1180 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1182 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1185 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1186 cm_id
->state
= IB_CM_REQ_SENT
;
1187 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1190 error2
: cm_free_msg(cm_id_priv
->msg
);
1191 error1
: kfree(cm_id_priv
->timewait_info
);
1194 EXPORT_SYMBOL(ib_send_cm_req
);
1196 static int cm_issue_rej(struct cm_port
*port
,
1197 struct ib_mad_recv_wc
*mad_recv_wc
,
1198 enum ib_cm_rej_reason reason
,
1199 enum cm_msg_response msg_rejected
,
1200 void *ari
, u8 ari_length
)
1202 struct ib_mad_send_buf
*msg
= NULL
;
1203 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1206 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1210 /* We just need common CM header information. Cast to any message. */
1211 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1212 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1214 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1215 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1216 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1217 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1218 rej_msg
->reason
= cpu_to_be16(reason
);
1220 if (ari
&& ari_length
) {
1221 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1222 memcpy(rej_msg
->ari
, ari
, ari_length
);
1225 ret
= ib_post_send_mad(msg
, NULL
);
1232 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1233 __be32 local_qpn
, __be32 remote_qpn
)
1235 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1236 ((local_ca_guid
== remote_ca_guid
) &&
1237 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1240 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1241 struct ib_sa_path_rec
*primary_path
,
1242 struct ib_sa_path_rec
*alt_path
)
1244 memset(primary_path
, 0, sizeof *primary_path
);
1245 primary_path
->dgid
= req_msg
->primary_local_gid
;
1246 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1247 primary_path
->dlid
= req_msg
->primary_local_lid
;
1248 primary_path
->slid
= req_msg
->primary_remote_lid
;
1249 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1250 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1251 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1252 primary_path
->reversible
= 1;
1253 primary_path
->pkey
= req_msg
->pkey
;
1254 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1255 primary_path
->mtu_selector
= IB_SA_EQ
;
1256 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1257 primary_path
->rate_selector
= IB_SA_EQ
;
1258 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1259 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1260 primary_path
->packet_life_time
=
1261 cm_req_get_primary_local_ack_timeout(req_msg
);
1262 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1264 if (req_msg
->alt_local_lid
) {
1265 memset(alt_path
, 0, sizeof *alt_path
);
1266 alt_path
->dgid
= req_msg
->alt_local_gid
;
1267 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1268 alt_path
->dlid
= req_msg
->alt_local_lid
;
1269 alt_path
->slid
= req_msg
->alt_remote_lid
;
1270 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1271 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1272 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1273 alt_path
->reversible
= 1;
1274 alt_path
->pkey
= req_msg
->pkey
;
1275 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1276 alt_path
->mtu_selector
= IB_SA_EQ
;
1277 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1278 alt_path
->rate_selector
= IB_SA_EQ
;
1279 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1280 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1281 alt_path
->packet_life_time
=
1282 cm_req_get_alt_local_ack_timeout(req_msg
);
1283 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1287 static void cm_format_req_event(struct cm_work
*work
,
1288 struct cm_id_private
*cm_id_priv
,
1289 struct ib_cm_id
*listen_id
)
1291 struct cm_req_msg
*req_msg
;
1292 struct ib_cm_req_event_param
*param
;
1294 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1295 param
= &work
->cm_event
.param
.req_rcvd
;
1296 param
->listen_id
= listen_id
;
1297 param
->port
= cm_id_priv
->av
.port
->port_num
;
1298 param
->primary_path
= &work
->path
[0];
1299 if (req_msg
->alt_local_lid
)
1300 param
->alternate_path
= &work
->path
[1];
1302 param
->alternate_path
= NULL
;
1303 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1304 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1305 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1306 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1307 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1308 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1309 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1310 param
->local_cm_response_timeout
=
1311 cm_req_get_remote_resp_timeout(req_msg
);
1312 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1313 param
->remote_cm_response_timeout
=
1314 cm_req_get_local_resp_timeout(req_msg
);
1315 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1316 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1317 param
->srq
= cm_req_get_srq(req_msg
);
1318 work
->cm_event
.private_data
= &req_msg
->private_data
;
1321 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1322 struct cm_work
*work
)
1326 /* We will typically only have the current event to report. */
1327 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1330 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1331 spin_lock_irq(&cm_id_priv
->lock
);
1332 work
= cm_dequeue_work(cm_id_priv
);
1333 spin_unlock_irq(&cm_id_priv
->lock
);
1335 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1339 cm_deref_id(cm_id_priv
);
1341 cm_destroy_id(&cm_id_priv
->id
, ret
);
1344 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1345 struct cm_id_private
*cm_id_priv
,
1346 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1347 const void *private_data
, u8 private_data_len
)
1349 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1350 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1351 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1352 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1353 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1355 if (private_data
&& private_data_len
)
1356 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1359 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1360 struct cm_id_private
*cm_id_priv
,
1361 enum ib_cm_rej_reason reason
,
1364 const void *private_data
,
1365 u8 private_data_len
)
1367 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1368 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1370 switch(cm_id_priv
->id
.state
) {
1371 case IB_CM_REQ_RCVD
:
1372 rej_msg
->local_comm_id
= 0;
1373 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1375 case IB_CM_MRA_REQ_SENT
:
1376 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1377 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1379 case IB_CM_REP_RCVD
:
1380 case IB_CM_MRA_REP_SENT
:
1381 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1382 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1385 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1386 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1390 rej_msg
->reason
= cpu_to_be16(reason
);
1391 if (ari
&& ari_length
) {
1392 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1393 memcpy(rej_msg
->ari
, ari
, ari_length
);
1396 if (private_data
&& private_data_len
)
1397 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1400 static void cm_dup_req_handler(struct cm_work
*work
,
1401 struct cm_id_private
*cm_id_priv
)
1403 struct ib_mad_send_buf
*msg
= NULL
;
1406 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1407 counter
[CM_REQ_COUNTER
]);
1409 /* Quick state check to discard duplicate REQs. */
1410 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1413 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1417 spin_lock_irq(&cm_id_priv
->lock
);
1418 switch (cm_id_priv
->id
.state
) {
1419 case IB_CM_MRA_REQ_SENT
:
1420 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1421 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1422 cm_id_priv
->private_data
,
1423 cm_id_priv
->private_data_len
);
1425 case IB_CM_TIMEWAIT
:
1426 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1427 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1432 spin_unlock_irq(&cm_id_priv
->lock
);
1434 ret
= ib_post_send_mad(msg
, NULL
);
1439 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1440 free
: cm_free_msg(msg
);
1443 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1444 struct cm_id_private
*cm_id_priv
)
1446 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1447 struct cm_timewait_info
*timewait_info
;
1448 struct cm_req_msg
*req_msg
;
1450 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1452 /* Check for possible duplicate REQ. */
1453 spin_lock_irq(&cm
.lock
);
1454 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1455 if (timewait_info
) {
1456 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1457 timewait_info
->work
.remote_id
);
1458 spin_unlock_irq(&cm
.lock
);
1459 if (cur_cm_id_priv
) {
1460 cm_dup_req_handler(work
, cur_cm_id_priv
);
1461 cm_deref_id(cur_cm_id_priv
);
1466 /* Check for stale connections. */
1467 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1468 if (timewait_info
) {
1469 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1470 spin_unlock_irq(&cm
.lock
);
1471 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1472 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1477 /* Find matching listen request. */
1478 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1479 req_msg
->service_id
,
1480 req_msg
->private_data
);
1481 if (!listen_cm_id_priv
) {
1482 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1483 spin_unlock_irq(&cm
.lock
);
1484 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1485 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1489 atomic_inc(&listen_cm_id_priv
->refcount
);
1490 atomic_inc(&cm_id_priv
->refcount
);
1491 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1492 atomic_inc(&cm_id_priv
->work_count
);
1493 spin_unlock_irq(&cm
.lock
);
1495 return listen_cm_id_priv
;
1499 * Work-around for inter-subnet connections. If the LIDs are permissive,
1500 * we need to override the LID/SL data in the REQ with the LID information
1501 * in the work completion.
1503 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1505 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1506 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1507 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1508 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1511 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1512 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1515 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1516 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1517 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1518 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1521 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1522 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1526 static int cm_req_handler(struct cm_work
*work
)
1528 struct ib_cm_id
*cm_id
;
1529 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1530 struct cm_req_msg
*req_msg
;
1533 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1535 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1537 return PTR_ERR(cm_id
);
1539 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1540 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1541 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1542 work
->mad_recv_wc
->recv_buf
.grh
,
1544 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1546 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1547 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1550 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1551 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1552 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1554 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1555 if (!listen_cm_id_priv
) {
1557 kfree(cm_id_priv
->timewait_info
);
1561 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1562 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1563 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1564 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1566 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1567 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1569 memcpy(work
->path
[0].dmac
, cm_id_priv
->av
.ah_attr
.dmac
, ETH_ALEN
);
1570 work
->path
[0].vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
1571 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1573 ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1574 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1575 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1576 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1580 if (req_msg
->alt_local_lid
) {
1581 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1583 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1584 &work
->path
[0].sgid
,
1585 sizeof work
->path
[0].sgid
, NULL
, 0);
1589 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1590 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1591 cm_req_get_local_resp_timeout(req_msg
));
1592 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1593 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1594 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1595 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1596 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1597 cm_id_priv
->pkey
= req_msg
->pkey
;
1598 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1599 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1600 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1601 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1603 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1604 cm_process_work(cm_id_priv
, work
);
1605 cm_deref_id(listen_cm_id_priv
);
1609 atomic_dec(&cm_id_priv
->refcount
);
1610 cm_deref_id(listen_cm_id_priv
);
1612 ib_destroy_cm_id(cm_id
);
1616 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1617 struct cm_id_private
*cm_id_priv
,
1618 struct ib_cm_rep_param
*param
)
1620 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1621 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1622 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1623 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1624 rep_msg
->resp_resources
= param
->responder_resources
;
1625 cm_rep_set_target_ack_delay(rep_msg
,
1626 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1627 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1628 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1629 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1631 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1632 rep_msg
->initiator_depth
= param
->initiator_depth
;
1633 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1634 cm_rep_set_srq(rep_msg
, param
->srq
);
1635 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1637 cm_rep_set_srq(rep_msg
, 1);
1638 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1641 if (param
->private_data
&& param
->private_data_len
)
1642 memcpy(rep_msg
->private_data
, param
->private_data
,
1643 param
->private_data_len
);
1646 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1647 struct ib_cm_rep_param
*param
)
1649 struct cm_id_private
*cm_id_priv
;
1650 struct ib_mad_send_buf
*msg
;
1651 struct cm_rep_msg
*rep_msg
;
1652 unsigned long flags
;
1655 if (param
->private_data
&&
1656 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1659 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1660 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1661 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1662 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1667 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1671 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1672 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1673 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1674 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1676 ret
= ib_post_send_mad(msg
, NULL
);
1678 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1683 cm_id
->state
= IB_CM_REP_SENT
;
1684 cm_id_priv
->msg
= msg
;
1685 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1686 cm_id_priv
->responder_resources
= param
->responder_resources
;
1687 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1688 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1690 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1693 EXPORT_SYMBOL(ib_send_cm_rep
);
1695 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1696 struct cm_id_private
*cm_id_priv
,
1697 const void *private_data
,
1698 u8 private_data_len
)
1700 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1701 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1702 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1704 if (private_data
&& private_data_len
)
1705 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1708 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1709 const void *private_data
,
1710 u8 private_data_len
)
1712 struct cm_id_private
*cm_id_priv
;
1713 struct ib_mad_send_buf
*msg
;
1714 unsigned long flags
;
1718 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1721 data
= cm_copy_private_data(private_data
, private_data_len
);
1723 return PTR_ERR(data
);
1725 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1726 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1727 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1728 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1733 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1737 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1738 private_data
, private_data_len
);
1740 ret
= ib_post_send_mad(msg
, NULL
);
1742 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1748 cm_id
->state
= IB_CM_ESTABLISHED
;
1749 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1750 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1753 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1757 EXPORT_SYMBOL(ib_send_cm_rtu
);
1759 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
1761 struct cm_rep_msg
*rep_msg
;
1762 struct ib_cm_rep_event_param
*param
;
1764 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1765 param
= &work
->cm_event
.param
.rep_rcvd
;
1766 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1767 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1768 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
1769 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1770 param
->responder_resources
= rep_msg
->initiator_depth
;
1771 param
->initiator_depth
= rep_msg
->resp_resources
;
1772 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1773 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1774 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1775 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1776 param
->srq
= cm_rep_get_srq(rep_msg
);
1777 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1780 static void cm_dup_rep_handler(struct cm_work
*work
)
1782 struct cm_id_private
*cm_id_priv
;
1783 struct cm_rep_msg
*rep_msg
;
1784 struct ib_mad_send_buf
*msg
= NULL
;
1787 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1788 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1789 rep_msg
->local_comm_id
);
1793 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1794 counter
[CM_REP_COUNTER
]);
1795 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1799 spin_lock_irq(&cm_id_priv
->lock
);
1800 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1801 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1802 cm_id_priv
->private_data
,
1803 cm_id_priv
->private_data_len
);
1804 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1805 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1806 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1807 cm_id_priv
->private_data
,
1808 cm_id_priv
->private_data_len
);
1811 spin_unlock_irq(&cm_id_priv
->lock
);
1813 ret
= ib_post_send_mad(msg
, NULL
);
1818 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1819 free
: cm_free_msg(msg
);
1820 deref
: cm_deref_id(cm_id_priv
);
1823 static int cm_rep_handler(struct cm_work
*work
)
1825 struct cm_id_private
*cm_id_priv
;
1826 struct cm_rep_msg
*rep_msg
;
1829 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1830 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1832 cm_dup_rep_handler(work
);
1836 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
1838 spin_lock_irq(&cm_id_priv
->lock
);
1839 switch (cm_id_priv
->id
.state
) {
1840 case IB_CM_REQ_SENT
:
1841 case IB_CM_MRA_REQ_RCVD
:
1844 spin_unlock_irq(&cm_id_priv
->lock
);
1849 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1850 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1851 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1853 spin_lock(&cm
.lock
);
1854 /* Check for duplicate REP. */
1855 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1856 spin_unlock(&cm
.lock
);
1857 spin_unlock_irq(&cm_id_priv
->lock
);
1861 /* Check for a stale connection. */
1862 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1863 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1864 &cm
.remote_id_table
);
1865 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1866 spin_unlock(&cm
.lock
);
1867 spin_unlock_irq(&cm_id_priv
->lock
);
1868 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1869 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1874 spin_unlock(&cm
.lock
);
1876 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1877 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1878 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1879 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1880 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1881 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1882 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1883 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1884 cm_id_priv
->av
.timeout
=
1885 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1886 cm_id_priv
->av
.timeout
- 1);
1887 cm_id_priv
->alt_av
.timeout
=
1888 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1889 cm_id_priv
->alt_av
.timeout
- 1);
1891 /* todo: handle peer_to_peer */
1893 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1894 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1896 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1897 spin_unlock_irq(&cm_id_priv
->lock
);
1900 cm_process_work(cm_id_priv
, work
);
1902 cm_deref_id(cm_id_priv
);
1906 cm_deref_id(cm_id_priv
);
1910 static int cm_establish_handler(struct cm_work
*work
)
1912 struct cm_id_private
*cm_id_priv
;
1915 /* See comment in cm_establish about lookup. */
1916 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1920 spin_lock_irq(&cm_id_priv
->lock
);
1921 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1922 spin_unlock_irq(&cm_id_priv
->lock
);
1926 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1927 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1929 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1930 spin_unlock_irq(&cm_id_priv
->lock
);
1933 cm_process_work(cm_id_priv
, work
);
1935 cm_deref_id(cm_id_priv
);
1938 cm_deref_id(cm_id_priv
);
1942 static int cm_rtu_handler(struct cm_work
*work
)
1944 struct cm_id_private
*cm_id_priv
;
1945 struct cm_rtu_msg
*rtu_msg
;
1948 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1949 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1950 rtu_msg
->local_comm_id
);
1954 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1956 spin_lock_irq(&cm_id_priv
->lock
);
1957 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1958 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1959 spin_unlock_irq(&cm_id_priv
->lock
);
1960 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1961 counter
[CM_RTU_COUNTER
]);
1964 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1966 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1967 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1969 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1970 spin_unlock_irq(&cm_id_priv
->lock
);
1973 cm_process_work(cm_id_priv
, work
);
1975 cm_deref_id(cm_id_priv
);
1978 cm_deref_id(cm_id_priv
);
1982 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1983 struct cm_id_private
*cm_id_priv
,
1984 const void *private_data
,
1985 u8 private_data_len
)
1987 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1988 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1989 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1990 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1991 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1993 if (private_data
&& private_data_len
)
1994 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1997 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1998 const void *private_data
,
1999 u8 private_data_len
)
2001 struct cm_id_private
*cm_id_priv
;
2002 struct ib_mad_send_buf
*msg
;
2003 unsigned long flags
;
2006 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2009 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2010 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2011 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2016 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2017 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2018 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2020 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2022 cm_enter_timewait(cm_id_priv
);
2026 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2027 private_data
, private_data_len
);
2028 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2029 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2031 ret
= ib_post_send_mad(msg
, NULL
);
2033 cm_enter_timewait(cm_id_priv
);
2034 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2039 cm_id
->state
= IB_CM_DREQ_SENT
;
2040 cm_id_priv
->msg
= msg
;
2041 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2044 EXPORT_SYMBOL(ib_send_cm_dreq
);
2046 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2047 struct cm_id_private
*cm_id_priv
,
2048 const void *private_data
,
2049 u8 private_data_len
)
2051 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2052 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2053 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2055 if (private_data
&& private_data_len
)
2056 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2059 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2060 const void *private_data
,
2061 u8 private_data_len
)
2063 struct cm_id_private
*cm_id_priv
;
2064 struct ib_mad_send_buf
*msg
;
2065 unsigned long flags
;
2069 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2072 data
= cm_copy_private_data(private_data
, private_data_len
);
2074 return PTR_ERR(data
);
2076 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2077 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2078 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2079 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2084 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2085 cm_enter_timewait(cm_id_priv
);
2087 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2091 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2092 private_data
, private_data_len
);
2094 ret
= ib_post_send_mad(msg
, NULL
);
2096 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2101 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2104 EXPORT_SYMBOL(ib_send_cm_drep
);
2106 static int cm_issue_drep(struct cm_port
*port
,
2107 struct ib_mad_recv_wc
*mad_recv_wc
)
2109 struct ib_mad_send_buf
*msg
= NULL
;
2110 struct cm_dreq_msg
*dreq_msg
;
2111 struct cm_drep_msg
*drep_msg
;
2114 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2118 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2119 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2121 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2122 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2123 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2125 ret
= ib_post_send_mad(msg
, NULL
);
2132 static int cm_dreq_handler(struct cm_work
*work
)
2134 struct cm_id_private
*cm_id_priv
;
2135 struct cm_dreq_msg
*dreq_msg
;
2136 struct ib_mad_send_buf
*msg
= NULL
;
2139 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2140 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2141 dreq_msg
->local_comm_id
);
2143 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2144 counter
[CM_DREQ_COUNTER
]);
2145 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2149 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2151 spin_lock_irq(&cm_id_priv
->lock
);
2152 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2155 switch (cm_id_priv
->id
.state
) {
2156 case IB_CM_REP_SENT
:
2157 case IB_CM_DREQ_SENT
:
2158 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2160 case IB_CM_ESTABLISHED
:
2161 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2162 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2163 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2165 case IB_CM_MRA_REP_RCVD
:
2167 case IB_CM_TIMEWAIT
:
2168 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2169 counter
[CM_DREQ_COUNTER
]);
2170 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2173 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2174 cm_id_priv
->private_data
,
2175 cm_id_priv
->private_data_len
);
2176 spin_unlock_irq(&cm_id_priv
->lock
);
2178 if (ib_post_send_mad(msg
, NULL
))
2181 case IB_CM_DREQ_RCVD
:
2182 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2183 counter
[CM_DREQ_COUNTER
]);
2188 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2189 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2190 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2192 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2193 spin_unlock_irq(&cm_id_priv
->lock
);
2196 cm_process_work(cm_id_priv
, work
);
2198 cm_deref_id(cm_id_priv
);
2201 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2202 deref
: cm_deref_id(cm_id_priv
);
2206 static int cm_drep_handler(struct cm_work
*work
)
2208 struct cm_id_private
*cm_id_priv
;
2209 struct cm_drep_msg
*drep_msg
;
2212 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2213 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2214 drep_msg
->local_comm_id
);
2218 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2220 spin_lock_irq(&cm_id_priv
->lock
);
2221 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2222 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2223 spin_unlock_irq(&cm_id_priv
->lock
);
2226 cm_enter_timewait(cm_id_priv
);
2228 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2229 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2231 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2232 spin_unlock_irq(&cm_id_priv
->lock
);
2235 cm_process_work(cm_id_priv
, work
);
2237 cm_deref_id(cm_id_priv
);
2240 cm_deref_id(cm_id_priv
);
2244 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2245 enum ib_cm_rej_reason reason
,
2248 const void *private_data
,
2249 u8 private_data_len
)
2251 struct cm_id_private
*cm_id_priv
;
2252 struct ib_mad_send_buf
*msg
;
2253 unsigned long flags
;
2256 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2257 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2260 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2262 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2263 switch (cm_id
->state
) {
2264 case IB_CM_REQ_SENT
:
2265 case IB_CM_MRA_REQ_RCVD
:
2266 case IB_CM_REQ_RCVD
:
2267 case IB_CM_MRA_REQ_SENT
:
2268 case IB_CM_REP_RCVD
:
2269 case IB_CM_MRA_REP_SENT
:
2270 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2272 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2273 cm_id_priv
, reason
, ari
, ari_length
,
2274 private_data
, private_data_len
);
2276 cm_reset_to_idle(cm_id_priv
);
2278 case IB_CM_REP_SENT
:
2279 case IB_CM_MRA_REP_RCVD
:
2280 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2282 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2283 cm_id_priv
, reason
, ari
, ari_length
,
2284 private_data
, private_data_len
);
2286 cm_enter_timewait(cm_id_priv
);
2296 ret
= ib_post_send_mad(msg
, NULL
);
2300 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2303 EXPORT_SYMBOL(ib_send_cm_rej
);
2305 static void cm_format_rej_event(struct cm_work
*work
)
2307 struct cm_rej_msg
*rej_msg
;
2308 struct ib_cm_rej_event_param
*param
;
2310 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2311 param
= &work
->cm_event
.param
.rej_rcvd
;
2312 param
->ari
= rej_msg
->ari
;
2313 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2314 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2315 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2318 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2320 struct cm_timewait_info
*timewait_info
;
2321 struct cm_id_private
*cm_id_priv
;
2324 remote_id
= rej_msg
->local_comm_id
;
2326 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2327 spin_lock_irq(&cm
.lock
);
2328 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2330 if (!timewait_info
) {
2331 spin_unlock_irq(&cm
.lock
);
2334 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2335 (timewait_info
->work
.local_id
^
2336 cm
.random_id_operand
));
2338 if (cm_id_priv
->id
.remote_id
== remote_id
)
2339 atomic_inc(&cm_id_priv
->refcount
);
2343 spin_unlock_irq(&cm
.lock
);
2344 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2345 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2347 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2352 static int cm_rej_handler(struct cm_work
*work
)
2354 struct cm_id_private
*cm_id_priv
;
2355 struct cm_rej_msg
*rej_msg
;
2358 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2359 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2363 cm_format_rej_event(work
);
2365 spin_lock_irq(&cm_id_priv
->lock
);
2366 switch (cm_id_priv
->id
.state
) {
2367 case IB_CM_REQ_SENT
:
2368 case IB_CM_MRA_REQ_RCVD
:
2369 case IB_CM_REP_SENT
:
2370 case IB_CM_MRA_REP_RCVD
:
2371 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2373 case IB_CM_REQ_RCVD
:
2374 case IB_CM_MRA_REQ_SENT
:
2375 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2376 cm_enter_timewait(cm_id_priv
);
2378 cm_reset_to_idle(cm_id_priv
);
2380 case IB_CM_DREQ_SENT
:
2381 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2383 case IB_CM_REP_RCVD
:
2384 case IB_CM_MRA_REP_SENT
:
2385 cm_enter_timewait(cm_id_priv
);
2387 case IB_CM_ESTABLISHED
:
2388 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2389 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2390 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2391 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2393 cm_enter_timewait(cm_id_priv
);
2398 spin_unlock_irq(&cm_id_priv
->lock
);
2403 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2405 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2406 spin_unlock_irq(&cm_id_priv
->lock
);
2409 cm_process_work(cm_id_priv
, work
);
2411 cm_deref_id(cm_id_priv
);
2414 cm_deref_id(cm_id_priv
);
2418 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2420 const void *private_data
,
2421 u8 private_data_len
)
2423 struct cm_id_private
*cm_id_priv
;
2424 struct ib_mad_send_buf
*msg
;
2425 enum ib_cm_state cm_state
;
2426 enum ib_cm_lap_state lap_state
;
2427 enum cm_msg_response msg_response
;
2429 unsigned long flags
;
2432 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2435 data
= cm_copy_private_data(private_data
, private_data_len
);
2437 return PTR_ERR(data
);
2439 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2441 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2442 switch(cm_id_priv
->id
.state
) {
2443 case IB_CM_REQ_RCVD
:
2444 cm_state
= IB_CM_MRA_REQ_SENT
;
2445 lap_state
= cm_id
->lap_state
;
2446 msg_response
= CM_MSG_RESPONSE_REQ
;
2448 case IB_CM_REP_RCVD
:
2449 cm_state
= IB_CM_MRA_REP_SENT
;
2450 lap_state
= cm_id
->lap_state
;
2451 msg_response
= CM_MSG_RESPONSE_REP
;
2453 case IB_CM_ESTABLISHED
:
2454 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2455 cm_state
= cm_id
->state
;
2456 lap_state
= IB_CM_MRA_LAP_SENT
;
2457 msg_response
= CM_MSG_RESPONSE_OTHER
;
2465 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2466 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2470 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2471 msg_response
, service_timeout
,
2472 private_data
, private_data_len
);
2473 ret
= ib_post_send_mad(msg
, NULL
);
2478 cm_id
->state
= cm_state
;
2479 cm_id
->lap_state
= lap_state
;
2480 cm_id_priv
->service_timeout
= service_timeout
;
2481 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2482 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2485 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2489 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2494 EXPORT_SYMBOL(ib_send_cm_mra
);
2496 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2498 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2499 case CM_MSG_RESPONSE_REQ
:
2500 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2501 case CM_MSG_RESPONSE_REP
:
2502 case CM_MSG_RESPONSE_OTHER
:
2503 return cm_acquire_id(mra_msg
->remote_comm_id
,
2504 mra_msg
->local_comm_id
);
2510 static int cm_mra_handler(struct cm_work
*work
)
2512 struct cm_id_private
*cm_id_priv
;
2513 struct cm_mra_msg
*mra_msg
;
2516 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2517 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2521 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2522 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2523 cm_mra_get_service_timeout(mra_msg
);
2524 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2525 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2527 spin_lock_irq(&cm_id_priv
->lock
);
2528 switch (cm_id_priv
->id
.state
) {
2529 case IB_CM_REQ_SENT
:
2530 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2531 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2532 cm_id_priv
->msg
, timeout
))
2534 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2536 case IB_CM_REP_SENT
:
2537 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2538 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2539 cm_id_priv
->msg
, timeout
))
2541 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2543 case IB_CM_ESTABLISHED
:
2544 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2545 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2546 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2547 cm_id_priv
->msg
, timeout
)) {
2548 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2549 atomic_long_inc(&work
->port
->
2550 counter_group
[CM_RECV_DUPLICATES
].
2551 counter
[CM_MRA_COUNTER
]);
2554 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2556 case IB_CM_MRA_REQ_RCVD
:
2557 case IB_CM_MRA_REP_RCVD
:
2558 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2559 counter
[CM_MRA_COUNTER
]);
2565 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2566 cm_id_priv
->id
.state
;
2567 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2569 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2570 spin_unlock_irq(&cm_id_priv
->lock
);
2573 cm_process_work(cm_id_priv
, work
);
2575 cm_deref_id(cm_id_priv
);
2578 spin_unlock_irq(&cm_id_priv
->lock
);
2579 cm_deref_id(cm_id_priv
);
2583 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2584 struct cm_id_private
*cm_id_priv
,
2585 struct ib_sa_path_rec
*alternate_path
,
2586 const void *private_data
,
2587 u8 private_data_len
)
2589 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2590 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2591 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2592 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2593 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2594 /* todo: need remote CM response timeout */
2595 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2596 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2597 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2598 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2599 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2600 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2601 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2602 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2603 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2604 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2605 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2606 cm_lap_set_local_ack_timeout(lap_msg
,
2607 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2608 alternate_path
->packet_life_time
));
2610 if (private_data
&& private_data_len
)
2611 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2614 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2615 struct ib_sa_path_rec
*alternate_path
,
2616 const void *private_data
,
2617 u8 private_data_len
)
2619 struct cm_id_private
*cm_id_priv
;
2620 struct ib_mad_send_buf
*msg
;
2621 unsigned long flags
;
2624 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2627 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2628 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2629 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2630 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2631 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2636 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2639 cm_id_priv
->alt_av
.timeout
=
2640 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2641 cm_id_priv
->alt_av
.timeout
- 1);
2643 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2647 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2648 alternate_path
, private_data
, private_data_len
);
2649 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2650 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2652 ret
= ib_post_send_mad(msg
, NULL
);
2654 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2659 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2660 cm_id_priv
->msg
= msg
;
2662 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2665 EXPORT_SYMBOL(ib_send_cm_lap
);
2667 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2668 struct ib_sa_path_rec
*path
,
2669 struct cm_lap_msg
*lap_msg
)
2671 memset(path
, 0, sizeof *path
);
2672 path
->dgid
= lap_msg
->alt_local_gid
;
2673 path
->sgid
= lap_msg
->alt_remote_gid
;
2674 path
->dlid
= lap_msg
->alt_local_lid
;
2675 path
->slid
= lap_msg
->alt_remote_lid
;
2676 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2677 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2678 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2679 path
->reversible
= 1;
2680 path
->pkey
= cm_id_priv
->pkey
;
2681 path
->sl
= cm_lap_get_sl(lap_msg
);
2682 path
->mtu_selector
= IB_SA_EQ
;
2683 path
->mtu
= cm_id_priv
->path_mtu
;
2684 path
->rate_selector
= IB_SA_EQ
;
2685 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2686 path
->packet_life_time_selector
= IB_SA_EQ
;
2687 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2688 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2691 static int cm_lap_handler(struct cm_work
*work
)
2693 struct cm_id_private
*cm_id_priv
;
2694 struct cm_lap_msg
*lap_msg
;
2695 struct ib_cm_lap_event_param
*param
;
2696 struct ib_mad_send_buf
*msg
= NULL
;
2699 /* todo: verify LAP request and send reject APR if invalid. */
2700 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2701 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2702 lap_msg
->local_comm_id
);
2706 param
= &work
->cm_event
.param
.lap_rcvd
;
2707 param
->alternate_path
= &work
->path
[0];
2708 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2709 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2711 spin_lock_irq(&cm_id_priv
->lock
);
2712 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2715 switch (cm_id_priv
->id
.lap_state
) {
2716 case IB_CM_LAP_UNINIT
:
2717 case IB_CM_LAP_IDLE
:
2719 case IB_CM_MRA_LAP_SENT
:
2720 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2721 counter
[CM_LAP_COUNTER
]);
2722 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2725 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2726 CM_MSG_RESPONSE_OTHER
,
2727 cm_id_priv
->service_timeout
,
2728 cm_id_priv
->private_data
,
2729 cm_id_priv
->private_data_len
);
2730 spin_unlock_irq(&cm_id_priv
->lock
);
2732 if (ib_post_send_mad(msg
, NULL
))
2735 case IB_CM_LAP_RCVD
:
2736 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2737 counter
[CM_LAP_COUNTER
]);
2743 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2744 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2745 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2746 work
->mad_recv_wc
->recv_buf
.grh
,
2748 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2749 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2751 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2752 spin_unlock_irq(&cm_id_priv
->lock
);
2755 cm_process_work(cm_id_priv
, work
);
2757 cm_deref_id(cm_id_priv
);
2760 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2761 deref
: cm_deref_id(cm_id_priv
);
2765 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2766 struct cm_id_private
*cm_id_priv
,
2767 enum ib_cm_apr_status status
,
2770 const void *private_data
,
2771 u8 private_data_len
)
2773 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2774 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2775 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2776 apr_msg
->ap_status
= (u8
) status
;
2778 if (info
&& info_length
) {
2779 apr_msg
->info_length
= info_length
;
2780 memcpy(apr_msg
->info
, info
, info_length
);
2783 if (private_data
&& private_data_len
)
2784 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2787 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2788 enum ib_cm_apr_status status
,
2791 const void *private_data
,
2792 u8 private_data_len
)
2794 struct cm_id_private
*cm_id_priv
;
2795 struct ib_mad_send_buf
*msg
;
2796 unsigned long flags
;
2799 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2800 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2803 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2804 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2805 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2806 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2807 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2812 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2816 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2817 info
, info_length
, private_data
, private_data_len
);
2818 ret
= ib_post_send_mad(msg
, NULL
);
2820 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2825 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2826 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2829 EXPORT_SYMBOL(ib_send_cm_apr
);
2831 static int cm_apr_handler(struct cm_work
*work
)
2833 struct cm_id_private
*cm_id_priv
;
2834 struct cm_apr_msg
*apr_msg
;
2837 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2838 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2839 apr_msg
->local_comm_id
);
2841 return -EINVAL
; /* Unmatched reply. */
2843 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2844 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2845 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2846 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2848 spin_lock_irq(&cm_id_priv
->lock
);
2849 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2850 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2851 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2852 spin_unlock_irq(&cm_id_priv
->lock
);
2855 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2856 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2857 cm_id_priv
->msg
= NULL
;
2859 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2861 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2862 spin_unlock_irq(&cm_id_priv
->lock
);
2865 cm_process_work(cm_id_priv
, work
);
2867 cm_deref_id(cm_id_priv
);
2870 cm_deref_id(cm_id_priv
);
2874 static int cm_timewait_handler(struct cm_work
*work
)
2876 struct cm_timewait_info
*timewait_info
;
2877 struct cm_id_private
*cm_id_priv
;
2880 timewait_info
= (struct cm_timewait_info
*)work
;
2881 spin_lock_irq(&cm
.lock
);
2882 list_del(&timewait_info
->list
);
2883 spin_unlock_irq(&cm
.lock
);
2885 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2886 timewait_info
->work
.remote_id
);
2890 spin_lock_irq(&cm_id_priv
->lock
);
2891 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2892 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2893 spin_unlock_irq(&cm_id_priv
->lock
);
2896 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2897 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2899 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2900 spin_unlock_irq(&cm_id_priv
->lock
);
2903 cm_process_work(cm_id_priv
, work
);
2905 cm_deref_id(cm_id_priv
);
2908 cm_deref_id(cm_id_priv
);
2912 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2913 struct cm_id_private
*cm_id_priv
,
2914 struct ib_cm_sidr_req_param
*param
)
2916 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2917 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2918 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2919 sidr_req_msg
->pkey
= param
->path
->pkey
;
2920 sidr_req_msg
->service_id
= param
->service_id
;
2922 if (param
->private_data
&& param
->private_data_len
)
2923 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2924 param
->private_data_len
);
2927 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2928 struct ib_cm_sidr_req_param
*param
)
2930 struct cm_id_private
*cm_id_priv
;
2931 struct ib_mad_send_buf
*msg
;
2932 unsigned long flags
;
2935 if (!param
->path
|| (param
->private_data
&&
2936 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2939 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2940 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2944 cm_id
->service_id
= param
->service_id
;
2945 cm_id
->service_mask
= ~cpu_to_be64(0);
2946 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2947 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2948 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2952 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2954 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2955 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2957 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2958 if (cm_id
->state
== IB_CM_IDLE
)
2959 ret
= ib_post_send_mad(msg
, NULL
);
2964 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2968 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2969 cm_id_priv
->msg
= msg
;
2970 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2974 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2976 static void cm_format_sidr_req_event(struct cm_work
*work
,
2977 struct ib_cm_id
*listen_id
)
2979 struct cm_sidr_req_msg
*sidr_req_msg
;
2980 struct ib_cm_sidr_req_event_param
*param
;
2982 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2983 work
->mad_recv_wc
->recv_buf
.mad
;
2984 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2985 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2986 param
->listen_id
= listen_id
;
2987 param
->port
= work
->port
->port_num
;
2988 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2991 static int cm_sidr_req_handler(struct cm_work
*work
)
2993 struct ib_cm_id
*cm_id
;
2994 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2995 struct cm_sidr_req_msg
*sidr_req_msg
;
2998 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3000 return PTR_ERR(cm_id
);
3001 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3003 /* Record SGID/SLID and request ID for lookup. */
3004 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3005 work
->mad_recv_wc
->recv_buf
.mad
;
3006 wc
= work
->mad_recv_wc
->wc
;
3007 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3008 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3009 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3010 work
->mad_recv_wc
->recv_buf
.grh
,
3012 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
3013 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3014 atomic_inc(&cm_id_priv
->work_count
);
3016 spin_lock_irq(&cm
.lock
);
3017 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3018 if (cur_cm_id_priv
) {
3019 spin_unlock_irq(&cm
.lock
);
3020 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3021 counter
[CM_SIDR_REQ_COUNTER
]);
3022 goto out
; /* Duplicate message. */
3024 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3025 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3026 sidr_req_msg
->service_id
,
3027 sidr_req_msg
->private_data
);
3028 if (!cur_cm_id_priv
) {
3029 spin_unlock_irq(&cm
.lock
);
3030 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3031 goto out
; /* No match. */
3033 atomic_inc(&cur_cm_id_priv
->refcount
);
3034 atomic_inc(&cm_id_priv
->refcount
);
3035 spin_unlock_irq(&cm
.lock
);
3037 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3038 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3039 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3040 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3042 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3043 cm_process_work(cm_id_priv
, work
);
3044 cm_deref_id(cur_cm_id_priv
);
3047 ib_destroy_cm_id(&cm_id_priv
->id
);
3051 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3052 struct cm_id_private
*cm_id_priv
,
3053 struct ib_cm_sidr_rep_param
*param
)
3055 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3057 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3058 sidr_rep_msg
->status
= param
->status
;
3059 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3060 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3061 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3063 if (param
->info
&& param
->info_length
)
3064 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3066 if (param
->private_data
&& param
->private_data_len
)
3067 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3068 param
->private_data_len
);
3071 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3072 struct ib_cm_sidr_rep_param
*param
)
3074 struct cm_id_private
*cm_id_priv
;
3075 struct ib_mad_send_buf
*msg
;
3076 unsigned long flags
;
3079 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3080 (param
->private_data
&&
3081 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3084 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3085 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3086 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3091 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3095 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3097 ret
= ib_post_send_mad(msg
, NULL
);
3099 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3103 cm_id
->state
= IB_CM_IDLE
;
3104 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3106 spin_lock_irqsave(&cm
.lock
, flags
);
3107 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
)) {
3108 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3109 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
3111 spin_unlock_irqrestore(&cm
.lock
, flags
);
3114 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3117 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3119 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3121 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3122 struct ib_cm_sidr_rep_event_param
*param
;
3124 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3125 work
->mad_recv_wc
->recv_buf
.mad
;
3126 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3127 param
->status
= sidr_rep_msg
->status
;
3128 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3129 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3130 param
->info
= &sidr_rep_msg
->info
;
3131 param
->info_len
= sidr_rep_msg
->info_length
;
3132 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3135 static int cm_sidr_rep_handler(struct cm_work
*work
)
3137 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3138 struct cm_id_private
*cm_id_priv
;
3140 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3141 work
->mad_recv_wc
->recv_buf
.mad
;
3142 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3144 return -EINVAL
; /* Unmatched reply. */
3146 spin_lock_irq(&cm_id_priv
->lock
);
3147 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3148 spin_unlock_irq(&cm_id_priv
->lock
);
3151 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3152 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3153 spin_unlock_irq(&cm_id_priv
->lock
);
3155 cm_format_sidr_rep_event(work
);
3156 cm_process_work(cm_id_priv
, work
);
3159 cm_deref_id(cm_id_priv
);
3163 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3164 enum ib_wc_status wc_status
)
3166 struct cm_id_private
*cm_id_priv
;
3167 struct ib_cm_event cm_event
;
3168 enum ib_cm_state state
;
3171 memset(&cm_event
, 0, sizeof cm_event
);
3172 cm_id_priv
= msg
->context
[0];
3174 /* Discard old sends or ones without a response. */
3175 spin_lock_irq(&cm_id_priv
->lock
);
3176 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3177 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3181 case IB_CM_REQ_SENT
:
3182 case IB_CM_MRA_REQ_RCVD
:
3183 cm_reset_to_idle(cm_id_priv
);
3184 cm_event
.event
= IB_CM_REQ_ERROR
;
3186 case IB_CM_REP_SENT
:
3187 case IB_CM_MRA_REP_RCVD
:
3188 cm_reset_to_idle(cm_id_priv
);
3189 cm_event
.event
= IB_CM_REP_ERROR
;
3191 case IB_CM_DREQ_SENT
:
3192 cm_enter_timewait(cm_id_priv
);
3193 cm_event
.event
= IB_CM_DREQ_ERROR
;
3195 case IB_CM_SIDR_REQ_SENT
:
3196 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3197 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3202 spin_unlock_irq(&cm_id_priv
->lock
);
3203 cm_event
.param
.send_status
= wc_status
;
3205 /* No other events can occur on the cm_id at this point. */
3206 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3209 ib_destroy_cm_id(&cm_id_priv
->id
);
3212 spin_unlock_irq(&cm_id_priv
->lock
);
3216 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3217 struct ib_mad_send_wc
*mad_send_wc
)
3219 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3220 struct cm_port
*port
;
3223 port
= mad_agent
->context
;
3224 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3225 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3228 * If the send was in response to a received message (context[0] is not
3229 * set to a cm_id), and is not a REJ, then it is a send that was
3232 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3235 atomic_long_add(1 + msg
->retries
,
3236 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3238 atomic_long_add(msg
->retries
,
3239 &port
->counter_group
[CM_XMIT_RETRIES
].
3240 counter
[attr_index
]);
3242 switch (mad_send_wc
->status
) {
3244 case IB_WC_WR_FLUSH_ERR
:
3248 if (msg
->context
[0] && msg
->context
[1])
3249 cm_process_send_error(msg
, mad_send_wc
->status
);
3256 static void cm_work_handler(struct work_struct
*_work
)
3258 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3261 switch (work
->cm_event
.event
) {
3262 case IB_CM_REQ_RECEIVED
:
3263 ret
= cm_req_handler(work
);
3265 case IB_CM_MRA_RECEIVED
:
3266 ret
= cm_mra_handler(work
);
3268 case IB_CM_REJ_RECEIVED
:
3269 ret
= cm_rej_handler(work
);
3271 case IB_CM_REP_RECEIVED
:
3272 ret
= cm_rep_handler(work
);
3274 case IB_CM_RTU_RECEIVED
:
3275 ret
= cm_rtu_handler(work
);
3277 case IB_CM_USER_ESTABLISHED
:
3278 ret
= cm_establish_handler(work
);
3280 case IB_CM_DREQ_RECEIVED
:
3281 ret
= cm_dreq_handler(work
);
3283 case IB_CM_DREP_RECEIVED
:
3284 ret
= cm_drep_handler(work
);
3286 case IB_CM_SIDR_REQ_RECEIVED
:
3287 ret
= cm_sidr_req_handler(work
);
3289 case IB_CM_SIDR_REP_RECEIVED
:
3290 ret
= cm_sidr_rep_handler(work
);
3292 case IB_CM_LAP_RECEIVED
:
3293 ret
= cm_lap_handler(work
);
3295 case IB_CM_APR_RECEIVED
:
3296 ret
= cm_apr_handler(work
);
3298 case IB_CM_TIMEWAIT_EXIT
:
3299 ret
= cm_timewait_handler(work
);
3309 static int cm_establish(struct ib_cm_id
*cm_id
)
3311 struct cm_id_private
*cm_id_priv
;
3312 struct cm_work
*work
;
3313 unsigned long flags
;
3316 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3320 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3321 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3322 switch (cm_id
->state
)
3324 case IB_CM_REP_SENT
:
3325 case IB_CM_MRA_REP_RCVD
:
3326 cm_id
->state
= IB_CM_ESTABLISHED
;
3328 case IB_CM_ESTABLISHED
:
3335 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3343 * The CM worker thread may try to destroy the cm_id before it
3344 * can execute this work item. To prevent potential deadlock,
3345 * we need to find the cm_id once we're in the context of the
3346 * worker thread, rather than holding a reference on it.
3348 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3349 work
->local_id
= cm_id
->local_id
;
3350 work
->remote_id
= cm_id
->remote_id
;
3351 work
->mad_recv_wc
= NULL
;
3352 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3353 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3358 static int cm_migrate(struct ib_cm_id
*cm_id
)
3360 struct cm_id_private
*cm_id_priv
;
3361 unsigned long flags
;
3364 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3365 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3366 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3367 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3368 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3369 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3370 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3373 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3378 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3383 case IB_EVENT_COMM_EST
:
3384 ret
= cm_establish(cm_id
);
3386 case IB_EVENT_PATH_MIG
:
3387 ret
= cm_migrate(cm_id
);
3394 EXPORT_SYMBOL(ib_cm_notify
);
3396 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3397 struct ib_mad_recv_wc
*mad_recv_wc
)
3399 struct cm_port
*port
= mad_agent
->context
;
3400 struct cm_work
*work
;
3401 enum ib_cm_event_type event
;
3405 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3406 case CM_REQ_ATTR_ID
:
3407 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3408 alt_local_lid
!= 0);
3409 event
= IB_CM_REQ_RECEIVED
;
3411 case CM_MRA_ATTR_ID
:
3412 event
= IB_CM_MRA_RECEIVED
;
3414 case CM_REJ_ATTR_ID
:
3415 event
= IB_CM_REJ_RECEIVED
;
3417 case CM_REP_ATTR_ID
:
3418 event
= IB_CM_REP_RECEIVED
;
3420 case CM_RTU_ATTR_ID
:
3421 event
= IB_CM_RTU_RECEIVED
;
3423 case CM_DREQ_ATTR_ID
:
3424 event
= IB_CM_DREQ_RECEIVED
;
3426 case CM_DREP_ATTR_ID
:
3427 event
= IB_CM_DREP_RECEIVED
;
3429 case CM_SIDR_REQ_ATTR_ID
:
3430 event
= IB_CM_SIDR_REQ_RECEIVED
;
3432 case CM_SIDR_REP_ATTR_ID
:
3433 event
= IB_CM_SIDR_REP_RECEIVED
;
3435 case CM_LAP_ATTR_ID
:
3437 event
= IB_CM_LAP_RECEIVED
;
3439 case CM_APR_ATTR_ID
:
3440 event
= IB_CM_APR_RECEIVED
;
3443 ib_free_recv_mad(mad_recv_wc
);
3447 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3448 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3449 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3451 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3454 ib_free_recv_mad(mad_recv_wc
);
3458 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3459 work
->cm_event
.event
= event
;
3460 work
->mad_recv_wc
= mad_recv_wc
;
3462 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3465 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3466 struct ib_qp_attr
*qp_attr
,
3469 unsigned long flags
;
3472 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3473 switch (cm_id_priv
->id
.state
) {
3474 case IB_CM_REQ_SENT
:
3475 case IB_CM_MRA_REQ_RCVD
:
3476 case IB_CM_REQ_RCVD
:
3477 case IB_CM_MRA_REQ_SENT
:
3478 case IB_CM_REP_RCVD
:
3479 case IB_CM_MRA_REP_SENT
:
3480 case IB_CM_REP_SENT
:
3481 case IB_CM_MRA_REP_RCVD
:
3482 case IB_CM_ESTABLISHED
:
3483 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3484 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3485 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3486 if (cm_id_priv
->responder_resources
)
3487 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3488 IB_ACCESS_REMOTE_ATOMIC
;
3489 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3490 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3497 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3501 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3502 struct ib_qp_attr
*qp_attr
,
3505 unsigned long flags
;
3508 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3509 switch (cm_id_priv
->id
.state
) {
3510 case IB_CM_REQ_RCVD
:
3511 case IB_CM_MRA_REQ_SENT
:
3512 case IB_CM_REP_RCVD
:
3513 case IB_CM_MRA_REP_SENT
:
3514 case IB_CM_REP_SENT
:
3515 case IB_CM_MRA_REP_RCVD
:
3516 case IB_CM_ESTABLISHED
:
3517 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3518 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3519 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3520 if (!cm_id_priv
->av
.valid
) {
3521 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3524 if (cm_id_priv
->av
.ah_attr
.vlan_id
!= 0xffff) {
3525 qp_attr
->vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
3526 *qp_attr_mask
|= IB_QP_VID
;
3528 if (!is_zero_ether_addr(cm_id_priv
->av
.smac
)) {
3529 memcpy(qp_attr
->smac
, cm_id_priv
->av
.smac
,
3530 sizeof(qp_attr
->smac
));
3531 *qp_attr_mask
|= IB_QP_SMAC
;
3533 if (cm_id_priv
->alt_av
.valid
) {
3534 if (cm_id_priv
->alt_av
.ah_attr
.vlan_id
!= 0xffff) {
3535 qp_attr
->alt_vlan_id
=
3536 cm_id_priv
->alt_av
.ah_attr
.vlan_id
;
3537 *qp_attr_mask
|= IB_QP_ALT_VID
;
3539 if (!is_zero_ether_addr(cm_id_priv
->alt_av
.smac
)) {
3540 memcpy(qp_attr
->alt_smac
,
3541 cm_id_priv
->alt_av
.smac
,
3542 sizeof(qp_attr
->alt_smac
));
3543 *qp_attr_mask
|= IB_QP_ALT_SMAC
;
3546 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3547 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3548 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3549 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3550 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3551 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3552 IB_QP_MIN_RNR_TIMER
;
3553 qp_attr
->max_dest_rd_atomic
=
3554 cm_id_priv
->responder_resources
;
3555 qp_attr
->min_rnr_timer
= 0;
3557 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3558 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3559 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3560 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3561 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3562 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3570 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3574 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3575 struct ib_qp_attr
*qp_attr
,
3578 unsigned long flags
;
3581 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3582 switch (cm_id_priv
->id
.state
) {
3583 /* Allow transition to RTS before sending REP */
3584 case IB_CM_REQ_RCVD
:
3585 case IB_CM_MRA_REQ_SENT
:
3587 case IB_CM_REP_RCVD
:
3588 case IB_CM_MRA_REP_SENT
:
3589 case IB_CM_REP_SENT
:
3590 case IB_CM_MRA_REP_RCVD
:
3591 case IB_CM_ESTABLISHED
:
3592 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3593 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3594 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3595 switch (cm_id_priv
->qp_type
) {
3597 case IB_QPT_XRC_INI
:
3598 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3599 IB_QP_MAX_QP_RD_ATOMIC
;
3600 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3601 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3602 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3604 case IB_QPT_XRC_TGT
:
3605 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3606 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3611 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3612 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3613 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3616 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3617 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3618 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3619 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3620 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3621 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3629 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3633 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3634 struct ib_qp_attr
*qp_attr
,
3637 struct cm_id_private
*cm_id_priv
;
3640 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3641 switch (qp_attr
->qp_state
) {
3643 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3646 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3649 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3657 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3659 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3661 struct ib_device_attr attr
;
3663 if (ib_query_device(cm_dev
->ib_device
, &attr
))
3664 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3666 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3669 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3672 struct cm_counter_group
*group
;
3673 struct cm_counter_attribute
*cm_attr
;
3675 group
= container_of(obj
, struct cm_counter_group
, obj
);
3676 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3678 return sprintf(buf
, "%ld\n",
3679 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3682 static const struct sysfs_ops cm_counter_ops
= {
3683 .show
= cm_show_counter
3686 static struct kobj_type cm_counter_obj_type
= {
3687 .sysfs_ops
= &cm_counter_ops
,
3688 .default_attrs
= cm_counter_default_attrs
3691 static void cm_release_port_obj(struct kobject
*obj
)
3693 struct cm_port
*cm_port
;
3695 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3699 static struct kobj_type cm_port_obj_type
= {
3700 .release
= cm_release_port_obj
3703 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3707 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3710 struct class cm_class
= {
3711 .owner
= THIS_MODULE
,
3712 .name
= "infiniband_cm",
3713 .devnode
= cm_devnode
,
3715 EXPORT_SYMBOL(cm_class
);
3717 static int cm_create_port_fs(struct cm_port
*port
)
3721 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3722 &port
->cm_dev
->device
->kobj
,
3723 "%d", port
->port_num
);
3729 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3730 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3731 &cm_counter_obj_type
,
3733 "%s", counter_group_names
[i
]);
3742 kobject_put(&port
->counter_group
[i
].obj
);
3743 kobject_put(&port
->port_obj
);
3748 static void cm_remove_port_fs(struct cm_port
*port
)
3752 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3753 kobject_put(&port
->counter_group
[i
].obj
);
3755 kobject_put(&port
->port_obj
);
3758 static void cm_add_one(struct ib_device
*ib_device
)
3760 struct cm_device
*cm_dev
;
3761 struct cm_port
*port
;
3762 struct ib_mad_reg_req reg_req
= {
3763 .mgmt_class
= IB_MGMT_CLASS_CM
,
3764 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
3766 struct ib_port_modify port_modify
= {
3767 .set_port_cap_mask
= IB_PORT_CM_SUP
3769 unsigned long flags
;
3773 if (rdma_node_get_transport(ib_device
->node_type
) != RDMA_TRANSPORT_IB
)
3776 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3777 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3781 cm_dev
->ib_device
= ib_device
;
3782 cm_get_ack_delay(cm_dev
);
3784 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3786 "%s", ib_device
->name
);
3787 if (IS_ERR(cm_dev
->device
)) {
3792 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3793 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3794 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3798 cm_dev
->port
[i
-1] = port
;
3799 port
->cm_dev
= cm_dev
;
3802 ret
= cm_create_port_fs(port
);
3806 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3814 if (IS_ERR(port
->mad_agent
))
3817 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3821 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3823 write_lock_irqsave(&cm
.device_lock
, flags
);
3824 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3825 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3829 ib_unregister_mad_agent(port
->mad_agent
);
3831 cm_remove_port_fs(port
);
3833 port_modify
.set_port_cap_mask
= 0;
3834 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3836 port
= cm_dev
->port
[i
-1];
3837 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3838 ib_unregister_mad_agent(port
->mad_agent
);
3839 cm_remove_port_fs(port
);
3841 device_unregister(cm_dev
->device
);
3845 static void cm_remove_one(struct ib_device
*ib_device
)
3847 struct cm_device
*cm_dev
;
3848 struct cm_port
*port
;
3849 struct ib_port_modify port_modify
= {
3850 .clr_port_cap_mask
= IB_PORT_CM_SUP
3852 unsigned long flags
;
3855 cm_dev
= ib_get_client_data(ib_device
, &cm_client
);
3859 write_lock_irqsave(&cm
.device_lock
, flags
);
3860 list_del(&cm_dev
->list
);
3861 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3863 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3864 port
= cm_dev
->port
[i
-1];
3865 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3866 ib_unregister_mad_agent(port
->mad_agent
);
3867 flush_workqueue(cm
.wq
);
3868 cm_remove_port_fs(port
);
3870 device_unregister(cm_dev
->device
);
3874 static int __init
ib_cm_init(void)
3878 memset(&cm
, 0, sizeof cm
);
3879 INIT_LIST_HEAD(&cm
.device_list
);
3880 rwlock_init(&cm
.device_lock
);
3881 spin_lock_init(&cm
.lock
);
3882 cm
.listen_service_table
= RB_ROOT
;
3883 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3884 cm
.remote_id_table
= RB_ROOT
;
3885 cm
.remote_qp_table
= RB_ROOT
;
3886 cm
.remote_sidr_table
= RB_ROOT
;
3887 idr_init(&cm
.local_id_table
);
3888 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3889 INIT_LIST_HEAD(&cm
.timewait_list
);
3891 ret
= class_register(&cm_class
);
3897 cm
.wq
= create_workqueue("ib_cm");
3903 ret
= ib_register_client(&cm_client
);
3909 destroy_workqueue(cm
.wq
);
3911 class_unregister(&cm_class
);
3913 idr_destroy(&cm
.local_id_table
);
3917 static void __exit
ib_cm_cleanup(void)
3919 struct cm_timewait_info
*timewait_info
, *tmp
;
3921 spin_lock_irq(&cm
.lock
);
3922 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3923 cancel_delayed_work(&timewait_info
->work
.work
);
3924 spin_unlock_irq(&cm
.lock
);
3926 ib_unregister_client(&cm_client
);
3927 destroy_workqueue(cm
.wq
);
3929 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3930 list_del(&timewait_info
->list
);
3931 kfree(timewait_info
);
3934 class_unregister(&cm_class
);
3935 idr_destroy(&cm
.local_id_table
);
3938 module_init(ib_cm_init
);
3939 module_exit(ib_cm_cleanup
);