blob: faa7ce318a6d8e71300cbe7445685a6f114e975d [file] [log] [blame]
Hal Rosenstocka9770492005-07-27 11:45:40 -07001/*
Sean Hefty9af57b72007-07-16 21:49:35 -07002 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
Hal Rosenstocka9770492005-07-27 11:45:40 -07003 * 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 *
Sean Hefty6e61d042006-06-17 20:37:28 -070035 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
Hal Rosenstocka9770492005-07-27 11:45:40 -070036 */
Sean Hefty1b52fa982006-05-12 14:57:52 -070037
38#include <linux/completion.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070039#include <linux/dma-mapping.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070040#include <linux/device.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070041#include <linux/err.h>
42#include <linux/idr.h>
43#include <linux/interrupt.h>
Sean Heftyf06d2652006-08-28 15:15:18 -070044#include <linux/random.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070045#include <linux/rbtree.h>
46#include <linux/spinlock.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070047#include <linux/sysfs.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070048#include <linux/workqueue.h>
49
Roland Dreiera4d61e82005-08-25 13:40:04 -070050#include <rdma/ib_cache.h>
51#include <rdma/ib_cm.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070052#include "cm_msgs.h"
53
54MODULE_AUTHOR("Sean Hefty");
55MODULE_DESCRIPTION("InfiniBand CM");
56MODULE_LICENSE("Dual BSD/GPL");
57
58static void cm_add_one(struct ib_device *device);
59static void cm_remove_one(struct ib_device *device);
60
61static struct ib_client cm_client = {
62 .name = "cm",
63 .add = cm_add_one,
64 .remove = cm_remove_one
65};
66
67static 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;
Sean Heftyf06d2652006-08-28 15:15:18 -070078 __be32 random_id_operand;
Sean Hefty85753292006-10-04 11:29:59 -070079 struct list_head timewait_list;
Hal Rosenstocka9770492005-07-27 11:45:40 -070080 struct workqueue_struct *wq;
81} cm;
82
Sean Hefty9af57b72007-07-16 21:49:35 -070083/* Counter indexes ordered by attribute ID */
84enum {
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
100enum {
101 CM_XMIT,
102 CM_XMIT_RETRIES,
103 CM_RECV,
104 CM_RECV_DUPLICATES,
105 CM_COUNTER_GROUPS
106};
107
108static 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
114struct cm_counter_group {
115 struct kobject obj;
116 atomic_long_t counter[CM_ATTR_COUNT];
117};
118
119struct cm_counter_attribute {
120 struct attribute attr;
121 int index;
122};
123
124#define CM_COUNTER_ATTR(_name, _index) \
125struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \
127 .index = _index \
128}
129
130static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
131static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
132static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
133static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
134static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
135static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
136static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
137static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
138static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
139static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
140static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
141
142static 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
Hal Rosenstocka9770492005-07-27 11:45:40 -0700157struct cm_port {
158 struct cm_device *cm_dev;
159 struct ib_mad_agent *mad_agent;
Sean Hefty9af57b72007-07-16 21:49:35 -0700160 struct kobject port_obj;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700161 u8 port_num;
Sean Hefty9af57b72007-07-16 21:49:35 -0700162 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700163};
164
165struct cm_device {
166 struct list_head list;
167 struct ib_device *device;
Sean Hefty9af57b72007-07-16 21:49:35 -0700168 struct kobject dev_obj;
Sean Hefty1d846122007-06-18 11:09:37 -0700169 u8 ack_delay;
Sean Hefty9af57b72007-07-16 21:49:35 -0700170 struct cm_port *port[0];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700171};
172
173struct cm_av {
174 struct cm_port *port;
175 union ib_gid dgid;
176 struct ib_ah_attr ah_attr;
177 u16 pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -0700178 u8 timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700179};
180
181struct cm_work {
David Howellsc4028952006-11-22 14:57:56 +0000182 struct delayed_work work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700183 struct list_head list;
184 struct cm_port *port;
185 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700186 __be32 local_id; /* Established / timewait */
187 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700188 struct ib_cm_event cm_event;
189 struct ib_sa_path_rec path[0];
190};
191
192struct cm_timewait_info {
193 struct cm_work work; /* Must be first. */
Sean Hefty85753292006-10-04 11:29:59 -0700194 struct list_head list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700195 struct rb_node remote_qp_node;
196 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700197 __be64 remote_ca_guid;
198 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700199 u8 inserted_remote_qp;
200 u8 inserted_remote_id;
201};
202
203struct cm_id_private {
204 struct ib_cm_id id;
205
206 struct rb_node service_node;
207 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800208 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700209 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700210 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;
Sean Hefty6e61d042006-06-17 20:37:28 -0700217 struct ib_cm_compare_data *compare_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700218
219 void *private_data;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700220 __be64 tid;
221 __be32 local_qpn;
222 __be32 remote_qpn;
Sean Heftyae7971a2005-10-24 12:33:56 -0700223 enum ib_qp_type qp_type;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700224 __be32 sq_psn;
225 __be32 rq_psn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700226 int timeout_ms;
227 enum ib_mtu path_mtu;
Sean Heftye1444b52006-11-28 14:57:13 -0800228 __be16 pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700229 u8 private_data_len;
230 u8 max_cm_retries;
231 u8 peer_to_peer;
232 u8 responder_resources;
233 u8 initiator_depth;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700234 u8 retry_count;
235 u8 rnr_retry_count;
236 u8 service_timeout;
Sean Hefty1d846122007-06-18 11:09:37 -0700237 u8 target_ack_delay;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700238
239 struct list_head work_list;
240 atomic_t work_count;
241};
242
David Howellsc4028952006-11-22 14:57:56 +0000243static void cm_work_handler(struct work_struct *work);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700244
245static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
246{
247 if (atomic_dec_and_test(&cm_id_priv->refcount))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700248 complete(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700249}
250
251static 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
Roland Dreier3cd96562006-09-22 15:22:46 -0700263 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
John Kingman354ba392005-09-09 18:23:32 -0700264 cm_id_priv->av.pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700265 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700266 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. */
Sean Hefty34816ad2005-10-25 10:51:39 -0700273 m->ah = ah;
274 m->retries = cm_id_priv->max_cm_retries;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700275
276 atomic_inc(&cm_id_priv->refcount);
277 m->context[0] = cm_id_priv;
278 *msg = m;
279 return 0;
280}
281
282static 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,
Sean Hefty34816ad2005-10-25 10:51:39 -0700295 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700296 GFP_ATOMIC);
297 if (IS_ERR(m)) {
298 ib_destroy_ah(ah);
299 return PTR_ERR(m);
300 }
Sean Hefty34816ad2005-10-25 10:51:39 -0700301 m->ah = ah;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700302 *msg = m;
303 return 0;
304}
305
306static void cm_free_msg(struct ib_mad_send_buf *msg)
307{
Sean Hefty34816ad2005-10-25 10:51:39 -0700308 ib_destroy_ah(msg->ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700309 if (msg->context[0])
310 cm_deref_id(msg->context[0]);
311 ib_free_send_mad(msg);
312}
313
314static 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
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200322 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700323 if (!data)
324 return ERR_PTR(-ENOMEM);
325
Hal Rosenstocka9770492005-07-27 11:45:40 -0700326 return data;
327}
328
329static 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
Sean Heftyca222c62006-06-17 20:37:40 -0700339static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
340 struct ib_grh *grh, struct cm_av *av)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700341{
342 av->port = port;
343 av->pkey_index = wc->pkey_index;
Sean Heftyca222c62006-06-17 20:37:40 -0700344 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
345 grh, &av->ah_attr);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700346}
347
348static 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)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700360 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700361 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;
Sean Heftyca222c62006-06-17 20:37:40 -0700375 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
376 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700377 av->timeout = path->packet_life_time + 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700378 return 0;
379}
380
381static int cm_alloc_id(struct cm_id_private *cm_id_priv)
382{
383 unsigned long flags;
Sean Heftyf06d2652006-08-28 15:15:18 -0700384 int ret, id;
Sean Heftyde1bb1a2005-11-30 10:01:13 -0800385 static int next_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700386
387 do {
388 spin_lock_irqsave(&cm.lock, flags);
Sean Heftyf06d2652006-08-28 15:15:18 -0700389 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
Michael S. Tsirkin9f81036c2007-05-21 19:06:54 +0300390 next_id, &id);
391 if (!ret)
392 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700393 spin_unlock_irqrestore(&cm.lock, flags);
394 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
Sean Heftyf06d2652006-08-28 15:15:18 -0700395
Al Viro1b90c132008-03-29 03:10:28 +0000396 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700397 return ret;
398}
399
Sean Hefty97f52eb2005-08-13 21:05:57 -0700400static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700401{
Sean Hefty24be6e82007-06-18 11:09:36 -0700402 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700403 idr_remove(&cm.local_id_table,
404 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700405 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700406}
407
Sean Hefty97f52eb2005-08-13 21:05:57 -0700408static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700409{
410 struct cm_id_private *cm_id_priv;
411
Sean Heftyf06d2652006-08-28 15:15:18 -0700412 cm_id_priv = idr_find(&cm.local_id_table,
413 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700414 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
Sean Hefty97f52eb2005-08-13 21:05:57 -0700424static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700425{
426 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700427
Sean Hefty24be6e82007-06-18 11:09:36 -0700428 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700429 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700430 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700431
432 return cm_id_priv;
433}
434
Sean Hefty6e61d042006-06-17 20:37:28 -0700435static 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
444static 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
458static 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
Hal Rosenstocka9770492005-07-27 11:45:40 -0700470static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
471{
472 struct rb_node **link = &cm.listen_service_table.rb_node;
473 struct rb_node *parent = NULL;
474 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700475 __be64 service_id = cm_id_priv->id.service_id;
476 __be64 service_mask = cm_id_priv->id.service_mask;
Sean Hefty6e61d042006-06-17 20:37:28 -0700477 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700478
479 while (*link) {
480 parent = *link;
481 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
482 service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700483 data_cmp = cm_compare_data(cm_id_priv->compare_data,
484 cur_cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700485 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700486 (service_mask & cur_cm_id_priv->id.service_id) &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700487 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
488 !data_cmp)
Sean Hefty07d357d2005-10-17 15:37:43 -0700489 return cur_cm_id_priv;
490
491 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
492 link = &(*link)->rb_left;
493 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
494 link = &(*link)->rb_right;
495 else if (service_id < cur_cm_id_priv->id.service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700496 link = &(*link)->rb_left;
Sean Hefty6e61d042006-06-17 20:37:28 -0700497 else if (service_id > cur_cm_id_priv->id.service_id)
498 link = &(*link)->rb_right;
499 else if (data_cmp < 0)
500 link = &(*link)->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700501 else
502 link = &(*link)->rb_right;
503 }
504 rb_link_node(&cm_id_priv->service_node, parent, link);
505 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
506 return NULL;
507}
508
Sean Hefty07d357d2005-10-17 15:37:43 -0700509static struct cm_id_private * cm_find_listen(struct ib_device *device,
Sean Hefty6e61d042006-06-17 20:37:28 -0700510 __be64 service_id,
511 u8 *private_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700512{
513 struct rb_node *node = cm.listen_service_table.rb_node;
514 struct cm_id_private *cm_id_priv;
Sean Hefty6e61d042006-06-17 20:37:28 -0700515 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700516
517 while (node) {
518 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700519 data_cmp = cm_compare_private_data(private_data,
520 cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700521 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700522 cm_id_priv->id.service_id &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700523 (cm_id_priv->id.device == device) && !data_cmp)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700524 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700525
526 if (device < cm_id_priv->id.device)
527 node = node->rb_left;
528 else if (device > cm_id_priv->id.device)
529 node = node->rb_right;
530 else if (service_id < cm_id_priv->id.service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700531 node = node->rb_left;
Sean Hefty6e61d042006-06-17 20:37:28 -0700532 else if (service_id > cm_id_priv->id.service_id)
533 node = node->rb_right;
534 else if (data_cmp < 0)
535 node = node->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700536 else
537 node = node->rb_right;
538 }
539 return NULL;
540}
541
542static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
543 *timewait_info)
544{
545 struct rb_node **link = &cm.remote_id_table.rb_node;
546 struct rb_node *parent = NULL;
547 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700548 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
549 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700550
551 while (*link) {
552 parent = *link;
553 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
554 remote_id_node);
555 if (remote_id < cur_timewait_info->work.remote_id)
556 link = &(*link)->rb_left;
557 else if (remote_id > cur_timewait_info->work.remote_id)
558 link = &(*link)->rb_right;
559 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
560 link = &(*link)->rb_left;
561 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
562 link = &(*link)->rb_right;
563 else
564 return cur_timewait_info;
565 }
566 timewait_info->inserted_remote_id = 1;
567 rb_link_node(&timewait_info->remote_id_node, parent, link);
568 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
569 return NULL;
570}
571
Sean Hefty97f52eb2005-08-13 21:05:57 -0700572static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
573 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700574{
575 struct rb_node *node = cm.remote_id_table.rb_node;
576 struct cm_timewait_info *timewait_info;
577
578 while (node) {
579 timewait_info = rb_entry(node, struct cm_timewait_info,
580 remote_id_node);
581 if (remote_id < timewait_info->work.remote_id)
582 node = node->rb_left;
583 else if (remote_id > timewait_info->work.remote_id)
584 node = node->rb_right;
585 else if (remote_ca_guid < timewait_info->remote_ca_guid)
586 node = node->rb_left;
587 else if (remote_ca_guid > timewait_info->remote_ca_guid)
588 node = node->rb_right;
589 else
590 return timewait_info;
591 }
592 return NULL;
593}
594
595static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
596 *timewait_info)
597{
598 struct rb_node **link = &cm.remote_qp_table.rb_node;
599 struct rb_node *parent = NULL;
600 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700601 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
602 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700603
604 while (*link) {
605 parent = *link;
606 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
607 remote_qp_node);
608 if (remote_qpn < cur_timewait_info->remote_qpn)
609 link = &(*link)->rb_left;
610 else if (remote_qpn > cur_timewait_info->remote_qpn)
611 link = &(*link)->rb_right;
612 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
613 link = &(*link)->rb_left;
614 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
615 link = &(*link)->rb_right;
616 else
617 return cur_timewait_info;
618 }
619 timewait_info->inserted_remote_qp = 1;
620 rb_link_node(&timewait_info->remote_qp_node, parent, link);
621 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
622 return NULL;
623}
624
625static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
626 *cm_id_priv)
627{
628 struct rb_node **link = &cm.remote_sidr_table.rb_node;
629 struct rb_node *parent = NULL;
630 struct cm_id_private *cur_cm_id_priv;
631 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700632 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700633
634 while (*link) {
635 parent = *link;
636 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
637 sidr_id_node);
638 if (remote_id < cur_cm_id_priv->id.remote_id)
639 link = &(*link)->rb_left;
640 else if (remote_id > cur_cm_id_priv->id.remote_id)
641 link = &(*link)->rb_right;
642 else {
643 int cmp;
644 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
645 sizeof *port_gid);
646 if (cmp < 0)
647 link = &(*link)->rb_left;
648 else if (cmp > 0)
649 link = &(*link)->rb_right;
650 else
651 return cur_cm_id_priv;
652 }
653 }
654 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
655 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
656 return NULL;
657}
658
659static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
660 enum ib_cm_sidr_status status)
661{
662 struct ib_cm_sidr_rep_param param;
663
664 memset(&param, 0, sizeof param);
665 param.status = status;
666 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
667}
668
Sean Hefty07d357d2005-10-17 15:37:43 -0700669struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
670 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700671 void *context)
672{
673 struct cm_id_private *cm_id_priv;
674 int ret;
675
Roland Dreierde6eb662005-11-02 07:23:14 -0800676 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700677 if (!cm_id_priv)
678 return ERR_PTR(-ENOMEM);
679
Hal Rosenstocka9770492005-07-27 11:45:40 -0700680 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700681 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700682 cm_id_priv->id.cm_handler = cm_handler;
683 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700684 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700685 ret = cm_alloc_id(cm_id_priv);
686 if (ret)
687 goto error;
688
689 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700690 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700691 INIT_LIST_HEAD(&cm_id_priv->work_list);
692 atomic_set(&cm_id_priv->work_count, -1);
693 atomic_set(&cm_id_priv->refcount, 1);
694 return &cm_id_priv->id;
695
696error:
697 kfree(cm_id_priv);
698 return ERR_PTR(-ENOMEM);
699}
700EXPORT_SYMBOL(ib_create_cm_id);
701
702static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
703{
704 struct cm_work *work;
705
706 if (list_empty(&cm_id_priv->work_list))
707 return NULL;
708
709 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
710 list_del(&work->list);
711 return work;
712}
713
714static void cm_free_work(struct cm_work *work)
715{
716 if (work->mad_recv_wc)
717 ib_free_recv_mad(work->mad_recv_wc);
718 kfree(work);
719}
720
721static inline int cm_convert_to_ms(int iba_time)
722{
723 /* approximate conversion to ms from 4.096us x 2^iba_time */
724 return 1 << max(iba_time - 8, 0);
725}
726
Sean Hefty1d846122007-06-18 11:09:37 -0700727/*
728 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
729 * Because of how ack_timeout is stored, adding one doubles the timeout.
730 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
731 * increment it (round up) only if the other is within 50%.
732 */
733static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
734{
735 int ack_timeout = packet_life_time + 1;
736
737 if (ack_timeout >= ca_ack_delay)
738 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
739 else
740 ack_timeout = ca_ack_delay +
741 (ack_timeout >= (ca_ack_delay - 1));
742
743 return min(31, ack_timeout);
744}
745
Hal Rosenstocka9770492005-07-27 11:45:40 -0700746static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
747{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700748 if (timewait_info->inserted_remote_id) {
749 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
750 timewait_info->inserted_remote_id = 0;
751 }
752
753 if (timewait_info->inserted_remote_qp) {
754 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
755 timewait_info->inserted_remote_qp = 0;
756 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700757}
758
Sean Hefty97f52eb2005-08-13 21:05:57 -0700759static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700760{
761 struct cm_timewait_info *timewait_info;
762
Roland Dreierde6eb662005-11-02 07:23:14 -0800763 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700764 if (!timewait_info)
765 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700766
767 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000768 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700769 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
770 return timewait_info;
771}
772
773static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
774{
775 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700776 unsigned long flags;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700777
Sean Hefty85753292006-10-04 11:29:59 -0700778 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300779 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700780 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
781 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300782
Hal Rosenstocka9770492005-07-27 11:45:40 -0700783 /*
784 * The cm_id could be destroyed by the user before we exit timewait.
785 * To protect against this, we search for the cm_id after exiting
786 * timewait before notifying the user that we've exited timewait.
787 */
788 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700789 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700790 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
791 msecs_to_jiffies(wait_time));
792 cm_id_priv->timewait_info = NULL;
793}
794
795static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
796{
Sean Hefty85753292006-10-04 11:29:59 -0700797 unsigned long flags;
798
Hal Rosenstocka9770492005-07-27 11:45:40 -0700799 cm_id_priv->id.state = IB_CM_IDLE;
800 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700801 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700802 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700803 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700804 kfree(cm_id_priv->timewait_info);
805 cm_id_priv->timewait_info = NULL;
806 }
807}
808
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700809static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700810{
811 struct cm_id_private *cm_id_priv;
812 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700813
814 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
815retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700816 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700817 switch (cm_id->state) {
818 case IB_CM_LISTEN:
819 cm_id->state = IB_CM_IDLE;
Sean Hefty24be6e82007-06-18 11:09:36 -0700820 spin_unlock_irq(&cm_id_priv->lock);
821 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700822 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700823 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700824 break;
825 case IB_CM_SIDR_REQ_SENT:
826 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700827 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700828 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700829 break;
830 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700831 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700832 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
833 break;
834 case IB_CM_REQ_SENT:
Sean Hefty227eca82005-11-30 10:00:25 -0800835 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700836 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800837 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800838 &cm_id_priv->id.device->node_guid,
839 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800840 NULL, 0);
841 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700842 case IB_CM_REQ_RCVD:
843 if (err == -ENOMEM) {
844 /* Do not reject to allow future retries. */
845 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700846 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700847 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700848 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700849 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
850 NULL, 0, NULL, 0);
851 }
852 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700853 case IB_CM_MRA_REQ_RCVD:
854 case IB_CM_REP_SENT:
855 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700856 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700857 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700858 case IB_CM_MRA_REQ_SENT:
859 case IB_CM_REP_RCVD:
860 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700861 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800862 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
863 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700864 break;
865 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700866 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700867 ib_send_cm_dreq(cm_id, NULL, 0);
868 goto retest;
869 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700870 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700871 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700872 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700873 break;
874 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700875 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700876 ib_send_cm_drep(cm_id, NULL, 0);
877 break;
878 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700879 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700880 break;
881 }
882
883 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700884 cm_deref_id(cm_id_priv);
885 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700886 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
887 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -0700888 kfree(cm_id_priv->compare_data);
889 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700890 kfree(cm_id_priv);
891}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700892
893void ib_destroy_cm_id(struct ib_cm_id *cm_id)
894{
895 cm_destroy_id(cm_id, 0);
896}
Hal Rosenstocka9770492005-07-27 11:45:40 -0700897EXPORT_SYMBOL(ib_destroy_cm_id);
898
Sean Hefty6e61d042006-06-17 20:37:28 -0700899int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
900 struct ib_cm_compare_data *compare_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700901{
902 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
903 unsigned long flags;
904 int ret = 0;
905
Sean Hefty97f52eb2005-08-13 21:05:57 -0700906 service_mask = service_mask ? service_mask :
907 __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700908 service_id &= service_mask;
909 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
910 (service_id != IB_CM_ASSIGN_SERVICE_ID))
911 return -EINVAL;
912
913 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -0700914 if (cm_id->state != IB_CM_IDLE)
915 return -EINVAL;
916
917 if (compare_data) {
918 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
919 GFP_KERNEL);
920 if (!cm_id_priv->compare_data)
921 return -ENOMEM;
922 cm_mask_copy(cm_id_priv->compare_data->data,
923 compare_data->data, compare_data->mask);
924 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
925 IB_CM_COMPARE_SIZE);
926 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700927
928 cm_id->state = IB_CM_LISTEN;
929
930 spin_lock_irqsave(&cm.lock, flags);
931 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700932 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
933 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700934 } else {
935 cm_id->service_id = service_id;
936 cm_id->service_mask = service_mask;
937 }
938 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
939 spin_unlock_irqrestore(&cm.lock, flags);
940
941 if (cur_cm_id_priv) {
942 cm_id->state = IB_CM_IDLE;
Sean Hefty6e61d042006-06-17 20:37:28 -0700943 kfree(cm_id_priv->compare_data);
944 cm_id_priv->compare_data = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700945 ret = -EBUSY;
946 }
947 return ret;
948}
949EXPORT_SYMBOL(ib_cm_listen);
950
Sean Hefty97f52eb2005-08-13 21:05:57 -0700951static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
952 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700953{
954 u64 hi_tid, low_tid;
955
956 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700957 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
958 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700959 return cpu_to_be64(hi_tid | low_tid);
960}
961
962static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -0700963 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700964{
965 hdr->base_version = IB_MGMT_BASE_VERSION;
966 hdr->mgmt_class = IB_MGMT_CLASS_CM;
967 hdr->class_version = IB_CM_CLASS_VERSION;
968 hdr->method = IB_MGMT_METHOD_SEND;
969 hdr->attr_id = attr_id;
970 hdr->tid = tid;
971}
972
973static void cm_format_req(struct cm_req_msg *req_msg,
974 struct cm_id_private *cm_id_priv,
975 struct ib_cm_req_param *param)
976{
Sean Hefty3971c9f2007-12-10 15:53:25 -0800977 struct ib_sa_path_rec *pri_path = param->primary_path;
978 struct ib_sa_path_rec *alt_path = param->alternate_path;
979
Hal Rosenstocka9770492005-07-27 11:45:40 -0700980 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
981 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
982
983 req_msg->local_comm_id = cm_id_priv->id.local_id;
984 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -0800985 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700986 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
987 cm_req_set_resp_res(req_msg, param->responder_resources);
988 cm_req_set_init_depth(req_msg, param->initiator_depth);
989 cm_req_set_remote_resp_timeout(req_msg,
990 param->remote_cm_response_timeout);
991 cm_req_set_qp_type(req_msg, param->qp_type);
992 cm_req_set_flow_ctrl(req_msg, param->flow_control);
993 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
994 cm_req_set_local_resp_timeout(req_msg,
995 param->local_cm_response_timeout);
996 cm_req_set_retry_count(req_msg, param->retry_count);
997 req_msg->pkey = param->primary_path->pkey;
998 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
999 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1000 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1001 cm_req_set_srq(req_msg, param->srq);
1002
Sean Hefty3971c9f2007-12-10 15:53:25 -08001003 if (pri_path->hop_limit <= 1) {
1004 req_msg->primary_local_lid = pri_path->slid;
1005 req_msg->primary_remote_lid = pri_path->dlid;
1006 } else {
1007 /* Work-around until there's a way to obtain remote LID info */
1008 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1009 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1010 }
1011 req_msg->primary_local_gid = pri_path->sgid;
1012 req_msg->primary_remote_gid = pri_path->dgid;
1013 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1014 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1015 req_msg->primary_traffic_class = pri_path->traffic_class;
1016 req_msg->primary_hop_limit = pri_path->hop_limit;
1017 cm_req_set_primary_sl(req_msg, pri_path->sl);
1018 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001019 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001020 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001021 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001022
Sean Hefty3971c9f2007-12-10 15:53:25 -08001023 if (alt_path) {
1024 if (alt_path->hop_limit <= 1) {
1025 req_msg->alt_local_lid = alt_path->slid;
1026 req_msg->alt_remote_lid = alt_path->dlid;
1027 } else {
1028 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1029 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1030 }
1031 req_msg->alt_local_gid = alt_path->sgid;
1032 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001033 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001034 alt_path->flow_label);
1035 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1036 req_msg->alt_traffic_class = alt_path->traffic_class;
1037 req_msg->alt_hop_limit = alt_path->hop_limit;
1038 cm_req_set_alt_sl(req_msg, alt_path->sl);
1039 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001040 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001041 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001042 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001043 }
1044
1045 if (param->private_data && param->private_data_len)
1046 memcpy(req_msg->private_data, param->private_data,
1047 param->private_data_len);
1048}
1049
Arjan van de Ven858119e2006-01-14 13:20:43 -08001050static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001051{
1052 /* peer-to-peer not supported */
1053 if (param->peer_to_peer)
1054 return -EINVAL;
1055
1056 if (!param->primary_path)
1057 return -EINVAL;
1058
1059 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1060 return -EINVAL;
1061
1062 if (param->private_data &&
1063 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1064 return -EINVAL;
1065
1066 if (param->alternate_path &&
1067 (param->alternate_path->pkey != param->primary_path->pkey ||
1068 param->alternate_path->mtu != param->primary_path->mtu))
1069 return -EINVAL;
1070
1071 return 0;
1072}
1073
1074int ib_send_cm_req(struct ib_cm_id *cm_id,
1075 struct ib_cm_req_param *param)
1076{
1077 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001078 struct cm_req_msg *req_msg;
1079 unsigned long flags;
1080 int ret;
1081
1082 ret = cm_validate_req_param(param);
1083 if (ret)
1084 return ret;
1085
1086 /* Verify that we're not in timewait. */
1087 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1088 spin_lock_irqsave(&cm_id_priv->lock, flags);
1089 if (cm_id->state != IB_CM_IDLE) {
1090 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1095
1096 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1097 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001098 if (IS_ERR(cm_id_priv->timewait_info)) {
1099 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001100 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001101 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001102
1103 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1104 if (ret)
1105 goto error1;
1106 if (param->alternate_path) {
1107 ret = cm_init_av_by_path(param->alternate_path,
1108 &cm_id_priv->alt_av);
1109 if (ret)
1110 goto error1;
1111 }
1112 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001113 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001114 cm_id_priv->timeout_ms = cm_convert_to_ms(
1115 param->primary_path->packet_life_time) * 2 +
1116 cm_convert_to_ms(
1117 param->remote_cm_response_timeout);
1118 cm_id_priv->max_cm_retries = param->max_cm_retries;
1119 cm_id_priv->initiator_depth = param->initiator_depth;
1120 cm_id_priv->responder_resources = param->responder_resources;
1121 cm_id_priv->retry_count = param->retry_count;
1122 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001123 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001124 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001125
1126 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1127 if (ret)
1128 goto error1;
1129
1130 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1131 cm_format_req(req_msg, cm_id_priv, param);
1132 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001133 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001134 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1135
1136 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1137 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001138
1139 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001140 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001141 if (ret) {
1142 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1143 goto error2;
1144 }
1145 BUG_ON(cm_id->state != IB_CM_IDLE);
1146 cm_id->state = IB_CM_REQ_SENT;
1147 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1148 return 0;
1149
1150error2: cm_free_msg(cm_id_priv->msg);
1151error1: kfree(cm_id_priv->timewait_info);
1152out: return ret;
1153}
1154EXPORT_SYMBOL(ib_send_cm_req);
1155
1156static int cm_issue_rej(struct cm_port *port,
1157 struct ib_mad_recv_wc *mad_recv_wc,
1158 enum ib_cm_rej_reason reason,
1159 enum cm_msg_response msg_rejected,
1160 void *ari, u8 ari_length)
1161{
1162 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001163 struct cm_rej_msg *rej_msg, *rcv_msg;
1164 int ret;
1165
1166 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1167 if (ret)
1168 return ret;
1169
1170 /* We just need common CM header information. Cast to any message. */
1171 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1172 rej_msg = (struct cm_rej_msg *) msg->mad;
1173
1174 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1175 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1176 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1177 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001178 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001179
1180 if (ari && ari_length) {
1181 cm_rej_set_reject_info_len(rej_msg, ari_length);
1182 memcpy(rej_msg->ari, ari, ari_length);
1183 }
1184
Sean Hefty34816ad2005-10-25 10:51:39 -07001185 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001186 if (ret)
1187 cm_free_msg(msg);
1188
1189 return ret;
1190}
1191
Sean Hefty97f52eb2005-08-13 21:05:57 -07001192static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1193 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001194{
1195 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1196 ((local_ca_guid == remote_ca_guid) &&
1197 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1198}
1199
Arjan van de Ven858119e2006-01-14 13:20:43 -08001200static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001201 struct ib_sa_path_rec *primary_path,
1202 struct ib_sa_path_rec *alt_path)
1203{
1204 memset(primary_path, 0, sizeof *primary_path);
1205 primary_path->dgid = req_msg->primary_local_gid;
1206 primary_path->sgid = req_msg->primary_remote_gid;
1207 primary_path->dlid = req_msg->primary_local_lid;
1208 primary_path->slid = req_msg->primary_remote_lid;
1209 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1210 primary_path->hop_limit = req_msg->primary_hop_limit;
1211 primary_path->traffic_class = req_msg->primary_traffic_class;
1212 primary_path->reversible = 1;
1213 primary_path->pkey = req_msg->pkey;
1214 primary_path->sl = cm_req_get_primary_sl(req_msg);
1215 primary_path->mtu_selector = IB_SA_EQ;
1216 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1217 primary_path->rate_selector = IB_SA_EQ;
1218 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1219 primary_path->packet_life_time_selector = IB_SA_EQ;
1220 primary_path->packet_life_time =
1221 cm_req_get_primary_local_ack_timeout(req_msg);
1222 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1223
1224 if (req_msg->alt_local_lid) {
1225 memset(alt_path, 0, sizeof *alt_path);
1226 alt_path->dgid = req_msg->alt_local_gid;
1227 alt_path->sgid = req_msg->alt_remote_gid;
1228 alt_path->dlid = req_msg->alt_local_lid;
1229 alt_path->slid = req_msg->alt_remote_lid;
1230 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1231 alt_path->hop_limit = req_msg->alt_hop_limit;
1232 alt_path->traffic_class = req_msg->alt_traffic_class;
1233 alt_path->reversible = 1;
1234 alt_path->pkey = req_msg->pkey;
1235 alt_path->sl = cm_req_get_alt_sl(req_msg);
1236 alt_path->mtu_selector = IB_SA_EQ;
1237 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1238 alt_path->rate_selector = IB_SA_EQ;
1239 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1240 alt_path->packet_life_time_selector = IB_SA_EQ;
1241 alt_path->packet_life_time =
1242 cm_req_get_alt_local_ack_timeout(req_msg);
1243 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1244 }
1245}
1246
1247static void cm_format_req_event(struct cm_work *work,
1248 struct cm_id_private *cm_id_priv,
1249 struct ib_cm_id *listen_id)
1250{
1251 struct cm_req_msg *req_msg;
1252 struct ib_cm_req_event_param *param;
1253
1254 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1255 param = &work->cm_event.param.req_rcvd;
1256 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001257 param->port = cm_id_priv->av.port->port_num;
1258 param->primary_path = &work->path[0];
1259 if (req_msg->alt_local_lid)
1260 param->alternate_path = &work->path[1];
1261 else
1262 param->alternate_path = NULL;
1263 param->remote_ca_guid = req_msg->local_ca_guid;
1264 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1265 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1266 param->qp_type = cm_req_get_qp_type(req_msg);
1267 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1268 param->responder_resources = cm_req_get_init_depth(req_msg);
1269 param->initiator_depth = cm_req_get_resp_res(req_msg);
1270 param->local_cm_response_timeout =
1271 cm_req_get_remote_resp_timeout(req_msg);
1272 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1273 param->remote_cm_response_timeout =
1274 cm_req_get_local_resp_timeout(req_msg);
1275 param->retry_count = cm_req_get_retry_count(req_msg);
1276 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1277 param->srq = cm_req_get_srq(req_msg);
1278 work->cm_event.private_data = &req_msg->private_data;
1279}
1280
1281static void cm_process_work(struct cm_id_private *cm_id_priv,
1282 struct cm_work *work)
1283{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001284 int ret;
1285
1286 /* We will typically only have the current event to report. */
1287 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1288 cm_free_work(work);
1289
1290 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001291 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001292 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001293 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001294 BUG_ON(!work);
1295 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1296 &work->cm_event);
1297 cm_free_work(work);
1298 }
1299 cm_deref_id(cm_id_priv);
1300 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001301 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001302}
1303
1304static void cm_format_mra(struct cm_mra_msg *mra_msg,
1305 struct cm_id_private *cm_id_priv,
1306 enum cm_msg_response msg_mraed, u8 service_timeout,
1307 const void *private_data, u8 private_data_len)
1308{
1309 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1310 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1311 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1312 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1313 cm_mra_set_service_timeout(mra_msg, service_timeout);
1314
1315 if (private_data && private_data_len)
1316 memcpy(mra_msg->private_data, private_data, private_data_len);
1317}
1318
1319static void cm_format_rej(struct cm_rej_msg *rej_msg,
1320 struct cm_id_private *cm_id_priv,
1321 enum ib_cm_rej_reason reason,
1322 void *ari,
1323 u8 ari_length,
1324 const void *private_data,
1325 u8 private_data_len)
1326{
1327 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1328 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1329
1330 switch(cm_id_priv->id.state) {
1331 case IB_CM_REQ_RCVD:
1332 rej_msg->local_comm_id = 0;
1333 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1334 break;
1335 case IB_CM_MRA_REQ_SENT:
1336 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1337 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1338 break;
1339 case IB_CM_REP_RCVD:
1340 case IB_CM_MRA_REP_SENT:
1341 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1342 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1343 break;
1344 default:
1345 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1346 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1347 break;
1348 }
1349
Sean Hefty97f52eb2005-08-13 21:05:57 -07001350 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001351 if (ari && ari_length) {
1352 cm_rej_set_reject_info_len(rej_msg, ari_length);
1353 memcpy(rej_msg->ari, ari, ari_length);
1354 }
1355
1356 if (private_data && private_data_len)
1357 memcpy(rej_msg->private_data, private_data, private_data_len);
1358}
1359
1360static void cm_dup_req_handler(struct cm_work *work,
1361 struct cm_id_private *cm_id_priv)
1362{
1363 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001364 int ret;
1365
Sean Hefty9af57b72007-07-16 21:49:35 -07001366 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1367 counter[CM_REQ_COUNTER]);
1368
Hal Rosenstocka9770492005-07-27 11:45:40 -07001369 /* Quick state check to discard duplicate REQs. */
1370 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1371 return;
1372
1373 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1374 if (ret)
1375 return;
1376
Sean Hefty24be6e82007-06-18 11:09:36 -07001377 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001378 switch (cm_id_priv->id.state) {
1379 case IB_CM_MRA_REQ_SENT:
1380 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1381 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1382 cm_id_priv->private_data,
1383 cm_id_priv->private_data_len);
1384 break;
1385 case IB_CM_TIMEWAIT:
1386 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1387 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1388 break;
1389 default:
1390 goto unlock;
1391 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001392 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001393
Sean Hefty34816ad2005-10-25 10:51:39 -07001394 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001395 if (ret)
1396 goto free;
1397 return;
1398
Sean Hefty24be6e82007-06-18 11:09:36 -07001399unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001400free: cm_free_msg(msg);
1401}
1402
1403static struct cm_id_private * cm_match_req(struct cm_work *work,
1404 struct cm_id_private *cm_id_priv)
1405{
1406 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1407 struct cm_timewait_info *timewait_info;
1408 struct cm_req_msg *req_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001409
1410 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1411
Sean Heftyd998ccc2007-05-21 17:38:02 -07001412 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001413 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001414 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001415 if (timewait_info) {
1416 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1417 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001418 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001419 if (cur_cm_id_priv) {
1420 cm_dup_req_handler(work, cur_cm_id_priv);
1421 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001422 }
1423 return NULL;
1424 }
1425
1426 /* Check for stale connections. */
1427 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1428 if (timewait_info) {
1429 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001430 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001431 cm_issue_rej(work->port, work->mad_recv_wc,
1432 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1433 NULL, 0);
1434 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001435 }
1436
1437 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001438 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Sean Hefty6e61d042006-06-17 20:37:28 -07001439 req_msg->service_id,
1440 req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001441 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001442 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001443 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001444 cm_issue_rej(work->port, work->mad_recv_wc,
1445 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1446 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001447 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001448 }
1449 atomic_inc(&listen_cm_id_priv->refcount);
1450 atomic_inc(&cm_id_priv->refcount);
1451 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1452 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001453 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001454out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001455 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001456}
1457
Sean Hefty3971c9f2007-12-10 15:53:25 -08001458/*
1459 * Work-around for inter-subnet connections. If the LIDs are permissive,
1460 * we need to override the LID/SL data in the REQ with the LID information
1461 * in the work completion.
1462 */
1463static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1464{
1465 if (!cm_req_get_primary_subnet_local(req_msg)) {
1466 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1467 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1468 cm_req_set_primary_sl(req_msg, wc->sl);
1469 }
1470
1471 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1472 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1473 }
1474
1475 if (!cm_req_get_alt_subnet_local(req_msg)) {
1476 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1477 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1478 cm_req_set_alt_sl(req_msg, wc->sl);
1479 }
1480
1481 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1482 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1483 }
1484}
1485
Hal Rosenstocka9770492005-07-27 11:45:40 -07001486static int cm_req_handler(struct cm_work *work)
1487{
1488 struct ib_cm_id *cm_id;
1489 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1490 struct cm_req_msg *req_msg;
1491 int ret;
1492
1493 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1494
Sean Hefty07d357d2005-10-17 15:37:43 -07001495 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001496 if (IS_ERR(cm_id))
1497 return PTR_ERR(cm_id);
1498
1499 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1500 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1501 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001502 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001503 &cm_id_priv->av);
1504 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1505 id.local_id);
1506 if (IS_ERR(cm_id_priv->timewait_info)) {
1507 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001508 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001509 }
1510 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1511 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1512 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1513
1514 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1515 if (!listen_cm_id_priv) {
1516 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001517 kfree(cm_id_priv->timewait_info);
1518 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001519 }
1520
1521 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1522 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1523 cm_id_priv->id.service_id = req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001524 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001525
Sean Hefty3971c9f2007-12-10 15:53:25 -08001526 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001527 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1528 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
Sean Hefty76842402006-08-28 11:57:42 -07001529 if (ret) {
1530 ib_get_cached_gid(work->port->cm_dev->device,
1531 work->port->port_num, 0, &work->path[0].sgid);
1532 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1533 &work->path[0].sgid, sizeof work->path[0].sgid,
1534 NULL, 0);
1535 goto rejected;
1536 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001537 if (req_msg->alt_local_lid) {
1538 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
Sean Hefty76842402006-08-28 11:57:42 -07001539 if (ret) {
1540 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1541 &work->path[0].sgid,
1542 sizeof work->path[0].sgid, NULL, 0);
1543 goto rejected;
1544 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001545 }
1546 cm_id_priv->tid = req_msg->hdr.tid;
1547 cm_id_priv->timeout_ms = cm_convert_to_ms(
1548 cm_req_get_local_resp_timeout(req_msg));
1549 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1550 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1551 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1552 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1553 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001554 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001555 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001556 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1557 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001558 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001559
1560 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1561 cm_process_work(cm_id_priv, work);
1562 cm_deref_id(listen_cm_id_priv);
1563 return 0;
1564
Sean Hefty76842402006-08-28 11:57:42 -07001565rejected:
1566 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001567 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001568destroy:
1569 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001570 return ret;
1571}
1572
1573static void cm_format_rep(struct cm_rep_msg *rep_msg,
1574 struct cm_id_private *cm_id_priv,
1575 struct ib_cm_rep_param *param)
1576{
1577 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1578 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1579 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1580 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1581 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1582 rep_msg->resp_resources = param->responder_resources;
1583 rep_msg->initiator_depth = param->initiator_depth;
Sean Hefty1d846122007-06-18 11:09:37 -07001584 cm_rep_set_target_ack_delay(rep_msg,
1585 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001586 cm_rep_set_failover(rep_msg, param->failover_accepted);
1587 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1588 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1589 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftye971b8c2007-02-22 11:37:38 -08001590 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001591
1592 if (param->private_data && param->private_data_len)
1593 memcpy(rep_msg->private_data, param->private_data,
1594 param->private_data_len);
1595}
1596
1597int ib_send_cm_rep(struct ib_cm_id *cm_id,
1598 struct ib_cm_rep_param *param)
1599{
1600 struct cm_id_private *cm_id_priv;
1601 struct ib_mad_send_buf *msg;
1602 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001603 unsigned long flags;
1604 int ret;
1605
1606 if (param->private_data &&
1607 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1608 return -EINVAL;
1609
1610 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1611 spin_lock_irqsave(&cm_id_priv->lock, flags);
1612 if (cm_id->state != IB_CM_REQ_RCVD &&
1613 cm_id->state != IB_CM_MRA_REQ_SENT) {
1614 ret = -EINVAL;
1615 goto out;
1616 }
1617
1618 ret = cm_alloc_msg(cm_id_priv, &msg);
1619 if (ret)
1620 goto out;
1621
1622 rep_msg = (struct cm_rep_msg *) msg->mad;
1623 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001624 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001625 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1626
Sean Hefty34816ad2005-10-25 10:51:39 -07001627 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001628 if (ret) {
1629 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1630 cm_free_msg(msg);
1631 return ret;
1632 }
1633
1634 cm_id->state = IB_CM_REP_SENT;
1635 cm_id_priv->msg = msg;
1636 cm_id_priv->initiator_depth = param->initiator_depth;
1637 cm_id_priv->responder_resources = param->responder_resources;
1638 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1639 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1640
1641out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1642 return ret;
1643}
1644EXPORT_SYMBOL(ib_send_cm_rep);
1645
1646static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1647 struct cm_id_private *cm_id_priv,
1648 const void *private_data,
1649 u8 private_data_len)
1650{
1651 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1652 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1653 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1654
1655 if (private_data && private_data_len)
1656 memcpy(rtu_msg->private_data, private_data, private_data_len);
1657}
1658
1659int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1660 const void *private_data,
1661 u8 private_data_len)
1662{
1663 struct cm_id_private *cm_id_priv;
1664 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001665 unsigned long flags;
1666 void *data;
1667 int ret;
1668
1669 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1670 return -EINVAL;
1671
1672 data = cm_copy_private_data(private_data, private_data_len);
1673 if (IS_ERR(data))
1674 return PTR_ERR(data);
1675
1676 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1677 spin_lock_irqsave(&cm_id_priv->lock, flags);
1678 if (cm_id->state != IB_CM_REP_RCVD &&
1679 cm_id->state != IB_CM_MRA_REP_SENT) {
1680 ret = -EINVAL;
1681 goto error;
1682 }
1683
1684 ret = cm_alloc_msg(cm_id_priv, &msg);
1685 if (ret)
1686 goto error;
1687
1688 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1689 private_data, private_data_len);
1690
Sean Hefty34816ad2005-10-25 10:51:39 -07001691 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001692 if (ret) {
1693 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1694 cm_free_msg(msg);
1695 kfree(data);
1696 return ret;
1697 }
1698
1699 cm_id->state = IB_CM_ESTABLISHED;
1700 cm_set_private_data(cm_id_priv, data, private_data_len);
1701 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1702 return 0;
1703
1704error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1705 kfree(data);
1706 return ret;
1707}
1708EXPORT_SYMBOL(ib_send_cm_rtu);
1709
1710static void cm_format_rep_event(struct cm_work *work)
1711{
1712 struct cm_rep_msg *rep_msg;
1713 struct ib_cm_rep_event_param *param;
1714
1715 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1716 param = &work->cm_event.param.rep_rcvd;
1717 param->remote_ca_guid = rep_msg->local_ca_guid;
1718 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1719 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1720 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1721 param->responder_resources = rep_msg->initiator_depth;
1722 param->initiator_depth = rep_msg->resp_resources;
1723 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1724 param->failover_accepted = cm_rep_get_failover(rep_msg);
1725 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1726 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1727 param->srq = cm_rep_get_srq(rep_msg);
1728 work->cm_event.private_data = &rep_msg->private_data;
1729}
1730
1731static void cm_dup_rep_handler(struct cm_work *work)
1732{
1733 struct cm_id_private *cm_id_priv;
1734 struct cm_rep_msg *rep_msg;
1735 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001736 int ret;
1737
1738 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1739 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1740 rep_msg->local_comm_id);
1741 if (!cm_id_priv)
1742 return;
1743
Sean Hefty9af57b72007-07-16 21:49:35 -07001744 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1745 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001746 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1747 if (ret)
1748 goto deref;
1749
Sean Hefty24be6e82007-06-18 11:09:36 -07001750 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001751 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1752 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1753 cm_id_priv->private_data,
1754 cm_id_priv->private_data_len);
1755 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1756 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1757 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1758 cm_id_priv->private_data,
1759 cm_id_priv->private_data_len);
1760 else
1761 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07001762 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001763
Sean Hefty34816ad2005-10-25 10:51:39 -07001764 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001765 if (ret)
1766 goto free;
1767 goto deref;
1768
Sean Hefty24be6e82007-06-18 11:09:36 -07001769unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001770free: cm_free_msg(msg);
1771deref: cm_deref_id(cm_id_priv);
1772}
1773
1774static int cm_rep_handler(struct cm_work *work)
1775{
1776 struct cm_id_private *cm_id_priv;
1777 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001778 int ret;
1779
1780 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1781 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1782 if (!cm_id_priv) {
1783 cm_dup_rep_handler(work);
1784 return -EINVAL;
1785 }
1786
Hal Rosenstocka9770492005-07-27 11:45:40 -07001787 cm_format_rep_event(work);
1788
Sean Hefty24be6e82007-06-18 11:09:36 -07001789 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001790 switch (cm_id_priv->id.state) {
1791 case IB_CM_REQ_SENT:
1792 case IB_CM_MRA_REQ_RCVD:
1793 break;
1794 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07001795 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001796 ret = -EINVAL;
1797 goto error;
1798 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08001799
1800 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1801 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1802 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1803
1804 spin_lock(&cm.lock);
1805 /* Check for duplicate REP. */
1806 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1807 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001808 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001809 ret = -EINVAL;
1810 goto error;
1811 }
1812 /* Check for a stale connection. */
1813 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1814 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1815 &cm.remote_id_table);
1816 cm_id_priv->timewait_info->inserted_remote_id = 0;
1817 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001818 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001819 cm_issue_rej(work->port, work->mad_recv_wc,
1820 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1821 NULL, 0);
1822 ret = -EINVAL;
1823 goto error;
1824 }
1825 spin_unlock(&cm.lock);
1826
Hal Rosenstocka9770492005-07-27 11:45:40 -07001827 cm_id_priv->id.state = IB_CM_REP_RCVD;
1828 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1829 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1830 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1831 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1832 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1833 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07001834 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1835 cm_id_priv->av.timeout =
1836 cm_ack_timeout(cm_id_priv->target_ack_delay,
1837 cm_id_priv->av.timeout - 1);
1838 cm_id_priv->alt_av.timeout =
1839 cm_ack_timeout(cm_id_priv->target_ack_delay,
1840 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001841
1842 /* todo: handle peer_to_peer */
1843
Sean Hefty34816ad2005-10-25 10:51:39 -07001844 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001845 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1846 if (!ret)
1847 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001848 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001849
1850 if (ret)
1851 cm_process_work(cm_id_priv, work);
1852 else
1853 cm_deref_id(cm_id_priv);
1854 return 0;
1855
Sean Hefty87fd1a12006-03-02 16:50:37 -08001856error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001857 cm_deref_id(cm_id_priv);
1858 return ret;
1859}
1860
1861static int cm_establish_handler(struct cm_work *work)
1862{
1863 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001864 int ret;
1865
Sean Heftye1444b52006-11-28 14:57:13 -08001866 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07001867 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1868 if (!cm_id_priv)
1869 return -EINVAL;
1870
Sean Hefty24be6e82007-06-18 11:09:36 -07001871 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001872 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001873 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001874 goto out;
1875 }
1876
Sean Hefty34816ad2005-10-25 10:51:39 -07001877 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001878 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1879 if (!ret)
1880 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001881 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001882
1883 if (ret)
1884 cm_process_work(cm_id_priv, work);
1885 else
1886 cm_deref_id(cm_id_priv);
1887 return 0;
1888out:
1889 cm_deref_id(cm_id_priv);
1890 return -EINVAL;
1891}
1892
1893static int cm_rtu_handler(struct cm_work *work)
1894{
1895 struct cm_id_private *cm_id_priv;
1896 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001897 int ret;
1898
1899 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1900 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1901 rtu_msg->local_comm_id);
1902 if (!cm_id_priv)
1903 return -EINVAL;
1904
1905 work->cm_event.private_data = &rtu_msg->private_data;
1906
Sean Hefty24be6e82007-06-18 11:09:36 -07001907 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001908 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1909 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001910 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07001911 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1912 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001913 goto out;
1914 }
1915 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1916
Sean Hefty34816ad2005-10-25 10:51:39 -07001917 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001918 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1919 if (!ret)
1920 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001921 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001922
1923 if (ret)
1924 cm_process_work(cm_id_priv, work);
1925 else
1926 cm_deref_id(cm_id_priv);
1927 return 0;
1928out:
1929 cm_deref_id(cm_id_priv);
1930 return -EINVAL;
1931}
1932
1933static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1934 struct cm_id_private *cm_id_priv,
1935 const void *private_data,
1936 u8 private_data_len)
1937{
1938 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1939 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1940 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1941 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1942 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1943
1944 if (private_data && private_data_len)
1945 memcpy(dreq_msg->private_data, private_data, private_data_len);
1946}
1947
1948int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1949 const void *private_data,
1950 u8 private_data_len)
1951{
1952 struct cm_id_private *cm_id_priv;
1953 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001954 unsigned long flags;
1955 int ret;
1956
1957 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1958 return -EINVAL;
1959
1960 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1961 spin_lock_irqsave(&cm_id_priv->lock, flags);
1962 if (cm_id->state != IB_CM_ESTABLISHED) {
1963 ret = -EINVAL;
1964 goto out;
1965 }
1966
1967 ret = cm_alloc_msg(cm_id_priv, &msg);
1968 if (ret) {
1969 cm_enter_timewait(cm_id_priv);
1970 goto out;
1971 }
1972
1973 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1974 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07001975 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001976 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1977
Sean Hefty34816ad2005-10-25 10:51:39 -07001978 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001979 if (ret) {
1980 cm_enter_timewait(cm_id_priv);
1981 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1982 cm_free_msg(msg);
1983 return ret;
1984 }
1985
1986 cm_id->state = IB_CM_DREQ_SENT;
1987 cm_id_priv->msg = msg;
1988out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1989 return ret;
1990}
1991EXPORT_SYMBOL(ib_send_cm_dreq);
1992
1993static void cm_format_drep(struct cm_drep_msg *drep_msg,
1994 struct cm_id_private *cm_id_priv,
1995 const void *private_data,
1996 u8 private_data_len)
1997{
1998 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1999 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2000 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2001
2002 if (private_data && private_data_len)
2003 memcpy(drep_msg->private_data, private_data, private_data_len);
2004}
2005
2006int ib_send_cm_drep(struct ib_cm_id *cm_id,
2007 const void *private_data,
2008 u8 private_data_len)
2009{
2010 struct cm_id_private *cm_id_priv;
2011 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002012 unsigned long flags;
2013 void *data;
2014 int ret;
2015
2016 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2017 return -EINVAL;
2018
2019 data = cm_copy_private_data(private_data, private_data_len);
2020 if (IS_ERR(data))
2021 return PTR_ERR(data);
2022
2023 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2024 spin_lock_irqsave(&cm_id_priv->lock, flags);
2025 if (cm_id->state != IB_CM_DREQ_RCVD) {
2026 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2027 kfree(data);
2028 return -EINVAL;
2029 }
2030
2031 cm_set_private_data(cm_id_priv, data, private_data_len);
2032 cm_enter_timewait(cm_id_priv);
2033
2034 ret = cm_alloc_msg(cm_id_priv, &msg);
2035 if (ret)
2036 goto out;
2037
2038 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2039 private_data, private_data_len);
2040
Sean Hefty34816ad2005-10-25 10:51:39 -07002041 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002042 if (ret) {
2043 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2044 cm_free_msg(msg);
2045 return ret;
2046 }
2047
2048out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2049 return ret;
2050}
2051EXPORT_SYMBOL(ib_send_cm_drep);
2052
Sean Hefty82a9c162006-10-04 11:37:25 -07002053static int cm_issue_drep(struct cm_port *port,
2054 struct ib_mad_recv_wc *mad_recv_wc)
2055{
2056 struct ib_mad_send_buf *msg = NULL;
2057 struct cm_dreq_msg *dreq_msg;
2058 struct cm_drep_msg *drep_msg;
2059 int ret;
2060
2061 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2062 if (ret)
2063 return ret;
2064
2065 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2066 drep_msg = (struct cm_drep_msg *) msg->mad;
2067
2068 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2069 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2070 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2071
2072 ret = ib_post_send_mad(msg, NULL);
2073 if (ret)
2074 cm_free_msg(msg);
2075
2076 return ret;
2077}
2078
Hal Rosenstocka9770492005-07-27 11:45:40 -07002079static int cm_dreq_handler(struct cm_work *work)
2080{
2081 struct cm_id_private *cm_id_priv;
2082 struct cm_dreq_msg *dreq_msg;
2083 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002084 int ret;
2085
2086 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2087 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2088 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002089 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002090 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2091 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002092 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002093 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002094 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002095
2096 work->cm_event.private_data = &dreq_msg->private_data;
2097
Sean Hefty24be6e82007-06-18 11:09:36 -07002098 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002099 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2100 goto unlock;
2101
2102 switch (cm_id_priv->id.state) {
2103 case IB_CM_REP_SENT:
2104 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002105 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002106 break;
2107 case IB_CM_ESTABLISHED:
2108 case IB_CM_MRA_REP_RCVD:
2109 break;
2110 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002111 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2112 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002113 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2114 goto unlock;
2115
2116 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2117 cm_id_priv->private_data,
2118 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002119 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002120
Sean Hefty34816ad2005-10-25 10:51:39 -07002121 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002122 cm_free_msg(msg);
2123 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002124 case IB_CM_DREQ_RCVD:
2125 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2126 counter[CM_DREQ_COUNTER]);
2127 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002128 default:
2129 goto unlock;
2130 }
2131 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2132 cm_id_priv->tid = dreq_msg->hdr.tid;
2133 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2134 if (!ret)
2135 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002136 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002137
2138 if (ret)
2139 cm_process_work(cm_id_priv, work);
2140 else
2141 cm_deref_id(cm_id_priv);
2142 return 0;
2143
Sean Hefty24be6e82007-06-18 11:09:36 -07002144unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002145deref: cm_deref_id(cm_id_priv);
2146 return -EINVAL;
2147}
2148
2149static int cm_drep_handler(struct cm_work *work)
2150{
2151 struct cm_id_private *cm_id_priv;
2152 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002153 int ret;
2154
2155 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2156 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2157 drep_msg->local_comm_id);
2158 if (!cm_id_priv)
2159 return -EINVAL;
2160
2161 work->cm_event.private_data = &drep_msg->private_data;
2162
Sean Hefty24be6e82007-06-18 11:09:36 -07002163 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002164 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2165 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002166 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002167 goto out;
2168 }
2169 cm_enter_timewait(cm_id_priv);
2170
Sean Hefty34816ad2005-10-25 10:51:39 -07002171 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002172 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2173 if (!ret)
2174 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002175 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002176
2177 if (ret)
2178 cm_process_work(cm_id_priv, work);
2179 else
2180 cm_deref_id(cm_id_priv);
2181 return 0;
2182out:
2183 cm_deref_id(cm_id_priv);
2184 return -EINVAL;
2185}
2186
2187int ib_send_cm_rej(struct ib_cm_id *cm_id,
2188 enum ib_cm_rej_reason reason,
2189 void *ari,
2190 u8 ari_length,
2191 const void *private_data,
2192 u8 private_data_len)
2193{
2194 struct cm_id_private *cm_id_priv;
2195 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002196 unsigned long flags;
2197 int ret;
2198
2199 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2200 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2201 return -EINVAL;
2202
2203 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2204
2205 spin_lock_irqsave(&cm_id_priv->lock, flags);
2206 switch (cm_id->state) {
2207 case IB_CM_REQ_SENT:
2208 case IB_CM_MRA_REQ_RCVD:
2209 case IB_CM_REQ_RCVD:
2210 case IB_CM_MRA_REQ_SENT:
2211 case IB_CM_REP_RCVD:
2212 case IB_CM_MRA_REP_SENT:
2213 ret = cm_alloc_msg(cm_id_priv, &msg);
2214 if (!ret)
2215 cm_format_rej((struct cm_rej_msg *) msg->mad,
2216 cm_id_priv, reason, ari, ari_length,
2217 private_data, private_data_len);
2218
2219 cm_reset_to_idle(cm_id_priv);
2220 break;
2221 case IB_CM_REP_SENT:
2222 case IB_CM_MRA_REP_RCVD:
2223 ret = cm_alloc_msg(cm_id_priv, &msg);
2224 if (!ret)
2225 cm_format_rej((struct cm_rej_msg *) msg->mad,
2226 cm_id_priv, reason, ari, ari_length,
2227 private_data, private_data_len);
2228
2229 cm_enter_timewait(cm_id_priv);
2230 break;
2231 default:
2232 ret = -EINVAL;
2233 goto out;
2234 }
2235
2236 if (ret)
2237 goto out;
2238
Sean Hefty34816ad2005-10-25 10:51:39 -07002239 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002240 if (ret)
2241 cm_free_msg(msg);
2242
2243out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2244 return ret;
2245}
2246EXPORT_SYMBOL(ib_send_cm_rej);
2247
2248static void cm_format_rej_event(struct cm_work *work)
2249{
2250 struct cm_rej_msg *rej_msg;
2251 struct ib_cm_rej_event_param *param;
2252
2253 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2254 param = &work->cm_event.param.rej_rcvd;
2255 param->ari = rej_msg->ari;
2256 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002257 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002258 work->cm_event.private_data = &rej_msg->private_data;
2259}
2260
2261static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2262{
2263 struct cm_timewait_info *timewait_info;
2264 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002265 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002266
2267 remote_id = rej_msg->local_comm_id;
2268
Sean Hefty97f52eb2005-08-13 21:05:57 -07002269 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002270 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002271 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002272 remote_id);
2273 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002274 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002275 return NULL;
2276 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002277 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2278 (timewait_info->work.local_id ^
2279 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002280 if (cm_id_priv) {
2281 if (cm_id_priv->id.remote_id == remote_id)
2282 atomic_inc(&cm_id_priv->refcount);
2283 else
2284 cm_id_priv = NULL;
2285 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002286 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002287 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2288 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2289 else
2290 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2291
2292 return cm_id_priv;
2293}
2294
2295static int cm_rej_handler(struct cm_work *work)
2296{
2297 struct cm_id_private *cm_id_priv;
2298 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002299 int ret;
2300
2301 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2302 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2303 if (!cm_id_priv)
2304 return -EINVAL;
2305
2306 cm_format_rej_event(work);
2307
Sean Hefty24be6e82007-06-18 11:09:36 -07002308 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002309 switch (cm_id_priv->id.state) {
2310 case IB_CM_REQ_SENT:
2311 case IB_CM_MRA_REQ_RCVD:
2312 case IB_CM_REP_SENT:
2313 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002314 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002315 /* fall through */
2316 case IB_CM_REQ_RCVD:
2317 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002318 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002319 cm_enter_timewait(cm_id_priv);
2320 else
2321 cm_reset_to_idle(cm_id_priv);
2322 break;
2323 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002324 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002325 /* fall through */
2326 case IB_CM_REP_RCVD:
2327 case IB_CM_MRA_REP_SENT:
2328 case IB_CM_ESTABLISHED:
2329 cm_enter_timewait(cm_id_priv);
2330 break;
2331 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002332 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002333 ret = -EINVAL;
2334 goto out;
2335 }
2336
2337 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2338 if (!ret)
2339 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002340 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002341
2342 if (ret)
2343 cm_process_work(cm_id_priv, work);
2344 else
2345 cm_deref_id(cm_id_priv);
2346 return 0;
2347out:
2348 cm_deref_id(cm_id_priv);
2349 return -EINVAL;
2350}
2351
2352int ib_send_cm_mra(struct ib_cm_id *cm_id,
2353 u8 service_timeout,
2354 const void *private_data,
2355 u8 private_data_len)
2356{
2357 struct cm_id_private *cm_id_priv;
2358 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002359 enum ib_cm_state cm_state;
2360 enum ib_cm_lap_state lap_state;
2361 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002362 void *data;
2363 unsigned long flags;
2364 int ret;
2365
2366 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2367 return -EINVAL;
2368
2369 data = cm_copy_private_data(private_data, private_data_len);
2370 if (IS_ERR(data))
2371 return PTR_ERR(data);
2372
2373 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2374
2375 spin_lock_irqsave(&cm_id_priv->lock, flags);
2376 switch(cm_id_priv->id.state) {
2377 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002378 cm_state = IB_CM_MRA_REQ_SENT;
2379 lap_state = cm_id->lap_state;
2380 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002381 break;
2382 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002383 cm_state = IB_CM_MRA_REP_SENT;
2384 lap_state = cm_id->lap_state;
2385 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002386 break;
2387 case IB_CM_ESTABLISHED:
Sean Heftyde98b692007-08-01 13:49:53 -07002388 cm_state = cm_id->state;
2389 lap_state = IB_CM_MRA_LAP_SENT;
2390 msg_response = CM_MSG_RESPONSE_OTHER;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002391 break;
2392 default:
2393 ret = -EINVAL;
2394 goto error1;
2395 }
Sean Heftyde98b692007-08-01 13:49:53 -07002396
2397 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2398 ret = cm_alloc_msg(cm_id_priv, &msg);
2399 if (ret)
2400 goto error1;
2401
2402 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2403 msg_response, service_timeout,
2404 private_data, private_data_len);
2405 ret = ib_post_send_mad(msg, NULL);
2406 if (ret)
2407 goto error2;
2408 }
2409
2410 cm_id->state = cm_state;
2411 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002412 cm_id_priv->service_timeout = service_timeout;
2413 cm_set_private_data(cm_id_priv, data, private_data_len);
2414 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2415 return 0;
2416
2417error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2418 kfree(data);
2419 return ret;
2420
2421error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2422 kfree(data);
2423 cm_free_msg(msg);
2424 return ret;
2425}
2426EXPORT_SYMBOL(ib_send_cm_mra);
2427
2428static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2429{
2430 switch (cm_mra_get_msg_mraed(mra_msg)) {
2431 case CM_MSG_RESPONSE_REQ:
2432 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2433 case CM_MSG_RESPONSE_REP:
2434 case CM_MSG_RESPONSE_OTHER:
2435 return cm_acquire_id(mra_msg->remote_comm_id,
2436 mra_msg->local_comm_id);
2437 default:
2438 return NULL;
2439 }
2440}
2441
2442static int cm_mra_handler(struct cm_work *work)
2443{
2444 struct cm_id_private *cm_id_priv;
2445 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002446 int timeout, ret;
2447
2448 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2449 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2450 if (!cm_id_priv)
2451 return -EINVAL;
2452
2453 work->cm_event.private_data = &mra_msg->private_data;
2454 work->cm_event.param.mra_rcvd.service_timeout =
2455 cm_mra_get_service_timeout(mra_msg);
2456 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002457 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002458
Sean Hefty24be6e82007-06-18 11:09:36 -07002459 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002460 switch (cm_id_priv->id.state) {
2461 case IB_CM_REQ_SENT:
2462 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2463 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002464 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002465 goto out;
2466 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2467 break;
2468 case IB_CM_REP_SENT:
2469 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2470 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002471 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002472 goto out;
2473 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2474 break;
2475 case IB_CM_ESTABLISHED:
2476 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2477 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2478 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002479 cm_id_priv->msg, timeout)) {
2480 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2481 atomic_long_inc(&work->port->
2482 counter_group[CM_RECV_DUPLICATES].
2483 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002484 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002485 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002486 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2487 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002488 case IB_CM_MRA_REQ_RCVD:
2489 case IB_CM_MRA_REP_RCVD:
2490 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2491 counter[CM_MRA_COUNTER]);
2492 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002493 default:
2494 goto out;
2495 }
2496
2497 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2498 cm_id_priv->id.state;
2499 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2500 if (!ret)
2501 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002502 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002503
2504 if (ret)
2505 cm_process_work(cm_id_priv, work);
2506 else
2507 cm_deref_id(cm_id_priv);
2508 return 0;
2509out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002510 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002511 cm_deref_id(cm_id_priv);
2512 return -EINVAL;
2513}
2514
2515static void cm_format_lap(struct cm_lap_msg *lap_msg,
2516 struct cm_id_private *cm_id_priv,
2517 struct ib_sa_path_rec *alternate_path,
2518 const void *private_data,
2519 u8 private_data_len)
2520{
2521 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2522 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2523 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2524 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2525 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2526 /* todo: need remote CM response timeout */
2527 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2528 lap_msg->alt_local_lid = alternate_path->slid;
2529 lap_msg->alt_remote_lid = alternate_path->dlid;
2530 lap_msg->alt_local_gid = alternate_path->sgid;
2531 lap_msg->alt_remote_gid = alternate_path->dgid;
2532 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2533 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2534 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2535 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2536 cm_lap_set_sl(lap_msg, alternate_path->sl);
2537 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2538 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002539 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2540 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002541
2542 if (private_data && private_data_len)
2543 memcpy(lap_msg->private_data, private_data, private_data_len);
2544}
2545
2546int ib_send_cm_lap(struct ib_cm_id *cm_id,
2547 struct ib_sa_path_rec *alternate_path,
2548 const void *private_data,
2549 u8 private_data_len)
2550{
2551 struct cm_id_private *cm_id_priv;
2552 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002553 unsigned long flags;
2554 int ret;
2555
2556 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2557 return -EINVAL;
2558
2559 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2560 spin_lock_irqsave(&cm_id_priv->lock, flags);
2561 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002562 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2563 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002564 ret = -EINVAL;
2565 goto out;
2566 }
2567
Sean Heftye1444b52006-11-28 14:57:13 -08002568 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2569 if (ret)
2570 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002571 cm_id_priv->alt_av.timeout =
2572 cm_ack_timeout(cm_id_priv->target_ack_delay,
2573 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002574
Hal Rosenstocka9770492005-07-27 11:45:40 -07002575 ret = cm_alloc_msg(cm_id_priv, &msg);
2576 if (ret)
2577 goto out;
2578
2579 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2580 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002581 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002582 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2583
Sean Hefty34816ad2005-10-25 10:51:39 -07002584 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002585 if (ret) {
2586 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2587 cm_free_msg(msg);
2588 return ret;
2589 }
2590
2591 cm_id->lap_state = IB_CM_LAP_SENT;
2592 cm_id_priv->msg = msg;
2593
2594out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2595 return ret;
2596}
2597EXPORT_SYMBOL(ib_send_cm_lap);
2598
Sean Heftye1444b52006-11-28 14:57:13 -08002599static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2600 struct ib_sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002601 struct cm_lap_msg *lap_msg)
2602{
2603 memset(path, 0, sizeof *path);
2604 path->dgid = lap_msg->alt_local_gid;
2605 path->sgid = lap_msg->alt_remote_gid;
2606 path->dlid = lap_msg->alt_local_lid;
2607 path->slid = lap_msg->alt_remote_lid;
2608 path->flow_label = cm_lap_get_flow_label(lap_msg);
2609 path->hop_limit = lap_msg->alt_hop_limit;
2610 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2611 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002612 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002613 path->sl = cm_lap_get_sl(lap_msg);
2614 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002615 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002616 path->rate_selector = IB_SA_EQ;
2617 path->rate = cm_lap_get_packet_rate(lap_msg);
2618 path->packet_life_time_selector = IB_SA_EQ;
2619 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2620 path->packet_life_time -= (path->packet_life_time > 0);
2621}
2622
2623static int cm_lap_handler(struct cm_work *work)
2624{
2625 struct cm_id_private *cm_id_priv;
2626 struct cm_lap_msg *lap_msg;
2627 struct ib_cm_lap_event_param *param;
2628 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002629 int ret;
2630
2631 /* todo: verify LAP request and send reject APR if invalid. */
2632 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2633 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2634 lap_msg->local_comm_id);
2635 if (!cm_id_priv)
2636 return -EINVAL;
2637
2638 param = &work->cm_event.param.lap_rcvd;
2639 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002640 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002641 work->cm_event.private_data = &lap_msg->private_data;
2642
Sean Hefty24be6e82007-06-18 11:09:36 -07002643 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002644 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2645 goto unlock;
2646
2647 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002648 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002649 case IB_CM_LAP_IDLE:
2650 break;
2651 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002652 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2653 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002654 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2655 goto unlock;
2656
2657 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2658 CM_MSG_RESPONSE_OTHER,
2659 cm_id_priv->service_timeout,
2660 cm_id_priv->private_data,
2661 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002662 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002663
Sean Hefty34816ad2005-10-25 10:51:39 -07002664 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002665 cm_free_msg(msg);
2666 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002667 case IB_CM_LAP_RCVD:
2668 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2669 counter[CM_LAP_COUNTER]);
2670 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002671 default:
2672 goto unlock;
2673 }
2674
2675 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2676 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08002677 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2678 work->mad_recv_wc->recv_buf.grh,
2679 &cm_id_priv->av);
2680 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002681 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2682 if (!ret)
2683 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002684 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002685
2686 if (ret)
2687 cm_process_work(cm_id_priv, work);
2688 else
2689 cm_deref_id(cm_id_priv);
2690 return 0;
2691
Sean Hefty24be6e82007-06-18 11:09:36 -07002692unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002693deref: cm_deref_id(cm_id_priv);
2694 return -EINVAL;
2695}
2696
2697static void cm_format_apr(struct cm_apr_msg *apr_msg,
2698 struct cm_id_private *cm_id_priv,
2699 enum ib_cm_apr_status status,
2700 void *info,
2701 u8 info_length,
2702 const void *private_data,
2703 u8 private_data_len)
2704{
2705 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2706 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2707 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2708 apr_msg->ap_status = (u8) status;
2709
2710 if (info && info_length) {
2711 apr_msg->info_length = info_length;
2712 memcpy(apr_msg->info, info, info_length);
2713 }
2714
2715 if (private_data && private_data_len)
2716 memcpy(apr_msg->private_data, private_data, private_data_len);
2717}
2718
2719int ib_send_cm_apr(struct ib_cm_id *cm_id,
2720 enum ib_cm_apr_status status,
2721 void *info,
2722 u8 info_length,
2723 const void *private_data,
2724 u8 private_data_len)
2725{
2726 struct cm_id_private *cm_id_priv;
2727 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002728 unsigned long flags;
2729 int ret;
2730
2731 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2732 (info && info_length > IB_CM_APR_INFO_LENGTH))
2733 return -EINVAL;
2734
2735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2736 spin_lock_irqsave(&cm_id_priv->lock, flags);
2737 if (cm_id->state != IB_CM_ESTABLISHED ||
2738 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2739 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2740 ret = -EINVAL;
2741 goto out;
2742 }
2743
2744 ret = cm_alloc_msg(cm_id_priv, &msg);
2745 if (ret)
2746 goto out;
2747
2748 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2749 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002750 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002751 if (ret) {
2752 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2753 cm_free_msg(msg);
2754 return ret;
2755 }
2756
2757 cm_id->lap_state = IB_CM_LAP_IDLE;
2758out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2759 return ret;
2760}
2761EXPORT_SYMBOL(ib_send_cm_apr);
2762
2763static int cm_apr_handler(struct cm_work *work)
2764{
2765 struct cm_id_private *cm_id_priv;
2766 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002767 int ret;
2768
2769 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2770 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2771 apr_msg->local_comm_id);
2772 if (!cm_id_priv)
2773 return -EINVAL; /* Unmatched reply. */
2774
2775 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2776 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2777 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2778 work->cm_event.private_data = &apr_msg->private_data;
2779
Sean Hefty24be6e82007-06-18 11:09:36 -07002780 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002781 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2782 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2783 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002784 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002785 goto out;
2786 }
2787 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002788 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002789 cm_id_priv->msg = NULL;
2790
2791 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2792 if (!ret)
2793 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002794 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002795
2796 if (ret)
2797 cm_process_work(cm_id_priv, work);
2798 else
2799 cm_deref_id(cm_id_priv);
2800 return 0;
2801out:
2802 cm_deref_id(cm_id_priv);
2803 return -EINVAL;
2804}
2805
2806static int cm_timewait_handler(struct cm_work *work)
2807{
2808 struct cm_timewait_info *timewait_info;
2809 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002810 int ret;
2811
2812 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07002813 spin_lock_irq(&cm.lock);
2814 list_del(&timewait_info->list);
2815 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002816
2817 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2818 timewait_info->work.remote_id);
2819 if (!cm_id_priv)
2820 return -EINVAL;
2821
Sean Hefty85753292006-10-04 11:29:59 -07002822 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002823 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2824 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07002825 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002826 goto out;
2827 }
2828 cm_id_priv->id.state = IB_CM_IDLE;
2829 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2830 if (!ret)
2831 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07002832 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002833
2834 if (ret)
2835 cm_process_work(cm_id_priv, work);
2836 else
2837 cm_deref_id(cm_id_priv);
2838 return 0;
2839out:
2840 cm_deref_id(cm_id_priv);
2841 return -EINVAL;
2842}
2843
2844static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2845 struct cm_id_private *cm_id_priv,
2846 struct ib_cm_sidr_req_param *param)
2847{
2848 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2849 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2850 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Sean Hefty75af9082006-06-17 20:37:39 -07002851 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002852 sidr_req_msg->service_id = param->service_id;
2853
2854 if (param->private_data && param->private_data_len)
2855 memcpy(sidr_req_msg->private_data, param->private_data,
2856 param->private_data_len);
2857}
2858
2859int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2860 struct ib_cm_sidr_req_param *param)
2861{
2862 struct cm_id_private *cm_id_priv;
2863 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002864 unsigned long flags;
2865 int ret;
2866
2867 if (!param->path || (param->private_data &&
2868 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2869 return -EINVAL;
2870
2871 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2872 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2873 if (ret)
2874 goto out;
2875
2876 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002877 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002878 cm_id_priv->timeout_ms = param->timeout_ms;
2879 cm_id_priv->max_cm_retries = param->max_cm_retries;
2880 ret = cm_alloc_msg(cm_id_priv, &msg);
2881 if (ret)
2882 goto out;
2883
2884 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2885 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07002886 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002887 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2888
2889 spin_lock_irqsave(&cm_id_priv->lock, flags);
2890 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07002891 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002892 else
2893 ret = -EINVAL;
2894
2895 if (ret) {
2896 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2897 cm_free_msg(msg);
2898 goto out;
2899 }
2900 cm_id->state = IB_CM_SIDR_REQ_SENT;
2901 cm_id_priv->msg = msg;
2902 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2903out:
2904 return ret;
2905}
2906EXPORT_SYMBOL(ib_send_cm_sidr_req);
2907
2908static void cm_format_sidr_req_event(struct cm_work *work,
2909 struct ib_cm_id *listen_id)
2910{
2911 struct cm_sidr_req_msg *sidr_req_msg;
2912 struct ib_cm_sidr_req_event_param *param;
2913
2914 sidr_req_msg = (struct cm_sidr_req_msg *)
2915 work->mad_recv_wc->recv_buf.mad;
2916 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002917 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002918 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002919 param->port = work->port->port_num;
2920 work->cm_event.private_data = &sidr_req_msg->private_data;
2921}
2922
2923static int cm_sidr_req_handler(struct cm_work *work)
2924{
2925 struct ib_cm_id *cm_id;
2926 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2927 struct cm_sidr_req_msg *sidr_req_msg;
2928 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002929
Sean Hefty07d357d2005-10-17 15:37:43 -07002930 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002931 if (IS_ERR(cm_id))
2932 return PTR_ERR(cm_id);
2933 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2934
2935 /* Record SGID/SLID and request ID for lookup. */
2936 sidr_req_msg = (struct cm_sidr_req_msg *)
2937 work->mad_recv_wc->recv_buf.mad;
2938 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002939 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002940 cm_id_priv->av.dgid.global.interface_id = 0;
2941 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07002942 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002943 &cm_id_priv->av);
2944 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002945 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2946 atomic_inc(&cm_id_priv->work_count);
2947
Sean Hefty24be6e82007-06-18 11:09:36 -07002948 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002949 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2950 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002951 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07002952 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2953 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002954 goto out; /* Duplicate message. */
2955 }
Sean Hefty29c27312007-07-02 14:36:19 -07002956 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07002957 cur_cm_id_priv = cm_find_listen(cm_id->device,
Sean Hefty6e61d042006-06-17 20:37:28 -07002958 sidr_req_msg->service_id,
2959 sidr_req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002960 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002961 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07002962 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002963 goto out; /* No match. */
2964 }
2965 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07002966 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002967
2968 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2969 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2970 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002971 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002972
2973 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2974 cm_process_work(cm_id_priv, work);
2975 cm_deref_id(cur_cm_id_priv);
2976 return 0;
2977out:
2978 ib_destroy_cm_id(&cm_id_priv->id);
2979 return -EINVAL;
2980}
2981
2982static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2983 struct cm_id_private *cm_id_priv,
2984 struct ib_cm_sidr_rep_param *param)
2985{
2986 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2987 cm_id_priv->tid);
2988 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2989 sidr_rep_msg->status = param->status;
2990 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2991 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2992 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2993
2994 if (param->info && param->info_length)
2995 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2996
2997 if (param->private_data && param->private_data_len)
2998 memcpy(sidr_rep_msg->private_data, param->private_data,
2999 param->private_data_len);
3000}
3001
3002int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3003 struct ib_cm_sidr_rep_param *param)
3004{
3005 struct cm_id_private *cm_id_priv;
3006 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003007 unsigned long flags;
3008 int ret;
3009
3010 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3011 (param->private_data &&
3012 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3013 return -EINVAL;
3014
3015 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3016 spin_lock_irqsave(&cm_id_priv->lock, flags);
3017 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3018 ret = -EINVAL;
3019 goto error;
3020 }
3021
3022 ret = cm_alloc_msg(cm_id_priv, &msg);
3023 if (ret)
3024 goto error;
3025
3026 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3027 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003028 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003029 if (ret) {
3030 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3031 cm_free_msg(msg);
3032 return ret;
3033 }
3034 cm_id->state = IB_CM_IDLE;
3035 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3036
3037 spin_lock_irqsave(&cm.lock, flags);
3038 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3039 spin_unlock_irqrestore(&cm.lock, flags);
3040 return 0;
3041
3042error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3043 return ret;
3044}
3045EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3046
3047static void cm_format_sidr_rep_event(struct cm_work *work)
3048{
3049 struct cm_sidr_rep_msg *sidr_rep_msg;
3050 struct ib_cm_sidr_rep_event_param *param;
3051
3052 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3053 work->mad_recv_wc->recv_buf.mad;
3054 param = &work->cm_event.param.sidr_rep_rcvd;
3055 param->status = sidr_rep_msg->status;
3056 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3057 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3058 param->info = &sidr_rep_msg->info;
3059 param->info_len = sidr_rep_msg->info_length;
3060 work->cm_event.private_data = &sidr_rep_msg->private_data;
3061}
3062
3063static int cm_sidr_rep_handler(struct cm_work *work)
3064{
3065 struct cm_sidr_rep_msg *sidr_rep_msg;
3066 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003067
3068 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3069 work->mad_recv_wc->recv_buf.mad;
3070 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3071 if (!cm_id_priv)
3072 return -EINVAL; /* Unmatched reply. */
3073
Sean Hefty24be6e82007-06-18 11:09:36 -07003074 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003075 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003076 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003077 goto out;
3078 }
3079 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003080 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003081 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003082
3083 cm_format_sidr_rep_event(work);
3084 cm_process_work(cm_id_priv, work);
3085 return 0;
3086out:
3087 cm_deref_id(cm_id_priv);
3088 return -EINVAL;
3089}
3090
3091static void cm_process_send_error(struct ib_mad_send_buf *msg,
3092 enum ib_wc_status wc_status)
3093{
3094 struct cm_id_private *cm_id_priv;
3095 struct ib_cm_event cm_event;
3096 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003097 int ret;
3098
3099 memset(&cm_event, 0, sizeof cm_event);
3100 cm_id_priv = msg->context[0];
3101
3102 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003103 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003104 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3105 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3106 goto discard;
3107
3108 switch (state) {
3109 case IB_CM_REQ_SENT:
3110 case IB_CM_MRA_REQ_RCVD:
3111 cm_reset_to_idle(cm_id_priv);
3112 cm_event.event = IB_CM_REQ_ERROR;
3113 break;
3114 case IB_CM_REP_SENT:
3115 case IB_CM_MRA_REP_RCVD:
3116 cm_reset_to_idle(cm_id_priv);
3117 cm_event.event = IB_CM_REP_ERROR;
3118 break;
3119 case IB_CM_DREQ_SENT:
3120 cm_enter_timewait(cm_id_priv);
3121 cm_event.event = IB_CM_DREQ_ERROR;
3122 break;
3123 case IB_CM_SIDR_REQ_SENT:
3124 cm_id_priv->id.state = IB_CM_IDLE;
3125 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3126 break;
3127 default:
3128 goto discard;
3129 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003130 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003131 cm_event.param.send_status = wc_status;
3132
3133 /* No other events can occur on the cm_id at this point. */
3134 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3135 cm_free_msg(msg);
3136 if (ret)
3137 ib_destroy_cm_id(&cm_id_priv->id);
3138 return;
3139discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003140 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003141 cm_free_msg(msg);
3142}
3143
3144static void cm_send_handler(struct ib_mad_agent *mad_agent,
3145 struct ib_mad_send_wc *mad_send_wc)
3146{
Sean Hefty34816ad2005-10-25 10:51:39 -07003147 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003148 struct cm_port *port;
3149 u16 attr_index;
3150
3151 port = mad_agent->context;
3152 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3153 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3154
3155 /*
3156 * If the send was in response to a received message (context[0] is not
3157 * set to a cm_id), and is not a REJ, then it is a send that was
3158 * manually retried.
3159 */
3160 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3161 msg->retries = 1;
3162
3163 atomic_long_add(1 + msg->retries,
3164 &port->counter_group[CM_XMIT].counter[attr_index]);
3165 if (msg->retries)
3166 atomic_long_add(msg->retries,
3167 &port->counter_group[CM_XMIT_RETRIES].
3168 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003169
3170 switch (mad_send_wc->status) {
3171 case IB_WC_SUCCESS:
3172 case IB_WC_WR_FLUSH_ERR:
3173 cm_free_msg(msg);
3174 break;
3175 default:
3176 if (msg->context[0] && msg->context[1])
3177 cm_process_send_error(msg, mad_send_wc->status);
3178 else
3179 cm_free_msg(msg);
3180 break;
3181 }
3182}
3183
David Howellsc4028952006-11-22 14:57:56 +00003184static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003185{
David Howellsc4028952006-11-22 14:57:56 +00003186 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003187 int ret;
3188
3189 switch (work->cm_event.event) {
3190 case IB_CM_REQ_RECEIVED:
3191 ret = cm_req_handler(work);
3192 break;
3193 case IB_CM_MRA_RECEIVED:
3194 ret = cm_mra_handler(work);
3195 break;
3196 case IB_CM_REJ_RECEIVED:
3197 ret = cm_rej_handler(work);
3198 break;
3199 case IB_CM_REP_RECEIVED:
3200 ret = cm_rep_handler(work);
3201 break;
3202 case IB_CM_RTU_RECEIVED:
3203 ret = cm_rtu_handler(work);
3204 break;
3205 case IB_CM_USER_ESTABLISHED:
3206 ret = cm_establish_handler(work);
3207 break;
3208 case IB_CM_DREQ_RECEIVED:
3209 ret = cm_dreq_handler(work);
3210 break;
3211 case IB_CM_DREP_RECEIVED:
3212 ret = cm_drep_handler(work);
3213 break;
3214 case IB_CM_SIDR_REQ_RECEIVED:
3215 ret = cm_sidr_req_handler(work);
3216 break;
3217 case IB_CM_SIDR_REP_RECEIVED:
3218 ret = cm_sidr_rep_handler(work);
3219 break;
3220 case IB_CM_LAP_RECEIVED:
3221 ret = cm_lap_handler(work);
3222 break;
3223 case IB_CM_APR_RECEIVED:
3224 ret = cm_apr_handler(work);
3225 break;
3226 case IB_CM_TIMEWAIT_EXIT:
3227 ret = cm_timewait_handler(work);
3228 break;
3229 default:
3230 ret = -EINVAL;
3231 break;
3232 }
3233 if (ret)
3234 cm_free_work(work);
3235}
3236
Sean Heftye1444b52006-11-28 14:57:13 -08003237static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003238{
3239 struct cm_id_private *cm_id_priv;
3240 struct cm_work *work;
3241 unsigned long flags;
3242 int ret = 0;
3243
3244 work = kmalloc(sizeof *work, GFP_ATOMIC);
3245 if (!work)
3246 return -ENOMEM;
3247
3248 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3249 spin_lock_irqsave(&cm_id_priv->lock, flags);
3250 switch (cm_id->state)
3251 {
3252 case IB_CM_REP_SENT:
3253 case IB_CM_MRA_REP_RCVD:
3254 cm_id->state = IB_CM_ESTABLISHED;
3255 break;
3256 case IB_CM_ESTABLISHED:
3257 ret = -EISCONN;
3258 break;
3259 default:
3260 ret = -EINVAL;
3261 break;
3262 }
3263 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3264
3265 if (ret) {
3266 kfree(work);
3267 goto out;
3268 }
3269
3270 /*
3271 * The CM worker thread may try to destroy the cm_id before it
3272 * can execute this work item. To prevent potential deadlock,
3273 * we need to find the cm_id once we're in the context of the
3274 * worker thread, rather than holding a reference on it.
3275 */
David Howellsc4028952006-11-22 14:57:56 +00003276 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003277 work->local_id = cm_id->local_id;
3278 work->remote_id = cm_id->remote_id;
3279 work->mad_recv_wc = NULL;
3280 work->cm_event.event = IB_CM_USER_ESTABLISHED;
David Howellsc4028952006-11-22 14:57:56 +00003281 queue_delayed_work(cm.wq, &work->work, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003282out:
3283 return ret;
3284}
Sean Heftye1444b52006-11-28 14:57:13 -08003285
3286static int cm_migrate(struct ib_cm_id *cm_id)
3287{
3288 struct cm_id_private *cm_id_priv;
3289 unsigned long flags;
3290 int ret = 0;
3291
3292 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3293 spin_lock_irqsave(&cm_id_priv->lock, flags);
3294 if (cm_id->state == IB_CM_ESTABLISHED &&
3295 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3296 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3297 cm_id->lap_state = IB_CM_LAP_IDLE;
3298 cm_id_priv->av = cm_id_priv->alt_av;
3299 } else
3300 ret = -EINVAL;
3301 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3302
3303 return ret;
3304}
3305
3306int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3307{
3308 int ret;
3309
3310 switch (event) {
3311 case IB_EVENT_COMM_EST:
3312 ret = cm_establish(cm_id);
3313 break;
3314 case IB_EVENT_PATH_MIG:
3315 ret = cm_migrate(cm_id);
3316 break;
3317 default:
3318 ret = -EINVAL;
3319 }
3320 return ret;
3321}
3322EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003323
3324static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3325 struct ib_mad_recv_wc *mad_recv_wc)
3326{
Sean Hefty9af57b72007-07-16 21:49:35 -07003327 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003328 struct cm_work *work;
3329 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003330 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003331 int paths = 0;
3332
3333 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3334 case CM_REQ_ATTR_ID:
3335 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3336 alt_local_lid != 0);
3337 event = IB_CM_REQ_RECEIVED;
3338 break;
3339 case CM_MRA_ATTR_ID:
3340 event = IB_CM_MRA_RECEIVED;
3341 break;
3342 case CM_REJ_ATTR_ID:
3343 event = IB_CM_REJ_RECEIVED;
3344 break;
3345 case CM_REP_ATTR_ID:
3346 event = IB_CM_REP_RECEIVED;
3347 break;
3348 case CM_RTU_ATTR_ID:
3349 event = IB_CM_RTU_RECEIVED;
3350 break;
3351 case CM_DREQ_ATTR_ID:
3352 event = IB_CM_DREQ_RECEIVED;
3353 break;
3354 case CM_DREP_ATTR_ID:
3355 event = IB_CM_DREP_RECEIVED;
3356 break;
3357 case CM_SIDR_REQ_ATTR_ID:
3358 event = IB_CM_SIDR_REQ_RECEIVED;
3359 break;
3360 case CM_SIDR_REP_ATTR_ID:
3361 event = IB_CM_SIDR_REP_RECEIVED;
3362 break;
3363 case CM_LAP_ATTR_ID:
3364 paths = 1;
3365 event = IB_CM_LAP_RECEIVED;
3366 break;
3367 case CM_APR_ATTR_ID:
3368 event = IB_CM_APR_RECEIVED;
3369 break;
3370 default:
3371 ib_free_recv_mad(mad_recv_wc);
3372 return;
3373 }
3374
Sean Hefty9af57b72007-07-16 21:49:35 -07003375 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3376 atomic_long_inc(&port->counter_group[CM_RECV].
3377 counter[attr_id - CM_ATTR_ID_OFFSET]);
3378
Hal Rosenstocka9770492005-07-27 11:45:40 -07003379 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3380 GFP_KERNEL);
3381 if (!work) {
3382 ib_free_recv_mad(mad_recv_wc);
3383 return;
3384 }
3385
David Howellsc4028952006-11-22 14:57:56 +00003386 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003387 work->cm_event.event = event;
3388 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003389 work->port = port;
David Howellsc4028952006-11-22 14:57:56 +00003390 queue_delayed_work(cm.wq, &work->work, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003391}
3392
3393static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3394 struct ib_qp_attr *qp_attr,
3395 int *qp_attr_mask)
3396{
3397 unsigned long flags;
3398 int ret;
3399
3400 spin_lock_irqsave(&cm_id_priv->lock, flags);
3401 switch (cm_id_priv->id.state) {
3402 case IB_CM_REQ_SENT:
3403 case IB_CM_MRA_REQ_RCVD:
3404 case IB_CM_REQ_RCVD:
3405 case IB_CM_MRA_REQ_SENT:
3406 case IB_CM_REP_RCVD:
3407 case IB_CM_MRA_REP_SENT:
3408 case IB_CM_REP_SENT:
3409 case IB_CM_MRA_REP_RCVD:
3410 case IB_CM_ESTABLISHED:
3411 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3412 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353e2006-10-24 13:35:27 -07003413 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003414 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003415 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3416 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003417 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3418 qp_attr->port_num = cm_id_priv->av.port->port_num;
3419 ret = 0;
3420 break;
3421 default:
3422 ret = -EINVAL;
3423 break;
3424 }
3425 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3426 return ret;
3427}
3428
3429static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3430 struct ib_qp_attr *qp_attr,
3431 int *qp_attr_mask)
3432{
3433 unsigned long flags;
3434 int ret;
3435
3436 spin_lock_irqsave(&cm_id_priv->lock, flags);
3437 switch (cm_id_priv->id.state) {
3438 case IB_CM_REQ_RCVD:
3439 case IB_CM_MRA_REQ_SENT:
3440 case IB_CM_REP_RCVD:
3441 case IB_CM_MRA_REP_SENT:
3442 case IB_CM_REP_SENT:
3443 case IB_CM_MRA_REP_RCVD:
3444 case IB_CM_ESTABLISHED:
3445 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003446 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003447 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3448 qp_attr->path_mtu = cm_id_priv->path_mtu;
3449 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3450 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyae7971a2005-10-24 12:33:56 -07003451 if (cm_id_priv->qp_type == IB_QPT_RC) {
3452 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3453 IB_QP_MIN_RNR_TIMER;
3454 qp_attr->max_dest_rd_atomic =
3455 cm_id_priv->responder_resources;
3456 qp_attr->min_rnr_timer = 0;
3457 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003458 if (cm_id_priv->alt_av.ah_attr.dlid) {
3459 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003460 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003461 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003462 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003463 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3464 }
3465 ret = 0;
3466 break;
3467 default:
3468 ret = -EINVAL;
3469 break;
3470 }
3471 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3472 return ret;
3473}
3474
3475static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3476 struct ib_qp_attr *qp_attr,
3477 int *qp_attr_mask)
3478{
3479 unsigned long flags;
3480 int ret;
3481
3482 spin_lock_irqsave(&cm_id_priv->lock, flags);
3483 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003484 /* Allow transition to RTS before sending REP */
3485 case IB_CM_REQ_RCVD:
3486 case IB_CM_MRA_REQ_SENT:
3487
Hal Rosenstocka9770492005-07-27 11:45:40 -07003488 case IB_CM_REP_RCVD:
3489 case IB_CM_MRA_REP_SENT:
3490 case IB_CM_REP_SENT:
3491 case IB_CM_MRA_REP_RCVD:
3492 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003493 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3494 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3495 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3496 if (cm_id_priv->qp_type == IB_QPT_RC) {
3497 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3498 IB_QP_RNR_RETRY |
3499 IB_QP_MAX_QP_RD_ATOMIC;
Sean Hefty1d846122007-06-18 11:09:37 -07003500 qp_attr->timeout = cm_id_priv->av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003501 qp_attr->retry_cnt = cm_id_priv->retry_count;
3502 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3503 qp_attr->max_rd_atomic =
3504 cm_id_priv->initiator_depth;
3505 }
3506 if (cm_id_priv->alt_av.ah_attr.dlid) {
3507 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3508 qp_attr->path_mig_state = IB_MIG_REARM;
3509 }
3510 } else {
3511 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3512 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3513 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003514 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003515 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003516 qp_attr->path_mig_state = IB_MIG_REARM;
3517 }
3518 ret = 0;
3519 break;
3520 default:
3521 ret = -EINVAL;
3522 break;
3523 }
3524 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3525 return ret;
3526}
3527
3528int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3529 struct ib_qp_attr *qp_attr,
3530 int *qp_attr_mask)
3531{
3532 struct cm_id_private *cm_id_priv;
3533 int ret;
3534
3535 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3536 switch (qp_attr->qp_state) {
3537 case IB_QPS_INIT:
3538 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3539 break;
3540 case IB_QPS_RTR:
3541 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3542 break;
3543 case IB_QPS_RTS:
3544 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3545 break;
3546 default:
3547 ret = -EINVAL;
3548 break;
3549 }
3550 return ret;
3551}
3552EXPORT_SYMBOL(ib_cm_init_qp_attr);
3553
Roland Dreier454a01e2007-07-17 18:37:43 -07003554static void cm_get_ack_delay(struct cm_device *cm_dev)
Sean Hefty1d846122007-06-18 11:09:37 -07003555{
3556 struct ib_device_attr attr;
3557
3558 if (ib_query_device(cm_dev->device, &attr))
3559 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3560 else
3561 cm_dev->ack_delay = attr.local_ca_ack_delay;
3562}
3563
Sean Hefty9af57b72007-07-16 21:49:35 -07003564static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3565 char *buf)
3566{
3567 struct cm_counter_group *group;
3568 struct cm_counter_attribute *cm_attr;
3569
3570 group = container_of(obj, struct cm_counter_group, obj);
3571 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3572
3573 return sprintf(buf, "%ld\n",
3574 atomic_long_read(&group->counter[cm_attr->index]));
3575}
3576
3577static struct sysfs_ops cm_counter_ops = {
3578 .show = cm_show_counter
3579};
3580
3581static struct kobj_type cm_counter_obj_type = {
3582 .sysfs_ops = &cm_counter_ops,
3583 .default_attrs = cm_counter_default_attrs
3584};
3585
3586static void cm_release_port_obj(struct kobject *obj)
3587{
3588 struct cm_port *cm_port;
3589
Sean Hefty9af57b72007-07-16 21:49:35 -07003590 cm_port = container_of(obj, struct cm_port, port_obj);
3591 kfree(cm_port);
3592}
3593
3594static struct kobj_type cm_port_obj_type = {
3595 .release = cm_release_port_obj
3596};
3597
3598static void cm_release_dev_obj(struct kobject *obj)
3599{
3600 struct cm_device *cm_dev;
3601
Sean Hefty9af57b72007-07-16 21:49:35 -07003602 cm_dev = container_of(obj, struct cm_device, dev_obj);
3603 kfree(cm_dev);
3604}
3605
3606static struct kobj_type cm_dev_obj_type = {
3607 .release = cm_release_dev_obj
3608};
3609
3610struct class cm_class = {
3611 .name = "infiniband_cm",
3612};
3613EXPORT_SYMBOL(cm_class);
3614
Sean Hefty9af57b72007-07-16 21:49:35 -07003615static int cm_create_port_fs(struct cm_port *port)
3616{
3617 int i, ret;
3618
3619 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003620 &port->cm_dev->dev_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003621 "%d", port->port_num);
3622 if (ret) {
3623 kfree(port);
3624 return ret;
3625 }
3626
3627 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3628 ret = kobject_init_and_add(&port->counter_group[i].obj,
3629 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003630 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003631 "%s", counter_group_names[i]);
3632 if (ret)
3633 goto error;
3634 }
3635
3636 return 0;
3637
3638error:
3639 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003640 kobject_put(&port->counter_group[i].obj);
3641 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003642 return ret;
3643
3644}
3645
3646static void cm_remove_port_fs(struct cm_port *port)
3647{
3648 int i;
3649
3650 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003651 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003652
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003653 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003654}
3655
Hal Rosenstocka9770492005-07-27 11:45:40 -07003656static void cm_add_one(struct ib_device *device)
3657{
3658 struct cm_device *cm_dev;
3659 struct cm_port *port;
3660 struct ib_mad_reg_req reg_req = {
3661 .mgmt_class = IB_MGMT_CLASS_CM,
3662 .mgmt_class_version = IB_CM_CLASS_VERSION
3663 };
3664 struct ib_port_modify port_modify = {
3665 .set_port_cap_mask = IB_PORT_CM_SUP
3666 };
3667 unsigned long flags;
3668 int ret;
3669 u8 i;
3670
Tom Tucker07ebafb2006-08-03 16:02:42 -05003671 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3672 return;
3673
Sean Hefty9af57b72007-07-16 21:49:35 -07003674 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Hal Rosenstocka9770492005-07-27 11:45:40 -07003675 device->phys_port_cnt, GFP_KERNEL);
3676 if (!cm_dev)
3677 return;
3678
3679 cm_dev->device = device;
Sean Hefty1d846122007-06-18 11:09:37 -07003680 cm_get_ack_delay(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003681
Sean Hefty9af57b72007-07-16 21:49:35 -07003682 ret = kobject_init_and_add(&cm_dev->dev_obj, &cm_dev_obj_type,
3683 &cm_class.subsys.kobj, "%s", device->name);
3684 if (ret) {
3685 kfree(cm_dev);
3686 return;
3687 }
3688
Hal Rosenstocka9770492005-07-27 11:45:40 -07003689 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3690 for (i = 1; i <= device->phys_port_cnt; i++) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003691 port = kzalloc(sizeof *port, GFP_KERNEL);
3692 if (!port)
3693 goto error1;
3694
3695 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003696 port->cm_dev = cm_dev;
3697 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07003698
3699 ret = cm_create_port_fs(port);
3700 if (ret)
3701 goto error1;
3702
Hal Rosenstocka9770492005-07-27 11:45:40 -07003703 port->mad_agent = ib_register_mad_agent(device, i,
3704 IB_QPT_GSI,
3705 &reg_req,
3706 0,
3707 cm_send_handler,
3708 cm_recv_handler,
3709 port);
3710 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07003711 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003712
3713 ret = ib_modify_port(device, i, 0, &port_modify);
3714 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003715 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003716 }
3717 ib_set_client_data(device, &cm_client, cm_dev);
3718
3719 write_lock_irqsave(&cm.device_lock, flags);
3720 list_add_tail(&cm_dev->list, &cm.device_list);
3721 write_unlock_irqrestore(&cm.device_lock, flags);
3722 return;
3723
Sean Hefty9af57b72007-07-16 21:49:35 -07003724error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08003725 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003726error2:
3727 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08003728error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003729 port_modify.set_port_cap_mask = 0;
3730 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3731 while (--i) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003732 port = cm_dev->port[i-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -07003733 ib_modify_port(device, port->port_num, 0, &port_modify);
3734 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003735 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003736 }
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003737 kobject_put(&cm_dev->dev_obj);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003738}
3739
3740static void cm_remove_one(struct ib_device *device)
3741{
3742 struct cm_device *cm_dev;
3743 struct cm_port *port;
3744 struct ib_port_modify port_modify = {
3745 .clr_port_cap_mask = IB_PORT_CM_SUP
3746 };
3747 unsigned long flags;
3748 int i;
3749
3750 cm_dev = ib_get_client_data(device, &cm_client);
3751 if (!cm_dev)
3752 return;
3753
3754 write_lock_irqsave(&cm.device_lock, flags);
3755 list_del(&cm_dev->list);
3756 write_unlock_irqrestore(&cm.device_lock, flags);
3757
3758 for (i = 1; i <= device->phys_port_cnt; i++) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003759 port = cm_dev->port[i-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -07003760 ib_modify_port(device, port->port_num, 0, &port_modify);
3761 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty84ba2842008-02-22 10:40:45 -08003762 flush_workqueue(cm.wq);
Sean Hefty9af57b72007-07-16 21:49:35 -07003763 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003764 }
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003765 kobject_put(&cm_dev->dev_obj);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003766}
3767
3768static int __init ib_cm_init(void)
3769{
3770 int ret;
3771
3772 memset(&cm, 0, sizeof cm);
3773 INIT_LIST_HEAD(&cm.device_list);
3774 rwlock_init(&cm.device_lock);
3775 spin_lock_init(&cm.lock);
3776 cm.listen_service_table = RB_ROOT;
3777 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3778 cm.remote_id_table = RB_ROOT;
3779 cm.remote_qp_table = RB_ROOT;
3780 cm.remote_sidr_table = RB_ROOT;
3781 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07003782 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003783 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
Sean Hefty85753292006-10-04 11:29:59 -07003784 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003785
Sean Hefty9af57b72007-07-16 21:49:35 -07003786 ret = class_register(&cm_class);
3787 if (ret)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003788 return -ENOMEM;
3789
Sean Hefty9af57b72007-07-16 21:49:35 -07003790 cm.wq = create_workqueue("ib_cm");
3791 if (!cm.wq) {
3792 ret = -ENOMEM;
3793 goto error1;
3794 }
3795
Hal Rosenstocka9770492005-07-27 11:45:40 -07003796 ret = ib_register_client(&cm_client);
3797 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003798 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003799
3800 return 0;
Sean Hefty9af57b72007-07-16 21:49:35 -07003801error2:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003802 destroy_workqueue(cm.wq);
Sean Hefty9af57b72007-07-16 21:49:35 -07003803error1:
3804 class_unregister(&cm_class);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003805 return ret;
3806}
3807
3808static void __exit ib_cm_cleanup(void)
3809{
Sean Hefty85753292006-10-04 11:29:59 -07003810 struct cm_timewait_info *timewait_info, *tmp;
3811
3812 spin_lock_irq(&cm.lock);
3813 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3814 cancel_delayed_work(&timewait_info->work.work);
3815 spin_unlock_irq(&cm.lock);
3816
Sean Hefty84ba2842008-02-22 10:40:45 -08003817 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003818 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07003819
3820 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3821 list_del(&timewait_info->list);
3822 kfree(timewait_info);
3823 }
3824
Sean Hefty9af57b72007-07-16 21:49:35 -07003825 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07003826 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003827}
3828
3829module_init(ib_cm_init);
3830module_exit(ib_cm_cleanup);
3831