2 * Copyright (c) 2004-2006 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/pci.h>
44 #include <linux/random.h>
45 #include <linux/rbtree.h>
46 #include <linux/spinlock.h>
47 #include <linux/workqueue.h>
49 #include <rdma/ib_cache.h>
50 #include <rdma/ib_cm.h>
53 MODULE_AUTHOR("Sean Hefty");
54 MODULE_DESCRIPTION("InfiniBand CM");
55 MODULE_LICENSE("Dual BSD/GPL");
57 static void cm_add_one(struct ib_device *device);
58 static void cm_remove_one(struct ib_device *device);
60 static struct ib_client cm_client = {
63 .remove = cm_remove_one
68 struct list_head device_list;
70 struct rb_root listen_service_table;
71 u64 listen_service_id;
72 /* struct rb_root peer_service_table; todo: fix peer to peer */
73 struct rb_root remote_qp_table;
74 struct rb_root remote_id_table;
75 struct rb_root remote_sidr_table;
76 struct idr local_id_table;
77 __be32 random_id_operand;
78 struct workqueue_struct *wq;
82 struct cm_device *cm_dev;
83 struct ib_mad_agent *mad_agent;
88 struct list_head list;
89 struct ib_device *device;
91 struct cm_port port[0];
97 struct ib_ah_attr ah_attr;
103 struct work_struct work;
104 struct list_head list;
105 struct cm_port *port;
106 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
107 __be32 local_id; /* Established / timewait */
109 struct ib_cm_event cm_event;
110 struct ib_sa_path_rec path[0];
113 struct cm_timewait_info {
114 struct cm_work work; /* Must be first. */
115 struct rb_node remote_qp_node;
116 struct rb_node remote_id_node;
117 __be64 remote_ca_guid;
119 u8 inserted_remote_qp;
120 u8 inserted_remote_id;
123 struct cm_id_private {
126 struct rb_node service_node;
127 struct rb_node sidr_id_node;
128 spinlock_t lock; /* Do not acquire inside cm.lock */
129 struct completion comp;
132 struct ib_mad_send_buf *msg;
133 struct cm_timewait_info *timewait_info;
134 /* todo: use alternate port on send failure */
137 struct ib_cm_compare_data *compare_data;
143 enum ib_qp_type qp_type;
147 enum ib_mtu path_mtu;
151 u8 responder_resources;
153 u8 local_ack_timeout;
158 struct list_head work_list;
162 static void cm_work_handler(void *data);
164 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
166 if (atomic_dec_and_test(&cm_id_priv->refcount))
167 complete(&cm_id_priv->comp);
170 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
171 struct ib_mad_send_buf **msg)
173 struct ib_mad_agent *mad_agent;
174 struct ib_mad_send_buf *m;
177 mad_agent = cm_id_priv->av.port->mad_agent;
178 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
182 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
183 cm_id_priv->av.pkey_index,
184 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
191 /* Timeout set by caller if response is expected. */
193 m->retries = cm_id_priv->max_cm_retries;
195 atomic_inc(&cm_id_priv->refcount);
196 m->context[0] = cm_id_priv;
201 static int cm_alloc_response_msg(struct cm_port *port,
202 struct ib_mad_recv_wc *mad_recv_wc,
203 struct ib_mad_send_buf **msg)
205 struct ib_mad_send_buf *m;
208 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
209 mad_recv_wc->recv_buf.grh, port->port_num);
213 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
214 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
225 static void cm_free_msg(struct ib_mad_send_buf *msg)
227 ib_destroy_ah(msg->ah);
229 cm_deref_id(msg->context[0]);
230 ib_free_send_mad(msg);
233 static void * cm_copy_private_data(const void *private_data,
238 if (!private_data || !private_data_len)
241 data = kmalloc(private_data_len, GFP_KERNEL);
243 return ERR_PTR(-ENOMEM);
245 memcpy(data, private_data, private_data_len);
249 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
250 void *private_data, u8 private_data_len)
252 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
253 kfree(cm_id_priv->private_data);
255 cm_id_priv->private_data = private_data;
256 cm_id_priv->private_data_len = private_data_len;
259 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
260 struct ib_grh *grh, struct cm_av *av)
263 av->pkey_index = wc->pkey_index;
264 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
268 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
270 struct cm_device *cm_dev;
271 struct cm_port *port = NULL;
276 read_lock_irqsave(&cm.device_lock, flags);
277 list_for_each_entry(cm_dev, &cm.device_list, list) {
278 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
280 port = &cm_dev->port[p-1];
284 read_unlock_irqrestore(&cm.device_lock, flags);
289 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
290 be16_to_cpu(path->pkey), &av->pkey_index);
295 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
297 av->packet_life_time = path->packet_life_time;
301 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
308 spin_lock_irqsave(&cm.lock, flags);
309 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
311 spin_unlock_irqrestore(&cm.lock, flags);
312 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
314 cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand);
318 static void cm_free_id(__be32 local_id)
322 spin_lock_irqsave(&cm.lock, flags);
323 idr_remove(&cm.local_id_table,
324 (__force int) (local_id ^ cm.random_id_operand));
325 spin_unlock_irqrestore(&cm.lock, flags);
328 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
330 struct cm_id_private *cm_id_priv;
332 cm_id_priv = idr_find(&cm.local_id_table,
333 (__force int) (local_id ^ cm.random_id_operand));
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 void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
360 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
361 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
362 ((unsigned long *) mask)[i];
365 static int cm_compare_data(struct ib_cm_compare_data *src_data,
366 struct ib_cm_compare_data *dst_data)
368 u8 src[IB_CM_COMPARE_SIZE];
369 u8 dst[IB_CM_COMPARE_SIZE];
371 if (!src_data || !dst_data)
374 cm_mask_copy(src, src_data->data, dst_data->mask);
375 cm_mask_copy(dst, dst_data->data, src_data->mask);
376 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
379 static int cm_compare_private_data(u8 *private_data,
380 struct ib_cm_compare_data *dst_data)
382 u8 src[IB_CM_COMPARE_SIZE];
387 cm_mask_copy(src, private_data, dst_data->mask);
388 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
391 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
393 struct rb_node **link = &cm.listen_service_table.rb_node;
394 struct rb_node *parent = NULL;
395 struct cm_id_private *cur_cm_id_priv;
396 __be64 service_id = cm_id_priv->id.service_id;
397 __be64 service_mask = cm_id_priv->id.service_mask;
402 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
404 data_cmp = cm_compare_data(cm_id_priv->compare_data,
405 cur_cm_id_priv->compare_data);
406 if ((cur_cm_id_priv->id.service_mask & service_id) ==
407 (service_mask & cur_cm_id_priv->id.service_id) &&
408 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
410 return cur_cm_id_priv;
412 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
413 link = &(*link)->rb_left;
414 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
415 link = &(*link)->rb_right;
416 else if (service_id < cur_cm_id_priv->id.service_id)
417 link = &(*link)->rb_left;
418 else if (service_id > cur_cm_id_priv->id.service_id)
419 link = &(*link)->rb_right;
420 else if (data_cmp < 0)
421 link = &(*link)->rb_left;
423 link = &(*link)->rb_right;
425 rb_link_node(&cm_id_priv->service_node, parent, link);
426 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
430 static struct cm_id_private * cm_find_listen(struct ib_device *device,
434 struct rb_node *node = cm.listen_service_table.rb_node;
435 struct cm_id_private *cm_id_priv;
439 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
440 data_cmp = cm_compare_private_data(private_data,
441 cm_id_priv->compare_data);
442 if ((cm_id_priv->id.service_mask & service_id) ==
443 cm_id_priv->id.service_id &&
444 (cm_id_priv->id.device == device) && !data_cmp)
447 if (device < cm_id_priv->id.device)
448 node = node->rb_left;
449 else if (device > cm_id_priv->id.device)
450 node = node->rb_right;
451 else if (service_id < cm_id_priv->id.service_id)
452 node = node->rb_left;
453 else if (service_id > cm_id_priv->id.service_id)
454 node = node->rb_right;
455 else if (data_cmp < 0)
456 node = node->rb_left;
458 node = node->rb_right;
463 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
466 struct rb_node **link = &cm.remote_id_table.rb_node;
467 struct rb_node *parent = NULL;
468 struct cm_timewait_info *cur_timewait_info;
469 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
470 __be32 remote_id = timewait_info->work.remote_id;
474 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
476 if (remote_id < cur_timewait_info->work.remote_id)
477 link = &(*link)->rb_left;
478 else if (remote_id > cur_timewait_info->work.remote_id)
479 link = &(*link)->rb_right;
480 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
481 link = &(*link)->rb_left;
482 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
483 link = &(*link)->rb_right;
485 return cur_timewait_info;
487 timewait_info->inserted_remote_id = 1;
488 rb_link_node(&timewait_info->remote_id_node, parent, link);
489 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
493 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
496 struct rb_node *node = cm.remote_id_table.rb_node;
497 struct cm_timewait_info *timewait_info;
500 timewait_info = rb_entry(node, struct cm_timewait_info,
502 if (remote_id < timewait_info->work.remote_id)
503 node = node->rb_left;
504 else if (remote_id > timewait_info->work.remote_id)
505 node = node->rb_right;
506 else if (remote_ca_guid < timewait_info->remote_ca_guid)
507 node = node->rb_left;
508 else if (remote_ca_guid > timewait_info->remote_ca_guid)
509 node = node->rb_right;
511 return timewait_info;
516 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
519 struct rb_node **link = &cm.remote_qp_table.rb_node;
520 struct rb_node *parent = NULL;
521 struct cm_timewait_info *cur_timewait_info;
522 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
523 __be32 remote_qpn = timewait_info->remote_qpn;
527 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
529 if (remote_qpn < cur_timewait_info->remote_qpn)
530 link = &(*link)->rb_left;
531 else if (remote_qpn > cur_timewait_info->remote_qpn)
532 link = &(*link)->rb_right;
533 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
534 link = &(*link)->rb_left;
535 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
536 link = &(*link)->rb_right;
538 return cur_timewait_info;
540 timewait_info->inserted_remote_qp = 1;
541 rb_link_node(&timewait_info->remote_qp_node, parent, link);
542 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
546 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
549 struct rb_node **link = &cm.remote_sidr_table.rb_node;
550 struct rb_node *parent = NULL;
551 struct cm_id_private *cur_cm_id_priv;
552 union ib_gid *port_gid = &cm_id_priv->av.dgid;
553 __be32 remote_id = cm_id_priv->id.remote_id;
557 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
559 if (remote_id < cur_cm_id_priv->id.remote_id)
560 link = &(*link)->rb_left;
561 else if (remote_id > cur_cm_id_priv->id.remote_id)
562 link = &(*link)->rb_right;
565 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
568 link = &(*link)->rb_left;
570 link = &(*link)->rb_right;
572 return cur_cm_id_priv;
575 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
576 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
580 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
581 enum ib_cm_sidr_status status)
583 struct ib_cm_sidr_rep_param param;
585 memset(¶m, 0, sizeof param);
586 param.status = status;
587 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
590 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
591 ib_cm_handler cm_handler,
594 struct cm_id_private *cm_id_priv;
597 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
601 cm_id_priv->id.state = IB_CM_IDLE;
602 cm_id_priv->id.device = device;
603 cm_id_priv->id.cm_handler = cm_handler;
604 cm_id_priv->id.context = context;
605 cm_id_priv->id.remote_cm_qpn = 1;
606 ret = cm_alloc_id(cm_id_priv);
610 spin_lock_init(&cm_id_priv->lock);
611 init_completion(&cm_id_priv->comp);
612 INIT_LIST_HEAD(&cm_id_priv->work_list);
613 atomic_set(&cm_id_priv->work_count, -1);
614 atomic_set(&cm_id_priv->refcount, 1);
615 return &cm_id_priv->id;
619 return ERR_PTR(-ENOMEM);
621 EXPORT_SYMBOL(ib_create_cm_id);
623 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
625 struct cm_work *work;
627 if (list_empty(&cm_id_priv->work_list))
630 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
631 list_del(&work->list);
635 static void cm_free_work(struct cm_work *work)
637 if (work->mad_recv_wc)
638 ib_free_recv_mad(work->mad_recv_wc);
642 static inline int cm_convert_to_ms(int iba_time)
644 /* approximate conversion to ms from 4.096us x 2^iba_time */
645 return 1 << max(iba_time - 8, 0);
648 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
652 if (!timewait_info->inserted_remote_id &&
653 !timewait_info->inserted_remote_qp)
656 spin_lock_irqsave(&cm.lock, flags);
657 if (timewait_info->inserted_remote_id) {
658 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
659 timewait_info->inserted_remote_id = 0;
662 if (timewait_info->inserted_remote_qp) {
663 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
664 timewait_info->inserted_remote_qp = 0;
666 spin_unlock_irqrestore(&cm.lock, flags);
669 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
671 struct cm_timewait_info *timewait_info;
673 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
675 return ERR_PTR(-ENOMEM);
677 timewait_info->work.local_id = local_id;
678 INIT_WORK(&timewait_info->work.work, cm_work_handler,
679 &timewait_info->work);
680 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
681 return timewait_info;
684 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
689 * The cm_id could be destroyed by the user before we exit timewait.
690 * To protect against this, we search for the cm_id after exiting
691 * timewait before notifying the user that we've exited timewait.
693 cm_id_priv->id.state = IB_CM_TIMEWAIT;
694 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
695 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
696 msecs_to_jiffies(wait_time));
697 cm_id_priv->timewait_info = NULL;
700 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
702 cm_id_priv->id.state = IB_CM_IDLE;
703 if (cm_id_priv->timewait_info) {
704 cm_cleanup_timewait(cm_id_priv->timewait_info);
705 kfree(cm_id_priv->timewait_info);
706 cm_id_priv->timewait_info = NULL;
710 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
712 struct cm_id_private *cm_id_priv;
713 struct cm_work *work;
716 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
718 spin_lock_irqsave(&cm_id_priv->lock, flags);
719 switch (cm_id->state) {
721 cm_id->state = IB_CM_IDLE;
722 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
723 spin_lock_irqsave(&cm.lock, flags);
724 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
725 spin_unlock_irqrestore(&cm.lock, flags);
727 case IB_CM_SIDR_REQ_SENT:
728 cm_id->state = IB_CM_IDLE;
729 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
730 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
732 case IB_CM_SIDR_REQ_RCVD:
733 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
734 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
737 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
738 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
739 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
740 &cm_id_priv->av.port->cm_dev->ca_guid,
741 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
745 if (err == -ENOMEM) {
746 /* Do not reject to allow future retries. */
747 cm_reset_to_idle(cm_id_priv);
748 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
750 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
751 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
755 case IB_CM_MRA_REQ_RCVD:
757 case IB_CM_MRA_REP_RCVD:
758 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
760 case IB_CM_MRA_REQ_SENT:
762 case IB_CM_MRA_REP_SENT:
763 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
764 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
767 case IB_CM_ESTABLISHED:
768 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
769 ib_send_cm_dreq(cm_id, NULL, 0);
771 case IB_CM_DREQ_SENT:
772 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
773 cm_enter_timewait(cm_id_priv);
774 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
776 case IB_CM_DREQ_RCVD:
777 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
778 ib_send_cm_drep(cm_id, NULL, 0);
781 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
785 cm_free_id(cm_id->local_id);
786 cm_deref_id(cm_id_priv);
787 wait_for_completion(&cm_id_priv->comp);
788 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
790 kfree(cm_id_priv->compare_data);
791 kfree(cm_id_priv->private_data);
795 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
797 cm_destroy_id(cm_id, 0);
799 EXPORT_SYMBOL(ib_destroy_cm_id);
801 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
802 struct ib_cm_compare_data *compare_data)
804 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
808 service_mask = service_mask ? service_mask :
809 __constant_cpu_to_be64(~0ULL);
810 service_id &= service_mask;
811 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
812 (service_id != IB_CM_ASSIGN_SERVICE_ID))
815 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
816 if (cm_id->state != IB_CM_IDLE)
820 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
822 if (!cm_id_priv->compare_data)
824 cm_mask_copy(cm_id_priv->compare_data->data,
825 compare_data->data, compare_data->mask);
826 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
830 cm_id->state = IB_CM_LISTEN;
832 spin_lock_irqsave(&cm.lock, flags);
833 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
834 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
835 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
837 cm_id->service_id = service_id;
838 cm_id->service_mask = service_mask;
840 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
841 spin_unlock_irqrestore(&cm.lock, flags);
843 if (cur_cm_id_priv) {
844 cm_id->state = IB_CM_IDLE;
845 kfree(cm_id_priv->compare_data);
846 cm_id_priv->compare_data = NULL;
851 EXPORT_SYMBOL(ib_cm_listen);
853 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
854 enum cm_msg_sequence msg_seq)
858 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
859 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
861 return cpu_to_be64(hi_tid | low_tid);
864 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
865 __be16 attr_id, __be64 tid)
867 hdr->base_version = IB_MGMT_BASE_VERSION;
868 hdr->mgmt_class = IB_MGMT_CLASS_CM;
869 hdr->class_version = IB_CM_CLASS_VERSION;
870 hdr->method = IB_MGMT_METHOD_SEND;
871 hdr->attr_id = attr_id;
875 static void cm_format_req(struct cm_req_msg *req_msg,
876 struct cm_id_private *cm_id_priv,
877 struct ib_cm_req_param *param)
879 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
880 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
882 req_msg->local_comm_id = cm_id_priv->id.local_id;
883 req_msg->service_id = param->service_id;
884 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
885 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
886 cm_req_set_resp_res(req_msg, param->responder_resources);
887 cm_req_set_init_depth(req_msg, param->initiator_depth);
888 cm_req_set_remote_resp_timeout(req_msg,
889 param->remote_cm_response_timeout);
890 cm_req_set_qp_type(req_msg, param->qp_type);
891 cm_req_set_flow_ctrl(req_msg, param->flow_control);
892 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
893 cm_req_set_local_resp_timeout(req_msg,
894 param->local_cm_response_timeout);
895 cm_req_set_retry_count(req_msg, param->retry_count);
896 req_msg->pkey = param->primary_path->pkey;
897 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
898 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
899 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
900 cm_req_set_srq(req_msg, param->srq);
902 req_msg->primary_local_lid = param->primary_path->slid;
903 req_msg->primary_remote_lid = param->primary_path->dlid;
904 req_msg->primary_local_gid = param->primary_path->sgid;
905 req_msg->primary_remote_gid = param->primary_path->dgid;
906 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
907 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
908 req_msg->primary_traffic_class = param->primary_path->traffic_class;
909 req_msg->primary_hop_limit = param->primary_path->hop_limit;
910 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
911 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
912 cm_req_set_primary_local_ack_timeout(req_msg,
913 min(31, param->primary_path->packet_life_time + 1));
915 if (param->alternate_path) {
916 req_msg->alt_local_lid = param->alternate_path->slid;
917 req_msg->alt_remote_lid = param->alternate_path->dlid;
918 req_msg->alt_local_gid = param->alternate_path->sgid;
919 req_msg->alt_remote_gid = param->alternate_path->dgid;
920 cm_req_set_alt_flow_label(req_msg,
921 param->alternate_path->flow_label);
922 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
923 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
924 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
925 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
926 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
927 cm_req_set_alt_local_ack_timeout(req_msg,
928 min(31, param->alternate_path->packet_life_time + 1));
931 if (param->private_data && param->private_data_len)
932 memcpy(req_msg->private_data, param->private_data,
933 param->private_data_len);
936 static int cm_validate_req_param(struct ib_cm_req_param *param)
938 /* peer-to-peer not supported */
939 if (param->peer_to_peer)
942 if (!param->primary_path)
945 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
948 if (param->private_data &&
949 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
952 if (param->alternate_path &&
953 (param->alternate_path->pkey != param->primary_path->pkey ||
954 param->alternate_path->mtu != param->primary_path->mtu))
960 int ib_send_cm_req(struct ib_cm_id *cm_id,
961 struct ib_cm_req_param *param)
963 struct cm_id_private *cm_id_priv;
964 struct cm_req_msg *req_msg;
968 ret = cm_validate_req_param(param);
972 /* Verify that we're not in timewait. */
973 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
974 spin_lock_irqsave(&cm_id_priv->lock, flags);
975 if (cm_id->state != IB_CM_IDLE) {
976 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
980 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
982 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
984 if (IS_ERR(cm_id_priv->timewait_info)) {
985 ret = PTR_ERR(cm_id_priv->timewait_info);
989 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
992 if (param->alternate_path) {
993 ret = cm_init_av_by_path(param->alternate_path,
994 &cm_id_priv->alt_av);
998 cm_id->service_id = param->service_id;
999 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1000 cm_id_priv->timeout_ms = cm_convert_to_ms(
1001 param->primary_path->packet_life_time) * 2 +
1003 param->remote_cm_response_timeout);
1004 cm_id_priv->max_cm_retries = param->max_cm_retries;
1005 cm_id_priv->initiator_depth = param->initiator_depth;
1006 cm_id_priv->responder_resources = param->responder_resources;
1007 cm_id_priv->retry_count = param->retry_count;
1008 cm_id_priv->path_mtu = param->primary_path->mtu;
1009 cm_id_priv->qp_type = param->qp_type;
1011 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1015 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1016 cm_format_req(req_msg, cm_id_priv, param);
1017 cm_id_priv->tid = req_msg->hdr.tid;
1018 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1019 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1021 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1022 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1023 cm_id_priv->local_ack_timeout =
1024 cm_req_get_primary_local_ack_timeout(req_msg);
1026 spin_lock_irqsave(&cm_id_priv->lock, flags);
1027 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1029 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1032 BUG_ON(cm_id->state != IB_CM_IDLE);
1033 cm_id->state = IB_CM_REQ_SENT;
1034 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1037 error2: cm_free_msg(cm_id_priv->msg);
1038 error1: kfree(cm_id_priv->timewait_info);
1041 EXPORT_SYMBOL(ib_send_cm_req);
1043 static int cm_issue_rej(struct cm_port *port,
1044 struct ib_mad_recv_wc *mad_recv_wc,
1045 enum ib_cm_rej_reason reason,
1046 enum cm_msg_response msg_rejected,
1047 void *ari, u8 ari_length)
1049 struct ib_mad_send_buf *msg = NULL;
1050 struct cm_rej_msg *rej_msg, *rcv_msg;
1053 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1057 /* We just need common CM header information. Cast to any message. */
1058 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1059 rej_msg = (struct cm_rej_msg *) msg->mad;
1061 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1062 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1063 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1064 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1065 rej_msg->reason = cpu_to_be16(reason);
1067 if (ari && ari_length) {
1068 cm_rej_set_reject_info_len(rej_msg, ari_length);
1069 memcpy(rej_msg->ari, ari, ari_length);
1072 ret = ib_post_send_mad(msg, NULL);
1079 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1080 __be32 local_qpn, __be32 remote_qpn)
1082 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1083 ((local_ca_guid == remote_ca_guid) &&
1084 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1087 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1088 struct ib_sa_path_rec *primary_path,
1089 struct ib_sa_path_rec *alt_path)
1091 memset(primary_path, 0, sizeof *primary_path);
1092 primary_path->dgid = req_msg->primary_local_gid;
1093 primary_path->sgid = req_msg->primary_remote_gid;
1094 primary_path->dlid = req_msg->primary_local_lid;
1095 primary_path->slid = req_msg->primary_remote_lid;
1096 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1097 primary_path->hop_limit = req_msg->primary_hop_limit;
1098 primary_path->traffic_class = req_msg->primary_traffic_class;
1099 primary_path->reversible = 1;
1100 primary_path->pkey = req_msg->pkey;
1101 primary_path->sl = cm_req_get_primary_sl(req_msg);
1102 primary_path->mtu_selector = IB_SA_EQ;
1103 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1104 primary_path->rate_selector = IB_SA_EQ;
1105 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1106 primary_path->packet_life_time_selector = IB_SA_EQ;
1107 primary_path->packet_life_time =
1108 cm_req_get_primary_local_ack_timeout(req_msg);
1109 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1111 if (req_msg->alt_local_lid) {
1112 memset(alt_path, 0, sizeof *alt_path);
1113 alt_path->dgid = req_msg->alt_local_gid;
1114 alt_path->sgid = req_msg->alt_remote_gid;
1115 alt_path->dlid = req_msg->alt_local_lid;
1116 alt_path->slid = req_msg->alt_remote_lid;
1117 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1118 alt_path->hop_limit = req_msg->alt_hop_limit;
1119 alt_path->traffic_class = req_msg->alt_traffic_class;
1120 alt_path->reversible = 1;
1121 alt_path->pkey = req_msg->pkey;
1122 alt_path->sl = cm_req_get_alt_sl(req_msg);
1123 alt_path->mtu_selector = IB_SA_EQ;
1124 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1125 alt_path->rate_selector = IB_SA_EQ;
1126 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1127 alt_path->packet_life_time_selector = IB_SA_EQ;
1128 alt_path->packet_life_time =
1129 cm_req_get_alt_local_ack_timeout(req_msg);
1130 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1134 static void cm_format_req_event(struct cm_work *work,
1135 struct cm_id_private *cm_id_priv,
1136 struct ib_cm_id *listen_id)
1138 struct cm_req_msg *req_msg;
1139 struct ib_cm_req_event_param *param;
1141 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1142 param = &work->cm_event.param.req_rcvd;
1143 param->listen_id = listen_id;
1144 param->port = cm_id_priv->av.port->port_num;
1145 param->primary_path = &work->path[0];
1146 if (req_msg->alt_local_lid)
1147 param->alternate_path = &work->path[1];
1149 param->alternate_path = NULL;
1150 param->remote_ca_guid = req_msg->local_ca_guid;
1151 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1152 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1153 param->qp_type = cm_req_get_qp_type(req_msg);
1154 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1155 param->responder_resources = cm_req_get_init_depth(req_msg);
1156 param->initiator_depth = cm_req_get_resp_res(req_msg);
1157 param->local_cm_response_timeout =
1158 cm_req_get_remote_resp_timeout(req_msg);
1159 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1160 param->remote_cm_response_timeout =
1161 cm_req_get_local_resp_timeout(req_msg);
1162 param->retry_count = cm_req_get_retry_count(req_msg);
1163 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1164 param->srq = cm_req_get_srq(req_msg);
1165 work->cm_event.private_data = &req_msg->private_data;
1168 static void cm_process_work(struct cm_id_private *cm_id_priv,
1169 struct cm_work *work)
1171 unsigned long flags;
1174 /* We will typically only have the current event to report. */
1175 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1178 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1179 spin_lock_irqsave(&cm_id_priv->lock, flags);
1180 work = cm_dequeue_work(cm_id_priv);
1181 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1183 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1187 cm_deref_id(cm_id_priv);
1189 cm_destroy_id(&cm_id_priv->id, ret);
1192 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1193 struct cm_id_private *cm_id_priv,
1194 enum cm_msg_response msg_mraed, u8 service_timeout,
1195 const void *private_data, u8 private_data_len)
1197 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1198 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1199 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1200 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1201 cm_mra_set_service_timeout(mra_msg, service_timeout);
1203 if (private_data && private_data_len)
1204 memcpy(mra_msg->private_data, private_data, private_data_len);
1207 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1208 struct cm_id_private *cm_id_priv,
1209 enum ib_cm_rej_reason reason,
1212 const void *private_data,
1213 u8 private_data_len)
1215 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1216 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1218 switch(cm_id_priv->id.state) {
1219 case IB_CM_REQ_RCVD:
1220 rej_msg->local_comm_id = 0;
1221 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1223 case IB_CM_MRA_REQ_SENT:
1224 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1225 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1227 case IB_CM_REP_RCVD:
1228 case IB_CM_MRA_REP_SENT:
1229 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1230 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1233 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1234 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1238 rej_msg->reason = cpu_to_be16(reason);
1239 if (ari && ari_length) {
1240 cm_rej_set_reject_info_len(rej_msg, ari_length);
1241 memcpy(rej_msg->ari, ari, ari_length);
1244 if (private_data && private_data_len)
1245 memcpy(rej_msg->private_data, private_data, private_data_len);
1248 static void cm_dup_req_handler(struct cm_work *work,
1249 struct cm_id_private *cm_id_priv)
1251 struct ib_mad_send_buf *msg = NULL;
1252 unsigned long flags;
1255 /* Quick state check to discard duplicate REQs. */
1256 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1259 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1263 spin_lock_irqsave(&cm_id_priv->lock, flags);
1264 switch (cm_id_priv->id.state) {
1265 case IB_CM_MRA_REQ_SENT:
1266 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1267 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1268 cm_id_priv->private_data,
1269 cm_id_priv->private_data_len);
1271 case IB_CM_TIMEWAIT:
1272 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1273 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1278 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1280 ret = ib_post_send_mad(msg, NULL);
1285 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1286 free: cm_free_msg(msg);
1289 static struct cm_id_private * cm_match_req(struct cm_work *work,
1290 struct cm_id_private *cm_id_priv)
1292 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1293 struct cm_timewait_info *timewait_info;
1294 struct cm_req_msg *req_msg;
1295 unsigned long flags;
1297 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1299 /* Check for duplicate REQ and stale connections. */
1300 spin_lock_irqsave(&cm.lock, flags);
1301 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1303 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1305 if (timewait_info) {
1306 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1307 timewait_info->work.remote_id);
1308 spin_unlock_irqrestore(&cm.lock, flags);
1309 if (cur_cm_id_priv) {
1310 cm_dup_req_handler(work, cur_cm_id_priv);
1311 cm_deref_id(cur_cm_id_priv);
1313 cm_issue_rej(work->port, work->mad_recv_wc,
1314 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1319 /* Find matching listen request. */
1320 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1321 req_msg->service_id,
1322 req_msg->private_data);
1323 if (!listen_cm_id_priv) {
1324 spin_unlock_irqrestore(&cm.lock, flags);
1325 cm_issue_rej(work->port, work->mad_recv_wc,
1326 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1330 atomic_inc(&listen_cm_id_priv->refcount);
1331 atomic_inc(&cm_id_priv->refcount);
1332 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1333 atomic_inc(&cm_id_priv->work_count);
1334 spin_unlock_irqrestore(&cm.lock, flags);
1335 return listen_cm_id_priv;
1337 error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1341 static int cm_req_handler(struct cm_work *work)
1343 struct ib_cm_id *cm_id;
1344 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1345 struct cm_req_msg *req_msg;
1348 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1350 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1352 return PTR_ERR(cm_id);
1354 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1355 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1356 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1357 work->mad_recv_wc->recv_buf.grh,
1359 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1361 if (IS_ERR(cm_id_priv->timewait_info)) {
1362 ret = PTR_ERR(cm_id_priv->timewait_info);
1365 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1366 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1367 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1369 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1370 if (!listen_cm_id_priv) {
1372 kfree(cm_id_priv->timewait_info);
1376 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1377 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1378 cm_id_priv->id.service_id = req_msg->service_id;
1379 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1381 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1382 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1384 ib_get_cached_gid(work->port->cm_dev->device,
1385 work->port->port_num, 0, &work->path[0].sgid);
1386 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1387 &work->path[0].sgid, sizeof work->path[0].sgid,
1391 if (req_msg->alt_local_lid) {
1392 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1394 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1395 &work->path[0].sgid,
1396 sizeof work->path[0].sgid, NULL, 0);
1400 cm_id_priv->tid = req_msg->hdr.tid;
1401 cm_id_priv->timeout_ms = cm_convert_to_ms(
1402 cm_req_get_local_resp_timeout(req_msg));
1403 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1404 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1405 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1406 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1407 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1408 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1409 cm_id_priv->local_ack_timeout =
1410 cm_req_get_primary_local_ack_timeout(req_msg);
1411 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1412 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1413 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1415 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1416 cm_process_work(cm_id_priv, work);
1417 cm_deref_id(listen_cm_id_priv);
1421 atomic_dec(&cm_id_priv->refcount);
1422 cm_deref_id(listen_cm_id_priv);
1424 ib_destroy_cm_id(cm_id);
1428 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1429 struct cm_id_private *cm_id_priv,
1430 struct ib_cm_rep_param *param)
1432 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1433 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1434 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1435 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1436 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1437 rep_msg->resp_resources = param->responder_resources;
1438 rep_msg->initiator_depth = param->initiator_depth;
1439 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1440 cm_rep_set_failover(rep_msg, param->failover_accepted);
1441 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1442 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1443 cm_rep_set_srq(rep_msg, param->srq);
1444 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1446 if (param->private_data && param->private_data_len)
1447 memcpy(rep_msg->private_data, param->private_data,
1448 param->private_data_len);
1451 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1452 struct ib_cm_rep_param *param)
1454 struct cm_id_private *cm_id_priv;
1455 struct ib_mad_send_buf *msg;
1456 struct cm_rep_msg *rep_msg;
1457 unsigned long flags;
1460 if (param->private_data &&
1461 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1464 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1465 spin_lock_irqsave(&cm_id_priv->lock, flags);
1466 if (cm_id->state != IB_CM_REQ_RCVD &&
1467 cm_id->state != IB_CM_MRA_REQ_SENT) {
1472 ret = cm_alloc_msg(cm_id_priv, &msg);
1476 rep_msg = (struct cm_rep_msg *) msg->mad;
1477 cm_format_rep(rep_msg, cm_id_priv, param);
1478 msg->timeout_ms = cm_id_priv->timeout_ms;
1479 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1481 ret = ib_post_send_mad(msg, NULL);
1483 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1488 cm_id->state = IB_CM_REP_SENT;
1489 cm_id_priv->msg = msg;
1490 cm_id_priv->initiator_depth = param->initiator_depth;
1491 cm_id_priv->responder_resources = param->responder_resources;
1492 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1493 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1495 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1498 EXPORT_SYMBOL(ib_send_cm_rep);
1500 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1501 struct cm_id_private *cm_id_priv,
1502 const void *private_data,
1503 u8 private_data_len)
1505 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1506 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1507 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1509 if (private_data && private_data_len)
1510 memcpy(rtu_msg->private_data, private_data, private_data_len);
1513 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1514 const void *private_data,
1515 u8 private_data_len)
1517 struct cm_id_private *cm_id_priv;
1518 struct ib_mad_send_buf *msg;
1519 unsigned long flags;
1523 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1526 data = cm_copy_private_data(private_data, private_data_len);
1528 return PTR_ERR(data);
1530 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1531 spin_lock_irqsave(&cm_id_priv->lock, flags);
1532 if (cm_id->state != IB_CM_REP_RCVD &&
1533 cm_id->state != IB_CM_MRA_REP_SENT) {
1538 ret = cm_alloc_msg(cm_id_priv, &msg);
1542 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1543 private_data, private_data_len);
1545 ret = ib_post_send_mad(msg, NULL);
1547 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1553 cm_id->state = IB_CM_ESTABLISHED;
1554 cm_set_private_data(cm_id_priv, data, private_data_len);
1555 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1558 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1562 EXPORT_SYMBOL(ib_send_cm_rtu);
1564 static void cm_format_rep_event(struct cm_work *work)
1566 struct cm_rep_msg *rep_msg;
1567 struct ib_cm_rep_event_param *param;
1569 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1570 param = &work->cm_event.param.rep_rcvd;
1571 param->remote_ca_guid = rep_msg->local_ca_guid;
1572 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1573 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1574 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1575 param->responder_resources = rep_msg->initiator_depth;
1576 param->initiator_depth = rep_msg->resp_resources;
1577 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1578 param->failover_accepted = cm_rep_get_failover(rep_msg);
1579 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1580 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1581 param->srq = cm_rep_get_srq(rep_msg);
1582 work->cm_event.private_data = &rep_msg->private_data;
1585 static void cm_dup_rep_handler(struct cm_work *work)
1587 struct cm_id_private *cm_id_priv;
1588 struct cm_rep_msg *rep_msg;
1589 struct ib_mad_send_buf *msg = NULL;
1590 unsigned long flags;
1593 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1594 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1595 rep_msg->local_comm_id);
1599 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1603 spin_lock_irqsave(&cm_id_priv->lock, flags);
1604 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1605 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1606 cm_id_priv->private_data,
1607 cm_id_priv->private_data_len);
1608 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1609 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1610 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1611 cm_id_priv->private_data,
1612 cm_id_priv->private_data_len);
1615 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1617 ret = ib_post_send_mad(msg, NULL);
1622 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1623 free: cm_free_msg(msg);
1624 deref: cm_deref_id(cm_id_priv);
1627 static int cm_rep_handler(struct cm_work *work)
1629 struct cm_id_private *cm_id_priv;
1630 struct cm_rep_msg *rep_msg;
1631 unsigned long flags;
1634 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1635 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1637 cm_dup_rep_handler(work);
1641 cm_format_rep_event(work);
1643 spin_lock_irqsave(&cm_id_priv->lock, flags);
1644 switch (cm_id_priv->id.state) {
1645 case IB_CM_REQ_SENT:
1646 case IB_CM_MRA_REQ_RCVD:
1649 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1654 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1655 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1656 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1658 spin_lock(&cm.lock);
1659 /* Check for duplicate REP. */
1660 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1661 spin_unlock(&cm.lock);
1662 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1666 /* Check for a stale connection. */
1667 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1668 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1669 &cm.remote_id_table);
1670 cm_id_priv->timewait_info->inserted_remote_id = 0;
1671 spin_unlock(&cm.lock);
1672 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1673 cm_issue_rej(work->port, work->mad_recv_wc,
1674 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1679 spin_unlock(&cm.lock);
1681 cm_id_priv->id.state = IB_CM_REP_RCVD;
1682 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1683 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1684 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1685 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1686 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1687 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1689 /* todo: handle peer_to_peer */
1691 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1692 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1694 list_add_tail(&work->list, &cm_id_priv->work_list);
1695 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1698 cm_process_work(cm_id_priv, work);
1700 cm_deref_id(cm_id_priv);
1704 cm_deref_id(cm_id_priv);
1708 static int cm_establish_handler(struct cm_work *work)
1710 struct cm_id_private *cm_id_priv;
1711 unsigned long flags;
1714 /* See comment in ib_cm_establish about lookup. */
1715 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1719 spin_lock_irqsave(&cm_id_priv->lock, flags);
1720 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1721 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1725 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1726 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1728 list_add_tail(&work->list, &cm_id_priv->work_list);
1729 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1732 cm_process_work(cm_id_priv, work);
1734 cm_deref_id(cm_id_priv);
1737 cm_deref_id(cm_id_priv);
1741 static int cm_rtu_handler(struct cm_work *work)
1743 struct cm_id_private *cm_id_priv;
1744 struct cm_rtu_msg *rtu_msg;
1745 unsigned long flags;
1748 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1749 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1750 rtu_msg->local_comm_id);
1754 work->cm_event.private_data = &rtu_msg->private_data;
1756 spin_lock_irqsave(&cm_id_priv->lock, flags);
1757 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1758 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1759 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1762 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1764 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1765 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1767 list_add_tail(&work->list, &cm_id_priv->work_list);
1768 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1771 cm_process_work(cm_id_priv, work);
1773 cm_deref_id(cm_id_priv);
1776 cm_deref_id(cm_id_priv);
1780 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1781 struct cm_id_private *cm_id_priv,
1782 const void *private_data,
1783 u8 private_data_len)
1785 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1786 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1787 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1788 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1789 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1791 if (private_data && private_data_len)
1792 memcpy(dreq_msg->private_data, private_data, private_data_len);
1795 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1796 const void *private_data,
1797 u8 private_data_len)
1799 struct cm_id_private *cm_id_priv;
1800 struct ib_mad_send_buf *msg;
1801 unsigned long flags;
1804 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1807 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1808 spin_lock_irqsave(&cm_id_priv->lock, flags);
1809 if (cm_id->state != IB_CM_ESTABLISHED) {
1814 ret = cm_alloc_msg(cm_id_priv, &msg);
1816 cm_enter_timewait(cm_id_priv);
1820 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1821 private_data, private_data_len);
1822 msg->timeout_ms = cm_id_priv->timeout_ms;
1823 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1825 ret = ib_post_send_mad(msg, NULL);
1827 cm_enter_timewait(cm_id_priv);
1828 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1833 cm_id->state = IB_CM_DREQ_SENT;
1834 cm_id_priv->msg = msg;
1835 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1838 EXPORT_SYMBOL(ib_send_cm_dreq);
1840 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1841 struct cm_id_private *cm_id_priv,
1842 const void *private_data,
1843 u8 private_data_len)
1845 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1846 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1847 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1849 if (private_data && private_data_len)
1850 memcpy(drep_msg->private_data, private_data, private_data_len);
1853 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1854 const void *private_data,
1855 u8 private_data_len)
1857 struct cm_id_private *cm_id_priv;
1858 struct ib_mad_send_buf *msg;
1859 unsigned long flags;
1863 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1866 data = cm_copy_private_data(private_data, private_data_len);
1868 return PTR_ERR(data);
1870 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1871 spin_lock_irqsave(&cm_id_priv->lock, flags);
1872 if (cm_id->state != IB_CM_DREQ_RCVD) {
1873 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1878 cm_set_private_data(cm_id_priv, data, private_data_len);
1879 cm_enter_timewait(cm_id_priv);
1881 ret = cm_alloc_msg(cm_id_priv, &msg);
1885 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1886 private_data, private_data_len);
1888 ret = ib_post_send_mad(msg, NULL);
1890 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1895 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1898 EXPORT_SYMBOL(ib_send_cm_drep);
1900 static int cm_dreq_handler(struct cm_work *work)
1902 struct cm_id_private *cm_id_priv;
1903 struct cm_dreq_msg *dreq_msg;
1904 struct ib_mad_send_buf *msg = NULL;
1905 unsigned long flags;
1908 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1909 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1910 dreq_msg->local_comm_id);
1914 work->cm_event.private_data = &dreq_msg->private_data;
1916 spin_lock_irqsave(&cm_id_priv->lock, flags);
1917 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1920 switch (cm_id_priv->id.state) {
1921 case IB_CM_REP_SENT:
1922 case IB_CM_DREQ_SENT:
1923 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1925 case IB_CM_ESTABLISHED:
1926 case IB_CM_MRA_REP_RCVD:
1928 case IB_CM_TIMEWAIT:
1929 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1932 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1933 cm_id_priv->private_data,
1934 cm_id_priv->private_data_len);
1935 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1937 if (ib_post_send_mad(msg, NULL))
1943 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1944 cm_id_priv->tid = dreq_msg->hdr.tid;
1945 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1947 list_add_tail(&work->list, &cm_id_priv->work_list);
1948 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1951 cm_process_work(cm_id_priv, work);
1953 cm_deref_id(cm_id_priv);
1956 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1957 deref: cm_deref_id(cm_id_priv);
1961 static int cm_drep_handler(struct cm_work *work)
1963 struct cm_id_private *cm_id_priv;
1964 struct cm_drep_msg *drep_msg;
1965 unsigned long flags;
1968 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1969 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1970 drep_msg->local_comm_id);
1974 work->cm_event.private_data = &drep_msg->private_data;
1976 spin_lock_irqsave(&cm_id_priv->lock, flags);
1977 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1978 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1979 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1982 cm_enter_timewait(cm_id_priv);
1984 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1985 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1987 list_add_tail(&work->list, &cm_id_priv->work_list);
1988 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1991 cm_process_work(cm_id_priv, work);
1993 cm_deref_id(cm_id_priv);
1996 cm_deref_id(cm_id_priv);
2000 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2001 enum ib_cm_rej_reason reason,
2004 const void *private_data,
2005 u8 private_data_len)
2007 struct cm_id_private *cm_id_priv;
2008 struct ib_mad_send_buf *msg;
2009 unsigned long flags;
2012 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2013 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2016 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2018 spin_lock_irqsave(&cm_id_priv->lock, flags);
2019 switch (cm_id->state) {
2020 case IB_CM_REQ_SENT:
2021 case IB_CM_MRA_REQ_RCVD:
2022 case IB_CM_REQ_RCVD:
2023 case IB_CM_MRA_REQ_SENT:
2024 case IB_CM_REP_RCVD:
2025 case IB_CM_MRA_REP_SENT:
2026 ret = cm_alloc_msg(cm_id_priv, &msg);
2028 cm_format_rej((struct cm_rej_msg *) msg->mad,
2029 cm_id_priv, reason, ari, ari_length,
2030 private_data, private_data_len);
2032 cm_reset_to_idle(cm_id_priv);
2034 case IB_CM_REP_SENT:
2035 case IB_CM_MRA_REP_RCVD:
2036 ret = cm_alloc_msg(cm_id_priv, &msg);
2038 cm_format_rej((struct cm_rej_msg *) msg->mad,
2039 cm_id_priv, reason, ari, ari_length,
2040 private_data, private_data_len);
2042 cm_enter_timewait(cm_id_priv);
2052 ret = ib_post_send_mad(msg, NULL);
2056 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2059 EXPORT_SYMBOL(ib_send_cm_rej);
2061 static void cm_format_rej_event(struct cm_work *work)
2063 struct cm_rej_msg *rej_msg;
2064 struct ib_cm_rej_event_param *param;
2066 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2067 param = &work->cm_event.param.rej_rcvd;
2068 param->ari = rej_msg->ari;
2069 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2070 param->reason = __be16_to_cpu(rej_msg->reason);
2071 work->cm_event.private_data = &rej_msg->private_data;
2074 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2076 struct cm_timewait_info *timewait_info;
2077 struct cm_id_private *cm_id_priv;
2078 unsigned long flags;
2081 remote_id = rej_msg->local_comm_id;
2083 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2084 spin_lock_irqsave(&cm.lock, flags);
2085 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2087 if (!timewait_info) {
2088 spin_unlock_irqrestore(&cm.lock, flags);
2091 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2092 (timewait_info->work.local_id ^
2093 cm.random_id_operand));
2095 if (cm_id_priv->id.remote_id == remote_id)
2096 atomic_inc(&cm_id_priv->refcount);
2100 spin_unlock_irqrestore(&cm.lock, flags);
2101 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2102 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2104 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2109 static int cm_rej_handler(struct cm_work *work)
2111 struct cm_id_private *cm_id_priv;
2112 struct cm_rej_msg *rej_msg;
2113 unsigned long flags;
2116 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2117 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2121 cm_format_rej_event(work);
2123 spin_lock_irqsave(&cm_id_priv->lock, flags);
2124 switch (cm_id_priv->id.state) {
2125 case IB_CM_REQ_SENT:
2126 case IB_CM_MRA_REQ_RCVD:
2127 case IB_CM_REP_SENT:
2128 case IB_CM_MRA_REP_RCVD:
2129 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2131 case IB_CM_REQ_RCVD:
2132 case IB_CM_MRA_REQ_SENT:
2133 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2134 cm_enter_timewait(cm_id_priv);
2136 cm_reset_to_idle(cm_id_priv);
2138 case IB_CM_DREQ_SENT:
2139 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2141 case IB_CM_REP_RCVD:
2142 case IB_CM_MRA_REP_SENT:
2143 case IB_CM_ESTABLISHED:
2144 cm_enter_timewait(cm_id_priv);
2147 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2152 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2154 list_add_tail(&work->list, &cm_id_priv->work_list);
2155 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2158 cm_process_work(cm_id_priv, work);
2160 cm_deref_id(cm_id_priv);
2163 cm_deref_id(cm_id_priv);
2167 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2169 const void *private_data,
2170 u8 private_data_len)
2172 struct cm_id_private *cm_id_priv;
2173 struct ib_mad_send_buf *msg;
2175 unsigned long flags;
2178 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2181 data = cm_copy_private_data(private_data, private_data_len);
2183 return PTR_ERR(data);
2185 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2187 spin_lock_irqsave(&cm_id_priv->lock, flags);
2188 switch(cm_id_priv->id.state) {
2189 case IB_CM_REQ_RCVD:
2190 ret = cm_alloc_msg(cm_id_priv, &msg);
2194 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2195 CM_MSG_RESPONSE_REQ, service_timeout,
2196 private_data, private_data_len);
2197 ret = ib_post_send_mad(msg, NULL);
2200 cm_id->state = IB_CM_MRA_REQ_SENT;
2202 case IB_CM_REP_RCVD:
2203 ret = cm_alloc_msg(cm_id_priv, &msg);
2207 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2208 CM_MSG_RESPONSE_REP, service_timeout,
2209 private_data, private_data_len);
2210 ret = ib_post_send_mad(msg, NULL);
2213 cm_id->state = IB_CM_MRA_REP_SENT;
2215 case IB_CM_ESTABLISHED:
2216 ret = cm_alloc_msg(cm_id_priv, &msg);
2220 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2221 CM_MSG_RESPONSE_OTHER, service_timeout,
2222 private_data, private_data_len);
2223 ret = ib_post_send_mad(msg, NULL);
2226 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2232 cm_id_priv->service_timeout = service_timeout;
2233 cm_set_private_data(cm_id_priv, data, private_data_len);
2234 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2237 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2241 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2246 EXPORT_SYMBOL(ib_send_cm_mra);
2248 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2250 switch (cm_mra_get_msg_mraed(mra_msg)) {
2251 case CM_MSG_RESPONSE_REQ:
2252 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2253 case CM_MSG_RESPONSE_REP:
2254 case CM_MSG_RESPONSE_OTHER:
2255 return cm_acquire_id(mra_msg->remote_comm_id,
2256 mra_msg->local_comm_id);
2262 static int cm_mra_handler(struct cm_work *work)
2264 struct cm_id_private *cm_id_priv;
2265 struct cm_mra_msg *mra_msg;
2266 unsigned long flags;
2269 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2270 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2274 work->cm_event.private_data = &mra_msg->private_data;
2275 work->cm_event.param.mra_rcvd.service_timeout =
2276 cm_mra_get_service_timeout(mra_msg);
2277 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2278 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2280 spin_lock_irqsave(&cm_id_priv->lock, flags);
2281 switch (cm_id_priv->id.state) {
2282 case IB_CM_REQ_SENT:
2283 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2284 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2285 cm_id_priv->msg, timeout))
2287 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2289 case IB_CM_REP_SENT:
2290 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2291 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2292 cm_id_priv->msg, timeout))
2294 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2296 case IB_CM_ESTABLISHED:
2297 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2298 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2299 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2300 cm_id_priv->msg, timeout))
2302 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2308 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2309 cm_id_priv->id.state;
2310 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2312 list_add_tail(&work->list, &cm_id_priv->work_list);
2313 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2316 cm_process_work(cm_id_priv, work);
2318 cm_deref_id(cm_id_priv);
2321 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2322 cm_deref_id(cm_id_priv);
2326 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2327 struct cm_id_private *cm_id_priv,
2328 struct ib_sa_path_rec *alternate_path,
2329 const void *private_data,
2330 u8 private_data_len)
2332 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2333 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2334 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2335 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2336 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2337 /* todo: need remote CM response timeout */
2338 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2339 lap_msg->alt_local_lid = alternate_path->slid;
2340 lap_msg->alt_remote_lid = alternate_path->dlid;
2341 lap_msg->alt_local_gid = alternate_path->sgid;
2342 lap_msg->alt_remote_gid = alternate_path->dgid;
2343 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2344 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2345 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2346 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2347 cm_lap_set_sl(lap_msg, alternate_path->sl);
2348 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2349 cm_lap_set_local_ack_timeout(lap_msg,
2350 min(31, alternate_path->packet_life_time + 1));
2352 if (private_data && private_data_len)
2353 memcpy(lap_msg->private_data, private_data, private_data_len);
2356 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2357 struct ib_sa_path_rec *alternate_path,
2358 const void *private_data,
2359 u8 private_data_len)
2361 struct cm_id_private *cm_id_priv;
2362 struct ib_mad_send_buf *msg;
2363 unsigned long flags;
2366 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2369 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2370 spin_lock_irqsave(&cm_id_priv->lock, flags);
2371 if (cm_id->state != IB_CM_ESTABLISHED ||
2372 cm_id->lap_state != IB_CM_LAP_IDLE) {
2377 ret = cm_alloc_msg(cm_id_priv, &msg);
2381 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2382 alternate_path, private_data, private_data_len);
2383 msg->timeout_ms = cm_id_priv->timeout_ms;
2384 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2386 ret = ib_post_send_mad(msg, NULL);
2388 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2393 cm_id->lap_state = IB_CM_LAP_SENT;
2394 cm_id_priv->msg = msg;
2396 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2399 EXPORT_SYMBOL(ib_send_cm_lap);
2401 static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2402 struct cm_lap_msg *lap_msg)
2404 memset(path, 0, sizeof *path);
2405 path->dgid = lap_msg->alt_local_gid;
2406 path->sgid = lap_msg->alt_remote_gid;
2407 path->dlid = lap_msg->alt_local_lid;
2408 path->slid = lap_msg->alt_remote_lid;
2409 path->flow_label = cm_lap_get_flow_label(lap_msg);
2410 path->hop_limit = lap_msg->alt_hop_limit;
2411 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2412 path->reversible = 1;
2413 /* pkey is same as in REQ */
2414 path->sl = cm_lap_get_sl(lap_msg);
2415 path->mtu_selector = IB_SA_EQ;
2416 /* mtu is same as in REQ */
2417 path->rate_selector = IB_SA_EQ;
2418 path->rate = cm_lap_get_packet_rate(lap_msg);
2419 path->packet_life_time_selector = IB_SA_EQ;
2420 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2421 path->packet_life_time -= (path->packet_life_time > 0);
2424 static int cm_lap_handler(struct cm_work *work)
2426 struct cm_id_private *cm_id_priv;
2427 struct cm_lap_msg *lap_msg;
2428 struct ib_cm_lap_event_param *param;
2429 struct ib_mad_send_buf *msg = NULL;
2430 unsigned long flags;
2433 /* todo: verify LAP request and send reject APR if invalid. */
2434 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2435 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2436 lap_msg->local_comm_id);
2440 param = &work->cm_event.param.lap_rcvd;
2441 param->alternate_path = &work->path[0];
2442 cm_format_path_from_lap(param->alternate_path, lap_msg);
2443 work->cm_event.private_data = &lap_msg->private_data;
2445 spin_lock_irqsave(&cm_id_priv->lock, flags);
2446 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2449 switch (cm_id_priv->id.lap_state) {
2450 case IB_CM_LAP_IDLE:
2452 case IB_CM_MRA_LAP_SENT:
2453 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2456 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2457 CM_MSG_RESPONSE_OTHER,
2458 cm_id_priv->service_timeout,
2459 cm_id_priv->private_data,
2460 cm_id_priv->private_data_len);
2461 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2463 if (ib_post_send_mad(msg, NULL))
2470 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2471 cm_id_priv->tid = lap_msg->hdr.tid;
2472 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2474 list_add_tail(&work->list, &cm_id_priv->work_list);
2475 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2478 cm_process_work(cm_id_priv, work);
2480 cm_deref_id(cm_id_priv);
2483 unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2484 deref: cm_deref_id(cm_id_priv);
2488 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2489 struct cm_id_private *cm_id_priv,
2490 enum ib_cm_apr_status status,
2493 const void *private_data,
2494 u8 private_data_len)
2496 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2497 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2498 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2499 apr_msg->ap_status = (u8) status;
2501 if (info && info_length) {
2502 apr_msg->info_length = info_length;
2503 memcpy(apr_msg->info, info, info_length);
2506 if (private_data && private_data_len)
2507 memcpy(apr_msg->private_data, private_data, private_data_len);
2510 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2511 enum ib_cm_apr_status status,
2514 const void *private_data,
2515 u8 private_data_len)
2517 struct cm_id_private *cm_id_priv;
2518 struct ib_mad_send_buf *msg;
2519 unsigned long flags;
2522 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2523 (info && info_length > IB_CM_APR_INFO_LENGTH))
2526 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2527 spin_lock_irqsave(&cm_id_priv->lock, flags);
2528 if (cm_id->state != IB_CM_ESTABLISHED ||
2529 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2530 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2535 ret = cm_alloc_msg(cm_id_priv, &msg);
2539 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2540 info, info_length, private_data, private_data_len);
2541 ret = ib_post_send_mad(msg, NULL);
2543 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2548 cm_id->lap_state = IB_CM_LAP_IDLE;
2549 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2552 EXPORT_SYMBOL(ib_send_cm_apr);
2554 static int cm_apr_handler(struct cm_work *work)
2556 struct cm_id_private *cm_id_priv;
2557 struct cm_apr_msg *apr_msg;
2558 unsigned long flags;
2561 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2562 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2563 apr_msg->local_comm_id);
2565 return -EINVAL; /* Unmatched reply. */
2567 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2568 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2569 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2570 work->cm_event.private_data = &apr_msg->private_data;
2572 spin_lock_irqsave(&cm_id_priv->lock, flags);
2573 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2574 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2575 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2576 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2579 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2580 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2581 cm_id_priv->msg = NULL;
2583 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2585 list_add_tail(&work->list, &cm_id_priv->work_list);
2586 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2589 cm_process_work(cm_id_priv, work);
2591 cm_deref_id(cm_id_priv);
2594 cm_deref_id(cm_id_priv);
2598 static int cm_timewait_handler(struct cm_work *work)
2600 struct cm_timewait_info *timewait_info;
2601 struct cm_id_private *cm_id_priv;
2602 unsigned long flags;
2605 timewait_info = (struct cm_timewait_info *)work;
2606 cm_cleanup_timewait(timewait_info);
2608 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2609 timewait_info->work.remote_id);
2613 spin_lock_irqsave(&cm_id_priv->lock, flags);
2614 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2615 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2616 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2619 cm_id_priv->id.state = IB_CM_IDLE;
2620 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2622 list_add_tail(&work->list, &cm_id_priv->work_list);
2623 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2626 cm_process_work(cm_id_priv, work);
2628 cm_deref_id(cm_id_priv);
2631 cm_deref_id(cm_id_priv);
2635 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2636 struct cm_id_private *cm_id_priv,
2637 struct ib_cm_sidr_req_param *param)
2639 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2640 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2641 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2642 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2643 sidr_req_msg->service_id = param->service_id;
2645 if (param->private_data && param->private_data_len)
2646 memcpy(sidr_req_msg->private_data, param->private_data,
2647 param->private_data_len);
2650 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2651 struct ib_cm_sidr_req_param *param)
2653 struct cm_id_private *cm_id_priv;
2654 struct ib_mad_send_buf *msg;
2655 unsigned long flags;
2658 if (!param->path || (param->private_data &&
2659 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2662 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2663 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2667 cm_id->service_id = param->service_id;
2668 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2669 cm_id_priv->timeout_ms = param->timeout_ms;
2670 cm_id_priv->max_cm_retries = param->max_cm_retries;
2671 ret = cm_alloc_msg(cm_id_priv, &msg);
2675 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2677 msg->timeout_ms = cm_id_priv->timeout_ms;
2678 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2680 spin_lock_irqsave(&cm_id_priv->lock, flags);
2681 if (cm_id->state == IB_CM_IDLE)
2682 ret = ib_post_send_mad(msg, NULL);
2687 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2691 cm_id->state = IB_CM_SIDR_REQ_SENT;
2692 cm_id_priv->msg = msg;
2693 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2697 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2699 static void cm_format_sidr_req_event(struct cm_work *work,
2700 struct ib_cm_id *listen_id)
2702 struct cm_sidr_req_msg *sidr_req_msg;
2703 struct ib_cm_sidr_req_event_param *param;
2705 sidr_req_msg = (struct cm_sidr_req_msg *)
2706 work->mad_recv_wc->recv_buf.mad;
2707 param = &work->cm_event.param.sidr_req_rcvd;
2708 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2709 param->listen_id = listen_id;
2710 param->port = work->port->port_num;
2711 work->cm_event.private_data = &sidr_req_msg->private_data;
2714 static int cm_sidr_req_handler(struct cm_work *work)
2716 struct ib_cm_id *cm_id;
2717 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2718 struct cm_sidr_req_msg *sidr_req_msg;
2720 unsigned long flags;
2722 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2724 return PTR_ERR(cm_id);
2725 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2727 /* Record SGID/SLID and request ID for lookup. */
2728 sidr_req_msg = (struct cm_sidr_req_msg *)
2729 work->mad_recv_wc->recv_buf.mad;
2730 wc = work->mad_recv_wc->wc;
2731 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2732 cm_id_priv->av.dgid.global.interface_id = 0;
2733 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2734 work->mad_recv_wc->recv_buf.grh,
2736 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2737 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2738 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2739 atomic_inc(&cm_id_priv->work_count);
2741 spin_lock_irqsave(&cm.lock, flags);
2742 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2743 if (cur_cm_id_priv) {
2744 spin_unlock_irqrestore(&cm.lock, flags);
2745 goto out; /* Duplicate message. */
2747 cur_cm_id_priv = cm_find_listen(cm_id->device,
2748 sidr_req_msg->service_id,
2749 sidr_req_msg->private_data);
2750 if (!cur_cm_id_priv) {
2751 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2752 spin_unlock_irqrestore(&cm.lock, flags);
2753 /* todo: reply with no match */
2754 goto out; /* No match. */
2756 atomic_inc(&cur_cm_id_priv->refcount);
2757 spin_unlock_irqrestore(&cm.lock, flags);
2759 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2760 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2761 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2762 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2764 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2765 cm_process_work(cm_id_priv, work);
2766 cm_deref_id(cur_cm_id_priv);
2769 ib_destroy_cm_id(&cm_id_priv->id);
2773 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2774 struct cm_id_private *cm_id_priv,
2775 struct ib_cm_sidr_rep_param *param)
2777 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2779 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2780 sidr_rep_msg->status = param->status;
2781 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2782 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2783 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2785 if (param->info && param->info_length)
2786 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2788 if (param->private_data && param->private_data_len)
2789 memcpy(sidr_rep_msg->private_data, param->private_data,
2790 param->private_data_len);
2793 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2794 struct ib_cm_sidr_rep_param *param)
2796 struct cm_id_private *cm_id_priv;
2797 struct ib_mad_send_buf *msg;
2798 unsigned long flags;
2801 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2802 (param->private_data &&
2803 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2806 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2807 spin_lock_irqsave(&cm_id_priv->lock, flags);
2808 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2813 ret = cm_alloc_msg(cm_id_priv, &msg);
2817 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2819 ret = ib_post_send_mad(msg, NULL);
2821 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2825 cm_id->state = IB_CM_IDLE;
2826 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2828 spin_lock_irqsave(&cm.lock, flags);
2829 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2830 spin_unlock_irqrestore(&cm.lock, flags);
2833 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2836 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2838 static void cm_format_sidr_rep_event(struct cm_work *work)
2840 struct cm_sidr_rep_msg *sidr_rep_msg;
2841 struct ib_cm_sidr_rep_event_param *param;
2843 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2844 work->mad_recv_wc->recv_buf.mad;
2845 param = &work->cm_event.param.sidr_rep_rcvd;
2846 param->status = sidr_rep_msg->status;
2847 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2848 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2849 param->info = &sidr_rep_msg->info;
2850 param->info_len = sidr_rep_msg->info_length;
2851 work->cm_event.private_data = &sidr_rep_msg->private_data;
2854 static int cm_sidr_rep_handler(struct cm_work *work)
2856 struct cm_sidr_rep_msg *sidr_rep_msg;
2857 struct cm_id_private *cm_id_priv;
2858 unsigned long flags;
2860 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2861 work->mad_recv_wc->recv_buf.mad;
2862 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2864 return -EINVAL; /* Unmatched reply. */
2866 spin_lock_irqsave(&cm_id_priv->lock, flags);
2867 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2868 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2871 cm_id_priv->id.state = IB_CM_IDLE;
2872 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2873 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2875 cm_format_sidr_rep_event(work);
2876 cm_process_work(cm_id_priv, work);
2879 cm_deref_id(cm_id_priv);
2883 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2884 enum ib_wc_status wc_status)
2886 struct cm_id_private *cm_id_priv;
2887 struct ib_cm_event cm_event;
2888 enum ib_cm_state state;
2889 unsigned long flags;
2892 memset(&cm_event, 0, sizeof cm_event);
2893 cm_id_priv = msg->context[0];
2895 /* Discard old sends or ones without a response. */
2896 spin_lock_irqsave(&cm_id_priv->lock, flags);
2897 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2898 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2902 case IB_CM_REQ_SENT:
2903 case IB_CM_MRA_REQ_RCVD:
2904 cm_reset_to_idle(cm_id_priv);
2905 cm_event.event = IB_CM_REQ_ERROR;
2907 case IB_CM_REP_SENT:
2908 case IB_CM_MRA_REP_RCVD:
2909 cm_reset_to_idle(cm_id_priv);
2910 cm_event.event = IB_CM_REP_ERROR;
2912 case IB_CM_DREQ_SENT:
2913 cm_enter_timewait(cm_id_priv);
2914 cm_event.event = IB_CM_DREQ_ERROR;
2916 case IB_CM_SIDR_REQ_SENT:
2917 cm_id_priv->id.state = IB_CM_IDLE;
2918 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2923 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2924 cm_event.param.send_status = wc_status;
2926 /* No other events can occur on the cm_id at this point. */
2927 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2930 ib_destroy_cm_id(&cm_id_priv->id);
2933 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2937 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2938 struct ib_mad_send_wc *mad_send_wc)
2940 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2942 switch (mad_send_wc->status) {
2944 case IB_WC_WR_FLUSH_ERR:
2948 if (msg->context[0] && msg->context[1])
2949 cm_process_send_error(msg, mad_send_wc->status);
2956 static void cm_work_handler(void *data)
2958 struct cm_work *work = data;
2961 switch (work->cm_event.event) {
2962 case IB_CM_REQ_RECEIVED:
2963 ret = cm_req_handler(work);
2965 case IB_CM_MRA_RECEIVED:
2966 ret = cm_mra_handler(work);
2968 case IB_CM_REJ_RECEIVED:
2969 ret = cm_rej_handler(work);
2971 case IB_CM_REP_RECEIVED:
2972 ret = cm_rep_handler(work);
2974 case IB_CM_RTU_RECEIVED:
2975 ret = cm_rtu_handler(work);
2977 case IB_CM_USER_ESTABLISHED:
2978 ret = cm_establish_handler(work);
2980 case IB_CM_DREQ_RECEIVED:
2981 ret = cm_dreq_handler(work);
2983 case IB_CM_DREP_RECEIVED:
2984 ret = cm_drep_handler(work);
2986 case IB_CM_SIDR_REQ_RECEIVED:
2987 ret = cm_sidr_req_handler(work);
2989 case IB_CM_SIDR_REP_RECEIVED:
2990 ret = cm_sidr_rep_handler(work);
2992 case IB_CM_LAP_RECEIVED:
2993 ret = cm_lap_handler(work);
2995 case IB_CM_APR_RECEIVED:
2996 ret = cm_apr_handler(work);
2998 case IB_CM_TIMEWAIT_EXIT:
2999 ret = cm_timewait_handler(work);
3009 int ib_cm_establish(struct ib_cm_id *cm_id)
3011 struct cm_id_private *cm_id_priv;
3012 struct cm_work *work;
3013 unsigned long flags;
3016 work = kmalloc(sizeof *work, GFP_ATOMIC);
3020 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3021 spin_lock_irqsave(&cm_id_priv->lock, flags);
3022 switch (cm_id->state)
3024 case IB_CM_REP_SENT:
3025 case IB_CM_MRA_REP_RCVD:
3026 cm_id->state = IB_CM_ESTABLISHED;
3028 case IB_CM_ESTABLISHED:
3035 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3043 * The CM worker thread may try to destroy the cm_id before it
3044 * can execute this work item. To prevent potential deadlock,
3045 * we need to find the cm_id once we're in the context of the
3046 * worker thread, rather than holding a reference on it.
3048 INIT_WORK(&work->work, cm_work_handler, work);
3049 work->local_id = cm_id->local_id;
3050 work->remote_id = cm_id->remote_id;
3051 work->mad_recv_wc = NULL;
3052 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3053 queue_work(cm.wq, &work->work);
3057 EXPORT_SYMBOL(ib_cm_establish);
3059 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3060 struct ib_mad_recv_wc *mad_recv_wc)
3062 struct cm_work *work;
3063 enum ib_cm_event_type event;
3066 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3067 case CM_REQ_ATTR_ID:
3068 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3069 alt_local_lid != 0);
3070 event = IB_CM_REQ_RECEIVED;
3072 case CM_MRA_ATTR_ID:
3073 event = IB_CM_MRA_RECEIVED;
3075 case CM_REJ_ATTR_ID:
3076 event = IB_CM_REJ_RECEIVED;
3078 case CM_REP_ATTR_ID:
3079 event = IB_CM_REP_RECEIVED;
3081 case CM_RTU_ATTR_ID:
3082 event = IB_CM_RTU_RECEIVED;
3084 case CM_DREQ_ATTR_ID:
3085 event = IB_CM_DREQ_RECEIVED;
3087 case CM_DREP_ATTR_ID:
3088 event = IB_CM_DREP_RECEIVED;
3090 case CM_SIDR_REQ_ATTR_ID:
3091 event = IB_CM_SIDR_REQ_RECEIVED;
3093 case CM_SIDR_REP_ATTR_ID:
3094 event = IB_CM_SIDR_REP_RECEIVED;
3096 case CM_LAP_ATTR_ID:
3098 event = IB_CM_LAP_RECEIVED;
3100 case CM_APR_ATTR_ID:
3101 event = IB_CM_APR_RECEIVED;
3104 ib_free_recv_mad(mad_recv_wc);
3108 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3111 ib_free_recv_mad(mad_recv_wc);
3115 INIT_WORK(&work->work, cm_work_handler, work);
3116 work->cm_event.event = event;
3117 work->mad_recv_wc = mad_recv_wc;
3118 work->port = (struct cm_port *)mad_agent->context;
3119 queue_work(cm.wq, &work->work);
3122 static int cm_init_qp_init_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_REQ_SENT:
3132 case IB_CM_MRA_REQ_RCVD:
3133 case IB_CM_REQ_RCVD:
3134 case IB_CM_MRA_REQ_SENT:
3135 case IB_CM_REP_RCVD:
3136 case IB_CM_MRA_REP_SENT:
3137 case IB_CM_REP_SENT:
3138 case IB_CM_MRA_REP_RCVD:
3139 case IB_CM_ESTABLISHED:
3140 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3141 IB_QP_PKEY_INDEX | IB_QP_PORT;
3142 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
3143 IB_ACCESS_REMOTE_WRITE;
3144 if (cm_id_priv->responder_resources)
3145 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3146 IB_ACCESS_REMOTE_ATOMIC;
3147 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3148 qp_attr->port_num = cm_id_priv->av.port->port_num;
3155 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3159 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3160 struct ib_qp_attr *qp_attr,
3163 unsigned long flags;
3166 spin_lock_irqsave(&cm_id_priv->lock, flags);
3167 switch (cm_id_priv->id.state) {
3168 case IB_CM_REQ_RCVD:
3169 case IB_CM_MRA_REQ_SENT:
3170 case IB_CM_REP_RCVD:
3171 case IB_CM_MRA_REP_SENT:
3172 case IB_CM_REP_SENT:
3173 case IB_CM_MRA_REP_RCVD:
3174 case IB_CM_ESTABLISHED:
3175 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3176 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3177 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3178 qp_attr->path_mtu = cm_id_priv->path_mtu;
3179 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3180 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3181 if (cm_id_priv->qp_type == IB_QPT_RC) {
3182 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3183 IB_QP_MIN_RNR_TIMER;
3184 qp_attr->max_dest_rd_atomic =
3185 cm_id_priv->responder_resources;
3186 qp_attr->min_rnr_timer = 0;
3188 if (cm_id_priv->alt_av.ah_attr.dlid) {
3189 *qp_attr_mask |= IB_QP_ALT_PATH;
3190 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3191 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3199 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3203 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3204 struct ib_qp_attr *qp_attr,
3207 unsigned long flags;
3210 spin_lock_irqsave(&cm_id_priv->lock, flags);
3211 switch (cm_id_priv->id.state) {
3212 case IB_CM_REP_RCVD:
3213 case IB_CM_MRA_REP_SENT:
3214 case IB_CM_REP_SENT:
3215 case IB_CM_MRA_REP_RCVD:
3216 case IB_CM_ESTABLISHED:
3217 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3218 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3219 if (cm_id_priv->qp_type == IB_QPT_RC) {
3220 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3222 IB_QP_MAX_QP_RD_ATOMIC;
3223 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3224 qp_attr->retry_cnt = cm_id_priv->retry_count;
3225 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3226 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3228 if (cm_id_priv->alt_av.ah_attr.dlid) {
3229 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3230 qp_attr->path_mig_state = IB_MIG_REARM;
3238 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3242 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3243 struct ib_qp_attr *qp_attr,
3246 struct cm_id_private *cm_id_priv;
3249 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3250 switch (qp_attr->qp_state) {
3252 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3255 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3258 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3266 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3268 static void cm_add_one(struct ib_device *device)
3270 struct cm_device *cm_dev;
3271 struct cm_port *port;
3272 struct ib_mad_reg_req reg_req = {
3273 .mgmt_class = IB_MGMT_CLASS_CM,
3274 .mgmt_class_version = IB_CM_CLASS_VERSION
3276 struct ib_port_modify port_modify = {
3277 .set_port_cap_mask = IB_PORT_CM_SUP
3279 unsigned long flags;
3283 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3284 device->phys_port_cnt, GFP_KERNEL);
3288 cm_dev->device = device;
3289 cm_dev->ca_guid = device->node_guid;
3291 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3292 for (i = 1; i <= device->phys_port_cnt; i++) {
3293 port = &cm_dev->port[i-1];
3294 port->cm_dev = cm_dev;
3296 port->mad_agent = ib_register_mad_agent(device, i,
3303 if (IS_ERR(port->mad_agent))
3306 ret = ib_modify_port(device, i, 0, &port_modify);
3310 ib_set_client_data(device, &cm_client, cm_dev);
3312 write_lock_irqsave(&cm.device_lock, flags);
3313 list_add_tail(&cm_dev->list, &cm.device_list);
3314 write_unlock_irqrestore(&cm.device_lock, flags);
3318 ib_unregister_mad_agent(port->mad_agent);
3320 port_modify.set_port_cap_mask = 0;
3321 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3323 port = &cm_dev->port[i-1];
3324 ib_modify_port(device, port->port_num, 0, &port_modify);
3325 ib_unregister_mad_agent(port->mad_agent);
3330 static void cm_remove_one(struct ib_device *device)
3332 struct cm_device *cm_dev;
3333 struct cm_port *port;
3334 struct ib_port_modify port_modify = {
3335 .clr_port_cap_mask = IB_PORT_CM_SUP
3337 unsigned long flags;
3340 cm_dev = ib_get_client_data(device, &cm_client);
3344 write_lock_irqsave(&cm.device_lock, flags);
3345 list_del(&cm_dev->list);
3346 write_unlock_irqrestore(&cm.device_lock, flags);
3348 for (i = 1; i <= device->phys_port_cnt; i++) {
3349 port = &cm_dev->port[i-1];
3350 ib_modify_port(device, port->port_num, 0, &port_modify);
3351 ib_unregister_mad_agent(port->mad_agent);
3356 static int __init ib_cm_init(void)
3360 memset(&cm, 0, sizeof cm);
3361 INIT_LIST_HEAD(&cm.device_list);
3362 rwlock_init(&cm.device_lock);
3363 spin_lock_init(&cm.lock);
3364 cm.listen_service_table = RB_ROOT;
3365 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3366 cm.remote_id_table = RB_ROOT;
3367 cm.remote_qp_table = RB_ROOT;
3368 cm.remote_sidr_table = RB_ROOT;
3369 idr_init(&cm.local_id_table);
3370 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3371 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3373 cm.wq = create_workqueue("ib_cm");
3377 ret = ib_register_client(&cm_client);
3383 destroy_workqueue(cm.wq);
3387 static void __exit ib_cm_cleanup(void)
3389 destroy_workqueue(cm.wq);
3390 ib_unregister_client(&cm_client);
3391 idr_destroy(&cm.local_id_table);
3394 module_init(ib_cm_init);
3395 module_exit(ib_cm_cleanup);