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