2 * Copyright (c) 2004-2006 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
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.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/workqueue.h>
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
56 static void cm_add_one(struct ib_device
*device
);
57 static void cm_remove_one(struct ib_device
*device
);
59 static struct ib_client cm_client
= {
62 .remove
= cm_remove_one
67 struct list_head device_list
;
69 struct rb_root listen_service_table
;
70 u64 listen_service_id
;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table
;
73 struct rb_root remote_id_table
;
74 struct rb_root remote_sidr_table
;
75 struct idr local_id_table
;
76 __be32 random_id_operand
;
77 struct list_head timewait_list
;
78 struct workqueue_struct
*wq
;
82 struct cm_device
*cm_dev
;
83 struct ib_mad_agent
*mad_agent
;
88 struct list_head list
;
89 struct ib_device
*device
;
91 struct cm_port port
[0];
97 struct ib_ah_attr ah_attr
;
103 struct delayed_work work
;
104 struct list_head list
;
105 struct cm_port
*port
;
106 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
107 __be32 local_id
; /* Established / timewait */
109 struct ib_cm_event cm_event
;
110 struct ib_sa_path_rec path
[0];
113 struct cm_timewait_info
{
114 struct cm_work work
; /* Must be first. */
115 struct list_head list
;
116 struct rb_node remote_qp_node
;
117 struct rb_node remote_id_node
;
118 __be64 remote_ca_guid
;
120 u8 inserted_remote_qp
;
121 u8 inserted_remote_id
;
124 struct cm_id_private
{
127 struct rb_node service_node
;
128 struct rb_node sidr_id_node
;
129 spinlock_t lock
; /* Do not acquire inside cm.lock */
130 struct completion comp
;
133 struct ib_mad_send_buf
*msg
;
134 struct cm_timewait_info
*timewait_info
;
135 /* todo: use alternate port on send failure */
138 struct ib_cm_compare_data
*compare_data
;
144 enum ib_qp_type qp_type
;
148 enum ib_mtu path_mtu
;
153 u8 responder_resources
;
160 struct list_head work_list
;
164 static void cm_work_handler(struct work_struct
*work
);
166 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
168 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
169 complete(&cm_id_priv
->comp
);
172 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
173 struct ib_mad_send_buf
**msg
)
175 struct ib_mad_agent
*mad_agent
;
176 struct ib_mad_send_buf
*m
;
179 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
180 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
184 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
185 cm_id_priv
->av
.pkey_index
,
186 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
193 /* Timeout set by caller if response is expected. */
195 m
->retries
= cm_id_priv
->max_cm_retries
;
197 atomic_inc(&cm_id_priv
->refcount
);
198 m
->context
[0] = cm_id_priv
;
203 static int cm_alloc_response_msg(struct cm_port
*port
,
204 struct ib_mad_recv_wc
*mad_recv_wc
,
205 struct ib_mad_send_buf
**msg
)
207 struct ib_mad_send_buf
*m
;
210 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
211 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
215 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
216 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
227 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
229 ib_destroy_ah(msg
->ah
);
231 cm_deref_id(msg
->context
[0]);
232 ib_free_send_mad(msg
);
235 static void * cm_copy_private_data(const void *private_data
,
240 if (!private_data
|| !private_data_len
)
243 data
= kmemdup(private_data
, private_data_len
, GFP_KERNEL
);
245 return ERR_PTR(-ENOMEM
);
250 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
251 void *private_data
, u8 private_data_len
)
253 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
254 kfree(cm_id_priv
->private_data
);
256 cm_id_priv
->private_data
= private_data
;
257 cm_id_priv
->private_data_len
= private_data_len
;
260 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
261 struct ib_grh
*grh
, struct cm_av
*av
)
264 av
->pkey_index
= wc
->pkey_index
;
265 ib_init_ah_from_wc(port
->cm_dev
->device
, port
->port_num
, wc
,
269 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
271 struct cm_device
*cm_dev
;
272 struct cm_port
*port
= NULL
;
277 read_lock_irqsave(&cm
.device_lock
, flags
);
278 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
279 if (!ib_find_cached_gid(cm_dev
->device
, &path
->sgid
,
281 port
= &cm_dev
->port
[p
-1];
285 read_unlock_irqrestore(&cm
.device_lock
, flags
);
290 ret
= ib_find_cached_pkey(cm_dev
->device
, port
->port_num
,
291 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
296 ib_init_ah_from_path(cm_dev
->device
, port
->port_num
, path
,
298 av
->timeout
= path
->packet_life_time
+ 1;
302 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
309 spin_lock_irqsave(&cm
.lock
, flags
);
310 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
,
313 next_id
= ((unsigned) id
+ 1) & MAX_ID_MASK
;
314 spin_unlock_irqrestore(&cm
.lock
, flags
);
315 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
317 cm_id_priv
->id
.local_id
= (__force __be32
) (id
^ cm
.random_id_operand
);
321 static void cm_free_id(__be32 local_id
)
323 spin_lock_irq(&cm
.lock
);
324 idr_remove(&cm
.local_id_table
,
325 (__force
int) (local_id
^ cm
.random_id_operand
));
326 spin_unlock_irq(&cm
.lock
);
329 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
331 struct cm_id_private
*cm_id_priv
;
333 cm_id_priv
= idr_find(&cm
.local_id_table
,
334 (__force
int) (local_id
^ cm
.random_id_operand
));
336 if (cm_id_priv
->id
.remote_id
== remote_id
)
337 atomic_inc(&cm_id_priv
->refcount
);
345 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
347 struct cm_id_private
*cm_id_priv
;
349 spin_lock_irq(&cm
.lock
);
350 cm_id_priv
= cm_get_id(local_id
, remote_id
);
351 spin_unlock_irq(&cm
.lock
);
356 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
360 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
361 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
362 ((unsigned long *) mask
)[i
];
365 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
366 struct ib_cm_compare_data
*dst_data
)
368 u8 src
[IB_CM_COMPARE_SIZE
];
369 u8 dst
[IB_CM_COMPARE_SIZE
];
371 if (!src_data
|| !dst_data
)
374 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
375 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
376 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
379 static int cm_compare_private_data(u8
*private_data
,
380 struct ib_cm_compare_data
*dst_data
)
382 u8 src
[IB_CM_COMPARE_SIZE
];
387 cm_mask_copy(src
, private_data
, dst_data
->mask
);
388 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
391 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
393 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
394 struct rb_node
*parent
= NULL
;
395 struct cm_id_private
*cur_cm_id_priv
;
396 __be64 service_id
= cm_id_priv
->id
.service_id
;
397 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
402 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
404 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
405 cur_cm_id_priv
->compare_data
);
406 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
407 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
408 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
410 return cur_cm_id_priv
;
412 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
413 link
= &(*link
)->rb_left
;
414 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
415 link
= &(*link
)->rb_right
;
416 else if (service_id
< cur_cm_id_priv
->id
.service_id
)
417 link
= &(*link
)->rb_left
;
418 else if (service_id
> cur_cm_id_priv
->id
.service_id
)
419 link
= &(*link
)->rb_right
;
420 else if (data_cmp
< 0)
421 link
= &(*link
)->rb_left
;
423 link
= &(*link
)->rb_right
;
425 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
426 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
430 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
434 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
435 struct cm_id_private
*cm_id_priv
;
439 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
440 data_cmp
= cm_compare_private_data(private_data
,
441 cm_id_priv
->compare_data
);
442 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
443 cm_id_priv
->id
.service_id
&&
444 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
447 if (device
< cm_id_priv
->id
.device
)
448 node
= node
->rb_left
;
449 else if (device
> cm_id_priv
->id
.device
)
450 node
= node
->rb_right
;
451 else if (service_id
< cm_id_priv
->id
.service_id
)
452 node
= node
->rb_left
;
453 else if (service_id
> cm_id_priv
->id
.service_id
)
454 node
= node
->rb_right
;
455 else if (data_cmp
< 0)
456 node
= node
->rb_left
;
458 node
= node
->rb_right
;
463 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
466 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
467 struct rb_node
*parent
= NULL
;
468 struct cm_timewait_info
*cur_timewait_info
;
469 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
470 __be32 remote_id
= timewait_info
->work
.remote_id
;
474 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
476 if (remote_id
< cur_timewait_info
->work
.remote_id
)
477 link
= &(*link
)->rb_left
;
478 else if (remote_id
> cur_timewait_info
->work
.remote_id
)
479 link
= &(*link
)->rb_right
;
480 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
481 link
= &(*link
)->rb_left
;
482 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
483 link
= &(*link
)->rb_right
;
485 return cur_timewait_info
;
487 timewait_info
->inserted_remote_id
= 1;
488 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
489 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
493 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
496 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
497 struct cm_timewait_info
*timewait_info
;
500 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
502 if (remote_id
< timewait_info
->work
.remote_id
)
503 node
= node
->rb_left
;
504 else if (remote_id
> timewait_info
->work
.remote_id
)
505 node
= node
->rb_right
;
506 else if (remote_ca_guid
< timewait_info
->remote_ca_guid
)
507 node
= node
->rb_left
;
508 else if (remote_ca_guid
> timewait_info
->remote_ca_guid
)
509 node
= node
->rb_right
;
511 return timewait_info
;
516 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
519 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
520 struct rb_node
*parent
= NULL
;
521 struct cm_timewait_info
*cur_timewait_info
;
522 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
523 __be32 remote_qpn
= timewait_info
->remote_qpn
;
527 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
529 if (remote_qpn
< cur_timewait_info
->remote_qpn
)
530 link
= &(*link
)->rb_left
;
531 else if (remote_qpn
> cur_timewait_info
->remote_qpn
)
532 link
= &(*link
)->rb_right
;
533 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
534 link
= &(*link
)->rb_left
;
535 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
536 link
= &(*link
)->rb_right
;
538 return cur_timewait_info
;
540 timewait_info
->inserted_remote_qp
= 1;
541 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
542 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
546 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
549 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
550 struct rb_node
*parent
= NULL
;
551 struct cm_id_private
*cur_cm_id_priv
;
552 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
553 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
557 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
559 if (remote_id
< cur_cm_id_priv
->id
.remote_id
)
560 link
= &(*link
)->rb_left
;
561 else if (remote_id
> cur_cm_id_priv
->id
.remote_id
)
562 link
= &(*link
)->rb_right
;
565 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
568 link
= &(*link
)->rb_left
;
570 link
= &(*link
)->rb_right
;
572 return cur_cm_id_priv
;
575 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
576 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
580 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
581 enum ib_cm_sidr_status status
)
583 struct ib_cm_sidr_rep_param param
;
585 memset(¶m
, 0, sizeof param
);
586 param
.status
= status
;
587 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
590 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
591 ib_cm_handler cm_handler
,
594 struct cm_id_private
*cm_id_priv
;
597 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
599 return ERR_PTR(-ENOMEM
);
601 cm_id_priv
->id
.state
= IB_CM_IDLE
;
602 cm_id_priv
->id
.device
= device
;
603 cm_id_priv
->id
.cm_handler
= cm_handler
;
604 cm_id_priv
->id
.context
= context
;
605 cm_id_priv
->id
.remote_cm_qpn
= 1;
606 ret
= cm_alloc_id(cm_id_priv
);
610 spin_lock_init(&cm_id_priv
->lock
);
611 init_completion(&cm_id_priv
->comp
);
612 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
613 atomic_set(&cm_id_priv
->work_count
, -1);
614 atomic_set(&cm_id_priv
->refcount
, 1);
615 return &cm_id_priv
->id
;
619 return ERR_PTR(-ENOMEM
);
621 EXPORT_SYMBOL(ib_create_cm_id
);
623 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
625 struct cm_work
*work
;
627 if (list_empty(&cm_id_priv
->work_list
))
630 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
631 list_del(&work
->list
);
635 static void cm_free_work(struct cm_work
*work
)
637 if (work
->mad_recv_wc
)
638 ib_free_recv_mad(work
->mad_recv_wc
);
642 static inline int cm_convert_to_ms(int iba_time
)
644 /* approximate conversion to ms from 4.096us x 2^iba_time */
645 return 1 << max(iba_time
- 8, 0);
649 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
650 * Because of how ack_timeout is stored, adding one doubles the timeout.
651 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
652 * increment it (round up) only if the other is within 50%.
654 static u8
cm_ack_timeout(u8 ca_ack_delay
, u8 packet_life_time
)
656 int ack_timeout
= packet_life_time
+ 1;
658 if (ack_timeout
>= ca_ack_delay
)
659 ack_timeout
+= (ca_ack_delay
>= (ack_timeout
- 1));
661 ack_timeout
= ca_ack_delay
+
662 (ack_timeout
>= (ca_ack_delay
- 1));
664 return min(31, ack_timeout
);
667 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
669 if (timewait_info
->inserted_remote_id
) {
670 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
671 timewait_info
->inserted_remote_id
= 0;
674 if (timewait_info
->inserted_remote_qp
) {
675 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
676 timewait_info
->inserted_remote_qp
= 0;
680 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
682 struct cm_timewait_info
*timewait_info
;
684 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
686 return ERR_PTR(-ENOMEM
);
688 timewait_info
->work
.local_id
= local_id
;
689 INIT_DELAYED_WORK(&timewait_info
->work
.work
, cm_work_handler
);
690 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
691 return timewait_info
;
694 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
699 spin_lock_irqsave(&cm
.lock
, flags
);
700 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
701 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
702 spin_unlock_irqrestore(&cm
.lock
, flags
);
705 * The cm_id could be destroyed by the user before we exit timewait.
706 * To protect against this, we search for the cm_id after exiting
707 * timewait before notifying the user that we've exited timewait.
709 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
710 wait_time
= cm_convert_to_ms(cm_id_priv
->av
.timeout
);
711 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
712 msecs_to_jiffies(wait_time
));
713 cm_id_priv
->timewait_info
= NULL
;
716 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
720 cm_id_priv
->id
.state
= IB_CM_IDLE
;
721 if (cm_id_priv
->timewait_info
) {
722 spin_lock_irqsave(&cm
.lock
, flags
);
723 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
724 spin_unlock_irqrestore(&cm
.lock
, flags
);
725 kfree(cm_id_priv
->timewait_info
);
726 cm_id_priv
->timewait_info
= NULL
;
730 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
732 struct cm_id_private
*cm_id_priv
;
733 struct cm_work
*work
;
735 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
737 spin_lock_irq(&cm_id_priv
->lock
);
738 switch (cm_id
->state
) {
740 cm_id
->state
= IB_CM_IDLE
;
741 spin_unlock_irq(&cm_id_priv
->lock
);
742 spin_lock_irq(&cm
.lock
);
743 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
744 spin_unlock_irq(&cm
.lock
);
746 case IB_CM_SIDR_REQ_SENT
:
747 cm_id
->state
= IB_CM_IDLE
;
748 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
749 spin_unlock_irq(&cm_id_priv
->lock
);
751 case IB_CM_SIDR_REQ_RCVD
:
752 spin_unlock_irq(&cm_id_priv
->lock
);
753 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
756 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
757 spin_unlock_irq(&cm_id_priv
->lock
);
758 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
759 &cm_id_priv
->id
.device
->node_guid
,
760 sizeof cm_id_priv
->id
.device
->node_guid
,
764 if (err
== -ENOMEM
) {
765 /* Do not reject to allow future retries. */
766 cm_reset_to_idle(cm_id_priv
);
767 spin_unlock_irq(&cm_id_priv
->lock
);
769 spin_unlock_irq(&cm_id_priv
->lock
);
770 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
774 case IB_CM_MRA_REQ_RCVD
:
776 case IB_CM_MRA_REP_RCVD
:
777 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
779 case IB_CM_MRA_REQ_SENT
:
781 case IB_CM_MRA_REP_SENT
:
782 spin_unlock_irq(&cm_id_priv
->lock
);
783 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
786 case IB_CM_ESTABLISHED
:
787 spin_unlock_irq(&cm_id_priv
->lock
);
788 ib_send_cm_dreq(cm_id
, NULL
, 0);
790 case IB_CM_DREQ_SENT
:
791 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
792 cm_enter_timewait(cm_id_priv
);
793 spin_unlock_irq(&cm_id_priv
->lock
);
795 case IB_CM_DREQ_RCVD
:
796 spin_unlock_irq(&cm_id_priv
->lock
);
797 ib_send_cm_drep(cm_id
, NULL
, 0);
800 spin_unlock_irq(&cm_id_priv
->lock
);
804 cm_free_id(cm_id
->local_id
);
805 cm_deref_id(cm_id_priv
);
806 wait_for_completion(&cm_id_priv
->comp
);
807 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
809 kfree(cm_id_priv
->compare_data
);
810 kfree(cm_id_priv
->private_data
);
814 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
816 cm_destroy_id(cm_id
, 0);
818 EXPORT_SYMBOL(ib_destroy_cm_id
);
820 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
821 struct ib_cm_compare_data
*compare_data
)
823 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
827 service_mask
= service_mask
? service_mask
:
828 __constant_cpu_to_be64(~0ULL);
829 service_id
&= service_mask
;
830 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
831 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
834 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
835 if (cm_id
->state
!= IB_CM_IDLE
)
839 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
841 if (!cm_id_priv
->compare_data
)
843 cm_mask_copy(cm_id_priv
->compare_data
->data
,
844 compare_data
->data
, compare_data
->mask
);
845 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
849 cm_id
->state
= IB_CM_LISTEN
;
851 spin_lock_irqsave(&cm
.lock
, flags
);
852 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
853 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
854 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
856 cm_id
->service_id
= service_id
;
857 cm_id
->service_mask
= service_mask
;
859 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
860 spin_unlock_irqrestore(&cm
.lock
, flags
);
862 if (cur_cm_id_priv
) {
863 cm_id
->state
= IB_CM_IDLE
;
864 kfree(cm_id_priv
->compare_data
);
865 cm_id_priv
->compare_data
= NULL
;
870 EXPORT_SYMBOL(ib_cm_listen
);
872 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
873 enum cm_msg_sequence msg_seq
)
877 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
878 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
880 return cpu_to_be64(hi_tid
| low_tid
);
883 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
884 __be16 attr_id
, __be64 tid
)
886 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
887 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
888 hdr
->class_version
= IB_CM_CLASS_VERSION
;
889 hdr
->method
= IB_MGMT_METHOD_SEND
;
890 hdr
->attr_id
= attr_id
;
894 static void cm_format_req(struct cm_req_msg
*req_msg
,
895 struct cm_id_private
*cm_id_priv
,
896 struct ib_cm_req_param
*param
)
898 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
899 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
901 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
902 req_msg
->service_id
= param
->service_id
;
903 req_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
904 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
905 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
906 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
907 cm_req_set_remote_resp_timeout(req_msg
,
908 param
->remote_cm_response_timeout
);
909 cm_req_set_qp_type(req_msg
, param
->qp_type
);
910 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
911 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
912 cm_req_set_local_resp_timeout(req_msg
,
913 param
->local_cm_response_timeout
);
914 cm_req_set_retry_count(req_msg
, param
->retry_count
);
915 req_msg
->pkey
= param
->primary_path
->pkey
;
916 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
917 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
918 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
919 cm_req_set_srq(req_msg
, param
->srq
);
921 req_msg
->primary_local_lid
= param
->primary_path
->slid
;
922 req_msg
->primary_remote_lid
= param
->primary_path
->dlid
;
923 req_msg
->primary_local_gid
= param
->primary_path
->sgid
;
924 req_msg
->primary_remote_gid
= param
->primary_path
->dgid
;
925 cm_req_set_primary_flow_label(req_msg
, param
->primary_path
->flow_label
);
926 cm_req_set_primary_packet_rate(req_msg
, param
->primary_path
->rate
);
927 req_msg
->primary_traffic_class
= param
->primary_path
->traffic_class
;
928 req_msg
->primary_hop_limit
= param
->primary_path
->hop_limit
;
929 cm_req_set_primary_sl(req_msg
, param
->primary_path
->sl
);
930 cm_req_set_primary_subnet_local(req_msg
, 1); /* local only... */
931 cm_req_set_primary_local_ack_timeout(req_msg
,
932 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
933 param
->primary_path
->packet_life_time
));
935 if (param
->alternate_path
) {
936 req_msg
->alt_local_lid
= param
->alternate_path
->slid
;
937 req_msg
->alt_remote_lid
= param
->alternate_path
->dlid
;
938 req_msg
->alt_local_gid
= param
->alternate_path
->sgid
;
939 req_msg
->alt_remote_gid
= param
->alternate_path
->dgid
;
940 cm_req_set_alt_flow_label(req_msg
,
941 param
->alternate_path
->flow_label
);
942 cm_req_set_alt_packet_rate(req_msg
, param
->alternate_path
->rate
);
943 req_msg
->alt_traffic_class
= param
->alternate_path
->traffic_class
;
944 req_msg
->alt_hop_limit
= param
->alternate_path
->hop_limit
;
945 cm_req_set_alt_sl(req_msg
, param
->alternate_path
->sl
);
946 cm_req_set_alt_subnet_local(req_msg
, 1); /* local only... */
947 cm_req_set_alt_local_ack_timeout(req_msg
,
948 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
949 param
->alternate_path
->packet_life_time
));
952 if (param
->private_data
&& param
->private_data_len
)
953 memcpy(req_msg
->private_data
, param
->private_data
,
954 param
->private_data_len
);
957 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
959 /* peer-to-peer not supported */
960 if (param
->peer_to_peer
)
963 if (!param
->primary_path
)
966 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
969 if (param
->private_data
&&
970 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
973 if (param
->alternate_path
&&
974 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
975 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
981 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
982 struct ib_cm_req_param
*param
)
984 struct cm_id_private
*cm_id_priv
;
985 struct cm_req_msg
*req_msg
;
989 ret
= cm_validate_req_param(param
);
993 /* Verify that we're not in timewait. */
994 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
995 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
996 if (cm_id
->state
!= IB_CM_IDLE
) {
997 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1001 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1003 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1005 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1006 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1010 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
1013 if (param
->alternate_path
) {
1014 ret
= cm_init_av_by_path(param
->alternate_path
,
1015 &cm_id_priv
->alt_av
);
1019 cm_id
->service_id
= param
->service_id
;
1020 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
1021 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1022 param
->primary_path
->packet_life_time
) * 2 +
1024 param
->remote_cm_response_timeout
);
1025 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1026 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1027 cm_id_priv
->responder_resources
= param
->responder_resources
;
1028 cm_id_priv
->retry_count
= param
->retry_count
;
1029 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1030 cm_id_priv
->pkey
= param
->primary_path
->pkey
;
1031 cm_id_priv
->qp_type
= param
->qp_type
;
1033 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1037 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1038 cm_format_req(req_msg
, cm_id_priv
, param
);
1039 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1040 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1041 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1043 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1044 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1046 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1047 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1049 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1052 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1053 cm_id
->state
= IB_CM_REQ_SENT
;
1054 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1057 error2
: cm_free_msg(cm_id_priv
->msg
);
1058 error1
: kfree(cm_id_priv
->timewait_info
);
1061 EXPORT_SYMBOL(ib_send_cm_req
);
1063 static int cm_issue_rej(struct cm_port
*port
,
1064 struct ib_mad_recv_wc
*mad_recv_wc
,
1065 enum ib_cm_rej_reason reason
,
1066 enum cm_msg_response msg_rejected
,
1067 void *ari
, u8 ari_length
)
1069 struct ib_mad_send_buf
*msg
= NULL
;
1070 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1073 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1077 /* We just need common CM header information. Cast to any message. */
1078 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1079 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1081 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1082 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1083 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1084 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1085 rej_msg
->reason
= cpu_to_be16(reason
);
1087 if (ari
&& ari_length
) {
1088 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1089 memcpy(rej_msg
->ari
, ari
, ari_length
);
1092 ret
= ib_post_send_mad(msg
, NULL
);
1099 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1100 __be32 local_qpn
, __be32 remote_qpn
)
1102 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1103 ((local_ca_guid
== remote_ca_guid
) &&
1104 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1107 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1108 struct ib_sa_path_rec
*primary_path
,
1109 struct ib_sa_path_rec
*alt_path
)
1111 memset(primary_path
, 0, sizeof *primary_path
);
1112 primary_path
->dgid
= req_msg
->primary_local_gid
;
1113 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1114 primary_path
->dlid
= req_msg
->primary_local_lid
;
1115 primary_path
->slid
= req_msg
->primary_remote_lid
;
1116 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1117 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1118 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1119 primary_path
->reversible
= 1;
1120 primary_path
->pkey
= req_msg
->pkey
;
1121 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1122 primary_path
->mtu_selector
= IB_SA_EQ
;
1123 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1124 primary_path
->rate_selector
= IB_SA_EQ
;
1125 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1126 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1127 primary_path
->packet_life_time
=
1128 cm_req_get_primary_local_ack_timeout(req_msg
);
1129 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1131 if (req_msg
->alt_local_lid
) {
1132 memset(alt_path
, 0, sizeof *alt_path
);
1133 alt_path
->dgid
= req_msg
->alt_local_gid
;
1134 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1135 alt_path
->dlid
= req_msg
->alt_local_lid
;
1136 alt_path
->slid
= req_msg
->alt_remote_lid
;
1137 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1138 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1139 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1140 alt_path
->reversible
= 1;
1141 alt_path
->pkey
= req_msg
->pkey
;
1142 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1143 alt_path
->mtu_selector
= IB_SA_EQ
;
1144 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1145 alt_path
->rate_selector
= IB_SA_EQ
;
1146 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1147 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1148 alt_path
->packet_life_time
=
1149 cm_req_get_alt_local_ack_timeout(req_msg
);
1150 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1154 static void cm_format_req_event(struct cm_work
*work
,
1155 struct cm_id_private
*cm_id_priv
,
1156 struct ib_cm_id
*listen_id
)
1158 struct cm_req_msg
*req_msg
;
1159 struct ib_cm_req_event_param
*param
;
1161 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1162 param
= &work
->cm_event
.param
.req_rcvd
;
1163 param
->listen_id
= listen_id
;
1164 param
->port
= cm_id_priv
->av
.port
->port_num
;
1165 param
->primary_path
= &work
->path
[0];
1166 if (req_msg
->alt_local_lid
)
1167 param
->alternate_path
= &work
->path
[1];
1169 param
->alternate_path
= NULL
;
1170 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1171 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1172 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1173 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1174 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1175 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1176 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1177 param
->local_cm_response_timeout
=
1178 cm_req_get_remote_resp_timeout(req_msg
);
1179 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1180 param
->remote_cm_response_timeout
=
1181 cm_req_get_local_resp_timeout(req_msg
);
1182 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1183 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1184 param
->srq
= cm_req_get_srq(req_msg
);
1185 work
->cm_event
.private_data
= &req_msg
->private_data
;
1188 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1189 struct cm_work
*work
)
1193 /* We will typically only have the current event to report. */
1194 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1197 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1198 spin_lock_irq(&cm_id_priv
->lock
);
1199 work
= cm_dequeue_work(cm_id_priv
);
1200 spin_unlock_irq(&cm_id_priv
->lock
);
1202 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1206 cm_deref_id(cm_id_priv
);
1208 cm_destroy_id(&cm_id_priv
->id
, ret
);
1211 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1212 struct cm_id_private
*cm_id_priv
,
1213 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1214 const void *private_data
, u8 private_data_len
)
1216 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1217 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1218 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1219 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1220 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1222 if (private_data
&& private_data_len
)
1223 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1226 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1227 struct cm_id_private
*cm_id_priv
,
1228 enum ib_cm_rej_reason reason
,
1231 const void *private_data
,
1232 u8 private_data_len
)
1234 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1235 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1237 switch(cm_id_priv
->id
.state
) {
1238 case IB_CM_REQ_RCVD
:
1239 rej_msg
->local_comm_id
= 0;
1240 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1242 case IB_CM_MRA_REQ_SENT
:
1243 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1244 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1246 case IB_CM_REP_RCVD
:
1247 case IB_CM_MRA_REP_SENT
:
1248 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1249 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1252 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1253 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1257 rej_msg
->reason
= cpu_to_be16(reason
);
1258 if (ari
&& ari_length
) {
1259 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1260 memcpy(rej_msg
->ari
, ari
, ari_length
);
1263 if (private_data
&& private_data_len
)
1264 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1267 static void cm_dup_req_handler(struct cm_work
*work
,
1268 struct cm_id_private
*cm_id_priv
)
1270 struct ib_mad_send_buf
*msg
= NULL
;
1273 /* Quick state check to discard duplicate REQs. */
1274 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1277 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1281 spin_lock_irq(&cm_id_priv
->lock
);
1282 switch (cm_id_priv
->id
.state
) {
1283 case IB_CM_MRA_REQ_SENT
:
1284 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1285 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1286 cm_id_priv
->private_data
,
1287 cm_id_priv
->private_data_len
);
1289 case IB_CM_TIMEWAIT
:
1290 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1291 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1296 spin_unlock_irq(&cm_id_priv
->lock
);
1298 ret
= ib_post_send_mad(msg
, NULL
);
1303 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1304 free
: cm_free_msg(msg
);
1307 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1308 struct cm_id_private
*cm_id_priv
)
1310 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1311 struct cm_timewait_info
*timewait_info
;
1312 struct cm_req_msg
*req_msg
;
1314 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1316 /* Check for possible duplicate REQ. */
1317 spin_lock_irq(&cm
.lock
);
1318 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1319 if (timewait_info
) {
1320 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1321 timewait_info
->work
.remote_id
);
1322 spin_unlock_irq(&cm
.lock
);
1323 if (cur_cm_id_priv
) {
1324 cm_dup_req_handler(work
, cur_cm_id_priv
);
1325 cm_deref_id(cur_cm_id_priv
);
1330 /* Check for stale connections. */
1331 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1332 if (timewait_info
) {
1333 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1334 spin_unlock_irq(&cm
.lock
);
1335 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1336 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1341 /* Find matching listen request. */
1342 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1343 req_msg
->service_id
,
1344 req_msg
->private_data
);
1345 if (!listen_cm_id_priv
) {
1346 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1347 spin_unlock_irq(&cm
.lock
);
1348 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1349 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1353 atomic_inc(&listen_cm_id_priv
->refcount
);
1354 atomic_inc(&cm_id_priv
->refcount
);
1355 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1356 atomic_inc(&cm_id_priv
->work_count
);
1357 spin_unlock_irq(&cm
.lock
);
1359 return listen_cm_id_priv
;
1362 static int cm_req_handler(struct cm_work
*work
)
1364 struct ib_cm_id
*cm_id
;
1365 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1366 struct cm_req_msg
*req_msg
;
1369 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1371 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
1373 return PTR_ERR(cm_id
);
1375 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1376 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1377 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1378 work
->mad_recv_wc
->recv_buf
.grh
,
1380 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1382 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1383 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1386 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1387 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1388 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1390 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1391 if (!listen_cm_id_priv
) {
1393 kfree(cm_id_priv
->timewait_info
);
1397 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1398 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1399 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1400 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
1402 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1403 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1405 ib_get_cached_gid(work
->port
->cm_dev
->device
,
1406 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1407 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1408 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1412 if (req_msg
->alt_local_lid
) {
1413 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1415 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1416 &work
->path
[0].sgid
,
1417 sizeof work
->path
[0].sgid
, NULL
, 0);
1421 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1422 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1423 cm_req_get_local_resp_timeout(req_msg
));
1424 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1425 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1426 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1427 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1428 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1429 cm_id_priv
->pkey
= req_msg
->pkey
;
1430 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1431 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1432 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1433 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1435 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1436 cm_process_work(cm_id_priv
, work
);
1437 cm_deref_id(listen_cm_id_priv
);
1441 atomic_dec(&cm_id_priv
->refcount
);
1442 cm_deref_id(listen_cm_id_priv
);
1444 ib_destroy_cm_id(cm_id
);
1448 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1449 struct cm_id_private
*cm_id_priv
,
1450 struct ib_cm_rep_param
*param
)
1452 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1453 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1454 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1455 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1456 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1457 rep_msg
->resp_resources
= param
->responder_resources
;
1458 rep_msg
->initiator_depth
= param
->initiator_depth
;
1459 cm_rep_set_target_ack_delay(rep_msg
,
1460 cm_id_priv
->av
.port
->cm_dev
->ack_delay
);
1461 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1462 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1463 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1464 cm_rep_set_srq(rep_msg
, param
->srq
);
1465 rep_msg
->local_ca_guid
= cm_id_priv
->id
.device
->node_guid
;
1467 if (param
->private_data
&& param
->private_data_len
)
1468 memcpy(rep_msg
->private_data
, param
->private_data
,
1469 param
->private_data_len
);
1472 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1473 struct ib_cm_rep_param
*param
)
1475 struct cm_id_private
*cm_id_priv
;
1476 struct ib_mad_send_buf
*msg
;
1477 struct cm_rep_msg
*rep_msg
;
1478 unsigned long flags
;
1481 if (param
->private_data
&&
1482 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1485 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1486 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1487 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1488 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1493 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1497 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1498 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1499 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1500 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1502 ret
= ib_post_send_mad(msg
, NULL
);
1504 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1509 cm_id
->state
= IB_CM_REP_SENT
;
1510 cm_id_priv
->msg
= msg
;
1511 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1512 cm_id_priv
->responder_resources
= param
->responder_resources
;
1513 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1514 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1516 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1519 EXPORT_SYMBOL(ib_send_cm_rep
);
1521 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1522 struct cm_id_private
*cm_id_priv
,
1523 const void *private_data
,
1524 u8 private_data_len
)
1526 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1527 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1528 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1530 if (private_data
&& private_data_len
)
1531 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1534 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1535 const void *private_data
,
1536 u8 private_data_len
)
1538 struct cm_id_private
*cm_id_priv
;
1539 struct ib_mad_send_buf
*msg
;
1540 unsigned long flags
;
1544 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1547 data
= cm_copy_private_data(private_data
, private_data_len
);
1549 return PTR_ERR(data
);
1551 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1552 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1553 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1554 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1559 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1563 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1564 private_data
, private_data_len
);
1566 ret
= ib_post_send_mad(msg
, NULL
);
1568 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1574 cm_id
->state
= IB_CM_ESTABLISHED
;
1575 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1576 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1579 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1583 EXPORT_SYMBOL(ib_send_cm_rtu
);
1585 static void cm_format_rep_event(struct cm_work
*work
)
1587 struct cm_rep_msg
*rep_msg
;
1588 struct ib_cm_rep_event_param
*param
;
1590 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1591 param
= &work
->cm_event
.param
.rep_rcvd
;
1592 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1593 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1594 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1595 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1596 param
->responder_resources
= rep_msg
->initiator_depth
;
1597 param
->initiator_depth
= rep_msg
->resp_resources
;
1598 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1599 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1600 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1601 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1602 param
->srq
= cm_rep_get_srq(rep_msg
);
1603 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1606 static void cm_dup_rep_handler(struct cm_work
*work
)
1608 struct cm_id_private
*cm_id_priv
;
1609 struct cm_rep_msg
*rep_msg
;
1610 struct ib_mad_send_buf
*msg
= NULL
;
1613 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1614 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1615 rep_msg
->local_comm_id
);
1619 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1623 spin_lock_irq(&cm_id_priv
->lock
);
1624 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1625 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1626 cm_id_priv
->private_data
,
1627 cm_id_priv
->private_data_len
);
1628 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1629 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1630 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1631 cm_id_priv
->private_data
,
1632 cm_id_priv
->private_data_len
);
1635 spin_unlock_irq(&cm_id_priv
->lock
);
1637 ret
= ib_post_send_mad(msg
, NULL
);
1642 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
1643 free
: cm_free_msg(msg
);
1644 deref
: cm_deref_id(cm_id_priv
);
1647 static int cm_rep_handler(struct cm_work
*work
)
1649 struct cm_id_private
*cm_id_priv
;
1650 struct cm_rep_msg
*rep_msg
;
1653 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1654 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1656 cm_dup_rep_handler(work
);
1660 cm_format_rep_event(work
);
1662 spin_lock_irq(&cm_id_priv
->lock
);
1663 switch (cm_id_priv
->id
.state
) {
1664 case IB_CM_REQ_SENT
:
1665 case IB_CM_MRA_REQ_RCVD
:
1668 spin_unlock_irq(&cm_id_priv
->lock
);
1673 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1674 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1675 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1677 spin_lock(&cm
.lock
);
1678 /* Check for duplicate REP. */
1679 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1680 spin_unlock(&cm
.lock
);
1681 spin_unlock_irq(&cm_id_priv
->lock
);
1685 /* Check for a stale connection. */
1686 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1687 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1688 &cm
.remote_id_table
);
1689 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1690 spin_unlock(&cm
.lock
);
1691 spin_unlock_irq(&cm_id_priv
->lock
);
1692 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1693 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1698 spin_unlock(&cm
.lock
);
1700 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1701 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1702 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1703 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1704 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1705 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1706 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1707 cm_id_priv
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1708 cm_id_priv
->av
.timeout
=
1709 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1710 cm_id_priv
->av
.timeout
- 1);
1711 cm_id_priv
->alt_av
.timeout
=
1712 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
1713 cm_id_priv
->alt_av
.timeout
- 1);
1715 /* todo: handle peer_to_peer */
1717 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1718 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1720 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1721 spin_unlock_irq(&cm_id_priv
->lock
);
1724 cm_process_work(cm_id_priv
, work
);
1726 cm_deref_id(cm_id_priv
);
1730 cm_deref_id(cm_id_priv
);
1734 static int cm_establish_handler(struct cm_work
*work
)
1736 struct cm_id_private
*cm_id_priv
;
1739 /* See comment in cm_establish about lookup. */
1740 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1744 spin_lock_irq(&cm_id_priv
->lock
);
1745 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1746 spin_unlock_irq(&cm_id_priv
->lock
);
1750 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1751 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1753 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1754 spin_unlock_irq(&cm_id_priv
->lock
);
1757 cm_process_work(cm_id_priv
, work
);
1759 cm_deref_id(cm_id_priv
);
1762 cm_deref_id(cm_id_priv
);
1766 static int cm_rtu_handler(struct cm_work
*work
)
1768 struct cm_id_private
*cm_id_priv
;
1769 struct cm_rtu_msg
*rtu_msg
;
1772 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1773 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1774 rtu_msg
->local_comm_id
);
1778 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1780 spin_lock_irq(&cm_id_priv
->lock
);
1781 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1782 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1783 spin_unlock_irq(&cm_id_priv
->lock
);
1786 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1788 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1789 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1791 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1792 spin_unlock_irq(&cm_id_priv
->lock
);
1795 cm_process_work(cm_id_priv
, work
);
1797 cm_deref_id(cm_id_priv
);
1800 cm_deref_id(cm_id_priv
);
1804 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1805 struct cm_id_private
*cm_id_priv
,
1806 const void *private_data
,
1807 u8 private_data_len
)
1809 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1810 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1811 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1812 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1813 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1815 if (private_data
&& private_data_len
)
1816 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1819 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1820 const void *private_data
,
1821 u8 private_data_len
)
1823 struct cm_id_private
*cm_id_priv
;
1824 struct ib_mad_send_buf
*msg
;
1825 unsigned long flags
;
1828 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1831 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1832 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1833 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1838 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1840 cm_enter_timewait(cm_id_priv
);
1844 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
1845 private_data
, private_data_len
);
1846 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1847 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
1849 ret
= ib_post_send_mad(msg
, NULL
);
1851 cm_enter_timewait(cm_id_priv
);
1852 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1857 cm_id
->state
= IB_CM_DREQ_SENT
;
1858 cm_id_priv
->msg
= msg
;
1859 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1862 EXPORT_SYMBOL(ib_send_cm_dreq
);
1864 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
1865 struct cm_id_private
*cm_id_priv
,
1866 const void *private_data
,
1867 u8 private_data_len
)
1869 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
1870 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1871 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1873 if (private_data
&& private_data_len
)
1874 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
1877 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
1878 const void *private_data
,
1879 u8 private_data_len
)
1881 struct cm_id_private
*cm_id_priv
;
1882 struct ib_mad_send_buf
*msg
;
1883 unsigned long flags
;
1887 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
1890 data
= cm_copy_private_data(private_data
, private_data_len
);
1892 return PTR_ERR(data
);
1894 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1895 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1896 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
1897 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1902 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1903 cm_enter_timewait(cm_id_priv
);
1905 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1909 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1910 private_data
, private_data_len
);
1912 ret
= ib_post_send_mad(msg
, NULL
);
1914 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1919 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1922 EXPORT_SYMBOL(ib_send_cm_drep
);
1924 static int cm_issue_drep(struct cm_port
*port
,
1925 struct ib_mad_recv_wc
*mad_recv_wc
)
1927 struct ib_mad_send_buf
*msg
= NULL
;
1928 struct cm_dreq_msg
*dreq_msg
;
1929 struct cm_drep_msg
*drep_msg
;
1932 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1936 dreq_msg
= (struct cm_dreq_msg
*) mad_recv_wc
->recv_buf
.mad
;
1937 drep_msg
= (struct cm_drep_msg
*) msg
->mad
;
1939 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, dreq_msg
->hdr
.tid
);
1940 drep_msg
->remote_comm_id
= dreq_msg
->local_comm_id
;
1941 drep_msg
->local_comm_id
= dreq_msg
->remote_comm_id
;
1943 ret
= ib_post_send_mad(msg
, NULL
);
1950 static int cm_dreq_handler(struct cm_work
*work
)
1952 struct cm_id_private
*cm_id_priv
;
1953 struct cm_dreq_msg
*dreq_msg
;
1954 struct ib_mad_send_buf
*msg
= NULL
;
1957 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1958 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
1959 dreq_msg
->local_comm_id
);
1961 cm_issue_drep(work
->port
, work
->mad_recv_wc
);
1965 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
1967 spin_lock_irq(&cm_id_priv
->lock
);
1968 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
1971 switch (cm_id_priv
->id
.state
) {
1972 case IB_CM_REP_SENT
:
1973 case IB_CM_DREQ_SENT
:
1974 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1976 case IB_CM_ESTABLISHED
:
1977 case IB_CM_MRA_REP_RCVD
:
1979 case IB_CM_TIMEWAIT
:
1980 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
1983 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1984 cm_id_priv
->private_data
,
1985 cm_id_priv
->private_data_len
);
1986 spin_unlock_irq(&cm_id_priv
->lock
);
1988 if (ib_post_send_mad(msg
, NULL
))
1994 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
1995 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
1996 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1998 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1999 spin_unlock_irq(&cm_id_priv
->lock
);
2002 cm_process_work(cm_id_priv
, work
);
2004 cm_deref_id(cm_id_priv
);
2007 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2008 deref
: cm_deref_id(cm_id_priv
);
2012 static int cm_drep_handler(struct cm_work
*work
)
2014 struct cm_id_private
*cm_id_priv
;
2015 struct cm_drep_msg
*drep_msg
;
2018 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2019 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
2020 drep_msg
->local_comm_id
);
2024 work
->cm_event
.private_data
= &drep_msg
->private_data
;
2026 spin_lock_irq(&cm_id_priv
->lock
);
2027 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
2028 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
2029 spin_unlock_irq(&cm_id_priv
->lock
);
2032 cm_enter_timewait(cm_id_priv
);
2034 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2035 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2037 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2038 spin_unlock_irq(&cm_id_priv
->lock
);
2041 cm_process_work(cm_id_priv
, work
);
2043 cm_deref_id(cm_id_priv
);
2046 cm_deref_id(cm_id_priv
);
2050 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2051 enum ib_cm_rej_reason reason
,
2054 const void *private_data
,
2055 u8 private_data_len
)
2057 struct cm_id_private
*cm_id_priv
;
2058 struct ib_mad_send_buf
*msg
;
2059 unsigned long flags
;
2062 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2063 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2066 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2068 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2069 switch (cm_id
->state
) {
2070 case IB_CM_REQ_SENT
:
2071 case IB_CM_MRA_REQ_RCVD
:
2072 case IB_CM_REQ_RCVD
:
2073 case IB_CM_MRA_REQ_SENT
:
2074 case IB_CM_REP_RCVD
:
2075 case IB_CM_MRA_REP_SENT
:
2076 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2078 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2079 cm_id_priv
, reason
, ari
, ari_length
,
2080 private_data
, private_data_len
);
2082 cm_reset_to_idle(cm_id_priv
);
2084 case IB_CM_REP_SENT
:
2085 case IB_CM_MRA_REP_RCVD
:
2086 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2088 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2089 cm_id_priv
, reason
, ari
, ari_length
,
2090 private_data
, private_data_len
);
2092 cm_enter_timewait(cm_id_priv
);
2102 ret
= ib_post_send_mad(msg
, NULL
);
2106 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2109 EXPORT_SYMBOL(ib_send_cm_rej
);
2111 static void cm_format_rej_event(struct cm_work
*work
)
2113 struct cm_rej_msg
*rej_msg
;
2114 struct ib_cm_rej_event_param
*param
;
2116 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2117 param
= &work
->cm_event
.param
.rej_rcvd
;
2118 param
->ari
= rej_msg
->ari
;
2119 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2120 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2121 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2124 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2126 struct cm_timewait_info
*timewait_info
;
2127 struct cm_id_private
*cm_id_priv
;
2130 remote_id
= rej_msg
->local_comm_id
;
2132 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2133 spin_lock_irq(&cm
.lock
);
2134 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2136 if (!timewait_info
) {
2137 spin_unlock_irq(&cm
.lock
);
2140 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2141 (timewait_info
->work
.local_id
^
2142 cm
.random_id_operand
));
2144 if (cm_id_priv
->id
.remote_id
== remote_id
)
2145 atomic_inc(&cm_id_priv
->refcount
);
2149 spin_unlock_irq(&cm
.lock
);
2150 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2151 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2153 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2158 static int cm_rej_handler(struct cm_work
*work
)
2160 struct cm_id_private
*cm_id_priv
;
2161 struct cm_rej_msg
*rej_msg
;
2164 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2165 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2169 cm_format_rej_event(work
);
2171 spin_lock_irq(&cm_id_priv
->lock
);
2172 switch (cm_id_priv
->id
.state
) {
2173 case IB_CM_REQ_SENT
:
2174 case IB_CM_MRA_REQ_RCVD
:
2175 case IB_CM_REP_SENT
:
2176 case IB_CM_MRA_REP_RCVD
:
2177 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2179 case IB_CM_REQ_RCVD
:
2180 case IB_CM_MRA_REQ_SENT
:
2181 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2182 cm_enter_timewait(cm_id_priv
);
2184 cm_reset_to_idle(cm_id_priv
);
2186 case IB_CM_DREQ_SENT
:
2187 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2189 case IB_CM_REP_RCVD
:
2190 case IB_CM_MRA_REP_SENT
:
2191 case IB_CM_ESTABLISHED
:
2192 cm_enter_timewait(cm_id_priv
);
2195 spin_unlock_irq(&cm_id_priv
->lock
);
2200 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2202 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2203 spin_unlock_irq(&cm_id_priv
->lock
);
2206 cm_process_work(cm_id_priv
, work
);
2208 cm_deref_id(cm_id_priv
);
2211 cm_deref_id(cm_id_priv
);
2215 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2217 const void *private_data
,
2218 u8 private_data_len
)
2220 struct cm_id_private
*cm_id_priv
;
2221 struct ib_mad_send_buf
*msg
;
2222 enum ib_cm_state cm_state
;
2223 enum ib_cm_lap_state lap_state
;
2224 enum cm_msg_response msg_response
;
2226 unsigned long flags
;
2229 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2232 data
= cm_copy_private_data(private_data
, private_data_len
);
2234 return PTR_ERR(data
);
2236 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2238 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2239 switch(cm_id_priv
->id
.state
) {
2240 case IB_CM_REQ_RCVD
:
2241 cm_state
= IB_CM_MRA_REQ_SENT
;
2242 lap_state
= cm_id
->lap_state
;
2243 msg_response
= CM_MSG_RESPONSE_REQ
;
2245 case IB_CM_REP_RCVD
:
2246 cm_state
= IB_CM_MRA_REP_SENT
;
2247 lap_state
= cm_id
->lap_state
;
2248 msg_response
= CM_MSG_RESPONSE_REP
;
2250 case IB_CM_ESTABLISHED
:
2251 cm_state
= cm_id
->state
;
2252 lap_state
= IB_CM_MRA_LAP_SENT
;
2253 msg_response
= CM_MSG_RESPONSE_OTHER
;
2260 if (!(service_timeout
& IB_CM_MRA_FLAG_DELAY
)) {
2261 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2265 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2266 msg_response
, service_timeout
,
2267 private_data
, private_data_len
);
2268 ret
= ib_post_send_mad(msg
, NULL
);
2273 cm_id
->state
= cm_state
;
2274 cm_id
->lap_state
= lap_state
;
2275 cm_id_priv
->service_timeout
= service_timeout
;
2276 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2277 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2280 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2284 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2289 EXPORT_SYMBOL(ib_send_cm_mra
);
2291 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2293 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2294 case CM_MSG_RESPONSE_REQ
:
2295 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2296 case CM_MSG_RESPONSE_REP
:
2297 case CM_MSG_RESPONSE_OTHER
:
2298 return cm_acquire_id(mra_msg
->remote_comm_id
,
2299 mra_msg
->local_comm_id
);
2305 static int cm_mra_handler(struct cm_work
*work
)
2307 struct cm_id_private
*cm_id_priv
;
2308 struct cm_mra_msg
*mra_msg
;
2311 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2312 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2316 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2317 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2318 cm_mra_get_service_timeout(mra_msg
);
2319 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2320 cm_convert_to_ms(cm_id_priv
->av
.timeout
);
2322 spin_lock_irq(&cm_id_priv
->lock
);
2323 switch (cm_id_priv
->id
.state
) {
2324 case IB_CM_REQ_SENT
:
2325 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2326 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2327 cm_id_priv
->msg
, timeout
))
2329 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2331 case IB_CM_REP_SENT
:
2332 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2333 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2334 cm_id_priv
->msg
, timeout
))
2336 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2338 case IB_CM_ESTABLISHED
:
2339 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2340 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2341 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2342 cm_id_priv
->msg
, timeout
))
2344 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2350 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2351 cm_id_priv
->id
.state
;
2352 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2354 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2355 spin_unlock_irq(&cm_id_priv
->lock
);
2358 cm_process_work(cm_id_priv
, work
);
2360 cm_deref_id(cm_id_priv
);
2363 spin_unlock_irq(&cm_id_priv
->lock
);
2364 cm_deref_id(cm_id_priv
);
2368 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2369 struct cm_id_private
*cm_id_priv
,
2370 struct ib_sa_path_rec
*alternate_path
,
2371 const void *private_data
,
2372 u8 private_data_len
)
2374 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2375 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2376 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2377 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2378 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2379 /* todo: need remote CM response timeout */
2380 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2381 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2382 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2383 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2384 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2385 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2386 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2387 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2388 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2389 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2390 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2391 cm_lap_set_local_ack_timeout(lap_msg
,
2392 cm_ack_timeout(cm_id_priv
->av
.port
->cm_dev
->ack_delay
,
2393 alternate_path
->packet_life_time
));
2395 if (private_data
&& private_data_len
)
2396 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2399 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2400 struct ib_sa_path_rec
*alternate_path
,
2401 const void *private_data
,
2402 u8 private_data_len
)
2404 struct cm_id_private
*cm_id_priv
;
2405 struct ib_mad_send_buf
*msg
;
2406 unsigned long flags
;
2409 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2412 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2413 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2414 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2415 (cm_id
->lap_state
!= IB_CM_LAP_UNINIT
&&
2416 cm_id
->lap_state
!= IB_CM_LAP_IDLE
)) {
2421 ret
= cm_init_av_by_path(alternate_path
, &cm_id_priv
->alt_av
);
2424 cm_id_priv
->alt_av
.timeout
=
2425 cm_ack_timeout(cm_id_priv
->target_ack_delay
,
2426 cm_id_priv
->alt_av
.timeout
- 1);
2428 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2432 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2433 alternate_path
, private_data
, private_data_len
);
2434 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2435 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2437 ret
= ib_post_send_mad(msg
, NULL
);
2439 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2444 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2445 cm_id_priv
->msg
= msg
;
2447 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2450 EXPORT_SYMBOL(ib_send_cm_lap
);
2452 static void cm_format_path_from_lap(struct cm_id_private
*cm_id_priv
,
2453 struct ib_sa_path_rec
*path
,
2454 struct cm_lap_msg
*lap_msg
)
2456 memset(path
, 0, sizeof *path
);
2457 path
->dgid
= lap_msg
->alt_local_gid
;
2458 path
->sgid
= lap_msg
->alt_remote_gid
;
2459 path
->dlid
= lap_msg
->alt_local_lid
;
2460 path
->slid
= lap_msg
->alt_remote_lid
;
2461 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2462 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2463 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2464 path
->reversible
= 1;
2465 path
->pkey
= cm_id_priv
->pkey
;
2466 path
->sl
= cm_lap_get_sl(lap_msg
);
2467 path
->mtu_selector
= IB_SA_EQ
;
2468 path
->mtu
= cm_id_priv
->path_mtu
;
2469 path
->rate_selector
= IB_SA_EQ
;
2470 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2471 path
->packet_life_time_selector
= IB_SA_EQ
;
2472 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2473 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2476 static int cm_lap_handler(struct cm_work
*work
)
2478 struct cm_id_private
*cm_id_priv
;
2479 struct cm_lap_msg
*lap_msg
;
2480 struct ib_cm_lap_event_param
*param
;
2481 struct ib_mad_send_buf
*msg
= NULL
;
2484 /* todo: verify LAP request and send reject APR if invalid. */
2485 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2486 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2487 lap_msg
->local_comm_id
);
2491 param
= &work
->cm_event
.param
.lap_rcvd
;
2492 param
->alternate_path
= &work
->path
[0];
2493 cm_format_path_from_lap(cm_id_priv
, param
->alternate_path
, lap_msg
);
2494 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2496 spin_lock_irq(&cm_id_priv
->lock
);
2497 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2500 switch (cm_id_priv
->id
.lap_state
) {
2501 case IB_CM_LAP_UNINIT
:
2502 case IB_CM_LAP_IDLE
:
2504 case IB_CM_MRA_LAP_SENT
:
2505 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2508 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2509 CM_MSG_RESPONSE_OTHER
,
2510 cm_id_priv
->service_timeout
,
2511 cm_id_priv
->private_data
,
2512 cm_id_priv
->private_data_len
);
2513 spin_unlock_irq(&cm_id_priv
->lock
);
2515 if (ib_post_send_mad(msg
, NULL
))
2522 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2523 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2524 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2525 work
->mad_recv_wc
->recv_buf
.grh
,
2527 cm_init_av_by_path(param
->alternate_path
, &cm_id_priv
->alt_av
);
2528 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2530 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2531 spin_unlock_irq(&cm_id_priv
->lock
);
2534 cm_process_work(cm_id_priv
, work
);
2536 cm_deref_id(cm_id_priv
);
2539 unlock
: spin_unlock_irq(&cm_id_priv
->lock
);
2540 deref
: cm_deref_id(cm_id_priv
);
2544 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2545 struct cm_id_private
*cm_id_priv
,
2546 enum ib_cm_apr_status status
,
2549 const void *private_data
,
2550 u8 private_data_len
)
2552 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2553 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2554 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2555 apr_msg
->ap_status
= (u8
) status
;
2557 if (info
&& info_length
) {
2558 apr_msg
->info_length
= info_length
;
2559 memcpy(apr_msg
->info
, info
, info_length
);
2562 if (private_data
&& private_data_len
)
2563 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2566 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2567 enum ib_cm_apr_status status
,
2570 const void *private_data
,
2571 u8 private_data_len
)
2573 struct cm_id_private
*cm_id_priv
;
2574 struct ib_mad_send_buf
*msg
;
2575 unsigned long flags
;
2578 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2579 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2582 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2583 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2584 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2585 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2586 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2591 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2595 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2596 info
, info_length
, private_data
, private_data_len
);
2597 ret
= ib_post_send_mad(msg
, NULL
);
2599 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2604 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2605 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2608 EXPORT_SYMBOL(ib_send_cm_apr
);
2610 static int cm_apr_handler(struct cm_work
*work
)
2612 struct cm_id_private
*cm_id_priv
;
2613 struct cm_apr_msg
*apr_msg
;
2616 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2617 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2618 apr_msg
->local_comm_id
);
2620 return -EINVAL
; /* Unmatched reply. */
2622 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2623 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2624 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2625 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2627 spin_lock_irq(&cm_id_priv
->lock
);
2628 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2629 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2630 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2631 spin_unlock_irq(&cm_id_priv
->lock
);
2634 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2635 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2636 cm_id_priv
->msg
= NULL
;
2638 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2640 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2641 spin_unlock_irq(&cm_id_priv
->lock
);
2644 cm_process_work(cm_id_priv
, work
);
2646 cm_deref_id(cm_id_priv
);
2649 cm_deref_id(cm_id_priv
);
2653 static int cm_timewait_handler(struct cm_work
*work
)
2655 struct cm_timewait_info
*timewait_info
;
2656 struct cm_id_private
*cm_id_priv
;
2659 timewait_info
= (struct cm_timewait_info
*)work
;
2660 spin_lock_irq(&cm
.lock
);
2661 list_del(&timewait_info
->list
);
2662 spin_unlock_irq(&cm
.lock
);
2664 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2665 timewait_info
->work
.remote_id
);
2669 spin_lock_irq(&cm_id_priv
->lock
);
2670 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2671 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2672 spin_unlock_irq(&cm_id_priv
->lock
);
2675 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2676 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2678 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2679 spin_unlock_irq(&cm_id_priv
->lock
);
2682 cm_process_work(cm_id_priv
, work
);
2684 cm_deref_id(cm_id_priv
);
2687 cm_deref_id(cm_id_priv
);
2691 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2692 struct cm_id_private
*cm_id_priv
,
2693 struct ib_cm_sidr_req_param
*param
)
2695 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2696 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2697 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2698 sidr_req_msg
->pkey
= cpu_to_be16(param
->path
->pkey
);
2699 sidr_req_msg
->service_id
= param
->service_id
;
2701 if (param
->private_data
&& param
->private_data_len
)
2702 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2703 param
->private_data_len
);
2706 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2707 struct ib_cm_sidr_req_param
*param
)
2709 struct cm_id_private
*cm_id_priv
;
2710 struct ib_mad_send_buf
*msg
;
2711 unsigned long flags
;
2714 if (!param
->path
|| (param
->private_data
&&
2715 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2718 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2719 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2723 cm_id
->service_id
= param
->service_id
;
2724 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
2725 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2726 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2727 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2731 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2733 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2734 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2736 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2737 if (cm_id
->state
== IB_CM_IDLE
)
2738 ret
= ib_post_send_mad(msg
, NULL
);
2743 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2747 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2748 cm_id_priv
->msg
= msg
;
2749 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2753 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2755 static void cm_format_sidr_req_event(struct cm_work
*work
,
2756 struct ib_cm_id
*listen_id
)
2758 struct cm_sidr_req_msg
*sidr_req_msg
;
2759 struct ib_cm_sidr_req_event_param
*param
;
2761 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2762 work
->mad_recv_wc
->recv_buf
.mad
;
2763 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2764 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2765 param
->listen_id
= listen_id
;
2766 param
->port
= work
->port
->port_num
;
2767 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2770 static int cm_sidr_req_handler(struct cm_work
*work
)
2772 struct ib_cm_id
*cm_id
;
2773 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2774 struct cm_sidr_req_msg
*sidr_req_msg
;
2777 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
2779 return PTR_ERR(cm_id
);
2780 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2782 /* Record SGID/SLID and request ID for lookup. */
2783 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2784 work
->mad_recv_wc
->recv_buf
.mad
;
2785 wc
= work
->mad_recv_wc
->wc
;
2786 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2787 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2788 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2789 work
->mad_recv_wc
->recv_buf
.grh
,
2791 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2792 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2793 atomic_inc(&cm_id_priv
->work_count
);
2795 spin_lock_irq(&cm
.lock
);
2796 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2797 if (cur_cm_id_priv
) {
2798 spin_unlock_irq(&cm
.lock
);
2799 goto out
; /* Duplicate message. */
2801 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
2802 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
2803 sidr_req_msg
->service_id
,
2804 sidr_req_msg
->private_data
);
2805 if (!cur_cm_id_priv
) {
2806 spin_unlock_irq(&cm
.lock
);
2807 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_UNSUPPORTED
);
2808 goto out
; /* No match. */
2810 atomic_inc(&cur_cm_id_priv
->refcount
);
2811 spin_unlock_irq(&cm
.lock
);
2813 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
2814 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
2815 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
2816 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
2818 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
2819 cm_process_work(cm_id_priv
, work
);
2820 cm_deref_id(cur_cm_id_priv
);
2823 ib_destroy_cm_id(&cm_id_priv
->id
);
2827 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
2828 struct cm_id_private
*cm_id_priv
,
2829 struct ib_cm_sidr_rep_param
*param
)
2831 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
2833 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
2834 sidr_rep_msg
->status
= param
->status
;
2835 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
2836 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
2837 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
2839 if (param
->info
&& param
->info_length
)
2840 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
2842 if (param
->private_data
&& param
->private_data_len
)
2843 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
2844 param
->private_data_len
);
2847 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
2848 struct ib_cm_sidr_rep_param
*param
)
2850 struct cm_id_private
*cm_id_priv
;
2851 struct ib_mad_send_buf
*msg
;
2852 unsigned long flags
;
2855 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
2856 (param
->private_data
&&
2857 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
2860 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2861 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2862 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
2867 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2871 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
2873 ret
= ib_post_send_mad(msg
, NULL
);
2875 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2879 cm_id
->state
= IB_CM_IDLE
;
2880 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2882 spin_lock_irqsave(&cm
.lock
, flags
);
2883 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2884 spin_unlock_irqrestore(&cm
.lock
, flags
);
2887 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2890 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
2892 static void cm_format_sidr_rep_event(struct cm_work
*work
)
2894 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2895 struct ib_cm_sidr_rep_event_param
*param
;
2897 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2898 work
->mad_recv_wc
->recv_buf
.mad
;
2899 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
2900 param
->status
= sidr_rep_msg
->status
;
2901 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
2902 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
2903 param
->info
= &sidr_rep_msg
->info
;
2904 param
->info_len
= sidr_rep_msg
->info_length
;
2905 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
2908 static int cm_sidr_rep_handler(struct cm_work
*work
)
2910 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2911 struct cm_id_private
*cm_id_priv
;
2913 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2914 work
->mad_recv_wc
->recv_buf
.mad
;
2915 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
2917 return -EINVAL
; /* Unmatched reply. */
2919 spin_lock_irq(&cm_id_priv
->lock
);
2920 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
2921 spin_unlock_irq(&cm_id_priv
->lock
);
2924 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2925 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2926 spin_unlock_irq(&cm_id_priv
->lock
);
2928 cm_format_sidr_rep_event(work
);
2929 cm_process_work(cm_id_priv
, work
);
2932 cm_deref_id(cm_id_priv
);
2936 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
2937 enum ib_wc_status wc_status
)
2939 struct cm_id_private
*cm_id_priv
;
2940 struct ib_cm_event cm_event
;
2941 enum ib_cm_state state
;
2944 memset(&cm_event
, 0, sizeof cm_event
);
2945 cm_id_priv
= msg
->context
[0];
2947 /* Discard old sends or ones without a response. */
2948 spin_lock_irq(&cm_id_priv
->lock
);
2949 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
2950 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
2954 case IB_CM_REQ_SENT
:
2955 case IB_CM_MRA_REQ_RCVD
:
2956 cm_reset_to_idle(cm_id_priv
);
2957 cm_event
.event
= IB_CM_REQ_ERROR
;
2959 case IB_CM_REP_SENT
:
2960 case IB_CM_MRA_REP_RCVD
:
2961 cm_reset_to_idle(cm_id_priv
);
2962 cm_event
.event
= IB_CM_REP_ERROR
;
2964 case IB_CM_DREQ_SENT
:
2965 cm_enter_timewait(cm_id_priv
);
2966 cm_event
.event
= IB_CM_DREQ_ERROR
;
2968 case IB_CM_SIDR_REQ_SENT
:
2969 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2970 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
2975 spin_unlock_irq(&cm_id_priv
->lock
);
2976 cm_event
.param
.send_status
= wc_status
;
2978 /* No other events can occur on the cm_id at this point. */
2979 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
2982 ib_destroy_cm_id(&cm_id_priv
->id
);
2985 spin_unlock_irq(&cm_id_priv
->lock
);
2989 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
2990 struct ib_mad_send_wc
*mad_send_wc
)
2992 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
2994 switch (mad_send_wc
->status
) {
2996 case IB_WC_WR_FLUSH_ERR
:
3000 if (msg
->context
[0] && msg
->context
[1])
3001 cm_process_send_error(msg
, mad_send_wc
->status
);
3008 static void cm_work_handler(struct work_struct
*_work
)
3010 struct cm_work
*work
= container_of(_work
, struct cm_work
, work
.work
);
3013 switch (work
->cm_event
.event
) {
3014 case IB_CM_REQ_RECEIVED
:
3015 ret
= cm_req_handler(work
);
3017 case IB_CM_MRA_RECEIVED
:
3018 ret
= cm_mra_handler(work
);
3020 case IB_CM_REJ_RECEIVED
:
3021 ret
= cm_rej_handler(work
);
3023 case IB_CM_REP_RECEIVED
:
3024 ret
= cm_rep_handler(work
);
3026 case IB_CM_RTU_RECEIVED
:
3027 ret
= cm_rtu_handler(work
);
3029 case IB_CM_USER_ESTABLISHED
:
3030 ret
= cm_establish_handler(work
);
3032 case IB_CM_DREQ_RECEIVED
:
3033 ret
= cm_dreq_handler(work
);
3035 case IB_CM_DREP_RECEIVED
:
3036 ret
= cm_drep_handler(work
);
3038 case IB_CM_SIDR_REQ_RECEIVED
:
3039 ret
= cm_sidr_req_handler(work
);
3041 case IB_CM_SIDR_REP_RECEIVED
:
3042 ret
= cm_sidr_rep_handler(work
);
3044 case IB_CM_LAP_RECEIVED
:
3045 ret
= cm_lap_handler(work
);
3047 case IB_CM_APR_RECEIVED
:
3048 ret
= cm_apr_handler(work
);
3050 case IB_CM_TIMEWAIT_EXIT
:
3051 ret
= cm_timewait_handler(work
);
3061 static int cm_establish(struct ib_cm_id
*cm_id
)
3063 struct cm_id_private
*cm_id_priv
;
3064 struct cm_work
*work
;
3065 unsigned long flags
;
3068 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3072 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3073 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3074 switch (cm_id
->state
)
3076 case IB_CM_REP_SENT
:
3077 case IB_CM_MRA_REP_RCVD
:
3078 cm_id
->state
= IB_CM_ESTABLISHED
;
3080 case IB_CM_ESTABLISHED
:
3087 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3095 * The CM worker thread may try to destroy the cm_id before it
3096 * can execute this work item. To prevent potential deadlock,
3097 * we need to find the cm_id once we're in the context of the
3098 * worker thread, rather than holding a reference on it.
3100 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3101 work
->local_id
= cm_id
->local_id
;
3102 work
->remote_id
= cm_id
->remote_id
;
3103 work
->mad_recv_wc
= NULL
;
3104 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3105 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3110 static int cm_migrate(struct ib_cm_id
*cm_id
)
3112 struct cm_id_private
*cm_id_priv
;
3113 unsigned long flags
;
3116 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3117 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3118 if (cm_id
->state
== IB_CM_ESTABLISHED
&&
3119 (cm_id
->lap_state
== IB_CM_LAP_UNINIT
||
3120 cm_id
->lap_state
== IB_CM_LAP_IDLE
)) {
3121 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
3122 cm_id_priv
->av
= cm_id_priv
->alt_av
;
3125 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3130 int ib_cm_notify(struct ib_cm_id
*cm_id
, enum ib_event_type event
)
3135 case IB_EVENT_COMM_EST
:
3136 ret
= cm_establish(cm_id
);
3138 case IB_EVENT_PATH_MIG
:
3139 ret
= cm_migrate(cm_id
);
3146 EXPORT_SYMBOL(ib_cm_notify
);
3148 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3149 struct ib_mad_recv_wc
*mad_recv_wc
)
3151 struct cm_work
*work
;
3152 enum ib_cm_event_type event
;
3155 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3156 case CM_REQ_ATTR_ID
:
3157 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3158 alt_local_lid
!= 0);
3159 event
= IB_CM_REQ_RECEIVED
;
3161 case CM_MRA_ATTR_ID
:
3162 event
= IB_CM_MRA_RECEIVED
;
3164 case CM_REJ_ATTR_ID
:
3165 event
= IB_CM_REJ_RECEIVED
;
3167 case CM_REP_ATTR_ID
:
3168 event
= IB_CM_REP_RECEIVED
;
3170 case CM_RTU_ATTR_ID
:
3171 event
= IB_CM_RTU_RECEIVED
;
3173 case CM_DREQ_ATTR_ID
:
3174 event
= IB_CM_DREQ_RECEIVED
;
3176 case CM_DREP_ATTR_ID
:
3177 event
= IB_CM_DREP_RECEIVED
;
3179 case CM_SIDR_REQ_ATTR_ID
:
3180 event
= IB_CM_SIDR_REQ_RECEIVED
;
3182 case CM_SIDR_REP_ATTR_ID
:
3183 event
= IB_CM_SIDR_REP_RECEIVED
;
3185 case CM_LAP_ATTR_ID
:
3187 event
= IB_CM_LAP_RECEIVED
;
3189 case CM_APR_ATTR_ID
:
3190 event
= IB_CM_APR_RECEIVED
;
3193 ib_free_recv_mad(mad_recv_wc
);
3197 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3200 ib_free_recv_mad(mad_recv_wc
);
3204 INIT_DELAYED_WORK(&work
->work
, cm_work_handler
);
3205 work
->cm_event
.event
= event
;
3206 work
->mad_recv_wc
= mad_recv_wc
;
3207 work
->port
= (struct cm_port
*)mad_agent
->context
;
3208 queue_delayed_work(cm
.wq
, &work
->work
, 0);
3211 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3212 struct ib_qp_attr
*qp_attr
,
3215 unsigned long flags
;
3218 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3219 switch (cm_id_priv
->id
.state
) {
3220 case IB_CM_REQ_SENT
:
3221 case IB_CM_MRA_REQ_RCVD
:
3222 case IB_CM_REQ_RCVD
:
3223 case IB_CM_MRA_REQ_SENT
:
3224 case IB_CM_REP_RCVD
:
3225 case IB_CM_MRA_REP_SENT
:
3226 case IB_CM_REP_SENT
:
3227 case IB_CM_MRA_REP_RCVD
:
3228 case IB_CM_ESTABLISHED
:
3229 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3230 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3231 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
;
3232 if (cm_id_priv
->responder_resources
)
3233 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3234 IB_ACCESS_REMOTE_ATOMIC
;
3235 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3236 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3243 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3247 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3248 struct ib_qp_attr
*qp_attr
,
3251 unsigned long flags
;
3254 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3255 switch (cm_id_priv
->id
.state
) {
3256 case IB_CM_REQ_RCVD
:
3257 case IB_CM_MRA_REQ_SENT
:
3258 case IB_CM_REP_RCVD
:
3259 case IB_CM_MRA_REP_SENT
:
3260 case IB_CM_REP_SENT
:
3261 case IB_CM_MRA_REP_RCVD
:
3262 case IB_CM_ESTABLISHED
:
3263 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3264 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3265 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3266 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3267 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3268 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3269 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3270 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3271 IB_QP_MIN_RNR_TIMER
;
3272 qp_attr
->max_dest_rd_atomic
=
3273 cm_id_priv
->responder_resources
;
3274 qp_attr
->min_rnr_timer
= 0;
3276 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3277 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3278 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3279 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3280 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3281 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3289 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3293 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3294 struct ib_qp_attr
*qp_attr
,
3297 unsigned long flags
;
3300 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3301 switch (cm_id_priv
->id
.state
) {
3302 /* Allow transition to RTS before sending REP */
3303 case IB_CM_REQ_RCVD
:
3304 case IB_CM_MRA_REQ_SENT
:
3306 case IB_CM_REP_RCVD
:
3307 case IB_CM_MRA_REP_SENT
:
3308 case IB_CM_REP_SENT
:
3309 case IB_CM_MRA_REP_RCVD
:
3310 case IB_CM_ESTABLISHED
:
3311 if (cm_id_priv
->id
.lap_state
== IB_CM_LAP_UNINIT
) {
3312 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3313 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3314 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3315 *qp_attr_mask
|= IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3317 IB_QP_MAX_QP_RD_ATOMIC
;
3318 qp_attr
->timeout
= cm_id_priv
->av
.timeout
;
3319 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3320 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3321 qp_attr
->max_rd_atomic
=
3322 cm_id_priv
->initiator_depth
;
3324 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3325 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3326 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3329 *qp_attr_mask
= IB_QP_ALT_PATH
| IB_QP_PATH_MIG_STATE
;
3330 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3331 qp_attr
->alt_pkey_index
= cm_id_priv
->alt_av
.pkey_index
;
3332 qp_attr
->alt_timeout
= cm_id_priv
->alt_av
.timeout
;
3333 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3334 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3342 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3346 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3347 struct ib_qp_attr
*qp_attr
,
3350 struct cm_id_private
*cm_id_priv
;
3353 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3354 switch (qp_attr
->qp_state
) {
3356 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3359 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3362 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3370 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3372 static void cm_get_ack_delay(struct cm_device
*cm_dev
)
3374 struct ib_device_attr attr
;
3376 if (ib_query_device(cm_dev
->device
, &attr
))
3377 cm_dev
->ack_delay
= 0; /* acks will rely on packet life time */
3379 cm_dev
->ack_delay
= attr
.local_ca_ack_delay
;
3382 static void cm_add_one(struct ib_device
*device
)
3384 struct cm_device
*cm_dev
;
3385 struct cm_port
*port
;
3386 struct ib_mad_reg_req reg_req
= {
3387 .mgmt_class
= IB_MGMT_CLASS_CM
,
3388 .mgmt_class_version
= IB_CM_CLASS_VERSION
3390 struct ib_port_modify port_modify
= {
3391 .set_port_cap_mask
= IB_PORT_CM_SUP
3393 unsigned long flags
;
3397 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
3400 cm_dev
= kmalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3401 device
->phys_port_cnt
, GFP_KERNEL
);
3405 cm_dev
->device
= device
;
3406 cm_get_ack_delay(cm_dev
);
3408 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3409 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3410 port
= &cm_dev
->port
[i
-1];
3411 port
->cm_dev
= cm_dev
;
3413 port
->mad_agent
= ib_register_mad_agent(device
, i
,
3420 if (IS_ERR(port
->mad_agent
))
3423 ret
= ib_modify_port(device
, i
, 0, &port_modify
);
3427 ib_set_client_data(device
, &cm_client
, cm_dev
);
3429 write_lock_irqsave(&cm
.device_lock
, flags
);
3430 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3431 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3435 ib_unregister_mad_agent(port
->mad_agent
);
3437 port_modify
.set_port_cap_mask
= 0;
3438 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3440 port
= &cm_dev
->port
[i
-1];
3441 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3442 ib_unregister_mad_agent(port
->mad_agent
);
3447 static void cm_remove_one(struct ib_device
*device
)
3449 struct cm_device
*cm_dev
;
3450 struct cm_port
*port
;
3451 struct ib_port_modify port_modify
= {
3452 .clr_port_cap_mask
= IB_PORT_CM_SUP
3454 unsigned long flags
;
3457 cm_dev
= ib_get_client_data(device
, &cm_client
);
3461 write_lock_irqsave(&cm
.device_lock
, flags
);
3462 list_del(&cm_dev
->list
);
3463 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3465 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3466 port
= &cm_dev
->port
[i
-1];
3467 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3468 ib_unregister_mad_agent(port
->mad_agent
);
3473 static int __init
ib_cm_init(void)
3477 memset(&cm
, 0, sizeof cm
);
3478 INIT_LIST_HEAD(&cm
.device_list
);
3479 rwlock_init(&cm
.device_lock
);
3480 spin_lock_init(&cm
.lock
);
3481 cm
.listen_service_table
= RB_ROOT
;
3482 cm
.listen_service_id
= __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3483 cm
.remote_id_table
= RB_ROOT
;
3484 cm
.remote_qp_table
= RB_ROOT
;
3485 cm
.remote_sidr_table
= RB_ROOT
;
3486 idr_init(&cm
.local_id_table
);
3487 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3488 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3489 INIT_LIST_HEAD(&cm
.timewait_list
);
3491 cm
.wq
= create_workqueue("ib_cm");
3495 ret
= ib_register_client(&cm_client
);
3501 destroy_workqueue(cm
.wq
);
3505 static void __exit
ib_cm_cleanup(void)
3507 struct cm_timewait_info
*timewait_info
, *tmp
;
3509 spin_lock_irq(&cm
.lock
);
3510 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3511 cancel_delayed_work(&timewait_info
->work
.work
);
3512 spin_unlock_irq(&cm
.lock
);
3514 destroy_workqueue(cm
.wq
);
3516 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3517 list_del(&timewait_info
->list
);
3518 kfree(timewait_info
);
3521 ib_unregister_client(&cm_client
);
3522 idr_destroy(&cm
.local_id_table
);
3525 module_init(ib_cm_init
);
3526 module_exit(ib_cm_cleanup
);