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
, void *client_data
);
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
;
173 struct cm_port
*port
[0];
177 struct cm_port
*port
;
179 struct ib_ah_attr ah_attr
;
185 struct delayed_work work
;
186 struct list_head list
;
187 struct cm_port
*port
;
188 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
189 __be32 local_id
; /* Established / timewait */
191 struct ib_cm_event cm_event
;
192 struct ib_sa_path_rec path
[0];
195 struct cm_timewait_info
{
196 struct cm_work work
; /* Must be first. */
197 struct list_head list
;
198 struct rb_node remote_qp_node
;
199 struct rb_node remote_id_node
;
200 __be64 remote_ca_guid
;
202 u8 inserted_remote_qp
;
203 u8 inserted_remote_id
;
206 struct cm_id_private
{
209 struct rb_node service_node
;
210 struct rb_node sidr_id_node
;
211 spinlock_t lock
; /* Do not acquire inside cm.lock */
212 struct completion comp
;
214 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
215 * Protected by the cm.lock spinlock. */
216 int listen_sharecount
;
218 struct ib_mad_send_buf
*msg
;
219 struct cm_timewait_info
*timewait_info
;
220 /* todo: use alternate port on send failure */
228 enum ib_qp_type qp_type
;
232 enum ib_mtu path_mtu
;
237 u8 responder_resources
;
244 struct list_head work_list
;
248 static void cm_work_handler(struct work_struct
*work
);
250 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
252 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
253 complete(&cm_id_priv
->comp
);
256 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
257 struct ib_mad_send_buf
**msg
)
259 struct ib_mad_agent
*mad_agent
;
260 struct ib_mad_send_buf
*m
;
263 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
264 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
268 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
269 cm_id_priv
->av
.pkey_index
,
270 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
272 IB_MGMT_BASE_VERSION
);
278 /* Timeout set by caller if response is expected. */
280 m
->retries
= cm_id_priv
->max_cm_retries
;
282 atomic_inc(&cm_id_priv
->refcount
);
283 m
->context
[0] = cm_id_priv
;
288 static int cm_alloc_response_msg(struct cm_port
*port
,
289 struct ib_mad_recv_wc
*mad_recv_wc
,
290 struct ib_mad_send_buf
**msg
)
292 struct ib_mad_send_buf
*m
;
295 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
296 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
300 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
301 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
303 IB_MGMT_BASE_VERSION
);
313 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
315 ib_destroy_ah(msg
->ah
);
317 cm_deref_id(msg
->context
[0]);
318 ib_free_send_mad(msg
);
321 static void * cm_copy_private_data(const void *private_data
,
326 if (!private_data
|| !private_data_len
)
329 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
331 return ERR_PTR(-ENOMEM
);
336 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
337 void *private_data
, u8 private_data_len
)
339 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
340 kfree(cm_id_priv
->private_data
);
342 cm_id_priv
->private_data
= private_data
;
343 cm_id_priv
->private_data_len
= private_data_len
;
346 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
347 struct ib_grh
*grh
, struct cm_av
*av
)
350 av
->pkey_index
= wc
->pkey_index
;
351 ib_init_ah_from_wc(port
->cm_dev
->ib_device
, port
->port_num
, wc
,
355 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
357 struct cm_device
*cm_dev
;
358 struct cm_port
*port
= NULL
;
362 struct net_device
*ndev
= ib_get_ndev_from_path(path
);
364 read_lock_irqsave(&cm
.device_lock
, flags
);
365 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
366 if (!ib_find_cached_gid(cm_dev
->ib_device
, &path
->sgid
,
367 path
->gid_type
, ndev
, &p
, NULL
)) {
368 port
= cm_dev
->port
[p
-1];
372 read_unlock_irqrestore(&cm
.device_lock
, flags
);
380 ret
= ib_find_cached_pkey(cm_dev
->ib_device
, port
->port_num
,
381 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
386 ib_init_ah_from_path(cm_dev
->ib_device
, port
->port_num
, path
,
388 av
->timeout
= path
->packet_life_time
+ 1;
393 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
398 idr_preload(GFP_KERNEL
);
399 spin_lock_irqsave(&cm
.lock
, flags
);
401 id
= idr_alloc_cyclic(&cm
.local_id_table
, cm_id_priv
, 0, 0, GFP_NOWAIT
);
403 spin_unlock_irqrestore(&cm
.lock
, flags
);
406 cm_id_priv
->id
.local_id
= (__force __be32
)id
^ cm
.random_id_operand
;
407 return id
< 0 ? id
: 0;
410 static void cm_free_id(__be32 local_id
)
412 spin_lock_irq(&cm
.lock
);
413 idr_remove(&cm
.local_id_table
,
414 (__force
int) (local_id
^ cm
.random_id_operand
));
415 spin_unlock_irq(&cm
.lock
);
418 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
420 struct cm_id_private
*cm_id_priv
;
422 cm_id_priv
= idr_find(&cm
.local_id_table
,
423 (__force
int) (local_id
^ cm
.random_id_operand
));
425 if (cm_id_priv
->id
.remote_id
== remote_id
)
426 atomic_inc(&cm_id_priv
->refcount
);
434 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
436 struct cm_id_private
*cm_id_priv
;
438 spin_lock_irq(&cm
.lock
);
439 cm_id_priv
= cm_get_id(local_id
, remote_id
);
440 spin_unlock_irq(&cm
.lock
);
446 * Trivial helpers to strip endian annotation and compare; the
447 * endianness doesn't actually matter since we just need a stable
448 * order for the RB tree.
450 static int be32_lt(__be32 a
, __be32 b
)
452 return (__force u32
) a
< (__force u32
) b
;
455 static int be32_gt(__be32 a
, __be32 b
)
457 return (__force u32
) a
> (__force u32
) b
;
460 static int be64_lt(__be64 a
, __be64 b
)
462 return (__force u64
) a
< (__force u64
) b
;
465 static int be64_gt(__be64 a
, __be64 b
)
467 return (__force u64
) a
> (__force u64
) b
;
470 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
472 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
473 struct rb_node
*parent
= NULL
;
474 struct cm_id_private
*cur_cm_id_priv
;
475 __be64 service_id
= cm_id_priv
->id
.service_id
;
476 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
480 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
482 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
483 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
484 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
))
485 return cur_cm_id_priv
;
487 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
488 link
= &(*link
)->rb_left
;
489 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
490 link
= &(*link
)->rb_right
;
491 else if (be64_lt(service_id
, cur_cm_id_priv
->id
.service_id
))
492 link
= &(*link
)->rb_left
;
493 else if (be64_gt(service_id
, cur_cm_id_priv
->id
.service_id
))
494 link
= &(*link
)->rb_right
;
496 link
= &(*link
)->rb_right
;
498 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
499 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
503 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
506 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
507 struct cm_id_private
*cm_id_priv
;
510 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
511 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
512 cm_id_priv
->id
.service_id
&&
513 (cm_id_priv
->id
.device
== device
))
516 if (device
< cm_id_priv
->id
.device
)
517 node
= node
->rb_left
;
518 else if (device
> cm_id_priv
->id
.device
)
519 node
= node
->rb_right
;
520 else if (be64_lt(service_id
, cm_id_priv
->id
.service_id
))
521 node
= node
->rb_left
;
522 else if (be64_gt(service_id
, cm_id_priv
->id
.service_id
))
523 node
= node
->rb_right
;
525 node
= node
->rb_right
;
530 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
533 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
534 struct rb_node
*parent
= NULL
;
535 struct cm_timewait_info
*cur_timewait_info
;
536 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
537 __be32 remote_id
= timewait_info
->work
.remote_id
;
541 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
543 if (be32_lt(remote_id
, cur_timewait_info
->work
.remote_id
))
544 link
= &(*link
)->rb_left
;
545 else if (be32_gt(remote_id
, cur_timewait_info
->work
.remote_id
))
546 link
= &(*link
)->rb_right
;
547 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
548 link
= &(*link
)->rb_left
;
549 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
550 link
= &(*link
)->rb_right
;
552 return cur_timewait_info
;
554 timewait_info
->inserted_remote_id
= 1;
555 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
556 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
560 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
563 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
564 struct cm_timewait_info
*timewait_info
;
567 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
569 if (be32_lt(remote_id
, timewait_info
->work
.remote_id
))
570 node
= node
->rb_left
;
571 else if (be32_gt(remote_id
, timewait_info
->work
.remote_id
))
572 node
= node
->rb_right
;
573 else if (be64_lt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
574 node
= node
->rb_left
;
575 else if (be64_gt(remote_ca_guid
, timewait_info
->remote_ca_guid
))
576 node
= node
->rb_right
;
578 return timewait_info
;
583 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
586 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
587 struct rb_node
*parent
= NULL
;
588 struct cm_timewait_info
*cur_timewait_info
;
589 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
590 __be32 remote_qpn
= timewait_info
->remote_qpn
;
594 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
596 if (be32_lt(remote_qpn
, cur_timewait_info
->remote_qpn
))
597 link
= &(*link
)->rb_left
;
598 else if (be32_gt(remote_qpn
, cur_timewait_info
->remote_qpn
))
599 link
= &(*link
)->rb_right
;
600 else if (be64_lt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
601 link
= &(*link
)->rb_left
;
602 else if (be64_gt(remote_ca_guid
, cur_timewait_info
->remote_ca_guid
))
603 link
= &(*link
)->rb_right
;
605 return cur_timewait_info
;
607 timewait_info
->inserted_remote_qp
= 1;
608 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
609 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
613 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
616 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
617 struct rb_node
*parent
= NULL
;
618 struct cm_id_private
*cur_cm_id_priv
;
619 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
620 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
624 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
626 if (be32_lt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
627 link
= &(*link
)->rb_left
;
628 else if (be32_gt(remote_id
, cur_cm_id_priv
->id
.remote_id
))
629 link
= &(*link
)->rb_right
;
632 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
635 link
= &(*link
)->rb_left
;
637 link
= &(*link
)->rb_right
;
639 return cur_cm_id_priv
;
642 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
643 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
647 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
648 enum ib_cm_sidr_status status
)
650 struct ib_cm_sidr_rep_param param
;
652 memset(¶m
, 0, sizeof param
);
653 param
.status
= status
;
654 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
657 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
658 ib_cm_handler cm_handler
,
661 struct cm_id_private
*cm_id_priv
;
664 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
666 return ERR_PTR(-ENOMEM
);
668 cm_id_priv
->id
.state
= IB_CM_IDLE
;
669 cm_id_priv
->id
.device
= device
;
670 cm_id_priv
->id
.cm_handler
= cm_handler
;
671 cm_id_priv
->id
.context
= context
;
672 cm_id_priv
->id
.remote_cm_qpn
= 1;
673 ret
= cm_alloc_id(cm_id_priv
);
677 spin_lock_init(&cm_id_priv
->lock
);
678 init_completion(&cm_id_priv
->comp
);
679 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
680 atomic_set(&cm_id_priv
->work_count
, -1);
681 atomic_set(&cm_id_priv
->refcount
, 1);
682 return &cm_id_priv
->id
;
686 return ERR_PTR(-ENOMEM
);
688 EXPORT_SYMBOL(ib_create_cm_id
);
690 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
692 struct cm_work
*work
;
694 if (list_empty(&cm_id_priv
->work_list
))
697 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
698 list_del(&work
->list
);
702 static void cm_free_work(struct cm_work
*work
)
704 if (work
->mad_recv_wc
)
705 ib_free_recv_mad(work
->mad_recv_wc
);
709 static inline int cm_convert_to_ms(int iba_time
)
711 /* approximate conversion to ms from 4.096us x 2^iba_time */
712 return 1 << max(iba_time
- 8, 0);
716 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
717 * Because of how ack_timeout is stored, adding one doubles the timeout.
718 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
719 * increment it (round up) only if the other is within 50%.
721 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
723 int ack_timeout
= packet_life_time
+ 1;
725 if (ack_timeout
>= ca_ack_delay
)
726 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
728 ack_timeout
= ca_ack_delay
+
729 (ack_timeout
>= (ca_ack_delay
- 1));
731 return min(31, ack_timeout
);
734 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
736 if (timewait_info
->inserted_remote_id
) {
737 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
738 timewait_info
->inserted_remote_id
= 0;
741 if (timewait_info
->inserted_remote_qp
) {
742 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
743 timewait_info
->inserted_remote_qp
= 0;
747 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
749 struct cm_timewait_info
*timewait_info
;
751 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
753 return ERR_PTR(-ENOMEM
);
755 timewait_info
->work
.local_id
= local_id
;
756 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
757 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
758 return timewait_info
;
761 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
765 struct cm_device
*cm_dev
;
767 cm_dev
= ib_get_client_data(cm_id_priv
->id
.device
, &cm_client
);
771 spin_lock_irqsave(&cm
.lock
, flags
);
772 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
773 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
774 spin_unlock_irqrestore(&cm
.lock
, flags
);
777 * The cm_id could be destroyed by the user before we exit timewait.
778 * To protect against this, we search for the cm_id after exiting
779 * timewait before notifying the user that we've exited timewait.
781 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
782 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
784 /* Check if the device started its remove_one */
785 spin_lock_irqsave(&cm
.lock
, flags
);
786 if (!cm_dev
->going_down
)
787 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
788 msecs_to_jiffies(wait_time
));
789 spin_unlock_irqrestore(&cm
.lock
, flags
);
791 cm_id_priv
->timewait_info
= NULL
;
794 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
798 cm_id_priv
->id
.state
= IB_CM_IDLE
;
799 if (cm_id_priv
->timewait_info
) {
800 spin_lock_irqsave(&cm
.lock
, flags
);
801 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
802 spin_unlock_irqrestore(&cm
.lock
, flags
);
803 kfree(cm_id_priv
->timewait_info
);
804 cm_id_priv
->timewait_info
= NULL
;
808 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
810 struct cm_id_private
*cm_id_priv
;
811 struct cm_work
*work
;
813 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
815 spin_lock_irq(&cm_id_priv
->lock
);
816 switch (cm_id
->state
) {
818 spin_unlock_irq(&cm_id_priv
->lock
);
820 spin_lock_irq(&cm
.lock
);
821 if (--cm_id_priv
->listen_sharecount
> 0) {
822 /* The id is still shared. */
823 cm_deref_id(cm_id_priv
);
824 spin_unlock_irq(&cm
.lock
);
827 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
828 spin_unlock_irq(&cm
.lock
);
830 case IB_CM_SIDR_REQ_SENT
:
831 cm_id
->state
= IB_CM_IDLE
;
832 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
833 spin_unlock_irq(&cm_id_priv
->lock
);
835 case IB_CM_SIDR_REQ_RCVD
:
836 spin_unlock_irq(&cm_id_priv
->lock
);
837 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
838 spin_lock_irq(&cm
.lock
);
839 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
))
840 rb_erase(&cm_id_priv
->sidr_id_node
,
841 &cm
.remote_sidr_table
);
842 spin_unlock_irq(&cm
.lock
);
845 case IB_CM_MRA_REQ_RCVD
:
846 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
847 spin_unlock_irq(&cm_id_priv
->lock
);
848 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
849 &cm_id_priv
->id
.device
->node_guid
,
850 sizeof cm_id_priv
->id
.device
->node_guid
,
854 if (err
== -ENOMEM
) {
855 /* Do not reject to allow future retries. */
856 cm_reset_to_idle(cm_id_priv
);
857 spin_unlock_irq(&cm_id_priv
->lock
);
859 spin_unlock_irq(&cm_id_priv
->lock
);
860 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
865 case IB_CM_MRA_REP_RCVD
:
866 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
868 case IB_CM_MRA_REQ_SENT
:
870 case IB_CM_MRA_REP_SENT
:
871 spin_unlock_irq(&cm_id_priv
->lock
);
872 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
875 case IB_CM_ESTABLISHED
:
876 spin_unlock_irq(&cm_id_priv
->lock
);
877 if (cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
)
879 ib_send_cm_dreq(cm_id
, NULL
, 0);
881 case IB_CM_DREQ_SENT
:
882 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
883 cm_enter_timewait(cm_id_priv
);
884 spin_unlock_irq(&cm_id_priv
->lock
);
886 case IB_CM_DREQ_RCVD
:
887 spin_unlock_irq(&cm_id_priv
->lock
);
888 ib_send_cm_drep(cm_id
, NULL
, 0);
891 spin_unlock_irq(&cm_id_priv
->lock
);
895 cm_free_id(cm_id
->local_id
);
896 cm_deref_id(cm_id_priv
);
897 wait_for_completion(&cm_id_priv
->comp
);
898 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
900 kfree(cm_id_priv
->private_data
);
904 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
906 cm_destroy_id(cm_id
, 0);
908 EXPORT_SYMBOL(ib_destroy_cm_id
);
911 * __ib_cm_listen - Initiates listening on the specified service ID for
912 * connection and service ID resolution requests.
913 * @cm_id: Connection identifier associated with the listen request.
914 * @service_id: Service identifier matched against incoming connection
915 * and service ID resolution requests. The service ID should be specified
916 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
917 * assign a service ID to the caller.
918 * @service_mask: Mask applied to service ID used to listen across a
919 * range of service IDs. If set to 0, the service ID is matched
920 * exactly. This parameter is ignored if %service_id is set to
921 * IB_CM_ASSIGN_SERVICE_ID.
923 static int __ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
,
926 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
929 service_mask
= service_mask
? service_mask
: ~cpu_to_be64(0);
930 service_id
&= service_mask
;
931 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
932 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
935 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
936 if (cm_id
->state
!= IB_CM_IDLE
)
939 cm_id
->state
= IB_CM_LISTEN
;
940 ++cm_id_priv
->listen_sharecount
;
942 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
943 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
944 cm_id
->service_mask
= ~cpu_to_be64(0);
946 cm_id
->service_id
= service_id
;
947 cm_id
->service_mask
= service_mask
;
949 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
951 if (cur_cm_id_priv
) {
952 cm_id
->state
= IB_CM_IDLE
;
953 --cm_id_priv
->listen_sharecount
;
959 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
)
964 spin_lock_irqsave(&cm
.lock
, flags
);
965 ret
= __ib_cm_listen(cm_id
, service_id
, service_mask
);
966 spin_unlock_irqrestore(&cm
.lock
, flags
);
970 EXPORT_SYMBOL(ib_cm_listen
);
973 * Create a new listening ib_cm_id and listen on the given service ID.
975 * If there's an existing ID listening on that same device and service ID,
978 * @device: Device associated with the cm_id. All related communication will
979 * be associated with the specified device.
980 * @cm_handler: Callback invoked to notify the user of CM events.
981 * @service_id: Service identifier matched against incoming connection
982 * and service ID resolution requests. The service ID should be specified
983 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
984 * assign a service ID to the caller.
986 * Callers should call ib_destroy_cm_id when done with the listener ID.
988 struct ib_cm_id
*ib_cm_insert_listen(struct ib_device
*device
,
989 ib_cm_handler cm_handler
,
992 struct cm_id_private
*cm_id_priv
;
993 struct ib_cm_id
*cm_id
;
997 /* Create an ID in advance, since the creation may sleep */
998 cm_id
= ib_create_cm_id(device
, cm_handler
, NULL
);
1002 spin_lock_irqsave(&cm
.lock
, flags
);
1004 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
)
1007 /* Find an existing ID */
1008 cm_id_priv
= cm_find_listen(device
, service_id
);
1010 if (cm_id
->cm_handler
!= cm_handler
|| cm_id
->context
) {
1011 /* Sharing an ib_cm_id with different handlers is not
1013 spin_unlock_irqrestore(&cm
.lock
, flags
);
1014 return ERR_PTR(-EINVAL
);
1016 atomic_inc(&cm_id_priv
->refcount
);
1017 ++cm_id_priv
->listen_sharecount
;
1018 spin_unlock_irqrestore(&cm
.lock
, flags
);
1020 ib_destroy_cm_id(cm_id
);
1021 cm_id
= &cm_id_priv
->id
;
1026 /* Use newly created ID */
1027 err
= __ib_cm_listen(cm_id
, service_id
, 0);
1029 spin_unlock_irqrestore(&cm
.lock
, flags
);
1032 ib_destroy_cm_id(cm_id
);
1033 return ERR_PTR(err
);
1037 EXPORT_SYMBOL(ib_cm_insert_listen
);
1039 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
1040 enum cm_msg_sequence msg_seq
)
1042 u64 hi_tid
, low_tid
;
1044 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
1045 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
1047 return cpu_to_be64(hi_tid
| low_tid
);
1050 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
1051 __be16 attr_id
, __be64 tid
)
1053 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
1054 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
1055 hdr
->class_version
= IB_CM_CLASS_VERSION
;
1056 hdr
->method
= IB_MGMT_METHOD_SEND
;
1057 hdr
->attr_id
= attr_id
;
1061 static void cm_format_req(struct cm_req_msg
*req_msg
,
1062 struct cm_id_private
*cm_id_priv
,
1063 struct ib_cm_req_param
*param
)
1065 struct ib_sa_path_rec
*pri_path
= param
->primary_path
;
1066 struct ib_sa_path_rec
*alt_path
= param
->alternate_path
;
1068 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
1069 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
1071 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1072 req_msg
->service_id
= param
->service_id
;
1073 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1074 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
1075 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
1076 cm_req_set_remote_resp_timeout(req_msg
,
1077 param
->remote_cm_response_timeout
);
1078 cm_req_set_qp_type(req_msg
, param
->qp_type
);
1079 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
1080 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
1081 cm_req_set_local_resp_timeout(req_msg
,
1082 param
->local_cm_response_timeout
);
1083 req_msg
->pkey
= param
->primary_path
->pkey
;
1084 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
1085 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
1087 if (param
->qp_type
!= IB_QPT_XRC_INI
) {
1088 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
1089 cm_req_set_retry_count(req_msg
, param
->retry_count
);
1090 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
1091 cm_req_set_srq(req_msg
, param
->srq
);
1094 if (pri_path
->hop_limit
<= 1) {
1095 req_msg
->primary_local_lid
= pri_path
->slid
;
1096 req_msg
->primary_remote_lid
= pri_path
->dlid
;
1098 /* Work-around until there's a way to obtain remote LID info */
1099 req_msg
->primary_local_lid
= IB_LID_PERMISSIVE
;
1100 req_msg
->primary_remote_lid
= IB_LID_PERMISSIVE
;
1102 req_msg
->primary_local_gid
= pri_path
->sgid
;
1103 req_msg
->primary_remote_gid
= pri_path
->dgid
;
1104 cm_req_set_primary_flow_label(req_msg
, pri_path
->flow_label
);
1105 cm_req_set_primary_packet_rate(req_msg
, pri_path
->rate
);
1106 req_msg
->primary_traffic_class
= pri_path
->traffic_class
;
1107 req_msg
->primary_hop_limit
= pri_path
->hop_limit
;
1108 cm_req_set_primary_sl(req_msg
, pri_path
->sl
);
1109 cm_req_set_primary_subnet_local(req_msg
, (pri_path
->hop_limit
<= 1));
1110 cm_req_set_primary_local_ack_timeout(req_msg
,
1111 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1112 pri_path
->packet_life_time
));
1115 if (alt_path
->hop_limit
<= 1) {
1116 req_msg
->alt_local_lid
= alt_path
->slid
;
1117 req_msg
->alt_remote_lid
= alt_path
->dlid
;
1119 req_msg
->alt_local_lid
= IB_LID_PERMISSIVE
;
1120 req_msg
->alt_remote_lid
= IB_LID_PERMISSIVE
;
1122 req_msg
->alt_local_gid
= alt_path
->sgid
;
1123 req_msg
->alt_remote_gid
= alt_path
->dgid
;
1124 cm_req_set_alt_flow_label(req_msg
,
1125 alt_path
->flow_label
);
1126 cm_req_set_alt_packet_rate(req_msg
, alt_path
->rate
);
1127 req_msg
->alt_traffic_class
= alt_path
->traffic_class
;
1128 req_msg
->alt_hop_limit
= alt_path
->hop_limit
;
1129 cm_req_set_alt_sl(req_msg
, alt_path
->sl
);
1130 cm_req_set_alt_subnet_local(req_msg
, (alt_path
->hop_limit
<= 1));
1131 cm_req_set_alt_local_ack_timeout(req_msg
,
1132 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
1133 alt_path
->packet_life_time
));
1136 if (param
->private_data
&& param
->private_data_len
)
1137 memcpy(req_msg
->private_data
, param
->private_data
,
1138 param
->private_data_len
);
1141 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
1143 /* peer-to-peer not supported */
1144 if (param
->peer_to_peer
)
1147 if (!param
->primary_path
)
1150 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
&&
1151 param
->qp_type
!= IB_QPT_XRC_INI
)
1154 if (param
->private_data
&&
1155 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
1158 if (param
->alternate_path
&&
1159 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
1160 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
1166 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
1167 struct ib_cm_req_param
*param
)
1169 struct cm_id_private
*cm_id_priv
;
1170 struct cm_req_msg
*req_msg
;
1171 unsigned long flags
;
1174 ret
= cm_validate_req_param(param
);
1178 /* Verify that we're not in timewait. */
1179 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1180 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1181 if (cm_id
->state
!= IB_CM_IDLE
) {
1182 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1186 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1188 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1190 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1191 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1195 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1198 if (param
->alternate_path
) {
1199 ret
= cm_init_av_by_path(param
->alternate_path
,
1200 &cm_id_priv
->alt_av
);
1204 cm_id
->service_id
= param
->service_id
;
1205 cm_id
->service_mask
= ~cpu_to_be64(0);
1206 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1207 param
->primary_path
->packet_life_time
) * 2 +
1209 param
->remote_cm_response_timeout
);
1210 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1211 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1212 cm_id_priv
->responder_resources
= param
->responder_resources
;
1213 cm_id_priv
->retry_count
= param
->retry_count
;
1214 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1215 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1216 cm_id_priv
->qp_type
= param
->qp_type
;
1218 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1222 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1223 cm_format_req(req_msg
, cm_id_priv
, param
);
1224 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1225 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1226 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1228 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1229 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1231 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1232 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1234 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1237 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1238 cm_id
->state
= IB_CM_REQ_SENT
;
1239 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1242 error2
: cm_free_msg(cm_id_priv
->msg
);
1243 error1
: kfree(cm_id_priv
->timewait_info
);
1246 EXPORT_SYMBOL(ib_send_cm_req
);
1248 static int cm_issue_rej(struct cm_port
*port
,
1249 struct ib_mad_recv_wc
*mad_recv_wc
,
1250 enum ib_cm_rej_reason reason
,
1251 enum cm_msg_response msg_rejected
,
1252 void *ari
, u8 ari_length
)
1254 struct ib_mad_send_buf
*msg
= NULL
;
1255 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1258 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1262 /* We just need common CM header information. Cast to any message. */
1263 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1264 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1266 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1267 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1268 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1269 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1270 rej_msg
->reason
= cpu_to_be16(reason
);
1272 if (ari
&& ari_length
) {
1273 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1274 memcpy(rej_msg
->ari
, ari
, ari_length
);
1277 ret
= ib_post_send_mad(msg
, NULL
);
1284 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1285 __be32 local_qpn
, __be32 remote_qpn
)
1287 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1288 ((local_ca_guid
== remote_ca_guid
) &&
1289 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1292 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1293 struct ib_sa_path_rec
*primary_path
,
1294 struct ib_sa_path_rec
*alt_path
)
1296 memset(primary_path
, 0, sizeof *primary_path
);
1297 primary_path
->dgid
= req_msg
->primary_local_gid
;
1298 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1299 primary_path
->dlid
= req_msg
->primary_local_lid
;
1300 primary_path
->slid
= req_msg
->primary_remote_lid
;
1301 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1302 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1303 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1304 primary_path
->reversible
= 1;
1305 primary_path
->pkey
= req_msg
->pkey
;
1306 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1307 primary_path
->mtu_selector
= IB_SA_EQ
;
1308 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1309 primary_path
->rate_selector
= IB_SA_EQ
;
1310 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1311 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1312 primary_path
->packet_life_time
=
1313 cm_req_get_primary_local_ack_timeout(req_msg
);
1314 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1315 primary_path
->service_id
= req_msg
->service_id
;
1317 if (req_msg
->alt_local_lid
) {
1318 memset(alt_path
, 0, sizeof *alt_path
);
1319 alt_path
->dgid
= req_msg
->alt_local_gid
;
1320 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1321 alt_path
->dlid
= req_msg
->alt_local_lid
;
1322 alt_path
->slid
= req_msg
->alt_remote_lid
;
1323 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1324 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1325 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1326 alt_path
->reversible
= 1;
1327 alt_path
->pkey
= req_msg
->pkey
;
1328 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1329 alt_path
->mtu_selector
= IB_SA_EQ
;
1330 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1331 alt_path
->rate_selector
= IB_SA_EQ
;
1332 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1333 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1334 alt_path
->packet_life_time
=
1335 cm_req_get_alt_local_ack_timeout(req_msg
);
1336 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1337 alt_path
->service_id
= req_msg
->service_id
;
1341 static u16
cm_get_bth_pkey(struct cm_work
*work
)
1343 struct ib_device
*ib_dev
= work
->port
->cm_dev
->ib_device
;
1344 u8 port_num
= work
->port
->port_num
;
1345 u16 pkey_index
= work
->mad_recv_wc
->wc
->pkey_index
;
1349 ret
= ib_get_cached_pkey(ib_dev
, port_num
, pkey_index
, &pkey
);
1351 dev_warn_ratelimited(&ib_dev
->dev
, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1352 port_num
, pkey_index
, ret
);
1359 static void cm_format_req_event(struct cm_work
*work
,
1360 struct cm_id_private
*cm_id_priv
,
1361 struct ib_cm_id
*listen_id
)
1363 struct cm_req_msg
*req_msg
;
1364 struct ib_cm_req_event_param
*param
;
1366 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1367 param
= &work
->cm_event
.param
.req_rcvd
;
1368 param
->listen_id
= listen_id
;
1369 param
->bth_pkey
= cm_get_bth_pkey(work
);
1370 param
->port
= cm_id_priv
->av
.port
->port_num
;
1371 param
->primary_path
= &work
->path
[0];
1372 if (req_msg
->alt_local_lid
)
1373 param
->alternate_path
= &work
->path
[1];
1375 param
->alternate_path
= NULL
;
1376 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1377 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1378 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1379 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1380 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1381 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1382 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1383 param
->local_cm_response_timeout
=
1384 cm_req_get_remote_resp_timeout(req_msg
);
1385 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1386 param
->remote_cm_response_timeout
=
1387 cm_req_get_local_resp_timeout(req_msg
);
1388 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1389 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1390 param
->srq
= cm_req_get_srq(req_msg
);
1391 work
->cm_event
.private_data
= &req_msg
->private_data
;
1394 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1395 struct cm_work
*work
)
1399 /* We will typically only have the current event to report. */
1400 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1403 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1404 spin_lock_irq(&cm_id_priv
->lock
);
1405 work
= cm_dequeue_work(cm_id_priv
);
1406 spin_unlock_irq(&cm_id_priv
->lock
);
1408 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1412 cm_deref_id(cm_id_priv
);
1414 cm_destroy_id(&cm_id_priv
->id
, ret
);
1417 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1418 struct cm_id_private
*cm_id_priv
,
1419 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1420 const void *private_data
, u8 private_data_len
)
1422 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1423 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1424 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1425 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1426 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1428 if (private_data
&& private_data_len
)
1429 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1432 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1433 struct cm_id_private
*cm_id_priv
,
1434 enum ib_cm_rej_reason reason
,
1437 const void *private_data
,
1438 u8 private_data_len
)
1440 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1441 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1443 switch(cm_id_priv
->id
.state
) {
1444 case IB_CM_REQ_RCVD
:
1445 rej_msg
->local_comm_id
= 0;
1446 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1448 case IB_CM_MRA_REQ_SENT
:
1449 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1450 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1452 case IB_CM_REP_RCVD
:
1453 case IB_CM_MRA_REP_SENT
:
1454 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1455 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1458 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1459 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1463 rej_msg
->reason
= cpu_to_be16(reason
);
1464 if (ari
&& ari_length
) {
1465 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1466 memcpy(rej_msg
->ari
, ari
, ari_length
);
1469 if (private_data
&& private_data_len
)
1470 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1473 static void cm_dup_req_handler(struct cm_work
*work
,
1474 struct cm_id_private
*cm_id_priv
)
1476 struct ib_mad_send_buf
*msg
= NULL
;
1479 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1480 counter
[CM_REQ_COUNTER
]);
1482 /* Quick state check to discard duplicate REQs. */
1483 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1486 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1490 spin_lock_irq(&cm_id_priv
->lock
);
1491 switch (cm_id_priv
->id
.state
) {
1492 case IB_CM_MRA_REQ_SENT
:
1493 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1494 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1495 cm_id_priv
->private_data
,
1496 cm_id_priv
->private_data_len
);
1498 case IB_CM_TIMEWAIT
:
1499 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1500 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1505 spin_unlock_irq(&cm_id_priv
->lock
);
1507 ret
= ib_post_send_mad(msg
, NULL
);
1512 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1513 free
: cm_free_msg(msg
);
1516 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1517 struct cm_id_private
*cm_id_priv
)
1519 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1520 struct cm_timewait_info
*timewait_info
;
1521 struct cm_req_msg
*req_msg
;
1522 struct ib_cm_id
*cm_id
;
1524 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1526 /* Check for possible duplicate REQ. */
1527 spin_lock_irq(&cm
.lock
);
1528 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1529 if (timewait_info
) {
1530 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1531 timewait_info
->work
.remote_id
);
1532 spin_unlock_irq(&cm
.lock
);
1533 if (cur_cm_id_priv
) {
1534 cm_dup_req_handler(work
, cur_cm_id_priv
);
1535 cm_deref_id(cur_cm_id_priv
);
1540 /* Check for stale connections. */
1541 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1542 if (timewait_info
) {
1543 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1544 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1545 timewait_info
->work
.remote_id
);
1547 spin_unlock_irq(&cm
.lock
);
1548 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1549 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1551 if (cur_cm_id_priv
) {
1552 cm_id
= &cur_cm_id_priv
->id
;
1553 ib_send_cm_dreq(cm_id
, NULL
, 0);
1554 cm_deref_id(cur_cm_id_priv
);
1559 /* Find matching listen request. */
1560 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1561 req_msg
->service_id
);
1562 if (!listen_cm_id_priv
) {
1563 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1564 spin_unlock_irq(&cm
.lock
);
1565 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1566 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1570 atomic_inc(&listen_cm_id_priv
->refcount
);
1571 atomic_inc(&cm_id_priv
->refcount
);
1572 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1573 atomic_inc(&cm_id_priv
->work_count
);
1574 spin_unlock_irq(&cm
.lock
);
1576 return listen_cm_id_priv
;
1580 * Work-around for inter-subnet connections. If the LIDs are permissive,
1581 * we need to override the LID/SL data in the REQ with the LID information
1582 * in the work completion.
1584 static void cm_process_routed_req(struct cm_req_msg
*req_msg
, struct ib_wc
*wc
)
1586 if (!cm_req_get_primary_subnet_local(req_msg
)) {
1587 if (req_msg
->primary_local_lid
== IB_LID_PERMISSIVE
) {
1588 req_msg
->primary_local_lid
= cpu_to_be16(wc
->slid
);
1589 cm_req_set_primary_sl(req_msg
, wc
->sl
);
1592 if (req_msg
->primary_remote_lid
== IB_LID_PERMISSIVE
)
1593 req_msg
->primary_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1596 if (!cm_req_get_alt_subnet_local(req_msg
)) {
1597 if (req_msg
->alt_local_lid
== IB_LID_PERMISSIVE
) {
1598 req_msg
->alt_local_lid
= cpu_to_be16(wc
->slid
);
1599 cm_req_set_alt_sl(req_msg
, wc
->sl
);
1602 if (req_msg
->alt_remote_lid
== IB_LID_PERMISSIVE
)
1603 req_msg
->alt_remote_lid
= cpu_to_be16(wc
->dlid_path_bits
);
1607 static int cm_req_handler(struct cm_work
*work
)
1609 struct ib_cm_id
*cm_id
;
1610 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1611 struct cm_req_msg
*req_msg
;
1613 struct ib_gid_attr gid_attr
;
1616 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1618 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
1620 return PTR_ERR(cm_id
);
1622 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1623 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1624 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1625 work
->mad_recv_wc
->recv_buf
.grh
,
1627 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1629 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1630 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1633 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1634 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1635 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1637 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1638 if (!listen_cm_id_priv
) {
1640 kfree(cm_id_priv
->timewait_info
);
1644 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1645 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1646 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1647 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
1649 cm_process_routed_req(req_msg
, work
->mad_recv_wc
->wc
);
1650 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1652 memcpy(work
->path
[0].dmac
, cm_id_priv
->av
.ah_attr
.dmac
, ETH_ALEN
);
1653 work
->path
[0].hop_limit
= cm_id_priv
->av
.ah_attr
.grh
.hop_limit
;
1654 ret
= ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1655 work
->port
->port_num
,
1656 cm_id_priv
->av
.ah_attr
.grh
.sgid_index
,
1659 if (gid_attr
.ndev
) {
1660 work
->path
[0].ifindex
= gid_attr
.ndev
->ifindex
;
1661 work
->path
[0].net
= dev_net(gid_attr
.ndev
);
1662 dev_put(gid_attr
.ndev
);
1664 work
->path
[0].gid_type
= gid_attr
.gid_type
;
1665 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1668 int err
= ib_get_cached_gid(work
->port
->cm_dev
->ib_device
,
1669 work
->port
->port_num
, 0,
1670 &work
->path
[0].sgid
,
1672 if (!err
&& gid_attr
.ndev
) {
1673 work
->path
[0].ifindex
= gid_attr
.ndev
->ifindex
;
1674 work
->path
[0].net
= dev_net(gid_attr
.ndev
);
1675 dev_put(gid_attr
.ndev
);
1677 work
->path
[0].gid_type
= gid_attr
.gid_type
;
1678 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1679 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1683 if (req_msg
->alt_local_lid
) {
1684 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1686 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1687 &work
->path
[0].sgid
,
1688 sizeof work
->path
[0].sgid
, NULL
, 0);
1692 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1693 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1694 cm_req_get_local_resp_timeout(req_msg
));
1695 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1696 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1697 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1698 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1699 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1700 cm_id_priv
->pkey
= req_msg
->pkey
;
1701 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1702 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1703 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1704 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1706 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1707 cm_process_work(cm_id_priv
, work
);
1708 cm_deref_id(listen_cm_id_priv
);
1712 atomic_dec(&cm_id_priv
->refcount
);
1713 cm_deref_id(listen_cm_id_priv
);
1715 ib_destroy_cm_id(cm_id
);
1719 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1720 struct cm_id_private
*cm_id_priv
,
1721 struct ib_cm_rep_param
*param
)
1723 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1724 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1725 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1726 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1727 rep_msg
->resp_resources
= param
->responder_resources
;
1728 cm_rep_set_target_ack_delay(rep_msg
,
1729 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1730 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1731 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1732 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1734 if (cm_id_priv
->qp_type
!= IB_QPT_XRC_TGT
) {
1735 rep_msg
->initiator_depth
= param
->initiator_depth
;
1736 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1737 cm_rep_set_srq(rep_msg
, param
->srq
);
1738 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1740 cm_rep_set_srq(rep_msg
, 1);
1741 cm_rep_set_local_eecn(rep_msg
, cpu_to_be32(param
->qp_num
));
1744 if (param
->private_data
&& param
->private_data_len
)
1745 memcpy(rep_msg
->private_data
, param
->private_data
,
1746 param
->private_data_len
);
1749 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1750 struct ib_cm_rep_param
*param
)
1752 struct cm_id_private
*cm_id_priv
;
1753 struct ib_mad_send_buf
*msg
;
1754 struct cm_rep_msg
*rep_msg
;
1755 unsigned long flags
;
1758 if (param
->private_data
&&
1759 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1762 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1763 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1764 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1765 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1770 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1774 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1775 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1776 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1777 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1779 ret
= ib_post_send_mad(msg
, NULL
);
1781 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1786 cm_id
->state
= IB_CM_REP_SENT
;
1787 cm_id_priv
->msg
= msg
;
1788 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1789 cm_id_priv
->responder_resources
= param
->responder_resources
;
1790 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1791 cm_id_priv
->local_qpn
= cpu_to_be32(param
->qp_num
& 0xFFFFFF);
1793 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1796 EXPORT_SYMBOL(ib_send_cm_rep
);
1798 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1799 struct cm_id_private
*cm_id_priv
,
1800 const void *private_data
,
1801 u8 private_data_len
)
1803 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1804 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1805 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1807 if (private_data
&& private_data_len
)
1808 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1811 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1812 const void *private_data
,
1813 u8 private_data_len
)
1815 struct cm_id_private
*cm_id_priv
;
1816 struct ib_mad_send_buf
*msg
;
1817 unsigned long flags
;
1821 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1824 data
= cm_copy_private_data(private_data
, private_data_len
);
1826 return PTR_ERR(data
);
1828 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1829 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1830 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1831 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1836 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1840 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1841 private_data
, private_data_len
);
1843 ret
= ib_post_send_mad(msg
, NULL
);
1845 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1851 cm_id
->state
= IB_CM_ESTABLISHED
;
1852 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1853 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1856 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1860 EXPORT_SYMBOL(ib_send_cm_rtu
);
1862 static void cm_format_rep_event(struct cm_work
*work
, enum ib_qp_type qp_type
)
1864 struct cm_rep_msg
*rep_msg
;
1865 struct ib_cm_rep_event_param
*param
;
1867 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1868 param
= &work
->cm_event
.param
.rep_rcvd
;
1869 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1870 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1871 param
->remote_qpn
= be32_to_cpu(cm_rep_get_qpn(rep_msg
, qp_type
));
1872 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1873 param
->responder_resources
= rep_msg
->initiator_depth
;
1874 param
->initiator_depth
= rep_msg
->resp_resources
;
1875 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1876 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1877 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1878 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1879 param
->srq
= cm_rep_get_srq(rep_msg
);
1880 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1883 static void cm_dup_rep_handler(struct cm_work
*work
)
1885 struct cm_id_private
*cm_id_priv
;
1886 struct cm_rep_msg
*rep_msg
;
1887 struct ib_mad_send_buf
*msg
= NULL
;
1890 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1891 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1892 rep_msg
->local_comm_id
);
1896 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
1897 counter
[CM_REP_COUNTER
]);
1898 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1902 spin_lock_irq(&cm_id_priv
->lock
);
1903 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1904 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1905 cm_id_priv
->private_data
,
1906 cm_id_priv
->private_data_len
);
1907 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1908 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1909 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1910 cm_id_priv
->private_data
,
1911 cm_id_priv
->private_data_len
);
1914 spin_unlock_irq(&cm_id_priv
->lock
);
1916 ret
= ib_post_send_mad(msg
, NULL
);
1921 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1922 free
: cm_free_msg(msg
);
1923 deref
: cm_deref_id(cm_id_priv
);
1926 static int cm_rep_handler(struct cm_work
*work
)
1928 struct cm_id_private
*cm_id_priv
;
1929 struct cm_rep_msg
*rep_msg
;
1931 struct cm_id_private
*cur_cm_id_priv
;
1932 struct ib_cm_id
*cm_id
;
1933 struct cm_timewait_info
*timewait_info
;
1935 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1936 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1938 cm_dup_rep_handler(work
);
1942 cm_format_rep_event(work
, cm_id_priv
->qp_type
);
1944 spin_lock_irq(&cm_id_priv
->lock
);
1945 switch (cm_id_priv
->id
.state
) {
1946 case IB_CM_REQ_SENT
:
1947 case IB_CM_MRA_REQ_RCVD
:
1950 spin_unlock_irq(&cm_id_priv
->lock
);
1955 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1956 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1957 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1959 spin_lock(&cm
.lock
);
1960 /* Check for duplicate REP. */
1961 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1962 spin_unlock(&cm
.lock
);
1963 spin_unlock_irq(&cm_id_priv
->lock
);
1967 /* Check for a stale connection. */
1968 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1969 if (timewait_info
) {
1970 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1971 &cm
.remote_id_table
);
1972 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1973 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1974 timewait_info
->work
.remote_id
);
1976 spin_unlock(&cm
.lock
);
1977 spin_unlock_irq(&cm_id_priv
->lock
);
1978 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1979 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1982 if (cur_cm_id_priv
) {
1983 cm_id
= &cur_cm_id_priv
->id
;
1984 ib_send_cm_dreq(cm_id
, NULL
, 0);
1985 cm_deref_id(cur_cm_id_priv
);
1990 spin_unlock(&cm
.lock
);
1992 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1993 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1994 cm_id_priv
->remote_qpn
= cm_rep_get_qpn(rep_msg
, cm_id_priv
->qp_type
);
1995 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1996 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1997 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1998 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1999 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
2000 cm_id_priv
->av
.timeout
=
2001 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2002 cm_id_priv
->av
.timeout
- 1);
2003 cm_id_priv
->alt_av
.timeout
=
2004 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2005 cm_id_priv
->alt_av
.timeout
- 1);
2007 /* todo: handle peer_to_peer */
2009 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2010 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2012 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2013 spin_unlock_irq(&cm_id_priv
->lock
);
2016 cm_process_work(cm_id_priv
, work
);
2018 cm_deref_id(cm_id_priv
);
2022 cm_deref_id(cm_id_priv
);
2026 static int cm_establish_handler(struct cm_work
*work
)
2028 struct cm_id_private
*cm_id_priv
;
2031 /* See comment in cm_establish about lookup. */
2032 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
2036 spin_lock_irq(&cm_id_priv
->lock
);
2037 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
2038 spin_unlock_irq(&cm_id_priv
->lock
);
2042 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2043 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2045 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2046 spin_unlock_irq(&cm_id_priv
->lock
);
2049 cm_process_work(cm_id_priv
, work
);
2051 cm_deref_id(cm_id_priv
);
2054 cm_deref_id(cm_id_priv
);
2058 static int cm_rtu_handler(struct cm_work
*work
)
2060 struct cm_id_private
*cm_id_priv
;
2061 struct cm_rtu_msg
*rtu_msg
;
2064 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2065 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
2066 rtu_msg
->local_comm_id
);
2070 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
2072 spin_lock_irq(&cm_id_priv
->lock
);
2073 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
2074 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
2075 spin_unlock_irq(&cm_id_priv
->lock
);
2076 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2077 counter
[CM_RTU_COUNTER
]);
2080 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
2082 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2083 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2085 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2086 spin_unlock_irq(&cm_id_priv
->lock
);
2089 cm_process_work(cm_id_priv
, work
);
2091 cm_deref_id(cm_id_priv
);
2094 cm_deref_id(cm_id_priv
);
2098 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
2099 struct cm_id_private
*cm_id_priv
,
2100 const void *private_data
,
2101 u8 private_data_len
)
2103 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
2104 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
2105 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2106 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2107 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
2109 if (private_data
&& private_data_len
)
2110 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
2113 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
2114 const void *private_data
,
2115 u8 private_data_len
)
2117 struct cm_id_private
*cm_id_priv
;
2118 struct ib_mad_send_buf
*msg
;
2119 unsigned long flags
;
2122 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
2125 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2126 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2127 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
2132 if (cm_id
->lap_state
== IB_CM_LAP_SENT
||
2133 cm_id
->lap_state
== IB_CM_MRA_LAP_RCVD
)
2134 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2136 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2138 cm_enter_timewait(cm_id_priv
);
2142 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
2143 private_data
, private_data_len
);
2144 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2145 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
2147 ret
= ib_post_send_mad(msg
, NULL
);
2149 cm_enter_timewait(cm_id_priv
);
2150 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2155 cm_id
->state
= IB_CM_DREQ_SENT
;
2156 cm_id_priv
->msg
= msg
;
2157 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2160 EXPORT_SYMBOL(ib_send_cm_dreq
);
2162 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
2163 struct cm_id_private
*cm_id_priv
,
2164 const void *private_data
,
2165 u8 private_data_len
)
2167 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
2168 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2169 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2171 if (private_data
&& private_data_len
)
2172 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
2175 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
2176 const void *private_data
,
2177 u8 private_data_len
)
2179 struct cm_id_private
*cm_id_priv
;
2180 struct ib_mad_send_buf
*msg
;
2181 unsigned long flags
;
2185 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
2188 data
= cm_copy_private_data(private_data
, private_data_len
);
2190 return PTR_ERR(data
);
2192 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2193 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2194 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
2195 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2200 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2201 cm_enter_timewait(cm_id_priv
);
2203 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2207 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2208 private_data
, private_data_len
);
2210 ret
= ib_post_send_mad(msg
, NULL
);
2212 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2217 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2220 EXPORT_SYMBOL(ib_send_cm_drep
);
2222 static int cm_issue_drep(struct cm_port
*port
,
2223 struct ib_mad_recv_wc
*mad_recv_wc
)
2225 struct ib_mad_send_buf
*msg
= NULL
;
2226 struct cm_dreq_msg
*dreq_msg
;
2227 struct cm_drep_msg
*drep_msg
;
2230 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
2234 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
2235 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
2237 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
2238 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
2239 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
2241 ret
= ib_post_send_mad(msg
, NULL
);
2248 static int cm_dreq_handler(struct cm_work
*work
)
2250 struct cm_id_private
*cm_id_priv
;
2251 struct cm_dreq_msg
*dreq_msg
;
2252 struct ib_mad_send_buf
*msg
= NULL
;
2255 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2256 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
2257 dreq_msg
->local_comm_id
);
2259 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2260 counter
[CM_DREQ_COUNTER
]);
2261 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
2265 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
2267 spin_lock_irq(&cm_id_priv
->lock
);
2268 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
2271 switch (cm_id_priv
->id
.state
) {
2272 case IB_CM_REP_SENT
:
2273 case IB_CM_DREQ_SENT
:
2274 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2276 case IB_CM_ESTABLISHED
:
2277 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
||
2278 cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2279 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2281 case IB_CM_MRA_REP_RCVD
:
2283 case IB_CM_TIMEWAIT
:
2284 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2285 counter
[CM_DREQ_COUNTER
]);
2286 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2289 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
2290 cm_id_priv
->private_data
,
2291 cm_id_priv
->private_data_len
);
2292 spin_unlock_irq(&cm_id_priv
->lock
);
2294 if (ib_post_send_mad(msg
, NULL
))
2297 case IB_CM_DREQ_RCVD
:
2298 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2299 counter
[CM_DREQ_COUNTER
]);
2304 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
2305 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
2306 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2308 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2309 spin_unlock_irq(&cm_id_priv
->lock
);
2312 cm_process_work(cm_id_priv
, work
);
2314 cm_deref_id(cm_id_priv
);
2317 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2318 deref
: cm_deref_id(cm_id_priv
);
2322 static int cm_drep_handler(struct cm_work
*work
)
2324 struct cm_id_private
*cm_id_priv
;
2325 struct cm_drep_msg
*drep_msg
;
2328 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2329 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2330 drep_msg
->local_comm_id
);
2334 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2336 spin_lock_irq(&cm_id_priv
->lock
);
2337 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2338 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2339 spin_unlock_irq(&cm_id_priv
->lock
);
2342 cm_enter_timewait(cm_id_priv
);
2344 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2345 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2347 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2348 spin_unlock_irq(&cm_id_priv
->lock
);
2351 cm_process_work(cm_id_priv
, work
);
2353 cm_deref_id(cm_id_priv
);
2356 cm_deref_id(cm_id_priv
);
2360 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2361 enum ib_cm_rej_reason reason
,
2364 const void *private_data
,
2365 u8 private_data_len
)
2367 struct cm_id_private
*cm_id_priv
;
2368 struct ib_mad_send_buf
*msg
;
2369 unsigned long flags
;
2372 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2373 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2376 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2378 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2379 switch (cm_id
->state
) {
2380 case IB_CM_REQ_SENT
:
2381 case IB_CM_MRA_REQ_RCVD
:
2382 case IB_CM_REQ_RCVD
:
2383 case IB_CM_MRA_REQ_SENT
:
2384 case IB_CM_REP_RCVD
:
2385 case IB_CM_MRA_REP_SENT
:
2386 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2388 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2389 cm_id_priv
, reason
, ari
, ari_length
,
2390 private_data
, private_data_len
);
2392 cm_reset_to_idle(cm_id_priv
);
2394 case IB_CM_REP_SENT
:
2395 case IB_CM_MRA_REP_RCVD
:
2396 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2398 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2399 cm_id_priv
, reason
, ari
, ari_length
,
2400 private_data
, private_data_len
);
2402 cm_enter_timewait(cm_id_priv
);
2412 ret
= ib_post_send_mad(msg
, NULL
);
2416 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2419 EXPORT_SYMBOL(ib_send_cm_rej
);
2421 static void cm_format_rej_event(struct cm_work
*work
)
2423 struct cm_rej_msg
*rej_msg
;
2424 struct ib_cm_rej_event_param
*param
;
2426 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2427 param
= &work
->cm_event
.param
.rej_rcvd
;
2428 param
->ari
= rej_msg
->ari
;
2429 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2430 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2431 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2434 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2436 struct cm_timewait_info
*timewait_info
;
2437 struct cm_id_private
*cm_id_priv
;
2440 remote_id
= rej_msg
->local_comm_id
;
2442 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2443 spin_lock_irq(&cm
.lock
);
2444 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2446 if (!timewait_info
) {
2447 spin_unlock_irq(&cm
.lock
);
2450 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2451 (timewait_info
->work
.local_id
^
2452 cm
.random_id_operand
));
2454 if (cm_id_priv
->id
.remote_id
== remote_id
)
2455 atomic_inc(&cm_id_priv
->refcount
);
2459 spin_unlock_irq(&cm
.lock
);
2460 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2461 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2463 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2468 static int cm_rej_handler(struct cm_work
*work
)
2470 struct cm_id_private
*cm_id_priv
;
2471 struct cm_rej_msg
*rej_msg
;
2474 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2475 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2479 cm_format_rej_event(work
);
2481 spin_lock_irq(&cm_id_priv
->lock
);
2482 switch (cm_id_priv
->id
.state
) {
2483 case IB_CM_REQ_SENT
:
2484 case IB_CM_MRA_REQ_RCVD
:
2485 case IB_CM_REP_SENT
:
2486 case IB_CM_MRA_REP_RCVD
:
2487 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2489 case IB_CM_REQ_RCVD
:
2490 case IB_CM_MRA_REQ_SENT
:
2491 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2492 cm_enter_timewait(cm_id_priv
);
2494 cm_reset_to_idle(cm_id_priv
);
2496 case IB_CM_DREQ_SENT
:
2497 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2499 case IB_CM_REP_RCVD
:
2500 case IB_CM_MRA_REP_SENT
:
2501 cm_enter_timewait(cm_id_priv
);
2503 case IB_CM_ESTABLISHED
:
2504 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
||
2505 cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
) {
2506 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_SENT
)
2507 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2509 cm_enter_timewait(cm_id_priv
);
2514 spin_unlock_irq(&cm_id_priv
->lock
);
2519 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2521 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2522 spin_unlock_irq(&cm_id_priv
->lock
);
2525 cm_process_work(cm_id_priv
, work
);
2527 cm_deref_id(cm_id_priv
);
2530 cm_deref_id(cm_id_priv
);
2534 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2536 const void *private_data
,
2537 u8 private_data_len
)
2539 struct cm_id_private
*cm_id_priv
;
2540 struct ib_mad_send_buf
*msg
;
2541 enum ib_cm_state cm_state
;
2542 enum ib_cm_lap_state lap_state
;
2543 enum cm_msg_response msg_response
;
2545 unsigned long flags
;
2548 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2551 data
= cm_copy_private_data(private_data
, private_data_len
);
2553 return PTR_ERR(data
);
2555 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2557 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2558 switch(cm_id_priv
->id
.state
) {
2559 case IB_CM_REQ_RCVD
:
2560 cm_state
= IB_CM_MRA_REQ_SENT
;
2561 lap_state
= cm_id
->lap_state
;
2562 msg_response
= CM_MSG_RESPONSE_REQ
;
2564 case IB_CM_REP_RCVD
:
2565 cm_state
= IB_CM_MRA_REP_SENT
;
2566 lap_state
= cm_id
->lap_state
;
2567 msg_response
= CM_MSG_RESPONSE_REP
;
2569 case IB_CM_ESTABLISHED
:
2570 if (cm_id
->lap_state
== IB_CM_LAP_RCVD
) {
2571 cm_state
= cm_id
->state
;
2572 lap_state
= IB_CM_MRA_LAP_SENT
;
2573 msg_response
= CM_MSG_RESPONSE_OTHER
;
2581 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2582 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2586 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2587 msg_response
, service_timeout
,
2588 private_data
, private_data_len
);
2589 ret
= ib_post_send_mad(msg
, NULL
);
2594 cm_id
->state
= cm_state
;
2595 cm_id
->lap_state
= lap_state
;
2596 cm_id_priv
->service_timeout
= service_timeout
;
2597 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2598 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2601 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2605 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2610 EXPORT_SYMBOL(ib_send_cm_mra
);
2612 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2614 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2615 case CM_MSG_RESPONSE_REQ
:
2616 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2617 case CM_MSG_RESPONSE_REP
:
2618 case CM_MSG_RESPONSE_OTHER
:
2619 return cm_acquire_id(mra_msg
->remote_comm_id
,
2620 mra_msg
->local_comm_id
);
2626 static int cm_mra_handler(struct cm_work
*work
)
2628 struct cm_id_private
*cm_id_priv
;
2629 struct cm_mra_msg
*mra_msg
;
2632 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2633 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2637 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2638 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2639 cm_mra_get_service_timeout(mra_msg
);
2640 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2641 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2643 spin_lock_irq(&cm_id_priv
->lock
);
2644 switch (cm_id_priv
->id
.state
) {
2645 case IB_CM_REQ_SENT
:
2646 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2647 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2648 cm_id_priv
->msg
, timeout
))
2650 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2652 case IB_CM_REP_SENT
:
2653 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2654 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2655 cm_id_priv
->msg
, timeout
))
2657 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2659 case IB_CM_ESTABLISHED
:
2660 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2661 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2662 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2663 cm_id_priv
->msg
, timeout
)) {
2664 if (cm_id_priv
->id
.lap_state
== IB_CM_MRA_LAP_RCVD
)
2665 atomic_long_inc(&work
->port
->
2666 counter_group
[CM_RECV_DUPLICATES
].
2667 counter
[CM_MRA_COUNTER
]);
2670 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2672 case IB_CM_MRA_REQ_RCVD
:
2673 case IB_CM_MRA_REP_RCVD
:
2674 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2675 counter
[CM_MRA_COUNTER
]);
2681 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2682 cm_id_priv
->id
.state
;
2683 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2685 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2686 spin_unlock_irq(&cm_id_priv
->lock
);
2689 cm_process_work(cm_id_priv
, work
);
2691 cm_deref_id(cm_id_priv
);
2694 spin_unlock_irq(&cm_id_priv
->lock
);
2695 cm_deref_id(cm_id_priv
);
2699 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2700 struct cm_id_private
*cm_id_priv
,
2701 struct ib_sa_path_rec
*alternate_path
,
2702 const void *private_data
,
2703 u8 private_data_len
)
2705 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2706 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2707 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2708 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2709 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2710 /* todo: need remote CM response timeout */
2711 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2712 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2713 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2714 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2715 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2716 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2717 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2718 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2719 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2720 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2721 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2722 cm_lap_set_local_ack_timeout(lap_msg
,
2723 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2724 alternate_path
->packet_life_time
));
2726 if (private_data
&& private_data_len
)
2727 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2730 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2731 struct ib_sa_path_rec
*alternate_path
,
2732 const void *private_data
,
2733 u8 private_data_len
)
2735 struct cm_id_private
*cm_id_priv
;
2736 struct ib_mad_send_buf
*msg
;
2737 unsigned long flags
;
2740 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2743 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2744 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2745 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2746 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2747 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2752 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2755 cm_id_priv
->alt_av
.timeout
=
2756 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2757 cm_id_priv
->alt_av
.timeout
- 1);
2759 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2763 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2764 alternate_path
, private_data
, private_data_len
);
2765 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2766 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2768 ret
= ib_post_send_mad(msg
, NULL
);
2770 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2775 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2776 cm_id_priv
->msg
= msg
;
2778 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2781 EXPORT_SYMBOL(ib_send_cm_lap
);
2783 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2784 struct ib_sa_path_rec
*path
,
2785 struct cm_lap_msg
*lap_msg
)
2787 memset(path
, 0, sizeof *path
);
2788 path
->dgid
= lap_msg
->alt_local_gid
;
2789 path
->sgid
= lap_msg
->alt_remote_gid
;
2790 path
->dlid
= lap_msg
->alt_local_lid
;
2791 path
->slid
= lap_msg
->alt_remote_lid
;
2792 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2793 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2794 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2795 path
->reversible
= 1;
2796 path
->pkey
= cm_id_priv
->pkey
;
2797 path
->sl
= cm_lap_get_sl(lap_msg
);
2798 path
->mtu_selector
= IB_SA_EQ
;
2799 path
->mtu
= cm_id_priv
->path_mtu
;
2800 path
->rate_selector
= IB_SA_EQ
;
2801 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2802 path
->packet_life_time_selector
= IB_SA_EQ
;
2803 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2804 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2807 static int cm_lap_handler(struct cm_work
*work
)
2809 struct cm_id_private
*cm_id_priv
;
2810 struct cm_lap_msg
*lap_msg
;
2811 struct ib_cm_lap_event_param
*param
;
2812 struct ib_mad_send_buf
*msg
= NULL
;
2815 /* todo: verify LAP request and send reject APR if invalid. */
2816 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2817 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2818 lap_msg
->local_comm_id
);
2822 param
= &work
->cm_event
.param
.lap_rcvd
;
2823 param
->alternate_path
= &work
->path
[0];
2824 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2825 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2827 spin_lock_irq(&cm_id_priv
->lock
);
2828 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2831 switch (cm_id_priv
->id
.lap_state
) {
2832 case IB_CM_LAP_UNINIT
:
2833 case IB_CM_LAP_IDLE
:
2835 case IB_CM_MRA_LAP_SENT
:
2836 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2837 counter
[CM_LAP_COUNTER
]);
2838 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2841 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2842 CM_MSG_RESPONSE_OTHER
,
2843 cm_id_priv
->service_timeout
,
2844 cm_id_priv
->private_data
,
2845 cm_id_priv
->private_data_len
);
2846 spin_unlock_irq(&cm_id_priv
->lock
);
2848 if (ib_post_send_mad(msg
, NULL
))
2851 case IB_CM_LAP_RCVD
:
2852 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
2853 counter
[CM_LAP_COUNTER
]);
2859 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2860 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2861 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2862 work
->mad_recv_wc
->recv_buf
.grh
,
2864 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2865 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2867 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2868 spin_unlock_irq(&cm_id_priv
->lock
);
2871 cm_process_work(cm_id_priv
, work
);
2873 cm_deref_id(cm_id_priv
);
2876 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2877 deref
: cm_deref_id(cm_id_priv
);
2881 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2882 struct cm_id_private
*cm_id_priv
,
2883 enum ib_cm_apr_status status
,
2886 const void *private_data
,
2887 u8 private_data_len
)
2889 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2890 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2891 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2892 apr_msg
->ap_status
= (u8
) status
;
2894 if (info
&& info_length
) {
2895 apr_msg
->info_length
= info_length
;
2896 memcpy(apr_msg
->info
, info
, info_length
);
2899 if (private_data
&& private_data_len
)
2900 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2903 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2904 enum ib_cm_apr_status status
,
2907 const void *private_data
,
2908 u8 private_data_len
)
2910 struct cm_id_private
*cm_id_priv
;
2911 struct ib_mad_send_buf
*msg
;
2912 unsigned long flags
;
2915 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2916 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2919 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2920 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2921 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2922 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2923 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2928 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2932 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2933 info
, info_length
, private_data
, private_data_len
);
2934 ret
= ib_post_send_mad(msg
, NULL
);
2936 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2941 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2942 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2945 EXPORT_SYMBOL(ib_send_cm_apr
);
2947 static int cm_apr_handler(struct cm_work
*work
)
2949 struct cm_id_private
*cm_id_priv
;
2950 struct cm_apr_msg
*apr_msg
;
2953 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2954 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2955 apr_msg
->local_comm_id
);
2957 return -EINVAL
; /* Unmatched reply. */
2959 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2960 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2961 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2962 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2964 spin_lock_irq(&cm_id_priv
->lock
);
2965 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2966 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2967 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2968 spin_unlock_irq(&cm_id_priv
->lock
);
2971 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2972 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2973 cm_id_priv
->msg
= NULL
;
2975 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2977 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2978 spin_unlock_irq(&cm_id_priv
->lock
);
2981 cm_process_work(cm_id_priv
, work
);
2983 cm_deref_id(cm_id_priv
);
2986 cm_deref_id(cm_id_priv
);
2990 static int cm_timewait_handler(struct cm_work
*work
)
2992 struct cm_timewait_info
*timewait_info
;
2993 struct cm_id_private
*cm_id_priv
;
2996 timewait_info
= (struct cm_timewait_info
*)work
;
2997 spin_lock_irq(&cm
.lock
);
2998 list_del(&timewait_info
->list
);
2999 spin_unlock_irq(&cm
.lock
);
3001 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
3002 timewait_info
->work
.remote_id
);
3006 spin_lock_irq(&cm_id_priv
->lock
);
3007 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
3008 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
3009 spin_unlock_irq(&cm_id_priv
->lock
);
3012 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3013 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
3015 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
3016 spin_unlock_irq(&cm_id_priv
->lock
);
3019 cm_process_work(cm_id_priv
, work
);
3021 cm_deref_id(cm_id_priv
);
3024 cm_deref_id(cm_id_priv
);
3028 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
3029 struct cm_id_private
*cm_id_priv
,
3030 struct ib_cm_sidr_req_param
*param
)
3032 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
3033 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
3034 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
3035 sidr_req_msg
->pkey
= param
->path
->pkey
;
3036 sidr_req_msg
->service_id
= param
->service_id
;
3038 if (param
->private_data
&& param
->private_data_len
)
3039 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
3040 param
->private_data_len
);
3043 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
3044 struct ib_cm_sidr_req_param
*param
)
3046 struct cm_id_private
*cm_id_priv
;
3047 struct ib_mad_send_buf
*msg
;
3048 unsigned long flags
;
3051 if (!param
->path
|| (param
->private_data
&&
3052 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
3055 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3056 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
3060 cm_id
->service_id
= param
->service_id
;
3061 cm_id
->service_mask
= ~cpu_to_be64(0);
3062 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
3063 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
3064 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3068 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
3070 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
3071 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
3073 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3074 if (cm_id
->state
== IB_CM_IDLE
)
3075 ret
= ib_post_send_mad(msg
, NULL
);
3080 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3084 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
3085 cm_id_priv
->msg
= msg
;
3086 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3090 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
3092 static void cm_format_sidr_req_event(struct cm_work
*work
,
3093 struct ib_cm_id
*listen_id
)
3095 struct cm_sidr_req_msg
*sidr_req_msg
;
3096 struct ib_cm_sidr_req_event_param
*param
;
3098 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3099 work
->mad_recv_wc
->recv_buf
.mad
;
3100 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
3101 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
3102 param
->listen_id
= listen_id
;
3103 param
->service_id
= sidr_req_msg
->service_id
;
3104 param
->bth_pkey
= cm_get_bth_pkey(work
);
3105 param
->port
= work
->port
->port_num
;
3106 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
3109 static int cm_sidr_req_handler(struct cm_work
*work
)
3111 struct ib_cm_id
*cm_id
;
3112 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
3113 struct cm_sidr_req_msg
*sidr_req_msg
;
3116 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->ib_device
, NULL
, NULL
);
3118 return PTR_ERR(cm_id
);
3119 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3121 /* Record SGID/SLID and request ID for lookup. */
3122 sidr_req_msg
= (struct cm_sidr_req_msg
*)
3123 work
->mad_recv_wc
->recv_buf
.mad
;
3124 wc
= work
->mad_recv_wc
->wc
;
3125 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
3126 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
3127 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
3128 work
->mad_recv_wc
->recv_buf
.grh
,
3130 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
3131 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
3132 atomic_inc(&cm_id_priv
->work_count
);
3134 spin_lock_irq(&cm
.lock
);
3135 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
3136 if (cur_cm_id_priv
) {
3137 spin_unlock_irq(&cm
.lock
);
3138 atomic_long_inc(&work
->port
->counter_group
[CM_RECV_DUPLICATES
].
3139 counter
[CM_SIDR_REQ_COUNTER
]);
3140 goto out
; /* Duplicate message. */
3142 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
3143 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
3144 sidr_req_msg
->service_id
);
3145 if (!cur_cm_id_priv
) {
3146 spin_unlock_irq(&cm
.lock
);
3147 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
3148 goto out
; /* No match. */
3150 atomic_inc(&cur_cm_id_priv
->refcount
);
3151 atomic_inc(&cm_id_priv
->refcount
);
3152 spin_unlock_irq(&cm
.lock
);
3154 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
3155 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
3156 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
3157 cm_id_priv
->id
.service_mask
= ~cpu_to_be64(0);
3159 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
3160 cm_process_work(cm_id_priv
, work
);
3161 cm_deref_id(cur_cm_id_priv
);
3164 ib_destroy_cm_id(&cm_id_priv
->id
);
3168 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
3169 struct cm_id_private
*cm_id_priv
,
3170 struct ib_cm_sidr_rep_param
*param
)
3172 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
3174 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
3175 sidr_rep_msg
->status
= param
->status
;
3176 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
3177 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
3178 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
3180 if (param
->info
&& param
->info_length
)
3181 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
3183 if (param
->private_data
&& param
->private_data_len
)
3184 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
3185 param
->private_data_len
);
3188 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
3189 struct ib_cm_sidr_rep_param
*param
)
3191 struct cm_id_private
*cm_id_priv
;
3192 struct ib_mad_send_buf
*msg
;
3193 unsigned long flags
;
3196 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
3197 (param
->private_data
&&
3198 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
3201 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3202 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3203 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
3208 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
3212 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
3214 ret
= ib_post_send_mad(msg
, NULL
);
3216 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3220 cm_id
->state
= IB_CM_IDLE
;
3221 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3223 spin_lock_irqsave(&cm
.lock
, flags
);
3224 if (!RB_EMPTY_NODE(&cm_id_priv
->sidr_id_node
)) {
3225 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
3226 RB_CLEAR_NODE(&cm_id_priv
->sidr_id_node
);
3228 spin_unlock_irqrestore(&cm
.lock
, flags
);
3231 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3234 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
3236 static void cm_format_sidr_rep_event(struct cm_work
*work
)
3238 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3239 struct ib_cm_sidr_rep_event_param
*param
;
3241 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3242 work
->mad_recv_wc
->recv_buf
.mad
;
3243 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
3244 param
->status
= sidr_rep_msg
->status
;
3245 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
3246 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
3247 param
->info
= &sidr_rep_msg
->info
;
3248 param
->info_len
= sidr_rep_msg
->info_length
;
3249 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
3252 static int cm_sidr_rep_handler(struct cm_work
*work
)
3254 struct cm_sidr_rep_msg
*sidr_rep_msg
;
3255 struct cm_id_private
*cm_id_priv
;
3257 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
3258 work
->mad_recv_wc
->recv_buf
.mad
;
3259 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
3261 return -EINVAL
; /* Unmatched reply. */
3263 spin_lock_irq(&cm_id_priv
->lock
);
3264 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
3265 spin_unlock_irq(&cm_id_priv
->lock
);
3268 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3269 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
3270 spin_unlock_irq(&cm_id_priv
->lock
);
3272 cm_format_sidr_rep_event(work
);
3273 cm_process_work(cm_id_priv
, work
);
3276 cm_deref_id(cm_id_priv
);
3280 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
3281 enum ib_wc_status wc_status
)
3283 struct cm_id_private
*cm_id_priv
;
3284 struct ib_cm_event cm_event
;
3285 enum ib_cm_state state
;
3288 memset(&cm_event
, 0, sizeof cm_event
);
3289 cm_id_priv
= msg
->context
[0];
3291 /* Discard old sends or ones without a response. */
3292 spin_lock_irq(&cm_id_priv
->lock
);
3293 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
3294 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
3298 case IB_CM_REQ_SENT
:
3299 case IB_CM_MRA_REQ_RCVD
:
3300 cm_reset_to_idle(cm_id_priv
);
3301 cm_event
.event
= IB_CM_REQ_ERROR
;
3303 case IB_CM_REP_SENT
:
3304 case IB_CM_MRA_REP_RCVD
:
3305 cm_reset_to_idle(cm_id_priv
);
3306 cm_event
.event
= IB_CM_REP_ERROR
;
3308 case IB_CM_DREQ_SENT
:
3309 cm_enter_timewait(cm_id_priv
);
3310 cm_event
.event
= IB_CM_DREQ_ERROR
;
3312 case IB_CM_SIDR_REQ_SENT
:
3313 cm_id_priv
->id
.state
= IB_CM_IDLE
;
3314 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
3319 spin_unlock_irq(&cm_id_priv
->lock
);
3320 cm_event
.param
.send_status
= wc_status
;
3322 /* No other events can occur on the cm_id at this point. */
3323 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
3326 ib_destroy_cm_id(&cm_id_priv
->id
);
3329 spin_unlock_irq(&cm_id_priv
->lock
);
3333 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
3334 struct ib_mad_send_wc
*mad_send_wc
)
3336 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
3337 struct cm_port
*port
;
3340 port
= mad_agent
->context
;
3341 attr_index
= be16_to_cpu(((struct ib_mad_hdr
*)
3342 msg
->mad
)->attr_id
) - CM_ATTR_ID_OFFSET
;
3345 * If the send was in response to a received message (context[0] is not
3346 * set to a cm_id), and is not a REJ, then it is a send that was
3349 if (!msg
->context
[0] && (attr_index
!= CM_REJ_COUNTER
))
3352 atomic_long_add(1 + msg
->retries
,
3353 &port
->counter_group
[CM_XMIT
].counter
[attr_index
]);
3355 atomic_long_add(msg
->retries
,
3356 &port
->counter_group
[CM_XMIT_RETRIES
].
3357 counter
[attr_index
]);
3359 switch (mad_send_wc
->status
) {
3361 case IB_WC_WR_FLUSH_ERR
:
3365 if (msg
->context
[0] && msg
->context
[1])
3366 cm_process_send_error(msg
, mad_send_wc
->status
);
3373 static void cm_work_handler(struct work_struct
*_work
)
3375 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3378 switch (work
->cm_event
.event
) {
3379 case IB_CM_REQ_RECEIVED
:
3380 ret
= cm_req_handler(work
);
3382 case IB_CM_MRA_RECEIVED
:
3383 ret
= cm_mra_handler(work
);
3385 case IB_CM_REJ_RECEIVED
:
3386 ret
= cm_rej_handler(work
);
3388 case IB_CM_REP_RECEIVED
:
3389 ret
= cm_rep_handler(work
);
3391 case IB_CM_RTU_RECEIVED
:
3392 ret
= cm_rtu_handler(work
);
3394 case IB_CM_USER_ESTABLISHED
:
3395 ret
= cm_establish_handler(work
);
3397 case IB_CM_DREQ_RECEIVED
:
3398 ret
= cm_dreq_handler(work
);
3400 case IB_CM_DREP_RECEIVED
:
3401 ret
= cm_drep_handler(work
);
3403 case IB_CM_SIDR_REQ_RECEIVED
:
3404 ret
= cm_sidr_req_handler(work
);
3406 case IB_CM_SIDR_REP_RECEIVED
:
3407 ret
= cm_sidr_rep_handler(work
);
3409 case IB_CM_LAP_RECEIVED
:
3410 ret
= cm_lap_handler(work
);
3412 case IB_CM_APR_RECEIVED
:
3413 ret
= cm_apr_handler(work
);
3415 case IB_CM_TIMEWAIT_EXIT
:
3416 ret
= cm_timewait_handler(work
);
3426 static int cm_establish(struct ib_cm_id
*cm_id
)
3428 struct cm_id_private
*cm_id_priv
;
3429 struct cm_work
*work
;
3430 unsigned long flags
;
3432 struct cm_device
*cm_dev
;
3434 cm_dev
= ib_get_client_data(cm_id
->device
, &cm_client
);
3438 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3442 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3443 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3444 switch (cm_id
->state
)
3446 case IB_CM_REP_SENT
:
3447 case IB_CM_MRA_REP_RCVD
:
3448 cm_id
->state
= IB_CM_ESTABLISHED
;
3450 case IB_CM_ESTABLISHED
:
3457 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3465 * The CM worker thread may try to destroy the cm_id before it
3466 * can execute this work item. To prevent potential deadlock,
3467 * we need to find the cm_id once we're in the context of the
3468 * worker thread, rather than holding a reference on it.
3470 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3471 work
->local_id
= cm_id
->local_id
;
3472 work
->remote_id
= cm_id
->remote_id
;
3473 work
->mad_recv_wc
= NULL
;
3474 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3476 /* Check if the device started its remove_one */
3477 spin_lock_irqsave(&cm
.lock
, flags
);
3478 if (!cm_dev
->going_down
) {
3479 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3484 spin_unlock_irqrestore(&cm
.lock
, flags
);
3490 static int cm_migrate(struct ib_cm_id
*cm_id
)
3492 struct cm_id_private
*cm_id_priv
;
3493 unsigned long flags
;
3496 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3497 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3498 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3499 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3500 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3501 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3502 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3505 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3510 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3515 case IB_EVENT_COMM_EST
:
3516 ret
= cm_establish(cm_id
);
3518 case IB_EVENT_PATH_MIG
:
3519 ret
= cm_migrate(cm_id
);
3526 EXPORT_SYMBOL(ib_cm_notify
);
3528 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3529 struct ib_mad_send_buf
*send_buf
,
3530 struct ib_mad_recv_wc
*mad_recv_wc
)
3532 struct cm_port
*port
= mad_agent
->context
;
3533 struct cm_work
*work
;
3534 enum ib_cm_event_type event
;
3539 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3540 case CM_REQ_ATTR_ID
:
3541 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3542 alt_local_lid
!= 0);
3543 event
= IB_CM_REQ_RECEIVED
;
3545 case CM_MRA_ATTR_ID
:
3546 event
= IB_CM_MRA_RECEIVED
;
3548 case CM_REJ_ATTR_ID
:
3549 event
= IB_CM_REJ_RECEIVED
;
3551 case CM_REP_ATTR_ID
:
3552 event
= IB_CM_REP_RECEIVED
;
3554 case CM_RTU_ATTR_ID
:
3555 event
= IB_CM_RTU_RECEIVED
;
3557 case CM_DREQ_ATTR_ID
:
3558 event
= IB_CM_DREQ_RECEIVED
;
3560 case CM_DREP_ATTR_ID
:
3561 event
= IB_CM_DREP_RECEIVED
;
3563 case CM_SIDR_REQ_ATTR_ID
:
3564 event
= IB_CM_SIDR_REQ_RECEIVED
;
3566 case CM_SIDR_REP_ATTR_ID
:
3567 event
= IB_CM_SIDR_REP_RECEIVED
;
3569 case CM_LAP_ATTR_ID
:
3571 event
= IB_CM_LAP_RECEIVED
;
3573 case CM_APR_ATTR_ID
:
3574 event
= IB_CM_APR_RECEIVED
;
3577 ib_free_recv_mad(mad_recv_wc
);
3581 attr_id
= be16_to_cpu(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
);
3582 atomic_long_inc(&port
->counter_group
[CM_RECV
].
3583 counter
[attr_id
- CM_ATTR_ID_OFFSET
]);
3585 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3588 ib_free_recv_mad(mad_recv_wc
);
3592 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3593 work
->cm_event
.event
= event
;
3594 work
->mad_recv_wc
= mad_recv_wc
;
3597 /* Check if the device started its remove_one */
3598 spin_lock_irq(&cm
.lock
);
3599 if (!port
->cm_dev
->going_down
)
3600 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3603 spin_unlock_irq(&cm
.lock
);
3607 ib_free_recv_mad(mad_recv_wc
);
3611 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3612 struct ib_qp_attr
*qp_attr
,
3615 unsigned long flags
;
3618 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3619 switch (cm_id_priv
->id
.state
) {
3620 case IB_CM_REQ_SENT
:
3621 case IB_CM_MRA_REQ_RCVD
:
3622 case IB_CM_REQ_RCVD
:
3623 case IB_CM_MRA_REQ_SENT
:
3624 case IB_CM_REP_RCVD
:
3625 case IB_CM_MRA_REP_SENT
:
3626 case IB_CM_REP_SENT
:
3627 case IB_CM_MRA_REP_RCVD
:
3628 case IB_CM_ESTABLISHED
:
3629 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3630 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3631 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3632 if (cm_id_priv
->responder_resources
)
3633 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3634 IB_ACCESS_REMOTE_ATOMIC
;
3635 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3636 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3643 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3647 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3648 struct ib_qp_attr
*qp_attr
,
3651 unsigned long flags
;
3654 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3655 switch (cm_id_priv
->id
.state
) {
3656 case IB_CM_REQ_RCVD
:
3657 case IB_CM_MRA_REQ_SENT
:
3658 case IB_CM_REP_RCVD
:
3659 case IB_CM_MRA_REP_SENT
:
3660 case IB_CM_REP_SENT
:
3661 case IB_CM_MRA_REP_RCVD
:
3662 case IB_CM_ESTABLISHED
:
3663 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3664 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3665 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3666 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3667 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3668 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3669 if (cm_id_priv
->qp_type
== IB_QPT_RC
||
3670 cm_id_priv
->qp_type
== IB_QPT_XRC_TGT
) {
3671 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3672 IB_QP_MIN_RNR_TIMER
;
3673 qp_attr
->max_dest_rd_atomic
=
3674 cm_id_priv
->responder_resources
;
3675 qp_attr
->min_rnr_timer
= 0;
3677 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3678 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3679 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3680 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3681 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3682 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3690 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3694 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3695 struct ib_qp_attr
*qp_attr
,
3698 unsigned long flags
;
3701 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3702 switch (cm_id_priv
->id
.state
) {
3703 /* Allow transition to RTS before sending REP */
3704 case IB_CM_REQ_RCVD
:
3705 case IB_CM_MRA_REQ_SENT
:
3707 case IB_CM_REP_RCVD
:
3708 case IB_CM_MRA_REP_SENT
:
3709 case IB_CM_REP_SENT
:
3710 case IB_CM_MRA_REP_RCVD
:
3711 case IB_CM_ESTABLISHED
:
3712 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3713 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3714 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3715 switch (cm_id_priv
->qp_type
) {
3717 case IB_QPT_XRC_INI
:
3718 *qp_attr_mask
|= IB_QP_RETRY_CNT
| IB_QP_RNR_RETRY
|
3719 IB_QP_MAX_QP_RD_ATOMIC
;
3720 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3721 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3722 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3724 case IB_QPT_XRC_TGT
:
3725 *qp_attr_mask
|= IB_QP_TIMEOUT
;
3726 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3731 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3732 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3733 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3736 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3737 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3738 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3739 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3740 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3741 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3749 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3753 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3754 struct ib_qp_attr
*qp_attr
,
3757 struct cm_id_private
*cm_id_priv
;
3760 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3761 switch (qp_attr
->qp_state
) {
3763 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3766 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3769 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3777 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3779 static ssize_t
cm_show_counter(struct kobject
*obj
, struct attribute
*attr
,
3782 struct cm_counter_group
*group
;
3783 struct cm_counter_attribute
*cm_attr
;
3785 group
= container_of(obj
, struct cm_counter_group
, obj
);
3786 cm_attr
= container_of(attr
, struct cm_counter_attribute
, attr
);
3788 return sprintf(buf
, "%ld\n",
3789 atomic_long_read(&group
->counter
[cm_attr
->index
]));
3792 static const struct sysfs_ops cm_counter_ops
= {
3793 .show
= cm_show_counter
3796 static struct kobj_type cm_counter_obj_type
= {
3797 .sysfs_ops
= &cm_counter_ops
,
3798 .default_attrs
= cm_counter_default_attrs
3801 static void cm_release_port_obj(struct kobject
*obj
)
3803 struct cm_port
*cm_port
;
3805 cm_port
= container_of(obj
, struct cm_port
, port_obj
);
3809 static struct kobj_type cm_port_obj_type
= {
3810 .release
= cm_release_port_obj
3813 static char *cm_devnode(struct device
*dev
, umode_t
*mode
)
3817 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
3820 struct class cm_class
= {
3821 .owner
= THIS_MODULE
,
3822 .name
= "infiniband_cm",
3823 .devnode
= cm_devnode
,
3825 EXPORT_SYMBOL(cm_class
);
3827 static int cm_create_port_fs(struct cm_port
*port
)
3831 ret
= kobject_init_and_add(&port
->port_obj
, &cm_port_obj_type
,
3832 &port
->cm_dev
->device
->kobj
,
3833 "%d", port
->port_num
);
3839 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++) {
3840 ret
= kobject_init_and_add(&port
->counter_group
[i
].obj
,
3841 &cm_counter_obj_type
,
3843 "%s", counter_group_names
[i
]);
3852 kobject_put(&port
->counter_group
[i
].obj
);
3853 kobject_put(&port
->port_obj
);
3858 static void cm_remove_port_fs(struct cm_port
*port
)
3862 for (i
= 0; i
< CM_COUNTER_GROUPS
; i
++)
3863 kobject_put(&port
->counter_group
[i
].obj
);
3865 kobject_put(&port
->port_obj
);
3868 static void cm_add_one(struct ib_device
*ib_device
)
3870 struct cm_device
*cm_dev
;
3871 struct cm_port
*port
;
3872 struct ib_mad_reg_req reg_req
= {
3873 .mgmt_class
= IB_MGMT_CLASS_CM
,
3874 .mgmt_class_version
= IB_CM_CLASS_VERSION
,
3876 struct ib_port_modify port_modify
= {
3877 .set_port_cap_mask
= IB_PORT_CM_SUP
3879 unsigned long flags
;
3884 cm_dev
= kzalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3885 ib_device
->phys_port_cnt
, GFP_KERNEL
);
3889 cm_dev
->ib_device
= ib_device
;
3890 cm_dev
->ack_delay
= ib_device
->attrs
.local_ca_ack_delay
;
3891 cm_dev
->going_down
= 0;
3892 cm_dev
->device
= device_create(&cm_class
, &ib_device
->dev
,
3894 "%s", ib_device
->name
);
3895 if (IS_ERR(cm_dev
->device
)) {
3900 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3901 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3902 if (!rdma_cap_ib_cm(ib_device
, i
))
3905 port
= kzalloc(sizeof *port
, GFP_KERNEL
);
3909 cm_dev
->port
[i
-1] = port
;
3910 port
->cm_dev
= cm_dev
;
3913 ret
= cm_create_port_fs(port
);
3917 port
->mad_agent
= ib_register_mad_agent(ib_device
, i
,
3925 if (IS_ERR(port
->mad_agent
))
3928 ret
= ib_modify_port(ib_device
, i
, 0, &port_modify
);
3938 ib_set_client_data(ib_device
, &cm_client
, cm_dev
);
3940 write_lock_irqsave(&cm
.device_lock
, flags
);
3941 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3942 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3946 ib_unregister_mad_agent(port
->mad_agent
);
3948 cm_remove_port_fs(port
);
3950 port_modify
.set_port_cap_mask
= 0;
3951 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3953 if (!rdma_cap_ib_cm(ib_device
, i
))
3956 port
= cm_dev
->port
[i
-1];
3957 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3958 ib_unregister_mad_agent(port
->mad_agent
);
3959 cm_remove_port_fs(port
);
3962 device_unregister(cm_dev
->device
);
3966 static void cm_remove_one(struct ib_device
*ib_device
, void *client_data
)
3968 struct cm_device
*cm_dev
= client_data
;
3969 struct cm_port
*port
;
3970 struct ib_port_modify port_modify
= {
3971 .clr_port_cap_mask
= IB_PORT_CM_SUP
3973 unsigned long flags
;
3979 write_lock_irqsave(&cm
.device_lock
, flags
);
3980 list_del(&cm_dev
->list
);
3981 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3983 spin_lock_irq(&cm
.lock
);
3984 cm_dev
->going_down
= 1;
3985 spin_unlock_irq(&cm
.lock
);
3987 for (i
= 1; i
<= ib_device
->phys_port_cnt
; i
++) {
3988 if (!rdma_cap_ib_cm(ib_device
, i
))
3991 port
= cm_dev
->port
[i
-1];
3992 ib_modify_port(ib_device
, port
->port_num
, 0, &port_modify
);
3994 * We flush the queue here after the going_down set, this
3995 * verify that no new works will be queued in the recv handler,
3996 * after that we can call the unregister_mad_agent
3998 flush_workqueue(cm
.wq
);
3999 ib_unregister_mad_agent(port
->mad_agent
);
4000 cm_remove_port_fs(port
);
4002 device_unregister(cm_dev
->device
);
4006 static int __init
ib_cm_init(void)
4010 memset(&cm
, 0, sizeof cm
);
4011 INIT_LIST_HEAD(&cm
.device_list
);
4012 rwlock_init(&cm
.device_lock
);
4013 spin_lock_init(&cm
.lock
);
4014 cm
.listen_service_table
= RB_ROOT
;
4015 cm
.listen_service_id
= be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
4016 cm
.remote_id_table
= RB_ROOT
;
4017 cm
.remote_qp_table
= RB_ROOT
;
4018 cm
.remote_sidr_table
= RB_ROOT
;
4019 idr_init(&cm
.local_id_table
);
4020 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
4021 INIT_LIST_HEAD(&cm
.timewait_list
);
4023 ret
= class_register(&cm_class
);
4029 cm
.wq
= create_workqueue("ib_cm");
4035 ret
= ib_register_client(&cm_client
);
4041 destroy_workqueue(cm
.wq
);
4043 class_unregister(&cm_class
);
4045 idr_destroy(&cm
.local_id_table
);
4049 static void __exit
ib_cm_cleanup(void)
4051 struct cm_timewait_info
*timewait_info
, *tmp
;
4053 spin_lock_irq(&cm
.lock
);
4054 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
4055 cancel_delayed_work(&timewait_info
->work
.work
);
4056 spin_unlock_irq(&cm
.lock
);
4058 ib_unregister_client(&cm_client
);
4059 destroy_workqueue(cm
.wq
);
4061 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
4062 list_del(&timewait_info
->list
);
4063 kfree(timewait_info
);
4066 class_unregister(&cm_class
);
4067 idr_destroy(&cm
.local_id_table
);
4070 module_init(ib_cm_init
);
4071 module_exit(ib_cm_cleanup
);