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;
90 struct cm_port port[0];
96 struct ib_ah_attr ah_attr;
102 struct delayed_work work;
103 struct list_head list;
104 struct cm_port *port;
105 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
106 __be32 local_id; /* Established / timewait */
108 struct ib_cm_event cm_event;
109 struct ib_sa_path_rec path[0];
112 struct cm_timewait_info {
113 struct cm_work work; /* Must be first. */
114 struct list_head list;
115 struct rb_node remote_qp_node;
116 struct rb_node remote_id_node;
117 __be64 remote_ca_guid;
119 u8 inserted_remote_qp;
120 u8 inserted_remote_id;
123 struct cm_id_private {
126 struct rb_node service_node;
127 struct rb_node sidr_id_node;
128 spinlock_t lock; /* Do not acquire inside cm.lock */
129 struct completion comp;
132 struct ib_mad_send_buf *msg;
133 struct cm_timewait_info *timewait_info;
134 /* todo: use alternate port on send failure */
137 struct ib_cm_compare_data *compare_data;
143 enum ib_qp_type qp_type;
147 enum ib_mtu path_mtu;
152 u8 responder_resources;
158 struct list_head work_list;
162 static void cm_work_handler(struct work_struct *work);
164 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
166 if (atomic_dec_and_test(&cm_id_priv->refcount))
167 complete(&cm_id_priv->comp);
170 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
171 struct ib_mad_send_buf **msg)
173 struct ib_mad_agent *mad_agent;
174 struct ib_mad_send_buf *m;
177 mad_agent = cm_id_priv->av.port->mad_agent;
178 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
182 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
183 cm_id_priv->av.pkey_index,
184 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
191 /* Timeout set by caller if response is expected. */
193 m->retries = cm_id_priv->max_cm_retries;
195 atomic_inc(&cm_id_priv->refcount);
196 m->context[0] = cm_id_priv;
201 static int cm_alloc_response_msg(struct cm_port *port,
202 struct ib_mad_recv_wc *mad_recv_wc,
203 struct ib_mad_send_buf **msg)
205 struct ib_mad_send_buf *m;
208 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
209 mad_recv_wc->recv_buf.grh, port->port_num);
213 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
214 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
225 static void cm_free_msg(struct ib_mad_send_buf *msg)
227 ib_destroy_ah(msg->ah);
229 cm_deref_id(msg->context[0]);
230 ib_free_send_mad(msg);
233 static void * cm_copy_private_data(const void *private_data,
238 if (!private_data || !private_data_len)
241 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
243 return ERR_PTR(-ENOMEM);
248 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
249 void *private_data, u8 private_data_len)
251 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
252 kfree(cm_id_priv->private_data);
254 cm_id_priv->private_data = private_data;
255 cm_id_priv->private_data_len = private_data_len;
258 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
259 struct ib_grh *grh, struct cm_av *av)
262 av->pkey_index = wc->pkey_index;
263 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
267 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
269 struct cm_device *cm_dev;
270 struct cm_port *port = NULL;
275 read_lock_irqsave(&cm.device_lock, flags);
276 list_for_each_entry(cm_dev, &cm.device_list, list) {
277 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
279 port = &cm_dev->port[p-1];
283 read_unlock_irqrestore(&cm.device_lock, flags);
288 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
289 be16_to_cpu(path->pkey), &av->pkey_index);
294 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
296 av->packet_life_time = path->packet_life_time;
300 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
307 spin_lock_irqsave(&cm.lock, flags);
308 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
311 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
312 spin_unlock_irqrestore(&cm.lock, flags);
313 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
315 cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand);
319 static void cm_free_id(__be32 local_id)
321 spin_lock_irq(&cm.lock);
322 idr_remove(&cm.local_id_table,
323 (__force int) (local_id ^ cm.random_id_operand));
324 spin_unlock_irq(&cm.lock);
327 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
329 struct cm_id_private *cm_id_priv;
331 cm_id_priv = idr_find(&cm.local_id_table,
332 (__force int) (local_id ^ cm.random_id_operand));
334 if (cm_id_priv->id.remote_id == remote_id)
335 atomic_inc(&cm_id_priv->refcount);
343 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
345 struct cm_id_private *cm_id_priv;
347 spin_lock_irq(&cm.lock);
348 cm_id_priv = cm_get_id(local_id, remote_id);
349 spin_unlock_irq(&cm.lock);
354 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
358 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
359 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
360 ((unsigned long *) mask)[i];
363 static int cm_compare_data(struct ib_cm_compare_data *src_data,
364 struct ib_cm_compare_data *dst_data)
366 u8 src[IB_CM_COMPARE_SIZE];
367 u8 dst[IB_CM_COMPARE_SIZE];
369 if (!src_data || !dst_data)
372 cm_mask_copy(src, src_data->data, dst_data->mask);
373 cm_mask_copy(dst, dst_data->data, src_data->mask);
374 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
377 static int cm_compare_private_data(u8 *private_data,
378 struct ib_cm_compare_data *dst_data)
380 u8 src[IB_CM_COMPARE_SIZE];
385 cm_mask_copy(src, private_data, dst_data->mask);
386 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
389 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
391 struct rb_node **link = &cm.listen_service_table.rb_node;
392 struct rb_node *parent = NULL;
393 struct cm_id_private *cur_cm_id_priv;
394 __be64 service_id = cm_id_priv->id.service_id;
395 __be64 service_mask = cm_id_priv->id.service_mask;
400 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
402 data_cmp = cm_compare_data(cm_id_priv->compare_data,
403 cur_cm_id_priv->compare_data);
404 if ((cur_cm_id_priv->id.service_mask & service_id) ==
405 (service_mask & cur_cm_id_priv->id.service_id) &&
406 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
408 return cur_cm_id_priv;
410 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
411 link = &(*link)->rb_left;
412 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
413 link = &(*link)->rb_right;
414 else if (service_id < cur_cm_id_priv->id.service_id)
415 link = &(*link)->rb_left;
416 else if (service_id > cur_cm_id_priv->id.service_id)
417 link = &(*link)->rb_right;
418 else if (data_cmp < 0)
419 link = &(*link)->rb_left;
421 link = &(*link)->rb_right;
423 rb_link_node(&cm_id_priv->service_node, parent, link);
424 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
428 static struct cm_id_private * cm_find_listen(struct ib_device *device,
432 struct rb_node *node = cm.listen_service_table.rb_node;
433 struct cm_id_private *cm_id_priv;
437 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
438 data_cmp = cm_compare_private_data(private_data,
439 cm_id_priv->compare_data);
440 if ((cm_id_priv->id.service_mask & service_id) ==
441 cm_id_priv->id.service_id &&
442 (cm_id_priv->id.device == device) && !data_cmp)
445 if (device < cm_id_priv->id.device)
446 node = node->rb_left;
447 else if (device > cm_id_priv->id.device)
448 node = node->rb_right;
449 else if (service_id < cm_id_priv->id.service_id)
450 node = node->rb_left;
451 else if (service_id > cm_id_priv->id.service_id)
452 node = node->rb_right;
453 else if (data_cmp < 0)
454 node = node->rb_left;
456 node = node->rb_right;
461 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
464 struct rb_node **link = &cm.remote_id_table.rb_node;
465 struct rb_node *parent = NULL;
466 struct cm_timewait_info *cur_timewait_info;
467 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
468 __be32 remote_id = timewait_info->work.remote_id;
472 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
474 if (remote_id < cur_timewait_info->work.remote_id)
475 link = &(*link)->rb_left;
476 else if (remote_id > cur_timewait_info->work.remote_id)
477 link = &(*link)->rb_right;
478 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
479 link = &(*link)->rb_left;
480 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
481 link = &(*link)->rb_right;
483 return cur_timewait_info;
485 timewait_info->inserted_remote_id = 1;
486 rb_link_node(&timewait_info->remote_id_node, parent, link);
487 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
491 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
494 struct rb_node *node = cm.remote_id_table.rb_node;
495 struct cm_timewait_info *timewait_info;
498 timewait_info = rb_entry(node, struct cm_timewait_info,
500 if (remote_id < timewait_info->work.remote_id)
501 node = node->rb_left;
502 else if (remote_id > timewait_info->work.remote_id)
503 node = node->rb_right;
504 else if (remote_ca_guid < timewait_info->remote_ca_guid)
505 node = node->rb_left;
506 else if (remote_ca_guid > timewait_info->remote_ca_guid)
507 node = node->rb_right;
509 return timewait_info;
514 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
517 struct rb_node **link = &cm.remote_qp_table.rb_node;
518 struct rb_node *parent = NULL;
519 struct cm_timewait_info *cur_timewait_info;
520 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
521 __be32 remote_qpn = timewait_info->remote_qpn;
525 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
527 if (remote_qpn < cur_timewait_info->remote_qpn)
528 link = &(*link)->rb_left;
529 else if (remote_qpn > cur_timewait_info->remote_qpn)
530 link = &(*link)->rb_right;
531 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
532 link = &(*link)->rb_left;
533 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
534 link = &(*link)->rb_right;
536 return cur_timewait_info;
538 timewait_info->inserted_remote_qp = 1;
539 rb_link_node(&timewait_info->remote_qp_node, parent, link);
540 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
544 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
547 struct rb_node **link = &cm.remote_sidr_table.rb_node;
548 struct rb_node *parent = NULL;
549 struct cm_id_private *cur_cm_id_priv;
550 union ib_gid *port_gid = &cm_id_priv->av.dgid;
551 __be32 remote_id = cm_id_priv->id.remote_id;
555 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
557 if (remote_id < cur_cm_id_priv->id.remote_id)
558 link = &(*link)->rb_left;
559 else if (remote_id > cur_cm_id_priv->id.remote_id)
560 link = &(*link)->rb_right;
563 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
566 link = &(*link)->rb_left;
568 link = &(*link)->rb_right;
570 return cur_cm_id_priv;
573 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
574 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
578 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
579 enum ib_cm_sidr_status status)
581 struct ib_cm_sidr_rep_param param;
583 memset(¶m, 0, sizeof param);
584 param.status = status;
585 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
588 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
589 ib_cm_handler cm_handler,
592 struct cm_id_private *cm_id_priv;
595 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
597 return ERR_PTR(-ENOMEM);
599 cm_id_priv->id.state = IB_CM_IDLE;
600 cm_id_priv->id.device = device;
601 cm_id_priv->id.cm_handler = cm_handler;
602 cm_id_priv->id.context = context;
603 cm_id_priv->id.remote_cm_qpn = 1;
604 ret = cm_alloc_id(cm_id_priv);
608 spin_lock_init(&cm_id_priv->lock);
609 init_completion(&cm_id_priv->comp);
610 INIT_LIST_HEAD(&cm_id_priv->work_list);
611 atomic_set(&cm_id_priv->work_count, -1);
612 atomic_set(&cm_id_priv->refcount, 1);
613 return &cm_id_priv->id;
617 return ERR_PTR(-ENOMEM);
619 EXPORT_SYMBOL(ib_create_cm_id);
621 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
623 struct cm_work *work;
625 if (list_empty(&cm_id_priv->work_list))
628 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
629 list_del(&work->list);
633 static void cm_free_work(struct cm_work *work)
635 if (work->mad_recv_wc)
636 ib_free_recv_mad(work->mad_recv_wc);
640 static inline int cm_convert_to_ms(int iba_time)
642 /* approximate conversion to ms from 4.096us x 2^iba_time */
643 return 1 << max(iba_time - 8, 0);
646 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
648 if (timewait_info->inserted_remote_id) {
649 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
650 timewait_info->inserted_remote_id = 0;
653 if (timewait_info->inserted_remote_qp) {
654 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
655 timewait_info->inserted_remote_qp = 0;
659 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
661 struct cm_timewait_info *timewait_info;
663 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
665 return ERR_PTR(-ENOMEM);
667 timewait_info->work.local_id = local_id;
668 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
669 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
670 return timewait_info;
673 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
678 spin_lock_irqsave(&cm.lock, flags);
679 cm_cleanup_timewait(cm_id_priv->timewait_info);
680 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
681 spin_unlock_irqrestore(&cm.lock, flags);
684 * The cm_id could be destroyed by the user before we exit timewait.
685 * To protect against this, we search for the cm_id after exiting
686 * timewait before notifying the user that we've exited timewait.
688 cm_id_priv->id.state = IB_CM_TIMEWAIT;
689 wait_time = cm_convert_to_ms(cm_id_priv->av.packet_life_time + 1);
690 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
691 msecs_to_jiffies(wait_time));
692 cm_id_priv->timewait_info = NULL;
695 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
699 cm_id_priv->id.state = IB_CM_IDLE;
700 if (cm_id_priv->timewait_info) {
701 spin_lock_irqsave(&cm.lock, flags);
702 cm_cleanup_timewait(cm_id_priv->timewait_info);
703 spin_unlock_irqrestore(&cm.lock, flags);
704 kfree(cm_id_priv->timewait_info);
705 cm_id_priv->timewait_info = NULL;
709 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
711 struct cm_id_private *cm_id_priv;
712 struct cm_work *work;
714 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
716 spin_lock_irq(&cm_id_priv->lock);
717 switch (cm_id->state) {
719 cm_id->state = IB_CM_IDLE;
720 spin_unlock_irq(&cm_id_priv->lock);
721 spin_lock_irq(&cm.lock);
722 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
723 spin_unlock_irq(&cm.lock);
725 case IB_CM_SIDR_REQ_SENT:
726 cm_id->state = IB_CM_IDLE;
727 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
728 spin_unlock_irq(&cm_id_priv->lock);
730 case IB_CM_SIDR_REQ_RCVD:
731 spin_unlock_irq(&cm_id_priv->lock);
732 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
735 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
736 spin_unlock_irq(&cm_id_priv->lock);
737 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
738 &cm_id_priv->id.device->node_guid,
739 sizeof cm_id_priv->id.device->node_guid,
743 if (err == -ENOMEM) {
744 /* Do not reject to allow future retries. */
745 cm_reset_to_idle(cm_id_priv);
746 spin_unlock_irq(&cm_id_priv->lock);
748 spin_unlock_irq(&cm_id_priv->lock);
749 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
753 case IB_CM_MRA_REQ_RCVD:
755 case IB_CM_MRA_REP_RCVD:
756 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
758 case IB_CM_MRA_REQ_SENT:
760 case IB_CM_MRA_REP_SENT:
761 spin_unlock_irq(&cm_id_priv->lock);
762 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
765 case IB_CM_ESTABLISHED:
766 spin_unlock_irq(&cm_id_priv->lock);
767 ib_send_cm_dreq(cm_id, NULL, 0);
769 case IB_CM_DREQ_SENT:
770 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
771 cm_enter_timewait(cm_id_priv);
772 spin_unlock_irq(&cm_id_priv->lock);
774 case IB_CM_DREQ_RCVD:
775 spin_unlock_irq(&cm_id_priv->lock);
776 ib_send_cm_drep(cm_id, NULL, 0);
779 spin_unlock_irq(&cm_id_priv->lock);
783 cm_free_id(cm_id->local_id);
784 cm_deref_id(cm_id_priv);
785 wait_for_completion(&cm_id_priv->comp);
786 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
788 kfree(cm_id_priv->compare_data);
789 kfree(cm_id_priv->private_data);
793 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
795 cm_destroy_id(cm_id, 0);
797 EXPORT_SYMBOL(ib_destroy_cm_id);
799 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
800 struct ib_cm_compare_data *compare_data)
802 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
806 service_mask = service_mask ? service_mask :
807 __constant_cpu_to_be64(~0ULL);
808 service_id &= service_mask;
809 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
810 (service_id != IB_CM_ASSIGN_SERVICE_ID))
813 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
814 if (cm_id->state != IB_CM_IDLE)
818 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
820 if (!cm_id_priv->compare_data)
822 cm_mask_copy(cm_id_priv->compare_data->data,
823 compare_data->data, compare_data->mask);
824 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
828 cm_id->state = IB_CM_LISTEN;
830 spin_lock_irqsave(&cm.lock, flags);
831 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
832 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
833 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
835 cm_id->service_id = service_id;
836 cm_id->service_mask = service_mask;
838 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
839 spin_unlock_irqrestore(&cm.lock, flags);
841 if (cur_cm_id_priv) {
842 cm_id->state = IB_CM_IDLE;
843 kfree(cm_id_priv->compare_data);
844 cm_id_priv->compare_data = NULL;
849 EXPORT_SYMBOL(ib_cm_listen);
851 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
852 enum cm_msg_sequence msg_seq)
856 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
857 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
859 return cpu_to_be64(hi_tid | low_tid);
862 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
863 __be16 attr_id, __be64 tid)
865 hdr->base_version = IB_MGMT_BASE_VERSION;
866 hdr->mgmt_class = IB_MGMT_CLASS_CM;
867 hdr->class_version = IB_CM_CLASS_VERSION;
868 hdr->method = IB_MGMT_METHOD_SEND;
869 hdr->attr_id = attr_id;
873 static void cm_format_req(struct cm_req_msg *req_msg,
874 struct cm_id_private *cm_id_priv,
875 struct ib_cm_req_param *param)
877 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
878 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
880 req_msg->local_comm_id = cm_id_priv->id.local_id;
881 req_msg->service_id = param->service_id;
882 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
883 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
884 cm_req_set_resp_res(req_msg, param->responder_resources);
885 cm_req_set_init_depth(req_msg, param->initiator_depth);
886 cm_req_set_remote_resp_timeout(req_msg,
887 param->remote_cm_response_timeout);
888 cm_req_set_qp_type(req_msg, param->qp_type);
889 cm_req_set_flow_ctrl(req_msg, param->flow_control);
890 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
891 cm_req_set_local_resp_timeout(req_msg,
892 param->local_cm_response_timeout);
893 cm_req_set_retry_count(req_msg, param->retry_count);
894 req_msg->pkey = param->primary_path->pkey;
895 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
896 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
897 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
898 cm_req_set_srq(req_msg, param->srq);
900 req_msg->primary_local_lid = param->primary_path->slid;
901 req_msg->primary_remote_lid = param->primary_path->dlid;
902 req_msg->primary_local_gid = param->primary_path->sgid;
903 req_msg->primary_remote_gid = param->primary_path->dgid;
904 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
905 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
906 req_msg->primary_traffic_class = param->primary_path->traffic_class;
907 req_msg->primary_hop_limit = param->primary_path->hop_limit;
908 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
909 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
910 cm_req_set_primary_local_ack_timeout(req_msg,
911 min(31, param->primary_path->packet_life_time + 1));
913 if (param->alternate_path) {
914 req_msg->alt_local_lid = param->alternate_path->slid;
915 req_msg->alt_remote_lid = param->alternate_path->dlid;
916 req_msg->alt_local_gid = param->alternate_path->sgid;
917 req_msg->alt_remote_gid = param->alternate_path->dgid;
918 cm_req_set_alt_flow_label(req_msg,
919 param->alternate_path->flow_label);
920 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
921 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
922 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
923 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
924 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
925 cm_req_set_alt_local_ack_timeout(req_msg,
926 min(31, param->alternate_path->packet_life_time + 1));
929 if (param->private_data && param->private_data_len)
930 memcpy(req_msg->private_data, param->private_data,
931 param->private_data_len);
934 static int cm_validate_req_param(struct ib_cm_req_param *param)
936 /* peer-to-peer not supported */
937 if (param->peer_to_peer)
940 if (!param->primary_path)
943 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
946 if (param->private_data &&
947 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
950 if (param->alternate_path &&
951 (param->alternate_path->pkey != param->primary_path->pkey ||
952 param->alternate_path->mtu != param->primary_path->mtu))
958 int ib_send_cm_req(struct ib_cm_id *cm_id,
959 struct ib_cm_req_param *param)
961 struct cm_id_private *cm_id_priv;
962 struct cm_req_msg *req_msg;
966 ret = cm_validate_req_param(param);
970 /* Verify that we're not in timewait. */
971 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
972 spin_lock_irqsave(&cm_id_priv->lock, flags);
973 if (cm_id->state != IB_CM_IDLE) {
974 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
978 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
980 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
982 if (IS_ERR(cm_id_priv->timewait_info)) {
983 ret = PTR_ERR(cm_id_priv->timewait_info);
987 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
990 if (param->alternate_path) {
991 ret = cm_init_av_by_path(param->alternate_path,
992 &cm_id_priv->alt_av);
996 cm_id->service_id = param->service_id;
997 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
998 cm_id_priv->timeout_ms = cm_convert_to_ms(
999 param->primary_path->packet_life_time) * 2 +
1001 param->remote_cm_response_timeout);
1002 cm_id_priv->max_cm_retries = param->max_cm_retries;
1003 cm_id_priv->initiator_depth = param->initiator_depth;
1004 cm_id_priv->responder_resources = param->responder_resources;
1005 cm_id_priv->retry_count = param->retry_count;
1006 cm_id_priv->path_mtu = param->primary_path->mtu;
1007 cm_id_priv->pkey = param->primary_path->pkey;
1008 cm_id_priv->qp_type = param->qp_type;
1010 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1014 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1015 cm_format_req(req_msg, cm_id_priv, param);
1016 cm_id_priv->tid = req_msg->hdr.tid;
1017 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1018 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1020 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1021 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1023 spin_lock_irqsave(&cm_id_priv->lock, flags);
1024 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1026 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1029 BUG_ON(cm_id->state != IB_CM_IDLE);
1030 cm_id->state = IB_CM_REQ_SENT;
1031 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1034 error2: cm_free_msg(cm_id_priv->msg);
1035 error1: kfree(cm_id_priv->timewait_info);
1038 EXPORT_SYMBOL(ib_send_cm_req);
1040 static int cm_issue_rej(struct cm_port *port,
1041 struct ib_mad_recv_wc *mad_recv_wc,
1042 enum ib_cm_rej_reason reason,
1043 enum cm_msg_response msg_rejected,
1044 void *ari, u8 ari_length)
1046 struct ib_mad_send_buf *msg = NULL;
1047 struct cm_rej_msg *rej_msg, *rcv_msg;
1050 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1054 /* We just need common CM header information. Cast to any message. */
1055 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1056 rej_msg = (struct cm_rej_msg *) msg->mad;
1058 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1059 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1060 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1061 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1062 rej_msg->reason = cpu_to_be16(reason);
1064 if (ari && ari_length) {
1065 cm_rej_set_reject_info_len(rej_msg, ari_length);
1066 memcpy(rej_msg->ari, ari, ari_length);
1069 ret = ib_post_send_mad(msg, NULL);
1076 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1077 __be32 local_qpn, __be32 remote_qpn)
1079 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1080 ((local_ca_guid == remote_ca_guid) &&
1081 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1084 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1085 struct ib_sa_path_rec *primary_path,
1086 struct ib_sa_path_rec *alt_path)
1088 memset(primary_path, 0, sizeof *primary_path);
1089 primary_path->dgid = req_msg->primary_local_gid;
1090 primary_path->sgid = req_msg->primary_remote_gid;
1091 primary_path->dlid = req_msg->primary_local_lid;
1092 primary_path->slid = req_msg->primary_remote_lid;
1093 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1094 primary_path->hop_limit = req_msg->primary_hop_limit;
1095 primary_path->traffic_class = req_msg->primary_traffic_class;
1096 primary_path->reversible = 1;
1097 primary_path->pkey = req_msg->pkey;
1098 primary_path->sl = cm_req_get_primary_sl(req_msg);
1099 primary_path->mtu_selector = IB_SA_EQ;
1100 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1101 primary_path->rate_selector = IB_SA_EQ;
1102 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1103 primary_path->packet_life_time_selector = IB_SA_EQ;
1104 primary_path->packet_life_time =
1105 cm_req_get_primary_local_ack_timeout(req_msg);
1106 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1108 if (req_msg->alt_local_lid) {
1109 memset(alt_path, 0, sizeof *alt_path);
1110 alt_path->dgid = req_msg->alt_local_gid;
1111 alt_path->sgid = req_msg->alt_remote_gid;
1112 alt_path->dlid = req_msg->alt_local_lid;
1113 alt_path->slid = req_msg->alt_remote_lid;
1114 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1115 alt_path->hop_limit = req_msg->alt_hop_limit;
1116 alt_path->traffic_class = req_msg->alt_traffic_class;
1117 alt_path->reversible = 1;
1118 alt_path->pkey = req_msg->pkey;
1119 alt_path->sl = cm_req_get_alt_sl(req_msg);
1120 alt_path->mtu_selector = IB_SA_EQ;
1121 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1122 alt_path->rate_selector = IB_SA_EQ;
1123 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1124 alt_path->packet_life_time_selector = IB_SA_EQ;
1125 alt_path->packet_life_time =
1126 cm_req_get_alt_local_ack_timeout(req_msg);
1127 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1131 static void cm_format_req_event(struct cm_work *work,
1132 struct cm_id_private *cm_id_priv,
1133 struct ib_cm_id *listen_id)
1135 struct cm_req_msg *req_msg;
1136 struct ib_cm_req_event_param *param;
1138 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1139 param = &work->cm_event.param.req_rcvd;
1140 param->listen_id = listen_id;
1141 param->port = cm_id_priv->av.port->port_num;
1142 param->primary_path = &work->path[0];
1143 if (req_msg->alt_local_lid)
1144 param->alternate_path = &work->path[1];
1146 param->alternate_path = NULL;
1147 param->remote_ca_guid = req_msg->local_ca_guid;
1148 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1149 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1150 param->qp_type = cm_req_get_qp_type(req_msg);
1151 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1152 param->responder_resources = cm_req_get_init_depth(req_msg);
1153 param->initiator_depth = cm_req_get_resp_res(req_msg);
1154 param->local_cm_response_timeout =
1155 cm_req_get_remote_resp_timeout(req_msg);
1156 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1157 param->remote_cm_response_timeout =
1158 cm_req_get_local_resp_timeout(req_msg);
1159 param->retry_count = cm_req_get_retry_count(req_msg);
1160 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1161 param->srq = cm_req_get_srq(req_msg);
1162 work->cm_event.private_data = &req_msg->private_data;
1165 static void cm_process_work(struct cm_id_private *cm_id_priv,
1166 struct cm_work *work)
1170 /* We will typically only have the current event to report. */
1171 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1174 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1175 spin_lock_irq(&cm_id_priv->lock);
1176 work = cm_dequeue_work(cm_id_priv);
1177 spin_unlock_irq(&cm_id_priv->lock);
1179 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1183 cm_deref_id(cm_id_priv);
1185 cm_destroy_id(&cm_id_priv->id, ret);
1188 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1189 struct cm_id_private *cm_id_priv,
1190 enum cm_msg_response msg_mraed, u8 service_timeout,
1191 const void *private_data, u8 private_data_len)
1193 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1194 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1195 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1196 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1197 cm_mra_set_service_timeout(mra_msg, service_timeout);
1199 if (private_data && private_data_len)
1200 memcpy(mra_msg->private_data, private_data, private_data_len);
1203 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1204 struct cm_id_private *cm_id_priv,
1205 enum ib_cm_rej_reason reason,
1208 const void *private_data,
1209 u8 private_data_len)
1211 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1212 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1214 switch(cm_id_priv->id.state) {
1215 case IB_CM_REQ_RCVD:
1216 rej_msg->local_comm_id = 0;
1217 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1219 case IB_CM_MRA_REQ_SENT:
1220 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1221 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1223 case IB_CM_REP_RCVD:
1224 case IB_CM_MRA_REP_SENT:
1225 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1226 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1229 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1230 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1234 rej_msg->reason = cpu_to_be16(reason);
1235 if (ari && ari_length) {
1236 cm_rej_set_reject_info_len(rej_msg, ari_length);
1237 memcpy(rej_msg->ari, ari, ari_length);
1240 if (private_data && private_data_len)
1241 memcpy(rej_msg->private_data, private_data, private_data_len);
1244 static void cm_dup_req_handler(struct cm_work *work,
1245 struct cm_id_private *cm_id_priv)
1247 struct ib_mad_send_buf *msg = NULL;
1250 /* Quick state check to discard duplicate REQs. */
1251 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1254 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1258 spin_lock_irq(&cm_id_priv->lock);
1259 switch (cm_id_priv->id.state) {
1260 case IB_CM_MRA_REQ_SENT:
1261 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1262 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1263 cm_id_priv->private_data,
1264 cm_id_priv->private_data_len);
1266 case IB_CM_TIMEWAIT:
1267 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1268 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1273 spin_unlock_irq(&cm_id_priv->lock);
1275 ret = ib_post_send_mad(msg, NULL);
1280 unlock: spin_unlock_irq(&cm_id_priv->lock);
1281 free: cm_free_msg(msg);
1284 static struct cm_id_private * cm_match_req(struct cm_work *work,
1285 struct cm_id_private *cm_id_priv)
1287 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1288 struct cm_timewait_info *timewait_info;
1289 struct cm_req_msg *req_msg;
1291 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1293 /* Check for possible duplicate REQ. */
1294 spin_lock_irq(&cm.lock);
1295 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1296 if (timewait_info) {
1297 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1298 timewait_info->work.remote_id);
1299 spin_unlock_irq(&cm.lock);
1300 if (cur_cm_id_priv) {
1301 cm_dup_req_handler(work, cur_cm_id_priv);
1302 cm_deref_id(cur_cm_id_priv);
1307 /* Check for stale connections. */
1308 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1309 if (timewait_info) {
1310 cm_cleanup_timewait(cm_id_priv->timewait_info);
1311 spin_unlock_irq(&cm.lock);
1312 cm_issue_rej(work->port, work->mad_recv_wc,
1313 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1318 /* Find matching listen request. */
1319 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1320 req_msg->service_id,
1321 req_msg->private_data);
1322 if (!listen_cm_id_priv) {
1323 cm_cleanup_timewait(cm_id_priv->timewait_info);
1324 spin_unlock_irq(&cm.lock);
1325 cm_issue_rej(work->port, work->mad_recv_wc,
1326 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1330 atomic_inc(&listen_cm_id_priv->refcount);
1331 atomic_inc(&cm_id_priv->refcount);
1332 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1333 atomic_inc(&cm_id_priv->work_count);
1334 spin_unlock_irq(&cm.lock);
1336 return listen_cm_id_priv;
1339 static int cm_req_handler(struct cm_work *work)
1341 struct ib_cm_id *cm_id;
1342 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1343 struct cm_req_msg *req_msg;
1346 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1348 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1350 return PTR_ERR(cm_id);
1352 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1353 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1354 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1355 work->mad_recv_wc->recv_buf.grh,
1357 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1359 if (IS_ERR(cm_id_priv->timewait_info)) {
1360 ret = PTR_ERR(cm_id_priv->timewait_info);
1363 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1364 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1365 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1367 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1368 if (!listen_cm_id_priv) {
1370 kfree(cm_id_priv->timewait_info);
1374 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1375 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1376 cm_id_priv->id.service_id = req_msg->service_id;
1377 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1379 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1380 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1382 ib_get_cached_gid(work->port->cm_dev->device,
1383 work->port->port_num, 0, &work->path[0].sgid);
1384 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1385 &work->path[0].sgid, sizeof work->path[0].sgid,
1389 if (req_msg->alt_local_lid) {
1390 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1392 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1393 &work->path[0].sgid,
1394 sizeof work->path[0].sgid, NULL, 0);
1398 cm_id_priv->tid = req_msg->hdr.tid;
1399 cm_id_priv->timeout_ms = cm_convert_to_ms(
1400 cm_req_get_local_resp_timeout(req_msg));
1401 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1402 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1403 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1404 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1405 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1406 cm_id_priv->pkey = req_msg->pkey;
1407 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1408 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1409 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1410 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1412 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1413 cm_process_work(cm_id_priv, work);
1414 cm_deref_id(listen_cm_id_priv);
1418 atomic_dec(&cm_id_priv->refcount);
1419 cm_deref_id(listen_cm_id_priv);
1421 ib_destroy_cm_id(cm_id);
1425 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1426 struct cm_id_private *cm_id_priv,
1427 struct ib_cm_rep_param *param)
1429 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1430 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1431 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1432 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1433 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1434 rep_msg->resp_resources = param->responder_resources;
1435 rep_msg->initiator_depth = param->initiator_depth;
1436 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1437 cm_rep_set_failover(rep_msg, param->failover_accepted);
1438 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1439 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1440 cm_rep_set_srq(rep_msg, param->srq);
1441 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1443 if (param->private_data && param->private_data_len)
1444 memcpy(rep_msg->private_data, param->private_data,
1445 param->private_data_len);
1448 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1449 struct ib_cm_rep_param *param)
1451 struct cm_id_private *cm_id_priv;
1452 struct ib_mad_send_buf *msg;
1453 struct cm_rep_msg *rep_msg;
1454 unsigned long flags;
1457 if (param->private_data &&
1458 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1461 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1462 spin_lock_irqsave(&cm_id_priv->lock, flags);
1463 if (cm_id->state != IB_CM_REQ_RCVD &&
1464 cm_id->state != IB_CM_MRA_REQ_SENT) {
1469 ret = cm_alloc_msg(cm_id_priv, &msg);
1473 rep_msg = (struct cm_rep_msg *) msg->mad;
1474 cm_format_rep(rep_msg, cm_id_priv, param);
1475 msg->timeout_ms = cm_id_priv->timeout_ms;
1476 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1478 ret = ib_post_send_mad(msg, NULL);
1480 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1485 cm_id->state = IB_CM_REP_SENT;
1486 cm_id_priv->msg = msg;
1487 cm_id_priv->initiator_depth = param->initiator_depth;
1488 cm_id_priv->responder_resources = param->responder_resources;
1489 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1490 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1492 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1495 EXPORT_SYMBOL(ib_send_cm_rep);
1497 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1498 struct cm_id_private *cm_id_priv,
1499 const void *private_data,
1500 u8 private_data_len)
1502 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1503 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1504 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1506 if (private_data && private_data_len)
1507 memcpy(rtu_msg->private_data, private_data, private_data_len);
1510 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1511 const void *private_data,
1512 u8 private_data_len)
1514 struct cm_id_private *cm_id_priv;
1515 struct ib_mad_send_buf *msg;
1516 unsigned long flags;
1520 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1523 data = cm_copy_private_data(private_data, private_data_len);
1525 return PTR_ERR(data);
1527 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1528 spin_lock_irqsave(&cm_id_priv->lock, flags);
1529 if (cm_id->state != IB_CM_REP_RCVD &&
1530 cm_id->state != IB_CM_MRA_REP_SENT) {
1535 ret = cm_alloc_msg(cm_id_priv, &msg);
1539 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1540 private_data, private_data_len);
1542 ret = ib_post_send_mad(msg, NULL);
1544 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1550 cm_id->state = IB_CM_ESTABLISHED;
1551 cm_set_private_data(cm_id_priv, data, private_data_len);
1552 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1555 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1559 EXPORT_SYMBOL(ib_send_cm_rtu);
1561 static void cm_format_rep_event(struct cm_work *work)
1563 struct cm_rep_msg *rep_msg;
1564 struct ib_cm_rep_event_param *param;
1566 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1567 param = &work->cm_event.param.rep_rcvd;
1568 param->remote_ca_guid = rep_msg->local_ca_guid;
1569 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1570 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1571 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1572 param->responder_resources = rep_msg->initiator_depth;
1573 param->initiator_depth = rep_msg->resp_resources;
1574 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1575 param->failover_accepted = cm_rep_get_failover(rep_msg);
1576 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1577 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1578 param->srq = cm_rep_get_srq(rep_msg);
1579 work->cm_event.private_data = &rep_msg->private_data;
1582 static void cm_dup_rep_handler(struct cm_work *work)
1584 struct cm_id_private *cm_id_priv;
1585 struct cm_rep_msg *rep_msg;
1586 struct ib_mad_send_buf *msg = NULL;
1589 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1590 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1591 rep_msg->local_comm_id);
1595 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1599 spin_lock_irq(&cm_id_priv->lock);
1600 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1601 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1602 cm_id_priv->private_data,
1603 cm_id_priv->private_data_len);
1604 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1605 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1606 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1607 cm_id_priv->private_data,
1608 cm_id_priv->private_data_len);
1611 spin_unlock_irq(&cm_id_priv->lock);
1613 ret = ib_post_send_mad(msg, NULL);
1618 unlock: spin_unlock_irq(&cm_id_priv->lock);
1619 free: cm_free_msg(msg);
1620 deref: cm_deref_id(cm_id_priv);
1623 static int cm_rep_handler(struct cm_work *work)
1625 struct cm_id_private *cm_id_priv;
1626 struct cm_rep_msg *rep_msg;
1629 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1630 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1632 cm_dup_rep_handler(work);
1636 cm_format_rep_event(work);
1638 spin_lock_irq(&cm_id_priv->lock);
1639 switch (cm_id_priv->id.state) {
1640 case IB_CM_REQ_SENT:
1641 case IB_CM_MRA_REQ_RCVD:
1644 spin_unlock_irq(&cm_id_priv->lock);
1649 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1650 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1651 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1653 spin_lock(&cm.lock);
1654 /* Check for duplicate REP. */
1655 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1656 spin_unlock(&cm.lock);
1657 spin_unlock_irq(&cm_id_priv->lock);
1661 /* Check for a stale connection. */
1662 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1663 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1664 &cm.remote_id_table);
1665 cm_id_priv->timewait_info->inserted_remote_id = 0;
1666 spin_unlock(&cm.lock);
1667 spin_unlock_irq(&cm_id_priv->lock);
1668 cm_issue_rej(work->port, work->mad_recv_wc,
1669 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1674 spin_unlock(&cm.lock);
1676 cm_id_priv->id.state = IB_CM_REP_RCVD;
1677 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1678 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1679 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1680 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1681 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1682 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1684 /* todo: handle peer_to_peer */
1686 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1687 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1689 list_add_tail(&work->list, &cm_id_priv->work_list);
1690 spin_unlock_irq(&cm_id_priv->lock);
1693 cm_process_work(cm_id_priv, work);
1695 cm_deref_id(cm_id_priv);
1699 cm_deref_id(cm_id_priv);
1703 static int cm_establish_handler(struct cm_work *work)
1705 struct cm_id_private *cm_id_priv;
1708 /* See comment in cm_establish about lookup. */
1709 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1713 spin_lock_irq(&cm_id_priv->lock);
1714 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1715 spin_unlock_irq(&cm_id_priv->lock);
1719 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1720 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1722 list_add_tail(&work->list, &cm_id_priv->work_list);
1723 spin_unlock_irq(&cm_id_priv->lock);
1726 cm_process_work(cm_id_priv, work);
1728 cm_deref_id(cm_id_priv);
1731 cm_deref_id(cm_id_priv);
1735 static int cm_rtu_handler(struct cm_work *work)
1737 struct cm_id_private *cm_id_priv;
1738 struct cm_rtu_msg *rtu_msg;
1741 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1742 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1743 rtu_msg->local_comm_id);
1747 work->cm_event.private_data = &rtu_msg->private_data;
1749 spin_lock_irq(&cm_id_priv->lock);
1750 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1751 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1752 spin_unlock_irq(&cm_id_priv->lock);
1755 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1757 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1758 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1760 list_add_tail(&work->list, &cm_id_priv->work_list);
1761 spin_unlock_irq(&cm_id_priv->lock);
1764 cm_process_work(cm_id_priv, work);
1766 cm_deref_id(cm_id_priv);
1769 cm_deref_id(cm_id_priv);
1773 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1774 struct cm_id_private *cm_id_priv,
1775 const void *private_data,
1776 u8 private_data_len)
1778 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1779 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1780 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1781 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1782 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1784 if (private_data && private_data_len)
1785 memcpy(dreq_msg->private_data, private_data, private_data_len);
1788 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1789 const void *private_data,
1790 u8 private_data_len)
1792 struct cm_id_private *cm_id_priv;
1793 struct ib_mad_send_buf *msg;
1794 unsigned long flags;
1797 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1800 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1801 spin_lock_irqsave(&cm_id_priv->lock, flags);
1802 if (cm_id->state != IB_CM_ESTABLISHED) {
1807 ret = cm_alloc_msg(cm_id_priv, &msg);
1809 cm_enter_timewait(cm_id_priv);
1813 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1814 private_data, private_data_len);
1815 msg->timeout_ms = cm_id_priv->timeout_ms;
1816 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1818 ret = ib_post_send_mad(msg, NULL);
1820 cm_enter_timewait(cm_id_priv);
1821 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1826 cm_id->state = IB_CM_DREQ_SENT;
1827 cm_id_priv->msg = msg;
1828 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1831 EXPORT_SYMBOL(ib_send_cm_dreq);
1833 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1834 struct cm_id_private *cm_id_priv,
1835 const void *private_data,
1836 u8 private_data_len)
1838 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1839 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1840 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1842 if (private_data && private_data_len)
1843 memcpy(drep_msg->private_data, private_data, private_data_len);
1846 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1847 const void *private_data,
1848 u8 private_data_len)
1850 struct cm_id_private *cm_id_priv;
1851 struct ib_mad_send_buf *msg;
1852 unsigned long flags;
1856 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1859 data = cm_copy_private_data(private_data, private_data_len);
1861 return PTR_ERR(data);
1863 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1864 spin_lock_irqsave(&cm_id_priv->lock, flags);
1865 if (cm_id->state != IB_CM_DREQ_RCVD) {
1866 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1871 cm_set_private_data(cm_id_priv, data, private_data_len);
1872 cm_enter_timewait(cm_id_priv);
1874 ret = cm_alloc_msg(cm_id_priv, &msg);
1878 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1879 private_data, private_data_len);
1881 ret = ib_post_send_mad(msg, NULL);
1883 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1888 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1891 EXPORT_SYMBOL(ib_send_cm_drep);
1893 static int cm_issue_drep(struct cm_port *port,
1894 struct ib_mad_recv_wc *mad_recv_wc)
1896 struct ib_mad_send_buf *msg = NULL;
1897 struct cm_dreq_msg *dreq_msg;
1898 struct cm_drep_msg *drep_msg;
1901 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1905 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
1906 drep_msg = (struct cm_drep_msg *) msg->mad;
1908 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
1909 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
1910 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
1912 ret = ib_post_send_mad(msg, NULL);
1919 static int cm_dreq_handler(struct cm_work *work)
1921 struct cm_id_private *cm_id_priv;
1922 struct cm_dreq_msg *dreq_msg;
1923 struct ib_mad_send_buf *msg = NULL;
1926 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1927 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1928 dreq_msg->local_comm_id);
1930 cm_issue_drep(work->port, work->mad_recv_wc);
1934 work->cm_event.private_data = &dreq_msg->private_data;
1936 spin_lock_irq(&cm_id_priv->lock);
1937 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1940 switch (cm_id_priv->id.state) {
1941 case IB_CM_REP_SENT:
1942 case IB_CM_DREQ_SENT:
1943 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1945 case IB_CM_ESTABLISHED:
1946 case IB_CM_MRA_REP_RCVD:
1948 case IB_CM_TIMEWAIT:
1949 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1952 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1953 cm_id_priv->private_data,
1954 cm_id_priv->private_data_len);
1955 spin_unlock_irq(&cm_id_priv->lock);
1957 if (ib_post_send_mad(msg, NULL))
1963 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1964 cm_id_priv->tid = dreq_msg->hdr.tid;
1965 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1967 list_add_tail(&work->list, &cm_id_priv->work_list);
1968 spin_unlock_irq(&cm_id_priv->lock);
1971 cm_process_work(cm_id_priv, work);
1973 cm_deref_id(cm_id_priv);
1976 unlock: spin_unlock_irq(&cm_id_priv->lock);
1977 deref: cm_deref_id(cm_id_priv);
1981 static int cm_drep_handler(struct cm_work *work)
1983 struct cm_id_private *cm_id_priv;
1984 struct cm_drep_msg *drep_msg;
1987 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1988 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1989 drep_msg->local_comm_id);
1993 work->cm_event.private_data = &drep_msg->private_data;
1995 spin_lock_irq(&cm_id_priv->lock);
1996 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1997 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1998 spin_unlock_irq(&cm_id_priv->lock);
2001 cm_enter_timewait(cm_id_priv);
2003 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2004 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2006 list_add_tail(&work->list, &cm_id_priv->work_list);
2007 spin_unlock_irq(&cm_id_priv->lock);
2010 cm_process_work(cm_id_priv, work);
2012 cm_deref_id(cm_id_priv);
2015 cm_deref_id(cm_id_priv);
2019 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2020 enum ib_cm_rej_reason reason,
2023 const void *private_data,
2024 u8 private_data_len)
2026 struct cm_id_private *cm_id_priv;
2027 struct ib_mad_send_buf *msg;
2028 unsigned long flags;
2031 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2032 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2035 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2037 spin_lock_irqsave(&cm_id_priv->lock, flags);
2038 switch (cm_id->state) {
2039 case IB_CM_REQ_SENT:
2040 case IB_CM_MRA_REQ_RCVD:
2041 case IB_CM_REQ_RCVD:
2042 case IB_CM_MRA_REQ_SENT:
2043 case IB_CM_REP_RCVD:
2044 case IB_CM_MRA_REP_SENT:
2045 ret = cm_alloc_msg(cm_id_priv, &msg);
2047 cm_format_rej((struct cm_rej_msg *) msg->mad,
2048 cm_id_priv, reason, ari, ari_length,
2049 private_data, private_data_len);
2051 cm_reset_to_idle(cm_id_priv);
2053 case IB_CM_REP_SENT:
2054 case IB_CM_MRA_REP_RCVD:
2055 ret = cm_alloc_msg(cm_id_priv, &msg);
2057 cm_format_rej((struct cm_rej_msg *) msg->mad,
2058 cm_id_priv, reason, ari, ari_length,
2059 private_data, private_data_len);
2061 cm_enter_timewait(cm_id_priv);
2071 ret = ib_post_send_mad(msg, NULL);
2075 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2078 EXPORT_SYMBOL(ib_send_cm_rej);
2080 static void cm_format_rej_event(struct cm_work *work)
2082 struct cm_rej_msg *rej_msg;
2083 struct ib_cm_rej_event_param *param;
2085 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2086 param = &work->cm_event.param.rej_rcvd;
2087 param->ari = rej_msg->ari;
2088 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2089 param->reason = __be16_to_cpu(rej_msg->reason);
2090 work->cm_event.private_data = &rej_msg->private_data;
2093 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2095 struct cm_timewait_info *timewait_info;
2096 struct cm_id_private *cm_id_priv;
2099 remote_id = rej_msg->local_comm_id;
2101 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2102 spin_lock_irq(&cm.lock);
2103 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2105 if (!timewait_info) {
2106 spin_unlock_irq(&cm.lock);
2109 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2110 (timewait_info->work.local_id ^
2111 cm.random_id_operand));
2113 if (cm_id_priv->id.remote_id == remote_id)
2114 atomic_inc(&cm_id_priv->refcount);
2118 spin_unlock_irq(&cm.lock);
2119 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2120 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2122 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2127 static int cm_rej_handler(struct cm_work *work)
2129 struct cm_id_private *cm_id_priv;
2130 struct cm_rej_msg *rej_msg;
2133 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2134 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2138 cm_format_rej_event(work);
2140 spin_lock_irq(&cm_id_priv->lock);
2141 switch (cm_id_priv->id.state) {
2142 case IB_CM_REQ_SENT:
2143 case IB_CM_MRA_REQ_RCVD:
2144 case IB_CM_REP_SENT:
2145 case IB_CM_MRA_REP_RCVD:
2146 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2148 case IB_CM_REQ_RCVD:
2149 case IB_CM_MRA_REQ_SENT:
2150 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2151 cm_enter_timewait(cm_id_priv);
2153 cm_reset_to_idle(cm_id_priv);
2155 case IB_CM_DREQ_SENT:
2156 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2158 case IB_CM_REP_RCVD:
2159 case IB_CM_MRA_REP_SENT:
2160 case IB_CM_ESTABLISHED:
2161 cm_enter_timewait(cm_id_priv);
2164 spin_unlock_irq(&cm_id_priv->lock);
2169 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2171 list_add_tail(&work->list, &cm_id_priv->work_list);
2172 spin_unlock_irq(&cm_id_priv->lock);
2175 cm_process_work(cm_id_priv, work);
2177 cm_deref_id(cm_id_priv);
2180 cm_deref_id(cm_id_priv);
2184 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2186 const void *private_data,
2187 u8 private_data_len)
2189 struct cm_id_private *cm_id_priv;
2190 struct ib_mad_send_buf *msg;
2192 unsigned long flags;
2195 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2198 data = cm_copy_private_data(private_data, private_data_len);
2200 return PTR_ERR(data);
2202 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2204 spin_lock_irqsave(&cm_id_priv->lock, flags);
2205 switch(cm_id_priv->id.state) {
2206 case IB_CM_REQ_RCVD:
2207 ret = cm_alloc_msg(cm_id_priv, &msg);
2211 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2212 CM_MSG_RESPONSE_REQ, service_timeout,
2213 private_data, private_data_len);
2214 ret = ib_post_send_mad(msg, NULL);
2217 cm_id->state = IB_CM_MRA_REQ_SENT;
2219 case IB_CM_REP_RCVD:
2220 ret = cm_alloc_msg(cm_id_priv, &msg);
2224 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2225 CM_MSG_RESPONSE_REP, service_timeout,
2226 private_data, private_data_len);
2227 ret = ib_post_send_mad(msg, NULL);
2230 cm_id->state = IB_CM_MRA_REP_SENT;
2232 case IB_CM_ESTABLISHED:
2233 ret = cm_alloc_msg(cm_id_priv, &msg);
2237 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2238 CM_MSG_RESPONSE_OTHER, service_timeout,
2239 private_data, private_data_len);
2240 ret = ib_post_send_mad(msg, NULL);
2243 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2249 cm_id_priv->service_timeout = service_timeout;
2250 cm_set_private_data(cm_id_priv, data, private_data_len);
2251 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2254 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2258 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2263 EXPORT_SYMBOL(ib_send_cm_mra);
2265 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2267 switch (cm_mra_get_msg_mraed(mra_msg)) {
2268 case CM_MSG_RESPONSE_REQ:
2269 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2270 case CM_MSG_RESPONSE_REP:
2271 case CM_MSG_RESPONSE_OTHER:
2272 return cm_acquire_id(mra_msg->remote_comm_id,
2273 mra_msg->local_comm_id);
2279 static int cm_mra_handler(struct cm_work *work)
2281 struct cm_id_private *cm_id_priv;
2282 struct cm_mra_msg *mra_msg;
2285 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2286 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2290 work->cm_event.private_data = &mra_msg->private_data;
2291 work->cm_event.param.mra_rcvd.service_timeout =
2292 cm_mra_get_service_timeout(mra_msg);
2293 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2294 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2296 spin_lock_irq(&cm_id_priv->lock);
2297 switch (cm_id_priv->id.state) {
2298 case IB_CM_REQ_SENT:
2299 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2300 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2301 cm_id_priv->msg, timeout))
2303 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2305 case IB_CM_REP_SENT:
2306 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2307 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2308 cm_id_priv->msg, timeout))
2310 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2312 case IB_CM_ESTABLISHED:
2313 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2314 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2315 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2316 cm_id_priv->msg, timeout))
2318 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2324 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2325 cm_id_priv->id.state;
2326 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2328 list_add_tail(&work->list, &cm_id_priv->work_list);
2329 spin_unlock_irq(&cm_id_priv->lock);
2332 cm_process_work(cm_id_priv, work);
2334 cm_deref_id(cm_id_priv);
2337 spin_unlock_irq(&cm_id_priv->lock);
2338 cm_deref_id(cm_id_priv);
2342 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2343 struct cm_id_private *cm_id_priv,
2344 struct ib_sa_path_rec *alternate_path,
2345 const void *private_data,
2346 u8 private_data_len)
2348 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2349 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2350 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2351 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2352 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2353 /* todo: need remote CM response timeout */
2354 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2355 lap_msg->alt_local_lid = alternate_path->slid;
2356 lap_msg->alt_remote_lid = alternate_path->dlid;
2357 lap_msg->alt_local_gid = alternate_path->sgid;
2358 lap_msg->alt_remote_gid = alternate_path->dgid;
2359 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2360 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2361 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2362 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2363 cm_lap_set_sl(lap_msg, alternate_path->sl);
2364 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2365 cm_lap_set_local_ack_timeout(lap_msg,
2366 min(31, alternate_path->packet_life_time + 1));
2368 if (private_data && private_data_len)
2369 memcpy(lap_msg->private_data, private_data, private_data_len);
2372 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2373 struct ib_sa_path_rec *alternate_path,
2374 const void *private_data,
2375 u8 private_data_len)
2377 struct cm_id_private *cm_id_priv;
2378 struct ib_mad_send_buf *msg;
2379 unsigned long flags;
2382 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2385 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2386 spin_lock_irqsave(&cm_id_priv->lock, flags);
2387 if (cm_id->state != IB_CM_ESTABLISHED ||
2388 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2389 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2394 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2398 ret = cm_alloc_msg(cm_id_priv, &msg);
2402 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2403 alternate_path, private_data, private_data_len);
2404 msg->timeout_ms = cm_id_priv->timeout_ms;
2405 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2407 ret = ib_post_send_mad(msg, NULL);
2409 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2414 cm_id->lap_state = IB_CM_LAP_SENT;
2415 cm_id_priv->msg = msg;
2417 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2420 EXPORT_SYMBOL(ib_send_cm_lap);
2422 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2423 struct ib_sa_path_rec *path,
2424 struct cm_lap_msg *lap_msg)
2426 memset(path, 0, sizeof *path);
2427 path->dgid = lap_msg->alt_local_gid;
2428 path->sgid = lap_msg->alt_remote_gid;
2429 path->dlid = lap_msg->alt_local_lid;
2430 path->slid = lap_msg->alt_remote_lid;
2431 path->flow_label = cm_lap_get_flow_label(lap_msg);
2432 path->hop_limit = lap_msg->alt_hop_limit;
2433 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2434 path->reversible = 1;
2435 path->pkey = cm_id_priv->pkey;
2436 path->sl = cm_lap_get_sl(lap_msg);
2437 path->mtu_selector = IB_SA_EQ;
2438 path->mtu = cm_id_priv->path_mtu;
2439 path->rate_selector = IB_SA_EQ;
2440 path->rate = cm_lap_get_packet_rate(lap_msg);
2441 path->packet_life_time_selector = IB_SA_EQ;
2442 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2443 path->packet_life_time -= (path->packet_life_time > 0);
2446 static int cm_lap_handler(struct cm_work *work)
2448 struct cm_id_private *cm_id_priv;
2449 struct cm_lap_msg *lap_msg;
2450 struct ib_cm_lap_event_param *param;
2451 struct ib_mad_send_buf *msg = NULL;
2454 /* todo: verify LAP request and send reject APR if invalid. */
2455 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2456 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2457 lap_msg->local_comm_id);
2461 param = &work->cm_event.param.lap_rcvd;
2462 param->alternate_path = &work->path[0];
2463 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2464 work->cm_event.private_data = &lap_msg->private_data;
2466 spin_lock_irq(&cm_id_priv->lock);
2467 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2470 switch (cm_id_priv->id.lap_state) {
2471 case IB_CM_LAP_UNINIT:
2472 case IB_CM_LAP_IDLE:
2474 case IB_CM_MRA_LAP_SENT:
2475 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2478 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2479 CM_MSG_RESPONSE_OTHER,
2480 cm_id_priv->service_timeout,
2481 cm_id_priv->private_data,
2482 cm_id_priv->private_data_len);
2483 spin_unlock_irq(&cm_id_priv->lock);
2485 if (ib_post_send_mad(msg, NULL))
2492 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2493 cm_id_priv->tid = lap_msg->hdr.tid;
2494 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2495 work->mad_recv_wc->recv_buf.grh,
2497 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2498 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2500 list_add_tail(&work->list, &cm_id_priv->work_list);
2501 spin_unlock_irq(&cm_id_priv->lock);
2504 cm_process_work(cm_id_priv, work);
2506 cm_deref_id(cm_id_priv);
2509 unlock: spin_unlock_irq(&cm_id_priv->lock);
2510 deref: cm_deref_id(cm_id_priv);
2514 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2515 struct cm_id_private *cm_id_priv,
2516 enum ib_cm_apr_status status,
2519 const void *private_data,
2520 u8 private_data_len)
2522 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2523 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2524 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2525 apr_msg->ap_status = (u8) status;
2527 if (info && info_length) {
2528 apr_msg->info_length = info_length;
2529 memcpy(apr_msg->info, info, info_length);
2532 if (private_data && private_data_len)
2533 memcpy(apr_msg->private_data, private_data, private_data_len);
2536 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2537 enum ib_cm_apr_status status,
2540 const void *private_data,
2541 u8 private_data_len)
2543 struct cm_id_private *cm_id_priv;
2544 struct ib_mad_send_buf *msg;
2545 unsigned long flags;
2548 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2549 (info && info_length > IB_CM_APR_INFO_LENGTH))
2552 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2553 spin_lock_irqsave(&cm_id_priv->lock, flags);
2554 if (cm_id->state != IB_CM_ESTABLISHED ||
2555 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2556 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2561 ret = cm_alloc_msg(cm_id_priv, &msg);
2565 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2566 info, info_length, private_data, private_data_len);
2567 ret = ib_post_send_mad(msg, NULL);
2569 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2574 cm_id->lap_state = IB_CM_LAP_IDLE;
2575 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2578 EXPORT_SYMBOL(ib_send_cm_apr);
2580 static int cm_apr_handler(struct cm_work *work)
2582 struct cm_id_private *cm_id_priv;
2583 struct cm_apr_msg *apr_msg;
2586 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2587 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2588 apr_msg->local_comm_id);
2590 return -EINVAL; /* Unmatched reply. */
2592 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2593 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2594 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2595 work->cm_event.private_data = &apr_msg->private_data;
2597 spin_lock_irq(&cm_id_priv->lock);
2598 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2599 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2600 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2601 spin_unlock_irq(&cm_id_priv->lock);
2604 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2605 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2606 cm_id_priv->msg = NULL;
2608 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2610 list_add_tail(&work->list, &cm_id_priv->work_list);
2611 spin_unlock_irq(&cm_id_priv->lock);
2614 cm_process_work(cm_id_priv, work);
2616 cm_deref_id(cm_id_priv);
2619 cm_deref_id(cm_id_priv);
2623 static int cm_timewait_handler(struct cm_work *work)
2625 struct cm_timewait_info *timewait_info;
2626 struct cm_id_private *cm_id_priv;
2629 timewait_info = (struct cm_timewait_info *)work;
2630 spin_lock_irq(&cm.lock);
2631 list_del(&timewait_info->list);
2632 spin_unlock_irq(&cm.lock);
2634 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2635 timewait_info->work.remote_id);
2639 spin_lock_irq(&cm_id_priv->lock);
2640 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2641 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2642 spin_unlock_irq(&cm_id_priv->lock);
2645 cm_id_priv->id.state = IB_CM_IDLE;
2646 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2648 list_add_tail(&work->list, &cm_id_priv->work_list);
2649 spin_unlock_irq(&cm_id_priv->lock);
2652 cm_process_work(cm_id_priv, work);
2654 cm_deref_id(cm_id_priv);
2657 cm_deref_id(cm_id_priv);
2661 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2662 struct cm_id_private *cm_id_priv,
2663 struct ib_cm_sidr_req_param *param)
2665 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2666 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2667 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2668 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2669 sidr_req_msg->service_id = param->service_id;
2671 if (param->private_data && param->private_data_len)
2672 memcpy(sidr_req_msg->private_data, param->private_data,
2673 param->private_data_len);
2676 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2677 struct ib_cm_sidr_req_param *param)
2679 struct cm_id_private *cm_id_priv;
2680 struct ib_mad_send_buf *msg;
2681 unsigned long flags;
2684 if (!param->path || (param->private_data &&
2685 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2688 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2689 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2693 cm_id->service_id = param->service_id;
2694 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2695 cm_id_priv->timeout_ms = param->timeout_ms;
2696 cm_id_priv->max_cm_retries = param->max_cm_retries;
2697 ret = cm_alloc_msg(cm_id_priv, &msg);
2701 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2703 msg->timeout_ms = cm_id_priv->timeout_ms;
2704 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2706 spin_lock_irqsave(&cm_id_priv->lock, flags);
2707 if (cm_id->state == IB_CM_IDLE)
2708 ret = ib_post_send_mad(msg, NULL);
2713 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2717 cm_id->state = IB_CM_SIDR_REQ_SENT;
2718 cm_id_priv->msg = msg;
2719 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2723 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2725 static void cm_format_sidr_req_event(struct cm_work *work,
2726 struct ib_cm_id *listen_id)
2728 struct cm_sidr_req_msg *sidr_req_msg;
2729 struct ib_cm_sidr_req_event_param *param;
2731 sidr_req_msg = (struct cm_sidr_req_msg *)
2732 work->mad_recv_wc->recv_buf.mad;
2733 param = &work->cm_event.param.sidr_req_rcvd;
2734 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2735 param->listen_id = listen_id;
2736 param->port = work->port->port_num;
2737 work->cm_event.private_data = &sidr_req_msg->private_data;
2740 static int cm_sidr_req_handler(struct cm_work *work)
2742 struct ib_cm_id *cm_id;
2743 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2744 struct cm_sidr_req_msg *sidr_req_msg;
2747 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2749 return PTR_ERR(cm_id);
2750 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2752 /* Record SGID/SLID and request ID for lookup. */
2753 sidr_req_msg = (struct cm_sidr_req_msg *)
2754 work->mad_recv_wc->recv_buf.mad;
2755 wc = work->mad_recv_wc->wc;
2756 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2757 cm_id_priv->av.dgid.global.interface_id = 0;
2758 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2759 work->mad_recv_wc->recv_buf.grh,
2761 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2762 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2763 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2764 atomic_inc(&cm_id_priv->work_count);
2766 spin_lock_irq(&cm.lock);
2767 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2768 if (cur_cm_id_priv) {
2769 spin_unlock_irq(&cm.lock);
2770 goto out; /* Duplicate message. */
2772 cur_cm_id_priv = cm_find_listen(cm_id->device,
2773 sidr_req_msg->service_id,
2774 sidr_req_msg->private_data);
2775 if (!cur_cm_id_priv) {
2776 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2777 spin_unlock_irq(&cm.lock);
2778 /* todo: reply with no match */
2779 goto out; /* No match. */
2781 atomic_inc(&cur_cm_id_priv->refcount);
2782 spin_unlock_irq(&cm.lock);
2784 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2785 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2786 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2787 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2789 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2790 cm_process_work(cm_id_priv, work);
2791 cm_deref_id(cur_cm_id_priv);
2794 ib_destroy_cm_id(&cm_id_priv->id);
2798 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2799 struct cm_id_private *cm_id_priv,
2800 struct ib_cm_sidr_rep_param *param)
2802 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2804 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2805 sidr_rep_msg->status = param->status;
2806 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2807 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2808 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2810 if (param->info && param->info_length)
2811 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2813 if (param->private_data && param->private_data_len)
2814 memcpy(sidr_rep_msg->private_data, param->private_data,
2815 param->private_data_len);
2818 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2819 struct ib_cm_sidr_rep_param *param)
2821 struct cm_id_private *cm_id_priv;
2822 struct ib_mad_send_buf *msg;
2823 unsigned long flags;
2826 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2827 (param->private_data &&
2828 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2831 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2832 spin_lock_irqsave(&cm_id_priv->lock, flags);
2833 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2838 ret = cm_alloc_msg(cm_id_priv, &msg);
2842 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2844 ret = ib_post_send_mad(msg, NULL);
2846 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2850 cm_id->state = IB_CM_IDLE;
2851 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2853 spin_lock_irqsave(&cm.lock, flags);
2854 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2855 spin_unlock_irqrestore(&cm.lock, flags);
2858 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2861 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2863 static void cm_format_sidr_rep_event(struct cm_work *work)
2865 struct cm_sidr_rep_msg *sidr_rep_msg;
2866 struct ib_cm_sidr_rep_event_param *param;
2868 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2869 work->mad_recv_wc->recv_buf.mad;
2870 param = &work->cm_event.param.sidr_rep_rcvd;
2871 param->status = sidr_rep_msg->status;
2872 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2873 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2874 param->info = &sidr_rep_msg->info;
2875 param->info_len = sidr_rep_msg->info_length;
2876 work->cm_event.private_data = &sidr_rep_msg->private_data;
2879 static int cm_sidr_rep_handler(struct cm_work *work)
2881 struct cm_sidr_rep_msg *sidr_rep_msg;
2882 struct cm_id_private *cm_id_priv;
2884 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2885 work->mad_recv_wc->recv_buf.mad;
2886 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2888 return -EINVAL; /* Unmatched reply. */
2890 spin_lock_irq(&cm_id_priv->lock);
2891 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2892 spin_unlock_irq(&cm_id_priv->lock);
2895 cm_id_priv->id.state = IB_CM_IDLE;
2896 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2897 spin_unlock_irq(&cm_id_priv->lock);
2899 cm_format_sidr_rep_event(work);
2900 cm_process_work(cm_id_priv, work);
2903 cm_deref_id(cm_id_priv);
2907 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2908 enum ib_wc_status wc_status)
2910 struct cm_id_private *cm_id_priv;
2911 struct ib_cm_event cm_event;
2912 enum ib_cm_state state;
2915 memset(&cm_event, 0, sizeof cm_event);
2916 cm_id_priv = msg->context[0];
2918 /* Discard old sends or ones without a response. */
2919 spin_lock_irq(&cm_id_priv->lock);
2920 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2921 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2925 case IB_CM_REQ_SENT:
2926 case IB_CM_MRA_REQ_RCVD:
2927 cm_reset_to_idle(cm_id_priv);
2928 cm_event.event = IB_CM_REQ_ERROR;
2930 case IB_CM_REP_SENT:
2931 case IB_CM_MRA_REP_RCVD:
2932 cm_reset_to_idle(cm_id_priv);
2933 cm_event.event = IB_CM_REP_ERROR;
2935 case IB_CM_DREQ_SENT:
2936 cm_enter_timewait(cm_id_priv);
2937 cm_event.event = IB_CM_DREQ_ERROR;
2939 case IB_CM_SIDR_REQ_SENT:
2940 cm_id_priv->id.state = IB_CM_IDLE;
2941 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2946 spin_unlock_irq(&cm_id_priv->lock);
2947 cm_event.param.send_status = wc_status;
2949 /* No other events can occur on the cm_id at this point. */
2950 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2953 ib_destroy_cm_id(&cm_id_priv->id);
2956 spin_unlock_irq(&cm_id_priv->lock);
2960 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2961 struct ib_mad_send_wc *mad_send_wc)
2963 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2965 switch (mad_send_wc->status) {
2967 case IB_WC_WR_FLUSH_ERR:
2971 if (msg->context[0] && msg->context[1])
2972 cm_process_send_error(msg, mad_send_wc->status);
2979 static void cm_work_handler(struct work_struct *_work)
2981 struct cm_work *work = container_of(_work, struct cm_work, work.work);
2984 switch (work->cm_event.event) {
2985 case IB_CM_REQ_RECEIVED:
2986 ret = cm_req_handler(work);
2988 case IB_CM_MRA_RECEIVED:
2989 ret = cm_mra_handler(work);
2991 case IB_CM_REJ_RECEIVED:
2992 ret = cm_rej_handler(work);
2994 case IB_CM_REP_RECEIVED:
2995 ret = cm_rep_handler(work);
2997 case IB_CM_RTU_RECEIVED:
2998 ret = cm_rtu_handler(work);
3000 case IB_CM_USER_ESTABLISHED:
3001 ret = cm_establish_handler(work);
3003 case IB_CM_DREQ_RECEIVED:
3004 ret = cm_dreq_handler(work);
3006 case IB_CM_DREP_RECEIVED:
3007 ret = cm_drep_handler(work);
3009 case IB_CM_SIDR_REQ_RECEIVED:
3010 ret = cm_sidr_req_handler(work);
3012 case IB_CM_SIDR_REP_RECEIVED:
3013 ret = cm_sidr_rep_handler(work);
3015 case IB_CM_LAP_RECEIVED:
3016 ret = cm_lap_handler(work);
3018 case IB_CM_APR_RECEIVED:
3019 ret = cm_apr_handler(work);
3021 case IB_CM_TIMEWAIT_EXIT:
3022 ret = cm_timewait_handler(work);
3032 static int cm_establish(struct ib_cm_id *cm_id)
3034 struct cm_id_private *cm_id_priv;
3035 struct cm_work *work;
3036 unsigned long flags;
3039 work = kmalloc(sizeof *work, GFP_ATOMIC);
3043 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3044 spin_lock_irqsave(&cm_id_priv->lock, flags);
3045 switch (cm_id->state)
3047 case IB_CM_REP_SENT:
3048 case IB_CM_MRA_REP_RCVD:
3049 cm_id->state = IB_CM_ESTABLISHED;
3051 case IB_CM_ESTABLISHED:
3058 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3066 * The CM worker thread may try to destroy the cm_id before it
3067 * can execute this work item. To prevent potential deadlock,
3068 * we need to find the cm_id once we're in the context of the
3069 * worker thread, rather than holding a reference on it.
3071 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3072 work->local_id = cm_id->local_id;
3073 work->remote_id = cm_id->remote_id;
3074 work->mad_recv_wc = NULL;
3075 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3076 queue_delayed_work(cm.wq, &work->work, 0);
3081 static int cm_migrate(struct ib_cm_id *cm_id)
3083 struct cm_id_private *cm_id_priv;
3084 unsigned long flags;
3087 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3088 spin_lock_irqsave(&cm_id_priv->lock, flags);
3089 if (cm_id->state == IB_CM_ESTABLISHED &&
3090 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3091 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3092 cm_id->lap_state = IB_CM_LAP_IDLE;
3093 cm_id_priv->av = cm_id_priv->alt_av;
3096 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3101 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3106 case IB_EVENT_COMM_EST:
3107 ret = cm_establish(cm_id);
3109 case IB_EVENT_PATH_MIG:
3110 ret = cm_migrate(cm_id);
3117 EXPORT_SYMBOL(ib_cm_notify);
3119 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3120 struct ib_mad_recv_wc *mad_recv_wc)
3122 struct cm_work *work;
3123 enum ib_cm_event_type event;
3126 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3127 case CM_REQ_ATTR_ID:
3128 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3129 alt_local_lid != 0);
3130 event = IB_CM_REQ_RECEIVED;
3132 case CM_MRA_ATTR_ID:
3133 event = IB_CM_MRA_RECEIVED;
3135 case CM_REJ_ATTR_ID:
3136 event = IB_CM_REJ_RECEIVED;
3138 case CM_REP_ATTR_ID:
3139 event = IB_CM_REP_RECEIVED;
3141 case CM_RTU_ATTR_ID:
3142 event = IB_CM_RTU_RECEIVED;
3144 case CM_DREQ_ATTR_ID:
3145 event = IB_CM_DREQ_RECEIVED;
3147 case CM_DREP_ATTR_ID:
3148 event = IB_CM_DREP_RECEIVED;
3150 case CM_SIDR_REQ_ATTR_ID:
3151 event = IB_CM_SIDR_REQ_RECEIVED;
3153 case CM_SIDR_REP_ATTR_ID:
3154 event = IB_CM_SIDR_REP_RECEIVED;
3156 case CM_LAP_ATTR_ID:
3158 event = IB_CM_LAP_RECEIVED;
3160 case CM_APR_ATTR_ID:
3161 event = IB_CM_APR_RECEIVED;
3164 ib_free_recv_mad(mad_recv_wc);
3168 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3171 ib_free_recv_mad(mad_recv_wc);
3175 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3176 work->cm_event.event = event;
3177 work->mad_recv_wc = mad_recv_wc;
3178 work->port = (struct cm_port *)mad_agent->context;
3179 queue_delayed_work(cm.wq, &work->work, 0);
3182 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3183 struct ib_qp_attr *qp_attr,
3186 unsigned long flags;
3189 spin_lock_irqsave(&cm_id_priv->lock, flags);
3190 switch (cm_id_priv->id.state) {
3191 case IB_CM_REQ_SENT:
3192 case IB_CM_MRA_REQ_RCVD:
3193 case IB_CM_REQ_RCVD:
3194 case IB_CM_MRA_REQ_SENT:
3195 case IB_CM_REP_RCVD:
3196 case IB_CM_MRA_REP_SENT:
3197 case IB_CM_REP_SENT:
3198 case IB_CM_MRA_REP_RCVD:
3199 case IB_CM_ESTABLISHED:
3200 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3201 IB_QP_PKEY_INDEX | IB_QP_PORT;
3202 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3203 if (cm_id_priv->responder_resources)
3204 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3205 IB_ACCESS_REMOTE_ATOMIC;
3206 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3207 qp_attr->port_num = cm_id_priv->av.port->port_num;
3214 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3218 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3219 struct ib_qp_attr *qp_attr,
3222 unsigned long flags;
3225 spin_lock_irqsave(&cm_id_priv->lock, flags);
3226 switch (cm_id_priv->id.state) {
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_AV | IB_QP_PATH_MTU |
3235 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3236 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3237 qp_attr->path_mtu = cm_id_priv->path_mtu;
3238 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3239 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3240 if (cm_id_priv->qp_type == IB_QPT_RC) {
3241 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3242 IB_QP_MIN_RNR_TIMER;
3243 qp_attr->max_dest_rd_atomic =
3244 cm_id_priv->responder_resources;
3245 qp_attr->min_rnr_timer = 0;
3247 if (cm_id_priv->alt_av.ah_attr.dlid) {
3248 *qp_attr_mask |= IB_QP_ALT_PATH;
3249 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3250 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3251 qp_attr->alt_timeout =
3252 cm_id_priv->alt_av.packet_life_time + 1;
3253 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3261 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3265 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3266 struct ib_qp_attr *qp_attr,
3269 unsigned long flags;
3272 spin_lock_irqsave(&cm_id_priv->lock, flags);
3273 switch (cm_id_priv->id.state) {
3274 /* Allow transition to RTS before sending REP */
3275 case IB_CM_REQ_RCVD:
3276 case IB_CM_MRA_REQ_SENT:
3278 case IB_CM_REP_RCVD:
3279 case IB_CM_MRA_REP_SENT:
3280 case IB_CM_REP_SENT:
3281 case IB_CM_MRA_REP_RCVD:
3282 case IB_CM_ESTABLISHED:
3283 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3284 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3285 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3286 if (cm_id_priv->qp_type == IB_QPT_RC) {
3287 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3289 IB_QP_MAX_QP_RD_ATOMIC;
3291 cm_id_priv->av.packet_life_time + 1;
3292 qp_attr->retry_cnt = cm_id_priv->retry_count;
3293 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3294 qp_attr->max_rd_atomic =
3295 cm_id_priv->initiator_depth;
3297 if (cm_id_priv->alt_av.ah_attr.dlid) {
3298 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3299 qp_attr->path_mig_state = IB_MIG_REARM;
3302 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3303 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3304 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3305 qp_attr->alt_timeout =
3306 cm_id_priv->alt_av.packet_life_time + 1;
3307 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3308 qp_attr->path_mig_state = IB_MIG_REARM;
3316 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3320 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3321 struct ib_qp_attr *qp_attr,
3324 struct cm_id_private *cm_id_priv;
3327 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3328 switch (qp_attr->qp_state) {
3330 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3333 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3336 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3344 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3346 static void cm_add_one(struct ib_device *device)
3348 struct cm_device *cm_dev;
3349 struct cm_port *port;
3350 struct ib_mad_reg_req reg_req = {
3351 .mgmt_class = IB_MGMT_CLASS_CM,
3352 .mgmt_class_version = IB_CM_CLASS_VERSION
3354 struct ib_port_modify port_modify = {
3355 .set_port_cap_mask = IB_PORT_CM_SUP
3357 unsigned long flags;
3361 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3364 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3365 device->phys_port_cnt, GFP_KERNEL);
3369 cm_dev->device = device;
3371 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3372 for (i = 1; i <= device->phys_port_cnt; i++) {
3373 port = &cm_dev->port[i-1];
3374 port->cm_dev = cm_dev;
3376 port->mad_agent = ib_register_mad_agent(device, i,
3383 if (IS_ERR(port->mad_agent))
3386 ret = ib_modify_port(device, i, 0, &port_modify);
3390 ib_set_client_data(device, &cm_client, cm_dev);
3392 write_lock_irqsave(&cm.device_lock, flags);
3393 list_add_tail(&cm_dev->list, &cm.device_list);
3394 write_unlock_irqrestore(&cm.device_lock, flags);
3398 ib_unregister_mad_agent(port->mad_agent);
3400 port_modify.set_port_cap_mask = 0;
3401 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3403 port = &cm_dev->port[i-1];
3404 ib_modify_port(device, port->port_num, 0, &port_modify);
3405 ib_unregister_mad_agent(port->mad_agent);
3410 static void cm_remove_one(struct ib_device *device)
3412 struct cm_device *cm_dev;
3413 struct cm_port *port;
3414 struct ib_port_modify port_modify = {
3415 .clr_port_cap_mask = IB_PORT_CM_SUP
3417 unsigned long flags;
3420 cm_dev = ib_get_client_data(device, &cm_client);
3424 write_lock_irqsave(&cm.device_lock, flags);
3425 list_del(&cm_dev->list);
3426 write_unlock_irqrestore(&cm.device_lock, flags);
3428 for (i = 1; i <= device->phys_port_cnt; i++) {
3429 port = &cm_dev->port[i-1];
3430 ib_modify_port(device, port->port_num, 0, &port_modify);
3431 ib_unregister_mad_agent(port->mad_agent);
3436 static int __init ib_cm_init(void)
3440 memset(&cm, 0, sizeof cm);
3441 INIT_LIST_HEAD(&cm.device_list);
3442 rwlock_init(&cm.device_lock);
3443 spin_lock_init(&cm.lock);
3444 cm.listen_service_table = RB_ROOT;
3445 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3446 cm.remote_id_table = RB_ROOT;
3447 cm.remote_qp_table = RB_ROOT;
3448 cm.remote_sidr_table = RB_ROOT;
3449 idr_init(&cm.local_id_table);
3450 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3451 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3452 INIT_LIST_HEAD(&cm.timewait_list);
3454 cm.wq = create_workqueue("ib_cm");
3458 ret = ib_register_client(&cm_client);
3464 destroy_workqueue(cm.wq);
3468 static void __exit ib_cm_cleanup(void)
3470 struct cm_timewait_info *timewait_info, *tmp;
3472 spin_lock_irq(&cm.lock);
3473 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3474 cancel_delayed_work(&timewait_info->work.work);
3475 spin_unlock_irq(&cm.lock);
3477 destroy_workqueue(cm.wq);
3479 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3480 list_del(&timewait_info->list);
3481 kfree(timewait_info);
3484 ib_unregister_client(&cm_client);
3485 idr_destroy(&cm.local_id_table);
3488 module_init(ib_cm_init);
3489 module_exit(ib_cm_cleanup);