2 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/pci.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
56 static void cm_add_one(struct ib_device *device);
57 static void cm_remove_one(struct ib_device *device);
59 static struct ib_client cm_client = {
62 .remove = cm_remove_one
67 struct list_head device_list;
69 struct rb_root listen_service_table;
70 u64 listen_service_id;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table;
73 struct rb_root remote_id_table;
74 struct rb_root remote_sidr_table;
75 struct idr local_id_table;
76 struct workqueue_struct *wq;
80 struct cm_device *cm_dev;
81 struct ib_mad_agent *mad_agent;
86 struct list_head list;
87 struct ib_device *device;
89 struct cm_port port[0];
95 struct ib_ah_attr ah_attr;
101 struct work_struct work;
102 struct list_head list;
103 struct cm_port *port;
104 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
105 __be32 local_id; /* Established / timewait */
107 struct ib_cm_event cm_event;
108 struct ib_sa_path_rec path[0];
111 struct cm_timewait_info {
112 struct cm_work work; /* Must be first. */
113 struct rb_node remote_qp_node;
114 struct rb_node remote_id_node;
115 __be64 remote_ca_guid;
117 u8 inserted_remote_qp;
118 u8 inserted_remote_id;
121 struct cm_id_private {
124 struct rb_node service_node;
125 struct rb_node sidr_id_node;
126 spinlock_t lock; /* Do not acquire inside cm.lock */
127 struct completion comp;
130 struct ib_mad_send_buf *msg;
131 struct cm_timewait_info *timewait_info;
132 /* todo: use alternate port on send failure */
135 struct ib_cm_compare_data *compare_data;
141 enum ib_qp_type qp_type;
145 enum ib_mtu path_mtu;
149 u8 responder_resources;
151 u8 local_ack_timeout;
156 struct list_head work_list;
160 static void cm_work_handler(void *data);
162 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
164 if (atomic_dec_and_test(&cm_id_priv->refcount))
165 complete(&cm_id_priv->comp);
168 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
169 struct ib_mad_send_buf **msg)
171 struct ib_mad_agent *mad_agent;
172 struct ib_mad_send_buf *m;
175 mad_agent = cm_id_priv->av.port->mad_agent;
176 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
180 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
181 cm_id_priv->av.pkey_index,
182 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
189 /* Timeout set by caller if response is expected. */
191 m->retries = cm_id_priv->max_cm_retries;
193 atomic_inc(&cm_id_priv->refcount);
194 m->context[0] = cm_id_priv;
199 static int cm_alloc_response_msg(struct cm_port *port,
200 struct ib_mad_recv_wc *mad_recv_wc,
201 struct ib_mad_send_buf **msg)
203 struct ib_mad_send_buf *m;
206 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
207 mad_recv_wc->recv_buf.grh, port->port_num);
211 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
212 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
223 static void cm_free_msg(struct ib_mad_send_buf *msg)
225 ib_destroy_ah(msg->ah);
227 cm_deref_id(msg->context[0]);
228 ib_free_send_mad(msg);
231 static void * cm_copy_private_data(const void *private_data,
236 if (!private_data || !private_data_len)
239 data = kmalloc(private_data_len, GFP_KERNEL);
241 return ERR_PTR(-ENOMEM);
243 memcpy(data, private_data, private_data_len);
247 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
248 void *private_data, u8 private_data_len)
250 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
251 kfree(cm_id_priv->private_data);
253 cm_id_priv->private_data = private_data;
254 cm_id_priv->private_data_len = private_data_len;
257 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
258 struct ib_grh *grh, struct cm_av *av)
261 av->pkey_index = wc->pkey_index;
262 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
266 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
268 struct cm_device *cm_dev;
269 struct cm_port *port = NULL;
274 read_lock_irqsave(&cm.device_lock, flags);
275 list_for_each_entry(cm_dev, &cm.device_list, list) {
276 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
278 port = &cm_dev->port[p-1];
282 read_unlock_irqrestore(&cm.device_lock, flags);
287 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
288 be16_to_cpu(path->pkey), &av->pkey_index);
293 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
295 av->packet_life_time = path->packet_life_time;
299 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
306 spin_lock_irqsave(&cm.lock, flags);
307 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, next_id++,
308 (__force int *) &cm_id_priv->id.local_id);
309 spin_unlock_irqrestore(&cm.lock, flags);
310 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
314 static void cm_free_id(__be32 local_id)
318 spin_lock_irqsave(&cm.lock, flags);
319 idr_remove(&cm.local_id_table, (__force int) local_id);
320 spin_unlock_irqrestore(&cm.lock, flags);
323 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
325 struct cm_id_private *cm_id_priv;
327 cm_id_priv = idr_find(&cm.local_id_table, (__force int) local_id);
329 if (cm_id_priv->id.remote_id == remote_id)
330 atomic_inc(&cm_id_priv->refcount);
338 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
340 struct cm_id_private *cm_id_priv;
343 spin_lock_irqsave(&cm.lock, flags);
344 cm_id_priv = cm_get_id(local_id, remote_id);
345 spin_unlock_irqrestore(&cm.lock, flags);
350 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
354 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
355 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
356 ((unsigned long *) mask)[i];
359 static int cm_compare_data(struct ib_cm_compare_data *src_data,
360 struct ib_cm_compare_data *dst_data)
362 u8 src[IB_CM_COMPARE_SIZE];
363 u8 dst[IB_CM_COMPARE_SIZE];
365 if (!src_data || !dst_data)
368 cm_mask_copy(src, src_data->data, dst_data->mask);
369 cm_mask_copy(dst, dst_data->data, src_data->mask);
370 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
373 static int cm_compare_private_data(u8 *private_data,
374 struct ib_cm_compare_data *dst_data)
376 u8 src[IB_CM_COMPARE_SIZE];
381 cm_mask_copy(src, private_data, dst_data->mask);
382 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
385 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
387 struct rb_node **link = &cm.listen_service_table.rb_node;
388 struct rb_node *parent = NULL;
389 struct cm_id_private *cur_cm_id_priv;
390 __be64 service_id = cm_id_priv->id.service_id;
391 __be64 service_mask = cm_id_priv->id.service_mask;
396 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
398 data_cmp = cm_compare_data(cm_id_priv->compare_data,
399 cur_cm_id_priv->compare_data);
400 if ((cur_cm_id_priv->id.service_mask & service_id) ==
401 (service_mask & cur_cm_id_priv->id.service_id) &&
402 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
404 return cur_cm_id_priv;
406 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
407 link = &(*link)->rb_left;
408 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
409 link = &(*link)->rb_right;
410 else if (service_id < cur_cm_id_priv->id.service_id)
411 link = &(*link)->rb_left;
412 else if (service_id > cur_cm_id_priv->id.service_id)
413 link = &(*link)->rb_right;
414 else if (data_cmp < 0)
415 link = &(*link)->rb_left;
417 link = &(*link)->rb_right;
419 rb_link_node(&cm_id_priv->service_node, parent, link);
420 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
424 static struct cm_id_private * cm_find_listen(struct ib_device *device,
428 struct rb_node *node = cm.listen_service_table.rb_node;
429 struct cm_id_private *cm_id_priv;
433 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
434 data_cmp = cm_compare_private_data(private_data,
435 cm_id_priv->compare_data);
436 if ((cm_id_priv->id.service_mask & service_id) ==
437 cm_id_priv->id.service_id &&
438 (cm_id_priv->id.device == device) && !data_cmp)
441 if (device < cm_id_priv->id.device)
442 node = node->rb_left;
443 else if (device > cm_id_priv->id.device)
444 node = node->rb_right;
445 else if (service_id < cm_id_priv->id.service_id)
446 node = node->rb_left;
447 else if (service_id > cm_id_priv->id.service_id)
448 node = node->rb_right;
449 else if (data_cmp < 0)
450 node = node->rb_left;
452 node = node->rb_right;
457 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
460 struct rb_node **link = &cm.remote_id_table.rb_node;
461 struct rb_node *parent = NULL;
462 struct cm_timewait_info *cur_timewait_info;
463 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
464 __be32 remote_id = timewait_info->work.remote_id;
468 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
470 if (remote_id < cur_timewait_info->work.remote_id)
471 link = &(*link)->rb_left;
472 else if (remote_id > cur_timewait_info->work.remote_id)
473 link = &(*link)->rb_right;
474 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
475 link = &(*link)->rb_left;
476 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
477 link = &(*link)->rb_right;
479 return cur_timewait_info;
481 timewait_info->inserted_remote_id = 1;
482 rb_link_node(&timewait_info->remote_id_node, parent, link);
483 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
487 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
490 struct rb_node *node = cm.remote_id_table.rb_node;
491 struct cm_timewait_info *timewait_info;
494 timewait_info = rb_entry(node, struct cm_timewait_info,
496 if (remote_id < timewait_info->work.remote_id)
497 node = node->rb_left;
498 else if (remote_id > timewait_info->work.remote_id)
499 node = node->rb_right;
500 else if (remote_ca_guid < timewait_info->remote_ca_guid)
501 node = node->rb_left;
502 else if (remote_ca_guid > timewait_info->remote_ca_guid)
503 node = node->rb_right;
505 return timewait_info;
510 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
513 struct rb_node **link = &cm.remote_qp_table.rb_node;
514 struct rb_node *parent = NULL;
515 struct cm_timewait_info *cur_timewait_info;
516 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
517 __be32 remote_qpn = timewait_info->remote_qpn;
521 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
523 if (remote_qpn < cur_timewait_info->remote_qpn)
524 link = &(*link)->rb_left;
525 else if (remote_qpn > cur_timewait_info->remote_qpn)
526 link = &(*link)->rb_right;
527 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
528 link = &(*link)->rb_left;
529 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
530 link = &(*link)->rb_right;
532 return cur_timewait_info;
534 timewait_info->inserted_remote_qp = 1;
535 rb_link_node(&timewait_info->remote_qp_node, parent, link);
536 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
540 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
543 struct rb_node **link = &cm.remote_sidr_table.rb_node;
544 struct rb_node *parent = NULL;
545 struct cm_id_private *cur_cm_id_priv;
546 union ib_gid *port_gid = &cm_id_priv->av.dgid;
547 __be32 remote_id = cm_id_priv->id.remote_id;
551 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
553 if (remote_id < cur_cm_id_priv->id.remote_id)
554 link = &(*link)->rb_left;
555 else if (remote_id > cur_cm_id_priv->id.remote_id)
556 link = &(*link)->rb_right;
559 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
562 link = &(*link)->rb_left;
564 link = &(*link)->rb_right;
566 return cur_cm_id_priv;
569 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
570 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
574 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
575 enum ib_cm_sidr_status status)
577 struct ib_cm_sidr_rep_param param;
579 memset(¶m, 0, sizeof param);
580 param.status = status;
581 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
584 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
585 ib_cm_handler cm_handler,
588 struct cm_id_private *cm_id_priv;
591 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
593 return ERR_PTR(-ENOMEM);
595 cm_id_priv->id.state = IB_CM_IDLE;
596 cm_id_priv->id.device = device;
597 cm_id_priv->id.cm_handler = cm_handler;
598 cm_id_priv->id.context = context;
599 cm_id_priv->id.remote_cm_qpn = 1;
600 ret = cm_alloc_id(cm_id_priv);
604 spin_lock_init(&cm_id_priv->lock);
605 init_completion(&cm_id_priv->comp);
606 INIT_LIST_HEAD(&cm_id_priv->work_list);
607 atomic_set(&cm_id_priv->work_count, -1);
608 atomic_set(&cm_id_priv->refcount, 1);
609 return &cm_id_priv->id;
613 return ERR_PTR(-ENOMEM);
615 EXPORT_SYMBOL(ib_create_cm_id);
617 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
619 struct cm_work *work;
621 if (list_empty(&cm_id_priv->work_list))
624 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
625 list_del(&work->list);
629 static void cm_free_work(struct cm_work *work)
631 if (work->mad_recv_wc)
632 ib_free_recv_mad(work->mad_recv_wc);
636 static inline int cm_convert_to_ms(int iba_time)
638 /* approximate conversion to ms from 4.096us x 2^iba_time */
639 return 1 << max(iba_time - 8, 0);
642 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
646 if (!timewait_info->inserted_remote_id &&
647 !timewait_info->inserted_remote_qp)
650 spin_lock_irqsave(&cm.lock, flags);
651 if (timewait_info->inserted_remote_id) {
652 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
653 timewait_info->inserted_remote_id = 0;
656 if (timewait_info->inserted_remote_qp) {
657 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
658 timewait_info->inserted_remote_qp = 0;
660 spin_unlock_irqrestore(&cm.lock, flags);
663 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
665 struct cm_timewait_info *timewait_info;
667 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
669 return ERR_PTR(-ENOMEM);
671 timewait_info->work.local_id = local_id;
672 INIT_WORK(&timewait_info->work.work, cm_work_handler,
673 &timewait_info->work);
674 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
675 return timewait_info;
678 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
683 * The cm_id could be destroyed by the user before we exit timewait.
684 * To protect against this, we search for the cm_id after exiting
685 * timewait before notifying the user that we've exited timewait.
687 cm_id_priv->id.state = IB_CM_TIMEWAIT;
688 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
689 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
690 msecs_to_jiffies(wait_time));
691 cm_id_priv->timewait_info = NULL;
694 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
696 cm_id_priv->id.state = IB_CM_IDLE;
697 if (cm_id_priv->timewait_info) {
698 cm_cleanup_timewait(cm_id_priv->timewait_info);
699 kfree(cm_id_priv->timewait_info);
700 cm_id_priv->timewait_info = NULL;
704 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
706 struct cm_id_private *cm_id_priv;
707 struct cm_work *work;
710 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
712 spin_lock_irqsave(&cm_id_priv->lock, flags);
713 switch (cm_id->state) {
715 cm_id->state = IB_CM_IDLE;
716 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
717 spin_lock_irqsave(&cm.lock, flags);
718 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
719 spin_unlock_irqrestore(&cm.lock, flags);
721 case IB_CM_SIDR_REQ_SENT:
722 cm_id->state = IB_CM_IDLE;
723 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
724 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
726 case IB_CM_SIDR_REQ_RCVD:
727 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
728 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
731 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
732 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
733 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
734 &cm_id_priv->av.port->cm_dev->ca_guid,
735 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
738 case IB_CM_MRA_REQ_RCVD:
740 case IB_CM_MRA_REP_RCVD:
741 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
744 case IB_CM_MRA_REQ_SENT:
746 case IB_CM_MRA_REP_SENT:
747 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
748 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
751 case IB_CM_ESTABLISHED:
752 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
753 ib_send_cm_dreq(cm_id, NULL, 0);
755 case IB_CM_DREQ_SENT:
756 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
757 cm_enter_timewait(cm_id_priv);
758 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
760 case IB_CM_DREQ_RCVD:
761 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
762 ib_send_cm_drep(cm_id, NULL, 0);
765 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
769 cm_free_id(cm_id->local_id);
770 cm_deref_id(cm_id_priv);
771 wait_for_completion(&cm_id_priv->comp);
772 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
774 kfree(cm_id_priv->compare_data);
775 kfree(cm_id_priv->private_data);
778 EXPORT_SYMBOL(ib_destroy_cm_id);
780 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
781 struct ib_cm_compare_data *compare_data)
783 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
787 service_mask = service_mask ? service_mask :
788 __constant_cpu_to_be64(~0ULL);
789 service_id &= service_mask;
790 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
791 (service_id != IB_CM_ASSIGN_SERVICE_ID))
794 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
795 if (cm_id->state != IB_CM_IDLE)
799 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
801 if (!cm_id_priv->compare_data)
803 cm_mask_copy(cm_id_priv->compare_data->data,
804 compare_data->data, compare_data->mask);
805 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
809 cm_id->state = IB_CM_LISTEN;
811 spin_lock_irqsave(&cm.lock, flags);
812 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
813 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
814 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
816 cm_id->service_id = service_id;
817 cm_id->service_mask = service_mask;
819 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
820 spin_unlock_irqrestore(&cm.lock, flags);
822 if (cur_cm_id_priv) {
823 cm_id->state = IB_CM_IDLE;
824 kfree(cm_id_priv->compare_data);
825 cm_id_priv->compare_data = NULL;
830 EXPORT_SYMBOL(ib_cm_listen);
832 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
833 enum cm_msg_sequence msg_seq)
837 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
838 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
840 return cpu_to_be64(hi_tid | low_tid);
843 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
844 __be16 attr_id, __be64 tid)
846 hdr->base_version = IB_MGMT_BASE_VERSION;
847 hdr->mgmt_class = IB_MGMT_CLASS_CM;
848 hdr->class_version = IB_CM_CLASS_VERSION;
849 hdr->method = IB_MGMT_METHOD_SEND;
850 hdr->attr_id = attr_id;
854 static void cm_format_req(struct cm_req_msg *req_msg,
855 struct cm_id_private *cm_id_priv,
856 struct ib_cm_req_param *param)
858 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
859 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
861 req_msg->local_comm_id = cm_id_priv->id.local_id;
862 req_msg->service_id = param->service_id;
863 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
864 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
865 cm_req_set_resp_res(req_msg, param->responder_resources);
866 cm_req_set_init_depth(req_msg, param->initiator_depth);
867 cm_req_set_remote_resp_timeout(req_msg,
868 param->remote_cm_response_timeout);
869 cm_req_set_qp_type(req_msg, param->qp_type);
870 cm_req_set_flow_ctrl(req_msg, param->flow_control);
871 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
872 cm_req_set_local_resp_timeout(req_msg,
873 param->local_cm_response_timeout);
874 cm_req_set_retry_count(req_msg, param->retry_count);
875 req_msg->pkey = param->primary_path->pkey;
876 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
877 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
878 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
879 cm_req_set_srq(req_msg, param->srq);
881 req_msg->primary_local_lid = param->primary_path->slid;
882 req_msg->primary_remote_lid = param->primary_path->dlid;
883 req_msg->primary_local_gid = param->primary_path->sgid;
884 req_msg->primary_remote_gid = param->primary_path->dgid;
885 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
886 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
887 req_msg->primary_traffic_class = param->primary_path->traffic_class;
888 req_msg->primary_hop_limit = param->primary_path->hop_limit;
889 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
890 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
891 cm_req_set_primary_local_ack_timeout(req_msg,
892 min(31, param->primary_path->packet_life_time + 1));
894 if (param->alternate_path) {
895 req_msg->alt_local_lid = param->alternate_path->slid;
896 req_msg->alt_remote_lid = param->alternate_path->dlid;
897 req_msg->alt_local_gid = param->alternate_path->sgid;
898 req_msg->alt_remote_gid = param->alternate_path->dgid;
899 cm_req_set_alt_flow_label(req_msg,
900 param->alternate_path->flow_label);
901 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
902 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
903 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
904 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
905 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
906 cm_req_set_alt_local_ack_timeout(req_msg,
907 min(31, param->alternate_path->packet_life_time + 1));
910 if (param->private_data && param->private_data_len)
911 memcpy(req_msg->private_data, param->private_data,
912 param->private_data_len);
915 static int cm_validate_req_param(struct ib_cm_req_param *param)
917 /* peer-to-peer not supported */
918 if (param->peer_to_peer)
921 if (!param->primary_path)
924 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
927 if (param->private_data &&
928 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
931 if (param->alternate_path &&
932 (param->alternate_path->pkey != param->primary_path->pkey ||
933 param->alternate_path->mtu != param->primary_path->mtu))
939 int ib_send_cm_req(struct ib_cm_id *cm_id,
940 struct ib_cm_req_param *param)
942 struct cm_id_private *cm_id_priv;
943 struct cm_req_msg *req_msg;
947 ret = cm_validate_req_param(param);
951 /* Verify that we're not in timewait. */
952 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
953 spin_lock_irqsave(&cm_id_priv->lock, flags);
954 if (cm_id->state != IB_CM_IDLE) {
955 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
959 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
961 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
963 if (IS_ERR(cm_id_priv->timewait_info))
966 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
969 if (param->alternate_path) {
970 ret = cm_init_av_by_path(param->alternate_path,
971 &cm_id_priv->alt_av);
975 cm_id->service_id = param->service_id;
976 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
977 cm_id_priv->timeout_ms = cm_convert_to_ms(
978 param->primary_path->packet_life_time) * 2 +
980 param->remote_cm_response_timeout);
981 cm_id_priv->max_cm_retries = param->max_cm_retries;
982 cm_id_priv->initiator_depth = param->initiator_depth;
983 cm_id_priv->responder_resources = param->responder_resources;
984 cm_id_priv->retry_count = param->retry_count;
985 cm_id_priv->path_mtu = param->primary_path->mtu;
986 cm_id_priv->qp_type = param->qp_type;
988 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
992 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
993 cm_format_req(req_msg, cm_id_priv, param);
994 cm_id_priv->tid = req_msg->hdr.tid;
995 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
996 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
998 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
999 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1000 cm_id_priv->local_ack_timeout =
1001 cm_req_get_primary_local_ack_timeout(req_msg);
1003 spin_lock_irqsave(&cm_id_priv->lock, flags);
1004 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1006 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1009 BUG_ON(cm_id->state != IB_CM_IDLE);
1010 cm_id->state = IB_CM_REQ_SENT;
1011 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1014 error2: cm_free_msg(cm_id_priv->msg);
1015 error1: kfree(cm_id_priv->timewait_info);
1018 EXPORT_SYMBOL(ib_send_cm_req);
1020 static int cm_issue_rej(struct cm_port *port,
1021 struct ib_mad_recv_wc *mad_recv_wc,
1022 enum ib_cm_rej_reason reason,
1023 enum cm_msg_response msg_rejected,
1024 void *ari, u8 ari_length)
1026 struct ib_mad_send_buf *msg = NULL;
1027 struct cm_rej_msg *rej_msg, *rcv_msg;
1030 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1034 /* We just need common CM header information. Cast to any message. */
1035 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1036 rej_msg = (struct cm_rej_msg *) msg->mad;
1038 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1039 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1040 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1041 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1042 rej_msg->reason = cpu_to_be16(reason);
1044 if (ari && ari_length) {
1045 cm_rej_set_reject_info_len(rej_msg, ari_length);
1046 memcpy(rej_msg->ari, ari, ari_length);
1049 ret = ib_post_send_mad(msg, NULL);
1056 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1057 __be32 local_qpn, __be32 remote_qpn)
1059 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1060 ((local_ca_guid == remote_ca_guid) &&
1061 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1064 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1065 struct ib_sa_path_rec *primary_path,
1066 struct ib_sa_path_rec *alt_path)
1068 memset(primary_path, 0, sizeof *primary_path);
1069 primary_path->dgid = req_msg->primary_local_gid;
1070 primary_path->sgid = req_msg->primary_remote_gid;
1071 primary_path->dlid = req_msg->primary_local_lid;
1072 primary_path->slid = req_msg->primary_remote_lid;
1073 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1074 primary_path->hop_limit = req_msg->primary_hop_limit;
1075 primary_path->traffic_class = req_msg->primary_traffic_class;
1076 primary_path->reversible = 1;
1077 primary_path->pkey = req_msg->pkey;
1078 primary_path->sl = cm_req_get_primary_sl(req_msg);
1079 primary_path->mtu_selector = IB_SA_EQ;
1080 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1081 primary_path->rate_selector = IB_SA_EQ;
1082 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1083 primary_path->packet_life_time_selector = IB_SA_EQ;
1084 primary_path->packet_life_time =
1085 cm_req_get_primary_local_ack_timeout(req_msg);
1086 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1088 if (req_msg->alt_local_lid) {
1089 memset(alt_path, 0, sizeof *alt_path);
1090 alt_path->dgid = req_msg->alt_local_gid;
1091 alt_path->sgid = req_msg->alt_remote_gid;
1092 alt_path->dlid = req_msg->alt_local_lid;
1093 alt_path->slid = req_msg->alt_remote_lid;
1094 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1095 alt_path->hop_limit = req_msg->alt_hop_limit;
1096 alt_path->traffic_class = req_msg->alt_traffic_class;
1097 alt_path->reversible = 1;
1098 alt_path->pkey = req_msg->pkey;
1099 alt_path->sl = cm_req_get_alt_sl(req_msg);
1100 alt_path->mtu_selector = IB_SA_EQ;
1101 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1102 alt_path->rate_selector = IB_SA_EQ;
1103 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1104 alt_path->packet_life_time_selector = IB_SA_EQ;
1105 alt_path->packet_life_time =
1106 cm_req_get_alt_local_ack_timeout(req_msg);
1107 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1111 static void cm_format_req_event(struct cm_work *work,
1112 struct cm_id_private *cm_id_priv,
1113 struct ib_cm_id *listen_id)
1115 struct cm_req_msg *req_msg;
1116 struct ib_cm_req_event_param *param;
1118 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1119 param = &work->cm_event.param.req_rcvd;
1120 param->listen_id = listen_id;
1121 param->port = cm_id_priv->av.port->port_num;
1122 param->primary_path = &work->path[0];
1123 if (req_msg->alt_local_lid)
1124 param->alternate_path = &work->path[1];
1126 param->alternate_path = NULL;
1127 param->remote_ca_guid = req_msg->local_ca_guid;
1128 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1129 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1130 param->qp_type = cm_req_get_qp_type(req_msg);
1131 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1132 param->responder_resources = cm_req_get_init_depth(req_msg);
1133 param->initiator_depth = cm_req_get_resp_res(req_msg);
1134 param->local_cm_response_timeout =
1135 cm_req_get_remote_resp_timeout(req_msg);
1136 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1137 param->remote_cm_response_timeout =
1138 cm_req_get_local_resp_timeout(req_msg);
1139 param->retry_count = cm_req_get_retry_count(req_msg);
1140 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1141 param->srq = cm_req_get_srq(req_msg);
1142 work->cm_event.private_data = &req_msg->private_data;
1145 static void cm_process_work(struct cm_id_private *cm_id_priv,
1146 struct cm_work *work)
1148 unsigned long flags;
1151 /* We will typically only have the current event to report. */
1152 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1155 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1156 spin_lock_irqsave(&cm_id_priv->lock, flags);
1157 work = cm_dequeue_work(cm_id_priv);
1158 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1160 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1164 cm_deref_id(cm_id_priv);
1166 ib_destroy_cm_id(&cm_id_priv->id);
1169 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1170 struct cm_id_private *cm_id_priv,
1171 enum cm_msg_response msg_mraed, u8 service_timeout,
1172 const void *private_data, u8 private_data_len)
1174 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1175 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1176 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1177 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1178 cm_mra_set_service_timeout(mra_msg, service_timeout);
1180 if (private_data && private_data_len)
1181 memcpy(mra_msg->private_data, private_data, private_data_len);
1184 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1185 struct cm_id_private *cm_id_priv,
1186 enum ib_cm_rej_reason reason,
1189 const void *private_data,
1190 u8 private_data_len)
1192 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1193 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1195 switch(cm_id_priv->id.state) {
1196 case IB_CM_REQ_RCVD:
1197 rej_msg->local_comm_id = 0;
1198 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1200 case IB_CM_MRA_REQ_SENT:
1201 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1202 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1204 case IB_CM_REP_RCVD:
1205 case IB_CM_MRA_REP_SENT:
1206 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1207 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1210 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1211 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1215 rej_msg->reason = cpu_to_be16(reason);
1216 if (ari && ari_length) {
1217 cm_rej_set_reject_info_len(rej_msg, ari_length);
1218 memcpy(rej_msg->ari, ari, ari_length);
1221 if (private_data && private_data_len)
1222 memcpy(rej_msg->private_data, private_data, private_data_len);
1225 static void cm_dup_req_handler(struct cm_work *work,
1226 struct cm_id_private *cm_id_priv)
1228 struct ib_mad_send_buf *msg = NULL;
1229 unsigned long flags;
1232 /* Quick state check to discard duplicate REQs. */
1233 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1236 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1240 spin_lock_irqsave(&cm_id_priv->lock, flags);
1241 switch (cm_id_priv->id.state) {
1242 case IB_CM_MRA_REQ_SENT:
1243 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1244 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1245 cm_id_priv->private_data,
1246 cm_id_priv->private_data_len);
1248 case IB_CM_TIMEWAIT:
1249 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1250 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1255 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1257 ret = ib_post_send_mad(msg, NULL);
1262 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1263 free: cm_free_msg(msg);
1266 static struct cm_id_private * cm_match_req(struct cm_work *work,
1267 struct cm_id_private *cm_id_priv)
1269 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1270 struct cm_timewait_info *timewait_info;
1271 struct cm_req_msg *req_msg;
1272 unsigned long flags;
1274 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1276 /* Check for duplicate REQ and stale connections. */
1277 spin_lock_irqsave(&cm.lock, flags);
1278 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1280 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1282 if (timewait_info) {
1283 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1284 timewait_info->work.remote_id);
1285 spin_unlock_irqrestore(&cm.lock, flags);
1286 if (cur_cm_id_priv) {
1287 cm_dup_req_handler(work, cur_cm_id_priv);
1288 cm_deref_id(cur_cm_id_priv);
1290 cm_issue_rej(work->port, work->mad_recv_wc,
1291 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1296 /* Find matching listen request. */
1297 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1298 req_msg->service_id,
1299 req_msg->private_data);
1300 if (!listen_cm_id_priv) {
1301 spin_unlock_irqrestore(&cm.lock, flags);
1302 cm_issue_rej(work->port, work->mad_recv_wc,
1303 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1307 atomic_inc(&listen_cm_id_priv->refcount);
1308 atomic_inc(&cm_id_priv->refcount);
1309 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1310 atomic_inc(&cm_id_priv->work_count);
1311 spin_unlock_irqrestore(&cm.lock, flags);
1312 return listen_cm_id_priv;
1314 error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1318 static int cm_req_handler(struct cm_work *work)
1320 struct ib_cm_id *cm_id;
1321 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1322 struct cm_req_msg *req_msg;
1325 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1327 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1329 return PTR_ERR(cm_id);
1331 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1332 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1333 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1334 work->mad_recv_wc->recv_buf.grh,
1336 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1338 if (IS_ERR(cm_id_priv->timewait_info)) {
1339 ret = PTR_ERR(cm_id_priv->timewait_info);
1342 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1343 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1344 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1346 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1347 if (!listen_cm_id_priv) {
1352 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1353 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1354 cm_id_priv->id.service_id = req_msg->service_id;
1355 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1357 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1358 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1361 if (req_msg->alt_local_lid) {
1362 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1366 cm_id_priv->tid = req_msg->hdr.tid;
1367 cm_id_priv->timeout_ms = cm_convert_to_ms(
1368 cm_req_get_local_resp_timeout(req_msg));
1369 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1370 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1371 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1372 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1373 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1374 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1375 cm_id_priv->local_ack_timeout =
1376 cm_req_get_primary_local_ack_timeout(req_msg);
1377 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1378 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1379 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1381 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1382 cm_process_work(cm_id_priv, work);
1383 cm_deref_id(listen_cm_id_priv);
1386 error3: atomic_dec(&cm_id_priv->refcount);
1387 cm_deref_id(listen_cm_id_priv);
1388 cm_cleanup_timewait(cm_id_priv->timewait_info);
1389 error2: kfree(cm_id_priv->timewait_info);
1390 cm_id_priv->timewait_info = NULL;
1391 error1: ib_destroy_cm_id(&cm_id_priv->id);
1395 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1396 struct cm_id_private *cm_id_priv,
1397 struct ib_cm_rep_param *param)
1399 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1400 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1401 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1402 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1403 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1404 rep_msg->resp_resources = param->responder_resources;
1405 rep_msg->initiator_depth = param->initiator_depth;
1406 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1407 cm_rep_set_failover(rep_msg, param->failover_accepted);
1408 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1409 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1410 cm_rep_set_srq(rep_msg, param->srq);
1411 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1413 if (param->private_data && param->private_data_len)
1414 memcpy(rep_msg->private_data, param->private_data,
1415 param->private_data_len);
1418 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1419 struct ib_cm_rep_param *param)
1421 struct cm_id_private *cm_id_priv;
1422 struct ib_mad_send_buf *msg;
1423 struct cm_rep_msg *rep_msg;
1424 unsigned long flags;
1427 if (param->private_data &&
1428 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1431 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1432 spin_lock_irqsave(&cm_id_priv->lock, flags);
1433 if (cm_id->state != IB_CM_REQ_RCVD &&
1434 cm_id->state != IB_CM_MRA_REQ_SENT) {
1439 ret = cm_alloc_msg(cm_id_priv, &msg);
1443 rep_msg = (struct cm_rep_msg *) msg->mad;
1444 cm_format_rep(rep_msg, cm_id_priv, param);
1445 msg->timeout_ms = cm_id_priv->timeout_ms;
1446 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1448 ret = ib_post_send_mad(msg, NULL);
1450 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1455 cm_id->state = IB_CM_REP_SENT;
1456 cm_id_priv->msg = msg;
1457 cm_id_priv->initiator_depth = param->initiator_depth;
1458 cm_id_priv->responder_resources = param->responder_resources;
1459 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1460 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1462 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1465 EXPORT_SYMBOL(ib_send_cm_rep);
1467 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1468 struct cm_id_private *cm_id_priv,
1469 const void *private_data,
1470 u8 private_data_len)
1472 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1473 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1474 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1476 if (private_data && private_data_len)
1477 memcpy(rtu_msg->private_data, private_data, private_data_len);
1480 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1481 const void *private_data,
1482 u8 private_data_len)
1484 struct cm_id_private *cm_id_priv;
1485 struct ib_mad_send_buf *msg;
1486 unsigned long flags;
1490 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1493 data = cm_copy_private_data(private_data, private_data_len);
1495 return PTR_ERR(data);
1497 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1498 spin_lock_irqsave(&cm_id_priv->lock, flags);
1499 if (cm_id->state != IB_CM_REP_RCVD &&
1500 cm_id->state != IB_CM_MRA_REP_SENT) {
1505 ret = cm_alloc_msg(cm_id_priv, &msg);
1509 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1510 private_data, private_data_len);
1512 ret = ib_post_send_mad(msg, NULL);
1514 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1520 cm_id->state = IB_CM_ESTABLISHED;
1521 cm_set_private_data(cm_id_priv, data, private_data_len);
1522 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1525 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1529 EXPORT_SYMBOL(ib_send_cm_rtu);
1531 static void cm_format_rep_event(struct cm_work *work)
1533 struct cm_rep_msg *rep_msg;
1534 struct ib_cm_rep_event_param *param;
1536 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1537 param = &work->cm_event.param.rep_rcvd;
1538 param->remote_ca_guid = rep_msg->local_ca_guid;
1539 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1540 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1541 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1542 param->responder_resources = rep_msg->initiator_depth;
1543 param->initiator_depth = rep_msg->resp_resources;
1544 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1545 param->failover_accepted = cm_rep_get_failover(rep_msg);
1546 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1547 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1548 param->srq = cm_rep_get_srq(rep_msg);
1549 work->cm_event.private_data = &rep_msg->private_data;
1552 static void cm_dup_rep_handler(struct cm_work *work)
1554 struct cm_id_private *cm_id_priv;
1555 struct cm_rep_msg *rep_msg;
1556 struct ib_mad_send_buf *msg = NULL;
1557 unsigned long flags;
1560 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1561 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1562 rep_msg->local_comm_id);
1566 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1570 spin_lock_irqsave(&cm_id_priv->lock, flags);
1571 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1572 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1573 cm_id_priv->private_data,
1574 cm_id_priv->private_data_len);
1575 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1576 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1577 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1578 cm_id_priv->private_data,
1579 cm_id_priv->private_data_len);
1582 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1584 ret = ib_post_send_mad(msg, NULL);
1589 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1590 free: cm_free_msg(msg);
1591 deref: cm_deref_id(cm_id_priv);
1594 static int cm_rep_handler(struct cm_work *work)
1596 struct cm_id_private *cm_id_priv;
1597 struct cm_rep_msg *rep_msg;
1598 unsigned long flags;
1601 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1602 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1604 cm_dup_rep_handler(work);
1608 cm_format_rep_event(work);
1610 spin_lock_irqsave(&cm_id_priv->lock, flags);
1611 switch (cm_id_priv->id.state) {
1612 case IB_CM_REQ_SENT:
1613 case IB_CM_MRA_REQ_RCVD:
1616 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1621 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1622 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1623 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1625 spin_lock(&cm.lock);
1626 /* Check for duplicate REP. */
1627 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1628 spin_unlock(&cm.lock);
1629 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1633 /* Check for a stale connection. */
1634 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1635 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1636 &cm.remote_id_table);
1637 cm_id_priv->timewait_info->inserted_remote_id = 0;
1638 spin_unlock(&cm.lock);
1639 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1640 cm_issue_rej(work->port, work->mad_recv_wc,
1641 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1646 spin_unlock(&cm.lock);
1648 cm_id_priv->id.state = IB_CM_REP_RCVD;
1649 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1650 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1651 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1652 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1653 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1654 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1656 /* todo: handle peer_to_peer */
1658 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1659 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1661 list_add_tail(&work->list, &cm_id_priv->work_list);
1662 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1665 cm_process_work(cm_id_priv, work);
1667 cm_deref_id(cm_id_priv);
1671 cm_deref_id(cm_id_priv);
1675 static int cm_establish_handler(struct cm_work *work)
1677 struct cm_id_private *cm_id_priv;
1678 unsigned long flags;
1681 /* See comment in ib_cm_establish about lookup. */
1682 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1686 spin_lock_irqsave(&cm_id_priv->lock, flags);
1687 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1688 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1692 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1693 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1695 list_add_tail(&work->list, &cm_id_priv->work_list);
1696 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1699 cm_process_work(cm_id_priv, work);
1701 cm_deref_id(cm_id_priv);
1704 cm_deref_id(cm_id_priv);
1708 static int cm_rtu_handler(struct cm_work *work)
1710 struct cm_id_private *cm_id_priv;
1711 struct cm_rtu_msg *rtu_msg;
1712 unsigned long flags;
1715 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1716 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1717 rtu_msg->local_comm_id);
1721 work->cm_event.private_data = &rtu_msg->private_data;
1723 spin_lock_irqsave(&cm_id_priv->lock, flags);
1724 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1725 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1726 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1729 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1731 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1732 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1734 list_add_tail(&work->list, &cm_id_priv->work_list);
1735 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1738 cm_process_work(cm_id_priv, work);
1740 cm_deref_id(cm_id_priv);
1743 cm_deref_id(cm_id_priv);
1747 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1748 struct cm_id_private *cm_id_priv,
1749 const void *private_data,
1750 u8 private_data_len)
1752 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1753 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1754 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1755 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1756 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1758 if (private_data && private_data_len)
1759 memcpy(dreq_msg->private_data, private_data, private_data_len);
1762 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1763 const void *private_data,
1764 u8 private_data_len)
1766 struct cm_id_private *cm_id_priv;
1767 struct ib_mad_send_buf *msg;
1768 unsigned long flags;
1771 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1774 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1775 spin_lock_irqsave(&cm_id_priv->lock, flags);
1776 if (cm_id->state != IB_CM_ESTABLISHED) {
1781 ret = cm_alloc_msg(cm_id_priv, &msg);
1783 cm_enter_timewait(cm_id_priv);
1787 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1788 private_data, private_data_len);
1789 msg->timeout_ms = cm_id_priv->timeout_ms;
1790 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1792 ret = ib_post_send_mad(msg, NULL);
1794 cm_enter_timewait(cm_id_priv);
1795 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1800 cm_id->state = IB_CM_DREQ_SENT;
1801 cm_id_priv->msg = msg;
1802 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1805 EXPORT_SYMBOL(ib_send_cm_dreq);
1807 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1808 struct cm_id_private *cm_id_priv,
1809 const void *private_data,
1810 u8 private_data_len)
1812 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1813 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1814 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1816 if (private_data && private_data_len)
1817 memcpy(drep_msg->private_data, private_data, private_data_len);
1820 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1821 const void *private_data,
1822 u8 private_data_len)
1824 struct cm_id_private *cm_id_priv;
1825 struct ib_mad_send_buf *msg;
1826 unsigned long flags;
1830 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1833 data = cm_copy_private_data(private_data, private_data_len);
1835 return PTR_ERR(data);
1837 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1838 spin_lock_irqsave(&cm_id_priv->lock, flags);
1839 if (cm_id->state != IB_CM_DREQ_RCVD) {
1840 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1845 cm_set_private_data(cm_id_priv, data, private_data_len);
1846 cm_enter_timewait(cm_id_priv);
1848 ret = cm_alloc_msg(cm_id_priv, &msg);
1852 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1853 private_data, private_data_len);
1855 ret = ib_post_send_mad(msg, NULL);
1857 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1862 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1865 EXPORT_SYMBOL(ib_send_cm_drep);
1867 static int cm_dreq_handler(struct cm_work *work)
1869 struct cm_id_private *cm_id_priv;
1870 struct cm_dreq_msg *dreq_msg;
1871 struct ib_mad_send_buf *msg = NULL;
1872 unsigned long flags;
1875 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1876 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1877 dreq_msg->local_comm_id);
1881 work->cm_event.private_data = &dreq_msg->private_data;
1883 spin_lock_irqsave(&cm_id_priv->lock, flags);
1884 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1887 switch (cm_id_priv->id.state) {
1888 case IB_CM_REP_SENT:
1889 case IB_CM_DREQ_SENT:
1890 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1892 case IB_CM_ESTABLISHED:
1893 case IB_CM_MRA_REP_RCVD:
1895 case IB_CM_TIMEWAIT:
1896 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1899 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1900 cm_id_priv->private_data,
1901 cm_id_priv->private_data_len);
1902 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1904 if (ib_post_send_mad(msg, NULL))
1910 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1911 cm_id_priv->tid = dreq_msg->hdr.tid;
1912 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1914 list_add_tail(&work->list, &cm_id_priv->work_list);
1915 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1918 cm_process_work(cm_id_priv, work);
1920 cm_deref_id(cm_id_priv);
1923 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1924 deref: cm_deref_id(cm_id_priv);
1928 static int cm_drep_handler(struct cm_work *work)
1930 struct cm_id_private *cm_id_priv;
1931 struct cm_drep_msg *drep_msg;
1932 unsigned long flags;
1935 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1936 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1937 drep_msg->local_comm_id);
1941 work->cm_event.private_data = &drep_msg->private_data;
1943 spin_lock_irqsave(&cm_id_priv->lock, flags);
1944 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1945 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1946 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1949 cm_enter_timewait(cm_id_priv);
1951 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1952 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1954 list_add_tail(&work->list, &cm_id_priv->work_list);
1955 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1958 cm_process_work(cm_id_priv, work);
1960 cm_deref_id(cm_id_priv);
1963 cm_deref_id(cm_id_priv);
1967 int ib_send_cm_rej(struct ib_cm_id *cm_id,
1968 enum ib_cm_rej_reason reason,
1971 const void *private_data,
1972 u8 private_data_len)
1974 struct cm_id_private *cm_id_priv;
1975 struct ib_mad_send_buf *msg;
1976 unsigned long flags;
1979 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
1980 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
1983 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1985 spin_lock_irqsave(&cm_id_priv->lock, flags);
1986 switch (cm_id->state) {
1987 case IB_CM_REQ_SENT:
1988 case IB_CM_MRA_REQ_RCVD:
1989 case IB_CM_REQ_RCVD:
1990 case IB_CM_MRA_REQ_SENT:
1991 case IB_CM_REP_RCVD:
1992 case IB_CM_MRA_REP_SENT:
1993 ret = cm_alloc_msg(cm_id_priv, &msg);
1995 cm_format_rej((struct cm_rej_msg *) msg->mad,
1996 cm_id_priv, reason, ari, ari_length,
1997 private_data, private_data_len);
1999 cm_reset_to_idle(cm_id_priv);
2001 case IB_CM_REP_SENT:
2002 case IB_CM_MRA_REP_RCVD:
2003 ret = cm_alloc_msg(cm_id_priv, &msg);
2005 cm_format_rej((struct cm_rej_msg *) msg->mad,
2006 cm_id_priv, reason, ari, ari_length,
2007 private_data, private_data_len);
2009 cm_enter_timewait(cm_id_priv);
2019 ret = ib_post_send_mad(msg, NULL);
2023 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2026 EXPORT_SYMBOL(ib_send_cm_rej);
2028 static void cm_format_rej_event(struct cm_work *work)
2030 struct cm_rej_msg *rej_msg;
2031 struct ib_cm_rej_event_param *param;
2033 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2034 param = &work->cm_event.param.rej_rcvd;
2035 param->ari = rej_msg->ari;
2036 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2037 param->reason = __be16_to_cpu(rej_msg->reason);
2038 work->cm_event.private_data = &rej_msg->private_data;
2041 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2043 struct cm_timewait_info *timewait_info;
2044 struct cm_id_private *cm_id_priv;
2045 unsigned long flags;
2048 remote_id = rej_msg->local_comm_id;
2050 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2051 spin_lock_irqsave(&cm.lock, flags);
2052 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2054 if (!timewait_info) {
2055 spin_unlock_irqrestore(&cm.lock, flags);
2058 cm_id_priv = idr_find(&cm.local_id_table,
2059 (__force int) timewait_info->work.local_id);
2061 if (cm_id_priv->id.remote_id == remote_id)
2062 atomic_inc(&cm_id_priv->refcount);
2066 spin_unlock_irqrestore(&cm.lock, flags);
2067 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2068 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2070 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2075 static int cm_rej_handler(struct cm_work *work)
2077 struct cm_id_private *cm_id_priv;
2078 struct cm_rej_msg *rej_msg;
2079 unsigned long flags;
2082 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2083 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2087 cm_format_rej_event(work);
2089 spin_lock_irqsave(&cm_id_priv->lock, flags);
2090 switch (cm_id_priv->id.state) {
2091 case IB_CM_REQ_SENT:
2092 case IB_CM_MRA_REQ_RCVD:
2093 case IB_CM_REP_SENT:
2094 case IB_CM_MRA_REP_RCVD:
2095 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2097 case IB_CM_REQ_RCVD:
2098 case IB_CM_MRA_REQ_SENT:
2099 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2100 cm_enter_timewait(cm_id_priv);
2102 cm_reset_to_idle(cm_id_priv);
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_REP_RCVD:
2108 case IB_CM_MRA_REP_SENT:
2109 case IB_CM_ESTABLISHED:
2110 cm_enter_timewait(cm_id_priv);
2113 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2118 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2120 list_add_tail(&work->list, &cm_id_priv->work_list);
2121 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2124 cm_process_work(cm_id_priv, work);
2126 cm_deref_id(cm_id_priv);
2129 cm_deref_id(cm_id_priv);
2133 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2135 const void *private_data,
2136 u8 private_data_len)
2138 struct cm_id_private *cm_id_priv;
2139 struct ib_mad_send_buf *msg;
2141 unsigned long flags;
2144 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2147 data = cm_copy_private_data(private_data, private_data_len);
2149 return PTR_ERR(data);
2151 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2153 spin_lock_irqsave(&cm_id_priv->lock, flags);
2154 switch(cm_id_priv->id.state) {
2155 case IB_CM_REQ_RCVD:
2156 ret = cm_alloc_msg(cm_id_priv, &msg);
2160 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2161 CM_MSG_RESPONSE_REQ, service_timeout,
2162 private_data, private_data_len);
2163 ret = ib_post_send_mad(msg, NULL);
2166 cm_id->state = IB_CM_MRA_REQ_SENT;
2168 case IB_CM_REP_RCVD:
2169 ret = cm_alloc_msg(cm_id_priv, &msg);
2173 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2174 CM_MSG_RESPONSE_REP, service_timeout,
2175 private_data, private_data_len);
2176 ret = ib_post_send_mad(msg, NULL);
2179 cm_id->state = IB_CM_MRA_REP_SENT;
2181 case IB_CM_ESTABLISHED:
2182 ret = cm_alloc_msg(cm_id_priv, &msg);
2186 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2187 CM_MSG_RESPONSE_OTHER, service_timeout,
2188 private_data, private_data_len);
2189 ret = ib_post_send_mad(msg, NULL);
2192 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2198 cm_id_priv->service_timeout = service_timeout;
2199 cm_set_private_data(cm_id_priv, data, private_data_len);
2200 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2203 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2207 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2212 EXPORT_SYMBOL(ib_send_cm_mra);
2214 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2216 switch (cm_mra_get_msg_mraed(mra_msg)) {
2217 case CM_MSG_RESPONSE_REQ:
2218 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2219 case CM_MSG_RESPONSE_REP:
2220 case CM_MSG_RESPONSE_OTHER:
2221 return cm_acquire_id(mra_msg->remote_comm_id,
2222 mra_msg->local_comm_id);
2228 static int cm_mra_handler(struct cm_work *work)
2230 struct cm_id_private *cm_id_priv;
2231 struct cm_mra_msg *mra_msg;
2232 unsigned long flags;
2235 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2236 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2240 work->cm_event.private_data = &mra_msg->private_data;
2241 work->cm_event.param.mra_rcvd.service_timeout =
2242 cm_mra_get_service_timeout(mra_msg);
2243 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2244 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2246 spin_lock_irqsave(&cm_id_priv->lock, flags);
2247 switch (cm_id_priv->id.state) {
2248 case IB_CM_REQ_SENT:
2249 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2250 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2251 cm_id_priv->msg, timeout))
2253 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2255 case IB_CM_REP_SENT:
2256 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2257 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2258 cm_id_priv->msg, timeout))
2260 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2262 case IB_CM_ESTABLISHED:
2263 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2264 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2265 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2266 cm_id_priv->msg, timeout))
2268 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2274 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2275 cm_id_priv->id.state;
2276 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2278 list_add_tail(&work->list, &cm_id_priv->work_list);
2279 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2282 cm_process_work(cm_id_priv, work);
2284 cm_deref_id(cm_id_priv);
2287 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2288 cm_deref_id(cm_id_priv);
2292 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2293 struct cm_id_private *cm_id_priv,
2294 struct ib_sa_path_rec *alternate_path,
2295 const void *private_data,
2296 u8 private_data_len)
2298 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2299 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2300 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2301 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2302 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2303 /* todo: need remote CM response timeout */
2304 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2305 lap_msg->alt_local_lid = alternate_path->slid;
2306 lap_msg->alt_remote_lid = alternate_path->dlid;
2307 lap_msg->alt_local_gid = alternate_path->sgid;
2308 lap_msg->alt_remote_gid = alternate_path->dgid;
2309 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2310 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2311 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2312 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2313 cm_lap_set_sl(lap_msg, alternate_path->sl);
2314 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2315 cm_lap_set_local_ack_timeout(lap_msg,
2316 min(31, alternate_path->packet_life_time + 1));
2318 if (private_data && private_data_len)
2319 memcpy(lap_msg->private_data, private_data, private_data_len);
2322 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2323 struct ib_sa_path_rec *alternate_path,
2324 const void *private_data,
2325 u8 private_data_len)
2327 struct cm_id_private *cm_id_priv;
2328 struct ib_mad_send_buf *msg;
2329 unsigned long flags;
2332 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2335 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2336 spin_lock_irqsave(&cm_id_priv->lock, flags);
2337 if (cm_id->state != IB_CM_ESTABLISHED ||
2338 cm_id->lap_state != IB_CM_LAP_IDLE) {
2343 ret = cm_alloc_msg(cm_id_priv, &msg);
2347 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2348 alternate_path, private_data, private_data_len);
2349 msg->timeout_ms = cm_id_priv->timeout_ms;
2350 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2352 ret = ib_post_send_mad(msg, NULL);
2354 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2359 cm_id->lap_state = IB_CM_LAP_SENT;
2360 cm_id_priv->msg = msg;
2362 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2365 EXPORT_SYMBOL(ib_send_cm_lap);
2367 static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2368 struct cm_lap_msg *lap_msg)
2370 memset(path, 0, sizeof *path);
2371 path->dgid = lap_msg->alt_local_gid;
2372 path->sgid = lap_msg->alt_remote_gid;
2373 path->dlid = lap_msg->alt_local_lid;
2374 path->slid = lap_msg->alt_remote_lid;
2375 path->flow_label = cm_lap_get_flow_label(lap_msg);
2376 path->hop_limit = lap_msg->alt_hop_limit;
2377 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2378 path->reversible = 1;
2379 /* pkey is same as in REQ */
2380 path->sl = cm_lap_get_sl(lap_msg);
2381 path->mtu_selector = IB_SA_EQ;
2382 /* mtu is same as in REQ */
2383 path->rate_selector = IB_SA_EQ;
2384 path->rate = cm_lap_get_packet_rate(lap_msg);
2385 path->packet_life_time_selector = IB_SA_EQ;
2386 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2387 path->packet_life_time -= (path->packet_life_time > 0);
2390 static int cm_lap_handler(struct cm_work *work)
2392 struct cm_id_private *cm_id_priv;
2393 struct cm_lap_msg *lap_msg;
2394 struct ib_cm_lap_event_param *param;
2395 struct ib_mad_send_buf *msg = NULL;
2396 unsigned long flags;
2399 /* todo: verify LAP request and send reject APR if invalid. */
2400 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2401 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2402 lap_msg->local_comm_id);
2406 param = &work->cm_event.param.lap_rcvd;
2407 param->alternate_path = &work->path[0];
2408 cm_format_path_from_lap(param->alternate_path, lap_msg);
2409 work->cm_event.private_data = &lap_msg->private_data;
2411 spin_lock_irqsave(&cm_id_priv->lock, flags);
2412 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2415 switch (cm_id_priv->id.lap_state) {
2416 case IB_CM_LAP_IDLE:
2418 case IB_CM_MRA_LAP_SENT:
2419 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2422 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2423 CM_MSG_RESPONSE_OTHER,
2424 cm_id_priv->service_timeout,
2425 cm_id_priv->private_data,
2426 cm_id_priv->private_data_len);
2427 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2429 if (ib_post_send_mad(msg, NULL))
2436 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2437 cm_id_priv->tid = lap_msg->hdr.tid;
2438 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2440 list_add_tail(&work->list, &cm_id_priv->work_list);
2441 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2444 cm_process_work(cm_id_priv, work);
2446 cm_deref_id(cm_id_priv);
2449 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2450 deref: cm_deref_id(cm_id_priv);
2454 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2455 struct cm_id_private *cm_id_priv,
2456 enum ib_cm_apr_status status,
2459 const void *private_data,
2460 u8 private_data_len)
2462 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2463 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2464 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2465 apr_msg->ap_status = (u8) status;
2467 if (info && info_length) {
2468 apr_msg->info_length = info_length;
2469 memcpy(apr_msg->info, info, info_length);
2472 if (private_data && private_data_len)
2473 memcpy(apr_msg->private_data, private_data, private_data_len);
2476 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2477 enum ib_cm_apr_status status,
2480 const void *private_data,
2481 u8 private_data_len)
2483 struct cm_id_private *cm_id_priv;
2484 struct ib_mad_send_buf *msg;
2485 unsigned long flags;
2488 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2489 (info && info_length > IB_CM_APR_INFO_LENGTH))
2492 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2493 spin_lock_irqsave(&cm_id_priv->lock, flags);
2494 if (cm_id->state != IB_CM_ESTABLISHED ||
2495 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2496 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2501 ret = cm_alloc_msg(cm_id_priv, &msg);
2505 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2506 info, info_length, private_data, private_data_len);
2507 ret = ib_post_send_mad(msg, NULL);
2509 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2514 cm_id->lap_state = IB_CM_LAP_IDLE;
2515 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2518 EXPORT_SYMBOL(ib_send_cm_apr);
2520 static int cm_apr_handler(struct cm_work *work)
2522 struct cm_id_private *cm_id_priv;
2523 struct cm_apr_msg *apr_msg;
2524 unsigned long flags;
2527 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2528 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2529 apr_msg->local_comm_id);
2531 return -EINVAL; /* Unmatched reply. */
2533 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2534 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2535 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2536 work->cm_event.private_data = &apr_msg->private_data;
2538 spin_lock_irqsave(&cm_id_priv->lock, flags);
2539 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2540 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2541 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2542 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2545 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2546 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2547 cm_id_priv->msg = NULL;
2549 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2551 list_add_tail(&work->list, &cm_id_priv->work_list);
2552 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2555 cm_process_work(cm_id_priv, work);
2557 cm_deref_id(cm_id_priv);
2560 cm_deref_id(cm_id_priv);
2564 static int cm_timewait_handler(struct cm_work *work)
2566 struct cm_timewait_info *timewait_info;
2567 struct cm_id_private *cm_id_priv;
2568 unsigned long flags;
2571 timewait_info = (struct cm_timewait_info *)work;
2572 cm_cleanup_timewait(timewait_info);
2574 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2575 timewait_info->work.remote_id);
2579 spin_lock_irqsave(&cm_id_priv->lock, flags);
2580 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2581 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2582 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2585 cm_id_priv->id.state = IB_CM_IDLE;
2586 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2588 list_add_tail(&work->list, &cm_id_priv->work_list);
2589 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2592 cm_process_work(cm_id_priv, work);
2594 cm_deref_id(cm_id_priv);
2597 cm_deref_id(cm_id_priv);
2601 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2602 struct cm_id_private *cm_id_priv,
2603 struct ib_cm_sidr_req_param *param)
2605 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2606 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2607 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2608 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2609 sidr_req_msg->service_id = param->service_id;
2611 if (param->private_data && param->private_data_len)
2612 memcpy(sidr_req_msg->private_data, param->private_data,
2613 param->private_data_len);
2616 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2617 struct ib_cm_sidr_req_param *param)
2619 struct cm_id_private *cm_id_priv;
2620 struct ib_mad_send_buf *msg;
2621 unsigned long flags;
2624 if (!param->path || (param->private_data &&
2625 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2628 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2629 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2633 cm_id->service_id = param->service_id;
2634 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2635 cm_id_priv->timeout_ms = param->timeout_ms;
2636 cm_id_priv->max_cm_retries = param->max_cm_retries;
2637 ret = cm_alloc_msg(cm_id_priv, &msg);
2641 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2643 msg->timeout_ms = cm_id_priv->timeout_ms;
2644 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2646 spin_lock_irqsave(&cm_id_priv->lock, flags);
2647 if (cm_id->state == IB_CM_IDLE)
2648 ret = ib_post_send_mad(msg, NULL);
2653 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2657 cm_id->state = IB_CM_SIDR_REQ_SENT;
2658 cm_id_priv->msg = msg;
2659 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2663 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2665 static void cm_format_sidr_req_event(struct cm_work *work,
2666 struct ib_cm_id *listen_id)
2668 struct cm_sidr_req_msg *sidr_req_msg;
2669 struct ib_cm_sidr_req_event_param *param;
2671 sidr_req_msg = (struct cm_sidr_req_msg *)
2672 work->mad_recv_wc->recv_buf.mad;
2673 param = &work->cm_event.param.sidr_req_rcvd;
2674 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2675 param->listen_id = listen_id;
2676 param->port = work->port->port_num;
2677 work->cm_event.private_data = &sidr_req_msg->private_data;
2680 static int cm_sidr_req_handler(struct cm_work *work)
2682 struct ib_cm_id *cm_id;
2683 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2684 struct cm_sidr_req_msg *sidr_req_msg;
2686 unsigned long flags;
2688 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2690 return PTR_ERR(cm_id);
2691 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2693 /* Record SGID/SLID and request ID for lookup. */
2694 sidr_req_msg = (struct cm_sidr_req_msg *)
2695 work->mad_recv_wc->recv_buf.mad;
2696 wc = work->mad_recv_wc->wc;
2697 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2698 cm_id_priv->av.dgid.global.interface_id = 0;
2699 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2700 work->mad_recv_wc->recv_buf.grh,
2702 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2703 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2704 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2705 atomic_inc(&cm_id_priv->work_count);
2707 spin_lock_irqsave(&cm.lock, flags);
2708 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2709 if (cur_cm_id_priv) {
2710 spin_unlock_irqrestore(&cm.lock, flags);
2711 goto out; /* Duplicate message. */
2713 cur_cm_id_priv = cm_find_listen(cm_id->device,
2714 sidr_req_msg->service_id,
2715 sidr_req_msg->private_data);
2716 if (!cur_cm_id_priv) {
2717 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2718 spin_unlock_irqrestore(&cm.lock, flags);
2719 /* todo: reply with no match */
2720 goto out; /* No match. */
2722 atomic_inc(&cur_cm_id_priv->refcount);
2723 spin_unlock_irqrestore(&cm.lock, flags);
2725 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2726 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2727 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2728 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2730 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2731 cm_process_work(cm_id_priv, work);
2732 cm_deref_id(cur_cm_id_priv);
2735 ib_destroy_cm_id(&cm_id_priv->id);
2739 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2740 struct cm_id_private *cm_id_priv,
2741 struct ib_cm_sidr_rep_param *param)
2743 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2745 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2746 sidr_rep_msg->status = param->status;
2747 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2748 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2749 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2751 if (param->info && param->info_length)
2752 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2754 if (param->private_data && param->private_data_len)
2755 memcpy(sidr_rep_msg->private_data, param->private_data,
2756 param->private_data_len);
2759 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2760 struct ib_cm_sidr_rep_param *param)
2762 struct cm_id_private *cm_id_priv;
2763 struct ib_mad_send_buf *msg;
2764 unsigned long flags;
2767 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2768 (param->private_data &&
2769 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2772 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2773 spin_lock_irqsave(&cm_id_priv->lock, flags);
2774 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2779 ret = cm_alloc_msg(cm_id_priv, &msg);
2783 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2785 ret = ib_post_send_mad(msg, NULL);
2787 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2791 cm_id->state = IB_CM_IDLE;
2792 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2794 spin_lock_irqsave(&cm.lock, flags);
2795 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2796 spin_unlock_irqrestore(&cm.lock, flags);
2799 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2802 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2804 static void cm_format_sidr_rep_event(struct cm_work *work)
2806 struct cm_sidr_rep_msg *sidr_rep_msg;
2807 struct ib_cm_sidr_rep_event_param *param;
2809 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2810 work->mad_recv_wc->recv_buf.mad;
2811 param = &work->cm_event.param.sidr_rep_rcvd;
2812 param->status = sidr_rep_msg->status;
2813 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2814 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2815 param->info = &sidr_rep_msg->info;
2816 param->info_len = sidr_rep_msg->info_length;
2817 work->cm_event.private_data = &sidr_rep_msg->private_data;
2820 static int cm_sidr_rep_handler(struct cm_work *work)
2822 struct cm_sidr_rep_msg *sidr_rep_msg;
2823 struct cm_id_private *cm_id_priv;
2824 unsigned long flags;
2826 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2827 work->mad_recv_wc->recv_buf.mad;
2828 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2830 return -EINVAL; /* Unmatched reply. */
2832 spin_lock_irqsave(&cm_id_priv->lock, flags);
2833 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2834 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2837 cm_id_priv->id.state = IB_CM_IDLE;
2838 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2839 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2841 cm_format_sidr_rep_event(work);
2842 cm_process_work(cm_id_priv, work);
2845 cm_deref_id(cm_id_priv);
2849 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2850 enum ib_wc_status wc_status)
2852 struct cm_id_private *cm_id_priv;
2853 struct ib_cm_event cm_event;
2854 enum ib_cm_state state;
2855 unsigned long flags;
2858 memset(&cm_event, 0, sizeof cm_event);
2859 cm_id_priv = msg->context[0];
2861 /* Discard old sends or ones without a response. */
2862 spin_lock_irqsave(&cm_id_priv->lock, flags);
2863 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2864 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2868 case IB_CM_REQ_SENT:
2869 case IB_CM_MRA_REQ_RCVD:
2870 cm_reset_to_idle(cm_id_priv);
2871 cm_event.event = IB_CM_REQ_ERROR;
2873 case IB_CM_REP_SENT:
2874 case IB_CM_MRA_REP_RCVD:
2875 cm_reset_to_idle(cm_id_priv);
2876 cm_event.event = IB_CM_REP_ERROR;
2878 case IB_CM_DREQ_SENT:
2879 cm_enter_timewait(cm_id_priv);
2880 cm_event.event = IB_CM_DREQ_ERROR;
2882 case IB_CM_SIDR_REQ_SENT:
2883 cm_id_priv->id.state = IB_CM_IDLE;
2884 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2889 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2890 cm_event.param.send_status = wc_status;
2892 /* No other events can occur on the cm_id at this point. */
2893 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2896 ib_destroy_cm_id(&cm_id_priv->id);
2899 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2903 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2904 struct ib_mad_send_wc *mad_send_wc)
2906 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2908 switch (mad_send_wc->status) {
2910 case IB_WC_WR_FLUSH_ERR:
2914 if (msg->context[0] && msg->context[1])
2915 cm_process_send_error(msg, mad_send_wc->status);
2922 static void cm_work_handler(void *data)
2924 struct cm_work *work = data;
2927 switch (work->cm_event.event) {
2928 case IB_CM_REQ_RECEIVED:
2929 ret = cm_req_handler(work);
2931 case IB_CM_MRA_RECEIVED:
2932 ret = cm_mra_handler(work);
2934 case IB_CM_REJ_RECEIVED:
2935 ret = cm_rej_handler(work);
2937 case IB_CM_REP_RECEIVED:
2938 ret = cm_rep_handler(work);
2940 case IB_CM_RTU_RECEIVED:
2941 ret = cm_rtu_handler(work);
2943 case IB_CM_USER_ESTABLISHED:
2944 ret = cm_establish_handler(work);
2946 case IB_CM_DREQ_RECEIVED:
2947 ret = cm_dreq_handler(work);
2949 case IB_CM_DREP_RECEIVED:
2950 ret = cm_drep_handler(work);
2952 case IB_CM_SIDR_REQ_RECEIVED:
2953 ret = cm_sidr_req_handler(work);
2955 case IB_CM_SIDR_REP_RECEIVED:
2956 ret = cm_sidr_rep_handler(work);
2958 case IB_CM_LAP_RECEIVED:
2959 ret = cm_lap_handler(work);
2961 case IB_CM_APR_RECEIVED:
2962 ret = cm_apr_handler(work);
2964 case IB_CM_TIMEWAIT_EXIT:
2965 ret = cm_timewait_handler(work);
2975 int ib_cm_establish(struct ib_cm_id *cm_id)
2977 struct cm_id_private *cm_id_priv;
2978 struct cm_work *work;
2979 unsigned long flags;
2982 work = kmalloc(sizeof *work, GFP_ATOMIC);
2986 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2987 spin_lock_irqsave(&cm_id_priv->lock, flags);
2988 switch (cm_id->state)
2990 case IB_CM_REP_SENT:
2991 case IB_CM_MRA_REP_RCVD:
2992 cm_id->state = IB_CM_ESTABLISHED;
2994 case IB_CM_ESTABLISHED:
3001 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3009 * The CM worker thread may try to destroy the cm_id before it
3010 * can execute this work item. To prevent potential deadlock,
3011 * we need to find the cm_id once we're in the context of the
3012 * worker thread, rather than holding a reference on it.
3014 INIT_WORK(&work->work, cm_work_handler, work);
3015 work->local_id = cm_id->local_id;
3016 work->remote_id = cm_id->remote_id;
3017 work->mad_recv_wc = NULL;
3018 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3019 queue_work(cm.wq, &work->work);
3023 EXPORT_SYMBOL(ib_cm_establish);
3025 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3026 struct ib_mad_recv_wc *mad_recv_wc)
3028 struct cm_work *work;
3029 enum ib_cm_event_type event;
3032 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3033 case CM_REQ_ATTR_ID:
3034 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3035 alt_local_lid != 0);
3036 event = IB_CM_REQ_RECEIVED;
3038 case CM_MRA_ATTR_ID:
3039 event = IB_CM_MRA_RECEIVED;
3041 case CM_REJ_ATTR_ID:
3042 event = IB_CM_REJ_RECEIVED;
3044 case CM_REP_ATTR_ID:
3045 event = IB_CM_REP_RECEIVED;
3047 case CM_RTU_ATTR_ID:
3048 event = IB_CM_RTU_RECEIVED;
3050 case CM_DREQ_ATTR_ID:
3051 event = IB_CM_DREQ_RECEIVED;
3053 case CM_DREP_ATTR_ID:
3054 event = IB_CM_DREP_RECEIVED;
3056 case CM_SIDR_REQ_ATTR_ID:
3057 event = IB_CM_SIDR_REQ_RECEIVED;
3059 case CM_SIDR_REP_ATTR_ID:
3060 event = IB_CM_SIDR_REP_RECEIVED;
3062 case CM_LAP_ATTR_ID:
3064 event = IB_CM_LAP_RECEIVED;
3066 case CM_APR_ATTR_ID:
3067 event = IB_CM_APR_RECEIVED;
3070 ib_free_recv_mad(mad_recv_wc);
3074 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3077 ib_free_recv_mad(mad_recv_wc);
3081 INIT_WORK(&work->work, cm_work_handler, work);
3082 work->cm_event.event = event;
3083 work->mad_recv_wc = mad_recv_wc;
3084 work->port = (struct cm_port *)mad_agent->context;
3085 queue_work(cm.wq, &work->work);
3088 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3089 struct ib_qp_attr *qp_attr,
3092 unsigned long flags;
3095 spin_lock_irqsave(&cm_id_priv->lock, flags);
3096 switch (cm_id_priv->id.state) {
3097 case IB_CM_REQ_SENT:
3098 case IB_CM_MRA_REQ_RCVD:
3099 case IB_CM_REQ_RCVD:
3100 case IB_CM_MRA_REQ_SENT:
3101 case IB_CM_REP_RCVD:
3102 case IB_CM_MRA_REP_SENT:
3103 case IB_CM_REP_SENT:
3104 case IB_CM_MRA_REP_RCVD:
3105 case IB_CM_ESTABLISHED:
3106 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3107 IB_QP_PKEY_INDEX | IB_QP_PORT;
3108 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
3109 IB_ACCESS_REMOTE_WRITE;
3110 if (cm_id_priv->responder_resources)
3111 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
3112 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3113 qp_attr->port_num = cm_id_priv->av.port->port_num;
3120 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3124 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3125 struct ib_qp_attr *qp_attr,
3128 unsigned long flags;
3131 spin_lock_irqsave(&cm_id_priv->lock, flags);
3132 switch (cm_id_priv->id.state) {
3133 case IB_CM_REQ_RCVD:
3134 case IB_CM_MRA_REQ_SENT:
3135 case IB_CM_REP_RCVD:
3136 case IB_CM_MRA_REP_SENT:
3137 case IB_CM_REP_SENT:
3138 case IB_CM_MRA_REP_RCVD:
3139 case IB_CM_ESTABLISHED:
3140 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3141 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3142 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3143 qp_attr->path_mtu = cm_id_priv->path_mtu;
3144 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3145 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3146 if (cm_id_priv->qp_type == IB_QPT_RC) {
3147 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3148 IB_QP_MIN_RNR_TIMER;
3149 qp_attr->max_dest_rd_atomic =
3150 cm_id_priv->responder_resources;
3151 qp_attr->min_rnr_timer = 0;
3153 if (cm_id_priv->alt_av.ah_attr.dlid) {
3154 *qp_attr_mask |= IB_QP_ALT_PATH;
3155 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3163 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3167 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3168 struct ib_qp_attr *qp_attr,
3171 unsigned long flags;
3174 spin_lock_irqsave(&cm_id_priv->lock, flags);
3175 switch (cm_id_priv->id.state) {
3176 case IB_CM_REP_RCVD:
3177 case IB_CM_MRA_REP_SENT:
3178 case IB_CM_REP_SENT:
3179 case IB_CM_MRA_REP_RCVD:
3180 case IB_CM_ESTABLISHED:
3181 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3182 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3183 if (cm_id_priv->qp_type == IB_QPT_RC) {
3184 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3186 IB_QP_MAX_QP_RD_ATOMIC;
3187 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3188 qp_attr->retry_cnt = cm_id_priv->retry_count;
3189 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3190 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3192 if (cm_id_priv->alt_av.ah_attr.dlid) {
3193 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3194 qp_attr->path_mig_state = IB_MIG_REARM;
3202 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3206 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3207 struct ib_qp_attr *qp_attr,
3210 struct cm_id_private *cm_id_priv;
3213 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3214 switch (qp_attr->qp_state) {
3216 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3219 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3222 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3230 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3232 static void cm_add_one(struct ib_device *device)
3234 struct cm_device *cm_dev;
3235 struct cm_port *port;
3236 struct ib_mad_reg_req reg_req = {
3237 .mgmt_class = IB_MGMT_CLASS_CM,
3238 .mgmt_class_version = IB_CM_CLASS_VERSION
3240 struct ib_port_modify port_modify = {
3241 .set_port_cap_mask = IB_PORT_CM_SUP
3243 unsigned long flags;
3247 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3248 device->phys_port_cnt, GFP_KERNEL);
3252 cm_dev->device = device;
3253 cm_dev->ca_guid = device->node_guid;
3255 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3256 for (i = 1; i <= device->phys_port_cnt; i++) {
3257 port = &cm_dev->port[i-1];
3258 port->cm_dev = cm_dev;
3260 port->mad_agent = ib_register_mad_agent(device, i,
3267 if (IS_ERR(port->mad_agent))
3270 ret = ib_modify_port(device, i, 0, &port_modify);
3274 ib_set_client_data(device, &cm_client, cm_dev);
3276 write_lock_irqsave(&cm.device_lock, flags);
3277 list_add_tail(&cm_dev->list, &cm.device_list);
3278 write_unlock_irqrestore(&cm.device_lock, flags);
3282 ib_unregister_mad_agent(port->mad_agent);
3284 port_modify.set_port_cap_mask = 0;
3285 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3287 port = &cm_dev->port[i-1];
3288 ib_modify_port(device, port->port_num, 0, &port_modify);
3289 ib_unregister_mad_agent(port->mad_agent);
3294 static void cm_remove_one(struct ib_device *device)
3296 struct cm_device *cm_dev;
3297 struct cm_port *port;
3298 struct ib_port_modify port_modify = {
3299 .clr_port_cap_mask = IB_PORT_CM_SUP
3301 unsigned long flags;
3304 cm_dev = ib_get_client_data(device, &cm_client);
3308 write_lock_irqsave(&cm.device_lock, flags);
3309 list_del(&cm_dev->list);
3310 write_unlock_irqrestore(&cm.device_lock, flags);
3312 for (i = 1; i <= device->phys_port_cnt; i++) {
3313 port = &cm_dev->port[i-1];
3314 ib_modify_port(device, port->port_num, 0, &port_modify);
3315 ib_unregister_mad_agent(port->mad_agent);
3320 static int __init ib_cm_init(void)
3324 memset(&cm, 0, sizeof cm);
3325 INIT_LIST_HEAD(&cm.device_list);
3326 rwlock_init(&cm.device_lock);
3327 spin_lock_init(&cm.lock);
3328 cm.listen_service_table = RB_ROOT;
3329 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3330 cm.remote_id_table = RB_ROOT;
3331 cm.remote_qp_table = RB_ROOT;
3332 cm.remote_sidr_table = RB_ROOT;
3333 idr_init(&cm.local_id_table);
3334 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3336 cm.wq = create_workqueue("ib_cm");
3340 ret = ib_register_client(&cm_client);
3346 destroy_workqueue(cm.wq);
3350 static void __exit ib_cm_cleanup(void)
3352 destroy_workqueue(cm.wq);
3353 ib_unregister_client(&cm_client);
3354 idr_destroy(&cm.local_id_table);
3357 module_init(ib_cm_init);
3358 module_exit(ib_cm_cleanup);