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 int ib_update_cm_av(struct ib_cm_id
*id
, const u8
*smac
, const u8
*alt_smac
)
354 struct cm_id_private
*cm_id_priv
;
356 cm_id_priv
= container_of(id
, struct cm_id_private
, id
);
359 memcpy(cm_id_priv
->av
.smac
, smac
, sizeof(cm_id_priv
->av
.smac
));
361 if (alt_smac
!= NULL
)
362 memcpy(cm_id_priv
->alt_av
.smac
, alt_smac
,
363 sizeof(cm_id_priv
->alt_av
.smac
));
367 EXPORT_SYMBOL(ib_update_cm_av
);
369 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
371 struct cm_device
*cm_dev
;
372 struct cm_port
*port
= NULL
;
377 read_lock_irqsave(&cm
.device_lock
, flags
);
378 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
379 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
381 port
= cm_dev
->port
[p
-1];
385 read_unlock_irqrestore(&cm
.device_lock
, flags
);
390 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
391 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
396 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
398 av
->timeout
= path
->packet_life_time
+ 1;
399 memcpy(av
->smac
, path
->smac
, sizeof(av
->smac
));
405 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
410 idr_preload(GFP_KERNEL
);
411 spin_lock_irqsave(&cm
.lock
, flags
);
413 id
= idr_alloc_cyclic(&cm
.local_id_table
, cm_id_priv
, 0, 0, GFP_NOWAIT
);
415 spin_unlock_irqrestore(&cm
.lock
, flags
);
418 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
419 return id
< 0 ? id
: 0;
422 static void cm_free_id(__be32 local_id
)
424 spin_lock_irq(&cm
.lock
);
425 idr_remove(&cm
.local_id_table
,
426 (__force
int) (local_id
^ cm
.random_id_operand
));
427 spin_unlock_irq(&cm
.lock
);
430 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
432 struct cm_id_private
*cm_id_priv
;
434 cm_id_priv
= idr_find(&cm
.local_id_table
,
435 (__force
int) (local_id
^ cm
.random_id_operand
));
437 if (cm_id_priv
->id
.remote_id
== remote_id
)
438 atomic_inc(&cm_id_priv
->refcount
);
446 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
448 struct cm_id_private
*cm_id_priv
;
450 spin_lock_irq(&cm
.lock
);
451 cm_id_priv
= cm_get_id(local_id
, remote_id
);
452 spin_unlock_irq(&cm
.lock
);
457 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
461 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
462 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
463 ((unsigned long *) mask
)[i
];
466 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
467 struct ib_cm_compare_data
*dst_data
)
469 u8 src
[IB_CM_COMPARE_SIZE
];
470 u8 dst
[IB_CM_COMPARE_SIZE
];
472 if (!src_data
|| !dst_data
)
475 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
476 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
477 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
480 static int cm_compare_private_data(u8
*private_data
,
481 struct ib_cm_compare_data
*dst_data
)
483 u8 src
[IB_CM_COMPARE_SIZE
];
488 cm_mask_copy(src
, private_data
, dst_data
->mask
);
489 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
493 * Trivial helpers to strip endian annotation and compare; the
494 * endianness doesn't actually matter since we just need a stable
495 * order for the RB tree.
497 static int be32_lt(__be32 a
, __be32 b
)
499 return (__force u32
) a
< (__force u32
) b
;
502 static int be32_gt(__be32 a
, __be32 b
)
504 return (__force u32
) a
> (__force u32
) b
;
507 static int be64_lt(__be64 a
, __be64 b
)
509 return (__force u64
) a
< (__force u64
) b
;
512 static int be64_gt(__be64 a
, __be64 b
)
514 return (__force u64
) a
> (__force u64
) b
;
517 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
519 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
520 struct rb_node
*parent
= NULL
;
521 struct cm_id_private
*cur_cm_id_priv
;
522 __be64 service_id
= cm_id_priv
->id
.service_id
;
523 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
528 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
530 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
531 cur_cm_id_priv
->compare_data
);
532 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
533 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
534 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
536 return cur_cm_id_priv
;
538 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
539 link
= &(*link
)->rb_left
;
540 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
541 link
= &(*link
)->rb_right
;
542 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
543 link
= &(*link
)->rb_left
;
544 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
545 link
= &(*link
)->rb_right
;
546 else if (data_cmp
< 0)
547 link
= &(*link
)->rb_left
;
549 link
= &(*link
)->rb_right
;
551 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
552 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
556 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
560 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
561 struct cm_id_private
*cm_id_priv
;
565 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
566 data_cmp
= cm_compare_private_data(private_data
,
567 cm_id_priv
->compare_data
);
568 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
569 cm_id_priv
->id
.service_id
&&
570 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
573 if (device
< cm_id_priv
->id
.device
)
574 node
= node
->rb_left
;
575 else if (device
> cm_id_priv
->id
.device
)
576 node
= node
->rb_right
;
577 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
578 node
= node
->rb_left
;
579 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
580 node
= node
->rb_right
;
581 else if (data_cmp
< 0)
582 node
= node
->rb_left
;
584 node
= node
->rb_right
;
589 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
592 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
593 struct rb_node
*parent
= NULL
;
594 struct cm_timewait_info
*cur_timewait_info
;
595 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
596 __be32 remote_id
= timewait_info
->work
.remote_id
;
600 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
602 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
603 link
= &(*link
)->rb_left
;
604 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
605 link
= &(*link
)->rb_right
;
606 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
607 link
= &(*link
)->rb_left
;
608 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
609 link
= &(*link
)->rb_right
;
611 return cur_timewait_info
;
613 timewait_info
->inserted_remote_id
= 1;
614 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
615 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
619 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
622 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
623 struct cm_timewait_info
*timewait_info
;
626 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
628 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
629 node
= node
->rb_left
;
630 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
631 node
= node
->rb_right
;
632 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
633 node
= node
->rb_left
;
634 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
635 node
= node
->rb_right
;
637 return timewait_info
;
642 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
645 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
646 struct rb_node
*parent
= NULL
;
647 struct cm_timewait_info
*cur_timewait_info
;
648 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
649 __be32 remote_qpn
= timewait_info
->remote_qpn
;
653 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
655 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
656 link
= &(*link
)->rb_left
;
657 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
658 link
= &(*link
)->rb_right
;
659 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
660 link
= &(*link
)->rb_left
;
661 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
662 link
= &(*link
)->rb_right
;
664 return cur_timewait_info
;
666 timewait_info
->inserted_remote_qp
= 1;
667 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
668 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
672 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
675 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
676 struct rb_node
*parent
= NULL
;
677 struct cm_id_private
*cur_cm_id_priv
;
678 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
679 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
683 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
685 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
686 link
= &(*link
)->rb_left
;
687 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
688 link
= &(*link
)->rb_right
;
691 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
694 link
= &(*link
)->rb_left
;
696 link
= &(*link
)->rb_right
;
698 return cur_cm_id_priv
;
701 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
702 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
706 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
707 enum ib_cm_sidr_status status
)
709 struct ib_cm_sidr_rep_param param
;
711 memset(¶m
, 0, sizeof param
);
712 param
.status
= status
;
713 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
716 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
717 ib_cm_handler cm_handler
,
720 struct cm_id_private
*cm_id_priv
;
723 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
725 return ERR_PTR(-ENOMEM
);
727 cm_id_priv
->id
.state
= IB_CM_IDLE
;
728 cm_id_priv
->id
.device
= device
;
729 cm_id_priv
->id
.cm_handler
= cm_handler
;
730 cm_id_priv
->id
.context
= context
;
731 cm_id_priv
->id
.remote_cm_qpn
= 1;
732 ret
= cm_alloc_id(cm_id_priv
);
736 spin_lock_init(&cm_id_priv
->lock
);
737 init_completion(&cm_id_priv
->comp
);
738 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
739 atomic_set(&cm_id_priv
->work_count
, -1);
740 atomic_set(&cm_id_priv
->refcount
, 1);
741 return &cm_id_priv
->id
;
745 return ERR_PTR(-ENOMEM
);
747 EXPORT_SYMBOL(ib_create_cm_id
);
749 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
751 struct cm_work
*work
;
753 if (list_empty(&cm_id_priv
->work_list
))
756 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
757 list_del(&work
->list
);
761 static void cm_free_work(struct cm_work
*work
)
763 if (work
->mad_recv_wc
)
764 ib_free_recv_mad(work
->mad_recv_wc
);
768 static inline int cm_convert_to_ms(int iba_time
)
770 /* approximate conversion to ms from 4.096us x 2^iba_time */
771 return 1 << max(iba_time
- 8, 0);
775 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
776 * Because of how ack_timeout is stored, adding one doubles the timeout.
777 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
778 * increment it (round up) only if the other is within 50%.
780 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
782 int ack_timeout
= packet_life_time
+ 1;
784 if (ack_timeout
>= ca_ack_delay
)
785 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
787 ack_timeout
= ca_ack_delay
+
788 (ack_timeout
>= (ca_ack_delay
- 1));
790 return min(31, ack_timeout
);
793 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
795 if (timewait_info
->inserted_remote_id
) {
796 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
797 timewait_info
->inserted_remote_id
= 0;
800 if (timewait_info
->inserted_remote_qp
) {
801 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
802 timewait_info
->inserted_remote_qp
= 0;
806 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
808 struct cm_timewait_info
*timewait_info
;
810 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
812 return ERR_PTR(-ENOMEM
);
814 timewait_info
->work
.local_id
= local_id
;
815 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
816 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
817 return timewait_info
;
820 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
825 spin_lock_irqsave(&cm
.lock
, flags
);
826 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
827 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
828 spin_unlock_irqrestore(&cm
.lock
, flags
);
831 * The cm_id could be destroyed by the user before we exit timewait.
832 * To protect against this, we search for the cm_id after exiting
833 * timewait before notifying the user that we've exited timewait.
835 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
836 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
837 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
838 msecs_to_jiffies(wait_time
));
839 cm_id_priv
->timewait_info
= NULL
;
842 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
846 cm_id_priv
->id
.state
= IB_CM_IDLE
;
847 if (cm_id_priv
->timewait_info
) {
848 spin_lock_irqsave(&cm
.lock
, flags
);
849 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
850 spin_unlock_irqrestore(&cm
.lock
, flags
);
851 kfree(cm_id_priv
->timewait_info
);
852 cm_id_priv
->timewait_info
= NULL
;
856 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
858 struct cm_id_private
*cm_id_priv
;
859 struct cm_work
*work
;
861 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
863 spin_lock_irq(&cm_id_priv
->lock
);
864 switch (cm_id
->state
) {
866 cm_id
->state
= IB_CM_IDLE
;
867 spin_unlock_irq(&cm_id_priv
->lock
);
868 spin_lock_irq(&cm
.lock
);
869 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
870 spin_unlock_irq(&cm
.lock
);
872 case IB_CM_SIDR_REQ_SENT
:
873 cm_id
->state
= IB_CM_IDLE
;
874 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
875 spin_unlock_irq(&cm_id_priv
->lock
);
877 case IB_CM_SIDR_REQ_RCVD
:
878 spin_unlock_irq(&cm_id_priv
->lock
);
879 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
882 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
883 spin_unlock_irq(&cm_id_priv
->lock
);
884 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
885 &cm_id_priv
->id
.device
->node_guid
,
886 sizeof cm_id_priv
->id
.device
->node_guid
,
890 if (err
== -ENOMEM
) {
891 /* Do not reject to allow future retries. */
892 cm_reset_to_idle(cm_id_priv
);
893 spin_unlock_irq(&cm_id_priv
->lock
);
895 spin_unlock_irq(&cm_id_priv
->lock
);
896 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
900 case IB_CM_MRA_REQ_RCVD
:
902 case IB_CM_MRA_REP_RCVD
:
903 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
905 case IB_CM_MRA_REQ_SENT
:
907 case IB_CM_MRA_REP_SENT
:
908 spin_unlock_irq(&cm_id_priv
->lock
);
909 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
912 case IB_CM_ESTABLISHED
:
913 spin_unlock_irq(&cm_id_priv
->lock
);
914 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
916 ib_send_cm_dreq(cm_id
, NULL
, 0);
918 case IB_CM_DREQ_SENT
:
919 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
920 cm_enter_timewait(cm_id_priv
);
921 spin_unlock_irq(&cm_id_priv
->lock
);
923 case IB_CM_DREQ_RCVD
:
924 spin_unlock_irq(&cm_id_priv
->lock
);
925 ib_send_cm_drep(cm_id
, NULL
, 0);
928 spin_unlock_irq(&cm_id_priv
->lock
);
932 cm_free_id(cm_id
->local_id
);
933 cm_deref_id(cm_id_priv
);
934 wait_for_completion(&cm_id_priv
->comp
);
935 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
937 kfree(cm_id_priv
->compare_data
);
938 kfree(cm_id_priv
->private_data
);
942 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
944 cm_destroy_id(cm_id
, 0);
946 EXPORT_SYMBOL(ib_destroy_cm_id
);
948 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
949 struct ib_cm_compare_data
*compare_data
)
951 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
955 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
956 service_id
&= service_mask
;
957 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
958 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
961 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
962 if (cm_id
->state
!= IB_CM_IDLE
)
966 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
968 if (!cm_id_priv
->compare_data
)
970 cm_mask_copy(cm_id_priv
->compare_data
->data
,
971 compare_data
->data
, compare_data
->mask
);
972 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
976 cm_id
->state
= IB_CM_LISTEN
;
978 spin_lock_irqsave(&cm
.lock
, flags
);
979 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
980 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
981 cm_id
->service_mask
= ~cpu_to_be64(0);
983 cm_id
->service_id
= service_id
;
984 cm_id
->service_mask
= service_mask
;
986 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
987 spin_unlock_irqrestore(&cm
.lock
, flags
);
989 if (cur_cm_id_priv
) {
990 cm_id
->state
= IB_CM_IDLE
;
991 kfree(cm_id_priv
->compare_data
);
992 cm_id_priv
->compare_data
= NULL
;
997 EXPORT_SYMBOL(ib_cm_listen
);
999 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
1000 enum cm_msg_sequence msg_seq
)
1002 u64 hi_tid
, low_tid
;
1004 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
1005 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
1007 return cpu_to_be64(hi_tid
| low_tid
);
1010 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
1011 __be16 attr_id
, __be64 tid
)
1013 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1014 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1015 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1016 hdr
->method
= IB_MGMT_METHOD_SEND
;
1017 hdr
->attr_id
= attr_id
;
1021 static void cm_format_req(struct cm_req_msg
*req_msg
,
1022 struct cm_id_private
*cm_id_priv
,
1023 struct ib_cm_req_param
*param
)
1025 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1026 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1028 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1029 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1031 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1032 req_msg
->service_id
= param
->service_id
;
1033 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1034 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1035 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1036 cm_req_set_remote_resp_timeout(req_msg
,
1037 param
->remote_cm_response_timeout
);
1038 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1039 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1040 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1041 cm_req_set_local_resp_timeout(req_msg
,
1042 param
->local_cm_response_timeout
);
1043 req_msg
->pkey
= param
->primary_path
->pkey
;
1044 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1045 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1047 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1048 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1049 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1050 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1051 cm_req_set_srq(req_msg
, param
->srq
);
1054 if (pri_path
->hop_limit
<= 1) {
1055 req_msg
->primary_local_lid
= pri_path
->slid
;
1056 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1058 /* Work-around until there's a way to obtain remote LID info */
1059 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1060 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1062 req_msg
->primary_local_gid
= pri_path
->sgid
;
1063 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1064 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1065 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1066 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1067 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1068 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1069 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1070 cm_req_set_primary_local_ack_timeout(req_msg
,
1071 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1072 pri_path
->packet_life_time
));
1075 if (alt_path
->hop_limit
<= 1) {
1076 req_msg
->alt_local_lid
= alt_path
->slid
;
1077 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1079 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1080 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1082 req_msg
->alt_local_gid
= alt_path
->sgid
;
1083 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1084 cm_req_set_alt_flow_label(req_msg
,
1085 alt_path
->flow_label
);
1086 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1087 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1088 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1089 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1090 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1091 cm_req_set_alt_local_ack_timeout(req_msg
,
1092 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1093 alt_path
->packet_life_time
));
1096 if (param
->private_data
&& param
->private_data_len
)
1097 memcpy(req_msg
->private_data
, param
->private_data
,
1098 param
->private_data_len
);
1101 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1103 /* peer-to-peer not supported */
1104 if (param
->peer_to_peer
)
1107 if (!param
->primary_path
)
1110 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1111 param
->qp_type
!= IB_QPT_XRC_INI
)
1114 if (param
->private_data
&&
1115 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1118 if (param
->alternate_path
&&
1119 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1120 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1126 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1127 struct ib_cm_req_param
*param
)
1129 struct cm_id_private
*cm_id_priv
;
1130 struct cm_req_msg
*req_msg
;
1131 unsigned long flags
;
1134 ret
= cm_validate_req_param(param
);
1138 /* Verify that we're not in timewait. */
1139 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1140 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1141 if (cm_id
->state
!= IB_CM_IDLE
) {
1142 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1146 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1148 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1150 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1151 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1155 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1158 if (param
->alternate_path
) {
1159 ret
= cm_init_av_by_path(param
->alternate_path
,
1160 &cm_id_priv
->alt_av
);
1164 cm_id
->service_id
= param
->service_id
;
1165 cm_id
->service_mask
= ~cpu_to_be64(0);
1166 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1167 param
->primary_path
->packet_life_time
) * 2 +
1169 param
->remote_cm_response_timeout
);
1170 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1171 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1172 cm_id_priv
->responder_resources
= param
->responder_resources
;
1173 cm_id_priv
->retry_count
= param
->retry_count
;
1174 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1175 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1176 cm_id_priv
->qp_type
= param
->qp_type
;
1178 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1182 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1183 cm_format_req(req_msg
, cm_id_priv
, param
);
1184 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1185 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1186 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1188 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1189 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1191 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1192 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1194 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1197 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1198 cm_id
->state
= IB_CM_REQ_SENT
;
1199 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1202 error2
: cm_free_msg(cm_id_priv
->msg
);
1203 error1
: kfree(cm_id_priv
->timewait_info
);
1206 EXPORT_SYMBOL(ib_send_cm_req
);
1208 static int cm_issue_rej(struct cm_port
*port
,
1209 struct ib_mad_recv_wc
*mad_recv_wc
,
1210 enum ib_cm_rej_reason reason
,
1211 enum cm_msg_response msg_rejected
,
1212 void *ari
, u8 ari_length
)
1214 struct ib_mad_send_buf
*msg
= NULL
;
1215 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1218 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1222 /* We just need common CM header information. Cast to any message. */
1223 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1224 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1226 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1227 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1228 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1229 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1230 rej_msg
->reason
= cpu_to_be16(reason
);
1232 if (ari
&& ari_length
) {
1233 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1234 memcpy(rej_msg
->ari
, ari
, ari_length
);
1237 ret
= ib_post_send_mad(msg
, NULL
);
1244 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1245 __be32 local_qpn
, __be32 remote_qpn
)
1247 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1248 ((local_ca_guid
== remote_ca_guid
) &&
1249 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1252 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1253 struct ib_sa_path_rec
*primary_path
,
1254 struct ib_sa_path_rec
*alt_path
)
1256 memset(primary_path
, 0, sizeof *primary_path
);
1257 primary_path
->dgid
= req_msg
->primary_local_gid
;
1258 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1259 primary_path
->dlid
= req_msg
->primary_local_lid
;
1260 primary_path
->slid
= req_msg
->primary_remote_lid
;
1261 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1262 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1263 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1264 primary_path
->reversible
= 1;
1265 primary_path
->pkey
= req_msg
->pkey
;
1266 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1267 primary_path
->mtu_selector
= IB_SA_EQ
;
1268 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1269 primary_path
->rate_selector
= IB_SA_EQ
;
1270 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1271 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1272 primary_path
->packet_life_time
=
1273 cm_req_get_primary_local_ack_timeout(req_msg
);
1274 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1276 if (req_msg
->alt_local_lid
) {
1277 memset(alt_path
, 0, sizeof *alt_path
);
1278 alt_path
->dgid
= req_msg
->alt_local_gid
;
1279 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1280 alt_path
->dlid
= req_msg
->alt_local_lid
;
1281 alt_path
->slid
= req_msg
->alt_remote_lid
;
1282 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1283 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1284 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1285 alt_path
->reversible
= 1;
1286 alt_path
->pkey
= req_msg
->pkey
;
1287 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1288 alt_path
->mtu_selector
= IB_SA_EQ
;
1289 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1290 alt_path
->rate_selector
= IB_SA_EQ
;
1291 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1292 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1293 alt_path
->packet_life_time
=
1294 cm_req_get_alt_local_ack_timeout(req_msg
);
1295 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1299 static void cm_format_req_event(struct cm_work
*work
,
1300 struct cm_id_private
*cm_id_priv
,
1301 struct ib_cm_id
*listen_id
)
1303 struct cm_req_msg
*req_msg
;
1304 struct ib_cm_req_event_param
*param
;
1306 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1307 param
= &work
->cm_event
.param
.req_rcvd
;
1308 param
->listen_id
= listen_id
;
1309 param
->port
= cm_id_priv
->av
.port
->port_num
;
1310 param
->primary_path
= &work
->path
[0];
1311 if (req_msg
->alt_local_lid
)
1312 param
->alternate_path
= &work
->path
[1];
1314 param
->alternate_path
= NULL
;
1315 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1316 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1317 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1318 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1319 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1320 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1321 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1322 param
->local_cm_response_timeout
=
1323 cm_req_get_remote_resp_timeout(req_msg
);
1324 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1325 param
->remote_cm_response_timeout
=
1326 cm_req_get_local_resp_timeout(req_msg
);
1327 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1328 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1329 param
->srq
= cm_req_get_srq(req_msg
);
1330 work
->cm_event
.private_data
= &req_msg
->private_data
;
1333 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1334 struct cm_work
*work
)
1338 /* We will typically only have the current event to report. */
1339 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1342 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1343 spin_lock_irq(&cm_id_priv
->lock
);
1344 work
= cm_dequeue_work(cm_id_priv
);
1345 spin_unlock_irq(&cm_id_priv
->lock
);
1347 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1351 cm_deref_id(cm_id_priv
);
1353 cm_destroy_id(&cm_id_priv
->id
, ret
);
1356 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1357 struct cm_id_private
*cm_id_priv
,
1358 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1359 const void *private_data
, u8 private_data_len
)
1361 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1362 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1363 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1364 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1365 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1367 if (private_data
&& private_data_len
)
1368 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1371 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1372 struct cm_id_private
*cm_id_priv
,
1373 enum ib_cm_rej_reason reason
,
1376 const void *private_data
,
1377 u8 private_data_len
)
1379 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1380 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1382 switch(cm_id_priv
->id
.state
) {
1383 case IB_CM_REQ_RCVD
:
1384 rej_msg
->local_comm_id
= 0;
1385 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1387 case IB_CM_MRA_REQ_SENT
:
1388 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1389 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1391 case IB_CM_REP_RCVD
:
1392 case IB_CM_MRA_REP_SENT
:
1393 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1394 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1397 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1398 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1402 rej_msg
->reason
= cpu_to_be16(reason
);
1403 if (ari
&& ari_length
) {
1404 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1405 memcpy(rej_msg
->ari
, ari
, ari_length
);
1408 if (private_data
&& private_data_len
)
1409 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1412 static void cm_dup_req_handler(struct cm_work
*work
,
1413 struct cm_id_private
*cm_id_priv
)
1415 struct ib_mad_send_buf
*msg
= NULL
;
1418 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1419 counter
[CM_REQ_COUNTER
]);
1421 /* Quick state check to discard duplicate REQs. */
1422 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1425 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1429 spin_lock_irq(&cm_id_priv
->lock
);
1430 switch (cm_id_priv
->id
.state
) {
1431 case IB_CM_MRA_REQ_SENT
:
1432 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1433 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1434 cm_id_priv
->private_data
,
1435 cm_id_priv
->private_data_len
);
1437 case IB_CM_TIMEWAIT
:
1438 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1439 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1444 spin_unlock_irq(&cm_id_priv
->lock
);
1446 ret
= ib_post_send_mad(msg
, NULL
);
1451 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1452 free
: cm_free_msg(msg
);
1455 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1456 struct cm_id_private
*cm_id_priv
)
1458 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1459 struct cm_timewait_info
*timewait_info
;
1460 struct cm_req_msg
*req_msg
;
1462 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1464 /* Check for possible duplicate REQ. */
1465 spin_lock_irq(&cm
.lock
);
1466 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1467 if (timewait_info
) {
1468 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1469 timewait_info
->work
.remote_id
);
1470 spin_unlock_irq(&cm
.lock
);
1471 if (cur_cm_id_priv
) {
1472 cm_dup_req_handler(work
, cur_cm_id_priv
);
1473 cm_deref_id(cur_cm_id_priv
);
1478 /* Check for stale connections. */
1479 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1480 if (timewait_info
) {
1481 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1482 spin_unlock_irq(&cm
.lock
);
1483 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1484 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1489 /* Find matching listen request. */
1490 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1491 req_msg
->service_id
,
1492 req_msg
->private_data
);
1493 if (!listen_cm_id_priv
) {
1494 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1495 spin_unlock_irq(&cm
.lock
);
1496 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1497 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1501 atomic_inc(&listen_cm_id_priv
->refcount
);
1502 atomic_inc(&cm_id_priv
->refcount
);
1503 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1504 atomic_inc(&cm_id_priv
->work_count
);
1505 spin_unlock_irq(&cm
.lock
);
1507 return listen_cm_id_priv
;
1511 * Work-around for inter-subnet connections. If the LIDs are permissive,
1512 * we need to override the LID/SL data in the REQ with the LID information
1513 * in the work completion.
1515 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1517 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1518 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1519 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1520 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1523 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1524 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1527 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1528 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1529 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1530 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1533 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1534 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1538 static int cm_req_handler(struct cm_work
*work
)
1540 struct ib_cm_id
*cm_id
;
1541 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1542 struct cm_req_msg
*req_msg
;
1545 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1547 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1549 return PTR_ERR(cm_id
);
1551 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1552 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1553 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1554 work
->mad_recv_wc
->recv_buf
.grh
,
1556 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1558 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1559 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1562 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1563 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1564 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1566 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1567 if (!listen_cm_id_priv
) {
1569 kfree(cm_id_priv
->timewait_info
);
1573 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1574 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1575 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1576 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1578 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1579 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1581 memcpy(work
->path
[0].dmac
, cm_id_priv
->av
.ah_attr
.dmac
, ETH_ALEN
);
1582 work
->path
[0].vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
1583 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1585 ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1586 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1587 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1588 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1592 if (req_msg
->alt_local_lid
) {
1593 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1595 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1596 &work
->path
[0].sgid
,
1597 sizeof work
->path
[0].sgid
, NULL
, 0);
1601 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1602 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1603 cm_req_get_local_resp_timeout(req_msg
));
1604 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1605 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1606 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1607 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1608 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1609 cm_id_priv
->pkey
= req_msg
->pkey
;
1610 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1611 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1612 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1613 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1615 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1616 cm_process_work(cm_id_priv
, work
);
1617 cm_deref_id(listen_cm_id_priv
);
1621 atomic_dec(&cm_id_priv
->refcount
);
1622 cm_deref_id(listen_cm_id_priv
);
1624 ib_destroy_cm_id(cm_id
);
1628 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1629 struct cm_id_private
*cm_id_priv
,
1630 struct ib_cm_rep_param
*param
)
1632 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1633 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1634 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1635 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1636 rep_msg
->resp_resources
= param
->responder_resources
;
1637 cm_rep_set_target_ack_delay(rep_msg
,
1638 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1639 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1640 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1641 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1643 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1644 rep_msg
->initiator_depth
= param
->initiator_depth
;
1645 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1646 cm_rep_set_srq(rep_msg
, param
->srq
);
1647 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1649 cm_rep_set_srq(rep_msg
, 1);
1650 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1653 if (param
->private_data
&& param
->private_data_len
)
1654 memcpy(rep_msg
->private_data
, param
->private_data
,
1655 param
->private_data_len
);
1658 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1659 struct ib_cm_rep_param
*param
)
1661 struct cm_id_private
*cm_id_priv
;
1662 struct ib_mad_send_buf
*msg
;
1663 struct cm_rep_msg
*rep_msg
;
1664 unsigned long flags
;
1667 if (param
->private_data
&&
1668 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1671 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1672 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1673 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1674 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1679 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1683 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1684 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1685 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1686 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1688 ret
= ib_post_send_mad(msg
, NULL
);
1690 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1695 cm_id
->state
= IB_CM_REP_SENT
;
1696 cm_id_priv
->msg
= msg
;
1697 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1698 cm_id_priv
->responder_resources
= param
->responder_resources
;
1699 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1700 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1702 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1705 EXPORT_SYMBOL(ib_send_cm_rep
);
1707 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1708 struct cm_id_private
*cm_id_priv
,
1709 const void *private_data
,
1710 u8 private_data_len
)
1712 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1713 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1714 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1716 if (private_data
&& private_data_len
)
1717 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1720 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1721 const void *private_data
,
1722 u8 private_data_len
)
1724 struct cm_id_private
*cm_id_priv
;
1725 struct ib_mad_send_buf
*msg
;
1726 unsigned long flags
;
1730 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1733 data
= cm_copy_private_data(private_data
, private_data_len
);
1735 return PTR_ERR(data
);
1737 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1738 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1739 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1740 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1745 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1749 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1750 private_data
, private_data_len
);
1752 ret
= ib_post_send_mad(msg
, NULL
);
1754 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1760 cm_id
->state
= IB_CM_ESTABLISHED
;
1761 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1762 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1765 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1769 EXPORT_SYMBOL(ib_send_cm_rtu
);
1771 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
1773 struct cm_rep_msg
*rep_msg
;
1774 struct ib_cm_rep_event_param
*param
;
1776 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1777 param
= &work
->cm_event
.param
.rep_rcvd
;
1778 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1779 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1780 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
1781 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1782 param
->responder_resources
= rep_msg
->initiator_depth
;
1783 param
->initiator_depth
= rep_msg
->resp_resources
;
1784 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1785 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1786 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1787 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1788 param
->srq
= cm_rep_get_srq(rep_msg
);
1789 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1792 static void cm_dup_rep_handler(struct cm_work
*work
)
1794 struct cm_id_private
*cm_id_priv
;
1795 struct cm_rep_msg
*rep_msg
;
1796 struct ib_mad_send_buf
*msg
= NULL
;
1799 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1800 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1801 rep_msg
->local_comm_id
);
1805 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1806 counter
[CM_REP_COUNTER
]);
1807 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1811 spin_lock_irq(&cm_id_priv
->lock
);
1812 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1813 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1814 cm_id_priv
->private_data
,
1815 cm_id_priv
->private_data_len
);
1816 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1817 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1818 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1819 cm_id_priv
->private_data
,
1820 cm_id_priv
->private_data_len
);
1823 spin_unlock_irq(&cm_id_priv
->lock
);
1825 ret
= ib_post_send_mad(msg
, NULL
);
1830 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1831 free
: cm_free_msg(msg
);
1832 deref
: cm_deref_id(cm_id_priv
);
1835 static int cm_rep_handler(struct cm_work
*work
)
1837 struct cm_id_private
*cm_id_priv
;
1838 struct cm_rep_msg
*rep_msg
;
1841 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1842 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1844 cm_dup_rep_handler(work
);
1848 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
1850 spin_lock_irq(&cm_id_priv
->lock
);
1851 switch (cm_id_priv
->id
.state
) {
1852 case IB_CM_REQ_SENT
:
1853 case IB_CM_MRA_REQ_RCVD
:
1856 spin_unlock_irq(&cm_id_priv
->lock
);
1861 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1862 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1863 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1865 spin_lock(&cm
.lock
);
1866 /* Check for duplicate REP. */
1867 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1868 spin_unlock(&cm
.lock
);
1869 spin_unlock_irq(&cm_id_priv
->lock
);
1873 /* Check for a stale connection. */
1874 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1875 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1876 &cm
.remote_id_table
);
1877 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1878 spin_unlock(&cm
.lock
);
1879 spin_unlock_irq(&cm_id_priv
->lock
);
1880 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1881 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1886 spin_unlock(&cm
.lock
);
1888 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1889 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1890 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1891 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1892 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1893 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1894 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1895 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1896 cm_id_priv
->av
.timeout
=
1897 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1898 cm_id_priv
->av
.timeout
- 1);
1899 cm_id_priv
->alt_av
.timeout
=
1900 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1901 cm_id_priv
->alt_av
.timeout
- 1);
1903 /* todo: handle peer_to_peer */
1905 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1906 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1908 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1909 spin_unlock_irq(&cm_id_priv
->lock
);
1912 cm_process_work(cm_id_priv
, work
);
1914 cm_deref_id(cm_id_priv
);
1918 cm_deref_id(cm_id_priv
);
1922 static int cm_establish_handler(struct cm_work
*work
)
1924 struct cm_id_private
*cm_id_priv
;
1927 /* See comment in cm_establish about lookup. */
1928 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1932 spin_lock_irq(&cm_id_priv
->lock
);
1933 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1934 spin_unlock_irq(&cm_id_priv
->lock
);
1938 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1939 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1941 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1942 spin_unlock_irq(&cm_id_priv
->lock
);
1945 cm_process_work(cm_id_priv
, work
);
1947 cm_deref_id(cm_id_priv
);
1950 cm_deref_id(cm_id_priv
);
1954 static int cm_rtu_handler(struct cm_work
*work
)
1956 struct cm_id_private
*cm_id_priv
;
1957 struct cm_rtu_msg
*rtu_msg
;
1960 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1961 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1962 rtu_msg
->local_comm_id
);
1966 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1968 spin_lock_irq(&cm_id_priv
->lock
);
1969 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1970 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1971 spin_unlock_irq(&cm_id_priv
->lock
);
1972 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1973 counter
[CM_RTU_COUNTER
]);
1976 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1978 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1979 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1981 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1982 spin_unlock_irq(&cm_id_priv
->lock
);
1985 cm_process_work(cm_id_priv
, work
);
1987 cm_deref_id(cm_id_priv
);
1990 cm_deref_id(cm_id_priv
);
1994 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1995 struct cm_id_private
*cm_id_priv
,
1996 const void *private_data
,
1997 u8 private_data_len
)
1999 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
2000 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
2001 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2002 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2003 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
2005 if (private_data
&& private_data_len
)
2006 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
2009 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
2010 const void *private_data
,
2011 u8 private_data_len
)
2013 struct cm_id_private
*cm_id_priv
;
2014 struct ib_mad_send_buf
*msg
;
2015 unsigned long flags
;
2018 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2021 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2022 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2023 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2028 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2029 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2030 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2032 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2034 cm_enter_timewait(cm_id_priv
);
2038 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2039 private_data
, private_data_len
);
2040 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2041 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2043 ret
= ib_post_send_mad(msg
, NULL
);
2045 cm_enter_timewait(cm_id_priv
);
2046 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2051 cm_id
->state
= IB_CM_DREQ_SENT
;
2052 cm_id_priv
->msg
= msg
;
2053 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2056 EXPORT_SYMBOL(ib_send_cm_dreq
);
2058 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2059 struct cm_id_private
*cm_id_priv
,
2060 const void *private_data
,
2061 u8 private_data_len
)
2063 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2064 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2065 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2067 if (private_data
&& private_data_len
)
2068 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2071 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2072 const void *private_data
,
2073 u8 private_data_len
)
2075 struct cm_id_private
*cm_id_priv
;
2076 struct ib_mad_send_buf
*msg
;
2077 unsigned long flags
;
2081 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2084 data
= cm_copy_private_data(private_data
, private_data_len
);
2086 return PTR_ERR(data
);
2088 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2089 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2090 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2091 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2096 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2097 cm_enter_timewait(cm_id_priv
);
2099 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2103 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2104 private_data
, private_data_len
);
2106 ret
= ib_post_send_mad(msg
, NULL
);
2108 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2113 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2116 EXPORT_SYMBOL(ib_send_cm_drep
);
2118 static int cm_issue_drep(struct cm_port
*port
,
2119 struct ib_mad_recv_wc
*mad_recv_wc
)
2121 struct ib_mad_send_buf
*msg
= NULL
;
2122 struct cm_dreq_msg
*dreq_msg
;
2123 struct cm_drep_msg
*drep_msg
;
2126 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2130 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2131 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2133 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2134 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2135 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2137 ret
= ib_post_send_mad(msg
, NULL
);
2144 static int cm_dreq_handler(struct cm_work
*work
)
2146 struct cm_id_private
*cm_id_priv
;
2147 struct cm_dreq_msg
*dreq_msg
;
2148 struct ib_mad_send_buf
*msg
= NULL
;
2151 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2152 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2153 dreq_msg
->local_comm_id
);
2155 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2156 counter
[CM_DREQ_COUNTER
]);
2157 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2161 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2163 spin_lock_irq(&cm_id_priv
->lock
);
2164 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2167 switch (cm_id_priv
->id
.state
) {
2168 case IB_CM_REP_SENT
:
2169 case IB_CM_DREQ_SENT
:
2170 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2172 case IB_CM_ESTABLISHED
:
2173 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2174 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2175 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2177 case IB_CM_MRA_REP_RCVD
:
2179 case IB_CM_TIMEWAIT
:
2180 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2181 counter
[CM_DREQ_COUNTER
]);
2182 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2185 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2186 cm_id_priv
->private_data
,
2187 cm_id_priv
->private_data_len
);
2188 spin_unlock_irq(&cm_id_priv
->lock
);
2190 if (ib_post_send_mad(msg
, NULL
))
2193 case IB_CM_DREQ_RCVD
:
2194 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2195 counter
[CM_DREQ_COUNTER
]);
2200 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2201 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2202 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2204 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2205 spin_unlock_irq(&cm_id_priv
->lock
);
2208 cm_process_work(cm_id_priv
, work
);
2210 cm_deref_id(cm_id_priv
);
2213 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2214 deref
: cm_deref_id(cm_id_priv
);
2218 static int cm_drep_handler(struct cm_work
*work
)
2220 struct cm_id_private
*cm_id_priv
;
2221 struct cm_drep_msg
*drep_msg
;
2224 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2225 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2226 drep_msg
->local_comm_id
);
2230 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2232 spin_lock_irq(&cm_id_priv
->lock
);
2233 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2234 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2235 spin_unlock_irq(&cm_id_priv
->lock
);
2238 cm_enter_timewait(cm_id_priv
);
2240 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2241 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2243 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2244 spin_unlock_irq(&cm_id_priv
->lock
);
2247 cm_process_work(cm_id_priv
, work
);
2249 cm_deref_id(cm_id_priv
);
2252 cm_deref_id(cm_id_priv
);
2256 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2257 enum ib_cm_rej_reason reason
,
2260 const void *private_data
,
2261 u8 private_data_len
)
2263 struct cm_id_private
*cm_id_priv
;
2264 struct ib_mad_send_buf
*msg
;
2265 unsigned long flags
;
2268 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2269 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2272 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2274 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2275 switch (cm_id
->state
) {
2276 case IB_CM_REQ_SENT
:
2277 case IB_CM_MRA_REQ_RCVD
:
2278 case IB_CM_REQ_RCVD
:
2279 case IB_CM_MRA_REQ_SENT
:
2280 case IB_CM_REP_RCVD
:
2281 case IB_CM_MRA_REP_SENT
:
2282 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2284 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2285 cm_id_priv
, reason
, ari
, ari_length
,
2286 private_data
, private_data_len
);
2288 cm_reset_to_idle(cm_id_priv
);
2290 case IB_CM_REP_SENT
:
2291 case IB_CM_MRA_REP_RCVD
:
2292 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2294 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2295 cm_id_priv
, reason
, ari
, ari_length
,
2296 private_data
, private_data_len
);
2298 cm_enter_timewait(cm_id_priv
);
2308 ret
= ib_post_send_mad(msg
, NULL
);
2312 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2315 EXPORT_SYMBOL(ib_send_cm_rej
);
2317 static void cm_format_rej_event(struct cm_work
*work
)
2319 struct cm_rej_msg
*rej_msg
;
2320 struct ib_cm_rej_event_param
*param
;
2322 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2323 param
= &work
->cm_event
.param
.rej_rcvd
;
2324 param
->ari
= rej_msg
->ari
;
2325 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2326 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2327 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2330 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2332 struct cm_timewait_info
*timewait_info
;
2333 struct cm_id_private
*cm_id_priv
;
2336 remote_id
= rej_msg
->local_comm_id
;
2338 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2339 spin_lock_irq(&cm
.lock
);
2340 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2342 if (!timewait_info
) {
2343 spin_unlock_irq(&cm
.lock
);
2346 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2347 (timewait_info
->work
.local_id
^
2348 cm
.random_id_operand
));
2350 if (cm_id_priv
->id
.remote_id
== remote_id
)
2351 atomic_inc(&cm_id_priv
->refcount
);
2355 spin_unlock_irq(&cm
.lock
);
2356 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2357 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2359 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2364 static int cm_rej_handler(struct cm_work
*work
)
2366 struct cm_id_private
*cm_id_priv
;
2367 struct cm_rej_msg
*rej_msg
;
2370 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2371 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2375 cm_format_rej_event(work
);
2377 spin_lock_irq(&cm_id_priv
->lock
);
2378 switch (cm_id_priv
->id
.state
) {
2379 case IB_CM_REQ_SENT
:
2380 case IB_CM_MRA_REQ_RCVD
:
2381 case IB_CM_REP_SENT
:
2382 case IB_CM_MRA_REP_RCVD
:
2383 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2385 case IB_CM_REQ_RCVD
:
2386 case IB_CM_MRA_REQ_SENT
:
2387 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2388 cm_enter_timewait(cm_id_priv
);
2390 cm_reset_to_idle(cm_id_priv
);
2392 case IB_CM_DREQ_SENT
:
2393 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2395 case IB_CM_REP_RCVD
:
2396 case IB_CM_MRA_REP_SENT
:
2397 cm_enter_timewait(cm_id_priv
);
2399 case IB_CM_ESTABLISHED
:
2400 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2401 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2402 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2403 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2405 cm_enter_timewait(cm_id_priv
);
2410 spin_unlock_irq(&cm_id_priv
->lock
);
2415 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2417 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2418 spin_unlock_irq(&cm_id_priv
->lock
);
2421 cm_process_work(cm_id_priv
, work
);
2423 cm_deref_id(cm_id_priv
);
2426 cm_deref_id(cm_id_priv
);
2430 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2432 const void *private_data
,
2433 u8 private_data_len
)
2435 struct cm_id_private
*cm_id_priv
;
2436 struct ib_mad_send_buf
*msg
;
2437 enum ib_cm_state cm_state
;
2438 enum ib_cm_lap_state lap_state
;
2439 enum cm_msg_response msg_response
;
2441 unsigned long flags
;
2444 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2447 data
= cm_copy_private_data(private_data
, private_data_len
);
2449 return PTR_ERR(data
);
2451 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2453 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2454 switch(cm_id_priv
->id
.state
) {
2455 case IB_CM_REQ_RCVD
:
2456 cm_state
= IB_CM_MRA_REQ_SENT
;
2457 lap_state
= cm_id
->lap_state
;
2458 msg_response
= CM_MSG_RESPONSE_REQ
;
2460 case IB_CM_REP_RCVD
:
2461 cm_state
= IB_CM_MRA_REP_SENT
;
2462 lap_state
= cm_id
->lap_state
;
2463 msg_response
= CM_MSG_RESPONSE_REP
;
2465 case IB_CM_ESTABLISHED
:
2466 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2467 cm_state
= cm_id
->state
;
2468 lap_state
= IB_CM_MRA_LAP_SENT
;
2469 msg_response
= CM_MSG_RESPONSE_OTHER
;
2477 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2478 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2482 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2483 msg_response
, service_timeout
,
2484 private_data
, private_data_len
);
2485 ret
= ib_post_send_mad(msg
, NULL
);
2490 cm_id
->state
= cm_state
;
2491 cm_id
->lap_state
= lap_state
;
2492 cm_id_priv
->service_timeout
= service_timeout
;
2493 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2494 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2497 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2501 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2506 EXPORT_SYMBOL(ib_send_cm_mra
);
2508 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2510 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2511 case CM_MSG_RESPONSE_REQ
:
2512 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2513 case CM_MSG_RESPONSE_REP
:
2514 case CM_MSG_RESPONSE_OTHER
:
2515 return cm_acquire_id(mra_msg
->remote_comm_id
,
2516 mra_msg
->local_comm_id
);
2522 static int cm_mra_handler(struct cm_work
*work
)
2524 struct cm_id_private
*cm_id_priv
;
2525 struct cm_mra_msg
*mra_msg
;
2528 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2529 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2533 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2534 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2535 cm_mra_get_service_timeout(mra_msg
);
2536 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2537 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2539 spin_lock_irq(&cm_id_priv
->lock
);
2540 switch (cm_id_priv
->id
.state
) {
2541 case IB_CM_REQ_SENT
:
2542 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2543 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2544 cm_id_priv
->msg
, timeout
))
2546 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2548 case IB_CM_REP_SENT
:
2549 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2550 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2551 cm_id_priv
->msg
, timeout
))
2553 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2555 case IB_CM_ESTABLISHED
:
2556 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2557 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2558 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2559 cm_id_priv
->msg
, timeout
)) {
2560 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2561 atomic_long_inc(&work
->port
->
2562 counter_group
[CM_RECV_DUPLICATES
].
2563 counter
[CM_MRA_COUNTER
]);
2566 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2568 case IB_CM_MRA_REQ_RCVD
:
2569 case IB_CM_MRA_REP_RCVD
:
2570 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2571 counter
[CM_MRA_COUNTER
]);
2577 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2578 cm_id_priv
->id
.state
;
2579 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2581 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2582 spin_unlock_irq(&cm_id_priv
->lock
);
2585 cm_process_work(cm_id_priv
, work
);
2587 cm_deref_id(cm_id_priv
);
2590 spin_unlock_irq(&cm_id_priv
->lock
);
2591 cm_deref_id(cm_id_priv
);
2595 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2596 struct cm_id_private
*cm_id_priv
,
2597 struct ib_sa_path_rec
*alternate_path
,
2598 const void *private_data
,
2599 u8 private_data_len
)
2601 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2602 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2603 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2604 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2605 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2606 /* todo: need remote CM response timeout */
2607 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2608 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2609 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2610 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2611 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2612 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2613 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2614 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2615 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2616 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2617 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2618 cm_lap_set_local_ack_timeout(lap_msg
,
2619 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2620 alternate_path
->packet_life_time
));
2622 if (private_data
&& private_data_len
)
2623 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2626 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2627 struct ib_sa_path_rec
*alternate_path
,
2628 const void *private_data
,
2629 u8 private_data_len
)
2631 struct cm_id_private
*cm_id_priv
;
2632 struct ib_mad_send_buf
*msg
;
2633 unsigned long flags
;
2636 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2639 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2640 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2641 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2642 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2643 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2648 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2651 cm_id_priv
->alt_av
.timeout
=
2652 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2653 cm_id_priv
->alt_av
.timeout
- 1);
2655 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2659 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2660 alternate_path
, private_data
, private_data_len
);
2661 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2662 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2664 ret
= ib_post_send_mad(msg
, NULL
);
2666 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2671 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2672 cm_id_priv
->msg
= msg
;
2674 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2677 EXPORT_SYMBOL(ib_send_cm_lap
);
2679 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2680 struct ib_sa_path_rec
*path
,
2681 struct cm_lap_msg
*lap_msg
)
2683 memset(path
, 0, sizeof *path
);
2684 path
->dgid
= lap_msg
->alt_local_gid
;
2685 path
->sgid
= lap_msg
->alt_remote_gid
;
2686 path
->dlid
= lap_msg
->alt_local_lid
;
2687 path
->slid
= lap_msg
->alt_remote_lid
;
2688 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2689 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2690 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2691 path
->reversible
= 1;
2692 path
->pkey
= cm_id_priv
->pkey
;
2693 path
->sl
= cm_lap_get_sl(lap_msg
);
2694 path
->mtu_selector
= IB_SA_EQ
;
2695 path
->mtu
= cm_id_priv
->path_mtu
;
2696 path
->rate_selector
= IB_SA_EQ
;
2697 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2698 path
->packet_life_time_selector
= IB_SA_EQ
;
2699 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2700 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2703 static int cm_lap_handler(struct cm_work
*work
)
2705 struct cm_id_private
*cm_id_priv
;
2706 struct cm_lap_msg
*lap_msg
;
2707 struct ib_cm_lap_event_param
*param
;
2708 struct ib_mad_send_buf
*msg
= NULL
;
2711 /* todo: verify LAP request and send reject APR if invalid. */
2712 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2713 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2714 lap_msg
->local_comm_id
);
2718 param
= &work
->cm_event
.param
.lap_rcvd
;
2719 param
->alternate_path
= &work
->path
[0];
2720 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2721 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2723 spin_lock_irq(&cm_id_priv
->lock
);
2724 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2727 switch (cm_id_priv
->id
.lap_state
) {
2728 case IB_CM_LAP_UNINIT
:
2729 case IB_CM_LAP_IDLE
:
2731 case IB_CM_MRA_LAP_SENT
:
2732 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2733 counter
[CM_LAP_COUNTER
]);
2734 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2737 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2738 CM_MSG_RESPONSE_OTHER
,
2739 cm_id_priv
->service_timeout
,
2740 cm_id_priv
->private_data
,
2741 cm_id_priv
->private_data_len
);
2742 spin_unlock_irq(&cm_id_priv
->lock
);
2744 if (ib_post_send_mad(msg
, NULL
))
2747 case IB_CM_LAP_RCVD
:
2748 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2749 counter
[CM_LAP_COUNTER
]);
2755 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2756 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2757 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2758 work
->mad_recv_wc
->recv_buf
.grh
,
2760 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2761 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2763 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2764 spin_unlock_irq(&cm_id_priv
->lock
);
2767 cm_process_work(cm_id_priv
, work
);
2769 cm_deref_id(cm_id_priv
);
2772 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2773 deref
: cm_deref_id(cm_id_priv
);
2777 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2778 struct cm_id_private
*cm_id_priv
,
2779 enum ib_cm_apr_status status
,
2782 const void *private_data
,
2783 u8 private_data_len
)
2785 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2786 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2787 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2788 apr_msg
->ap_status
= (u8
) status
;
2790 if (info
&& info_length
) {
2791 apr_msg
->info_length
= info_length
;
2792 memcpy(apr_msg
->info
, info
, info_length
);
2795 if (private_data
&& private_data_len
)
2796 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2799 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2800 enum ib_cm_apr_status status
,
2803 const void *private_data
,
2804 u8 private_data_len
)
2806 struct cm_id_private
*cm_id_priv
;
2807 struct ib_mad_send_buf
*msg
;
2808 unsigned long flags
;
2811 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2812 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2815 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2816 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2817 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2818 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2819 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2824 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2828 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2829 info
, info_length
, private_data
, private_data_len
);
2830 ret
= ib_post_send_mad(msg
, NULL
);
2832 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2837 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2838 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2841 EXPORT_SYMBOL(ib_send_cm_apr
);
2843 static int cm_apr_handler(struct cm_work
*work
)
2845 struct cm_id_private
*cm_id_priv
;
2846 struct cm_apr_msg
*apr_msg
;
2849 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2850 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2851 apr_msg
->local_comm_id
);
2853 return -EINVAL
; /* Unmatched reply. */
2855 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2856 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2857 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2858 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2860 spin_lock_irq(&cm_id_priv
->lock
);
2861 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2862 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2863 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2864 spin_unlock_irq(&cm_id_priv
->lock
);
2867 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2868 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2869 cm_id_priv
->msg
= NULL
;
2871 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2873 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2874 spin_unlock_irq(&cm_id_priv
->lock
);
2877 cm_process_work(cm_id_priv
, work
);
2879 cm_deref_id(cm_id_priv
);
2882 cm_deref_id(cm_id_priv
);
2886 static int cm_timewait_handler(struct cm_work
*work
)
2888 struct cm_timewait_info
*timewait_info
;
2889 struct cm_id_private
*cm_id_priv
;
2892 timewait_info
= (struct cm_timewait_info
*)work
;
2893 spin_lock_irq(&cm
.lock
);
2894 list_del(&timewait_info
->list
);
2895 spin_unlock_irq(&cm
.lock
);
2897 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2898 timewait_info
->work
.remote_id
);
2902 spin_lock_irq(&cm_id_priv
->lock
);
2903 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2904 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2905 spin_unlock_irq(&cm_id_priv
->lock
);
2908 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2909 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2911 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2912 spin_unlock_irq(&cm_id_priv
->lock
);
2915 cm_process_work(cm_id_priv
, work
);
2917 cm_deref_id(cm_id_priv
);
2920 cm_deref_id(cm_id_priv
);
2924 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2925 struct cm_id_private
*cm_id_priv
,
2926 struct ib_cm_sidr_req_param
*param
)
2928 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2929 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2930 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2931 sidr_req_msg
->pkey
= param
->path
->pkey
;
2932 sidr_req_msg
->service_id
= param
->service_id
;
2934 if (param
->private_data
&& param
->private_data_len
)
2935 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2936 param
->private_data_len
);
2939 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2940 struct ib_cm_sidr_req_param
*param
)
2942 struct cm_id_private
*cm_id_priv
;
2943 struct ib_mad_send_buf
*msg
;
2944 unsigned long flags
;
2947 if (!param
->path
|| (param
->private_data
&&
2948 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2951 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2952 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2956 cm_id
->service_id
= param
->service_id
;
2957 cm_id
->service_mask
= ~cpu_to_be64(0);
2958 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2959 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2960 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2964 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2966 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2967 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2969 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2970 if (cm_id
->state
== IB_CM_IDLE
)
2971 ret
= ib_post_send_mad(msg
, NULL
);
2976 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2980 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2981 cm_id_priv
->msg
= msg
;
2982 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2986 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2988 static void cm_format_sidr_req_event(struct cm_work
*work
,
2989 struct ib_cm_id
*listen_id
)
2991 struct cm_sidr_req_msg
*sidr_req_msg
;
2992 struct ib_cm_sidr_req_event_param
*param
;
2994 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2995 work
->mad_recv_wc
->recv_buf
.mad
;
2996 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2997 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2998 param
->listen_id
= listen_id
;
2999 param
->port
= work
->port
->port_num
;
3000 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
3003 static int cm_sidr_req_handler(struct cm_work
*work
)
3005 struct ib_cm_id
*cm_id
;
3006 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
3007 struct cm_sidr_req_msg
*sidr_req_msg
;
3010 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3012 return PTR_ERR(cm_id
);
3013 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3015 /* Record SGID/SLID and request ID for lookup. */
3016 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3017 work
->mad_recv_wc
->recv_buf
.mad
;
3018 wc
= work
->mad_recv_wc
->wc
;
3019 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3020 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3021 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3022 work
->mad_recv_wc
->recv_buf
.grh
,
3024 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
3025 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3026 atomic_inc(&cm_id_priv
->work_count
);
3028 spin_lock_irq(&cm
.lock
);
3029 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3030 if (cur_cm_id_priv
) {
3031 spin_unlock_irq(&cm
.lock
);
3032 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3033 counter
[CM_SIDR_REQ_COUNTER
]);
3034 goto out
; /* Duplicate message. */
3036 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3037 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3038 sidr_req_msg
->service_id
,
3039 sidr_req_msg
->private_data
);
3040 if (!cur_cm_id_priv
) {
3041 spin_unlock_irq(&cm
.lock
);
3042 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3043 goto out
; /* No match. */
3045 atomic_inc(&cur_cm_id_priv
->refcount
);
3046 atomic_inc(&cm_id_priv
->refcount
);
3047 spin_unlock_irq(&cm
.lock
);
3049 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3050 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3051 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3052 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3054 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3055 cm_process_work(cm_id_priv
, work
);
3056 cm_deref_id(cur_cm_id_priv
);
3059 ib_destroy_cm_id(&cm_id_priv
->id
);
3063 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3064 struct cm_id_private
*cm_id_priv
,
3065 struct ib_cm_sidr_rep_param
*param
)
3067 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3069 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3070 sidr_rep_msg
->status
= param
->status
;
3071 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3072 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3073 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3075 if (param
->info
&& param
->info_length
)
3076 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3078 if (param
->private_data
&& param
->private_data_len
)
3079 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3080 param
->private_data_len
);
3083 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3084 struct ib_cm_sidr_rep_param
*param
)
3086 struct cm_id_private
*cm_id_priv
;
3087 struct ib_mad_send_buf
*msg
;
3088 unsigned long flags
;
3091 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3092 (param
->private_data
&&
3093 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3096 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3097 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3098 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3103 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3107 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3109 ret
= ib_post_send_mad(msg
, NULL
);
3111 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3115 cm_id
->state
= IB_CM_IDLE
;
3116 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3118 spin_lock_irqsave(&cm
.lock
, flags
);
3119 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3120 spin_unlock_irqrestore(&cm
.lock
, flags
);
3123 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3126 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3128 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3130 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3131 struct ib_cm_sidr_rep_event_param
*param
;
3133 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3134 work
->mad_recv_wc
->recv_buf
.mad
;
3135 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3136 param
->status
= sidr_rep_msg
->status
;
3137 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3138 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3139 param
->info
= &sidr_rep_msg
->info
;
3140 param
->info_len
= sidr_rep_msg
->info_length
;
3141 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3144 static int cm_sidr_rep_handler(struct cm_work
*work
)
3146 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3147 struct cm_id_private
*cm_id_priv
;
3149 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3150 work
->mad_recv_wc
->recv_buf
.mad
;
3151 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3153 return -EINVAL
; /* Unmatched reply. */
3155 spin_lock_irq(&cm_id_priv
->lock
);
3156 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3157 spin_unlock_irq(&cm_id_priv
->lock
);
3160 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3161 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3162 spin_unlock_irq(&cm_id_priv
->lock
);
3164 cm_format_sidr_rep_event(work
);
3165 cm_process_work(cm_id_priv
, work
);
3168 cm_deref_id(cm_id_priv
);
3172 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3173 enum ib_wc_status wc_status
)
3175 struct cm_id_private
*cm_id_priv
;
3176 struct ib_cm_event cm_event
;
3177 enum ib_cm_state state
;
3180 memset(&cm_event
, 0, sizeof cm_event
);
3181 cm_id_priv
= msg
->context
[0];
3183 /* Discard old sends or ones without a response. */
3184 spin_lock_irq(&cm_id_priv
->lock
);
3185 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3186 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3190 case IB_CM_REQ_SENT
:
3191 case IB_CM_MRA_REQ_RCVD
:
3192 cm_reset_to_idle(cm_id_priv
);
3193 cm_event
.event
= IB_CM_REQ_ERROR
;
3195 case IB_CM_REP_SENT
:
3196 case IB_CM_MRA_REP_RCVD
:
3197 cm_reset_to_idle(cm_id_priv
);
3198 cm_event
.event
= IB_CM_REP_ERROR
;
3200 case IB_CM_DREQ_SENT
:
3201 cm_enter_timewait(cm_id_priv
);
3202 cm_event
.event
= IB_CM_DREQ_ERROR
;
3204 case IB_CM_SIDR_REQ_SENT
:
3205 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3206 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3211 spin_unlock_irq(&cm_id_priv
->lock
);
3212 cm_event
.param
.send_status
= wc_status
;
3214 /* No other events can occur on the cm_id at this point. */
3215 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3218 ib_destroy_cm_id(&cm_id_priv
->id
);
3221 spin_unlock_irq(&cm_id_priv
->lock
);
3225 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3226 struct ib_mad_send_wc
*mad_send_wc
)
3228 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3229 struct cm_port
*port
;
3232 port
= mad_agent
->context
;
3233 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3234 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3237 * If the send was in response to a received message (context[0] is not
3238 * set to a cm_id), and is not a REJ, then it is a send that was
3241 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3244 atomic_long_add(1 + msg
->retries
,
3245 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3247 atomic_long_add(msg
->retries
,
3248 &port
->counter_group
[CM_XMIT_RETRIES
].
3249 counter
[attr_index
]);
3251 switch (mad_send_wc
->status
) {
3253 case IB_WC_WR_FLUSH_ERR
:
3257 if (msg
->context
[0] && msg
->context
[1])
3258 cm_process_send_error(msg
, mad_send_wc
->status
);
3265 static void cm_work_handler(struct work_struct
*_work
)
3267 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3270 switch (work
->cm_event
.event
) {
3271 case IB_CM_REQ_RECEIVED
:
3272 ret
= cm_req_handler(work
);
3274 case IB_CM_MRA_RECEIVED
:
3275 ret
= cm_mra_handler(work
);
3277 case IB_CM_REJ_RECEIVED
:
3278 ret
= cm_rej_handler(work
);
3280 case IB_CM_REP_RECEIVED
:
3281 ret
= cm_rep_handler(work
);
3283 case IB_CM_RTU_RECEIVED
:
3284 ret
= cm_rtu_handler(work
);
3286 case IB_CM_USER_ESTABLISHED
:
3287 ret
= cm_establish_handler(work
);
3289 case IB_CM_DREQ_RECEIVED
:
3290 ret
= cm_dreq_handler(work
);
3292 case IB_CM_DREP_RECEIVED
:
3293 ret
= cm_drep_handler(work
);
3295 case IB_CM_SIDR_REQ_RECEIVED
:
3296 ret
= cm_sidr_req_handler(work
);
3298 case IB_CM_SIDR_REP_RECEIVED
:
3299 ret
= cm_sidr_rep_handler(work
);
3301 case IB_CM_LAP_RECEIVED
:
3302 ret
= cm_lap_handler(work
);
3304 case IB_CM_APR_RECEIVED
:
3305 ret
= cm_apr_handler(work
);
3307 case IB_CM_TIMEWAIT_EXIT
:
3308 ret
= cm_timewait_handler(work
);
3318 static int cm_establish(struct ib_cm_id
*cm_id
)
3320 struct cm_id_private
*cm_id_priv
;
3321 struct cm_work
*work
;
3322 unsigned long flags
;
3325 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3329 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3330 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3331 switch (cm_id
->state
)
3333 case IB_CM_REP_SENT
:
3334 case IB_CM_MRA_REP_RCVD
:
3335 cm_id
->state
= IB_CM_ESTABLISHED
;
3337 case IB_CM_ESTABLISHED
:
3344 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3352 * The CM worker thread may try to destroy the cm_id before it
3353 * can execute this work item. To prevent potential deadlock,
3354 * we need to find the cm_id once we're in the context of the
3355 * worker thread, rather than holding a reference on it.
3357 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3358 work
->local_id
= cm_id
->local_id
;
3359 work
->remote_id
= cm_id
->remote_id
;
3360 work
->mad_recv_wc
= NULL
;
3361 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3362 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3367 static int cm_migrate(struct ib_cm_id
*cm_id
)
3369 struct cm_id_private
*cm_id_priv
;
3370 unsigned long flags
;
3373 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3374 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3375 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3376 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3377 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3378 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3379 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3382 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3387 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3392 case IB_EVENT_COMM_EST
:
3393 ret
= cm_establish(cm_id
);
3395 case IB_EVENT_PATH_MIG
:
3396 ret
= cm_migrate(cm_id
);
3403 EXPORT_SYMBOL(ib_cm_notify
);
3405 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3406 struct ib_mad_recv_wc
*mad_recv_wc
)
3408 struct cm_port
*port
= mad_agent
->context
;
3409 struct cm_work
*work
;
3410 enum ib_cm_event_type event
;
3414 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3415 case CM_REQ_ATTR_ID
:
3416 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3417 alt_local_lid
!= 0);
3418 event
= IB_CM_REQ_RECEIVED
;
3420 case CM_MRA_ATTR_ID
:
3421 event
= IB_CM_MRA_RECEIVED
;
3423 case CM_REJ_ATTR_ID
:
3424 event
= IB_CM_REJ_RECEIVED
;
3426 case CM_REP_ATTR_ID
:
3427 event
= IB_CM_REP_RECEIVED
;
3429 case CM_RTU_ATTR_ID
:
3430 event
= IB_CM_RTU_RECEIVED
;
3432 case CM_DREQ_ATTR_ID
:
3433 event
= IB_CM_DREQ_RECEIVED
;
3435 case CM_DREP_ATTR_ID
:
3436 event
= IB_CM_DREP_RECEIVED
;
3438 case CM_SIDR_REQ_ATTR_ID
:
3439 event
= IB_CM_SIDR_REQ_RECEIVED
;
3441 case CM_SIDR_REP_ATTR_ID
:
3442 event
= IB_CM_SIDR_REP_RECEIVED
;
3444 case CM_LAP_ATTR_ID
:
3446 event
= IB_CM_LAP_RECEIVED
;
3448 case CM_APR_ATTR_ID
:
3449 event
= IB_CM_APR_RECEIVED
;
3452 ib_free_recv_mad(mad_recv_wc
);
3456 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3457 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3458 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3460 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3463 ib_free_recv_mad(mad_recv_wc
);
3467 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3468 work
->cm_event
.event
= event
;
3469 work
->mad_recv_wc
= mad_recv_wc
;
3471 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3474 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3475 struct ib_qp_attr
*qp_attr
,
3478 unsigned long flags
;
3481 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3482 switch (cm_id_priv
->id
.state
) {
3483 case IB_CM_REQ_SENT
:
3484 case IB_CM_MRA_REQ_RCVD
:
3485 case IB_CM_REQ_RCVD
:
3486 case IB_CM_MRA_REQ_SENT
:
3487 case IB_CM_REP_RCVD
:
3488 case IB_CM_MRA_REP_SENT
:
3489 case IB_CM_REP_SENT
:
3490 case IB_CM_MRA_REP_RCVD
:
3491 case IB_CM_ESTABLISHED
:
3492 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3493 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3494 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3495 if (cm_id_priv
->responder_resources
)
3496 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3497 IB_ACCESS_REMOTE_ATOMIC
;
3498 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3499 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3506 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3510 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3511 struct ib_qp_attr
*qp_attr
,
3514 unsigned long flags
;
3517 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3518 switch (cm_id_priv
->id
.state
) {
3519 case IB_CM_REQ_RCVD
:
3520 case IB_CM_MRA_REQ_SENT
:
3521 case IB_CM_REP_RCVD
:
3522 case IB_CM_MRA_REP_SENT
:
3523 case IB_CM_REP_SENT
:
3524 case IB_CM_MRA_REP_RCVD
:
3525 case IB_CM_ESTABLISHED
:
3526 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3527 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3528 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3529 if (!cm_id_priv
->av
.valid
) {
3530 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3533 if (cm_id_priv
->av
.ah_attr
.vlan_id
!= 0xffff) {
3534 qp_attr
->vlan_id
= cm_id_priv
->av
.ah_attr
.vlan_id
;
3535 *qp_attr_mask
|= IB_QP_VID
;
3537 if (!is_zero_ether_addr(cm_id_priv
->av
.smac
)) {
3538 memcpy(qp_attr
->smac
, cm_id_priv
->av
.smac
,
3539 sizeof(qp_attr
->smac
));
3540 *qp_attr_mask
|= IB_QP_SMAC
;
3542 if (cm_id_priv
->alt_av
.valid
) {
3543 if (cm_id_priv
->alt_av
.ah_attr
.vlan_id
!= 0xffff) {
3544 qp_attr
->alt_vlan_id
=
3545 cm_id_priv
->alt_av
.ah_attr
.vlan_id
;
3546 *qp_attr_mask
|= IB_QP_ALT_VID
;
3548 if (!is_zero_ether_addr(cm_id_priv
->alt_av
.smac
)) {
3549 memcpy(qp_attr
->alt_smac
,
3550 cm_id_priv
->alt_av
.smac
,
3551 sizeof(qp_attr
->alt_smac
));
3552 *qp_attr_mask
|= IB_QP_ALT_SMAC
;
3555 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3556 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3557 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3558 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3559 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3560 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3561 IB_QP_MIN_RNR_TIMER
;
3562 qp_attr
->max_dest_rd_atomic
=
3563 cm_id_priv
->responder_resources
;
3564 qp_attr
->min_rnr_timer
= 0;
3566 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3567 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3568 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3569 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3570 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3571 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3579 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3583 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3584 struct ib_qp_attr
*qp_attr
,
3587 unsigned long flags
;
3590 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3591 switch (cm_id_priv
->id
.state
) {
3592 /* Allow transition to RTS before sending REP */
3593 case IB_CM_REQ_RCVD
:
3594 case IB_CM_MRA_REQ_SENT
:
3596 case IB_CM_REP_RCVD
:
3597 case IB_CM_MRA_REP_SENT
:
3598 case IB_CM_REP_SENT
:
3599 case IB_CM_MRA_REP_RCVD
:
3600 case IB_CM_ESTABLISHED
:
3601 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3602 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3603 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3604 switch (cm_id_priv
->qp_type
) {
3606 case IB_QPT_XRC_INI
:
3607 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3608 IB_QP_MAX_QP_RD_ATOMIC
;
3609 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3610 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3611 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3613 case IB_QPT_XRC_TGT
:
3614 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3615 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3620 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3621 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3622 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3625 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3626 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3627 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3628 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3629 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3630 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3638 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3642 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3643 struct ib_qp_attr
*qp_attr
,
3646 struct cm_id_private
*cm_id_priv
;
3649 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3650 switch (qp_attr
->qp_state
) {
3652 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3655 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3658 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3666 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3668 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3670 struct ib_device_attr attr
;
3672 if (ib_query_device(cm_dev
->ib_device
, &attr
))
3673 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3675 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3678 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3681 struct cm_counter_group
*group
;
3682 struct cm_counter_attribute
*cm_attr
;
3684 group
= container_of(obj
, struct cm_counter_group
, obj
);
3685 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3687 return sprintf(buf
, "%ld\n",
3688 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3691 static const struct sysfs_ops cm_counter_ops
= {
3692 .show
= cm_show_counter
3695 static struct kobj_type cm_counter_obj_type
= {
3696 .sysfs_ops
= &cm_counter_ops
,
3697 .default_attrs
= cm_counter_default_attrs
3700 static void cm_release_port_obj(struct kobject
*obj
)
3702 struct cm_port
*cm_port
;
3704 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3708 static struct kobj_type cm_port_obj_type
= {
3709 .release
= cm_release_port_obj
3712 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3716 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3719 struct class cm_class
= {
3720 .owner
= THIS_MODULE
,
3721 .name
= "infiniband_cm",
3722 .devnode
= cm_devnode
,
3724 EXPORT_SYMBOL(cm_class
);
3726 static int cm_create_port_fs(struct cm_port
*port
)
3730 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3731 &port
->cm_dev
->device
->kobj
,
3732 "%d", port
->port_num
);
3738 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3739 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3740 &cm_counter_obj_type
,
3742 "%s", counter_group_names
[i
]);
3751 kobject_put(&port
->counter_group
[i
].obj
);
3752 kobject_put(&port
->port_obj
);
3757 static void cm_remove_port_fs(struct cm_port
*port
)
3761 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3762 kobject_put(&port
->counter_group
[i
].obj
);
3764 kobject_put(&port
->port_obj
);
3767 static void cm_add_one(struct ib_device
*ib_device
)
3769 struct cm_device
*cm_dev
;
3770 struct cm_port
*port
;
3771 struct ib_mad_reg_req reg_req
= {
3772 .mgmt_class
= IB_MGMT_CLASS_CM
,
3773 .mgmt_class_version
= IB_CM_CLASS_VERSION
3775 struct ib_port_modify port_modify
= {
3776 .set_port_cap_mask
= IB_PORT_CM_SUP
3778 unsigned long flags
;
3782 if (rdma_node_get_transport(ib_device
->node_type
) != RDMA_TRANSPORT_IB
)
3785 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3786 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3790 cm_dev
->ib_device
= ib_device
;
3791 cm_get_ack_delay(cm_dev
);
3793 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3795 "%s", ib_device
->name
);
3796 if (IS_ERR(cm_dev
->device
)) {
3801 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3802 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3803 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3807 cm_dev
->port
[i
-1] = port
;
3808 port
->cm_dev
= cm_dev
;
3811 ret
= cm_create_port_fs(port
);
3815 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3822 if (IS_ERR(port
->mad_agent
))
3825 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3829 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3831 write_lock_irqsave(&cm
.device_lock
, flags
);
3832 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3833 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3837 ib_unregister_mad_agent(port
->mad_agent
);
3839 cm_remove_port_fs(port
);
3841 port_modify
.set_port_cap_mask
= 0;
3842 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3844 port
= cm_dev
->port
[i
-1];
3845 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3846 ib_unregister_mad_agent(port
->mad_agent
);
3847 cm_remove_port_fs(port
);
3849 device_unregister(cm_dev
->device
);
3853 static void cm_remove_one(struct ib_device
*ib_device
)
3855 struct cm_device
*cm_dev
;
3856 struct cm_port
*port
;
3857 struct ib_port_modify port_modify
= {
3858 .clr_port_cap_mask
= IB_PORT_CM_SUP
3860 unsigned long flags
;
3863 cm_dev
= ib_get_client_data(ib_device
, &cm_client
);
3867 write_lock_irqsave(&cm
.device_lock
, flags
);
3868 list_del(&cm_dev
->list
);
3869 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3871 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3872 port
= cm_dev
->port
[i
-1];
3873 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3874 ib_unregister_mad_agent(port
->mad_agent
);
3875 flush_workqueue(cm
.wq
);
3876 cm_remove_port_fs(port
);
3878 device_unregister(cm_dev
->device
);
3882 static int __init
ib_cm_init(void)
3886 memset(&cm
, 0, sizeof cm
);
3887 INIT_LIST_HEAD(&cm
.device_list
);
3888 rwlock_init(&cm
.device_lock
);
3889 spin_lock_init(&cm
.lock
);
3890 cm
.listen_service_table
= RB_ROOT
;
3891 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3892 cm
.remote_id_table
= RB_ROOT
;
3893 cm
.remote_qp_table
= RB_ROOT
;
3894 cm
.remote_sidr_table
= RB_ROOT
;
3895 idr_init(&cm
.local_id_table
);
3896 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3897 INIT_LIST_HEAD(&cm
.timewait_list
);
3899 ret
= class_register(&cm_class
);
3905 cm
.wq
= create_workqueue("ib_cm");
3911 ret
= ib_register_client(&cm_client
);
3917 destroy_workqueue(cm
.wq
);
3919 class_unregister(&cm_class
);
3921 idr_destroy(&cm
.local_id_table
);
3925 static void __exit
ib_cm_cleanup(void)
3927 struct cm_timewait_info
*timewait_info
, *tmp
;
3929 spin_lock_irq(&cm
.lock
);
3930 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3931 cancel_delayed_work(&timewait_info
->work
.work
);
3932 spin_unlock_irq(&cm
.lock
);
3934 ib_unregister_client(&cm_client
);
3935 destroy_workqueue(cm
.wq
);
3937 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3938 list_del(&timewait_info
->list
);
3939 kfree(timewait_info
);
3942 class_unregister(&cm_class
);
3943 idr_destroy(&cm
.local_id_table
);
3946 module_init(ib_cm_init
);
3947 module_exit(ib_cm_cleanup
);