Merge branch 'upstream-davem' of master.kernel.org:/pub/scm/linux/kernel/git/linville...
[linux-2.6] / drivers / infiniband / core / cm.c
1 /*
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.
6  *
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:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
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.
25  *
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
33  * SOFTWARE.
34  *
35  * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
36  */
37
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/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/workqueue.h>
47
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
50 #include "cm_msgs.h"
51
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
55
56 static void cm_add_one(struct ib_device *device);
57 static void cm_remove_one(struct ib_device *device);
58
59 static struct ib_client cm_client = {
60         .name   = "cm",
61         .add    = cm_add_one,
62         .remove = cm_remove_one
63 };
64
65 static struct ib_cm {
66         spinlock_t lock;
67         struct list_head device_list;
68         rwlock_t device_lock;
69         struct rb_root listen_service_table;
70         u64 listen_service_id;
71         /* struct rb_root peer_service_table; todo: fix peer to peer */
72         struct rb_root remote_qp_table;
73         struct rb_root remote_id_table;
74         struct rb_root remote_sidr_table;
75         struct idr local_id_table;
76         __be32 random_id_operand;
77         struct list_head timewait_list;
78         struct workqueue_struct *wq;
79 } cm;
80
81 struct cm_port {
82         struct cm_device *cm_dev;
83         struct ib_mad_agent *mad_agent;
84         u8 port_num;
85 };
86
87 struct cm_device {
88         struct list_head list;
89         struct ib_device *device;
90         u8 ack_delay;
91         struct cm_port port[0];
92 };
93
94 struct cm_av {
95         struct cm_port *port;
96         union ib_gid dgid;
97         struct ib_ah_attr ah_attr;
98         u16 pkey_index;
99         u8 timeout;
100 };
101
102 struct cm_work {
103         struct delayed_work 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 */
108         __be32 remote_id;
109         struct ib_cm_event cm_event;
110         struct ib_sa_path_rec path[0];
111 };
112
113 struct cm_timewait_info {
114         struct cm_work work;                    /* Must be first. */
115         struct list_head list;
116         struct rb_node remote_qp_node;
117         struct rb_node remote_id_node;
118         __be64 remote_ca_guid;
119         __be32 remote_qpn;
120         u8 inserted_remote_qp;
121         u8 inserted_remote_id;
122 };
123
124 struct cm_id_private {
125         struct ib_cm_id id;
126
127         struct rb_node service_node;
128         struct rb_node sidr_id_node;
129         spinlock_t lock;        /* Do not acquire inside cm.lock */
130         struct completion comp;
131         atomic_t refcount;
132
133         struct ib_mad_send_buf *msg;
134         struct cm_timewait_info *timewait_info;
135         /* todo: use alternate port on send failure */
136         struct cm_av av;
137         struct cm_av alt_av;
138         struct ib_cm_compare_data *compare_data;
139
140         void *private_data;
141         __be64 tid;
142         __be32 local_qpn;
143         __be32 remote_qpn;
144         enum ib_qp_type qp_type;
145         __be32 sq_psn;
146         __be32 rq_psn;
147         int timeout_ms;
148         enum ib_mtu path_mtu;
149         __be16 pkey;
150         u8 private_data_len;
151         u8 max_cm_retries;
152         u8 peer_to_peer;
153         u8 responder_resources;
154         u8 initiator_depth;
155         u8 retry_count;
156         u8 rnr_retry_count;
157         u8 service_timeout;
158         u8 target_ack_delay;
159
160         struct list_head work_list;
161         atomic_t work_count;
162 };
163
164 static void cm_work_handler(struct work_struct *work);
165
166 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
167 {
168         if (atomic_dec_and_test(&cm_id_priv->refcount))
169                 complete(&cm_id_priv->comp);
170 }
171
172 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
173                         struct ib_mad_send_buf **msg)
174 {
175         struct ib_mad_agent *mad_agent;
176         struct ib_mad_send_buf *m;
177         struct ib_ah *ah;
178
179         mad_agent = cm_id_priv->av.port->mad_agent;
180         ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
181         if (IS_ERR(ah))
182                 return PTR_ERR(ah);
183
184         m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
185                                cm_id_priv->av.pkey_index,
186                                0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
187                                GFP_ATOMIC);
188         if (IS_ERR(m)) {
189                 ib_destroy_ah(ah);
190                 return PTR_ERR(m);
191         }
192
193         /* Timeout set by caller if response is expected. */
194         m->ah = ah;
195         m->retries = cm_id_priv->max_cm_retries;
196
197         atomic_inc(&cm_id_priv->refcount);
198         m->context[0] = cm_id_priv;
199         *msg = m;
200         return 0;
201 }
202
203 static int cm_alloc_response_msg(struct cm_port *port,
204                                  struct ib_mad_recv_wc *mad_recv_wc,
205                                  struct ib_mad_send_buf **msg)
206 {
207         struct ib_mad_send_buf *m;
208         struct ib_ah *ah;
209
210         ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
211                                   mad_recv_wc->recv_buf.grh, port->port_num);
212         if (IS_ERR(ah))
213                 return PTR_ERR(ah);
214
215         m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
216                                0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
217                                GFP_ATOMIC);
218         if (IS_ERR(m)) {
219                 ib_destroy_ah(ah);
220                 return PTR_ERR(m);
221         }
222         m->ah = ah;
223         *msg = m;
224         return 0;
225 }
226
227 static void cm_free_msg(struct ib_mad_send_buf *msg)
228 {
229         ib_destroy_ah(msg->ah);
230         if (msg->context[0])
231                 cm_deref_id(msg->context[0]);
232         ib_free_send_mad(msg);
233 }
234
235 static void * cm_copy_private_data(const void *private_data,
236                                    u8 private_data_len)
237 {
238         void *data;
239
240         if (!private_data || !private_data_len)
241                 return NULL;
242
243         data = kmemdup(private_data, private_data_len, GFP_KERNEL);
244         if (!data)
245                 return ERR_PTR(-ENOMEM);
246
247         return data;
248 }
249
250 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
251                                  void *private_data, u8 private_data_len)
252 {
253         if (cm_id_priv->private_data && cm_id_priv->private_data_len)
254                 kfree(cm_id_priv->private_data);
255
256         cm_id_priv->private_data = private_data;
257         cm_id_priv->private_data_len = private_data_len;
258 }
259
260 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
261                                     struct ib_grh *grh, struct cm_av *av)
262 {
263         av->port = port;
264         av->pkey_index = wc->pkey_index;
265         ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
266                            grh, &av->ah_attr);
267 }
268
269 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
270 {
271         struct cm_device *cm_dev;
272         struct cm_port *port = NULL;
273         unsigned long flags;
274         int ret;
275         u8 p;
276
277         read_lock_irqsave(&cm.device_lock, flags);
278         list_for_each_entry(cm_dev, &cm.device_list, list) {
279                 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
280                                         &p, NULL)) {
281                         port = &cm_dev->port[p-1];
282                         break;
283                 }
284         }
285         read_unlock_irqrestore(&cm.device_lock, flags);
286
287         if (!port)
288                 return -EINVAL;
289
290         ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
291                                   be16_to_cpu(path->pkey), &av->pkey_index);
292         if (ret)
293                 return ret;
294
295         av->port = port;
296         ib_init_ah_from_path(cm_dev->device, port->port_num, path,
297                              &av->ah_attr);
298         av->timeout = path->packet_life_time + 1;
299         return 0;
300 }
301
302 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
303 {
304         unsigned long flags;
305         int ret, id;
306         static int next_id;
307
308         do {
309                 spin_lock_irqsave(&cm.lock, flags);
310                 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
311                                         next_id, &id);
312                 if (!ret)
313                         next_id = ((unsigned) id + 1) & MAX_ID_MASK;
314                 spin_unlock_irqrestore(&cm.lock, flags);
315         } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
316
317         cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand);
318         return ret;
319 }
320
321 static void cm_free_id(__be32 local_id)
322 {
323         spin_lock_irq(&cm.lock);
324         idr_remove(&cm.local_id_table,
325                    (__force int) (local_id ^ cm.random_id_operand));
326         spin_unlock_irq(&cm.lock);
327 }
328
329 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
330 {
331         struct cm_id_private *cm_id_priv;
332
333         cm_id_priv = idr_find(&cm.local_id_table,
334                               (__force int) (local_id ^ cm.random_id_operand));
335         if (cm_id_priv) {
336                 if (cm_id_priv->id.remote_id == remote_id)
337                         atomic_inc(&cm_id_priv->refcount);
338                 else
339                         cm_id_priv = NULL;
340         }
341
342         return cm_id_priv;
343 }
344
345 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
346 {
347         struct cm_id_private *cm_id_priv;
348
349         spin_lock_irq(&cm.lock);
350         cm_id_priv = cm_get_id(local_id, remote_id);
351         spin_unlock_irq(&cm.lock);
352
353         return cm_id_priv;
354 }
355
356 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
357 {
358         int i;
359
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];
363 }
364
365 static int cm_compare_data(struct ib_cm_compare_data *src_data,
366                            struct ib_cm_compare_data *dst_data)
367 {
368         u8 src[IB_CM_COMPARE_SIZE];
369         u8 dst[IB_CM_COMPARE_SIZE];
370
371         if (!src_data || !dst_data)
372                 return 0;
373
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);
377 }
378
379 static int cm_compare_private_data(u8 *private_data,
380                                    struct ib_cm_compare_data *dst_data)
381 {
382         u8 src[IB_CM_COMPARE_SIZE];
383
384         if (!dst_data)
385                 return 0;
386
387         cm_mask_copy(src, private_data, dst_data->mask);
388         return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
389 }
390
391 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
392 {
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;
398         int data_cmp;
399
400         while (*link) {
401                 parent = *link;
402                 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
403                                           service_node);
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) &&
409                     !data_cmp)
410                         return cur_cm_id_priv;
411
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;
422                 else
423                         link = &(*link)->rb_right;
424         }
425         rb_link_node(&cm_id_priv->service_node, parent, link);
426         rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
427         return NULL;
428 }
429
430 static struct cm_id_private * cm_find_listen(struct ib_device *device,
431                                              __be64 service_id,
432                                              u8 *private_data)
433 {
434         struct rb_node *node = cm.listen_service_table.rb_node;
435         struct cm_id_private *cm_id_priv;
436         int data_cmp;
437
438         while (node) {
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)
445                         return cm_id_priv;
446
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;
457                 else
458                         node = node->rb_right;
459         }
460         return NULL;
461 }
462
463 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
464                                                      *timewait_info)
465 {
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;
471
472         while (*link) {
473                 parent = *link;
474                 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
475                                              remote_id_node);
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;
484                 else
485                         return cur_timewait_info;
486         }
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);
490         return NULL;
491 }
492
493 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
494                                                    __be32 remote_id)
495 {
496         struct rb_node *node = cm.remote_id_table.rb_node;
497         struct cm_timewait_info *timewait_info;
498
499         while (node) {
500                 timewait_info = rb_entry(node, struct cm_timewait_info,
501                                          remote_id_node);
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;
510                 else
511                         return timewait_info;
512         }
513         return NULL;
514 }
515
516 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
517                                                       *timewait_info)
518 {
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;
524
525         while (*link) {
526                 parent = *link;
527                 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
528                                              remote_qp_node);
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;
537                 else
538                         return cur_timewait_info;
539         }
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);
543         return NULL;
544 }
545
546 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
547                                                     *cm_id_priv)
548 {
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;
554
555         while (*link) {
556                 parent = *link;
557                 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
558                                           sidr_id_node);
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;
563                 else {
564                         int cmp;
565                         cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
566                                      sizeof *port_gid);
567                         if (cmp < 0)
568                                 link = &(*link)->rb_left;
569                         else if (cmp > 0)
570                                 link = &(*link)->rb_right;
571                         else
572                                 return cur_cm_id_priv;
573                 }
574         }
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);
577         return NULL;
578 }
579
580 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
581                                enum ib_cm_sidr_status status)
582 {
583         struct ib_cm_sidr_rep_param param;
584
585         memset(&param, 0, sizeof param);
586         param.status = status;
587         ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
588 }
589
590 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
591                                  ib_cm_handler cm_handler,
592                                  void *context)
593 {
594         struct cm_id_private *cm_id_priv;
595         int ret;
596
597         cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
598         if (!cm_id_priv)
599                 return ERR_PTR(-ENOMEM);
600
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);
607         if (ret)
608                 goto error;
609
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;
616
617 error:
618         kfree(cm_id_priv);
619         return ERR_PTR(-ENOMEM);
620 }
621 EXPORT_SYMBOL(ib_create_cm_id);
622
623 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
624 {
625         struct cm_work *work;
626
627         if (list_empty(&cm_id_priv->work_list))
628                 return NULL;
629
630         work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
631         list_del(&work->list);
632         return work;
633 }
634
635 static void cm_free_work(struct cm_work *work)
636 {
637         if (work->mad_recv_wc)
638                 ib_free_recv_mad(work->mad_recv_wc);
639         kfree(work);
640 }
641
642 static inline int cm_convert_to_ms(int iba_time)
643 {
644         /* approximate conversion to ms from 4.096us x 2^iba_time */
645         return 1 << max(iba_time - 8, 0);
646 }
647
648 /*
649  * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
650  * Because of how ack_timeout is stored, adding one doubles the timeout.
651  * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
652  * increment it (round up) only if the other is within 50%.
653  */
654 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
655 {
656         int ack_timeout = packet_life_time + 1;
657
658         if (ack_timeout >= ca_ack_delay)
659                 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
660         else
661                 ack_timeout = ca_ack_delay +
662                               (ack_timeout >= (ca_ack_delay - 1));
663
664         return min(31, ack_timeout);
665 }
666
667 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
668 {
669         if (timewait_info->inserted_remote_id) {
670                 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
671                 timewait_info->inserted_remote_id = 0;
672         }
673
674         if (timewait_info->inserted_remote_qp) {
675                 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
676                 timewait_info->inserted_remote_qp = 0;
677         }
678 }
679
680 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
681 {
682         struct cm_timewait_info *timewait_info;
683
684         timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
685         if (!timewait_info)
686                 return ERR_PTR(-ENOMEM);
687
688         timewait_info->work.local_id = local_id;
689         INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
690         timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
691         return timewait_info;
692 }
693
694 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
695 {
696         int wait_time;
697         unsigned long flags;
698
699         spin_lock_irqsave(&cm.lock, flags);
700         cm_cleanup_timewait(cm_id_priv->timewait_info);
701         list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
702         spin_unlock_irqrestore(&cm.lock, flags);
703
704         /*
705          * The cm_id could be destroyed by the user before we exit timewait.
706          * To protect against this, we search for the cm_id after exiting
707          * timewait before notifying the user that we've exited timewait.
708          */
709         cm_id_priv->id.state = IB_CM_TIMEWAIT;
710         wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
711         queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
712                            msecs_to_jiffies(wait_time));
713         cm_id_priv->timewait_info = NULL;
714 }
715
716 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
717 {
718         unsigned long flags;
719
720         cm_id_priv->id.state = IB_CM_IDLE;
721         if (cm_id_priv->timewait_info) {
722                 spin_lock_irqsave(&cm.lock, flags);
723                 cm_cleanup_timewait(cm_id_priv->timewait_info);
724                 spin_unlock_irqrestore(&cm.lock, flags);
725                 kfree(cm_id_priv->timewait_info);
726                 cm_id_priv->timewait_info = NULL;
727         }
728 }
729
730 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
731 {
732         struct cm_id_private *cm_id_priv;
733         struct cm_work *work;
734
735         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
736 retest:
737         spin_lock_irq(&cm_id_priv->lock);
738         switch (cm_id->state) {
739         case IB_CM_LISTEN:
740                 cm_id->state = IB_CM_IDLE;
741                 spin_unlock_irq(&cm_id_priv->lock);
742                 spin_lock_irq(&cm.lock);
743                 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
744                 spin_unlock_irq(&cm.lock);
745                 break;
746         case IB_CM_SIDR_REQ_SENT:
747                 cm_id->state = IB_CM_IDLE;
748                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
749                 spin_unlock_irq(&cm_id_priv->lock);
750                 break;
751         case IB_CM_SIDR_REQ_RCVD:
752                 spin_unlock_irq(&cm_id_priv->lock);
753                 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
754                 break;
755         case IB_CM_REQ_SENT:
756                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
757                 spin_unlock_irq(&cm_id_priv->lock);
758                 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
759                                &cm_id_priv->id.device->node_guid,
760                                sizeof cm_id_priv->id.device->node_guid,
761                                NULL, 0);
762                 break;
763         case IB_CM_REQ_RCVD:
764                 if (err == -ENOMEM) {
765                         /* Do not reject to allow future retries. */
766                         cm_reset_to_idle(cm_id_priv);
767                         spin_unlock_irq(&cm_id_priv->lock);
768                 } else {
769                         spin_unlock_irq(&cm_id_priv->lock);
770                         ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
771                                        NULL, 0, NULL, 0);
772                 }
773                 break;
774         case IB_CM_MRA_REQ_RCVD:
775         case IB_CM_REP_SENT:
776         case IB_CM_MRA_REP_RCVD:
777                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
778                 /* Fall through */
779         case IB_CM_MRA_REQ_SENT:
780         case IB_CM_REP_RCVD:
781         case IB_CM_MRA_REP_SENT:
782                 spin_unlock_irq(&cm_id_priv->lock);
783                 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
784                                NULL, 0, NULL, 0);
785                 break;
786         case IB_CM_ESTABLISHED:
787                 spin_unlock_irq(&cm_id_priv->lock);
788                 ib_send_cm_dreq(cm_id, NULL, 0);
789                 goto retest;
790         case IB_CM_DREQ_SENT:
791                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
792                 cm_enter_timewait(cm_id_priv);
793                 spin_unlock_irq(&cm_id_priv->lock);
794                 break;
795         case IB_CM_DREQ_RCVD:
796                 spin_unlock_irq(&cm_id_priv->lock);
797                 ib_send_cm_drep(cm_id, NULL, 0);
798                 break;
799         default:
800                 spin_unlock_irq(&cm_id_priv->lock);
801                 break;
802         }
803
804         cm_free_id(cm_id->local_id);
805         cm_deref_id(cm_id_priv);
806         wait_for_completion(&cm_id_priv->comp);
807         while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
808                 cm_free_work(work);
809         kfree(cm_id_priv->compare_data);
810         kfree(cm_id_priv->private_data);
811         kfree(cm_id_priv);
812 }
813
814 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
815 {
816         cm_destroy_id(cm_id, 0);
817 }
818 EXPORT_SYMBOL(ib_destroy_cm_id);
819
820 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
821                  struct ib_cm_compare_data *compare_data)
822 {
823         struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
824         unsigned long flags;
825         int ret = 0;
826
827         service_mask = service_mask ? service_mask :
828                        __constant_cpu_to_be64(~0ULL);
829         service_id &= service_mask;
830         if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
831             (service_id != IB_CM_ASSIGN_SERVICE_ID))
832                 return -EINVAL;
833
834         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
835         if (cm_id->state != IB_CM_IDLE)
836                 return -EINVAL;
837
838         if (compare_data) {
839                 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
840                                                    GFP_KERNEL);
841                 if (!cm_id_priv->compare_data)
842                         return -ENOMEM;
843                 cm_mask_copy(cm_id_priv->compare_data->data,
844                              compare_data->data, compare_data->mask);
845                 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
846                        IB_CM_COMPARE_SIZE);
847         }
848
849         cm_id->state = IB_CM_LISTEN;
850
851         spin_lock_irqsave(&cm.lock, flags);
852         if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
853                 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
854                 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
855         } else {
856                 cm_id->service_id = service_id;
857                 cm_id->service_mask = service_mask;
858         }
859         cur_cm_id_priv = cm_insert_listen(cm_id_priv);
860         spin_unlock_irqrestore(&cm.lock, flags);
861
862         if (cur_cm_id_priv) {
863                 cm_id->state = IB_CM_IDLE;
864                 kfree(cm_id_priv->compare_data);
865                 cm_id_priv->compare_data = NULL;
866                 ret = -EBUSY;
867         }
868         return ret;
869 }
870 EXPORT_SYMBOL(ib_cm_listen);
871
872 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
873                           enum cm_msg_sequence msg_seq)
874 {
875         u64 hi_tid, low_tid;
876
877         hi_tid   = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
878         low_tid  = (u64) ((__force u32)cm_id_priv->id.local_id |
879                           (msg_seq << 30));
880         return cpu_to_be64(hi_tid | low_tid);
881 }
882
883 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
884                               __be16 attr_id, __be64 tid)
885 {
886         hdr->base_version  = IB_MGMT_BASE_VERSION;
887         hdr->mgmt_class    = IB_MGMT_CLASS_CM;
888         hdr->class_version = IB_CM_CLASS_VERSION;
889         hdr->method        = IB_MGMT_METHOD_SEND;
890         hdr->attr_id       = attr_id;
891         hdr->tid           = tid;
892 }
893
894 static void cm_format_req(struct cm_req_msg *req_msg,
895                           struct cm_id_private *cm_id_priv,
896                           struct ib_cm_req_param *param)
897 {
898         cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
899                           cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
900
901         req_msg->local_comm_id = cm_id_priv->id.local_id;
902         req_msg->service_id = param->service_id;
903         req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
904         cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
905         cm_req_set_resp_res(req_msg, param->responder_resources);
906         cm_req_set_init_depth(req_msg, param->initiator_depth);
907         cm_req_set_remote_resp_timeout(req_msg,
908                                        param->remote_cm_response_timeout);
909         cm_req_set_qp_type(req_msg, param->qp_type);
910         cm_req_set_flow_ctrl(req_msg, param->flow_control);
911         cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
912         cm_req_set_local_resp_timeout(req_msg,
913                                       param->local_cm_response_timeout);
914         cm_req_set_retry_count(req_msg, param->retry_count);
915         req_msg->pkey = param->primary_path->pkey;
916         cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
917         cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
918         cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
919         cm_req_set_srq(req_msg, param->srq);
920
921         req_msg->primary_local_lid = param->primary_path->slid;
922         req_msg->primary_remote_lid = param->primary_path->dlid;
923         req_msg->primary_local_gid = param->primary_path->sgid;
924         req_msg->primary_remote_gid = param->primary_path->dgid;
925         cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
926         cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
927         req_msg->primary_traffic_class = param->primary_path->traffic_class;
928         req_msg->primary_hop_limit = param->primary_path->hop_limit;
929         cm_req_set_primary_sl(req_msg, param->primary_path->sl);
930         cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
931         cm_req_set_primary_local_ack_timeout(req_msg,
932                 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
933                                param->primary_path->packet_life_time));
934
935         if (param->alternate_path) {
936                 req_msg->alt_local_lid = param->alternate_path->slid;
937                 req_msg->alt_remote_lid = param->alternate_path->dlid;
938                 req_msg->alt_local_gid = param->alternate_path->sgid;
939                 req_msg->alt_remote_gid = param->alternate_path->dgid;
940                 cm_req_set_alt_flow_label(req_msg,
941                                           param->alternate_path->flow_label);
942                 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
943                 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
944                 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
945                 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
946                 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
947                 cm_req_set_alt_local_ack_timeout(req_msg,
948                         cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
949                                        param->alternate_path->packet_life_time));
950         }
951
952         if (param->private_data && param->private_data_len)
953                 memcpy(req_msg->private_data, param->private_data,
954                        param->private_data_len);
955 }
956
957 static int cm_validate_req_param(struct ib_cm_req_param *param)
958 {
959         /* peer-to-peer not supported */
960         if (param->peer_to_peer)
961                 return -EINVAL;
962
963         if (!param->primary_path)
964                 return -EINVAL;
965
966         if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
967                 return -EINVAL;
968
969         if (param->private_data &&
970             param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
971                 return -EINVAL;
972
973         if (param->alternate_path &&
974             (param->alternate_path->pkey != param->primary_path->pkey ||
975              param->alternate_path->mtu != param->primary_path->mtu))
976                 return -EINVAL;
977
978         return 0;
979 }
980
981 int ib_send_cm_req(struct ib_cm_id *cm_id,
982                    struct ib_cm_req_param *param)
983 {
984         struct cm_id_private *cm_id_priv;
985         struct cm_req_msg *req_msg;
986         unsigned long flags;
987         int ret;
988
989         ret = cm_validate_req_param(param);
990         if (ret)
991                 return ret;
992
993         /* Verify that we're not in timewait. */
994         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
995         spin_lock_irqsave(&cm_id_priv->lock, flags);
996         if (cm_id->state != IB_CM_IDLE) {
997                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
998                 ret = -EINVAL;
999                 goto out;
1000         }
1001         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1002
1003         cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1004                                                             id.local_id);
1005         if (IS_ERR(cm_id_priv->timewait_info)) {
1006                 ret = PTR_ERR(cm_id_priv->timewait_info);
1007                 goto out;
1008         }
1009
1010         ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1011         if (ret)
1012                 goto error1;
1013         if (param->alternate_path) {
1014                 ret = cm_init_av_by_path(param->alternate_path,
1015                                          &cm_id_priv->alt_av);
1016                 if (ret)
1017                         goto error1;
1018         }
1019         cm_id->service_id = param->service_id;
1020         cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1021         cm_id_priv->timeout_ms = cm_convert_to_ms(
1022                                     param->primary_path->packet_life_time) * 2 +
1023                                  cm_convert_to_ms(
1024                                     param->remote_cm_response_timeout);
1025         cm_id_priv->max_cm_retries = param->max_cm_retries;
1026         cm_id_priv->initiator_depth = param->initiator_depth;
1027         cm_id_priv->responder_resources = param->responder_resources;
1028         cm_id_priv->retry_count = param->retry_count;
1029         cm_id_priv->path_mtu = param->primary_path->mtu;
1030         cm_id_priv->pkey = param->primary_path->pkey;
1031         cm_id_priv->qp_type = param->qp_type;
1032
1033         ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1034         if (ret)
1035                 goto error1;
1036
1037         req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1038         cm_format_req(req_msg, cm_id_priv, param);
1039         cm_id_priv->tid = req_msg->hdr.tid;
1040         cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1041         cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1042
1043         cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1044         cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1045
1046         spin_lock_irqsave(&cm_id_priv->lock, flags);
1047         ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1048         if (ret) {
1049                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1050                 goto error2;
1051         }
1052         BUG_ON(cm_id->state != IB_CM_IDLE);
1053         cm_id->state = IB_CM_REQ_SENT;
1054         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1055         return 0;
1056
1057 error2: cm_free_msg(cm_id_priv->msg);
1058 error1: kfree(cm_id_priv->timewait_info);
1059 out:    return ret;
1060 }
1061 EXPORT_SYMBOL(ib_send_cm_req);
1062
1063 static int cm_issue_rej(struct cm_port *port,
1064                         struct ib_mad_recv_wc *mad_recv_wc,
1065                         enum ib_cm_rej_reason reason,
1066                         enum cm_msg_response msg_rejected,
1067                         void *ari, u8 ari_length)
1068 {
1069         struct ib_mad_send_buf *msg = NULL;
1070         struct cm_rej_msg *rej_msg, *rcv_msg;
1071         int ret;
1072
1073         ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1074         if (ret)
1075                 return ret;
1076
1077         /* We just need common CM header information.  Cast to any message. */
1078         rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1079         rej_msg = (struct cm_rej_msg *) msg->mad;
1080
1081         cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1082         rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1083         rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1084         cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1085         rej_msg->reason = cpu_to_be16(reason);
1086
1087         if (ari && ari_length) {
1088                 cm_rej_set_reject_info_len(rej_msg, ari_length);
1089                 memcpy(rej_msg->ari, ari, ari_length);
1090         }
1091
1092         ret = ib_post_send_mad(msg, NULL);
1093         if (ret)
1094                 cm_free_msg(msg);
1095
1096         return ret;
1097 }
1098
1099 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1100                                     __be32 local_qpn, __be32 remote_qpn)
1101 {
1102         return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1103                 ((local_ca_guid == remote_ca_guid) &&
1104                  (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1105 }
1106
1107 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1108                                             struct ib_sa_path_rec *primary_path,
1109                                             struct ib_sa_path_rec *alt_path)
1110 {
1111         memset(primary_path, 0, sizeof *primary_path);
1112         primary_path->dgid = req_msg->primary_local_gid;
1113         primary_path->sgid = req_msg->primary_remote_gid;
1114         primary_path->dlid = req_msg->primary_local_lid;
1115         primary_path->slid = req_msg->primary_remote_lid;
1116         primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1117         primary_path->hop_limit = req_msg->primary_hop_limit;
1118         primary_path->traffic_class = req_msg->primary_traffic_class;
1119         primary_path->reversible = 1;
1120         primary_path->pkey = req_msg->pkey;
1121         primary_path->sl = cm_req_get_primary_sl(req_msg);
1122         primary_path->mtu_selector = IB_SA_EQ;
1123         primary_path->mtu = cm_req_get_path_mtu(req_msg);
1124         primary_path->rate_selector = IB_SA_EQ;
1125         primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1126         primary_path->packet_life_time_selector = IB_SA_EQ;
1127         primary_path->packet_life_time =
1128                 cm_req_get_primary_local_ack_timeout(req_msg);
1129         primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1130
1131         if (req_msg->alt_local_lid) {
1132                 memset(alt_path, 0, sizeof *alt_path);
1133                 alt_path->dgid = req_msg->alt_local_gid;
1134                 alt_path->sgid = req_msg->alt_remote_gid;
1135                 alt_path->dlid = req_msg->alt_local_lid;
1136                 alt_path->slid = req_msg->alt_remote_lid;
1137                 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1138                 alt_path->hop_limit = req_msg->alt_hop_limit;
1139                 alt_path->traffic_class = req_msg->alt_traffic_class;
1140                 alt_path->reversible = 1;
1141                 alt_path->pkey = req_msg->pkey;
1142                 alt_path->sl = cm_req_get_alt_sl(req_msg);
1143                 alt_path->mtu_selector = IB_SA_EQ;
1144                 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1145                 alt_path->rate_selector = IB_SA_EQ;
1146                 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1147                 alt_path->packet_life_time_selector = IB_SA_EQ;
1148                 alt_path->packet_life_time =
1149                         cm_req_get_alt_local_ack_timeout(req_msg);
1150                 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1151         }
1152 }
1153
1154 static void cm_format_req_event(struct cm_work *work,
1155                                 struct cm_id_private *cm_id_priv,
1156                                 struct ib_cm_id *listen_id)
1157 {
1158         struct cm_req_msg *req_msg;
1159         struct ib_cm_req_event_param *param;
1160
1161         req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1162         param = &work->cm_event.param.req_rcvd;
1163         param->listen_id = listen_id;
1164         param->port = cm_id_priv->av.port->port_num;
1165         param->primary_path = &work->path[0];
1166         if (req_msg->alt_local_lid)
1167                 param->alternate_path = &work->path[1];
1168         else
1169                 param->alternate_path = NULL;
1170         param->remote_ca_guid = req_msg->local_ca_guid;
1171         param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1172         param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1173         param->qp_type = cm_req_get_qp_type(req_msg);
1174         param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1175         param->responder_resources = cm_req_get_init_depth(req_msg);
1176         param->initiator_depth = cm_req_get_resp_res(req_msg);
1177         param->local_cm_response_timeout =
1178                                         cm_req_get_remote_resp_timeout(req_msg);
1179         param->flow_control = cm_req_get_flow_ctrl(req_msg);
1180         param->remote_cm_response_timeout =
1181                                         cm_req_get_local_resp_timeout(req_msg);
1182         param->retry_count = cm_req_get_retry_count(req_msg);
1183         param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1184         param->srq = cm_req_get_srq(req_msg);
1185         work->cm_event.private_data = &req_msg->private_data;
1186 }
1187
1188 static void cm_process_work(struct cm_id_private *cm_id_priv,
1189                             struct cm_work *work)
1190 {
1191         int ret;
1192
1193         /* We will typically only have the current event to report. */
1194         ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1195         cm_free_work(work);
1196
1197         while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1198                 spin_lock_irq(&cm_id_priv->lock);
1199                 work = cm_dequeue_work(cm_id_priv);
1200                 spin_unlock_irq(&cm_id_priv->lock);
1201                 BUG_ON(!work);
1202                 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1203                                                 &work->cm_event);
1204                 cm_free_work(work);
1205         }
1206         cm_deref_id(cm_id_priv);
1207         if (ret)
1208                 cm_destroy_id(&cm_id_priv->id, ret);
1209 }
1210
1211 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1212                           struct cm_id_private *cm_id_priv,
1213                           enum cm_msg_response msg_mraed, u8 service_timeout,
1214                           const void *private_data, u8 private_data_len)
1215 {
1216         cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1217         cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1218         mra_msg->local_comm_id = cm_id_priv->id.local_id;
1219         mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1220         cm_mra_set_service_timeout(mra_msg, service_timeout);
1221
1222         if (private_data && private_data_len)
1223                 memcpy(mra_msg->private_data, private_data, private_data_len);
1224 }
1225
1226 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1227                           struct cm_id_private *cm_id_priv,
1228                           enum ib_cm_rej_reason reason,
1229                           void *ari,
1230                           u8 ari_length,
1231                           const void *private_data,
1232                           u8 private_data_len)
1233 {
1234         cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1235         rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1236
1237         switch(cm_id_priv->id.state) {
1238         case IB_CM_REQ_RCVD:
1239                 rej_msg->local_comm_id = 0;
1240                 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1241                 break;
1242         case IB_CM_MRA_REQ_SENT:
1243                 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1244                 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1245                 break;
1246         case IB_CM_REP_RCVD:
1247         case IB_CM_MRA_REP_SENT:
1248                 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1249                 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1250                 break;
1251         default:
1252                 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1253                 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1254                 break;
1255         }
1256
1257         rej_msg->reason = cpu_to_be16(reason);
1258         if (ari && ari_length) {
1259                 cm_rej_set_reject_info_len(rej_msg, ari_length);
1260                 memcpy(rej_msg->ari, ari, ari_length);
1261         }
1262
1263         if (private_data && private_data_len)
1264                 memcpy(rej_msg->private_data, private_data, private_data_len);
1265 }
1266
1267 static void cm_dup_req_handler(struct cm_work *work,
1268                                struct cm_id_private *cm_id_priv)
1269 {
1270         struct ib_mad_send_buf *msg = NULL;
1271         int ret;
1272
1273         /* Quick state check to discard duplicate REQs. */
1274         if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1275                 return;
1276
1277         ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1278         if (ret)
1279                 return;
1280
1281         spin_lock_irq(&cm_id_priv->lock);
1282         switch (cm_id_priv->id.state) {
1283         case IB_CM_MRA_REQ_SENT:
1284                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1285                               CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1286                               cm_id_priv->private_data,
1287                               cm_id_priv->private_data_len);
1288                 break;
1289         case IB_CM_TIMEWAIT:
1290                 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1291                               IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1292                 break;
1293         default:
1294                 goto unlock;
1295         }
1296         spin_unlock_irq(&cm_id_priv->lock);
1297
1298         ret = ib_post_send_mad(msg, NULL);
1299         if (ret)
1300                 goto free;
1301         return;
1302
1303 unlock: spin_unlock_irq(&cm_id_priv->lock);
1304 free:   cm_free_msg(msg);
1305 }
1306
1307 static struct cm_id_private * cm_match_req(struct cm_work *work,
1308                                            struct cm_id_private *cm_id_priv)
1309 {
1310         struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1311         struct cm_timewait_info *timewait_info;
1312         struct cm_req_msg *req_msg;
1313
1314         req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1315
1316         /* Check for possible duplicate REQ. */
1317         spin_lock_irq(&cm.lock);
1318         timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1319         if (timewait_info) {
1320                 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1321                                            timewait_info->work.remote_id);
1322                 spin_unlock_irq(&cm.lock);
1323                 if (cur_cm_id_priv) {
1324                         cm_dup_req_handler(work, cur_cm_id_priv);
1325                         cm_deref_id(cur_cm_id_priv);
1326                 }
1327                 return NULL;
1328         }
1329
1330         /* Check for stale connections. */
1331         timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1332         if (timewait_info) {
1333                 cm_cleanup_timewait(cm_id_priv->timewait_info);
1334                 spin_unlock_irq(&cm.lock);
1335                 cm_issue_rej(work->port, work->mad_recv_wc,
1336                              IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1337                              NULL, 0);
1338                 return NULL;
1339         }
1340
1341         /* Find matching listen request. */
1342         listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1343                                            req_msg->service_id,
1344                                            req_msg->private_data);
1345         if (!listen_cm_id_priv) {
1346                 cm_cleanup_timewait(cm_id_priv->timewait_info);
1347                 spin_unlock_irq(&cm.lock);
1348                 cm_issue_rej(work->port, work->mad_recv_wc,
1349                              IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1350                              NULL, 0);
1351                 goto out;
1352         }
1353         atomic_inc(&listen_cm_id_priv->refcount);
1354         atomic_inc(&cm_id_priv->refcount);
1355         cm_id_priv->id.state = IB_CM_REQ_RCVD;
1356         atomic_inc(&cm_id_priv->work_count);
1357         spin_unlock_irq(&cm.lock);
1358 out:
1359         return listen_cm_id_priv;
1360 }
1361
1362 static int cm_req_handler(struct cm_work *work)
1363 {
1364         struct ib_cm_id *cm_id;
1365         struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1366         struct cm_req_msg *req_msg;
1367         int ret;
1368
1369         req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1370
1371         cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1372         if (IS_ERR(cm_id))
1373                 return PTR_ERR(cm_id);
1374
1375         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1376         cm_id_priv->id.remote_id = req_msg->local_comm_id;
1377         cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1378                                 work->mad_recv_wc->recv_buf.grh,
1379                                 &cm_id_priv->av);
1380         cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1381                                                             id.local_id);
1382         if (IS_ERR(cm_id_priv->timewait_info)) {
1383                 ret = PTR_ERR(cm_id_priv->timewait_info);
1384                 goto destroy;
1385         }
1386         cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1387         cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1388         cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1389
1390         listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1391         if (!listen_cm_id_priv) {
1392                 ret = -EINVAL;
1393                 kfree(cm_id_priv->timewait_info);
1394                 goto destroy;
1395         }
1396
1397         cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1398         cm_id_priv->id.context = listen_cm_id_priv->id.context;
1399         cm_id_priv->id.service_id = req_msg->service_id;
1400         cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1401
1402         cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1403         ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1404         if (ret) {
1405                 ib_get_cached_gid(work->port->cm_dev->device,
1406                                   work->port->port_num, 0, &work->path[0].sgid);
1407                 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1408                                &work->path[0].sgid, sizeof work->path[0].sgid,
1409                                NULL, 0);
1410                 goto rejected;
1411         }
1412         if (req_msg->alt_local_lid) {
1413                 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1414                 if (ret) {
1415                         ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1416                                        &work->path[0].sgid,
1417                                        sizeof work->path[0].sgid, NULL, 0);
1418                         goto rejected;
1419                 }
1420         }
1421         cm_id_priv->tid = req_msg->hdr.tid;
1422         cm_id_priv->timeout_ms = cm_convert_to_ms(
1423                                         cm_req_get_local_resp_timeout(req_msg));
1424         cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1425         cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1426         cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1427         cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1428         cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1429         cm_id_priv->pkey = req_msg->pkey;
1430         cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1431         cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1432         cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1433         cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1434
1435         cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1436         cm_process_work(cm_id_priv, work);
1437         cm_deref_id(listen_cm_id_priv);
1438         return 0;
1439
1440 rejected:
1441         atomic_dec(&cm_id_priv->refcount);
1442         cm_deref_id(listen_cm_id_priv);
1443 destroy:
1444         ib_destroy_cm_id(cm_id);
1445         return ret;
1446 }
1447
1448 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1449                           struct cm_id_private *cm_id_priv,
1450                           struct ib_cm_rep_param *param)
1451 {
1452         cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1453         rep_msg->local_comm_id = cm_id_priv->id.local_id;
1454         rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1455         cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1456         cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1457         rep_msg->resp_resources = param->responder_resources;
1458         rep_msg->initiator_depth = param->initiator_depth;
1459         cm_rep_set_target_ack_delay(rep_msg,
1460                                     cm_id_priv->av.port->cm_dev->ack_delay);
1461         cm_rep_set_failover(rep_msg, param->failover_accepted);
1462         cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1463         cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1464         cm_rep_set_srq(rep_msg, param->srq);
1465         rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1466
1467         if (param->private_data && param->private_data_len)
1468                 memcpy(rep_msg->private_data, param->private_data,
1469                        param->private_data_len);
1470 }
1471
1472 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1473                    struct ib_cm_rep_param *param)
1474 {
1475         struct cm_id_private *cm_id_priv;
1476         struct ib_mad_send_buf *msg;
1477         struct cm_rep_msg *rep_msg;
1478         unsigned long flags;
1479         int ret;
1480
1481         if (param->private_data &&
1482             param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1483                 return -EINVAL;
1484
1485         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1486         spin_lock_irqsave(&cm_id_priv->lock, flags);
1487         if (cm_id->state != IB_CM_REQ_RCVD &&
1488             cm_id->state != IB_CM_MRA_REQ_SENT) {
1489                 ret = -EINVAL;
1490                 goto out;
1491         }
1492
1493         ret = cm_alloc_msg(cm_id_priv, &msg);
1494         if (ret)
1495                 goto out;
1496
1497         rep_msg = (struct cm_rep_msg *) msg->mad;
1498         cm_format_rep(rep_msg, cm_id_priv, param);
1499         msg->timeout_ms = cm_id_priv->timeout_ms;
1500         msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1501
1502         ret = ib_post_send_mad(msg, NULL);
1503         if (ret) {
1504                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1505                 cm_free_msg(msg);
1506                 return ret;
1507         }
1508
1509         cm_id->state = IB_CM_REP_SENT;
1510         cm_id_priv->msg = msg;
1511         cm_id_priv->initiator_depth = param->initiator_depth;
1512         cm_id_priv->responder_resources = param->responder_resources;
1513         cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1514         cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1515
1516 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1517         return ret;
1518 }
1519 EXPORT_SYMBOL(ib_send_cm_rep);
1520
1521 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1522                           struct cm_id_private *cm_id_priv,
1523                           const void *private_data,
1524                           u8 private_data_len)
1525 {
1526         cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1527         rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1528         rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1529
1530         if (private_data && private_data_len)
1531                 memcpy(rtu_msg->private_data, private_data, private_data_len);
1532 }
1533
1534 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1535                    const void *private_data,
1536                    u8 private_data_len)
1537 {
1538         struct cm_id_private *cm_id_priv;
1539         struct ib_mad_send_buf *msg;
1540         unsigned long flags;
1541         void *data;
1542         int ret;
1543
1544         if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1545                 return -EINVAL;
1546
1547         data = cm_copy_private_data(private_data, private_data_len);
1548         if (IS_ERR(data))
1549                 return PTR_ERR(data);
1550
1551         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1552         spin_lock_irqsave(&cm_id_priv->lock, flags);
1553         if (cm_id->state != IB_CM_REP_RCVD &&
1554             cm_id->state != IB_CM_MRA_REP_SENT) {
1555                 ret = -EINVAL;
1556                 goto error;
1557         }
1558
1559         ret = cm_alloc_msg(cm_id_priv, &msg);
1560         if (ret)
1561                 goto error;
1562
1563         cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1564                       private_data, private_data_len);
1565
1566         ret = ib_post_send_mad(msg, NULL);
1567         if (ret) {
1568                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1569                 cm_free_msg(msg);
1570                 kfree(data);
1571                 return ret;
1572         }
1573
1574         cm_id->state = IB_CM_ESTABLISHED;
1575         cm_set_private_data(cm_id_priv, data, private_data_len);
1576         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1577         return 0;
1578
1579 error:  spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1580         kfree(data);
1581         return ret;
1582 }
1583 EXPORT_SYMBOL(ib_send_cm_rtu);
1584
1585 static void cm_format_rep_event(struct cm_work *work)
1586 {
1587         struct cm_rep_msg *rep_msg;
1588         struct ib_cm_rep_event_param *param;
1589
1590         rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1591         param = &work->cm_event.param.rep_rcvd;
1592         param->remote_ca_guid = rep_msg->local_ca_guid;
1593         param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1594         param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1595         param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1596         param->responder_resources = rep_msg->initiator_depth;
1597         param->initiator_depth = rep_msg->resp_resources;
1598         param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1599         param->failover_accepted = cm_rep_get_failover(rep_msg);
1600         param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1601         param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1602         param->srq = cm_rep_get_srq(rep_msg);
1603         work->cm_event.private_data = &rep_msg->private_data;
1604 }
1605
1606 static void cm_dup_rep_handler(struct cm_work *work)
1607 {
1608         struct cm_id_private *cm_id_priv;
1609         struct cm_rep_msg *rep_msg;
1610         struct ib_mad_send_buf *msg = NULL;
1611         int ret;
1612
1613         rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1614         cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1615                                    rep_msg->local_comm_id);
1616         if (!cm_id_priv)
1617                 return;
1618
1619         ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1620         if (ret)
1621                 goto deref;
1622
1623         spin_lock_irq(&cm_id_priv->lock);
1624         if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1625                 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1626                               cm_id_priv->private_data,
1627                               cm_id_priv->private_data_len);
1628         else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1629                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1630                               CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1631                               cm_id_priv->private_data,
1632                               cm_id_priv->private_data_len);
1633         else
1634                 goto unlock;
1635         spin_unlock_irq(&cm_id_priv->lock);
1636
1637         ret = ib_post_send_mad(msg, NULL);
1638         if (ret)
1639                 goto free;
1640         goto deref;
1641
1642 unlock: spin_unlock_irq(&cm_id_priv->lock);
1643 free:   cm_free_msg(msg);
1644 deref:  cm_deref_id(cm_id_priv);
1645 }
1646
1647 static int cm_rep_handler(struct cm_work *work)
1648 {
1649         struct cm_id_private *cm_id_priv;
1650         struct cm_rep_msg *rep_msg;
1651         int ret;
1652
1653         rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1654         cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1655         if (!cm_id_priv) {
1656                 cm_dup_rep_handler(work);
1657                 return -EINVAL;
1658         }
1659
1660         cm_format_rep_event(work);
1661
1662         spin_lock_irq(&cm_id_priv->lock);
1663         switch (cm_id_priv->id.state) {
1664         case IB_CM_REQ_SENT:
1665         case IB_CM_MRA_REQ_RCVD:
1666                 break;
1667         default:
1668                 spin_unlock_irq(&cm_id_priv->lock);
1669                 ret = -EINVAL;
1670                 goto error;
1671         }
1672
1673         cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1674         cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1675         cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1676
1677         spin_lock(&cm.lock);
1678         /* Check for duplicate REP. */
1679         if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1680                 spin_unlock(&cm.lock);
1681                 spin_unlock_irq(&cm_id_priv->lock);
1682                 ret = -EINVAL;
1683                 goto error;
1684         }
1685         /* Check for a stale connection. */
1686         if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1687                 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1688                          &cm.remote_id_table);
1689                 cm_id_priv->timewait_info->inserted_remote_id = 0;
1690                 spin_unlock(&cm.lock);
1691                 spin_unlock_irq(&cm_id_priv->lock);
1692                 cm_issue_rej(work->port, work->mad_recv_wc,
1693                              IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1694                              NULL, 0);
1695                 ret = -EINVAL;
1696                 goto error;
1697         }
1698         spin_unlock(&cm.lock);
1699
1700         cm_id_priv->id.state = IB_CM_REP_RCVD;
1701         cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1702         cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1703         cm_id_priv->initiator_depth = rep_msg->resp_resources;
1704         cm_id_priv->responder_resources = rep_msg->initiator_depth;
1705         cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1706         cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1707         cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1708         cm_id_priv->av.timeout =
1709                         cm_ack_timeout(cm_id_priv->target_ack_delay,
1710                                        cm_id_priv->av.timeout - 1);
1711         cm_id_priv->alt_av.timeout =
1712                         cm_ack_timeout(cm_id_priv->target_ack_delay,
1713                                        cm_id_priv->alt_av.timeout - 1);
1714
1715         /* todo: handle peer_to_peer */
1716
1717         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1718         ret = atomic_inc_and_test(&cm_id_priv->work_count);
1719         if (!ret)
1720                 list_add_tail(&work->list, &cm_id_priv->work_list);
1721         spin_unlock_irq(&cm_id_priv->lock);
1722
1723         if (ret)
1724                 cm_process_work(cm_id_priv, work);
1725         else
1726                 cm_deref_id(cm_id_priv);
1727         return 0;
1728
1729 error:
1730         cm_deref_id(cm_id_priv);
1731         return ret;
1732 }
1733
1734 static int cm_establish_handler(struct cm_work *work)
1735 {
1736         struct cm_id_private *cm_id_priv;
1737         int ret;
1738
1739         /* See comment in cm_establish about lookup. */
1740         cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1741         if (!cm_id_priv)
1742                 return -EINVAL;
1743
1744         spin_lock_irq(&cm_id_priv->lock);
1745         if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1746                 spin_unlock_irq(&cm_id_priv->lock);
1747                 goto out;
1748         }
1749
1750         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1751         ret = atomic_inc_and_test(&cm_id_priv->work_count);
1752         if (!ret)
1753                 list_add_tail(&work->list, &cm_id_priv->work_list);
1754         spin_unlock_irq(&cm_id_priv->lock);
1755
1756         if (ret)
1757                 cm_process_work(cm_id_priv, work);
1758         else
1759                 cm_deref_id(cm_id_priv);
1760         return 0;
1761 out:
1762         cm_deref_id(cm_id_priv);
1763         return -EINVAL;
1764 }
1765
1766 static int cm_rtu_handler(struct cm_work *work)
1767 {
1768         struct cm_id_private *cm_id_priv;
1769         struct cm_rtu_msg *rtu_msg;
1770         int ret;
1771
1772         rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1773         cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1774                                    rtu_msg->local_comm_id);
1775         if (!cm_id_priv)
1776                 return -EINVAL;
1777
1778         work->cm_event.private_data = &rtu_msg->private_data;
1779
1780         spin_lock_irq(&cm_id_priv->lock);
1781         if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1782             cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1783                 spin_unlock_irq(&cm_id_priv->lock);
1784                 goto out;
1785         }
1786         cm_id_priv->id.state = IB_CM_ESTABLISHED;
1787
1788         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1789         ret = atomic_inc_and_test(&cm_id_priv->work_count);
1790         if (!ret)
1791                 list_add_tail(&work->list, &cm_id_priv->work_list);
1792         spin_unlock_irq(&cm_id_priv->lock);
1793
1794         if (ret)
1795                 cm_process_work(cm_id_priv, work);
1796         else
1797                 cm_deref_id(cm_id_priv);
1798         return 0;
1799 out:
1800         cm_deref_id(cm_id_priv);
1801         return -EINVAL;
1802 }
1803
1804 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1805                           struct cm_id_private *cm_id_priv,
1806                           const void *private_data,
1807                           u8 private_data_len)
1808 {
1809         cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1810                           cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1811         dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1812         dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1813         cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1814
1815         if (private_data && private_data_len)
1816                 memcpy(dreq_msg->private_data, private_data, private_data_len);
1817 }
1818
1819 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1820                     const void *private_data,
1821                     u8 private_data_len)
1822 {
1823         struct cm_id_private *cm_id_priv;
1824         struct ib_mad_send_buf *msg;
1825         unsigned long flags;
1826         int ret;
1827
1828         if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1829                 return -EINVAL;
1830
1831         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1832         spin_lock_irqsave(&cm_id_priv->lock, flags);
1833         if (cm_id->state != IB_CM_ESTABLISHED) {
1834                 ret = -EINVAL;
1835                 goto out;
1836         }
1837
1838         ret = cm_alloc_msg(cm_id_priv, &msg);
1839         if (ret) {
1840                 cm_enter_timewait(cm_id_priv);
1841                 goto out;
1842         }
1843
1844         cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1845                        private_data, private_data_len);
1846         msg->timeout_ms = cm_id_priv->timeout_ms;
1847         msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1848
1849         ret = ib_post_send_mad(msg, NULL);
1850         if (ret) {
1851                 cm_enter_timewait(cm_id_priv);
1852                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1853                 cm_free_msg(msg);
1854                 return ret;
1855         }
1856
1857         cm_id->state = IB_CM_DREQ_SENT;
1858         cm_id_priv->msg = msg;
1859 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1860         return ret;
1861 }
1862 EXPORT_SYMBOL(ib_send_cm_dreq);
1863
1864 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1865                           struct cm_id_private *cm_id_priv,
1866                           const void *private_data,
1867                           u8 private_data_len)
1868 {
1869         cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1870         drep_msg->local_comm_id = cm_id_priv->id.local_id;
1871         drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1872
1873         if (private_data && private_data_len)
1874                 memcpy(drep_msg->private_data, private_data, private_data_len);
1875 }
1876
1877 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1878                     const void *private_data,
1879                     u8 private_data_len)
1880 {
1881         struct cm_id_private *cm_id_priv;
1882         struct ib_mad_send_buf *msg;
1883         unsigned long flags;
1884         void *data;
1885         int ret;
1886
1887         if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1888                 return -EINVAL;
1889
1890         data = cm_copy_private_data(private_data, private_data_len);
1891         if (IS_ERR(data))
1892                 return PTR_ERR(data);
1893
1894         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1895         spin_lock_irqsave(&cm_id_priv->lock, flags);
1896         if (cm_id->state != IB_CM_DREQ_RCVD) {
1897                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1898                 kfree(data);
1899                 return -EINVAL;
1900         }
1901
1902         cm_set_private_data(cm_id_priv, data, private_data_len);
1903         cm_enter_timewait(cm_id_priv);
1904
1905         ret = cm_alloc_msg(cm_id_priv, &msg);
1906         if (ret)
1907                 goto out;
1908
1909         cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1910                        private_data, private_data_len);
1911
1912         ret = ib_post_send_mad(msg, NULL);
1913         if (ret) {
1914                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1915                 cm_free_msg(msg);
1916                 return ret;
1917         }
1918
1919 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1920         return ret;
1921 }
1922 EXPORT_SYMBOL(ib_send_cm_drep);
1923
1924 static int cm_issue_drep(struct cm_port *port,
1925                          struct ib_mad_recv_wc *mad_recv_wc)
1926 {
1927         struct ib_mad_send_buf *msg = NULL;
1928         struct cm_dreq_msg *dreq_msg;
1929         struct cm_drep_msg *drep_msg;
1930         int ret;
1931
1932         ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1933         if (ret)
1934                 return ret;
1935
1936         dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
1937         drep_msg = (struct cm_drep_msg *) msg->mad;
1938
1939         cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
1940         drep_msg->remote_comm_id = dreq_msg->local_comm_id;
1941         drep_msg->local_comm_id = dreq_msg->remote_comm_id;
1942
1943         ret = ib_post_send_mad(msg, NULL);
1944         if (ret)
1945                 cm_free_msg(msg);
1946
1947         return ret;
1948 }
1949
1950 static int cm_dreq_handler(struct cm_work *work)
1951 {
1952         struct cm_id_private *cm_id_priv;
1953         struct cm_dreq_msg *dreq_msg;
1954         struct ib_mad_send_buf *msg = NULL;
1955         int ret;
1956
1957         dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1958         cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1959                                    dreq_msg->local_comm_id);
1960         if (!cm_id_priv) {
1961                 cm_issue_drep(work->port, work->mad_recv_wc);
1962                 return -EINVAL;
1963         }
1964
1965         work->cm_event.private_data = &dreq_msg->private_data;
1966
1967         spin_lock_irq(&cm_id_priv->lock);
1968         if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1969                 goto unlock;
1970
1971         switch (cm_id_priv->id.state) {
1972         case IB_CM_REP_SENT:
1973         case IB_CM_DREQ_SENT:
1974                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1975                 break;
1976         case IB_CM_ESTABLISHED:
1977         case IB_CM_MRA_REP_RCVD:
1978                 break;
1979         case IB_CM_TIMEWAIT:
1980                 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1981                         goto unlock;
1982
1983                 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1984                                cm_id_priv->private_data,
1985                                cm_id_priv->private_data_len);
1986                 spin_unlock_irq(&cm_id_priv->lock);
1987
1988                 if (ib_post_send_mad(msg, NULL))
1989                         cm_free_msg(msg);
1990                 goto deref;
1991         default:
1992                 goto unlock;
1993         }
1994         cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1995         cm_id_priv->tid = dreq_msg->hdr.tid;
1996         ret = atomic_inc_and_test(&cm_id_priv->work_count);
1997         if (!ret)
1998                 list_add_tail(&work->list, &cm_id_priv->work_list);
1999         spin_unlock_irq(&cm_id_priv->lock);
2000
2001         if (ret)
2002                 cm_process_work(cm_id_priv, work);
2003         else
2004                 cm_deref_id(cm_id_priv);
2005         return 0;
2006
2007 unlock: spin_unlock_irq(&cm_id_priv->lock);
2008 deref:  cm_deref_id(cm_id_priv);
2009         return -EINVAL;
2010 }
2011
2012 static int cm_drep_handler(struct cm_work *work)
2013 {
2014         struct cm_id_private *cm_id_priv;
2015         struct cm_drep_msg *drep_msg;
2016         int ret;
2017
2018         drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2019         cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2020                                    drep_msg->local_comm_id);
2021         if (!cm_id_priv)
2022                 return -EINVAL;
2023
2024         work->cm_event.private_data = &drep_msg->private_data;
2025
2026         spin_lock_irq(&cm_id_priv->lock);
2027         if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2028             cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2029                 spin_unlock_irq(&cm_id_priv->lock);
2030                 goto out;
2031         }
2032         cm_enter_timewait(cm_id_priv);
2033
2034         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2035         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2036         if (!ret)
2037                 list_add_tail(&work->list, &cm_id_priv->work_list);
2038         spin_unlock_irq(&cm_id_priv->lock);
2039
2040         if (ret)
2041                 cm_process_work(cm_id_priv, work);
2042         else
2043                 cm_deref_id(cm_id_priv);
2044         return 0;
2045 out:
2046         cm_deref_id(cm_id_priv);
2047         return -EINVAL;
2048 }
2049
2050 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2051                    enum ib_cm_rej_reason reason,
2052                    void *ari,
2053                    u8 ari_length,
2054                    const void *private_data,
2055                    u8 private_data_len)
2056 {
2057         struct cm_id_private *cm_id_priv;
2058         struct ib_mad_send_buf *msg;
2059         unsigned long flags;
2060         int ret;
2061
2062         if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2063             (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2064                 return -EINVAL;
2065
2066         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2067
2068         spin_lock_irqsave(&cm_id_priv->lock, flags);
2069         switch (cm_id->state) {
2070         case IB_CM_REQ_SENT:
2071         case IB_CM_MRA_REQ_RCVD:
2072         case IB_CM_REQ_RCVD:
2073         case IB_CM_MRA_REQ_SENT:
2074         case IB_CM_REP_RCVD:
2075         case IB_CM_MRA_REP_SENT:
2076                 ret = cm_alloc_msg(cm_id_priv, &msg);
2077                 if (!ret)
2078                         cm_format_rej((struct cm_rej_msg *) msg->mad,
2079                                       cm_id_priv, reason, ari, ari_length,
2080                                       private_data, private_data_len);
2081
2082                 cm_reset_to_idle(cm_id_priv);
2083                 break;
2084         case IB_CM_REP_SENT:
2085         case IB_CM_MRA_REP_RCVD:
2086                 ret = cm_alloc_msg(cm_id_priv, &msg);
2087                 if (!ret)
2088                         cm_format_rej((struct cm_rej_msg *) msg->mad,
2089                                       cm_id_priv, reason, ari, ari_length,
2090                                       private_data, private_data_len);
2091
2092                 cm_enter_timewait(cm_id_priv);
2093                 break;
2094         default:
2095                 ret = -EINVAL;
2096                 goto out;
2097         }
2098
2099         if (ret)
2100                 goto out;
2101
2102         ret = ib_post_send_mad(msg, NULL);
2103         if (ret)
2104                 cm_free_msg(msg);
2105
2106 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2107         return ret;
2108 }
2109 EXPORT_SYMBOL(ib_send_cm_rej);
2110
2111 static void cm_format_rej_event(struct cm_work *work)
2112 {
2113         struct cm_rej_msg *rej_msg;
2114         struct ib_cm_rej_event_param *param;
2115
2116         rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2117         param = &work->cm_event.param.rej_rcvd;
2118         param->ari = rej_msg->ari;
2119         param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2120         param->reason = __be16_to_cpu(rej_msg->reason);
2121         work->cm_event.private_data = &rej_msg->private_data;
2122 }
2123
2124 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2125 {
2126         struct cm_timewait_info *timewait_info;
2127         struct cm_id_private *cm_id_priv;
2128         __be32 remote_id;
2129
2130         remote_id = rej_msg->local_comm_id;
2131
2132         if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2133                 spin_lock_irq(&cm.lock);
2134                 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2135                                                   remote_id);
2136                 if (!timewait_info) {
2137                         spin_unlock_irq(&cm.lock);
2138                         return NULL;
2139                 }
2140                 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2141                                       (timewait_info->work.local_id ^
2142                                        cm.random_id_operand));
2143                 if (cm_id_priv) {
2144                         if (cm_id_priv->id.remote_id == remote_id)
2145                                 atomic_inc(&cm_id_priv->refcount);
2146                         else
2147                                 cm_id_priv = NULL;
2148                 }
2149                 spin_unlock_irq(&cm.lock);
2150         } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2151                 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2152         else
2153                 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2154
2155         return cm_id_priv;
2156 }
2157
2158 static int cm_rej_handler(struct cm_work *work)
2159 {
2160         struct cm_id_private *cm_id_priv;
2161         struct cm_rej_msg *rej_msg;
2162         int ret;
2163
2164         rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2165         cm_id_priv = cm_acquire_rejected_id(rej_msg);
2166         if (!cm_id_priv)
2167                 return -EINVAL;
2168
2169         cm_format_rej_event(work);
2170
2171         spin_lock_irq(&cm_id_priv->lock);
2172         switch (cm_id_priv->id.state) {
2173         case IB_CM_REQ_SENT:
2174         case IB_CM_MRA_REQ_RCVD:
2175         case IB_CM_REP_SENT:
2176         case IB_CM_MRA_REP_RCVD:
2177                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2178                 /* fall through */
2179         case IB_CM_REQ_RCVD:
2180         case IB_CM_MRA_REQ_SENT:
2181                 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2182                         cm_enter_timewait(cm_id_priv);
2183                 else
2184                         cm_reset_to_idle(cm_id_priv);
2185                 break;
2186         case IB_CM_DREQ_SENT:
2187                 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2188                 /* fall through */
2189         case IB_CM_REP_RCVD:
2190         case IB_CM_MRA_REP_SENT:
2191         case IB_CM_ESTABLISHED:
2192                 cm_enter_timewait(cm_id_priv);
2193                 break;
2194         default:
2195                 spin_unlock_irq(&cm_id_priv->lock);
2196                 ret = -EINVAL;
2197                 goto out;
2198         }
2199
2200         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2201         if (!ret)
2202                 list_add_tail(&work->list, &cm_id_priv->work_list);
2203         spin_unlock_irq(&cm_id_priv->lock);
2204
2205         if (ret)
2206                 cm_process_work(cm_id_priv, work);
2207         else
2208                 cm_deref_id(cm_id_priv);
2209         return 0;
2210 out:
2211         cm_deref_id(cm_id_priv);
2212         return -EINVAL;
2213 }
2214
2215 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2216                    u8 service_timeout,
2217                    const void *private_data,
2218                    u8 private_data_len)
2219 {
2220         struct cm_id_private *cm_id_priv;
2221         struct ib_mad_send_buf *msg;
2222         void *data;
2223         unsigned long flags;
2224         int ret;
2225
2226         if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2227                 return -EINVAL;
2228
2229         data = cm_copy_private_data(private_data, private_data_len);
2230         if (IS_ERR(data))
2231                 return PTR_ERR(data);
2232
2233         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2234
2235         spin_lock_irqsave(&cm_id_priv->lock, flags);
2236         switch(cm_id_priv->id.state) {
2237         case IB_CM_REQ_RCVD:
2238                 ret = cm_alloc_msg(cm_id_priv, &msg);
2239                 if (ret)
2240                         goto error1;
2241
2242                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2243                               CM_MSG_RESPONSE_REQ, service_timeout,
2244                               private_data, private_data_len);
2245                 ret = ib_post_send_mad(msg, NULL);
2246                 if (ret)
2247                         goto error2;
2248                 cm_id->state = IB_CM_MRA_REQ_SENT;
2249                 break;
2250         case IB_CM_REP_RCVD:
2251                 ret = cm_alloc_msg(cm_id_priv, &msg);
2252                 if (ret)
2253                         goto error1;
2254
2255                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2256                               CM_MSG_RESPONSE_REP, service_timeout,
2257                               private_data, private_data_len);
2258                 ret = ib_post_send_mad(msg, NULL);
2259                 if (ret)
2260                         goto error2;
2261                 cm_id->state = IB_CM_MRA_REP_SENT;
2262                 break;
2263         case IB_CM_ESTABLISHED:
2264                 ret = cm_alloc_msg(cm_id_priv, &msg);
2265                 if (ret)
2266                         goto error1;
2267
2268                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2269                               CM_MSG_RESPONSE_OTHER, service_timeout,
2270                               private_data, private_data_len);
2271                 ret = ib_post_send_mad(msg, NULL);
2272                 if (ret)
2273                         goto error2;
2274                 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2275                 break;
2276         default:
2277                 ret = -EINVAL;
2278                 goto error1;
2279         }
2280         cm_id_priv->service_timeout = service_timeout;
2281         cm_set_private_data(cm_id_priv, data, private_data_len);
2282         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2283         return 0;
2284
2285 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2286         kfree(data);
2287         return ret;
2288
2289 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2290         kfree(data);
2291         cm_free_msg(msg);
2292         return ret;
2293 }
2294 EXPORT_SYMBOL(ib_send_cm_mra);
2295
2296 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2297 {
2298         switch (cm_mra_get_msg_mraed(mra_msg)) {
2299         case CM_MSG_RESPONSE_REQ:
2300                 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2301         case CM_MSG_RESPONSE_REP:
2302         case CM_MSG_RESPONSE_OTHER:
2303                 return cm_acquire_id(mra_msg->remote_comm_id,
2304                                      mra_msg->local_comm_id);
2305         default:
2306                 return NULL;
2307         }
2308 }
2309
2310 static int cm_mra_handler(struct cm_work *work)
2311 {
2312         struct cm_id_private *cm_id_priv;
2313         struct cm_mra_msg *mra_msg;
2314         int timeout, ret;
2315
2316         mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2317         cm_id_priv = cm_acquire_mraed_id(mra_msg);
2318         if (!cm_id_priv)
2319                 return -EINVAL;
2320
2321         work->cm_event.private_data = &mra_msg->private_data;
2322         work->cm_event.param.mra_rcvd.service_timeout =
2323                                         cm_mra_get_service_timeout(mra_msg);
2324         timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2325                   cm_convert_to_ms(cm_id_priv->av.timeout);
2326
2327         spin_lock_irq(&cm_id_priv->lock);
2328         switch (cm_id_priv->id.state) {
2329         case IB_CM_REQ_SENT:
2330                 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2331                     ib_modify_mad(cm_id_priv->av.port->mad_agent,
2332                                   cm_id_priv->msg, timeout))
2333                         goto out;
2334                 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2335                 break;
2336         case IB_CM_REP_SENT:
2337                 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2338                     ib_modify_mad(cm_id_priv->av.port->mad_agent,
2339                                   cm_id_priv->msg, timeout))
2340                         goto out;
2341                 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2342                 break;
2343         case IB_CM_ESTABLISHED:
2344                 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2345                     cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2346                     ib_modify_mad(cm_id_priv->av.port->mad_agent,
2347                                   cm_id_priv->msg, timeout))
2348                         goto out;
2349                 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2350                 break;
2351         default:
2352                 goto out;
2353         }
2354
2355         cm_id_priv->msg->context[1] = (void *) (unsigned long)
2356                                       cm_id_priv->id.state;
2357         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2358         if (!ret)
2359                 list_add_tail(&work->list, &cm_id_priv->work_list);
2360         spin_unlock_irq(&cm_id_priv->lock);
2361
2362         if (ret)
2363                 cm_process_work(cm_id_priv, work);
2364         else
2365                 cm_deref_id(cm_id_priv);
2366         return 0;
2367 out:
2368         spin_unlock_irq(&cm_id_priv->lock);
2369         cm_deref_id(cm_id_priv);
2370         return -EINVAL;
2371 }
2372
2373 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2374                           struct cm_id_private *cm_id_priv,
2375                           struct ib_sa_path_rec *alternate_path,
2376                           const void *private_data,
2377                           u8 private_data_len)
2378 {
2379         cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2380                           cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2381         lap_msg->local_comm_id = cm_id_priv->id.local_id;
2382         lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2383         cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2384         /* todo: need remote CM response timeout */
2385         cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2386         lap_msg->alt_local_lid = alternate_path->slid;
2387         lap_msg->alt_remote_lid = alternate_path->dlid;
2388         lap_msg->alt_local_gid = alternate_path->sgid;
2389         lap_msg->alt_remote_gid = alternate_path->dgid;
2390         cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2391         cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2392         lap_msg->alt_hop_limit = alternate_path->hop_limit;
2393         cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2394         cm_lap_set_sl(lap_msg, alternate_path->sl);
2395         cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2396         cm_lap_set_local_ack_timeout(lap_msg,
2397                 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2398                                alternate_path->packet_life_time));
2399
2400         if (private_data && private_data_len)
2401                 memcpy(lap_msg->private_data, private_data, private_data_len);
2402 }
2403
2404 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2405                    struct ib_sa_path_rec *alternate_path,
2406                    const void *private_data,
2407                    u8 private_data_len)
2408 {
2409         struct cm_id_private *cm_id_priv;
2410         struct ib_mad_send_buf *msg;
2411         unsigned long flags;
2412         int ret;
2413
2414         if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2415                 return -EINVAL;
2416
2417         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2418         spin_lock_irqsave(&cm_id_priv->lock, flags);
2419         if (cm_id->state != IB_CM_ESTABLISHED ||
2420             (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2421              cm_id->lap_state != IB_CM_LAP_IDLE)) {
2422                 ret = -EINVAL;
2423                 goto out;
2424         }
2425
2426         ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2427         if (ret)
2428                 goto out;
2429         cm_id_priv->alt_av.timeout =
2430                         cm_ack_timeout(cm_id_priv->target_ack_delay,
2431                                        cm_id_priv->alt_av.timeout - 1);
2432
2433         ret = cm_alloc_msg(cm_id_priv, &msg);
2434         if (ret)
2435                 goto out;
2436
2437         cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2438                       alternate_path, private_data, private_data_len);
2439         msg->timeout_ms = cm_id_priv->timeout_ms;
2440         msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2441
2442         ret = ib_post_send_mad(msg, NULL);
2443         if (ret) {
2444                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2445                 cm_free_msg(msg);
2446                 return ret;
2447         }
2448
2449         cm_id->lap_state = IB_CM_LAP_SENT;
2450         cm_id_priv->msg = msg;
2451
2452 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2453         return ret;
2454 }
2455 EXPORT_SYMBOL(ib_send_cm_lap);
2456
2457 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2458                                     struct ib_sa_path_rec *path,
2459                                     struct cm_lap_msg *lap_msg)
2460 {
2461         memset(path, 0, sizeof *path);
2462         path->dgid = lap_msg->alt_local_gid;
2463         path->sgid = lap_msg->alt_remote_gid;
2464         path->dlid = lap_msg->alt_local_lid;
2465         path->slid = lap_msg->alt_remote_lid;
2466         path->flow_label = cm_lap_get_flow_label(lap_msg);
2467         path->hop_limit = lap_msg->alt_hop_limit;
2468         path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2469         path->reversible = 1;
2470         path->pkey = cm_id_priv->pkey;
2471         path->sl = cm_lap_get_sl(lap_msg);
2472         path->mtu_selector = IB_SA_EQ;
2473         path->mtu = cm_id_priv->path_mtu;
2474         path->rate_selector = IB_SA_EQ;
2475         path->rate = cm_lap_get_packet_rate(lap_msg);
2476         path->packet_life_time_selector = IB_SA_EQ;
2477         path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2478         path->packet_life_time -= (path->packet_life_time > 0);
2479 }
2480
2481 static int cm_lap_handler(struct cm_work *work)
2482 {
2483         struct cm_id_private *cm_id_priv;
2484         struct cm_lap_msg *lap_msg;
2485         struct ib_cm_lap_event_param *param;
2486         struct ib_mad_send_buf *msg = NULL;
2487         int ret;
2488
2489         /* todo: verify LAP request and send reject APR if invalid. */
2490         lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2491         cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2492                                    lap_msg->local_comm_id);
2493         if (!cm_id_priv)
2494                 return -EINVAL;
2495
2496         param = &work->cm_event.param.lap_rcvd;
2497         param->alternate_path = &work->path[0];
2498         cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2499         work->cm_event.private_data = &lap_msg->private_data;
2500
2501         spin_lock_irq(&cm_id_priv->lock);
2502         if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2503                 goto unlock;
2504
2505         switch (cm_id_priv->id.lap_state) {
2506         case IB_CM_LAP_UNINIT:
2507         case IB_CM_LAP_IDLE:
2508                 break;
2509         case IB_CM_MRA_LAP_SENT:
2510                 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2511                         goto unlock;
2512
2513                 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2514                               CM_MSG_RESPONSE_OTHER,
2515                               cm_id_priv->service_timeout,
2516                               cm_id_priv->private_data,
2517                               cm_id_priv->private_data_len);
2518                 spin_unlock_irq(&cm_id_priv->lock);
2519
2520                 if (ib_post_send_mad(msg, NULL))
2521                         cm_free_msg(msg);
2522                 goto deref;
2523         default:
2524                 goto unlock;
2525         }
2526
2527         cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2528         cm_id_priv->tid = lap_msg->hdr.tid;
2529         cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2530                                 work->mad_recv_wc->recv_buf.grh,
2531                                 &cm_id_priv->av);
2532         cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2533         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2534         if (!ret)
2535                 list_add_tail(&work->list, &cm_id_priv->work_list);
2536         spin_unlock_irq(&cm_id_priv->lock);
2537
2538         if (ret)
2539                 cm_process_work(cm_id_priv, work);
2540         else
2541                 cm_deref_id(cm_id_priv);
2542         return 0;
2543
2544 unlock: spin_unlock_irq(&cm_id_priv->lock);
2545 deref:  cm_deref_id(cm_id_priv);
2546         return -EINVAL;
2547 }
2548
2549 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2550                           struct cm_id_private *cm_id_priv,
2551                           enum ib_cm_apr_status status,
2552                           void *info,
2553                           u8 info_length,
2554                           const void *private_data,
2555                           u8 private_data_len)
2556 {
2557         cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2558         apr_msg->local_comm_id = cm_id_priv->id.local_id;
2559         apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2560         apr_msg->ap_status = (u8) status;
2561
2562         if (info && info_length) {
2563                 apr_msg->info_length = info_length;
2564                 memcpy(apr_msg->info, info, info_length);
2565         }
2566
2567         if (private_data && private_data_len)
2568                 memcpy(apr_msg->private_data, private_data, private_data_len);
2569 }
2570
2571 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2572                    enum ib_cm_apr_status status,
2573                    void *info,
2574                    u8 info_length,
2575                    const void *private_data,
2576                    u8 private_data_len)
2577 {
2578         struct cm_id_private *cm_id_priv;
2579         struct ib_mad_send_buf *msg;
2580         unsigned long flags;
2581         int ret;
2582
2583         if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2584             (info && info_length > IB_CM_APR_INFO_LENGTH))
2585                 return -EINVAL;
2586
2587         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2588         spin_lock_irqsave(&cm_id_priv->lock, flags);
2589         if (cm_id->state != IB_CM_ESTABLISHED ||
2590             (cm_id->lap_state != IB_CM_LAP_RCVD &&
2591              cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2592                 ret = -EINVAL;
2593                 goto out;
2594         }
2595
2596         ret = cm_alloc_msg(cm_id_priv, &msg);
2597         if (ret)
2598                 goto out;
2599
2600         cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2601                       info, info_length, private_data, private_data_len);
2602         ret = ib_post_send_mad(msg, NULL);
2603         if (ret) {
2604                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2605                 cm_free_msg(msg);
2606                 return ret;
2607         }
2608
2609         cm_id->lap_state = IB_CM_LAP_IDLE;
2610 out:    spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2611         return ret;
2612 }
2613 EXPORT_SYMBOL(ib_send_cm_apr);
2614
2615 static int cm_apr_handler(struct cm_work *work)
2616 {
2617         struct cm_id_private *cm_id_priv;
2618         struct cm_apr_msg *apr_msg;
2619         int ret;
2620
2621         apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2622         cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2623                                    apr_msg->local_comm_id);
2624         if (!cm_id_priv)
2625                 return -EINVAL; /* Unmatched reply. */
2626
2627         work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2628         work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2629         work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2630         work->cm_event.private_data = &apr_msg->private_data;
2631
2632         spin_lock_irq(&cm_id_priv->lock);
2633         if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2634             (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2635              cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2636                 spin_unlock_irq(&cm_id_priv->lock);
2637                 goto out;
2638         }
2639         cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2640         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2641         cm_id_priv->msg = NULL;
2642
2643         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2644         if (!ret)
2645                 list_add_tail(&work->list, &cm_id_priv->work_list);
2646         spin_unlock_irq(&cm_id_priv->lock);
2647
2648         if (ret)
2649                 cm_process_work(cm_id_priv, work);
2650         else
2651                 cm_deref_id(cm_id_priv);
2652         return 0;
2653 out:
2654         cm_deref_id(cm_id_priv);
2655         return -EINVAL;
2656 }
2657
2658 static int cm_timewait_handler(struct cm_work *work)
2659 {
2660         struct cm_timewait_info *timewait_info;
2661         struct cm_id_private *cm_id_priv;
2662         int ret;
2663
2664         timewait_info = (struct cm_timewait_info *)work;
2665         spin_lock_irq(&cm.lock);
2666         list_del(&timewait_info->list);
2667         spin_unlock_irq(&cm.lock);
2668
2669         cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2670                                    timewait_info->work.remote_id);
2671         if (!cm_id_priv)
2672                 return -EINVAL;
2673
2674         spin_lock_irq(&cm_id_priv->lock);
2675         if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2676             cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2677                 spin_unlock_irq(&cm_id_priv->lock);
2678                 goto out;
2679         }
2680         cm_id_priv->id.state = IB_CM_IDLE;
2681         ret = atomic_inc_and_test(&cm_id_priv->work_count);
2682         if (!ret)
2683                 list_add_tail(&work->list, &cm_id_priv->work_list);
2684         spin_unlock_irq(&cm_id_priv->lock);
2685
2686         if (ret)
2687                 cm_process_work(cm_id_priv, work);
2688         else
2689                 cm_deref_id(cm_id_priv);
2690         return 0;
2691 out:
2692         cm_deref_id(cm_id_priv);
2693         return -EINVAL;
2694 }
2695
2696 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2697                                struct cm_id_private *cm_id_priv,
2698                                struct ib_cm_sidr_req_param *param)
2699 {
2700         cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2701                           cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2702         sidr_req_msg->request_id = cm_id_priv->id.local_id;
2703         sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2704         sidr_req_msg->service_id = param->service_id;
2705
2706         if (param->private_data && param->private_data_len)
2707                 memcpy(sidr_req_msg->private_data, param->private_data,
2708                        param->private_data_len);
2709 }
2710
2711 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2712                         struct ib_cm_sidr_req_param *param)
2713 {
2714         struct cm_id_private *cm_id_priv;
2715         struct ib_mad_send_buf *msg;
2716         unsigned long flags;
2717         int ret;
2718
2719         if (!param->path || (param->private_data &&
2720              param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2721                 return -EINVAL;
2722
2723         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2724         ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2725         if (ret)
2726                 goto out;
2727
2728         cm_id->service_id = param->service_id;
2729         cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2730         cm_id_priv->timeout_ms = param->timeout_ms;
2731         cm_id_priv->max_cm_retries = param->max_cm_retries;
2732         ret = cm_alloc_msg(cm_id_priv, &msg);
2733         if (ret)
2734                 goto out;
2735
2736         cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2737                            param);
2738         msg->timeout_ms = cm_id_priv->timeout_ms;
2739         msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2740
2741         spin_lock_irqsave(&cm_id_priv->lock, flags);
2742         if (cm_id->state == IB_CM_IDLE)
2743                 ret = ib_post_send_mad(msg, NULL);
2744         else
2745                 ret = -EINVAL;
2746
2747         if (ret) {
2748                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2749                 cm_free_msg(msg);
2750                 goto out;
2751         }
2752         cm_id->state = IB_CM_SIDR_REQ_SENT;
2753         cm_id_priv->msg = msg;
2754         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2755 out:
2756         return ret;
2757 }
2758 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2759
2760 static void cm_format_sidr_req_event(struct cm_work *work,
2761                                      struct ib_cm_id *listen_id)
2762 {
2763         struct cm_sidr_req_msg *sidr_req_msg;
2764         struct ib_cm_sidr_req_event_param *param;
2765
2766         sidr_req_msg = (struct cm_sidr_req_msg *)
2767                                 work->mad_recv_wc->recv_buf.mad;
2768         param = &work->cm_event.param.sidr_req_rcvd;
2769         param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2770         param->listen_id = listen_id;
2771         param->port = work->port->port_num;
2772         work->cm_event.private_data = &sidr_req_msg->private_data;
2773 }
2774
2775 static int cm_sidr_req_handler(struct cm_work *work)
2776 {
2777         struct ib_cm_id *cm_id;
2778         struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2779         struct cm_sidr_req_msg *sidr_req_msg;
2780         struct ib_wc *wc;
2781
2782         cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2783         if (IS_ERR(cm_id))
2784                 return PTR_ERR(cm_id);
2785         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2786
2787         /* Record SGID/SLID and request ID for lookup. */
2788         sidr_req_msg = (struct cm_sidr_req_msg *)
2789                                 work->mad_recv_wc->recv_buf.mad;
2790         wc = work->mad_recv_wc->wc;
2791         cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2792         cm_id_priv->av.dgid.global.interface_id = 0;
2793         cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2794                                 work->mad_recv_wc->recv_buf.grh,
2795                                 &cm_id_priv->av);
2796         cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2797         cm_id_priv->tid = sidr_req_msg->hdr.tid;
2798         atomic_inc(&cm_id_priv->work_count);
2799
2800         spin_lock_irq(&cm.lock);
2801         cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2802         if (cur_cm_id_priv) {
2803                 spin_unlock_irq(&cm.lock);
2804                 goto out; /* Duplicate message. */
2805         }
2806         cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2807         cur_cm_id_priv = cm_find_listen(cm_id->device,
2808                                         sidr_req_msg->service_id,
2809                                         sidr_req_msg->private_data);
2810         if (!cur_cm_id_priv) {
2811                 spin_unlock_irq(&cm.lock);
2812                 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
2813                 goto out; /* No match. */
2814         }
2815         atomic_inc(&cur_cm_id_priv->refcount);
2816         spin_unlock_irq(&cm.lock);
2817
2818         cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2819         cm_id_priv->id.context = cur_cm_id_priv->id.context;
2820         cm_id_priv->id.service_id = sidr_req_msg->service_id;
2821         cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2822
2823         cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2824         cm_process_work(cm_id_priv, work);
2825         cm_deref_id(cur_cm_id_priv);
2826         return 0;
2827 out:
2828         ib_destroy_cm_id(&cm_id_priv->id);
2829         return -EINVAL;
2830 }
2831
2832 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2833                                struct cm_id_private *cm_id_priv,
2834                                struct ib_cm_sidr_rep_param *param)
2835 {
2836         cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2837                           cm_id_priv->tid);
2838         sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2839         sidr_rep_msg->status = param->status;
2840         cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2841         sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2842         sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2843
2844         if (param->info && param->info_length)
2845                 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2846
2847         if (param->private_data && param->private_data_len)
2848                 memcpy(sidr_rep_msg->private_data, param->private_data,
2849                        param->private_data_len);
2850 }
2851
2852 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2853                         struct ib_cm_sidr_rep_param *param)
2854 {
2855         struct cm_id_private *cm_id_priv;
2856         struct ib_mad_send_buf *msg;
2857         unsigned long flags;
2858         int ret;
2859
2860         if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2861             (param->private_data &&
2862              param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2863                 return -EINVAL;
2864
2865         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2866         spin_lock_irqsave(&cm_id_priv->lock, flags);
2867         if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2868                 ret = -EINVAL;
2869                 goto error;
2870         }
2871
2872         ret = cm_alloc_msg(cm_id_priv, &msg);
2873         if (ret)
2874                 goto error;
2875
2876         cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2877                            param);
2878         ret = ib_post_send_mad(msg, NULL);
2879         if (ret) {
2880                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2881                 cm_free_msg(msg);
2882                 return ret;
2883         }
2884         cm_id->state = IB_CM_IDLE;
2885         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2886
2887         spin_lock_irqsave(&cm.lock, flags);
2888         rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2889         spin_unlock_irqrestore(&cm.lock, flags);
2890         return 0;
2891
2892 error:  spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2893         return ret;
2894 }
2895 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2896
2897 static void cm_format_sidr_rep_event(struct cm_work *work)
2898 {
2899         struct cm_sidr_rep_msg *sidr_rep_msg;
2900         struct ib_cm_sidr_rep_event_param *param;
2901
2902         sidr_rep_msg = (struct cm_sidr_rep_msg *)
2903                                 work->mad_recv_wc->recv_buf.mad;
2904         param = &work->cm_event.param.sidr_rep_rcvd;
2905         param->status = sidr_rep_msg->status;
2906         param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2907         param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2908         param->info = &sidr_rep_msg->info;
2909         param->info_len = sidr_rep_msg->info_length;
2910         work->cm_event.private_data = &sidr_rep_msg->private_data;
2911 }
2912
2913 static int cm_sidr_rep_handler(struct cm_work *work)
2914 {
2915         struct cm_sidr_rep_msg *sidr_rep_msg;
2916         struct cm_id_private *cm_id_priv;
2917
2918         sidr_rep_msg = (struct cm_sidr_rep_msg *)
2919                                 work->mad_recv_wc->recv_buf.mad;
2920         cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2921         if (!cm_id_priv)
2922                 return -EINVAL; /* Unmatched reply. */
2923
2924         spin_lock_irq(&cm_id_priv->lock);
2925         if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2926                 spin_unlock_irq(&cm_id_priv->lock);
2927                 goto out;
2928         }
2929         cm_id_priv->id.state = IB_CM_IDLE;
2930         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2931         spin_unlock_irq(&cm_id_priv->lock);
2932
2933         cm_format_sidr_rep_event(work);
2934         cm_process_work(cm_id_priv, work);
2935         return 0;
2936 out:
2937         cm_deref_id(cm_id_priv);
2938         return -EINVAL;
2939 }
2940
2941 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2942                                   enum ib_wc_status wc_status)
2943 {
2944         struct cm_id_private *cm_id_priv;
2945         struct ib_cm_event cm_event;
2946         enum ib_cm_state state;
2947         int ret;
2948
2949         memset(&cm_event, 0, sizeof cm_event);
2950         cm_id_priv = msg->context[0];
2951
2952         /* Discard old sends or ones without a response. */
2953         spin_lock_irq(&cm_id_priv->lock);
2954         state = (enum ib_cm_state) (unsigned long) msg->context[1];
2955         if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2956                 goto discard;
2957
2958         switch (state) {
2959         case IB_CM_REQ_SENT:
2960         case IB_CM_MRA_REQ_RCVD:
2961                 cm_reset_to_idle(cm_id_priv);
2962                 cm_event.event = IB_CM_REQ_ERROR;
2963                 break;
2964         case IB_CM_REP_SENT:
2965         case IB_CM_MRA_REP_RCVD:
2966                 cm_reset_to_idle(cm_id_priv);
2967                 cm_event.event = IB_CM_REP_ERROR;
2968                 break;
2969         case IB_CM_DREQ_SENT:
2970                 cm_enter_timewait(cm_id_priv);
2971                 cm_event.event = IB_CM_DREQ_ERROR;
2972                 break;
2973         case IB_CM_SIDR_REQ_SENT:
2974                 cm_id_priv->id.state = IB_CM_IDLE;
2975                 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2976                 break;
2977         default:
2978                 goto discard;
2979         }
2980         spin_unlock_irq(&cm_id_priv->lock);
2981         cm_event.param.send_status = wc_status;
2982
2983         /* No other events can occur on the cm_id at this point. */
2984         ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2985         cm_free_msg(msg);
2986         if (ret)
2987                 ib_destroy_cm_id(&cm_id_priv->id);
2988         return;
2989 discard:
2990         spin_unlock_irq(&cm_id_priv->lock);
2991         cm_free_msg(msg);
2992 }
2993
2994 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2995                             struct ib_mad_send_wc *mad_send_wc)
2996 {
2997         struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2998
2999         switch (mad_send_wc->status) {
3000         case IB_WC_SUCCESS:
3001         case IB_WC_WR_FLUSH_ERR:
3002                 cm_free_msg(msg);
3003                 break;
3004         default:
3005                 if (msg->context[0] && msg->context[1])
3006                         cm_process_send_error(msg, mad_send_wc->status);
3007                 else
3008                         cm_free_msg(msg);
3009                 break;
3010         }
3011 }
3012
3013 static void cm_work_handler(struct work_struct *_work)
3014 {
3015         struct cm_work *work = container_of(_work, struct cm_work, work.work);
3016         int ret;
3017
3018         switch (work->cm_event.event) {
3019         case IB_CM_REQ_RECEIVED:
3020                 ret = cm_req_handler(work);
3021                 break;
3022         case IB_CM_MRA_RECEIVED:
3023                 ret = cm_mra_handler(work);
3024                 break;
3025         case IB_CM_REJ_RECEIVED:
3026                 ret = cm_rej_handler(work);
3027                 break;
3028         case IB_CM_REP_RECEIVED:
3029                 ret = cm_rep_handler(work);
3030                 break;
3031         case IB_CM_RTU_RECEIVED:
3032                 ret = cm_rtu_handler(work);
3033                 break;
3034         case IB_CM_USER_ESTABLISHED:
3035                 ret = cm_establish_handler(work);
3036                 break;
3037         case IB_CM_DREQ_RECEIVED:
3038                 ret = cm_dreq_handler(work);
3039                 break;
3040         case IB_CM_DREP_RECEIVED:
3041                 ret = cm_drep_handler(work);
3042                 break;
3043         case IB_CM_SIDR_REQ_RECEIVED:
3044                 ret = cm_sidr_req_handler(work);
3045                 break;
3046         case IB_CM_SIDR_REP_RECEIVED:
3047                 ret = cm_sidr_rep_handler(work);
3048                 break;
3049         case IB_CM_LAP_RECEIVED:
3050                 ret = cm_lap_handler(work);
3051                 break;
3052         case IB_CM_APR_RECEIVED:
3053                 ret = cm_apr_handler(work);
3054                 break;
3055         case IB_CM_TIMEWAIT_EXIT:
3056                 ret = cm_timewait_handler(work);
3057                 break;
3058         default:
3059                 ret = -EINVAL;
3060                 break;
3061         }
3062         if (ret)
3063                 cm_free_work(work);
3064 }
3065
3066 static int cm_establish(struct ib_cm_id *cm_id)
3067 {
3068         struct cm_id_private *cm_id_priv;
3069         struct cm_work *work;
3070         unsigned long flags;
3071         int ret = 0;
3072
3073         work = kmalloc(sizeof *work, GFP_ATOMIC);
3074         if (!work)
3075                 return -ENOMEM;
3076
3077         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3078         spin_lock_irqsave(&cm_id_priv->lock, flags);
3079         switch (cm_id->state)
3080         {
3081         case IB_CM_REP_SENT:
3082         case IB_CM_MRA_REP_RCVD:
3083                 cm_id->state = IB_CM_ESTABLISHED;
3084                 break;
3085         case IB_CM_ESTABLISHED:
3086                 ret = -EISCONN;
3087                 break;
3088         default:
3089                 ret = -EINVAL;
3090                 break;
3091         }
3092         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3093
3094         if (ret) {
3095                 kfree(work);
3096                 goto out;
3097         }
3098
3099         /*
3100          * The CM worker thread may try to destroy the cm_id before it
3101          * can execute this work item.  To prevent potential deadlock,
3102          * we need to find the cm_id once we're in the context of the
3103          * worker thread, rather than holding a reference on it.
3104          */
3105         INIT_DELAYED_WORK(&work->work, cm_work_handler);
3106         work->local_id = cm_id->local_id;
3107         work->remote_id = cm_id->remote_id;
3108         work->mad_recv_wc = NULL;
3109         work->cm_event.event = IB_CM_USER_ESTABLISHED;
3110         queue_delayed_work(cm.wq, &work->work, 0);
3111 out:
3112         return ret;
3113 }
3114
3115 static int cm_migrate(struct ib_cm_id *cm_id)
3116 {
3117         struct cm_id_private *cm_id_priv;
3118         unsigned long flags;
3119         int ret = 0;
3120
3121         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3122         spin_lock_irqsave(&cm_id_priv->lock, flags);
3123         if (cm_id->state == IB_CM_ESTABLISHED &&
3124             (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3125              cm_id->lap_state == IB_CM_LAP_IDLE)) {
3126                 cm_id->lap_state = IB_CM_LAP_IDLE;
3127                 cm_id_priv->av = cm_id_priv->alt_av;
3128         } else
3129                 ret = -EINVAL;
3130         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3131
3132         return ret;
3133 }
3134
3135 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3136 {
3137         int ret;
3138
3139         switch (event) {
3140         case IB_EVENT_COMM_EST:
3141                 ret = cm_establish(cm_id);
3142                 break;
3143         case IB_EVENT_PATH_MIG:
3144                 ret = cm_migrate(cm_id);
3145                 break;
3146         default:
3147                 ret = -EINVAL;
3148         }
3149         return ret;
3150 }
3151 EXPORT_SYMBOL(ib_cm_notify);
3152
3153 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3154                             struct ib_mad_recv_wc *mad_recv_wc)
3155 {
3156         struct cm_work *work;
3157         enum ib_cm_event_type event;
3158         int paths = 0;
3159
3160         switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3161         case CM_REQ_ATTR_ID:
3162                 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3163                                                     alt_local_lid != 0);
3164                 event = IB_CM_REQ_RECEIVED;
3165                 break;
3166         case CM_MRA_ATTR_ID:
3167                 event = IB_CM_MRA_RECEIVED;
3168                 break;
3169         case CM_REJ_ATTR_ID:
3170                 event = IB_CM_REJ_RECEIVED;
3171                 break;
3172         case CM_REP_ATTR_ID:
3173                 event = IB_CM_REP_RECEIVED;
3174                 break;
3175         case CM_RTU_ATTR_ID:
3176                 event = IB_CM_RTU_RECEIVED;
3177                 break;
3178         case CM_DREQ_ATTR_ID:
3179                 event = IB_CM_DREQ_RECEIVED;
3180                 break;
3181         case CM_DREP_ATTR_ID:
3182                 event = IB_CM_DREP_RECEIVED;
3183                 break;
3184         case CM_SIDR_REQ_ATTR_ID:
3185                 event = IB_CM_SIDR_REQ_RECEIVED;
3186                 break;
3187         case CM_SIDR_REP_ATTR_ID:
3188                 event = IB_CM_SIDR_REP_RECEIVED;
3189                 break;
3190         case CM_LAP_ATTR_ID:
3191                 paths = 1;
3192                 event = IB_CM_LAP_RECEIVED;
3193                 break;
3194         case CM_APR_ATTR_ID:
3195                 event = IB_CM_APR_RECEIVED;
3196                 break;
3197         default:
3198                 ib_free_recv_mad(mad_recv_wc);
3199                 return;
3200         }
3201
3202         work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3203                        GFP_KERNEL);
3204         if (!work) {
3205                 ib_free_recv_mad(mad_recv_wc);
3206                 return;
3207         }
3208
3209         INIT_DELAYED_WORK(&work->work, cm_work_handler);
3210         work->cm_event.event = event;
3211         work->mad_recv_wc = mad_recv_wc;
3212         work->port = (struct cm_port *)mad_agent->context;
3213         queue_delayed_work(cm.wq, &work->work, 0);
3214 }
3215
3216 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3217                                 struct ib_qp_attr *qp_attr,
3218                                 int *qp_attr_mask)
3219 {
3220         unsigned long flags;
3221         int ret;
3222
3223         spin_lock_irqsave(&cm_id_priv->lock, flags);
3224         switch (cm_id_priv->id.state) {
3225         case IB_CM_REQ_SENT:
3226         case IB_CM_MRA_REQ_RCVD:
3227         case IB_CM_REQ_RCVD:
3228         case IB_CM_MRA_REQ_SENT:
3229         case IB_CM_REP_RCVD:
3230         case IB_CM_MRA_REP_SENT:
3231         case IB_CM_REP_SENT:
3232         case IB_CM_MRA_REP_RCVD:
3233         case IB_CM_ESTABLISHED:
3234                 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3235                                 IB_QP_PKEY_INDEX | IB_QP_PORT;
3236                 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3237                 if (cm_id_priv->responder_resources)
3238                         qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3239                                                     IB_ACCESS_REMOTE_ATOMIC;
3240                 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3241                 qp_attr->port_num = cm_id_priv->av.port->port_num;
3242                 ret = 0;
3243                 break;
3244         default:
3245                 ret = -EINVAL;
3246                 break;
3247         }
3248         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3249         return ret;
3250 }
3251
3252 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3253                                struct ib_qp_attr *qp_attr,
3254                                int *qp_attr_mask)
3255 {
3256         unsigned long flags;
3257         int ret;
3258
3259         spin_lock_irqsave(&cm_id_priv->lock, flags);
3260         switch (cm_id_priv->id.state) {
3261         case IB_CM_REQ_RCVD:
3262         case IB_CM_MRA_REQ_SENT:
3263         case IB_CM_REP_RCVD:
3264         case IB_CM_MRA_REP_SENT:
3265         case IB_CM_REP_SENT:
3266         case IB_CM_MRA_REP_RCVD:
3267         case IB_CM_ESTABLISHED:
3268                 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3269                                 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3270                 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3271                 qp_attr->path_mtu = cm_id_priv->path_mtu;
3272                 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3273                 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3274                 if (cm_id_priv->qp_type == IB_QPT_RC) {
3275                         *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3276                                          IB_QP_MIN_RNR_TIMER;
3277                         qp_attr->max_dest_rd_atomic =
3278                                         cm_id_priv->responder_resources;
3279                         qp_attr->min_rnr_timer = 0;
3280                 }
3281                 if (cm_id_priv->alt_av.ah_attr.dlid) {
3282                         *qp_attr_mask |= IB_QP_ALT_PATH;
3283                         qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3284                         qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3285                         qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3286                         qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3287                 }
3288                 ret = 0;
3289                 break;
3290         default:
3291                 ret = -EINVAL;
3292                 break;
3293         }
3294         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3295         return ret;
3296 }
3297
3298 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3299                                struct ib_qp_attr *qp_attr,
3300                                int *qp_attr_mask)
3301 {
3302         unsigned long flags;
3303         int ret;
3304
3305         spin_lock_irqsave(&cm_id_priv->lock, flags);
3306         switch (cm_id_priv->id.state) {
3307         /* Allow transition to RTS before sending REP */
3308         case IB_CM_REQ_RCVD:
3309         case IB_CM_MRA_REQ_SENT:
3310
3311         case IB_CM_REP_RCVD:
3312         case IB_CM_MRA_REP_SENT:
3313         case IB_CM_REP_SENT:
3314         case IB_CM_MRA_REP_RCVD:
3315         case IB_CM_ESTABLISHED:
3316                 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3317                         *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3318                         qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3319                         if (cm_id_priv->qp_type == IB_QPT_RC) {
3320                                 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3321                                                  IB_QP_RNR_RETRY |
3322                                                  IB_QP_MAX_QP_RD_ATOMIC;
3323                                 qp_attr->timeout = cm_id_priv->av.timeout;
3324                                 qp_attr->retry_cnt = cm_id_priv->retry_count;
3325                                 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3326                                 qp_attr->max_rd_atomic =
3327                                         cm_id_priv->initiator_depth;
3328                         }
3329                         if (cm_id_priv->alt_av.ah_attr.dlid) {
3330                                 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3331                                 qp_attr->path_mig_state = IB_MIG_REARM;
3332                         }
3333                 } else {
3334                         *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3335                         qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3336                         qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3337                         qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3338                         qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3339                         qp_attr->path_mig_state = IB_MIG_REARM;
3340                 }
3341                 ret = 0;
3342                 break;
3343         default:
3344                 ret = -EINVAL;
3345                 break;
3346         }
3347         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3348         return ret;
3349 }
3350
3351 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3352                        struct ib_qp_attr *qp_attr,
3353                        int *qp_attr_mask)
3354 {
3355         struct cm_id_private *cm_id_priv;
3356         int ret;
3357
3358         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3359         switch (qp_attr->qp_state) {
3360         case IB_QPS_INIT:
3361                 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3362                 break;
3363         case IB_QPS_RTR:
3364                 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3365                 break;
3366         case IB_QPS_RTS:
3367                 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3368                 break;
3369         default:
3370                 ret = -EINVAL;
3371                 break;
3372         }
3373         return ret;
3374 }
3375 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3376
3377 void cm_get_ack_delay(struct cm_device *cm_dev)
3378 {
3379         struct ib_device_attr attr;
3380
3381         if (ib_query_device(cm_dev->device, &attr))
3382                 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3383         else
3384                 cm_dev->ack_delay = attr.local_ca_ack_delay;
3385 }
3386
3387 static void cm_add_one(struct ib_device *device)
3388 {
3389         struct cm_device *cm_dev;
3390         struct cm_port *port;
3391         struct ib_mad_reg_req reg_req = {
3392                 .mgmt_class = IB_MGMT_CLASS_CM,
3393                 .mgmt_class_version = IB_CM_CLASS_VERSION
3394         };
3395         struct ib_port_modify port_modify = {
3396                 .set_port_cap_mask = IB_PORT_CM_SUP
3397         };
3398         unsigned long flags;
3399         int ret;
3400         u8 i;
3401
3402         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3403                 return;
3404
3405         cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3406                          device->phys_port_cnt, GFP_KERNEL);
3407         if (!cm_dev)
3408                 return;
3409
3410         cm_dev->device = device;
3411         cm_get_ack_delay(cm_dev);
3412
3413         set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3414         for (i = 1; i <= device->phys_port_cnt; i++) {
3415                 port = &cm_dev->port[i-1];
3416                 port->cm_dev = cm_dev;
3417                 port->port_num = i;
3418                 port->mad_agent = ib_register_mad_agent(device, i,
3419                                                         IB_QPT_GSI,
3420                                                         &reg_req,
3421                                                         0,
3422                                                         cm_send_handler,
3423                                                         cm_recv_handler,
3424                                                         port);
3425                 if (IS_ERR(port->mad_agent))
3426                         goto error1;
3427
3428                 ret = ib_modify_port(device, i, 0, &port_modify);
3429                 if (ret)
3430                         goto error2;
3431         }
3432         ib_set_client_data(device, &cm_client, cm_dev);
3433
3434         write_lock_irqsave(&cm.device_lock, flags);
3435         list_add_tail(&cm_dev->list, &cm.device_list);
3436         write_unlock_irqrestore(&cm.device_lock, flags);
3437         return;
3438
3439 error2:
3440         ib_unregister_mad_agent(port->mad_agent);
3441 error1:
3442         port_modify.set_port_cap_mask = 0;
3443         port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3444         while (--i) {
3445                 port = &cm_dev->port[i-1];
3446                 ib_modify_port(device, port->port_num, 0, &port_modify);
3447                 ib_unregister_mad_agent(port->mad_agent);
3448         }
3449         kfree(cm_dev);
3450 }
3451
3452 static void cm_remove_one(struct ib_device *device)
3453 {
3454         struct cm_device *cm_dev;
3455         struct cm_port *port;
3456         struct ib_port_modify port_modify = {
3457                 .clr_port_cap_mask = IB_PORT_CM_SUP
3458         };
3459         unsigned long flags;
3460         int i;
3461
3462         cm_dev = ib_get_client_data(device, &cm_client);
3463         if (!cm_dev)
3464                 return;
3465
3466         write_lock_irqsave(&cm.device_lock, flags);
3467         list_del(&cm_dev->list);
3468         write_unlock_irqrestore(&cm.device_lock, flags);
3469
3470         for (i = 1; i <= device->phys_port_cnt; i++) {
3471                 port = &cm_dev->port[i-1];
3472                 ib_modify_port(device, port->port_num, 0, &port_modify);
3473                 ib_unregister_mad_agent(port->mad_agent);
3474         }
3475         kfree(cm_dev);
3476 }
3477
3478 static int __init ib_cm_init(void)
3479 {
3480         int ret;
3481
3482         memset(&cm, 0, sizeof cm);
3483         INIT_LIST_HEAD(&cm.device_list);
3484         rwlock_init(&cm.device_lock);
3485         spin_lock_init(&cm.lock);
3486         cm.listen_service_table = RB_ROOT;
3487         cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3488         cm.remote_id_table = RB_ROOT;
3489         cm.remote_qp_table = RB_ROOT;
3490         cm.remote_sidr_table = RB_ROOT;
3491         idr_init(&cm.local_id_table);
3492         get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3493         idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3494         INIT_LIST_HEAD(&cm.timewait_list);
3495
3496         cm.wq = create_workqueue("ib_cm");
3497         if (!cm.wq)
3498                 return -ENOMEM;
3499
3500         ret = ib_register_client(&cm_client);
3501         if (ret)
3502                 goto error;
3503
3504         return 0;
3505 error:
3506         destroy_workqueue(cm.wq);
3507         return ret;
3508 }
3509
3510 static void __exit ib_cm_cleanup(void)
3511 {
3512         struct cm_timewait_info *timewait_info, *tmp;
3513
3514         spin_lock_irq(&cm.lock);
3515         list_for_each_entry(timewait_info, &cm.timewait_list, list)
3516                 cancel_delayed_work(&timewait_info->work.work);
3517         spin_unlock_irq(&cm.lock);
3518
3519         destroy_workqueue(cm.wq);
3520
3521         list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3522                 list_del(&timewait_info->list);
3523                 kfree(timewait_info);
3524         }
3525
3526         ib_unregister_client(&cm_client);
3527         idr_destroy(&cm.local_id_table);
3528 }
3529
3530 module_init(ib_cm_init);
3531 module_exit(ib_cm_cleanup);
3532