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/pci.h>
44 #include <linux/random.h>
45 #include <linux/rbtree.h>
46 #include <linux/spinlock.h>
47 #include <linux/workqueue.h>
49 #include <rdma/ib_cache.h>
50 #include <rdma/ib_cm.h>
53 MODULE_AUTHOR("Sean Hefty");
54 MODULE_DESCRIPTION("InfiniBand CM");
55 MODULE_LICENSE("Dual BSD/GPL");
57 static void cm_add_one(struct ib_device
*device
);
58 static void cm_remove_one(struct ib_device
*device
);
60 static struct ib_client cm_client
= {
63 .remove
= cm_remove_one
68 struct list_head device_list
;
70 struct rb_root listen_service_table
;
71 u64 listen_service_id
;
72 /* struct rb_root peer_service_table; todo: fix peer to peer */
73 struct rb_root remote_qp_table
;
74 struct rb_root remote_id_table
;
75 struct rb_root remote_sidr_table
;
76 struct idr local_id_table
;
77 __be32 random_id_operand
;
78 struct list_head timewait_list
;
79 struct workqueue_struct
*wq
;
83 struct cm_device
*cm_dev
;
84 struct ib_mad_agent
*mad_agent
;
89 struct list_head list
;
90 struct ib_device
*device
;
92 struct cm_port port
[0];
98 struct ib_ah_attr ah_attr
;
104 struct work_struct work
;
105 struct list_head list
;
106 struct cm_port
*port
;
107 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
108 __be32 local_id
; /* Established / timewait */
110 struct ib_cm_event cm_event
;
111 struct ib_sa_path_rec path
[0];
114 struct cm_timewait_info
{
115 struct cm_work work
; /* Must be first. */
116 struct list_head list
;
117 struct rb_node remote_qp_node
;
118 struct rb_node remote_id_node
;
119 __be64 remote_ca_guid
;
121 u8 inserted_remote_qp
;
122 u8 inserted_remote_id
;
125 struct cm_id_private
{
128 struct rb_node service_node
;
129 struct rb_node sidr_id_node
;
130 spinlock_t lock
; /* Do not acquire inside cm.lock */
131 struct completion comp
;
134 struct ib_mad_send_buf
*msg
;
135 struct cm_timewait_info
*timewait_info
;
136 /* todo: use alternate port on send failure */
139 struct ib_cm_compare_data
*compare_data
;
145 enum ib_qp_type qp_type
;
149 enum ib_mtu path_mtu
;
153 u8 responder_resources
;
155 u8 local_ack_timeout
;
160 struct list_head work_list
;
164 static void cm_work_handler(void *data
);
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
= kmalloc(private_data_len
, GFP_KERNEL
);
245 return ERR_PTR(-ENOMEM
);
247 memcpy(data
, private_data
, private_data_len
);
251 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
252 void *private_data
, u8 private_data_len
)
254 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
255 kfree(cm_id_priv
->private_data
);
257 cm_id_priv
->private_data
= private_data
;
258 cm_id_priv
->private_data_len
= private_data_len
;
261 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
262 struct ib_grh
*grh
, struct cm_av
*av
)
265 av
->pkey_index
= wc
->pkey_index
;
266 ib_init_ah_from_wc(port
->cm_dev
->device
, port
->port_num
, wc
,
270 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
272 struct cm_device
*cm_dev
;
273 struct cm_port
*port
= NULL
;
278 read_lock_irqsave(&cm
.device_lock
, flags
);
279 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
280 if (!ib_find_cached_gid(cm_dev
->device
, &path
->sgid
,
282 port
= &cm_dev
->port
[p
-1];
286 read_unlock_irqrestore(&cm
.device_lock
, flags
);
291 ret
= ib_find_cached_pkey(cm_dev
->device
, port
->port_num
,
292 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
297 ib_init_ah_from_path(cm_dev
->device
, port
->port_num
, path
,
299 av
->packet_life_time
= path
->packet_life_time
;
303 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
310 spin_lock_irqsave(&cm
.lock
, flags
);
311 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
,
313 spin_unlock_irqrestore(&cm
.lock
, flags
);
314 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
316 cm_id_priv
->id
.local_id
= (__force __be32
) (id
^ cm
.random_id_operand
);
320 static void cm_free_id(__be32 local_id
)
324 spin_lock_irqsave(&cm
.lock
, flags
);
325 idr_remove(&cm
.local_id_table
,
326 (__force
int) (local_id
^ cm
.random_id_operand
));
327 spin_unlock_irqrestore(&cm
.lock
, flags
);
330 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
332 struct cm_id_private
*cm_id_priv
;
334 cm_id_priv
= idr_find(&cm
.local_id_table
,
335 (__force
int) (local_id
^ cm
.random_id_operand
));
337 if (cm_id_priv
->id
.remote_id
== remote_id
)
338 atomic_inc(&cm_id_priv
->refcount
);
346 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
348 struct cm_id_private
*cm_id_priv
;
351 spin_lock_irqsave(&cm
.lock
, flags
);
352 cm_id_priv
= cm_get_id(local_id
, remote_id
);
353 spin_unlock_irqrestore(&cm
.lock
, flags
);
358 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
362 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
363 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
364 ((unsigned long *) mask
)[i
];
367 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
368 struct ib_cm_compare_data
*dst_data
)
370 u8 src
[IB_CM_COMPARE_SIZE
];
371 u8 dst
[IB_CM_COMPARE_SIZE
];
373 if (!src_data
|| !dst_data
)
376 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
377 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
378 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
381 static int cm_compare_private_data(u8
*private_data
,
382 struct ib_cm_compare_data
*dst_data
)
384 u8 src
[IB_CM_COMPARE_SIZE
];
389 cm_mask_copy(src
, private_data
, dst_data
->mask
);
390 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
393 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
395 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
396 struct rb_node
*parent
= NULL
;
397 struct cm_id_private
*cur_cm_id_priv
;
398 __be64 service_id
= cm_id_priv
->id
.service_id
;
399 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
404 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
406 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
407 cur_cm_id_priv
->compare_data
);
408 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
409 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
410 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
412 return cur_cm_id_priv
;
414 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
415 link
= &(*link
)->rb_left
;
416 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
417 link
= &(*link
)->rb_right
;
418 else if (service_id
< cur_cm_id_priv
->id
.service_id
)
419 link
= &(*link
)->rb_left
;
420 else if (service_id
> cur_cm_id_priv
->id
.service_id
)
421 link
= &(*link
)->rb_right
;
422 else if (data_cmp
< 0)
423 link
= &(*link
)->rb_left
;
425 link
= &(*link
)->rb_right
;
427 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
428 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
432 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
436 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
437 struct cm_id_private
*cm_id_priv
;
441 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
442 data_cmp
= cm_compare_private_data(private_data
,
443 cm_id_priv
->compare_data
);
444 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
445 cm_id_priv
->id
.service_id
&&
446 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
449 if (device
< cm_id_priv
->id
.device
)
450 node
= node
->rb_left
;
451 else if (device
> cm_id_priv
->id
.device
)
452 node
= node
->rb_right
;
453 else if (service_id
< cm_id_priv
->id
.service_id
)
454 node
= node
->rb_left
;
455 else if (service_id
> cm_id_priv
->id
.service_id
)
456 node
= node
->rb_right
;
457 else if (data_cmp
< 0)
458 node
= node
->rb_left
;
460 node
= node
->rb_right
;
465 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
468 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
469 struct rb_node
*parent
= NULL
;
470 struct cm_timewait_info
*cur_timewait_info
;
471 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
472 __be32 remote_id
= timewait_info
->work
.remote_id
;
476 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
478 if (remote_id
< cur_timewait_info
->work
.remote_id
)
479 link
= &(*link
)->rb_left
;
480 else if (remote_id
> cur_timewait_info
->work
.remote_id
)
481 link
= &(*link
)->rb_right
;
482 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
483 link
= &(*link
)->rb_left
;
484 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
485 link
= &(*link
)->rb_right
;
487 return cur_timewait_info
;
489 timewait_info
->inserted_remote_id
= 1;
490 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
491 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
495 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
498 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
499 struct cm_timewait_info
*timewait_info
;
502 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
504 if (remote_id
< timewait_info
->work
.remote_id
)
505 node
= node
->rb_left
;
506 else if (remote_id
> timewait_info
->work
.remote_id
)
507 node
= node
->rb_right
;
508 else if (remote_ca_guid
< timewait_info
->remote_ca_guid
)
509 node
= node
->rb_left
;
510 else if (remote_ca_guid
> timewait_info
->remote_ca_guid
)
511 node
= node
->rb_right
;
513 return timewait_info
;
518 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
521 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
522 struct rb_node
*parent
= NULL
;
523 struct cm_timewait_info
*cur_timewait_info
;
524 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
525 __be32 remote_qpn
= timewait_info
->remote_qpn
;
529 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
531 if (remote_qpn
< cur_timewait_info
->remote_qpn
)
532 link
= &(*link
)->rb_left
;
533 else if (remote_qpn
> cur_timewait_info
->remote_qpn
)
534 link
= &(*link
)->rb_right
;
535 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
536 link
= &(*link
)->rb_left
;
537 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
538 link
= &(*link
)->rb_right
;
540 return cur_timewait_info
;
542 timewait_info
->inserted_remote_qp
= 1;
543 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
544 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
548 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
551 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
552 struct rb_node
*parent
= NULL
;
553 struct cm_id_private
*cur_cm_id_priv
;
554 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
555 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
559 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
561 if (remote_id
< cur_cm_id_priv
->id
.remote_id
)
562 link
= &(*link
)->rb_left
;
563 else if (remote_id
> cur_cm_id_priv
->id
.remote_id
)
564 link
= &(*link
)->rb_right
;
567 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
570 link
= &(*link
)->rb_left
;
572 link
= &(*link
)->rb_right
;
574 return cur_cm_id_priv
;
577 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
578 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
582 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
583 enum ib_cm_sidr_status status
)
585 struct ib_cm_sidr_rep_param param
;
587 memset(¶m
, 0, sizeof param
);
588 param
.status
= status
;
589 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
592 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
593 ib_cm_handler cm_handler
,
596 struct cm_id_private
*cm_id_priv
;
599 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
601 return ERR_PTR(-ENOMEM
);
603 cm_id_priv
->id
.state
= IB_CM_IDLE
;
604 cm_id_priv
->id
.device
= device
;
605 cm_id_priv
->id
.cm_handler
= cm_handler
;
606 cm_id_priv
->id
.context
= context
;
607 cm_id_priv
->id
.remote_cm_qpn
= 1;
608 ret
= cm_alloc_id(cm_id_priv
);
612 spin_lock_init(&cm_id_priv
->lock
);
613 init_completion(&cm_id_priv
->comp
);
614 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
615 atomic_set(&cm_id_priv
->work_count
, -1);
616 atomic_set(&cm_id_priv
->refcount
, 1);
617 return &cm_id_priv
->id
;
621 return ERR_PTR(-ENOMEM
);
623 EXPORT_SYMBOL(ib_create_cm_id
);
625 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
627 struct cm_work
*work
;
629 if (list_empty(&cm_id_priv
->work_list
))
632 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
633 list_del(&work
->list
);
637 static void cm_free_work(struct cm_work
*work
)
639 if (work
->mad_recv_wc
)
640 ib_free_recv_mad(work
->mad_recv_wc
);
644 static inline int cm_convert_to_ms(int iba_time
)
646 /* approximate conversion to ms from 4.096us x 2^iba_time */
647 return 1 << max(iba_time
- 8, 0);
650 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
652 if (timewait_info
->inserted_remote_id
) {
653 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
654 timewait_info
->inserted_remote_id
= 0;
657 if (timewait_info
->inserted_remote_qp
) {
658 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
659 timewait_info
->inserted_remote_qp
= 0;
663 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
665 struct cm_timewait_info
*timewait_info
;
667 timewait_info
= kzalloc(sizeof *timewait_info
, GFP_KERNEL
);
669 return ERR_PTR(-ENOMEM
);
671 timewait_info
->work
.local_id
= local_id
;
672 INIT_WORK(&timewait_info
->work
.work
, cm_work_handler
,
673 &timewait_info
->work
);
674 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
675 return timewait_info
;
678 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
683 spin_lock_irqsave(&cm
.lock
, flags
);
684 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
685 list_add_tail(&cm_id_priv
->timewait_info
->list
, &cm
.timewait_list
);
686 spin_unlock_irqrestore(&cm
.lock
, flags
);
689 * The cm_id could be destroyed by the user before we exit timewait.
690 * To protect against this, we search for the cm_id after exiting
691 * timewait before notifying the user that we've exited timewait.
693 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
694 wait_time
= cm_convert_to_ms(cm_id_priv
->local_ack_timeout
);
695 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
696 msecs_to_jiffies(wait_time
));
697 cm_id_priv
->timewait_info
= NULL
;
700 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
704 cm_id_priv
->id
.state
= IB_CM_IDLE
;
705 if (cm_id_priv
->timewait_info
) {
706 spin_lock_irqsave(&cm
.lock
, flags
);
707 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
708 spin_unlock_irqrestore(&cm
.lock
, flags
);
709 kfree(cm_id_priv
->timewait_info
);
710 cm_id_priv
->timewait_info
= NULL
;
714 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
716 struct cm_id_private
*cm_id_priv
;
717 struct cm_work
*work
;
720 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
722 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
723 switch (cm_id
->state
) {
725 cm_id
->state
= IB_CM_IDLE
;
726 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
727 spin_lock_irqsave(&cm
.lock
, flags
);
728 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
729 spin_unlock_irqrestore(&cm
.lock
, flags
);
731 case IB_CM_SIDR_REQ_SENT
:
732 cm_id
->state
= IB_CM_IDLE
;
733 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
734 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
736 case IB_CM_SIDR_REQ_RCVD
:
737 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
738 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
741 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
742 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
743 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
744 &cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
745 sizeof cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
749 if (err
== -ENOMEM
) {
750 /* Do not reject to allow future retries. */
751 cm_reset_to_idle(cm_id_priv
);
752 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
754 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
755 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
759 case IB_CM_MRA_REQ_RCVD
:
761 case IB_CM_MRA_REP_RCVD
:
762 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
764 case IB_CM_MRA_REQ_SENT
:
766 case IB_CM_MRA_REP_SENT
:
767 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
768 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
771 case IB_CM_ESTABLISHED
:
772 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
773 ib_send_cm_dreq(cm_id
, NULL
, 0);
775 case IB_CM_DREQ_SENT
:
776 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
777 cm_enter_timewait(cm_id_priv
);
778 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
780 case IB_CM_DREQ_RCVD
:
781 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
782 ib_send_cm_drep(cm_id
, NULL
, 0);
785 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
789 cm_free_id(cm_id
->local_id
);
790 cm_deref_id(cm_id_priv
);
791 wait_for_completion(&cm_id_priv
->comp
);
792 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
794 kfree(cm_id_priv
->compare_data
);
795 kfree(cm_id_priv
->private_data
);
799 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
801 cm_destroy_id(cm_id
, 0);
803 EXPORT_SYMBOL(ib_destroy_cm_id
);
805 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
806 struct ib_cm_compare_data
*compare_data
)
808 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
812 service_mask
= service_mask
? service_mask
:
813 __constant_cpu_to_be64(~0ULL);
814 service_id
&= service_mask
;
815 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
816 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
819 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
820 if (cm_id
->state
!= IB_CM_IDLE
)
824 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
826 if (!cm_id_priv
->compare_data
)
828 cm_mask_copy(cm_id_priv
->compare_data
->data
,
829 compare_data
->data
, compare_data
->mask
);
830 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
834 cm_id
->state
= IB_CM_LISTEN
;
836 spin_lock_irqsave(&cm
.lock
, flags
);
837 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
838 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
839 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
841 cm_id
->service_id
= service_id
;
842 cm_id
->service_mask
= service_mask
;
844 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
845 spin_unlock_irqrestore(&cm
.lock
, flags
);
847 if (cur_cm_id_priv
) {
848 cm_id
->state
= IB_CM_IDLE
;
849 kfree(cm_id_priv
->compare_data
);
850 cm_id_priv
->compare_data
= NULL
;
855 EXPORT_SYMBOL(ib_cm_listen
);
857 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
858 enum cm_msg_sequence msg_seq
)
862 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
863 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
865 return cpu_to_be64(hi_tid
| low_tid
);
868 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
869 __be16 attr_id
, __be64 tid
)
871 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
872 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
873 hdr
->class_version
= IB_CM_CLASS_VERSION
;
874 hdr
->method
= IB_MGMT_METHOD_SEND
;
875 hdr
->attr_id
= attr_id
;
879 static void cm_format_req(struct cm_req_msg
*req_msg
,
880 struct cm_id_private
*cm_id_priv
,
881 struct ib_cm_req_param
*param
)
883 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
884 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
886 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
887 req_msg
->service_id
= param
->service_id
;
888 req_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
889 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
890 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
891 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
892 cm_req_set_remote_resp_timeout(req_msg
,
893 param
->remote_cm_response_timeout
);
894 cm_req_set_qp_type(req_msg
, param
->qp_type
);
895 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
896 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
897 cm_req_set_local_resp_timeout(req_msg
,
898 param
->local_cm_response_timeout
);
899 cm_req_set_retry_count(req_msg
, param
->retry_count
);
900 req_msg
->pkey
= param
->primary_path
->pkey
;
901 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
902 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
903 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
904 cm_req_set_srq(req_msg
, param
->srq
);
906 req_msg
->primary_local_lid
= param
->primary_path
->slid
;
907 req_msg
->primary_remote_lid
= param
->primary_path
->dlid
;
908 req_msg
->primary_local_gid
= param
->primary_path
->sgid
;
909 req_msg
->primary_remote_gid
= param
->primary_path
->dgid
;
910 cm_req_set_primary_flow_label(req_msg
, param
->primary_path
->flow_label
);
911 cm_req_set_primary_packet_rate(req_msg
, param
->primary_path
->rate
);
912 req_msg
->primary_traffic_class
= param
->primary_path
->traffic_class
;
913 req_msg
->primary_hop_limit
= param
->primary_path
->hop_limit
;
914 cm_req_set_primary_sl(req_msg
, param
->primary_path
->sl
);
915 cm_req_set_primary_subnet_local(req_msg
, 1); /* local only... */
916 cm_req_set_primary_local_ack_timeout(req_msg
,
917 min(31, param
->primary_path
->packet_life_time
+ 1));
919 if (param
->alternate_path
) {
920 req_msg
->alt_local_lid
= param
->alternate_path
->slid
;
921 req_msg
->alt_remote_lid
= param
->alternate_path
->dlid
;
922 req_msg
->alt_local_gid
= param
->alternate_path
->sgid
;
923 req_msg
->alt_remote_gid
= param
->alternate_path
->dgid
;
924 cm_req_set_alt_flow_label(req_msg
,
925 param
->alternate_path
->flow_label
);
926 cm_req_set_alt_packet_rate(req_msg
, param
->alternate_path
->rate
);
927 req_msg
->alt_traffic_class
= param
->alternate_path
->traffic_class
;
928 req_msg
->alt_hop_limit
= param
->alternate_path
->hop_limit
;
929 cm_req_set_alt_sl(req_msg
, param
->alternate_path
->sl
);
930 cm_req_set_alt_subnet_local(req_msg
, 1); /* local only... */
931 cm_req_set_alt_local_ack_timeout(req_msg
,
932 min(31, param
->alternate_path
->packet_life_time
+ 1));
935 if (param
->private_data
&& param
->private_data_len
)
936 memcpy(req_msg
->private_data
, param
->private_data
,
937 param
->private_data_len
);
940 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
942 /* peer-to-peer not supported */
943 if (param
->peer_to_peer
)
946 if (!param
->primary_path
)
949 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
952 if (param
->private_data
&&
953 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
956 if (param
->alternate_path
&&
957 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
958 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
964 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
965 struct ib_cm_req_param
*param
)
967 struct cm_id_private
*cm_id_priv
;
968 struct cm_req_msg
*req_msg
;
972 ret
= cm_validate_req_param(param
);
976 /* Verify that we're not in timewait. */
977 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
978 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
979 if (cm_id
->state
!= IB_CM_IDLE
) {
980 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
984 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
986 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
988 if (IS_ERR(cm_id_priv
->timewait_info
)) {
989 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
993 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
996 if (param
->alternate_path
) {
997 ret
= cm_init_av_by_path(param
->alternate_path
,
998 &cm_id_priv
->alt_av
);
1002 cm_id
->service_id
= param
->service_id
;
1003 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
1004 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1005 param
->primary_path
->packet_life_time
) * 2 +
1007 param
->remote_cm_response_timeout
);
1008 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
1009 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1010 cm_id_priv
->responder_resources
= param
->responder_resources
;
1011 cm_id_priv
->retry_count
= param
->retry_count
;
1012 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1013 cm_id_priv
->qp_type
= param
->qp_type
;
1015 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1019 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1020 cm_format_req(req_msg
, cm_id_priv
, param
);
1021 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1022 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1023 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1025 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1026 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1027 cm_id_priv
->local_ack_timeout
=
1028 cm_req_get_primary_local_ack_timeout(req_msg
);
1030 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1031 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1033 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1036 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1037 cm_id
->state
= IB_CM_REQ_SENT
;
1038 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1041 error2
: cm_free_msg(cm_id_priv
->msg
);
1042 error1
: kfree(cm_id_priv
->timewait_info
);
1045 EXPORT_SYMBOL(ib_send_cm_req
);
1047 static int cm_issue_rej(struct cm_port
*port
,
1048 struct ib_mad_recv_wc
*mad_recv_wc
,
1049 enum ib_cm_rej_reason reason
,
1050 enum cm_msg_response msg_rejected
,
1051 void *ari
, u8 ari_length
)
1053 struct ib_mad_send_buf
*msg
= NULL
;
1054 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1057 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1061 /* We just need common CM header information. Cast to any message. */
1062 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1063 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1065 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1066 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1067 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1068 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1069 rej_msg
->reason
= cpu_to_be16(reason
);
1071 if (ari
&& ari_length
) {
1072 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1073 memcpy(rej_msg
->ari
, ari
, ari_length
);
1076 ret
= ib_post_send_mad(msg
, NULL
);
1083 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1084 __be32 local_qpn
, __be32 remote_qpn
)
1086 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1087 ((local_ca_guid
== remote_ca_guid
) &&
1088 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1091 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1092 struct ib_sa_path_rec
*primary_path
,
1093 struct ib_sa_path_rec
*alt_path
)
1095 memset(primary_path
, 0, sizeof *primary_path
);
1096 primary_path
->dgid
= req_msg
->primary_local_gid
;
1097 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1098 primary_path
->dlid
= req_msg
->primary_local_lid
;
1099 primary_path
->slid
= req_msg
->primary_remote_lid
;
1100 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1101 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1102 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1103 primary_path
->reversible
= 1;
1104 primary_path
->pkey
= req_msg
->pkey
;
1105 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1106 primary_path
->mtu_selector
= IB_SA_EQ
;
1107 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1108 primary_path
->rate_selector
= IB_SA_EQ
;
1109 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1110 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1111 primary_path
->packet_life_time
=
1112 cm_req_get_primary_local_ack_timeout(req_msg
);
1113 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1115 if (req_msg
->alt_local_lid
) {
1116 memset(alt_path
, 0, sizeof *alt_path
);
1117 alt_path
->dgid
= req_msg
->alt_local_gid
;
1118 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1119 alt_path
->dlid
= req_msg
->alt_local_lid
;
1120 alt_path
->slid
= req_msg
->alt_remote_lid
;
1121 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1122 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1123 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1124 alt_path
->reversible
= 1;
1125 alt_path
->pkey
= req_msg
->pkey
;
1126 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1127 alt_path
->mtu_selector
= IB_SA_EQ
;
1128 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1129 alt_path
->rate_selector
= IB_SA_EQ
;
1130 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1131 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1132 alt_path
->packet_life_time
=
1133 cm_req_get_alt_local_ack_timeout(req_msg
);
1134 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1138 static void cm_format_req_event(struct cm_work
*work
,
1139 struct cm_id_private
*cm_id_priv
,
1140 struct ib_cm_id
*listen_id
)
1142 struct cm_req_msg
*req_msg
;
1143 struct ib_cm_req_event_param
*param
;
1145 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1146 param
= &work
->cm_event
.param
.req_rcvd
;
1147 param
->listen_id
= listen_id
;
1148 param
->port
= cm_id_priv
->av
.port
->port_num
;
1149 param
->primary_path
= &work
->path
[0];
1150 if (req_msg
->alt_local_lid
)
1151 param
->alternate_path
= &work
->path
[1];
1153 param
->alternate_path
= NULL
;
1154 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1155 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1156 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1157 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1158 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1159 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1160 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1161 param
->local_cm_response_timeout
=
1162 cm_req_get_remote_resp_timeout(req_msg
);
1163 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1164 param
->remote_cm_response_timeout
=
1165 cm_req_get_local_resp_timeout(req_msg
);
1166 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1167 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1168 param
->srq
= cm_req_get_srq(req_msg
);
1169 work
->cm_event
.private_data
= &req_msg
->private_data
;
1172 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1173 struct cm_work
*work
)
1175 unsigned long flags
;
1178 /* We will typically only have the current event to report. */
1179 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1182 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1183 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1184 work
= cm_dequeue_work(cm_id_priv
);
1185 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1187 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1191 cm_deref_id(cm_id_priv
);
1193 cm_destroy_id(&cm_id_priv
->id
, ret
);
1196 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1197 struct cm_id_private
*cm_id_priv
,
1198 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1199 const void *private_data
, u8 private_data_len
)
1201 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1202 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1203 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1204 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1205 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1207 if (private_data
&& private_data_len
)
1208 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1211 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1212 struct cm_id_private
*cm_id_priv
,
1213 enum ib_cm_rej_reason reason
,
1216 const void *private_data
,
1217 u8 private_data_len
)
1219 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1220 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1222 switch(cm_id_priv
->id
.state
) {
1223 case IB_CM_REQ_RCVD
:
1224 rej_msg
->local_comm_id
= 0;
1225 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1227 case IB_CM_MRA_REQ_SENT
:
1228 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1229 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1231 case IB_CM_REP_RCVD
:
1232 case IB_CM_MRA_REP_SENT
:
1233 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1234 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1237 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1238 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1242 rej_msg
->reason
= cpu_to_be16(reason
);
1243 if (ari
&& ari_length
) {
1244 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1245 memcpy(rej_msg
->ari
, ari
, ari_length
);
1248 if (private_data
&& private_data_len
)
1249 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1252 static void cm_dup_req_handler(struct cm_work
*work
,
1253 struct cm_id_private
*cm_id_priv
)
1255 struct ib_mad_send_buf
*msg
= NULL
;
1256 unsigned long flags
;
1259 /* Quick state check to discard duplicate REQs. */
1260 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1263 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1267 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1268 switch (cm_id_priv
->id
.state
) {
1269 case IB_CM_MRA_REQ_SENT
:
1270 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1271 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1272 cm_id_priv
->private_data
,
1273 cm_id_priv
->private_data_len
);
1275 case IB_CM_TIMEWAIT
:
1276 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1277 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1282 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1284 ret
= ib_post_send_mad(msg
, NULL
);
1289 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1290 free
: cm_free_msg(msg
);
1293 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1294 struct cm_id_private
*cm_id_priv
)
1296 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1297 struct cm_timewait_info
*timewait_info
;
1298 struct cm_req_msg
*req_msg
;
1299 unsigned long flags
;
1301 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1303 /* Check for duplicate REQ and stale connections. */
1304 spin_lock_irqsave(&cm
.lock
, flags
);
1305 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1307 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1309 if (timewait_info
) {
1310 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1311 timewait_info
->work
.remote_id
);
1312 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1313 spin_unlock_irqrestore(&cm
.lock
, flags
);
1314 if (cur_cm_id_priv
) {
1315 cm_dup_req_handler(work
, cur_cm_id_priv
);
1316 cm_deref_id(cur_cm_id_priv
);
1318 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1319 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1321 listen_cm_id_priv
= NULL
;
1325 /* Find matching listen request. */
1326 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1327 req_msg
->service_id
,
1328 req_msg
->private_data
);
1329 if (!listen_cm_id_priv
) {
1330 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1331 spin_unlock_irqrestore(&cm
.lock
, flags
);
1332 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1333 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1337 atomic_inc(&listen_cm_id_priv
->refcount
);
1338 atomic_inc(&cm_id_priv
->refcount
);
1339 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1340 atomic_inc(&cm_id_priv
->work_count
);
1341 spin_unlock_irqrestore(&cm
.lock
, flags
);
1343 return listen_cm_id_priv
;
1346 static int cm_req_handler(struct cm_work
*work
)
1348 struct ib_cm_id
*cm_id
;
1349 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1350 struct cm_req_msg
*req_msg
;
1353 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1355 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
1357 return PTR_ERR(cm_id
);
1359 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1360 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1361 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1362 work
->mad_recv_wc
->recv_buf
.grh
,
1364 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1366 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1367 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1370 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1371 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1372 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1374 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1375 if (!listen_cm_id_priv
) {
1377 kfree(cm_id_priv
->timewait_info
);
1381 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1382 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1383 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1384 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
1386 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1387 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1389 ib_get_cached_gid(work
->port
->cm_dev
->device
,
1390 work
->port
->port_num
, 0, &work
->path
[0].sgid
);
1391 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_GID
,
1392 &work
->path
[0].sgid
, sizeof work
->path
[0].sgid
,
1396 if (req_msg
->alt_local_lid
) {
1397 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1399 ib_send_cm_rej(cm_id
, IB_CM_REJ_INVALID_ALT_GID
,
1400 &work
->path
[0].sgid
,
1401 sizeof work
->path
[0].sgid
, NULL
, 0);
1405 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1406 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1407 cm_req_get_local_resp_timeout(req_msg
));
1408 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1409 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1410 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1411 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1412 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1413 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1414 cm_id_priv
->local_ack_timeout
=
1415 cm_req_get_primary_local_ack_timeout(req_msg
);
1416 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1417 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1418 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1420 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1421 cm_process_work(cm_id_priv
, work
);
1422 cm_deref_id(listen_cm_id_priv
);
1426 atomic_dec(&cm_id_priv
->refcount
);
1427 cm_deref_id(listen_cm_id_priv
);
1429 ib_destroy_cm_id(cm_id
);
1433 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1434 struct cm_id_private
*cm_id_priv
,
1435 struct ib_cm_rep_param
*param
)
1437 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1438 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1439 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1440 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1441 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1442 rep_msg
->resp_resources
= param
->responder_resources
;
1443 rep_msg
->initiator_depth
= param
->initiator_depth
;
1444 cm_rep_set_target_ack_delay(rep_msg
, param
->target_ack_delay
);
1445 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1446 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1447 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1448 cm_rep_set_srq(rep_msg
, param
->srq
);
1449 rep_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
1451 if (param
->private_data
&& param
->private_data_len
)
1452 memcpy(rep_msg
->private_data
, param
->private_data
,
1453 param
->private_data_len
);
1456 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1457 struct ib_cm_rep_param
*param
)
1459 struct cm_id_private
*cm_id_priv
;
1460 struct ib_mad_send_buf
*msg
;
1461 struct cm_rep_msg
*rep_msg
;
1462 unsigned long flags
;
1465 if (param
->private_data
&&
1466 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1469 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1470 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1471 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1472 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1477 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1481 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1482 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1483 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1484 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1486 ret
= ib_post_send_mad(msg
, NULL
);
1488 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1493 cm_id
->state
= IB_CM_REP_SENT
;
1494 cm_id_priv
->msg
= msg
;
1495 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1496 cm_id_priv
->responder_resources
= param
->responder_resources
;
1497 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1498 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1500 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1503 EXPORT_SYMBOL(ib_send_cm_rep
);
1505 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1506 struct cm_id_private
*cm_id_priv
,
1507 const void *private_data
,
1508 u8 private_data_len
)
1510 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1511 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1512 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1514 if (private_data
&& private_data_len
)
1515 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1518 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1519 const void *private_data
,
1520 u8 private_data_len
)
1522 struct cm_id_private
*cm_id_priv
;
1523 struct ib_mad_send_buf
*msg
;
1524 unsigned long flags
;
1528 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1531 data
= cm_copy_private_data(private_data
, private_data_len
);
1533 return PTR_ERR(data
);
1535 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1536 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1537 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1538 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1543 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1547 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1548 private_data
, private_data_len
);
1550 ret
= ib_post_send_mad(msg
, NULL
);
1552 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1558 cm_id
->state
= IB_CM_ESTABLISHED
;
1559 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1560 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1563 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1567 EXPORT_SYMBOL(ib_send_cm_rtu
);
1569 static void cm_format_rep_event(struct cm_work
*work
)
1571 struct cm_rep_msg
*rep_msg
;
1572 struct ib_cm_rep_event_param
*param
;
1574 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1575 param
= &work
->cm_event
.param
.rep_rcvd
;
1576 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1577 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1578 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1579 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1580 param
->responder_resources
= rep_msg
->initiator_depth
;
1581 param
->initiator_depth
= rep_msg
->resp_resources
;
1582 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1583 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1584 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1585 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1586 param
->srq
= cm_rep_get_srq(rep_msg
);
1587 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1590 static void cm_dup_rep_handler(struct cm_work
*work
)
1592 struct cm_id_private
*cm_id_priv
;
1593 struct cm_rep_msg
*rep_msg
;
1594 struct ib_mad_send_buf
*msg
= NULL
;
1595 unsigned long flags
;
1598 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1599 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1600 rep_msg
->local_comm_id
);
1604 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1608 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1609 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1610 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1611 cm_id_priv
->private_data
,
1612 cm_id_priv
->private_data_len
);
1613 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1614 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1615 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1616 cm_id_priv
->private_data
,
1617 cm_id_priv
->private_data_len
);
1620 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1622 ret
= ib_post_send_mad(msg
, NULL
);
1627 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1628 free
: cm_free_msg(msg
);
1629 deref
: cm_deref_id(cm_id_priv
);
1632 static int cm_rep_handler(struct cm_work
*work
)
1634 struct cm_id_private
*cm_id_priv
;
1635 struct cm_rep_msg
*rep_msg
;
1636 unsigned long flags
;
1639 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1640 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1642 cm_dup_rep_handler(work
);
1646 cm_format_rep_event(work
);
1648 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1649 switch (cm_id_priv
->id
.state
) {
1650 case IB_CM_REQ_SENT
:
1651 case IB_CM_MRA_REQ_RCVD
:
1654 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1659 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1660 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1661 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1663 spin_lock(&cm
.lock
);
1664 /* Check for duplicate REP. */
1665 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1666 spin_unlock(&cm
.lock
);
1667 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1671 /* Check for a stale connection. */
1672 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1673 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1674 &cm
.remote_id_table
);
1675 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1676 spin_unlock(&cm
.lock
);
1677 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1678 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1679 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1684 spin_unlock(&cm
.lock
);
1686 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1687 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1688 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1689 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1690 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1691 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1692 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1694 /* todo: handle peer_to_peer */
1696 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1697 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1699 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1700 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1703 cm_process_work(cm_id_priv
, work
);
1705 cm_deref_id(cm_id_priv
);
1709 cm_deref_id(cm_id_priv
);
1713 static int cm_establish_handler(struct cm_work
*work
)
1715 struct cm_id_private
*cm_id_priv
;
1716 unsigned long flags
;
1719 /* See comment in ib_cm_establish about lookup. */
1720 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1724 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1725 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1726 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1730 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1731 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1733 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1734 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1737 cm_process_work(cm_id_priv
, work
);
1739 cm_deref_id(cm_id_priv
);
1742 cm_deref_id(cm_id_priv
);
1746 static int cm_rtu_handler(struct cm_work
*work
)
1748 struct cm_id_private
*cm_id_priv
;
1749 struct cm_rtu_msg
*rtu_msg
;
1750 unsigned long flags
;
1753 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1754 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1755 rtu_msg
->local_comm_id
);
1759 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1761 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1762 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1763 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1764 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1767 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1769 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1770 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1772 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1773 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1776 cm_process_work(cm_id_priv
, work
);
1778 cm_deref_id(cm_id_priv
);
1781 cm_deref_id(cm_id_priv
);
1785 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1786 struct cm_id_private
*cm_id_priv
,
1787 const void *private_data
,
1788 u8 private_data_len
)
1790 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1791 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1792 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1793 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1794 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1796 if (private_data
&& private_data_len
)
1797 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1800 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1801 const void *private_data
,
1802 u8 private_data_len
)
1804 struct cm_id_private
*cm_id_priv
;
1805 struct ib_mad_send_buf
*msg
;
1806 unsigned long flags
;
1809 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1812 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1813 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1814 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1819 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1821 cm_enter_timewait(cm_id_priv
);
1825 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
1826 private_data
, private_data_len
);
1827 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1828 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
1830 ret
= ib_post_send_mad(msg
, NULL
);
1832 cm_enter_timewait(cm_id_priv
);
1833 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1838 cm_id
->state
= IB_CM_DREQ_SENT
;
1839 cm_id_priv
->msg
= msg
;
1840 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1843 EXPORT_SYMBOL(ib_send_cm_dreq
);
1845 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
1846 struct cm_id_private
*cm_id_priv
,
1847 const void *private_data
,
1848 u8 private_data_len
)
1850 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
1851 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1852 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1854 if (private_data
&& private_data_len
)
1855 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
1858 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
1859 const void *private_data
,
1860 u8 private_data_len
)
1862 struct cm_id_private
*cm_id_priv
;
1863 struct ib_mad_send_buf
*msg
;
1864 unsigned long flags
;
1868 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
1871 data
= cm_copy_private_data(private_data
, private_data_len
);
1873 return PTR_ERR(data
);
1875 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1876 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1877 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
1878 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1883 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1884 cm_enter_timewait(cm_id_priv
);
1886 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1890 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1891 private_data
, private_data_len
);
1893 ret
= ib_post_send_mad(msg
, NULL
);
1895 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1900 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1903 EXPORT_SYMBOL(ib_send_cm_drep
);
1905 static int cm_dreq_handler(struct cm_work
*work
)
1907 struct cm_id_private
*cm_id_priv
;
1908 struct cm_dreq_msg
*dreq_msg
;
1909 struct ib_mad_send_buf
*msg
= NULL
;
1910 unsigned long flags
;
1913 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1914 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
1915 dreq_msg
->local_comm_id
);
1919 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
1921 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1922 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
1925 switch (cm_id_priv
->id
.state
) {
1926 case IB_CM_REP_SENT
:
1927 case IB_CM_DREQ_SENT
:
1928 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1930 case IB_CM_ESTABLISHED
:
1931 case IB_CM_MRA_REP_RCVD
:
1933 case IB_CM_TIMEWAIT
:
1934 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
1937 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1938 cm_id_priv
->private_data
,
1939 cm_id_priv
->private_data_len
);
1940 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1942 if (ib_post_send_mad(msg
, NULL
))
1948 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
1949 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
1950 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1952 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1953 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1956 cm_process_work(cm_id_priv
, work
);
1958 cm_deref_id(cm_id_priv
);
1961 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1962 deref
: cm_deref_id(cm_id_priv
);
1966 static int cm_drep_handler(struct cm_work
*work
)
1968 struct cm_id_private
*cm_id_priv
;
1969 struct cm_drep_msg
*drep_msg
;
1970 unsigned long flags
;
1973 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1974 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
1975 drep_msg
->local_comm_id
);
1979 work
->cm_event
.private_data
= &drep_msg
->private_data
;
1981 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1982 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
1983 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
1984 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1987 cm_enter_timewait(cm_id_priv
);
1989 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1990 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1992 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1993 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1996 cm_process_work(cm_id_priv
, work
);
1998 cm_deref_id(cm_id_priv
);
2001 cm_deref_id(cm_id_priv
);
2005 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
2006 enum ib_cm_rej_reason reason
,
2009 const void *private_data
,
2010 u8 private_data_len
)
2012 struct cm_id_private
*cm_id_priv
;
2013 struct ib_mad_send_buf
*msg
;
2014 unsigned long flags
;
2017 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
2018 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
2021 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2023 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2024 switch (cm_id
->state
) {
2025 case IB_CM_REQ_SENT
:
2026 case IB_CM_MRA_REQ_RCVD
:
2027 case IB_CM_REQ_RCVD
:
2028 case IB_CM_MRA_REQ_SENT
:
2029 case IB_CM_REP_RCVD
:
2030 case IB_CM_MRA_REP_SENT
:
2031 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2033 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2034 cm_id_priv
, reason
, ari
, ari_length
,
2035 private_data
, private_data_len
);
2037 cm_reset_to_idle(cm_id_priv
);
2039 case IB_CM_REP_SENT
:
2040 case IB_CM_MRA_REP_RCVD
:
2041 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2043 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2044 cm_id_priv
, reason
, ari
, ari_length
,
2045 private_data
, private_data_len
);
2047 cm_enter_timewait(cm_id_priv
);
2057 ret
= ib_post_send_mad(msg
, NULL
);
2061 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2064 EXPORT_SYMBOL(ib_send_cm_rej
);
2066 static void cm_format_rej_event(struct cm_work
*work
)
2068 struct cm_rej_msg
*rej_msg
;
2069 struct ib_cm_rej_event_param
*param
;
2071 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2072 param
= &work
->cm_event
.param
.rej_rcvd
;
2073 param
->ari
= rej_msg
->ari
;
2074 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2075 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2076 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2079 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2081 struct cm_timewait_info
*timewait_info
;
2082 struct cm_id_private
*cm_id_priv
;
2083 unsigned long flags
;
2086 remote_id
= rej_msg
->local_comm_id
;
2088 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2089 spin_lock_irqsave(&cm
.lock
, flags
);
2090 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2092 if (!timewait_info
) {
2093 spin_unlock_irqrestore(&cm
.lock
, flags
);
2096 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int)
2097 (timewait_info
->work
.local_id
^
2098 cm
.random_id_operand
));
2100 if (cm_id_priv
->id
.remote_id
== remote_id
)
2101 atomic_inc(&cm_id_priv
->refcount
);
2105 spin_unlock_irqrestore(&cm
.lock
, flags
);
2106 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2107 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2109 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2114 static int cm_rej_handler(struct cm_work
*work
)
2116 struct cm_id_private
*cm_id_priv
;
2117 struct cm_rej_msg
*rej_msg
;
2118 unsigned long flags
;
2121 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2122 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2126 cm_format_rej_event(work
);
2128 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2129 switch (cm_id_priv
->id
.state
) {
2130 case IB_CM_REQ_SENT
:
2131 case IB_CM_MRA_REQ_RCVD
:
2132 case IB_CM_REP_SENT
:
2133 case IB_CM_MRA_REP_RCVD
:
2134 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2136 case IB_CM_REQ_RCVD
:
2137 case IB_CM_MRA_REQ_SENT
:
2138 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2139 cm_enter_timewait(cm_id_priv
);
2141 cm_reset_to_idle(cm_id_priv
);
2143 case IB_CM_DREQ_SENT
:
2144 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2146 case IB_CM_REP_RCVD
:
2147 case IB_CM_MRA_REP_SENT
:
2148 case IB_CM_ESTABLISHED
:
2149 cm_enter_timewait(cm_id_priv
);
2152 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2157 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2159 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2160 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2163 cm_process_work(cm_id_priv
, work
);
2165 cm_deref_id(cm_id_priv
);
2168 cm_deref_id(cm_id_priv
);
2172 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2174 const void *private_data
,
2175 u8 private_data_len
)
2177 struct cm_id_private
*cm_id_priv
;
2178 struct ib_mad_send_buf
*msg
;
2180 unsigned long flags
;
2183 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2186 data
= cm_copy_private_data(private_data
, private_data_len
);
2188 return PTR_ERR(data
);
2190 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2192 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2193 switch(cm_id_priv
->id
.state
) {
2194 case IB_CM_REQ_RCVD
:
2195 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2199 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2200 CM_MSG_RESPONSE_REQ
, service_timeout
,
2201 private_data
, private_data_len
);
2202 ret
= ib_post_send_mad(msg
, NULL
);
2205 cm_id
->state
= IB_CM_MRA_REQ_SENT
;
2207 case IB_CM_REP_RCVD
:
2208 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2212 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2213 CM_MSG_RESPONSE_REP
, service_timeout
,
2214 private_data
, private_data_len
);
2215 ret
= ib_post_send_mad(msg
, NULL
);
2218 cm_id
->state
= IB_CM_MRA_REP_SENT
;
2220 case IB_CM_ESTABLISHED
:
2221 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2225 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2226 CM_MSG_RESPONSE_OTHER
, service_timeout
,
2227 private_data
, private_data_len
);
2228 ret
= ib_post_send_mad(msg
, NULL
);
2231 cm_id
->lap_state
= IB_CM_MRA_LAP_SENT
;
2237 cm_id_priv
->service_timeout
= service_timeout
;
2238 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2239 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2242 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2246 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2251 EXPORT_SYMBOL(ib_send_cm_mra
);
2253 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2255 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2256 case CM_MSG_RESPONSE_REQ
:
2257 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2258 case CM_MSG_RESPONSE_REP
:
2259 case CM_MSG_RESPONSE_OTHER
:
2260 return cm_acquire_id(mra_msg
->remote_comm_id
,
2261 mra_msg
->local_comm_id
);
2267 static int cm_mra_handler(struct cm_work
*work
)
2269 struct cm_id_private
*cm_id_priv
;
2270 struct cm_mra_msg
*mra_msg
;
2271 unsigned long flags
;
2274 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2275 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2279 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2280 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2281 cm_mra_get_service_timeout(mra_msg
);
2282 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2283 cm_convert_to_ms(cm_id_priv
->av
.packet_life_time
);
2285 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2286 switch (cm_id_priv
->id
.state
) {
2287 case IB_CM_REQ_SENT
:
2288 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2289 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2290 cm_id_priv
->msg
, timeout
))
2292 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2294 case IB_CM_REP_SENT
:
2295 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2296 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2297 cm_id_priv
->msg
, timeout
))
2299 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2301 case IB_CM_ESTABLISHED
:
2302 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2303 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2304 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2305 cm_id_priv
->msg
, timeout
))
2307 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2313 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2314 cm_id_priv
->id
.state
;
2315 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2317 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2318 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2321 cm_process_work(cm_id_priv
, work
);
2323 cm_deref_id(cm_id_priv
);
2326 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2327 cm_deref_id(cm_id_priv
);
2331 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2332 struct cm_id_private
*cm_id_priv
,
2333 struct ib_sa_path_rec
*alternate_path
,
2334 const void *private_data
,
2335 u8 private_data_len
)
2337 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2338 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2339 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2340 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2341 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2342 /* todo: need remote CM response timeout */
2343 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2344 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2345 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2346 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2347 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2348 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2349 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2350 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2351 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2352 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2353 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2354 cm_lap_set_local_ack_timeout(lap_msg
,
2355 min(31, alternate_path
->packet_life_time
+ 1));
2357 if (private_data
&& private_data_len
)
2358 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2361 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2362 struct ib_sa_path_rec
*alternate_path
,
2363 const void *private_data
,
2364 u8 private_data_len
)
2366 struct cm_id_private
*cm_id_priv
;
2367 struct ib_mad_send_buf
*msg
;
2368 unsigned long flags
;
2371 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2374 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2375 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2376 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2377 cm_id
->lap_state
!= IB_CM_LAP_IDLE
) {
2382 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2386 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2387 alternate_path
, private_data
, private_data_len
);
2388 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2389 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2391 ret
= ib_post_send_mad(msg
, NULL
);
2393 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2398 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2399 cm_id_priv
->msg
= msg
;
2401 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2404 EXPORT_SYMBOL(ib_send_cm_lap
);
2406 static void cm_format_path_from_lap(struct ib_sa_path_rec
*path
,
2407 struct cm_lap_msg
*lap_msg
)
2409 memset(path
, 0, sizeof *path
);
2410 path
->dgid
= lap_msg
->alt_local_gid
;
2411 path
->sgid
= lap_msg
->alt_remote_gid
;
2412 path
->dlid
= lap_msg
->alt_local_lid
;
2413 path
->slid
= lap_msg
->alt_remote_lid
;
2414 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2415 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2416 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2417 path
->reversible
= 1;
2418 /* pkey is same as in REQ */
2419 path
->sl
= cm_lap_get_sl(lap_msg
);
2420 path
->mtu_selector
= IB_SA_EQ
;
2421 /* mtu is same as in REQ */
2422 path
->rate_selector
= IB_SA_EQ
;
2423 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2424 path
->packet_life_time_selector
= IB_SA_EQ
;
2425 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2426 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2429 static int cm_lap_handler(struct cm_work
*work
)
2431 struct cm_id_private
*cm_id_priv
;
2432 struct cm_lap_msg
*lap_msg
;
2433 struct ib_cm_lap_event_param
*param
;
2434 struct ib_mad_send_buf
*msg
= NULL
;
2435 unsigned long flags
;
2438 /* todo: verify LAP request and send reject APR if invalid. */
2439 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2440 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2441 lap_msg
->local_comm_id
);
2445 param
= &work
->cm_event
.param
.lap_rcvd
;
2446 param
->alternate_path
= &work
->path
[0];
2447 cm_format_path_from_lap(param
->alternate_path
, lap_msg
);
2448 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2450 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2451 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2454 switch (cm_id_priv
->id
.lap_state
) {
2455 case IB_CM_LAP_IDLE
:
2457 case IB_CM_MRA_LAP_SENT
:
2458 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2461 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2462 CM_MSG_RESPONSE_OTHER
,
2463 cm_id_priv
->service_timeout
,
2464 cm_id_priv
->private_data
,
2465 cm_id_priv
->private_data_len
);
2466 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2468 if (ib_post_send_mad(msg
, NULL
))
2475 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2476 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2477 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2479 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2480 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2483 cm_process_work(cm_id_priv
, work
);
2485 cm_deref_id(cm_id_priv
);
2488 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2489 deref
: cm_deref_id(cm_id_priv
);
2493 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2494 struct cm_id_private
*cm_id_priv
,
2495 enum ib_cm_apr_status status
,
2498 const void *private_data
,
2499 u8 private_data_len
)
2501 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2502 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2503 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2504 apr_msg
->ap_status
= (u8
) status
;
2506 if (info
&& info_length
) {
2507 apr_msg
->info_length
= info_length
;
2508 memcpy(apr_msg
->info
, info
, info_length
);
2511 if (private_data
&& private_data_len
)
2512 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2515 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2516 enum ib_cm_apr_status status
,
2519 const void *private_data
,
2520 u8 private_data_len
)
2522 struct cm_id_private
*cm_id_priv
;
2523 struct ib_mad_send_buf
*msg
;
2524 unsigned long flags
;
2527 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2528 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2531 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2532 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2533 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2534 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2535 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2540 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2544 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2545 info
, info_length
, private_data
, private_data_len
);
2546 ret
= ib_post_send_mad(msg
, NULL
);
2548 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2553 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2554 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2557 EXPORT_SYMBOL(ib_send_cm_apr
);
2559 static int cm_apr_handler(struct cm_work
*work
)
2561 struct cm_id_private
*cm_id_priv
;
2562 struct cm_apr_msg
*apr_msg
;
2563 unsigned long flags
;
2566 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2567 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2568 apr_msg
->local_comm_id
);
2570 return -EINVAL
; /* Unmatched reply. */
2572 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2573 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2574 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2575 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2577 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2578 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2579 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2580 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2581 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2584 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2585 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2586 cm_id_priv
->msg
= NULL
;
2588 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2590 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2591 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2594 cm_process_work(cm_id_priv
, work
);
2596 cm_deref_id(cm_id_priv
);
2599 cm_deref_id(cm_id_priv
);
2603 static int cm_timewait_handler(struct cm_work
*work
)
2605 struct cm_timewait_info
*timewait_info
;
2606 struct cm_id_private
*cm_id_priv
;
2609 timewait_info
= (struct cm_timewait_info
*)work
;
2610 spin_lock_irq(&cm
.lock
);
2611 list_del(&timewait_info
->list
);
2612 spin_unlock_irq(&cm
.lock
);
2614 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2615 timewait_info
->work
.remote_id
);
2619 spin_lock_irq(&cm_id_priv
->lock
);
2620 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2621 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2622 spin_unlock_irq(&cm_id_priv
->lock
);
2625 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2626 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2628 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2629 spin_unlock_irq(&cm_id_priv
->lock
);
2632 cm_process_work(cm_id_priv
, work
);
2634 cm_deref_id(cm_id_priv
);
2637 cm_deref_id(cm_id_priv
);
2641 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2642 struct cm_id_private
*cm_id_priv
,
2643 struct ib_cm_sidr_req_param
*param
)
2645 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2646 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2647 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2648 sidr_req_msg
->pkey
= cpu_to_be16(param
->path
->pkey
);
2649 sidr_req_msg
->service_id
= param
->service_id
;
2651 if (param
->private_data
&& param
->private_data_len
)
2652 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2653 param
->private_data_len
);
2656 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2657 struct ib_cm_sidr_req_param
*param
)
2659 struct cm_id_private
*cm_id_priv
;
2660 struct ib_mad_send_buf
*msg
;
2661 unsigned long flags
;
2664 if (!param
->path
|| (param
->private_data
&&
2665 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2668 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2669 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2673 cm_id
->service_id
= param
->service_id
;
2674 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
2675 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2676 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2677 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2681 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2683 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2684 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2686 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2687 if (cm_id
->state
== IB_CM_IDLE
)
2688 ret
= ib_post_send_mad(msg
, NULL
);
2693 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2697 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2698 cm_id_priv
->msg
= msg
;
2699 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2703 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2705 static void cm_format_sidr_req_event(struct cm_work
*work
,
2706 struct ib_cm_id
*listen_id
)
2708 struct cm_sidr_req_msg
*sidr_req_msg
;
2709 struct ib_cm_sidr_req_event_param
*param
;
2711 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2712 work
->mad_recv_wc
->recv_buf
.mad
;
2713 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2714 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2715 param
->listen_id
= listen_id
;
2716 param
->port
= work
->port
->port_num
;
2717 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2720 static int cm_sidr_req_handler(struct cm_work
*work
)
2722 struct ib_cm_id
*cm_id
;
2723 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2724 struct cm_sidr_req_msg
*sidr_req_msg
;
2726 unsigned long flags
;
2728 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
2730 return PTR_ERR(cm_id
);
2731 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2733 /* Record SGID/SLID and request ID for lookup. */
2734 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2735 work
->mad_recv_wc
->recv_buf
.mad
;
2736 wc
= work
->mad_recv_wc
->wc
;
2737 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2738 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2739 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2740 work
->mad_recv_wc
->recv_buf
.grh
,
2742 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2743 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
2744 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2745 atomic_inc(&cm_id_priv
->work_count
);
2747 spin_lock_irqsave(&cm
.lock
, flags
);
2748 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2749 if (cur_cm_id_priv
) {
2750 spin_unlock_irqrestore(&cm
.lock
, flags
);
2751 goto out
; /* Duplicate message. */
2753 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
2754 sidr_req_msg
->service_id
,
2755 sidr_req_msg
->private_data
);
2756 if (!cur_cm_id_priv
) {
2757 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2758 spin_unlock_irqrestore(&cm
.lock
, flags
);
2759 /* todo: reply with no match */
2760 goto out
; /* No match. */
2762 atomic_inc(&cur_cm_id_priv
->refcount
);
2763 spin_unlock_irqrestore(&cm
.lock
, flags
);
2765 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
2766 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
2767 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
2768 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
2770 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
2771 cm_process_work(cm_id_priv
, work
);
2772 cm_deref_id(cur_cm_id_priv
);
2775 ib_destroy_cm_id(&cm_id_priv
->id
);
2779 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
2780 struct cm_id_private
*cm_id_priv
,
2781 struct ib_cm_sidr_rep_param
*param
)
2783 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
2785 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
2786 sidr_rep_msg
->status
= param
->status
;
2787 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
2788 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
2789 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
2791 if (param
->info
&& param
->info_length
)
2792 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
2794 if (param
->private_data
&& param
->private_data_len
)
2795 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
2796 param
->private_data_len
);
2799 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
2800 struct ib_cm_sidr_rep_param
*param
)
2802 struct cm_id_private
*cm_id_priv
;
2803 struct ib_mad_send_buf
*msg
;
2804 unsigned long flags
;
2807 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
2808 (param
->private_data
&&
2809 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
2812 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2813 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2814 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
2819 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2823 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
2825 ret
= ib_post_send_mad(msg
, NULL
);
2827 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2831 cm_id
->state
= IB_CM_IDLE
;
2832 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2834 spin_lock_irqsave(&cm
.lock
, flags
);
2835 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2836 spin_unlock_irqrestore(&cm
.lock
, flags
);
2839 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2842 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
2844 static void cm_format_sidr_rep_event(struct cm_work
*work
)
2846 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2847 struct ib_cm_sidr_rep_event_param
*param
;
2849 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2850 work
->mad_recv_wc
->recv_buf
.mad
;
2851 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
2852 param
->status
= sidr_rep_msg
->status
;
2853 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
2854 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
2855 param
->info
= &sidr_rep_msg
->info
;
2856 param
->info_len
= sidr_rep_msg
->info_length
;
2857 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
2860 static int cm_sidr_rep_handler(struct cm_work
*work
)
2862 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2863 struct cm_id_private
*cm_id_priv
;
2864 unsigned long flags
;
2866 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2867 work
->mad_recv_wc
->recv_buf
.mad
;
2868 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
2870 return -EINVAL
; /* Unmatched reply. */
2872 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2873 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
2874 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2877 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2878 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2879 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2881 cm_format_sidr_rep_event(work
);
2882 cm_process_work(cm_id_priv
, work
);
2885 cm_deref_id(cm_id_priv
);
2889 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
2890 enum ib_wc_status wc_status
)
2892 struct cm_id_private
*cm_id_priv
;
2893 struct ib_cm_event cm_event
;
2894 enum ib_cm_state state
;
2895 unsigned long flags
;
2898 memset(&cm_event
, 0, sizeof cm_event
);
2899 cm_id_priv
= msg
->context
[0];
2901 /* Discard old sends or ones without a response. */
2902 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2903 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
2904 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
2908 case IB_CM_REQ_SENT
:
2909 case IB_CM_MRA_REQ_RCVD
:
2910 cm_reset_to_idle(cm_id_priv
);
2911 cm_event
.event
= IB_CM_REQ_ERROR
;
2913 case IB_CM_REP_SENT
:
2914 case IB_CM_MRA_REP_RCVD
:
2915 cm_reset_to_idle(cm_id_priv
);
2916 cm_event
.event
= IB_CM_REP_ERROR
;
2918 case IB_CM_DREQ_SENT
:
2919 cm_enter_timewait(cm_id_priv
);
2920 cm_event
.event
= IB_CM_DREQ_ERROR
;
2922 case IB_CM_SIDR_REQ_SENT
:
2923 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2924 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
2929 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2930 cm_event
.param
.send_status
= wc_status
;
2932 /* No other events can occur on the cm_id at this point. */
2933 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
2936 ib_destroy_cm_id(&cm_id_priv
->id
);
2939 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2943 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
2944 struct ib_mad_send_wc
*mad_send_wc
)
2946 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
2948 switch (mad_send_wc
->status
) {
2950 case IB_WC_WR_FLUSH_ERR
:
2954 if (msg
->context
[0] && msg
->context
[1])
2955 cm_process_send_error(msg
, mad_send_wc
->status
);
2962 static void cm_work_handler(void *data
)
2964 struct cm_work
*work
= data
;
2967 switch (work
->cm_event
.event
) {
2968 case IB_CM_REQ_RECEIVED
:
2969 ret
= cm_req_handler(work
);
2971 case IB_CM_MRA_RECEIVED
:
2972 ret
= cm_mra_handler(work
);
2974 case IB_CM_REJ_RECEIVED
:
2975 ret
= cm_rej_handler(work
);
2977 case IB_CM_REP_RECEIVED
:
2978 ret
= cm_rep_handler(work
);
2980 case IB_CM_RTU_RECEIVED
:
2981 ret
= cm_rtu_handler(work
);
2983 case IB_CM_USER_ESTABLISHED
:
2984 ret
= cm_establish_handler(work
);
2986 case IB_CM_DREQ_RECEIVED
:
2987 ret
= cm_dreq_handler(work
);
2989 case IB_CM_DREP_RECEIVED
:
2990 ret
= cm_drep_handler(work
);
2992 case IB_CM_SIDR_REQ_RECEIVED
:
2993 ret
= cm_sidr_req_handler(work
);
2995 case IB_CM_SIDR_REP_RECEIVED
:
2996 ret
= cm_sidr_rep_handler(work
);
2998 case IB_CM_LAP_RECEIVED
:
2999 ret
= cm_lap_handler(work
);
3001 case IB_CM_APR_RECEIVED
:
3002 ret
= cm_apr_handler(work
);
3004 case IB_CM_TIMEWAIT_EXIT
:
3005 ret
= cm_timewait_handler(work
);
3015 int ib_cm_establish(struct ib_cm_id
*cm_id
)
3017 struct cm_id_private
*cm_id_priv
;
3018 struct cm_work
*work
;
3019 unsigned long flags
;
3022 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3026 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3027 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3028 switch (cm_id
->state
)
3030 case IB_CM_REP_SENT
:
3031 case IB_CM_MRA_REP_RCVD
:
3032 cm_id
->state
= IB_CM_ESTABLISHED
;
3034 case IB_CM_ESTABLISHED
:
3041 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3049 * The CM worker thread may try to destroy the cm_id before it
3050 * can execute this work item. To prevent potential deadlock,
3051 * we need to find the cm_id once we're in the context of the
3052 * worker thread, rather than holding a reference on it.
3054 INIT_WORK(&work
->work
, cm_work_handler
, work
);
3055 work
->local_id
= cm_id
->local_id
;
3056 work
->remote_id
= cm_id
->remote_id
;
3057 work
->mad_recv_wc
= NULL
;
3058 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3059 queue_work(cm
.wq
, &work
->work
);
3063 EXPORT_SYMBOL(ib_cm_establish
);
3065 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3066 struct ib_mad_recv_wc
*mad_recv_wc
)
3068 struct cm_work
*work
;
3069 enum ib_cm_event_type event
;
3072 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3073 case CM_REQ_ATTR_ID
:
3074 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3075 alt_local_lid
!= 0);
3076 event
= IB_CM_REQ_RECEIVED
;
3078 case CM_MRA_ATTR_ID
:
3079 event
= IB_CM_MRA_RECEIVED
;
3081 case CM_REJ_ATTR_ID
:
3082 event
= IB_CM_REJ_RECEIVED
;
3084 case CM_REP_ATTR_ID
:
3085 event
= IB_CM_REP_RECEIVED
;
3087 case CM_RTU_ATTR_ID
:
3088 event
= IB_CM_RTU_RECEIVED
;
3090 case CM_DREQ_ATTR_ID
:
3091 event
= IB_CM_DREQ_RECEIVED
;
3093 case CM_DREP_ATTR_ID
:
3094 event
= IB_CM_DREP_RECEIVED
;
3096 case CM_SIDR_REQ_ATTR_ID
:
3097 event
= IB_CM_SIDR_REQ_RECEIVED
;
3099 case CM_SIDR_REP_ATTR_ID
:
3100 event
= IB_CM_SIDR_REP_RECEIVED
;
3102 case CM_LAP_ATTR_ID
:
3104 event
= IB_CM_LAP_RECEIVED
;
3106 case CM_APR_ATTR_ID
:
3107 event
= IB_CM_APR_RECEIVED
;
3110 ib_free_recv_mad(mad_recv_wc
);
3114 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3117 ib_free_recv_mad(mad_recv_wc
);
3121 INIT_WORK(&work
->work
, cm_work_handler
, work
);
3122 work
->cm_event
.event
= event
;
3123 work
->mad_recv_wc
= mad_recv_wc
;
3124 work
->port
= (struct cm_port
*)mad_agent
->context
;
3125 queue_work(cm
.wq
, &work
->work
);
3128 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3129 struct ib_qp_attr
*qp_attr
,
3132 unsigned long flags
;
3135 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3136 switch (cm_id_priv
->id
.state
) {
3137 case IB_CM_REQ_SENT
:
3138 case IB_CM_MRA_REQ_RCVD
:
3139 case IB_CM_REQ_RCVD
:
3140 case IB_CM_MRA_REQ_SENT
:
3141 case IB_CM_REP_RCVD
:
3142 case IB_CM_MRA_REP_SENT
:
3143 case IB_CM_REP_SENT
:
3144 case IB_CM_MRA_REP_RCVD
:
3145 case IB_CM_ESTABLISHED
:
3146 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3147 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3148 qp_attr
->qp_access_flags
= IB_ACCESS_LOCAL_WRITE
|
3149 IB_ACCESS_REMOTE_WRITE
;
3150 if (cm_id_priv
->responder_resources
)
3151 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
|
3152 IB_ACCESS_REMOTE_ATOMIC
;
3153 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3154 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3161 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3165 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3166 struct ib_qp_attr
*qp_attr
,
3169 unsigned long flags
;
3172 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3173 switch (cm_id_priv
->id
.state
) {
3174 case IB_CM_REQ_RCVD
:
3175 case IB_CM_MRA_REQ_SENT
:
3176 case IB_CM_REP_RCVD
:
3177 case IB_CM_MRA_REP_SENT
:
3178 case IB_CM_REP_SENT
:
3179 case IB_CM_MRA_REP_RCVD
:
3180 case IB_CM_ESTABLISHED
:
3181 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3182 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3183 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3184 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3185 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3186 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3187 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3188 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3189 IB_QP_MIN_RNR_TIMER
;
3190 qp_attr
->max_dest_rd_atomic
=
3191 cm_id_priv
->responder_resources
;
3192 qp_attr
->min_rnr_timer
= 0;
3194 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3195 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3196 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3197 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3205 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3209 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3210 struct ib_qp_attr
*qp_attr
,
3213 unsigned long flags
;
3216 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3217 switch (cm_id_priv
->id
.state
) {
3218 case IB_CM_REP_RCVD
:
3219 case IB_CM_MRA_REP_SENT
:
3220 case IB_CM_REP_SENT
:
3221 case IB_CM_MRA_REP_RCVD
:
3222 case IB_CM_ESTABLISHED
:
3223 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3224 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3225 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3226 *qp_attr_mask
|= IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3228 IB_QP_MAX_QP_RD_ATOMIC
;
3229 qp_attr
->timeout
= cm_id_priv
->local_ack_timeout
;
3230 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3231 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3232 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3234 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3235 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3236 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3244 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3248 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3249 struct ib_qp_attr
*qp_attr
,
3252 struct cm_id_private
*cm_id_priv
;
3255 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3256 switch (qp_attr
->qp_state
) {
3258 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3261 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3264 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3272 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3274 static void cm_add_one(struct ib_device
*device
)
3276 struct cm_device
*cm_dev
;
3277 struct cm_port
*port
;
3278 struct ib_mad_reg_req reg_req
= {
3279 .mgmt_class
= IB_MGMT_CLASS_CM
,
3280 .mgmt_class_version
= IB_CM_CLASS_VERSION
3282 struct ib_port_modify port_modify
= {
3283 .set_port_cap_mask
= IB_PORT_CM_SUP
3285 unsigned long flags
;
3289 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
3292 cm_dev
= kmalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3293 device
->phys_port_cnt
, GFP_KERNEL
);
3297 cm_dev
->device
= device
;
3298 cm_dev
->ca_guid
= device
->node_guid
;
3300 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3301 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3302 port
= &cm_dev
->port
[i
-1];
3303 port
->cm_dev
= cm_dev
;
3305 port
->mad_agent
= ib_register_mad_agent(device
, i
,
3312 if (IS_ERR(port
->mad_agent
))
3315 ret
= ib_modify_port(device
, i
, 0, &port_modify
);
3319 ib_set_client_data(device
, &cm_client
, cm_dev
);
3321 write_lock_irqsave(&cm
.device_lock
, flags
);
3322 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3323 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3327 ib_unregister_mad_agent(port
->mad_agent
);
3329 port_modify
.set_port_cap_mask
= 0;
3330 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3332 port
= &cm_dev
->port
[i
-1];
3333 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3334 ib_unregister_mad_agent(port
->mad_agent
);
3339 static void cm_remove_one(struct ib_device
*device
)
3341 struct cm_device
*cm_dev
;
3342 struct cm_port
*port
;
3343 struct ib_port_modify port_modify
= {
3344 .clr_port_cap_mask
= IB_PORT_CM_SUP
3346 unsigned long flags
;
3349 cm_dev
= ib_get_client_data(device
, &cm_client
);
3353 write_lock_irqsave(&cm
.device_lock
, flags
);
3354 list_del(&cm_dev
->list
);
3355 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3357 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3358 port
= &cm_dev
->port
[i
-1];
3359 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3360 ib_unregister_mad_agent(port
->mad_agent
);
3365 static int __init
ib_cm_init(void)
3369 memset(&cm
, 0, sizeof cm
);
3370 INIT_LIST_HEAD(&cm
.device_list
);
3371 rwlock_init(&cm
.device_lock
);
3372 spin_lock_init(&cm
.lock
);
3373 cm
.listen_service_table
= RB_ROOT
;
3374 cm
.listen_service_id
= __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3375 cm
.remote_id_table
= RB_ROOT
;
3376 cm
.remote_qp_table
= RB_ROOT
;
3377 cm
.remote_sidr_table
= RB_ROOT
;
3378 idr_init(&cm
.local_id_table
);
3379 get_random_bytes(&cm
.random_id_operand
, sizeof cm
.random_id_operand
);
3380 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3381 INIT_LIST_HEAD(&cm
.timewait_list
);
3383 cm
.wq
= create_workqueue("ib_cm");
3387 ret
= ib_register_client(&cm_client
);
3393 destroy_workqueue(cm
.wq
);
3397 static void __exit
ib_cm_cleanup(void)
3399 struct cm_timewait_info
*timewait_info
, *tmp
;
3401 spin_lock_irq(&cm
.lock
);
3402 list_for_each_entry(timewait_info
, &cm
.timewait_list
, list
)
3403 cancel_delayed_work(&timewait_info
->work
.work
);
3404 spin_unlock_irq(&cm
.lock
);
3406 destroy_workqueue(cm
.wq
);
3408 list_for_each_entry_safe(timewait_info
, tmp
, &cm
.timewait_list
, list
) {
3409 list_del(&timewait_info
->list
);
3410 kfree(timewait_info
);
3413 ib_unregister_client(&cm_client
);
3414 idr_destroy(&cm
.local_id_table
);
3417 module_init(ib_cm_init
);
3418 module_exit(ib_cm_cleanup
);