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 $
37 #include <linux/dma-mapping.h>
38 #include <linux/err.h>
39 #include <linux/idr.h>
40 #include <linux/interrupt.h>
41 #include <linux/pci.h>
42 #include <linux/rbtree.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
46 #include <rdma/ib_cache.h>
47 #include <rdma/ib_cm.h>
50 MODULE_AUTHOR("Sean Hefty");
51 MODULE_DESCRIPTION("InfiniBand CM");
52 MODULE_LICENSE("Dual BSD/GPL");
54 static void cm_add_one(struct ib_device *device);
55 static void cm_remove_one(struct ib_device *device);
57 static struct ib_client cm_client = {
60 .remove = cm_remove_one
65 struct list_head device_list;
67 struct rb_root listen_service_table;
68 u64 listen_service_id;
69 /* struct rb_root peer_service_table; todo: fix peer to peer */
70 struct rb_root remote_qp_table;
71 struct rb_root remote_id_table;
72 struct rb_root remote_sidr_table;
73 struct idr local_id_table;
74 struct workqueue_struct *wq;
78 struct cm_device *cm_dev;
79 struct ib_mad_agent *mad_agent;
84 struct list_head list;
85 struct ib_device *device;
87 struct cm_port port[0];
93 struct ib_ah_attr ah_attr;
99 struct work_struct work;
100 struct list_head list;
101 struct cm_port *port;
102 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
103 __be32 local_id; /* Established / timewait */
105 struct ib_cm_event cm_event;
106 struct ib_sa_path_rec path[0];
109 struct cm_timewait_info {
110 struct cm_work work; /* Must be first. */
111 struct rb_node remote_qp_node;
112 struct rb_node remote_id_node;
113 __be64 remote_ca_guid;
115 u8 inserted_remote_qp;
116 u8 inserted_remote_id;
119 struct cm_id_private {
122 struct rb_node service_node;
123 struct rb_node sidr_id_node;
125 wait_queue_head_t wait;
128 struct ib_mad_send_buf *msg;
129 struct cm_timewait_info *timewait_info;
130 /* todo: use alternate port on send failure */
141 enum ib_mtu path_mtu;
145 u8 responder_resources;
147 u8 local_ack_timeout;
152 struct list_head work_list;
156 static void cm_work_handler(void *data);
158 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
160 if (atomic_dec_and_test(&cm_id_priv->refcount))
161 wake_up(&cm_id_priv->wait);
164 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
165 struct ib_mad_send_buf **msg)
167 struct ib_mad_agent *mad_agent;
168 struct ib_mad_send_buf *m;
171 mad_agent = cm_id_priv->av.port->mad_agent;
172 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
176 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
177 cm_id_priv->av.pkey_index,
178 ah, 0, sizeof(struct ib_mad_hdr),
179 sizeof(struct ib_mad)-sizeof(struct ib_mad_hdr),
186 /* Timeout set by caller if response is expected. */
187 m->send_wr.wr.ud.retries = cm_id_priv->max_cm_retries;
189 atomic_inc(&cm_id_priv->refcount);
190 m->context[0] = cm_id_priv;
195 static int cm_alloc_response_msg(struct cm_port *port,
196 struct ib_mad_recv_wc *mad_recv_wc,
197 struct ib_mad_send_buf **msg)
199 struct ib_mad_send_buf *m;
202 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
203 mad_recv_wc->recv_buf.grh, port->port_num);
207 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
208 ah, 0, sizeof(struct ib_mad_hdr),
209 sizeof(struct ib_mad)-sizeof(struct ib_mad_hdr),
219 static void cm_free_msg(struct ib_mad_send_buf *msg)
221 ib_destroy_ah(msg->send_wr.wr.ud.ah);
223 cm_deref_id(msg->context[0]);
224 ib_free_send_mad(msg);
227 static void * cm_copy_private_data(const void *private_data,
232 if (!private_data || !private_data_len)
235 data = kmalloc(private_data_len, GFP_KERNEL);
237 return ERR_PTR(-ENOMEM);
239 memcpy(data, private_data, private_data_len);
243 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
244 void *private_data, u8 private_data_len)
246 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
247 kfree(cm_id_priv->private_data);
249 cm_id_priv->private_data = private_data;
250 cm_id_priv->private_data_len = private_data_len;
253 static void cm_set_ah_attr(struct ib_ah_attr *ah_attr, u8 port_num,
254 u16 dlid, u8 sl, u16 src_path_bits)
256 memset(ah_attr, 0, sizeof ah_attr);
257 ah_attr->dlid = dlid;
259 ah_attr->src_path_bits = src_path_bits;
260 ah_attr->port_num = port_num;
263 static void cm_init_av_for_response(struct cm_port *port,
264 struct ib_wc *wc, struct cm_av *av)
267 av->pkey_index = wc->pkey_index;
268 cm_set_ah_attr(&av->ah_attr, port->port_num, wc->slid,
269 wc->sl, wc->dlid_path_bits);
272 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
274 struct cm_device *cm_dev;
275 struct cm_port *port = NULL;
280 read_lock_irqsave(&cm.device_lock, flags);
281 list_for_each_entry(cm_dev, &cm.device_list, list) {
282 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
284 port = &cm_dev->port[p-1];
288 read_unlock_irqrestore(&cm.device_lock, flags);
293 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
294 be16_to_cpu(path->pkey), &av->pkey_index);
299 cm_set_ah_attr(&av->ah_attr, av->port->port_num,
300 be16_to_cpu(path->dlid), path->sl,
301 be16_to_cpu(path->slid) & 0x7F);
302 av->packet_life_time = path->packet_life_time;
306 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
312 spin_lock_irqsave(&cm.lock, flags);
313 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, 1,
314 (__force int *) &cm_id_priv->id.local_id);
315 spin_unlock_irqrestore(&cm.lock, flags);
316 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
320 static void cm_free_id(__be32 local_id)
324 spin_lock_irqsave(&cm.lock, flags);
325 idr_remove(&cm.local_id_table, (__force int) local_id);
326 spin_unlock_irqrestore(&cm.lock, flags);
329 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
331 struct cm_id_private *cm_id_priv;
333 cm_id_priv = idr_find(&cm.local_id_table, (__force int) local_id);
335 if (cm_id_priv->id.remote_id == remote_id)
336 atomic_inc(&cm_id_priv->refcount);
344 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
346 struct cm_id_private *cm_id_priv;
349 spin_lock_irqsave(&cm.lock, flags);
350 cm_id_priv = cm_get_id(local_id, remote_id);
351 spin_unlock_irqrestore(&cm.lock, flags);
356 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
358 struct rb_node **link = &cm.listen_service_table.rb_node;
359 struct rb_node *parent = NULL;
360 struct cm_id_private *cur_cm_id_priv;
361 __be64 service_id = cm_id_priv->id.service_id;
362 __be64 service_mask = cm_id_priv->id.service_mask;
366 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
368 if ((cur_cm_id_priv->id.service_mask & service_id) ==
369 (service_mask & cur_cm_id_priv->id.service_id))
371 if (service_id < cur_cm_id_priv->id.service_id)
372 link = &(*link)->rb_left;
374 link = &(*link)->rb_right;
376 rb_link_node(&cm_id_priv->service_node, parent, link);
377 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
381 static struct cm_id_private * cm_find_listen(__be64 service_id)
383 struct rb_node *node = cm.listen_service_table.rb_node;
384 struct cm_id_private *cm_id_priv;
387 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
388 if ((cm_id_priv->id.service_mask & service_id) ==
389 (cm_id_priv->id.service_mask & cm_id_priv->id.service_id))
391 if (service_id < cm_id_priv->id.service_id)
392 node = node->rb_left;
394 node = node->rb_right;
399 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
402 struct rb_node **link = &cm.remote_id_table.rb_node;
403 struct rb_node *parent = NULL;
404 struct cm_timewait_info *cur_timewait_info;
405 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
406 __be32 remote_id = timewait_info->work.remote_id;
410 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
412 if (remote_id < cur_timewait_info->work.remote_id)
413 link = &(*link)->rb_left;
414 else if (remote_id > cur_timewait_info->work.remote_id)
415 link = &(*link)->rb_right;
416 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
417 link = &(*link)->rb_left;
418 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
419 link = &(*link)->rb_right;
421 return cur_timewait_info;
423 timewait_info->inserted_remote_id = 1;
424 rb_link_node(&timewait_info->remote_id_node, parent, link);
425 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
429 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
432 struct rb_node *node = cm.remote_id_table.rb_node;
433 struct cm_timewait_info *timewait_info;
436 timewait_info = rb_entry(node, struct cm_timewait_info,
438 if (remote_id < timewait_info->work.remote_id)
439 node = node->rb_left;
440 else if (remote_id > timewait_info->work.remote_id)
441 node = node->rb_right;
442 else if (remote_ca_guid < timewait_info->remote_ca_guid)
443 node = node->rb_left;
444 else if (remote_ca_guid > timewait_info->remote_ca_guid)
445 node = node->rb_right;
447 return timewait_info;
452 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
455 struct rb_node **link = &cm.remote_qp_table.rb_node;
456 struct rb_node *parent = NULL;
457 struct cm_timewait_info *cur_timewait_info;
458 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
459 __be32 remote_qpn = timewait_info->remote_qpn;
463 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
465 if (remote_qpn < cur_timewait_info->remote_qpn)
466 link = &(*link)->rb_left;
467 else if (remote_qpn > cur_timewait_info->remote_qpn)
468 link = &(*link)->rb_right;
469 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
470 link = &(*link)->rb_left;
471 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
472 link = &(*link)->rb_right;
474 return cur_timewait_info;
476 timewait_info->inserted_remote_qp = 1;
477 rb_link_node(&timewait_info->remote_qp_node, parent, link);
478 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
482 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
485 struct rb_node **link = &cm.remote_sidr_table.rb_node;
486 struct rb_node *parent = NULL;
487 struct cm_id_private *cur_cm_id_priv;
488 union ib_gid *port_gid = &cm_id_priv->av.dgid;
489 __be32 remote_id = cm_id_priv->id.remote_id;
493 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
495 if (remote_id < cur_cm_id_priv->id.remote_id)
496 link = &(*link)->rb_left;
497 else if (remote_id > cur_cm_id_priv->id.remote_id)
498 link = &(*link)->rb_right;
501 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
504 link = &(*link)->rb_left;
506 link = &(*link)->rb_right;
508 return cur_cm_id_priv;
511 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
512 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
516 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
517 enum ib_cm_sidr_status status)
519 struct ib_cm_sidr_rep_param param;
521 memset(¶m, 0, sizeof param);
522 param.status = status;
523 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
526 struct ib_cm_id *ib_create_cm_id(ib_cm_handler cm_handler,
529 struct cm_id_private *cm_id_priv;
532 cm_id_priv = kmalloc(sizeof *cm_id_priv, GFP_KERNEL);
534 return ERR_PTR(-ENOMEM);
536 memset(cm_id_priv, 0, sizeof *cm_id_priv);
537 cm_id_priv->id.state = IB_CM_IDLE;
538 cm_id_priv->id.cm_handler = cm_handler;
539 cm_id_priv->id.context = context;
540 cm_id_priv->id.remote_cm_qpn = 1;
541 ret = cm_alloc_id(cm_id_priv);
545 spin_lock_init(&cm_id_priv->lock);
546 init_waitqueue_head(&cm_id_priv->wait);
547 INIT_LIST_HEAD(&cm_id_priv->work_list);
548 atomic_set(&cm_id_priv->work_count, -1);
549 atomic_set(&cm_id_priv->refcount, 1);
550 return &cm_id_priv->id;
554 return ERR_PTR(-ENOMEM);
556 EXPORT_SYMBOL(ib_create_cm_id);
558 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
560 struct cm_work *work;
562 if (list_empty(&cm_id_priv->work_list))
565 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
566 list_del(&work->list);
570 static void cm_free_work(struct cm_work *work)
572 if (work->mad_recv_wc)
573 ib_free_recv_mad(work->mad_recv_wc);
577 static inline int cm_convert_to_ms(int iba_time)
579 /* approximate conversion to ms from 4.096us x 2^iba_time */
580 return 1 << max(iba_time - 8, 0);
583 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
587 if (!timewait_info->inserted_remote_id &&
588 !timewait_info->inserted_remote_qp)
591 spin_lock_irqsave(&cm.lock, flags);
592 if (timewait_info->inserted_remote_id) {
593 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
594 timewait_info->inserted_remote_id = 0;
597 if (timewait_info->inserted_remote_qp) {
598 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
599 timewait_info->inserted_remote_qp = 0;
601 spin_unlock_irqrestore(&cm.lock, flags);
604 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
606 struct cm_timewait_info *timewait_info;
608 timewait_info = kmalloc(sizeof *timewait_info, GFP_KERNEL);
610 return ERR_PTR(-ENOMEM);
611 memset(timewait_info, 0, sizeof *timewait_info);
613 timewait_info->work.local_id = local_id;
614 INIT_WORK(&timewait_info->work.work, cm_work_handler,
615 &timewait_info->work);
616 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
617 return timewait_info;
620 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
625 * The cm_id could be destroyed by the user before we exit timewait.
626 * To protect against this, we search for the cm_id after exiting
627 * timewait before notifying the user that we've exited timewait.
629 cm_id_priv->id.state = IB_CM_TIMEWAIT;
630 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
631 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
632 msecs_to_jiffies(wait_time));
633 cm_id_priv->timewait_info = NULL;
636 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
638 cm_id_priv->id.state = IB_CM_IDLE;
639 if (cm_id_priv->timewait_info) {
640 cm_cleanup_timewait(cm_id_priv->timewait_info);
641 kfree(cm_id_priv->timewait_info);
642 cm_id_priv->timewait_info = NULL;
646 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
648 struct cm_id_private *cm_id_priv;
649 struct cm_work *work;
652 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
654 spin_lock_irqsave(&cm_id_priv->lock, flags);
655 switch (cm_id->state) {
657 cm_id->state = IB_CM_IDLE;
658 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
659 spin_lock_irqsave(&cm.lock, flags);
660 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
661 spin_unlock_irqrestore(&cm.lock, flags);
663 case IB_CM_SIDR_REQ_SENT:
664 cm_id->state = IB_CM_IDLE;
665 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
666 (unsigned long) cm_id_priv->msg);
667 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
669 case IB_CM_SIDR_REQ_RCVD:
670 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
671 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
674 case IB_CM_MRA_REQ_RCVD:
676 case IB_CM_MRA_REP_RCVD:
677 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
678 (unsigned long) cm_id_priv->msg);
681 case IB_CM_MRA_REQ_SENT:
683 case IB_CM_MRA_REP_SENT:
684 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
685 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
686 &cm_id_priv->av.port->cm_dev->ca_guid,
687 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
690 case IB_CM_ESTABLISHED:
691 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
692 ib_send_cm_dreq(cm_id, NULL, 0);
694 case IB_CM_DREQ_SENT:
695 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
696 (unsigned long) cm_id_priv->msg);
697 cm_enter_timewait(cm_id_priv);
698 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
700 case IB_CM_DREQ_RCVD:
701 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
702 ib_send_cm_drep(cm_id, NULL, 0);
705 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
709 cm_free_id(cm_id->local_id);
710 atomic_dec(&cm_id_priv->refcount);
711 wait_event(cm_id_priv->wait, !atomic_read(&cm_id_priv->refcount));
712 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
714 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
715 kfree(cm_id_priv->private_data);
718 EXPORT_SYMBOL(ib_destroy_cm_id);
720 int ib_cm_listen(struct ib_cm_id *cm_id,
724 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
728 service_mask = service_mask ? service_mask :
729 __constant_cpu_to_be64(~0ULL);
730 service_id &= service_mask;
731 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
732 (service_id != IB_CM_ASSIGN_SERVICE_ID))
735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
736 BUG_ON(cm_id->state != IB_CM_IDLE);
738 cm_id->state = IB_CM_LISTEN;
740 spin_lock_irqsave(&cm.lock, flags);
741 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
742 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
743 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
745 cm_id->service_id = service_id;
746 cm_id->service_mask = service_mask;
748 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
749 spin_unlock_irqrestore(&cm.lock, flags);
751 if (cur_cm_id_priv) {
752 cm_id->state = IB_CM_IDLE;
757 EXPORT_SYMBOL(ib_cm_listen);
759 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
760 enum cm_msg_sequence msg_seq)
764 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
765 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
767 return cpu_to_be64(hi_tid | low_tid);
770 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
771 __be16 attr_id, __be64 tid)
773 hdr->base_version = IB_MGMT_BASE_VERSION;
774 hdr->mgmt_class = IB_MGMT_CLASS_CM;
775 hdr->class_version = IB_CM_CLASS_VERSION;
776 hdr->method = IB_MGMT_METHOD_SEND;
777 hdr->attr_id = attr_id;
781 static void cm_format_req(struct cm_req_msg *req_msg,
782 struct cm_id_private *cm_id_priv,
783 struct ib_cm_req_param *param)
785 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
786 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
788 req_msg->local_comm_id = cm_id_priv->id.local_id;
789 req_msg->service_id = param->service_id;
790 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
791 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
792 cm_req_set_resp_res(req_msg, param->responder_resources);
793 cm_req_set_init_depth(req_msg, param->initiator_depth);
794 cm_req_set_remote_resp_timeout(req_msg,
795 param->remote_cm_response_timeout);
796 cm_req_set_qp_type(req_msg, param->qp_type);
797 cm_req_set_flow_ctrl(req_msg, param->flow_control);
798 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
799 cm_req_set_local_resp_timeout(req_msg,
800 param->local_cm_response_timeout);
801 cm_req_set_retry_count(req_msg, param->retry_count);
802 req_msg->pkey = param->primary_path->pkey;
803 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
804 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
805 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
806 cm_req_set_srq(req_msg, param->srq);
808 req_msg->primary_local_lid = param->primary_path->slid;
809 req_msg->primary_remote_lid = param->primary_path->dlid;
810 req_msg->primary_local_gid = param->primary_path->sgid;
811 req_msg->primary_remote_gid = param->primary_path->dgid;
812 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
813 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
814 req_msg->primary_traffic_class = param->primary_path->traffic_class;
815 req_msg->primary_hop_limit = param->primary_path->hop_limit;
816 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
817 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
818 cm_req_set_primary_local_ack_timeout(req_msg,
819 min(31, param->primary_path->packet_life_time + 1));
821 if (param->alternate_path) {
822 req_msg->alt_local_lid = param->alternate_path->slid;
823 req_msg->alt_remote_lid = param->alternate_path->dlid;
824 req_msg->alt_local_gid = param->alternate_path->sgid;
825 req_msg->alt_remote_gid = param->alternate_path->dgid;
826 cm_req_set_alt_flow_label(req_msg,
827 param->alternate_path->flow_label);
828 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
829 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
830 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
831 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
832 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
833 cm_req_set_alt_local_ack_timeout(req_msg,
834 min(31, param->alternate_path->packet_life_time + 1));
837 if (param->private_data && param->private_data_len)
838 memcpy(req_msg->private_data, param->private_data,
839 param->private_data_len);
842 static inline int cm_validate_req_param(struct ib_cm_req_param *param)
844 /* peer-to-peer not supported */
845 if (param->peer_to_peer)
848 if (!param->primary_path)
851 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
854 if (param->private_data &&
855 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
858 if (param->alternate_path &&
859 (param->alternate_path->pkey != param->primary_path->pkey ||
860 param->alternate_path->mtu != param->primary_path->mtu))
866 int ib_send_cm_req(struct ib_cm_id *cm_id,
867 struct ib_cm_req_param *param)
869 struct cm_id_private *cm_id_priv;
870 struct ib_send_wr *bad_send_wr;
871 struct cm_req_msg *req_msg;
875 ret = cm_validate_req_param(param);
879 /* Verify that we're not in timewait. */
880 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
881 spin_lock_irqsave(&cm_id_priv->lock, flags);
882 if (cm_id->state != IB_CM_IDLE) {
883 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
887 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
889 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
891 if (IS_ERR(cm_id_priv->timewait_info))
894 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
897 if (param->alternate_path) {
898 ret = cm_init_av_by_path(param->alternate_path,
899 &cm_id_priv->alt_av);
903 cm_id->service_id = param->service_id;
904 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
905 cm_id_priv->timeout_ms = cm_convert_to_ms(
906 param->primary_path->packet_life_time) * 2 +
908 param->remote_cm_response_timeout);
909 cm_id_priv->max_cm_retries = param->max_cm_retries;
910 cm_id_priv->initiator_depth = param->initiator_depth;
911 cm_id_priv->responder_resources = param->responder_resources;
912 cm_id_priv->retry_count = param->retry_count;
913 cm_id_priv->path_mtu = param->primary_path->mtu;
915 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
919 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
920 cm_format_req(req_msg, cm_id_priv, param);
921 cm_id_priv->tid = req_msg->hdr.tid;
922 cm_id_priv->msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
923 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
925 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
926 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
927 cm_id_priv->local_ack_timeout =
928 cm_req_get_primary_local_ack_timeout(req_msg);
930 spin_lock_irqsave(&cm_id_priv->lock, flags);
931 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
932 &cm_id_priv->msg->send_wr, &bad_send_wr);
934 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
937 BUG_ON(cm_id->state != IB_CM_IDLE);
938 cm_id->state = IB_CM_REQ_SENT;
939 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
942 error2: cm_free_msg(cm_id_priv->msg);
943 error1: kfree(cm_id_priv->timewait_info);
946 EXPORT_SYMBOL(ib_send_cm_req);
948 static int cm_issue_rej(struct cm_port *port,
949 struct ib_mad_recv_wc *mad_recv_wc,
950 enum ib_cm_rej_reason reason,
951 enum cm_msg_response msg_rejected,
952 void *ari, u8 ari_length)
954 struct ib_mad_send_buf *msg = NULL;
955 struct ib_send_wr *bad_send_wr;
956 struct cm_rej_msg *rej_msg, *rcv_msg;
959 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
963 /* We just need common CM header information. Cast to any message. */
964 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
965 rej_msg = (struct cm_rej_msg *) msg->mad;
967 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
968 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
969 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
970 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
971 rej_msg->reason = cpu_to_be16(reason);
973 if (ari && ari_length) {
974 cm_rej_set_reject_info_len(rej_msg, ari_length);
975 memcpy(rej_msg->ari, ari, ari_length);
978 ret = ib_post_send_mad(port->mad_agent, &msg->send_wr, &bad_send_wr);
985 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
986 __be32 local_qpn, __be32 remote_qpn)
988 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
989 ((local_ca_guid == remote_ca_guid) &&
990 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
993 static inline void cm_format_paths_from_req(struct cm_req_msg *req_msg,
994 struct ib_sa_path_rec *primary_path,
995 struct ib_sa_path_rec *alt_path)
997 memset(primary_path, 0, sizeof *primary_path);
998 primary_path->dgid = req_msg->primary_local_gid;
999 primary_path->sgid = req_msg->primary_remote_gid;
1000 primary_path->dlid = req_msg->primary_local_lid;
1001 primary_path->slid = req_msg->primary_remote_lid;
1002 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1003 primary_path->hop_limit = req_msg->primary_hop_limit;
1004 primary_path->traffic_class = req_msg->primary_traffic_class;
1005 primary_path->reversible = 1;
1006 primary_path->pkey = req_msg->pkey;
1007 primary_path->sl = cm_req_get_primary_sl(req_msg);
1008 primary_path->mtu_selector = IB_SA_EQ;
1009 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1010 primary_path->rate_selector = IB_SA_EQ;
1011 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1012 primary_path->packet_life_time_selector = IB_SA_EQ;
1013 primary_path->packet_life_time =
1014 cm_req_get_primary_local_ack_timeout(req_msg);
1015 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1017 if (req_msg->alt_local_lid) {
1018 memset(alt_path, 0, sizeof *alt_path);
1019 alt_path->dgid = req_msg->alt_local_gid;
1020 alt_path->sgid = req_msg->alt_remote_gid;
1021 alt_path->dlid = req_msg->alt_local_lid;
1022 alt_path->slid = req_msg->alt_remote_lid;
1023 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1024 alt_path->hop_limit = req_msg->alt_hop_limit;
1025 alt_path->traffic_class = req_msg->alt_traffic_class;
1026 alt_path->reversible = 1;
1027 alt_path->pkey = req_msg->pkey;
1028 alt_path->sl = cm_req_get_alt_sl(req_msg);
1029 alt_path->mtu_selector = IB_SA_EQ;
1030 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1031 alt_path->rate_selector = IB_SA_EQ;
1032 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1033 alt_path->packet_life_time_selector = IB_SA_EQ;
1034 alt_path->packet_life_time =
1035 cm_req_get_alt_local_ack_timeout(req_msg);
1036 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1040 static void cm_format_req_event(struct cm_work *work,
1041 struct cm_id_private *cm_id_priv,
1042 struct ib_cm_id *listen_id)
1044 struct cm_req_msg *req_msg;
1045 struct ib_cm_req_event_param *param;
1047 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1048 param = &work->cm_event.param.req_rcvd;
1049 param->listen_id = listen_id;
1050 param->device = cm_id_priv->av.port->mad_agent->device;
1051 param->port = cm_id_priv->av.port->port_num;
1052 param->primary_path = &work->path[0];
1053 if (req_msg->alt_local_lid)
1054 param->alternate_path = &work->path[1];
1056 param->alternate_path = NULL;
1057 param->remote_ca_guid = req_msg->local_ca_guid;
1058 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1059 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1060 param->qp_type = cm_req_get_qp_type(req_msg);
1061 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1062 param->responder_resources = cm_req_get_init_depth(req_msg);
1063 param->initiator_depth = cm_req_get_resp_res(req_msg);
1064 param->local_cm_response_timeout =
1065 cm_req_get_remote_resp_timeout(req_msg);
1066 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1067 param->remote_cm_response_timeout =
1068 cm_req_get_local_resp_timeout(req_msg);
1069 param->retry_count = cm_req_get_retry_count(req_msg);
1070 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1071 param->srq = cm_req_get_srq(req_msg);
1072 work->cm_event.private_data = &req_msg->private_data;
1075 static void cm_process_work(struct cm_id_private *cm_id_priv,
1076 struct cm_work *work)
1078 unsigned long flags;
1081 /* We will typically only have the current event to report. */
1082 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1085 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1086 spin_lock_irqsave(&cm_id_priv->lock, flags);
1087 work = cm_dequeue_work(cm_id_priv);
1088 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1090 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1094 cm_deref_id(cm_id_priv);
1096 ib_destroy_cm_id(&cm_id_priv->id);
1099 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1100 struct cm_id_private *cm_id_priv,
1101 enum cm_msg_response msg_mraed, u8 service_timeout,
1102 const void *private_data, u8 private_data_len)
1104 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1105 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1106 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1107 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1108 cm_mra_set_service_timeout(mra_msg, service_timeout);
1110 if (private_data && private_data_len)
1111 memcpy(mra_msg->private_data, private_data, private_data_len);
1114 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1115 struct cm_id_private *cm_id_priv,
1116 enum ib_cm_rej_reason reason,
1119 const void *private_data,
1120 u8 private_data_len)
1122 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1123 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1125 switch(cm_id_priv->id.state) {
1126 case IB_CM_REQ_RCVD:
1127 rej_msg->local_comm_id = 0;
1128 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1130 case IB_CM_MRA_REQ_SENT:
1131 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1132 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1134 case IB_CM_REP_RCVD:
1135 case IB_CM_MRA_REP_SENT:
1136 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1137 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1140 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1141 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1145 rej_msg->reason = cpu_to_be16(reason);
1146 if (ari && ari_length) {
1147 cm_rej_set_reject_info_len(rej_msg, ari_length);
1148 memcpy(rej_msg->ari, ari, ari_length);
1151 if (private_data && private_data_len)
1152 memcpy(rej_msg->private_data, private_data, private_data_len);
1155 static void cm_dup_req_handler(struct cm_work *work,
1156 struct cm_id_private *cm_id_priv)
1158 struct ib_mad_send_buf *msg = NULL;
1159 struct ib_send_wr *bad_send_wr;
1160 unsigned long flags;
1163 /* Quick state check to discard duplicate REQs. */
1164 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1167 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1171 spin_lock_irqsave(&cm_id_priv->lock, flags);
1172 switch (cm_id_priv->id.state) {
1173 case IB_CM_MRA_REQ_SENT:
1174 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1175 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1176 cm_id_priv->private_data,
1177 cm_id_priv->private_data_len);
1179 case IB_CM_TIMEWAIT:
1180 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1181 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1186 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1188 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1194 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1195 free: cm_free_msg(msg);
1198 static struct cm_id_private * cm_match_req(struct cm_work *work,
1199 struct cm_id_private *cm_id_priv)
1201 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1202 struct cm_timewait_info *timewait_info;
1203 struct cm_req_msg *req_msg;
1204 unsigned long flags;
1206 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1208 /* Check for duplicate REQ and stale connections. */
1209 spin_lock_irqsave(&cm.lock, flags);
1210 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1212 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1214 if (timewait_info) {
1215 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1216 timewait_info->work.remote_id);
1217 spin_unlock_irqrestore(&cm.lock, flags);
1218 if (cur_cm_id_priv) {
1219 cm_dup_req_handler(work, cur_cm_id_priv);
1220 cm_deref_id(cur_cm_id_priv);
1222 cm_issue_rej(work->port, work->mad_recv_wc,
1223 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1228 /* Find matching listen request. */
1229 listen_cm_id_priv = cm_find_listen(req_msg->service_id);
1230 if (!listen_cm_id_priv) {
1231 spin_unlock_irqrestore(&cm.lock, flags);
1232 cm_issue_rej(work->port, work->mad_recv_wc,
1233 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1237 atomic_inc(&listen_cm_id_priv->refcount);
1238 atomic_inc(&cm_id_priv->refcount);
1239 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1240 atomic_inc(&cm_id_priv->work_count);
1241 spin_unlock_irqrestore(&cm.lock, flags);
1242 return listen_cm_id_priv;
1244 error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1248 static int cm_req_handler(struct cm_work *work)
1250 struct ib_cm_id *cm_id;
1251 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1252 struct cm_req_msg *req_msg;
1255 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1257 cm_id = ib_create_cm_id(NULL, NULL);
1259 return PTR_ERR(cm_id);
1261 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1262 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1263 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1265 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1267 if (IS_ERR(cm_id_priv->timewait_info)) {
1268 ret = PTR_ERR(cm_id_priv->timewait_info);
1271 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1272 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1273 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1275 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1276 if (!listen_cm_id_priv) {
1281 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1282 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1283 cm_id_priv->id.service_id = req_msg->service_id;
1284 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1286 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1287 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1290 if (req_msg->alt_local_lid) {
1291 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1295 cm_id_priv->tid = req_msg->hdr.tid;
1296 cm_id_priv->timeout_ms = cm_convert_to_ms(
1297 cm_req_get_local_resp_timeout(req_msg));
1298 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1299 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1300 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1301 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1302 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1303 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1304 cm_id_priv->local_ack_timeout =
1305 cm_req_get_primary_local_ack_timeout(req_msg);
1306 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1307 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1309 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1310 cm_process_work(cm_id_priv, work);
1311 cm_deref_id(listen_cm_id_priv);
1314 error3: atomic_dec(&cm_id_priv->refcount);
1315 cm_deref_id(listen_cm_id_priv);
1316 cm_cleanup_timewait(cm_id_priv->timewait_info);
1317 error2: kfree(cm_id_priv->timewait_info);
1318 cm_id_priv->timewait_info = NULL;
1319 error1: ib_destroy_cm_id(&cm_id_priv->id);
1323 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1324 struct cm_id_private *cm_id_priv,
1325 struct ib_cm_rep_param *param)
1327 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1328 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1329 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1330 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1331 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1332 rep_msg->resp_resources = param->responder_resources;
1333 rep_msg->initiator_depth = param->initiator_depth;
1334 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1335 cm_rep_set_failover(rep_msg, param->failover_accepted);
1336 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1337 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1338 cm_rep_set_srq(rep_msg, param->srq);
1339 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1341 if (param->private_data && param->private_data_len)
1342 memcpy(rep_msg->private_data, param->private_data,
1343 param->private_data_len);
1346 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1347 struct ib_cm_rep_param *param)
1349 struct cm_id_private *cm_id_priv;
1350 struct ib_mad_send_buf *msg;
1351 struct cm_rep_msg *rep_msg;
1352 struct ib_send_wr *bad_send_wr;
1353 unsigned long flags;
1356 if (param->private_data &&
1357 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1360 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1361 spin_lock_irqsave(&cm_id_priv->lock, flags);
1362 if (cm_id->state != IB_CM_REQ_RCVD &&
1363 cm_id->state != IB_CM_MRA_REQ_SENT) {
1368 ret = cm_alloc_msg(cm_id_priv, &msg);
1372 rep_msg = (struct cm_rep_msg *) msg->mad;
1373 cm_format_rep(rep_msg, cm_id_priv, param);
1374 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
1375 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1377 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1378 &msg->send_wr, &bad_send_wr);
1380 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1385 cm_id->state = IB_CM_REP_SENT;
1386 cm_id_priv->msg = msg;
1387 cm_id_priv->initiator_depth = param->initiator_depth;
1388 cm_id_priv->responder_resources = param->responder_resources;
1389 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1390 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1392 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1395 EXPORT_SYMBOL(ib_send_cm_rep);
1397 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1398 struct cm_id_private *cm_id_priv,
1399 const void *private_data,
1400 u8 private_data_len)
1402 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1403 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1404 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1406 if (private_data && private_data_len)
1407 memcpy(rtu_msg->private_data, private_data, private_data_len);
1410 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1411 const void *private_data,
1412 u8 private_data_len)
1414 struct cm_id_private *cm_id_priv;
1415 struct ib_mad_send_buf *msg;
1416 struct ib_send_wr *bad_send_wr;
1417 unsigned long flags;
1421 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1424 data = cm_copy_private_data(private_data, private_data_len);
1426 return PTR_ERR(data);
1428 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1429 spin_lock_irqsave(&cm_id_priv->lock, flags);
1430 if (cm_id->state != IB_CM_REP_RCVD &&
1431 cm_id->state != IB_CM_MRA_REP_SENT) {
1436 ret = cm_alloc_msg(cm_id_priv, &msg);
1440 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1441 private_data, private_data_len);
1443 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1444 &msg->send_wr, &bad_send_wr);
1446 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1452 cm_id->state = IB_CM_ESTABLISHED;
1453 cm_set_private_data(cm_id_priv, data, private_data_len);
1454 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1457 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1461 EXPORT_SYMBOL(ib_send_cm_rtu);
1463 static void cm_format_rep_event(struct cm_work *work)
1465 struct cm_rep_msg *rep_msg;
1466 struct ib_cm_rep_event_param *param;
1468 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1469 param = &work->cm_event.param.rep_rcvd;
1470 param->remote_ca_guid = rep_msg->local_ca_guid;
1471 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1472 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1473 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1474 param->responder_resources = rep_msg->initiator_depth;
1475 param->initiator_depth = rep_msg->resp_resources;
1476 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1477 param->failover_accepted = cm_rep_get_failover(rep_msg);
1478 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1479 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1480 param->srq = cm_rep_get_srq(rep_msg);
1481 work->cm_event.private_data = &rep_msg->private_data;
1484 static void cm_dup_rep_handler(struct cm_work *work)
1486 struct cm_id_private *cm_id_priv;
1487 struct cm_rep_msg *rep_msg;
1488 struct ib_mad_send_buf *msg = NULL;
1489 struct ib_send_wr *bad_send_wr;
1490 unsigned long flags;
1493 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1494 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1495 rep_msg->local_comm_id);
1499 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1503 spin_lock_irqsave(&cm_id_priv->lock, flags);
1504 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1505 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1506 cm_id_priv->private_data,
1507 cm_id_priv->private_data_len);
1508 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1509 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1510 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1511 cm_id_priv->private_data,
1512 cm_id_priv->private_data_len);
1515 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1517 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1523 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1524 free: cm_free_msg(msg);
1525 deref: cm_deref_id(cm_id_priv);
1528 static int cm_rep_handler(struct cm_work *work)
1530 struct cm_id_private *cm_id_priv;
1531 struct cm_rep_msg *rep_msg;
1532 unsigned long flags;
1535 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1536 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1538 cm_dup_rep_handler(work);
1542 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1543 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1544 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1546 spin_lock_irqsave(&cm.lock, flags);
1547 /* Check for duplicate REP. */
1548 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1549 spin_unlock_irqrestore(&cm.lock, flags);
1553 /* Check for a stale connection. */
1554 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1555 spin_unlock_irqrestore(&cm.lock, flags);
1556 cm_issue_rej(work->port, work->mad_recv_wc,
1557 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1562 spin_unlock_irqrestore(&cm.lock, flags);
1564 cm_format_rep_event(work);
1566 spin_lock_irqsave(&cm_id_priv->lock, flags);
1567 switch (cm_id_priv->id.state) {
1568 case IB_CM_REQ_SENT:
1569 case IB_CM_MRA_REQ_RCVD:
1572 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1576 cm_id_priv->id.state = IB_CM_REP_RCVD;
1577 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1578 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1579 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1580 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1581 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1582 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1584 /* todo: handle peer_to_peer */
1586 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1587 (unsigned long) cm_id_priv->msg);
1588 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1590 list_add_tail(&work->list, &cm_id_priv->work_list);
1591 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1594 cm_process_work(cm_id_priv, work);
1596 cm_deref_id(cm_id_priv);
1599 error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1600 cm_deref_id(cm_id_priv);
1604 static int cm_establish_handler(struct cm_work *work)
1606 struct cm_id_private *cm_id_priv;
1607 unsigned long flags;
1610 /* See comment in ib_cm_establish about lookup. */
1611 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1615 spin_lock_irqsave(&cm_id_priv->lock, flags);
1616 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1617 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1621 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1622 (unsigned long) cm_id_priv->msg);
1623 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1625 list_add_tail(&work->list, &cm_id_priv->work_list);
1626 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1629 cm_process_work(cm_id_priv, work);
1631 cm_deref_id(cm_id_priv);
1634 cm_deref_id(cm_id_priv);
1638 static int cm_rtu_handler(struct cm_work *work)
1640 struct cm_id_private *cm_id_priv;
1641 struct cm_rtu_msg *rtu_msg;
1642 unsigned long flags;
1645 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1646 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1647 rtu_msg->local_comm_id);
1651 work->cm_event.private_data = &rtu_msg->private_data;
1653 spin_lock_irqsave(&cm_id_priv->lock, flags);
1654 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1655 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1656 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1659 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1661 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1662 (unsigned long) cm_id_priv->msg);
1663 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1665 list_add_tail(&work->list, &cm_id_priv->work_list);
1666 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1669 cm_process_work(cm_id_priv, work);
1671 cm_deref_id(cm_id_priv);
1674 cm_deref_id(cm_id_priv);
1678 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1679 struct cm_id_private *cm_id_priv,
1680 const void *private_data,
1681 u8 private_data_len)
1683 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1684 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1685 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1686 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1687 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1689 if (private_data && private_data_len)
1690 memcpy(dreq_msg->private_data, private_data, private_data_len);
1693 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1694 const void *private_data,
1695 u8 private_data_len)
1697 struct cm_id_private *cm_id_priv;
1698 struct ib_mad_send_buf *msg;
1699 struct ib_send_wr *bad_send_wr;
1700 unsigned long flags;
1703 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1706 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1707 spin_lock_irqsave(&cm_id_priv->lock, flags);
1708 if (cm_id->state != IB_CM_ESTABLISHED) {
1713 ret = cm_alloc_msg(cm_id_priv, &msg);
1715 cm_enter_timewait(cm_id_priv);
1719 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1720 private_data, private_data_len);
1721 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
1722 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1724 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1725 &msg->send_wr, &bad_send_wr);
1727 cm_enter_timewait(cm_id_priv);
1728 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1733 cm_id->state = IB_CM_DREQ_SENT;
1734 cm_id_priv->msg = msg;
1735 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1738 EXPORT_SYMBOL(ib_send_cm_dreq);
1740 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1741 struct cm_id_private *cm_id_priv,
1742 const void *private_data,
1743 u8 private_data_len)
1745 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1746 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1747 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1749 if (private_data && private_data_len)
1750 memcpy(drep_msg->private_data, private_data, private_data_len);
1753 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1754 const void *private_data,
1755 u8 private_data_len)
1757 struct cm_id_private *cm_id_priv;
1758 struct ib_mad_send_buf *msg;
1759 struct ib_send_wr *bad_send_wr;
1760 unsigned long flags;
1764 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1767 data = cm_copy_private_data(private_data, private_data_len);
1769 return PTR_ERR(data);
1771 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1772 spin_lock_irqsave(&cm_id_priv->lock, flags);
1773 if (cm_id->state != IB_CM_DREQ_RCVD) {
1774 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1779 cm_set_private_data(cm_id_priv, data, private_data_len);
1780 cm_enter_timewait(cm_id_priv);
1782 ret = cm_alloc_msg(cm_id_priv, &msg);
1786 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1787 private_data, private_data_len);
1789 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1792 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1797 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1800 EXPORT_SYMBOL(ib_send_cm_drep);
1802 static int cm_dreq_handler(struct cm_work *work)
1804 struct cm_id_private *cm_id_priv;
1805 struct cm_dreq_msg *dreq_msg;
1806 struct ib_mad_send_buf *msg = NULL;
1807 struct ib_send_wr *bad_send_wr;
1808 unsigned long flags;
1811 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1812 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1813 dreq_msg->local_comm_id);
1817 work->cm_event.private_data = &dreq_msg->private_data;
1819 spin_lock_irqsave(&cm_id_priv->lock, flags);
1820 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1823 switch (cm_id_priv->id.state) {
1824 case IB_CM_REP_SENT:
1825 case IB_CM_DREQ_SENT:
1826 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1827 (unsigned long) cm_id_priv->msg);
1829 case IB_CM_ESTABLISHED:
1830 case IB_CM_MRA_REP_RCVD:
1832 case IB_CM_TIMEWAIT:
1833 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1836 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1837 cm_id_priv->private_data,
1838 cm_id_priv->private_data_len);
1839 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1841 if (ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1842 &msg->send_wr, &bad_send_wr))
1848 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1849 cm_id_priv->tid = dreq_msg->hdr.tid;
1850 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1852 list_add_tail(&work->list, &cm_id_priv->work_list);
1853 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1856 cm_process_work(cm_id_priv, work);
1858 cm_deref_id(cm_id_priv);
1861 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1862 deref: cm_deref_id(cm_id_priv);
1866 static int cm_drep_handler(struct cm_work *work)
1868 struct cm_id_private *cm_id_priv;
1869 struct cm_drep_msg *drep_msg;
1870 unsigned long flags;
1873 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1874 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1875 drep_msg->local_comm_id);
1879 work->cm_event.private_data = &drep_msg->private_data;
1881 spin_lock_irqsave(&cm_id_priv->lock, flags);
1882 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1883 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1884 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1887 cm_enter_timewait(cm_id_priv);
1889 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1890 (unsigned long) cm_id_priv->msg);
1891 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1893 list_add_tail(&work->list, &cm_id_priv->work_list);
1894 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1897 cm_process_work(cm_id_priv, work);
1899 cm_deref_id(cm_id_priv);
1902 cm_deref_id(cm_id_priv);
1906 int ib_send_cm_rej(struct ib_cm_id *cm_id,
1907 enum ib_cm_rej_reason reason,
1910 const void *private_data,
1911 u8 private_data_len)
1913 struct cm_id_private *cm_id_priv;
1914 struct ib_mad_send_buf *msg;
1915 struct ib_send_wr *bad_send_wr;
1916 unsigned long flags;
1919 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
1920 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
1923 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1925 spin_lock_irqsave(&cm_id_priv->lock, flags);
1926 switch (cm_id->state) {
1927 case IB_CM_REQ_SENT:
1928 case IB_CM_MRA_REQ_RCVD:
1929 case IB_CM_REQ_RCVD:
1930 case IB_CM_MRA_REQ_SENT:
1931 case IB_CM_REP_RCVD:
1932 case IB_CM_MRA_REP_SENT:
1933 ret = cm_alloc_msg(cm_id_priv, &msg);
1935 cm_format_rej((struct cm_rej_msg *) msg->mad,
1936 cm_id_priv, reason, ari, ari_length,
1937 private_data, private_data_len);
1939 cm_reset_to_idle(cm_id_priv);
1941 case IB_CM_REP_SENT:
1942 case IB_CM_MRA_REP_RCVD:
1943 ret = cm_alloc_msg(cm_id_priv, &msg);
1945 cm_format_rej((struct cm_rej_msg *) msg->mad,
1946 cm_id_priv, reason, ari, ari_length,
1947 private_data, private_data_len);
1949 cm_enter_timewait(cm_id_priv);
1959 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1960 &msg->send_wr, &bad_send_wr);
1964 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1967 EXPORT_SYMBOL(ib_send_cm_rej);
1969 static void cm_format_rej_event(struct cm_work *work)
1971 struct cm_rej_msg *rej_msg;
1972 struct ib_cm_rej_event_param *param;
1974 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
1975 param = &work->cm_event.param.rej_rcvd;
1976 param->ari = rej_msg->ari;
1977 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
1978 param->reason = __be16_to_cpu(rej_msg->reason);
1979 work->cm_event.private_data = &rej_msg->private_data;
1982 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
1984 struct cm_timewait_info *timewait_info;
1985 struct cm_id_private *cm_id_priv;
1986 unsigned long flags;
1989 remote_id = rej_msg->local_comm_id;
1991 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
1992 spin_lock_irqsave(&cm.lock, flags);
1993 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
1995 if (!timewait_info) {
1996 spin_unlock_irqrestore(&cm.lock, flags);
1999 cm_id_priv = idr_find(&cm.local_id_table,
2000 (__force int) timewait_info->work.local_id);
2002 if (cm_id_priv->id.remote_id == remote_id)
2003 atomic_inc(&cm_id_priv->refcount);
2007 spin_unlock_irqrestore(&cm.lock, flags);
2008 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2009 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2011 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2016 static int cm_rej_handler(struct cm_work *work)
2018 struct cm_id_private *cm_id_priv;
2019 struct cm_rej_msg *rej_msg;
2020 unsigned long flags;
2023 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2024 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2028 cm_format_rej_event(work);
2030 spin_lock_irqsave(&cm_id_priv->lock, flags);
2031 switch (cm_id_priv->id.state) {
2032 case IB_CM_REQ_SENT:
2033 case IB_CM_MRA_REQ_RCVD:
2034 case IB_CM_REP_SENT:
2035 case IB_CM_MRA_REP_RCVD:
2036 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2037 (unsigned long) cm_id_priv->msg);
2039 case IB_CM_REQ_RCVD:
2040 case IB_CM_MRA_REQ_SENT:
2041 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2042 cm_enter_timewait(cm_id_priv);
2044 cm_reset_to_idle(cm_id_priv);
2046 case IB_CM_DREQ_SENT:
2047 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2048 (unsigned long) cm_id_priv->msg);
2050 case IB_CM_REP_RCVD:
2051 case IB_CM_MRA_REP_SENT:
2052 case IB_CM_ESTABLISHED:
2053 cm_enter_timewait(cm_id_priv);
2056 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2061 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2063 list_add_tail(&work->list, &cm_id_priv->work_list);
2064 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2067 cm_process_work(cm_id_priv, work);
2069 cm_deref_id(cm_id_priv);
2072 cm_deref_id(cm_id_priv);
2076 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2078 const void *private_data,
2079 u8 private_data_len)
2081 struct cm_id_private *cm_id_priv;
2082 struct ib_mad_send_buf *msg;
2083 struct ib_send_wr *bad_send_wr;
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(cm_id_priv->av.port->mad_agent,
2108 &msg->send_wr, &bad_send_wr);
2111 cm_id->state = IB_CM_MRA_REQ_SENT;
2113 case IB_CM_REP_RCVD:
2114 ret = cm_alloc_msg(cm_id_priv, &msg);
2118 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2119 CM_MSG_RESPONSE_REP, service_timeout,
2120 private_data, private_data_len);
2121 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2122 &msg->send_wr, &bad_send_wr);
2125 cm_id->state = IB_CM_MRA_REP_SENT;
2127 case IB_CM_ESTABLISHED:
2128 ret = cm_alloc_msg(cm_id_priv, &msg);
2132 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2133 CM_MSG_RESPONSE_OTHER, service_timeout,
2134 private_data, private_data_len);
2135 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2136 &msg->send_wr, &bad_send_wr);
2139 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2145 cm_id_priv->service_timeout = service_timeout;
2146 cm_set_private_data(cm_id_priv, data, private_data_len);
2147 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2150 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2154 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2159 EXPORT_SYMBOL(ib_send_cm_mra);
2161 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2163 switch (cm_mra_get_msg_mraed(mra_msg)) {
2164 case CM_MSG_RESPONSE_REQ:
2165 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2166 case CM_MSG_RESPONSE_REP:
2167 case CM_MSG_RESPONSE_OTHER:
2168 return cm_acquire_id(mra_msg->remote_comm_id,
2169 mra_msg->local_comm_id);
2175 static int cm_mra_handler(struct cm_work *work)
2177 struct cm_id_private *cm_id_priv;
2178 struct cm_mra_msg *mra_msg;
2179 unsigned long flags;
2182 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2183 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2187 work->cm_event.private_data = &mra_msg->private_data;
2188 work->cm_event.param.mra_rcvd.service_timeout =
2189 cm_mra_get_service_timeout(mra_msg);
2190 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2191 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2193 spin_lock_irqsave(&cm_id_priv->lock, flags);
2194 switch (cm_id_priv->id.state) {
2195 case IB_CM_REQ_SENT:
2196 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2197 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2198 (unsigned long) cm_id_priv->msg, timeout))
2200 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2202 case IB_CM_REP_SENT:
2203 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2204 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2205 (unsigned long) cm_id_priv->msg, timeout))
2207 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2209 case IB_CM_ESTABLISHED:
2210 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2211 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2212 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2213 (unsigned long) cm_id_priv->msg, timeout))
2215 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2221 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2222 cm_id_priv->id.state;
2223 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2225 list_add_tail(&work->list, &cm_id_priv->work_list);
2226 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2229 cm_process_work(cm_id_priv, work);
2231 cm_deref_id(cm_id_priv);
2234 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2235 cm_deref_id(cm_id_priv);
2239 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2240 struct cm_id_private *cm_id_priv,
2241 struct ib_sa_path_rec *alternate_path,
2242 const void *private_data,
2243 u8 private_data_len)
2245 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2246 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2247 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2248 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2249 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2250 /* todo: need remote CM response timeout */
2251 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2252 lap_msg->alt_local_lid = alternate_path->slid;
2253 lap_msg->alt_remote_lid = alternate_path->dlid;
2254 lap_msg->alt_local_gid = alternate_path->sgid;
2255 lap_msg->alt_remote_gid = alternate_path->dgid;
2256 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2257 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2258 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2259 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2260 cm_lap_set_sl(lap_msg, alternate_path->sl);
2261 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2262 cm_lap_set_local_ack_timeout(lap_msg,
2263 min(31, alternate_path->packet_life_time + 1));
2265 if (private_data && private_data_len)
2266 memcpy(lap_msg->private_data, private_data, private_data_len);
2269 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2270 struct ib_sa_path_rec *alternate_path,
2271 const void *private_data,
2272 u8 private_data_len)
2274 struct cm_id_private *cm_id_priv;
2275 struct ib_mad_send_buf *msg;
2276 struct ib_send_wr *bad_send_wr;
2277 unsigned long flags;
2280 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2283 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2284 spin_lock_irqsave(&cm_id_priv->lock, flags);
2285 if (cm_id->state != IB_CM_ESTABLISHED ||
2286 cm_id->lap_state != IB_CM_LAP_IDLE) {
2291 ret = cm_alloc_msg(cm_id_priv, &msg);
2295 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2296 alternate_path, private_data, private_data_len);
2297 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
2298 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2300 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2301 &msg->send_wr, &bad_send_wr);
2303 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2308 cm_id->lap_state = IB_CM_LAP_SENT;
2309 cm_id_priv->msg = msg;
2311 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2314 EXPORT_SYMBOL(ib_send_cm_lap);
2316 static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2317 struct cm_lap_msg *lap_msg)
2319 memset(path, 0, sizeof *path);
2320 path->dgid = lap_msg->alt_local_gid;
2321 path->sgid = lap_msg->alt_remote_gid;
2322 path->dlid = lap_msg->alt_local_lid;
2323 path->slid = lap_msg->alt_remote_lid;
2324 path->flow_label = cm_lap_get_flow_label(lap_msg);
2325 path->hop_limit = lap_msg->alt_hop_limit;
2326 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2327 path->reversible = 1;
2328 /* pkey is same as in REQ */
2329 path->sl = cm_lap_get_sl(lap_msg);
2330 path->mtu_selector = IB_SA_EQ;
2331 /* mtu is same as in REQ */
2332 path->rate_selector = IB_SA_EQ;
2333 path->rate = cm_lap_get_packet_rate(lap_msg);
2334 path->packet_life_time_selector = IB_SA_EQ;
2335 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2336 path->packet_life_time -= (path->packet_life_time > 0);
2339 static int cm_lap_handler(struct cm_work *work)
2341 struct cm_id_private *cm_id_priv;
2342 struct cm_lap_msg *lap_msg;
2343 struct ib_cm_lap_event_param *param;
2344 struct ib_mad_send_buf *msg = NULL;
2345 struct ib_send_wr *bad_send_wr;
2346 unsigned long flags;
2349 /* todo: verify LAP request and send reject APR if invalid. */
2350 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2351 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2352 lap_msg->local_comm_id);
2356 param = &work->cm_event.param.lap_rcvd;
2357 param->alternate_path = &work->path[0];
2358 cm_format_path_from_lap(param->alternate_path, lap_msg);
2359 work->cm_event.private_data = &lap_msg->private_data;
2361 spin_lock_irqsave(&cm_id_priv->lock, flags);
2362 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2365 switch (cm_id_priv->id.lap_state) {
2366 case IB_CM_LAP_IDLE:
2368 case IB_CM_MRA_LAP_SENT:
2369 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2372 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2373 CM_MSG_RESPONSE_OTHER,
2374 cm_id_priv->service_timeout,
2375 cm_id_priv->private_data,
2376 cm_id_priv->private_data_len);
2377 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2379 if (ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2380 &msg->send_wr, &bad_send_wr))
2387 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2388 cm_id_priv->tid = lap_msg->hdr.tid;
2389 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2391 list_add_tail(&work->list, &cm_id_priv->work_list);
2392 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2395 cm_process_work(cm_id_priv, work);
2397 cm_deref_id(cm_id_priv);
2400 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2401 deref: cm_deref_id(cm_id_priv);
2405 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2406 struct cm_id_private *cm_id_priv,
2407 enum ib_cm_apr_status status,
2410 const void *private_data,
2411 u8 private_data_len)
2413 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2414 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2415 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2416 apr_msg->ap_status = (u8) status;
2418 if (info && info_length) {
2419 apr_msg->info_length = info_length;
2420 memcpy(apr_msg->info, info, info_length);
2423 if (private_data && private_data_len)
2424 memcpy(apr_msg->private_data, private_data, private_data_len);
2427 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2428 enum ib_cm_apr_status status,
2431 const void *private_data,
2432 u8 private_data_len)
2434 struct cm_id_private *cm_id_priv;
2435 struct ib_mad_send_buf *msg;
2436 struct ib_send_wr *bad_send_wr;
2437 unsigned long flags;
2440 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2441 (info && info_length > IB_CM_APR_INFO_LENGTH))
2444 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2445 spin_lock_irqsave(&cm_id_priv->lock, flags);
2446 if (cm_id->state != IB_CM_ESTABLISHED ||
2447 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2448 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2453 ret = cm_alloc_msg(cm_id_priv, &msg);
2457 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2458 info, info_length, private_data, private_data_len);
2459 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2460 &msg->send_wr, &bad_send_wr);
2462 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2467 cm_id->lap_state = IB_CM_LAP_IDLE;
2468 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2471 EXPORT_SYMBOL(ib_send_cm_apr);
2473 static int cm_apr_handler(struct cm_work *work)
2475 struct cm_id_private *cm_id_priv;
2476 struct cm_apr_msg *apr_msg;
2477 unsigned long flags;
2480 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2481 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2482 apr_msg->local_comm_id);
2484 return -EINVAL; /* Unmatched reply. */
2486 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2487 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2488 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2489 work->cm_event.private_data = &apr_msg->private_data;
2491 spin_lock_irqsave(&cm_id_priv->lock, flags);
2492 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2493 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2494 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2495 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2498 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2499 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2500 (unsigned long) cm_id_priv->msg);
2501 cm_id_priv->msg = NULL;
2503 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2505 list_add_tail(&work->list, &cm_id_priv->work_list);
2506 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2509 cm_process_work(cm_id_priv, work);
2511 cm_deref_id(cm_id_priv);
2514 cm_deref_id(cm_id_priv);
2518 static int cm_timewait_handler(struct cm_work *work)
2520 struct cm_timewait_info *timewait_info;
2521 struct cm_id_private *cm_id_priv;
2522 unsigned long flags;
2525 timewait_info = (struct cm_timewait_info *)work;
2526 cm_cleanup_timewait(timewait_info);
2528 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2529 timewait_info->work.remote_id);
2533 spin_lock_irqsave(&cm_id_priv->lock, flags);
2534 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2535 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2536 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2539 cm_id_priv->id.state = IB_CM_IDLE;
2540 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2542 list_add_tail(&work->list, &cm_id_priv->work_list);
2543 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2546 cm_process_work(cm_id_priv, work);
2548 cm_deref_id(cm_id_priv);
2551 cm_deref_id(cm_id_priv);
2555 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2556 struct cm_id_private *cm_id_priv,
2557 struct ib_cm_sidr_req_param *param)
2559 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2560 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2561 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2562 sidr_req_msg->pkey = cpu_to_be16(param->pkey);
2563 sidr_req_msg->service_id = param->service_id;
2565 if (param->private_data && param->private_data_len)
2566 memcpy(sidr_req_msg->private_data, param->private_data,
2567 param->private_data_len);
2570 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2571 struct ib_cm_sidr_req_param *param)
2573 struct cm_id_private *cm_id_priv;
2574 struct ib_mad_send_buf *msg;
2575 struct ib_send_wr *bad_send_wr;
2576 unsigned long flags;
2579 if (!param->path || (param->private_data &&
2580 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2583 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2584 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2588 cm_id->service_id = param->service_id;
2589 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2590 cm_id_priv->timeout_ms = param->timeout_ms;
2591 cm_id_priv->max_cm_retries = param->max_cm_retries;
2592 ret = cm_alloc_msg(cm_id_priv, &msg);
2596 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2598 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
2599 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2601 spin_lock_irqsave(&cm_id_priv->lock, flags);
2602 if (cm_id->state == IB_CM_IDLE)
2603 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2604 &msg->send_wr, &bad_send_wr);
2609 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2613 cm_id->state = IB_CM_SIDR_REQ_SENT;
2614 cm_id_priv->msg = msg;
2615 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2619 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2621 static void cm_format_sidr_req_event(struct cm_work *work,
2622 struct ib_cm_id *listen_id)
2624 struct cm_sidr_req_msg *sidr_req_msg;
2625 struct ib_cm_sidr_req_event_param *param;
2627 sidr_req_msg = (struct cm_sidr_req_msg *)
2628 work->mad_recv_wc->recv_buf.mad;
2629 param = &work->cm_event.param.sidr_req_rcvd;
2630 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2631 param->listen_id = listen_id;
2632 param->device = work->port->mad_agent->device;
2633 param->port = work->port->port_num;
2634 work->cm_event.private_data = &sidr_req_msg->private_data;
2637 static int cm_sidr_req_handler(struct cm_work *work)
2639 struct ib_cm_id *cm_id;
2640 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2641 struct cm_sidr_req_msg *sidr_req_msg;
2643 unsigned long flags;
2645 cm_id = ib_create_cm_id(NULL, NULL);
2647 return PTR_ERR(cm_id);
2648 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2650 /* Record SGID/SLID and request ID for lookup. */
2651 sidr_req_msg = (struct cm_sidr_req_msg *)
2652 work->mad_recv_wc->recv_buf.mad;
2653 wc = work->mad_recv_wc->wc;
2654 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2655 cm_id_priv->av.dgid.global.interface_id = 0;
2656 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2658 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2659 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2660 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2661 atomic_inc(&cm_id_priv->work_count);
2663 spin_lock_irqsave(&cm.lock, flags);
2664 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2665 if (cur_cm_id_priv) {
2666 spin_unlock_irqrestore(&cm.lock, flags);
2667 goto out; /* Duplicate message. */
2669 cur_cm_id_priv = cm_find_listen(sidr_req_msg->service_id);
2670 if (!cur_cm_id_priv) {
2671 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2672 spin_unlock_irqrestore(&cm.lock, flags);
2673 /* todo: reply with no match */
2674 goto out; /* No match. */
2676 atomic_inc(&cur_cm_id_priv->refcount);
2677 spin_unlock_irqrestore(&cm.lock, flags);
2679 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2680 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2681 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2682 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2684 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2685 cm_process_work(cm_id_priv, work);
2686 cm_deref_id(cur_cm_id_priv);
2689 ib_destroy_cm_id(&cm_id_priv->id);
2693 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2694 struct cm_id_private *cm_id_priv,
2695 struct ib_cm_sidr_rep_param *param)
2697 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2699 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2700 sidr_rep_msg->status = param->status;
2701 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2702 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2703 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2705 if (param->info && param->info_length)
2706 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2708 if (param->private_data && param->private_data_len)
2709 memcpy(sidr_rep_msg->private_data, param->private_data,
2710 param->private_data_len);
2713 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2714 struct ib_cm_sidr_rep_param *param)
2716 struct cm_id_private *cm_id_priv;
2717 struct ib_mad_send_buf *msg;
2718 struct ib_send_wr *bad_send_wr;
2719 unsigned long flags;
2722 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2723 (param->private_data &&
2724 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2727 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2728 spin_lock_irqsave(&cm_id_priv->lock, flags);
2729 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2734 ret = cm_alloc_msg(cm_id_priv, &msg);
2738 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2740 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2741 &msg->send_wr, &bad_send_wr);
2743 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2747 cm_id->state = IB_CM_IDLE;
2748 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2750 spin_lock_irqsave(&cm.lock, flags);
2751 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2752 spin_unlock_irqrestore(&cm.lock, flags);
2755 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2758 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2760 static void cm_format_sidr_rep_event(struct cm_work *work)
2762 struct cm_sidr_rep_msg *sidr_rep_msg;
2763 struct ib_cm_sidr_rep_event_param *param;
2765 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2766 work->mad_recv_wc->recv_buf.mad;
2767 param = &work->cm_event.param.sidr_rep_rcvd;
2768 param->status = sidr_rep_msg->status;
2769 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2770 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2771 param->info = &sidr_rep_msg->info;
2772 param->info_len = sidr_rep_msg->info_length;
2773 work->cm_event.private_data = &sidr_rep_msg->private_data;
2776 static int cm_sidr_rep_handler(struct cm_work *work)
2778 struct cm_sidr_rep_msg *sidr_rep_msg;
2779 struct cm_id_private *cm_id_priv;
2780 unsigned long flags;
2782 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2783 work->mad_recv_wc->recv_buf.mad;
2784 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2786 return -EINVAL; /* Unmatched reply. */
2788 spin_lock_irqsave(&cm_id_priv->lock, flags);
2789 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2790 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2793 cm_id_priv->id.state = IB_CM_IDLE;
2794 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2795 (unsigned long) cm_id_priv->msg);
2796 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2798 cm_format_sidr_rep_event(work);
2799 cm_process_work(cm_id_priv, work);
2802 cm_deref_id(cm_id_priv);
2806 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2807 enum ib_wc_status wc_status)
2809 struct cm_id_private *cm_id_priv;
2810 struct ib_cm_event cm_event;
2811 enum ib_cm_state state;
2812 unsigned long flags;
2815 memset(&cm_event, 0, sizeof cm_event);
2816 cm_id_priv = msg->context[0];
2818 /* Discard old sends or ones without a response. */
2819 spin_lock_irqsave(&cm_id_priv->lock, flags);
2820 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2821 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2825 case IB_CM_REQ_SENT:
2826 case IB_CM_MRA_REQ_RCVD:
2827 cm_reset_to_idle(cm_id_priv);
2828 cm_event.event = IB_CM_REQ_ERROR;
2830 case IB_CM_REP_SENT:
2831 case IB_CM_MRA_REP_RCVD:
2832 cm_reset_to_idle(cm_id_priv);
2833 cm_event.event = IB_CM_REP_ERROR;
2835 case IB_CM_DREQ_SENT:
2836 cm_enter_timewait(cm_id_priv);
2837 cm_event.event = IB_CM_DREQ_ERROR;
2839 case IB_CM_SIDR_REQ_SENT:
2840 cm_id_priv->id.state = IB_CM_IDLE;
2841 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2846 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2847 cm_event.param.send_status = wc_status;
2849 /* No other events can occur on the cm_id at this point. */
2850 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2853 ib_destroy_cm_id(&cm_id_priv->id);
2856 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2860 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2861 struct ib_mad_send_wc *mad_send_wc)
2863 struct ib_mad_send_buf *msg;
2865 msg = (struct ib_mad_send_buf *)(unsigned long)mad_send_wc->wr_id;
2867 switch (mad_send_wc->status) {
2869 case IB_WC_WR_FLUSH_ERR:
2873 if (msg->context[0] && msg->context[1])
2874 cm_process_send_error(msg, mad_send_wc->status);
2881 static void cm_work_handler(void *data)
2883 struct cm_work *work = data;
2886 switch (work->cm_event.event) {
2887 case IB_CM_REQ_RECEIVED:
2888 ret = cm_req_handler(work);
2890 case IB_CM_MRA_RECEIVED:
2891 ret = cm_mra_handler(work);
2893 case IB_CM_REJ_RECEIVED:
2894 ret = cm_rej_handler(work);
2896 case IB_CM_REP_RECEIVED:
2897 ret = cm_rep_handler(work);
2899 case IB_CM_RTU_RECEIVED:
2900 ret = cm_rtu_handler(work);
2902 case IB_CM_USER_ESTABLISHED:
2903 ret = cm_establish_handler(work);
2905 case IB_CM_DREQ_RECEIVED:
2906 ret = cm_dreq_handler(work);
2908 case IB_CM_DREP_RECEIVED:
2909 ret = cm_drep_handler(work);
2911 case IB_CM_SIDR_REQ_RECEIVED:
2912 ret = cm_sidr_req_handler(work);
2914 case IB_CM_SIDR_REP_RECEIVED:
2915 ret = cm_sidr_rep_handler(work);
2917 case IB_CM_LAP_RECEIVED:
2918 ret = cm_lap_handler(work);
2920 case IB_CM_APR_RECEIVED:
2921 ret = cm_apr_handler(work);
2923 case IB_CM_TIMEWAIT_EXIT:
2924 ret = cm_timewait_handler(work);
2934 int ib_cm_establish(struct ib_cm_id *cm_id)
2936 struct cm_id_private *cm_id_priv;
2937 struct cm_work *work;
2938 unsigned long flags;
2941 work = kmalloc(sizeof *work, GFP_ATOMIC);
2945 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2946 spin_lock_irqsave(&cm_id_priv->lock, flags);
2947 switch (cm_id->state)
2949 case IB_CM_REP_SENT:
2950 case IB_CM_MRA_REP_RCVD:
2951 cm_id->state = IB_CM_ESTABLISHED;
2953 case IB_CM_ESTABLISHED:
2960 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2968 * The CM worker thread may try to destroy the cm_id before it
2969 * can execute this work item. To prevent potential deadlock,
2970 * we need to find the cm_id once we're in the context of the
2971 * worker thread, rather than holding a reference on it.
2973 INIT_WORK(&work->work, cm_work_handler, work);
2974 work->local_id = cm_id->local_id;
2975 work->remote_id = cm_id->remote_id;
2976 work->mad_recv_wc = NULL;
2977 work->cm_event.event = IB_CM_USER_ESTABLISHED;
2978 queue_work(cm.wq, &work->work);
2982 EXPORT_SYMBOL(ib_cm_establish);
2984 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
2985 struct ib_mad_recv_wc *mad_recv_wc)
2987 struct cm_work *work;
2988 enum ib_cm_event_type event;
2991 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
2992 case CM_REQ_ATTR_ID:
2993 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
2994 alt_local_lid != 0);
2995 event = IB_CM_REQ_RECEIVED;
2997 case CM_MRA_ATTR_ID:
2998 event = IB_CM_MRA_RECEIVED;
3000 case CM_REJ_ATTR_ID:
3001 event = IB_CM_REJ_RECEIVED;
3003 case CM_REP_ATTR_ID:
3004 event = IB_CM_REP_RECEIVED;
3006 case CM_RTU_ATTR_ID:
3007 event = IB_CM_RTU_RECEIVED;
3009 case CM_DREQ_ATTR_ID:
3010 event = IB_CM_DREQ_RECEIVED;
3012 case CM_DREP_ATTR_ID:
3013 event = IB_CM_DREP_RECEIVED;
3015 case CM_SIDR_REQ_ATTR_ID:
3016 event = IB_CM_SIDR_REQ_RECEIVED;
3018 case CM_SIDR_REP_ATTR_ID:
3019 event = IB_CM_SIDR_REP_RECEIVED;
3021 case CM_LAP_ATTR_ID:
3023 event = IB_CM_LAP_RECEIVED;
3025 case CM_APR_ATTR_ID:
3026 event = IB_CM_APR_RECEIVED;
3029 ib_free_recv_mad(mad_recv_wc);
3033 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3036 ib_free_recv_mad(mad_recv_wc);
3040 INIT_WORK(&work->work, cm_work_handler, work);
3041 work->cm_event.event = event;
3042 work->mad_recv_wc = mad_recv_wc;
3043 work->port = (struct cm_port *)mad_agent->context;
3044 queue_work(cm.wq, &work->work);
3047 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3048 struct ib_qp_attr *qp_attr,
3051 unsigned long flags;
3054 spin_lock_irqsave(&cm_id_priv->lock, flags);
3055 switch (cm_id_priv->id.state) {
3056 case IB_CM_REQ_SENT:
3057 case IB_CM_MRA_REQ_RCVD:
3058 case IB_CM_REQ_RCVD:
3059 case IB_CM_MRA_REQ_SENT:
3060 case IB_CM_REP_RCVD:
3061 case IB_CM_MRA_REP_SENT:
3062 case IB_CM_REP_SENT:
3063 case IB_CM_MRA_REP_RCVD:
3064 case IB_CM_ESTABLISHED:
3065 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3066 IB_QP_PKEY_INDEX | IB_QP_PORT;
3067 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
3068 if (cm_id_priv->responder_resources)
3069 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_WRITE |
3070 IB_ACCESS_REMOTE_READ;
3071 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3072 qp_attr->port_num = cm_id_priv->av.port->port_num;
3079 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3083 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3084 struct ib_qp_attr *qp_attr,
3087 unsigned long flags;
3090 spin_lock_irqsave(&cm_id_priv->lock, flags);
3091 switch (cm_id_priv->id.state) {
3092 case IB_CM_REQ_RCVD:
3093 case IB_CM_MRA_REQ_SENT:
3094 case IB_CM_REP_RCVD:
3095 case IB_CM_MRA_REP_SENT:
3096 case IB_CM_REP_SENT:
3097 case IB_CM_MRA_REP_RCVD:
3098 case IB_CM_ESTABLISHED:
3099 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3100 IB_QP_DEST_QPN | IB_QP_RQ_PSN |
3101 IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER;
3102 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3103 qp_attr->path_mtu = cm_id_priv->path_mtu;
3104 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3105 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3106 qp_attr->max_dest_rd_atomic = cm_id_priv->responder_resources;
3107 qp_attr->min_rnr_timer = 0;
3108 if (cm_id_priv->alt_av.ah_attr.dlid) {
3109 *qp_attr_mask |= IB_QP_ALT_PATH;
3110 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3118 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3122 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3123 struct ib_qp_attr *qp_attr,
3126 unsigned long flags;
3129 spin_lock_irqsave(&cm_id_priv->lock, flags);
3130 switch (cm_id_priv->id.state) {
3131 case IB_CM_REP_RCVD:
3132 case IB_CM_MRA_REP_SENT:
3133 case IB_CM_REP_SENT:
3134 case IB_CM_MRA_REP_RCVD:
3135 case IB_CM_ESTABLISHED:
3136 *qp_attr_mask = IB_QP_STATE | IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3137 IB_QP_RNR_RETRY | IB_QP_SQ_PSN |
3138 IB_QP_MAX_QP_RD_ATOMIC;
3139 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3140 qp_attr->retry_cnt = cm_id_priv->retry_count;
3141 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3142 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3143 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3144 if (cm_id_priv->alt_av.ah_attr.dlid) {
3145 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3146 qp_attr->path_mig_state = IB_MIG_REARM;
3154 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3158 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3159 struct ib_qp_attr *qp_attr,
3162 struct cm_id_private *cm_id_priv;
3165 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3166 switch (qp_attr->qp_state) {
3168 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3171 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3174 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3182 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3184 static __be64 cm_get_ca_guid(struct ib_device *device)
3186 struct ib_device_attr *device_attr;
3190 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
3194 ret = ib_query_device(device, device_attr);
3195 guid = ret ? 0 : device_attr->node_guid;
3200 static void cm_add_one(struct ib_device *device)
3202 struct cm_device *cm_dev;
3203 struct cm_port *port;
3204 struct ib_mad_reg_req reg_req = {
3205 .mgmt_class = IB_MGMT_CLASS_CM,
3206 .mgmt_class_version = IB_CM_CLASS_VERSION
3208 struct ib_port_modify port_modify = {
3209 .set_port_cap_mask = IB_PORT_CM_SUP
3211 unsigned long flags;
3215 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3216 device->phys_port_cnt, GFP_KERNEL);
3220 cm_dev->device = device;
3221 cm_dev->ca_guid = cm_get_ca_guid(device);
3222 if (!cm_dev->ca_guid)
3225 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3226 for (i = 1; i <= device->phys_port_cnt; i++) {
3227 port = &cm_dev->port[i-1];
3228 port->cm_dev = cm_dev;
3230 port->mad_agent = ib_register_mad_agent(device, i,
3237 if (IS_ERR(port->mad_agent))
3240 ret = ib_modify_port(device, i, 0, &port_modify);
3244 ib_set_client_data(device, &cm_client, cm_dev);
3246 write_lock_irqsave(&cm.device_lock, flags);
3247 list_add_tail(&cm_dev->list, &cm.device_list);
3248 write_unlock_irqrestore(&cm.device_lock, flags);
3252 ib_unregister_mad_agent(port->mad_agent);
3254 port_modify.set_port_cap_mask = 0;
3255 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3257 port = &cm_dev->port[i-1];
3258 ib_modify_port(device, port->port_num, 0, &port_modify);
3259 ib_unregister_mad_agent(port->mad_agent);
3265 static void cm_remove_one(struct ib_device *device)
3267 struct cm_device *cm_dev;
3268 struct cm_port *port;
3269 struct ib_port_modify port_modify = {
3270 .clr_port_cap_mask = IB_PORT_CM_SUP
3272 unsigned long flags;
3275 cm_dev = ib_get_client_data(device, &cm_client);
3279 write_lock_irqsave(&cm.device_lock, flags);
3280 list_del(&cm_dev->list);
3281 write_unlock_irqrestore(&cm.device_lock, flags);
3283 for (i = 1; i <= device->phys_port_cnt; i++) {
3284 port = &cm_dev->port[i-1];
3285 ib_modify_port(device, port->port_num, 0, &port_modify);
3286 ib_unregister_mad_agent(port->mad_agent);
3291 static int __init ib_cm_init(void)
3295 memset(&cm, 0, sizeof cm);
3296 INIT_LIST_HEAD(&cm.device_list);
3297 rwlock_init(&cm.device_lock);
3298 spin_lock_init(&cm.lock);
3299 cm.listen_service_table = RB_ROOT;
3300 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3301 cm.remote_id_table = RB_ROOT;
3302 cm.remote_qp_table = RB_ROOT;
3303 cm.remote_sidr_table = RB_ROOT;
3304 idr_init(&cm.local_id_table);
3305 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3307 cm.wq = create_workqueue("ib_cm");
3311 ret = ib_register_client(&cm_client);
3317 destroy_workqueue(cm.wq);
3321 static void __exit ib_cm_cleanup(void)
3323 flush_workqueue(cm.wq);
3324 destroy_workqueue(cm.wq);
3325 ib_unregister_client(&cm_client);
3328 module_init(ib_cm_init);
3329 module_exit(ib_cm_cleanup);