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 2821 2005-07-08 17:07:28Z sean.hefty $
37 #include <linux/dma-mapping.h>
38 #include <linux/err.h>
39 #include <linux/idr.h>
40 #include <linux/interrupt.h>
41 #include <linux/pci.h>
42 #include <linux/rbtree.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
46 #include <rdma/ib_cache.h>
47 #include <rdma/ib_cm.h>
50 MODULE_AUTHOR("Sean Hefty");
51 MODULE_DESCRIPTION("InfiniBand CM");
52 MODULE_LICENSE("Dual BSD/GPL");
54 static void cm_add_one(struct ib_device
*device
);
55 static void cm_remove_one(struct ib_device
*device
);
57 static struct ib_client cm_client
= {
60 .remove
= cm_remove_one
65 struct list_head device_list
;
67 struct rb_root listen_service_table
;
68 u64 listen_service_id
;
69 /* struct rb_root peer_service_table; todo: fix peer to peer */
70 struct rb_root remote_qp_table
;
71 struct rb_root remote_id_table
;
72 struct rb_root remote_sidr_table
;
73 struct idr local_id_table
;
74 struct workqueue_struct
*wq
;
78 struct cm_device
*cm_dev
;
79 struct ib_mad_agent
*mad_agent
;
84 struct list_head list
;
85 struct ib_device
*device
;
87 struct cm_port port
[0];
93 struct ib_ah_attr ah_attr
;
99 struct work_struct work
;
100 struct list_head list
;
101 struct cm_port
*port
;
102 struct ib_mad_recv_wc
*mad_recv_wc
; /* Received MADs */
103 __be32 local_id
; /* Established / timewait */
105 struct ib_cm_event cm_event
;
106 struct ib_sa_path_rec path
[0];
109 struct cm_timewait_info
{
110 struct cm_work work
; /* Must be first. */
111 struct rb_node remote_qp_node
;
112 struct rb_node remote_id_node
;
113 __be64 remote_ca_guid
;
115 u8 inserted_remote_qp
;
116 u8 inserted_remote_id
;
119 struct cm_id_private
{
122 struct rb_node service_node
;
123 struct rb_node sidr_id_node
;
125 wait_queue_head_t wait
;
128 struct ib_mad_send_buf
*msg
;
129 struct cm_timewait_info
*timewait_info
;
130 /* todo: use alternate port on send failure */
141 enum ib_mtu path_mtu
;
145 u8 responder_resources
;
147 u8 local_ack_timeout
;
152 struct list_head work_list
;
156 static void cm_work_handler(void *data
);
158 static inline void cm_deref_id(struct cm_id_private
*cm_id_priv
)
160 if (atomic_dec_and_test(&cm_id_priv
->refcount
))
161 wake_up(&cm_id_priv
->wait
);
164 static int cm_alloc_msg(struct cm_id_private
*cm_id_priv
,
165 struct ib_mad_send_buf
**msg
)
167 struct ib_mad_agent
*mad_agent
;
168 struct ib_mad_send_buf
*m
;
171 mad_agent
= cm_id_priv
->av
.port
->mad_agent
;
172 ah
= ib_create_ah(mad_agent
->qp
->pd
, &cm_id_priv
->av
.ah_attr
);
176 m
= ib_create_send_mad(mad_agent
, 1, cm_id_priv
->av
.pkey_index
,
177 ah
, 0, sizeof(struct ib_mad_hdr
),
178 sizeof(struct ib_mad
)-sizeof(struct ib_mad_hdr
),
185 /* Timeout set by caller if response is expected. */
186 m
->send_wr
.wr
.ud
.retries
= cm_id_priv
->max_cm_retries
;
188 atomic_inc(&cm_id_priv
->refcount
);
189 m
->context
[0] = cm_id_priv
;
194 static int cm_alloc_response_msg(struct cm_port
*port
,
195 struct ib_mad_recv_wc
*mad_recv_wc
,
196 struct ib_mad_send_buf
**msg
)
198 struct ib_mad_send_buf
*m
;
201 ah
= ib_create_ah_from_wc(port
->mad_agent
->qp
->pd
, mad_recv_wc
->wc
,
202 mad_recv_wc
->recv_buf
.grh
, port
->port_num
);
206 m
= ib_create_send_mad(port
->mad_agent
, 1, mad_recv_wc
->wc
->pkey_index
,
207 ah
, 0, sizeof(struct ib_mad_hdr
),
208 sizeof(struct ib_mad
)-sizeof(struct ib_mad_hdr
),
218 static void cm_free_msg(struct ib_mad_send_buf
*msg
)
220 ib_destroy_ah(msg
->send_wr
.wr
.ud
.ah
);
222 cm_deref_id(msg
->context
[0]);
223 ib_free_send_mad(msg
);
226 static void * cm_copy_private_data(const void *private_data
,
231 if (!private_data
|| !private_data_len
)
234 data
= kmalloc(private_data_len
, GFP_KERNEL
);
236 return ERR_PTR(-ENOMEM
);
238 memcpy(data
, private_data
, private_data_len
);
242 static void cm_set_private_data(struct cm_id_private
*cm_id_priv
,
243 void *private_data
, u8 private_data_len
)
245 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
246 kfree(cm_id_priv
->private_data
);
248 cm_id_priv
->private_data
= private_data
;
249 cm_id_priv
->private_data_len
= private_data_len
;
252 static void cm_set_ah_attr(struct ib_ah_attr
*ah_attr
, u8 port_num
,
253 u16 dlid
, u8 sl
, u16 src_path_bits
)
255 memset(ah_attr
, 0, sizeof ah_attr
);
256 ah_attr
->dlid
= dlid
;
258 ah_attr
->src_path_bits
= src_path_bits
;
259 ah_attr
->port_num
= port_num
;
262 static void cm_init_av_for_response(struct cm_port
*port
,
263 struct ib_wc
*wc
, struct cm_av
*av
)
266 av
->pkey_index
= wc
->pkey_index
;
267 cm_set_ah_attr(&av
->ah_attr
, port
->port_num
, wc
->slid
,
268 wc
->sl
, wc
->dlid_path_bits
);
271 static int cm_init_av_by_path(struct ib_sa_path_rec
*path
, struct cm_av
*av
)
273 struct cm_device
*cm_dev
;
274 struct cm_port
*port
= NULL
;
279 read_lock_irqsave(&cm
.device_lock
, flags
);
280 list_for_each_entry(cm_dev
, &cm
.device_list
, list
) {
281 if (!ib_find_cached_gid(cm_dev
->device
, &path
->sgid
,
283 port
= &cm_dev
->port
[p
-1];
287 read_unlock_irqrestore(&cm
.device_lock
, flags
);
292 ret
= ib_find_cached_pkey(cm_dev
->device
, port
->port_num
,
293 be16_to_cpu(path
->pkey
), &av
->pkey_index
);
298 cm_set_ah_attr(&av
->ah_attr
, av
->port
->port_num
,
299 be16_to_cpu(path
->dlid
), path
->sl
,
300 be16_to_cpu(path
->slid
) & 0x7F);
301 av
->packet_life_time
= path
->packet_life_time
;
305 static int cm_alloc_id(struct cm_id_private
*cm_id_priv
)
311 spin_lock_irqsave(&cm
.lock
, flags
);
312 ret
= idr_get_new_above(&cm
.local_id_table
, cm_id_priv
, 1,
313 (__force
int *) &cm_id_priv
->id
.local_id
);
314 spin_unlock_irqrestore(&cm
.lock
, flags
);
315 } while( (ret
== -EAGAIN
) && idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
) );
319 static void cm_free_id(__be32 local_id
)
323 spin_lock_irqsave(&cm
.lock
, flags
);
324 idr_remove(&cm
.local_id_table
, (__force
int) local_id
);
325 spin_unlock_irqrestore(&cm
.lock
, flags
);
328 static struct cm_id_private
* cm_get_id(__be32 local_id
, __be32 remote_id
)
330 struct cm_id_private
*cm_id_priv
;
332 cm_id_priv
= idr_find(&cm
.local_id_table
, (__force
int) local_id
);
334 if (cm_id_priv
->id
.remote_id
== remote_id
)
335 atomic_inc(&cm_id_priv
->refcount
);
343 static struct cm_id_private
* cm_acquire_id(__be32 local_id
, __be32 remote_id
)
345 struct cm_id_private
*cm_id_priv
;
348 spin_lock_irqsave(&cm
.lock
, flags
);
349 cm_id_priv
= cm_get_id(local_id
, remote_id
);
350 spin_unlock_irqrestore(&cm
.lock
, flags
);
355 static struct cm_id_private
* cm_insert_listen(struct cm_id_private
*cm_id_priv
)
357 struct rb_node
**link
= &cm
.listen_service_table
.rb_node
;
358 struct rb_node
*parent
= NULL
;
359 struct cm_id_private
*cur_cm_id_priv
;
360 __be64 service_id
= cm_id_priv
->id
.service_id
;
361 __be64 service_mask
= cm_id_priv
->id
.service_mask
;
365 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
367 if ((cur_cm_id_priv
->id
.service_mask
& service_id
) ==
368 (service_mask
& cur_cm_id_priv
->id
.service_id
))
370 if (service_id
< cur_cm_id_priv
->id
.service_id
)
371 link
= &(*link
)->rb_left
;
373 link
= &(*link
)->rb_right
;
375 rb_link_node(&cm_id_priv
->service_node
, parent
, link
);
376 rb_insert_color(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
380 static struct cm_id_private
* cm_find_listen(__be64 service_id
)
382 struct rb_node
*node
= cm
.listen_service_table
.rb_node
;
383 struct cm_id_private
*cm_id_priv
;
386 cm_id_priv
= rb_entry(node
, struct cm_id_private
, service_node
);
387 if ((cm_id_priv
->id
.service_mask
& service_id
) ==
388 (cm_id_priv
->id
.service_mask
& cm_id_priv
->id
.service_id
))
390 if (service_id
< cm_id_priv
->id
.service_id
)
391 node
= node
->rb_left
;
393 node
= node
->rb_right
;
398 static struct cm_timewait_info
* cm_insert_remote_id(struct cm_timewait_info
401 struct rb_node
**link
= &cm
.remote_id_table
.rb_node
;
402 struct rb_node
*parent
= NULL
;
403 struct cm_timewait_info
*cur_timewait_info
;
404 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
405 __be32 remote_id
= timewait_info
->work
.remote_id
;
409 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
411 if (remote_id
< cur_timewait_info
->work
.remote_id
)
412 link
= &(*link
)->rb_left
;
413 else if (remote_id
> cur_timewait_info
->work
.remote_id
)
414 link
= &(*link
)->rb_right
;
415 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
416 link
= &(*link
)->rb_left
;
417 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
418 link
= &(*link
)->rb_right
;
420 return cur_timewait_info
;
422 timewait_info
->inserted_remote_id
= 1;
423 rb_link_node(&timewait_info
->remote_id_node
, parent
, link
);
424 rb_insert_color(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
428 static struct cm_timewait_info
* cm_find_remote_id(__be64 remote_ca_guid
,
431 struct rb_node
*node
= cm
.remote_id_table
.rb_node
;
432 struct cm_timewait_info
*timewait_info
;
435 timewait_info
= rb_entry(node
, struct cm_timewait_info
,
437 if (remote_id
< timewait_info
->work
.remote_id
)
438 node
= node
->rb_left
;
439 else if (remote_id
> timewait_info
->work
.remote_id
)
440 node
= node
->rb_right
;
441 else if (remote_ca_guid
< timewait_info
->remote_ca_guid
)
442 node
= node
->rb_left
;
443 else if (remote_ca_guid
> timewait_info
->remote_ca_guid
)
444 node
= node
->rb_right
;
446 return timewait_info
;
451 static struct cm_timewait_info
* cm_insert_remote_qpn(struct cm_timewait_info
454 struct rb_node
**link
= &cm
.remote_qp_table
.rb_node
;
455 struct rb_node
*parent
= NULL
;
456 struct cm_timewait_info
*cur_timewait_info
;
457 __be64 remote_ca_guid
= timewait_info
->remote_ca_guid
;
458 __be32 remote_qpn
= timewait_info
->remote_qpn
;
462 cur_timewait_info
= rb_entry(parent
, struct cm_timewait_info
,
464 if (remote_qpn
< cur_timewait_info
->remote_qpn
)
465 link
= &(*link
)->rb_left
;
466 else if (remote_qpn
> cur_timewait_info
->remote_qpn
)
467 link
= &(*link
)->rb_right
;
468 else if (remote_ca_guid
< cur_timewait_info
->remote_ca_guid
)
469 link
= &(*link
)->rb_left
;
470 else if (remote_ca_guid
> cur_timewait_info
->remote_ca_guid
)
471 link
= &(*link
)->rb_right
;
473 return cur_timewait_info
;
475 timewait_info
->inserted_remote_qp
= 1;
476 rb_link_node(&timewait_info
->remote_qp_node
, parent
, link
);
477 rb_insert_color(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
481 static struct cm_id_private
* cm_insert_remote_sidr(struct cm_id_private
484 struct rb_node
**link
= &cm
.remote_sidr_table
.rb_node
;
485 struct rb_node
*parent
= NULL
;
486 struct cm_id_private
*cur_cm_id_priv
;
487 union ib_gid
*port_gid
= &cm_id_priv
->av
.dgid
;
488 __be32 remote_id
= cm_id_priv
->id
.remote_id
;
492 cur_cm_id_priv
= rb_entry(parent
, struct cm_id_private
,
494 if (remote_id
< cur_cm_id_priv
->id
.remote_id
)
495 link
= &(*link
)->rb_left
;
496 else if (remote_id
> cur_cm_id_priv
->id
.remote_id
)
497 link
= &(*link
)->rb_right
;
500 cmp
= memcmp(port_gid
, &cur_cm_id_priv
->av
.dgid
,
503 link
= &(*link
)->rb_left
;
505 link
= &(*link
)->rb_right
;
507 return cur_cm_id_priv
;
510 rb_link_node(&cm_id_priv
->sidr_id_node
, parent
, link
);
511 rb_insert_color(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
515 static void cm_reject_sidr_req(struct cm_id_private
*cm_id_priv
,
516 enum ib_cm_sidr_status status
)
518 struct ib_cm_sidr_rep_param param
;
520 memset(¶m
, 0, sizeof param
);
521 param
.status
= status
;
522 ib_send_cm_sidr_rep(&cm_id_priv
->id
, ¶m
);
525 struct ib_cm_id
*ib_create_cm_id(ib_cm_handler cm_handler
,
528 struct cm_id_private
*cm_id_priv
;
531 cm_id_priv
= kmalloc(sizeof *cm_id_priv
, GFP_KERNEL
);
533 return ERR_PTR(-ENOMEM
);
535 memset(cm_id_priv
, 0, sizeof *cm_id_priv
);
536 cm_id_priv
->id
.state
= IB_CM_IDLE
;
537 cm_id_priv
->id
.cm_handler
= cm_handler
;
538 cm_id_priv
->id
.context
= context
;
539 ret
= cm_alloc_id(cm_id_priv
);
543 spin_lock_init(&cm_id_priv
->lock
);
544 init_waitqueue_head(&cm_id_priv
->wait
);
545 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
546 atomic_set(&cm_id_priv
->work_count
, -1);
547 atomic_set(&cm_id_priv
->refcount
, 1);
548 return &cm_id_priv
->id
;
552 return ERR_PTR(-ENOMEM
);
554 EXPORT_SYMBOL(ib_create_cm_id
);
556 static struct cm_work
* cm_dequeue_work(struct cm_id_private
*cm_id_priv
)
558 struct cm_work
*work
;
560 if (list_empty(&cm_id_priv
->work_list
))
563 work
= list_entry(cm_id_priv
->work_list
.next
, struct cm_work
, list
);
564 list_del(&work
->list
);
568 static void cm_free_work(struct cm_work
*work
)
570 if (work
->mad_recv_wc
)
571 ib_free_recv_mad(work
->mad_recv_wc
);
575 static inline int cm_convert_to_ms(int iba_time
)
577 /* approximate conversion to ms from 4.096us x 2^iba_time */
578 return 1 << max(iba_time
- 8, 0);
581 static void cm_cleanup_timewait(struct cm_timewait_info
*timewait_info
)
585 if (!timewait_info
->inserted_remote_id
&&
586 !timewait_info
->inserted_remote_qp
)
589 spin_lock_irqsave(&cm
.lock
, flags
);
590 if (timewait_info
->inserted_remote_id
) {
591 rb_erase(&timewait_info
->remote_id_node
, &cm
.remote_id_table
);
592 timewait_info
->inserted_remote_id
= 0;
595 if (timewait_info
->inserted_remote_qp
) {
596 rb_erase(&timewait_info
->remote_qp_node
, &cm
.remote_qp_table
);
597 timewait_info
->inserted_remote_qp
= 0;
599 spin_unlock_irqrestore(&cm
.lock
, flags
);
602 static struct cm_timewait_info
* cm_create_timewait_info(__be32 local_id
)
604 struct cm_timewait_info
*timewait_info
;
606 timewait_info
= kmalloc(sizeof *timewait_info
, GFP_KERNEL
);
608 return ERR_PTR(-ENOMEM
);
609 memset(timewait_info
, 0, sizeof *timewait_info
);
611 timewait_info
->work
.local_id
= local_id
;
612 INIT_WORK(&timewait_info
->work
.work
, cm_work_handler
,
613 &timewait_info
->work
);
614 timewait_info
->work
.cm_event
.event
= IB_CM_TIMEWAIT_EXIT
;
615 return timewait_info
;
618 static void cm_enter_timewait(struct cm_id_private
*cm_id_priv
)
623 * The cm_id could be destroyed by the user before we exit timewait.
624 * To protect against this, we search for the cm_id after exiting
625 * timewait before notifying the user that we've exited timewait.
627 cm_id_priv
->id
.state
= IB_CM_TIMEWAIT
;
628 wait_time
= cm_convert_to_ms(cm_id_priv
->local_ack_timeout
);
629 queue_delayed_work(cm
.wq
, &cm_id_priv
->timewait_info
->work
.work
,
630 msecs_to_jiffies(wait_time
));
631 cm_id_priv
->timewait_info
= NULL
;
634 static void cm_reset_to_idle(struct cm_id_private
*cm_id_priv
)
636 cm_id_priv
->id
.state
= IB_CM_IDLE
;
637 if (cm_id_priv
->timewait_info
) {
638 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
639 kfree(cm_id_priv
->timewait_info
);
640 cm_id_priv
->timewait_info
= NULL
;
644 void ib_destroy_cm_id(struct ib_cm_id
*cm_id
)
646 struct cm_id_private
*cm_id_priv
;
647 struct cm_work
*work
;
650 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
652 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
653 switch (cm_id
->state
) {
655 cm_id
->state
= IB_CM_IDLE
;
656 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
657 spin_lock_irqsave(&cm
.lock
, flags
);
658 rb_erase(&cm_id_priv
->service_node
, &cm
.listen_service_table
);
659 spin_unlock_irqrestore(&cm
.lock
, flags
);
661 case IB_CM_SIDR_REQ_SENT
:
662 cm_id
->state
= IB_CM_IDLE
;
663 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
664 (unsigned long) cm_id_priv
->msg
);
665 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
667 case IB_CM_SIDR_REQ_RCVD
:
668 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
669 cm_reject_sidr_req(cm_id_priv
, IB_SIDR_REJECT
);
672 case IB_CM_MRA_REQ_RCVD
:
674 case IB_CM_MRA_REP_RCVD
:
675 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
676 (unsigned long) cm_id_priv
->msg
);
679 case IB_CM_MRA_REQ_SENT
:
681 case IB_CM_MRA_REP_SENT
:
682 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
683 ib_send_cm_rej(cm_id
, IB_CM_REJ_TIMEOUT
,
684 &cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
685 sizeof cm_id_priv
->av
.port
->cm_dev
->ca_guid
,
688 case IB_CM_ESTABLISHED
:
689 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
690 ib_send_cm_dreq(cm_id
, NULL
, 0);
692 case IB_CM_DREQ_SENT
:
693 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
694 (unsigned long) cm_id_priv
->msg
);
695 cm_enter_timewait(cm_id_priv
);
696 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
698 case IB_CM_DREQ_RCVD
:
699 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
700 ib_send_cm_drep(cm_id
, NULL
, 0);
703 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
707 cm_free_id(cm_id
->local_id
);
708 atomic_dec(&cm_id_priv
->refcount
);
709 wait_event(cm_id_priv
->wait
, !atomic_read(&cm_id_priv
->refcount
));
710 while ((work
= cm_dequeue_work(cm_id_priv
)) != NULL
)
712 if (cm_id_priv
->private_data
&& cm_id_priv
->private_data_len
)
713 kfree(cm_id_priv
->private_data
);
716 EXPORT_SYMBOL(ib_destroy_cm_id
);
718 int ib_cm_listen(struct ib_cm_id
*cm_id
,
722 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
726 service_mask
= service_mask
? service_mask
:
727 __constant_cpu_to_be64(~0ULL);
728 service_id
&= service_mask
;
729 if ((service_id
& IB_SERVICE_ID_AGN_MASK
) == IB_CM_ASSIGN_SERVICE_ID
&&
730 (service_id
!= IB_CM_ASSIGN_SERVICE_ID
))
733 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
734 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
736 cm_id
->state
= IB_CM_LISTEN
;
738 spin_lock_irqsave(&cm
.lock
, flags
);
739 if (service_id
== IB_CM_ASSIGN_SERVICE_ID
) {
740 cm_id
->service_id
= cpu_to_be64(cm
.listen_service_id
++);
741 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
743 cm_id
->service_id
= service_id
;
744 cm_id
->service_mask
= service_mask
;
746 cur_cm_id_priv
= cm_insert_listen(cm_id_priv
);
747 spin_unlock_irqrestore(&cm
.lock
, flags
);
749 if (cur_cm_id_priv
) {
750 cm_id
->state
= IB_CM_IDLE
;
755 EXPORT_SYMBOL(ib_cm_listen
);
757 static __be64
cm_form_tid(struct cm_id_private
*cm_id_priv
,
758 enum cm_msg_sequence msg_seq
)
762 hi_tid
= ((u64
) cm_id_priv
->av
.port
->mad_agent
->hi_tid
) << 32;
763 low_tid
= (u64
) ((__force u32
)cm_id_priv
->id
.local_id
|
765 return cpu_to_be64(hi_tid
| low_tid
);
768 static void cm_format_mad_hdr(struct ib_mad_hdr
*hdr
,
769 __be16 attr_id
, __be64 tid
)
771 hdr
->base_version
= IB_MGMT_BASE_VERSION
;
772 hdr
->mgmt_class
= IB_MGMT_CLASS_CM
;
773 hdr
->class_version
= IB_CM_CLASS_VERSION
;
774 hdr
->method
= IB_MGMT_METHOD_SEND
;
775 hdr
->attr_id
= attr_id
;
779 static void cm_format_req(struct cm_req_msg
*req_msg
,
780 struct cm_id_private
*cm_id_priv
,
781 struct ib_cm_req_param
*param
)
783 cm_format_mad_hdr(&req_msg
->hdr
, CM_REQ_ATTR_ID
,
784 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_REQ
));
786 req_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
787 req_msg
->service_id
= param
->service_id
;
788 req_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
789 cm_req_set_local_qpn(req_msg
, cpu_to_be32(param
->qp_num
));
790 cm_req_set_resp_res(req_msg
, param
->responder_resources
);
791 cm_req_set_init_depth(req_msg
, param
->initiator_depth
);
792 cm_req_set_remote_resp_timeout(req_msg
,
793 param
->remote_cm_response_timeout
);
794 cm_req_set_qp_type(req_msg
, param
->qp_type
);
795 cm_req_set_flow_ctrl(req_msg
, param
->flow_control
);
796 cm_req_set_starting_psn(req_msg
, cpu_to_be32(param
->starting_psn
));
797 cm_req_set_local_resp_timeout(req_msg
,
798 param
->local_cm_response_timeout
);
799 cm_req_set_retry_count(req_msg
, param
->retry_count
);
800 req_msg
->pkey
= param
->primary_path
->pkey
;
801 cm_req_set_path_mtu(req_msg
, param
->primary_path
->mtu
);
802 cm_req_set_rnr_retry_count(req_msg
, param
->rnr_retry_count
);
803 cm_req_set_max_cm_retries(req_msg
, param
->max_cm_retries
);
804 cm_req_set_srq(req_msg
, param
->srq
);
806 req_msg
->primary_local_lid
= param
->primary_path
->slid
;
807 req_msg
->primary_remote_lid
= param
->primary_path
->dlid
;
808 req_msg
->primary_local_gid
= param
->primary_path
->sgid
;
809 req_msg
->primary_remote_gid
= param
->primary_path
->dgid
;
810 cm_req_set_primary_flow_label(req_msg
, param
->primary_path
->flow_label
);
811 cm_req_set_primary_packet_rate(req_msg
, param
->primary_path
->rate
);
812 req_msg
->primary_traffic_class
= param
->primary_path
->traffic_class
;
813 req_msg
->primary_hop_limit
= param
->primary_path
->hop_limit
;
814 cm_req_set_primary_sl(req_msg
, param
->primary_path
->sl
);
815 cm_req_set_primary_subnet_local(req_msg
, 1); /* local only... */
816 cm_req_set_primary_local_ack_timeout(req_msg
,
817 min(31, param
->primary_path
->packet_life_time
+ 1));
819 if (param
->alternate_path
) {
820 req_msg
->alt_local_lid
= param
->alternate_path
->slid
;
821 req_msg
->alt_remote_lid
= param
->alternate_path
->dlid
;
822 req_msg
->alt_local_gid
= param
->alternate_path
->sgid
;
823 req_msg
->alt_remote_gid
= param
->alternate_path
->dgid
;
824 cm_req_set_alt_flow_label(req_msg
,
825 param
->alternate_path
->flow_label
);
826 cm_req_set_alt_packet_rate(req_msg
, param
->alternate_path
->rate
);
827 req_msg
->alt_traffic_class
= param
->alternate_path
->traffic_class
;
828 req_msg
->alt_hop_limit
= param
->alternate_path
->hop_limit
;
829 cm_req_set_alt_sl(req_msg
, param
->alternate_path
->sl
);
830 cm_req_set_alt_subnet_local(req_msg
, 1); /* local only... */
831 cm_req_set_alt_local_ack_timeout(req_msg
,
832 min(31, param
->alternate_path
->packet_life_time
+ 1));
835 if (param
->private_data
&& param
->private_data_len
)
836 memcpy(req_msg
->private_data
, param
->private_data
,
837 param
->private_data_len
);
840 static inline int cm_validate_req_param(struct ib_cm_req_param
*param
)
842 /* peer-to-peer not supported */
843 if (param
->peer_to_peer
)
846 if (!param
->primary_path
)
849 if (param
->qp_type
!= IB_QPT_RC
&& param
->qp_type
!= IB_QPT_UC
)
852 if (param
->private_data
&&
853 param
->private_data_len
> IB_CM_REQ_PRIVATE_DATA_SIZE
)
856 if (param
->alternate_path
&&
857 (param
->alternate_path
->pkey
!= param
->primary_path
->pkey
||
858 param
->alternate_path
->mtu
!= param
->primary_path
->mtu
))
864 int ib_send_cm_req(struct ib_cm_id
*cm_id
,
865 struct ib_cm_req_param
*param
)
867 struct cm_id_private
*cm_id_priv
;
868 struct ib_send_wr
*bad_send_wr
;
869 struct cm_req_msg
*req_msg
;
873 ret
= cm_validate_req_param(param
);
877 /* Verify that we're not in timewait. */
878 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
879 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
880 if (cm_id
->state
!= IB_CM_IDLE
) {
881 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
885 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
887 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
889 if (IS_ERR(cm_id_priv
->timewait_info
))
892 ret
= cm_init_av_by_path(param
->primary_path
, &cm_id_priv
->av
);
895 if (param
->alternate_path
) {
896 ret
= cm_init_av_by_path(param
->alternate_path
,
897 &cm_id_priv
->alt_av
);
901 cm_id
->service_id
= param
->service_id
;
902 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
903 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
904 param
->primary_path
->packet_life_time
) * 2 +
906 param
->remote_cm_response_timeout
);
907 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
908 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
909 cm_id_priv
->responder_resources
= param
->responder_resources
;
910 cm_id_priv
->retry_count
= param
->retry_count
;
911 cm_id_priv
->path_mtu
= param
->primary_path
->mtu
;
913 ret
= cm_alloc_msg(cm_id_priv
, &cm_id_priv
->msg
);
917 req_msg
= (struct cm_req_msg
*) cm_id_priv
->msg
->mad
;
918 cm_format_req(req_msg
, cm_id_priv
, param
);
919 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
920 cm_id_priv
->msg
->send_wr
.wr
.ud
.timeout_ms
= cm_id_priv
->timeout_ms
;
921 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long) IB_CM_REQ_SENT
;
923 cm_id_priv
->local_qpn
= cm_req_get_local_qpn(req_msg
);
924 cm_id_priv
->rq_psn
= cm_req_get_starting_psn(req_msg
);
925 cm_id_priv
->local_ack_timeout
=
926 cm_req_get_primary_local_ack_timeout(req_msg
);
928 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
929 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
930 &cm_id_priv
->msg
->send_wr
, &bad_send_wr
);
932 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
935 BUG_ON(cm_id
->state
!= IB_CM_IDLE
);
936 cm_id
->state
= IB_CM_REQ_SENT
;
937 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
940 error2
: cm_free_msg(cm_id_priv
->msg
);
941 error1
: kfree(cm_id_priv
->timewait_info
);
944 EXPORT_SYMBOL(ib_send_cm_req
);
946 static int cm_issue_rej(struct cm_port
*port
,
947 struct ib_mad_recv_wc
*mad_recv_wc
,
948 enum ib_cm_rej_reason reason
,
949 enum cm_msg_response msg_rejected
,
950 void *ari
, u8 ari_length
)
952 struct ib_mad_send_buf
*msg
= NULL
;
953 struct ib_send_wr
*bad_send_wr
;
954 struct cm_rej_msg
*rej_msg
, *rcv_msg
;
957 ret
= cm_alloc_response_msg(port
, mad_recv_wc
, &msg
);
961 /* We just need common CM header information. Cast to any message. */
962 rcv_msg
= (struct cm_rej_msg
*) mad_recv_wc
->recv_buf
.mad
;
963 rej_msg
= (struct cm_rej_msg
*) msg
->mad
;
965 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, rcv_msg
->hdr
.tid
);
966 rej_msg
->remote_comm_id
= rcv_msg
->local_comm_id
;
967 rej_msg
->local_comm_id
= rcv_msg
->remote_comm_id
;
968 cm_rej_set_msg_rejected(rej_msg
, msg_rejected
);
969 rej_msg
->reason
= cpu_to_be16(reason
);
971 if (ari
&& ari_length
) {
972 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
973 memcpy(rej_msg
->ari
, ari
, ari_length
);
976 ret
= ib_post_send_mad(port
->mad_agent
, &msg
->send_wr
, &bad_send_wr
);
983 static inline int cm_is_active_peer(__be64 local_ca_guid
, __be64 remote_ca_guid
,
984 __be32 local_qpn
, __be32 remote_qpn
)
986 return (be64_to_cpu(local_ca_guid
) > be64_to_cpu(remote_ca_guid
) ||
987 ((local_ca_guid
== remote_ca_guid
) &&
988 (be32_to_cpu(local_qpn
) > be32_to_cpu(remote_qpn
))));
991 static inline void cm_format_paths_from_req(struct cm_req_msg
*req_msg
,
992 struct ib_sa_path_rec
*primary_path
,
993 struct ib_sa_path_rec
*alt_path
)
995 memset(primary_path
, 0, sizeof *primary_path
);
996 primary_path
->dgid
= req_msg
->primary_local_gid
;
997 primary_path
->sgid
= req_msg
->primary_remote_gid
;
998 primary_path
->dlid
= req_msg
->primary_local_lid
;
999 primary_path
->slid
= req_msg
->primary_remote_lid
;
1000 primary_path
->flow_label
= cm_req_get_primary_flow_label(req_msg
);
1001 primary_path
->hop_limit
= req_msg
->primary_hop_limit
;
1002 primary_path
->traffic_class
= req_msg
->primary_traffic_class
;
1003 primary_path
->reversible
= 1;
1004 primary_path
->pkey
= req_msg
->pkey
;
1005 primary_path
->sl
= cm_req_get_primary_sl(req_msg
);
1006 primary_path
->mtu_selector
= IB_SA_EQ
;
1007 primary_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1008 primary_path
->rate_selector
= IB_SA_EQ
;
1009 primary_path
->rate
= cm_req_get_primary_packet_rate(req_msg
);
1010 primary_path
->packet_life_time_selector
= IB_SA_EQ
;
1011 primary_path
->packet_life_time
=
1012 cm_req_get_primary_local_ack_timeout(req_msg
);
1013 primary_path
->packet_life_time
-= (primary_path
->packet_life_time
> 0);
1015 if (req_msg
->alt_local_lid
) {
1016 memset(alt_path
, 0, sizeof *alt_path
);
1017 alt_path
->dgid
= req_msg
->alt_local_gid
;
1018 alt_path
->sgid
= req_msg
->alt_remote_gid
;
1019 alt_path
->dlid
= req_msg
->alt_local_lid
;
1020 alt_path
->slid
= req_msg
->alt_remote_lid
;
1021 alt_path
->flow_label
= cm_req_get_alt_flow_label(req_msg
);
1022 alt_path
->hop_limit
= req_msg
->alt_hop_limit
;
1023 alt_path
->traffic_class
= req_msg
->alt_traffic_class
;
1024 alt_path
->reversible
= 1;
1025 alt_path
->pkey
= req_msg
->pkey
;
1026 alt_path
->sl
= cm_req_get_alt_sl(req_msg
);
1027 alt_path
->mtu_selector
= IB_SA_EQ
;
1028 alt_path
->mtu
= cm_req_get_path_mtu(req_msg
);
1029 alt_path
->rate_selector
= IB_SA_EQ
;
1030 alt_path
->rate
= cm_req_get_alt_packet_rate(req_msg
);
1031 alt_path
->packet_life_time_selector
= IB_SA_EQ
;
1032 alt_path
->packet_life_time
=
1033 cm_req_get_alt_local_ack_timeout(req_msg
);
1034 alt_path
->packet_life_time
-= (alt_path
->packet_life_time
> 0);
1038 static void cm_format_req_event(struct cm_work
*work
,
1039 struct cm_id_private
*cm_id_priv
,
1040 struct ib_cm_id
*listen_id
)
1042 struct cm_req_msg
*req_msg
;
1043 struct ib_cm_req_event_param
*param
;
1045 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1046 param
= &work
->cm_event
.param
.req_rcvd
;
1047 param
->listen_id
= listen_id
;
1048 param
->device
= cm_id_priv
->av
.port
->mad_agent
->device
;
1049 param
->port
= cm_id_priv
->av
.port
->port_num
;
1050 param
->primary_path
= &work
->path
[0];
1051 if (req_msg
->alt_local_lid
)
1052 param
->alternate_path
= &work
->path
[1];
1054 param
->alternate_path
= NULL
;
1055 param
->remote_ca_guid
= req_msg
->local_ca_guid
;
1056 param
->remote_qkey
= be32_to_cpu(req_msg
->local_qkey
);
1057 param
->remote_qpn
= be32_to_cpu(cm_req_get_local_qpn(req_msg
));
1058 param
->qp_type
= cm_req_get_qp_type(req_msg
);
1059 param
->starting_psn
= be32_to_cpu(cm_req_get_starting_psn(req_msg
));
1060 param
->responder_resources
= cm_req_get_init_depth(req_msg
);
1061 param
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1062 param
->local_cm_response_timeout
=
1063 cm_req_get_remote_resp_timeout(req_msg
);
1064 param
->flow_control
= cm_req_get_flow_ctrl(req_msg
);
1065 param
->remote_cm_response_timeout
=
1066 cm_req_get_local_resp_timeout(req_msg
);
1067 param
->retry_count
= cm_req_get_retry_count(req_msg
);
1068 param
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1069 param
->srq
= cm_req_get_srq(req_msg
);
1070 work
->cm_event
.private_data
= &req_msg
->private_data
;
1073 static void cm_process_work(struct cm_id_private
*cm_id_priv
,
1074 struct cm_work
*work
)
1076 unsigned long flags
;
1079 /* We will typically only have the current event to report. */
1080 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &work
->cm_event
);
1083 while (!ret
&& !atomic_add_negative(-1, &cm_id_priv
->work_count
)) {
1084 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1085 work
= cm_dequeue_work(cm_id_priv
);
1086 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1088 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
,
1092 cm_deref_id(cm_id_priv
);
1094 ib_destroy_cm_id(&cm_id_priv
->id
);
1097 static void cm_format_mra(struct cm_mra_msg
*mra_msg
,
1098 struct cm_id_private
*cm_id_priv
,
1099 enum cm_msg_response msg_mraed
, u8 service_timeout
,
1100 const void *private_data
, u8 private_data_len
)
1102 cm_format_mad_hdr(&mra_msg
->hdr
, CM_MRA_ATTR_ID
, cm_id_priv
->tid
);
1103 cm_mra_set_msg_mraed(mra_msg
, msg_mraed
);
1104 mra_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1105 mra_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1106 cm_mra_set_service_timeout(mra_msg
, service_timeout
);
1108 if (private_data
&& private_data_len
)
1109 memcpy(mra_msg
->private_data
, private_data
, private_data_len
);
1112 static void cm_format_rej(struct cm_rej_msg
*rej_msg
,
1113 struct cm_id_private
*cm_id_priv
,
1114 enum ib_cm_rej_reason reason
,
1117 const void *private_data
,
1118 u8 private_data_len
)
1120 cm_format_mad_hdr(&rej_msg
->hdr
, CM_REJ_ATTR_ID
, cm_id_priv
->tid
);
1121 rej_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1123 switch(cm_id_priv
->id
.state
) {
1124 case IB_CM_REQ_RCVD
:
1125 rej_msg
->local_comm_id
= 0;
1126 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1128 case IB_CM_MRA_REQ_SENT
:
1129 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1130 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REQ
);
1132 case IB_CM_REP_RCVD
:
1133 case IB_CM_MRA_REP_SENT
:
1134 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1135 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_REP
);
1138 rej_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1139 cm_rej_set_msg_rejected(rej_msg
, CM_MSG_RESPONSE_OTHER
);
1143 rej_msg
->reason
= cpu_to_be16(reason
);
1144 if (ari
&& ari_length
) {
1145 cm_rej_set_reject_info_len(rej_msg
, ari_length
);
1146 memcpy(rej_msg
->ari
, ari
, ari_length
);
1149 if (private_data
&& private_data_len
)
1150 memcpy(rej_msg
->private_data
, private_data
, private_data_len
);
1153 static void cm_dup_req_handler(struct cm_work
*work
,
1154 struct cm_id_private
*cm_id_priv
)
1156 struct ib_mad_send_buf
*msg
= NULL
;
1157 struct ib_send_wr
*bad_send_wr
;
1158 unsigned long flags
;
1161 /* Quick state check to discard duplicate REQs. */
1162 if (cm_id_priv
->id
.state
== IB_CM_REQ_RCVD
)
1165 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1169 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1170 switch (cm_id_priv
->id
.state
) {
1171 case IB_CM_MRA_REQ_SENT
:
1172 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1173 CM_MSG_RESPONSE_REQ
, cm_id_priv
->service_timeout
,
1174 cm_id_priv
->private_data
,
1175 cm_id_priv
->private_data_len
);
1177 case IB_CM_TIMEWAIT
:
1178 cm_format_rej((struct cm_rej_msg
*) msg
->mad
, cm_id_priv
,
1179 IB_CM_REJ_STALE_CONN
, NULL
, 0, NULL
, 0);
1184 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1186 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
, &msg
->send_wr
,
1192 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1193 free
: cm_free_msg(msg
);
1196 static struct cm_id_private
* cm_match_req(struct cm_work
*work
,
1197 struct cm_id_private
*cm_id_priv
)
1199 struct cm_id_private
*listen_cm_id_priv
, *cur_cm_id_priv
;
1200 struct cm_timewait_info
*timewait_info
;
1201 struct cm_req_msg
*req_msg
;
1202 unsigned long flags
;
1204 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1206 /* Check for duplicate REQ and stale connections. */
1207 spin_lock_irqsave(&cm
.lock
, flags
);
1208 timewait_info
= cm_insert_remote_id(cm_id_priv
->timewait_info
);
1210 timewait_info
= cm_insert_remote_qpn(cm_id_priv
->timewait_info
);
1212 if (timewait_info
) {
1213 cur_cm_id_priv
= cm_get_id(timewait_info
->work
.local_id
,
1214 timewait_info
->work
.remote_id
);
1215 spin_unlock_irqrestore(&cm
.lock
, flags
);
1216 if (cur_cm_id_priv
) {
1217 cm_dup_req_handler(work
, cur_cm_id_priv
);
1218 cm_deref_id(cur_cm_id_priv
);
1220 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1221 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REQ
,
1226 /* Find matching listen request. */
1227 listen_cm_id_priv
= cm_find_listen(req_msg
->service_id
);
1228 if (!listen_cm_id_priv
) {
1229 spin_unlock_irqrestore(&cm
.lock
, flags
);
1230 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1231 IB_CM_REJ_INVALID_SERVICE_ID
, CM_MSG_RESPONSE_REQ
,
1235 atomic_inc(&listen_cm_id_priv
->refcount
);
1236 atomic_inc(&cm_id_priv
->refcount
);
1237 cm_id_priv
->id
.state
= IB_CM_REQ_RCVD
;
1238 atomic_inc(&cm_id_priv
->work_count
);
1239 spin_unlock_irqrestore(&cm
.lock
, flags
);
1240 return listen_cm_id_priv
;
1242 error
: cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1246 static int cm_req_handler(struct cm_work
*work
)
1248 struct ib_cm_id
*cm_id
;
1249 struct cm_id_private
*cm_id_priv
, *listen_cm_id_priv
;
1250 struct cm_req_msg
*req_msg
;
1253 req_msg
= (struct cm_req_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1255 cm_id
= ib_create_cm_id(NULL
, NULL
);
1257 return PTR_ERR(cm_id
);
1259 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1260 cm_id_priv
->id
.remote_id
= req_msg
->local_comm_id
;
1261 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
1263 cm_id_priv
->timewait_info
= cm_create_timewait_info(cm_id_priv
->
1265 if (IS_ERR(cm_id_priv
->timewait_info
)) {
1266 ret
= PTR_ERR(cm_id_priv
->timewait_info
);
1269 cm_id_priv
->timewait_info
->work
.remote_id
= req_msg
->local_comm_id
;
1270 cm_id_priv
->timewait_info
->remote_ca_guid
= req_msg
->local_ca_guid
;
1271 cm_id_priv
->timewait_info
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1273 listen_cm_id_priv
= cm_match_req(work
, cm_id_priv
);
1274 if (!listen_cm_id_priv
) {
1279 cm_id_priv
->id
.cm_handler
= listen_cm_id_priv
->id
.cm_handler
;
1280 cm_id_priv
->id
.context
= listen_cm_id_priv
->id
.context
;
1281 cm_id_priv
->id
.service_id
= req_msg
->service_id
;
1282 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
1284 cm_format_paths_from_req(req_msg
, &work
->path
[0], &work
->path
[1]);
1285 ret
= cm_init_av_by_path(&work
->path
[0], &cm_id_priv
->av
);
1288 if (req_msg
->alt_local_lid
) {
1289 ret
= cm_init_av_by_path(&work
->path
[1], &cm_id_priv
->alt_av
);
1293 cm_id_priv
->tid
= req_msg
->hdr
.tid
;
1294 cm_id_priv
->timeout_ms
= cm_convert_to_ms(
1295 cm_req_get_local_resp_timeout(req_msg
));
1296 cm_id_priv
->max_cm_retries
= cm_req_get_max_cm_retries(req_msg
);
1297 cm_id_priv
->remote_qpn
= cm_req_get_local_qpn(req_msg
);
1298 cm_id_priv
->initiator_depth
= cm_req_get_resp_res(req_msg
);
1299 cm_id_priv
->responder_resources
= cm_req_get_init_depth(req_msg
);
1300 cm_id_priv
->path_mtu
= cm_req_get_path_mtu(req_msg
);
1301 cm_id_priv
->sq_psn
= cm_req_get_starting_psn(req_msg
);
1302 cm_id_priv
->local_ack_timeout
=
1303 cm_req_get_primary_local_ack_timeout(req_msg
);
1304 cm_id_priv
->retry_count
= cm_req_get_retry_count(req_msg
);
1305 cm_id_priv
->rnr_retry_count
= cm_req_get_rnr_retry_count(req_msg
);
1307 cm_format_req_event(work
, cm_id_priv
, &listen_cm_id_priv
->id
);
1308 cm_process_work(cm_id_priv
, work
);
1309 cm_deref_id(listen_cm_id_priv
);
1312 error3
: atomic_dec(&cm_id_priv
->refcount
);
1313 cm_deref_id(listen_cm_id_priv
);
1314 cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1315 error2
: kfree(cm_id_priv
->timewait_info
);
1316 error1
: ib_destroy_cm_id(&cm_id_priv
->id
);
1320 static void cm_format_rep(struct cm_rep_msg
*rep_msg
,
1321 struct cm_id_private
*cm_id_priv
,
1322 struct ib_cm_rep_param
*param
)
1324 cm_format_mad_hdr(&rep_msg
->hdr
, CM_REP_ATTR_ID
, cm_id_priv
->tid
);
1325 rep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1326 rep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1327 cm_rep_set_local_qpn(rep_msg
, cpu_to_be32(param
->qp_num
));
1328 cm_rep_set_starting_psn(rep_msg
, cpu_to_be32(param
->starting_psn
));
1329 rep_msg
->resp_resources
= param
->responder_resources
;
1330 rep_msg
->initiator_depth
= param
->initiator_depth
;
1331 cm_rep_set_target_ack_delay(rep_msg
, param
->target_ack_delay
);
1332 cm_rep_set_failover(rep_msg
, param
->failover_accepted
);
1333 cm_rep_set_flow_ctrl(rep_msg
, param
->flow_control
);
1334 cm_rep_set_rnr_retry_count(rep_msg
, param
->rnr_retry_count
);
1335 cm_rep_set_srq(rep_msg
, param
->srq
);
1336 rep_msg
->local_ca_guid
= cm_id_priv
->av
.port
->cm_dev
->ca_guid
;
1338 if (param
->private_data
&& param
->private_data_len
)
1339 memcpy(rep_msg
->private_data
, param
->private_data
,
1340 param
->private_data_len
);
1343 int ib_send_cm_rep(struct ib_cm_id
*cm_id
,
1344 struct ib_cm_rep_param
*param
)
1346 struct cm_id_private
*cm_id_priv
;
1347 struct ib_mad_send_buf
*msg
;
1348 struct cm_rep_msg
*rep_msg
;
1349 struct ib_send_wr
*bad_send_wr
;
1350 unsigned long flags
;
1353 if (param
->private_data
&&
1354 param
->private_data_len
> IB_CM_REP_PRIVATE_DATA_SIZE
)
1357 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1358 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1359 if (cm_id
->state
!= IB_CM_REQ_RCVD
&&
1360 cm_id
->state
!= IB_CM_MRA_REQ_SENT
) {
1365 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1369 rep_msg
= (struct cm_rep_msg
*) msg
->mad
;
1370 cm_format_rep(rep_msg
, cm_id_priv
, param
);
1371 msg
->send_wr
.wr
.ud
.timeout_ms
= cm_id_priv
->timeout_ms
;
1372 msg
->context
[1] = (void *) (unsigned long) IB_CM_REP_SENT
;
1374 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
1375 &msg
->send_wr
, &bad_send_wr
);
1377 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1382 cm_id
->state
= IB_CM_REP_SENT
;
1383 cm_id_priv
->msg
= msg
;
1384 cm_id_priv
->initiator_depth
= param
->initiator_depth
;
1385 cm_id_priv
->responder_resources
= param
->responder_resources
;
1386 cm_id_priv
->rq_psn
= cm_rep_get_starting_psn(rep_msg
);
1387 cm_id_priv
->local_qpn
= cm_rep_get_local_qpn(rep_msg
);
1389 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1392 EXPORT_SYMBOL(ib_send_cm_rep
);
1394 static void cm_format_rtu(struct cm_rtu_msg
*rtu_msg
,
1395 struct cm_id_private
*cm_id_priv
,
1396 const void *private_data
,
1397 u8 private_data_len
)
1399 cm_format_mad_hdr(&rtu_msg
->hdr
, CM_RTU_ATTR_ID
, cm_id_priv
->tid
);
1400 rtu_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1401 rtu_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1403 if (private_data
&& private_data_len
)
1404 memcpy(rtu_msg
->private_data
, private_data
, private_data_len
);
1407 int ib_send_cm_rtu(struct ib_cm_id
*cm_id
,
1408 const void *private_data
,
1409 u8 private_data_len
)
1411 struct cm_id_private
*cm_id_priv
;
1412 struct ib_mad_send_buf
*msg
;
1413 struct ib_send_wr
*bad_send_wr
;
1414 unsigned long flags
;
1418 if (private_data
&& private_data_len
> IB_CM_RTU_PRIVATE_DATA_SIZE
)
1421 data
= cm_copy_private_data(private_data
, private_data_len
);
1423 return PTR_ERR(data
);
1425 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1426 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1427 if (cm_id
->state
!= IB_CM_REP_RCVD
&&
1428 cm_id
->state
!= IB_CM_MRA_REP_SENT
) {
1433 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1437 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1438 private_data
, private_data_len
);
1440 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
1441 &msg
->send_wr
, &bad_send_wr
);
1443 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1449 cm_id
->state
= IB_CM_ESTABLISHED
;
1450 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1451 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1454 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1458 EXPORT_SYMBOL(ib_send_cm_rtu
);
1460 static void cm_format_rep_event(struct cm_work
*work
)
1462 struct cm_rep_msg
*rep_msg
;
1463 struct ib_cm_rep_event_param
*param
;
1465 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1466 param
= &work
->cm_event
.param
.rep_rcvd
;
1467 param
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1468 param
->remote_qkey
= be32_to_cpu(rep_msg
->local_qkey
);
1469 param
->remote_qpn
= be32_to_cpu(cm_rep_get_local_qpn(rep_msg
));
1470 param
->starting_psn
= be32_to_cpu(cm_rep_get_starting_psn(rep_msg
));
1471 param
->responder_resources
= rep_msg
->initiator_depth
;
1472 param
->initiator_depth
= rep_msg
->resp_resources
;
1473 param
->target_ack_delay
= cm_rep_get_target_ack_delay(rep_msg
);
1474 param
->failover_accepted
= cm_rep_get_failover(rep_msg
);
1475 param
->flow_control
= cm_rep_get_flow_ctrl(rep_msg
);
1476 param
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1477 param
->srq
= cm_rep_get_srq(rep_msg
);
1478 work
->cm_event
.private_data
= &rep_msg
->private_data
;
1481 static void cm_dup_rep_handler(struct cm_work
*work
)
1483 struct cm_id_private
*cm_id_priv
;
1484 struct cm_rep_msg
*rep_msg
;
1485 struct ib_mad_send_buf
*msg
= NULL
;
1486 struct ib_send_wr
*bad_send_wr
;
1487 unsigned long flags
;
1490 rep_msg
= (struct cm_rep_msg
*) work
->mad_recv_wc
->recv_buf
.mad
;
1491 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
,
1492 rep_msg
->local_comm_id
);
1496 ret
= cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
);
1500 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1501 if (cm_id_priv
->id
.state
== IB_CM_ESTABLISHED
)
1502 cm_format_rtu((struct cm_rtu_msg
*) msg
->mad
, cm_id_priv
,
1503 cm_id_priv
->private_data
,
1504 cm_id_priv
->private_data_len
);
1505 else if (cm_id_priv
->id
.state
== IB_CM_MRA_REP_SENT
)
1506 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
1507 CM_MSG_RESPONSE_REP
, cm_id_priv
->service_timeout
,
1508 cm_id_priv
->private_data
,
1509 cm_id_priv
->private_data_len
);
1512 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1514 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
, &msg
->send_wr
,
1520 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1521 free
: cm_free_msg(msg
);
1522 deref
: cm_deref_id(cm_id_priv
);
1525 static int cm_rep_handler(struct cm_work
*work
)
1527 struct cm_id_private
*cm_id_priv
;
1528 struct cm_rep_msg
*rep_msg
;
1529 unsigned long flags
;
1532 rep_msg
= (struct cm_rep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1533 cm_id_priv
= cm_acquire_id(rep_msg
->remote_comm_id
, 0);
1535 cm_dup_rep_handler(work
);
1539 cm_id_priv
->timewait_info
->work
.remote_id
= rep_msg
->local_comm_id
;
1540 cm_id_priv
->timewait_info
->remote_ca_guid
= rep_msg
->local_ca_guid
;
1541 cm_id_priv
->timewait_info
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1543 spin_lock_irqsave(&cm
.lock
, flags
);
1544 /* Check for duplicate REP. */
1545 if (cm_insert_remote_id(cm_id_priv
->timewait_info
)) {
1546 spin_unlock_irqrestore(&cm
.lock
, flags
);
1550 /* Check for a stale connection. */
1551 if (cm_insert_remote_qpn(cm_id_priv
->timewait_info
)) {
1552 spin_unlock_irqrestore(&cm
.lock
, flags
);
1553 cm_issue_rej(work
->port
, work
->mad_recv_wc
,
1554 IB_CM_REJ_STALE_CONN
, CM_MSG_RESPONSE_REP
,
1559 spin_unlock_irqrestore(&cm
.lock
, flags
);
1561 cm_format_rep_event(work
);
1563 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1564 switch (cm_id_priv
->id
.state
) {
1565 case IB_CM_REQ_SENT
:
1566 case IB_CM_MRA_REQ_RCVD
:
1569 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1573 cm_id_priv
->id
.state
= IB_CM_REP_RCVD
;
1574 cm_id_priv
->id
.remote_id
= rep_msg
->local_comm_id
;
1575 cm_id_priv
->remote_qpn
= cm_rep_get_local_qpn(rep_msg
);
1576 cm_id_priv
->initiator_depth
= rep_msg
->resp_resources
;
1577 cm_id_priv
->responder_resources
= rep_msg
->initiator_depth
;
1578 cm_id_priv
->sq_psn
= cm_rep_get_starting_psn(rep_msg
);
1579 cm_id_priv
->rnr_retry_count
= cm_rep_get_rnr_retry_count(rep_msg
);
1581 /* todo: handle peer_to_peer */
1583 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
1584 (unsigned long) cm_id_priv
->msg
);
1585 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1587 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1588 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1591 cm_process_work(cm_id_priv
, work
);
1593 cm_deref_id(cm_id_priv
);
1596 error
: cm_cleanup_timewait(cm_id_priv
->timewait_info
);
1597 cm_deref_id(cm_id_priv
);
1601 static int cm_establish_handler(struct cm_work
*work
)
1603 struct cm_id_private
*cm_id_priv
;
1604 unsigned long flags
;
1607 /* See comment in ib_cm_establish about lookup. */
1608 cm_id_priv
= cm_acquire_id(work
->local_id
, work
->remote_id
);
1612 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1613 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
) {
1614 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1618 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
1619 (unsigned long) cm_id_priv
->msg
);
1620 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1622 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1623 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1626 cm_process_work(cm_id_priv
, work
);
1628 cm_deref_id(cm_id_priv
);
1631 cm_deref_id(cm_id_priv
);
1635 static int cm_rtu_handler(struct cm_work
*work
)
1637 struct cm_id_private
*cm_id_priv
;
1638 struct cm_rtu_msg
*rtu_msg
;
1639 unsigned long flags
;
1642 rtu_msg
= (struct cm_rtu_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1643 cm_id_priv
= cm_acquire_id(rtu_msg
->remote_comm_id
,
1644 rtu_msg
->local_comm_id
);
1648 work
->cm_event
.private_data
= &rtu_msg
->private_data
;
1650 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1651 if (cm_id_priv
->id
.state
!= IB_CM_REP_SENT
&&
1652 cm_id_priv
->id
.state
!= IB_CM_MRA_REP_RCVD
) {
1653 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1656 cm_id_priv
->id
.state
= IB_CM_ESTABLISHED
;
1658 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
1659 (unsigned long) cm_id_priv
->msg
);
1660 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1662 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1663 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1666 cm_process_work(cm_id_priv
, work
);
1668 cm_deref_id(cm_id_priv
);
1671 cm_deref_id(cm_id_priv
);
1675 static void cm_format_dreq(struct cm_dreq_msg
*dreq_msg
,
1676 struct cm_id_private
*cm_id_priv
,
1677 const void *private_data
,
1678 u8 private_data_len
)
1680 cm_format_mad_hdr(&dreq_msg
->hdr
, CM_DREQ_ATTR_ID
,
1681 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_DREQ
));
1682 dreq_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1683 dreq_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1684 cm_dreq_set_remote_qpn(dreq_msg
, cm_id_priv
->remote_qpn
);
1686 if (private_data
&& private_data_len
)
1687 memcpy(dreq_msg
->private_data
, private_data
, private_data_len
);
1690 int ib_send_cm_dreq(struct ib_cm_id
*cm_id
,
1691 const void *private_data
,
1692 u8 private_data_len
)
1694 struct cm_id_private
*cm_id_priv
;
1695 struct ib_mad_send_buf
*msg
;
1696 struct ib_send_wr
*bad_send_wr
;
1697 unsigned long flags
;
1700 if (private_data
&& private_data_len
> IB_CM_DREQ_PRIVATE_DATA_SIZE
)
1703 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1704 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1705 if (cm_id
->state
!= IB_CM_ESTABLISHED
) {
1710 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1712 cm_enter_timewait(cm_id_priv
);
1716 cm_format_dreq((struct cm_dreq_msg
*) msg
->mad
, cm_id_priv
,
1717 private_data
, private_data_len
);
1718 msg
->send_wr
.wr
.ud
.timeout_ms
= cm_id_priv
->timeout_ms
;
1719 msg
->context
[1] = (void *) (unsigned long) IB_CM_DREQ_SENT
;
1721 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
1722 &msg
->send_wr
, &bad_send_wr
);
1724 cm_enter_timewait(cm_id_priv
);
1725 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1730 cm_id
->state
= IB_CM_DREQ_SENT
;
1731 cm_id_priv
->msg
= msg
;
1732 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1735 EXPORT_SYMBOL(ib_send_cm_dreq
);
1737 static void cm_format_drep(struct cm_drep_msg
*drep_msg
,
1738 struct cm_id_private
*cm_id_priv
,
1739 const void *private_data
,
1740 u8 private_data_len
)
1742 cm_format_mad_hdr(&drep_msg
->hdr
, CM_DREP_ATTR_ID
, cm_id_priv
->tid
);
1743 drep_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
1744 drep_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
1746 if (private_data
&& private_data_len
)
1747 memcpy(drep_msg
->private_data
, private_data
, private_data_len
);
1750 int ib_send_cm_drep(struct ib_cm_id
*cm_id
,
1751 const void *private_data
,
1752 u8 private_data_len
)
1754 struct cm_id_private
*cm_id_priv
;
1755 struct ib_mad_send_buf
*msg
;
1756 struct ib_send_wr
*bad_send_wr
;
1757 unsigned long flags
;
1761 if (private_data
&& private_data_len
> IB_CM_DREP_PRIVATE_DATA_SIZE
)
1764 data
= cm_copy_private_data(private_data
, private_data_len
);
1766 return PTR_ERR(data
);
1768 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1769 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1770 if (cm_id
->state
!= IB_CM_DREQ_RCVD
) {
1771 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1776 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
1777 cm_enter_timewait(cm_id_priv
);
1779 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1783 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1784 private_data
, private_data_len
);
1786 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
, &msg
->send_wr
,
1789 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1794 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1797 EXPORT_SYMBOL(ib_send_cm_drep
);
1799 static int cm_dreq_handler(struct cm_work
*work
)
1801 struct cm_id_private
*cm_id_priv
;
1802 struct cm_dreq_msg
*dreq_msg
;
1803 struct ib_mad_send_buf
*msg
= NULL
;
1804 struct ib_send_wr
*bad_send_wr
;
1805 unsigned long flags
;
1808 dreq_msg
= (struct cm_dreq_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1809 cm_id_priv
= cm_acquire_id(dreq_msg
->remote_comm_id
,
1810 dreq_msg
->local_comm_id
);
1814 work
->cm_event
.private_data
= &dreq_msg
->private_data
;
1816 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1817 if (cm_id_priv
->local_qpn
!= cm_dreq_get_remote_qpn(dreq_msg
))
1820 switch (cm_id_priv
->id
.state
) {
1821 case IB_CM_REP_SENT
:
1822 case IB_CM_DREQ_SENT
:
1823 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
1824 (unsigned long) cm_id_priv
->msg
);
1826 case IB_CM_ESTABLISHED
:
1827 case IB_CM_MRA_REP_RCVD
:
1829 case IB_CM_TIMEWAIT
:
1830 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
1833 cm_format_drep((struct cm_drep_msg
*) msg
->mad
, cm_id_priv
,
1834 cm_id_priv
->private_data
,
1835 cm_id_priv
->private_data_len
);
1836 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1838 if (ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
1839 &msg
->send_wr
, &bad_send_wr
))
1845 cm_id_priv
->id
.state
= IB_CM_DREQ_RCVD
;
1846 cm_id_priv
->tid
= dreq_msg
->hdr
.tid
;
1847 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1849 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1850 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1853 cm_process_work(cm_id_priv
, work
);
1855 cm_deref_id(cm_id_priv
);
1858 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1859 deref
: cm_deref_id(cm_id_priv
);
1863 static int cm_drep_handler(struct cm_work
*work
)
1865 struct cm_id_private
*cm_id_priv
;
1866 struct cm_drep_msg
*drep_msg
;
1867 unsigned long flags
;
1870 drep_msg
= (struct cm_drep_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1871 cm_id_priv
= cm_acquire_id(drep_msg
->remote_comm_id
,
1872 drep_msg
->local_comm_id
);
1876 work
->cm_event
.private_data
= &drep_msg
->private_data
;
1878 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1879 if (cm_id_priv
->id
.state
!= IB_CM_DREQ_SENT
&&
1880 cm_id_priv
->id
.state
!= IB_CM_DREQ_RCVD
) {
1881 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1884 cm_enter_timewait(cm_id_priv
);
1886 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
1887 (unsigned long) cm_id_priv
->msg
);
1888 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
1890 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1891 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1894 cm_process_work(cm_id_priv
, work
);
1896 cm_deref_id(cm_id_priv
);
1899 cm_deref_id(cm_id_priv
);
1903 int ib_send_cm_rej(struct ib_cm_id
*cm_id
,
1904 enum ib_cm_rej_reason reason
,
1907 const void *private_data
,
1908 u8 private_data_len
)
1910 struct cm_id_private
*cm_id_priv
;
1911 struct ib_mad_send_buf
*msg
;
1912 struct ib_send_wr
*bad_send_wr
;
1913 unsigned long flags
;
1916 if ((private_data
&& private_data_len
> IB_CM_REJ_PRIVATE_DATA_SIZE
) ||
1917 (ari
&& ari_length
> IB_CM_REJ_ARI_LENGTH
))
1920 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
1922 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1923 switch (cm_id
->state
) {
1924 case IB_CM_REQ_SENT
:
1925 case IB_CM_MRA_REQ_RCVD
:
1926 case IB_CM_REQ_RCVD
:
1927 case IB_CM_MRA_REQ_SENT
:
1928 case IB_CM_REP_RCVD
:
1929 case IB_CM_MRA_REP_SENT
:
1930 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1932 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
1933 cm_id_priv
, reason
, ari
, ari_length
,
1934 private_data
, private_data_len
);
1936 cm_reset_to_idle(cm_id_priv
);
1938 case IB_CM_REP_SENT
:
1939 case IB_CM_MRA_REP_RCVD
:
1940 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
1942 cm_format_rej((struct cm_rej_msg
*) msg
->mad
,
1943 cm_id_priv
, reason
, ari
, ari_length
,
1944 private_data
, private_data_len
);
1946 cm_enter_timewait(cm_id_priv
);
1956 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
1957 &msg
->send_wr
, &bad_send_wr
);
1961 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1964 EXPORT_SYMBOL(ib_send_cm_rej
);
1966 static void cm_format_rej_event(struct cm_work
*work
)
1968 struct cm_rej_msg
*rej_msg
;
1969 struct ib_cm_rej_event_param
*param
;
1971 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
1972 param
= &work
->cm_event
.param
.rej_rcvd
;
1973 param
->ari
= rej_msg
->ari
;
1974 param
->ari_length
= cm_rej_get_reject_info_len(rej_msg
);
1975 param
->reason
= __be16_to_cpu(rej_msg
->reason
);
1976 work
->cm_event
.private_data
= &rej_msg
->private_data
;
1979 static struct cm_id_private
* cm_acquire_rejected_id(struct cm_rej_msg
*rej_msg
)
1981 struct cm_timewait_info
*timewait_info
;
1982 struct cm_id_private
*cm_id_priv
;
1983 unsigned long flags
;
1986 remote_id
= rej_msg
->local_comm_id
;
1988 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_TIMEOUT
) {
1989 spin_lock_irqsave(&cm
.lock
, flags
);
1990 timewait_info
= cm_find_remote_id( *((__be64
*) rej_msg
->ari
),
1992 if (!timewait_info
) {
1993 spin_unlock_irqrestore(&cm
.lock
, flags
);
1996 cm_id_priv
= idr_find(&cm
.local_id_table
,
1997 (__force
int) timewait_info
->work
.local_id
);
1999 if (cm_id_priv
->id
.remote_id
== remote_id
)
2000 atomic_inc(&cm_id_priv
->refcount
);
2004 spin_unlock_irqrestore(&cm
.lock
, flags
);
2005 } else if (cm_rej_get_msg_rejected(rej_msg
) == CM_MSG_RESPONSE_REQ
)
2006 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, 0);
2008 cm_id_priv
= cm_acquire_id(rej_msg
->remote_comm_id
, remote_id
);
2013 static int cm_rej_handler(struct cm_work
*work
)
2015 struct cm_id_private
*cm_id_priv
;
2016 struct cm_rej_msg
*rej_msg
;
2017 unsigned long flags
;
2020 rej_msg
= (struct cm_rej_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2021 cm_id_priv
= cm_acquire_rejected_id(rej_msg
);
2025 cm_format_rej_event(work
);
2027 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2028 switch (cm_id_priv
->id
.state
) {
2029 case IB_CM_REQ_SENT
:
2030 case IB_CM_MRA_REQ_RCVD
:
2031 case IB_CM_REP_SENT
:
2032 case IB_CM_MRA_REP_RCVD
:
2033 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2034 (unsigned long) cm_id_priv
->msg
);
2036 case IB_CM_REQ_RCVD
:
2037 case IB_CM_MRA_REQ_SENT
:
2038 if (__be16_to_cpu(rej_msg
->reason
) == IB_CM_REJ_STALE_CONN
)
2039 cm_enter_timewait(cm_id_priv
);
2041 cm_reset_to_idle(cm_id_priv
);
2043 case IB_CM_DREQ_SENT
:
2044 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2045 (unsigned long) cm_id_priv
->msg
);
2047 case IB_CM_REP_RCVD
:
2048 case IB_CM_MRA_REP_SENT
:
2049 case IB_CM_ESTABLISHED
:
2050 cm_enter_timewait(cm_id_priv
);
2053 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2058 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2060 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2061 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2064 cm_process_work(cm_id_priv
, work
);
2066 cm_deref_id(cm_id_priv
);
2069 cm_deref_id(cm_id_priv
);
2073 int ib_send_cm_mra(struct ib_cm_id
*cm_id
,
2075 const void *private_data
,
2076 u8 private_data_len
)
2078 struct cm_id_private
*cm_id_priv
;
2079 struct ib_mad_send_buf
*msg
;
2080 struct ib_send_wr
*bad_send_wr
;
2082 unsigned long flags
;
2085 if (private_data
&& private_data_len
> IB_CM_MRA_PRIVATE_DATA_SIZE
)
2088 data
= cm_copy_private_data(private_data
, private_data_len
);
2090 return PTR_ERR(data
);
2092 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2094 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2095 switch(cm_id_priv
->id
.state
) {
2096 case IB_CM_REQ_RCVD
:
2097 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2101 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2102 CM_MSG_RESPONSE_REQ
, service_timeout
,
2103 private_data
, private_data_len
);
2104 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2105 &msg
->send_wr
, &bad_send_wr
);
2108 cm_id
->state
= IB_CM_MRA_REQ_SENT
;
2110 case IB_CM_REP_RCVD
:
2111 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2115 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2116 CM_MSG_RESPONSE_REP
, service_timeout
,
2117 private_data
, private_data_len
);
2118 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2119 &msg
->send_wr
, &bad_send_wr
);
2122 cm_id
->state
= IB_CM_MRA_REP_SENT
;
2124 case IB_CM_ESTABLISHED
:
2125 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2129 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2130 CM_MSG_RESPONSE_OTHER
, service_timeout
,
2131 private_data
, private_data_len
);
2132 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2133 &msg
->send_wr
, &bad_send_wr
);
2136 cm_id
->lap_state
= IB_CM_MRA_LAP_SENT
;
2142 cm_id_priv
->service_timeout
= service_timeout
;
2143 cm_set_private_data(cm_id_priv
, data
, private_data_len
);
2144 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2147 error1
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2151 error2
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2156 EXPORT_SYMBOL(ib_send_cm_mra
);
2158 static struct cm_id_private
* cm_acquire_mraed_id(struct cm_mra_msg
*mra_msg
)
2160 switch (cm_mra_get_msg_mraed(mra_msg
)) {
2161 case CM_MSG_RESPONSE_REQ
:
2162 return cm_acquire_id(mra_msg
->remote_comm_id
, 0);
2163 case CM_MSG_RESPONSE_REP
:
2164 case CM_MSG_RESPONSE_OTHER
:
2165 return cm_acquire_id(mra_msg
->remote_comm_id
,
2166 mra_msg
->local_comm_id
);
2172 static int cm_mra_handler(struct cm_work
*work
)
2174 struct cm_id_private
*cm_id_priv
;
2175 struct cm_mra_msg
*mra_msg
;
2176 unsigned long flags
;
2179 mra_msg
= (struct cm_mra_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2180 cm_id_priv
= cm_acquire_mraed_id(mra_msg
);
2184 work
->cm_event
.private_data
= &mra_msg
->private_data
;
2185 work
->cm_event
.param
.mra_rcvd
.service_timeout
=
2186 cm_mra_get_service_timeout(mra_msg
);
2187 timeout
= cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg
)) +
2188 cm_convert_to_ms(cm_id_priv
->av
.packet_life_time
);
2190 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2191 switch (cm_id_priv
->id
.state
) {
2192 case IB_CM_REQ_SENT
:
2193 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REQ
||
2194 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2195 (unsigned long) cm_id_priv
->msg
, timeout
))
2197 cm_id_priv
->id
.state
= IB_CM_MRA_REQ_RCVD
;
2199 case IB_CM_REP_SENT
:
2200 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_REP
||
2201 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2202 (unsigned long) cm_id_priv
->msg
, timeout
))
2204 cm_id_priv
->id
.state
= IB_CM_MRA_REP_RCVD
;
2206 case IB_CM_ESTABLISHED
:
2207 if (cm_mra_get_msg_mraed(mra_msg
) != CM_MSG_RESPONSE_OTHER
||
2208 cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
||
2209 ib_modify_mad(cm_id_priv
->av
.port
->mad_agent
,
2210 (unsigned long) cm_id_priv
->msg
, timeout
))
2212 cm_id_priv
->id
.lap_state
= IB_CM_MRA_LAP_RCVD
;
2218 cm_id_priv
->msg
->context
[1] = (void *) (unsigned long)
2219 cm_id_priv
->id
.state
;
2220 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2222 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2223 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2226 cm_process_work(cm_id_priv
, work
);
2228 cm_deref_id(cm_id_priv
);
2231 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2232 cm_deref_id(cm_id_priv
);
2236 static void cm_format_lap(struct cm_lap_msg
*lap_msg
,
2237 struct cm_id_private
*cm_id_priv
,
2238 struct ib_sa_path_rec
*alternate_path
,
2239 const void *private_data
,
2240 u8 private_data_len
)
2242 cm_format_mad_hdr(&lap_msg
->hdr
, CM_LAP_ATTR_ID
,
2243 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_LAP
));
2244 lap_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2245 lap_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2246 cm_lap_set_remote_qpn(lap_msg
, cm_id_priv
->remote_qpn
);
2247 /* todo: need remote CM response timeout */
2248 cm_lap_set_remote_resp_timeout(lap_msg
, 0x1F);
2249 lap_msg
->alt_local_lid
= alternate_path
->slid
;
2250 lap_msg
->alt_remote_lid
= alternate_path
->dlid
;
2251 lap_msg
->alt_local_gid
= alternate_path
->sgid
;
2252 lap_msg
->alt_remote_gid
= alternate_path
->dgid
;
2253 cm_lap_set_flow_label(lap_msg
, alternate_path
->flow_label
);
2254 cm_lap_set_traffic_class(lap_msg
, alternate_path
->traffic_class
);
2255 lap_msg
->alt_hop_limit
= alternate_path
->hop_limit
;
2256 cm_lap_set_packet_rate(lap_msg
, alternate_path
->rate
);
2257 cm_lap_set_sl(lap_msg
, alternate_path
->sl
);
2258 cm_lap_set_subnet_local(lap_msg
, 1); /* local only... */
2259 cm_lap_set_local_ack_timeout(lap_msg
,
2260 min(31, alternate_path
->packet_life_time
+ 1));
2262 if (private_data
&& private_data_len
)
2263 memcpy(lap_msg
->private_data
, private_data
, private_data_len
);
2266 int ib_send_cm_lap(struct ib_cm_id
*cm_id
,
2267 struct ib_sa_path_rec
*alternate_path
,
2268 const void *private_data
,
2269 u8 private_data_len
)
2271 struct cm_id_private
*cm_id_priv
;
2272 struct ib_mad_send_buf
*msg
;
2273 struct ib_send_wr
*bad_send_wr
;
2274 unsigned long flags
;
2277 if (private_data
&& private_data_len
> IB_CM_LAP_PRIVATE_DATA_SIZE
)
2280 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2281 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2282 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2283 cm_id
->lap_state
!= IB_CM_LAP_IDLE
) {
2288 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2292 cm_format_lap((struct cm_lap_msg
*) msg
->mad
, cm_id_priv
,
2293 alternate_path
, private_data
, private_data_len
);
2294 msg
->send_wr
.wr
.ud
.timeout_ms
= cm_id_priv
->timeout_ms
;
2295 msg
->context
[1] = (void *) (unsigned long) IB_CM_ESTABLISHED
;
2297 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2298 &msg
->send_wr
, &bad_send_wr
);
2300 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2305 cm_id
->lap_state
= IB_CM_LAP_SENT
;
2306 cm_id_priv
->msg
= msg
;
2308 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2311 EXPORT_SYMBOL(ib_send_cm_lap
);
2313 static void cm_format_path_from_lap(struct ib_sa_path_rec
*path
,
2314 struct cm_lap_msg
*lap_msg
)
2316 memset(path
, 0, sizeof *path
);
2317 path
->dgid
= lap_msg
->alt_local_gid
;
2318 path
->sgid
= lap_msg
->alt_remote_gid
;
2319 path
->dlid
= lap_msg
->alt_local_lid
;
2320 path
->slid
= lap_msg
->alt_remote_lid
;
2321 path
->flow_label
= cm_lap_get_flow_label(lap_msg
);
2322 path
->hop_limit
= lap_msg
->alt_hop_limit
;
2323 path
->traffic_class
= cm_lap_get_traffic_class(lap_msg
);
2324 path
->reversible
= 1;
2325 /* pkey is same as in REQ */
2326 path
->sl
= cm_lap_get_sl(lap_msg
);
2327 path
->mtu_selector
= IB_SA_EQ
;
2328 /* mtu is same as in REQ */
2329 path
->rate_selector
= IB_SA_EQ
;
2330 path
->rate
= cm_lap_get_packet_rate(lap_msg
);
2331 path
->packet_life_time_selector
= IB_SA_EQ
;
2332 path
->packet_life_time
= cm_lap_get_local_ack_timeout(lap_msg
);
2333 path
->packet_life_time
-= (path
->packet_life_time
> 0);
2336 static int cm_lap_handler(struct cm_work
*work
)
2338 struct cm_id_private
*cm_id_priv
;
2339 struct cm_lap_msg
*lap_msg
;
2340 struct ib_cm_lap_event_param
*param
;
2341 struct ib_mad_send_buf
*msg
= NULL
;
2342 struct ib_send_wr
*bad_send_wr
;
2343 unsigned long flags
;
2346 /* todo: verify LAP request and send reject APR if invalid. */
2347 lap_msg
= (struct cm_lap_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2348 cm_id_priv
= cm_acquire_id(lap_msg
->remote_comm_id
,
2349 lap_msg
->local_comm_id
);
2353 param
= &work
->cm_event
.param
.lap_rcvd
;
2354 param
->alternate_path
= &work
->path
[0];
2355 cm_format_path_from_lap(param
->alternate_path
, lap_msg
);
2356 work
->cm_event
.private_data
= &lap_msg
->private_data
;
2358 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2359 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
)
2362 switch (cm_id_priv
->id
.lap_state
) {
2363 case IB_CM_LAP_IDLE
:
2365 case IB_CM_MRA_LAP_SENT
:
2366 if (cm_alloc_response_msg(work
->port
, work
->mad_recv_wc
, &msg
))
2369 cm_format_mra((struct cm_mra_msg
*) msg
->mad
, cm_id_priv
,
2370 CM_MSG_RESPONSE_OTHER
,
2371 cm_id_priv
->service_timeout
,
2372 cm_id_priv
->private_data
,
2373 cm_id_priv
->private_data_len
);
2374 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2376 if (ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2377 &msg
->send_wr
, &bad_send_wr
))
2384 cm_id_priv
->id
.lap_state
= IB_CM_LAP_RCVD
;
2385 cm_id_priv
->tid
= lap_msg
->hdr
.tid
;
2386 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2388 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2389 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2392 cm_process_work(cm_id_priv
, work
);
2394 cm_deref_id(cm_id_priv
);
2397 unlock
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2398 deref
: cm_deref_id(cm_id_priv
);
2402 static void cm_format_apr(struct cm_apr_msg
*apr_msg
,
2403 struct cm_id_private
*cm_id_priv
,
2404 enum ib_cm_apr_status status
,
2407 const void *private_data
,
2408 u8 private_data_len
)
2410 cm_format_mad_hdr(&apr_msg
->hdr
, CM_APR_ATTR_ID
, cm_id_priv
->tid
);
2411 apr_msg
->local_comm_id
= cm_id_priv
->id
.local_id
;
2412 apr_msg
->remote_comm_id
= cm_id_priv
->id
.remote_id
;
2413 apr_msg
->ap_status
= (u8
) status
;
2415 if (info
&& info_length
) {
2416 apr_msg
->info_length
= info_length
;
2417 memcpy(apr_msg
->info
, info
, info_length
);
2420 if (private_data
&& private_data_len
)
2421 memcpy(apr_msg
->private_data
, private_data
, private_data_len
);
2424 int ib_send_cm_apr(struct ib_cm_id
*cm_id
,
2425 enum ib_cm_apr_status status
,
2428 const void *private_data
,
2429 u8 private_data_len
)
2431 struct cm_id_private
*cm_id_priv
;
2432 struct ib_mad_send_buf
*msg
;
2433 struct ib_send_wr
*bad_send_wr
;
2434 unsigned long flags
;
2437 if ((private_data
&& private_data_len
> IB_CM_APR_PRIVATE_DATA_SIZE
) ||
2438 (info
&& info_length
> IB_CM_APR_INFO_LENGTH
))
2441 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2442 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2443 if (cm_id
->state
!= IB_CM_ESTABLISHED
||
2444 (cm_id
->lap_state
!= IB_CM_LAP_RCVD
&&
2445 cm_id
->lap_state
!= IB_CM_MRA_LAP_SENT
)) {
2450 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2454 cm_format_apr((struct cm_apr_msg
*) msg
->mad
, cm_id_priv
, status
,
2455 info
, info_length
, private_data
, private_data_len
);
2456 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2457 &msg
->send_wr
, &bad_send_wr
);
2459 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2464 cm_id
->lap_state
= IB_CM_LAP_IDLE
;
2465 out
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2468 EXPORT_SYMBOL(ib_send_cm_apr
);
2470 static int cm_apr_handler(struct cm_work
*work
)
2472 struct cm_id_private
*cm_id_priv
;
2473 struct cm_apr_msg
*apr_msg
;
2474 unsigned long flags
;
2477 apr_msg
= (struct cm_apr_msg
*)work
->mad_recv_wc
->recv_buf
.mad
;
2478 cm_id_priv
= cm_acquire_id(apr_msg
->remote_comm_id
,
2479 apr_msg
->local_comm_id
);
2481 return -EINVAL
; /* Unmatched reply. */
2483 work
->cm_event
.param
.apr_rcvd
.ap_status
= apr_msg
->ap_status
;
2484 work
->cm_event
.param
.apr_rcvd
.apr_info
= &apr_msg
->info
;
2485 work
->cm_event
.param
.apr_rcvd
.info_len
= apr_msg
->info_length
;
2486 work
->cm_event
.private_data
= &apr_msg
->private_data
;
2488 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2489 if (cm_id_priv
->id
.state
!= IB_CM_ESTABLISHED
||
2490 (cm_id_priv
->id
.lap_state
!= IB_CM_LAP_SENT
&&
2491 cm_id_priv
->id
.lap_state
!= IB_CM_MRA_LAP_RCVD
)) {
2492 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2495 cm_id_priv
->id
.lap_state
= IB_CM_LAP_IDLE
;
2496 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2497 (unsigned long) cm_id_priv
->msg
);
2498 cm_id_priv
->msg
= NULL
;
2500 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2502 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2503 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2506 cm_process_work(cm_id_priv
, work
);
2508 cm_deref_id(cm_id_priv
);
2511 cm_deref_id(cm_id_priv
);
2515 static int cm_timewait_handler(struct cm_work
*work
)
2517 struct cm_timewait_info
*timewait_info
;
2518 struct cm_id_private
*cm_id_priv
;
2519 unsigned long flags
;
2522 timewait_info
= (struct cm_timewait_info
*)work
;
2523 cm_cleanup_timewait(timewait_info
);
2525 cm_id_priv
= cm_acquire_id(timewait_info
->work
.local_id
,
2526 timewait_info
->work
.remote_id
);
2530 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2531 if (cm_id_priv
->id
.state
!= IB_CM_TIMEWAIT
||
2532 cm_id_priv
->remote_qpn
!= timewait_info
->remote_qpn
) {
2533 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2536 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2537 ret
= atomic_inc_and_test(&cm_id_priv
->work_count
);
2539 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
2540 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2543 cm_process_work(cm_id_priv
, work
);
2545 cm_deref_id(cm_id_priv
);
2548 cm_deref_id(cm_id_priv
);
2552 static void cm_format_sidr_req(struct cm_sidr_req_msg
*sidr_req_msg
,
2553 struct cm_id_private
*cm_id_priv
,
2554 struct ib_cm_sidr_req_param
*param
)
2556 cm_format_mad_hdr(&sidr_req_msg
->hdr
, CM_SIDR_REQ_ATTR_ID
,
2557 cm_form_tid(cm_id_priv
, CM_MSG_SEQUENCE_SIDR
));
2558 sidr_req_msg
->request_id
= cm_id_priv
->id
.local_id
;
2559 sidr_req_msg
->pkey
= cpu_to_be16(param
->pkey
);
2560 sidr_req_msg
->service_id
= param
->service_id
;
2562 if (param
->private_data
&& param
->private_data_len
)
2563 memcpy(sidr_req_msg
->private_data
, param
->private_data
,
2564 param
->private_data_len
);
2567 int ib_send_cm_sidr_req(struct ib_cm_id
*cm_id
,
2568 struct ib_cm_sidr_req_param
*param
)
2570 struct cm_id_private
*cm_id_priv
;
2571 struct ib_mad_send_buf
*msg
;
2572 struct ib_send_wr
*bad_send_wr
;
2573 unsigned long flags
;
2576 if (!param
->path
|| (param
->private_data
&&
2577 param
->private_data_len
> IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
))
2580 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2581 ret
= cm_init_av_by_path(param
->path
, &cm_id_priv
->av
);
2585 cm_id
->service_id
= param
->service_id
;
2586 cm_id
->service_mask
= __constant_cpu_to_be64(~0ULL);
2587 cm_id_priv
->timeout_ms
= param
->timeout_ms
;
2588 cm_id_priv
->max_cm_retries
= param
->max_cm_retries
;
2589 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2593 cm_format_sidr_req((struct cm_sidr_req_msg
*) msg
->mad
, cm_id_priv
,
2595 msg
->send_wr
.wr
.ud
.timeout_ms
= cm_id_priv
->timeout_ms
;
2596 msg
->context
[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT
;
2598 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2599 if (cm_id
->state
== IB_CM_IDLE
)
2600 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2601 &msg
->send_wr
, &bad_send_wr
);
2606 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2610 cm_id
->state
= IB_CM_SIDR_REQ_SENT
;
2611 cm_id_priv
->msg
= msg
;
2612 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2616 EXPORT_SYMBOL(ib_send_cm_sidr_req
);
2618 static void cm_format_sidr_req_event(struct cm_work
*work
,
2619 struct ib_cm_id
*listen_id
)
2621 struct cm_sidr_req_msg
*sidr_req_msg
;
2622 struct ib_cm_sidr_req_event_param
*param
;
2624 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2625 work
->mad_recv_wc
->recv_buf
.mad
;
2626 param
= &work
->cm_event
.param
.sidr_req_rcvd
;
2627 param
->pkey
= __be16_to_cpu(sidr_req_msg
->pkey
);
2628 param
->listen_id
= listen_id
;
2629 param
->device
= work
->port
->mad_agent
->device
;
2630 param
->port
= work
->port
->port_num
;
2631 work
->cm_event
.private_data
= &sidr_req_msg
->private_data
;
2634 static int cm_sidr_req_handler(struct cm_work
*work
)
2636 struct ib_cm_id
*cm_id
;
2637 struct cm_id_private
*cm_id_priv
, *cur_cm_id_priv
;
2638 struct cm_sidr_req_msg
*sidr_req_msg
;
2640 unsigned long flags
;
2642 cm_id
= ib_create_cm_id(NULL
, NULL
);
2644 return PTR_ERR(cm_id
);
2645 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2647 /* Record SGID/SLID and request ID for lookup. */
2648 sidr_req_msg
= (struct cm_sidr_req_msg
*)
2649 work
->mad_recv_wc
->recv_buf
.mad
;
2650 wc
= work
->mad_recv_wc
->wc
;
2651 cm_id_priv
->av
.dgid
.global
.subnet_prefix
= cpu_to_be64(wc
->slid
);
2652 cm_id_priv
->av
.dgid
.global
.interface_id
= 0;
2653 cm_init_av_for_response(work
->port
, work
->mad_recv_wc
->wc
,
2655 cm_id_priv
->id
.remote_id
= sidr_req_msg
->request_id
;
2656 cm_id_priv
->id
.state
= IB_CM_SIDR_REQ_RCVD
;
2657 cm_id_priv
->tid
= sidr_req_msg
->hdr
.tid
;
2658 atomic_inc(&cm_id_priv
->work_count
);
2660 spin_lock_irqsave(&cm
.lock
, flags
);
2661 cur_cm_id_priv
= cm_insert_remote_sidr(cm_id_priv
);
2662 if (cur_cm_id_priv
) {
2663 spin_unlock_irqrestore(&cm
.lock
, flags
);
2664 goto out
; /* Duplicate message. */
2666 cur_cm_id_priv
= cm_find_listen(sidr_req_msg
->service_id
);
2667 if (!cur_cm_id_priv
) {
2668 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2669 spin_unlock_irqrestore(&cm
.lock
, flags
);
2670 /* todo: reply with no match */
2671 goto out
; /* No match. */
2673 atomic_inc(&cur_cm_id_priv
->refcount
);
2674 spin_unlock_irqrestore(&cm
.lock
, flags
);
2676 cm_id_priv
->id
.cm_handler
= cur_cm_id_priv
->id
.cm_handler
;
2677 cm_id_priv
->id
.context
= cur_cm_id_priv
->id
.context
;
2678 cm_id_priv
->id
.service_id
= sidr_req_msg
->service_id
;
2679 cm_id_priv
->id
.service_mask
= __constant_cpu_to_be64(~0ULL);
2681 cm_format_sidr_req_event(work
, &cur_cm_id_priv
->id
);
2682 cm_process_work(cm_id_priv
, work
);
2683 cm_deref_id(cur_cm_id_priv
);
2686 ib_destroy_cm_id(&cm_id_priv
->id
);
2690 static void cm_format_sidr_rep(struct cm_sidr_rep_msg
*sidr_rep_msg
,
2691 struct cm_id_private
*cm_id_priv
,
2692 struct ib_cm_sidr_rep_param
*param
)
2694 cm_format_mad_hdr(&sidr_rep_msg
->hdr
, CM_SIDR_REP_ATTR_ID
,
2696 sidr_rep_msg
->request_id
= cm_id_priv
->id
.remote_id
;
2697 sidr_rep_msg
->status
= param
->status
;
2698 cm_sidr_rep_set_qpn(sidr_rep_msg
, cpu_to_be32(param
->qp_num
));
2699 sidr_rep_msg
->service_id
= cm_id_priv
->id
.service_id
;
2700 sidr_rep_msg
->qkey
= cpu_to_be32(param
->qkey
);
2702 if (param
->info
&& param
->info_length
)
2703 memcpy(sidr_rep_msg
->info
, param
->info
, param
->info_length
);
2705 if (param
->private_data
&& param
->private_data_len
)
2706 memcpy(sidr_rep_msg
->private_data
, param
->private_data
,
2707 param
->private_data_len
);
2710 int ib_send_cm_sidr_rep(struct ib_cm_id
*cm_id
,
2711 struct ib_cm_sidr_rep_param
*param
)
2713 struct cm_id_private
*cm_id_priv
;
2714 struct ib_mad_send_buf
*msg
;
2715 struct ib_send_wr
*bad_send_wr
;
2716 unsigned long flags
;
2719 if ((param
->info
&& param
->info_length
> IB_CM_SIDR_REP_INFO_LENGTH
) ||
2720 (param
->private_data
&&
2721 param
->private_data_len
> IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
))
2724 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2725 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2726 if (cm_id
->state
!= IB_CM_SIDR_REQ_RCVD
) {
2731 ret
= cm_alloc_msg(cm_id_priv
, &msg
);
2735 cm_format_sidr_rep((struct cm_sidr_rep_msg
*) msg
->mad
, cm_id_priv
,
2737 ret
= ib_post_send_mad(cm_id_priv
->av
.port
->mad_agent
,
2738 &msg
->send_wr
, &bad_send_wr
);
2740 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2744 cm_id
->state
= IB_CM_IDLE
;
2745 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2747 spin_lock_irqsave(&cm
.lock
, flags
);
2748 rb_erase(&cm_id_priv
->sidr_id_node
, &cm
.remote_sidr_table
);
2749 spin_unlock_irqrestore(&cm
.lock
, flags
);
2752 error
: spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2755 EXPORT_SYMBOL(ib_send_cm_sidr_rep
);
2757 static void cm_format_sidr_rep_event(struct cm_work
*work
)
2759 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2760 struct ib_cm_sidr_rep_event_param
*param
;
2762 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2763 work
->mad_recv_wc
->recv_buf
.mad
;
2764 param
= &work
->cm_event
.param
.sidr_rep_rcvd
;
2765 param
->status
= sidr_rep_msg
->status
;
2766 param
->qkey
= be32_to_cpu(sidr_rep_msg
->qkey
);
2767 param
->qpn
= be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg
));
2768 param
->info
= &sidr_rep_msg
->info
;
2769 param
->info_len
= sidr_rep_msg
->info_length
;
2770 work
->cm_event
.private_data
= &sidr_rep_msg
->private_data
;
2773 static int cm_sidr_rep_handler(struct cm_work
*work
)
2775 struct cm_sidr_rep_msg
*sidr_rep_msg
;
2776 struct cm_id_private
*cm_id_priv
;
2777 unsigned long flags
;
2779 sidr_rep_msg
= (struct cm_sidr_rep_msg
*)
2780 work
->mad_recv_wc
->recv_buf
.mad
;
2781 cm_id_priv
= cm_acquire_id(sidr_rep_msg
->request_id
, 0);
2783 return -EINVAL
; /* Unmatched reply. */
2785 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2786 if (cm_id_priv
->id
.state
!= IB_CM_SIDR_REQ_SENT
) {
2787 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2790 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2791 ib_cancel_mad(cm_id_priv
->av
.port
->mad_agent
,
2792 (unsigned long) cm_id_priv
->msg
);
2793 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2795 cm_format_sidr_rep_event(work
);
2796 cm_process_work(cm_id_priv
, work
);
2799 cm_deref_id(cm_id_priv
);
2803 static void cm_process_send_error(struct ib_mad_send_buf
*msg
,
2804 enum ib_wc_status wc_status
)
2806 struct cm_id_private
*cm_id_priv
;
2807 struct ib_cm_event cm_event
;
2808 enum ib_cm_state state
;
2809 unsigned long flags
;
2812 memset(&cm_event
, 0, sizeof cm_event
);
2813 cm_id_priv
= msg
->context
[0];
2815 /* Discard old sends or ones without a response. */
2816 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2817 state
= (enum ib_cm_state
) (unsigned long) msg
->context
[1];
2818 if (msg
!= cm_id_priv
->msg
|| state
!= cm_id_priv
->id
.state
)
2822 case IB_CM_REQ_SENT
:
2823 case IB_CM_MRA_REQ_RCVD
:
2824 cm_reset_to_idle(cm_id_priv
);
2825 cm_event
.event
= IB_CM_REQ_ERROR
;
2827 case IB_CM_REP_SENT
:
2828 case IB_CM_MRA_REP_RCVD
:
2829 cm_reset_to_idle(cm_id_priv
);
2830 cm_event
.event
= IB_CM_REP_ERROR
;
2832 case IB_CM_DREQ_SENT
:
2833 cm_enter_timewait(cm_id_priv
);
2834 cm_event
.event
= IB_CM_DREQ_ERROR
;
2836 case IB_CM_SIDR_REQ_SENT
:
2837 cm_id_priv
->id
.state
= IB_CM_IDLE
;
2838 cm_event
.event
= IB_CM_SIDR_REQ_ERROR
;
2843 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2844 cm_event
.param
.send_status
= wc_status
;
2846 /* No other events can occur on the cm_id at this point. */
2847 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, &cm_event
);
2850 ib_destroy_cm_id(&cm_id_priv
->id
);
2853 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2857 static void cm_send_handler(struct ib_mad_agent
*mad_agent
,
2858 struct ib_mad_send_wc
*mad_send_wc
)
2860 struct ib_mad_send_buf
*msg
;
2862 msg
= (struct ib_mad_send_buf
*)(unsigned long)mad_send_wc
->wr_id
;
2864 switch (mad_send_wc
->status
) {
2866 case IB_WC_WR_FLUSH_ERR
:
2870 if (msg
->context
[0] && msg
->context
[1])
2871 cm_process_send_error(msg
, mad_send_wc
->status
);
2878 static void cm_work_handler(void *data
)
2880 struct cm_work
*work
= data
;
2883 switch (work
->cm_event
.event
) {
2884 case IB_CM_REQ_RECEIVED
:
2885 ret
= cm_req_handler(work
);
2887 case IB_CM_MRA_RECEIVED
:
2888 ret
= cm_mra_handler(work
);
2890 case IB_CM_REJ_RECEIVED
:
2891 ret
= cm_rej_handler(work
);
2893 case IB_CM_REP_RECEIVED
:
2894 ret
= cm_rep_handler(work
);
2896 case IB_CM_RTU_RECEIVED
:
2897 ret
= cm_rtu_handler(work
);
2899 case IB_CM_USER_ESTABLISHED
:
2900 ret
= cm_establish_handler(work
);
2902 case IB_CM_DREQ_RECEIVED
:
2903 ret
= cm_dreq_handler(work
);
2905 case IB_CM_DREP_RECEIVED
:
2906 ret
= cm_drep_handler(work
);
2908 case IB_CM_SIDR_REQ_RECEIVED
:
2909 ret
= cm_sidr_req_handler(work
);
2911 case IB_CM_SIDR_REP_RECEIVED
:
2912 ret
= cm_sidr_rep_handler(work
);
2914 case IB_CM_LAP_RECEIVED
:
2915 ret
= cm_lap_handler(work
);
2917 case IB_CM_APR_RECEIVED
:
2918 ret
= cm_apr_handler(work
);
2920 case IB_CM_TIMEWAIT_EXIT
:
2921 ret
= cm_timewait_handler(work
);
2931 int ib_cm_establish(struct ib_cm_id
*cm_id
)
2933 struct cm_id_private
*cm_id_priv
;
2934 struct cm_work
*work
;
2935 unsigned long flags
;
2938 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
2942 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
2943 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
2944 switch (cm_id
->state
)
2946 case IB_CM_REP_SENT
:
2947 case IB_CM_MRA_REP_RCVD
:
2948 cm_id
->state
= IB_CM_ESTABLISHED
;
2950 case IB_CM_ESTABLISHED
:
2957 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
2965 * The CM worker thread may try to destroy the cm_id before it
2966 * can execute this work item. To prevent potential deadlock,
2967 * we need to find the cm_id once we're in the context of the
2968 * worker thread, rather than holding a reference on it.
2970 INIT_WORK(&work
->work
, cm_work_handler
, work
);
2971 work
->local_id
= cm_id
->local_id
;
2972 work
->remote_id
= cm_id
->remote_id
;
2973 work
->mad_recv_wc
= NULL
;
2974 work
->cm_event
.event
= IB_CM_USER_ESTABLISHED
;
2975 queue_work(cm
.wq
, &work
->work
);
2979 EXPORT_SYMBOL(ib_cm_establish
);
2981 static void cm_recv_handler(struct ib_mad_agent
*mad_agent
,
2982 struct ib_mad_recv_wc
*mad_recv_wc
)
2984 struct cm_work
*work
;
2985 enum ib_cm_event_type event
;
2988 switch (mad_recv_wc
->recv_buf
.mad
->mad_hdr
.attr_id
) {
2989 case CM_REQ_ATTR_ID
:
2990 paths
= 1 + (((struct cm_req_msg
*) mad_recv_wc
->recv_buf
.mad
)->
2991 alt_local_lid
!= 0);
2992 event
= IB_CM_REQ_RECEIVED
;
2994 case CM_MRA_ATTR_ID
:
2995 event
= IB_CM_MRA_RECEIVED
;
2997 case CM_REJ_ATTR_ID
:
2998 event
= IB_CM_REJ_RECEIVED
;
3000 case CM_REP_ATTR_ID
:
3001 event
= IB_CM_REP_RECEIVED
;
3003 case CM_RTU_ATTR_ID
:
3004 event
= IB_CM_RTU_RECEIVED
;
3006 case CM_DREQ_ATTR_ID
:
3007 event
= IB_CM_DREQ_RECEIVED
;
3009 case CM_DREP_ATTR_ID
:
3010 event
= IB_CM_DREP_RECEIVED
;
3012 case CM_SIDR_REQ_ATTR_ID
:
3013 event
= IB_CM_SIDR_REQ_RECEIVED
;
3015 case CM_SIDR_REP_ATTR_ID
:
3016 event
= IB_CM_SIDR_REP_RECEIVED
;
3018 case CM_LAP_ATTR_ID
:
3020 event
= IB_CM_LAP_RECEIVED
;
3022 case CM_APR_ATTR_ID
:
3023 event
= IB_CM_APR_RECEIVED
;
3026 ib_free_recv_mad(mad_recv_wc
);
3030 work
= kmalloc(sizeof *work
+ sizeof(struct ib_sa_path_rec
) * paths
,
3033 ib_free_recv_mad(mad_recv_wc
);
3037 INIT_WORK(&work
->work
, cm_work_handler
, work
);
3038 work
->cm_event
.event
= event
;
3039 work
->mad_recv_wc
= mad_recv_wc
;
3040 work
->port
= (struct cm_port
*)mad_agent
->context
;
3041 queue_work(cm
.wq
, &work
->work
);
3044 static int cm_init_qp_init_attr(struct cm_id_private
*cm_id_priv
,
3045 struct ib_qp_attr
*qp_attr
,
3048 unsigned long flags
;
3051 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3052 switch (cm_id_priv
->id
.state
) {
3053 case IB_CM_REQ_SENT
:
3054 case IB_CM_MRA_REQ_RCVD
:
3055 case IB_CM_REQ_RCVD
:
3056 case IB_CM_MRA_REQ_SENT
:
3057 case IB_CM_REP_RCVD
:
3058 case IB_CM_MRA_REP_SENT
:
3059 case IB_CM_REP_SENT
:
3060 case IB_CM_MRA_REP_RCVD
:
3061 case IB_CM_ESTABLISHED
:
3062 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
|
3063 IB_QP_PKEY_INDEX
| IB_QP_PORT
;
3064 qp_attr
->qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
3065 if (cm_id_priv
->responder_resources
)
3066 qp_attr
->qp_access_flags
|= IB_ACCESS_REMOTE_WRITE
|
3067 IB_ACCESS_REMOTE_READ
;
3068 qp_attr
->pkey_index
= cm_id_priv
->av
.pkey_index
;
3069 qp_attr
->port_num
= cm_id_priv
->av
.port
->port_num
;
3076 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3080 static int cm_init_qp_rtr_attr(struct cm_id_private
*cm_id_priv
,
3081 struct ib_qp_attr
*qp_attr
,
3084 unsigned long flags
;
3087 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3088 switch (cm_id_priv
->id
.state
) {
3089 case IB_CM_REQ_RCVD
:
3090 case IB_CM_MRA_REQ_SENT
:
3091 case IB_CM_REP_RCVD
:
3092 case IB_CM_MRA_REP_SENT
:
3093 case IB_CM_REP_SENT
:
3094 case IB_CM_MRA_REP_RCVD
:
3095 case IB_CM_ESTABLISHED
:
3096 *qp_attr_mask
= IB_QP_STATE
| IB_QP_AV
| IB_QP_PATH_MTU
|
3097 IB_QP_DEST_QPN
| IB_QP_RQ_PSN
|
3098 IB_QP_MAX_DEST_RD_ATOMIC
| IB_QP_MIN_RNR_TIMER
;
3099 qp_attr
->ah_attr
= cm_id_priv
->av
.ah_attr
;
3100 qp_attr
->path_mtu
= cm_id_priv
->path_mtu
;
3101 qp_attr
->dest_qp_num
= be32_to_cpu(cm_id_priv
->remote_qpn
);
3102 qp_attr
->rq_psn
= be32_to_cpu(cm_id_priv
->rq_psn
);
3103 qp_attr
->max_dest_rd_atomic
= cm_id_priv
->responder_resources
;
3104 qp_attr
->min_rnr_timer
= 0;
3105 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3106 *qp_attr_mask
|= IB_QP_ALT_PATH
;
3107 qp_attr
->alt_ah_attr
= cm_id_priv
->alt_av
.ah_attr
;
3115 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3119 static int cm_init_qp_rts_attr(struct cm_id_private
*cm_id_priv
,
3120 struct ib_qp_attr
*qp_attr
,
3123 unsigned long flags
;
3126 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
3127 switch (cm_id_priv
->id
.state
) {
3128 case IB_CM_REP_RCVD
:
3129 case IB_CM_MRA_REP_SENT
:
3130 case IB_CM_REP_SENT
:
3131 case IB_CM_MRA_REP_RCVD
:
3132 case IB_CM_ESTABLISHED
:
3133 *qp_attr_mask
= IB_QP_STATE
| IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
|
3134 IB_QP_RNR_RETRY
| IB_QP_SQ_PSN
|
3135 IB_QP_MAX_QP_RD_ATOMIC
;
3136 qp_attr
->timeout
= cm_id_priv
->local_ack_timeout
;
3137 qp_attr
->retry_cnt
= cm_id_priv
->retry_count
;
3138 qp_attr
->rnr_retry
= cm_id_priv
->rnr_retry_count
;
3139 qp_attr
->sq_psn
= be32_to_cpu(cm_id_priv
->sq_psn
);
3140 qp_attr
->max_rd_atomic
= cm_id_priv
->initiator_depth
;
3141 if (cm_id_priv
->alt_av
.ah_attr
.dlid
) {
3142 *qp_attr_mask
|= IB_QP_PATH_MIG_STATE
;
3143 qp_attr
->path_mig_state
= IB_MIG_REARM
;
3151 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
3155 int ib_cm_init_qp_attr(struct ib_cm_id
*cm_id
,
3156 struct ib_qp_attr
*qp_attr
,
3159 struct cm_id_private
*cm_id_priv
;
3162 cm_id_priv
= container_of(cm_id
, struct cm_id_private
, id
);
3163 switch (qp_attr
->qp_state
) {
3165 ret
= cm_init_qp_init_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3168 ret
= cm_init_qp_rtr_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3171 ret
= cm_init_qp_rts_attr(cm_id_priv
, qp_attr
, qp_attr_mask
);
3179 EXPORT_SYMBOL(ib_cm_init_qp_attr
);
3181 static __be64
cm_get_ca_guid(struct ib_device
*device
)
3183 struct ib_device_attr
*device_attr
;
3187 device_attr
= kmalloc(sizeof *device_attr
, GFP_KERNEL
);
3191 ret
= ib_query_device(device
, device_attr
);
3192 guid
= ret
? 0 : device_attr
->node_guid
;
3197 static void cm_add_one(struct ib_device
*device
)
3199 struct cm_device
*cm_dev
;
3200 struct cm_port
*port
;
3201 struct ib_mad_reg_req reg_req
= {
3202 .mgmt_class
= IB_MGMT_CLASS_CM
,
3203 .mgmt_class_version
= IB_CM_CLASS_VERSION
3205 struct ib_port_modify port_modify
= {
3206 .set_port_cap_mask
= IB_PORT_CM_SUP
3208 unsigned long flags
;
3212 cm_dev
= kmalloc(sizeof(*cm_dev
) + sizeof(*port
) *
3213 device
->phys_port_cnt
, GFP_KERNEL
);
3217 cm_dev
->device
= device
;
3218 cm_dev
->ca_guid
= cm_get_ca_guid(device
);
3219 if (!cm_dev
->ca_guid
)
3222 set_bit(IB_MGMT_METHOD_SEND
, reg_req
.method_mask
);
3223 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3224 port
= &cm_dev
->port
[i
-1];
3225 port
->cm_dev
= cm_dev
;
3227 port
->mad_agent
= ib_register_mad_agent(device
, i
,
3234 if (IS_ERR(port
->mad_agent
))
3237 ret
= ib_modify_port(device
, i
, 0, &port_modify
);
3241 ib_set_client_data(device
, &cm_client
, cm_dev
);
3243 write_lock_irqsave(&cm
.device_lock
, flags
);
3244 list_add_tail(&cm_dev
->list
, &cm
.device_list
);
3245 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3249 ib_unregister_mad_agent(port
->mad_agent
);
3251 port_modify
.set_port_cap_mask
= 0;
3252 port_modify
.clr_port_cap_mask
= IB_PORT_CM_SUP
;
3254 port
= &cm_dev
->port
[i
-1];
3255 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3256 ib_unregister_mad_agent(port
->mad_agent
);
3262 static void cm_remove_one(struct ib_device
*device
)
3264 struct cm_device
*cm_dev
;
3265 struct cm_port
*port
;
3266 struct ib_port_modify port_modify
= {
3267 .clr_port_cap_mask
= IB_PORT_CM_SUP
3269 unsigned long flags
;
3272 cm_dev
= ib_get_client_data(device
, &cm_client
);
3276 write_lock_irqsave(&cm
.device_lock
, flags
);
3277 list_del(&cm_dev
->list
);
3278 write_unlock_irqrestore(&cm
.device_lock
, flags
);
3280 for (i
= 1; i
<= device
->phys_port_cnt
; i
++) {
3281 port
= &cm_dev
->port
[i
-1];
3282 ib_modify_port(device
, port
->port_num
, 0, &port_modify
);
3283 ib_unregister_mad_agent(port
->mad_agent
);
3288 static int __init
ib_cm_init(void)
3292 memset(&cm
, 0, sizeof cm
);
3293 INIT_LIST_HEAD(&cm
.device_list
);
3294 rwlock_init(&cm
.device_lock
);
3295 spin_lock_init(&cm
.lock
);
3296 cm
.listen_service_table
= RB_ROOT
;
3297 cm
.listen_service_id
= __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID
);
3298 cm
.remote_id_table
= RB_ROOT
;
3299 cm
.remote_qp_table
= RB_ROOT
;
3300 cm
.remote_sidr_table
= RB_ROOT
;
3301 idr_init(&cm
.local_id_table
);
3302 idr_pre_get(&cm
.local_id_table
, GFP_KERNEL
);
3304 cm
.wq
= create_workqueue("ib_cm");
3308 ret
= ib_register_client(&cm_client
);
3314 destroy_workqueue(cm
.wq
);
3318 static void __exit
ib_cm_cleanup(void)
3320 flush_workqueue(cm
.wq
);
3321 destroy_workqueue(cm
.wq
);
3322 ib_unregister_client(&cm_client
);
3325 module_init(ib_cm_init
);
3326 module_exit(ib_cm_cleanup
);