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 2821 2005-07-08 17:07:28Z 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 */
140 enum ib_qp_type qp_type;
144 enum ib_mtu path_mtu;
148 u8 responder_resources;
150 u8 local_ack_timeout;
155 struct list_head work_list;
159 static void cm_work_handler(void *data);
161 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
163 if (atomic_dec_and_test(&cm_id_priv->refcount))
164 complete(&cm_id_priv->comp);
167 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
168 struct ib_mad_send_buf **msg)
170 struct ib_mad_agent *mad_agent;
171 struct ib_mad_send_buf *m;
174 mad_agent = cm_id_priv->av.port->mad_agent;
175 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
179 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
180 cm_id_priv->av.pkey_index,
181 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
188 /* Timeout set by caller if response is expected. */
190 m->retries = cm_id_priv->max_cm_retries;
192 atomic_inc(&cm_id_priv->refcount);
193 m->context[0] = cm_id_priv;
198 static int cm_alloc_response_msg(struct cm_port *port,
199 struct ib_mad_recv_wc *mad_recv_wc,
200 struct ib_mad_send_buf **msg)
202 struct ib_mad_send_buf *m;
205 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
206 mad_recv_wc->recv_buf.grh, port->port_num);
210 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
211 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
222 static void cm_free_msg(struct ib_mad_send_buf *msg)
224 ib_destroy_ah(msg->ah);
226 cm_deref_id(msg->context[0]);
227 ib_free_send_mad(msg);
230 static void * cm_copy_private_data(const void *private_data,
235 if (!private_data || !private_data_len)
238 data = kmalloc(private_data_len, GFP_KERNEL);
240 return ERR_PTR(-ENOMEM);
242 memcpy(data, private_data, private_data_len);
246 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
247 void *private_data, u8 private_data_len)
249 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
250 kfree(cm_id_priv->private_data);
252 cm_id_priv->private_data = private_data;
253 cm_id_priv->private_data_len = private_data_len;
256 static void cm_set_ah_attr(struct ib_ah_attr *ah_attr, u8 port_num,
257 u16 dlid, u8 sl, u16 src_path_bits)
259 memset(ah_attr, 0, sizeof ah_attr);
260 ah_attr->dlid = dlid;
262 ah_attr->src_path_bits = src_path_bits;
263 ah_attr->port_num = port_num;
266 static void cm_init_av_for_response(struct cm_port *port,
267 struct ib_wc *wc, struct cm_av *av)
270 av->pkey_index = wc->pkey_index;
271 cm_set_ah_attr(&av->ah_attr, port->port_num, wc->slid,
272 wc->sl, wc->dlid_path_bits);
275 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
277 struct cm_device *cm_dev;
278 struct cm_port *port = NULL;
283 read_lock_irqsave(&cm.device_lock, flags);
284 list_for_each_entry(cm_dev, &cm.device_list, list) {
285 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
287 port = &cm_dev->port[p-1];
291 read_unlock_irqrestore(&cm.device_lock, flags);
296 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
297 be16_to_cpu(path->pkey), &av->pkey_index);
302 cm_set_ah_attr(&av->ah_attr, av->port->port_num,
303 be16_to_cpu(path->dlid), path->sl,
304 be16_to_cpu(path->slid) & 0x7F);
305 av->packet_life_time = path->packet_life_time;
309 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
316 spin_lock_irqsave(&cm.lock, flags);
317 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, next_id++,
318 (__force int *) &cm_id_priv->id.local_id);
319 spin_unlock_irqrestore(&cm.lock, flags);
320 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
324 static void cm_free_id(__be32 local_id)
328 spin_lock_irqsave(&cm.lock, flags);
329 idr_remove(&cm.local_id_table, (__force int) local_id);
330 spin_unlock_irqrestore(&cm.lock, flags);
333 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
335 struct cm_id_private *cm_id_priv;
337 cm_id_priv = idr_find(&cm.local_id_table, (__force int) local_id);
339 if (cm_id_priv->id.remote_id == remote_id)
340 atomic_inc(&cm_id_priv->refcount);
348 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
350 struct cm_id_private *cm_id_priv;
353 spin_lock_irqsave(&cm.lock, flags);
354 cm_id_priv = cm_get_id(local_id, remote_id);
355 spin_unlock_irqrestore(&cm.lock, flags);
360 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
362 struct rb_node **link = &cm.listen_service_table.rb_node;
363 struct rb_node *parent = NULL;
364 struct cm_id_private *cur_cm_id_priv;
365 __be64 service_id = cm_id_priv->id.service_id;
366 __be64 service_mask = cm_id_priv->id.service_mask;
370 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
372 if ((cur_cm_id_priv->id.service_mask & service_id) ==
373 (service_mask & cur_cm_id_priv->id.service_id) &&
374 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
375 return cur_cm_id_priv;
377 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
378 link = &(*link)->rb_left;
379 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
380 link = &(*link)->rb_right;
381 else if (service_id < cur_cm_id_priv->id.service_id)
382 link = &(*link)->rb_left;
384 link = &(*link)->rb_right;
386 rb_link_node(&cm_id_priv->service_node, parent, link);
387 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
391 static struct cm_id_private * cm_find_listen(struct ib_device *device,
394 struct rb_node *node = cm.listen_service_table.rb_node;
395 struct cm_id_private *cm_id_priv;
398 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
399 if ((cm_id_priv->id.service_mask & service_id) ==
400 cm_id_priv->id.service_id &&
401 (cm_id_priv->id.device == device))
404 if (device < cm_id_priv->id.device)
405 node = node->rb_left;
406 else if (device > cm_id_priv->id.device)
407 node = node->rb_right;
408 else if (service_id < cm_id_priv->id.service_id)
409 node = node->rb_left;
411 node = node->rb_right;
416 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
419 struct rb_node **link = &cm.remote_id_table.rb_node;
420 struct rb_node *parent = NULL;
421 struct cm_timewait_info *cur_timewait_info;
422 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
423 __be32 remote_id = timewait_info->work.remote_id;
427 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
429 if (remote_id < cur_timewait_info->work.remote_id)
430 link = &(*link)->rb_left;
431 else if (remote_id > cur_timewait_info->work.remote_id)
432 link = &(*link)->rb_right;
433 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
434 link = &(*link)->rb_left;
435 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
436 link = &(*link)->rb_right;
438 return cur_timewait_info;
440 timewait_info->inserted_remote_id = 1;
441 rb_link_node(&timewait_info->remote_id_node, parent, link);
442 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
446 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
449 struct rb_node *node = cm.remote_id_table.rb_node;
450 struct cm_timewait_info *timewait_info;
453 timewait_info = rb_entry(node, struct cm_timewait_info,
455 if (remote_id < timewait_info->work.remote_id)
456 node = node->rb_left;
457 else if (remote_id > timewait_info->work.remote_id)
458 node = node->rb_right;
459 else if (remote_ca_guid < timewait_info->remote_ca_guid)
460 node = node->rb_left;
461 else if (remote_ca_guid > timewait_info->remote_ca_guid)
462 node = node->rb_right;
464 return timewait_info;
469 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
472 struct rb_node **link = &cm.remote_qp_table.rb_node;
473 struct rb_node *parent = NULL;
474 struct cm_timewait_info *cur_timewait_info;
475 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
476 __be32 remote_qpn = timewait_info->remote_qpn;
480 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
482 if (remote_qpn < cur_timewait_info->remote_qpn)
483 link = &(*link)->rb_left;
484 else if (remote_qpn > cur_timewait_info->remote_qpn)
485 link = &(*link)->rb_right;
486 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
487 link = &(*link)->rb_left;
488 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
489 link = &(*link)->rb_right;
491 return cur_timewait_info;
493 timewait_info->inserted_remote_qp = 1;
494 rb_link_node(&timewait_info->remote_qp_node, parent, link);
495 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
499 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
502 struct rb_node **link = &cm.remote_sidr_table.rb_node;
503 struct rb_node *parent = NULL;
504 struct cm_id_private *cur_cm_id_priv;
505 union ib_gid *port_gid = &cm_id_priv->av.dgid;
506 __be32 remote_id = cm_id_priv->id.remote_id;
510 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
512 if (remote_id < cur_cm_id_priv->id.remote_id)
513 link = &(*link)->rb_left;
514 else if (remote_id > cur_cm_id_priv->id.remote_id)
515 link = &(*link)->rb_right;
518 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
521 link = &(*link)->rb_left;
523 link = &(*link)->rb_right;
525 return cur_cm_id_priv;
528 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
529 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
533 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
534 enum ib_cm_sidr_status status)
536 struct ib_cm_sidr_rep_param param;
538 memset(¶m, 0, sizeof param);
539 param.status = status;
540 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
543 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
544 ib_cm_handler cm_handler,
547 struct cm_id_private *cm_id_priv;
550 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
554 cm_id_priv->id.state = IB_CM_IDLE;
555 cm_id_priv->id.device = device;
556 cm_id_priv->id.cm_handler = cm_handler;
557 cm_id_priv->id.context = context;
558 cm_id_priv->id.remote_cm_qpn = 1;
559 ret = cm_alloc_id(cm_id_priv);
563 spin_lock_init(&cm_id_priv->lock);
564 init_completion(&cm_id_priv->comp);
565 INIT_LIST_HEAD(&cm_id_priv->work_list);
566 atomic_set(&cm_id_priv->work_count, -1);
567 atomic_set(&cm_id_priv->refcount, 1);
568 return &cm_id_priv->id;
572 return ERR_PTR(-ENOMEM);
574 EXPORT_SYMBOL(ib_create_cm_id);
576 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
578 struct cm_work *work;
580 if (list_empty(&cm_id_priv->work_list))
583 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
584 list_del(&work->list);
588 static void cm_free_work(struct cm_work *work)
590 if (work->mad_recv_wc)
591 ib_free_recv_mad(work->mad_recv_wc);
595 static inline int cm_convert_to_ms(int iba_time)
597 /* approximate conversion to ms from 4.096us x 2^iba_time */
598 return 1 << max(iba_time - 8, 0);
601 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
605 if (!timewait_info->inserted_remote_id &&
606 !timewait_info->inserted_remote_qp)
609 spin_lock_irqsave(&cm.lock, flags);
610 if (timewait_info->inserted_remote_id) {
611 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
612 timewait_info->inserted_remote_id = 0;
615 if (timewait_info->inserted_remote_qp) {
616 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
617 timewait_info->inserted_remote_qp = 0;
619 spin_unlock_irqrestore(&cm.lock, flags);
622 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
624 struct cm_timewait_info *timewait_info;
626 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
628 return ERR_PTR(-ENOMEM);
630 timewait_info->work.local_id = local_id;
631 INIT_WORK(&timewait_info->work.work, cm_work_handler,
632 &timewait_info->work);
633 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
634 return timewait_info;
637 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
642 * The cm_id could be destroyed by the user before we exit timewait.
643 * To protect against this, we search for the cm_id after exiting
644 * timewait before notifying the user that we've exited timewait.
646 cm_id_priv->id.state = IB_CM_TIMEWAIT;
647 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
648 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
649 msecs_to_jiffies(wait_time));
650 cm_id_priv->timewait_info = NULL;
653 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
655 cm_id_priv->id.state = IB_CM_IDLE;
656 if (cm_id_priv->timewait_info) {
657 cm_cleanup_timewait(cm_id_priv->timewait_info);
658 kfree(cm_id_priv->timewait_info);
659 cm_id_priv->timewait_info = NULL;
663 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
665 struct cm_id_private *cm_id_priv;
666 struct cm_work *work;
669 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
671 spin_lock_irqsave(&cm_id_priv->lock, flags);
672 switch (cm_id->state) {
674 cm_id->state = IB_CM_IDLE;
675 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
676 spin_lock_irqsave(&cm.lock, flags);
677 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
678 spin_unlock_irqrestore(&cm.lock, flags);
680 case IB_CM_SIDR_REQ_SENT:
681 cm_id->state = IB_CM_IDLE;
682 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
683 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
685 case IB_CM_SIDR_REQ_RCVD:
686 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
687 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
690 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
691 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
692 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
693 &cm_id_priv->av.port->cm_dev->ca_guid,
694 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
697 case IB_CM_MRA_REQ_RCVD:
699 case IB_CM_MRA_REP_RCVD:
700 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
703 case IB_CM_MRA_REQ_SENT:
705 case IB_CM_MRA_REP_SENT:
706 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
707 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
710 case IB_CM_ESTABLISHED:
711 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
712 ib_send_cm_dreq(cm_id, NULL, 0);
714 case IB_CM_DREQ_SENT:
715 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
716 cm_enter_timewait(cm_id_priv);
717 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
719 case IB_CM_DREQ_RCVD:
720 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
721 ib_send_cm_drep(cm_id, NULL, 0);
724 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
728 cm_free_id(cm_id->local_id);
729 cm_deref_id(cm_id_priv);
730 wait_for_completion(&cm_id_priv->comp);
731 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
733 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
734 kfree(cm_id_priv->private_data);
737 EXPORT_SYMBOL(ib_destroy_cm_id);
739 int ib_cm_listen(struct ib_cm_id *cm_id,
743 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
747 service_mask = service_mask ? service_mask :
748 __constant_cpu_to_be64(~0ULL);
749 service_id &= service_mask;
750 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
751 (service_id != IB_CM_ASSIGN_SERVICE_ID))
754 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
755 BUG_ON(cm_id->state != IB_CM_IDLE);
757 cm_id->state = IB_CM_LISTEN;
759 spin_lock_irqsave(&cm.lock, flags);
760 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
761 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
762 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
764 cm_id->service_id = service_id;
765 cm_id->service_mask = service_mask;
767 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
768 spin_unlock_irqrestore(&cm.lock, flags);
770 if (cur_cm_id_priv) {
771 cm_id->state = IB_CM_IDLE;
776 EXPORT_SYMBOL(ib_cm_listen);
778 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
779 enum cm_msg_sequence msg_seq)
783 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
784 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
786 return cpu_to_be64(hi_tid | low_tid);
789 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
790 __be16 attr_id, __be64 tid)
792 hdr->base_version = IB_MGMT_BASE_VERSION;
793 hdr->mgmt_class = IB_MGMT_CLASS_CM;
794 hdr->class_version = IB_CM_CLASS_VERSION;
795 hdr->method = IB_MGMT_METHOD_SEND;
796 hdr->attr_id = attr_id;
800 static void cm_format_req(struct cm_req_msg *req_msg,
801 struct cm_id_private *cm_id_priv,
802 struct ib_cm_req_param *param)
804 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
805 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
807 req_msg->local_comm_id = cm_id_priv->id.local_id;
808 req_msg->service_id = param->service_id;
809 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
810 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
811 cm_req_set_resp_res(req_msg, param->responder_resources);
812 cm_req_set_init_depth(req_msg, param->initiator_depth);
813 cm_req_set_remote_resp_timeout(req_msg,
814 param->remote_cm_response_timeout);
815 cm_req_set_qp_type(req_msg, param->qp_type);
816 cm_req_set_flow_ctrl(req_msg, param->flow_control);
817 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
818 cm_req_set_local_resp_timeout(req_msg,
819 param->local_cm_response_timeout);
820 cm_req_set_retry_count(req_msg, param->retry_count);
821 req_msg->pkey = param->primary_path->pkey;
822 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
823 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
824 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
825 cm_req_set_srq(req_msg, param->srq);
827 req_msg->primary_local_lid = param->primary_path->slid;
828 req_msg->primary_remote_lid = param->primary_path->dlid;
829 req_msg->primary_local_gid = param->primary_path->sgid;
830 req_msg->primary_remote_gid = param->primary_path->dgid;
831 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
832 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
833 req_msg->primary_traffic_class = param->primary_path->traffic_class;
834 req_msg->primary_hop_limit = param->primary_path->hop_limit;
835 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
836 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
837 cm_req_set_primary_local_ack_timeout(req_msg,
838 min(31, param->primary_path->packet_life_time + 1));
840 if (param->alternate_path) {
841 req_msg->alt_local_lid = param->alternate_path->slid;
842 req_msg->alt_remote_lid = param->alternate_path->dlid;
843 req_msg->alt_local_gid = param->alternate_path->sgid;
844 req_msg->alt_remote_gid = param->alternate_path->dgid;
845 cm_req_set_alt_flow_label(req_msg,
846 param->alternate_path->flow_label);
847 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
848 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
849 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
850 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
851 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
852 cm_req_set_alt_local_ack_timeout(req_msg,
853 min(31, param->alternate_path->packet_life_time + 1));
856 if (param->private_data && param->private_data_len)
857 memcpy(req_msg->private_data, param->private_data,
858 param->private_data_len);
861 static int cm_validate_req_param(struct ib_cm_req_param *param)
863 /* peer-to-peer not supported */
864 if (param->peer_to_peer)
867 if (!param->primary_path)
870 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
873 if (param->private_data &&
874 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
877 if (param->alternate_path &&
878 (param->alternate_path->pkey != param->primary_path->pkey ||
879 param->alternate_path->mtu != param->primary_path->mtu))
885 int ib_send_cm_req(struct ib_cm_id *cm_id,
886 struct ib_cm_req_param *param)
888 struct cm_id_private *cm_id_priv;
889 struct cm_req_msg *req_msg;
893 ret = cm_validate_req_param(param);
897 /* Verify that we're not in timewait. */
898 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
899 spin_lock_irqsave(&cm_id_priv->lock, flags);
900 if (cm_id->state != IB_CM_IDLE) {
901 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
905 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
907 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
909 if (IS_ERR(cm_id_priv->timewait_info))
912 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
915 if (param->alternate_path) {
916 ret = cm_init_av_by_path(param->alternate_path,
917 &cm_id_priv->alt_av);
921 cm_id->service_id = param->service_id;
922 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
923 cm_id_priv->timeout_ms = cm_convert_to_ms(
924 param->primary_path->packet_life_time) * 2 +
926 param->remote_cm_response_timeout);
927 cm_id_priv->max_cm_retries = param->max_cm_retries;
928 cm_id_priv->initiator_depth = param->initiator_depth;
929 cm_id_priv->responder_resources = param->responder_resources;
930 cm_id_priv->retry_count = param->retry_count;
931 cm_id_priv->path_mtu = param->primary_path->mtu;
932 cm_id_priv->qp_type = param->qp_type;
934 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
938 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
939 cm_format_req(req_msg, cm_id_priv, param);
940 cm_id_priv->tid = req_msg->hdr.tid;
941 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
942 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
944 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
945 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
946 cm_id_priv->local_ack_timeout =
947 cm_req_get_primary_local_ack_timeout(req_msg);
949 spin_lock_irqsave(&cm_id_priv->lock, flags);
950 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
952 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
955 BUG_ON(cm_id->state != IB_CM_IDLE);
956 cm_id->state = IB_CM_REQ_SENT;
957 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
960 error2: cm_free_msg(cm_id_priv->msg);
961 error1: kfree(cm_id_priv->timewait_info);
964 EXPORT_SYMBOL(ib_send_cm_req);
966 static int cm_issue_rej(struct cm_port *port,
967 struct ib_mad_recv_wc *mad_recv_wc,
968 enum ib_cm_rej_reason reason,
969 enum cm_msg_response msg_rejected,
970 void *ari, u8 ari_length)
972 struct ib_mad_send_buf *msg = NULL;
973 struct cm_rej_msg *rej_msg, *rcv_msg;
976 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
980 /* We just need common CM header information. Cast to any message. */
981 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
982 rej_msg = (struct cm_rej_msg *) msg->mad;
984 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
985 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
986 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
987 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
988 rej_msg->reason = cpu_to_be16(reason);
990 if (ari && ari_length) {
991 cm_rej_set_reject_info_len(rej_msg, ari_length);
992 memcpy(rej_msg->ari, ari, ari_length);
995 ret = ib_post_send_mad(msg, NULL);
1002 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1003 __be32 local_qpn, __be32 remote_qpn)
1005 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1006 ((local_ca_guid == remote_ca_guid) &&
1007 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1010 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1011 struct ib_sa_path_rec *primary_path,
1012 struct ib_sa_path_rec *alt_path)
1014 memset(primary_path, 0, sizeof *primary_path);
1015 primary_path->dgid = req_msg->primary_local_gid;
1016 primary_path->sgid = req_msg->primary_remote_gid;
1017 primary_path->dlid = req_msg->primary_local_lid;
1018 primary_path->slid = req_msg->primary_remote_lid;
1019 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1020 primary_path->hop_limit = req_msg->primary_hop_limit;
1021 primary_path->traffic_class = req_msg->primary_traffic_class;
1022 primary_path->reversible = 1;
1023 primary_path->pkey = req_msg->pkey;
1024 primary_path->sl = cm_req_get_primary_sl(req_msg);
1025 primary_path->mtu_selector = IB_SA_EQ;
1026 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1027 primary_path->rate_selector = IB_SA_EQ;
1028 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1029 primary_path->packet_life_time_selector = IB_SA_EQ;
1030 primary_path->packet_life_time =
1031 cm_req_get_primary_local_ack_timeout(req_msg);
1032 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1034 if (req_msg->alt_local_lid) {
1035 memset(alt_path, 0, sizeof *alt_path);
1036 alt_path->dgid = req_msg->alt_local_gid;
1037 alt_path->sgid = req_msg->alt_remote_gid;
1038 alt_path->dlid = req_msg->alt_local_lid;
1039 alt_path->slid = req_msg->alt_remote_lid;
1040 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1041 alt_path->hop_limit = req_msg->alt_hop_limit;
1042 alt_path->traffic_class = req_msg->alt_traffic_class;
1043 alt_path->reversible = 1;
1044 alt_path->pkey = req_msg->pkey;
1045 alt_path->sl = cm_req_get_alt_sl(req_msg);
1046 alt_path->mtu_selector = IB_SA_EQ;
1047 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1048 alt_path->rate_selector = IB_SA_EQ;
1049 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1050 alt_path->packet_life_time_selector = IB_SA_EQ;
1051 alt_path->packet_life_time =
1052 cm_req_get_alt_local_ack_timeout(req_msg);
1053 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1057 static void cm_format_req_event(struct cm_work *work,
1058 struct cm_id_private *cm_id_priv,
1059 struct ib_cm_id *listen_id)
1061 struct cm_req_msg *req_msg;
1062 struct ib_cm_req_event_param *param;
1064 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1065 param = &work->cm_event.param.req_rcvd;
1066 param->listen_id = listen_id;
1067 param->port = cm_id_priv->av.port->port_num;
1068 param->primary_path = &work->path[0];
1069 if (req_msg->alt_local_lid)
1070 param->alternate_path = &work->path[1];
1072 param->alternate_path = NULL;
1073 param->remote_ca_guid = req_msg->local_ca_guid;
1074 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1075 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1076 param->qp_type = cm_req_get_qp_type(req_msg);
1077 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1078 param->responder_resources = cm_req_get_init_depth(req_msg);
1079 param->initiator_depth = cm_req_get_resp_res(req_msg);
1080 param->local_cm_response_timeout =
1081 cm_req_get_remote_resp_timeout(req_msg);
1082 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1083 param->remote_cm_response_timeout =
1084 cm_req_get_local_resp_timeout(req_msg);
1085 param->retry_count = cm_req_get_retry_count(req_msg);
1086 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1087 param->srq = cm_req_get_srq(req_msg);
1088 work->cm_event.private_data = &req_msg->private_data;
1091 static void cm_process_work(struct cm_id_private *cm_id_priv,
1092 struct cm_work *work)
1094 unsigned long flags;
1097 /* We will typically only have the current event to report. */
1098 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1101 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1102 spin_lock_irqsave(&cm_id_priv->lock, flags);
1103 work = cm_dequeue_work(cm_id_priv);
1104 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1106 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1110 cm_deref_id(cm_id_priv);
1112 ib_destroy_cm_id(&cm_id_priv->id);
1115 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1116 struct cm_id_private *cm_id_priv,
1117 enum cm_msg_response msg_mraed, u8 service_timeout,
1118 const void *private_data, u8 private_data_len)
1120 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1121 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1122 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1123 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1124 cm_mra_set_service_timeout(mra_msg, service_timeout);
1126 if (private_data && private_data_len)
1127 memcpy(mra_msg->private_data, private_data, private_data_len);
1130 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1131 struct cm_id_private *cm_id_priv,
1132 enum ib_cm_rej_reason reason,
1135 const void *private_data,
1136 u8 private_data_len)
1138 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1139 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1141 switch(cm_id_priv->id.state) {
1142 case IB_CM_REQ_RCVD:
1143 rej_msg->local_comm_id = 0;
1144 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1146 case IB_CM_MRA_REQ_SENT:
1147 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1148 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1150 case IB_CM_REP_RCVD:
1151 case IB_CM_MRA_REP_SENT:
1152 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1153 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1156 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1157 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1161 rej_msg->reason = cpu_to_be16(reason);
1162 if (ari && ari_length) {
1163 cm_rej_set_reject_info_len(rej_msg, ari_length);
1164 memcpy(rej_msg->ari, ari, ari_length);
1167 if (private_data && private_data_len)
1168 memcpy(rej_msg->private_data, private_data, private_data_len);
1171 static void cm_dup_req_handler(struct cm_work *work,
1172 struct cm_id_private *cm_id_priv)
1174 struct ib_mad_send_buf *msg = NULL;
1175 unsigned long flags;
1178 /* Quick state check to discard duplicate REQs. */
1179 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1182 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1186 spin_lock_irqsave(&cm_id_priv->lock, flags);
1187 switch (cm_id_priv->id.state) {
1188 case IB_CM_MRA_REQ_SENT:
1189 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1190 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1191 cm_id_priv->private_data,
1192 cm_id_priv->private_data_len);
1194 case IB_CM_TIMEWAIT:
1195 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1196 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1201 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1203 ret = ib_post_send_mad(msg, NULL);
1208 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1209 free: cm_free_msg(msg);
1212 static struct cm_id_private * cm_match_req(struct cm_work *work,
1213 struct cm_id_private *cm_id_priv)
1215 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1216 struct cm_timewait_info *timewait_info;
1217 struct cm_req_msg *req_msg;
1218 unsigned long flags;
1220 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1222 /* Check for duplicate REQ and stale connections. */
1223 spin_lock_irqsave(&cm.lock, flags);
1224 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1226 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1228 if (timewait_info) {
1229 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1230 timewait_info->work.remote_id);
1231 spin_unlock_irqrestore(&cm.lock, flags);
1232 if (cur_cm_id_priv) {
1233 cm_dup_req_handler(work, cur_cm_id_priv);
1234 cm_deref_id(cur_cm_id_priv);
1236 cm_issue_rej(work->port, work->mad_recv_wc,
1237 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1242 /* Find matching listen request. */
1243 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1244 req_msg->service_id);
1245 if (!listen_cm_id_priv) {
1246 spin_unlock_irqrestore(&cm.lock, flags);
1247 cm_issue_rej(work->port, work->mad_recv_wc,
1248 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1252 atomic_inc(&listen_cm_id_priv->refcount);
1253 atomic_inc(&cm_id_priv->refcount);
1254 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1255 atomic_inc(&cm_id_priv->work_count);
1256 spin_unlock_irqrestore(&cm.lock, flags);
1257 return listen_cm_id_priv;
1259 error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1263 static int cm_req_handler(struct cm_work *work)
1265 struct ib_cm_id *cm_id;
1266 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1267 struct cm_req_msg *req_msg;
1270 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1272 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1274 return PTR_ERR(cm_id);
1276 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1277 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1278 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1280 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1282 if (IS_ERR(cm_id_priv->timewait_info)) {
1283 ret = PTR_ERR(cm_id_priv->timewait_info);
1286 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1287 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1288 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1290 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1291 if (!listen_cm_id_priv) {
1296 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1297 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1298 cm_id_priv->id.service_id = req_msg->service_id;
1299 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1301 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1302 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1305 if (req_msg->alt_local_lid) {
1306 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1310 cm_id_priv->tid = req_msg->hdr.tid;
1311 cm_id_priv->timeout_ms = cm_convert_to_ms(
1312 cm_req_get_local_resp_timeout(req_msg));
1313 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1314 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1315 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1316 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1317 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1318 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1319 cm_id_priv->local_ack_timeout =
1320 cm_req_get_primary_local_ack_timeout(req_msg);
1321 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1322 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1323 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1325 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1326 cm_process_work(cm_id_priv, work);
1327 cm_deref_id(listen_cm_id_priv);
1330 error3: atomic_dec(&cm_id_priv->refcount);
1331 cm_deref_id(listen_cm_id_priv);
1332 cm_cleanup_timewait(cm_id_priv->timewait_info);
1333 error2: kfree(cm_id_priv->timewait_info);
1334 cm_id_priv->timewait_info = NULL;
1335 error1: ib_destroy_cm_id(&cm_id_priv->id);
1339 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1340 struct cm_id_private *cm_id_priv,
1341 struct ib_cm_rep_param *param)
1343 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1344 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1345 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1346 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1347 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1348 rep_msg->resp_resources = param->responder_resources;
1349 rep_msg->initiator_depth = param->initiator_depth;
1350 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1351 cm_rep_set_failover(rep_msg, param->failover_accepted);
1352 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1353 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1354 cm_rep_set_srq(rep_msg, param->srq);
1355 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1357 if (param->private_data && param->private_data_len)
1358 memcpy(rep_msg->private_data, param->private_data,
1359 param->private_data_len);
1362 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1363 struct ib_cm_rep_param *param)
1365 struct cm_id_private *cm_id_priv;
1366 struct ib_mad_send_buf *msg;
1367 struct cm_rep_msg *rep_msg;
1368 unsigned long flags;
1371 if (param->private_data &&
1372 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1375 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1376 spin_lock_irqsave(&cm_id_priv->lock, flags);
1377 if (cm_id->state != IB_CM_REQ_RCVD &&
1378 cm_id->state != IB_CM_MRA_REQ_SENT) {
1383 ret = cm_alloc_msg(cm_id_priv, &msg);
1387 rep_msg = (struct cm_rep_msg *) msg->mad;
1388 cm_format_rep(rep_msg, cm_id_priv, param);
1389 msg->timeout_ms = cm_id_priv->timeout_ms;
1390 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1392 ret = ib_post_send_mad(msg, NULL);
1394 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1399 cm_id->state = IB_CM_REP_SENT;
1400 cm_id_priv->msg = msg;
1401 cm_id_priv->initiator_depth = param->initiator_depth;
1402 cm_id_priv->responder_resources = param->responder_resources;
1403 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1404 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1406 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1409 EXPORT_SYMBOL(ib_send_cm_rep);
1411 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1412 struct cm_id_private *cm_id_priv,
1413 const void *private_data,
1414 u8 private_data_len)
1416 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1417 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1418 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1420 if (private_data && private_data_len)
1421 memcpy(rtu_msg->private_data, private_data, private_data_len);
1424 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1425 const void *private_data,
1426 u8 private_data_len)
1428 struct cm_id_private *cm_id_priv;
1429 struct ib_mad_send_buf *msg;
1430 unsigned long flags;
1434 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1437 data = cm_copy_private_data(private_data, private_data_len);
1439 return PTR_ERR(data);
1441 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1442 spin_lock_irqsave(&cm_id_priv->lock, flags);
1443 if (cm_id->state != IB_CM_REP_RCVD &&
1444 cm_id->state != IB_CM_MRA_REP_SENT) {
1449 ret = cm_alloc_msg(cm_id_priv, &msg);
1453 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1454 private_data, private_data_len);
1456 ret = ib_post_send_mad(msg, NULL);
1458 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1464 cm_id->state = IB_CM_ESTABLISHED;
1465 cm_set_private_data(cm_id_priv, data, private_data_len);
1466 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1469 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1473 EXPORT_SYMBOL(ib_send_cm_rtu);
1475 static void cm_format_rep_event(struct cm_work *work)
1477 struct cm_rep_msg *rep_msg;
1478 struct ib_cm_rep_event_param *param;
1480 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1481 param = &work->cm_event.param.rep_rcvd;
1482 param->remote_ca_guid = rep_msg->local_ca_guid;
1483 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1484 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1485 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1486 param->responder_resources = rep_msg->initiator_depth;
1487 param->initiator_depth = rep_msg->resp_resources;
1488 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1489 param->failover_accepted = cm_rep_get_failover(rep_msg);
1490 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1491 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1492 param->srq = cm_rep_get_srq(rep_msg);
1493 work->cm_event.private_data = &rep_msg->private_data;
1496 static void cm_dup_rep_handler(struct cm_work *work)
1498 struct cm_id_private *cm_id_priv;
1499 struct cm_rep_msg *rep_msg;
1500 struct ib_mad_send_buf *msg = NULL;
1501 unsigned long flags;
1504 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1505 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1506 rep_msg->local_comm_id);
1510 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1514 spin_lock_irqsave(&cm_id_priv->lock, flags);
1515 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1516 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1517 cm_id_priv->private_data,
1518 cm_id_priv->private_data_len);
1519 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1520 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1521 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1522 cm_id_priv->private_data,
1523 cm_id_priv->private_data_len);
1526 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1528 ret = ib_post_send_mad(msg, NULL);
1533 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1534 free: cm_free_msg(msg);
1535 deref: cm_deref_id(cm_id_priv);
1538 static int cm_rep_handler(struct cm_work *work)
1540 struct cm_id_private *cm_id_priv;
1541 struct cm_rep_msg *rep_msg;
1542 unsigned long flags;
1545 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1546 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1548 cm_dup_rep_handler(work);
1552 cm_format_rep_event(work);
1554 spin_lock_irqsave(&cm_id_priv->lock, flags);
1555 switch (cm_id_priv->id.state) {
1556 case IB_CM_REQ_SENT:
1557 case IB_CM_MRA_REQ_RCVD:
1560 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1565 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1566 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1567 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1569 spin_lock(&cm.lock);
1570 /* Check for duplicate REP. */
1571 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1572 spin_unlock(&cm.lock);
1573 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1577 /* Check for a stale connection. */
1578 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1579 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1580 &cm.remote_id_table);
1581 cm_id_priv->timewait_info->inserted_remote_id = 0;
1582 spin_unlock(&cm.lock);
1583 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1584 cm_issue_rej(work->port, work->mad_recv_wc,
1585 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1590 spin_unlock(&cm.lock);
1592 cm_id_priv->id.state = IB_CM_REP_RCVD;
1593 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1594 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1595 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1596 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1597 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1598 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1600 /* todo: handle peer_to_peer */
1602 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1603 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1605 list_add_tail(&work->list, &cm_id_priv->work_list);
1606 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1609 cm_process_work(cm_id_priv, work);
1611 cm_deref_id(cm_id_priv);
1615 cm_deref_id(cm_id_priv);
1619 static int cm_establish_handler(struct cm_work *work)
1621 struct cm_id_private *cm_id_priv;
1622 unsigned long flags;
1625 /* See comment in ib_cm_establish about lookup. */
1626 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1630 spin_lock_irqsave(&cm_id_priv->lock, flags);
1631 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1632 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1636 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1637 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1639 list_add_tail(&work->list, &cm_id_priv->work_list);
1640 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1643 cm_process_work(cm_id_priv, work);
1645 cm_deref_id(cm_id_priv);
1648 cm_deref_id(cm_id_priv);
1652 static int cm_rtu_handler(struct cm_work *work)
1654 struct cm_id_private *cm_id_priv;
1655 struct cm_rtu_msg *rtu_msg;
1656 unsigned long flags;
1659 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1660 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1661 rtu_msg->local_comm_id);
1665 work->cm_event.private_data = &rtu_msg->private_data;
1667 spin_lock_irqsave(&cm_id_priv->lock, flags);
1668 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1669 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1670 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1673 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1675 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1676 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1678 list_add_tail(&work->list, &cm_id_priv->work_list);
1679 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1682 cm_process_work(cm_id_priv, work);
1684 cm_deref_id(cm_id_priv);
1687 cm_deref_id(cm_id_priv);
1691 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1692 struct cm_id_private *cm_id_priv,
1693 const void *private_data,
1694 u8 private_data_len)
1696 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1697 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1698 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1699 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1700 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1702 if (private_data && private_data_len)
1703 memcpy(dreq_msg->private_data, private_data, private_data_len);
1706 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1707 const void *private_data,
1708 u8 private_data_len)
1710 struct cm_id_private *cm_id_priv;
1711 struct ib_mad_send_buf *msg;
1712 unsigned long flags;
1715 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1718 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1719 spin_lock_irqsave(&cm_id_priv->lock, flags);
1720 if (cm_id->state != IB_CM_ESTABLISHED) {
1725 ret = cm_alloc_msg(cm_id_priv, &msg);
1727 cm_enter_timewait(cm_id_priv);
1731 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1732 private_data, private_data_len);
1733 msg->timeout_ms = cm_id_priv->timeout_ms;
1734 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1736 ret = ib_post_send_mad(msg, NULL);
1738 cm_enter_timewait(cm_id_priv);
1739 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1744 cm_id->state = IB_CM_DREQ_SENT;
1745 cm_id_priv->msg = msg;
1746 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1749 EXPORT_SYMBOL(ib_send_cm_dreq);
1751 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1752 struct cm_id_private *cm_id_priv,
1753 const void *private_data,
1754 u8 private_data_len)
1756 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1757 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1758 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1760 if (private_data && private_data_len)
1761 memcpy(drep_msg->private_data, private_data, private_data_len);
1764 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1765 const void *private_data,
1766 u8 private_data_len)
1768 struct cm_id_private *cm_id_priv;
1769 struct ib_mad_send_buf *msg;
1770 unsigned long flags;
1774 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1777 data = cm_copy_private_data(private_data, private_data_len);
1779 return PTR_ERR(data);
1781 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1782 spin_lock_irqsave(&cm_id_priv->lock, flags);
1783 if (cm_id->state != IB_CM_DREQ_RCVD) {
1784 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1789 cm_set_private_data(cm_id_priv, data, private_data_len);
1790 cm_enter_timewait(cm_id_priv);
1792 ret = cm_alloc_msg(cm_id_priv, &msg);
1796 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1797 private_data, private_data_len);
1799 ret = ib_post_send_mad(msg, NULL);
1801 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1806 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1809 EXPORT_SYMBOL(ib_send_cm_drep);
1811 static int cm_dreq_handler(struct cm_work *work)
1813 struct cm_id_private *cm_id_priv;
1814 struct cm_dreq_msg *dreq_msg;
1815 struct ib_mad_send_buf *msg = NULL;
1816 unsigned long flags;
1819 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1820 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1821 dreq_msg->local_comm_id);
1825 work->cm_event.private_data = &dreq_msg->private_data;
1827 spin_lock_irqsave(&cm_id_priv->lock, flags);
1828 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1831 switch (cm_id_priv->id.state) {
1832 case IB_CM_REP_SENT:
1833 case IB_CM_DREQ_SENT:
1834 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1836 case IB_CM_ESTABLISHED:
1837 case IB_CM_MRA_REP_RCVD:
1839 case IB_CM_TIMEWAIT:
1840 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1843 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1844 cm_id_priv->private_data,
1845 cm_id_priv->private_data_len);
1846 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1848 if (ib_post_send_mad(msg, NULL))
1854 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1855 cm_id_priv->tid = dreq_msg->hdr.tid;
1856 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1858 list_add_tail(&work->list, &cm_id_priv->work_list);
1859 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1862 cm_process_work(cm_id_priv, work);
1864 cm_deref_id(cm_id_priv);
1867 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1868 deref: cm_deref_id(cm_id_priv);
1872 static int cm_drep_handler(struct cm_work *work)
1874 struct cm_id_private *cm_id_priv;
1875 struct cm_drep_msg *drep_msg;
1876 unsigned long flags;
1879 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1880 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1881 drep_msg->local_comm_id);
1885 work->cm_event.private_data = &drep_msg->private_data;
1887 spin_lock_irqsave(&cm_id_priv->lock, flags);
1888 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1889 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1890 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1893 cm_enter_timewait(cm_id_priv);
1895 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1896 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1898 list_add_tail(&work->list, &cm_id_priv->work_list);
1899 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1902 cm_process_work(cm_id_priv, work);
1904 cm_deref_id(cm_id_priv);
1907 cm_deref_id(cm_id_priv);
1911 int ib_send_cm_rej(struct ib_cm_id *cm_id,
1912 enum ib_cm_rej_reason reason,
1915 const void *private_data,
1916 u8 private_data_len)
1918 struct cm_id_private *cm_id_priv;
1919 struct ib_mad_send_buf *msg;
1920 unsigned long flags;
1923 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
1924 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
1927 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1929 spin_lock_irqsave(&cm_id_priv->lock, flags);
1930 switch (cm_id->state) {
1931 case IB_CM_REQ_SENT:
1932 case IB_CM_MRA_REQ_RCVD:
1933 case IB_CM_REQ_RCVD:
1934 case IB_CM_MRA_REQ_SENT:
1935 case IB_CM_REP_RCVD:
1936 case IB_CM_MRA_REP_SENT:
1937 ret = cm_alloc_msg(cm_id_priv, &msg);
1939 cm_format_rej((struct cm_rej_msg *) msg->mad,
1940 cm_id_priv, reason, ari, ari_length,
1941 private_data, private_data_len);
1943 cm_reset_to_idle(cm_id_priv);
1945 case IB_CM_REP_SENT:
1946 case IB_CM_MRA_REP_RCVD:
1947 ret = cm_alloc_msg(cm_id_priv, &msg);
1949 cm_format_rej((struct cm_rej_msg *) msg->mad,
1950 cm_id_priv, reason, ari, ari_length,
1951 private_data, private_data_len);
1953 cm_enter_timewait(cm_id_priv);
1963 ret = ib_post_send_mad(msg, NULL);
1967 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1970 EXPORT_SYMBOL(ib_send_cm_rej);
1972 static void cm_format_rej_event(struct cm_work *work)
1974 struct cm_rej_msg *rej_msg;
1975 struct ib_cm_rej_event_param *param;
1977 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
1978 param = &work->cm_event.param.rej_rcvd;
1979 param->ari = rej_msg->ari;
1980 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
1981 param->reason = __be16_to_cpu(rej_msg->reason);
1982 work->cm_event.private_data = &rej_msg->private_data;
1985 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
1987 struct cm_timewait_info *timewait_info;
1988 struct cm_id_private *cm_id_priv;
1989 unsigned long flags;
1992 remote_id = rej_msg->local_comm_id;
1994 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
1995 spin_lock_irqsave(&cm.lock, flags);
1996 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
1998 if (!timewait_info) {
1999 spin_unlock_irqrestore(&cm.lock, flags);
2002 cm_id_priv = idr_find(&cm.local_id_table,
2003 (__force int) timewait_info->work.local_id);
2005 if (cm_id_priv->id.remote_id == remote_id)
2006 atomic_inc(&cm_id_priv->refcount);
2010 spin_unlock_irqrestore(&cm.lock, flags);
2011 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2012 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2014 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2019 static int cm_rej_handler(struct cm_work *work)
2021 struct cm_id_private *cm_id_priv;
2022 struct cm_rej_msg *rej_msg;
2023 unsigned long flags;
2026 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2027 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2031 cm_format_rej_event(work);
2033 spin_lock_irqsave(&cm_id_priv->lock, flags);
2034 switch (cm_id_priv->id.state) {
2035 case IB_CM_REQ_SENT:
2036 case IB_CM_MRA_REQ_RCVD:
2037 case IB_CM_REP_SENT:
2038 case IB_CM_MRA_REP_RCVD:
2039 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2041 case IB_CM_REQ_RCVD:
2042 case IB_CM_MRA_REQ_SENT:
2043 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2044 cm_enter_timewait(cm_id_priv);
2046 cm_reset_to_idle(cm_id_priv);
2048 case IB_CM_DREQ_SENT:
2049 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2051 case IB_CM_REP_RCVD:
2052 case IB_CM_MRA_REP_SENT:
2053 case IB_CM_ESTABLISHED:
2054 cm_enter_timewait(cm_id_priv);
2057 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2062 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2064 list_add_tail(&work->list, &cm_id_priv->work_list);
2065 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2068 cm_process_work(cm_id_priv, work);
2070 cm_deref_id(cm_id_priv);
2073 cm_deref_id(cm_id_priv);
2077 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2079 const void *private_data,
2080 u8 private_data_len)
2082 struct cm_id_private *cm_id_priv;
2083 struct ib_mad_send_buf *msg;
2085 unsigned long flags;
2088 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2091 data = cm_copy_private_data(private_data, private_data_len);
2093 return PTR_ERR(data);
2095 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2097 spin_lock_irqsave(&cm_id_priv->lock, flags);
2098 switch(cm_id_priv->id.state) {
2099 case IB_CM_REQ_RCVD:
2100 ret = cm_alloc_msg(cm_id_priv, &msg);
2104 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2105 CM_MSG_RESPONSE_REQ, service_timeout,
2106 private_data, private_data_len);
2107 ret = ib_post_send_mad(msg, NULL);
2110 cm_id->state = IB_CM_MRA_REQ_SENT;
2112 case IB_CM_REP_RCVD:
2113 ret = cm_alloc_msg(cm_id_priv, &msg);
2117 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2118 CM_MSG_RESPONSE_REP, service_timeout,
2119 private_data, private_data_len);
2120 ret = ib_post_send_mad(msg, NULL);
2123 cm_id->state = IB_CM_MRA_REP_SENT;
2125 case IB_CM_ESTABLISHED:
2126 ret = cm_alloc_msg(cm_id_priv, &msg);
2130 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2131 CM_MSG_RESPONSE_OTHER, service_timeout,
2132 private_data, private_data_len);
2133 ret = ib_post_send_mad(msg, NULL);
2136 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2142 cm_id_priv->service_timeout = service_timeout;
2143 cm_set_private_data(cm_id_priv, data, private_data_len);
2144 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2147 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2151 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2156 EXPORT_SYMBOL(ib_send_cm_mra);
2158 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2160 switch (cm_mra_get_msg_mraed(mra_msg)) {
2161 case CM_MSG_RESPONSE_REQ:
2162 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2163 case CM_MSG_RESPONSE_REP:
2164 case CM_MSG_RESPONSE_OTHER:
2165 return cm_acquire_id(mra_msg->remote_comm_id,
2166 mra_msg->local_comm_id);
2172 static int cm_mra_handler(struct cm_work *work)
2174 struct cm_id_private *cm_id_priv;
2175 struct cm_mra_msg *mra_msg;
2176 unsigned long flags;
2179 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2180 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2184 work->cm_event.private_data = &mra_msg->private_data;
2185 work->cm_event.param.mra_rcvd.service_timeout =
2186 cm_mra_get_service_timeout(mra_msg);
2187 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2188 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2190 spin_lock_irqsave(&cm_id_priv->lock, flags);
2191 switch (cm_id_priv->id.state) {
2192 case IB_CM_REQ_SENT:
2193 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2194 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2195 cm_id_priv->msg, timeout))
2197 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2199 case IB_CM_REP_SENT:
2200 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2201 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2202 cm_id_priv->msg, timeout))
2204 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2206 case IB_CM_ESTABLISHED:
2207 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2208 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2209 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2210 cm_id_priv->msg, timeout))
2212 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2218 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2219 cm_id_priv->id.state;
2220 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2222 list_add_tail(&work->list, &cm_id_priv->work_list);
2223 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2226 cm_process_work(cm_id_priv, work);
2228 cm_deref_id(cm_id_priv);
2231 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2232 cm_deref_id(cm_id_priv);
2236 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2237 struct cm_id_private *cm_id_priv,
2238 struct ib_sa_path_rec *alternate_path,
2239 const void *private_data,
2240 u8 private_data_len)
2242 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2243 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2244 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2245 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2246 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2247 /* todo: need remote CM response timeout */
2248 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2249 lap_msg->alt_local_lid = alternate_path->slid;
2250 lap_msg->alt_remote_lid = alternate_path->dlid;
2251 lap_msg->alt_local_gid = alternate_path->sgid;
2252 lap_msg->alt_remote_gid = alternate_path->dgid;
2253 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2254 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2255 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2256 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2257 cm_lap_set_sl(lap_msg, alternate_path->sl);
2258 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2259 cm_lap_set_local_ack_timeout(lap_msg,
2260 min(31, alternate_path->packet_life_time + 1));
2262 if (private_data && private_data_len)
2263 memcpy(lap_msg->private_data, private_data, private_data_len);
2266 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2267 struct ib_sa_path_rec *alternate_path,
2268 const void *private_data,
2269 u8 private_data_len)
2271 struct cm_id_private *cm_id_priv;
2272 struct ib_mad_send_buf *msg;
2273 unsigned long flags;
2276 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2279 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2280 spin_lock_irqsave(&cm_id_priv->lock, flags);
2281 if (cm_id->state != IB_CM_ESTABLISHED ||
2282 cm_id->lap_state != IB_CM_LAP_IDLE) {
2287 ret = cm_alloc_msg(cm_id_priv, &msg);
2291 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2292 alternate_path, private_data, private_data_len);
2293 msg->timeout_ms = cm_id_priv->timeout_ms;
2294 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2296 ret = ib_post_send_mad(msg, NULL);
2298 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2303 cm_id->lap_state = IB_CM_LAP_SENT;
2304 cm_id_priv->msg = msg;
2306 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2309 EXPORT_SYMBOL(ib_send_cm_lap);
2311 static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2312 struct cm_lap_msg *lap_msg)
2314 memset(path, 0, sizeof *path);
2315 path->dgid = lap_msg->alt_local_gid;
2316 path->sgid = lap_msg->alt_remote_gid;
2317 path->dlid = lap_msg->alt_local_lid;
2318 path->slid = lap_msg->alt_remote_lid;
2319 path->flow_label = cm_lap_get_flow_label(lap_msg);
2320 path->hop_limit = lap_msg->alt_hop_limit;
2321 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2322 path->reversible = 1;
2323 /* pkey is same as in REQ */
2324 path->sl = cm_lap_get_sl(lap_msg);
2325 path->mtu_selector = IB_SA_EQ;
2326 /* mtu is same as in REQ */
2327 path->rate_selector = IB_SA_EQ;
2328 path->rate = cm_lap_get_packet_rate(lap_msg);
2329 path->packet_life_time_selector = IB_SA_EQ;
2330 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2331 path->packet_life_time -= (path->packet_life_time > 0);
2334 static int cm_lap_handler(struct cm_work *work)
2336 struct cm_id_private *cm_id_priv;
2337 struct cm_lap_msg *lap_msg;
2338 struct ib_cm_lap_event_param *param;
2339 struct ib_mad_send_buf *msg = NULL;
2340 unsigned long flags;
2343 /* todo: verify LAP request and send reject APR if invalid. */
2344 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2345 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2346 lap_msg->local_comm_id);
2350 param = &work->cm_event.param.lap_rcvd;
2351 param->alternate_path = &work->path[0];
2352 cm_format_path_from_lap(param->alternate_path, lap_msg);
2353 work->cm_event.private_data = &lap_msg->private_data;
2355 spin_lock_irqsave(&cm_id_priv->lock, flags);
2356 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2359 switch (cm_id_priv->id.lap_state) {
2360 case IB_CM_LAP_IDLE:
2362 case IB_CM_MRA_LAP_SENT:
2363 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2366 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2367 CM_MSG_RESPONSE_OTHER,
2368 cm_id_priv->service_timeout,
2369 cm_id_priv->private_data,
2370 cm_id_priv->private_data_len);
2371 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2373 if (ib_post_send_mad(msg, NULL))
2380 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2381 cm_id_priv->tid = lap_msg->hdr.tid;
2382 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2384 list_add_tail(&work->list, &cm_id_priv->work_list);
2385 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2388 cm_process_work(cm_id_priv, work);
2390 cm_deref_id(cm_id_priv);
2393 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2394 deref: cm_deref_id(cm_id_priv);
2398 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2399 struct cm_id_private *cm_id_priv,
2400 enum ib_cm_apr_status status,
2403 const void *private_data,
2404 u8 private_data_len)
2406 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2407 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2408 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2409 apr_msg->ap_status = (u8) status;
2411 if (info && info_length) {
2412 apr_msg->info_length = info_length;
2413 memcpy(apr_msg->info, info, info_length);
2416 if (private_data && private_data_len)
2417 memcpy(apr_msg->private_data, private_data, private_data_len);
2420 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2421 enum ib_cm_apr_status status,
2424 const void *private_data,
2425 u8 private_data_len)
2427 struct cm_id_private *cm_id_priv;
2428 struct ib_mad_send_buf *msg;
2429 unsigned long flags;
2432 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2433 (info && info_length > IB_CM_APR_INFO_LENGTH))
2436 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2437 spin_lock_irqsave(&cm_id_priv->lock, flags);
2438 if (cm_id->state != IB_CM_ESTABLISHED ||
2439 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2440 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2445 ret = cm_alloc_msg(cm_id_priv, &msg);
2449 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2450 info, info_length, private_data, private_data_len);
2451 ret = ib_post_send_mad(msg, NULL);
2453 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2458 cm_id->lap_state = IB_CM_LAP_IDLE;
2459 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2462 EXPORT_SYMBOL(ib_send_cm_apr);
2464 static int cm_apr_handler(struct cm_work *work)
2466 struct cm_id_private *cm_id_priv;
2467 struct cm_apr_msg *apr_msg;
2468 unsigned long flags;
2471 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2472 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2473 apr_msg->local_comm_id);
2475 return -EINVAL; /* Unmatched reply. */
2477 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2478 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2479 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2480 work->cm_event.private_data = &apr_msg->private_data;
2482 spin_lock_irqsave(&cm_id_priv->lock, flags);
2483 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2484 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2485 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2486 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2489 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2490 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2491 cm_id_priv->msg = NULL;
2493 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2495 list_add_tail(&work->list, &cm_id_priv->work_list);
2496 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2499 cm_process_work(cm_id_priv, work);
2501 cm_deref_id(cm_id_priv);
2504 cm_deref_id(cm_id_priv);
2508 static int cm_timewait_handler(struct cm_work *work)
2510 struct cm_timewait_info *timewait_info;
2511 struct cm_id_private *cm_id_priv;
2512 unsigned long flags;
2515 timewait_info = (struct cm_timewait_info *)work;
2516 cm_cleanup_timewait(timewait_info);
2518 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2519 timewait_info->work.remote_id);
2523 spin_lock_irqsave(&cm_id_priv->lock, flags);
2524 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2525 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2526 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2529 cm_id_priv->id.state = IB_CM_IDLE;
2530 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2532 list_add_tail(&work->list, &cm_id_priv->work_list);
2533 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2536 cm_process_work(cm_id_priv, work);
2538 cm_deref_id(cm_id_priv);
2541 cm_deref_id(cm_id_priv);
2545 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2546 struct cm_id_private *cm_id_priv,
2547 struct ib_cm_sidr_req_param *param)
2549 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2550 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2551 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2552 sidr_req_msg->pkey = cpu_to_be16(param->pkey);
2553 sidr_req_msg->service_id = param->service_id;
2555 if (param->private_data && param->private_data_len)
2556 memcpy(sidr_req_msg->private_data, param->private_data,
2557 param->private_data_len);
2560 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2561 struct ib_cm_sidr_req_param *param)
2563 struct cm_id_private *cm_id_priv;
2564 struct ib_mad_send_buf *msg;
2565 unsigned long flags;
2568 if (!param->path || (param->private_data &&
2569 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2572 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2573 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2577 cm_id->service_id = param->service_id;
2578 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2579 cm_id_priv->timeout_ms = param->timeout_ms;
2580 cm_id_priv->max_cm_retries = param->max_cm_retries;
2581 ret = cm_alloc_msg(cm_id_priv, &msg);
2585 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2587 msg->timeout_ms = cm_id_priv->timeout_ms;
2588 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2590 spin_lock_irqsave(&cm_id_priv->lock, flags);
2591 if (cm_id->state == IB_CM_IDLE)
2592 ret = ib_post_send_mad(msg, NULL);
2597 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2601 cm_id->state = IB_CM_SIDR_REQ_SENT;
2602 cm_id_priv->msg = msg;
2603 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2607 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2609 static void cm_format_sidr_req_event(struct cm_work *work,
2610 struct ib_cm_id *listen_id)
2612 struct cm_sidr_req_msg *sidr_req_msg;
2613 struct ib_cm_sidr_req_event_param *param;
2615 sidr_req_msg = (struct cm_sidr_req_msg *)
2616 work->mad_recv_wc->recv_buf.mad;
2617 param = &work->cm_event.param.sidr_req_rcvd;
2618 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2619 param->listen_id = listen_id;
2620 param->port = work->port->port_num;
2621 work->cm_event.private_data = &sidr_req_msg->private_data;
2624 static int cm_sidr_req_handler(struct cm_work *work)
2626 struct ib_cm_id *cm_id;
2627 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2628 struct cm_sidr_req_msg *sidr_req_msg;
2630 unsigned long flags;
2632 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2634 return PTR_ERR(cm_id);
2635 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2637 /* Record SGID/SLID and request ID for lookup. */
2638 sidr_req_msg = (struct cm_sidr_req_msg *)
2639 work->mad_recv_wc->recv_buf.mad;
2640 wc = work->mad_recv_wc->wc;
2641 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2642 cm_id_priv->av.dgid.global.interface_id = 0;
2643 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2645 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2646 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2647 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2648 atomic_inc(&cm_id_priv->work_count);
2650 spin_lock_irqsave(&cm.lock, flags);
2651 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2652 if (cur_cm_id_priv) {
2653 spin_unlock_irqrestore(&cm.lock, flags);
2654 goto out; /* Duplicate message. */
2656 cur_cm_id_priv = cm_find_listen(cm_id->device,
2657 sidr_req_msg->service_id);
2658 if (!cur_cm_id_priv) {
2659 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2660 spin_unlock_irqrestore(&cm.lock, flags);
2661 /* todo: reply with no match */
2662 goto out; /* No match. */
2664 atomic_inc(&cur_cm_id_priv->refcount);
2665 spin_unlock_irqrestore(&cm.lock, flags);
2667 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2668 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2669 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2670 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2672 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2673 cm_process_work(cm_id_priv, work);
2674 cm_deref_id(cur_cm_id_priv);
2677 ib_destroy_cm_id(&cm_id_priv->id);
2681 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2682 struct cm_id_private *cm_id_priv,
2683 struct ib_cm_sidr_rep_param *param)
2685 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2687 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2688 sidr_rep_msg->status = param->status;
2689 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2690 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2691 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2693 if (param->info && param->info_length)
2694 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2696 if (param->private_data && param->private_data_len)
2697 memcpy(sidr_rep_msg->private_data, param->private_data,
2698 param->private_data_len);
2701 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2702 struct ib_cm_sidr_rep_param *param)
2704 struct cm_id_private *cm_id_priv;
2705 struct ib_mad_send_buf *msg;
2706 unsigned long flags;
2709 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2710 (param->private_data &&
2711 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2714 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2715 spin_lock_irqsave(&cm_id_priv->lock, flags);
2716 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2721 ret = cm_alloc_msg(cm_id_priv, &msg);
2725 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2727 ret = ib_post_send_mad(msg, NULL);
2729 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2733 cm_id->state = IB_CM_IDLE;
2734 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2736 spin_lock_irqsave(&cm.lock, flags);
2737 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2738 spin_unlock_irqrestore(&cm.lock, flags);
2741 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2744 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2746 static void cm_format_sidr_rep_event(struct cm_work *work)
2748 struct cm_sidr_rep_msg *sidr_rep_msg;
2749 struct ib_cm_sidr_rep_event_param *param;
2751 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2752 work->mad_recv_wc->recv_buf.mad;
2753 param = &work->cm_event.param.sidr_rep_rcvd;
2754 param->status = sidr_rep_msg->status;
2755 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2756 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2757 param->info = &sidr_rep_msg->info;
2758 param->info_len = sidr_rep_msg->info_length;
2759 work->cm_event.private_data = &sidr_rep_msg->private_data;
2762 static int cm_sidr_rep_handler(struct cm_work *work)
2764 struct cm_sidr_rep_msg *sidr_rep_msg;
2765 struct cm_id_private *cm_id_priv;
2766 unsigned long flags;
2768 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2769 work->mad_recv_wc->recv_buf.mad;
2770 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2772 return -EINVAL; /* Unmatched reply. */
2774 spin_lock_irqsave(&cm_id_priv->lock, flags);
2775 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2776 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2779 cm_id_priv->id.state = IB_CM_IDLE;
2780 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2781 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2783 cm_format_sidr_rep_event(work);
2784 cm_process_work(cm_id_priv, work);
2787 cm_deref_id(cm_id_priv);
2791 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2792 enum ib_wc_status wc_status)
2794 struct cm_id_private *cm_id_priv;
2795 struct ib_cm_event cm_event;
2796 enum ib_cm_state state;
2797 unsigned long flags;
2800 memset(&cm_event, 0, sizeof cm_event);
2801 cm_id_priv = msg->context[0];
2803 /* Discard old sends or ones without a response. */
2804 spin_lock_irqsave(&cm_id_priv->lock, flags);
2805 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2806 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2810 case IB_CM_REQ_SENT:
2811 case IB_CM_MRA_REQ_RCVD:
2812 cm_reset_to_idle(cm_id_priv);
2813 cm_event.event = IB_CM_REQ_ERROR;
2815 case IB_CM_REP_SENT:
2816 case IB_CM_MRA_REP_RCVD:
2817 cm_reset_to_idle(cm_id_priv);
2818 cm_event.event = IB_CM_REP_ERROR;
2820 case IB_CM_DREQ_SENT:
2821 cm_enter_timewait(cm_id_priv);
2822 cm_event.event = IB_CM_DREQ_ERROR;
2824 case IB_CM_SIDR_REQ_SENT:
2825 cm_id_priv->id.state = IB_CM_IDLE;
2826 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2831 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2832 cm_event.param.send_status = wc_status;
2834 /* No other events can occur on the cm_id at this point. */
2835 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2838 ib_destroy_cm_id(&cm_id_priv->id);
2841 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2845 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2846 struct ib_mad_send_wc *mad_send_wc)
2848 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2850 switch (mad_send_wc->status) {
2852 case IB_WC_WR_FLUSH_ERR:
2856 if (msg->context[0] && msg->context[1])
2857 cm_process_send_error(msg, mad_send_wc->status);
2864 static void cm_work_handler(void *data)
2866 struct cm_work *work = data;
2869 switch (work->cm_event.event) {
2870 case IB_CM_REQ_RECEIVED:
2871 ret = cm_req_handler(work);
2873 case IB_CM_MRA_RECEIVED:
2874 ret = cm_mra_handler(work);
2876 case IB_CM_REJ_RECEIVED:
2877 ret = cm_rej_handler(work);
2879 case IB_CM_REP_RECEIVED:
2880 ret = cm_rep_handler(work);
2882 case IB_CM_RTU_RECEIVED:
2883 ret = cm_rtu_handler(work);
2885 case IB_CM_USER_ESTABLISHED:
2886 ret = cm_establish_handler(work);
2888 case IB_CM_DREQ_RECEIVED:
2889 ret = cm_dreq_handler(work);
2891 case IB_CM_DREP_RECEIVED:
2892 ret = cm_drep_handler(work);
2894 case IB_CM_SIDR_REQ_RECEIVED:
2895 ret = cm_sidr_req_handler(work);
2897 case IB_CM_SIDR_REP_RECEIVED:
2898 ret = cm_sidr_rep_handler(work);
2900 case IB_CM_LAP_RECEIVED:
2901 ret = cm_lap_handler(work);
2903 case IB_CM_APR_RECEIVED:
2904 ret = cm_apr_handler(work);
2906 case IB_CM_TIMEWAIT_EXIT:
2907 ret = cm_timewait_handler(work);
2917 int ib_cm_establish(struct ib_cm_id *cm_id)
2919 struct cm_id_private *cm_id_priv;
2920 struct cm_work *work;
2921 unsigned long flags;
2924 work = kmalloc(sizeof *work, GFP_ATOMIC);
2928 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2929 spin_lock_irqsave(&cm_id_priv->lock, flags);
2930 switch (cm_id->state)
2932 case IB_CM_REP_SENT:
2933 case IB_CM_MRA_REP_RCVD:
2934 cm_id->state = IB_CM_ESTABLISHED;
2936 case IB_CM_ESTABLISHED:
2943 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2951 * The CM worker thread may try to destroy the cm_id before it
2952 * can execute this work item. To prevent potential deadlock,
2953 * we need to find the cm_id once we're in the context of the
2954 * worker thread, rather than holding a reference on it.
2956 INIT_WORK(&work->work, cm_work_handler, work);
2957 work->local_id = cm_id->local_id;
2958 work->remote_id = cm_id->remote_id;
2959 work->mad_recv_wc = NULL;
2960 work->cm_event.event = IB_CM_USER_ESTABLISHED;
2961 queue_work(cm.wq, &work->work);
2965 EXPORT_SYMBOL(ib_cm_establish);
2967 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
2968 struct ib_mad_recv_wc *mad_recv_wc)
2970 struct cm_work *work;
2971 enum ib_cm_event_type event;
2974 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
2975 case CM_REQ_ATTR_ID:
2976 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
2977 alt_local_lid != 0);
2978 event = IB_CM_REQ_RECEIVED;
2980 case CM_MRA_ATTR_ID:
2981 event = IB_CM_MRA_RECEIVED;
2983 case CM_REJ_ATTR_ID:
2984 event = IB_CM_REJ_RECEIVED;
2986 case CM_REP_ATTR_ID:
2987 event = IB_CM_REP_RECEIVED;
2989 case CM_RTU_ATTR_ID:
2990 event = IB_CM_RTU_RECEIVED;
2992 case CM_DREQ_ATTR_ID:
2993 event = IB_CM_DREQ_RECEIVED;
2995 case CM_DREP_ATTR_ID:
2996 event = IB_CM_DREP_RECEIVED;
2998 case CM_SIDR_REQ_ATTR_ID:
2999 event = IB_CM_SIDR_REQ_RECEIVED;
3001 case CM_SIDR_REP_ATTR_ID:
3002 event = IB_CM_SIDR_REP_RECEIVED;
3004 case CM_LAP_ATTR_ID:
3006 event = IB_CM_LAP_RECEIVED;
3008 case CM_APR_ATTR_ID:
3009 event = IB_CM_APR_RECEIVED;
3012 ib_free_recv_mad(mad_recv_wc);
3016 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3019 ib_free_recv_mad(mad_recv_wc);
3023 INIT_WORK(&work->work, cm_work_handler, work);
3024 work->cm_event.event = event;
3025 work->mad_recv_wc = mad_recv_wc;
3026 work->port = (struct cm_port *)mad_agent->context;
3027 queue_work(cm.wq, &work->work);
3030 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3031 struct ib_qp_attr *qp_attr,
3034 unsigned long flags;
3037 spin_lock_irqsave(&cm_id_priv->lock, flags);
3038 switch (cm_id_priv->id.state) {
3039 case IB_CM_REQ_SENT:
3040 case IB_CM_MRA_REQ_RCVD:
3041 case IB_CM_REQ_RCVD:
3042 case IB_CM_MRA_REQ_SENT:
3043 case IB_CM_REP_RCVD:
3044 case IB_CM_MRA_REP_SENT:
3045 case IB_CM_REP_SENT:
3046 case IB_CM_MRA_REP_RCVD:
3047 case IB_CM_ESTABLISHED:
3048 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3049 IB_QP_PKEY_INDEX | IB_QP_PORT;
3050 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
3051 IB_ACCESS_REMOTE_WRITE;
3052 if (cm_id_priv->responder_resources)
3053 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
3054 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3055 qp_attr->port_num = cm_id_priv->av.port->port_num;
3062 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3066 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3067 struct ib_qp_attr *qp_attr,
3070 unsigned long flags;
3073 spin_lock_irqsave(&cm_id_priv->lock, flags);
3074 switch (cm_id_priv->id.state) {
3075 case IB_CM_REQ_RCVD:
3076 case IB_CM_MRA_REQ_SENT:
3077 case IB_CM_REP_RCVD:
3078 case IB_CM_MRA_REP_SENT:
3079 case IB_CM_REP_SENT:
3080 case IB_CM_MRA_REP_RCVD:
3081 case IB_CM_ESTABLISHED:
3082 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3083 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3084 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3085 qp_attr->path_mtu = cm_id_priv->path_mtu;
3086 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3087 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3088 if (cm_id_priv->qp_type == IB_QPT_RC) {
3089 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3090 IB_QP_MIN_RNR_TIMER;
3091 qp_attr->max_dest_rd_atomic =
3092 cm_id_priv->responder_resources;
3093 qp_attr->min_rnr_timer = 0;
3095 if (cm_id_priv->alt_av.ah_attr.dlid) {
3096 *qp_attr_mask |= IB_QP_ALT_PATH;
3097 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3105 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3109 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3110 struct ib_qp_attr *qp_attr,
3113 unsigned long flags;
3116 spin_lock_irqsave(&cm_id_priv->lock, flags);
3117 switch (cm_id_priv->id.state) {
3118 case IB_CM_REP_RCVD:
3119 case IB_CM_MRA_REP_SENT:
3120 case IB_CM_REP_SENT:
3121 case IB_CM_MRA_REP_RCVD:
3122 case IB_CM_ESTABLISHED:
3123 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3124 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3125 if (cm_id_priv->qp_type == IB_QPT_RC) {
3126 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3128 IB_QP_MAX_QP_RD_ATOMIC;
3129 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3130 qp_attr->retry_cnt = cm_id_priv->retry_count;
3131 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3132 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3134 if (cm_id_priv->alt_av.ah_attr.dlid) {
3135 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3136 qp_attr->path_mig_state = IB_MIG_REARM;
3144 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3148 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3149 struct ib_qp_attr *qp_attr,
3152 struct cm_id_private *cm_id_priv;
3155 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3156 switch (qp_attr->qp_state) {
3158 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3161 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3164 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3172 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3174 static void cm_add_one(struct ib_device *device)
3176 struct cm_device *cm_dev;
3177 struct cm_port *port;
3178 struct ib_mad_reg_req reg_req = {
3179 .mgmt_class = IB_MGMT_CLASS_CM,
3180 .mgmt_class_version = IB_CM_CLASS_VERSION
3182 struct ib_port_modify port_modify = {
3183 .set_port_cap_mask = IB_PORT_CM_SUP
3185 unsigned long flags;
3189 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3190 device->phys_port_cnt, GFP_KERNEL);
3194 cm_dev->device = device;
3195 cm_dev->ca_guid = device->node_guid;
3197 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3198 for (i = 1; i <= device->phys_port_cnt; i++) {
3199 port = &cm_dev->port[i-1];
3200 port->cm_dev = cm_dev;
3202 port->mad_agent = ib_register_mad_agent(device, i,
3209 if (IS_ERR(port->mad_agent))
3212 ret = ib_modify_port(device, i, 0, &port_modify);
3216 ib_set_client_data(device, &cm_client, cm_dev);
3218 write_lock_irqsave(&cm.device_lock, flags);
3219 list_add_tail(&cm_dev->list, &cm.device_list);
3220 write_unlock_irqrestore(&cm.device_lock, flags);
3224 ib_unregister_mad_agent(port->mad_agent);
3226 port_modify.set_port_cap_mask = 0;
3227 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3229 port = &cm_dev->port[i-1];
3230 ib_modify_port(device, port->port_num, 0, &port_modify);
3231 ib_unregister_mad_agent(port->mad_agent);
3236 static void cm_remove_one(struct ib_device *device)
3238 struct cm_device *cm_dev;
3239 struct cm_port *port;
3240 struct ib_port_modify port_modify = {
3241 .clr_port_cap_mask = IB_PORT_CM_SUP
3243 unsigned long flags;
3246 cm_dev = ib_get_client_data(device, &cm_client);
3250 write_lock_irqsave(&cm.device_lock, flags);
3251 list_del(&cm_dev->list);
3252 write_unlock_irqrestore(&cm.device_lock, flags);
3254 for (i = 1; i <= device->phys_port_cnt; i++) {
3255 port = &cm_dev->port[i-1];
3256 ib_modify_port(device, port->port_num, 0, &port_modify);
3257 ib_unregister_mad_agent(port->mad_agent);
3262 static int __init ib_cm_init(void)
3266 memset(&cm, 0, sizeof cm);
3267 INIT_LIST_HEAD(&cm.device_list);
3268 rwlock_init(&cm.device_lock);
3269 spin_lock_init(&cm.lock);
3270 cm.listen_service_table = RB_ROOT;
3271 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3272 cm.remote_id_table = RB_ROOT;
3273 cm.remote_qp_table = RB_ROOT;
3274 cm.remote_sidr_table = RB_ROOT;
3275 idr_init(&cm.local_id_table);
3276 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3278 cm.wq = create_workqueue("ib_cm");
3282 ret = ib_register_client(&cm_client);
3288 destroy_workqueue(cm.wq);
3292 static void __exit ib_cm_cleanup(void)
3294 flush_workqueue(cm.wq);
3295 destroy_workqueue(cm.wq);
3296 ib_unregister_client(&cm_client);
3297 idr_destroy(&cm.local_id_table);
3300 module_init(ib_cm_init);
3301 module_exit(ib_cm_cleanup);