2 * Copyright (c) 2004-2006 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
56 static void cm_add_one(struct ib_device *device);
57 static void cm_remove_one(struct ib_device *device);
59 static struct ib_client cm_client = {
62 .remove = cm_remove_one
67 struct list_head device_list;
69 struct rb_root listen_service_table;
70 u64 listen_service_id;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table;
73 struct rb_root remote_id_table;
74 struct rb_root remote_sidr_table;
75 struct idr local_id_table;
76 __be32 random_id_operand;
77 struct list_head timewait_list;
78 struct workqueue_struct *wq;
82 struct cm_device *cm_dev;
83 struct ib_mad_agent *mad_agent;
88 struct list_head list;
89 struct ib_device *device;
91 struct cm_port port[0];
97 struct ib_ah_attr ah_attr;
103 struct delayed_work work;
104 struct list_head list;
105 struct cm_port *port;
106 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
107 __be32 local_id; /* Established / timewait */
109 struct ib_cm_event cm_event;
110 struct ib_sa_path_rec path[0];
113 struct cm_timewait_info {
114 struct cm_work work; /* Must be first. */
115 struct list_head list;
116 struct rb_node remote_qp_node;
117 struct rb_node remote_id_node;
118 __be64 remote_ca_guid;
120 u8 inserted_remote_qp;
121 u8 inserted_remote_id;
124 struct cm_id_private {
127 struct rb_node service_node;
128 struct rb_node sidr_id_node;
129 spinlock_t lock; /* Do not acquire inside cm.lock */
130 struct completion comp;
133 struct ib_mad_send_buf *msg;
134 struct cm_timewait_info *timewait_info;
135 /* todo: use alternate port on send failure */
138 struct ib_cm_compare_data *compare_data;
144 enum ib_qp_type qp_type;
148 enum ib_mtu path_mtu;
153 u8 responder_resources;
160 struct list_head work_list;
164 static void cm_work_handler(struct work_struct *work);
166 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
168 if (atomic_dec_and_test(&cm_id_priv->refcount))
169 complete(&cm_id_priv->comp);
172 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
173 struct ib_mad_send_buf **msg)
175 struct ib_mad_agent *mad_agent;
176 struct ib_mad_send_buf *m;
179 mad_agent = cm_id_priv->av.port->mad_agent;
180 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
184 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
185 cm_id_priv->av.pkey_index,
186 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
193 /* Timeout set by caller if response is expected. */
195 m->retries = cm_id_priv->max_cm_retries;
197 atomic_inc(&cm_id_priv->refcount);
198 m->context[0] = cm_id_priv;
203 static int cm_alloc_response_msg(struct cm_port *port,
204 struct ib_mad_recv_wc *mad_recv_wc,
205 struct ib_mad_send_buf **msg)
207 struct ib_mad_send_buf *m;
210 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
211 mad_recv_wc->recv_buf.grh, port->port_num);
215 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
216 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
227 static void cm_free_msg(struct ib_mad_send_buf *msg)
229 ib_destroy_ah(msg->ah);
231 cm_deref_id(msg->context[0]);
232 ib_free_send_mad(msg);
235 static void * cm_copy_private_data(const void *private_data,
240 if (!private_data || !private_data_len)
243 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
245 return ERR_PTR(-ENOMEM);
250 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
251 void *private_data, u8 private_data_len)
253 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
254 kfree(cm_id_priv->private_data);
256 cm_id_priv->private_data = private_data;
257 cm_id_priv->private_data_len = private_data_len;
260 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
261 struct ib_grh *grh, struct cm_av *av)
264 av->pkey_index = wc->pkey_index;
265 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
269 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
271 struct cm_device *cm_dev;
272 struct cm_port *port = NULL;
277 read_lock_irqsave(&cm.device_lock, flags);
278 list_for_each_entry(cm_dev, &cm.device_list, list) {
279 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
281 port = &cm_dev->port[p-1];
285 read_unlock_irqrestore(&cm.device_lock, flags);
290 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
291 be16_to_cpu(path->pkey), &av->pkey_index);
296 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
298 av->timeout = path->packet_life_time + 1;
302 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
309 spin_lock_irqsave(&cm.lock, flags);
310 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
313 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
314 spin_unlock_irqrestore(&cm.lock, flags);
315 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
317 cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand);
321 static void cm_free_id(__be32 local_id)
323 spin_lock_irq(&cm.lock);
324 idr_remove(&cm.local_id_table,
325 (__force int) (local_id ^ cm.random_id_operand));
326 spin_unlock_irq(&cm.lock);
329 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
331 struct cm_id_private *cm_id_priv;
333 cm_id_priv = idr_find(&cm.local_id_table,
334 (__force int) (local_id ^ cm.random_id_operand));
336 if (cm_id_priv->id.remote_id == remote_id)
337 atomic_inc(&cm_id_priv->refcount);
345 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
347 struct cm_id_private *cm_id_priv;
349 spin_lock_irq(&cm.lock);
350 cm_id_priv = cm_get_id(local_id, remote_id);
351 spin_unlock_irq(&cm.lock);
356 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
360 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
361 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
362 ((unsigned long *) mask)[i];
365 static int cm_compare_data(struct ib_cm_compare_data *src_data,
366 struct ib_cm_compare_data *dst_data)
368 u8 src[IB_CM_COMPARE_SIZE];
369 u8 dst[IB_CM_COMPARE_SIZE];
371 if (!src_data || !dst_data)
374 cm_mask_copy(src, src_data->data, dst_data->mask);
375 cm_mask_copy(dst, dst_data->data, src_data->mask);
376 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
379 static int cm_compare_private_data(u8 *private_data,
380 struct ib_cm_compare_data *dst_data)
382 u8 src[IB_CM_COMPARE_SIZE];
387 cm_mask_copy(src, private_data, dst_data->mask);
388 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
391 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
393 struct rb_node **link = &cm.listen_service_table.rb_node;
394 struct rb_node *parent = NULL;
395 struct cm_id_private *cur_cm_id_priv;
396 __be64 service_id = cm_id_priv->id.service_id;
397 __be64 service_mask = cm_id_priv->id.service_mask;
402 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
404 data_cmp = cm_compare_data(cm_id_priv->compare_data,
405 cur_cm_id_priv->compare_data);
406 if ((cur_cm_id_priv->id.service_mask & service_id) ==
407 (service_mask & cur_cm_id_priv->id.service_id) &&
408 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
410 return cur_cm_id_priv;
412 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
413 link = &(*link)->rb_left;
414 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
415 link = &(*link)->rb_right;
416 else if (service_id < cur_cm_id_priv->id.service_id)
417 link = &(*link)->rb_left;
418 else if (service_id > cur_cm_id_priv->id.service_id)
419 link = &(*link)->rb_right;
420 else if (data_cmp < 0)
421 link = &(*link)->rb_left;
423 link = &(*link)->rb_right;
425 rb_link_node(&cm_id_priv->service_node, parent, link);
426 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
430 static struct cm_id_private * cm_find_listen(struct ib_device *device,
434 struct rb_node *node = cm.listen_service_table.rb_node;
435 struct cm_id_private *cm_id_priv;
439 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
440 data_cmp = cm_compare_private_data(private_data,
441 cm_id_priv->compare_data);
442 if ((cm_id_priv->id.service_mask & service_id) ==
443 cm_id_priv->id.service_id &&
444 (cm_id_priv->id.device == device) && !data_cmp)
447 if (device < cm_id_priv->id.device)
448 node = node->rb_left;
449 else if (device > cm_id_priv->id.device)
450 node = node->rb_right;
451 else if (service_id < cm_id_priv->id.service_id)
452 node = node->rb_left;
453 else if (service_id > cm_id_priv->id.service_id)
454 node = node->rb_right;
455 else if (data_cmp < 0)
456 node = node->rb_left;
458 node = node->rb_right;
463 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
466 struct rb_node **link = &cm.remote_id_table.rb_node;
467 struct rb_node *parent = NULL;
468 struct cm_timewait_info *cur_timewait_info;
469 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
470 __be32 remote_id = timewait_info->work.remote_id;
474 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
476 if (remote_id < cur_timewait_info->work.remote_id)
477 link = &(*link)->rb_left;
478 else if (remote_id > cur_timewait_info->work.remote_id)
479 link = &(*link)->rb_right;
480 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
481 link = &(*link)->rb_left;
482 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
483 link = &(*link)->rb_right;
485 return cur_timewait_info;
487 timewait_info->inserted_remote_id = 1;
488 rb_link_node(&timewait_info->remote_id_node, parent, link);
489 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
493 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
496 struct rb_node *node = cm.remote_id_table.rb_node;
497 struct cm_timewait_info *timewait_info;
500 timewait_info = rb_entry(node, struct cm_timewait_info,
502 if (remote_id < timewait_info->work.remote_id)
503 node = node->rb_left;
504 else if (remote_id > timewait_info->work.remote_id)
505 node = node->rb_right;
506 else if (remote_ca_guid < timewait_info->remote_ca_guid)
507 node = node->rb_left;
508 else if (remote_ca_guid > timewait_info->remote_ca_guid)
509 node = node->rb_right;
511 return timewait_info;
516 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
519 struct rb_node **link = &cm.remote_qp_table.rb_node;
520 struct rb_node *parent = NULL;
521 struct cm_timewait_info *cur_timewait_info;
522 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
523 __be32 remote_qpn = timewait_info->remote_qpn;
527 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
529 if (remote_qpn < cur_timewait_info->remote_qpn)
530 link = &(*link)->rb_left;
531 else if (remote_qpn > cur_timewait_info->remote_qpn)
532 link = &(*link)->rb_right;
533 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
534 link = &(*link)->rb_left;
535 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
536 link = &(*link)->rb_right;
538 return cur_timewait_info;
540 timewait_info->inserted_remote_qp = 1;
541 rb_link_node(&timewait_info->remote_qp_node, parent, link);
542 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
546 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
549 struct rb_node **link = &cm.remote_sidr_table.rb_node;
550 struct rb_node *parent = NULL;
551 struct cm_id_private *cur_cm_id_priv;
552 union ib_gid *port_gid = &cm_id_priv->av.dgid;
553 __be32 remote_id = cm_id_priv->id.remote_id;
557 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
559 if (remote_id < cur_cm_id_priv->id.remote_id)
560 link = &(*link)->rb_left;
561 else if (remote_id > cur_cm_id_priv->id.remote_id)
562 link = &(*link)->rb_right;
565 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
568 link = &(*link)->rb_left;
570 link = &(*link)->rb_right;
572 return cur_cm_id_priv;
575 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
576 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
580 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
581 enum ib_cm_sidr_status status)
583 struct ib_cm_sidr_rep_param param;
585 memset(¶m, 0, sizeof param);
586 param.status = status;
587 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
590 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
591 ib_cm_handler cm_handler,
594 struct cm_id_private *cm_id_priv;
597 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
601 cm_id_priv->id.state = IB_CM_IDLE;
602 cm_id_priv->id.device = device;
603 cm_id_priv->id.cm_handler = cm_handler;
604 cm_id_priv->id.context = context;
605 cm_id_priv->id.remote_cm_qpn = 1;
606 ret = cm_alloc_id(cm_id_priv);
610 spin_lock_init(&cm_id_priv->lock);
611 init_completion(&cm_id_priv->comp);
612 INIT_LIST_HEAD(&cm_id_priv->work_list);
613 atomic_set(&cm_id_priv->work_count, -1);
614 atomic_set(&cm_id_priv->refcount, 1);
615 return &cm_id_priv->id;
619 return ERR_PTR(-ENOMEM);
621 EXPORT_SYMBOL(ib_create_cm_id);
623 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
625 struct cm_work *work;
627 if (list_empty(&cm_id_priv->work_list))
630 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
631 list_del(&work->list);
635 static void cm_free_work(struct cm_work *work)
637 if (work->mad_recv_wc)
638 ib_free_recv_mad(work->mad_recv_wc);
642 static inline int cm_convert_to_ms(int iba_time)
644 /* approximate conversion to ms from 4.096us x 2^iba_time */
645 return 1 << max(iba_time - 8, 0);
649 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
650 * Because of how ack_timeout is stored, adding one doubles the timeout.
651 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
652 * increment it (round up) only if the other is within 50%.
654 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
656 int ack_timeout = packet_life_time + 1;
658 if (ack_timeout >= ca_ack_delay)
659 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
661 ack_timeout = ca_ack_delay +
662 (ack_timeout >= (ca_ack_delay - 1));
664 return min(31, ack_timeout);
667 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
669 if (timewait_info->inserted_remote_id) {
670 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
671 timewait_info->inserted_remote_id = 0;
674 if (timewait_info->inserted_remote_qp) {
675 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
676 timewait_info->inserted_remote_qp = 0;
680 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
682 struct cm_timewait_info *timewait_info;
684 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
686 return ERR_PTR(-ENOMEM);
688 timewait_info->work.local_id = local_id;
689 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
690 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
691 return timewait_info;
694 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
699 spin_lock_irqsave(&cm.lock, flags);
700 cm_cleanup_timewait(cm_id_priv->timewait_info);
701 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
702 spin_unlock_irqrestore(&cm.lock, flags);
705 * The cm_id could be destroyed by the user before we exit timewait.
706 * To protect against this, we search for the cm_id after exiting
707 * timewait before notifying the user that we've exited timewait.
709 cm_id_priv->id.state = IB_CM_TIMEWAIT;
710 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
711 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
712 msecs_to_jiffies(wait_time));
713 cm_id_priv->timewait_info = NULL;
716 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
720 cm_id_priv->id.state = IB_CM_IDLE;
721 if (cm_id_priv->timewait_info) {
722 spin_lock_irqsave(&cm.lock, flags);
723 cm_cleanup_timewait(cm_id_priv->timewait_info);
724 spin_unlock_irqrestore(&cm.lock, flags);
725 kfree(cm_id_priv->timewait_info);
726 cm_id_priv->timewait_info = NULL;
730 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
732 struct cm_id_private *cm_id_priv;
733 struct cm_work *work;
735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
737 spin_lock_irq(&cm_id_priv->lock);
738 switch (cm_id->state) {
740 cm_id->state = IB_CM_IDLE;
741 spin_unlock_irq(&cm_id_priv->lock);
742 spin_lock_irq(&cm.lock);
743 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
744 spin_unlock_irq(&cm.lock);
746 case IB_CM_SIDR_REQ_SENT:
747 cm_id->state = IB_CM_IDLE;
748 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
749 spin_unlock_irq(&cm_id_priv->lock);
751 case IB_CM_SIDR_REQ_RCVD:
752 spin_unlock_irq(&cm_id_priv->lock);
753 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
756 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
757 spin_unlock_irq(&cm_id_priv->lock);
758 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
759 &cm_id_priv->id.device->node_guid,
760 sizeof cm_id_priv->id.device->node_guid,
764 if (err == -ENOMEM) {
765 /* Do not reject to allow future retries. */
766 cm_reset_to_idle(cm_id_priv);
767 spin_unlock_irq(&cm_id_priv->lock);
769 spin_unlock_irq(&cm_id_priv->lock);
770 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
774 case IB_CM_MRA_REQ_RCVD:
776 case IB_CM_MRA_REP_RCVD:
777 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
779 case IB_CM_MRA_REQ_SENT:
781 case IB_CM_MRA_REP_SENT:
782 spin_unlock_irq(&cm_id_priv->lock);
783 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
786 case IB_CM_ESTABLISHED:
787 spin_unlock_irq(&cm_id_priv->lock);
788 ib_send_cm_dreq(cm_id, NULL, 0);
790 case IB_CM_DREQ_SENT:
791 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
792 cm_enter_timewait(cm_id_priv);
793 spin_unlock_irq(&cm_id_priv->lock);
795 case IB_CM_DREQ_RCVD:
796 spin_unlock_irq(&cm_id_priv->lock);
797 ib_send_cm_drep(cm_id, NULL, 0);
800 spin_unlock_irq(&cm_id_priv->lock);
804 cm_free_id(cm_id->local_id);
805 cm_deref_id(cm_id_priv);
806 wait_for_completion(&cm_id_priv->comp);
807 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
809 kfree(cm_id_priv->compare_data);
810 kfree(cm_id_priv->private_data);
814 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
816 cm_destroy_id(cm_id, 0);
818 EXPORT_SYMBOL(ib_destroy_cm_id);
820 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
821 struct ib_cm_compare_data *compare_data)
823 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
827 service_mask = service_mask ? service_mask :
828 __constant_cpu_to_be64(~0ULL);
829 service_id &= service_mask;
830 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
831 (service_id != IB_CM_ASSIGN_SERVICE_ID))
834 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
835 if (cm_id->state != IB_CM_IDLE)
839 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
841 if (!cm_id_priv->compare_data)
843 cm_mask_copy(cm_id_priv->compare_data->data,
844 compare_data->data, compare_data->mask);
845 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
849 cm_id->state = IB_CM_LISTEN;
851 spin_lock_irqsave(&cm.lock, flags);
852 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
853 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
854 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
856 cm_id->service_id = service_id;
857 cm_id->service_mask = service_mask;
859 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
860 spin_unlock_irqrestore(&cm.lock, flags);
862 if (cur_cm_id_priv) {
863 cm_id->state = IB_CM_IDLE;
864 kfree(cm_id_priv->compare_data);
865 cm_id_priv->compare_data = NULL;
870 EXPORT_SYMBOL(ib_cm_listen);
872 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
873 enum cm_msg_sequence msg_seq)
877 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
878 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
880 return cpu_to_be64(hi_tid | low_tid);
883 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
884 __be16 attr_id, __be64 tid)
886 hdr->base_version = IB_MGMT_BASE_VERSION;
887 hdr->mgmt_class = IB_MGMT_CLASS_CM;
888 hdr->class_version = IB_CM_CLASS_VERSION;
889 hdr->method = IB_MGMT_METHOD_SEND;
890 hdr->attr_id = attr_id;
894 static void cm_format_req(struct cm_req_msg *req_msg,
895 struct cm_id_private *cm_id_priv,
896 struct ib_cm_req_param *param)
898 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
899 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
901 req_msg->local_comm_id = cm_id_priv->id.local_id;
902 req_msg->service_id = param->service_id;
903 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
904 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
905 cm_req_set_resp_res(req_msg, param->responder_resources);
906 cm_req_set_init_depth(req_msg, param->initiator_depth);
907 cm_req_set_remote_resp_timeout(req_msg,
908 param->remote_cm_response_timeout);
909 cm_req_set_qp_type(req_msg, param->qp_type);
910 cm_req_set_flow_ctrl(req_msg, param->flow_control);
911 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
912 cm_req_set_local_resp_timeout(req_msg,
913 param->local_cm_response_timeout);
914 cm_req_set_retry_count(req_msg, param->retry_count);
915 req_msg->pkey = param->primary_path->pkey;
916 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
917 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
918 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
919 cm_req_set_srq(req_msg, param->srq);
921 req_msg->primary_local_lid = param->primary_path->slid;
922 req_msg->primary_remote_lid = param->primary_path->dlid;
923 req_msg->primary_local_gid = param->primary_path->sgid;
924 req_msg->primary_remote_gid = param->primary_path->dgid;
925 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
926 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
927 req_msg->primary_traffic_class = param->primary_path->traffic_class;
928 req_msg->primary_hop_limit = param->primary_path->hop_limit;
929 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
930 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
931 cm_req_set_primary_local_ack_timeout(req_msg,
932 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
933 param->primary_path->packet_life_time));
935 if (param->alternate_path) {
936 req_msg->alt_local_lid = param->alternate_path->slid;
937 req_msg->alt_remote_lid = param->alternate_path->dlid;
938 req_msg->alt_local_gid = param->alternate_path->sgid;
939 req_msg->alt_remote_gid = param->alternate_path->dgid;
940 cm_req_set_alt_flow_label(req_msg,
941 param->alternate_path->flow_label);
942 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
943 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
944 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
945 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
946 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
947 cm_req_set_alt_local_ack_timeout(req_msg,
948 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
949 param->alternate_path->packet_life_time));
952 if (param->private_data && param->private_data_len)
953 memcpy(req_msg->private_data, param->private_data,
954 param->private_data_len);
957 static int cm_validate_req_param(struct ib_cm_req_param *param)
959 /* peer-to-peer not supported */
960 if (param->peer_to_peer)
963 if (!param->primary_path)
966 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
969 if (param->private_data &&
970 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
973 if (param->alternate_path &&
974 (param->alternate_path->pkey != param->primary_path->pkey ||
975 param->alternate_path->mtu != param->primary_path->mtu))
981 int ib_send_cm_req(struct ib_cm_id *cm_id,
982 struct ib_cm_req_param *param)
984 struct cm_id_private *cm_id_priv;
985 struct cm_req_msg *req_msg;
989 ret = cm_validate_req_param(param);
993 /* Verify that we're not in timewait. */
994 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
995 spin_lock_irqsave(&cm_id_priv->lock, flags);
996 if (cm_id->state != IB_CM_IDLE) {
997 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1001 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1003 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1005 if (IS_ERR(cm_id_priv->timewait_info)) {
1006 ret = PTR_ERR(cm_id_priv->timewait_info);
1010 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1013 if (param->alternate_path) {
1014 ret = cm_init_av_by_path(param->alternate_path,
1015 &cm_id_priv->alt_av);
1019 cm_id->service_id = param->service_id;
1020 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1021 cm_id_priv->timeout_ms = cm_convert_to_ms(
1022 param->primary_path->packet_life_time) * 2 +
1024 param->remote_cm_response_timeout);
1025 cm_id_priv->max_cm_retries = param->max_cm_retries;
1026 cm_id_priv->initiator_depth = param->initiator_depth;
1027 cm_id_priv->responder_resources = param->responder_resources;
1028 cm_id_priv->retry_count = param->retry_count;
1029 cm_id_priv->path_mtu = param->primary_path->mtu;
1030 cm_id_priv->pkey = param->primary_path->pkey;
1031 cm_id_priv->qp_type = param->qp_type;
1033 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1037 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1038 cm_format_req(req_msg, cm_id_priv, param);
1039 cm_id_priv->tid = req_msg->hdr.tid;
1040 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1041 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1043 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1044 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1046 spin_lock_irqsave(&cm_id_priv->lock, flags);
1047 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1049 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1052 BUG_ON(cm_id->state != IB_CM_IDLE);
1053 cm_id->state = IB_CM_REQ_SENT;
1054 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1057 error2: cm_free_msg(cm_id_priv->msg);
1058 error1: kfree(cm_id_priv->timewait_info);
1061 EXPORT_SYMBOL(ib_send_cm_req);
1063 static int cm_issue_rej(struct cm_port *port,
1064 struct ib_mad_recv_wc *mad_recv_wc,
1065 enum ib_cm_rej_reason reason,
1066 enum cm_msg_response msg_rejected,
1067 void *ari, u8 ari_length)
1069 struct ib_mad_send_buf *msg = NULL;
1070 struct cm_rej_msg *rej_msg, *rcv_msg;
1073 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1077 /* We just need common CM header information. Cast to any message. */
1078 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1079 rej_msg = (struct cm_rej_msg *) msg->mad;
1081 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1082 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1083 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1084 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1085 rej_msg->reason = cpu_to_be16(reason);
1087 if (ari && ari_length) {
1088 cm_rej_set_reject_info_len(rej_msg, ari_length);
1089 memcpy(rej_msg->ari, ari, ari_length);
1092 ret = ib_post_send_mad(msg, NULL);
1099 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1100 __be32 local_qpn, __be32 remote_qpn)
1102 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1103 ((local_ca_guid == remote_ca_guid) &&
1104 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1107 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1108 struct ib_sa_path_rec *primary_path,
1109 struct ib_sa_path_rec *alt_path)
1111 memset(primary_path, 0, sizeof *primary_path);
1112 primary_path->dgid = req_msg->primary_local_gid;
1113 primary_path->sgid = req_msg->primary_remote_gid;
1114 primary_path->dlid = req_msg->primary_local_lid;
1115 primary_path->slid = req_msg->primary_remote_lid;
1116 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1117 primary_path->hop_limit = req_msg->primary_hop_limit;
1118 primary_path->traffic_class = req_msg->primary_traffic_class;
1119 primary_path->reversible = 1;
1120 primary_path->pkey = req_msg->pkey;
1121 primary_path->sl = cm_req_get_primary_sl(req_msg);
1122 primary_path->mtu_selector = IB_SA_EQ;
1123 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1124 primary_path->rate_selector = IB_SA_EQ;
1125 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1126 primary_path->packet_life_time_selector = IB_SA_EQ;
1127 primary_path->packet_life_time =
1128 cm_req_get_primary_local_ack_timeout(req_msg);
1129 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1131 if (req_msg->alt_local_lid) {
1132 memset(alt_path, 0, sizeof *alt_path);
1133 alt_path->dgid = req_msg->alt_local_gid;
1134 alt_path->sgid = req_msg->alt_remote_gid;
1135 alt_path->dlid = req_msg->alt_local_lid;
1136 alt_path->slid = req_msg->alt_remote_lid;
1137 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1138 alt_path->hop_limit = req_msg->alt_hop_limit;
1139 alt_path->traffic_class = req_msg->alt_traffic_class;
1140 alt_path->reversible = 1;
1141 alt_path->pkey = req_msg->pkey;
1142 alt_path->sl = cm_req_get_alt_sl(req_msg);
1143 alt_path->mtu_selector = IB_SA_EQ;
1144 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1145 alt_path->rate_selector = IB_SA_EQ;
1146 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1147 alt_path->packet_life_time_selector = IB_SA_EQ;
1148 alt_path->packet_life_time =
1149 cm_req_get_alt_local_ack_timeout(req_msg);
1150 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1154 static void cm_format_req_event(struct cm_work *work,
1155 struct cm_id_private *cm_id_priv,
1156 struct ib_cm_id *listen_id)
1158 struct cm_req_msg *req_msg;
1159 struct ib_cm_req_event_param *param;
1161 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1162 param = &work->cm_event.param.req_rcvd;
1163 param->listen_id = listen_id;
1164 param->port = cm_id_priv->av.port->port_num;
1165 param->primary_path = &work->path[0];
1166 if (req_msg->alt_local_lid)
1167 param->alternate_path = &work->path[1];
1169 param->alternate_path = NULL;
1170 param->remote_ca_guid = req_msg->local_ca_guid;
1171 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1172 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1173 param->qp_type = cm_req_get_qp_type(req_msg);
1174 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1175 param->responder_resources = cm_req_get_init_depth(req_msg);
1176 param->initiator_depth = cm_req_get_resp_res(req_msg);
1177 param->local_cm_response_timeout =
1178 cm_req_get_remote_resp_timeout(req_msg);
1179 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1180 param->remote_cm_response_timeout =
1181 cm_req_get_local_resp_timeout(req_msg);
1182 param->retry_count = cm_req_get_retry_count(req_msg);
1183 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1184 param->srq = cm_req_get_srq(req_msg);
1185 work->cm_event.private_data = &req_msg->private_data;
1188 static void cm_process_work(struct cm_id_private *cm_id_priv,
1189 struct cm_work *work)
1193 /* We will typically only have the current event to report. */
1194 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1197 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1198 spin_lock_irq(&cm_id_priv->lock);
1199 work = cm_dequeue_work(cm_id_priv);
1200 spin_unlock_irq(&cm_id_priv->lock);
1202 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1206 cm_deref_id(cm_id_priv);
1208 cm_destroy_id(&cm_id_priv->id, ret);
1211 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1212 struct cm_id_private *cm_id_priv,
1213 enum cm_msg_response msg_mraed, u8 service_timeout,
1214 const void *private_data, u8 private_data_len)
1216 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1217 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1218 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1219 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1220 cm_mra_set_service_timeout(mra_msg, service_timeout);
1222 if (private_data && private_data_len)
1223 memcpy(mra_msg->private_data, private_data, private_data_len);
1226 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1227 struct cm_id_private *cm_id_priv,
1228 enum ib_cm_rej_reason reason,
1231 const void *private_data,
1232 u8 private_data_len)
1234 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1235 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1237 switch(cm_id_priv->id.state) {
1238 case IB_CM_REQ_RCVD:
1239 rej_msg->local_comm_id = 0;
1240 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1242 case IB_CM_MRA_REQ_SENT:
1243 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1244 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1246 case IB_CM_REP_RCVD:
1247 case IB_CM_MRA_REP_SENT:
1248 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1249 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1252 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1253 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1257 rej_msg->reason = cpu_to_be16(reason);
1258 if (ari && ari_length) {
1259 cm_rej_set_reject_info_len(rej_msg, ari_length);
1260 memcpy(rej_msg->ari, ari, ari_length);
1263 if (private_data && private_data_len)
1264 memcpy(rej_msg->private_data, private_data, private_data_len);
1267 static void cm_dup_req_handler(struct cm_work *work,
1268 struct cm_id_private *cm_id_priv)
1270 struct ib_mad_send_buf *msg = NULL;
1273 /* Quick state check to discard duplicate REQs. */
1274 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1277 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1281 spin_lock_irq(&cm_id_priv->lock);
1282 switch (cm_id_priv->id.state) {
1283 case IB_CM_MRA_REQ_SENT:
1284 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1285 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1286 cm_id_priv->private_data,
1287 cm_id_priv->private_data_len);
1289 case IB_CM_TIMEWAIT:
1290 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1291 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1296 spin_unlock_irq(&cm_id_priv->lock);
1298 ret = ib_post_send_mad(msg, NULL);
1303 unlock: spin_unlock_irq(&cm_id_priv->lock);
1304 free: cm_free_msg(msg);
1307 static struct cm_id_private * cm_match_req(struct cm_work *work,
1308 struct cm_id_private *cm_id_priv)
1310 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1311 struct cm_timewait_info *timewait_info;
1312 struct cm_req_msg *req_msg;
1314 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1316 /* Check for possible duplicate REQ. */
1317 spin_lock_irq(&cm.lock);
1318 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1319 if (timewait_info) {
1320 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1321 timewait_info->work.remote_id);
1322 spin_unlock_irq(&cm.lock);
1323 if (cur_cm_id_priv) {
1324 cm_dup_req_handler(work, cur_cm_id_priv);
1325 cm_deref_id(cur_cm_id_priv);
1330 /* Check for stale connections. */
1331 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1332 if (timewait_info) {
1333 cm_cleanup_timewait(cm_id_priv->timewait_info);
1334 spin_unlock_irq(&cm.lock);
1335 cm_issue_rej(work->port, work->mad_recv_wc,
1336 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1341 /* Find matching listen request. */
1342 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1343 req_msg->service_id,
1344 req_msg->private_data);
1345 if (!listen_cm_id_priv) {
1346 cm_cleanup_timewait(cm_id_priv->timewait_info);
1347 spin_unlock_irq(&cm.lock);
1348 cm_issue_rej(work->port, work->mad_recv_wc,
1349 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1353 atomic_inc(&listen_cm_id_priv->refcount);
1354 atomic_inc(&cm_id_priv->refcount);
1355 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1356 atomic_inc(&cm_id_priv->work_count);
1357 spin_unlock_irq(&cm.lock);
1359 return listen_cm_id_priv;
1362 static int cm_req_handler(struct cm_work *work)
1364 struct ib_cm_id *cm_id;
1365 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1366 struct cm_req_msg *req_msg;
1369 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1371 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1373 return PTR_ERR(cm_id);
1375 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1376 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1377 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1378 work->mad_recv_wc->recv_buf.grh,
1380 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1382 if (IS_ERR(cm_id_priv->timewait_info)) {
1383 ret = PTR_ERR(cm_id_priv->timewait_info);
1386 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1387 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1388 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1390 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1391 if (!listen_cm_id_priv) {
1393 kfree(cm_id_priv->timewait_info);
1397 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1398 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1399 cm_id_priv->id.service_id = req_msg->service_id;
1400 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1402 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1403 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1405 ib_get_cached_gid(work->port->cm_dev->device,
1406 work->port->port_num, 0, &work->path[0].sgid);
1407 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1408 &work->path[0].sgid, sizeof work->path[0].sgid,
1412 if (req_msg->alt_local_lid) {
1413 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1415 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1416 &work->path[0].sgid,
1417 sizeof work->path[0].sgid, NULL, 0);
1421 cm_id_priv->tid = req_msg->hdr.tid;
1422 cm_id_priv->timeout_ms = cm_convert_to_ms(
1423 cm_req_get_local_resp_timeout(req_msg));
1424 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1425 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1426 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1427 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1428 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1429 cm_id_priv->pkey = req_msg->pkey;
1430 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1431 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1432 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1433 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1435 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1436 cm_process_work(cm_id_priv, work);
1437 cm_deref_id(listen_cm_id_priv);
1441 atomic_dec(&cm_id_priv->refcount);
1442 cm_deref_id(listen_cm_id_priv);
1444 ib_destroy_cm_id(cm_id);
1448 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1449 struct cm_id_private *cm_id_priv,
1450 struct ib_cm_rep_param *param)
1452 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1453 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1454 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1455 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1456 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1457 rep_msg->resp_resources = param->responder_resources;
1458 rep_msg->initiator_depth = param->initiator_depth;
1459 cm_rep_set_target_ack_delay(rep_msg,
1460 cm_id_priv->av.port->cm_dev->ack_delay);
1461 cm_rep_set_failover(rep_msg, param->failover_accepted);
1462 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1463 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1464 cm_rep_set_srq(rep_msg, param->srq);
1465 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1467 if (param->private_data && param->private_data_len)
1468 memcpy(rep_msg->private_data, param->private_data,
1469 param->private_data_len);
1472 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1473 struct ib_cm_rep_param *param)
1475 struct cm_id_private *cm_id_priv;
1476 struct ib_mad_send_buf *msg;
1477 struct cm_rep_msg *rep_msg;
1478 unsigned long flags;
1481 if (param->private_data &&
1482 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1485 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1486 spin_lock_irqsave(&cm_id_priv->lock, flags);
1487 if (cm_id->state != IB_CM_REQ_RCVD &&
1488 cm_id->state != IB_CM_MRA_REQ_SENT) {
1493 ret = cm_alloc_msg(cm_id_priv, &msg);
1497 rep_msg = (struct cm_rep_msg *) msg->mad;
1498 cm_format_rep(rep_msg, cm_id_priv, param);
1499 msg->timeout_ms = cm_id_priv->timeout_ms;
1500 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1502 ret = ib_post_send_mad(msg, NULL);
1504 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1509 cm_id->state = IB_CM_REP_SENT;
1510 cm_id_priv->msg = msg;
1511 cm_id_priv->initiator_depth = param->initiator_depth;
1512 cm_id_priv->responder_resources = param->responder_resources;
1513 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1514 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1516 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1519 EXPORT_SYMBOL(ib_send_cm_rep);
1521 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1522 struct cm_id_private *cm_id_priv,
1523 const void *private_data,
1524 u8 private_data_len)
1526 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1527 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1528 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1530 if (private_data && private_data_len)
1531 memcpy(rtu_msg->private_data, private_data, private_data_len);
1534 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1535 const void *private_data,
1536 u8 private_data_len)
1538 struct cm_id_private *cm_id_priv;
1539 struct ib_mad_send_buf *msg;
1540 unsigned long flags;
1544 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1547 data = cm_copy_private_data(private_data, private_data_len);
1549 return PTR_ERR(data);
1551 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1552 spin_lock_irqsave(&cm_id_priv->lock, flags);
1553 if (cm_id->state != IB_CM_REP_RCVD &&
1554 cm_id->state != IB_CM_MRA_REP_SENT) {
1559 ret = cm_alloc_msg(cm_id_priv, &msg);
1563 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1564 private_data, private_data_len);
1566 ret = ib_post_send_mad(msg, NULL);
1568 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1574 cm_id->state = IB_CM_ESTABLISHED;
1575 cm_set_private_data(cm_id_priv, data, private_data_len);
1576 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1579 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1583 EXPORT_SYMBOL(ib_send_cm_rtu);
1585 static void cm_format_rep_event(struct cm_work *work)
1587 struct cm_rep_msg *rep_msg;
1588 struct ib_cm_rep_event_param *param;
1590 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1591 param = &work->cm_event.param.rep_rcvd;
1592 param->remote_ca_guid = rep_msg->local_ca_guid;
1593 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1594 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1595 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1596 param->responder_resources = rep_msg->initiator_depth;
1597 param->initiator_depth = rep_msg->resp_resources;
1598 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1599 param->failover_accepted = cm_rep_get_failover(rep_msg);
1600 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1601 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1602 param->srq = cm_rep_get_srq(rep_msg);
1603 work->cm_event.private_data = &rep_msg->private_data;
1606 static void cm_dup_rep_handler(struct cm_work *work)
1608 struct cm_id_private *cm_id_priv;
1609 struct cm_rep_msg *rep_msg;
1610 struct ib_mad_send_buf *msg = NULL;
1613 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1614 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1615 rep_msg->local_comm_id);
1619 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1623 spin_lock_irq(&cm_id_priv->lock);
1624 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1625 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1626 cm_id_priv->private_data,
1627 cm_id_priv->private_data_len);
1628 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1629 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1630 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1631 cm_id_priv->private_data,
1632 cm_id_priv->private_data_len);
1635 spin_unlock_irq(&cm_id_priv->lock);
1637 ret = ib_post_send_mad(msg, NULL);
1642 unlock: spin_unlock_irq(&cm_id_priv->lock);
1643 free: cm_free_msg(msg);
1644 deref: cm_deref_id(cm_id_priv);
1647 static int cm_rep_handler(struct cm_work *work)
1649 struct cm_id_private *cm_id_priv;
1650 struct cm_rep_msg *rep_msg;
1653 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1654 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1656 cm_dup_rep_handler(work);
1660 cm_format_rep_event(work);
1662 spin_lock_irq(&cm_id_priv->lock);
1663 switch (cm_id_priv->id.state) {
1664 case IB_CM_REQ_SENT:
1665 case IB_CM_MRA_REQ_RCVD:
1668 spin_unlock_irq(&cm_id_priv->lock);
1673 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1674 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1675 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1677 spin_lock(&cm.lock);
1678 /* Check for duplicate REP. */
1679 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1680 spin_unlock(&cm.lock);
1681 spin_unlock_irq(&cm_id_priv->lock);
1685 /* Check for a stale connection. */
1686 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1687 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1688 &cm.remote_id_table);
1689 cm_id_priv->timewait_info->inserted_remote_id = 0;
1690 spin_unlock(&cm.lock);
1691 spin_unlock_irq(&cm_id_priv->lock);
1692 cm_issue_rej(work->port, work->mad_recv_wc,
1693 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1698 spin_unlock(&cm.lock);
1700 cm_id_priv->id.state = IB_CM_REP_RCVD;
1701 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1702 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1703 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1704 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1705 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1706 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1707 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1708 cm_id_priv->av.timeout =
1709 cm_ack_timeout(cm_id_priv->target_ack_delay,
1710 cm_id_priv->av.timeout - 1);
1711 cm_id_priv->alt_av.timeout =
1712 cm_ack_timeout(cm_id_priv->target_ack_delay,
1713 cm_id_priv->alt_av.timeout - 1);
1715 /* todo: handle peer_to_peer */
1717 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1718 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1720 list_add_tail(&work->list, &cm_id_priv->work_list);
1721 spin_unlock_irq(&cm_id_priv->lock);
1724 cm_process_work(cm_id_priv, work);
1726 cm_deref_id(cm_id_priv);
1730 cm_deref_id(cm_id_priv);
1734 static int cm_establish_handler(struct cm_work *work)
1736 struct cm_id_private *cm_id_priv;
1739 /* See comment in cm_establish about lookup. */
1740 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1744 spin_lock_irq(&cm_id_priv->lock);
1745 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1746 spin_unlock_irq(&cm_id_priv->lock);
1750 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1751 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1753 list_add_tail(&work->list, &cm_id_priv->work_list);
1754 spin_unlock_irq(&cm_id_priv->lock);
1757 cm_process_work(cm_id_priv, work);
1759 cm_deref_id(cm_id_priv);
1762 cm_deref_id(cm_id_priv);
1766 static int cm_rtu_handler(struct cm_work *work)
1768 struct cm_id_private *cm_id_priv;
1769 struct cm_rtu_msg *rtu_msg;
1772 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1773 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1774 rtu_msg->local_comm_id);
1778 work->cm_event.private_data = &rtu_msg->private_data;
1780 spin_lock_irq(&cm_id_priv->lock);
1781 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1782 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1783 spin_unlock_irq(&cm_id_priv->lock);
1786 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1788 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1789 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1791 list_add_tail(&work->list, &cm_id_priv->work_list);
1792 spin_unlock_irq(&cm_id_priv->lock);
1795 cm_process_work(cm_id_priv, work);
1797 cm_deref_id(cm_id_priv);
1800 cm_deref_id(cm_id_priv);
1804 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1805 struct cm_id_private *cm_id_priv,
1806 const void *private_data,
1807 u8 private_data_len)
1809 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1810 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1811 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1812 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1813 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1815 if (private_data && private_data_len)
1816 memcpy(dreq_msg->private_data, private_data, private_data_len);
1819 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1820 const void *private_data,
1821 u8 private_data_len)
1823 struct cm_id_private *cm_id_priv;
1824 struct ib_mad_send_buf *msg;
1825 unsigned long flags;
1828 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1831 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1832 spin_lock_irqsave(&cm_id_priv->lock, flags);
1833 if (cm_id->state != IB_CM_ESTABLISHED) {
1838 ret = cm_alloc_msg(cm_id_priv, &msg);
1840 cm_enter_timewait(cm_id_priv);
1844 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1845 private_data, private_data_len);
1846 msg->timeout_ms = cm_id_priv->timeout_ms;
1847 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1849 ret = ib_post_send_mad(msg, NULL);
1851 cm_enter_timewait(cm_id_priv);
1852 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1857 cm_id->state = IB_CM_DREQ_SENT;
1858 cm_id_priv->msg = msg;
1859 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1862 EXPORT_SYMBOL(ib_send_cm_dreq);
1864 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1865 struct cm_id_private *cm_id_priv,
1866 const void *private_data,
1867 u8 private_data_len)
1869 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1870 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1871 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1873 if (private_data && private_data_len)
1874 memcpy(drep_msg->private_data, private_data, private_data_len);
1877 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1878 const void *private_data,
1879 u8 private_data_len)
1881 struct cm_id_private *cm_id_priv;
1882 struct ib_mad_send_buf *msg;
1883 unsigned long flags;
1887 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1890 data = cm_copy_private_data(private_data, private_data_len);
1892 return PTR_ERR(data);
1894 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1895 spin_lock_irqsave(&cm_id_priv->lock, flags);
1896 if (cm_id->state != IB_CM_DREQ_RCVD) {
1897 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1902 cm_set_private_data(cm_id_priv, data, private_data_len);
1903 cm_enter_timewait(cm_id_priv);
1905 ret = cm_alloc_msg(cm_id_priv, &msg);
1909 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1910 private_data, private_data_len);
1912 ret = ib_post_send_mad(msg, NULL);
1914 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1919 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1922 EXPORT_SYMBOL(ib_send_cm_drep);
1924 static int cm_issue_drep(struct cm_port *port,
1925 struct ib_mad_recv_wc *mad_recv_wc)
1927 struct ib_mad_send_buf *msg = NULL;
1928 struct cm_dreq_msg *dreq_msg;
1929 struct cm_drep_msg *drep_msg;
1932 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1936 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
1937 drep_msg = (struct cm_drep_msg *) msg->mad;
1939 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
1940 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
1941 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
1943 ret = ib_post_send_mad(msg, NULL);
1950 static int cm_dreq_handler(struct cm_work *work)
1952 struct cm_id_private *cm_id_priv;
1953 struct cm_dreq_msg *dreq_msg;
1954 struct ib_mad_send_buf *msg = NULL;
1957 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1958 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1959 dreq_msg->local_comm_id);
1961 cm_issue_drep(work->port, work->mad_recv_wc);
1965 work->cm_event.private_data = &dreq_msg->private_data;
1967 spin_lock_irq(&cm_id_priv->lock);
1968 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1971 switch (cm_id_priv->id.state) {
1972 case IB_CM_REP_SENT:
1973 case IB_CM_DREQ_SENT:
1974 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1976 case IB_CM_ESTABLISHED:
1977 case IB_CM_MRA_REP_RCVD:
1979 case IB_CM_TIMEWAIT:
1980 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1983 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1984 cm_id_priv->private_data,
1985 cm_id_priv->private_data_len);
1986 spin_unlock_irq(&cm_id_priv->lock);
1988 if (ib_post_send_mad(msg, NULL))
1994 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1995 cm_id_priv->tid = dreq_msg->hdr.tid;
1996 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1998 list_add_tail(&work->list, &cm_id_priv->work_list);
1999 spin_unlock_irq(&cm_id_priv->lock);
2002 cm_process_work(cm_id_priv, work);
2004 cm_deref_id(cm_id_priv);
2007 unlock: spin_unlock_irq(&cm_id_priv->lock);
2008 deref: cm_deref_id(cm_id_priv);
2012 static int cm_drep_handler(struct cm_work *work)
2014 struct cm_id_private *cm_id_priv;
2015 struct cm_drep_msg *drep_msg;
2018 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2019 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2020 drep_msg->local_comm_id);
2024 work->cm_event.private_data = &drep_msg->private_data;
2026 spin_lock_irq(&cm_id_priv->lock);
2027 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2028 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2029 spin_unlock_irq(&cm_id_priv->lock);
2032 cm_enter_timewait(cm_id_priv);
2034 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2035 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2037 list_add_tail(&work->list, &cm_id_priv->work_list);
2038 spin_unlock_irq(&cm_id_priv->lock);
2041 cm_process_work(cm_id_priv, work);
2043 cm_deref_id(cm_id_priv);
2046 cm_deref_id(cm_id_priv);
2050 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2051 enum ib_cm_rej_reason reason,
2054 const void *private_data,
2055 u8 private_data_len)
2057 struct cm_id_private *cm_id_priv;
2058 struct ib_mad_send_buf *msg;
2059 unsigned long flags;
2062 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2063 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2066 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2068 spin_lock_irqsave(&cm_id_priv->lock, flags);
2069 switch (cm_id->state) {
2070 case IB_CM_REQ_SENT:
2071 case IB_CM_MRA_REQ_RCVD:
2072 case IB_CM_REQ_RCVD:
2073 case IB_CM_MRA_REQ_SENT:
2074 case IB_CM_REP_RCVD:
2075 case IB_CM_MRA_REP_SENT:
2076 ret = cm_alloc_msg(cm_id_priv, &msg);
2078 cm_format_rej((struct cm_rej_msg *) msg->mad,
2079 cm_id_priv, reason, ari, ari_length,
2080 private_data, private_data_len);
2082 cm_reset_to_idle(cm_id_priv);
2084 case IB_CM_REP_SENT:
2085 case IB_CM_MRA_REP_RCVD:
2086 ret = cm_alloc_msg(cm_id_priv, &msg);
2088 cm_format_rej((struct cm_rej_msg *) msg->mad,
2089 cm_id_priv, reason, ari, ari_length,
2090 private_data, private_data_len);
2092 cm_enter_timewait(cm_id_priv);
2102 ret = ib_post_send_mad(msg, NULL);
2106 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2109 EXPORT_SYMBOL(ib_send_cm_rej);
2111 static void cm_format_rej_event(struct cm_work *work)
2113 struct cm_rej_msg *rej_msg;
2114 struct ib_cm_rej_event_param *param;
2116 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2117 param = &work->cm_event.param.rej_rcvd;
2118 param->ari = rej_msg->ari;
2119 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2120 param->reason = __be16_to_cpu(rej_msg->reason);
2121 work->cm_event.private_data = &rej_msg->private_data;
2124 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2126 struct cm_timewait_info *timewait_info;
2127 struct cm_id_private *cm_id_priv;
2130 remote_id = rej_msg->local_comm_id;
2132 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2133 spin_lock_irq(&cm.lock);
2134 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2136 if (!timewait_info) {
2137 spin_unlock_irq(&cm.lock);
2140 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2141 (timewait_info->work.local_id ^
2142 cm.random_id_operand));
2144 if (cm_id_priv->id.remote_id == remote_id)
2145 atomic_inc(&cm_id_priv->refcount);
2149 spin_unlock_irq(&cm.lock);
2150 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2151 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2153 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2158 static int cm_rej_handler(struct cm_work *work)
2160 struct cm_id_private *cm_id_priv;
2161 struct cm_rej_msg *rej_msg;
2164 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2165 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2169 cm_format_rej_event(work);
2171 spin_lock_irq(&cm_id_priv->lock);
2172 switch (cm_id_priv->id.state) {
2173 case IB_CM_REQ_SENT:
2174 case IB_CM_MRA_REQ_RCVD:
2175 case IB_CM_REP_SENT:
2176 case IB_CM_MRA_REP_RCVD:
2177 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2179 case IB_CM_REQ_RCVD:
2180 case IB_CM_MRA_REQ_SENT:
2181 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2182 cm_enter_timewait(cm_id_priv);
2184 cm_reset_to_idle(cm_id_priv);
2186 case IB_CM_DREQ_SENT:
2187 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2189 case IB_CM_REP_RCVD:
2190 case IB_CM_MRA_REP_SENT:
2191 case IB_CM_ESTABLISHED:
2192 cm_enter_timewait(cm_id_priv);
2195 spin_unlock_irq(&cm_id_priv->lock);
2200 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2202 list_add_tail(&work->list, &cm_id_priv->work_list);
2203 spin_unlock_irq(&cm_id_priv->lock);
2206 cm_process_work(cm_id_priv, work);
2208 cm_deref_id(cm_id_priv);
2211 cm_deref_id(cm_id_priv);
2215 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2217 const void *private_data,
2218 u8 private_data_len)
2220 struct cm_id_private *cm_id_priv;
2221 struct ib_mad_send_buf *msg;
2223 unsigned long flags;
2226 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2229 data = cm_copy_private_data(private_data, private_data_len);
2231 return PTR_ERR(data);
2233 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2235 spin_lock_irqsave(&cm_id_priv->lock, flags);
2236 switch(cm_id_priv->id.state) {
2237 case IB_CM_REQ_RCVD:
2238 ret = cm_alloc_msg(cm_id_priv, &msg);
2242 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2243 CM_MSG_RESPONSE_REQ, service_timeout,
2244 private_data, private_data_len);
2245 ret = ib_post_send_mad(msg, NULL);
2248 cm_id->state = IB_CM_MRA_REQ_SENT;
2250 case IB_CM_REP_RCVD:
2251 ret = cm_alloc_msg(cm_id_priv, &msg);
2255 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2256 CM_MSG_RESPONSE_REP, service_timeout,
2257 private_data, private_data_len);
2258 ret = ib_post_send_mad(msg, NULL);
2261 cm_id->state = IB_CM_MRA_REP_SENT;
2263 case IB_CM_ESTABLISHED:
2264 ret = cm_alloc_msg(cm_id_priv, &msg);
2268 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2269 CM_MSG_RESPONSE_OTHER, service_timeout,
2270 private_data, private_data_len);
2271 ret = ib_post_send_mad(msg, NULL);
2274 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2280 cm_id_priv->service_timeout = service_timeout;
2281 cm_set_private_data(cm_id_priv, data, private_data_len);
2282 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2285 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2289 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2294 EXPORT_SYMBOL(ib_send_cm_mra);
2296 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2298 switch (cm_mra_get_msg_mraed(mra_msg)) {
2299 case CM_MSG_RESPONSE_REQ:
2300 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2301 case CM_MSG_RESPONSE_REP:
2302 case CM_MSG_RESPONSE_OTHER:
2303 return cm_acquire_id(mra_msg->remote_comm_id,
2304 mra_msg->local_comm_id);
2310 static int cm_mra_handler(struct cm_work *work)
2312 struct cm_id_private *cm_id_priv;
2313 struct cm_mra_msg *mra_msg;
2316 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2317 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2321 work->cm_event.private_data = &mra_msg->private_data;
2322 work->cm_event.param.mra_rcvd.service_timeout =
2323 cm_mra_get_service_timeout(mra_msg);
2324 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2325 cm_convert_to_ms(cm_id_priv->av.timeout);
2327 spin_lock_irq(&cm_id_priv->lock);
2328 switch (cm_id_priv->id.state) {
2329 case IB_CM_REQ_SENT:
2330 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2331 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2332 cm_id_priv->msg, timeout))
2334 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2336 case IB_CM_REP_SENT:
2337 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2338 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2339 cm_id_priv->msg, timeout))
2341 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2343 case IB_CM_ESTABLISHED:
2344 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2345 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2346 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2347 cm_id_priv->msg, timeout))
2349 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2355 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2356 cm_id_priv->id.state;
2357 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2359 list_add_tail(&work->list, &cm_id_priv->work_list);
2360 spin_unlock_irq(&cm_id_priv->lock);
2363 cm_process_work(cm_id_priv, work);
2365 cm_deref_id(cm_id_priv);
2368 spin_unlock_irq(&cm_id_priv->lock);
2369 cm_deref_id(cm_id_priv);
2373 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2374 struct cm_id_private *cm_id_priv,
2375 struct ib_sa_path_rec *alternate_path,
2376 const void *private_data,
2377 u8 private_data_len)
2379 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2380 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2381 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2382 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2383 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2384 /* todo: need remote CM response timeout */
2385 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2386 lap_msg->alt_local_lid = alternate_path->slid;
2387 lap_msg->alt_remote_lid = alternate_path->dlid;
2388 lap_msg->alt_local_gid = alternate_path->sgid;
2389 lap_msg->alt_remote_gid = alternate_path->dgid;
2390 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2391 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2392 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2393 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2394 cm_lap_set_sl(lap_msg, alternate_path->sl);
2395 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2396 cm_lap_set_local_ack_timeout(lap_msg,
2397 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2398 alternate_path->packet_life_time));
2400 if (private_data && private_data_len)
2401 memcpy(lap_msg->private_data, private_data, private_data_len);
2404 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2405 struct ib_sa_path_rec *alternate_path,
2406 const void *private_data,
2407 u8 private_data_len)
2409 struct cm_id_private *cm_id_priv;
2410 struct ib_mad_send_buf *msg;
2411 unsigned long flags;
2414 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2417 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2418 spin_lock_irqsave(&cm_id_priv->lock, flags);
2419 if (cm_id->state != IB_CM_ESTABLISHED ||
2420 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2421 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2426 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2429 cm_id_priv->alt_av.timeout =
2430 cm_ack_timeout(cm_id_priv->target_ack_delay,
2431 cm_id_priv->alt_av.timeout - 1);
2433 ret = cm_alloc_msg(cm_id_priv, &msg);
2437 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2438 alternate_path, private_data, private_data_len);
2439 msg->timeout_ms = cm_id_priv->timeout_ms;
2440 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2442 ret = ib_post_send_mad(msg, NULL);
2444 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2449 cm_id->lap_state = IB_CM_LAP_SENT;
2450 cm_id_priv->msg = msg;
2452 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2455 EXPORT_SYMBOL(ib_send_cm_lap);
2457 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2458 struct ib_sa_path_rec *path,
2459 struct cm_lap_msg *lap_msg)
2461 memset(path, 0, sizeof *path);
2462 path->dgid = lap_msg->alt_local_gid;
2463 path->sgid = lap_msg->alt_remote_gid;
2464 path->dlid = lap_msg->alt_local_lid;
2465 path->slid = lap_msg->alt_remote_lid;
2466 path->flow_label = cm_lap_get_flow_label(lap_msg);
2467 path->hop_limit = lap_msg->alt_hop_limit;
2468 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2469 path->reversible = 1;
2470 path->pkey = cm_id_priv->pkey;
2471 path->sl = cm_lap_get_sl(lap_msg);
2472 path->mtu_selector = IB_SA_EQ;
2473 path->mtu = cm_id_priv->path_mtu;
2474 path->rate_selector = IB_SA_EQ;
2475 path->rate = cm_lap_get_packet_rate(lap_msg);
2476 path->packet_life_time_selector = IB_SA_EQ;
2477 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2478 path->packet_life_time -= (path->packet_life_time > 0);
2481 static int cm_lap_handler(struct cm_work *work)
2483 struct cm_id_private *cm_id_priv;
2484 struct cm_lap_msg *lap_msg;
2485 struct ib_cm_lap_event_param *param;
2486 struct ib_mad_send_buf *msg = NULL;
2489 /* todo: verify LAP request and send reject APR if invalid. */
2490 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2491 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2492 lap_msg->local_comm_id);
2496 param = &work->cm_event.param.lap_rcvd;
2497 param->alternate_path = &work->path[0];
2498 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2499 work->cm_event.private_data = &lap_msg->private_data;
2501 spin_lock_irq(&cm_id_priv->lock);
2502 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2505 switch (cm_id_priv->id.lap_state) {
2506 case IB_CM_LAP_UNINIT:
2507 case IB_CM_LAP_IDLE:
2509 case IB_CM_MRA_LAP_SENT:
2510 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2513 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2514 CM_MSG_RESPONSE_OTHER,
2515 cm_id_priv->service_timeout,
2516 cm_id_priv->private_data,
2517 cm_id_priv->private_data_len);
2518 spin_unlock_irq(&cm_id_priv->lock);
2520 if (ib_post_send_mad(msg, NULL))
2527 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2528 cm_id_priv->tid = lap_msg->hdr.tid;
2529 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2530 work->mad_recv_wc->recv_buf.grh,
2532 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2533 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2535 list_add_tail(&work->list, &cm_id_priv->work_list);
2536 spin_unlock_irq(&cm_id_priv->lock);
2539 cm_process_work(cm_id_priv, work);
2541 cm_deref_id(cm_id_priv);
2544 unlock: spin_unlock_irq(&cm_id_priv->lock);
2545 deref: cm_deref_id(cm_id_priv);
2549 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2550 struct cm_id_private *cm_id_priv,
2551 enum ib_cm_apr_status status,
2554 const void *private_data,
2555 u8 private_data_len)
2557 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2558 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2559 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2560 apr_msg->ap_status = (u8) status;
2562 if (info && info_length) {
2563 apr_msg->info_length = info_length;
2564 memcpy(apr_msg->info, info, info_length);
2567 if (private_data && private_data_len)
2568 memcpy(apr_msg->private_data, private_data, private_data_len);
2571 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2572 enum ib_cm_apr_status status,
2575 const void *private_data,
2576 u8 private_data_len)
2578 struct cm_id_private *cm_id_priv;
2579 struct ib_mad_send_buf *msg;
2580 unsigned long flags;
2583 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2584 (info && info_length > IB_CM_APR_INFO_LENGTH))
2587 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2588 spin_lock_irqsave(&cm_id_priv->lock, flags);
2589 if (cm_id->state != IB_CM_ESTABLISHED ||
2590 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2591 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2596 ret = cm_alloc_msg(cm_id_priv, &msg);
2600 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2601 info, info_length, private_data, private_data_len);
2602 ret = ib_post_send_mad(msg, NULL);
2604 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2609 cm_id->lap_state = IB_CM_LAP_IDLE;
2610 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2613 EXPORT_SYMBOL(ib_send_cm_apr);
2615 static int cm_apr_handler(struct cm_work *work)
2617 struct cm_id_private *cm_id_priv;
2618 struct cm_apr_msg *apr_msg;
2621 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2622 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2623 apr_msg->local_comm_id);
2625 return -EINVAL; /* Unmatched reply. */
2627 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2628 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2629 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2630 work->cm_event.private_data = &apr_msg->private_data;
2632 spin_lock_irq(&cm_id_priv->lock);
2633 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2634 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2635 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2636 spin_unlock_irq(&cm_id_priv->lock);
2639 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2640 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2641 cm_id_priv->msg = NULL;
2643 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2645 list_add_tail(&work->list, &cm_id_priv->work_list);
2646 spin_unlock_irq(&cm_id_priv->lock);
2649 cm_process_work(cm_id_priv, work);
2651 cm_deref_id(cm_id_priv);
2654 cm_deref_id(cm_id_priv);
2658 static int cm_timewait_handler(struct cm_work *work)
2660 struct cm_timewait_info *timewait_info;
2661 struct cm_id_private *cm_id_priv;
2664 timewait_info = (struct cm_timewait_info *)work;
2665 spin_lock_irq(&cm.lock);
2666 list_del(&timewait_info->list);
2667 spin_unlock_irq(&cm.lock);
2669 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2670 timewait_info->work.remote_id);
2674 spin_lock_irq(&cm_id_priv->lock);
2675 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2676 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2677 spin_unlock_irq(&cm_id_priv->lock);
2680 cm_id_priv->id.state = IB_CM_IDLE;
2681 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2683 list_add_tail(&work->list, &cm_id_priv->work_list);
2684 spin_unlock_irq(&cm_id_priv->lock);
2687 cm_process_work(cm_id_priv, work);
2689 cm_deref_id(cm_id_priv);
2692 cm_deref_id(cm_id_priv);
2696 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2697 struct cm_id_private *cm_id_priv,
2698 struct ib_cm_sidr_req_param *param)
2700 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2701 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2702 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2703 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2704 sidr_req_msg->service_id = param->service_id;
2706 if (param->private_data && param->private_data_len)
2707 memcpy(sidr_req_msg->private_data, param->private_data,
2708 param->private_data_len);
2711 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2712 struct ib_cm_sidr_req_param *param)
2714 struct cm_id_private *cm_id_priv;
2715 struct ib_mad_send_buf *msg;
2716 unsigned long flags;
2719 if (!param->path || (param->private_data &&
2720 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2723 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2724 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2728 cm_id->service_id = param->service_id;
2729 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2730 cm_id_priv->timeout_ms = param->timeout_ms;
2731 cm_id_priv->max_cm_retries = param->max_cm_retries;
2732 ret = cm_alloc_msg(cm_id_priv, &msg);
2736 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2738 msg->timeout_ms = cm_id_priv->timeout_ms;
2739 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2741 spin_lock_irqsave(&cm_id_priv->lock, flags);
2742 if (cm_id->state == IB_CM_IDLE)
2743 ret = ib_post_send_mad(msg, NULL);
2748 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2752 cm_id->state = IB_CM_SIDR_REQ_SENT;
2753 cm_id_priv->msg = msg;
2754 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2758 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2760 static void cm_format_sidr_req_event(struct cm_work *work,
2761 struct ib_cm_id *listen_id)
2763 struct cm_sidr_req_msg *sidr_req_msg;
2764 struct ib_cm_sidr_req_event_param *param;
2766 sidr_req_msg = (struct cm_sidr_req_msg *)
2767 work->mad_recv_wc->recv_buf.mad;
2768 param = &work->cm_event.param.sidr_req_rcvd;
2769 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2770 param->listen_id = listen_id;
2771 param->port = work->port->port_num;
2772 work->cm_event.private_data = &sidr_req_msg->private_data;
2775 static int cm_sidr_req_handler(struct cm_work *work)
2777 struct ib_cm_id *cm_id;
2778 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2779 struct cm_sidr_req_msg *sidr_req_msg;
2782 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2784 return PTR_ERR(cm_id);
2785 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2787 /* Record SGID/SLID and request ID for lookup. */
2788 sidr_req_msg = (struct cm_sidr_req_msg *)
2789 work->mad_recv_wc->recv_buf.mad;
2790 wc = work->mad_recv_wc->wc;
2791 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2792 cm_id_priv->av.dgid.global.interface_id = 0;
2793 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2794 work->mad_recv_wc->recv_buf.grh,
2796 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2797 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2798 atomic_inc(&cm_id_priv->work_count);
2800 spin_lock_irq(&cm.lock);
2801 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2802 if (cur_cm_id_priv) {
2803 spin_unlock_irq(&cm.lock);
2804 goto out; /* Duplicate message. */
2806 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2807 cur_cm_id_priv = cm_find_listen(cm_id->device,
2808 sidr_req_msg->service_id,
2809 sidr_req_msg->private_data);
2810 if (!cur_cm_id_priv) {
2811 spin_unlock_irq(&cm.lock);
2812 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
2813 goto out; /* No match. */
2815 atomic_inc(&cur_cm_id_priv->refcount);
2816 spin_unlock_irq(&cm.lock);
2818 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2819 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2820 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2821 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2823 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2824 cm_process_work(cm_id_priv, work);
2825 cm_deref_id(cur_cm_id_priv);
2828 ib_destroy_cm_id(&cm_id_priv->id);
2832 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2833 struct cm_id_private *cm_id_priv,
2834 struct ib_cm_sidr_rep_param *param)
2836 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2838 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2839 sidr_rep_msg->status = param->status;
2840 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2841 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2842 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2844 if (param->info && param->info_length)
2845 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2847 if (param->private_data && param->private_data_len)
2848 memcpy(sidr_rep_msg->private_data, param->private_data,
2849 param->private_data_len);
2852 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2853 struct ib_cm_sidr_rep_param *param)
2855 struct cm_id_private *cm_id_priv;
2856 struct ib_mad_send_buf *msg;
2857 unsigned long flags;
2860 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2861 (param->private_data &&
2862 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2865 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2866 spin_lock_irqsave(&cm_id_priv->lock, flags);
2867 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2872 ret = cm_alloc_msg(cm_id_priv, &msg);
2876 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2878 ret = ib_post_send_mad(msg, NULL);
2880 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2884 cm_id->state = IB_CM_IDLE;
2885 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2887 spin_lock_irqsave(&cm.lock, flags);
2888 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2889 spin_unlock_irqrestore(&cm.lock, flags);
2892 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2895 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2897 static void cm_format_sidr_rep_event(struct cm_work *work)
2899 struct cm_sidr_rep_msg *sidr_rep_msg;
2900 struct ib_cm_sidr_rep_event_param *param;
2902 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2903 work->mad_recv_wc->recv_buf.mad;
2904 param = &work->cm_event.param.sidr_rep_rcvd;
2905 param->status = sidr_rep_msg->status;
2906 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2907 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2908 param->info = &sidr_rep_msg->info;
2909 param->info_len = sidr_rep_msg->info_length;
2910 work->cm_event.private_data = &sidr_rep_msg->private_data;
2913 static int cm_sidr_rep_handler(struct cm_work *work)
2915 struct cm_sidr_rep_msg *sidr_rep_msg;
2916 struct cm_id_private *cm_id_priv;
2918 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2919 work->mad_recv_wc->recv_buf.mad;
2920 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2922 return -EINVAL; /* Unmatched reply. */
2924 spin_lock_irq(&cm_id_priv->lock);
2925 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2926 spin_unlock_irq(&cm_id_priv->lock);
2929 cm_id_priv->id.state = IB_CM_IDLE;
2930 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2931 spin_unlock_irq(&cm_id_priv->lock);
2933 cm_format_sidr_rep_event(work);
2934 cm_process_work(cm_id_priv, work);
2937 cm_deref_id(cm_id_priv);
2941 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2942 enum ib_wc_status wc_status)
2944 struct cm_id_private *cm_id_priv;
2945 struct ib_cm_event cm_event;
2946 enum ib_cm_state state;
2949 memset(&cm_event, 0, sizeof cm_event);
2950 cm_id_priv = msg->context[0];
2952 /* Discard old sends or ones without a response. */
2953 spin_lock_irq(&cm_id_priv->lock);
2954 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2955 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2959 case IB_CM_REQ_SENT:
2960 case IB_CM_MRA_REQ_RCVD:
2961 cm_reset_to_idle(cm_id_priv);
2962 cm_event.event = IB_CM_REQ_ERROR;
2964 case IB_CM_REP_SENT:
2965 case IB_CM_MRA_REP_RCVD:
2966 cm_reset_to_idle(cm_id_priv);
2967 cm_event.event = IB_CM_REP_ERROR;
2969 case IB_CM_DREQ_SENT:
2970 cm_enter_timewait(cm_id_priv);
2971 cm_event.event = IB_CM_DREQ_ERROR;
2973 case IB_CM_SIDR_REQ_SENT:
2974 cm_id_priv->id.state = IB_CM_IDLE;
2975 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2980 spin_unlock_irq(&cm_id_priv->lock);
2981 cm_event.param.send_status = wc_status;
2983 /* No other events can occur on the cm_id at this point. */
2984 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2987 ib_destroy_cm_id(&cm_id_priv->id);
2990 spin_unlock_irq(&cm_id_priv->lock);
2994 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2995 struct ib_mad_send_wc *mad_send_wc)
2997 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2999 switch (mad_send_wc->status) {
3001 case IB_WC_WR_FLUSH_ERR:
3005 if (msg->context[0] && msg->context[1])
3006 cm_process_send_error(msg, mad_send_wc->status);
3013 static void cm_work_handler(struct work_struct *_work)
3015 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3018 switch (work->cm_event.event) {
3019 case IB_CM_REQ_RECEIVED:
3020 ret = cm_req_handler(work);
3022 case IB_CM_MRA_RECEIVED:
3023 ret = cm_mra_handler(work);
3025 case IB_CM_REJ_RECEIVED:
3026 ret = cm_rej_handler(work);
3028 case IB_CM_REP_RECEIVED:
3029 ret = cm_rep_handler(work);
3031 case IB_CM_RTU_RECEIVED:
3032 ret = cm_rtu_handler(work);
3034 case IB_CM_USER_ESTABLISHED:
3035 ret = cm_establish_handler(work);
3037 case IB_CM_DREQ_RECEIVED:
3038 ret = cm_dreq_handler(work);
3040 case IB_CM_DREP_RECEIVED:
3041 ret = cm_drep_handler(work);
3043 case IB_CM_SIDR_REQ_RECEIVED:
3044 ret = cm_sidr_req_handler(work);
3046 case IB_CM_SIDR_REP_RECEIVED:
3047 ret = cm_sidr_rep_handler(work);
3049 case IB_CM_LAP_RECEIVED:
3050 ret = cm_lap_handler(work);
3052 case IB_CM_APR_RECEIVED:
3053 ret = cm_apr_handler(work);
3055 case IB_CM_TIMEWAIT_EXIT:
3056 ret = cm_timewait_handler(work);
3066 static int cm_establish(struct ib_cm_id *cm_id)
3068 struct cm_id_private *cm_id_priv;
3069 struct cm_work *work;
3070 unsigned long flags;
3073 work = kmalloc(sizeof *work, GFP_ATOMIC);
3077 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3078 spin_lock_irqsave(&cm_id_priv->lock, flags);
3079 switch (cm_id->state)
3081 case IB_CM_REP_SENT:
3082 case IB_CM_MRA_REP_RCVD:
3083 cm_id->state = IB_CM_ESTABLISHED;
3085 case IB_CM_ESTABLISHED:
3092 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3100 * The CM worker thread may try to destroy the cm_id before it
3101 * can execute this work item. To prevent potential deadlock,
3102 * we need to find the cm_id once we're in the context of the
3103 * worker thread, rather than holding a reference on it.
3105 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3106 work->local_id = cm_id->local_id;
3107 work->remote_id = cm_id->remote_id;
3108 work->mad_recv_wc = NULL;
3109 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3110 queue_delayed_work(cm.wq, &work->work, 0);
3115 static int cm_migrate(struct ib_cm_id *cm_id)
3117 struct cm_id_private *cm_id_priv;
3118 unsigned long flags;
3121 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3122 spin_lock_irqsave(&cm_id_priv->lock, flags);
3123 if (cm_id->state == IB_CM_ESTABLISHED &&
3124 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3125 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3126 cm_id->lap_state = IB_CM_LAP_IDLE;
3127 cm_id_priv->av = cm_id_priv->alt_av;
3130 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3135 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3140 case IB_EVENT_COMM_EST:
3141 ret = cm_establish(cm_id);
3143 case IB_EVENT_PATH_MIG:
3144 ret = cm_migrate(cm_id);
3151 EXPORT_SYMBOL(ib_cm_notify);
3153 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3154 struct ib_mad_recv_wc *mad_recv_wc)
3156 struct cm_work *work;
3157 enum ib_cm_event_type event;
3160 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3161 case CM_REQ_ATTR_ID:
3162 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3163 alt_local_lid != 0);
3164 event = IB_CM_REQ_RECEIVED;
3166 case CM_MRA_ATTR_ID:
3167 event = IB_CM_MRA_RECEIVED;
3169 case CM_REJ_ATTR_ID:
3170 event = IB_CM_REJ_RECEIVED;
3172 case CM_REP_ATTR_ID:
3173 event = IB_CM_REP_RECEIVED;
3175 case CM_RTU_ATTR_ID:
3176 event = IB_CM_RTU_RECEIVED;
3178 case CM_DREQ_ATTR_ID:
3179 event = IB_CM_DREQ_RECEIVED;
3181 case CM_DREP_ATTR_ID:
3182 event = IB_CM_DREP_RECEIVED;
3184 case CM_SIDR_REQ_ATTR_ID:
3185 event = IB_CM_SIDR_REQ_RECEIVED;
3187 case CM_SIDR_REP_ATTR_ID:
3188 event = IB_CM_SIDR_REP_RECEIVED;
3190 case CM_LAP_ATTR_ID:
3192 event = IB_CM_LAP_RECEIVED;
3194 case CM_APR_ATTR_ID:
3195 event = IB_CM_APR_RECEIVED;
3198 ib_free_recv_mad(mad_recv_wc);
3202 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3205 ib_free_recv_mad(mad_recv_wc);
3209 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3210 work->cm_event.event = event;
3211 work->mad_recv_wc = mad_recv_wc;
3212 work->port = (struct cm_port *)mad_agent->context;
3213 queue_delayed_work(cm.wq, &work->work, 0);
3216 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3217 struct ib_qp_attr *qp_attr,
3220 unsigned long flags;
3223 spin_lock_irqsave(&cm_id_priv->lock, flags);
3224 switch (cm_id_priv->id.state) {
3225 case IB_CM_REQ_SENT:
3226 case IB_CM_MRA_REQ_RCVD:
3227 case IB_CM_REQ_RCVD:
3228 case IB_CM_MRA_REQ_SENT:
3229 case IB_CM_REP_RCVD:
3230 case IB_CM_MRA_REP_SENT:
3231 case IB_CM_REP_SENT:
3232 case IB_CM_MRA_REP_RCVD:
3233 case IB_CM_ESTABLISHED:
3234 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3235 IB_QP_PKEY_INDEX | IB_QP_PORT;
3236 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3237 if (cm_id_priv->responder_resources)
3238 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3239 IB_ACCESS_REMOTE_ATOMIC;
3240 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3241 qp_attr->port_num = cm_id_priv->av.port->port_num;
3248 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3252 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3253 struct ib_qp_attr *qp_attr,
3256 unsigned long flags;
3259 spin_lock_irqsave(&cm_id_priv->lock, flags);
3260 switch (cm_id_priv->id.state) {
3261 case IB_CM_REQ_RCVD:
3262 case IB_CM_MRA_REQ_SENT:
3263 case IB_CM_REP_RCVD:
3264 case IB_CM_MRA_REP_SENT:
3265 case IB_CM_REP_SENT:
3266 case IB_CM_MRA_REP_RCVD:
3267 case IB_CM_ESTABLISHED:
3268 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3269 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3270 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3271 qp_attr->path_mtu = cm_id_priv->path_mtu;
3272 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3273 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3274 if (cm_id_priv->qp_type == IB_QPT_RC) {
3275 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3276 IB_QP_MIN_RNR_TIMER;
3277 qp_attr->max_dest_rd_atomic =
3278 cm_id_priv->responder_resources;
3279 qp_attr->min_rnr_timer = 0;
3281 if (cm_id_priv->alt_av.ah_attr.dlid) {
3282 *qp_attr_mask |= IB_QP_ALT_PATH;
3283 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3284 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3285 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3286 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3294 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3298 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3299 struct ib_qp_attr *qp_attr,
3302 unsigned long flags;
3305 spin_lock_irqsave(&cm_id_priv->lock, flags);
3306 switch (cm_id_priv->id.state) {
3307 /* Allow transition to RTS before sending REP */
3308 case IB_CM_REQ_RCVD:
3309 case IB_CM_MRA_REQ_SENT:
3311 case IB_CM_REP_RCVD:
3312 case IB_CM_MRA_REP_SENT:
3313 case IB_CM_REP_SENT:
3314 case IB_CM_MRA_REP_RCVD:
3315 case IB_CM_ESTABLISHED:
3316 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3317 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3318 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3319 if (cm_id_priv->qp_type == IB_QPT_RC) {
3320 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3322 IB_QP_MAX_QP_RD_ATOMIC;
3323 qp_attr->timeout = cm_id_priv->av.timeout;
3324 qp_attr->retry_cnt = cm_id_priv->retry_count;
3325 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3326 qp_attr->max_rd_atomic =
3327 cm_id_priv->initiator_depth;
3329 if (cm_id_priv->alt_av.ah_attr.dlid) {
3330 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3331 qp_attr->path_mig_state = IB_MIG_REARM;
3334 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3335 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3336 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3337 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3338 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3339 qp_attr->path_mig_state = IB_MIG_REARM;
3347 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3351 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3352 struct ib_qp_attr *qp_attr,
3355 struct cm_id_private *cm_id_priv;
3358 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3359 switch (qp_attr->qp_state) {
3361 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3364 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3367 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3375 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3377 void cm_get_ack_delay(struct cm_device *cm_dev)
3379 struct ib_device_attr attr;
3381 if (ib_query_device(cm_dev->device, &attr))
3382 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3384 cm_dev->ack_delay = attr.local_ca_ack_delay;
3387 static void cm_add_one(struct ib_device *device)
3389 struct cm_device *cm_dev;
3390 struct cm_port *port;
3391 struct ib_mad_reg_req reg_req = {
3392 .mgmt_class = IB_MGMT_CLASS_CM,
3393 .mgmt_class_version = IB_CM_CLASS_VERSION
3395 struct ib_port_modify port_modify = {
3396 .set_port_cap_mask = IB_PORT_CM_SUP
3398 unsigned long flags;
3402 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3405 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3406 device->phys_port_cnt, GFP_KERNEL);
3410 cm_dev->device = device;
3411 cm_get_ack_delay(cm_dev);
3413 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3414 for (i = 1; i <= device->phys_port_cnt; i++) {
3415 port = &cm_dev->port[i-1];
3416 port->cm_dev = cm_dev;
3418 port->mad_agent = ib_register_mad_agent(device, i,
3425 if (IS_ERR(port->mad_agent))
3428 ret = ib_modify_port(device, i, 0, &port_modify);
3432 ib_set_client_data(device, &cm_client, cm_dev);
3434 write_lock_irqsave(&cm.device_lock, flags);
3435 list_add_tail(&cm_dev->list, &cm.device_list);
3436 write_unlock_irqrestore(&cm.device_lock, flags);
3440 ib_unregister_mad_agent(port->mad_agent);
3442 port_modify.set_port_cap_mask = 0;
3443 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3445 port = &cm_dev->port[i-1];
3446 ib_modify_port(device, port->port_num, 0, &port_modify);
3447 ib_unregister_mad_agent(port->mad_agent);
3452 static void cm_remove_one(struct ib_device *device)
3454 struct cm_device *cm_dev;
3455 struct cm_port *port;
3456 struct ib_port_modify port_modify = {
3457 .clr_port_cap_mask = IB_PORT_CM_SUP
3459 unsigned long flags;
3462 cm_dev = ib_get_client_data(device, &cm_client);
3466 write_lock_irqsave(&cm.device_lock, flags);
3467 list_del(&cm_dev->list);
3468 write_unlock_irqrestore(&cm.device_lock, flags);
3470 for (i = 1; i <= device->phys_port_cnt; i++) {
3471 port = &cm_dev->port[i-1];
3472 ib_modify_port(device, port->port_num, 0, &port_modify);
3473 ib_unregister_mad_agent(port->mad_agent);
3478 static int __init ib_cm_init(void)
3482 memset(&cm, 0, sizeof cm);
3483 INIT_LIST_HEAD(&cm.device_list);
3484 rwlock_init(&cm.device_lock);
3485 spin_lock_init(&cm.lock);
3486 cm.listen_service_table = RB_ROOT;
3487 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3488 cm.remote_id_table = RB_ROOT;
3489 cm.remote_qp_table = RB_ROOT;
3490 cm.remote_sidr_table = RB_ROOT;
3491 idr_init(&cm.local_id_table);
3492 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3493 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3494 INIT_LIST_HEAD(&cm.timewait_list);
3496 cm.wq = create_workqueue("ib_cm");
3500 ret = ib_register_client(&cm_client);
3506 destroy_workqueue(cm.wq);
3510 static void __exit ib_cm_cleanup(void)
3512 struct cm_timewait_info *timewait_info, *tmp;
3514 spin_lock_irq(&cm.lock);
3515 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3516 cancel_delayed_work(&timewait_info->work.work);
3517 spin_unlock_irq(&cm.lock);
3519 destroy_workqueue(cm.wq);
3521 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3522 list_del(&timewait_info->list);
3523 kfree(timewait_info);
3526 ib_unregister_client(&cm_client);
3527 idr_destroy(&cm.local_id_table);
3530 module_init(ib_cm_init);
3531 module_exit(ib_cm_cleanup);