IB/cm: Include HCA ACK delay in local ACK timeout
[linux-2.6.git] / 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->id.state = IB_CM_SIDR_REQ_RCVD;
2798         cm_id_priv->tid = sidr_req_msg->hdr.tid;
2799         atomic_inc(&cm_id_priv->work_count);
2800
2801         spin_lock_irq(&cm.lock);
2802         cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2803         if (cur_cm_id_priv) {
2804                 spin_unlock_irq(&cm.lock);
2805                 goto out; /* Duplicate message. */
2806         }
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                 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2812                 spin_unlock_irq(&cm.lock);
2813                 /* todo: reply with no match */
2814                 goto out; /* No match. */
2815         }
2816         atomic_inc(&cur_cm_id_priv->refcount);
2817         spin_unlock_irq(&cm.lock);
2818
2819         cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2820         cm_id_priv->id.context = cur_cm_id_priv->id.context;
2821         cm_id_priv->id.service_id = sidr_req_msg->service_id;
2822         cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2823
2824         cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2825         cm_process_work(cm_id_priv, work);
2826         cm_deref_id(cur_cm_id_priv);
2827         return 0;
2828 out:
2829         ib_destroy_cm_id(&cm_id_priv->id);
2830         return -EINVAL;
2831 }
2832
2833 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2834                                struct cm_id_private *cm_id_priv,
2835                                struct ib_cm_sidr_rep_param *param)
2836 {
2837         cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2838                           cm_id_priv->tid);
2839         sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2840         sidr_rep_msg->status = param->status;
2841         cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2842         sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2843         sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2844
2845         if (param->info && param->info_length)
2846                 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2847
2848         if (param->private_data && param->private_data_len)
2849                 memcpy(sidr_rep_msg->private_data, param->private_data,
2850                        param->private_data_len);
2851 }
2852
2853 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2854                         struct ib_cm_sidr_rep_param *param)
2855 {
2856         struct cm_id_private *cm_id_priv;
2857         struct ib_mad_send_buf *msg;
2858         unsigned long flags;
2859         int ret;
2860
2861         if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2862             (param->private_data &&
2863              param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2864                 return -EINVAL;
2865
2866         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2867         spin_lock_irqsave(&cm_id_priv->lock, flags);
2868         if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2869                 ret = -EINVAL;
2870                 goto error;
2871         }
2872
2873         ret = cm_alloc_msg(cm_id_priv, &msg);
2874         if (ret)
2875                 goto error;
2876
2877         cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2878                            param);
2879         ret = ib_post_send_mad(msg, NULL);
2880         if (ret) {
2881                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2882                 cm_free_msg(msg);
2883                 return ret;
2884         }
2885         cm_id->state = IB_CM_IDLE;
2886         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2887
2888         spin_lock_irqsave(&cm.lock, flags);
2889         rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2890         spin_unlock_irqrestore(&cm.lock, flags);
2891         return 0;
2892
2893 error:  spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2894         return ret;
2895 }
2896 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2897
2898 static void cm_format_sidr_rep_event(struct cm_work *work)
2899 {
2900         struct cm_sidr_rep_msg *sidr_rep_msg;
2901         struct ib_cm_sidr_rep_event_param *param;
2902
2903         sidr_rep_msg = (struct cm_sidr_rep_msg *)
2904                                 work->mad_recv_wc->recv_buf.mad;
2905         param = &work->cm_event.param.sidr_rep_rcvd;
2906         param->status = sidr_rep_msg->status;
2907         param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2908         param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2909         param->info = &sidr_rep_msg->info;
2910         param->info_len = sidr_rep_msg->info_length;
2911         work->cm_event.private_data = &sidr_rep_msg->private_data;
2912 }
2913
2914 static int cm_sidr_rep_handler(struct cm_work *work)
2915 {
2916         struct cm_sidr_rep_msg *sidr_rep_msg;
2917         struct cm_id_private *cm_id_priv;
2918
2919         sidr_rep_msg = (struct cm_sidr_rep_msg *)
2920                                 work->mad_recv_wc->recv_buf.mad;
2921         cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2922         if (!cm_id_priv)
2923                 return -EINVAL; /* Unmatched reply. */
2924
2925         spin_lock_irq(&cm_id_priv->lock);
2926         if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2927                 spin_unlock_irq(&cm_id_priv->lock);
2928                 goto out;
2929         }
2930         cm_id_priv->id.state = IB_CM_IDLE;
2931         ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2932         spin_unlock_irq(&cm_id_priv->lock);
2933
2934         cm_format_sidr_rep_event(work);
2935         cm_process_work(cm_id_priv, work);
2936         return 0;
2937 out:
2938         cm_deref_id(cm_id_priv);
2939         return -EINVAL;
2940 }
2941
2942 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2943                                   enum ib_wc_status wc_status)
2944 {
2945         struct cm_id_private *cm_id_priv;
2946         struct ib_cm_event cm_event;
2947         enum ib_cm_state state;
2948         int ret;
2949
2950         memset(&cm_event, 0, sizeof cm_event);
2951         cm_id_priv = msg->context[0];
2952
2953         /* Discard old sends or ones without a response. */
2954         spin_lock_irq(&cm_id_priv->lock);
2955         state = (enum ib_cm_state) (unsigned long) msg->context[1];
2956         if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2957                 goto discard;
2958
2959         switch (state) {
2960         case IB_CM_REQ_SENT:
2961         case IB_CM_MRA_REQ_RCVD:
2962                 cm_reset_to_idle(cm_id_priv);
2963                 cm_event.event = IB_CM_REQ_ERROR;
2964                 break;
2965         case IB_CM_REP_SENT:
2966         case IB_CM_MRA_REP_RCVD:
2967                 cm_reset_to_idle(cm_id_priv);
2968                 cm_event.event = IB_CM_REP_ERROR;
2969                 break;
2970         case IB_CM_DREQ_SENT:
2971                 cm_enter_timewait(cm_id_priv);
2972                 cm_event.event = IB_CM_DREQ_ERROR;
2973                 break;
2974         case IB_CM_SIDR_REQ_SENT:
2975                 cm_id_priv->id.state = IB_CM_IDLE;
2976                 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2977                 break;
2978         default:
2979                 goto discard;
2980         }
2981         spin_unlock_irq(&cm_id_priv->lock);
2982         cm_event.param.send_status = wc_status;
2983
2984         /* No other events can occur on the cm_id at this point. */
2985         ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2986         cm_free_msg(msg);
2987         if (ret)
2988                 ib_destroy_cm_id(&cm_id_priv->id);
2989         return;
2990 discard:
2991         spin_unlock_irq(&cm_id_priv->lock);
2992         cm_free_msg(msg);
2993 }
2994
2995 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2996                             struct ib_mad_send_wc *mad_send_wc)
2997 {
2998         struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2999
3000         switch (mad_send_wc->status) {
3001         case IB_WC_SUCCESS:
3002         case IB_WC_WR_FLUSH_ERR:
3003                 cm_free_msg(msg);
3004                 break;
3005         default:
3006                 if (msg->context[0] && msg->context[1])
3007                         cm_process_send_error(msg, mad_send_wc->status);
3008                 else
3009                         cm_free_msg(msg);
3010                 break;
3011         }
3012 }
3013
3014 static void cm_work_handler(struct work_struct *_work)
3015 {
3016         struct cm_work *work = container_of(_work, struct cm_work, work.work);
3017         int ret;
3018
3019         switch (work->cm_event.event) {
3020         case IB_CM_REQ_RECEIVED:
3021                 ret = cm_req_handler(work);
3022                 break;
3023         case IB_CM_MRA_RECEIVED:
3024                 ret = cm_mra_handler(work);
3025                 break;
3026         case IB_CM_REJ_RECEIVED:
3027                 ret = cm_rej_handler(work);
3028                 break;
3029         case IB_CM_REP_RECEIVED:
3030                 ret = cm_rep_handler(work);
3031                 break;
3032         case IB_CM_RTU_RECEIVED:
3033                 ret = cm_rtu_handler(work);
3034                 break;
3035         case IB_CM_USER_ESTABLISHED:
3036                 ret = cm_establish_handler(work);
3037                 break;
3038         case IB_CM_DREQ_RECEIVED:
3039                 ret = cm_dreq_handler(work);
3040                 break;
3041         case IB_CM_DREP_RECEIVED:
3042                 ret = cm_drep_handler(work);
3043                 break;
3044         case IB_CM_SIDR_REQ_RECEIVED:
3045                 ret = cm_sidr_req_handler(work);
3046                 break;
3047         case IB_CM_SIDR_REP_RECEIVED:
3048                 ret = cm_sidr_rep_handler(work);
3049                 break;
3050         case IB_CM_LAP_RECEIVED:
3051                 ret = cm_lap_handler(work);
3052                 break;
3053         case IB_CM_APR_RECEIVED:
3054                 ret = cm_apr_handler(work);
3055                 break;
3056         case IB_CM_TIMEWAIT_EXIT:
3057                 ret = cm_timewait_handler(work);
3058                 break;
3059         default:
3060                 ret = -EINVAL;
3061                 break;
3062         }
3063         if (ret)
3064                 cm_free_work(work);
3065 }
3066
3067 static int cm_establish(struct ib_cm_id *cm_id)
3068 {
3069         struct cm_id_private *cm_id_priv;
3070         struct cm_work *work;
3071         unsigned long flags;
3072         int ret = 0;
3073
3074         work = kmalloc(sizeof *work, GFP_ATOMIC);
3075         if (!work)
3076                 return -ENOMEM;
3077
3078         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3079         spin_lock_irqsave(&cm_id_priv->lock, flags);
3080         switch (cm_id->state)
3081         {
3082         case IB_CM_REP_SENT:
3083         case IB_CM_MRA_REP_RCVD:
3084                 cm_id->state = IB_CM_ESTABLISHED;
3085                 break;
3086         case IB_CM_ESTABLISHED:
3087                 ret = -EISCONN;
3088                 break;
3089         default:
3090                 ret = -EINVAL;
3091                 break;
3092         }
3093         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3094
3095         if (ret) {
3096                 kfree(work);
3097                 goto out;
3098         }
3099
3100         /*
3101          * The CM worker thread may try to destroy the cm_id before it
3102          * can execute this work item.  To prevent potential deadlock,
3103          * we need to find the cm_id once we're in the context of the
3104          * worker thread, rather than holding a reference on it.
3105          */
3106         INIT_DELAYED_WORK(&work->work, cm_work_handler);
3107         work->local_id = cm_id->local_id;
3108         work->remote_id = cm_id->remote_id;
3109         work->mad_recv_wc = NULL;
3110         work->cm_event.event = IB_CM_USER_ESTABLISHED;
3111         queue_delayed_work(cm.wq, &work->work, 0);
3112 out:
3113         return ret;
3114 }
3115
3116 static int cm_migrate(struct ib_cm_id *cm_id)
3117 {
3118         struct cm_id_private *cm_id_priv;
3119         unsigned long flags;
3120         int ret = 0;
3121
3122         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3123         spin_lock_irqsave(&cm_id_priv->lock, flags);
3124         if (cm_id->state == IB_CM_ESTABLISHED &&
3125             (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3126              cm_id->lap_state == IB_CM_LAP_IDLE)) {
3127                 cm_id->lap_state = IB_CM_LAP_IDLE;
3128                 cm_id_priv->av = cm_id_priv->alt_av;
3129         } else
3130                 ret = -EINVAL;
3131         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3132
3133         return ret;
3134 }
3135
3136 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3137 {
3138         int ret;
3139
3140         switch (event) {
3141         case IB_EVENT_COMM_EST:
3142                 ret = cm_establish(cm_id);
3143                 break;
3144         case IB_EVENT_PATH_MIG:
3145                 ret = cm_migrate(cm_id);
3146                 break;
3147         default:
3148                 ret = -EINVAL;
3149         }
3150         return ret;
3151 }
3152 EXPORT_SYMBOL(ib_cm_notify);
3153
3154 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3155                             struct ib_mad_recv_wc *mad_recv_wc)
3156 {
3157         struct cm_work *work;
3158         enum ib_cm_event_type event;
3159         int paths = 0;
3160
3161         switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3162         case CM_REQ_ATTR_ID:
3163                 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3164                                                     alt_local_lid != 0);
3165                 event = IB_CM_REQ_RECEIVED;
3166                 break;
3167         case CM_MRA_ATTR_ID:
3168                 event = IB_CM_MRA_RECEIVED;
3169                 break;
3170         case CM_REJ_ATTR_ID:
3171                 event = IB_CM_REJ_RECEIVED;
3172                 break;
3173         case CM_REP_ATTR_ID:
3174                 event = IB_CM_REP_RECEIVED;
3175                 break;
3176         case CM_RTU_ATTR_ID:
3177                 event = IB_CM_RTU_RECEIVED;
3178                 break;
3179         case CM_DREQ_ATTR_ID:
3180                 event = IB_CM_DREQ_RECEIVED;
3181                 break;
3182         case CM_DREP_ATTR_ID:
3183                 event = IB_CM_DREP_RECEIVED;
3184                 break;
3185         case CM_SIDR_REQ_ATTR_ID:
3186                 event = IB_CM_SIDR_REQ_RECEIVED;
3187                 break;
3188         case CM_SIDR_REP_ATTR_ID:
3189                 event = IB_CM_SIDR_REP_RECEIVED;
3190                 break;
3191         case CM_LAP_ATTR_ID:
3192                 paths = 1;
3193                 event = IB_CM_LAP_RECEIVED;
3194                 break;
3195         case CM_APR_ATTR_ID:
3196                 event = IB_CM_APR_RECEIVED;
3197                 break;
3198         default:
3199                 ib_free_recv_mad(mad_recv_wc);
3200                 return;
3201         }
3202
3203         work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3204                        GFP_KERNEL);
3205         if (!work) {
3206                 ib_free_recv_mad(mad_recv_wc);
3207                 return;
3208         }
3209
3210         INIT_DELAYED_WORK(&work->work, cm_work_handler);
3211         work->cm_event.event = event;
3212         work->mad_recv_wc = mad_recv_wc;
3213         work->port = (struct cm_port *)mad_agent->context;
3214         queue_delayed_work(cm.wq, &work->work, 0);
3215 }
3216
3217 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3218                                 struct ib_qp_attr *qp_attr,
3219                                 int *qp_attr_mask)
3220 {
3221         unsigned long flags;
3222         int ret;
3223
3224         spin_lock_irqsave(&cm_id_priv->lock, flags);
3225         switch (cm_id_priv->id.state) {
3226         case IB_CM_REQ_SENT:
3227         case IB_CM_MRA_REQ_RCVD:
3228         case IB_CM_REQ_RCVD:
3229         case IB_CM_MRA_REQ_SENT:
3230         case IB_CM_REP_RCVD:
3231         case IB_CM_MRA_REP_SENT:
3232         case IB_CM_REP_SENT:
3233         case IB_CM_MRA_REP_RCVD:
3234         case IB_CM_ESTABLISHED:
3235                 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3236                                 IB_QP_PKEY_INDEX | IB_QP_PORT;
3237                 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3238                 if (cm_id_priv->responder_resources)
3239                         qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3240                                                     IB_ACCESS_REMOTE_ATOMIC;
3241                 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3242                 qp_attr->port_num = cm_id_priv->av.port->port_num;
3243                 ret = 0;
3244                 break;
3245         default:
3246                 ret = -EINVAL;
3247                 break;
3248         }
3249         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3250         return ret;
3251 }
3252
3253 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3254                                struct ib_qp_attr *qp_attr,
3255                                int *qp_attr_mask)
3256 {
3257         unsigned long flags;
3258         int ret;
3259
3260         spin_lock_irqsave(&cm_id_priv->lock, flags);
3261         switch (cm_id_priv->id.state) {
3262         case IB_CM_REQ_RCVD:
3263         case IB_CM_MRA_REQ_SENT:
3264         case IB_CM_REP_RCVD:
3265         case IB_CM_MRA_REP_SENT:
3266         case IB_CM_REP_SENT:
3267         case IB_CM_MRA_REP_RCVD:
3268         case IB_CM_ESTABLISHED:
3269                 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3270                                 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3271                 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3272                 qp_attr->path_mtu = cm_id_priv->path_mtu;
3273                 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3274                 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3275                 if (cm_id_priv->qp_type == IB_QPT_RC) {
3276                         *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3277                                          IB_QP_MIN_RNR_TIMER;
3278                         qp_attr->max_dest_rd_atomic =
3279                                         cm_id_priv->responder_resources;
3280                         qp_attr->min_rnr_timer = 0;
3281                 }
3282                 if (cm_id_priv->alt_av.ah_attr.dlid) {
3283                         *qp_attr_mask |= IB_QP_ALT_PATH;
3284                         qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3285                         qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3286                         qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3287                         qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3288                 }
3289                 ret = 0;
3290                 break;
3291         default:
3292                 ret = -EINVAL;
3293                 break;
3294         }
3295         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3296         return ret;
3297 }
3298
3299 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3300                                struct ib_qp_attr *qp_attr,
3301                                int *qp_attr_mask)
3302 {
3303         unsigned long flags;
3304         int ret;
3305
3306         spin_lock_irqsave(&cm_id_priv->lock, flags);
3307         switch (cm_id_priv->id.state) {
3308         /* Allow transition to RTS before sending REP */
3309         case IB_CM_REQ_RCVD:
3310         case IB_CM_MRA_REQ_SENT:
3311
3312         case IB_CM_REP_RCVD:
3313         case IB_CM_MRA_REP_SENT:
3314         case IB_CM_REP_SENT:
3315         case IB_CM_MRA_REP_RCVD:
3316         case IB_CM_ESTABLISHED:
3317                 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3318                         *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3319                         qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3320                         if (cm_id_priv->qp_type == IB_QPT_RC) {
3321                                 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3322                                                  IB_QP_RNR_RETRY |
3323                                                  IB_QP_MAX_QP_RD_ATOMIC;
3324                                 qp_attr->timeout = cm_id_priv->av.timeout;
3325                                 qp_attr->retry_cnt = cm_id_priv->retry_count;
3326                                 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3327                                 qp_attr->max_rd_atomic =
3328                                         cm_id_priv->initiator_depth;
3329                         }
3330                         if (cm_id_priv->alt_av.ah_attr.dlid) {
3331                                 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3332                                 qp_attr->path_mig_state = IB_MIG_REARM;
3333                         }
3334                 } else {
3335                         *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3336                         qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3337                         qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3338                         qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3339                         qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3340                         qp_attr->path_mig_state = IB_MIG_REARM;
3341                 }
3342                 ret = 0;
3343                 break;
3344         default:
3345                 ret = -EINVAL;
3346                 break;
3347         }
3348         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3349         return ret;
3350 }
3351
3352 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3353                        struct ib_qp_attr *qp_attr,
3354                        int *qp_attr_mask)
3355 {
3356         struct cm_id_private *cm_id_priv;
3357         int ret;
3358
3359         cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3360         switch (qp_attr->qp_state) {
3361         case IB_QPS_INIT:
3362                 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3363                 break;
3364         case IB_QPS_RTR:
3365                 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3366                 break;
3367         case IB_QPS_RTS:
3368                 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3369                 break;
3370         default:
3371                 ret = -EINVAL;
3372                 break;
3373         }
3374         return ret;
3375 }
3376 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3377
3378 void cm_get_ack_delay(struct cm_device *cm_dev)
3379 {
3380         struct ib_device_attr attr;
3381
3382         if (ib_query_device(cm_dev->device, &attr))
3383                 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3384         else
3385                 cm_dev->ack_delay = attr.local_ca_ack_delay;
3386 }
3387
3388 static void cm_add_one(struct ib_device *device)
3389 {
3390         struct cm_device *cm_dev;
3391         struct cm_port *port;
3392         struct ib_mad_reg_req reg_req = {
3393                 .mgmt_class = IB_MGMT_CLASS_CM,
3394                 .mgmt_class_version = IB_CM_CLASS_VERSION
3395         };
3396         struct ib_port_modify port_modify = {
3397                 .set_port_cap_mask = IB_PORT_CM_SUP
3398         };
3399         unsigned long flags;
3400         int ret;
3401         u8 i;
3402
3403         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3404                 return;
3405
3406         cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3407                          device->phys_port_cnt, GFP_KERNEL);
3408         if (!cm_dev)
3409                 return;
3410
3411         cm_dev->device = device;
3412         cm_get_ack_delay(cm_dev);
3413
3414         set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3415         for (i = 1; i <= device->phys_port_cnt; i++) {
3416                 port = &cm_dev->port[i-1];
3417                 port->cm_dev = cm_dev;
3418                 port->port_num = i;
3419                 port->mad_agent = ib_register_mad_agent(device, i,
3420                                                         IB_QPT_GSI,
3421                                                         &reg_req,
3422                                                         0,
3423                                                         cm_send_handler,
3424                                                         cm_recv_handler,
3425                                                         port);
3426                 if (IS_ERR(port->mad_agent))
3427                         goto error1;
3428
3429                 ret = ib_modify_port(device, i, 0, &port_modify);
3430                 if (ret)
3431                         goto error2;
3432         }
3433         ib_set_client_data(device, &cm_client, cm_dev);
3434
3435         write_lock_irqsave(&cm.device_lock, flags);
3436         list_add_tail(&cm_dev->list, &cm.device_list);
3437         write_unlock_irqrestore(&cm.device_lock, flags);
3438         return;
3439
3440 error2:
3441         ib_unregister_mad_agent(port->mad_agent);
3442 error1:
3443         port_modify.set_port_cap_mask = 0;
3444         port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3445         while (--i) {
3446                 port = &cm_dev->port[i-1];
3447                 ib_modify_port(device, port->port_num, 0, &port_modify);
3448                 ib_unregister_mad_agent(port->mad_agent);
3449         }
3450         kfree(cm_dev);
3451 }
3452
3453 static void cm_remove_one(struct ib_device *device)
3454 {
3455         struct cm_device *cm_dev;
3456         struct cm_port *port;
3457         struct ib_port_modify port_modify = {
3458                 .clr_port_cap_mask = IB_PORT_CM_SUP
3459         };
3460         unsigned long flags;
3461         int i;
3462
3463         cm_dev = ib_get_client_data(device, &cm_client);
3464         if (!cm_dev)
3465                 return;
3466
3467         write_lock_irqsave(&cm.device_lock, flags);
3468         list_del(&cm_dev->list);
3469         write_unlock_irqrestore(&cm.device_lock, flags);
3470
3471         for (i = 1; i <= device->phys_port_cnt; i++) {
3472                 port = &cm_dev->port[i-1];
3473                 ib_modify_port(device, port->port_num, 0, &port_modify);
3474                 ib_unregister_mad_agent(port->mad_agent);
3475         }
3476         kfree(cm_dev);
3477 }
3478
3479 static int __init ib_cm_init(void)
3480 {
3481         int ret;
3482
3483         memset(&cm, 0, sizeof cm);
3484         INIT_LIST_HEAD(&cm.device_list);
3485         rwlock_init(&cm.device_lock);
3486         spin_lock_init(&cm.lock);
3487         cm.listen_service_table = RB_ROOT;
3488         cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3489         cm.remote_id_table = RB_ROOT;
3490         cm.remote_qp_table = RB_ROOT;
3491         cm.remote_sidr_table = RB_ROOT;
3492         idr_init(&cm.local_id_table);
3493         get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3494         idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3495         INIT_LIST_HEAD(&cm.timewait_list);
3496
3497         cm.wq = create_workqueue("ib_cm");
3498         if (!cm.wq)
3499                 return -ENOMEM;
3500
3501         ret = ib_register_client(&cm_client);
3502         if (ret)
3503                 goto error;
3504
3505         return 0;
3506 error:
3507         destroy_workqueue(cm.wq);
3508         return ret;
3509 }
3510
3511 static void __exit ib_cm_cleanup(void)
3512 {
3513         struct cm_timewait_info *timewait_info, *tmp;
3514
3515         spin_lock_irq(&cm.lock);
3516         list_for_each_entry(timewait_info, &cm.timewait_list, list)
3517                 cancel_delayed_work(&timewait_info->work.work);
3518         spin_unlock_irq(&cm.lock);
3519
3520         destroy_workqueue(cm.wq);
3521
3522         list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3523                 list_del(&timewait_info->list);
3524                 kfree(timewait_info);
3525         }
3526
3527         ib_unregister_client(&cm_client);
3528         idr_destroy(&cm.local_id_table);
3529 }
3530
3531 module_init(ib_cm_init);
3532 module_exit(ib_cm_cleanup);
3533