2 * Copyright (c) 2004, 2005 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/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
56 static void cm_add_one(struct ib_device
*device
);
57 static void cm_remove_one(struct ib_device
*device
);
59 static struct ib_client cm_client
= {
62 .remove
= cm_remove_one
67 struct list_head device_list
;
69 struct rb_root listen_service_table
;
70 u64 listen_service_id
;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table
;
73 struct rb_root remote_id_table
;
74 struct rb_root remote_sidr_table
;
75 struct idr local_id_table
;
76 struct workqueue_struct
*wq
;
80 struct cm_device
*cm_dev
;
81 struct ib_mad_agent
*mad_agent
;
86 struct list_head list
;
87 struct ib_device
*device
;
89 struct cm_port port
[0];
95 struct ib_ah_attr ah_attr
;
101 struct work_struct work
;
102 struct list_head list
;
103 struct cm_port
*port
;
104 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
105 __be32 local_id
; /* Established / timewait */
107 struct ib_cm_event cm_event
;
108 struct ib_sa_path_rec path
[0];
111 struct cm_timewait_info
{
112 struct cm_work work
; /* Must be first. */
113 struct rb_node remote_qp_node
;
114 struct rb_node remote_id_node
;
115 __be64 remote_ca_guid
;
117 u8 inserted_remote_qp
;
118 u8 inserted_remote_id
;
121 struct cm_id_private
{
124 struct rb_node service_node
;
125 struct rb_node sidr_id_node
;
126 spinlock_t lock
; /* Do not acquire inside cm.lock */
127 struct completion comp
;
130 struct ib_mad_send_buf
*msg
;
131 struct cm_timewait_info
*timewait_info
;
132 /* todo: use alternate port on send failure */
135 struct ib_cm_compare_data
*compare_data
;
141 enum ib_qp_type qp_type
;
145 enum ib_mtu path_mtu
;
149 u8 responder_resources
;
151 u8 local_ack_timeout
;
156 struct list_head work_list
;
160 static void cm_work_handler(void *data
);
162 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
164 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
165 complete(&cm_id_priv
->comp
);
168 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
169 struct ib_mad_send_buf
**msg
)
171 struct ib_mad_agent
*mad_agent
;
172 struct ib_mad_send_buf
*m
;
175 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
176 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
180 m
= ib_create_send_mad(mad_agent
, cm_id_priv
->id
.remote_cm_qpn
,
181 cm_id_priv
->av
.pkey_index
,
182 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
189 /* Timeout set by caller if response is expected. */
191 m
->retries
= cm_id_priv
->max_cm_retries
;
193 atomic_inc(&cm_id_priv
->refcount
);
194 m
->context
[0] = cm_id_priv
;
199 static int cm_alloc_response_msg(struct cm_port
*port
,
200 struct ib_mad_recv_wc
*mad_recv_wc
,
201 struct ib_mad_send_buf
**msg
)
203 struct ib_mad_send_buf
*m
;
206 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
207 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
211 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
212 0, IB_MGMT_MAD_HDR
, IB_MGMT_MAD_DATA
,
223 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
225 ib_destroy_ah(msg
->ah
);
227 cm_deref_id(msg
->context
[0]);
228 ib_free_send_mad(msg
);
231 static void * cm_copy_private_data(const void *private_data
,
236 if (!private_data
|| !private_data_len
)
239 data
= kmalloc(private_data_len
, GFP_KERNEL
);
241 return ERR_PTR(-ENOMEM
);
243 memcpy(data
, private_data
, private_data_len
);
247 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
248 void *private_data
, u8 private_data_len
)
250 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
251 kfree(cm_id_priv
->private_data
);
253 cm_id_priv
->private_data
= private_data
;
254 cm_id_priv
->private_data_len
= private_data_len
;
257 static void cm_init_av_for_response(struct cm_port
*port
, struct ib_wc
*wc
,
258 struct ib_grh
*grh
, struct cm_av
*av
)
261 av
->pkey_index
= wc
->pkey_index
;
262 ib_init_ah_from_wc(port
->cm_dev
->device
, port
->port_num
, wc
,
266 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
268 struct cm_device
*cm_dev
;
269 struct cm_port
*port
= NULL
;
274 read_lock_irqsave(&cm
.device_lock
, flags
);
275 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
276 if (!ib_find_cached_gid(cm_dev
->device
, &path
->sgid
,
278 port
= &cm_dev
->port
[p
-1];
282 read_unlock_irqrestore(&cm
.device_lock
, flags
);
287 ret
= ib_find_cached_pkey(cm_dev
->device
, port
->port_num
,
288 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
293 ib_init_ah_from_path(cm_dev
->device
, port
->port_num
, path
,
295 av
->packet_life_time
= path
->packet_life_time
;
299 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
306 spin_lock_irqsave(&cm
.lock
, flags
);
307 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
, next_id
++,
308 (__force
int *) &cm_id_priv
->id
.local_id
);
309 spin_unlock_irqrestore(&cm
.lock
, flags
);
310 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
314 static void cm_free_id(__be32 local_id
)
318 spin_lock_irqsave(&cm
.lock
, flags
);
319 idr_remove(&cm
.local_id_table
, (__force
int) local_id
);
320 spin_unlock_irqrestore(&cm
.lock
, flags
);
323 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
325 struct cm_id_private
*cm_id_priv
;
327 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int) local_id
);
329 if (cm_id_priv
->id
.remote_id
== remote_id
)
330 atomic_inc(&cm_id_priv
->refcount
);
338 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
340 struct cm_id_private
*cm_id_priv
;
343 spin_lock_irqsave(&cm
.lock
, flags
);
344 cm_id_priv
= cm_get_id(local_id
, remote_id
);
345 spin_unlock_irqrestore(&cm
.lock
, flags
);
350 static void cm_mask_copy(u8
*dst
, u8
*src
, u8
*mask
)
354 for (i
= 0; i
< IB_CM_COMPARE_SIZE
/ sizeof(unsigned long); i
++)
355 ((unsigned long *) dst
)[i
] = ((unsigned long *) src
)[i
] &
356 ((unsigned long *) mask
)[i
];
359 static int cm_compare_data(struct ib_cm_compare_data
*src_data
,
360 struct ib_cm_compare_data
*dst_data
)
362 u8 src
[IB_CM_COMPARE_SIZE
];
363 u8 dst
[IB_CM_COMPARE_SIZE
];
365 if (!src_data
|| !dst_data
)
368 cm_mask_copy(src
, src_data
->data
, dst_data
->mask
);
369 cm_mask_copy(dst
, dst_data
->data
, src_data
->mask
);
370 return memcmp(src
, dst
, IB_CM_COMPARE_SIZE
);
373 static int cm_compare_private_data(u8
*private_data
,
374 struct ib_cm_compare_data
*dst_data
)
376 u8 src
[IB_CM_COMPARE_SIZE
];
381 cm_mask_copy(src
, private_data
, dst_data
->mask
);
382 return memcmp(src
, dst_data
->data
, IB_CM_COMPARE_SIZE
);
385 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
387 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
388 struct rb_node
*parent
= NULL
;
389 struct cm_id_private
*cur_cm_id_priv
;
390 __be64 service_id
= cm_id_priv
->id
.service_id
;
391 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
396 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
398 data_cmp
= cm_compare_data(cm_id_priv
->compare_data
,
399 cur_cm_id_priv
->compare_data
);
400 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
401 (service_mask
& cur_cm_id_priv
->id
.service_id
) &&
402 (cm_id_priv
->id
.device
== cur_cm_id_priv
->id
.device
) &&
404 return cur_cm_id_priv
;
406 if (cm_id_priv
->id
.device
< cur_cm_id_priv
->id
.device
)
407 link
= &(*link
)->rb_left
;
408 else if (cm_id_priv
->id
.device
> cur_cm_id_priv
->id
.device
)
409 link
= &(*link
)->rb_right
;
410 else if (service_id
< cur_cm_id_priv
->id
.service_id
)
411 link
= &(*link
)->rb_left
;
412 else if (service_id
> cur_cm_id_priv
->id
.service_id
)
413 link
= &(*link
)->rb_right
;
414 else if (data_cmp
< 0)
415 link
= &(*link
)->rb_left
;
417 link
= &(*link
)->rb_right
;
419 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
420 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
424 static struct cm_id_private
* cm_find_listen(struct ib_device
*device
,
428 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
429 struct cm_id_private
*cm_id_priv
;
433 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
434 data_cmp
= cm_compare_private_data(private_data
,
435 cm_id_priv
->compare_data
);
436 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
437 cm_id_priv
->id
.service_id
&&
438 (cm_id_priv
->id
.device
== device
) && !data_cmp
)
441 if (device
< cm_id_priv
->id
.device
)
442 node
= node
->rb_left
;
443 else if (device
> cm_id_priv
->id
.device
)
444 node
= node
->rb_right
;
445 else if (service_id
< cm_id_priv
->id
.service_id
)
446 node
= node
->rb_left
;
447 else if (service_id
> cm_id_priv
->id
.service_id
)
448 node
= node
->rb_right
;
449 else if (data_cmp
< 0)
450 node
= node
->rb_left
;
452 node
= node
->rb_right
;
457 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
460 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
461 struct rb_node
*parent
= NULL
;
462 struct cm_timewait_info
*cur_timewait_info
;
463 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
464 __be32 remote_id
= timewait_info
->work
.remote_id
;
468 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
470 if (remote_id
< cur_timewait_info
->work
.remote_id
)
471 link
= &(*link
)->rb_left
;
472 else if (remote_id
> cur_timewait_info
->work
.remote_id
)
473 link
= &(*link
)->rb_right
;
474 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
475 link
= &(*link
)->rb_left
;
476 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
477 link
= &(*link
)->rb_right
;
479 return cur_timewait_info
;
481 timewait_info
->inserted_remote_id
= 1;
482 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
483 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
487 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
490 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
491 struct cm_timewait_info
*timewait_info
;
494 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
496 if (remote_id
< timewait_info
->work
.remote_id
)
497 node
= node
->rb_left
;
498 else if (remote_id
> timewait_info
->work
.remote_id
)
499 node
= node
->rb_right
;
500 else if (remote_ca_guid
< timewait_info
->remote_ca_guid
)
501 node
= node
->rb_left
;
502 else if (remote_ca_guid
> timewait_info
->remote_ca_guid
)
503 node
= node
->rb_right
;
505 return timewait_info
;
510 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
513 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
514 struct rb_node
*parent
= NULL
;
515 struct cm_timewait_info
*cur_timewait_info
;
516 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
517 __be32 remote_qpn
= timewait_info
->remote_qpn
;
521 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
523 if (remote_qpn
< cur_timewait_info
->remote_qpn
)
524 link
= &(*link
)->rb_left
;
525 else if (remote_qpn
> cur_timewait_info
->remote_qpn
)
526 link
= &(*link
)->rb_right
;
527 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
528 link
= &(*link
)->rb_left
;
529 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
530 link
= &(*link
)->rb_right
;
532 return cur_timewait_info
;
534 timewait_info
->inserted_remote_qp
= 1;
535 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
536 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
540 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
543 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
544 struct rb_node
*parent
= NULL
;
545 struct cm_id_private
*cur_cm_id_priv
;
546 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
547 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
551 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
553 if (remote_id
< cur_cm_id_priv
->id
.remote_id
)
554 link
= &(*link
)->rb_left
;
555 else if (remote_id
> cur_cm_id_priv
->id
.remote_id
)
556 link
= &(*link
)->rb_right
;
559 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
562 link
= &(*link
)->rb_left
;
564 link
= &(*link
)->rb_right
;
566 return cur_cm_id_priv
;
569 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
570 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
574 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
575 enum ib_cm_sidr_status status
)
577 struct ib_cm_sidr_rep_param param
;
579 memset(¶m
, 0, sizeof param
);
580 param
.status
= status
;
581 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
584 struct ib_cm_id
*ib_create_cm_id(struct ib_device
*device
,
585 ib_cm_handler cm_handler
,
588 struct cm_id_private
*cm_id_priv
;
591 cm_id_priv
= kzalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
593 return ERR_PTR(-ENOMEM
);
595 cm_id_priv
->id
.state
= IB_CM_IDLE
;
596 cm_id_priv
->id
.device
= device
;
597 cm_id_priv
->id
.cm_handler
= cm_handler
;
598 cm_id_priv
->id
.context
= context
;
599 cm_id_priv
->id
.remote_cm_qpn
= 1;
600 ret
= cm_alloc_id(cm_id_priv
);
604 spin_lock_init(&cm_id_priv
->lock
);
605 init_completion(&cm_id_priv
->comp
);
606 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
607 atomic_set(&cm_id_priv
->work_count
, -1);
608 atomic_set(&cm_id_priv
->refcount
, 1);
609 return &cm_id_priv
->id
;
613 return ERR_PTR(-ENOMEM
);
615 EXPORT_SYMBOL(ib_create_cm_id
);
617 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
619 struct cm_work
*work
;
621 if (list_empty(&cm_id_priv
->work_list
))
624 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
625 list_del(&work
->list
);
629 static void cm_free_work(struct cm_work
*work
)
631 if (work
->mad_recv_wc
)
632 ib_free_recv_mad(work
->mad_recv_wc
);
636 static inline int cm_convert_to_ms(int iba_time
)
638 /* approximate conversion to ms from 4.096us x 2^iba_time */
639 return 1 << max(iba_time
- 8, 0);
642 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
646 if (!timewait_info
->inserted_remote_id
&&
647 !timewait_info
->inserted_remote_qp
)
650 spin_lock_irqsave(&cm
.lock
, flags
);
651 if (timewait_info
->inserted_remote_id
) {
652 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
653 timewait_info
->inserted_remote_id
= 0;
656 if (timewait_info
->inserted_remote_qp
) {
657 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
658 timewait_info
->inserted_remote_qp
= 0;
660 spin_unlock_irqrestore(&cm
.lock
, flags
);
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 * The cm_id could be destroyed by the user before we exit timewait.
684 * To protect against this, we search for the cm_id after exiting
685 * timewait before notifying the user that we've exited timewait.
687 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
688 wait_time
= cm_convert_to_ms(cm_id_priv
->local_ack_timeout
);
689 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
690 msecs_to_jiffies(wait_time
));
691 cm_id_priv
->timewait_info
= NULL
;
694 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
696 cm_id_priv
->id
.state
= IB_CM_IDLE
;
697 if (cm_id_priv
->timewait_info
) {
698 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
699 kfree(cm_id_priv
->timewait_info
);
700 cm_id_priv
->timewait_info
= NULL
;
704 static void cm_destroy_id(struct ib_cm_id
*cm_id
, int err
)
706 struct cm_id_private
*cm_id_priv
;
707 struct cm_work
*work
;
710 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
712 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
713 switch (cm_id
->state
) {
715 cm_id
->state
= IB_CM_IDLE
;
716 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
717 spin_lock_irqsave(&cm
.lock
, flags
);
718 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
719 spin_unlock_irqrestore(&cm
.lock
, flags
);
721 case IB_CM_SIDR_REQ_SENT
:
722 cm_id
->state
= IB_CM_IDLE
;
723 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
724 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
726 case IB_CM_SIDR_REQ_RCVD
:
727 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
728 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
731 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
732 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
733 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
734 &cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
735 sizeof cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
739 if (err
== -ENOMEM
) {
740 /* Do not reject to allow future retries. */
741 cm_reset_to_idle(cm_id_priv
);
742 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
744 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
745 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
749 case IB_CM_MRA_REQ_RCVD
:
751 case IB_CM_MRA_REP_RCVD
:
752 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
754 case IB_CM_MRA_REQ_SENT
:
756 case IB_CM_MRA_REP_SENT
:
757 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
758 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
761 case IB_CM_ESTABLISHED
:
762 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
763 ib_send_cm_dreq(cm_id
, NULL
, 0);
765 case IB_CM_DREQ_SENT
:
766 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
767 cm_enter_timewait(cm_id_priv
);
768 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
770 case IB_CM_DREQ_RCVD
:
771 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
772 ib_send_cm_drep(cm_id
, NULL
, 0);
775 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
779 cm_free_id(cm_id
->local_id
);
780 cm_deref_id(cm_id_priv
);
781 wait_for_completion(&cm_id_priv
->comp
);
782 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
784 kfree(cm_id_priv
->compare_data
);
785 kfree(cm_id_priv
->private_data
);
789 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
791 cm_destroy_id(cm_id
, 0);
793 EXPORT_SYMBOL(ib_destroy_cm_id
);
795 int ib_cm_listen(struct ib_cm_id
*cm_id
, __be64 service_id
, __be64 service_mask
,
796 struct ib_cm_compare_data
*compare_data
)
798 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
802 service_mask
= service_mask
? service_mask
:
803 __constant_cpu_to_be64(~0ULL);
804 service_id
&= service_mask
;
805 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
806 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
809 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
810 if (cm_id
->state
!= IB_CM_IDLE
)
814 cm_id_priv
->compare_data
= kzalloc(sizeof *compare_data
,
816 if (!cm_id_priv
->compare_data
)
818 cm_mask_copy(cm_id_priv
->compare_data
->data
,
819 compare_data
->data
, compare_data
->mask
);
820 memcpy(cm_id_priv
->compare_data
->mask
, compare_data
->mask
,
824 cm_id
->state
= IB_CM_LISTEN
;
826 spin_lock_irqsave(&cm
.lock
, flags
);
827 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
828 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
829 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
831 cm_id
->service_id
= service_id
;
832 cm_id
->service_mask
= service_mask
;
834 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
835 spin_unlock_irqrestore(&cm
.lock
, flags
);
837 if (cur_cm_id_priv
) {
838 cm_id
->state
= IB_CM_IDLE
;
839 kfree(cm_id_priv
->compare_data
);
840 cm_id_priv
->compare_data
= NULL
;
845 EXPORT_SYMBOL(ib_cm_listen
);
847 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
848 enum cm_msg_sequence msg_seq
)
852 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
853 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
855 return cpu_to_be64(hi_tid
| low_tid
);
858 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
859 __be16 attr_id
, __be64 tid
)
861 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
862 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
863 hdr
->class_version
= IB_CM_CLASS_VERSION
;
864 hdr
->method
= IB_MGMT_METHOD_SEND
;
865 hdr
->attr_id
= attr_id
;
869 static void cm_format_req(struct cm_req_msg
*req_msg
,
870 struct cm_id_private
*cm_id_priv
,
871 struct ib_cm_req_param
*param
)
873 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
874 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
876 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
877 req_msg
->service_id
= param
->service_id
;
878 req_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
879 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
880 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
881 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
882 cm_req_set_remote_resp_timeout(req_msg
,
883 param
->remote_cm_response_timeout
);
884 cm_req_set_qp_type(req_msg
, param
->qp_type
);
885 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
886 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
887 cm_req_set_local_resp_timeout(req_msg
,
888 param
->local_cm_response_timeout
);
889 cm_req_set_retry_count(req_msg
, param
->retry_count
);
890 req_msg
->pkey
= param
->primary_path
->pkey
;
891 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
892 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
893 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
894 cm_req_set_srq(req_msg
, param
->srq
);
896 req_msg
->primary_local_lid
= param
->primary_path
->slid
;
897 req_msg
->primary_remote_lid
= param
->primary_path
->dlid
;
898 req_msg
->primary_local_gid
= param
->primary_path
->sgid
;
899 req_msg
->primary_remote_gid
= param
->primary_path
->dgid
;
900 cm_req_set_primary_flow_label(req_msg
, param
->primary_path
->flow_label
);
901 cm_req_set_primary_packet_rate(req_msg
, param
->primary_path
->rate
);
902 req_msg
->primary_traffic_class
= param
->primary_path
->traffic_class
;
903 req_msg
->primary_hop_limit
= param
->primary_path
->hop_limit
;
904 cm_req_set_primary_sl(req_msg
, param
->primary_path
->sl
);
905 cm_req_set_primary_subnet_local(req_msg
, 1); /* local only... */
906 cm_req_set_primary_local_ack_timeout(req_msg
,
907 min(31, param
->primary_path
->packet_life_time
+ 1));
909 if (param
->alternate_path
) {
910 req_msg
->alt_local_lid
= param
->alternate_path
->slid
;
911 req_msg
->alt_remote_lid
= param
->alternate_path
->dlid
;
912 req_msg
->alt_local_gid
= param
->alternate_path
->sgid
;
913 req_msg
->alt_remote_gid
= param
->alternate_path
->dgid
;
914 cm_req_set_alt_flow_label(req_msg
,
915 param
->alternate_path
->flow_label
);
916 cm_req_set_alt_packet_rate(req_msg
, param
->alternate_path
->rate
);
917 req_msg
->alt_traffic_class
= param
->alternate_path
->traffic_class
;
918 req_msg
->alt_hop_limit
= param
->alternate_path
->hop_limit
;
919 cm_req_set_alt_sl(req_msg
, param
->alternate_path
->sl
);
920 cm_req_set_alt_subnet_local(req_msg
, 1); /* local only... */
921 cm_req_set_alt_local_ack_timeout(req_msg
,
922 min(31, param
->alternate_path
->packet_life_time
+ 1));
925 if (param
->private_data
&& param
->private_data_len
)
926 memcpy(req_msg
->private_data
, param
->private_data
,
927 param
->private_data_len
);
930 static int cm_validate_req_param(struct ib_cm_req_param
*param
)
932 /* peer-to-peer not supported */
933 if (param
->peer_to_peer
)
936 if (!param
->primary_path
)
939 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
942 if (param
->private_data
&&
943 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
946 if (param
->alternate_path
&&
947 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
948 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
954 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
955 struct ib_cm_req_param
*param
)
957 struct cm_id_private
*cm_id_priv
;
958 struct cm_req_msg
*req_msg
;
962 ret
= cm_validate_req_param(param
);
966 /* Verify that we're not in timewait. */
967 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
968 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
969 if (cm_id
->state
!= IB_CM_IDLE
) {
970 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
974 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
976 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
978 if (IS_ERR(cm_id_priv
->timewait_info
))
981 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
984 if (param
->alternate_path
) {
985 ret
= cm_init_av_by_path(param
->alternate_path
,
986 &cm_id_priv
->alt_av
);
990 cm_id
->service_id
= param
->service_id
;
991 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
992 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
993 param
->primary_path
->packet_life_time
) * 2 +
995 param
->remote_cm_response_timeout
);
996 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
997 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
998 cm_id_priv
->responder_resources
= param
->responder_resources
;
999 cm_id_priv
->retry_count
= param
->retry_count
;
1000 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
1001 cm_id_priv
->qp_type
= param
->qp_type
;
1003 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
1007 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
1008 cm_format_req(req_msg
, cm_id_priv
, param
);
1009 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1010 cm_id_priv
->msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1011 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
1013 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
1014 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
1015 cm_id_priv
->local_ack_timeout
=
1016 cm_req_get_primary_local_ack_timeout(req_msg
);
1018 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1019 ret
= ib_post_send_mad(cm_id_priv
->msg
, NULL
);
1021 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1024 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
1025 cm_id
->state
= IB_CM_REQ_SENT
;
1026 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1029 error2
: cm_free_msg(cm_id_priv
->msg
);
1030 error1
: kfree(cm_id_priv
->timewait_info
);
1033 EXPORT_SYMBOL(ib_send_cm_req
);
1035 static int cm_issue_rej(struct cm_port
*port
,
1036 struct ib_mad_recv_wc
*mad_recv_wc
,
1037 enum ib_cm_rej_reason reason
,
1038 enum cm_msg_response msg_rejected
,
1039 void *ari
, u8 ari_length
)
1041 struct ib_mad_send_buf
*msg
= NULL
;
1042 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
1045 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
1049 /* We just need common CM header information. Cast to any message. */
1050 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
1051 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
1053 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
1054 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
1055 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
1056 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
1057 rej_msg
->reason
= cpu_to_be16(reason
);
1059 if (ari
&& ari_length
) {
1060 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1061 memcpy(rej_msg
->ari
, ari
, ari_length
);
1064 ret
= ib_post_send_mad(msg
, NULL
);
1071 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
1072 __be32 local_qpn
, __be32 remote_qpn
)
1074 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
1075 ((local_ca_guid
== remote_ca_guid
) &&
1076 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
1079 static void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
1080 struct ib_sa_path_rec
*primary_path
,
1081 struct ib_sa_path_rec
*alt_path
)
1083 memset(primary_path
, 0, sizeof *primary_path
);
1084 primary_path
->dgid
= req_msg
->primary_local_gid
;
1085 primary_path
->sgid
= req_msg
->primary_remote_gid
;
1086 primary_path
->dlid
= req_msg
->primary_local_lid
;
1087 primary_path
->slid
= req_msg
->primary_remote_lid
;
1088 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1089 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1090 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1091 primary_path
->reversible
= 1;
1092 primary_path
->pkey
= req_msg
->pkey
;
1093 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1094 primary_path
->mtu_selector
= IB_SA_EQ
;
1095 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1096 primary_path
->rate_selector
= IB_SA_EQ
;
1097 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1098 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1099 primary_path
->packet_life_time
=
1100 cm_req_get_primary_local_ack_timeout(req_msg
);
1101 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1103 if (req_msg
->alt_local_lid
) {
1104 memset(alt_path
, 0, sizeof *alt_path
);
1105 alt_path
->dgid
= req_msg
->alt_local_gid
;
1106 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1107 alt_path
->dlid
= req_msg
->alt_local_lid
;
1108 alt_path
->slid
= req_msg
->alt_remote_lid
;
1109 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1110 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1111 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1112 alt_path
->reversible
= 1;
1113 alt_path
->pkey
= req_msg
->pkey
;
1114 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1115 alt_path
->mtu_selector
= IB_SA_EQ
;
1116 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1117 alt_path
->rate_selector
= IB_SA_EQ
;
1118 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1119 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1120 alt_path
->packet_life_time
=
1121 cm_req_get_alt_local_ack_timeout(req_msg
);
1122 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1126 static void cm_format_req_event(struct cm_work
*work
,
1127 struct cm_id_private
*cm_id_priv
,
1128 struct ib_cm_id
*listen_id
)
1130 struct cm_req_msg
*req_msg
;
1131 struct ib_cm_req_event_param
*param
;
1133 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1134 param
= &work
->cm_event
.param
.req_rcvd
;
1135 param
->listen_id
= listen_id
;
1136 param
->port
= cm_id_priv
->av
.port
->port_num
;
1137 param
->primary_path
= &work
->path
[0];
1138 if (req_msg
->alt_local_lid
)
1139 param
->alternate_path
= &work
->path
[1];
1141 param
->alternate_path
= NULL
;
1142 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1143 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1144 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1145 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1146 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1147 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1148 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1149 param
->local_cm_response_timeout
=
1150 cm_req_get_remote_resp_timeout(req_msg
);
1151 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1152 param
->remote_cm_response_timeout
=
1153 cm_req_get_local_resp_timeout(req_msg
);
1154 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1155 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1156 param
->srq
= cm_req_get_srq(req_msg
);
1157 work
->cm_event
.private_data
= &req_msg
->private_data
;
1160 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1161 struct cm_work
*work
)
1163 unsigned long flags
;
1166 /* We will typically only have the current event to report. */
1167 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1170 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1171 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1172 work
= cm_dequeue_work(cm_id_priv
);
1173 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1175 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1179 cm_deref_id(cm_id_priv
);
1181 cm_destroy_id(&cm_id_priv
->id
, ret
);
1184 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1185 struct cm_id_private
*cm_id_priv
,
1186 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1187 const void *private_data
, u8 private_data_len
)
1189 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1190 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1191 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1192 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1193 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1195 if (private_data
&& private_data_len
)
1196 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1199 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1200 struct cm_id_private
*cm_id_priv
,
1201 enum ib_cm_rej_reason reason
,
1204 const void *private_data
,
1205 u8 private_data_len
)
1207 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1208 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1210 switch(cm_id_priv
->id
.state
) {
1211 case IB_CM_REQ_RCVD
:
1212 rej_msg
->local_comm_id
= 0;
1213 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1215 case IB_CM_MRA_REQ_SENT
:
1216 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1217 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1219 case IB_CM_REP_RCVD
:
1220 case IB_CM_MRA_REP_SENT
:
1221 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1222 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1225 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1226 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1230 rej_msg
->reason
= cpu_to_be16(reason
);
1231 if (ari
&& ari_length
) {
1232 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1233 memcpy(rej_msg
->ari
, ari
, ari_length
);
1236 if (private_data
&& private_data_len
)
1237 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1240 static void cm_dup_req_handler(struct cm_work
*work
,
1241 struct cm_id_private
*cm_id_priv
)
1243 struct ib_mad_send_buf
*msg
= NULL
;
1244 unsigned long flags
;
1247 /* Quick state check to discard duplicate REQs. */
1248 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1251 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1255 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1256 switch (cm_id_priv
->id
.state
) {
1257 case IB_CM_MRA_REQ_SENT
:
1258 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1259 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1260 cm_id_priv
->private_data
,
1261 cm_id_priv
->private_data_len
);
1263 case IB_CM_TIMEWAIT
:
1264 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1265 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1270 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1272 ret
= ib_post_send_mad(msg
, NULL
);
1277 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1278 free
: cm_free_msg(msg
);
1281 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1282 struct cm_id_private
*cm_id_priv
)
1284 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1285 struct cm_timewait_info
*timewait_info
;
1286 struct cm_req_msg
*req_msg
;
1287 unsigned long flags
;
1289 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1291 /* Check for duplicate REQ and stale connections. */
1292 spin_lock_irqsave(&cm
.lock
, flags
);
1293 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1295 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1297 if (timewait_info
) {
1298 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1299 timewait_info
->work
.remote_id
);
1300 spin_unlock_irqrestore(&cm
.lock
, flags
);
1301 if (cur_cm_id_priv
) {
1302 cm_dup_req_handler(work
, cur_cm_id_priv
);
1303 cm_deref_id(cur_cm_id_priv
);
1305 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1306 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1311 /* Find matching listen request. */
1312 listen_cm_id_priv
= cm_find_listen(cm_id_priv
->id
.device
,
1313 req_msg
->service_id
,
1314 req_msg
->private_data
);
1315 if (!listen_cm_id_priv
) {
1316 spin_unlock_irqrestore(&cm
.lock
, flags
);
1317 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1318 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1322 atomic_inc(&listen_cm_id_priv
->refcount
);
1323 atomic_inc(&cm_id_priv
->refcount
);
1324 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1325 atomic_inc(&cm_id_priv
->work_count
);
1326 spin_unlock_irqrestore(&cm
.lock
, flags
);
1327 return listen_cm_id_priv
;
1329 error
: cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1333 static int cm_req_handler(struct cm_work
*work
)
1335 struct ib_cm_id
*cm_id
;
1336 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1337 struct cm_req_msg
*req_msg
;
1340 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1342 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
1344 return PTR_ERR(cm_id
);
1346 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1347 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1348 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1349 work
->mad_recv_wc
->recv_buf
.grh
,
1351 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1353 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1354 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1357 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1358 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1359 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1361 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1362 if (!listen_cm_id_priv
) {
1367 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1368 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1369 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1370 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
1372 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1373 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1376 if (req_msg
->alt_local_lid
) {
1377 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1381 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1382 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1383 cm_req_get_local_resp_timeout(req_msg
));
1384 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1385 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1386 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1387 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1388 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1389 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1390 cm_id_priv
->local_ack_timeout
=
1391 cm_req_get_primary_local_ack_timeout(req_msg
);
1392 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1393 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1394 cm_id_priv
->qp_type
= cm_req_get_qp_type(req_msg
);
1396 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1397 cm_process_work(cm_id_priv
, work
);
1398 cm_deref_id(listen_cm_id_priv
);
1401 error3
: atomic_dec(&cm_id_priv
->refcount
);
1402 cm_deref_id(listen_cm_id_priv
);
1403 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1404 error2
: kfree(cm_id_priv
->timewait_info
);
1405 cm_id_priv
->timewait_info
= NULL
;
1406 error1
: ib_destroy_cm_id(&cm_id_priv
->id
);
1410 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1411 struct cm_id_private
*cm_id_priv
,
1412 struct ib_cm_rep_param
*param
)
1414 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1415 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1416 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1417 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1418 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1419 rep_msg
->resp_resources
= param
->responder_resources
;
1420 rep_msg
->initiator_depth
= param
->initiator_depth
;
1421 cm_rep_set_target_ack_delay(rep_msg
, param
->target_ack_delay
);
1422 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1423 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1424 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1425 cm_rep_set_srq(rep_msg
, param
->srq
);
1426 rep_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
1428 if (param
->private_data
&& param
->private_data_len
)
1429 memcpy(rep_msg
->private_data
, param
->private_data
,
1430 param
->private_data_len
);
1433 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1434 struct ib_cm_rep_param
*param
)
1436 struct cm_id_private
*cm_id_priv
;
1437 struct ib_mad_send_buf
*msg
;
1438 struct cm_rep_msg
*rep_msg
;
1439 unsigned long flags
;
1442 if (param
->private_data
&&
1443 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1446 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1447 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1448 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1449 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1454 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1458 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1459 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1460 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1461 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1463 ret
= ib_post_send_mad(msg
, NULL
);
1465 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1470 cm_id
->state
= IB_CM_REP_SENT
;
1471 cm_id_priv
->msg
= msg
;
1472 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1473 cm_id_priv
->responder_resources
= param
->responder_resources
;
1474 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1475 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1477 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1480 EXPORT_SYMBOL(ib_send_cm_rep
);
1482 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1483 struct cm_id_private
*cm_id_priv
,
1484 const void *private_data
,
1485 u8 private_data_len
)
1487 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1488 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1489 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1491 if (private_data
&& private_data_len
)
1492 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1495 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1496 const void *private_data
,
1497 u8 private_data_len
)
1499 struct cm_id_private
*cm_id_priv
;
1500 struct ib_mad_send_buf
*msg
;
1501 unsigned long flags
;
1505 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1508 data
= cm_copy_private_data(private_data
, private_data_len
);
1510 return PTR_ERR(data
);
1512 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1513 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1514 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1515 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1520 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1524 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1525 private_data
, private_data_len
);
1527 ret
= ib_post_send_mad(msg
, NULL
);
1529 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1535 cm_id
->state
= IB_CM_ESTABLISHED
;
1536 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1537 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1540 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1544 EXPORT_SYMBOL(ib_send_cm_rtu
);
1546 static void cm_format_rep_event(struct cm_work
*work
)
1548 struct cm_rep_msg
*rep_msg
;
1549 struct ib_cm_rep_event_param
*param
;
1551 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1552 param
= &work
->cm_event
.param
.rep_rcvd
;
1553 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1554 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1555 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1556 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1557 param
->responder_resources
= rep_msg
->initiator_depth
;
1558 param
->initiator_depth
= rep_msg
->resp_resources
;
1559 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1560 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1561 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1562 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1563 param
->srq
= cm_rep_get_srq(rep_msg
);
1564 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1567 static void cm_dup_rep_handler(struct cm_work
*work
)
1569 struct cm_id_private
*cm_id_priv
;
1570 struct cm_rep_msg
*rep_msg
;
1571 struct ib_mad_send_buf
*msg
= NULL
;
1572 unsigned long flags
;
1575 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1576 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1577 rep_msg
->local_comm_id
);
1581 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1585 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1586 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1587 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1588 cm_id_priv
->private_data
,
1589 cm_id_priv
->private_data_len
);
1590 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1591 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1592 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1593 cm_id_priv
->private_data
,
1594 cm_id_priv
->private_data_len
);
1597 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1599 ret
= ib_post_send_mad(msg
, NULL
);
1604 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1605 free
: cm_free_msg(msg
);
1606 deref
: cm_deref_id(cm_id_priv
);
1609 static int cm_rep_handler(struct cm_work
*work
)
1611 struct cm_id_private
*cm_id_priv
;
1612 struct cm_rep_msg
*rep_msg
;
1613 unsigned long flags
;
1616 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1617 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1619 cm_dup_rep_handler(work
);
1623 cm_format_rep_event(work
);
1625 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1626 switch (cm_id_priv
->id
.state
) {
1627 case IB_CM_REQ_SENT
:
1628 case IB_CM_MRA_REQ_RCVD
:
1631 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1636 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1637 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1638 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1640 spin_lock(&cm
.lock
);
1641 /* Check for duplicate REP. */
1642 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1643 spin_unlock(&cm
.lock
);
1644 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1648 /* Check for a stale connection. */
1649 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1650 rb_erase(&cm_id_priv
->timewait_info
->remote_id_node
,
1651 &cm
.remote_id_table
);
1652 cm_id_priv
->timewait_info
->inserted_remote_id
= 0;
1653 spin_unlock(&cm
.lock
);
1654 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1655 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1656 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1661 spin_unlock(&cm
.lock
);
1663 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1664 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1665 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1666 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1667 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1668 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1669 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1671 /* todo: handle peer_to_peer */
1673 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1674 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1676 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1677 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1680 cm_process_work(cm_id_priv
, work
);
1682 cm_deref_id(cm_id_priv
);
1686 cm_deref_id(cm_id_priv
);
1690 static int cm_establish_handler(struct cm_work
*work
)
1692 struct cm_id_private
*cm_id_priv
;
1693 unsigned long flags
;
1696 /* See comment in ib_cm_establish about lookup. */
1697 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1701 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1702 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1703 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1707 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1708 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1710 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1711 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1714 cm_process_work(cm_id_priv
, work
);
1716 cm_deref_id(cm_id_priv
);
1719 cm_deref_id(cm_id_priv
);
1723 static int cm_rtu_handler(struct cm_work
*work
)
1725 struct cm_id_private
*cm_id_priv
;
1726 struct cm_rtu_msg
*rtu_msg
;
1727 unsigned long flags
;
1730 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1731 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1732 rtu_msg
->local_comm_id
);
1736 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1738 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1739 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1740 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1741 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1744 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1746 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1747 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1749 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1750 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1753 cm_process_work(cm_id_priv
, work
);
1755 cm_deref_id(cm_id_priv
);
1758 cm_deref_id(cm_id_priv
);
1762 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1763 struct cm_id_private
*cm_id_priv
,
1764 const void *private_data
,
1765 u8 private_data_len
)
1767 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1768 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1769 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1770 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1771 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1773 if (private_data
&& private_data_len
)
1774 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1777 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1778 const void *private_data
,
1779 u8 private_data_len
)
1781 struct cm_id_private
*cm_id_priv
;
1782 struct ib_mad_send_buf
*msg
;
1783 unsigned long flags
;
1786 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1789 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1790 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1791 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1796 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1798 cm_enter_timewait(cm_id_priv
);
1802 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
1803 private_data
, private_data_len
);
1804 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
1805 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
1807 ret
= ib_post_send_mad(msg
, NULL
);
1809 cm_enter_timewait(cm_id_priv
);
1810 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1815 cm_id
->state
= IB_CM_DREQ_SENT
;
1816 cm_id_priv
->msg
= msg
;
1817 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1820 EXPORT_SYMBOL(ib_send_cm_dreq
);
1822 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
1823 struct cm_id_private
*cm_id_priv
,
1824 const void *private_data
,
1825 u8 private_data_len
)
1827 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
1828 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1829 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1831 if (private_data
&& private_data_len
)
1832 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
1835 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
1836 const void *private_data
,
1837 u8 private_data_len
)
1839 struct cm_id_private
*cm_id_priv
;
1840 struct ib_mad_send_buf
*msg
;
1841 unsigned long flags
;
1845 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
1848 data
= cm_copy_private_data(private_data
, private_data_len
);
1850 return PTR_ERR(data
);
1852 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1853 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1854 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
1855 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1860 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1861 cm_enter_timewait(cm_id_priv
);
1863 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1867 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1868 private_data
, private_data_len
);
1870 ret
= ib_post_send_mad(msg
, NULL
);
1872 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1877 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1880 EXPORT_SYMBOL(ib_send_cm_drep
);
1882 static int cm_dreq_handler(struct cm_work
*work
)
1884 struct cm_id_private
*cm_id_priv
;
1885 struct cm_dreq_msg
*dreq_msg
;
1886 struct ib_mad_send_buf
*msg
= NULL
;
1887 unsigned long flags
;
1890 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1891 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
1892 dreq_msg
->local_comm_id
);
1896 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
1898 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1899 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
1902 switch (cm_id_priv
->id
.state
) {
1903 case IB_CM_REP_SENT
:
1904 case IB_CM_DREQ_SENT
:
1905 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1907 case IB_CM_ESTABLISHED
:
1908 case IB_CM_MRA_REP_RCVD
:
1910 case IB_CM_TIMEWAIT
:
1911 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
1914 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1915 cm_id_priv
->private_data
,
1916 cm_id_priv
->private_data_len
);
1917 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1919 if (ib_post_send_mad(msg
, NULL
))
1925 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
1926 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
1927 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1929 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1930 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1933 cm_process_work(cm_id_priv
, work
);
1935 cm_deref_id(cm_id_priv
);
1938 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1939 deref
: cm_deref_id(cm_id_priv
);
1943 static int cm_drep_handler(struct cm_work
*work
)
1945 struct cm_id_private
*cm_id_priv
;
1946 struct cm_drep_msg
*drep_msg
;
1947 unsigned long flags
;
1950 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1951 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
1952 drep_msg
->local_comm_id
);
1956 work
->cm_event
.private_data
= &drep_msg
->private_data
;
1958 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1959 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
1960 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
1961 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1964 cm_enter_timewait(cm_id_priv
);
1966 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
1967 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1969 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1970 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1973 cm_process_work(cm_id_priv
, work
);
1975 cm_deref_id(cm_id_priv
);
1978 cm_deref_id(cm_id_priv
);
1982 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
1983 enum ib_cm_rej_reason reason
,
1986 const void *private_data
,
1987 u8 private_data_len
)
1989 struct cm_id_private
*cm_id_priv
;
1990 struct ib_mad_send_buf
*msg
;
1991 unsigned long flags
;
1994 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
1995 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
1998 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2000 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2001 switch (cm_id
->state
) {
2002 case IB_CM_REQ_SENT
:
2003 case IB_CM_MRA_REQ_RCVD
:
2004 case IB_CM_REQ_RCVD
:
2005 case IB_CM_MRA_REQ_SENT
:
2006 case IB_CM_REP_RCVD
:
2007 case IB_CM_MRA_REP_SENT
:
2008 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2010 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2011 cm_id_priv
, reason
, ari
, ari_length
,
2012 private_data
, private_data_len
);
2014 cm_reset_to_idle(cm_id_priv
);
2016 case IB_CM_REP_SENT
:
2017 case IB_CM_MRA_REP_RCVD
:
2018 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2020 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
2021 cm_id_priv
, reason
, ari
, ari_length
,
2022 private_data
, private_data_len
);
2024 cm_enter_timewait(cm_id_priv
);
2034 ret
= ib_post_send_mad(msg
, NULL
);
2038 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2041 EXPORT_SYMBOL(ib_send_cm_rej
);
2043 static void cm_format_rej_event(struct cm_work
*work
)
2045 struct cm_rej_msg
*rej_msg
;
2046 struct ib_cm_rej_event_param
*param
;
2048 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2049 param
= &work
->cm_event
.param
.rej_rcvd
;
2050 param
->ari
= rej_msg
->ari
;
2051 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
2052 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
2053 work
->cm_event
.private_data
= &rej_msg
->private_data
;
2056 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
2058 struct cm_timewait_info
*timewait_info
;
2059 struct cm_id_private
*cm_id_priv
;
2060 unsigned long flags
;
2063 remote_id
= rej_msg
->local_comm_id
;
2065 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
2066 spin_lock_irqsave(&cm
.lock
, flags
);
2067 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
2069 if (!timewait_info
) {
2070 spin_unlock_irqrestore(&cm
.lock
, flags
);
2073 cm_id_priv
= idr_find(&cm
.local_id_table
,
2074 (__force
int) timewait_info
->work
.local_id
);
2076 if (cm_id_priv
->id
.remote_id
== remote_id
)
2077 atomic_inc(&cm_id_priv
->refcount
);
2081 spin_unlock_irqrestore(&cm
.lock
, flags
);
2082 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2083 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2085 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2090 static int cm_rej_handler(struct cm_work
*work
)
2092 struct cm_id_private
*cm_id_priv
;
2093 struct cm_rej_msg
*rej_msg
;
2094 unsigned long flags
;
2097 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2098 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2102 cm_format_rej_event(work
);
2104 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2105 switch (cm_id_priv
->id
.state
) {
2106 case IB_CM_REQ_SENT
:
2107 case IB_CM_MRA_REQ_RCVD
:
2108 case IB_CM_REP_SENT
:
2109 case IB_CM_MRA_REP_RCVD
:
2110 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2112 case IB_CM_REQ_RCVD
:
2113 case IB_CM_MRA_REQ_SENT
:
2114 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2115 cm_enter_timewait(cm_id_priv
);
2117 cm_reset_to_idle(cm_id_priv
);
2119 case IB_CM_DREQ_SENT
:
2120 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2122 case IB_CM_REP_RCVD
:
2123 case IB_CM_MRA_REP_SENT
:
2124 case IB_CM_ESTABLISHED
:
2125 cm_enter_timewait(cm_id_priv
);
2128 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2133 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2135 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2136 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2139 cm_process_work(cm_id_priv
, work
);
2141 cm_deref_id(cm_id_priv
);
2144 cm_deref_id(cm_id_priv
);
2148 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2150 const void *private_data
,
2151 u8 private_data_len
)
2153 struct cm_id_private
*cm_id_priv
;
2154 struct ib_mad_send_buf
*msg
;
2156 unsigned long flags
;
2159 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2162 data
= cm_copy_private_data(private_data
, private_data_len
);
2164 return PTR_ERR(data
);
2166 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2168 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2169 switch(cm_id_priv
->id
.state
) {
2170 case IB_CM_REQ_RCVD
:
2171 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2175 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2176 CM_MSG_RESPONSE_REQ
, service_timeout
,
2177 private_data
, private_data_len
);
2178 ret
= ib_post_send_mad(msg
, NULL
);
2181 cm_id
->state
= IB_CM_MRA_REQ_SENT
;
2183 case IB_CM_REP_RCVD
:
2184 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2188 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2189 CM_MSG_RESPONSE_REP
, service_timeout
,
2190 private_data
, private_data_len
);
2191 ret
= ib_post_send_mad(msg
, NULL
);
2194 cm_id
->state
= IB_CM_MRA_REP_SENT
;
2196 case IB_CM_ESTABLISHED
:
2197 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2201 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2202 CM_MSG_RESPONSE_OTHER
, service_timeout
,
2203 private_data
, private_data_len
);
2204 ret
= ib_post_send_mad(msg
, NULL
);
2207 cm_id
->lap_state
= IB_CM_MRA_LAP_SENT
;
2213 cm_id_priv
->service_timeout
= service_timeout
;
2214 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2215 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2218 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2222 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2227 EXPORT_SYMBOL(ib_send_cm_mra
);
2229 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2231 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2232 case CM_MSG_RESPONSE_REQ
:
2233 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2234 case CM_MSG_RESPONSE_REP
:
2235 case CM_MSG_RESPONSE_OTHER
:
2236 return cm_acquire_id(mra_msg
->remote_comm_id
,
2237 mra_msg
->local_comm_id
);
2243 static int cm_mra_handler(struct cm_work
*work
)
2245 struct cm_id_private
*cm_id_priv
;
2246 struct cm_mra_msg
*mra_msg
;
2247 unsigned long flags
;
2250 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2251 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2255 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2256 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2257 cm_mra_get_service_timeout(mra_msg
);
2258 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2259 cm_convert_to_ms(cm_id_priv
->av
.packet_life_time
);
2261 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2262 switch (cm_id_priv
->id
.state
) {
2263 case IB_CM_REQ_SENT
:
2264 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2265 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2266 cm_id_priv
->msg
, timeout
))
2268 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2270 case IB_CM_REP_SENT
:
2271 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2272 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2273 cm_id_priv
->msg
, timeout
))
2275 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2277 case IB_CM_ESTABLISHED
:
2278 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2279 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2280 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2281 cm_id_priv
->msg
, timeout
))
2283 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2289 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2290 cm_id_priv
->id
.state
;
2291 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2293 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2294 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2297 cm_process_work(cm_id_priv
, work
);
2299 cm_deref_id(cm_id_priv
);
2302 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2303 cm_deref_id(cm_id_priv
);
2307 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2308 struct cm_id_private
*cm_id_priv
,
2309 struct ib_sa_path_rec
*alternate_path
,
2310 const void *private_data
,
2311 u8 private_data_len
)
2313 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2314 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2315 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2316 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2317 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2318 /* todo: need remote CM response timeout */
2319 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2320 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2321 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2322 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2323 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2324 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2325 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2326 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2327 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2328 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2329 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2330 cm_lap_set_local_ack_timeout(lap_msg
,
2331 min(31, alternate_path
->packet_life_time
+ 1));
2333 if (private_data
&& private_data_len
)
2334 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2337 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2338 struct ib_sa_path_rec
*alternate_path
,
2339 const void *private_data
,
2340 u8 private_data_len
)
2342 struct cm_id_private
*cm_id_priv
;
2343 struct ib_mad_send_buf
*msg
;
2344 unsigned long flags
;
2347 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2350 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2351 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2352 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2353 cm_id
->lap_state
!= IB_CM_LAP_IDLE
) {
2358 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2362 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2363 alternate_path
, private_data
, private_data_len
);
2364 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2365 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2367 ret
= ib_post_send_mad(msg
, NULL
);
2369 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2374 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2375 cm_id_priv
->msg
= msg
;
2377 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2380 EXPORT_SYMBOL(ib_send_cm_lap
);
2382 static void cm_format_path_from_lap(struct ib_sa_path_rec
*path
,
2383 struct cm_lap_msg
*lap_msg
)
2385 memset(path
, 0, sizeof *path
);
2386 path
->dgid
= lap_msg
->alt_local_gid
;
2387 path
->sgid
= lap_msg
->alt_remote_gid
;
2388 path
->dlid
= lap_msg
->alt_local_lid
;
2389 path
->slid
= lap_msg
->alt_remote_lid
;
2390 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2391 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2392 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2393 path
->reversible
= 1;
2394 /* pkey is same as in REQ */
2395 path
->sl
= cm_lap_get_sl(lap_msg
);
2396 path
->mtu_selector
= IB_SA_EQ
;
2397 /* mtu is same as in REQ */
2398 path
->rate_selector
= IB_SA_EQ
;
2399 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2400 path
->packet_life_time_selector
= IB_SA_EQ
;
2401 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2402 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2405 static int cm_lap_handler(struct cm_work
*work
)
2407 struct cm_id_private
*cm_id_priv
;
2408 struct cm_lap_msg
*lap_msg
;
2409 struct ib_cm_lap_event_param
*param
;
2410 struct ib_mad_send_buf
*msg
= NULL
;
2411 unsigned long flags
;
2414 /* todo: verify LAP request and send reject APR if invalid. */
2415 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2416 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2417 lap_msg
->local_comm_id
);
2421 param
= &work
->cm_event
.param
.lap_rcvd
;
2422 param
->alternate_path
= &work
->path
[0];
2423 cm_format_path_from_lap(param
->alternate_path
, lap_msg
);
2424 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2426 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2427 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2430 switch (cm_id_priv
->id
.lap_state
) {
2431 case IB_CM_LAP_IDLE
:
2433 case IB_CM_MRA_LAP_SENT
:
2434 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2437 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2438 CM_MSG_RESPONSE_OTHER
,
2439 cm_id_priv
->service_timeout
,
2440 cm_id_priv
->private_data
,
2441 cm_id_priv
->private_data_len
);
2442 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2444 if (ib_post_send_mad(msg
, NULL
))
2451 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2452 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2453 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2455 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2456 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2459 cm_process_work(cm_id_priv
, work
);
2461 cm_deref_id(cm_id_priv
);
2464 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2465 deref
: cm_deref_id(cm_id_priv
);
2469 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2470 struct cm_id_private
*cm_id_priv
,
2471 enum ib_cm_apr_status status
,
2474 const void *private_data
,
2475 u8 private_data_len
)
2477 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2478 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2479 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2480 apr_msg
->ap_status
= (u8
) status
;
2482 if (info
&& info_length
) {
2483 apr_msg
->info_length
= info_length
;
2484 memcpy(apr_msg
->info
, info
, info_length
);
2487 if (private_data
&& private_data_len
)
2488 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2491 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2492 enum ib_cm_apr_status status
,
2495 const void *private_data
,
2496 u8 private_data_len
)
2498 struct cm_id_private
*cm_id_priv
;
2499 struct ib_mad_send_buf
*msg
;
2500 unsigned long flags
;
2503 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2504 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2507 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2508 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2509 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2510 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2511 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2516 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2520 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2521 info
, info_length
, private_data
, private_data_len
);
2522 ret
= ib_post_send_mad(msg
, NULL
);
2524 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2529 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2530 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2533 EXPORT_SYMBOL(ib_send_cm_apr
);
2535 static int cm_apr_handler(struct cm_work
*work
)
2537 struct cm_id_private
*cm_id_priv
;
2538 struct cm_apr_msg
*apr_msg
;
2539 unsigned long flags
;
2542 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2543 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2544 apr_msg
->local_comm_id
);
2546 return -EINVAL
; /* Unmatched reply. */
2548 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2549 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2550 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2551 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2553 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2554 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2555 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2556 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2557 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2560 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2561 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2562 cm_id_priv
->msg
= NULL
;
2564 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2566 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2567 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2570 cm_process_work(cm_id_priv
, work
);
2572 cm_deref_id(cm_id_priv
);
2575 cm_deref_id(cm_id_priv
);
2579 static int cm_timewait_handler(struct cm_work
*work
)
2581 struct cm_timewait_info
*timewait_info
;
2582 struct cm_id_private
*cm_id_priv
;
2583 unsigned long flags
;
2586 timewait_info
= (struct cm_timewait_info
*)work
;
2587 cm_cleanup_timewait(timewait_info
);
2589 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2590 timewait_info
->work
.remote_id
);
2594 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2595 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2596 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2597 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2600 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2601 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2603 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2604 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2607 cm_process_work(cm_id_priv
, work
);
2609 cm_deref_id(cm_id_priv
);
2612 cm_deref_id(cm_id_priv
);
2616 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2617 struct cm_id_private
*cm_id_priv
,
2618 struct ib_cm_sidr_req_param
*param
)
2620 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2621 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2622 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2623 sidr_req_msg
->pkey
= cpu_to_be16(param
->path
->pkey
);
2624 sidr_req_msg
->service_id
= param
->service_id
;
2626 if (param
->private_data
&& param
->private_data_len
)
2627 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2628 param
->private_data_len
);
2631 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2632 struct ib_cm_sidr_req_param
*param
)
2634 struct cm_id_private
*cm_id_priv
;
2635 struct ib_mad_send_buf
*msg
;
2636 unsigned long flags
;
2639 if (!param
->path
|| (param
->private_data
&&
2640 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2643 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2644 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2648 cm_id
->service_id
= param
->service_id
;
2649 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
2650 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2651 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2652 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2656 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2658 msg
->timeout_ms
= cm_id_priv
->timeout_ms
;
2659 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2661 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2662 if (cm_id
->state
== IB_CM_IDLE
)
2663 ret
= ib_post_send_mad(msg
, NULL
);
2668 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2672 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2673 cm_id_priv
->msg
= msg
;
2674 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2678 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2680 static void cm_format_sidr_req_event(struct cm_work
*work
,
2681 struct ib_cm_id
*listen_id
)
2683 struct cm_sidr_req_msg
*sidr_req_msg
;
2684 struct ib_cm_sidr_req_event_param
*param
;
2686 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2687 work
->mad_recv_wc
->recv_buf
.mad
;
2688 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2689 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2690 param
->listen_id
= listen_id
;
2691 param
->port
= work
->port
->port_num
;
2692 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2695 static int cm_sidr_req_handler(struct cm_work
*work
)
2697 struct ib_cm_id
*cm_id
;
2698 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2699 struct cm_sidr_req_msg
*sidr_req_msg
;
2701 unsigned long flags
;
2703 cm_id
= ib_create_cm_id(work
->port
->cm_dev
->device
, NULL
, NULL
);
2705 return PTR_ERR(cm_id
);
2706 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2708 /* Record SGID/SLID and request ID for lookup. */
2709 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2710 work
->mad_recv_wc
->recv_buf
.mad
;
2711 wc
= work
->mad_recv_wc
->wc
;
2712 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2713 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2714 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2715 work
->mad_recv_wc
->recv_buf
.grh
,
2717 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2718 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
2719 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2720 atomic_inc(&cm_id_priv
->work_count
);
2722 spin_lock_irqsave(&cm
.lock
, flags
);
2723 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2724 if (cur_cm_id_priv
) {
2725 spin_unlock_irqrestore(&cm
.lock
, flags
);
2726 goto out
; /* Duplicate message. */
2728 cur_cm_id_priv
= cm_find_listen(cm_id
->device
,
2729 sidr_req_msg
->service_id
,
2730 sidr_req_msg
->private_data
);
2731 if (!cur_cm_id_priv
) {
2732 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2733 spin_unlock_irqrestore(&cm
.lock
, flags
);
2734 /* todo: reply with no match */
2735 goto out
; /* No match. */
2737 atomic_inc(&cur_cm_id_priv
->refcount
);
2738 spin_unlock_irqrestore(&cm
.lock
, flags
);
2740 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
2741 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
2742 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
2743 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
2745 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
2746 cm_process_work(cm_id_priv
, work
);
2747 cm_deref_id(cur_cm_id_priv
);
2750 ib_destroy_cm_id(&cm_id_priv
->id
);
2754 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
2755 struct cm_id_private
*cm_id_priv
,
2756 struct ib_cm_sidr_rep_param
*param
)
2758 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
2760 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
2761 sidr_rep_msg
->status
= param
->status
;
2762 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
2763 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
2764 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
2766 if (param
->info
&& param
->info_length
)
2767 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
2769 if (param
->private_data
&& param
->private_data_len
)
2770 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
2771 param
->private_data_len
);
2774 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
2775 struct ib_cm_sidr_rep_param
*param
)
2777 struct cm_id_private
*cm_id_priv
;
2778 struct ib_mad_send_buf
*msg
;
2779 unsigned long flags
;
2782 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
2783 (param
->private_data
&&
2784 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
2787 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2788 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2789 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
2794 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2798 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
2800 ret
= ib_post_send_mad(msg
, NULL
);
2802 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2806 cm_id
->state
= IB_CM_IDLE
;
2807 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2809 spin_lock_irqsave(&cm
.lock
, flags
);
2810 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2811 spin_unlock_irqrestore(&cm
.lock
, flags
);
2814 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2817 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
2819 static void cm_format_sidr_rep_event(struct cm_work
*work
)
2821 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2822 struct ib_cm_sidr_rep_event_param
*param
;
2824 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2825 work
->mad_recv_wc
->recv_buf
.mad
;
2826 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
2827 param
->status
= sidr_rep_msg
->status
;
2828 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
2829 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
2830 param
->info
= &sidr_rep_msg
->info
;
2831 param
->info_len
= sidr_rep_msg
->info_length
;
2832 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
2835 static int cm_sidr_rep_handler(struct cm_work
*work
)
2837 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2838 struct cm_id_private
*cm_id_priv
;
2839 unsigned long flags
;
2841 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2842 work
->mad_recv_wc
->recv_buf
.mad
;
2843 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
2845 return -EINVAL
; /* Unmatched reply. */
2847 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2848 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
2849 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2852 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2853 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
, cm_id_priv
->msg
);
2854 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2856 cm_format_sidr_rep_event(work
);
2857 cm_process_work(cm_id_priv
, work
);
2860 cm_deref_id(cm_id_priv
);
2864 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
2865 enum ib_wc_status wc_status
)
2867 struct cm_id_private
*cm_id_priv
;
2868 struct ib_cm_event cm_event
;
2869 enum ib_cm_state state
;
2870 unsigned long flags
;
2873 memset(&cm_event
, 0, sizeof cm_event
);
2874 cm_id_priv
= msg
->context
[0];
2876 /* Discard old sends or ones without a response. */
2877 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2878 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
2879 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
2883 case IB_CM_REQ_SENT
:
2884 case IB_CM_MRA_REQ_RCVD
:
2885 cm_reset_to_idle(cm_id_priv
);
2886 cm_event
.event
= IB_CM_REQ_ERROR
;
2888 case IB_CM_REP_SENT
:
2889 case IB_CM_MRA_REP_RCVD
:
2890 cm_reset_to_idle(cm_id_priv
);
2891 cm_event
.event
= IB_CM_REP_ERROR
;
2893 case IB_CM_DREQ_SENT
:
2894 cm_enter_timewait(cm_id_priv
);
2895 cm_event
.event
= IB_CM_DREQ_ERROR
;
2897 case IB_CM_SIDR_REQ_SENT
:
2898 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2899 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
2904 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2905 cm_event
.param
.send_status
= wc_status
;
2907 /* No other events can occur on the cm_id at this point. */
2908 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
2911 ib_destroy_cm_id(&cm_id_priv
->id
);
2914 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2918 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
2919 struct ib_mad_send_wc
*mad_send_wc
)
2921 struct ib_mad_send_buf
*msg
= mad_send_wc
->send_buf
;
2923 switch (mad_send_wc
->status
) {
2925 case IB_WC_WR_FLUSH_ERR
:
2929 if (msg
->context
[0] && msg
->context
[1])
2930 cm_process_send_error(msg
, mad_send_wc
->status
);
2937 static void cm_work_handler(void *data
)
2939 struct cm_work
*work
= data
;
2942 switch (work
->cm_event
.event
) {
2943 case IB_CM_REQ_RECEIVED
:
2944 ret
= cm_req_handler(work
);
2946 case IB_CM_MRA_RECEIVED
:
2947 ret
= cm_mra_handler(work
);
2949 case IB_CM_REJ_RECEIVED
:
2950 ret
= cm_rej_handler(work
);
2952 case IB_CM_REP_RECEIVED
:
2953 ret
= cm_rep_handler(work
);
2955 case IB_CM_RTU_RECEIVED
:
2956 ret
= cm_rtu_handler(work
);
2958 case IB_CM_USER_ESTABLISHED
:
2959 ret
= cm_establish_handler(work
);
2961 case IB_CM_DREQ_RECEIVED
:
2962 ret
= cm_dreq_handler(work
);
2964 case IB_CM_DREP_RECEIVED
:
2965 ret
= cm_drep_handler(work
);
2967 case IB_CM_SIDR_REQ_RECEIVED
:
2968 ret
= cm_sidr_req_handler(work
);
2970 case IB_CM_SIDR_REP_RECEIVED
:
2971 ret
= cm_sidr_rep_handler(work
);
2973 case IB_CM_LAP_RECEIVED
:
2974 ret
= cm_lap_handler(work
);
2976 case IB_CM_APR_RECEIVED
:
2977 ret
= cm_apr_handler(work
);
2979 case IB_CM_TIMEWAIT_EXIT
:
2980 ret
= cm_timewait_handler(work
);
2990 int ib_cm_establish(struct ib_cm_id
*cm_id
)
2992 struct cm_id_private
*cm_id_priv
;
2993 struct cm_work
*work
;
2994 unsigned long flags
;
2997 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
3001 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3002 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3003 switch (cm_id
->state
)
3005 case IB_CM_REP_SENT
:
3006 case IB_CM_MRA_REP_RCVD
:
3007 cm_id
->state
= IB_CM_ESTABLISHED
;
3009 case IB_CM_ESTABLISHED
:
3016 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3024 * The CM worker thread may try to destroy the cm_id before it
3025 * can execute this work item. To prevent potential deadlock,
3026 * we need to find the cm_id once we're in the context of the
3027 * worker thread, rather than holding a reference on it.
3029 INIT_WORK(&work
->work
, cm_work_handler
, work
);
3030 work
->local_id
= cm_id
->local_id
;
3031 work
->remote_id
= cm_id
->remote_id
;
3032 work
->mad_recv_wc
= NULL
;
3033 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
3034 queue_work(cm
.wq
, &work
->work
);
3038 EXPORT_SYMBOL(ib_cm_establish
);
3040 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
3041 struct ib_mad_recv_wc
*mad_recv_wc
)
3043 struct cm_work
*work
;
3044 enum ib_cm_event_type event
;
3047 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
3048 case CM_REQ_ATTR_ID
:
3049 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
3050 alt_local_lid
!= 0);
3051 event
= IB_CM_REQ_RECEIVED
;
3053 case CM_MRA_ATTR_ID
:
3054 event
= IB_CM_MRA_RECEIVED
;
3056 case CM_REJ_ATTR_ID
:
3057 event
= IB_CM_REJ_RECEIVED
;
3059 case CM_REP_ATTR_ID
:
3060 event
= IB_CM_REP_RECEIVED
;
3062 case CM_RTU_ATTR_ID
:
3063 event
= IB_CM_RTU_RECEIVED
;
3065 case CM_DREQ_ATTR_ID
:
3066 event
= IB_CM_DREQ_RECEIVED
;
3068 case CM_DREP_ATTR_ID
:
3069 event
= IB_CM_DREP_RECEIVED
;
3071 case CM_SIDR_REQ_ATTR_ID
:
3072 event
= IB_CM_SIDR_REQ_RECEIVED
;
3074 case CM_SIDR_REP_ATTR_ID
:
3075 event
= IB_CM_SIDR_REP_RECEIVED
;
3077 case CM_LAP_ATTR_ID
:
3079 event
= IB_CM_LAP_RECEIVED
;
3081 case CM_APR_ATTR_ID
:
3082 event
= IB_CM_APR_RECEIVED
;
3085 ib_free_recv_mad(mad_recv_wc
);
3089 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3092 ib_free_recv_mad(mad_recv_wc
);
3096 INIT_WORK(&work
->work
, cm_work_handler
, work
);
3097 work
->cm_event
.event
= event
;
3098 work
->mad_recv_wc
= mad_recv_wc
;
3099 work
->port
= (struct cm_port
*)mad_agent
->context
;
3100 queue_work(cm
.wq
, &work
->work
);
3103 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3104 struct ib_qp_attr
*qp_attr
,
3107 unsigned long flags
;
3110 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3111 switch (cm_id_priv
->id
.state
) {
3112 case IB_CM_REQ_SENT
:
3113 case IB_CM_MRA_REQ_RCVD
:
3114 case IB_CM_REQ_RCVD
:
3115 case IB_CM_MRA_REQ_SENT
:
3116 case IB_CM_REP_RCVD
:
3117 case IB_CM_MRA_REP_SENT
:
3118 case IB_CM_REP_SENT
:
3119 case IB_CM_MRA_REP_RCVD
:
3120 case IB_CM_ESTABLISHED
:
3121 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3122 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3123 qp_attr
->qp_access_flags
= IB_ACCESS_LOCAL_WRITE
|
3124 IB_ACCESS_REMOTE_WRITE
;
3125 if (cm_id_priv
->responder_resources
)
3126 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_READ
;
3127 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3128 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3135 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3139 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3140 struct ib_qp_attr
*qp_attr
,
3143 unsigned long flags
;
3146 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3147 switch (cm_id_priv
->id
.state
) {
3148 case IB_CM_REQ_RCVD
:
3149 case IB_CM_MRA_REQ_SENT
:
3150 case IB_CM_REP_RCVD
:
3151 case IB_CM_MRA_REP_SENT
:
3152 case IB_CM_REP_SENT
:
3153 case IB_CM_MRA_REP_RCVD
:
3154 case IB_CM_ESTABLISHED
:
3155 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3156 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
;
3157 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3158 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3159 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3160 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3161 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3162 *qp_attr_mask
|= IB_QP_MAX_DEST_RD_ATOMIC
|
3163 IB_QP_MIN_RNR_TIMER
;
3164 qp_attr
->max_dest_rd_atomic
=
3165 cm_id_priv
->responder_resources
;
3166 qp_attr
->min_rnr_timer
= 0;
3168 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3169 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3170 qp_attr
->alt_port_num
= cm_id_priv
->alt_av
.port
->port_num
;
3171 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3179 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3183 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3184 struct ib_qp_attr
*qp_attr
,
3187 unsigned long flags
;
3190 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3191 switch (cm_id_priv
->id
.state
) {
3192 case IB_CM_REP_RCVD
:
3193 case IB_CM_MRA_REP_SENT
:
3194 case IB_CM_REP_SENT
:
3195 case IB_CM_MRA_REP_RCVD
:
3196 case IB_CM_ESTABLISHED
:
3197 *qp_attr_mask
= IB_QP_STATE
| IB_QP_SQ_PSN
;
3198 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3199 if (cm_id_priv
->qp_type
== IB_QPT_RC
) {
3200 *qp_attr_mask
|= IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3202 IB_QP_MAX_QP_RD_ATOMIC
;
3203 qp_attr
->timeout
= cm_id_priv
->local_ack_timeout
;
3204 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3205 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3206 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3208 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3209 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3210 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3218 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3222 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3223 struct ib_qp_attr
*qp_attr
,
3226 struct cm_id_private
*cm_id_priv
;
3229 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3230 switch (qp_attr
->qp_state
) {
3232 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3235 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3238 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3246 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3248 static void cm_add_one(struct ib_device
*device
)
3250 struct cm_device
*cm_dev
;
3251 struct cm_port
*port
;
3252 struct ib_mad_reg_req reg_req
= {
3253 .mgmt_class
= IB_MGMT_CLASS_CM
,
3254 .mgmt_class_version
= IB_CM_CLASS_VERSION
3256 struct ib_port_modify port_modify
= {
3257 .set_port_cap_mask
= IB_PORT_CM_SUP
3259 unsigned long flags
;
3263 cm_dev
= kmalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3264 device
->phys_port_cnt
, GFP_KERNEL
);
3268 cm_dev
->device
= device
;
3269 cm_dev
->ca_guid
= device
->node_guid
;
3271 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3272 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3273 port
= &cm_dev
->port
[i
-1];
3274 port
->cm_dev
= cm_dev
;
3276 port
->mad_agent
= ib_register_mad_agent(device
, i
,
3283 if (IS_ERR(port
->mad_agent
))
3286 ret
= ib_modify_port(device
, i
, 0, &port_modify
);
3290 ib_set_client_data(device
, &cm_client
, cm_dev
);
3292 write_lock_irqsave(&cm
.device_lock
, flags
);
3293 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3294 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3298 ib_unregister_mad_agent(port
->mad_agent
);
3300 port_modify
.set_port_cap_mask
= 0;
3301 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3303 port
= &cm_dev
->port
[i
-1];
3304 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3305 ib_unregister_mad_agent(port
->mad_agent
);
3310 static void cm_remove_one(struct ib_device
*device
)
3312 struct cm_device
*cm_dev
;
3313 struct cm_port
*port
;
3314 struct ib_port_modify port_modify
= {
3315 .clr_port_cap_mask
= IB_PORT_CM_SUP
3317 unsigned long flags
;
3320 cm_dev
= ib_get_client_data(device
, &cm_client
);
3324 write_lock_irqsave(&cm
.device_lock
, flags
);
3325 list_del(&cm_dev
->list
);
3326 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3328 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3329 port
= &cm_dev
->port
[i
-1];
3330 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3331 ib_unregister_mad_agent(port
->mad_agent
);
3336 static int __init
ib_cm_init(void)
3340 memset(&cm
, 0, sizeof cm
);
3341 INIT_LIST_HEAD(&cm
.device_list
);
3342 rwlock_init(&cm
.device_lock
);
3343 spin_lock_init(&cm
.lock
);
3344 cm
.listen_service_table
= RB_ROOT
;
3345 cm
.listen_service_id
= __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3346 cm
.remote_id_table
= RB_ROOT
;
3347 cm
.remote_qp_table
= RB_ROOT
;
3348 cm
.remote_sidr_table
= RB_ROOT
;
3349 idr_init(&cm
.local_id_table
);
3350 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3352 cm
.wq
= create_workqueue("ib_cm");
3356 ret
= ib_register_client(&cm_client
);
3362 destroy_workqueue(cm
.wq
);
3366 static void __exit
ib_cm_cleanup(void)
3368 destroy_workqueue(cm
.wq
);
3369 ib_unregister_client(&cm_client
);
3370 idr_destroy(&cm
.local_id_table
);
3373 module_init(ib_cm_init
);
3374 module_exit(ib_cm_cleanup
);