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);
470 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
472 struct rb_node **link = &cm.listen_service_table.rb_node;
473 struct rb_node *parent = NULL;
474 struct cm_id_private *cur_cm_id_priv;
475 __be64 service_id = cm_id_priv->id.service_id;
476 __be64 service_mask = cm_id_priv->id.service_mask;
481 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
483 data_cmp = cm_compare_data(cm_id_priv->compare_data,
484 cur_cm_id_priv->compare_data);
485 if ((cur_cm_id_priv->id.service_mask & service_id) ==
486 (service_mask & cur_cm_id_priv->id.service_id) &&
487 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
489 return cur_cm_id_priv;
491 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
492 link = &(*link)->rb_left;
493 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
494 link = &(*link)->rb_right;
495 else if (service_id < cur_cm_id_priv->id.service_id)
496 link = &(*link)->rb_left;
497 else if (service_id > cur_cm_id_priv->id.service_id)
498 link = &(*link)->rb_right;
499 else if (data_cmp < 0)
500 link = &(*link)->rb_left;
502 link = &(*link)->rb_right;
504 rb_link_node(&cm_id_priv->service_node, parent, link);
505 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
509 static struct cm_id_private * cm_find_listen(struct ib_device *device,
513 struct rb_node *node = cm.listen_service_table.rb_node;
514 struct cm_id_private *cm_id_priv;
518 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
519 data_cmp = cm_compare_private_data(private_data,
520 cm_id_priv->compare_data);
521 if ((cm_id_priv->id.service_mask & service_id) ==
522 cm_id_priv->id.service_id &&
523 (cm_id_priv->id.device == device) && !data_cmp)
526 if (device < cm_id_priv->id.device)
527 node = node->rb_left;
528 else if (device > cm_id_priv->id.device)
529 node = node->rb_right;
530 else if (service_id < cm_id_priv->id.service_id)
531 node = node->rb_left;
532 else if (service_id > cm_id_priv->id.service_id)
533 node = node->rb_right;
534 else if (data_cmp < 0)
535 node = node->rb_left;
537 node = node->rb_right;
542 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
545 struct rb_node **link = &cm.remote_id_table.rb_node;
546 struct rb_node *parent = NULL;
547 struct cm_timewait_info *cur_timewait_info;
548 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
549 __be32 remote_id = timewait_info->work.remote_id;
553 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
555 if (remote_id < cur_timewait_info->work.remote_id)
556 link = &(*link)->rb_left;
557 else if (remote_id > cur_timewait_info->work.remote_id)
558 link = &(*link)->rb_right;
559 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
560 link = &(*link)->rb_left;
561 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
562 link = &(*link)->rb_right;
564 return cur_timewait_info;
566 timewait_info->inserted_remote_id = 1;
567 rb_link_node(&timewait_info->remote_id_node, parent, link);
568 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
572 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
575 struct rb_node *node = cm.remote_id_table.rb_node;
576 struct cm_timewait_info *timewait_info;
579 timewait_info = rb_entry(node, struct cm_timewait_info,
581 if (remote_id < timewait_info->work.remote_id)
582 node = node->rb_left;
583 else if (remote_id > timewait_info->work.remote_id)
584 node = node->rb_right;
585 else if (remote_ca_guid < timewait_info->remote_ca_guid)
586 node = node->rb_left;
587 else if (remote_ca_guid > timewait_info->remote_ca_guid)
588 node = node->rb_right;
590 return timewait_info;
595 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
598 struct rb_node **link = &cm.remote_qp_table.rb_node;
599 struct rb_node *parent = NULL;
600 struct cm_timewait_info *cur_timewait_info;
601 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
602 __be32 remote_qpn = timewait_info->remote_qpn;
606 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
608 if (remote_qpn < cur_timewait_info->remote_qpn)
609 link = &(*link)->rb_left;
610 else if (remote_qpn > cur_timewait_info->remote_qpn)
611 link = &(*link)->rb_right;
612 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
613 link = &(*link)->rb_left;
614 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
615 link = &(*link)->rb_right;
617 return cur_timewait_info;
619 timewait_info->inserted_remote_qp = 1;
620 rb_link_node(&timewait_info->remote_qp_node, parent, link);
621 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
625 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
628 struct rb_node **link = &cm.remote_sidr_table.rb_node;
629 struct rb_node *parent = NULL;
630 struct cm_id_private *cur_cm_id_priv;
631 union ib_gid *port_gid = &cm_id_priv->av.dgid;
632 __be32 remote_id = cm_id_priv->id.remote_id;
636 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
638 if (remote_id < cur_cm_id_priv->id.remote_id)
639 link = &(*link)->rb_left;
640 else if (remote_id > cur_cm_id_priv->id.remote_id)
641 link = &(*link)->rb_right;
644 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
647 link = &(*link)->rb_left;
649 link = &(*link)->rb_right;
651 return cur_cm_id_priv;
654 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
655 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
659 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
660 enum ib_cm_sidr_status status)
662 struct ib_cm_sidr_rep_param param;
664 memset(¶m, 0, sizeof param);
665 param.status = status;
666 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
669 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
670 ib_cm_handler cm_handler,
673 struct cm_id_private *cm_id_priv;
676 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
678 return ERR_PTR(-ENOMEM);
680 cm_id_priv->id.state = IB_CM_IDLE;
681 cm_id_priv->id.device = device;
682 cm_id_priv->id.cm_handler = cm_handler;
683 cm_id_priv->id.context = context;
684 cm_id_priv->id.remote_cm_qpn = 1;
685 ret = cm_alloc_id(cm_id_priv);
689 spin_lock_init(&cm_id_priv->lock);
690 init_completion(&cm_id_priv->comp);
691 INIT_LIST_HEAD(&cm_id_priv->work_list);
692 atomic_set(&cm_id_priv->work_count, -1);
693 atomic_set(&cm_id_priv->refcount, 1);
694 return &cm_id_priv->id;
698 return ERR_PTR(-ENOMEM);
700 EXPORT_SYMBOL(ib_create_cm_id);
702 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
704 struct cm_work *work;
706 if (list_empty(&cm_id_priv->work_list))
709 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
710 list_del(&work->list);
714 static void cm_free_work(struct cm_work *work)
716 if (work->mad_recv_wc)
717 ib_free_recv_mad(work->mad_recv_wc);
721 static inline int cm_convert_to_ms(int iba_time)
723 /* approximate conversion to ms from 4.096us x 2^iba_time */
724 return 1 << max(iba_time - 8, 0);
728 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
729 * Because of how ack_timeout is stored, adding one doubles the timeout.
730 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
731 * increment it (round up) only if the other is within 50%.
733 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
735 int ack_timeout = packet_life_time + 1;
737 if (ack_timeout >= ca_ack_delay)
738 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
740 ack_timeout = ca_ack_delay +
741 (ack_timeout >= (ca_ack_delay - 1));
743 return min(31, ack_timeout);
746 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
748 if (timewait_info->inserted_remote_id) {
749 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
750 timewait_info->inserted_remote_id = 0;
753 if (timewait_info->inserted_remote_qp) {
754 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
755 timewait_info->inserted_remote_qp = 0;
759 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
761 struct cm_timewait_info *timewait_info;
763 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
765 return ERR_PTR(-ENOMEM);
767 timewait_info->work.local_id = local_id;
768 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
769 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
770 return timewait_info;
773 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
778 spin_lock_irqsave(&cm.lock, flags);
779 cm_cleanup_timewait(cm_id_priv->timewait_info);
780 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
781 spin_unlock_irqrestore(&cm.lock, flags);
784 * The cm_id could be destroyed by the user before we exit timewait.
785 * To protect against this, we search for the cm_id after exiting
786 * timewait before notifying the user that we've exited timewait.
788 cm_id_priv->id.state = IB_CM_TIMEWAIT;
789 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
790 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
791 msecs_to_jiffies(wait_time));
792 cm_id_priv->timewait_info = NULL;
795 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
799 cm_id_priv->id.state = IB_CM_IDLE;
800 if (cm_id_priv->timewait_info) {
801 spin_lock_irqsave(&cm.lock, flags);
802 cm_cleanup_timewait(cm_id_priv->timewait_info);
803 spin_unlock_irqrestore(&cm.lock, flags);
804 kfree(cm_id_priv->timewait_info);
805 cm_id_priv->timewait_info = NULL;
809 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
811 struct cm_id_private *cm_id_priv;
812 struct cm_work *work;
814 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
816 spin_lock_irq(&cm_id_priv->lock);
817 switch (cm_id->state) {
819 cm_id->state = IB_CM_IDLE;
820 spin_unlock_irq(&cm_id_priv->lock);
821 spin_lock_irq(&cm.lock);
822 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
823 spin_unlock_irq(&cm.lock);
825 case IB_CM_SIDR_REQ_SENT:
826 cm_id->state = IB_CM_IDLE;
827 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
828 spin_unlock_irq(&cm_id_priv->lock);
830 case IB_CM_SIDR_REQ_RCVD:
831 spin_unlock_irq(&cm_id_priv->lock);
832 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
835 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
836 spin_unlock_irq(&cm_id_priv->lock);
837 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
838 &cm_id_priv->id.device->node_guid,
839 sizeof cm_id_priv->id.device->node_guid,
843 if (err == -ENOMEM) {
844 /* Do not reject to allow future retries. */
845 cm_reset_to_idle(cm_id_priv);
846 spin_unlock_irq(&cm_id_priv->lock);
848 spin_unlock_irq(&cm_id_priv->lock);
849 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
853 case IB_CM_MRA_REQ_RCVD:
855 case IB_CM_MRA_REP_RCVD:
856 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
858 case IB_CM_MRA_REQ_SENT:
860 case IB_CM_MRA_REP_SENT:
861 spin_unlock_irq(&cm_id_priv->lock);
862 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
865 case IB_CM_ESTABLISHED:
866 spin_unlock_irq(&cm_id_priv->lock);
867 ib_send_cm_dreq(cm_id, NULL, 0);
869 case IB_CM_DREQ_SENT:
870 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
871 cm_enter_timewait(cm_id_priv);
872 spin_unlock_irq(&cm_id_priv->lock);
874 case IB_CM_DREQ_RCVD:
875 spin_unlock_irq(&cm_id_priv->lock);
876 ib_send_cm_drep(cm_id, NULL, 0);
879 spin_unlock_irq(&cm_id_priv->lock);
883 cm_free_id(cm_id->local_id);
884 cm_deref_id(cm_id_priv);
885 wait_for_completion(&cm_id_priv->comp);
886 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
888 kfree(cm_id_priv->compare_data);
889 kfree(cm_id_priv->private_data);
893 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
895 cm_destroy_id(cm_id, 0);
897 EXPORT_SYMBOL(ib_destroy_cm_id);
899 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
900 struct ib_cm_compare_data *compare_data)
902 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
906 service_mask = service_mask ? service_mask :
907 __constant_cpu_to_be64(~0ULL);
908 service_id &= service_mask;
909 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
910 (service_id != IB_CM_ASSIGN_SERVICE_ID))
913 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
914 if (cm_id->state != IB_CM_IDLE)
918 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
920 if (!cm_id_priv->compare_data)
922 cm_mask_copy(cm_id_priv->compare_data->data,
923 compare_data->data, compare_data->mask);
924 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
928 cm_id->state = IB_CM_LISTEN;
930 spin_lock_irqsave(&cm.lock, flags);
931 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
932 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
933 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
935 cm_id->service_id = service_id;
936 cm_id->service_mask = service_mask;
938 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
939 spin_unlock_irqrestore(&cm.lock, flags);
941 if (cur_cm_id_priv) {
942 cm_id->state = IB_CM_IDLE;
943 kfree(cm_id_priv->compare_data);
944 cm_id_priv->compare_data = NULL;
949 EXPORT_SYMBOL(ib_cm_listen);
951 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
952 enum cm_msg_sequence msg_seq)
956 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
957 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
959 return cpu_to_be64(hi_tid | low_tid);
962 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
963 __be16 attr_id, __be64 tid)
965 hdr->base_version = IB_MGMT_BASE_VERSION;
966 hdr->mgmt_class = IB_MGMT_CLASS_CM;
967 hdr->class_version = IB_CM_CLASS_VERSION;
968 hdr->method = IB_MGMT_METHOD_SEND;
969 hdr->attr_id = attr_id;
973 static void cm_format_req(struct cm_req_msg *req_msg,
974 struct cm_id_private *cm_id_priv,
975 struct ib_cm_req_param *param)
977 struct ib_sa_path_rec *pri_path = param->primary_path;
978 struct ib_sa_path_rec *alt_path = param->alternate_path;
980 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
981 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
983 req_msg->local_comm_id = cm_id_priv->id.local_id;
984 req_msg->service_id = param->service_id;
985 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
986 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
987 cm_req_set_resp_res(req_msg, param->responder_resources);
988 cm_req_set_init_depth(req_msg, param->initiator_depth);
989 cm_req_set_remote_resp_timeout(req_msg,
990 param->remote_cm_response_timeout);
991 cm_req_set_qp_type(req_msg, param->qp_type);
992 cm_req_set_flow_ctrl(req_msg, param->flow_control);
993 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
994 cm_req_set_local_resp_timeout(req_msg,
995 param->local_cm_response_timeout);
996 cm_req_set_retry_count(req_msg, param->retry_count);
997 req_msg->pkey = param->primary_path->pkey;
998 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
999 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1000 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1001 cm_req_set_srq(req_msg, param->srq);
1003 if (pri_path->hop_limit <= 1) {
1004 req_msg->primary_local_lid = pri_path->slid;
1005 req_msg->primary_remote_lid = pri_path->dlid;
1007 /* Work-around until there's a way to obtain remote LID info */
1008 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1009 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1011 req_msg->primary_local_gid = pri_path->sgid;
1012 req_msg->primary_remote_gid = pri_path->dgid;
1013 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1014 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1015 req_msg->primary_traffic_class = pri_path->traffic_class;
1016 req_msg->primary_hop_limit = pri_path->hop_limit;
1017 cm_req_set_primary_sl(req_msg, pri_path->sl);
1018 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1019 cm_req_set_primary_local_ack_timeout(req_msg,
1020 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1021 pri_path->packet_life_time));
1024 if (alt_path->hop_limit <= 1) {
1025 req_msg->alt_local_lid = alt_path->slid;
1026 req_msg->alt_remote_lid = alt_path->dlid;
1028 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1029 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1031 req_msg->alt_local_gid = alt_path->sgid;
1032 req_msg->alt_remote_gid = alt_path->dgid;
1033 cm_req_set_alt_flow_label(req_msg,
1034 alt_path->flow_label);
1035 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1036 req_msg->alt_traffic_class = alt_path->traffic_class;
1037 req_msg->alt_hop_limit = alt_path->hop_limit;
1038 cm_req_set_alt_sl(req_msg, alt_path->sl);
1039 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1040 cm_req_set_alt_local_ack_timeout(req_msg,
1041 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1042 alt_path->packet_life_time));
1045 if (param->private_data && param->private_data_len)
1046 memcpy(req_msg->private_data, param->private_data,
1047 param->private_data_len);
1050 static int cm_validate_req_param(struct ib_cm_req_param *param)
1052 /* peer-to-peer not supported */
1053 if (param->peer_to_peer)
1056 if (!param->primary_path)
1059 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1062 if (param->private_data &&
1063 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1066 if (param->alternate_path &&
1067 (param->alternate_path->pkey != param->primary_path->pkey ||
1068 param->alternate_path->mtu != param->primary_path->mtu))
1074 int ib_send_cm_req(struct ib_cm_id *cm_id,
1075 struct ib_cm_req_param *param)
1077 struct cm_id_private *cm_id_priv;
1078 struct cm_req_msg *req_msg;
1079 unsigned long flags;
1082 ret = cm_validate_req_param(param);
1086 /* Verify that we're not in timewait. */
1087 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1088 spin_lock_irqsave(&cm_id_priv->lock, flags);
1089 if (cm_id->state != IB_CM_IDLE) {
1090 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1094 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1096 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1098 if (IS_ERR(cm_id_priv->timewait_info)) {
1099 ret = PTR_ERR(cm_id_priv->timewait_info);
1103 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1106 if (param->alternate_path) {
1107 ret = cm_init_av_by_path(param->alternate_path,
1108 &cm_id_priv->alt_av);
1112 cm_id->service_id = param->service_id;
1113 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1114 cm_id_priv->timeout_ms = cm_convert_to_ms(
1115 param->primary_path->packet_life_time) * 2 +
1117 param->remote_cm_response_timeout);
1118 cm_id_priv->max_cm_retries = param->max_cm_retries;
1119 cm_id_priv->initiator_depth = param->initiator_depth;
1120 cm_id_priv->responder_resources = param->responder_resources;
1121 cm_id_priv->retry_count = param->retry_count;
1122 cm_id_priv->path_mtu = param->primary_path->mtu;
1123 cm_id_priv->pkey = param->primary_path->pkey;
1124 cm_id_priv->qp_type = param->qp_type;
1126 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1130 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1131 cm_format_req(req_msg, cm_id_priv, param);
1132 cm_id_priv->tid = req_msg->hdr.tid;
1133 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1134 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1136 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1137 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1139 spin_lock_irqsave(&cm_id_priv->lock, flags);
1140 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1142 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1145 BUG_ON(cm_id->state != IB_CM_IDLE);
1146 cm_id->state = IB_CM_REQ_SENT;
1147 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1150 error2: cm_free_msg(cm_id_priv->msg);
1151 error1: kfree(cm_id_priv->timewait_info);
1154 EXPORT_SYMBOL(ib_send_cm_req);
1156 static int cm_issue_rej(struct cm_port *port,
1157 struct ib_mad_recv_wc *mad_recv_wc,
1158 enum ib_cm_rej_reason reason,
1159 enum cm_msg_response msg_rejected,
1160 void *ari, u8 ari_length)
1162 struct ib_mad_send_buf *msg = NULL;
1163 struct cm_rej_msg *rej_msg, *rcv_msg;
1166 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1170 /* We just need common CM header information. Cast to any message. */
1171 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1172 rej_msg = (struct cm_rej_msg *) msg->mad;
1174 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1175 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1176 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1177 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1178 rej_msg->reason = cpu_to_be16(reason);
1180 if (ari && ari_length) {
1181 cm_rej_set_reject_info_len(rej_msg, ari_length);
1182 memcpy(rej_msg->ari, ari, ari_length);
1185 ret = ib_post_send_mad(msg, NULL);
1192 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1193 __be32 local_qpn, __be32 remote_qpn)
1195 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1196 ((local_ca_guid == remote_ca_guid) &&
1197 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1200 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1201 struct ib_sa_path_rec *primary_path,
1202 struct ib_sa_path_rec *alt_path)
1204 memset(primary_path, 0, sizeof *primary_path);
1205 primary_path->dgid = req_msg->primary_local_gid;
1206 primary_path->sgid = req_msg->primary_remote_gid;
1207 primary_path->dlid = req_msg->primary_local_lid;
1208 primary_path->slid = req_msg->primary_remote_lid;
1209 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1210 primary_path->hop_limit = req_msg->primary_hop_limit;
1211 primary_path->traffic_class = req_msg->primary_traffic_class;
1212 primary_path->reversible = 1;
1213 primary_path->pkey = req_msg->pkey;
1214 primary_path->sl = cm_req_get_primary_sl(req_msg);
1215 primary_path->mtu_selector = IB_SA_EQ;
1216 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1217 primary_path->rate_selector = IB_SA_EQ;
1218 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1219 primary_path->packet_life_time_selector = IB_SA_EQ;
1220 primary_path->packet_life_time =
1221 cm_req_get_primary_local_ack_timeout(req_msg);
1222 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1224 if (req_msg->alt_local_lid) {
1225 memset(alt_path, 0, sizeof *alt_path);
1226 alt_path->dgid = req_msg->alt_local_gid;
1227 alt_path->sgid = req_msg->alt_remote_gid;
1228 alt_path->dlid = req_msg->alt_local_lid;
1229 alt_path->slid = req_msg->alt_remote_lid;
1230 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1231 alt_path->hop_limit = req_msg->alt_hop_limit;
1232 alt_path->traffic_class = req_msg->alt_traffic_class;
1233 alt_path->reversible = 1;
1234 alt_path->pkey = req_msg->pkey;
1235 alt_path->sl = cm_req_get_alt_sl(req_msg);
1236 alt_path->mtu_selector = IB_SA_EQ;
1237 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1238 alt_path->rate_selector = IB_SA_EQ;
1239 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1240 alt_path->packet_life_time_selector = IB_SA_EQ;
1241 alt_path->packet_life_time =
1242 cm_req_get_alt_local_ack_timeout(req_msg);
1243 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1247 static void cm_format_req_event(struct cm_work *work,
1248 struct cm_id_private *cm_id_priv,
1249 struct ib_cm_id *listen_id)
1251 struct cm_req_msg *req_msg;
1252 struct ib_cm_req_event_param *param;
1254 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1255 param = &work->cm_event.param.req_rcvd;
1256 param->listen_id = listen_id;
1257 param->port = cm_id_priv->av.port->port_num;
1258 param->primary_path = &work->path[0];
1259 if (req_msg->alt_local_lid)
1260 param->alternate_path = &work->path[1];
1262 param->alternate_path = NULL;
1263 param->remote_ca_guid = req_msg->local_ca_guid;
1264 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1265 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1266 param->qp_type = cm_req_get_qp_type(req_msg);
1267 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1268 param->responder_resources = cm_req_get_init_depth(req_msg);
1269 param->initiator_depth = cm_req_get_resp_res(req_msg);
1270 param->local_cm_response_timeout =
1271 cm_req_get_remote_resp_timeout(req_msg);
1272 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1273 param->remote_cm_response_timeout =
1274 cm_req_get_local_resp_timeout(req_msg);
1275 param->retry_count = cm_req_get_retry_count(req_msg);
1276 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1277 param->srq = cm_req_get_srq(req_msg);
1278 work->cm_event.private_data = &req_msg->private_data;
1281 static void cm_process_work(struct cm_id_private *cm_id_priv,
1282 struct cm_work *work)
1286 /* We will typically only have the current event to report. */
1287 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1290 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1291 spin_lock_irq(&cm_id_priv->lock);
1292 work = cm_dequeue_work(cm_id_priv);
1293 spin_unlock_irq(&cm_id_priv->lock);
1295 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1299 cm_deref_id(cm_id_priv);
1301 cm_destroy_id(&cm_id_priv->id, ret);
1304 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1305 struct cm_id_private *cm_id_priv,
1306 enum cm_msg_response msg_mraed, u8 service_timeout,
1307 const void *private_data, u8 private_data_len)
1309 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1310 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1311 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1312 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1313 cm_mra_set_service_timeout(mra_msg, service_timeout);
1315 if (private_data && private_data_len)
1316 memcpy(mra_msg->private_data, private_data, private_data_len);
1319 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1320 struct cm_id_private *cm_id_priv,
1321 enum ib_cm_rej_reason reason,
1324 const void *private_data,
1325 u8 private_data_len)
1327 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1328 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1330 switch(cm_id_priv->id.state) {
1331 case IB_CM_REQ_RCVD:
1332 rej_msg->local_comm_id = 0;
1333 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1335 case IB_CM_MRA_REQ_SENT:
1336 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1337 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1339 case IB_CM_REP_RCVD:
1340 case IB_CM_MRA_REP_SENT:
1341 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1342 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1345 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1346 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1350 rej_msg->reason = cpu_to_be16(reason);
1351 if (ari && ari_length) {
1352 cm_rej_set_reject_info_len(rej_msg, ari_length);
1353 memcpy(rej_msg->ari, ari, ari_length);
1356 if (private_data && private_data_len)
1357 memcpy(rej_msg->private_data, private_data, private_data_len);
1360 static void cm_dup_req_handler(struct cm_work *work,
1361 struct cm_id_private *cm_id_priv)
1363 struct ib_mad_send_buf *msg = NULL;
1366 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1367 counter[CM_REQ_COUNTER]);
1369 /* Quick state check to discard duplicate REQs. */
1370 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1373 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1377 spin_lock_irq(&cm_id_priv->lock);
1378 switch (cm_id_priv->id.state) {
1379 case IB_CM_MRA_REQ_SENT:
1380 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1381 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1382 cm_id_priv->private_data,
1383 cm_id_priv->private_data_len);
1385 case IB_CM_TIMEWAIT:
1386 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1387 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1392 spin_unlock_irq(&cm_id_priv->lock);
1394 ret = ib_post_send_mad(msg, NULL);
1399 unlock: spin_unlock_irq(&cm_id_priv->lock);
1400 free: cm_free_msg(msg);
1403 static struct cm_id_private * cm_match_req(struct cm_work *work,
1404 struct cm_id_private *cm_id_priv)
1406 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1407 struct cm_timewait_info *timewait_info;
1408 struct cm_req_msg *req_msg;
1410 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1412 /* Check for possible duplicate REQ. */
1413 spin_lock_irq(&cm.lock);
1414 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1415 if (timewait_info) {
1416 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1417 timewait_info->work.remote_id);
1418 spin_unlock_irq(&cm.lock);
1419 if (cur_cm_id_priv) {
1420 cm_dup_req_handler(work, cur_cm_id_priv);
1421 cm_deref_id(cur_cm_id_priv);
1426 /* Check for stale connections. */
1427 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1428 if (timewait_info) {
1429 cm_cleanup_timewait(cm_id_priv->timewait_info);
1430 spin_unlock_irq(&cm.lock);
1431 cm_issue_rej(work->port, work->mad_recv_wc,
1432 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1437 /* Find matching listen request. */
1438 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1439 req_msg->service_id,
1440 req_msg->private_data);
1441 if (!listen_cm_id_priv) {
1442 cm_cleanup_timewait(cm_id_priv->timewait_info);
1443 spin_unlock_irq(&cm.lock);
1444 cm_issue_rej(work->port, work->mad_recv_wc,
1445 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1449 atomic_inc(&listen_cm_id_priv->refcount);
1450 atomic_inc(&cm_id_priv->refcount);
1451 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1452 atomic_inc(&cm_id_priv->work_count);
1453 spin_unlock_irq(&cm.lock);
1455 return listen_cm_id_priv;
1459 * Work-around for inter-subnet connections. If the LIDs are permissive,
1460 * we need to override the LID/SL data in the REQ with the LID information
1461 * in the work completion.
1463 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1465 if (!cm_req_get_primary_subnet_local(req_msg)) {
1466 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1467 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1468 cm_req_set_primary_sl(req_msg, wc->sl);
1471 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1472 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1475 if (!cm_req_get_alt_subnet_local(req_msg)) {
1476 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1477 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1478 cm_req_set_alt_sl(req_msg, wc->sl);
1481 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1482 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1486 static int cm_req_handler(struct cm_work *work)
1488 struct ib_cm_id *cm_id;
1489 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1490 struct cm_req_msg *req_msg;
1493 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1495 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1497 return PTR_ERR(cm_id);
1499 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1500 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1501 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1502 work->mad_recv_wc->recv_buf.grh,
1504 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1506 if (IS_ERR(cm_id_priv->timewait_info)) {
1507 ret = PTR_ERR(cm_id_priv->timewait_info);
1510 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1511 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1512 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1514 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1515 if (!listen_cm_id_priv) {
1517 kfree(cm_id_priv->timewait_info);
1521 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1522 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1523 cm_id_priv->id.service_id = req_msg->service_id;
1524 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1526 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1527 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1528 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1530 ib_get_cached_gid(work->port->cm_dev->device,
1531 work->port->port_num, 0, &work->path[0].sgid);
1532 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1533 &work->path[0].sgid, sizeof work->path[0].sgid,
1537 if (req_msg->alt_local_lid) {
1538 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1540 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1541 &work->path[0].sgid,
1542 sizeof work->path[0].sgid, NULL, 0);
1546 cm_id_priv->tid = req_msg->hdr.tid;
1547 cm_id_priv->timeout_ms = cm_convert_to_ms(
1548 cm_req_get_local_resp_timeout(req_msg));
1549 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1550 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1551 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1552 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1553 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1554 cm_id_priv->pkey = req_msg->pkey;
1555 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1556 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1557 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1558 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1560 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1561 cm_process_work(cm_id_priv, work);
1562 cm_deref_id(listen_cm_id_priv);
1566 atomic_dec(&cm_id_priv->refcount);
1567 cm_deref_id(listen_cm_id_priv);
1569 ib_destroy_cm_id(cm_id);
1573 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1574 struct cm_id_private *cm_id_priv,
1575 struct ib_cm_rep_param *param)
1577 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1578 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1579 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1580 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1581 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1582 rep_msg->resp_resources = param->responder_resources;
1583 rep_msg->initiator_depth = param->initiator_depth;
1584 cm_rep_set_target_ack_delay(rep_msg,
1585 cm_id_priv->av.port->cm_dev->ack_delay);
1586 cm_rep_set_failover(rep_msg, param->failover_accepted);
1587 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1588 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1589 cm_rep_set_srq(rep_msg, param->srq);
1590 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1592 if (param->private_data && param->private_data_len)
1593 memcpy(rep_msg->private_data, param->private_data,
1594 param->private_data_len);
1597 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1598 struct ib_cm_rep_param *param)
1600 struct cm_id_private *cm_id_priv;
1601 struct ib_mad_send_buf *msg;
1602 struct cm_rep_msg *rep_msg;
1603 unsigned long flags;
1606 if (param->private_data &&
1607 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1610 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1611 spin_lock_irqsave(&cm_id_priv->lock, flags);
1612 if (cm_id->state != IB_CM_REQ_RCVD &&
1613 cm_id->state != IB_CM_MRA_REQ_SENT) {
1618 ret = cm_alloc_msg(cm_id_priv, &msg);
1622 rep_msg = (struct cm_rep_msg *) msg->mad;
1623 cm_format_rep(rep_msg, cm_id_priv, param);
1624 msg->timeout_ms = cm_id_priv->timeout_ms;
1625 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1627 ret = ib_post_send_mad(msg, NULL);
1629 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1634 cm_id->state = IB_CM_REP_SENT;
1635 cm_id_priv->msg = msg;
1636 cm_id_priv->initiator_depth = param->initiator_depth;
1637 cm_id_priv->responder_resources = param->responder_resources;
1638 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1639 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1641 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1644 EXPORT_SYMBOL(ib_send_cm_rep);
1646 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1647 struct cm_id_private *cm_id_priv,
1648 const void *private_data,
1649 u8 private_data_len)
1651 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1652 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1653 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1655 if (private_data && private_data_len)
1656 memcpy(rtu_msg->private_data, private_data, private_data_len);
1659 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1660 const void *private_data,
1661 u8 private_data_len)
1663 struct cm_id_private *cm_id_priv;
1664 struct ib_mad_send_buf *msg;
1665 unsigned long flags;
1669 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1672 data = cm_copy_private_data(private_data, private_data_len);
1674 return PTR_ERR(data);
1676 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1677 spin_lock_irqsave(&cm_id_priv->lock, flags);
1678 if (cm_id->state != IB_CM_REP_RCVD &&
1679 cm_id->state != IB_CM_MRA_REP_SENT) {
1684 ret = cm_alloc_msg(cm_id_priv, &msg);
1688 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1689 private_data, private_data_len);
1691 ret = ib_post_send_mad(msg, NULL);
1693 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1699 cm_id->state = IB_CM_ESTABLISHED;
1700 cm_set_private_data(cm_id_priv, data, private_data_len);
1701 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1704 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1708 EXPORT_SYMBOL(ib_send_cm_rtu);
1710 static void cm_format_rep_event(struct cm_work *work)
1712 struct cm_rep_msg *rep_msg;
1713 struct ib_cm_rep_event_param *param;
1715 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1716 param = &work->cm_event.param.rep_rcvd;
1717 param->remote_ca_guid = rep_msg->local_ca_guid;
1718 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1719 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1720 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1721 param->responder_resources = rep_msg->initiator_depth;
1722 param->initiator_depth = rep_msg->resp_resources;
1723 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1724 param->failover_accepted = cm_rep_get_failover(rep_msg);
1725 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1726 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1727 param->srq = cm_rep_get_srq(rep_msg);
1728 work->cm_event.private_data = &rep_msg->private_data;
1731 static void cm_dup_rep_handler(struct cm_work *work)
1733 struct cm_id_private *cm_id_priv;
1734 struct cm_rep_msg *rep_msg;
1735 struct ib_mad_send_buf *msg = NULL;
1738 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1739 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1740 rep_msg->local_comm_id);
1744 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1745 counter[CM_REP_COUNTER]);
1746 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1750 spin_lock_irq(&cm_id_priv->lock);
1751 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1752 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1753 cm_id_priv->private_data,
1754 cm_id_priv->private_data_len);
1755 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1756 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1757 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1758 cm_id_priv->private_data,
1759 cm_id_priv->private_data_len);
1762 spin_unlock_irq(&cm_id_priv->lock);
1764 ret = ib_post_send_mad(msg, NULL);
1769 unlock: spin_unlock_irq(&cm_id_priv->lock);
1770 free: cm_free_msg(msg);
1771 deref: cm_deref_id(cm_id_priv);
1774 static int cm_rep_handler(struct cm_work *work)
1776 struct cm_id_private *cm_id_priv;
1777 struct cm_rep_msg *rep_msg;
1780 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1781 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1783 cm_dup_rep_handler(work);
1787 cm_format_rep_event(work);
1789 spin_lock_irq(&cm_id_priv->lock);
1790 switch (cm_id_priv->id.state) {
1791 case IB_CM_REQ_SENT:
1792 case IB_CM_MRA_REQ_RCVD:
1795 spin_unlock_irq(&cm_id_priv->lock);
1800 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1801 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1802 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1804 spin_lock(&cm.lock);
1805 /* Check for duplicate REP. */
1806 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1807 spin_unlock(&cm.lock);
1808 spin_unlock_irq(&cm_id_priv->lock);
1812 /* Check for a stale connection. */
1813 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1814 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1815 &cm.remote_id_table);
1816 cm_id_priv->timewait_info->inserted_remote_id = 0;
1817 spin_unlock(&cm.lock);
1818 spin_unlock_irq(&cm_id_priv->lock);
1819 cm_issue_rej(work->port, work->mad_recv_wc,
1820 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1825 spin_unlock(&cm.lock);
1827 cm_id_priv->id.state = IB_CM_REP_RCVD;
1828 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1829 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1830 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1831 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1832 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1833 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1834 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1835 cm_id_priv->av.timeout =
1836 cm_ack_timeout(cm_id_priv->target_ack_delay,
1837 cm_id_priv->av.timeout - 1);
1838 cm_id_priv->alt_av.timeout =
1839 cm_ack_timeout(cm_id_priv->target_ack_delay,
1840 cm_id_priv->alt_av.timeout - 1);
1842 /* todo: handle peer_to_peer */
1844 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1845 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1847 list_add_tail(&work->list, &cm_id_priv->work_list);
1848 spin_unlock_irq(&cm_id_priv->lock);
1851 cm_process_work(cm_id_priv, work);
1853 cm_deref_id(cm_id_priv);
1857 cm_deref_id(cm_id_priv);
1861 static int cm_establish_handler(struct cm_work *work)
1863 struct cm_id_private *cm_id_priv;
1866 /* See comment in cm_establish about lookup. */
1867 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1871 spin_lock_irq(&cm_id_priv->lock);
1872 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1873 spin_unlock_irq(&cm_id_priv->lock);
1877 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1878 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1880 list_add_tail(&work->list, &cm_id_priv->work_list);
1881 spin_unlock_irq(&cm_id_priv->lock);
1884 cm_process_work(cm_id_priv, work);
1886 cm_deref_id(cm_id_priv);
1889 cm_deref_id(cm_id_priv);
1893 static int cm_rtu_handler(struct cm_work *work)
1895 struct cm_id_private *cm_id_priv;
1896 struct cm_rtu_msg *rtu_msg;
1899 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1900 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1901 rtu_msg->local_comm_id);
1905 work->cm_event.private_data = &rtu_msg->private_data;
1907 spin_lock_irq(&cm_id_priv->lock);
1908 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1909 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1910 spin_unlock_irq(&cm_id_priv->lock);
1911 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1912 counter[CM_RTU_COUNTER]);
1915 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1917 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1918 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1920 list_add_tail(&work->list, &cm_id_priv->work_list);
1921 spin_unlock_irq(&cm_id_priv->lock);
1924 cm_process_work(cm_id_priv, work);
1926 cm_deref_id(cm_id_priv);
1929 cm_deref_id(cm_id_priv);
1933 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1934 struct cm_id_private *cm_id_priv,
1935 const void *private_data,
1936 u8 private_data_len)
1938 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1939 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1940 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1941 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1942 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1944 if (private_data && private_data_len)
1945 memcpy(dreq_msg->private_data, private_data, private_data_len);
1948 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1949 const void *private_data,
1950 u8 private_data_len)
1952 struct cm_id_private *cm_id_priv;
1953 struct ib_mad_send_buf *msg;
1954 unsigned long flags;
1957 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1960 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1961 spin_lock_irqsave(&cm_id_priv->lock, flags);
1962 if (cm_id->state != IB_CM_ESTABLISHED) {
1967 ret = cm_alloc_msg(cm_id_priv, &msg);
1969 cm_enter_timewait(cm_id_priv);
1973 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1974 private_data, private_data_len);
1975 msg->timeout_ms = cm_id_priv->timeout_ms;
1976 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1978 ret = ib_post_send_mad(msg, NULL);
1980 cm_enter_timewait(cm_id_priv);
1981 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1986 cm_id->state = IB_CM_DREQ_SENT;
1987 cm_id_priv->msg = msg;
1988 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1991 EXPORT_SYMBOL(ib_send_cm_dreq);
1993 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1994 struct cm_id_private *cm_id_priv,
1995 const void *private_data,
1996 u8 private_data_len)
1998 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1999 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2000 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2002 if (private_data && private_data_len)
2003 memcpy(drep_msg->private_data, private_data, private_data_len);
2006 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2007 const void *private_data,
2008 u8 private_data_len)
2010 struct cm_id_private *cm_id_priv;
2011 struct ib_mad_send_buf *msg;
2012 unsigned long flags;
2016 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2019 data = cm_copy_private_data(private_data, private_data_len);
2021 return PTR_ERR(data);
2023 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2024 spin_lock_irqsave(&cm_id_priv->lock, flags);
2025 if (cm_id->state != IB_CM_DREQ_RCVD) {
2026 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2031 cm_set_private_data(cm_id_priv, data, private_data_len);
2032 cm_enter_timewait(cm_id_priv);
2034 ret = cm_alloc_msg(cm_id_priv, &msg);
2038 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2039 private_data, private_data_len);
2041 ret = ib_post_send_mad(msg, NULL);
2043 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2048 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2051 EXPORT_SYMBOL(ib_send_cm_drep);
2053 static int cm_issue_drep(struct cm_port *port,
2054 struct ib_mad_recv_wc *mad_recv_wc)
2056 struct ib_mad_send_buf *msg = NULL;
2057 struct cm_dreq_msg *dreq_msg;
2058 struct cm_drep_msg *drep_msg;
2061 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2065 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2066 drep_msg = (struct cm_drep_msg *) msg->mad;
2068 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2069 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2070 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2072 ret = ib_post_send_mad(msg, NULL);
2079 static int cm_dreq_handler(struct cm_work *work)
2081 struct cm_id_private *cm_id_priv;
2082 struct cm_dreq_msg *dreq_msg;
2083 struct ib_mad_send_buf *msg = NULL;
2086 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2087 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2088 dreq_msg->local_comm_id);
2090 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2091 counter[CM_DREQ_COUNTER]);
2092 cm_issue_drep(work->port, work->mad_recv_wc);
2096 work->cm_event.private_data = &dreq_msg->private_data;
2098 spin_lock_irq(&cm_id_priv->lock);
2099 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2102 switch (cm_id_priv->id.state) {
2103 case IB_CM_REP_SENT:
2104 case IB_CM_DREQ_SENT:
2105 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2107 case IB_CM_ESTABLISHED:
2108 case IB_CM_MRA_REP_RCVD:
2110 case IB_CM_TIMEWAIT:
2111 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2112 counter[CM_DREQ_COUNTER]);
2113 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2116 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2117 cm_id_priv->private_data,
2118 cm_id_priv->private_data_len);
2119 spin_unlock_irq(&cm_id_priv->lock);
2121 if (ib_post_send_mad(msg, NULL))
2124 case IB_CM_DREQ_RCVD:
2125 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2126 counter[CM_DREQ_COUNTER]);
2131 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2132 cm_id_priv->tid = dreq_msg->hdr.tid;
2133 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2135 list_add_tail(&work->list, &cm_id_priv->work_list);
2136 spin_unlock_irq(&cm_id_priv->lock);
2139 cm_process_work(cm_id_priv, work);
2141 cm_deref_id(cm_id_priv);
2144 unlock: spin_unlock_irq(&cm_id_priv->lock);
2145 deref: cm_deref_id(cm_id_priv);
2149 static int cm_drep_handler(struct cm_work *work)
2151 struct cm_id_private *cm_id_priv;
2152 struct cm_drep_msg *drep_msg;
2155 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2156 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2157 drep_msg->local_comm_id);
2161 work->cm_event.private_data = &drep_msg->private_data;
2163 spin_lock_irq(&cm_id_priv->lock);
2164 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2165 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2166 spin_unlock_irq(&cm_id_priv->lock);
2169 cm_enter_timewait(cm_id_priv);
2171 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2172 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2174 list_add_tail(&work->list, &cm_id_priv->work_list);
2175 spin_unlock_irq(&cm_id_priv->lock);
2178 cm_process_work(cm_id_priv, work);
2180 cm_deref_id(cm_id_priv);
2183 cm_deref_id(cm_id_priv);
2187 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2188 enum ib_cm_rej_reason reason,
2191 const void *private_data,
2192 u8 private_data_len)
2194 struct cm_id_private *cm_id_priv;
2195 struct ib_mad_send_buf *msg;
2196 unsigned long flags;
2199 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2200 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2203 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2205 spin_lock_irqsave(&cm_id_priv->lock, flags);
2206 switch (cm_id->state) {
2207 case IB_CM_REQ_SENT:
2208 case IB_CM_MRA_REQ_RCVD:
2209 case IB_CM_REQ_RCVD:
2210 case IB_CM_MRA_REQ_SENT:
2211 case IB_CM_REP_RCVD:
2212 case IB_CM_MRA_REP_SENT:
2213 ret = cm_alloc_msg(cm_id_priv, &msg);
2215 cm_format_rej((struct cm_rej_msg *) msg->mad,
2216 cm_id_priv, reason, ari, ari_length,
2217 private_data, private_data_len);
2219 cm_reset_to_idle(cm_id_priv);
2221 case IB_CM_REP_SENT:
2222 case IB_CM_MRA_REP_RCVD:
2223 ret = cm_alloc_msg(cm_id_priv, &msg);
2225 cm_format_rej((struct cm_rej_msg *) msg->mad,
2226 cm_id_priv, reason, ari, ari_length,
2227 private_data, private_data_len);
2229 cm_enter_timewait(cm_id_priv);
2239 ret = ib_post_send_mad(msg, NULL);
2243 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2246 EXPORT_SYMBOL(ib_send_cm_rej);
2248 static void cm_format_rej_event(struct cm_work *work)
2250 struct cm_rej_msg *rej_msg;
2251 struct ib_cm_rej_event_param *param;
2253 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2254 param = &work->cm_event.param.rej_rcvd;
2255 param->ari = rej_msg->ari;
2256 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2257 param->reason = __be16_to_cpu(rej_msg->reason);
2258 work->cm_event.private_data = &rej_msg->private_data;
2261 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2263 struct cm_timewait_info *timewait_info;
2264 struct cm_id_private *cm_id_priv;
2267 remote_id = rej_msg->local_comm_id;
2269 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2270 spin_lock_irq(&cm.lock);
2271 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2273 if (!timewait_info) {
2274 spin_unlock_irq(&cm.lock);
2277 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2278 (timewait_info->work.local_id ^
2279 cm.random_id_operand));
2281 if (cm_id_priv->id.remote_id == remote_id)
2282 atomic_inc(&cm_id_priv->refcount);
2286 spin_unlock_irq(&cm.lock);
2287 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2288 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2290 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2295 static int cm_rej_handler(struct cm_work *work)
2297 struct cm_id_private *cm_id_priv;
2298 struct cm_rej_msg *rej_msg;
2301 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2302 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2306 cm_format_rej_event(work);
2308 spin_lock_irq(&cm_id_priv->lock);
2309 switch (cm_id_priv->id.state) {
2310 case IB_CM_REQ_SENT:
2311 case IB_CM_MRA_REQ_RCVD:
2312 case IB_CM_REP_SENT:
2313 case IB_CM_MRA_REP_RCVD:
2314 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2316 case IB_CM_REQ_RCVD:
2317 case IB_CM_MRA_REQ_SENT:
2318 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2319 cm_enter_timewait(cm_id_priv);
2321 cm_reset_to_idle(cm_id_priv);
2323 case IB_CM_DREQ_SENT:
2324 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2326 case IB_CM_REP_RCVD:
2327 case IB_CM_MRA_REP_SENT:
2328 case IB_CM_ESTABLISHED:
2329 cm_enter_timewait(cm_id_priv);
2332 spin_unlock_irq(&cm_id_priv->lock);
2337 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2339 list_add_tail(&work->list, &cm_id_priv->work_list);
2340 spin_unlock_irq(&cm_id_priv->lock);
2343 cm_process_work(cm_id_priv, work);
2345 cm_deref_id(cm_id_priv);
2348 cm_deref_id(cm_id_priv);
2352 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2354 const void *private_data,
2355 u8 private_data_len)
2357 struct cm_id_private *cm_id_priv;
2358 struct ib_mad_send_buf *msg;
2359 enum ib_cm_state cm_state;
2360 enum ib_cm_lap_state lap_state;
2361 enum cm_msg_response msg_response;
2363 unsigned long flags;
2366 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2369 data = cm_copy_private_data(private_data, private_data_len);
2371 return PTR_ERR(data);
2373 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2375 spin_lock_irqsave(&cm_id_priv->lock, flags);
2376 switch(cm_id_priv->id.state) {
2377 case IB_CM_REQ_RCVD:
2378 cm_state = IB_CM_MRA_REQ_SENT;
2379 lap_state = cm_id->lap_state;
2380 msg_response = CM_MSG_RESPONSE_REQ;
2382 case IB_CM_REP_RCVD:
2383 cm_state = IB_CM_MRA_REP_SENT;
2384 lap_state = cm_id->lap_state;
2385 msg_response = CM_MSG_RESPONSE_REP;
2387 case IB_CM_ESTABLISHED:
2388 cm_state = cm_id->state;
2389 lap_state = IB_CM_MRA_LAP_SENT;
2390 msg_response = CM_MSG_RESPONSE_OTHER;
2397 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2398 ret = cm_alloc_msg(cm_id_priv, &msg);
2402 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2403 msg_response, service_timeout,
2404 private_data, private_data_len);
2405 ret = ib_post_send_mad(msg, NULL);
2410 cm_id->state = cm_state;
2411 cm_id->lap_state = lap_state;
2412 cm_id_priv->service_timeout = service_timeout;
2413 cm_set_private_data(cm_id_priv, data, private_data_len);
2414 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2417 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2421 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2426 EXPORT_SYMBOL(ib_send_cm_mra);
2428 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2430 switch (cm_mra_get_msg_mraed(mra_msg)) {
2431 case CM_MSG_RESPONSE_REQ:
2432 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2433 case CM_MSG_RESPONSE_REP:
2434 case CM_MSG_RESPONSE_OTHER:
2435 return cm_acquire_id(mra_msg->remote_comm_id,
2436 mra_msg->local_comm_id);
2442 static int cm_mra_handler(struct cm_work *work)
2444 struct cm_id_private *cm_id_priv;
2445 struct cm_mra_msg *mra_msg;
2448 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2449 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2453 work->cm_event.private_data = &mra_msg->private_data;
2454 work->cm_event.param.mra_rcvd.service_timeout =
2455 cm_mra_get_service_timeout(mra_msg);
2456 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2457 cm_convert_to_ms(cm_id_priv->av.timeout);
2459 spin_lock_irq(&cm_id_priv->lock);
2460 switch (cm_id_priv->id.state) {
2461 case IB_CM_REQ_SENT:
2462 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2463 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2464 cm_id_priv->msg, timeout))
2466 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2468 case IB_CM_REP_SENT:
2469 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2470 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2471 cm_id_priv->msg, timeout))
2473 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2475 case IB_CM_ESTABLISHED:
2476 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2477 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2478 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2479 cm_id_priv->msg, timeout)) {
2480 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2481 atomic_long_inc(&work->port->
2482 counter_group[CM_RECV_DUPLICATES].
2483 counter[CM_MRA_COUNTER]);
2486 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2488 case IB_CM_MRA_REQ_RCVD:
2489 case IB_CM_MRA_REP_RCVD:
2490 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2491 counter[CM_MRA_COUNTER]);
2497 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2498 cm_id_priv->id.state;
2499 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2501 list_add_tail(&work->list, &cm_id_priv->work_list);
2502 spin_unlock_irq(&cm_id_priv->lock);
2505 cm_process_work(cm_id_priv, work);
2507 cm_deref_id(cm_id_priv);
2510 spin_unlock_irq(&cm_id_priv->lock);
2511 cm_deref_id(cm_id_priv);
2515 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2516 struct cm_id_private *cm_id_priv,
2517 struct ib_sa_path_rec *alternate_path,
2518 const void *private_data,
2519 u8 private_data_len)
2521 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2522 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2523 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2524 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2525 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2526 /* todo: need remote CM response timeout */
2527 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2528 lap_msg->alt_local_lid = alternate_path->slid;
2529 lap_msg->alt_remote_lid = alternate_path->dlid;
2530 lap_msg->alt_local_gid = alternate_path->sgid;
2531 lap_msg->alt_remote_gid = alternate_path->dgid;
2532 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2533 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2534 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2535 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2536 cm_lap_set_sl(lap_msg, alternate_path->sl);
2537 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2538 cm_lap_set_local_ack_timeout(lap_msg,
2539 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2540 alternate_path->packet_life_time));
2542 if (private_data && private_data_len)
2543 memcpy(lap_msg->private_data, private_data, private_data_len);
2546 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2547 struct ib_sa_path_rec *alternate_path,
2548 const void *private_data,
2549 u8 private_data_len)
2551 struct cm_id_private *cm_id_priv;
2552 struct ib_mad_send_buf *msg;
2553 unsigned long flags;
2556 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2559 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2560 spin_lock_irqsave(&cm_id_priv->lock, flags);
2561 if (cm_id->state != IB_CM_ESTABLISHED ||
2562 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2563 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2568 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2571 cm_id_priv->alt_av.timeout =
2572 cm_ack_timeout(cm_id_priv->target_ack_delay,
2573 cm_id_priv->alt_av.timeout - 1);
2575 ret = cm_alloc_msg(cm_id_priv, &msg);
2579 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2580 alternate_path, private_data, private_data_len);
2581 msg->timeout_ms = cm_id_priv->timeout_ms;
2582 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2584 ret = ib_post_send_mad(msg, NULL);
2586 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2591 cm_id->lap_state = IB_CM_LAP_SENT;
2592 cm_id_priv->msg = msg;
2594 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2597 EXPORT_SYMBOL(ib_send_cm_lap);
2599 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2600 struct ib_sa_path_rec *path,
2601 struct cm_lap_msg *lap_msg)
2603 memset(path, 0, sizeof *path);
2604 path->dgid = lap_msg->alt_local_gid;
2605 path->sgid = lap_msg->alt_remote_gid;
2606 path->dlid = lap_msg->alt_local_lid;
2607 path->slid = lap_msg->alt_remote_lid;
2608 path->flow_label = cm_lap_get_flow_label(lap_msg);
2609 path->hop_limit = lap_msg->alt_hop_limit;
2610 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2611 path->reversible = 1;
2612 path->pkey = cm_id_priv->pkey;
2613 path->sl = cm_lap_get_sl(lap_msg);
2614 path->mtu_selector = IB_SA_EQ;
2615 path->mtu = cm_id_priv->path_mtu;
2616 path->rate_selector = IB_SA_EQ;
2617 path->rate = cm_lap_get_packet_rate(lap_msg);
2618 path->packet_life_time_selector = IB_SA_EQ;
2619 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2620 path->packet_life_time -= (path->packet_life_time > 0);
2623 static int cm_lap_handler(struct cm_work *work)
2625 struct cm_id_private *cm_id_priv;
2626 struct cm_lap_msg *lap_msg;
2627 struct ib_cm_lap_event_param *param;
2628 struct ib_mad_send_buf *msg = NULL;
2631 /* todo: verify LAP request and send reject APR if invalid. */
2632 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2633 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2634 lap_msg->local_comm_id);
2638 param = &work->cm_event.param.lap_rcvd;
2639 param->alternate_path = &work->path[0];
2640 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2641 work->cm_event.private_data = &lap_msg->private_data;
2643 spin_lock_irq(&cm_id_priv->lock);
2644 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2647 switch (cm_id_priv->id.lap_state) {
2648 case IB_CM_LAP_UNINIT:
2649 case IB_CM_LAP_IDLE:
2651 case IB_CM_MRA_LAP_SENT:
2652 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2653 counter[CM_LAP_COUNTER]);
2654 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2657 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2658 CM_MSG_RESPONSE_OTHER,
2659 cm_id_priv->service_timeout,
2660 cm_id_priv->private_data,
2661 cm_id_priv->private_data_len);
2662 spin_unlock_irq(&cm_id_priv->lock);
2664 if (ib_post_send_mad(msg, NULL))
2667 case IB_CM_LAP_RCVD:
2668 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2669 counter[CM_LAP_COUNTER]);
2675 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2676 cm_id_priv->tid = lap_msg->hdr.tid;
2677 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2678 work->mad_recv_wc->recv_buf.grh,
2680 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2681 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2683 list_add_tail(&work->list, &cm_id_priv->work_list);
2684 spin_unlock_irq(&cm_id_priv->lock);
2687 cm_process_work(cm_id_priv, work);
2689 cm_deref_id(cm_id_priv);
2692 unlock: spin_unlock_irq(&cm_id_priv->lock);
2693 deref: cm_deref_id(cm_id_priv);
2697 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2698 struct cm_id_private *cm_id_priv,
2699 enum ib_cm_apr_status status,
2702 const void *private_data,
2703 u8 private_data_len)
2705 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2706 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2707 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2708 apr_msg->ap_status = (u8) status;
2710 if (info && info_length) {
2711 apr_msg->info_length = info_length;
2712 memcpy(apr_msg->info, info, info_length);
2715 if (private_data && private_data_len)
2716 memcpy(apr_msg->private_data, private_data, private_data_len);
2719 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2720 enum ib_cm_apr_status status,
2723 const void *private_data,
2724 u8 private_data_len)
2726 struct cm_id_private *cm_id_priv;
2727 struct ib_mad_send_buf *msg;
2728 unsigned long flags;
2731 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2732 (info && info_length > IB_CM_APR_INFO_LENGTH))
2735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2736 spin_lock_irqsave(&cm_id_priv->lock, flags);
2737 if (cm_id->state != IB_CM_ESTABLISHED ||
2738 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2739 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2744 ret = cm_alloc_msg(cm_id_priv, &msg);
2748 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2749 info, info_length, private_data, private_data_len);
2750 ret = ib_post_send_mad(msg, NULL);
2752 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2757 cm_id->lap_state = IB_CM_LAP_IDLE;
2758 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2761 EXPORT_SYMBOL(ib_send_cm_apr);
2763 static int cm_apr_handler(struct cm_work *work)
2765 struct cm_id_private *cm_id_priv;
2766 struct cm_apr_msg *apr_msg;
2769 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2770 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2771 apr_msg->local_comm_id);
2773 return -EINVAL; /* Unmatched reply. */
2775 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2776 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2777 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2778 work->cm_event.private_data = &apr_msg->private_data;
2780 spin_lock_irq(&cm_id_priv->lock);
2781 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2782 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2783 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2784 spin_unlock_irq(&cm_id_priv->lock);
2787 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2788 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2789 cm_id_priv->msg = NULL;
2791 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2793 list_add_tail(&work->list, &cm_id_priv->work_list);
2794 spin_unlock_irq(&cm_id_priv->lock);
2797 cm_process_work(cm_id_priv, work);
2799 cm_deref_id(cm_id_priv);
2802 cm_deref_id(cm_id_priv);
2806 static int cm_timewait_handler(struct cm_work *work)
2808 struct cm_timewait_info *timewait_info;
2809 struct cm_id_private *cm_id_priv;
2812 timewait_info = (struct cm_timewait_info *)work;
2813 spin_lock_irq(&cm.lock);
2814 list_del(&timewait_info->list);
2815 spin_unlock_irq(&cm.lock);
2817 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2818 timewait_info->work.remote_id);
2822 spin_lock_irq(&cm_id_priv->lock);
2823 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2824 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2825 spin_unlock_irq(&cm_id_priv->lock);
2828 cm_id_priv->id.state = IB_CM_IDLE;
2829 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2831 list_add_tail(&work->list, &cm_id_priv->work_list);
2832 spin_unlock_irq(&cm_id_priv->lock);
2835 cm_process_work(cm_id_priv, work);
2837 cm_deref_id(cm_id_priv);
2840 cm_deref_id(cm_id_priv);
2844 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2845 struct cm_id_private *cm_id_priv,
2846 struct ib_cm_sidr_req_param *param)
2848 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2849 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2850 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2851 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2852 sidr_req_msg->service_id = param->service_id;
2854 if (param->private_data && param->private_data_len)
2855 memcpy(sidr_req_msg->private_data, param->private_data,
2856 param->private_data_len);
2859 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2860 struct ib_cm_sidr_req_param *param)
2862 struct cm_id_private *cm_id_priv;
2863 struct ib_mad_send_buf *msg;
2864 unsigned long flags;
2867 if (!param->path || (param->private_data &&
2868 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2871 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2872 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2876 cm_id->service_id = param->service_id;
2877 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2878 cm_id_priv->timeout_ms = param->timeout_ms;
2879 cm_id_priv->max_cm_retries = param->max_cm_retries;
2880 ret = cm_alloc_msg(cm_id_priv, &msg);
2884 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2886 msg->timeout_ms = cm_id_priv->timeout_ms;
2887 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2889 spin_lock_irqsave(&cm_id_priv->lock, flags);
2890 if (cm_id->state == IB_CM_IDLE)
2891 ret = ib_post_send_mad(msg, NULL);
2896 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2900 cm_id->state = IB_CM_SIDR_REQ_SENT;
2901 cm_id_priv->msg = msg;
2902 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2906 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2908 static void cm_format_sidr_req_event(struct cm_work *work,
2909 struct ib_cm_id *listen_id)
2911 struct cm_sidr_req_msg *sidr_req_msg;
2912 struct ib_cm_sidr_req_event_param *param;
2914 sidr_req_msg = (struct cm_sidr_req_msg *)
2915 work->mad_recv_wc->recv_buf.mad;
2916 param = &work->cm_event.param.sidr_req_rcvd;
2917 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2918 param->listen_id = listen_id;
2919 param->port = work->port->port_num;
2920 work->cm_event.private_data = &sidr_req_msg->private_data;
2923 static int cm_sidr_req_handler(struct cm_work *work)
2925 struct ib_cm_id *cm_id;
2926 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2927 struct cm_sidr_req_msg *sidr_req_msg;
2930 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2932 return PTR_ERR(cm_id);
2933 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2935 /* Record SGID/SLID and request ID for lookup. */
2936 sidr_req_msg = (struct cm_sidr_req_msg *)
2937 work->mad_recv_wc->recv_buf.mad;
2938 wc = work->mad_recv_wc->wc;
2939 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2940 cm_id_priv->av.dgid.global.interface_id = 0;
2941 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2942 work->mad_recv_wc->recv_buf.grh,
2944 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2945 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2946 atomic_inc(&cm_id_priv->work_count);
2948 spin_lock_irq(&cm.lock);
2949 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2950 if (cur_cm_id_priv) {
2951 spin_unlock_irq(&cm.lock);
2952 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2953 counter[CM_SIDR_REQ_COUNTER]);
2954 goto out; /* Duplicate message. */
2956 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2957 cur_cm_id_priv = cm_find_listen(cm_id->device,
2958 sidr_req_msg->service_id,
2959 sidr_req_msg->private_data);
2960 if (!cur_cm_id_priv) {
2961 spin_unlock_irq(&cm.lock);
2962 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
2963 goto out; /* No match. */
2965 atomic_inc(&cur_cm_id_priv->refcount);
2966 spin_unlock_irq(&cm.lock);
2968 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2969 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2970 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2971 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2973 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2974 cm_process_work(cm_id_priv, work);
2975 cm_deref_id(cur_cm_id_priv);
2978 ib_destroy_cm_id(&cm_id_priv->id);
2982 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2983 struct cm_id_private *cm_id_priv,
2984 struct ib_cm_sidr_rep_param *param)
2986 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2988 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2989 sidr_rep_msg->status = param->status;
2990 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2991 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2992 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2994 if (param->info && param->info_length)
2995 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2997 if (param->private_data && param->private_data_len)
2998 memcpy(sidr_rep_msg->private_data, param->private_data,
2999 param->private_data_len);
3002 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3003 struct ib_cm_sidr_rep_param *param)
3005 struct cm_id_private *cm_id_priv;
3006 struct ib_mad_send_buf *msg;
3007 unsigned long flags;
3010 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3011 (param->private_data &&
3012 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3015 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3016 spin_lock_irqsave(&cm_id_priv->lock, flags);
3017 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3022 ret = cm_alloc_msg(cm_id_priv, &msg);
3026 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3028 ret = ib_post_send_mad(msg, NULL);
3030 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3034 cm_id->state = IB_CM_IDLE;
3035 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3037 spin_lock_irqsave(&cm.lock, flags);
3038 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3039 spin_unlock_irqrestore(&cm.lock, flags);
3042 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3045 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3047 static void cm_format_sidr_rep_event(struct cm_work *work)
3049 struct cm_sidr_rep_msg *sidr_rep_msg;
3050 struct ib_cm_sidr_rep_event_param *param;
3052 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3053 work->mad_recv_wc->recv_buf.mad;
3054 param = &work->cm_event.param.sidr_rep_rcvd;
3055 param->status = sidr_rep_msg->status;
3056 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3057 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3058 param->info = &sidr_rep_msg->info;
3059 param->info_len = sidr_rep_msg->info_length;
3060 work->cm_event.private_data = &sidr_rep_msg->private_data;
3063 static int cm_sidr_rep_handler(struct cm_work *work)
3065 struct cm_sidr_rep_msg *sidr_rep_msg;
3066 struct cm_id_private *cm_id_priv;
3068 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3069 work->mad_recv_wc->recv_buf.mad;
3070 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3072 return -EINVAL; /* Unmatched reply. */
3074 spin_lock_irq(&cm_id_priv->lock);
3075 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3076 spin_unlock_irq(&cm_id_priv->lock);
3079 cm_id_priv->id.state = IB_CM_IDLE;
3080 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3081 spin_unlock_irq(&cm_id_priv->lock);
3083 cm_format_sidr_rep_event(work);
3084 cm_process_work(cm_id_priv, work);
3087 cm_deref_id(cm_id_priv);
3091 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3092 enum ib_wc_status wc_status)
3094 struct cm_id_private *cm_id_priv;
3095 struct ib_cm_event cm_event;
3096 enum ib_cm_state state;
3099 memset(&cm_event, 0, sizeof cm_event);
3100 cm_id_priv = msg->context[0];
3102 /* Discard old sends or ones without a response. */
3103 spin_lock_irq(&cm_id_priv->lock);
3104 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3105 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3109 case IB_CM_REQ_SENT:
3110 case IB_CM_MRA_REQ_RCVD:
3111 cm_reset_to_idle(cm_id_priv);
3112 cm_event.event = IB_CM_REQ_ERROR;
3114 case IB_CM_REP_SENT:
3115 case IB_CM_MRA_REP_RCVD:
3116 cm_reset_to_idle(cm_id_priv);
3117 cm_event.event = IB_CM_REP_ERROR;
3119 case IB_CM_DREQ_SENT:
3120 cm_enter_timewait(cm_id_priv);
3121 cm_event.event = IB_CM_DREQ_ERROR;
3123 case IB_CM_SIDR_REQ_SENT:
3124 cm_id_priv->id.state = IB_CM_IDLE;
3125 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3130 spin_unlock_irq(&cm_id_priv->lock);
3131 cm_event.param.send_status = wc_status;
3133 /* No other events can occur on the cm_id at this point. */
3134 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3137 ib_destroy_cm_id(&cm_id_priv->id);
3140 spin_unlock_irq(&cm_id_priv->lock);
3144 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3145 struct ib_mad_send_wc *mad_send_wc)
3147 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3148 struct cm_port *port;
3151 port = mad_agent->context;
3152 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3153 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3156 * If the send was in response to a received message (context[0] is not
3157 * set to a cm_id), and is not a REJ, then it is a send that was
3160 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3163 atomic_long_add(1 + msg->retries,
3164 &port->counter_group[CM_XMIT].counter[attr_index]);
3166 atomic_long_add(msg->retries,
3167 &port->counter_group[CM_XMIT_RETRIES].
3168 counter[attr_index]);
3170 switch (mad_send_wc->status) {
3172 case IB_WC_WR_FLUSH_ERR:
3176 if (msg->context[0] && msg->context[1])
3177 cm_process_send_error(msg, mad_send_wc->status);
3184 static void cm_work_handler(struct work_struct *_work)
3186 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3189 switch (work->cm_event.event) {
3190 case IB_CM_REQ_RECEIVED:
3191 ret = cm_req_handler(work);
3193 case IB_CM_MRA_RECEIVED:
3194 ret = cm_mra_handler(work);
3196 case IB_CM_REJ_RECEIVED:
3197 ret = cm_rej_handler(work);
3199 case IB_CM_REP_RECEIVED:
3200 ret = cm_rep_handler(work);
3202 case IB_CM_RTU_RECEIVED:
3203 ret = cm_rtu_handler(work);
3205 case IB_CM_USER_ESTABLISHED:
3206 ret = cm_establish_handler(work);
3208 case IB_CM_DREQ_RECEIVED:
3209 ret = cm_dreq_handler(work);
3211 case IB_CM_DREP_RECEIVED:
3212 ret = cm_drep_handler(work);
3214 case IB_CM_SIDR_REQ_RECEIVED:
3215 ret = cm_sidr_req_handler(work);
3217 case IB_CM_SIDR_REP_RECEIVED:
3218 ret = cm_sidr_rep_handler(work);
3220 case IB_CM_LAP_RECEIVED:
3221 ret = cm_lap_handler(work);
3223 case IB_CM_APR_RECEIVED:
3224 ret = cm_apr_handler(work);
3226 case IB_CM_TIMEWAIT_EXIT:
3227 ret = cm_timewait_handler(work);
3237 static int cm_establish(struct ib_cm_id *cm_id)
3239 struct cm_id_private *cm_id_priv;
3240 struct cm_work *work;
3241 unsigned long flags;
3244 work = kmalloc(sizeof *work, GFP_ATOMIC);
3248 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3249 spin_lock_irqsave(&cm_id_priv->lock, flags);
3250 switch (cm_id->state)
3252 case IB_CM_REP_SENT:
3253 case IB_CM_MRA_REP_RCVD:
3254 cm_id->state = IB_CM_ESTABLISHED;
3256 case IB_CM_ESTABLISHED:
3263 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3271 * The CM worker thread may try to destroy the cm_id before it
3272 * can execute this work item. To prevent potential deadlock,
3273 * we need to find the cm_id once we're in the context of the
3274 * worker thread, rather than holding a reference on it.
3276 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3277 work->local_id = cm_id->local_id;
3278 work->remote_id = cm_id->remote_id;
3279 work->mad_recv_wc = NULL;
3280 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3281 queue_delayed_work(cm.wq, &work->work, 0);
3286 static int cm_migrate(struct ib_cm_id *cm_id)
3288 struct cm_id_private *cm_id_priv;
3289 unsigned long flags;
3292 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3293 spin_lock_irqsave(&cm_id_priv->lock, flags);
3294 if (cm_id->state == IB_CM_ESTABLISHED &&
3295 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3296 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3297 cm_id->lap_state = IB_CM_LAP_IDLE;
3298 cm_id_priv->av = cm_id_priv->alt_av;
3301 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3306 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3311 case IB_EVENT_COMM_EST:
3312 ret = cm_establish(cm_id);
3314 case IB_EVENT_PATH_MIG:
3315 ret = cm_migrate(cm_id);
3322 EXPORT_SYMBOL(ib_cm_notify);
3324 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3325 struct ib_mad_recv_wc *mad_recv_wc)
3327 struct cm_port *port = mad_agent->context;
3328 struct cm_work *work;
3329 enum ib_cm_event_type event;
3333 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3334 case CM_REQ_ATTR_ID:
3335 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3336 alt_local_lid != 0);
3337 event = IB_CM_REQ_RECEIVED;
3339 case CM_MRA_ATTR_ID:
3340 event = IB_CM_MRA_RECEIVED;
3342 case CM_REJ_ATTR_ID:
3343 event = IB_CM_REJ_RECEIVED;
3345 case CM_REP_ATTR_ID:
3346 event = IB_CM_REP_RECEIVED;
3348 case CM_RTU_ATTR_ID:
3349 event = IB_CM_RTU_RECEIVED;
3351 case CM_DREQ_ATTR_ID:
3352 event = IB_CM_DREQ_RECEIVED;
3354 case CM_DREP_ATTR_ID:
3355 event = IB_CM_DREP_RECEIVED;
3357 case CM_SIDR_REQ_ATTR_ID:
3358 event = IB_CM_SIDR_REQ_RECEIVED;
3360 case CM_SIDR_REP_ATTR_ID:
3361 event = IB_CM_SIDR_REP_RECEIVED;
3363 case CM_LAP_ATTR_ID:
3365 event = IB_CM_LAP_RECEIVED;
3367 case CM_APR_ATTR_ID:
3368 event = IB_CM_APR_RECEIVED;
3371 ib_free_recv_mad(mad_recv_wc);
3375 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3376 atomic_long_inc(&port->counter_group[CM_RECV].
3377 counter[attr_id - CM_ATTR_ID_OFFSET]);
3379 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3382 ib_free_recv_mad(mad_recv_wc);
3386 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3387 work->cm_event.event = event;
3388 work->mad_recv_wc = mad_recv_wc;
3390 queue_delayed_work(cm.wq, &work->work, 0);
3393 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3394 struct ib_qp_attr *qp_attr,
3397 unsigned long flags;
3400 spin_lock_irqsave(&cm_id_priv->lock, flags);
3401 switch (cm_id_priv->id.state) {
3402 case IB_CM_REQ_SENT:
3403 case IB_CM_MRA_REQ_RCVD:
3404 case IB_CM_REQ_RCVD:
3405 case IB_CM_MRA_REQ_SENT:
3406 case IB_CM_REP_RCVD:
3407 case IB_CM_MRA_REP_SENT:
3408 case IB_CM_REP_SENT:
3409 case IB_CM_MRA_REP_RCVD:
3410 case IB_CM_ESTABLISHED:
3411 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3412 IB_QP_PKEY_INDEX | IB_QP_PORT;
3413 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3414 if (cm_id_priv->responder_resources)
3415 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3416 IB_ACCESS_REMOTE_ATOMIC;
3417 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3418 qp_attr->port_num = cm_id_priv->av.port->port_num;
3425 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3429 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3430 struct ib_qp_attr *qp_attr,
3433 unsigned long flags;
3436 spin_lock_irqsave(&cm_id_priv->lock, flags);
3437 switch (cm_id_priv->id.state) {
3438 case IB_CM_REQ_RCVD:
3439 case IB_CM_MRA_REQ_SENT:
3440 case IB_CM_REP_RCVD:
3441 case IB_CM_MRA_REP_SENT:
3442 case IB_CM_REP_SENT:
3443 case IB_CM_MRA_REP_RCVD:
3444 case IB_CM_ESTABLISHED:
3445 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3446 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3447 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3448 qp_attr->path_mtu = cm_id_priv->path_mtu;
3449 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3450 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3451 if (cm_id_priv->qp_type == IB_QPT_RC) {
3452 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3453 IB_QP_MIN_RNR_TIMER;
3454 qp_attr->max_dest_rd_atomic =
3455 cm_id_priv->responder_resources;
3456 qp_attr->min_rnr_timer = 0;
3458 if (cm_id_priv->alt_av.ah_attr.dlid) {
3459 *qp_attr_mask |= IB_QP_ALT_PATH;
3460 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3461 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3462 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3463 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3471 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3475 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3476 struct ib_qp_attr *qp_attr,
3479 unsigned long flags;
3482 spin_lock_irqsave(&cm_id_priv->lock, flags);
3483 switch (cm_id_priv->id.state) {
3484 /* Allow transition to RTS before sending REP */
3485 case IB_CM_REQ_RCVD:
3486 case IB_CM_MRA_REQ_SENT:
3488 case IB_CM_REP_RCVD:
3489 case IB_CM_MRA_REP_SENT:
3490 case IB_CM_REP_SENT:
3491 case IB_CM_MRA_REP_RCVD:
3492 case IB_CM_ESTABLISHED:
3493 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3494 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3495 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3496 if (cm_id_priv->qp_type == IB_QPT_RC) {
3497 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3499 IB_QP_MAX_QP_RD_ATOMIC;
3500 qp_attr->timeout = cm_id_priv->av.timeout;
3501 qp_attr->retry_cnt = cm_id_priv->retry_count;
3502 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3503 qp_attr->max_rd_atomic =
3504 cm_id_priv->initiator_depth;
3506 if (cm_id_priv->alt_av.ah_attr.dlid) {
3507 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3508 qp_attr->path_mig_state = IB_MIG_REARM;
3511 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3512 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3513 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3514 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3515 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3516 qp_attr->path_mig_state = IB_MIG_REARM;
3524 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3528 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3529 struct ib_qp_attr *qp_attr,
3532 struct cm_id_private *cm_id_priv;
3535 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3536 switch (qp_attr->qp_state) {
3538 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3541 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3544 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3552 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3554 static void cm_get_ack_delay(struct cm_device *cm_dev)
3556 struct ib_device_attr attr;
3558 if (ib_query_device(cm_dev->device, &attr))
3559 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3561 cm_dev->ack_delay = attr.local_ca_ack_delay;
3564 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3567 struct cm_counter_group *group;
3568 struct cm_counter_attribute *cm_attr;
3570 group = container_of(obj, struct cm_counter_group, obj);
3571 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3573 return sprintf(buf, "%ld\n",
3574 atomic_long_read(&group->counter[cm_attr->index]));
3577 static struct sysfs_ops cm_counter_ops = {
3578 .show = cm_show_counter
3581 static struct kobj_type cm_counter_obj_type = {
3582 .sysfs_ops = &cm_counter_ops,
3583 .default_attrs = cm_counter_default_attrs
3586 static void cm_release_port_obj(struct kobject *obj)
3588 struct cm_port *cm_port;
3590 cm_port = container_of(obj, struct cm_port, port_obj);
3594 static struct kobj_type cm_port_obj_type = {
3595 .release = cm_release_port_obj
3598 static void cm_release_dev_obj(struct kobject *obj)
3600 struct cm_device *cm_dev;
3602 cm_dev = container_of(obj, struct cm_device, dev_obj);
3606 static struct kobj_type cm_dev_obj_type = {
3607 .release = cm_release_dev_obj
3610 struct class cm_class = {
3611 .name = "infiniband_cm",
3613 EXPORT_SYMBOL(cm_class);
3615 static int cm_create_port_fs(struct cm_port *port)
3619 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3620 &port->cm_dev->dev_obj,
3621 "%d", port->port_num);
3627 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3628 ret = kobject_init_and_add(&port->counter_group[i].obj,
3629 &cm_counter_obj_type,
3631 "%s", counter_group_names[i]);
3640 kobject_put(&port->counter_group[i].obj);
3641 kobject_put(&port->port_obj);
3646 static void cm_remove_port_fs(struct cm_port *port)
3650 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3651 kobject_put(&port->counter_group[i].obj);
3653 kobject_put(&port->port_obj);
3656 static void cm_add_one(struct ib_device *device)
3658 struct cm_device *cm_dev;
3659 struct cm_port *port;
3660 struct ib_mad_reg_req reg_req = {
3661 .mgmt_class = IB_MGMT_CLASS_CM,
3662 .mgmt_class_version = IB_CM_CLASS_VERSION
3664 struct ib_port_modify port_modify = {
3665 .set_port_cap_mask = IB_PORT_CM_SUP
3667 unsigned long flags;
3671 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3674 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3675 device->phys_port_cnt, GFP_KERNEL);
3679 cm_dev->device = device;
3680 cm_get_ack_delay(cm_dev);
3682 ret = kobject_init_and_add(&cm_dev->dev_obj, &cm_dev_obj_type,
3683 &cm_class.subsys.kobj, "%s", device->name);
3689 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3690 for (i = 1; i <= device->phys_port_cnt; i++) {
3691 port = kzalloc(sizeof *port, GFP_KERNEL);
3695 cm_dev->port[i-1] = port;
3696 port->cm_dev = cm_dev;
3699 ret = cm_create_port_fs(port);
3703 port->mad_agent = ib_register_mad_agent(device, i,
3710 if (IS_ERR(port->mad_agent))
3713 ret = ib_modify_port(device, i, 0, &port_modify);
3717 ib_set_client_data(device, &cm_client, cm_dev);
3719 write_lock_irqsave(&cm.device_lock, flags);
3720 list_add_tail(&cm_dev->list, &cm.device_list);
3721 write_unlock_irqrestore(&cm.device_lock, flags);
3725 ib_unregister_mad_agent(port->mad_agent);
3727 cm_remove_port_fs(port);
3729 port_modify.set_port_cap_mask = 0;
3730 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3732 port = cm_dev->port[i-1];
3733 ib_modify_port(device, port->port_num, 0, &port_modify);
3734 ib_unregister_mad_agent(port->mad_agent);
3735 cm_remove_port_fs(port);
3737 kobject_put(&cm_dev->dev_obj);
3740 static void cm_remove_one(struct ib_device *device)
3742 struct cm_device *cm_dev;
3743 struct cm_port *port;
3744 struct ib_port_modify port_modify = {
3745 .clr_port_cap_mask = IB_PORT_CM_SUP
3747 unsigned long flags;
3750 cm_dev = ib_get_client_data(device, &cm_client);
3754 write_lock_irqsave(&cm.device_lock, flags);
3755 list_del(&cm_dev->list);
3756 write_unlock_irqrestore(&cm.device_lock, flags);
3758 for (i = 1; i <= device->phys_port_cnt; i++) {
3759 port = cm_dev->port[i-1];
3760 ib_modify_port(device, port->port_num, 0, &port_modify);
3761 ib_unregister_mad_agent(port->mad_agent);
3762 flush_workqueue(cm.wq);
3763 cm_remove_port_fs(port);
3765 kobject_put(&cm_dev->dev_obj);
3768 static int __init ib_cm_init(void)
3772 memset(&cm, 0, sizeof cm);
3773 INIT_LIST_HEAD(&cm.device_list);
3774 rwlock_init(&cm.device_lock);
3775 spin_lock_init(&cm.lock);
3776 cm.listen_service_table = RB_ROOT;
3777 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3778 cm.remote_id_table = RB_ROOT;
3779 cm.remote_qp_table = RB_ROOT;
3780 cm.remote_sidr_table = RB_ROOT;
3781 idr_init(&cm.local_id_table);
3782 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3783 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3784 INIT_LIST_HEAD(&cm.timewait_list);
3786 ret = class_register(&cm_class);
3790 cm.wq = create_workqueue("ib_cm");
3796 ret = ib_register_client(&cm_client);
3802 destroy_workqueue(cm.wq);
3804 class_unregister(&cm_class);
3808 static void __exit ib_cm_cleanup(void)
3810 struct cm_timewait_info *timewait_info, *tmp;
3812 spin_lock_irq(&cm.lock);
3813 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3814 cancel_delayed_work(&timewait_info->work.work);
3815 spin_unlock_irq(&cm.lock);
3817 ib_unregister_client(&cm_client);
3818 destroy_workqueue(cm.wq);
3820 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3821 list_del(&timewait_info->list);
3822 kfree(timewait_info);
3825 class_unregister(&cm_class);
3826 idr_destroy(&cm.local_id_table);
3829 module_init(ib_cm_init);
3830 module_exit(ib_cm_cleanup);