2 * Copyright (c) 2004-2007 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
36 #include <linux/completion.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.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/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
50 #include <linux/etherdevice.h>
52 #include <rdma/ib_cache.h>
53 #include <rdma/ib_cm.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("InfiniBand CM");
58 MODULE_LICENSE("Dual BSD/GPL");
60 static void cm_add_one(struct ib_device *device);
61 static void cm_remove_one(struct ib_device *device, void *client_data);
63 static struct ib_client cm_client = {
66 .remove = cm_remove_one
71 struct list_head device_list;
73 struct rb_root listen_service_table;
74 u64 listen_service_id;
75 /* struct rb_root peer_service_table; todo: fix peer to peer */
76 struct rb_root remote_qp_table;
77 struct rb_root remote_id_table;
78 struct rb_root remote_sidr_table;
79 struct idr local_id_table;
80 __be32 random_id_operand;
81 struct list_head timewait_list;
82 struct workqueue_struct *wq;
83 /* Sync on cm change port state */
84 spinlock_t state_lock;
87 /* Counter indexes ordered by attribute ID */
101 CM_ATTR_ID_OFFSET = 0x0010,
112 static char const counter_group_names[CM_COUNTER_GROUPS]
113 [sizeof("cm_rx_duplicates")] = {
114 "cm_tx_msgs", "cm_tx_retries",
115 "cm_rx_msgs", "cm_rx_duplicates"
118 struct cm_counter_group {
120 atomic_long_t counter[CM_ATTR_COUNT];
123 struct cm_counter_attribute {
124 struct attribute attr;
128 #define CM_COUNTER_ATTR(_name, _index) \
129 struct cm_counter_attribute cm_##_name##_counter_attr = { \
130 .attr = { .name = __stringify(_name), .mode = 0444 }, \
134 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
135 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
136 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
137 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
138 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
139 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
140 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
141 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
142 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
143 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
144 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
146 static struct attribute *cm_counter_default_attrs[] = {
147 &cm_req_counter_attr.attr,
148 &cm_mra_counter_attr.attr,
149 &cm_rej_counter_attr.attr,
150 &cm_rep_counter_attr.attr,
151 &cm_rtu_counter_attr.attr,
152 &cm_dreq_counter_attr.attr,
153 &cm_drep_counter_attr.attr,
154 &cm_sidr_req_counter_attr.attr,
155 &cm_sidr_rep_counter_attr.attr,
156 &cm_lap_counter_attr.attr,
157 &cm_apr_counter_attr.attr,
162 struct cm_device *cm_dev;
163 struct ib_mad_agent *mad_agent;
164 struct kobject port_obj;
166 struct list_head cm_priv_prim_list;
167 struct list_head cm_priv_altr_list;
168 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
172 struct list_head list;
173 struct ib_device *ib_device;
174 struct device *device;
177 struct cm_port *port[0];
181 struct cm_port *port;
183 struct ib_ah_attr ah_attr;
189 struct delayed_work work;
190 struct list_head list;
191 struct cm_port *port;
192 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
193 __be32 local_id; /* Established / timewait */
195 struct ib_cm_event cm_event;
196 struct ib_sa_path_rec path[0];
199 struct cm_timewait_info {
200 struct cm_work work; /* Must be first. */
201 struct list_head list;
202 struct rb_node remote_qp_node;
203 struct rb_node remote_id_node;
204 __be64 remote_ca_guid;
206 u8 inserted_remote_qp;
207 u8 inserted_remote_id;
210 struct cm_id_private {
213 struct rb_node service_node;
214 struct rb_node sidr_id_node;
215 spinlock_t lock; /* Do not acquire inside cm.lock */
216 struct completion comp;
218 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
219 * Protected by the cm.lock spinlock. */
220 int listen_sharecount;
222 struct ib_mad_send_buf *msg;
223 struct cm_timewait_info *timewait_info;
224 /* todo: use alternate port on send failure */
232 enum ib_qp_type qp_type;
236 enum ib_mtu path_mtu;
241 u8 responder_resources;
248 struct list_head prim_list;
249 struct list_head altr_list;
250 /* Indicates that the send port mad is registered and av is set */
251 int prim_send_port_not_ready;
252 int altr_send_port_not_ready;
254 struct list_head work_list;
258 static void cm_work_handler(struct work_struct *work);
260 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
262 if (atomic_dec_and_test(&cm_id_priv->refcount))
263 complete(&cm_id_priv->comp);
266 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
267 struct ib_mad_send_buf **msg)
269 struct ib_mad_agent *mad_agent;
270 struct ib_mad_send_buf *m;
273 unsigned long flags, flags2;
276 /* don't let the port to be released till the agent is down */
277 spin_lock_irqsave(&cm.state_lock, flags2);
278 spin_lock_irqsave(&cm.lock, flags);
279 if (!cm_id_priv->prim_send_port_not_ready)
280 av = &cm_id_priv->av;
281 else if (!cm_id_priv->altr_send_port_not_ready &&
282 (cm_id_priv->alt_av.port))
283 av = &cm_id_priv->alt_av;
285 pr_info("%s: not valid CM id\n", __func__);
287 spin_unlock_irqrestore(&cm.lock, flags);
290 spin_unlock_irqrestore(&cm.lock, flags);
291 /* Make sure the port haven't released the mad yet */
292 mad_agent = cm_id_priv->av.port->mad_agent;
294 pr_info("%s: not a valid MAD agent\n", __func__);
298 ah = ib_create_ah(mad_agent->qp->pd, &av->ah_attr);
304 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
306 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
308 IB_MGMT_BASE_VERSION);
315 /* Timeout set by caller if response is expected. */
317 m->retries = cm_id_priv->max_cm_retries;
319 atomic_inc(&cm_id_priv->refcount);
320 m->context[0] = cm_id_priv;
324 spin_unlock_irqrestore(&cm.state_lock, flags2);
328 static int cm_alloc_response_msg(struct cm_port *port,
329 struct ib_mad_recv_wc *mad_recv_wc,
330 struct ib_mad_send_buf **msg)
332 struct ib_mad_send_buf *m;
335 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
336 mad_recv_wc->recv_buf.grh, port->port_num);
340 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
341 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
343 IB_MGMT_BASE_VERSION);
353 static void cm_free_msg(struct ib_mad_send_buf *msg)
355 ib_destroy_ah(msg->ah);
357 cm_deref_id(msg->context[0]);
358 ib_free_send_mad(msg);
361 static void * cm_copy_private_data(const void *private_data,
366 if (!private_data || !private_data_len)
369 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
371 return ERR_PTR(-ENOMEM);
376 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
377 void *private_data, u8 private_data_len)
379 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
380 kfree(cm_id_priv->private_data);
382 cm_id_priv->private_data = private_data;
383 cm_id_priv->private_data_len = private_data_len;
386 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
387 struct ib_grh *grh, struct cm_av *av)
390 av->pkey_index = wc->pkey_index;
391 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
395 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av,
396 struct cm_id_private *cm_id_priv)
398 struct cm_device *cm_dev;
399 struct cm_port *port = NULL;
403 struct net_device *ndev = ib_get_ndev_from_path(path);
405 read_lock_irqsave(&cm.device_lock, flags);
406 list_for_each_entry(cm_dev, &cm.device_list, list) {
407 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
409 port = cm_dev->port[p-1];
413 read_unlock_irqrestore(&cm.device_lock, flags);
421 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
422 be16_to_cpu(path->pkey), &av->pkey_index);
427 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
429 av->timeout = path->packet_life_time + 1;
431 spin_lock_irqsave(&cm.lock, flags);
432 if (&cm_id_priv->av == av)
433 list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list);
434 else if (&cm_id_priv->alt_av == av)
435 list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list);
439 spin_unlock_irqrestore(&cm.lock, flags);
444 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
449 idr_preload(GFP_KERNEL);
450 spin_lock_irqsave(&cm.lock, flags);
452 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
454 spin_unlock_irqrestore(&cm.lock, flags);
457 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
458 return id < 0 ? id : 0;
461 static void cm_free_id(__be32 local_id)
463 spin_lock_irq(&cm.lock);
464 idr_remove(&cm.local_id_table,
465 (__force int) (local_id ^ cm.random_id_operand));
466 spin_unlock_irq(&cm.lock);
469 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
471 struct cm_id_private *cm_id_priv;
473 cm_id_priv = idr_find(&cm.local_id_table,
474 (__force int) (local_id ^ cm.random_id_operand));
476 if (cm_id_priv->id.remote_id == remote_id)
477 atomic_inc(&cm_id_priv->refcount);
485 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
487 struct cm_id_private *cm_id_priv;
489 spin_lock_irq(&cm.lock);
490 cm_id_priv = cm_get_id(local_id, remote_id);
491 spin_unlock_irq(&cm.lock);
497 * Trivial helpers to strip endian annotation and compare; the
498 * endianness doesn't actually matter since we just need a stable
499 * order for the RB tree.
501 static int be32_lt(__be32 a, __be32 b)
503 return (__force u32) a < (__force u32) b;
506 static int be32_gt(__be32 a, __be32 b)
508 return (__force u32) a > (__force u32) b;
511 static int be64_lt(__be64 a, __be64 b)
513 return (__force u64) a < (__force u64) b;
516 static int be64_gt(__be64 a, __be64 b)
518 return (__force u64) a > (__force u64) b;
521 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
523 struct rb_node **link = &cm.listen_service_table.rb_node;
524 struct rb_node *parent = NULL;
525 struct cm_id_private *cur_cm_id_priv;
526 __be64 service_id = cm_id_priv->id.service_id;
527 __be64 service_mask = cm_id_priv->id.service_mask;
531 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
533 if ((cur_cm_id_priv->id.service_mask & service_id) ==
534 (service_mask & cur_cm_id_priv->id.service_id) &&
535 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
536 return cur_cm_id_priv;
538 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
539 link = &(*link)->rb_left;
540 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
541 link = &(*link)->rb_right;
542 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
543 link = &(*link)->rb_left;
544 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
545 link = &(*link)->rb_right;
547 link = &(*link)->rb_right;
549 rb_link_node(&cm_id_priv->service_node, parent, link);
550 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
554 static struct cm_id_private * cm_find_listen(struct ib_device *device,
557 struct rb_node *node = cm.listen_service_table.rb_node;
558 struct cm_id_private *cm_id_priv;
561 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
562 if ((cm_id_priv->id.service_mask & service_id) ==
563 cm_id_priv->id.service_id &&
564 (cm_id_priv->id.device == device))
567 if (device < cm_id_priv->id.device)
568 node = node->rb_left;
569 else if (device > cm_id_priv->id.device)
570 node = node->rb_right;
571 else if (be64_lt(service_id, cm_id_priv->id.service_id))
572 node = node->rb_left;
573 else if (be64_gt(service_id, cm_id_priv->id.service_id))
574 node = node->rb_right;
576 node = node->rb_right;
581 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
584 struct rb_node **link = &cm.remote_id_table.rb_node;
585 struct rb_node *parent = NULL;
586 struct cm_timewait_info *cur_timewait_info;
587 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
588 __be32 remote_id = timewait_info->work.remote_id;
592 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
594 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
595 link = &(*link)->rb_left;
596 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
597 link = &(*link)->rb_right;
598 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
599 link = &(*link)->rb_left;
600 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
601 link = &(*link)->rb_right;
603 return cur_timewait_info;
605 timewait_info->inserted_remote_id = 1;
606 rb_link_node(&timewait_info->remote_id_node, parent, link);
607 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
611 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
614 struct rb_node *node = cm.remote_id_table.rb_node;
615 struct cm_timewait_info *timewait_info;
618 timewait_info = rb_entry(node, struct cm_timewait_info,
620 if (be32_lt(remote_id, timewait_info->work.remote_id))
621 node = node->rb_left;
622 else if (be32_gt(remote_id, timewait_info->work.remote_id))
623 node = node->rb_right;
624 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
625 node = node->rb_left;
626 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
627 node = node->rb_right;
629 return timewait_info;
634 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
637 struct rb_node **link = &cm.remote_qp_table.rb_node;
638 struct rb_node *parent = NULL;
639 struct cm_timewait_info *cur_timewait_info;
640 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
641 __be32 remote_qpn = timewait_info->remote_qpn;
645 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
647 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
648 link = &(*link)->rb_left;
649 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
650 link = &(*link)->rb_right;
651 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
652 link = &(*link)->rb_left;
653 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
654 link = &(*link)->rb_right;
656 return cur_timewait_info;
658 timewait_info->inserted_remote_qp = 1;
659 rb_link_node(&timewait_info->remote_qp_node, parent, link);
660 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
664 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
667 struct rb_node **link = &cm.remote_sidr_table.rb_node;
668 struct rb_node *parent = NULL;
669 struct cm_id_private *cur_cm_id_priv;
670 union ib_gid *port_gid = &cm_id_priv->av.dgid;
671 __be32 remote_id = cm_id_priv->id.remote_id;
675 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
677 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
678 link = &(*link)->rb_left;
679 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
680 link = &(*link)->rb_right;
683 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
686 link = &(*link)->rb_left;
688 link = &(*link)->rb_right;
690 return cur_cm_id_priv;
693 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
694 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
698 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
699 enum ib_cm_sidr_status status)
701 struct ib_cm_sidr_rep_param param;
703 memset(¶m, 0, sizeof param);
704 param.status = status;
705 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
708 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
709 ib_cm_handler cm_handler,
712 struct cm_id_private *cm_id_priv;
715 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
717 return ERR_PTR(-ENOMEM);
719 cm_id_priv->id.state = IB_CM_IDLE;
720 cm_id_priv->id.device = device;
721 cm_id_priv->id.cm_handler = cm_handler;
722 cm_id_priv->id.context = context;
723 cm_id_priv->id.remote_cm_qpn = 1;
724 ret = cm_alloc_id(cm_id_priv);
728 spin_lock_init(&cm_id_priv->lock);
729 init_completion(&cm_id_priv->comp);
730 INIT_LIST_HEAD(&cm_id_priv->work_list);
731 INIT_LIST_HEAD(&cm_id_priv->prim_list);
732 INIT_LIST_HEAD(&cm_id_priv->altr_list);
733 atomic_set(&cm_id_priv->work_count, -1);
734 atomic_set(&cm_id_priv->refcount, 1);
735 return &cm_id_priv->id;
739 return ERR_PTR(-ENOMEM);
741 EXPORT_SYMBOL(ib_create_cm_id);
743 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
745 struct cm_work *work;
747 if (list_empty(&cm_id_priv->work_list))
750 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
751 list_del(&work->list);
755 static void cm_free_work(struct cm_work *work)
757 if (work->mad_recv_wc)
758 ib_free_recv_mad(work->mad_recv_wc);
762 static inline int cm_convert_to_ms(int iba_time)
764 /* approximate conversion to ms from 4.096us x 2^iba_time */
765 return 1 << max(iba_time - 8, 0);
769 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
770 * Because of how ack_timeout is stored, adding one doubles the timeout.
771 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
772 * increment it (round up) only if the other is within 50%.
774 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
776 int ack_timeout = packet_life_time + 1;
778 if (ack_timeout >= ca_ack_delay)
779 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
781 ack_timeout = ca_ack_delay +
782 (ack_timeout >= (ca_ack_delay - 1));
784 return min(31, ack_timeout);
787 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
789 if (timewait_info->inserted_remote_id) {
790 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
791 timewait_info->inserted_remote_id = 0;
794 if (timewait_info->inserted_remote_qp) {
795 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
796 timewait_info->inserted_remote_qp = 0;
800 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
802 struct cm_timewait_info *timewait_info;
804 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
806 return ERR_PTR(-ENOMEM);
808 timewait_info->work.local_id = local_id;
809 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
810 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
811 return timewait_info;
814 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
818 struct cm_device *cm_dev;
820 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
824 spin_lock_irqsave(&cm.lock, flags);
825 cm_cleanup_timewait(cm_id_priv->timewait_info);
826 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
827 spin_unlock_irqrestore(&cm.lock, flags);
830 * The cm_id could be destroyed by the user before we exit timewait.
831 * To protect against this, we search for the cm_id after exiting
832 * timewait before notifying the user that we've exited timewait.
834 cm_id_priv->id.state = IB_CM_TIMEWAIT;
835 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
837 /* Check if the device started its remove_one */
838 spin_lock_irqsave(&cm.lock, flags);
839 if (!cm_dev->going_down)
840 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
841 msecs_to_jiffies(wait_time));
842 spin_unlock_irqrestore(&cm.lock, flags);
844 cm_id_priv->timewait_info = NULL;
847 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
851 cm_id_priv->id.state = IB_CM_IDLE;
852 if (cm_id_priv->timewait_info) {
853 spin_lock_irqsave(&cm.lock, flags);
854 cm_cleanup_timewait(cm_id_priv->timewait_info);
855 spin_unlock_irqrestore(&cm.lock, flags);
856 kfree(cm_id_priv->timewait_info);
857 cm_id_priv->timewait_info = NULL;
861 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
863 struct cm_id_private *cm_id_priv;
864 struct cm_work *work;
866 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
868 spin_lock_irq(&cm_id_priv->lock);
869 switch (cm_id->state) {
871 spin_unlock_irq(&cm_id_priv->lock);
873 spin_lock_irq(&cm.lock);
874 if (--cm_id_priv->listen_sharecount > 0) {
875 /* The id is still shared. */
876 cm_deref_id(cm_id_priv);
877 spin_unlock_irq(&cm.lock);
880 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
881 spin_unlock_irq(&cm.lock);
883 case IB_CM_SIDR_REQ_SENT:
884 cm_id->state = IB_CM_IDLE;
885 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
886 spin_unlock_irq(&cm_id_priv->lock);
888 case IB_CM_SIDR_REQ_RCVD:
889 spin_unlock_irq(&cm_id_priv->lock);
890 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
891 spin_lock_irq(&cm.lock);
892 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
893 rb_erase(&cm_id_priv->sidr_id_node,
894 &cm.remote_sidr_table);
895 spin_unlock_irq(&cm.lock);
898 case IB_CM_MRA_REQ_RCVD:
899 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
900 spin_unlock_irq(&cm_id_priv->lock);
901 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
902 &cm_id_priv->id.device->node_guid,
903 sizeof cm_id_priv->id.device->node_guid,
907 if (err == -ENOMEM) {
908 /* Do not reject to allow future retries. */
909 cm_reset_to_idle(cm_id_priv);
910 spin_unlock_irq(&cm_id_priv->lock);
912 spin_unlock_irq(&cm_id_priv->lock);
913 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
918 case IB_CM_MRA_REP_RCVD:
919 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
921 case IB_CM_MRA_REQ_SENT:
923 case IB_CM_MRA_REP_SENT:
924 spin_unlock_irq(&cm_id_priv->lock);
925 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
928 case IB_CM_ESTABLISHED:
929 spin_unlock_irq(&cm_id_priv->lock);
930 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
932 ib_send_cm_dreq(cm_id, NULL, 0);
934 case IB_CM_DREQ_SENT:
935 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
936 cm_enter_timewait(cm_id_priv);
937 spin_unlock_irq(&cm_id_priv->lock);
939 case IB_CM_DREQ_RCVD:
940 spin_unlock_irq(&cm_id_priv->lock);
941 ib_send_cm_drep(cm_id, NULL, 0);
944 spin_unlock_irq(&cm_id_priv->lock);
948 spin_lock_irq(&cm.lock);
949 if (!list_empty(&cm_id_priv->altr_list) &&
950 (!cm_id_priv->altr_send_port_not_ready))
951 list_del(&cm_id_priv->altr_list);
952 if (!list_empty(&cm_id_priv->prim_list) &&
953 (!cm_id_priv->prim_send_port_not_ready))
954 list_del(&cm_id_priv->prim_list);
955 spin_unlock_irq(&cm.lock);
957 cm_free_id(cm_id->local_id);
958 cm_deref_id(cm_id_priv);
959 wait_for_completion(&cm_id_priv->comp);
960 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
962 kfree(cm_id_priv->private_data);
966 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
968 cm_destroy_id(cm_id, 0);
970 EXPORT_SYMBOL(ib_destroy_cm_id);
973 * __ib_cm_listen - Initiates listening on the specified service ID for
974 * connection and service ID resolution requests.
975 * @cm_id: Connection identifier associated with the listen request.
976 * @service_id: Service identifier matched against incoming connection
977 * and service ID resolution requests. The service ID should be specified
978 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
979 * assign a service ID to the caller.
980 * @service_mask: Mask applied to service ID used to listen across a
981 * range of service IDs. If set to 0, the service ID is matched
982 * exactly. This parameter is ignored if %service_id is set to
983 * IB_CM_ASSIGN_SERVICE_ID.
985 static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
988 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
991 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
992 service_id &= service_mask;
993 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
994 (service_id != IB_CM_ASSIGN_SERVICE_ID))
997 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
998 if (cm_id->state != IB_CM_IDLE)
1001 cm_id->state = IB_CM_LISTEN;
1002 ++cm_id_priv->listen_sharecount;
1004 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
1005 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
1006 cm_id->service_mask = ~cpu_to_be64(0);
1008 cm_id->service_id = service_id;
1009 cm_id->service_mask = service_mask;
1011 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
1013 if (cur_cm_id_priv) {
1014 cm_id->state = IB_CM_IDLE;
1015 --cm_id_priv->listen_sharecount;
1021 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
1023 unsigned long flags;
1026 spin_lock_irqsave(&cm.lock, flags);
1027 ret = __ib_cm_listen(cm_id, service_id, service_mask);
1028 spin_unlock_irqrestore(&cm.lock, flags);
1032 EXPORT_SYMBOL(ib_cm_listen);
1035 * Create a new listening ib_cm_id and listen on the given service ID.
1037 * If there's an existing ID listening on that same device and service ID,
1040 * @device: Device associated with the cm_id. All related communication will
1041 * be associated with the specified device.
1042 * @cm_handler: Callback invoked to notify the user of CM events.
1043 * @service_id: Service identifier matched against incoming connection
1044 * and service ID resolution requests. The service ID should be specified
1045 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1046 * assign a service ID to the caller.
1048 * Callers should call ib_destroy_cm_id when done with the listener ID.
1050 struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
1051 ib_cm_handler cm_handler,
1054 struct cm_id_private *cm_id_priv;
1055 struct ib_cm_id *cm_id;
1056 unsigned long flags;
1059 /* Create an ID in advance, since the creation may sleep */
1060 cm_id = ib_create_cm_id(device, cm_handler, NULL);
1064 spin_lock_irqsave(&cm.lock, flags);
1066 if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1069 /* Find an existing ID */
1070 cm_id_priv = cm_find_listen(device, service_id);
1072 if (cm_id->cm_handler != cm_handler || cm_id->context) {
1073 /* Sharing an ib_cm_id with different handlers is not
1075 spin_unlock_irqrestore(&cm.lock, flags);
1076 ib_destroy_cm_id(cm_id);
1077 return ERR_PTR(-EINVAL);
1079 atomic_inc(&cm_id_priv->refcount);
1080 ++cm_id_priv->listen_sharecount;
1081 spin_unlock_irqrestore(&cm.lock, flags);
1083 ib_destroy_cm_id(cm_id);
1084 cm_id = &cm_id_priv->id;
1089 /* Use newly created ID */
1090 err = __ib_cm_listen(cm_id, service_id, 0);
1092 spin_unlock_irqrestore(&cm.lock, flags);
1095 ib_destroy_cm_id(cm_id);
1096 return ERR_PTR(err);
1100 EXPORT_SYMBOL(ib_cm_insert_listen);
1102 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1103 enum cm_msg_sequence msg_seq)
1105 u64 hi_tid, low_tid;
1107 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1108 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1110 return cpu_to_be64(hi_tid | low_tid);
1113 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1114 __be16 attr_id, __be64 tid)
1116 hdr->base_version = IB_MGMT_BASE_VERSION;
1117 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1118 hdr->class_version = IB_CM_CLASS_VERSION;
1119 hdr->method = IB_MGMT_METHOD_SEND;
1120 hdr->attr_id = attr_id;
1124 static void cm_format_req(struct cm_req_msg *req_msg,
1125 struct cm_id_private *cm_id_priv,
1126 struct ib_cm_req_param *param)
1128 struct ib_sa_path_rec *pri_path = param->primary_path;
1129 struct ib_sa_path_rec *alt_path = param->alternate_path;
1131 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1132 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1134 req_msg->local_comm_id = cm_id_priv->id.local_id;
1135 req_msg->service_id = param->service_id;
1136 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1137 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1138 cm_req_set_init_depth(req_msg, param->initiator_depth);
1139 cm_req_set_remote_resp_timeout(req_msg,
1140 param->remote_cm_response_timeout);
1141 cm_req_set_qp_type(req_msg, param->qp_type);
1142 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1143 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1144 cm_req_set_local_resp_timeout(req_msg,
1145 param->local_cm_response_timeout);
1146 req_msg->pkey = param->primary_path->pkey;
1147 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1148 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1150 if (param->qp_type != IB_QPT_XRC_INI) {
1151 cm_req_set_resp_res(req_msg, param->responder_resources);
1152 cm_req_set_retry_count(req_msg, param->retry_count);
1153 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1154 cm_req_set_srq(req_msg, param->srq);
1157 if (pri_path->hop_limit <= 1) {
1158 req_msg->primary_local_lid = pri_path->slid;
1159 req_msg->primary_remote_lid = pri_path->dlid;
1161 /* Work-around until there's a way to obtain remote LID info */
1162 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1163 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1165 req_msg->primary_local_gid = pri_path->sgid;
1166 req_msg->primary_remote_gid = pri_path->dgid;
1167 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1168 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1169 req_msg->primary_traffic_class = pri_path->traffic_class;
1170 req_msg->primary_hop_limit = pri_path->hop_limit;
1171 cm_req_set_primary_sl(req_msg, pri_path->sl);
1172 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1173 cm_req_set_primary_local_ack_timeout(req_msg,
1174 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1175 pri_path->packet_life_time));
1178 if (alt_path->hop_limit <= 1) {
1179 req_msg->alt_local_lid = alt_path->slid;
1180 req_msg->alt_remote_lid = alt_path->dlid;
1182 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1183 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1185 req_msg->alt_local_gid = alt_path->sgid;
1186 req_msg->alt_remote_gid = alt_path->dgid;
1187 cm_req_set_alt_flow_label(req_msg,
1188 alt_path->flow_label);
1189 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1190 req_msg->alt_traffic_class = alt_path->traffic_class;
1191 req_msg->alt_hop_limit = alt_path->hop_limit;
1192 cm_req_set_alt_sl(req_msg, alt_path->sl);
1193 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1194 cm_req_set_alt_local_ack_timeout(req_msg,
1195 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1196 alt_path->packet_life_time));
1199 if (param->private_data && param->private_data_len)
1200 memcpy(req_msg->private_data, param->private_data,
1201 param->private_data_len);
1204 static int cm_validate_req_param(struct ib_cm_req_param *param)
1206 /* peer-to-peer not supported */
1207 if (param->peer_to_peer)
1210 if (!param->primary_path)
1213 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1214 param->qp_type != IB_QPT_XRC_INI)
1217 if (param->private_data &&
1218 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1221 if (param->alternate_path &&
1222 (param->alternate_path->pkey != param->primary_path->pkey ||
1223 param->alternate_path->mtu != param->primary_path->mtu))
1229 int ib_send_cm_req(struct ib_cm_id *cm_id,
1230 struct ib_cm_req_param *param)
1232 struct cm_id_private *cm_id_priv;
1233 struct cm_req_msg *req_msg;
1234 unsigned long flags;
1237 ret = cm_validate_req_param(param);
1241 /* Verify that we're not in timewait. */
1242 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1243 spin_lock_irqsave(&cm_id_priv->lock, flags);
1244 if (cm_id->state != IB_CM_IDLE) {
1245 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1249 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1251 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1253 if (IS_ERR(cm_id_priv->timewait_info)) {
1254 ret = PTR_ERR(cm_id_priv->timewait_info);
1258 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av,
1262 if (param->alternate_path) {
1263 ret = cm_init_av_by_path(param->alternate_path,
1264 &cm_id_priv->alt_av, cm_id_priv);
1268 cm_id->service_id = param->service_id;
1269 cm_id->service_mask = ~cpu_to_be64(0);
1270 cm_id_priv->timeout_ms = cm_convert_to_ms(
1271 param->primary_path->packet_life_time) * 2 +
1273 param->remote_cm_response_timeout);
1274 cm_id_priv->max_cm_retries = param->max_cm_retries;
1275 cm_id_priv->initiator_depth = param->initiator_depth;
1276 cm_id_priv->responder_resources = param->responder_resources;
1277 cm_id_priv->retry_count = param->retry_count;
1278 cm_id_priv->path_mtu = param->primary_path->mtu;
1279 cm_id_priv->pkey = param->primary_path->pkey;
1280 cm_id_priv->qp_type = param->qp_type;
1282 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1286 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1287 cm_format_req(req_msg, cm_id_priv, param);
1288 cm_id_priv->tid = req_msg->hdr.tid;
1289 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1290 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1292 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1293 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1295 spin_lock_irqsave(&cm_id_priv->lock, flags);
1296 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1298 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1301 BUG_ON(cm_id->state != IB_CM_IDLE);
1302 cm_id->state = IB_CM_REQ_SENT;
1303 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1306 error2: cm_free_msg(cm_id_priv->msg);
1307 error1: kfree(cm_id_priv->timewait_info);
1310 EXPORT_SYMBOL(ib_send_cm_req);
1312 static int cm_issue_rej(struct cm_port *port,
1313 struct ib_mad_recv_wc *mad_recv_wc,
1314 enum ib_cm_rej_reason reason,
1315 enum cm_msg_response msg_rejected,
1316 void *ari, u8 ari_length)
1318 struct ib_mad_send_buf *msg = NULL;
1319 struct cm_rej_msg *rej_msg, *rcv_msg;
1322 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1326 /* We just need common CM header information. Cast to any message. */
1327 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1328 rej_msg = (struct cm_rej_msg *) msg->mad;
1330 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1331 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1332 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1333 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1334 rej_msg->reason = cpu_to_be16(reason);
1336 if (ari && ari_length) {
1337 cm_rej_set_reject_info_len(rej_msg, ari_length);
1338 memcpy(rej_msg->ari, ari, ari_length);
1341 ret = ib_post_send_mad(msg, NULL);
1348 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1349 __be32 local_qpn, __be32 remote_qpn)
1351 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1352 ((local_ca_guid == remote_ca_guid) &&
1353 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1356 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1357 struct ib_sa_path_rec *primary_path,
1358 struct ib_sa_path_rec *alt_path)
1360 memset(primary_path, 0, sizeof *primary_path);
1361 primary_path->dgid = req_msg->primary_local_gid;
1362 primary_path->sgid = req_msg->primary_remote_gid;
1363 primary_path->dlid = req_msg->primary_local_lid;
1364 primary_path->slid = req_msg->primary_remote_lid;
1365 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1366 primary_path->hop_limit = req_msg->primary_hop_limit;
1367 primary_path->traffic_class = req_msg->primary_traffic_class;
1368 primary_path->reversible = 1;
1369 primary_path->pkey = req_msg->pkey;
1370 primary_path->sl = cm_req_get_primary_sl(req_msg);
1371 primary_path->mtu_selector = IB_SA_EQ;
1372 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1373 primary_path->rate_selector = IB_SA_EQ;
1374 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1375 primary_path->packet_life_time_selector = IB_SA_EQ;
1376 primary_path->packet_life_time =
1377 cm_req_get_primary_local_ack_timeout(req_msg);
1378 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1379 primary_path->service_id = req_msg->service_id;
1381 if (req_msg->alt_local_lid) {
1382 memset(alt_path, 0, sizeof *alt_path);
1383 alt_path->dgid = req_msg->alt_local_gid;
1384 alt_path->sgid = req_msg->alt_remote_gid;
1385 alt_path->dlid = req_msg->alt_local_lid;
1386 alt_path->slid = req_msg->alt_remote_lid;
1387 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1388 alt_path->hop_limit = req_msg->alt_hop_limit;
1389 alt_path->traffic_class = req_msg->alt_traffic_class;
1390 alt_path->reversible = 1;
1391 alt_path->pkey = req_msg->pkey;
1392 alt_path->sl = cm_req_get_alt_sl(req_msg);
1393 alt_path->mtu_selector = IB_SA_EQ;
1394 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1395 alt_path->rate_selector = IB_SA_EQ;
1396 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1397 alt_path->packet_life_time_selector = IB_SA_EQ;
1398 alt_path->packet_life_time =
1399 cm_req_get_alt_local_ack_timeout(req_msg);
1400 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1401 alt_path->service_id = req_msg->service_id;
1405 static u16 cm_get_bth_pkey(struct cm_work *work)
1407 struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1408 u8 port_num = work->port->port_num;
1409 u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1413 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1415 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1416 port_num, pkey_index, ret);
1423 static void cm_format_req_event(struct cm_work *work,
1424 struct cm_id_private *cm_id_priv,
1425 struct ib_cm_id *listen_id)
1427 struct cm_req_msg *req_msg;
1428 struct ib_cm_req_event_param *param;
1430 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1431 param = &work->cm_event.param.req_rcvd;
1432 param->listen_id = listen_id;
1433 param->bth_pkey = cm_get_bth_pkey(work);
1434 param->port = cm_id_priv->av.port->port_num;
1435 param->primary_path = &work->path[0];
1436 if (req_msg->alt_local_lid)
1437 param->alternate_path = &work->path[1];
1439 param->alternate_path = NULL;
1440 param->remote_ca_guid = req_msg->local_ca_guid;
1441 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1442 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1443 param->qp_type = cm_req_get_qp_type(req_msg);
1444 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1445 param->responder_resources = cm_req_get_init_depth(req_msg);
1446 param->initiator_depth = cm_req_get_resp_res(req_msg);
1447 param->local_cm_response_timeout =
1448 cm_req_get_remote_resp_timeout(req_msg);
1449 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1450 param->remote_cm_response_timeout =
1451 cm_req_get_local_resp_timeout(req_msg);
1452 param->retry_count = cm_req_get_retry_count(req_msg);
1453 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1454 param->srq = cm_req_get_srq(req_msg);
1455 work->cm_event.private_data = &req_msg->private_data;
1458 static void cm_process_work(struct cm_id_private *cm_id_priv,
1459 struct cm_work *work)
1463 /* We will typically only have the current event to report. */
1464 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1467 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1468 spin_lock_irq(&cm_id_priv->lock);
1469 work = cm_dequeue_work(cm_id_priv);
1470 spin_unlock_irq(&cm_id_priv->lock);
1472 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1476 cm_deref_id(cm_id_priv);
1478 cm_destroy_id(&cm_id_priv->id, ret);
1481 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1482 struct cm_id_private *cm_id_priv,
1483 enum cm_msg_response msg_mraed, u8 service_timeout,
1484 const void *private_data, u8 private_data_len)
1486 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1487 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1488 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1489 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1490 cm_mra_set_service_timeout(mra_msg, service_timeout);
1492 if (private_data && private_data_len)
1493 memcpy(mra_msg->private_data, private_data, private_data_len);
1496 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1497 struct cm_id_private *cm_id_priv,
1498 enum ib_cm_rej_reason reason,
1501 const void *private_data,
1502 u8 private_data_len)
1504 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1505 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1507 switch(cm_id_priv->id.state) {
1508 case IB_CM_REQ_RCVD:
1509 rej_msg->local_comm_id = 0;
1510 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1512 case IB_CM_MRA_REQ_SENT:
1513 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1514 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1516 case IB_CM_REP_RCVD:
1517 case IB_CM_MRA_REP_SENT:
1518 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1519 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1522 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1523 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1527 rej_msg->reason = cpu_to_be16(reason);
1528 if (ari && ari_length) {
1529 cm_rej_set_reject_info_len(rej_msg, ari_length);
1530 memcpy(rej_msg->ari, ari, ari_length);
1533 if (private_data && private_data_len)
1534 memcpy(rej_msg->private_data, private_data, private_data_len);
1537 static void cm_dup_req_handler(struct cm_work *work,
1538 struct cm_id_private *cm_id_priv)
1540 struct ib_mad_send_buf *msg = NULL;
1543 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1544 counter[CM_REQ_COUNTER]);
1546 /* Quick state check to discard duplicate REQs. */
1547 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1550 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1554 spin_lock_irq(&cm_id_priv->lock);
1555 switch (cm_id_priv->id.state) {
1556 case IB_CM_MRA_REQ_SENT:
1557 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1558 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1559 cm_id_priv->private_data,
1560 cm_id_priv->private_data_len);
1562 case IB_CM_TIMEWAIT:
1563 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1564 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1569 spin_unlock_irq(&cm_id_priv->lock);
1571 ret = ib_post_send_mad(msg, NULL);
1576 unlock: spin_unlock_irq(&cm_id_priv->lock);
1577 free: cm_free_msg(msg);
1580 static struct cm_id_private * cm_match_req(struct cm_work *work,
1581 struct cm_id_private *cm_id_priv)
1583 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1584 struct cm_timewait_info *timewait_info;
1585 struct cm_req_msg *req_msg;
1587 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1589 /* Check for possible duplicate REQ. */
1590 spin_lock_irq(&cm.lock);
1591 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1592 if (timewait_info) {
1593 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1594 timewait_info->work.remote_id);
1595 spin_unlock_irq(&cm.lock);
1596 if (cur_cm_id_priv) {
1597 cm_dup_req_handler(work, cur_cm_id_priv);
1598 cm_deref_id(cur_cm_id_priv);
1603 /* Check for stale connections. */
1604 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1605 if (timewait_info) {
1606 cm_cleanup_timewait(cm_id_priv->timewait_info);
1607 spin_unlock_irq(&cm.lock);
1608 cm_issue_rej(work->port, work->mad_recv_wc,
1609 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1614 /* Find matching listen request. */
1615 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1616 req_msg->service_id);
1617 if (!listen_cm_id_priv) {
1618 cm_cleanup_timewait(cm_id_priv->timewait_info);
1619 spin_unlock_irq(&cm.lock);
1620 cm_issue_rej(work->port, work->mad_recv_wc,
1621 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1625 atomic_inc(&listen_cm_id_priv->refcount);
1626 atomic_inc(&cm_id_priv->refcount);
1627 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1628 atomic_inc(&cm_id_priv->work_count);
1629 spin_unlock_irq(&cm.lock);
1631 return listen_cm_id_priv;
1635 * Work-around for inter-subnet connections. If the LIDs are permissive,
1636 * we need to override the LID/SL data in the REQ with the LID information
1637 * in the work completion.
1639 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1641 if (!cm_req_get_primary_subnet_local(req_msg)) {
1642 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1643 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1644 cm_req_set_primary_sl(req_msg, wc->sl);
1647 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1648 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1651 if (!cm_req_get_alt_subnet_local(req_msg)) {
1652 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1653 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1654 cm_req_set_alt_sl(req_msg, wc->sl);
1657 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1658 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1662 static int cm_req_handler(struct cm_work *work)
1664 struct ib_cm_id *cm_id;
1665 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1666 struct cm_req_msg *req_msg;
1669 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1671 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1673 return PTR_ERR(cm_id);
1675 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1676 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1677 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1678 work->mad_recv_wc->recv_buf.grh,
1680 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1682 if (IS_ERR(cm_id_priv->timewait_info)) {
1683 ret = PTR_ERR(cm_id_priv->timewait_info);
1686 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1687 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1688 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1690 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1691 if (!listen_cm_id_priv) {
1693 kfree(cm_id_priv->timewait_info);
1697 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1698 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1699 cm_id_priv->id.service_id = req_msg->service_id;
1700 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1702 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1703 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1705 memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
1706 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av,
1709 ib_get_cached_gid(work->port->cm_dev->ib_device,
1710 work->port->port_num, 0, &work->path[0].sgid,
1712 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1713 &work->path[0].sgid, sizeof work->path[0].sgid,
1717 if (req_msg->alt_local_lid) {
1718 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av,
1721 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1722 &work->path[0].sgid,
1723 sizeof work->path[0].sgid, NULL, 0);
1727 cm_id_priv->tid = req_msg->hdr.tid;
1728 cm_id_priv->timeout_ms = cm_convert_to_ms(
1729 cm_req_get_local_resp_timeout(req_msg));
1730 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1731 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1732 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1733 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1734 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1735 cm_id_priv->pkey = req_msg->pkey;
1736 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1737 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1738 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1739 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1741 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1742 cm_process_work(cm_id_priv, work);
1743 cm_deref_id(listen_cm_id_priv);
1747 atomic_dec(&cm_id_priv->refcount);
1748 cm_deref_id(listen_cm_id_priv);
1750 ib_destroy_cm_id(cm_id);
1754 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1755 struct cm_id_private *cm_id_priv,
1756 struct ib_cm_rep_param *param)
1758 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1759 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1760 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1761 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1762 rep_msg->resp_resources = param->responder_resources;
1763 cm_rep_set_target_ack_delay(rep_msg,
1764 cm_id_priv->av.port->cm_dev->ack_delay);
1765 cm_rep_set_failover(rep_msg, param->failover_accepted);
1766 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1767 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1769 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1770 rep_msg->initiator_depth = param->initiator_depth;
1771 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1772 cm_rep_set_srq(rep_msg, param->srq);
1773 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1775 cm_rep_set_srq(rep_msg, 1);
1776 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
1779 if (param->private_data && param->private_data_len)
1780 memcpy(rep_msg->private_data, param->private_data,
1781 param->private_data_len);
1784 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1785 struct ib_cm_rep_param *param)
1787 struct cm_id_private *cm_id_priv;
1788 struct ib_mad_send_buf *msg;
1789 struct cm_rep_msg *rep_msg;
1790 unsigned long flags;
1793 if (param->private_data &&
1794 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1797 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1798 spin_lock_irqsave(&cm_id_priv->lock, flags);
1799 if (cm_id->state != IB_CM_REQ_RCVD &&
1800 cm_id->state != IB_CM_MRA_REQ_SENT) {
1805 ret = cm_alloc_msg(cm_id_priv, &msg);
1809 rep_msg = (struct cm_rep_msg *) msg->mad;
1810 cm_format_rep(rep_msg, cm_id_priv, param);
1811 msg->timeout_ms = cm_id_priv->timeout_ms;
1812 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1814 ret = ib_post_send_mad(msg, NULL);
1816 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1821 cm_id->state = IB_CM_REP_SENT;
1822 cm_id_priv->msg = msg;
1823 cm_id_priv->initiator_depth = param->initiator_depth;
1824 cm_id_priv->responder_resources = param->responder_resources;
1825 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1826 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
1828 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1831 EXPORT_SYMBOL(ib_send_cm_rep);
1833 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1834 struct cm_id_private *cm_id_priv,
1835 const void *private_data,
1836 u8 private_data_len)
1838 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1839 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1840 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1842 if (private_data && private_data_len)
1843 memcpy(rtu_msg->private_data, private_data, private_data_len);
1846 int ib_send_cm_rtu(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_RTU_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_REP_RCVD &&
1866 cm_id->state != IB_CM_MRA_REP_SENT) {
1871 ret = cm_alloc_msg(cm_id_priv, &msg);
1875 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1876 private_data, private_data_len);
1878 ret = ib_post_send_mad(msg, NULL);
1880 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1886 cm_id->state = IB_CM_ESTABLISHED;
1887 cm_set_private_data(cm_id_priv, data, private_data_len);
1888 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1891 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1895 EXPORT_SYMBOL(ib_send_cm_rtu);
1897 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
1899 struct cm_rep_msg *rep_msg;
1900 struct ib_cm_rep_event_param *param;
1902 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1903 param = &work->cm_event.param.rep_rcvd;
1904 param->remote_ca_guid = rep_msg->local_ca_guid;
1905 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1906 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
1907 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1908 param->responder_resources = rep_msg->initiator_depth;
1909 param->initiator_depth = rep_msg->resp_resources;
1910 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1911 param->failover_accepted = cm_rep_get_failover(rep_msg);
1912 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1913 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1914 param->srq = cm_rep_get_srq(rep_msg);
1915 work->cm_event.private_data = &rep_msg->private_data;
1918 static void cm_dup_rep_handler(struct cm_work *work)
1920 struct cm_id_private *cm_id_priv;
1921 struct cm_rep_msg *rep_msg;
1922 struct ib_mad_send_buf *msg = NULL;
1925 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1926 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1927 rep_msg->local_comm_id);
1931 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1932 counter[CM_REP_COUNTER]);
1933 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1937 spin_lock_irq(&cm_id_priv->lock);
1938 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1939 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1940 cm_id_priv->private_data,
1941 cm_id_priv->private_data_len);
1942 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1943 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1944 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1945 cm_id_priv->private_data,
1946 cm_id_priv->private_data_len);
1949 spin_unlock_irq(&cm_id_priv->lock);
1951 ret = ib_post_send_mad(msg, NULL);
1956 unlock: spin_unlock_irq(&cm_id_priv->lock);
1957 free: cm_free_msg(msg);
1958 deref: cm_deref_id(cm_id_priv);
1961 static int cm_rep_handler(struct cm_work *work)
1963 struct cm_id_private *cm_id_priv;
1964 struct cm_rep_msg *rep_msg;
1967 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1968 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1970 cm_dup_rep_handler(work);
1974 cm_format_rep_event(work, cm_id_priv->qp_type);
1976 spin_lock_irq(&cm_id_priv->lock);
1977 switch (cm_id_priv->id.state) {
1978 case IB_CM_REQ_SENT:
1979 case IB_CM_MRA_REQ_RCVD:
1982 spin_unlock_irq(&cm_id_priv->lock);
1987 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1988 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1989 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
1991 spin_lock(&cm.lock);
1992 /* Check for duplicate REP. */
1993 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1994 spin_unlock(&cm.lock);
1995 spin_unlock_irq(&cm_id_priv->lock);
1999 /* Check for a stale connection. */
2000 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
2001 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
2002 &cm.remote_id_table);
2003 cm_id_priv->timewait_info->inserted_remote_id = 0;
2004 spin_unlock(&cm.lock);
2005 spin_unlock_irq(&cm_id_priv->lock);
2006 cm_issue_rej(work->port, work->mad_recv_wc,
2007 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2012 spin_unlock(&cm.lock);
2014 cm_id_priv->id.state = IB_CM_REP_RCVD;
2015 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
2016 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2017 cm_id_priv->initiator_depth = rep_msg->resp_resources;
2018 cm_id_priv->responder_resources = rep_msg->initiator_depth;
2019 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
2020 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
2021 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
2022 cm_id_priv->av.timeout =
2023 cm_ack_timeout(cm_id_priv->target_ack_delay,
2024 cm_id_priv->av.timeout - 1);
2025 cm_id_priv->alt_av.timeout =
2026 cm_ack_timeout(cm_id_priv->target_ack_delay,
2027 cm_id_priv->alt_av.timeout - 1);
2029 /* todo: handle peer_to_peer */
2031 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2032 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2034 list_add_tail(&work->list, &cm_id_priv->work_list);
2035 spin_unlock_irq(&cm_id_priv->lock);
2038 cm_process_work(cm_id_priv, work);
2040 cm_deref_id(cm_id_priv);
2044 cm_deref_id(cm_id_priv);
2048 static int cm_establish_handler(struct cm_work *work)
2050 struct cm_id_private *cm_id_priv;
2053 /* See comment in cm_establish about lookup. */
2054 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2058 spin_lock_irq(&cm_id_priv->lock);
2059 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2060 spin_unlock_irq(&cm_id_priv->lock);
2064 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2065 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2067 list_add_tail(&work->list, &cm_id_priv->work_list);
2068 spin_unlock_irq(&cm_id_priv->lock);
2071 cm_process_work(cm_id_priv, work);
2073 cm_deref_id(cm_id_priv);
2076 cm_deref_id(cm_id_priv);
2080 static int cm_rtu_handler(struct cm_work *work)
2082 struct cm_id_private *cm_id_priv;
2083 struct cm_rtu_msg *rtu_msg;
2086 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2087 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
2088 rtu_msg->local_comm_id);
2092 work->cm_event.private_data = &rtu_msg->private_data;
2094 spin_lock_irq(&cm_id_priv->lock);
2095 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2096 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2097 spin_unlock_irq(&cm_id_priv->lock);
2098 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2099 counter[CM_RTU_COUNTER]);
2102 cm_id_priv->id.state = IB_CM_ESTABLISHED;
2104 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2105 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2107 list_add_tail(&work->list, &cm_id_priv->work_list);
2108 spin_unlock_irq(&cm_id_priv->lock);
2111 cm_process_work(cm_id_priv, work);
2113 cm_deref_id(cm_id_priv);
2116 cm_deref_id(cm_id_priv);
2120 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2121 struct cm_id_private *cm_id_priv,
2122 const void *private_data,
2123 u8 private_data_len)
2125 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2126 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2127 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2128 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2129 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2131 if (private_data && private_data_len)
2132 memcpy(dreq_msg->private_data, private_data, private_data_len);
2135 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2136 const void *private_data,
2137 u8 private_data_len)
2139 struct cm_id_private *cm_id_priv;
2140 struct ib_mad_send_buf *msg;
2141 unsigned long flags;
2144 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2147 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2148 spin_lock_irqsave(&cm_id_priv->lock, flags);
2149 if (cm_id->state != IB_CM_ESTABLISHED) {
2154 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2155 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2156 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2158 ret = cm_alloc_msg(cm_id_priv, &msg);
2160 cm_enter_timewait(cm_id_priv);
2164 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2165 private_data, private_data_len);
2166 msg->timeout_ms = cm_id_priv->timeout_ms;
2167 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2169 ret = ib_post_send_mad(msg, NULL);
2171 cm_enter_timewait(cm_id_priv);
2172 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2177 cm_id->state = IB_CM_DREQ_SENT;
2178 cm_id_priv->msg = msg;
2179 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2182 EXPORT_SYMBOL(ib_send_cm_dreq);
2184 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2185 struct cm_id_private *cm_id_priv,
2186 const void *private_data,
2187 u8 private_data_len)
2189 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2190 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2191 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2193 if (private_data && private_data_len)
2194 memcpy(drep_msg->private_data, private_data, private_data_len);
2197 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2198 const void *private_data,
2199 u8 private_data_len)
2201 struct cm_id_private *cm_id_priv;
2202 struct ib_mad_send_buf *msg;
2203 unsigned long flags;
2207 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2210 data = cm_copy_private_data(private_data, private_data_len);
2212 return PTR_ERR(data);
2214 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2215 spin_lock_irqsave(&cm_id_priv->lock, flags);
2216 if (cm_id->state != IB_CM_DREQ_RCVD) {
2217 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2222 cm_set_private_data(cm_id_priv, data, private_data_len);
2223 cm_enter_timewait(cm_id_priv);
2225 ret = cm_alloc_msg(cm_id_priv, &msg);
2229 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2230 private_data, private_data_len);
2232 ret = ib_post_send_mad(msg, NULL);
2234 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2239 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2242 EXPORT_SYMBOL(ib_send_cm_drep);
2244 static int cm_issue_drep(struct cm_port *port,
2245 struct ib_mad_recv_wc *mad_recv_wc)
2247 struct ib_mad_send_buf *msg = NULL;
2248 struct cm_dreq_msg *dreq_msg;
2249 struct cm_drep_msg *drep_msg;
2252 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2256 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2257 drep_msg = (struct cm_drep_msg *) msg->mad;
2259 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2260 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2261 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2263 ret = ib_post_send_mad(msg, NULL);
2270 static int cm_dreq_handler(struct cm_work *work)
2272 struct cm_id_private *cm_id_priv;
2273 struct cm_dreq_msg *dreq_msg;
2274 struct ib_mad_send_buf *msg = NULL;
2277 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2278 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2279 dreq_msg->local_comm_id);
2281 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2282 counter[CM_DREQ_COUNTER]);
2283 cm_issue_drep(work->port, work->mad_recv_wc);
2287 work->cm_event.private_data = &dreq_msg->private_data;
2289 spin_lock_irq(&cm_id_priv->lock);
2290 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2293 switch (cm_id_priv->id.state) {
2294 case IB_CM_REP_SENT:
2295 case IB_CM_DREQ_SENT:
2296 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2298 case IB_CM_ESTABLISHED:
2299 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2300 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2301 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2303 case IB_CM_MRA_REP_RCVD:
2305 case IB_CM_TIMEWAIT:
2306 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2307 counter[CM_DREQ_COUNTER]);
2308 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2311 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2312 cm_id_priv->private_data,
2313 cm_id_priv->private_data_len);
2314 spin_unlock_irq(&cm_id_priv->lock);
2316 if (ib_post_send_mad(msg, NULL))
2319 case IB_CM_DREQ_RCVD:
2320 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2321 counter[CM_DREQ_COUNTER]);
2326 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2327 cm_id_priv->tid = dreq_msg->hdr.tid;
2328 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2330 list_add_tail(&work->list, &cm_id_priv->work_list);
2331 spin_unlock_irq(&cm_id_priv->lock);
2334 cm_process_work(cm_id_priv, work);
2336 cm_deref_id(cm_id_priv);
2339 unlock: spin_unlock_irq(&cm_id_priv->lock);
2340 deref: cm_deref_id(cm_id_priv);
2344 static int cm_drep_handler(struct cm_work *work)
2346 struct cm_id_private *cm_id_priv;
2347 struct cm_drep_msg *drep_msg;
2350 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2351 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2352 drep_msg->local_comm_id);
2356 work->cm_event.private_data = &drep_msg->private_data;
2358 spin_lock_irq(&cm_id_priv->lock);
2359 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2360 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2361 spin_unlock_irq(&cm_id_priv->lock);
2364 cm_enter_timewait(cm_id_priv);
2366 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2367 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2369 list_add_tail(&work->list, &cm_id_priv->work_list);
2370 spin_unlock_irq(&cm_id_priv->lock);
2373 cm_process_work(cm_id_priv, work);
2375 cm_deref_id(cm_id_priv);
2378 cm_deref_id(cm_id_priv);
2382 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2383 enum ib_cm_rej_reason reason,
2386 const void *private_data,
2387 u8 private_data_len)
2389 struct cm_id_private *cm_id_priv;
2390 struct ib_mad_send_buf *msg;
2391 unsigned long flags;
2394 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2395 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2398 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2400 spin_lock_irqsave(&cm_id_priv->lock, flags);
2401 switch (cm_id->state) {
2402 case IB_CM_REQ_SENT:
2403 case IB_CM_MRA_REQ_RCVD:
2404 case IB_CM_REQ_RCVD:
2405 case IB_CM_MRA_REQ_SENT:
2406 case IB_CM_REP_RCVD:
2407 case IB_CM_MRA_REP_SENT:
2408 ret = cm_alloc_msg(cm_id_priv, &msg);
2410 cm_format_rej((struct cm_rej_msg *) msg->mad,
2411 cm_id_priv, reason, ari, ari_length,
2412 private_data, private_data_len);
2414 cm_reset_to_idle(cm_id_priv);
2416 case IB_CM_REP_SENT:
2417 case IB_CM_MRA_REP_RCVD:
2418 ret = cm_alloc_msg(cm_id_priv, &msg);
2420 cm_format_rej((struct cm_rej_msg *) msg->mad,
2421 cm_id_priv, reason, ari, ari_length,
2422 private_data, private_data_len);
2424 cm_enter_timewait(cm_id_priv);
2434 ret = ib_post_send_mad(msg, NULL);
2438 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2441 EXPORT_SYMBOL(ib_send_cm_rej);
2443 static void cm_format_rej_event(struct cm_work *work)
2445 struct cm_rej_msg *rej_msg;
2446 struct ib_cm_rej_event_param *param;
2448 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2449 param = &work->cm_event.param.rej_rcvd;
2450 param->ari = rej_msg->ari;
2451 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2452 param->reason = __be16_to_cpu(rej_msg->reason);
2453 work->cm_event.private_data = &rej_msg->private_data;
2456 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2458 struct cm_timewait_info *timewait_info;
2459 struct cm_id_private *cm_id_priv;
2462 remote_id = rej_msg->local_comm_id;
2464 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2465 spin_lock_irq(&cm.lock);
2466 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2468 if (!timewait_info) {
2469 spin_unlock_irq(&cm.lock);
2472 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2473 (timewait_info->work.local_id ^
2474 cm.random_id_operand));
2476 if (cm_id_priv->id.remote_id == remote_id)
2477 atomic_inc(&cm_id_priv->refcount);
2481 spin_unlock_irq(&cm.lock);
2482 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2483 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2485 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2490 static int cm_rej_handler(struct cm_work *work)
2492 struct cm_id_private *cm_id_priv;
2493 struct cm_rej_msg *rej_msg;
2496 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2497 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2501 cm_format_rej_event(work);
2503 spin_lock_irq(&cm_id_priv->lock);
2504 switch (cm_id_priv->id.state) {
2505 case IB_CM_REQ_SENT:
2506 case IB_CM_MRA_REQ_RCVD:
2507 case IB_CM_REP_SENT:
2508 case IB_CM_MRA_REP_RCVD:
2509 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2511 case IB_CM_REQ_RCVD:
2512 case IB_CM_MRA_REQ_SENT:
2513 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2514 cm_enter_timewait(cm_id_priv);
2516 cm_reset_to_idle(cm_id_priv);
2518 case IB_CM_DREQ_SENT:
2519 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2521 case IB_CM_REP_RCVD:
2522 case IB_CM_MRA_REP_SENT:
2523 cm_enter_timewait(cm_id_priv);
2525 case IB_CM_ESTABLISHED:
2526 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2527 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2528 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2529 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2531 cm_enter_timewait(cm_id_priv);
2536 spin_unlock_irq(&cm_id_priv->lock);
2541 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2543 list_add_tail(&work->list, &cm_id_priv->work_list);
2544 spin_unlock_irq(&cm_id_priv->lock);
2547 cm_process_work(cm_id_priv, work);
2549 cm_deref_id(cm_id_priv);
2552 cm_deref_id(cm_id_priv);
2556 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2558 const void *private_data,
2559 u8 private_data_len)
2561 struct cm_id_private *cm_id_priv;
2562 struct ib_mad_send_buf *msg;
2563 enum ib_cm_state cm_state;
2564 enum ib_cm_lap_state lap_state;
2565 enum cm_msg_response msg_response;
2567 unsigned long flags;
2570 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2573 data = cm_copy_private_data(private_data, private_data_len);
2575 return PTR_ERR(data);
2577 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2579 spin_lock_irqsave(&cm_id_priv->lock, flags);
2580 switch(cm_id_priv->id.state) {
2581 case IB_CM_REQ_RCVD:
2582 cm_state = IB_CM_MRA_REQ_SENT;
2583 lap_state = cm_id->lap_state;
2584 msg_response = CM_MSG_RESPONSE_REQ;
2586 case IB_CM_REP_RCVD:
2587 cm_state = IB_CM_MRA_REP_SENT;
2588 lap_state = cm_id->lap_state;
2589 msg_response = CM_MSG_RESPONSE_REP;
2591 case IB_CM_ESTABLISHED:
2592 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2593 cm_state = cm_id->state;
2594 lap_state = IB_CM_MRA_LAP_SENT;
2595 msg_response = CM_MSG_RESPONSE_OTHER;
2603 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2604 ret = cm_alloc_msg(cm_id_priv, &msg);
2608 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2609 msg_response, service_timeout,
2610 private_data, private_data_len);
2611 ret = ib_post_send_mad(msg, NULL);
2616 cm_id->state = cm_state;
2617 cm_id->lap_state = lap_state;
2618 cm_id_priv->service_timeout = service_timeout;
2619 cm_set_private_data(cm_id_priv, data, private_data_len);
2620 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2623 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2627 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2632 EXPORT_SYMBOL(ib_send_cm_mra);
2634 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2636 switch (cm_mra_get_msg_mraed(mra_msg)) {
2637 case CM_MSG_RESPONSE_REQ:
2638 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2639 case CM_MSG_RESPONSE_REP:
2640 case CM_MSG_RESPONSE_OTHER:
2641 return cm_acquire_id(mra_msg->remote_comm_id,
2642 mra_msg->local_comm_id);
2648 static int cm_mra_handler(struct cm_work *work)
2650 struct cm_id_private *cm_id_priv;
2651 struct cm_mra_msg *mra_msg;
2654 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2655 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2659 work->cm_event.private_data = &mra_msg->private_data;
2660 work->cm_event.param.mra_rcvd.service_timeout =
2661 cm_mra_get_service_timeout(mra_msg);
2662 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2663 cm_convert_to_ms(cm_id_priv->av.timeout);
2665 spin_lock_irq(&cm_id_priv->lock);
2666 switch (cm_id_priv->id.state) {
2667 case IB_CM_REQ_SENT:
2668 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2669 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2670 cm_id_priv->msg, timeout))
2672 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2674 case IB_CM_REP_SENT:
2675 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2676 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2677 cm_id_priv->msg, timeout))
2679 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2681 case IB_CM_ESTABLISHED:
2682 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2683 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2684 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2685 cm_id_priv->msg, timeout)) {
2686 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2687 atomic_long_inc(&work->port->
2688 counter_group[CM_RECV_DUPLICATES].
2689 counter[CM_MRA_COUNTER]);
2692 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2694 case IB_CM_MRA_REQ_RCVD:
2695 case IB_CM_MRA_REP_RCVD:
2696 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2697 counter[CM_MRA_COUNTER]);
2703 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2704 cm_id_priv->id.state;
2705 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2707 list_add_tail(&work->list, &cm_id_priv->work_list);
2708 spin_unlock_irq(&cm_id_priv->lock);
2711 cm_process_work(cm_id_priv, work);
2713 cm_deref_id(cm_id_priv);
2716 spin_unlock_irq(&cm_id_priv->lock);
2717 cm_deref_id(cm_id_priv);
2721 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2722 struct cm_id_private *cm_id_priv,
2723 struct ib_sa_path_rec *alternate_path,
2724 const void *private_data,
2725 u8 private_data_len)
2727 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2728 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2729 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2730 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2731 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2732 /* todo: need remote CM response timeout */
2733 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2734 lap_msg->alt_local_lid = alternate_path->slid;
2735 lap_msg->alt_remote_lid = alternate_path->dlid;
2736 lap_msg->alt_local_gid = alternate_path->sgid;
2737 lap_msg->alt_remote_gid = alternate_path->dgid;
2738 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2739 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2740 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2741 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2742 cm_lap_set_sl(lap_msg, alternate_path->sl);
2743 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2744 cm_lap_set_local_ack_timeout(lap_msg,
2745 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2746 alternate_path->packet_life_time));
2748 if (private_data && private_data_len)
2749 memcpy(lap_msg->private_data, private_data, private_data_len);
2752 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2753 struct ib_sa_path_rec *alternate_path,
2754 const void *private_data,
2755 u8 private_data_len)
2757 struct cm_id_private *cm_id_priv;
2758 struct ib_mad_send_buf *msg;
2759 unsigned long flags;
2762 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2765 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2766 spin_lock_irqsave(&cm_id_priv->lock, flags);
2767 if (cm_id->state != IB_CM_ESTABLISHED ||
2768 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2769 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2774 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av,
2778 cm_id_priv->alt_av.timeout =
2779 cm_ack_timeout(cm_id_priv->target_ack_delay,
2780 cm_id_priv->alt_av.timeout - 1);
2782 ret = cm_alloc_msg(cm_id_priv, &msg);
2786 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2787 alternate_path, private_data, private_data_len);
2788 msg->timeout_ms = cm_id_priv->timeout_ms;
2789 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2791 ret = ib_post_send_mad(msg, NULL);
2793 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2798 cm_id->lap_state = IB_CM_LAP_SENT;
2799 cm_id_priv->msg = msg;
2801 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2804 EXPORT_SYMBOL(ib_send_cm_lap);
2806 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2807 struct ib_sa_path_rec *path,
2808 struct cm_lap_msg *lap_msg)
2810 memset(path, 0, sizeof *path);
2811 path->dgid = lap_msg->alt_local_gid;
2812 path->sgid = lap_msg->alt_remote_gid;
2813 path->dlid = lap_msg->alt_local_lid;
2814 path->slid = lap_msg->alt_remote_lid;
2815 path->flow_label = cm_lap_get_flow_label(lap_msg);
2816 path->hop_limit = lap_msg->alt_hop_limit;
2817 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2818 path->reversible = 1;
2819 path->pkey = cm_id_priv->pkey;
2820 path->sl = cm_lap_get_sl(lap_msg);
2821 path->mtu_selector = IB_SA_EQ;
2822 path->mtu = cm_id_priv->path_mtu;
2823 path->rate_selector = IB_SA_EQ;
2824 path->rate = cm_lap_get_packet_rate(lap_msg);
2825 path->packet_life_time_selector = IB_SA_EQ;
2826 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2827 path->packet_life_time -= (path->packet_life_time > 0);
2830 static int cm_lap_handler(struct cm_work *work)
2832 struct cm_id_private *cm_id_priv;
2833 struct cm_lap_msg *lap_msg;
2834 struct ib_cm_lap_event_param *param;
2835 struct ib_mad_send_buf *msg = NULL;
2838 /* todo: verify LAP request and send reject APR if invalid. */
2839 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2840 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2841 lap_msg->local_comm_id);
2845 param = &work->cm_event.param.lap_rcvd;
2846 param->alternate_path = &work->path[0];
2847 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2848 work->cm_event.private_data = &lap_msg->private_data;
2850 spin_lock_irq(&cm_id_priv->lock);
2851 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2854 switch (cm_id_priv->id.lap_state) {
2855 case IB_CM_LAP_UNINIT:
2856 case IB_CM_LAP_IDLE:
2858 case IB_CM_MRA_LAP_SENT:
2859 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2860 counter[CM_LAP_COUNTER]);
2861 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2864 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2865 CM_MSG_RESPONSE_OTHER,
2866 cm_id_priv->service_timeout,
2867 cm_id_priv->private_data,
2868 cm_id_priv->private_data_len);
2869 spin_unlock_irq(&cm_id_priv->lock);
2871 if (ib_post_send_mad(msg, NULL))
2874 case IB_CM_LAP_RCVD:
2875 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2876 counter[CM_LAP_COUNTER]);
2882 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2883 cm_id_priv->tid = lap_msg->hdr.tid;
2884 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2885 work->mad_recv_wc->recv_buf.grh,
2887 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av,
2889 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2891 list_add_tail(&work->list, &cm_id_priv->work_list);
2892 spin_unlock_irq(&cm_id_priv->lock);
2895 cm_process_work(cm_id_priv, work);
2897 cm_deref_id(cm_id_priv);
2900 unlock: spin_unlock_irq(&cm_id_priv->lock);
2901 deref: cm_deref_id(cm_id_priv);
2905 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2906 struct cm_id_private *cm_id_priv,
2907 enum ib_cm_apr_status status,
2910 const void *private_data,
2911 u8 private_data_len)
2913 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2914 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2915 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2916 apr_msg->ap_status = (u8) status;
2918 if (info && info_length) {
2919 apr_msg->info_length = info_length;
2920 memcpy(apr_msg->info, info, info_length);
2923 if (private_data && private_data_len)
2924 memcpy(apr_msg->private_data, private_data, private_data_len);
2927 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2928 enum ib_cm_apr_status status,
2931 const void *private_data,
2932 u8 private_data_len)
2934 struct cm_id_private *cm_id_priv;
2935 struct ib_mad_send_buf *msg;
2936 unsigned long flags;
2939 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2940 (info && info_length > IB_CM_APR_INFO_LENGTH))
2943 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2944 spin_lock_irqsave(&cm_id_priv->lock, flags);
2945 if (cm_id->state != IB_CM_ESTABLISHED ||
2946 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2947 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2952 ret = cm_alloc_msg(cm_id_priv, &msg);
2956 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2957 info, info_length, private_data, private_data_len);
2958 ret = ib_post_send_mad(msg, NULL);
2960 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2965 cm_id->lap_state = IB_CM_LAP_IDLE;
2966 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2969 EXPORT_SYMBOL(ib_send_cm_apr);
2971 static int cm_apr_handler(struct cm_work *work)
2973 struct cm_id_private *cm_id_priv;
2974 struct cm_apr_msg *apr_msg;
2977 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2978 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2979 apr_msg->local_comm_id);
2981 return -EINVAL; /* Unmatched reply. */
2983 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2984 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2985 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2986 work->cm_event.private_data = &apr_msg->private_data;
2988 spin_lock_irq(&cm_id_priv->lock);
2989 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2990 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2991 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2992 spin_unlock_irq(&cm_id_priv->lock);
2995 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2996 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2997 cm_id_priv->msg = NULL;
2999 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3001 list_add_tail(&work->list, &cm_id_priv->work_list);
3002 spin_unlock_irq(&cm_id_priv->lock);
3005 cm_process_work(cm_id_priv, work);
3007 cm_deref_id(cm_id_priv);
3010 cm_deref_id(cm_id_priv);
3014 static int cm_timewait_handler(struct cm_work *work)
3016 struct cm_timewait_info *timewait_info;
3017 struct cm_id_private *cm_id_priv;
3020 timewait_info = (struct cm_timewait_info *)work;
3021 spin_lock_irq(&cm.lock);
3022 list_del(&timewait_info->list);
3023 spin_unlock_irq(&cm.lock);
3025 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3026 timewait_info->work.remote_id);
3030 spin_lock_irq(&cm_id_priv->lock);
3031 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3032 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3033 spin_unlock_irq(&cm_id_priv->lock);
3036 cm_id_priv->id.state = IB_CM_IDLE;
3037 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3039 list_add_tail(&work->list, &cm_id_priv->work_list);
3040 spin_unlock_irq(&cm_id_priv->lock);
3043 cm_process_work(cm_id_priv, work);
3045 cm_deref_id(cm_id_priv);
3048 cm_deref_id(cm_id_priv);
3052 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3053 struct cm_id_private *cm_id_priv,
3054 struct ib_cm_sidr_req_param *param)
3056 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3057 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
3058 sidr_req_msg->request_id = cm_id_priv->id.local_id;
3059 sidr_req_msg->pkey = param->path->pkey;
3060 sidr_req_msg->service_id = param->service_id;
3062 if (param->private_data && param->private_data_len)
3063 memcpy(sidr_req_msg->private_data, param->private_data,
3064 param->private_data_len);
3067 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3068 struct ib_cm_sidr_req_param *param)
3070 struct cm_id_private *cm_id_priv;
3071 struct ib_mad_send_buf *msg;
3072 unsigned long flags;
3075 if (!param->path || (param->private_data &&
3076 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3079 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3080 ret = cm_init_av_by_path(param->path, &cm_id_priv->av, cm_id_priv);
3084 cm_id->service_id = param->service_id;
3085 cm_id->service_mask = ~cpu_to_be64(0);
3086 cm_id_priv->timeout_ms = param->timeout_ms;
3087 cm_id_priv->max_cm_retries = param->max_cm_retries;
3088 ret = cm_alloc_msg(cm_id_priv, &msg);
3092 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3094 msg->timeout_ms = cm_id_priv->timeout_ms;
3095 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3097 spin_lock_irqsave(&cm_id_priv->lock, flags);
3098 if (cm_id->state == IB_CM_IDLE)
3099 ret = ib_post_send_mad(msg, NULL);
3104 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3108 cm_id->state = IB_CM_SIDR_REQ_SENT;
3109 cm_id_priv->msg = msg;
3110 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3114 EXPORT_SYMBOL(ib_send_cm_sidr_req);
3116 static void cm_format_sidr_req_event(struct cm_work *work,
3117 struct ib_cm_id *listen_id)
3119 struct cm_sidr_req_msg *sidr_req_msg;
3120 struct ib_cm_sidr_req_event_param *param;
3122 sidr_req_msg = (struct cm_sidr_req_msg *)
3123 work->mad_recv_wc->recv_buf.mad;
3124 param = &work->cm_event.param.sidr_req_rcvd;
3125 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
3126 param->listen_id = listen_id;
3127 param->service_id = sidr_req_msg->service_id;
3128 param->bth_pkey = cm_get_bth_pkey(work);
3129 param->port = work->port->port_num;
3130 work->cm_event.private_data = &sidr_req_msg->private_data;
3133 static int cm_sidr_req_handler(struct cm_work *work)
3135 struct ib_cm_id *cm_id;
3136 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3137 struct cm_sidr_req_msg *sidr_req_msg;
3140 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
3142 return PTR_ERR(cm_id);
3143 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3145 /* Record SGID/SLID and request ID for lookup. */
3146 sidr_req_msg = (struct cm_sidr_req_msg *)
3147 work->mad_recv_wc->recv_buf.mad;
3148 wc = work->mad_recv_wc->wc;
3149 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3150 cm_id_priv->av.dgid.global.interface_id = 0;
3151 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3152 work->mad_recv_wc->recv_buf.grh,
3154 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
3155 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3156 atomic_inc(&cm_id_priv->work_count);
3158 spin_lock_irq(&cm.lock);
3159 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3160 if (cur_cm_id_priv) {
3161 spin_unlock_irq(&cm.lock);
3162 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3163 counter[CM_SIDR_REQ_COUNTER]);
3164 goto out; /* Duplicate message. */
3166 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3167 cur_cm_id_priv = cm_find_listen(cm_id->device,
3168 sidr_req_msg->service_id);
3169 if (!cur_cm_id_priv) {
3170 spin_unlock_irq(&cm.lock);
3171 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3172 goto out; /* No match. */
3174 atomic_inc(&cur_cm_id_priv->refcount);
3175 atomic_inc(&cm_id_priv->refcount);
3176 spin_unlock_irq(&cm.lock);
3178 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3179 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3180 cm_id_priv->id.service_id = sidr_req_msg->service_id;
3181 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3183 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3184 cm_process_work(cm_id_priv, work);
3185 cm_deref_id(cur_cm_id_priv);
3188 ib_destroy_cm_id(&cm_id_priv->id);
3192 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3193 struct cm_id_private *cm_id_priv,
3194 struct ib_cm_sidr_rep_param *param)
3196 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3198 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3199 sidr_rep_msg->status = param->status;
3200 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3201 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3202 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3204 if (param->info && param->info_length)
3205 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3207 if (param->private_data && param->private_data_len)
3208 memcpy(sidr_rep_msg->private_data, param->private_data,
3209 param->private_data_len);
3212 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3213 struct ib_cm_sidr_rep_param *param)
3215 struct cm_id_private *cm_id_priv;
3216 struct ib_mad_send_buf *msg;
3217 unsigned long flags;
3220 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3221 (param->private_data &&
3222 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3225 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3226 spin_lock_irqsave(&cm_id_priv->lock, flags);
3227 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3232 ret = cm_alloc_msg(cm_id_priv, &msg);
3236 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3238 ret = ib_post_send_mad(msg, NULL);
3240 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3244 cm_id->state = IB_CM_IDLE;
3245 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3247 spin_lock_irqsave(&cm.lock, flags);
3248 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3249 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3250 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3252 spin_unlock_irqrestore(&cm.lock, flags);
3255 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3258 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3260 static void cm_format_sidr_rep_event(struct cm_work *work)
3262 struct cm_sidr_rep_msg *sidr_rep_msg;
3263 struct ib_cm_sidr_rep_event_param *param;
3265 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3266 work->mad_recv_wc->recv_buf.mad;
3267 param = &work->cm_event.param.sidr_rep_rcvd;
3268 param->status = sidr_rep_msg->status;
3269 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3270 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3271 param->info = &sidr_rep_msg->info;
3272 param->info_len = sidr_rep_msg->info_length;
3273 work->cm_event.private_data = &sidr_rep_msg->private_data;
3276 static int cm_sidr_rep_handler(struct cm_work *work)
3278 struct cm_sidr_rep_msg *sidr_rep_msg;
3279 struct cm_id_private *cm_id_priv;
3281 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3282 work->mad_recv_wc->recv_buf.mad;
3283 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3285 return -EINVAL; /* Unmatched reply. */
3287 spin_lock_irq(&cm_id_priv->lock);
3288 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3289 spin_unlock_irq(&cm_id_priv->lock);
3292 cm_id_priv->id.state = IB_CM_IDLE;
3293 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3294 spin_unlock_irq(&cm_id_priv->lock);
3296 cm_format_sidr_rep_event(work);
3297 cm_process_work(cm_id_priv, work);
3300 cm_deref_id(cm_id_priv);
3304 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3305 enum ib_wc_status wc_status)
3307 struct cm_id_private *cm_id_priv;
3308 struct ib_cm_event cm_event;
3309 enum ib_cm_state state;
3312 memset(&cm_event, 0, sizeof cm_event);
3313 cm_id_priv = msg->context[0];
3315 /* Discard old sends or ones without a response. */
3316 spin_lock_irq(&cm_id_priv->lock);
3317 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3318 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3322 case IB_CM_REQ_SENT:
3323 case IB_CM_MRA_REQ_RCVD:
3324 cm_reset_to_idle(cm_id_priv);
3325 cm_event.event = IB_CM_REQ_ERROR;
3327 case IB_CM_REP_SENT:
3328 case IB_CM_MRA_REP_RCVD:
3329 cm_reset_to_idle(cm_id_priv);
3330 cm_event.event = IB_CM_REP_ERROR;
3332 case IB_CM_DREQ_SENT:
3333 cm_enter_timewait(cm_id_priv);
3334 cm_event.event = IB_CM_DREQ_ERROR;
3336 case IB_CM_SIDR_REQ_SENT:
3337 cm_id_priv->id.state = IB_CM_IDLE;
3338 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3343 spin_unlock_irq(&cm_id_priv->lock);
3344 cm_event.param.send_status = wc_status;
3346 /* No other events can occur on the cm_id at this point. */
3347 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3350 ib_destroy_cm_id(&cm_id_priv->id);
3353 spin_unlock_irq(&cm_id_priv->lock);
3357 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3358 struct ib_mad_send_wc *mad_send_wc)
3360 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3361 struct cm_port *port;
3364 port = mad_agent->context;
3365 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3366 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3369 * If the send was in response to a received message (context[0] is not
3370 * set to a cm_id), and is not a REJ, then it is a send that was
3373 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3376 atomic_long_add(1 + msg->retries,
3377 &port->counter_group[CM_XMIT].counter[attr_index]);
3379 atomic_long_add(msg->retries,
3380 &port->counter_group[CM_XMIT_RETRIES].
3381 counter[attr_index]);
3383 switch (mad_send_wc->status) {
3385 case IB_WC_WR_FLUSH_ERR:
3389 if (msg->context[0] && msg->context[1])
3390 cm_process_send_error(msg, mad_send_wc->status);
3397 static void cm_work_handler(struct work_struct *_work)
3399 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3402 switch (work->cm_event.event) {
3403 case IB_CM_REQ_RECEIVED:
3404 ret = cm_req_handler(work);
3406 case IB_CM_MRA_RECEIVED:
3407 ret = cm_mra_handler(work);
3409 case IB_CM_REJ_RECEIVED:
3410 ret = cm_rej_handler(work);
3412 case IB_CM_REP_RECEIVED:
3413 ret = cm_rep_handler(work);
3415 case IB_CM_RTU_RECEIVED:
3416 ret = cm_rtu_handler(work);
3418 case IB_CM_USER_ESTABLISHED:
3419 ret = cm_establish_handler(work);
3421 case IB_CM_DREQ_RECEIVED:
3422 ret = cm_dreq_handler(work);
3424 case IB_CM_DREP_RECEIVED:
3425 ret = cm_drep_handler(work);
3427 case IB_CM_SIDR_REQ_RECEIVED:
3428 ret = cm_sidr_req_handler(work);
3430 case IB_CM_SIDR_REP_RECEIVED:
3431 ret = cm_sidr_rep_handler(work);
3433 case IB_CM_LAP_RECEIVED:
3434 ret = cm_lap_handler(work);
3436 case IB_CM_APR_RECEIVED:
3437 ret = cm_apr_handler(work);
3439 case IB_CM_TIMEWAIT_EXIT:
3440 ret = cm_timewait_handler(work);
3450 static int cm_establish(struct ib_cm_id *cm_id)
3452 struct cm_id_private *cm_id_priv;
3453 struct cm_work *work;
3454 unsigned long flags;
3456 struct cm_device *cm_dev;
3458 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3462 work = kmalloc(sizeof *work, GFP_ATOMIC);
3466 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3467 spin_lock_irqsave(&cm_id_priv->lock, flags);
3468 switch (cm_id->state)
3470 case IB_CM_REP_SENT:
3471 case IB_CM_MRA_REP_RCVD:
3472 cm_id->state = IB_CM_ESTABLISHED;
3474 case IB_CM_ESTABLISHED:
3481 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3489 * The CM worker thread may try to destroy the cm_id before it
3490 * can execute this work item. To prevent potential deadlock,
3491 * we need to find the cm_id once we're in the context of the
3492 * worker thread, rather than holding a reference on it.
3494 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3495 work->local_id = cm_id->local_id;
3496 work->remote_id = cm_id->remote_id;
3497 work->mad_recv_wc = NULL;
3498 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3500 /* Check if the device started its remove_one */
3501 spin_lock_irqsave(&cm.lock, flags);
3502 if (!cm_dev->going_down) {
3503 queue_delayed_work(cm.wq, &work->work, 0);
3508 spin_unlock_irqrestore(&cm.lock, flags);
3514 static int cm_migrate(struct ib_cm_id *cm_id)
3516 struct cm_id_private *cm_id_priv;
3517 struct cm_av tmp_av;
3518 unsigned long flags;
3519 int tmp_send_port_not_ready;
3522 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3523 spin_lock_irqsave(&cm_id_priv->lock, flags);
3524 if (cm_id->state == IB_CM_ESTABLISHED &&
3525 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3526 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3527 cm_id->lap_state = IB_CM_LAP_IDLE;
3528 /* Swap address vector */
3529 tmp_av = cm_id_priv->av;
3530 cm_id_priv->av = cm_id_priv->alt_av;
3531 cm_id_priv->alt_av = tmp_av;
3532 /* Swap port send ready state */
3533 tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
3534 cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
3535 cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
3538 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3543 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3548 case IB_EVENT_COMM_EST:
3549 ret = cm_establish(cm_id);
3551 case IB_EVENT_PATH_MIG:
3552 ret = cm_migrate(cm_id);
3559 EXPORT_SYMBOL(ib_cm_notify);
3561 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3562 struct ib_mad_recv_wc *mad_recv_wc)
3564 struct cm_port *port = mad_agent->context;
3565 struct cm_work *work;
3566 enum ib_cm_event_type event;
3571 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3572 case CM_REQ_ATTR_ID:
3573 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3574 alt_local_lid != 0);
3575 event = IB_CM_REQ_RECEIVED;
3577 case CM_MRA_ATTR_ID:
3578 event = IB_CM_MRA_RECEIVED;
3580 case CM_REJ_ATTR_ID:
3581 event = IB_CM_REJ_RECEIVED;
3583 case CM_REP_ATTR_ID:
3584 event = IB_CM_REP_RECEIVED;
3586 case CM_RTU_ATTR_ID:
3587 event = IB_CM_RTU_RECEIVED;
3589 case CM_DREQ_ATTR_ID:
3590 event = IB_CM_DREQ_RECEIVED;
3592 case CM_DREP_ATTR_ID:
3593 event = IB_CM_DREP_RECEIVED;
3595 case CM_SIDR_REQ_ATTR_ID:
3596 event = IB_CM_SIDR_REQ_RECEIVED;
3598 case CM_SIDR_REP_ATTR_ID:
3599 event = IB_CM_SIDR_REP_RECEIVED;
3601 case CM_LAP_ATTR_ID:
3603 event = IB_CM_LAP_RECEIVED;
3605 case CM_APR_ATTR_ID:
3606 event = IB_CM_APR_RECEIVED;
3609 ib_free_recv_mad(mad_recv_wc);
3613 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3614 atomic_long_inc(&port->counter_group[CM_RECV].
3615 counter[attr_id - CM_ATTR_ID_OFFSET]);
3617 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3620 ib_free_recv_mad(mad_recv_wc);
3624 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3625 work->cm_event.event = event;
3626 work->mad_recv_wc = mad_recv_wc;
3629 /* Check if the device started its remove_one */
3630 spin_lock_irq(&cm.lock);
3631 if (!port->cm_dev->going_down)
3632 queue_delayed_work(cm.wq, &work->work, 0);
3635 spin_unlock_irq(&cm.lock);
3639 ib_free_recv_mad(mad_recv_wc);
3643 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3644 struct ib_qp_attr *qp_attr,
3647 unsigned long flags;
3650 spin_lock_irqsave(&cm_id_priv->lock, flags);
3651 switch (cm_id_priv->id.state) {
3652 case IB_CM_REQ_SENT:
3653 case IB_CM_MRA_REQ_RCVD:
3654 case IB_CM_REQ_RCVD:
3655 case IB_CM_MRA_REQ_SENT:
3656 case IB_CM_REP_RCVD:
3657 case IB_CM_MRA_REP_SENT:
3658 case IB_CM_REP_SENT:
3659 case IB_CM_MRA_REP_RCVD:
3660 case IB_CM_ESTABLISHED:
3661 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3662 IB_QP_PKEY_INDEX | IB_QP_PORT;
3663 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3664 if (cm_id_priv->responder_resources)
3665 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3666 IB_ACCESS_REMOTE_ATOMIC;
3667 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3668 qp_attr->port_num = cm_id_priv->av.port->port_num;
3675 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3679 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3680 struct ib_qp_attr *qp_attr,
3683 unsigned long flags;
3686 spin_lock_irqsave(&cm_id_priv->lock, flags);
3687 switch (cm_id_priv->id.state) {
3688 case IB_CM_REQ_RCVD:
3689 case IB_CM_MRA_REQ_SENT:
3690 case IB_CM_REP_RCVD:
3691 case IB_CM_MRA_REP_SENT:
3692 case IB_CM_REP_SENT:
3693 case IB_CM_MRA_REP_RCVD:
3694 case IB_CM_ESTABLISHED:
3695 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3696 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3697 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3698 qp_attr->path_mtu = cm_id_priv->path_mtu;
3699 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3700 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3701 if (cm_id_priv->qp_type == IB_QPT_RC ||
3702 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
3703 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3704 IB_QP_MIN_RNR_TIMER;
3705 qp_attr->max_dest_rd_atomic =
3706 cm_id_priv->responder_resources;
3707 qp_attr->min_rnr_timer = 0;
3709 if (cm_id_priv->alt_av.ah_attr.dlid) {
3710 *qp_attr_mask |= IB_QP_ALT_PATH;
3711 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3712 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3713 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3714 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3722 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3726 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3727 struct ib_qp_attr *qp_attr,
3730 unsigned long flags;
3733 spin_lock_irqsave(&cm_id_priv->lock, flags);
3734 switch (cm_id_priv->id.state) {
3735 /* Allow transition to RTS before sending REP */
3736 case IB_CM_REQ_RCVD:
3737 case IB_CM_MRA_REQ_SENT:
3739 case IB_CM_REP_RCVD:
3740 case IB_CM_MRA_REP_SENT:
3741 case IB_CM_REP_SENT:
3742 case IB_CM_MRA_REP_RCVD:
3743 case IB_CM_ESTABLISHED:
3744 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3745 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3746 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3747 switch (cm_id_priv->qp_type) {
3749 case IB_QPT_XRC_INI:
3750 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3751 IB_QP_MAX_QP_RD_ATOMIC;
3752 qp_attr->retry_cnt = cm_id_priv->retry_count;
3753 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3754 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3756 case IB_QPT_XRC_TGT:
3757 *qp_attr_mask |= IB_QP_TIMEOUT;
3758 qp_attr->timeout = cm_id_priv->av.timeout;
3763 if (cm_id_priv->alt_av.ah_attr.dlid) {
3764 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3765 qp_attr->path_mig_state = IB_MIG_REARM;
3768 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3769 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3770 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3771 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3772 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3773 qp_attr->path_mig_state = IB_MIG_REARM;
3781 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3785 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3786 struct ib_qp_attr *qp_attr,
3789 struct cm_id_private *cm_id_priv;
3792 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3793 switch (qp_attr->qp_state) {
3795 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3798 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3801 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3809 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3811 static void cm_get_ack_delay(struct cm_device *cm_dev)
3813 struct ib_device_attr attr;
3815 if (ib_query_device(cm_dev->ib_device, &attr))
3816 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3818 cm_dev->ack_delay = attr.local_ca_ack_delay;
3821 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3824 struct cm_counter_group *group;
3825 struct cm_counter_attribute *cm_attr;
3827 group = container_of(obj, struct cm_counter_group, obj);
3828 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3830 return sprintf(buf, "%ld\n",
3831 atomic_long_read(&group->counter[cm_attr->index]));
3834 static const struct sysfs_ops cm_counter_ops = {
3835 .show = cm_show_counter
3838 static struct kobj_type cm_counter_obj_type = {
3839 .sysfs_ops = &cm_counter_ops,
3840 .default_attrs = cm_counter_default_attrs
3843 static void cm_release_port_obj(struct kobject *obj)
3845 struct cm_port *cm_port;
3847 cm_port = container_of(obj, struct cm_port, port_obj);
3851 static struct kobj_type cm_port_obj_type = {
3852 .release = cm_release_port_obj
3855 static char *cm_devnode(struct device *dev, umode_t *mode)
3859 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3862 struct class cm_class = {
3863 .owner = THIS_MODULE,
3864 .name = "infiniband_cm",
3865 .devnode = cm_devnode,
3867 EXPORT_SYMBOL(cm_class);
3869 static int cm_create_port_fs(struct cm_port *port)
3873 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3874 &port->cm_dev->device->kobj,
3875 "%d", port->port_num);
3881 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3882 ret = kobject_init_and_add(&port->counter_group[i].obj,
3883 &cm_counter_obj_type,
3885 "%s", counter_group_names[i]);
3894 kobject_put(&port->counter_group[i].obj);
3895 kobject_put(&port->port_obj);
3900 static void cm_remove_port_fs(struct cm_port *port)
3904 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3905 kobject_put(&port->counter_group[i].obj);
3907 kobject_put(&port->port_obj);
3910 static void cm_add_one(struct ib_device *ib_device)
3912 struct cm_device *cm_dev;
3913 struct cm_port *port;
3914 struct ib_mad_reg_req reg_req = {
3915 .mgmt_class = IB_MGMT_CLASS_CM,
3916 .mgmt_class_version = IB_CM_CLASS_VERSION,
3918 struct ib_port_modify port_modify = {
3919 .set_port_cap_mask = IB_PORT_CM_SUP
3921 unsigned long flags;
3926 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3927 ib_device->phys_port_cnt, GFP_KERNEL);
3931 cm_dev->ib_device = ib_device;
3932 cm_get_ack_delay(cm_dev);
3933 cm_dev->going_down = 0;
3934 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3936 "%s", ib_device->name);
3937 if (IS_ERR(cm_dev->device)) {
3942 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3943 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3944 if (!rdma_cap_ib_cm(ib_device, i))
3947 port = kzalloc(sizeof *port, GFP_KERNEL);
3951 cm_dev->port[i-1] = port;
3952 port->cm_dev = cm_dev;
3955 INIT_LIST_HEAD(&port->cm_priv_prim_list);
3956 INIT_LIST_HEAD(&port->cm_priv_altr_list);
3958 ret = cm_create_port_fs(port);
3962 port->mad_agent = ib_register_mad_agent(ib_device, i,
3970 if (IS_ERR(port->mad_agent))
3973 ret = ib_modify_port(ib_device, i, 0, &port_modify);
3983 ib_set_client_data(ib_device, &cm_client, cm_dev);
3985 write_lock_irqsave(&cm.device_lock, flags);
3986 list_add_tail(&cm_dev->list, &cm.device_list);
3987 write_unlock_irqrestore(&cm.device_lock, flags);
3991 ib_unregister_mad_agent(port->mad_agent);
3993 cm_remove_port_fs(port);
3995 port_modify.set_port_cap_mask = 0;
3996 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3998 if (!rdma_cap_ib_cm(ib_device, i))
4001 port = cm_dev->port[i-1];
4002 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4003 ib_unregister_mad_agent(port->mad_agent);
4004 cm_remove_port_fs(port);
4007 device_unregister(cm_dev->device);
4011 static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4013 struct cm_device *cm_dev = client_data;
4014 struct cm_port *port;
4015 struct cm_id_private *cm_id_priv;
4016 struct ib_mad_agent *cur_mad_agent;
4017 struct ib_port_modify port_modify = {
4018 .clr_port_cap_mask = IB_PORT_CM_SUP
4020 unsigned long flags;
4026 write_lock_irqsave(&cm.device_lock, flags);
4027 list_del(&cm_dev->list);
4028 write_unlock_irqrestore(&cm.device_lock, flags);
4030 spin_lock_irq(&cm.lock);
4031 cm_dev->going_down = 1;
4032 spin_unlock_irq(&cm.lock);
4034 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
4035 if (!rdma_cap_ib_cm(ib_device, i))
4038 port = cm_dev->port[i-1];
4039 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4040 /* Mark all the cm_id's as not valid */
4041 spin_lock_irq(&cm.lock);
4042 list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
4043 cm_id_priv->altr_send_port_not_ready = 1;
4044 list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
4045 cm_id_priv->prim_send_port_not_ready = 1;
4046 spin_unlock_irq(&cm.lock);
4048 * We flush the queue here after the going_down set, this
4049 * verify that no new works will be queued in the recv handler,
4050 * after that we can call the unregister_mad_agent
4052 flush_workqueue(cm.wq);
4053 spin_lock_irq(&cm.state_lock);
4054 cur_mad_agent = port->mad_agent;
4055 port->mad_agent = NULL;
4056 spin_unlock_irq(&cm.state_lock);
4057 ib_unregister_mad_agent(cur_mad_agent);
4058 cm_remove_port_fs(port);
4061 device_unregister(cm_dev->device);
4065 static int __init ib_cm_init(void)
4069 memset(&cm, 0, sizeof cm);
4070 INIT_LIST_HEAD(&cm.device_list);
4071 rwlock_init(&cm.device_lock);
4072 spin_lock_init(&cm.lock);
4073 spin_lock_init(&cm.state_lock);
4074 cm.listen_service_table = RB_ROOT;
4075 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4076 cm.remote_id_table = RB_ROOT;
4077 cm.remote_qp_table = RB_ROOT;
4078 cm.remote_sidr_table = RB_ROOT;
4079 idr_init(&cm.local_id_table);
4080 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4081 INIT_LIST_HEAD(&cm.timewait_list);
4083 ret = class_register(&cm_class);
4089 cm.wq = create_workqueue("ib_cm");
4095 ret = ib_register_client(&cm_client);
4101 destroy_workqueue(cm.wq);
4103 class_unregister(&cm_class);
4105 idr_destroy(&cm.local_id_table);
4109 static void __exit ib_cm_cleanup(void)
4111 struct cm_timewait_info *timewait_info, *tmp;
4113 spin_lock_irq(&cm.lock);
4114 list_for_each_entry(timewait_info, &cm.timewait_list, list)
4115 cancel_delayed_work(&timewait_info->work.work);
4116 spin_unlock_irq(&cm.lock);
4118 ib_unregister_client(&cm_client);
4119 destroy_workqueue(cm.wq);
4121 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4122 list_del(&timewait_info->list);
4123 kfree(timewait_info);
4126 class_unregister(&cm_class);
4127 idr_destroy(&cm.local_id_table);
4130 module_init(ib_cm_init);
4131 module_exit(ib_cm_cleanup);