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
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/device.h>
41 #include <linux/err.h>
42 #include <linux/idr.h>
43 #include <linux/interrupt.h>
44 #include <linux/random.h>
45 #include <linux/rbtree.h>
46 #include <linux/spinlock.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_cm.h>
54 MODULE_AUTHOR("Sean Hefty");
55 MODULE_DESCRIPTION("InfiniBand CM");
56 MODULE_LICENSE("Dual BSD/GPL");
58 static void cm_add_one(struct ib_device *device);
59 static void cm_remove_one(struct ib_device *device);
61 static struct ib_client cm_client = {
64 .remove = cm_remove_one
69 struct list_head device_list;
71 struct rb_root listen_service_table;
72 u64 listen_service_id;
73 /* struct rb_root peer_service_table; todo: fix peer to peer */
74 struct rb_root remote_qp_table;
75 struct rb_root remote_id_table;
76 struct rb_root remote_sidr_table;
77 struct idr local_id_table;
78 __be32 random_id_operand;
79 struct list_head timewait_list;
80 struct workqueue_struct *wq;
83 /* Counter indexes ordered by attribute ID */
97 CM_ATTR_ID_OFFSET = 0x0010,
108 static char const counter_group_names[CM_COUNTER_GROUPS]
109 [sizeof("cm_rx_duplicates")] = {
110 "cm_tx_msgs", "cm_tx_retries",
111 "cm_rx_msgs", "cm_rx_duplicates"
114 struct cm_counter_group {
116 atomic_long_t counter[CM_ATTR_COUNT];
119 struct cm_counter_attribute {
120 struct attribute attr;
124 #define CM_COUNTER_ATTR(_name, _index) \
125 struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \
130 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
131 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
132 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
133 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
134 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
135 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
136 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
137 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
138 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
139 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
140 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
142 static struct attribute *cm_counter_default_attrs[] = {
143 &cm_req_counter_attr.attr,
144 &cm_mra_counter_attr.attr,
145 &cm_rej_counter_attr.attr,
146 &cm_rep_counter_attr.attr,
147 &cm_rtu_counter_attr.attr,
148 &cm_dreq_counter_attr.attr,
149 &cm_drep_counter_attr.attr,
150 &cm_sidr_req_counter_attr.attr,
151 &cm_sidr_rep_counter_attr.attr,
152 &cm_lap_counter_attr.attr,
153 &cm_apr_counter_attr.attr,
158 struct cm_device *cm_dev;
159 struct ib_mad_agent *mad_agent;
160 struct kobject port_obj;
162 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
166 struct list_head list;
167 struct ib_device *device;
168 struct kobject dev_obj;
170 struct cm_port *port[0];
174 struct cm_port *port;
176 struct ib_ah_attr ah_attr;
182 struct delayed_work work;
183 struct list_head list;
184 struct cm_port *port;
185 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
186 __be32 local_id; /* Established / timewait */
188 struct ib_cm_event cm_event;
189 struct ib_sa_path_rec path[0];
192 struct cm_timewait_info {
193 struct cm_work work; /* Must be first. */
194 struct list_head list;
195 struct rb_node remote_qp_node;
196 struct rb_node remote_id_node;
197 __be64 remote_ca_guid;
199 u8 inserted_remote_qp;
200 u8 inserted_remote_id;
203 struct cm_id_private {
206 struct rb_node service_node;
207 struct rb_node sidr_id_node;
208 spinlock_t lock; /* Do not acquire inside cm.lock */
209 struct completion comp;
212 struct ib_mad_send_buf *msg;
213 struct cm_timewait_info *timewait_info;
214 /* todo: use alternate port on send failure */
217 struct ib_cm_compare_data *compare_data;
223 enum ib_qp_type qp_type;
227 enum ib_mtu path_mtu;
232 u8 responder_resources;
239 struct list_head work_list;
243 static void cm_work_handler(struct work_struct *work);
245 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
247 if (atomic_dec_and_test(&cm_id_priv->refcount))
248 complete(&cm_id_priv->comp);
251 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
252 struct ib_mad_send_buf **msg)
254 struct ib_mad_agent *mad_agent;
255 struct ib_mad_send_buf *m;
258 mad_agent = cm_id_priv->av.port->mad_agent;
259 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
263 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
264 cm_id_priv->av.pkey_index,
265 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
272 /* Timeout set by caller if response is expected. */
274 m->retries = cm_id_priv->max_cm_retries;
276 atomic_inc(&cm_id_priv->refcount);
277 m->context[0] = cm_id_priv;
282 static int cm_alloc_response_msg(struct cm_port *port,
283 struct ib_mad_recv_wc *mad_recv_wc,
284 struct ib_mad_send_buf **msg)
286 struct ib_mad_send_buf *m;
289 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
290 mad_recv_wc->recv_buf.grh, port->port_num);
294 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
295 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
306 static void cm_free_msg(struct ib_mad_send_buf *msg)
308 ib_destroy_ah(msg->ah);
310 cm_deref_id(msg->context[0]);
311 ib_free_send_mad(msg);
314 static void * cm_copy_private_data(const void *private_data,
319 if (!private_data || !private_data_len)
322 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
324 return ERR_PTR(-ENOMEM);
329 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
330 void *private_data, u8 private_data_len)
332 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
333 kfree(cm_id_priv->private_data);
335 cm_id_priv->private_data = private_data;
336 cm_id_priv->private_data_len = private_data_len;
339 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
340 struct ib_grh *grh, struct cm_av *av)
343 av->pkey_index = wc->pkey_index;
344 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
348 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
350 struct cm_device *cm_dev;
351 struct cm_port *port = NULL;
356 read_lock_irqsave(&cm.device_lock, flags);
357 list_for_each_entry(cm_dev, &cm.device_list, list) {
358 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
360 port = cm_dev->port[p-1];
364 read_unlock_irqrestore(&cm.device_lock, flags);
369 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
370 be16_to_cpu(path->pkey), &av->pkey_index);
375 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
377 av->timeout = path->packet_life_time + 1;
381 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
388 spin_lock_irqsave(&cm.lock, flags);
389 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
392 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
393 spin_unlock_irqrestore(&cm.lock, flags);
394 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
396 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
400 static void cm_free_id(__be32 local_id)
402 spin_lock_irq(&cm.lock);
403 idr_remove(&cm.local_id_table,
404 (__force int) (local_id ^ cm.random_id_operand));
405 spin_unlock_irq(&cm.lock);
408 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
410 struct cm_id_private *cm_id_priv;
412 cm_id_priv = idr_find(&cm.local_id_table,
413 (__force int) (local_id ^ cm.random_id_operand));
415 if (cm_id_priv->id.remote_id == remote_id)
416 atomic_inc(&cm_id_priv->refcount);
424 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
426 struct cm_id_private *cm_id_priv;
428 spin_lock_irq(&cm.lock);
429 cm_id_priv = cm_get_id(local_id, remote_id);
430 spin_unlock_irq(&cm.lock);
435 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
439 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
440 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
441 ((unsigned long *) mask)[i];
444 static int cm_compare_data(struct ib_cm_compare_data *src_data,
445 struct ib_cm_compare_data *dst_data)
447 u8 src[IB_CM_COMPARE_SIZE];
448 u8 dst[IB_CM_COMPARE_SIZE];
450 if (!src_data || !dst_data)
453 cm_mask_copy(src, src_data->data, dst_data->mask);
454 cm_mask_copy(dst, dst_data->data, src_data->mask);
455 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
458 static int cm_compare_private_data(u8 *private_data,
459 struct ib_cm_compare_data *dst_data)
461 u8 src[IB_CM_COMPARE_SIZE];
466 cm_mask_copy(src, private_data, dst_data->mask);
467 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
471 * Trivial helpers to strip endian annotation and compare; the
472 * endianness doesn't actually matter since we just need a stable
473 * order for the RB tree.
475 static int be32_lt(__be32 a, __be32 b)
477 return (__force u32) a < (__force u32) b;
480 static int be32_gt(__be32 a, __be32 b)
482 return (__force u32) a > (__force u32) b;
485 static int be64_lt(__be64 a, __be64 b)
487 return (__force u64) a < (__force u64) b;
490 static int be64_gt(__be64 a, __be64 b)
492 return (__force u64) a > (__force u64) b;
495 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
497 struct rb_node **link = &cm.listen_service_table.rb_node;
498 struct rb_node *parent = NULL;
499 struct cm_id_private *cur_cm_id_priv;
500 __be64 service_id = cm_id_priv->id.service_id;
501 __be64 service_mask = cm_id_priv->id.service_mask;
506 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
508 data_cmp = cm_compare_data(cm_id_priv->compare_data,
509 cur_cm_id_priv->compare_data);
510 if ((cur_cm_id_priv->id.service_mask & service_id) ==
511 (service_mask & cur_cm_id_priv->id.service_id) &&
512 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
514 return cur_cm_id_priv;
516 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
517 link = &(*link)->rb_left;
518 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
519 link = &(*link)->rb_right;
520 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
521 link = &(*link)->rb_left;
522 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
523 link = &(*link)->rb_right;
524 else if (data_cmp < 0)
525 link = &(*link)->rb_left;
527 link = &(*link)->rb_right;
529 rb_link_node(&cm_id_priv->service_node, parent, link);
530 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
534 static struct cm_id_private * cm_find_listen(struct ib_device *device,
538 struct rb_node *node = cm.listen_service_table.rb_node;
539 struct cm_id_private *cm_id_priv;
543 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
544 data_cmp = cm_compare_private_data(private_data,
545 cm_id_priv->compare_data);
546 if ((cm_id_priv->id.service_mask & service_id) ==
547 cm_id_priv->id.service_id &&
548 (cm_id_priv->id.device == device) && !data_cmp)
551 if (device < cm_id_priv->id.device)
552 node = node->rb_left;
553 else if (device > cm_id_priv->id.device)
554 node = node->rb_right;
555 else if (be64_lt(service_id, cm_id_priv->id.service_id))
556 node = node->rb_left;
557 else if (be64_gt(service_id, cm_id_priv->id.service_id))
558 node = node->rb_right;
559 else if (data_cmp < 0)
560 node = node->rb_left;
562 node = node->rb_right;
567 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
570 struct rb_node **link = &cm.remote_id_table.rb_node;
571 struct rb_node *parent = NULL;
572 struct cm_timewait_info *cur_timewait_info;
573 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
574 __be32 remote_id = timewait_info->work.remote_id;
578 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
580 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
581 link = &(*link)->rb_left;
582 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
583 link = &(*link)->rb_right;
584 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
585 link = &(*link)->rb_left;
586 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
587 link = &(*link)->rb_right;
589 return cur_timewait_info;
591 timewait_info->inserted_remote_id = 1;
592 rb_link_node(&timewait_info->remote_id_node, parent, link);
593 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
597 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
600 struct rb_node *node = cm.remote_id_table.rb_node;
601 struct cm_timewait_info *timewait_info;
604 timewait_info = rb_entry(node, struct cm_timewait_info,
606 if (be32_lt(remote_id, timewait_info->work.remote_id))
607 node = node->rb_left;
608 else if (be32_gt(remote_id, timewait_info->work.remote_id))
609 node = node->rb_right;
610 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
611 node = node->rb_left;
612 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
613 node = node->rb_right;
615 return timewait_info;
620 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
623 struct rb_node **link = &cm.remote_qp_table.rb_node;
624 struct rb_node *parent = NULL;
625 struct cm_timewait_info *cur_timewait_info;
626 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
627 __be32 remote_qpn = timewait_info->remote_qpn;
631 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
633 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
634 link = &(*link)->rb_left;
635 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
636 link = &(*link)->rb_right;
637 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
638 link = &(*link)->rb_left;
639 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
640 link = &(*link)->rb_right;
642 return cur_timewait_info;
644 timewait_info->inserted_remote_qp = 1;
645 rb_link_node(&timewait_info->remote_qp_node, parent, link);
646 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
650 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
653 struct rb_node **link = &cm.remote_sidr_table.rb_node;
654 struct rb_node *parent = NULL;
655 struct cm_id_private *cur_cm_id_priv;
656 union ib_gid *port_gid = &cm_id_priv->av.dgid;
657 __be32 remote_id = cm_id_priv->id.remote_id;
661 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
663 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
664 link = &(*link)->rb_left;
665 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
666 link = &(*link)->rb_right;
669 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
672 link = &(*link)->rb_left;
674 link = &(*link)->rb_right;
676 return cur_cm_id_priv;
679 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
680 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
684 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
685 enum ib_cm_sidr_status status)
687 struct ib_cm_sidr_rep_param param;
689 memset(¶m, 0, sizeof param);
690 param.status = status;
691 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
694 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
695 ib_cm_handler cm_handler,
698 struct cm_id_private *cm_id_priv;
701 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
703 return ERR_PTR(-ENOMEM);
705 cm_id_priv->id.state = IB_CM_IDLE;
706 cm_id_priv->id.device = device;
707 cm_id_priv->id.cm_handler = cm_handler;
708 cm_id_priv->id.context = context;
709 cm_id_priv->id.remote_cm_qpn = 1;
710 ret = cm_alloc_id(cm_id_priv);
714 spin_lock_init(&cm_id_priv->lock);
715 init_completion(&cm_id_priv->comp);
716 INIT_LIST_HEAD(&cm_id_priv->work_list);
717 atomic_set(&cm_id_priv->work_count, -1);
718 atomic_set(&cm_id_priv->refcount, 1);
719 return &cm_id_priv->id;
723 return ERR_PTR(-ENOMEM);
725 EXPORT_SYMBOL(ib_create_cm_id);
727 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
729 struct cm_work *work;
731 if (list_empty(&cm_id_priv->work_list))
734 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
735 list_del(&work->list);
739 static void cm_free_work(struct cm_work *work)
741 if (work->mad_recv_wc)
742 ib_free_recv_mad(work->mad_recv_wc);
746 static inline int cm_convert_to_ms(int iba_time)
748 /* approximate conversion to ms from 4.096us x 2^iba_time */
749 return 1 << max(iba_time - 8, 0);
753 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
754 * Because of how ack_timeout is stored, adding one doubles the timeout.
755 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
756 * increment it (round up) only if the other is within 50%.
758 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
760 int ack_timeout = packet_life_time + 1;
762 if (ack_timeout >= ca_ack_delay)
763 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
765 ack_timeout = ca_ack_delay +
766 (ack_timeout >= (ca_ack_delay - 1));
768 return min(31, ack_timeout);
771 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
773 if (timewait_info->inserted_remote_id) {
774 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
775 timewait_info->inserted_remote_id = 0;
778 if (timewait_info->inserted_remote_qp) {
779 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
780 timewait_info->inserted_remote_qp = 0;
784 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
786 struct cm_timewait_info *timewait_info;
788 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
790 return ERR_PTR(-ENOMEM);
792 timewait_info->work.local_id = local_id;
793 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
794 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
795 return timewait_info;
798 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
803 spin_lock_irqsave(&cm.lock, flags);
804 cm_cleanup_timewait(cm_id_priv->timewait_info);
805 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
806 spin_unlock_irqrestore(&cm.lock, flags);
809 * The cm_id could be destroyed by the user before we exit timewait.
810 * To protect against this, we search for the cm_id after exiting
811 * timewait before notifying the user that we've exited timewait.
813 cm_id_priv->id.state = IB_CM_TIMEWAIT;
814 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
815 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
816 msecs_to_jiffies(wait_time));
817 cm_id_priv->timewait_info = NULL;
820 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
824 cm_id_priv->id.state = IB_CM_IDLE;
825 if (cm_id_priv->timewait_info) {
826 spin_lock_irqsave(&cm.lock, flags);
827 cm_cleanup_timewait(cm_id_priv->timewait_info);
828 spin_unlock_irqrestore(&cm.lock, flags);
829 kfree(cm_id_priv->timewait_info);
830 cm_id_priv->timewait_info = NULL;
834 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
836 struct cm_id_private *cm_id_priv;
837 struct cm_work *work;
839 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
841 spin_lock_irq(&cm_id_priv->lock);
842 switch (cm_id->state) {
844 cm_id->state = IB_CM_IDLE;
845 spin_unlock_irq(&cm_id_priv->lock);
846 spin_lock_irq(&cm.lock);
847 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
848 spin_unlock_irq(&cm.lock);
850 case IB_CM_SIDR_REQ_SENT:
851 cm_id->state = IB_CM_IDLE;
852 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
853 spin_unlock_irq(&cm_id_priv->lock);
855 case IB_CM_SIDR_REQ_RCVD:
856 spin_unlock_irq(&cm_id_priv->lock);
857 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
860 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
861 spin_unlock_irq(&cm_id_priv->lock);
862 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
863 &cm_id_priv->id.device->node_guid,
864 sizeof cm_id_priv->id.device->node_guid,
868 if (err == -ENOMEM) {
869 /* Do not reject to allow future retries. */
870 cm_reset_to_idle(cm_id_priv);
871 spin_unlock_irq(&cm_id_priv->lock);
873 spin_unlock_irq(&cm_id_priv->lock);
874 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
878 case IB_CM_MRA_REQ_RCVD:
880 case IB_CM_MRA_REP_RCVD:
881 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
883 case IB_CM_MRA_REQ_SENT:
885 case IB_CM_MRA_REP_SENT:
886 spin_unlock_irq(&cm_id_priv->lock);
887 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
890 case IB_CM_ESTABLISHED:
891 spin_unlock_irq(&cm_id_priv->lock);
892 ib_send_cm_dreq(cm_id, NULL, 0);
894 case IB_CM_DREQ_SENT:
895 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
896 cm_enter_timewait(cm_id_priv);
897 spin_unlock_irq(&cm_id_priv->lock);
899 case IB_CM_DREQ_RCVD:
900 spin_unlock_irq(&cm_id_priv->lock);
901 ib_send_cm_drep(cm_id, NULL, 0);
904 spin_unlock_irq(&cm_id_priv->lock);
908 cm_free_id(cm_id->local_id);
909 cm_deref_id(cm_id_priv);
910 wait_for_completion(&cm_id_priv->comp);
911 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
913 kfree(cm_id_priv->compare_data);
914 kfree(cm_id_priv->private_data);
918 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
920 cm_destroy_id(cm_id, 0);
922 EXPORT_SYMBOL(ib_destroy_cm_id);
924 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
925 struct ib_cm_compare_data *compare_data)
927 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
931 service_mask = service_mask ? service_mask :
932 __constant_cpu_to_be64(~0ULL);
933 service_id &= service_mask;
934 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
935 (service_id != IB_CM_ASSIGN_SERVICE_ID))
938 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
939 if (cm_id->state != IB_CM_IDLE)
943 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
945 if (!cm_id_priv->compare_data)
947 cm_mask_copy(cm_id_priv->compare_data->data,
948 compare_data->data, compare_data->mask);
949 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
953 cm_id->state = IB_CM_LISTEN;
955 spin_lock_irqsave(&cm.lock, flags);
956 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
957 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
958 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
960 cm_id->service_id = service_id;
961 cm_id->service_mask = service_mask;
963 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
964 spin_unlock_irqrestore(&cm.lock, flags);
966 if (cur_cm_id_priv) {
967 cm_id->state = IB_CM_IDLE;
968 kfree(cm_id_priv->compare_data);
969 cm_id_priv->compare_data = NULL;
974 EXPORT_SYMBOL(ib_cm_listen);
976 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
977 enum cm_msg_sequence msg_seq)
981 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
982 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
984 return cpu_to_be64(hi_tid | low_tid);
987 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
988 __be16 attr_id, __be64 tid)
990 hdr->base_version = IB_MGMT_BASE_VERSION;
991 hdr->mgmt_class = IB_MGMT_CLASS_CM;
992 hdr->class_version = IB_CM_CLASS_VERSION;
993 hdr->method = IB_MGMT_METHOD_SEND;
994 hdr->attr_id = attr_id;
998 static void cm_format_req(struct cm_req_msg *req_msg,
999 struct cm_id_private *cm_id_priv,
1000 struct ib_cm_req_param *param)
1002 struct ib_sa_path_rec *pri_path = param->primary_path;
1003 struct ib_sa_path_rec *alt_path = param->alternate_path;
1005 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1006 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1008 req_msg->local_comm_id = cm_id_priv->id.local_id;
1009 req_msg->service_id = param->service_id;
1010 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1011 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1012 cm_req_set_resp_res(req_msg, param->responder_resources);
1013 cm_req_set_init_depth(req_msg, param->initiator_depth);
1014 cm_req_set_remote_resp_timeout(req_msg,
1015 param->remote_cm_response_timeout);
1016 cm_req_set_qp_type(req_msg, param->qp_type);
1017 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1018 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1019 cm_req_set_local_resp_timeout(req_msg,
1020 param->local_cm_response_timeout);
1021 cm_req_set_retry_count(req_msg, param->retry_count);
1022 req_msg->pkey = param->primary_path->pkey;
1023 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1024 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1025 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1026 cm_req_set_srq(req_msg, param->srq);
1028 if (pri_path->hop_limit <= 1) {
1029 req_msg->primary_local_lid = pri_path->slid;
1030 req_msg->primary_remote_lid = pri_path->dlid;
1032 /* Work-around until there's a way to obtain remote LID info */
1033 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1034 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1036 req_msg->primary_local_gid = pri_path->sgid;
1037 req_msg->primary_remote_gid = pri_path->dgid;
1038 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1039 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1040 req_msg->primary_traffic_class = pri_path->traffic_class;
1041 req_msg->primary_hop_limit = pri_path->hop_limit;
1042 cm_req_set_primary_sl(req_msg, pri_path->sl);
1043 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1044 cm_req_set_primary_local_ack_timeout(req_msg,
1045 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1046 pri_path->packet_life_time));
1049 if (alt_path->hop_limit <= 1) {
1050 req_msg->alt_local_lid = alt_path->slid;
1051 req_msg->alt_remote_lid = alt_path->dlid;
1053 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1054 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1056 req_msg->alt_local_gid = alt_path->sgid;
1057 req_msg->alt_remote_gid = alt_path->dgid;
1058 cm_req_set_alt_flow_label(req_msg,
1059 alt_path->flow_label);
1060 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1061 req_msg->alt_traffic_class = alt_path->traffic_class;
1062 req_msg->alt_hop_limit = alt_path->hop_limit;
1063 cm_req_set_alt_sl(req_msg, alt_path->sl);
1064 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1065 cm_req_set_alt_local_ack_timeout(req_msg,
1066 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1067 alt_path->packet_life_time));
1070 if (param->private_data && param->private_data_len)
1071 memcpy(req_msg->private_data, param->private_data,
1072 param->private_data_len);
1075 static int cm_validate_req_param(struct ib_cm_req_param *param)
1077 /* peer-to-peer not supported */
1078 if (param->peer_to_peer)
1081 if (!param->primary_path)
1084 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1087 if (param->private_data &&
1088 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1091 if (param->alternate_path &&
1092 (param->alternate_path->pkey != param->primary_path->pkey ||
1093 param->alternate_path->mtu != param->primary_path->mtu))
1099 int ib_send_cm_req(struct ib_cm_id *cm_id,
1100 struct ib_cm_req_param *param)
1102 struct cm_id_private *cm_id_priv;
1103 struct cm_req_msg *req_msg;
1104 unsigned long flags;
1107 ret = cm_validate_req_param(param);
1111 /* Verify that we're not in timewait. */
1112 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1113 spin_lock_irqsave(&cm_id_priv->lock, flags);
1114 if (cm_id->state != IB_CM_IDLE) {
1115 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1119 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1121 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1123 if (IS_ERR(cm_id_priv->timewait_info)) {
1124 ret = PTR_ERR(cm_id_priv->timewait_info);
1128 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1131 if (param->alternate_path) {
1132 ret = cm_init_av_by_path(param->alternate_path,
1133 &cm_id_priv->alt_av);
1137 cm_id->service_id = param->service_id;
1138 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1139 cm_id_priv->timeout_ms = cm_convert_to_ms(
1140 param->primary_path->packet_life_time) * 2 +
1142 param->remote_cm_response_timeout);
1143 cm_id_priv->max_cm_retries = param->max_cm_retries;
1144 cm_id_priv->initiator_depth = param->initiator_depth;
1145 cm_id_priv->responder_resources = param->responder_resources;
1146 cm_id_priv->retry_count = param->retry_count;
1147 cm_id_priv->path_mtu = param->primary_path->mtu;
1148 cm_id_priv->pkey = param->primary_path->pkey;
1149 cm_id_priv->qp_type = param->qp_type;
1151 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1155 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1156 cm_format_req(req_msg, cm_id_priv, param);
1157 cm_id_priv->tid = req_msg->hdr.tid;
1158 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1159 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1161 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1162 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1164 spin_lock_irqsave(&cm_id_priv->lock, flags);
1165 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1167 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1170 BUG_ON(cm_id->state != IB_CM_IDLE);
1171 cm_id->state = IB_CM_REQ_SENT;
1172 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1175 error2: cm_free_msg(cm_id_priv->msg);
1176 error1: kfree(cm_id_priv->timewait_info);
1179 EXPORT_SYMBOL(ib_send_cm_req);
1181 static int cm_issue_rej(struct cm_port *port,
1182 struct ib_mad_recv_wc *mad_recv_wc,
1183 enum ib_cm_rej_reason reason,
1184 enum cm_msg_response msg_rejected,
1185 void *ari, u8 ari_length)
1187 struct ib_mad_send_buf *msg = NULL;
1188 struct cm_rej_msg *rej_msg, *rcv_msg;
1191 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1195 /* We just need common CM header information. Cast to any message. */
1196 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1197 rej_msg = (struct cm_rej_msg *) msg->mad;
1199 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1200 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1201 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1202 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1203 rej_msg->reason = cpu_to_be16(reason);
1205 if (ari && ari_length) {
1206 cm_rej_set_reject_info_len(rej_msg, ari_length);
1207 memcpy(rej_msg->ari, ari, ari_length);
1210 ret = ib_post_send_mad(msg, NULL);
1217 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1218 __be32 local_qpn, __be32 remote_qpn)
1220 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1221 ((local_ca_guid == remote_ca_guid) &&
1222 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1225 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1226 struct ib_sa_path_rec *primary_path,
1227 struct ib_sa_path_rec *alt_path)
1229 memset(primary_path, 0, sizeof *primary_path);
1230 primary_path->dgid = req_msg->primary_local_gid;
1231 primary_path->sgid = req_msg->primary_remote_gid;
1232 primary_path->dlid = req_msg->primary_local_lid;
1233 primary_path->slid = req_msg->primary_remote_lid;
1234 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1235 primary_path->hop_limit = req_msg->primary_hop_limit;
1236 primary_path->traffic_class = req_msg->primary_traffic_class;
1237 primary_path->reversible = 1;
1238 primary_path->pkey = req_msg->pkey;
1239 primary_path->sl = cm_req_get_primary_sl(req_msg);
1240 primary_path->mtu_selector = IB_SA_EQ;
1241 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1242 primary_path->rate_selector = IB_SA_EQ;
1243 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1244 primary_path->packet_life_time_selector = IB_SA_EQ;
1245 primary_path->packet_life_time =
1246 cm_req_get_primary_local_ack_timeout(req_msg);
1247 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1249 if (req_msg->alt_local_lid) {
1250 memset(alt_path, 0, sizeof *alt_path);
1251 alt_path->dgid = req_msg->alt_local_gid;
1252 alt_path->sgid = req_msg->alt_remote_gid;
1253 alt_path->dlid = req_msg->alt_local_lid;
1254 alt_path->slid = req_msg->alt_remote_lid;
1255 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1256 alt_path->hop_limit = req_msg->alt_hop_limit;
1257 alt_path->traffic_class = req_msg->alt_traffic_class;
1258 alt_path->reversible = 1;
1259 alt_path->pkey = req_msg->pkey;
1260 alt_path->sl = cm_req_get_alt_sl(req_msg);
1261 alt_path->mtu_selector = IB_SA_EQ;
1262 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1263 alt_path->rate_selector = IB_SA_EQ;
1264 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1265 alt_path->packet_life_time_selector = IB_SA_EQ;
1266 alt_path->packet_life_time =
1267 cm_req_get_alt_local_ack_timeout(req_msg);
1268 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1272 static void cm_format_req_event(struct cm_work *work,
1273 struct cm_id_private *cm_id_priv,
1274 struct ib_cm_id *listen_id)
1276 struct cm_req_msg *req_msg;
1277 struct ib_cm_req_event_param *param;
1279 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1280 param = &work->cm_event.param.req_rcvd;
1281 param->listen_id = listen_id;
1282 param->port = cm_id_priv->av.port->port_num;
1283 param->primary_path = &work->path[0];
1284 if (req_msg->alt_local_lid)
1285 param->alternate_path = &work->path[1];
1287 param->alternate_path = NULL;
1288 param->remote_ca_guid = req_msg->local_ca_guid;
1289 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1290 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1291 param->qp_type = cm_req_get_qp_type(req_msg);
1292 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1293 param->responder_resources = cm_req_get_init_depth(req_msg);
1294 param->initiator_depth = cm_req_get_resp_res(req_msg);
1295 param->local_cm_response_timeout =
1296 cm_req_get_remote_resp_timeout(req_msg);
1297 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1298 param->remote_cm_response_timeout =
1299 cm_req_get_local_resp_timeout(req_msg);
1300 param->retry_count = cm_req_get_retry_count(req_msg);
1301 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1302 param->srq = cm_req_get_srq(req_msg);
1303 work->cm_event.private_data = &req_msg->private_data;
1306 static void cm_process_work(struct cm_id_private *cm_id_priv,
1307 struct cm_work *work)
1311 /* We will typically only have the current event to report. */
1312 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1315 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1316 spin_lock_irq(&cm_id_priv->lock);
1317 work = cm_dequeue_work(cm_id_priv);
1318 spin_unlock_irq(&cm_id_priv->lock);
1320 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1324 cm_deref_id(cm_id_priv);
1326 cm_destroy_id(&cm_id_priv->id, ret);
1329 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1330 struct cm_id_private *cm_id_priv,
1331 enum cm_msg_response msg_mraed, u8 service_timeout,
1332 const void *private_data, u8 private_data_len)
1334 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1335 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1336 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1337 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1338 cm_mra_set_service_timeout(mra_msg, service_timeout);
1340 if (private_data && private_data_len)
1341 memcpy(mra_msg->private_data, private_data, private_data_len);
1344 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1345 struct cm_id_private *cm_id_priv,
1346 enum ib_cm_rej_reason reason,
1349 const void *private_data,
1350 u8 private_data_len)
1352 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1353 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1355 switch(cm_id_priv->id.state) {
1356 case IB_CM_REQ_RCVD:
1357 rej_msg->local_comm_id = 0;
1358 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1360 case IB_CM_MRA_REQ_SENT:
1361 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1362 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1364 case IB_CM_REP_RCVD:
1365 case IB_CM_MRA_REP_SENT:
1366 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1367 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1370 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1371 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1375 rej_msg->reason = cpu_to_be16(reason);
1376 if (ari && ari_length) {
1377 cm_rej_set_reject_info_len(rej_msg, ari_length);
1378 memcpy(rej_msg->ari, ari, ari_length);
1381 if (private_data && private_data_len)
1382 memcpy(rej_msg->private_data, private_data, private_data_len);
1385 static void cm_dup_req_handler(struct cm_work *work,
1386 struct cm_id_private *cm_id_priv)
1388 struct ib_mad_send_buf *msg = NULL;
1391 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1392 counter[CM_REQ_COUNTER]);
1394 /* Quick state check to discard duplicate REQs. */
1395 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1398 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1402 spin_lock_irq(&cm_id_priv->lock);
1403 switch (cm_id_priv->id.state) {
1404 case IB_CM_MRA_REQ_SENT:
1405 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1406 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1407 cm_id_priv->private_data,
1408 cm_id_priv->private_data_len);
1410 case IB_CM_TIMEWAIT:
1411 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1412 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1417 spin_unlock_irq(&cm_id_priv->lock);
1419 ret = ib_post_send_mad(msg, NULL);
1424 unlock: spin_unlock_irq(&cm_id_priv->lock);
1425 free: cm_free_msg(msg);
1428 static struct cm_id_private * cm_match_req(struct cm_work *work,
1429 struct cm_id_private *cm_id_priv)
1431 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1432 struct cm_timewait_info *timewait_info;
1433 struct cm_req_msg *req_msg;
1435 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1437 /* Check for possible duplicate REQ. */
1438 spin_lock_irq(&cm.lock);
1439 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1440 if (timewait_info) {
1441 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1442 timewait_info->work.remote_id);
1443 spin_unlock_irq(&cm.lock);
1444 if (cur_cm_id_priv) {
1445 cm_dup_req_handler(work, cur_cm_id_priv);
1446 cm_deref_id(cur_cm_id_priv);
1451 /* Check for stale connections. */
1452 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1453 if (timewait_info) {
1454 cm_cleanup_timewait(cm_id_priv->timewait_info);
1455 spin_unlock_irq(&cm.lock);
1456 cm_issue_rej(work->port, work->mad_recv_wc,
1457 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1462 /* Find matching listen request. */
1463 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1464 req_msg->service_id,
1465 req_msg->private_data);
1466 if (!listen_cm_id_priv) {
1467 cm_cleanup_timewait(cm_id_priv->timewait_info);
1468 spin_unlock_irq(&cm.lock);
1469 cm_issue_rej(work->port, work->mad_recv_wc,
1470 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1474 atomic_inc(&listen_cm_id_priv->refcount);
1475 atomic_inc(&cm_id_priv->refcount);
1476 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1477 atomic_inc(&cm_id_priv->work_count);
1478 spin_unlock_irq(&cm.lock);
1480 return listen_cm_id_priv;
1484 * Work-around for inter-subnet connections. If the LIDs are permissive,
1485 * we need to override the LID/SL data in the REQ with the LID information
1486 * in the work completion.
1488 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1490 if (!cm_req_get_primary_subnet_local(req_msg)) {
1491 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1492 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1493 cm_req_set_primary_sl(req_msg, wc->sl);
1496 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1497 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1500 if (!cm_req_get_alt_subnet_local(req_msg)) {
1501 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1502 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1503 cm_req_set_alt_sl(req_msg, wc->sl);
1506 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1507 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1511 static int cm_req_handler(struct cm_work *work)
1513 struct ib_cm_id *cm_id;
1514 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1515 struct cm_req_msg *req_msg;
1518 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1520 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1522 return PTR_ERR(cm_id);
1524 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1525 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1526 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1527 work->mad_recv_wc->recv_buf.grh,
1529 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1531 if (IS_ERR(cm_id_priv->timewait_info)) {
1532 ret = PTR_ERR(cm_id_priv->timewait_info);
1535 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1536 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1537 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1539 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1540 if (!listen_cm_id_priv) {
1542 kfree(cm_id_priv->timewait_info);
1546 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1547 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1548 cm_id_priv->id.service_id = req_msg->service_id;
1549 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1551 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1552 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1553 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1555 ib_get_cached_gid(work->port->cm_dev->device,
1556 work->port->port_num, 0, &work->path[0].sgid);
1557 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1558 &work->path[0].sgid, sizeof work->path[0].sgid,
1562 if (req_msg->alt_local_lid) {
1563 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1565 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1566 &work->path[0].sgid,
1567 sizeof work->path[0].sgid, NULL, 0);
1571 cm_id_priv->tid = req_msg->hdr.tid;
1572 cm_id_priv->timeout_ms = cm_convert_to_ms(
1573 cm_req_get_local_resp_timeout(req_msg));
1574 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1575 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1576 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1577 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1578 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1579 cm_id_priv->pkey = req_msg->pkey;
1580 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1581 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1582 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1583 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1585 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1586 cm_process_work(cm_id_priv, work);
1587 cm_deref_id(listen_cm_id_priv);
1591 atomic_dec(&cm_id_priv->refcount);
1592 cm_deref_id(listen_cm_id_priv);
1594 ib_destroy_cm_id(cm_id);
1598 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1599 struct cm_id_private *cm_id_priv,
1600 struct ib_cm_rep_param *param)
1602 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1603 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1604 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1605 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1606 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1607 rep_msg->resp_resources = param->responder_resources;
1608 rep_msg->initiator_depth = param->initiator_depth;
1609 cm_rep_set_target_ack_delay(rep_msg,
1610 cm_id_priv->av.port->cm_dev->ack_delay);
1611 cm_rep_set_failover(rep_msg, param->failover_accepted);
1612 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1613 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1614 cm_rep_set_srq(rep_msg, param->srq);
1615 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1617 if (param->private_data && param->private_data_len)
1618 memcpy(rep_msg->private_data, param->private_data,
1619 param->private_data_len);
1622 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1623 struct ib_cm_rep_param *param)
1625 struct cm_id_private *cm_id_priv;
1626 struct ib_mad_send_buf *msg;
1627 struct cm_rep_msg *rep_msg;
1628 unsigned long flags;
1631 if (param->private_data &&
1632 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1635 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1636 spin_lock_irqsave(&cm_id_priv->lock, flags);
1637 if (cm_id->state != IB_CM_REQ_RCVD &&
1638 cm_id->state != IB_CM_MRA_REQ_SENT) {
1643 ret = cm_alloc_msg(cm_id_priv, &msg);
1647 rep_msg = (struct cm_rep_msg *) msg->mad;
1648 cm_format_rep(rep_msg, cm_id_priv, param);
1649 msg->timeout_ms = cm_id_priv->timeout_ms;
1650 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1652 ret = ib_post_send_mad(msg, NULL);
1654 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1659 cm_id->state = IB_CM_REP_SENT;
1660 cm_id_priv->msg = msg;
1661 cm_id_priv->initiator_depth = param->initiator_depth;
1662 cm_id_priv->responder_resources = param->responder_resources;
1663 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1664 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1666 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1669 EXPORT_SYMBOL(ib_send_cm_rep);
1671 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1672 struct cm_id_private *cm_id_priv,
1673 const void *private_data,
1674 u8 private_data_len)
1676 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1677 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1678 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1680 if (private_data && private_data_len)
1681 memcpy(rtu_msg->private_data, private_data, private_data_len);
1684 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1685 const void *private_data,
1686 u8 private_data_len)
1688 struct cm_id_private *cm_id_priv;
1689 struct ib_mad_send_buf *msg;
1690 unsigned long flags;
1694 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1697 data = cm_copy_private_data(private_data, private_data_len);
1699 return PTR_ERR(data);
1701 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1702 spin_lock_irqsave(&cm_id_priv->lock, flags);
1703 if (cm_id->state != IB_CM_REP_RCVD &&
1704 cm_id->state != IB_CM_MRA_REP_SENT) {
1709 ret = cm_alloc_msg(cm_id_priv, &msg);
1713 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1714 private_data, private_data_len);
1716 ret = ib_post_send_mad(msg, NULL);
1718 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1724 cm_id->state = IB_CM_ESTABLISHED;
1725 cm_set_private_data(cm_id_priv, data, private_data_len);
1726 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1729 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1733 EXPORT_SYMBOL(ib_send_cm_rtu);
1735 static void cm_format_rep_event(struct cm_work *work)
1737 struct cm_rep_msg *rep_msg;
1738 struct ib_cm_rep_event_param *param;
1740 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1741 param = &work->cm_event.param.rep_rcvd;
1742 param->remote_ca_guid = rep_msg->local_ca_guid;
1743 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1744 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1745 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1746 param->responder_resources = rep_msg->initiator_depth;
1747 param->initiator_depth = rep_msg->resp_resources;
1748 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1749 param->failover_accepted = cm_rep_get_failover(rep_msg);
1750 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1751 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1752 param->srq = cm_rep_get_srq(rep_msg);
1753 work->cm_event.private_data = &rep_msg->private_data;
1756 static void cm_dup_rep_handler(struct cm_work *work)
1758 struct cm_id_private *cm_id_priv;
1759 struct cm_rep_msg *rep_msg;
1760 struct ib_mad_send_buf *msg = NULL;
1763 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1764 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1765 rep_msg->local_comm_id);
1769 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1770 counter[CM_REP_COUNTER]);
1771 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1775 spin_lock_irq(&cm_id_priv->lock);
1776 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1777 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1778 cm_id_priv->private_data,
1779 cm_id_priv->private_data_len);
1780 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1781 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1782 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1783 cm_id_priv->private_data,
1784 cm_id_priv->private_data_len);
1787 spin_unlock_irq(&cm_id_priv->lock);
1789 ret = ib_post_send_mad(msg, NULL);
1794 unlock: spin_unlock_irq(&cm_id_priv->lock);
1795 free: cm_free_msg(msg);
1796 deref: cm_deref_id(cm_id_priv);
1799 static int cm_rep_handler(struct cm_work *work)
1801 struct cm_id_private *cm_id_priv;
1802 struct cm_rep_msg *rep_msg;
1805 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1806 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1808 cm_dup_rep_handler(work);
1812 cm_format_rep_event(work);
1814 spin_lock_irq(&cm_id_priv->lock);
1815 switch (cm_id_priv->id.state) {
1816 case IB_CM_REQ_SENT:
1817 case IB_CM_MRA_REQ_RCVD:
1820 spin_unlock_irq(&cm_id_priv->lock);
1825 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1826 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1827 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1829 spin_lock(&cm.lock);
1830 /* Check for duplicate REP. */
1831 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1832 spin_unlock(&cm.lock);
1833 spin_unlock_irq(&cm_id_priv->lock);
1837 /* Check for a stale connection. */
1838 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1839 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1840 &cm.remote_id_table);
1841 cm_id_priv->timewait_info->inserted_remote_id = 0;
1842 spin_unlock(&cm.lock);
1843 spin_unlock_irq(&cm_id_priv->lock);
1844 cm_issue_rej(work->port, work->mad_recv_wc,
1845 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1850 spin_unlock(&cm.lock);
1852 cm_id_priv->id.state = IB_CM_REP_RCVD;
1853 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1854 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1855 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1856 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1857 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1858 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1859 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1860 cm_id_priv->av.timeout =
1861 cm_ack_timeout(cm_id_priv->target_ack_delay,
1862 cm_id_priv->av.timeout - 1);
1863 cm_id_priv->alt_av.timeout =
1864 cm_ack_timeout(cm_id_priv->target_ack_delay,
1865 cm_id_priv->alt_av.timeout - 1);
1867 /* todo: handle peer_to_peer */
1869 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1870 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1872 list_add_tail(&work->list, &cm_id_priv->work_list);
1873 spin_unlock_irq(&cm_id_priv->lock);
1876 cm_process_work(cm_id_priv, work);
1878 cm_deref_id(cm_id_priv);
1882 cm_deref_id(cm_id_priv);
1886 static int cm_establish_handler(struct cm_work *work)
1888 struct cm_id_private *cm_id_priv;
1891 /* See comment in cm_establish about lookup. */
1892 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1896 spin_lock_irq(&cm_id_priv->lock);
1897 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1898 spin_unlock_irq(&cm_id_priv->lock);
1902 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1903 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1905 list_add_tail(&work->list, &cm_id_priv->work_list);
1906 spin_unlock_irq(&cm_id_priv->lock);
1909 cm_process_work(cm_id_priv, work);
1911 cm_deref_id(cm_id_priv);
1914 cm_deref_id(cm_id_priv);
1918 static int cm_rtu_handler(struct cm_work *work)
1920 struct cm_id_private *cm_id_priv;
1921 struct cm_rtu_msg *rtu_msg;
1924 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1925 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1926 rtu_msg->local_comm_id);
1930 work->cm_event.private_data = &rtu_msg->private_data;
1932 spin_lock_irq(&cm_id_priv->lock);
1933 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1934 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1935 spin_unlock_irq(&cm_id_priv->lock);
1936 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1937 counter[CM_RTU_COUNTER]);
1940 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1942 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1943 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1945 list_add_tail(&work->list, &cm_id_priv->work_list);
1946 spin_unlock_irq(&cm_id_priv->lock);
1949 cm_process_work(cm_id_priv, work);
1951 cm_deref_id(cm_id_priv);
1954 cm_deref_id(cm_id_priv);
1958 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1959 struct cm_id_private *cm_id_priv,
1960 const void *private_data,
1961 u8 private_data_len)
1963 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1964 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1965 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1966 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1967 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1969 if (private_data && private_data_len)
1970 memcpy(dreq_msg->private_data, private_data, private_data_len);
1973 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1974 const void *private_data,
1975 u8 private_data_len)
1977 struct cm_id_private *cm_id_priv;
1978 struct ib_mad_send_buf *msg;
1979 unsigned long flags;
1982 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1985 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1986 spin_lock_irqsave(&cm_id_priv->lock, flags);
1987 if (cm_id->state != IB_CM_ESTABLISHED) {
1992 ret = cm_alloc_msg(cm_id_priv, &msg);
1994 cm_enter_timewait(cm_id_priv);
1998 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1999 private_data, private_data_len);
2000 msg->timeout_ms = cm_id_priv->timeout_ms;
2001 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2003 ret = ib_post_send_mad(msg, NULL);
2005 cm_enter_timewait(cm_id_priv);
2006 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2011 cm_id->state = IB_CM_DREQ_SENT;
2012 cm_id_priv->msg = msg;
2013 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2016 EXPORT_SYMBOL(ib_send_cm_dreq);
2018 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2019 struct cm_id_private *cm_id_priv,
2020 const void *private_data,
2021 u8 private_data_len)
2023 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2024 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2025 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2027 if (private_data && private_data_len)
2028 memcpy(drep_msg->private_data, private_data, private_data_len);
2031 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2032 const void *private_data,
2033 u8 private_data_len)
2035 struct cm_id_private *cm_id_priv;
2036 struct ib_mad_send_buf *msg;
2037 unsigned long flags;
2041 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2044 data = cm_copy_private_data(private_data, private_data_len);
2046 return PTR_ERR(data);
2048 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2049 spin_lock_irqsave(&cm_id_priv->lock, flags);
2050 if (cm_id->state != IB_CM_DREQ_RCVD) {
2051 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2056 cm_set_private_data(cm_id_priv, data, private_data_len);
2057 cm_enter_timewait(cm_id_priv);
2059 ret = cm_alloc_msg(cm_id_priv, &msg);
2063 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2064 private_data, private_data_len);
2066 ret = ib_post_send_mad(msg, NULL);
2068 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2073 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2076 EXPORT_SYMBOL(ib_send_cm_drep);
2078 static int cm_issue_drep(struct cm_port *port,
2079 struct ib_mad_recv_wc *mad_recv_wc)
2081 struct ib_mad_send_buf *msg = NULL;
2082 struct cm_dreq_msg *dreq_msg;
2083 struct cm_drep_msg *drep_msg;
2086 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2090 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2091 drep_msg = (struct cm_drep_msg *) msg->mad;
2093 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2094 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2095 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2097 ret = ib_post_send_mad(msg, NULL);
2104 static int cm_dreq_handler(struct cm_work *work)
2106 struct cm_id_private *cm_id_priv;
2107 struct cm_dreq_msg *dreq_msg;
2108 struct ib_mad_send_buf *msg = NULL;
2111 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2112 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2113 dreq_msg->local_comm_id);
2115 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2116 counter[CM_DREQ_COUNTER]);
2117 cm_issue_drep(work->port, work->mad_recv_wc);
2121 work->cm_event.private_data = &dreq_msg->private_data;
2123 spin_lock_irq(&cm_id_priv->lock);
2124 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2127 switch (cm_id_priv->id.state) {
2128 case IB_CM_REP_SENT:
2129 case IB_CM_DREQ_SENT:
2130 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2132 case IB_CM_ESTABLISHED:
2133 case IB_CM_MRA_REP_RCVD:
2135 case IB_CM_TIMEWAIT:
2136 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2137 counter[CM_DREQ_COUNTER]);
2138 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2141 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2142 cm_id_priv->private_data,
2143 cm_id_priv->private_data_len);
2144 spin_unlock_irq(&cm_id_priv->lock);
2146 if (ib_post_send_mad(msg, NULL))
2149 case IB_CM_DREQ_RCVD:
2150 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2151 counter[CM_DREQ_COUNTER]);
2156 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2157 cm_id_priv->tid = dreq_msg->hdr.tid;
2158 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2160 list_add_tail(&work->list, &cm_id_priv->work_list);
2161 spin_unlock_irq(&cm_id_priv->lock);
2164 cm_process_work(cm_id_priv, work);
2166 cm_deref_id(cm_id_priv);
2169 unlock: spin_unlock_irq(&cm_id_priv->lock);
2170 deref: cm_deref_id(cm_id_priv);
2174 static int cm_drep_handler(struct cm_work *work)
2176 struct cm_id_private *cm_id_priv;
2177 struct cm_drep_msg *drep_msg;
2180 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2181 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2182 drep_msg->local_comm_id);
2186 work->cm_event.private_data = &drep_msg->private_data;
2188 spin_lock_irq(&cm_id_priv->lock);
2189 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2190 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2191 spin_unlock_irq(&cm_id_priv->lock);
2194 cm_enter_timewait(cm_id_priv);
2196 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2197 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2199 list_add_tail(&work->list, &cm_id_priv->work_list);
2200 spin_unlock_irq(&cm_id_priv->lock);
2203 cm_process_work(cm_id_priv, work);
2205 cm_deref_id(cm_id_priv);
2208 cm_deref_id(cm_id_priv);
2212 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2213 enum ib_cm_rej_reason reason,
2216 const void *private_data,
2217 u8 private_data_len)
2219 struct cm_id_private *cm_id_priv;
2220 struct ib_mad_send_buf *msg;
2221 unsigned long flags;
2224 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2225 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2228 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2230 spin_lock_irqsave(&cm_id_priv->lock, flags);
2231 switch (cm_id->state) {
2232 case IB_CM_REQ_SENT:
2233 case IB_CM_MRA_REQ_RCVD:
2234 case IB_CM_REQ_RCVD:
2235 case IB_CM_MRA_REQ_SENT:
2236 case IB_CM_REP_RCVD:
2237 case IB_CM_MRA_REP_SENT:
2238 ret = cm_alloc_msg(cm_id_priv, &msg);
2240 cm_format_rej((struct cm_rej_msg *) msg->mad,
2241 cm_id_priv, reason, ari, ari_length,
2242 private_data, private_data_len);
2244 cm_reset_to_idle(cm_id_priv);
2246 case IB_CM_REP_SENT:
2247 case IB_CM_MRA_REP_RCVD:
2248 ret = cm_alloc_msg(cm_id_priv, &msg);
2250 cm_format_rej((struct cm_rej_msg *) msg->mad,
2251 cm_id_priv, reason, ari, ari_length,
2252 private_data, private_data_len);
2254 cm_enter_timewait(cm_id_priv);
2264 ret = ib_post_send_mad(msg, NULL);
2268 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2271 EXPORT_SYMBOL(ib_send_cm_rej);
2273 static void cm_format_rej_event(struct cm_work *work)
2275 struct cm_rej_msg *rej_msg;
2276 struct ib_cm_rej_event_param *param;
2278 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2279 param = &work->cm_event.param.rej_rcvd;
2280 param->ari = rej_msg->ari;
2281 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2282 param->reason = __be16_to_cpu(rej_msg->reason);
2283 work->cm_event.private_data = &rej_msg->private_data;
2286 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2288 struct cm_timewait_info *timewait_info;
2289 struct cm_id_private *cm_id_priv;
2292 remote_id = rej_msg->local_comm_id;
2294 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2295 spin_lock_irq(&cm.lock);
2296 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2298 if (!timewait_info) {
2299 spin_unlock_irq(&cm.lock);
2302 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2303 (timewait_info->work.local_id ^
2304 cm.random_id_operand));
2306 if (cm_id_priv->id.remote_id == remote_id)
2307 atomic_inc(&cm_id_priv->refcount);
2311 spin_unlock_irq(&cm.lock);
2312 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2313 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2315 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2320 static int cm_rej_handler(struct cm_work *work)
2322 struct cm_id_private *cm_id_priv;
2323 struct cm_rej_msg *rej_msg;
2326 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2327 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2331 cm_format_rej_event(work);
2333 spin_lock_irq(&cm_id_priv->lock);
2334 switch (cm_id_priv->id.state) {
2335 case IB_CM_REQ_SENT:
2336 case IB_CM_MRA_REQ_RCVD:
2337 case IB_CM_REP_SENT:
2338 case IB_CM_MRA_REP_RCVD:
2339 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2341 case IB_CM_REQ_RCVD:
2342 case IB_CM_MRA_REQ_SENT:
2343 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2344 cm_enter_timewait(cm_id_priv);
2346 cm_reset_to_idle(cm_id_priv);
2348 case IB_CM_DREQ_SENT:
2349 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2351 case IB_CM_REP_RCVD:
2352 case IB_CM_MRA_REP_SENT:
2353 case IB_CM_ESTABLISHED:
2354 cm_enter_timewait(cm_id_priv);
2357 spin_unlock_irq(&cm_id_priv->lock);
2362 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2364 list_add_tail(&work->list, &cm_id_priv->work_list);
2365 spin_unlock_irq(&cm_id_priv->lock);
2368 cm_process_work(cm_id_priv, work);
2370 cm_deref_id(cm_id_priv);
2373 cm_deref_id(cm_id_priv);
2377 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2379 const void *private_data,
2380 u8 private_data_len)
2382 struct cm_id_private *cm_id_priv;
2383 struct ib_mad_send_buf *msg;
2384 enum ib_cm_state cm_state;
2385 enum ib_cm_lap_state lap_state;
2386 enum cm_msg_response msg_response;
2388 unsigned long flags;
2391 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2394 data = cm_copy_private_data(private_data, private_data_len);
2396 return PTR_ERR(data);
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_priv->id.state) {
2402 case IB_CM_REQ_RCVD:
2403 cm_state = IB_CM_MRA_REQ_SENT;
2404 lap_state = cm_id->lap_state;
2405 msg_response = CM_MSG_RESPONSE_REQ;
2407 case IB_CM_REP_RCVD:
2408 cm_state = IB_CM_MRA_REP_SENT;
2409 lap_state = cm_id->lap_state;
2410 msg_response = CM_MSG_RESPONSE_REP;
2412 case IB_CM_ESTABLISHED:
2413 cm_state = cm_id->state;
2414 lap_state = IB_CM_MRA_LAP_SENT;
2415 msg_response = CM_MSG_RESPONSE_OTHER;
2422 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2423 ret = cm_alloc_msg(cm_id_priv, &msg);
2427 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2428 msg_response, service_timeout,
2429 private_data, private_data_len);
2430 ret = ib_post_send_mad(msg, NULL);
2435 cm_id->state = cm_state;
2436 cm_id->lap_state = lap_state;
2437 cm_id_priv->service_timeout = service_timeout;
2438 cm_set_private_data(cm_id_priv, data, private_data_len);
2439 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2442 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2446 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2451 EXPORT_SYMBOL(ib_send_cm_mra);
2453 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2455 switch (cm_mra_get_msg_mraed(mra_msg)) {
2456 case CM_MSG_RESPONSE_REQ:
2457 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2458 case CM_MSG_RESPONSE_REP:
2459 case CM_MSG_RESPONSE_OTHER:
2460 return cm_acquire_id(mra_msg->remote_comm_id,
2461 mra_msg->local_comm_id);
2467 static int cm_mra_handler(struct cm_work *work)
2469 struct cm_id_private *cm_id_priv;
2470 struct cm_mra_msg *mra_msg;
2473 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2474 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2478 work->cm_event.private_data = &mra_msg->private_data;
2479 work->cm_event.param.mra_rcvd.service_timeout =
2480 cm_mra_get_service_timeout(mra_msg);
2481 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2482 cm_convert_to_ms(cm_id_priv->av.timeout);
2484 spin_lock_irq(&cm_id_priv->lock);
2485 switch (cm_id_priv->id.state) {
2486 case IB_CM_REQ_SENT:
2487 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2488 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2489 cm_id_priv->msg, timeout))
2491 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2493 case IB_CM_REP_SENT:
2494 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2495 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2496 cm_id_priv->msg, timeout))
2498 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2500 case IB_CM_ESTABLISHED:
2501 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2502 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2503 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2504 cm_id_priv->msg, timeout)) {
2505 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2506 atomic_long_inc(&work->port->
2507 counter_group[CM_RECV_DUPLICATES].
2508 counter[CM_MRA_COUNTER]);
2511 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2513 case IB_CM_MRA_REQ_RCVD:
2514 case IB_CM_MRA_REP_RCVD:
2515 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2516 counter[CM_MRA_COUNTER]);
2522 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2523 cm_id_priv->id.state;
2524 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2526 list_add_tail(&work->list, &cm_id_priv->work_list);
2527 spin_unlock_irq(&cm_id_priv->lock);
2530 cm_process_work(cm_id_priv, work);
2532 cm_deref_id(cm_id_priv);
2535 spin_unlock_irq(&cm_id_priv->lock);
2536 cm_deref_id(cm_id_priv);
2540 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2541 struct cm_id_private *cm_id_priv,
2542 struct ib_sa_path_rec *alternate_path,
2543 const void *private_data,
2544 u8 private_data_len)
2546 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2547 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2548 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2549 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2550 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2551 /* todo: need remote CM response timeout */
2552 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2553 lap_msg->alt_local_lid = alternate_path->slid;
2554 lap_msg->alt_remote_lid = alternate_path->dlid;
2555 lap_msg->alt_local_gid = alternate_path->sgid;
2556 lap_msg->alt_remote_gid = alternate_path->dgid;
2557 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2558 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2559 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2560 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2561 cm_lap_set_sl(lap_msg, alternate_path->sl);
2562 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2563 cm_lap_set_local_ack_timeout(lap_msg,
2564 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2565 alternate_path->packet_life_time));
2567 if (private_data && private_data_len)
2568 memcpy(lap_msg->private_data, private_data, private_data_len);
2571 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2572 struct ib_sa_path_rec *alternate_path,
2573 const void *private_data,
2574 u8 private_data_len)
2576 struct cm_id_private *cm_id_priv;
2577 struct ib_mad_send_buf *msg;
2578 unsigned long flags;
2581 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2584 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2585 spin_lock_irqsave(&cm_id_priv->lock, flags);
2586 if (cm_id->state != IB_CM_ESTABLISHED ||
2587 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2588 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2593 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2596 cm_id_priv->alt_av.timeout =
2597 cm_ack_timeout(cm_id_priv->target_ack_delay,
2598 cm_id_priv->alt_av.timeout - 1);
2600 ret = cm_alloc_msg(cm_id_priv, &msg);
2604 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2605 alternate_path, private_data, private_data_len);
2606 msg->timeout_ms = cm_id_priv->timeout_ms;
2607 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2609 ret = ib_post_send_mad(msg, NULL);
2611 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2616 cm_id->lap_state = IB_CM_LAP_SENT;
2617 cm_id_priv->msg = msg;
2619 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2622 EXPORT_SYMBOL(ib_send_cm_lap);
2624 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2625 struct ib_sa_path_rec *path,
2626 struct cm_lap_msg *lap_msg)
2628 memset(path, 0, sizeof *path);
2629 path->dgid = lap_msg->alt_local_gid;
2630 path->sgid = lap_msg->alt_remote_gid;
2631 path->dlid = lap_msg->alt_local_lid;
2632 path->slid = lap_msg->alt_remote_lid;
2633 path->flow_label = cm_lap_get_flow_label(lap_msg);
2634 path->hop_limit = lap_msg->alt_hop_limit;
2635 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2636 path->reversible = 1;
2637 path->pkey = cm_id_priv->pkey;
2638 path->sl = cm_lap_get_sl(lap_msg);
2639 path->mtu_selector = IB_SA_EQ;
2640 path->mtu = cm_id_priv->path_mtu;
2641 path->rate_selector = IB_SA_EQ;
2642 path->rate = cm_lap_get_packet_rate(lap_msg);
2643 path->packet_life_time_selector = IB_SA_EQ;
2644 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2645 path->packet_life_time -= (path->packet_life_time > 0);
2648 static int cm_lap_handler(struct cm_work *work)
2650 struct cm_id_private *cm_id_priv;
2651 struct cm_lap_msg *lap_msg;
2652 struct ib_cm_lap_event_param *param;
2653 struct ib_mad_send_buf *msg = NULL;
2656 /* todo: verify LAP request and send reject APR if invalid. */
2657 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2658 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2659 lap_msg->local_comm_id);
2663 param = &work->cm_event.param.lap_rcvd;
2664 param->alternate_path = &work->path[0];
2665 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2666 work->cm_event.private_data = &lap_msg->private_data;
2668 spin_lock_irq(&cm_id_priv->lock);
2669 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2672 switch (cm_id_priv->id.lap_state) {
2673 case IB_CM_LAP_UNINIT:
2674 case IB_CM_LAP_IDLE:
2676 case IB_CM_MRA_LAP_SENT:
2677 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2678 counter[CM_LAP_COUNTER]);
2679 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2682 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2683 CM_MSG_RESPONSE_OTHER,
2684 cm_id_priv->service_timeout,
2685 cm_id_priv->private_data,
2686 cm_id_priv->private_data_len);
2687 spin_unlock_irq(&cm_id_priv->lock);
2689 if (ib_post_send_mad(msg, NULL))
2692 case IB_CM_LAP_RCVD:
2693 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2694 counter[CM_LAP_COUNTER]);
2700 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2701 cm_id_priv->tid = lap_msg->hdr.tid;
2702 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2703 work->mad_recv_wc->recv_buf.grh,
2705 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2706 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2708 list_add_tail(&work->list, &cm_id_priv->work_list);
2709 spin_unlock_irq(&cm_id_priv->lock);
2712 cm_process_work(cm_id_priv, work);
2714 cm_deref_id(cm_id_priv);
2717 unlock: spin_unlock_irq(&cm_id_priv->lock);
2718 deref: cm_deref_id(cm_id_priv);
2722 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2723 struct cm_id_private *cm_id_priv,
2724 enum ib_cm_apr_status status,
2727 const void *private_data,
2728 u8 private_data_len)
2730 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2731 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2732 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2733 apr_msg->ap_status = (u8) status;
2735 if (info && info_length) {
2736 apr_msg->info_length = info_length;
2737 memcpy(apr_msg->info, info, info_length);
2740 if (private_data && private_data_len)
2741 memcpy(apr_msg->private_data, private_data, private_data_len);
2744 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2745 enum ib_cm_apr_status status,
2748 const void *private_data,
2749 u8 private_data_len)
2751 struct cm_id_private *cm_id_priv;
2752 struct ib_mad_send_buf *msg;
2753 unsigned long flags;
2756 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2757 (info && info_length > IB_CM_APR_INFO_LENGTH))
2760 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2761 spin_lock_irqsave(&cm_id_priv->lock, flags);
2762 if (cm_id->state != IB_CM_ESTABLISHED ||
2763 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2764 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2769 ret = cm_alloc_msg(cm_id_priv, &msg);
2773 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2774 info, info_length, private_data, private_data_len);
2775 ret = ib_post_send_mad(msg, NULL);
2777 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2782 cm_id->lap_state = IB_CM_LAP_IDLE;
2783 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2786 EXPORT_SYMBOL(ib_send_cm_apr);
2788 static int cm_apr_handler(struct cm_work *work)
2790 struct cm_id_private *cm_id_priv;
2791 struct cm_apr_msg *apr_msg;
2794 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2795 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2796 apr_msg->local_comm_id);
2798 return -EINVAL; /* Unmatched reply. */
2800 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2801 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2802 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2803 work->cm_event.private_data = &apr_msg->private_data;
2805 spin_lock_irq(&cm_id_priv->lock);
2806 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2807 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2808 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2809 spin_unlock_irq(&cm_id_priv->lock);
2812 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2813 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2814 cm_id_priv->msg = NULL;
2816 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2818 list_add_tail(&work->list, &cm_id_priv->work_list);
2819 spin_unlock_irq(&cm_id_priv->lock);
2822 cm_process_work(cm_id_priv, work);
2824 cm_deref_id(cm_id_priv);
2827 cm_deref_id(cm_id_priv);
2831 static int cm_timewait_handler(struct cm_work *work)
2833 struct cm_timewait_info *timewait_info;
2834 struct cm_id_private *cm_id_priv;
2837 timewait_info = (struct cm_timewait_info *)work;
2838 spin_lock_irq(&cm.lock);
2839 list_del(&timewait_info->list);
2840 spin_unlock_irq(&cm.lock);
2842 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2843 timewait_info->work.remote_id);
2847 spin_lock_irq(&cm_id_priv->lock);
2848 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2849 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2850 spin_unlock_irq(&cm_id_priv->lock);
2853 cm_id_priv->id.state = IB_CM_IDLE;
2854 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2856 list_add_tail(&work->list, &cm_id_priv->work_list);
2857 spin_unlock_irq(&cm_id_priv->lock);
2860 cm_process_work(cm_id_priv, work);
2862 cm_deref_id(cm_id_priv);
2865 cm_deref_id(cm_id_priv);
2869 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2870 struct cm_id_private *cm_id_priv,
2871 struct ib_cm_sidr_req_param *param)
2873 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2874 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2875 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2876 sidr_req_msg->pkey = param->path->pkey;
2877 sidr_req_msg->service_id = param->service_id;
2879 if (param->private_data && param->private_data_len)
2880 memcpy(sidr_req_msg->private_data, param->private_data,
2881 param->private_data_len);
2884 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2885 struct ib_cm_sidr_req_param *param)
2887 struct cm_id_private *cm_id_priv;
2888 struct ib_mad_send_buf *msg;
2889 unsigned long flags;
2892 if (!param->path || (param->private_data &&
2893 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2896 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2897 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2901 cm_id->service_id = param->service_id;
2902 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2903 cm_id_priv->timeout_ms = param->timeout_ms;
2904 cm_id_priv->max_cm_retries = param->max_cm_retries;
2905 ret = cm_alloc_msg(cm_id_priv, &msg);
2909 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2911 msg->timeout_ms = cm_id_priv->timeout_ms;
2912 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2914 spin_lock_irqsave(&cm_id_priv->lock, flags);
2915 if (cm_id->state == IB_CM_IDLE)
2916 ret = ib_post_send_mad(msg, NULL);
2921 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2925 cm_id->state = IB_CM_SIDR_REQ_SENT;
2926 cm_id_priv->msg = msg;
2927 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2931 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2933 static void cm_format_sidr_req_event(struct cm_work *work,
2934 struct ib_cm_id *listen_id)
2936 struct cm_sidr_req_msg *sidr_req_msg;
2937 struct ib_cm_sidr_req_event_param *param;
2939 sidr_req_msg = (struct cm_sidr_req_msg *)
2940 work->mad_recv_wc->recv_buf.mad;
2941 param = &work->cm_event.param.sidr_req_rcvd;
2942 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2943 param->listen_id = listen_id;
2944 param->port = work->port->port_num;
2945 work->cm_event.private_data = &sidr_req_msg->private_data;
2948 static int cm_sidr_req_handler(struct cm_work *work)
2950 struct ib_cm_id *cm_id;
2951 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2952 struct cm_sidr_req_msg *sidr_req_msg;
2955 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2957 return PTR_ERR(cm_id);
2958 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2960 /* Record SGID/SLID and request ID for lookup. */
2961 sidr_req_msg = (struct cm_sidr_req_msg *)
2962 work->mad_recv_wc->recv_buf.mad;
2963 wc = work->mad_recv_wc->wc;
2964 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2965 cm_id_priv->av.dgid.global.interface_id = 0;
2966 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2967 work->mad_recv_wc->recv_buf.grh,
2969 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2970 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2971 atomic_inc(&cm_id_priv->work_count);
2973 spin_lock_irq(&cm.lock);
2974 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2975 if (cur_cm_id_priv) {
2976 spin_unlock_irq(&cm.lock);
2977 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2978 counter[CM_SIDR_REQ_COUNTER]);
2979 goto out; /* Duplicate message. */
2981 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2982 cur_cm_id_priv = cm_find_listen(cm_id->device,
2983 sidr_req_msg->service_id,
2984 sidr_req_msg->private_data);
2985 if (!cur_cm_id_priv) {
2986 spin_unlock_irq(&cm.lock);
2987 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
2988 goto out; /* No match. */
2990 atomic_inc(&cur_cm_id_priv->refcount);
2991 spin_unlock_irq(&cm.lock);
2993 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2994 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2995 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2996 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2998 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2999 cm_process_work(cm_id_priv, work);
3000 cm_deref_id(cur_cm_id_priv);
3003 ib_destroy_cm_id(&cm_id_priv->id);
3007 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3008 struct cm_id_private *cm_id_priv,
3009 struct ib_cm_sidr_rep_param *param)
3011 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3013 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3014 sidr_rep_msg->status = param->status;
3015 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3016 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3017 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3019 if (param->info && param->info_length)
3020 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3022 if (param->private_data && param->private_data_len)
3023 memcpy(sidr_rep_msg->private_data, param->private_data,
3024 param->private_data_len);
3027 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3028 struct ib_cm_sidr_rep_param *param)
3030 struct cm_id_private *cm_id_priv;
3031 struct ib_mad_send_buf *msg;
3032 unsigned long flags;
3035 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3036 (param->private_data &&
3037 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3040 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3041 spin_lock_irqsave(&cm_id_priv->lock, flags);
3042 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3047 ret = cm_alloc_msg(cm_id_priv, &msg);
3051 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3053 ret = ib_post_send_mad(msg, NULL);
3055 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3059 cm_id->state = IB_CM_IDLE;
3060 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3062 spin_lock_irqsave(&cm.lock, flags);
3063 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3064 spin_unlock_irqrestore(&cm.lock, flags);
3067 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3070 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3072 static void cm_format_sidr_rep_event(struct cm_work *work)
3074 struct cm_sidr_rep_msg *sidr_rep_msg;
3075 struct ib_cm_sidr_rep_event_param *param;
3077 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3078 work->mad_recv_wc->recv_buf.mad;
3079 param = &work->cm_event.param.sidr_rep_rcvd;
3080 param->status = sidr_rep_msg->status;
3081 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3082 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3083 param->info = &sidr_rep_msg->info;
3084 param->info_len = sidr_rep_msg->info_length;
3085 work->cm_event.private_data = &sidr_rep_msg->private_data;
3088 static int cm_sidr_rep_handler(struct cm_work *work)
3090 struct cm_sidr_rep_msg *sidr_rep_msg;
3091 struct cm_id_private *cm_id_priv;
3093 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3094 work->mad_recv_wc->recv_buf.mad;
3095 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3097 return -EINVAL; /* Unmatched reply. */
3099 spin_lock_irq(&cm_id_priv->lock);
3100 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3101 spin_unlock_irq(&cm_id_priv->lock);
3104 cm_id_priv->id.state = IB_CM_IDLE;
3105 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3106 spin_unlock_irq(&cm_id_priv->lock);
3108 cm_format_sidr_rep_event(work);
3109 cm_process_work(cm_id_priv, work);
3112 cm_deref_id(cm_id_priv);
3116 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3117 enum ib_wc_status wc_status)
3119 struct cm_id_private *cm_id_priv;
3120 struct ib_cm_event cm_event;
3121 enum ib_cm_state state;
3124 memset(&cm_event, 0, sizeof cm_event);
3125 cm_id_priv = msg->context[0];
3127 /* Discard old sends or ones without a response. */
3128 spin_lock_irq(&cm_id_priv->lock);
3129 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3130 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3134 case IB_CM_REQ_SENT:
3135 case IB_CM_MRA_REQ_RCVD:
3136 cm_reset_to_idle(cm_id_priv);
3137 cm_event.event = IB_CM_REQ_ERROR;
3139 case IB_CM_REP_SENT:
3140 case IB_CM_MRA_REP_RCVD:
3141 cm_reset_to_idle(cm_id_priv);
3142 cm_event.event = IB_CM_REP_ERROR;
3144 case IB_CM_DREQ_SENT:
3145 cm_enter_timewait(cm_id_priv);
3146 cm_event.event = IB_CM_DREQ_ERROR;
3148 case IB_CM_SIDR_REQ_SENT:
3149 cm_id_priv->id.state = IB_CM_IDLE;
3150 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3155 spin_unlock_irq(&cm_id_priv->lock);
3156 cm_event.param.send_status = wc_status;
3158 /* No other events can occur on the cm_id at this point. */
3159 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3162 ib_destroy_cm_id(&cm_id_priv->id);
3165 spin_unlock_irq(&cm_id_priv->lock);
3169 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3170 struct ib_mad_send_wc *mad_send_wc)
3172 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3173 struct cm_port *port;
3176 port = mad_agent->context;
3177 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3178 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3181 * If the send was in response to a received message (context[0] is not
3182 * set to a cm_id), and is not a REJ, then it is a send that was
3185 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3188 atomic_long_add(1 + msg->retries,
3189 &port->counter_group[CM_XMIT].counter[attr_index]);
3191 atomic_long_add(msg->retries,
3192 &port->counter_group[CM_XMIT_RETRIES].
3193 counter[attr_index]);
3195 switch (mad_send_wc->status) {
3197 case IB_WC_WR_FLUSH_ERR:
3201 if (msg->context[0] && msg->context[1])
3202 cm_process_send_error(msg, mad_send_wc->status);
3209 static void cm_work_handler(struct work_struct *_work)
3211 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3214 switch (work->cm_event.event) {
3215 case IB_CM_REQ_RECEIVED:
3216 ret = cm_req_handler(work);
3218 case IB_CM_MRA_RECEIVED:
3219 ret = cm_mra_handler(work);
3221 case IB_CM_REJ_RECEIVED:
3222 ret = cm_rej_handler(work);
3224 case IB_CM_REP_RECEIVED:
3225 ret = cm_rep_handler(work);
3227 case IB_CM_RTU_RECEIVED:
3228 ret = cm_rtu_handler(work);
3230 case IB_CM_USER_ESTABLISHED:
3231 ret = cm_establish_handler(work);
3233 case IB_CM_DREQ_RECEIVED:
3234 ret = cm_dreq_handler(work);
3236 case IB_CM_DREP_RECEIVED:
3237 ret = cm_drep_handler(work);
3239 case IB_CM_SIDR_REQ_RECEIVED:
3240 ret = cm_sidr_req_handler(work);
3242 case IB_CM_SIDR_REP_RECEIVED:
3243 ret = cm_sidr_rep_handler(work);
3245 case IB_CM_LAP_RECEIVED:
3246 ret = cm_lap_handler(work);
3248 case IB_CM_APR_RECEIVED:
3249 ret = cm_apr_handler(work);
3251 case IB_CM_TIMEWAIT_EXIT:
3252 ret = cm_timewait_handler(work);
3262 static int cm_establish(struct ib_cm_id *cm_id)
3264 struct cm_id_private *cm_id_priv;
3265 struct cm_work *work;
3266 unsigned long flags;
3269 work = kmalloc(sizeof *work, GFP_ATOMIC);
3273 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3274 spin_lock_irqsave(&cm_id_priv->lock, flags);
3275 switch (cm_id->state)
3277 case IB_CM_REP_SENT:
3278 case IB_CM_MRA_REP_RCVD:
3279 cm_id->state = IB_CM_ESTABLISHED;
3281 case IB_CM_ESTABLISHED:
3288 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3296 * The CM worker thread may try to destroy the cm_id before it
3297 * can execute this work item. To prevent potential deadlock,
3298 * we need to find the cm_id once we're in the context of the
3299 * worker thread, rather than holding a reference on it.
3301 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3302 work->local_id = cm_id->local_id;
3303 work->remote_id = cm_id->remote_id;
3304 work->mad_recv_wc = NULL;
3305 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3306 queue_delayed_work(cm.wq, &work->work, 0);
3311 static int cm_migrate(struct ib_cm_id *cm_id)
3313 struct cm_id_private *cm_id_priv;
3314 unsigned long flags;
3317 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3318 spin_lock_irqsave(&cm_id_priv->lock, flags);
3319 if (cm_id->state == IB_CM_ESTABLISHED &&
3320 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3321 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3322 cm_id->lap_state = IB_CM_LAP_IDLE;
3323 cm_id_priv->av = cm_id_priv->alt_av;
3326 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3331 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3336 case IB_EVENT_COMM_EST:
3337 ret = cm_establish(cm_id);
3339 case IB_EVENT_PATH_MIG:
3340 ret = cm_migrate(cm_id);
3347 EXPORT_SYMBOL(ib_cm_notify);
3349 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3350 struct ib_mad_recv_wc *mad_recv_wc)
3352 struct cm_port *port = mad_agent->context;
3353 struct cm_work *work;
3354 enum ib_cm_event_type event;
3358 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3359 case CM_REQ_ATTR_ID:
3360 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3361 alt_local_lid != 0);
3362 event = IB_CM_REQ_RECEIVED;
3364 case CM_MRA_ATTR_ID:
3365 event = IB_CM_MRA_RECEIVED;
3367 case CM_REJ_ATTR_ID:
3368 event = IB_CM_REJ_RECEIVED;
3370 case CM_REP_ATTR_ID:
3371 event = IB_CM_REP_RECEIVED;
3373 case CM_RTU_ATTR_ID:
3374 event = IB_CM_RTU_RECEIVED;
3376 case CM_DREQ_ATTR_ID:
3377 event = IB_CM_DREQ_RECEIVED;
3379 case CM_DREP_ATTR_ID:
3380 event = IB_CM_DREP_RECEIVED;
3382 case CM_SIDR_REQ_ATTR_ID:
3383 event = IB_CM_SIDR_REQ_RECEIVED;
3385 case CM_SIDR_REP_ATTR_ID:
3386 event = IB_CM_SIDR_REP_RECEIVED;
3388 case CM_LAP_ATTR_ID:
3390 event = IB_CM_LAP_RECEIVED;
3392 case CM_APR_ATTR_ID:
3393 event = IB_CM_APR_RECEIVED;
3396 ib_free_recv_mad(mad_recv_wc);
3400 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3401 atomic_long_inc(&port->counter_group[CM_RECV].
3402 counter[attr_id - CM_ATTR_ID_OFFSET]);
3404 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3407 ib_free_recv_mad(mad_recv_wc);
3411 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3412 work->cm_event.event = event;
3413 work->mad_recv_wc = mad_recv_wc;
3415 queue_delayed_work(cm.wq, &work->work, 0);
3418 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3419 struct ib_qp_attr *qp_attr,
3422 unsigned long flags;
3425 spin_lock_irqsave(&cm_id_priv->lock, flags);
3426 switch (cm_id_priv->id.state) {
3427 case IB_CM_REQ_SENT:
3428 case IB_CM_MRA_REQ_RCVD:
3429 case IB_CM_REQ_RCVD:
3430 case IB_CM_MRA_REQ_SENT:
3431 case IB_CM_REP_RCVD:
3432 case IB_CM_MRA_REP_SENT:
3433 case IB_CM_REP_SENT:
3434 case IB_CM_MRA_REP_RCVD:
3435 case IB_CM_ESTABLISHED:
3436 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3437 IB_QP_PKEY_INDEX | IB_QP_PORT;
3438 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3439 if (cm_id_priv->responder_resources)
3440 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3441 IB_ACCESS_REMOTE_ATOMIC;
3442 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3443 qp_attr->port_num = cm_id_priv->av.port->port_num;
3450 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3454 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3455 struct ib_qp_attr *qp_attr,
3458 unsigned long flags;
3461 spin_lock_irqsave(&cm_id_priv->lock, flags);
3462 switch (cm_id_priv->id.state) {
3463 case IB_CM_REQ_RCVD:
3464 case IB_CM_MRA_REQ_SENT:
3465 case IB_CM_REP_RCVD:
3466 case IB_CM_MRA_REP_SENT:
3467 case IB_CM_REP_SENT:
3468 case IB_CM_MRA_REP_RCVD:
3469 case IB_CM_ESTABLISHED:
3470 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3471 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3472 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3473 qp_attr->path_mtu = cm_id_priv->path_mtu;
3474 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3475 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3476 if (cm_id_priv->qp_type == IB_QPT_RC) {
3477 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3478 IB_QP_MIN_RNR_TIMER;
3479 qp_attr->max_dest_rd_atomic =
3480 cm_id_priv->responder_resources;
3481 qp_attr->min_rnr_timer = 0;
3483 if (cm_id_priv->alt_av.ah_attr.dlid) {
3484 *qp_attr_mask |= IB_QP_ALT_PATH;
3485 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3486 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3487 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3488 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3496 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3500 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3501 struct ib_qp_attr *qp_attr,
3504 unsigned long flags;
3507 spin_lock_irqsave(&cm_id_priv->lock, flags);
3508 switch (cm_id_priv->id.state) {
3509 /* Allow transition to RTS before sending REP */
3510 case IB_CM_REQ_RCVD:
3511 case IB_CM_MRA_REQ_SENT:
3513 case IB_CM_REP_RCVD:
3514 case IB_CM_MRA_REP_SENT:
3515 case IB_CM_REP_SENT:
3516 case IB_CM_MRA_REP_RCVD:
3517 case IB_CM_ESTABLISHED:
3518 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3519 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3520 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3521 if (cm_id_priv->qp_type == IB_QPT_RC) {
3522 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3524 IB_QP_MAX_QP_RD_ATOMIC;
3525 qp_attr->timeout = cm_id_priv->av.timeout;
3526 qp_attr->retry_cnt = cm_id_priv->retry_count;
3527 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3528 qp_attr->max_rd_atomic =
3529 cm_id_priv->initiator_depth;
3531 if (cm_id_priv->alt_av.ah_attr.dlid) {
3532 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3533 qp_attr->path_mig_state = IB_MIG_REARM;
3536 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3537 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3538 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3539 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3540 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3541 qp_attr->path_mig_state = IB_MIG_REARM;
3549 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3553 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3554 struct ib_qp_attr *qp_attr,
3557 struct cm_id_private *cm_id_priv;
3560 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3561 switch (qp_attr->qp_state) {
3563 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3566 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3569 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3577 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3579 static void cm_get_ack_delay(struct cm_device *cm_dev)
3581 struct ib_device_attr attr;
3583 if (ib_query_device(cm_dev->device, &attr))
3584 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3586 cm_dev->ack_delay = attr.local_ca_ack_delay;
3589 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3592 struct cm_counter_group *group;
3593 struct cm_counter_attribute *cm_attr;
3595 group = container_of(obj, struct cm_counter_group, obj);
3596 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3598 return sprintf(buf, "%ld\n",
3599 atomic_long_read(&group->counter[cm_attr->index]));
3602 static struct sysfs_ops cm_counter_ops = {
3603 .show = cm_show_counter
3606 static struct kobj_type cm_counter_obj_type = {
3607 .sysfs_ops = &cm_counter_ops,
3608 .default_attrs = cm_counter_default_attrs
3611 static void cm_release_port_obj(struct kobject *obj)
3613 struct cm_port *cm_port;
3615 cm_port = container_of(obj, struct cm_port, port_obj);
3619 static struct kobj_type cm_port_obj_type = {
3620 .release = cm_release_port_obj
3623 static void cm_release_dev_obj(struct kobject *obj)
3625 struct cm_device *cm_dev;
3627 cm_dev = container_of(obj, struct cm_device, dev_obj);
3631 static struct kobj_type cm_dev_obj_type = {
3632 .release = cm_release_dev_obj
3635 struct class cm_class = {
3636 .name = "infiniband_cm",
3638 EXPORT_SYMBOL(cm_class);
3640 static int cm_create_port_fs(struct cm_port *port)
3644 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3645 &port->cm_dev->dev_obj,
3646 "%d", port->port_num);
3652 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3653 ret = kobject_init_and_add(&port->counter_group[i].obj,
3654 &cm_counter_obj_type,
3656 "%s", counter_group_names[i]);
3665 kobject_put(&port->counter_group[i].obj);
3666 kobject_put(&port->port_obj);
3671 static void cm_remove_port_fs(struct cm_port *port)
3675 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3676 kobject_put(&port->counter_group[i].obj);
3678 kobject_put(&port->port_obj);
3681 static void cm_add_one(struct ib_device *device)
3683 struct cm_device *cm_dev;
3684 struct cm_port *port;
3685 struct ib_mad_reg_req reg_req = {
3686 .mgmt_class = IB_MGMT_CLASS_CM,
3687 .mgmt_class_version = IB_CM_CLASS_VERSION
3689 struct ib_port_modify port_modify = {
3690 .set_port_cap_mask = IB_PORT_CM_SUP
3692 unsigned long flags;
3696 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3699 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3700 device->phys_port_cnt, GFP_KERNEL);
3704 cm_dev->device = device;
3705 cm_get_ack_delay(cm_dev);
3707 ret = kobject_init_and_add(&cm_dev->dev_obj, &cm_dev_obj_type,
3708 &cm_class.subsys.kobj, "%s", device->name);
3714 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3715 for (i = 1; i <= device->phys_port_cnt; i++) {
3716 port = kzalloc(sizeof *port, GFP_KERNEL);
3720 cm_dev->port[i-1] = port;
3721 port->cm_dev = cm_dev;
3724 ret = cm_create_port_fs(port);
3728 port->mad_agent = ib_register_mad_agent(device, i,
3735 if (IS_ERR(port->mad_agent))
3738 ret = ib_modify_port(device, i, 0, &port_modify);
3742 ib_set_client_data(device, &cm_client, cm_dev);
3744 write_lock_irqsave(&cm.device_lock, flags);
3745 list_add_tail(&cm_dev->list, &cm.device_list);
3746 write_unlock_irqrestore(&cm.device_lock, flags);
3750 ib_unregister_mad_agent(port->mad_agent);
3752 cm_remove_port_fs(port);
3754 port_modify.set_port_cap_mask = 0;
3755 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3757 port = cm_dev->port[i-1];
3758 ib_modify_port(device, port->port_num, 0, &port_modify);
3759 ib_unregister_mad_agent(port->mad_agent);
3760 cm_remove_port_fs(port);
3762 kobject_put(&cm_dev->dev_obj);
3765 static void cm_remove_one(struct ib_device *device)
3767 struct cm_device *cm_dev;
3768 struct cm_port *port;
3769 struct ib_port_modify port_modify = {
3770 .clr_port_cap_mask = IB_PORT_CM_SUP
3772 unsigned long flags;
3775 cm_dev = ib_get_client_data(device, &cm_client);
3779 write_lock_irqsave(&cm.device_lock, flags);
3780 list_del(&cm_dev->list);
3781 write_unlock_irqrestore(&cm.device_lock, flags);
3783 for (i = 1; i <= device->phys_port_cnt; i++) {
3784 port = cm_dev->port[i-1];
3785 ib_modify_port(device, port->port_num, 0, &port_modify);
3786 ib_unregister_mad_agent(port->mad_agent);
3787 flush_workqueue(cm.wq);
3788 cm_remove_port_fs(port);
3790 kobject_put(&cm_dev->dev_obj);
3793 static int __init ib_cm_init(void)
3797 memset(&cm, 0, sizeof cm);
3798 INIT_LIST_HEAD(&cm.device_list);
3799 rwlock_init(&cm.device_lock);
3800 spin_lock_init(&cm.lock);
3801 cm.listen_service_table = RB_ROOT;
3802 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3803 cm.remote_id_table = RB_ROOT;
3804 cm.remote_qp_table = RB_ROOT;
3805 cm.remote_sidr_table = RB_ROOT;
3806 idr_init(&cm.local_id_table);
3807 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3808 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3809 INIT_LIST_HEAD(&cm.timewait_list);
3811 ret = class_register(&cm_class);
3815 cm.wq = create_workqueue("ib_cm");
3821 ret = ib_register_client(&cm_client);
3827 destroy_workqueue(cm.wq);
3829 class_unregister(&cm_class);
3833 static void __exit ib_cm_cleanup(void)
3835 struct cm_timewait_info *timewait_info, *tmp;
3837 spin_lock_irq(&cm.lock);
3838 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3839 cancel_delayed_work(&timewait_info->work.work);
3840 spin_unlock_irq(&cm.lock);
3842 ib_unregister_client(&cm_client);
3843 destroy_workqueue(cm.wq);
3845 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3846 list_del(&timewait_info->list);
3847 kfree(timewait_info);
3850 class_unregister(&cm_class);
3851 idr_destroy(&cm.local_id_table);
3854 module_init(ib_cm_init);
3855 module_exit(ib_cm_cleanup);