blob: f1e82a92e61ec09fb879a9b00ec41b5e35b192f4 [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.
Hal Rosenstocka9770492005-07-27 11:45:40 -070034 */
Sean Hefty1b52fa982006-05-12 14:57:52 -070035
36#include <linux/completion.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070037#include <linux/dma-mapping.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070038#include <linux/device.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070039#include <linux/err.h>
40#include <linux/idr.h>
41#include <linux/interrupt.h>
Sean Heftyf06d2652006-08-28 15:15:18 -070042#include <linux/random.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070043#include <linux/rbtree.h>
44#include <linux/spinlock.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070045#include <linux/sysfs.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070046#include <linux/workqueue.h>
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +030047#include <linux/kdev_t.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070048
Roland Dreiera4d61e82005-08-25 13:40:04 -070049#include <rdma/ib_cache.h>
50#include <rdma/ib_cm.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070051#include "cm_msgs.h"
52
53MODULE_AUTHOR("Sean Hefty");
54MODULE_DESCRIPTION("InfiniBand CM");
55MODULE_LICENSE("Dual BSD/GPL");
56
57static void cm_add_one(struct ib_device *device);
58static void cm_remove_one(struct ib_device *device);
59
60static struct ib_client cm_client = {
61 .name = "cm",
62 .add = cm_add_one,
63 .remove = cm_remove_one
64};
65
66static struct ib_cm {
67 spinlock_t lock;
68 struct list_head device_list;
69 rwlock_t device_lock;
70 struct rb_root listen_service_table;
71 u64 listen_service_id;
72 /* struct rb_root peer_service_table; todo: fix peer to peer */
73 struct rb_root remote_qp_table;
74 struct rb_root remote_id_table;
75 struct rb_root remote_sidr_table;
76 struct idr local_id_table;
Sean Heftyf06d2652006-08-28 15:15:18 -070077 __be32 random_id_operand;
Sean Hefty85753292006-10-04 11:29:59 -070078 struct list_head timewait_list;
Hal Rosenstocka9770492005-07-27 11:45:40 -070079 struct workqueue_struct *wq;
80} cm;
81
Sean Hefty9af57b72007-07-16 21:49:35 -070082/* Counter indexes ordered by attribute ID */
83enum {
84 CM_REQ_COUNTER,
85 CM_MRA_COUNTER,
86 CM_REJ_COUNTER,
87 CM_REP_COUNTER,
88 CM_RTU_COUNTER,
89 CM_DREQ_COUNTER,
90 CM_DREP_COUNTER,
91 CM_SIDR_REQ_COUNTER,
92 CM_SIDR_REP_COUNTER,
93 CM_LAP_COUNTER,
94 CM_APR_COUNTER,
95 CM_ATTR_COUNT,
96 CM_ATTR_ID_OFFSET = 0x0010,
97};
98
99enum {
100 CM_XMIT,
101 CM_XMIT_RETRIES,
102 CM_RECV,
103 CM_RECV_DUPLICATES,
104 CM_COUNTER_GROUPS
105};
106
107static char const counter_group_names[CM_COUNTER_GROUPS]
108 [sizeof("cm_rx_duplicates")] = {
109 "cm_tx_msgs", "cm_tx_retries",
110 "cm_rx_msgs", "cm_rx_duplicates"
111};
112
113struct cm_counter_group {
114 struct kobject obj;
115 atomic_long_t counter[CM_ATTR_COUNT];
116};
117
118struct cm_counter_attribute {
119 struct attribute attr;
120 int index;
121};
122
123#define CM_COUNTER_ATTR(_name, _index) \
124struct cm_counter_attribute cm_##_name##_counter_attr = { \
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700125 .attr = { .name = __stringify(_name), .mode = 0444 }, \
Sean Hefty9af57b72007-07-16 21:49:35 -0700126 .index = _index \
127}
128
129static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
130static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
131static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
132static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
133static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
134static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
135static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
136static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
137static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
138static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
139static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
140
141static struct attribute *cm_counter_default_attrs[] = {
142 &cm_req_counter_attr.attr,
143 &cm_mra_counter_attr.attr,
144 &cm_rej_counter_attr.attr,
145 &cm_rep_counter_attr.attr,
146 &cm_rtu_counter_attr.attr,
147 &cm_dreq_counter_attr.attr,
148 &cm_drep_counter_attr.attr,
149 &cm_sidr_req_counter_attr.attr,
150 &cm_sidr_rep_counter_attr.attr,
151 &cm_lap_counter_attr.attr,
152 &cm_apr_counter_attr.attr,
153 NULL
154};
155
Hal Rosenstocka9770492005-07-27 11:45:40 -0700156struct cm_port {
157 struct cm_device *cm_dev;
158 struct ib_mad_agent *mad_agent;
Sean Hefty9af57b72007-07-16 21:49:35 -0700159 struct kobject port_obj;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700160 u8 port_num;
Sean Hefty9af57b72007-07-16 21:49:35 -0700161 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700162};
163
164struct cm_device {
165 struct list_head list;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300166 struct ib_device *ib_device;
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +0300167 struct device *device;
Sean Hefty1d846122007-06-18 11:09:37 -0700168 u8 ack_delay;
Sean Hefty9af57b72007-07-16 21:49:35 -0700169 struct cm_port *port[0];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700170};
171
172struct cm_av {
173 struct cm_port *port;
174 union ib_gid dgid;
175 struct ib_ah_attr ah_attr;
176 u16 pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -0700177 u8 timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700178};
179
180struct cm_work {
David Howellsc4028952006-11-22 14:57:56 +0000181 struct delayed_work work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700182 struct list_head list;
183 struct cm_port *port;
184 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700185 __be32 local_id; /* Established / timewait */
186 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700187 struct ib_cm_event cm_event;
188 struct ib_sa_path_rec path[0];
189};
190
191struct cm_timewait_info {
192 struct cm_work work; /* Must be first. */
Sean Hefty85753292006-10-04 11:29:59 -0700193 struct list_head list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700194 struct rb_node remote_qp_node;
195 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700196 __be64 remote_ca_guid;
197 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700198 u8 inserted_remote_qp;
199 u8 inserted_remote_id;
200};
201
202struct cm_id_private {
203 struct ib_cm_id id;
204
205 struct rb_node service_node;
206 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800207 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700208 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700209 atomic_t refcount;
210
211 struct ib_mad_send_buf *msg;
212 struct cm_timewait_info *timewait_info;
213 /* todo: use alternate port on send failure */
214 struct cm_av av;
215 struct cm_av alt_av;
Sean Hefty6e61d042006-06-17 20:37:28 -0700216 struct ib_cm_compare_data *compare_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700217
218 void *private_data;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700219 __be64 tid;
220 __be32 local_qpn;
221 __be32 remote_qpn;
Sean Heftyae7971a2005-10-24 12:33:56 -0700222 enum ib_qp_type qp_type;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700223 __be32 sq_psn;
224 __be32 rq_psn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700225 int timeout_ms;
226 enum ib_mtu path_mtu;
Sean Heftye1444b52006-11-28 14:57:13 -0800227 __be16 pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700228 u8 private_data_len;
229 u8 max_cm_retries;
230 u8 peer_to_peer;
231 u8 responder_resources;
232 u8 initiator_depth;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700233 u8 retry_count;
234 u8 rnr_retry_count;
235 u8 service_timeout;
Sean Hefty1d846122007-06-18 11:09:37 -0700236 u8 target_ack_delay;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700237
238 struct list_head work_list;
239 atomic_t work_count;
240};
241
David Howellsc4028952006-11-22 14:57:56 +0000242static void cm_work_handler(struct work_struct *work);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700243
244static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
245{
246 if (atomic_dec_and_test(&cm_id_priv->refcount))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700247 complete(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700248}
249
250static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
251 struct ib_mad_send_buf **msg)
252{
253 struct ib_mad_agent *mad_agent;
254 struct ib_mad_send_buf *m;
255 struct ib_ah *ah;
256
257 mad_agent = cm_id_priv->av.port->mad_agent;
258 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
259 if (IS_ERR(ah))
260 return PTR_ERR(ah);
261
Roland Dreier3cd96562006-09-22 15:22:46 -0700262 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
John Kingman354ba392005-09-09 18:23:32 -0700263 cm_id_priv->av.pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700264 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700265 GFP_ATOMIC);
266 if (IS_ERR(m)) {
267 ib_destroy_ah(ah);
268 return PTR_ERR(m);
269 }
270
271 /* Timeout set by caller if response is expected. */
Sean Hefty34816ad2005-10-25 10:51:39 -0700272 m->ah = ah;
273 m->retries = cm_id_priv->max_cm_retries;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700274
275 atomic_inc(&cm_id_priv->refcount);
276 m->context[0] = cm_id_priv;
277 *msg = m;
278 return 0;
279}
280
281static int cm_alloc_response_msg(struct cm_port *port,
282 struct ib_mad_recv_wc *mad_recv_wc,
283 struct ib_mad_send_buf **msg)
284{
285 struct ib_mad_send_buf *m;
286 struct ib_ah *ah;
287
288 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
289 mad_recv_wc->recv_buf.grh, port->port_num);
290 if (IS_ERR(ah))
291 return PTR_ERR(ah);
292
293 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700294 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700295 GFP_ATOMIC);
296 if (IS_ERR(m)) {
297 ib_destroy_ah(ah);
298 return PTR_ERR(m);
299 }
Sean Hefty34816ad2005-10-25 10:51:39 -0700300 m->ah = ah;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700301 *msg = m;
302 return 0;
303}
304
305static void cm_free_msg(struct ib_mad_send_buf *msg)
306{
Sean Hefty34816ad2005-10-25 10:51:39 -0700307 ib_destroy_ah(msg->ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700308 if (msg->context[0])
309 cm_deref_id(msg->context[0]);
310 ib_free_send_mad(msg);
311}
312
313static void * cm_copy_private_data(const void *private_data,
314 u8 private_data_len)
315{
316 void *data;
317
318 if (!private_data || !private_data_len)
319 return NULL;
320
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200321 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700322 if (!data)
323 return ERR_PTR(-ENOMEM);
324
Hal Rosenstocka9770492005-07-27 11:45:40 -0700325 return data;
326}
327
328static void cm_set_private_data(struct cm_id_private *cm_id_priv,
329 void *private_data, u8 private_data_len)
330{
331 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
332 kfree(cm_id_priv->private_data);
333
334 cm_id_priv->private_data = private_data;
335 cm_id_priv->private_data_len = private_data_len;
336}
337
Sean Heftyca222c62006-06-17 20:37:40 -0700338static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
339 struct ib_grh *grh, struct cm_av *av)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700340{
341 av->port = port;
342 av->pkey_index = wc->pkey_index;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300343 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
Sean Heftyca222c62006-06-17 20:37:40 -0700344 grh, &av->ah_attr);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700345}
346
347static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
348{
349 struct cm_device *cm_dev;
350 struct cm_port *port = NULL;
351 unsigned long flags;
352 int ret;
353 u8 p;
354
355 read_lock_irqsave(&cm.device_lock, flags);
356 list_for_each_entry(cm_dev, &cm.device_list, list) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300357 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700358 &p, NULL)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700359 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700360 break;
361 }
362 }
363 read_unlock_irqrestore(&cm.device_lock, flags);
364
365 if (!port)
366 return -EINVAL;
367
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300368 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700369 be16_to_cpu(path->pkey), &av->pkey_index);
370 if (ret)
371 return ret;
372
373 av->port = port;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300374 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
Sean Heftyca222c62006-06-17 20:37:40 -0700375 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700376 av->timeout = path->packet_life_time + 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700377 return 0;
378}
379
380static int cm_alloc_id(struct cm_id_private *cm_id_priv)
381{
382 unsigned long flags;
Sean Heftyf06d2652006-08-28 15:15:18 -0700383 int ret, id;
Sean Heftyde1bb1a2005-11-30 10:01:13 -0800384 static int next_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700385
386 do {
387 spin_lock_irqsave(&cm.lock, flags);
Sean Heftyf06d2652006-08-28 15:15:18 -0700388 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
Michael S. Tsirkin9f81036c2007-05-21 19:06:54 +0300389 next_id, &id);
390 if (!ret)
391 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700392 spin_unlock_irqrestore(&cm.lock, flags);
393 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
Sean Heftyf06d2652006-08-28 15:15:18 -0700394
Al Viro1b90c132008-03-29 03:10:28 +0000395 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700396 return ret;
397}
398
Sean Hefty97f52eb2005-08-13 21:05:57 -0700399static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700400{
Sean Hefty24be6e82007-06-18 11:09:36 -0700401 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700402 idr_remove(&cm.local_id_table,
403 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700404 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700405}
406
Sean Hefty97f52eb2005-08-13 21:05:57 -0700407static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700408{
409 struct cm_id_private *cm_id_priv;
410
Sean Heftyf06d2652006-08-28 15:15:18 -0700411 cm_id_priv = idr_find(&cm.local_id_table,
412 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700413 if (cm_id_priv) {
414 if (cm_id_priv->id.remote_id == remote_id)
415 atomic_inc(&cm_id_priv->refcount);
416 else
417 cm_id_priv = NULL;
418 }
419
420 return cm_id_priv;
421}
422
Sean Hefty97f52eb2005-08-13 21:05:57 -0700423static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700424{
425 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700426
Sean Hefty24be6e82007-06-18 11:09:36 -0700427 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700428 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700429 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700430
431 return cm_id_priv;
432}
433
Sean Hefty6e61d042006-06-17 20:37:28 -0700434static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
435{
436 int i;
437
438 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
439 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
440 ((unsigned long *) mask)[i];
441}
442
443static int cm_compare_data(struct ib_cm_compare_data *src_data,
444 struct ib_cm_compare_data *dst_data)
445{
446 u8 src[IB_CM_COMPARE_SIZE];
447 u8 dst[IB_CM_COMPARE_SIZE];
448
449 if (!src_data || !dst_data)
450 return 0;
451
452 cm_mask_copy(src, src_data->data, dst_data->mask);
453 cm_mask_copy(dst, dst_data->data, src_data->mask);
454 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
455}
456
457static int cm_compare_private_data(u8 *private_data,
458 struct ib_cm_compare_data *dst_data)
459{
460 u8 src[IB_CM_COMPARE_SIZE];
461
462 if (!dst_data)
463 return 0;
464
465 cm_mask_copy(src, private_data, dst_data->mask);
466 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
467}
468
Roland Dreiera88f4882008-04-16 21:01:07 -0700469/*
470 * Trivial helpers to strip endian annotation and compare; the
471 * endianness doesn't actually matter since we just need a stable
472 * order for the RB tree.
473 */
474static int be32_lt(__be32 a, __be32 b)
475{
476 return (__force u32) a < (__force u32) b;
477}
478
479static int be32_gt(__be32 a, __be32 b)
480{
481 return (__force u32) a > (__force u32) b;
482}
483
484static int be64_lt(__be64 a, __be64 b)
485{
486 return (__force u64) a < (__force u64) b;
487}
488
489static int be64_gt(__be64 a, __be64 b)
490{
491 return (__force u64) a > (__force u64) b;
492}
493
Hal Rosenstocka9770492005-07-27 11:45:40 -0700494static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
495{
496 struct rb_node **link = &cm.listen_service_table.rb_node;
497 struct rb_node *parent = NULL;
498 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700499 __be64 service_id = cm_id_priv->id.service_id;
500 __be64 service_mask = cm_id_priv->id.service_mask;
Sean Hefty6e61d042006-06-17 20:37:28 -0700501 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700502
503 while (*link) {
504 parent = *link;
505 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
506 service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700507 data_cmp = cm_compare_data(cm_id_priv->compare_data,
508 cur_cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700509 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700510 (service_mask & cur_cm_id_priv->id.service_id) &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700511 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
512 !data_cmp)
Sean Hefty07d357d2005-10-17 15:37:43 -0700513 return cur_cm_id_priv;
514
515 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
516 link = &(*link)->rb_left;
517 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
518 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700519 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700520 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700521 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700522 link = &(*link)->rb_right;
523 else if (data_cmp < 0)
524 link = &(*link)->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700525 else
526 link = &(*link)->rb_right;
527 }
528 rb_link_node(&cm_id_priv->service_node, parent, link);
529 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
530 return NULL;
531}
532
Sean Hefty07d357d2005-10-17 15:37:43 -0700533static struct cm_id_private * cm_find_listen(struct ib_device *device,
Sean Hefty6e61d042006-06-17 20:37:28 -0700534 __be64 service_id,
535 u8 *private_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700536{
537 struct rb_node *node = cm.listen_service_table.rb_node;
538 struct cm_id_private *cm_id_priv;
Sean Hefty6e61d042006-06-17 20:37:28 -0700539 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700540
541 while (node) {
542 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700543 data_cmp = cm_compare_private_data(private_data,
544 cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700545 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700546 cm_id_priv->id.service_id &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700547 (cm_id_priv->id.device == device) && !data_cmp)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700548 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700549
550 if (device < cm_id_priv->id.device)
551 node = node->rb_left;
552 else if (device > cm_id_priv->id.device)
553 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700554 else if (be64_lt(service_id, cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700555 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700556 else if (be64_gt(service_id, cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700557 node = node->rb_right;
558 else if (data_cmp < 0)
559 node = node->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700560 else
561 node = node->rb_right;
562 }
563 return NULL;
564}
565
566static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
567 *timewait_info)
568{
569 struct rb_node **link = &cm.remote_id_table.rb_node;
570 struct rb_node *parent = NULL;
571 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700572 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
573 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700574
575 while (*link) {
576 parent = *link;
577 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
578 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700579 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700580 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700581 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700582 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700583 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700584 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700585 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700586 link = &(*link)->rb_right;
587 else
588 return cur_timewait_info;
589 }
590 timewait_info->inserted_remote_id = 1;
591 rb_link_node(&timewait_info->remote_id_node, parent, link);
592 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
593 return NULL;
594}
595
Sean Hefty97f52eb2005-08-13 21:05:57 -0700596static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
597 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700598{
599 struct rb_node *node = cm.remote_id_table.rb_node;
600 struct cm_timewait_info *timewait_info;
601
602 while (node) {
603 timewait_info = rb_entry(node, struct cm_timewait_info,
604 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700605 if (be32_lt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700606 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700607 else if (be32_gt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700608 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700609 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700610 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700611 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700612 node = node->rb_right;
613 else
614 return timewait_info;
615 }
616 return NULL;
617}
618
619static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
620 *timewait_info)
621{
622 struct rb_node **link = &cm.remote_qp_table.rb_node;
623 struct rb_node *parent = NULL;
624 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700625 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
626 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700627
628 while (*link) {
629 parent = *link;
630 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
631 remote_qp_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700632 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700633 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700634 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700635 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700636 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700637 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700638 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700639 link = &(*link)->rb_right;
640 else
641 return cur_timewait_info;
642 }
643 timewait_info->inserted_remote_qp = 1;
644 rb_link_node(&timewait_info->remote_qp_node, parent, link);
645 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
646 return NULL;
647}
648
649static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
650 *cm_id_priv)
651{
652 struct rb_node **link = &cm.remote_sidr_table.rb_node;
653 struct rb_node *parent = NULL;
654 struct cm_id_private *cur_cm_id_priv;
655 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700656 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700657
658 while (*link) {
659 parent = *link;
660 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
661 sidr_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700662 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700663 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700664 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700665 link = &(*link)->rb_right;
666 else {
667 int cmp;
668 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
669 sizeof *port_gid);
670 if (cmp < 0)
671 link = &(*link)->rb_left;
672 else if (cmp > 0)
673 link = &(*link)->rb_right;
674 else
675 return cur_cm_id_priv;
676 }
677 }
678 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
679 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
680 return NULL;
681}
682
683static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
684 enum ib_cm_sidr_status status)
685{
686 struct ib_cm_sidr_rep_param param;
687
688 memset(&param, 0, sizeof param);
689 param.status = status;
690 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
691}
692
Sean Hefty07d357d2005-10-17 15:37:43 -0700693struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
694 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700695 void *context)
696{
697 struct cm_id_private *cm_id_priv;
698 int ret;
699
Roland Dreierde6eb662005-11-02 07:23:14 -0800700 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700701 if (!cm_id_priv)
702 return ERR_PTR(-ENOMEM);
703
Hal Rosenstocka9770492005-07-27 11:45:40 -0700704 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700705 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700706 cm_id_priv->id.cm_handler = cm_handler;
707 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700708 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700709 ret = cm_alloc_id(cm_id_priv);
710 if (ret)
711 goto error;
712
713 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700714 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700715 INIT_LIST_HEAD(&cm_id_priv->work_list);
716 atomic_set(&cm_id_priv->work_count, -1);
717 atomic_set(&cm_id_priv->refcount, 1);
718 return &cm_id_priv->id;
719
720error:
721 kfree(cm_id_priv);
722 return ERR_PTR(-ENOMEM);
723}
724EXPORT_SYMBOL(ib_create_cm_id);
725
726static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
727{
728 struct cm_work *work;
729
730 if (list_empty(&cm_id_priv->work_list))
731 return NULL;
732
733 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
734 list_del(&work->list);
735 return work;
736}
737
738static void cm_free_work(struct cm_work *work)
739{
740 if (work->mad_recv_wc)
741 ib_free_recv_mad(work->mad_recv_wc);
742 kfree(work);
743}
744
745static inline int cm_convert_to_ms(int iba_time)
746{
747 /* approximate conversion to ms from 4.096us x 2^iba_time */
748 return 1 << max(iba_time - 8, 0);
749}
750
Sean Hefty1d846122007-06-18 11:09:37 -0700751/*
752 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
753 * Because of how ack_timeout is stored, adding one doubles the timeout.
754 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
755 * increment it (round up) only if the other is within 50%.
756 */
757static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
758{
759 int ack_timeout = packet_life_time + 1;
760
761 if (ack_timeout >= ca_ack_delay)
762 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
763 else
764 ack_timeout = ca_ack_delay +
765 (ack_timeout >= (ca_ack_delay - 1));
766
767 return min(31, ack_timeout);
768}
769
Hal Rosenstocka9770492005-07-27 11:45:40 -0700770static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
771{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700772 if (timewait_info->inserted_remote_id) {
773 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
774 timewait_info->inserted_remote_id = 0;
775 }
776
777 if (timewait_info->inserted_remote_qp) {
778 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
779 timewait_info->inserted_remote_qp = 0;
780 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700781}
782
Sean Hefty97f52eb2005-08-13 21:05:57 -0700783static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700784{
785 struct cm_timewait_info *timewait_info;
786
Roland Dreierde6eb662005-11-02 07:23:14 -0800787 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700788 if (!timewait_info)
789 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700790
791 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000792 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700793 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
794 return timewait_info;
795}
796
797static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
798{
799 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700800 unsigned long flags;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700801
Sean Hefty85753292006-10-04 11:29:59 -0700802 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300803 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700804 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
805 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300806
Hal Rosenstocka9770492005-07-27 11:45:40 -0700807 /*
808 * The cm_id could be destroyed by the user before we exit timewait.
809 * To protect against this, we search for the cm_id after exiting
810 * timewait before notifying the user that we've exited timewait.
811 */
812 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700813 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700814 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
815 msecs_to_jiffies(wait_time));
816 cm_id_priv->timewait_info = NULL;
817}
818
819static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
820{
Sean Hefty85753292006-10-04 11:29:59 -0700821 unsigned long flags;
822
Hal Rosenstocka9770492005-07-27 11:45:40 -0700823 cm_id_priv->id.state = IB_CM_IDLE;
824 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700825 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700826 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700827 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700828 kfree(cm_id_priv->timewait_info);
829 cm_id_priv->timewait_info = NULL;
830 }
831}
832
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700833static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700834{
835 struct cm_id_private *cm_id_priv;
836 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700837
838 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
839retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700840 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700841 switch (cm_id->state) {
842 case IB_CM_LISTEN:
843 cm_id->state = IB_CM_IDLE;
Sean Hefty24be6e82007-06-18 11:09:36 -0700844 spin_unlock_irq(&cm_id_priv->lock);
845 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700846 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700847 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700848 break;
849 case IB_CM_SIDR_REQ_SENT:
850 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700851 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700852 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700853 break;
854 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700855 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700856 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
857 break;
858 case IB_CM_REQ_SENT:
Sean Hefty227eca82005-11-30 10:00:25 -0800859 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700860 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800861 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800862 &cm_id_priv->id.device->node_guid,
863 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800864 NULL, 0);
865 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700866 case IB_CM_REQ_RCVD:
867 if (err == -ENOMEM) {
868 /* Do not reject to allow future retries. */
869 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700870 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700871 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700872 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700873 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
874 NULL, 0, NULL, 0);
875 }
876 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700877 case IB_CM_MRA_REQ_RCVD:
878 case IB_CM_REP_SENT:
879 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700880 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700881 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700882 case IB_CM_MRA_REQ_SENT:
883 case IB_CM_REP_RCVD:
884 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700885 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800886 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
887 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700888 break;
889 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700890 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700891 ib_send_cm_dreq(cm_id, NULL, 0);
892 goto retest;
893 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700894 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700895 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700896 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700897 break;
898 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700899 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700900 ib_send_cm_drep(cm_id, NULL, 0);
901 break;
902 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700903 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700904 break;
905 }
906
907 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700908 cm_deref_id(cm_id_priv);
909 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700910 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
911 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -0700912 kfree(cm_id_priv->compare_data);
913 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700914 kfree(cm_id_priv);
915}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700916
917void ib_destroy_cm_id(struct ib_cm_id *cm_id)
918{
919 cm_destroy_id(cm_id, 0);
920}
Hal Rosenstocka9770492005-07-27 11:45:40 -0700921EXPORT_SYMBOL(ib_destroy_cm_id);
922
Sean Hefty6e61d042006-06-17 20:37:28 -0700923int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
924 struct ib_cm_compare_data *compare_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700925{
926 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
927 unsigned long flags;
928 int ret = 0;
929
Sean Hefty97f52eb2005-08-13 21:05:57 -0700930 service_mask = service_mask ? service_mask :
931 __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700932 service_id &= service_mask;
933 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
934 (service_id != IB_CM_ASSIGN_SERVICE_ID))
935 return -EINVAL;
936
937 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -0700938 if (cm_id->state != IB_CM_IDLE)
939 return -EINVAL;
940
941 if (compare_data) {
942 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
943 GFP_KERNEL);
944 if (!cm_id_priv->compare_data)
945 return -ENOMEM;
946 cm_mask_copy(cm_id_priv->compare_data->data,
947 compare_data->data, compare_data->mask);
948 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
949 IB_CM_COMPARE_SIZE);
950 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700951
952 cm_id->state = IB_CM_LISTEN;
953
954 spin_lock_irqsave(&cm.lock, flags);
955 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700956 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
957 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700958 } else {
959 cm_id->service_id = service_id;
960 cm_id->service_mask = service_mask;
961 }
962 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
963 spin_unlock_irqrestore(&cm.lock, flags);
964
965 if (cur_cm_id_priv) {
966 cm_id->state = IB_CM_IDLE;
Sean Hefty6e61d042006-06-17 20:37:28 -0700967 kfree(cm_id_priv->compare_data);
968 cm_id_priv->compare_data = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700969 ret = -EBUSY;
970 }
971 return ret;
972}
973EXPORT_SYMBOL(ib_cm_listen);
974
Sean Hefty97f52eb2005-08-13 21:05:57 -0700975static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
976 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700977{
978 u64 hi_tid, low_tid;
979
980 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700981 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
982 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700983 return cpu_to_be64(hi_tid | low_tid);
984}
985
986static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -0700987 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700988{
989 hdr->base_version = IB_MGMT_BASE_VERSION;
990 hdr->mgmt_class = IB_MGMT_CLASS_CM;
991 hdr->class_version = IB_CM_CLASS_VERSION;
992 hdr->method = IB_MGMT_METHOD_SEND;
993 hdr->attr_id = attr_id;
994 hdr->tid = tid;
995}
996
997static void cm_format_req(struct cm_req_msg *req_msg,
998 struct cm_id_private *cm_id_priv,
999 struct ib_cm_req_param *param)
1000{
Sean Hefty3971c9f2007-12-10 15:53:25 -08001001 struct ib_sa_path_rec *pri_path = param->primary_path;
1002 struct ib_sa_path_rec *alt_path = param->alternate_path;
1003
Hal Rosenstocka9770492005-07-27 11:45:40 -07001004 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1005 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1006
1007 req_msg->local_comm_id = cm_id_priv->id.local_id;
1008 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -08001009 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001010 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1011 cm_req_set_resp_res(req_msg, param->responder_resources);
1012 cm_req_set_init_depth(req_msg, param->initiator_depth);
1013 cm_req_set_remote_resp_timeout(req_msg,
1014 param->remote_cm_response_timeout);
1015 cm_req_set_qp_type(req_msg, param->qp_type);
1016 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1017 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1018 cm_req_set_local_resp_timeout(req_msg,
1019 param->local_cm_response_timeout);
1020 cm_req_set_retry_count(req_msg, param->retry_count);
1021 req_msg->pkey = param->primary_path->pkey;
1022 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1023 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1024 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1025 cm_req_set_srq(req_msg, param->srq);
1026
Sean Hefty3971c9f2007-12-10 15:53:25 -08001027 if (pri_path->hop_limit <= 1) {
1028 req_msg->primary_local_lid = pri_path->slid;
1029 req_msg->primary_remote_lid = pri_path->dlid;
1030 } else {
1031 /* Work-around until there's a way to obtain remote LID info */
1032 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1033 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1034 }
1035 req_msg->primary_local_gid = pri_path->sgid;
1036 req_msg->primary_remote_gid = pri_path->dgid;
1037 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1038 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1039 req_msg->primary_traffic_class = pri_path->traffic_class;
1040 req_msg->primary_hop_limit = pri_path->hop_limit;
1041 cm_req_set_primary_sl(req_msg, pri_path->sl);
1042 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001043 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001044 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001045 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001046
Sean Hefty3971c9f2007-12-10 15:53:25 -08001047 if (alt_path) {
1048 if (alt_path->hop_limit <= 1) {
1049 req_msg->alt_local_lid = alt_path->slid;
1050 req_msg->alt_remote_lid = alt_path->dlid;
1051 } else {
1052 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1053 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1054 }
1055 req_msg->alt_local_gid = alt_path->sgid;
1056 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001057 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001058 alt_path->flow_label);
1059 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1060 req_msg->alt_traffic_class = alt_path->traffic_class;
1061 req_msg->alt_hop_limit = alt_path->hop_limit;
1062 cm_req_set_alt_sl(req_msg, alt_path->sl);
1063 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001064 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001065 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001066 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001067 }
1068
1069 if (param->private_data && param->private_data_len)
1070 memcpy(req_msg->private_data, param->private_data,
1071 param->private_data_len);
1072}
1073
Arjan van de Ven858119e2006-01-14 13:20:43 -08001074static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001075{
1076 /* peer-to-peer not supported */
1077 if (param->peer_to_peer)
1078 return -EINVAL;
1079
1080 if (!param->primary_path)
1081 return -EINVAL;
1082
1083 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1084 return -EINVAL;
1085
1086 if (param->private_data &&
1087 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1088 return -EINVAL;
1089
1090 if (param->alternate_path &&
1091 (param->alternate_path->pkey != param->primary_path->pkey ||
1092 param->alternate_path->mtu != param->primary_path->mtu))
1093 return -EINVAL;
1094
1095 return 0;
1096}
1097
1098int ib_send_cm_req(struct ib_cm_id *cm_id,
1099 struct ib_cm_req_param *param)
1100{
1101 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001102 struct cm_req_msg *req_msg;
1103 unsigned long flags;
1104 int ret;
1105
1106 ret = cm_validate_req_param(param);
1107 if (ret)
1108 return ret;
1109
1110 /* Verify that we're not in timewait. */
1111 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1112 spin_lock_irqsave(&cm_id_priv->lock, flags);
1113 if (cm_id->state != IB_CM_IDLE) {
1114 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1115 ret = -EINVAL;
1116 goto out;
1117 }
1118 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1119
1120 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1121 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001122 if (IS_ERR(cm_id_priv->timewait_info)) {
1123 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001124 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001125 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001126
1127 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1128 if (ret)
1129 goto error1;
1130 if (param->alternate_path) {
1131 ret = cm_init_av_by_path(param->alternate_path,
1132 &cm_id_priv->alt_av);
1133 if (ret)
1134 goto error1;
1135 }
1136 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001137 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001138 cm_id_priv->timeout_ms = cm_convert_to_ms(
1139 param->primary_path->packet_life_time) * 2 +
1140 cm_convert_to_ms(
1141 param->remote_cm_response_timeout);
1142 cm_id_priv->max_cm_retries = param->max_cm_retries;
1143 cm_id_priv->initiator_depth = param->initiator_depth;
1144 cm_id_priv->responder_resources = param->responder_resources;
1145 cm_id_priv->retry_count = param->retry_count;
1146 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001147 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001148 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001149
1150 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1151 if (ret)
1152 goto error1;
1153
1154 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1155 cm_format_req(req_msg, cm_id_priv, param);
1156 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001157 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001158 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1159
1160 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1161 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001162
1163 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001164 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001165 if (ret) {
1166 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1167 goto error2;
1168 }
1169 BUG_ON(cm_id->state != IB_CM_IDLE);
1170 cm_id->state = IB_CM_REQ_SENT;
1171 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1172 return 0;
1173
1174error2: cm_free_msg(cm_id_priv->msg);
1175error1: kfree(cm_id_priv->timewait_info);
1176out: return ret;
1177}
1178EXPORT_SYMBOL(ib_send_cm_req);
1179
1180static int cm_issue_rej(struct cm_port *port,
1181 struct ib_mad_recv_wc *mad_recv_wc,
1182 enum ib_cm_rej_reason reason,
1183 enum cm_msg_response msg_rejected,
1184 void *ari, u8 ari_length)
1185{
1186 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001187 struct cm_rej_msg *rej_msg, *rcv_msg;
1188 int ret;
1189
1190 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1191 if (ret)
1192 return ret;
1193
1194 /* We just need common CM header information. Cast to any message. */
1195 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1196 rej_msg = (struct cm_rej_msg *) msg->mad;
1197
1198 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1199 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1200 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1201 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001202 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001203
1204 if (ari && ari_length) {
1205 cm_rej_set_reject_info_len(rej_msg, ari_length);
1206 memcpy(rej_msg->ari, ari, ari_length);
1207 }
1208
Sean Hefty34816ad2005-10-25 10:51:39 -07001209 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001210 if (ret)
1211 cm_free_msg(msg);
1212
1213 return ret;
1214}
1215
Sean Hefty97f52eb2005-08-13 21:05:57 -07001216static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1217 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001218{
1219 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1220 ((local_ca_guid == remote_ca_guid) &&
1221 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1222}
1223
Arjan van de Ven858119e2006-01-14 13:20:43 -08001224static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001225 struct ib_sa_path_rec *primary_path,
1226 struct ib_sa_path_rec *alt_path)
1227{
1228 memset(primary_path, 0, sizeof *primary_path);
1229 primary_path->dgid = req_msg->primary_local_gid;
1230 primary_path->sgid = req_msg->primary_remote_gid;
1231 primary_path->dlid = req_msg->primary_local_lid;
1232 primary_path->slid = req_msg->primary_remote_lid;
1233 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1234 primary_path->hop_limit = req_msg->primary_hop_limit;
1235 primary_path->traffic_class = req_msg->primary_traffic_class;
1236 primary_path->reversible = 1;
1237 primary_path->pkey = req_msg->pkey;
1238 primary_path->sl = cm_req_get_primary_sl(req_msg);
1239 primary_path->mtu_selector = IB_SA_EQ;
1240 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1241 primary_path->rate_selector = IB_SA_EQ;
1242 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1243 primary_path->packet_life_time_selector = IB_SA_EQ;
1244 primary_path->packet_life_time =
1245 cm_req_get_primary_local_ack_timeout(req_msg);
1246 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1247
1248 if (req_msg->alt_local_lid) {
1249 memset(alt_path, 0, sizeof *alt_path);
1250 alt_path->dgid = req_msg->alt_local_gid;
1251 alt_path->sgid = req_msg->alt_remote_gid;
1252 alt_path->dlid = req_msg->alt_local_lid;
1253 alt_path->slid = req_msg->alt_remote_lid;
1254 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1255 alt_path->hop_limit = req_msg->alt_hop_limit;
1256 alt_path->traffic_class = req_msg->alt_traffic_class;
1257 alt_path->reversible = 1;
1258 alt_path->pkey = req_msg->pkey;
1259 alt_path->sl = cm_req_get_alt_sl(req_msg);
1260 alt_path->mtu_selector = IB_SA_EQ;
1261 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1262 alt_path->rate_selector = IB_SA_EQ;
1263 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1264 alt_path->packet_life_time_selector = IB_SA_EQ;
1265 alt_path->packet_life_time =
1266 cm_req_get_alt_local_ack_timeout(req_msg);
1267 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1268 }
1269}
1270
1271static void cm_format_req_event(struct cm_work *work,
1272 struct cm_id_private *cm_id_priv,
1273 struct ib_cm_id *listen_id)
1274{
1275 struct cm_req_msg *req_msg;
1276 struct ib_cm_req_event_param *param;
1277
1278 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1279 param = &work->cm_event.param.req_rcvd;
1280 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001281 param->port = cm_id_priv->av.port->port_num;
1282 param->primary_path = &work->path[0];
1283 if (req_msg->alt_local_lid)
1284 param->alternate_path = &work->path[1];
1285 else
1286 param->alternate_path = NULL;
1287 param->remote_ca_guid = req_msg->local_ca_guid;
1288 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1289 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1290 param->qp_type = cm_req_get_qp_type(req_msg);
1291 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1292 param->responder_resources = cm_req_get_init_depth(req_msg);
1293 param->initiator_depth = cm_req_get_resp_res(req_msg);
1294 param->local_cm_response_timeout =
1295 cm_req_get_remote_resp_timeout(req_msg);
1296 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1297 param->remote_cm_response_timeout =
1298 cm_req_get_local_resp_timeout(req_msg);
1299 param->retry_count = cm_req_get_retry_count(req_msg);
1300 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1301 param->srq = cm_req_get_srq(req_msg);
1302 work->cm_event.private_data = &req_msg->private_data;
1303}
1304
1305static void cm_process_work(struct cm_id_private *cm_id_priv,
1306 struct cm_work *work)
1307{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001308 int ret;
1309
1310 /* We will typically only have the current event to report. */
1311 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1312 cm_free_work(work);
1313
1314 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001315 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001316 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001317 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001318 BUG_ON(!work);
1319 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1320 &work->cm_event);
1321 cm_free_work(work);
1322 }
1323 cm_deref_id(cm_id_priv);
1324 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001325 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001326}
1327
1328static void cm_format_mra(struct cm_mra_msg *mra_msg,
1329 struct cm_id_private *cm_id_priv,
1330 enum cm_msg_response msg_mraed, u8 service_timeout,
1331 const void *private_data, u8 private_data_len)
1332{
1333 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1334 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1335 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1336 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1337 cm_mra_set_service_timeout(mra_msg, service_timeout);
1338
1339 if (private_data && private_data_len)
1340 memcpy(mra_msg->private_data, private_data, private_data_len);
1341}
1342
1343static void cm_format_rej(struct cm_rej_msg *rej_msg,
1344 struct cm_id_private *cm_id_priv,
1345 enum ib_cm_rej_reason reason,
1346 void *ari,
1347 u8 ari_length,
1348 const void *private_data,
1349 u8 private_data_len)
1350{
1351 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1352 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1353
1354 switch(cm_id_priv->id.state) {
1355 case IB_CM_REQ_RCVD:
1356 rej_msg->local_comm_id = 0;
1357 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1358 break;
1359 case IB_CM_MRA_REQ_SENT:
1360 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1361 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1362 break;
1363 case IB_CM_REP_RCVD:
1364 case IB_CM_MRA_REP_SENT:
1365 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1366 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1367 break;
1368 default:
1369 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1370 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1371 break;
1372 }
1373
Sean Hefty97f52eb2005-08-13 21:05:57 -07001374 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001375 if (ari && ari_length) {
1376 cm_rej_set_reject_info_len(rej_msg, ari_length);
1377 memcpy(rej_msg->ari, ari, ari_length);
1378 }
1379
1380 if (private_data && private_data_len)
1381 memcpy(rej_msg->private_data, private_data, private_data_len);
1382}
1383
1384static void cm_dup_req_handler(struct cm_work *work,
1385 struct cm_id_private *cm_id_priv)
1386{
1387 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001388 int ret;
1389
Sean Hefty9af57b72007-07-16 21:49:35 -07001390 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1391 counter[CM_REQ_COUNTER]);
1392
Hal Rosenstocka9770492005-07-27 11:45:40 -07001393 /* Quick state check to discard duplicate REQs. */
1394 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1395 return;
1396
1397 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1398 if (ret)
1399 return;
1400
Sean Hefty24be6e82007-06-18 11:09:36 -07001401 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001402 switch (cm_id_priv->id.state) {
1403 case IB_CM_MRA_REQ_SENT:
1404 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1405 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1406 cm_id_priv->private_data,
1407 cm_id_priv->private_data_len);
1408 break;
1409 case IB_CM_TIMEWAIT:
1410 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1411 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1412 break;
1413 default:
1414 goto unlock;
1415 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001416 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001417
Sean Hefty34816ad2005-10-25 10:51:39 -07001418 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001419 if (ret)
1420 goto free;
1421 return;
1422
Sean Hefty24be6e82007-06-18 11:09:36 -07001423unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001424free: cm_free_msg(msg);
1425}
1426
1427static struct cm_id_private * cm_match_req(struct cm_work *work,
1428 struct cm_id_private *cm_id_priv)
1429{
1430 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1431 struct cm_timewait_info *timewait_info;
1432 struct cm_req_msg *req_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001433
1434 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1435
Sean Heftyd998ccc2007-05-21 17:38:02 -07001436 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001437 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001438 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001439 if (timewait_info) {
1440 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1441 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001442 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001443 if (cur_cm_id_priv) {
1444 cm_dup_req_handler(work, cur_cm_id_priv);
1445 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001446 }
1447 return NULL;
1448 }
1449
1450 /* Check for stale connections. */
1451 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1452 if (timewait_info) {
1453 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001454 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001455 cm_issue_rej(work->port, work->mad_recv_wc,
1456 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1457 NULL, 0);
1458 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001459 }
1460
1461 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001462 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Sean Hefty6e61d042006-06-17 20:37:28 -07001463 req_msg->service_id,
1464 req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001465 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001466 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001467 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001468 cm_issue_rej(work->port, work->mad_recv_wc,
1469 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1470 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001471 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001472 }
1473 atomic_inc(&listen_cm_id_priv->refcount);
1474 atomic_inc(&cm_id_priv->refcount);
1475 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1476 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001477 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001478out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001479 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001480}
1481
Sean Hefty3971c9f2007-12-10 15:53:25 -08001482/*
1483 * Work-around for inter-subnet connections. If the LIDs are permissive,
1484 * we need to override the LID/SL data in the REQ with the LID information
1485 * in the work completion.
1486 */
1487static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1488{
1489 if (!cm_req_get_primary_subnet_local(req_msg)) {
1490 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1491 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1492 cm_req_set_primary_sl(req_msg, wc->sl);
1493 }
1494
1495 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1496 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1497 }
1498
1499 if (!cm_req_get_alt_subnet_local(req_msg)) {
1500 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1501 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1502 cm_req_set_alt_sl(req_msg, wc->sl);
1503 }
1504
1505 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1506 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1507 }
1508}
1509
Hal Rosenstocka9770492005-07-27 11:45:40 -07001510static int cm_req_handler(struct cm_work *work)
1511{
1512 struct ib_cm_id *cm_id;
1513 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1514 struct cm_req_msg *req_msg;
1515 int ret;
1516
1517 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1518
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001519 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001520 if (IS_ERR(cm_id))
1521 return PTR_ERR(cm_id);
1522
1523 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1524 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1525 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001526 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001527 &cm_id_priv->av);
1528 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1529 id.local_id);
1530 if (IS_ERR(cm_id_priv->timewait_info)) {
1531 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001532 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001533 }
1534 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1535 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1536 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1537
1538 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1539 if (!listen_cm_id_priv) {
1540 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001541 kfree(cm_id_priv->timewait_info);
1542 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001543 }
1544
1545 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1546 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1547 cm_id_priv->id.service_id = req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001548 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001549
Sean Hefty3971c9f2007-12-10 15:53:25 -08001550 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001551 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1552 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
Sean Hefty76842402006-08-28 11:57:42 -07001553 if (ret) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001554 ib_get_cached_gid(work->port->cm_dev->ib_device,
Sean Hefty76842402006-08-28 11:57:42 -07001555 work->port->port_num, 0, &work->path[0].sgid);
1556 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1557 &work->path[0].sgid, sizeof work->path[0].sgid,
1558 NULL, 0);
1559 goto rejected;
1560 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001561 if (req_msg->alt_local_lid) {
1562 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
Sean Hefty76842402006-08-28 11:57:42 -07001563 if (ret) {
1564 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1565 &work->path[0].sgid,
1566 sizeof work->path[0].sgid, NULL, 0);
1567 goto rejected;
1568 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001569 }
1570 cm_id_priv->tid = req_msg->hdr.tid;
1571 cm_id_priv->timeout_ms = cm_convert_to_ms(
1572 cm_req_get_local_resp_timeout(req_msg));
1573 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1574 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1575 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1576 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1577 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001578 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001579 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001580 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1581 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001582 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001583
1584 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1585 cm_process_work(cm_id_priv, work);
1586 cm_deref_id(listen_cm_id_priv);
1587 return 0;
1588
Sean Hefty76842402006-08-28 11:57:42 -07001589rejected:
1590 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001591 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001592destroy:
1593 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001594 return ret;
1595}
1596
1597static void cm_format_rep(struct cm_rep_msg *rep_msg,
1598 struct cm_id_private *cm_id_priv,
1599 struct ib_cm_rep_param *param)
1600{
1601 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1602 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1603 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1604 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1605 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1606 rep_msg->resp_resources = param->responder_resources;
1607 rep_msg->initiator_depth = param->initiator_depth;
Sean Hefty1d846122007-06-18 11:09:37 -07001608 cm_rep_set_target_ack_delay(rep_msg,
1609 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001610 cm_rep_set_failover(rep_msg, param->failover_accepted);
1611 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1612 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1613 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftye971b8c2007-02-22 11:37:38 -08001614 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001615
1616 if (param->private_data && param->private_data_len)
1617 memcpy(rep_msg->private_data, param->private_data,
1618 param->private_data_len);
1619}
1620
1621int ib_send_cm_rep(struct ib_cm_id *cm_id,
1622 struct ib_cm_rep_param *param)
1623{
1624 struct cm_id_private *cm_id_priv;
1625 struct ib_mad_send_buf *msg;
1626 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001627 unsigned long flags;
1628 int ret;
1629
1630 if (param->private_data &&
1631 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1632 return -EINVAL;
1633
1634 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1635 spin_lock_irqsave(&cm_id_priv->lock, flags);
1636 if (cm_id->state != IB_CM_REQ_RCVD &&
1637 cm_id->state != IB_CM_MRA_REQ_SENT) {
1638 ret = -EINVAL;
1639 goto out;
1640 }
1641
1642 ret = cm_alloc_msg(cm_id_priv, &msg);
1643 if (ret)
1644 goto out;
1645
1646 rep_msg = (struct cm_rep_msg *) msg->mad;
1647 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001648 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001649 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1650
Sean Hefty34816ad2005-10-25 10:51:39 -07001651 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001652 if (ret) {
1653 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1654 cm_free_msg(msg);
1655 return ret;
1656 }
1657
1658 cm_id->state = IB_CM_REP_SENT;
1659 cm_id_priv->msg = msg;
1660 cm_id_priv->initiator_depth = param->initiator_depth;
1661 cm_id_priv->responder_resources = param->responder_resources;
1662 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1663 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1664
1665out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1666 return ret;
1667}
1668EXPORT_SYMBOL(ib_send_cm_rep);
1669
1670static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1671 struct cm_id_private *cm_id_priv,
1672 const void *private_data,
1673 u8 private_data_len)
1674{
1675 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1676 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1677 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1678
1679 if (private_data && private_data_len)
1680 memcpy(rtu_msg->private_data, private_data, private_data_len);
1681}
1682
1683int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1684 const void *private_data,
1685 u8 private_data_len)
1686{
1687 struct cm_id_private *cm_id_priv;
1688 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001689 unsigned long flags;
1690 void *data;
1691 int ret;
1692
1693 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1694 return -EINVAL;
1695
1696 data = cm_copy_private_data(private_data, private_data_len);
1697 if (IS_ERR(data))
1698 return PTR_ERR(data);
1699
1700 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1701 spin_lock_irqsave(&cm_id_priv->lock, flags);
1702 if (cm_id->state != IB_CM_REP_RCVD &&
1703 cm_id->state != IB_CM_MRA_REP_SENT) {
1704 ret = -EINVAL;
1705 goto error;
1706 }
1707
1708 ret = cm_alloc_msg(cm_id_priv, &msg);
1709 if (ret)
1710 goto error;
1711
1712 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1713 private_data, private_data_len);
1714
Sean Hefty34816ad2005-10-25 10:51:39 -07001715 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001716 if (ret) {
1717 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1718 cm_free_msg(msg);
1719 kfree(data);
1720 return ret;
1721 }
1722
1723 cm_id->state = IB_CM_ESTABLISHED;
1724 cm_set_private_data(cm_id_priv, data, private_data_len);
1725 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1726 return 0;
1727
1728error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1729 kfree(data);
1730 return ret;
1731}
1732EXPORT_SYMBOL(ib_send_cm_rtu);
1733
1734static void cm_format_rep_event(struct cm_work *work)
1735{
1736 struct cm_rep_msg *rep_msg;
1737 struct ib_cm_rep_event_param *param;
1738
1739 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1740 param = &work->cm_event.param.rep_rcvd;
1741 param->remote_ca_guid = rep_msg->local_ca_guid;
1742 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1743 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1744 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1745 param->responder_resources = rep_msg->initiator_depth;
1746 param->initiator_depth = rep_msg->resp_resources;
1747 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1748 param->failover_accepted = cm_rep_get_failover(rep_msg);
1749 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1750 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1751 param->srq = cm_rep_get_srq(rep_msg);
1752 work->cm_event.private_data = &rep_msg->private_data;
1753}
1754
1755static void cm_dup_rep_handler(struct cm_work *work)
1756{
1757 struct cm_id_private *cm_id_priv;
1758 struct cm_rep_msg *rep_msg;
1759 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001760 int ret;
1761
1762 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1763 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1764 rep_msg->local_comm_id);
1765 if (!cm_id_priv)
1766 return;
1767
Sean Hefty9af57b72007-07-16 21:49:35 -07001768 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1769 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001770 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1771 if (ret)
1772 goto deref;
1773
Sean Hefty24be6e82007-06-18 11:09:36 -07001774 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001775 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1776 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1777 cm_id_priv->private_data,
1778 cm_id_priv->private_data_len);
1779 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1780 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1781 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1782 cm_id_priv->private_data,
1783 cm_id_priv->private_data_len);
1784 else
1785 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07001786 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001787
Sean Hefty34816ad2005-10-25 10:51:39 -07001788 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001789 if (ret)
1790 goto free;
1791 goto deref;
1792
Sean Hefty24be6e82007-06-18 11:09:36 -07001793unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001794free: cm_free_msg(msg);
1795deref: cm_deref_id(cm_id_priv);
1796}
1797
1798static int cm_rep_handler(struct cm_work *work)
1799{
1800 struct cm_id_private *cm_id_priv;
1801 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001802 int ret;
1803
1804 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1805 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1806 if (!cm_id_priv) {
1807 cm_dup_rep_handler(work);
1808 return -EINVAL;
1809 }
1810
Hal Rosenstocka9770492005-07-27 11:45:40 -07001811 cm_format_rep_event(work);
1812
Sean Hefty24be6e82007-06-18 11:09:36 -07001813 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001814 switch (cm_id_priv->id.state) {
1815 case IB_CM_REQ_SENT:
1816 case IB_CM_MRA_REQ_RCVD:
1817 break;
1818 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07001819 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001820 ret = -EINVAL;
1821 goto error;
1822 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08001823
1824 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1825 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1826 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1827
1828 spin_lock(&cm.lock);
1829 /* Check for duplicate REP. */
1830 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1831 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001832 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001833 ret = -EINVAL;
1834 goto error;
1835 }
1836 /* Check for a stale connection. */
1837 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1838 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1839 &cm.remote_id_table);
1840 cm_id_priv->timewait_info->inserted_remote_id = 0;
1841 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001842 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001843 cm_issue_rej(work->port, work->mad_recv_wc,
1844 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1845 NULL, 0);
1846 ret = -EINVAL;
1847 goto error;
1848 }
1849 spin_unlock(&cm.lock);
1850
Hal Rosenstocka9770492005-07-27 11:45:40 -07001851 cm_id_priv->id.state = IB_CM_REP_RCVD;
1852 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1853 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1854 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1855 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1856 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1857 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07001858 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1859 cm_id_priv->av.timeout =
1860 cm_ack_timeout(cm_id_priv->target_ack_delay,
1861 cm_id_priv->av.timeout - 1);
1862 cm_id_priv->alt_av.timeout =
1863 cm_ack_timeout(cm_id_priv->target_ack_delay,
1864 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001865
1866 /* todo: handle peer_to_peer */
1867
Sean Hefty34816ad2005-10-25 10:51:39 -07001868 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001869 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1870 if (!ret)
1871 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001872 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001873
1874 if (ret)
1875 cm_process_work(cm_id_priv, work);
1876 else
1877 cm_deref_id(cm_id_priv);
1878 return 0;
1879
Sean Hefty87fd1a12006-03-02 16:50:37 -08001880error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001881 cm_deref_id(cm_id_priv);
1882 return ret;
1883}
1884
1885static int cm_establish_handler(struct cm_work *work)
1886{
1887 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001888 int ret;
1889
Sean Heftye1444b52006-11-28 14:57:13 -08001890 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07001891 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1892 if (!cm_id_priv)
1893 return -EINVAL;
1894
Sean Hefty24be6e82007-06-18 11:09:36 -07001895 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001896 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001897 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001898 goto out;
1899 }
1900
Sean Hefty34816ad2005-10-25 10:51:39 -07001901 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001902 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1903 if (!ret)
1904 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001905 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001906
1907 if (ret)
1908 cm_process_work(cm_id_priv, work);
1909 else
1910 cm_deref_id(cm_id_priv);
1911 return 0;
1912out:
1913 cm_deref_id(cm_id_priv);
1914 return -EINVAL;
1915}
1916
1917static int cm_rtu_handler(struct cm_work *work)
1918{
1919 struct cm_id_private *cm_id_priv;
1920 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001921 int ret;
1922
1923 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1924 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1925 rtu_msg->local_comm_id);
1926 if (!cm_id_priv)
1927 return -EINVAL;
1928
1929 work->cm_event.private_data = &rtu_msg->private_data;
1930
Sean Hefty24be6e82007-06-18 11:09:36 -07001931 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001932 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1933 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001934 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07001935 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1936 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001937 goto out;
1938 }
1939 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1940
Sean Hefty34816ad2005-10-25 10:51:39 -07001941 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001942 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1943 if (!ret)
1944 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001945 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001946
1947 if (ret)
1948 cm_process_work(cm_id_priv, work);
1949 else
1950 cm_deref_id(cm_id_priv);
1951 return 0;
1952out:
1953 cm_deref_id(cm_id_priv);
1954 return -EINVAL;
1955}
1956
1957static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1958 struct cm_id_private *cm_id_priv,
1959 const void *private_data,
1960 u8 private_data_len)
1961{
1962 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1963 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1964 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1965 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1966 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1967
1968 if (private_data && private_data_len)
1969 memcpy(dreq_msg->private_data, private_data, private_data_len);
1970}
1971
1972int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1973 const void *private_data,
1974 u8 private_data_len)
1975{
1976 struct cm_id_private *cm_id_priv;
1977 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001978 unsigned long flags;
1979 int ret;
1980
1981 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1982 return -EINVAL;
1983
1984 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1985 spin_lock_irqsave(&cm_id_priv->lock, flags);
1986 if (cm_id->state != IB_CM_ESTABLISHED) {
1987 ret = -EINVAL;
1988 goto out;
1989 }
1990
1991 ret = cm_alloc_msg(cm_id_priv, &msg);
1992 if (ret) {
1993 cm_enter_timewait(cm_id_priv);
1994 goto out;
1995 }
1996
1997 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1998 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07001999 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002000 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2001
Sean Hefty34816ad2005-10-25 10:51:39 -07002002 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002003 if (ret) {
2004 cm_enter_timewait(cm_id_priv);
2005 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2006 cm_free_msg(msg);
2007 return ret;
2008 }
2009
2010 cm_id->state = IB_CM_DREQ_SENT;
2011 cm_id_priv->msg = msg;
2012out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2013 return ret;
2014}
2015EXPORT_SYMBOL(ib_send_cm_dreq);
2016
2017static void cm_format_drep(struct cm_drep_msg *drep_msg,
2018 struct cm_id_private *cm_id_priv,
2019 const void *private_data,
2020 u8 private_data_len)
2021{
2022 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2023 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2024 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2025
2026 if (private_data && private_data_len)
2027 memcpy(drep_msg->private_data, private_data, private_data_len);
2028}
2029
2030int ib_send_cm_drep(struct ib_cm_id *cm_id,
2031 const void *private_data,
2032 u8 private_data_len)
2033{
2034 struct cm_id_private *cm_id_priv;
2035 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002036 unsigned long flags;
2037 void *data;
2038 int ret;
2039
2040 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2041 return -EINVAL;
2042
2043 data = cm_copy_private_data(private_data, private_data_len);
2044 if (IS_ERR(data))
2045 return PTR_ERR(data);
2046
2047 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2048 spin_lock_irqsave(&cm_id_priv->lock, flags);
2049 if (cm_id->state != IB_CM_DREQ_RCVD) {
2050 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2051 kfree(data);
2052 return -EINVAL;
2053 }
2054
2055 cm_set_private_data(cm_id_priv, data, private_data_len);
2056 cm_enter_timewait(cm_id_priv);
2057
2058 ret = cm_alloc_msg(cm_id_priv, &msg);
2059 if (ret)
2060 goto out;
2061
2062 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2063 private_data, private_data_len);
2064
Sean Hefty34816ad2005-10-25 10:51:39 -07002065 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002066 if (ret) {
2067 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2068 cm_free_msg(msg);
2069 return ret;
2070 }
2071
2072out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2073 return ret;
2074}
2075EXPORT_SYMBOL(ib_send_cm_drep);
2076
Sean Hefty82a9c162006-10-04 11:37:25 -07002077static int cm_issue_drep(struct cm_port *port,
2078 struct ib_mad_recv_wc *mad_recv_wc)
2079{
2080 struct ib_mad_send_buf *msg = NULL;
2081 struct cm_dreq_msg *dreq_msg;
2082 struct cm_drep_msg *drep_msg;
2083 int ret;
2084
2085 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2086 if (ret)
2087 return ret;
2088
2089 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2090 drep_msg = (struct cm_drep_msg *) msg->mad;
2091
2092 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2093 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2094 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2095
2096 ret = ib_post_send_mad(msg, NULL);
2097 if (ret)
2098 cm_free_msg(msg);
2099
2100 return ret;
2101}
2102
Hal Rosenstocka9770492005-07-27 11:45:40 -07002103static int cm_dreq_handler(struct cm_work *work)
2104{
2105 struct cm_id_private *cm_id_priv;
2106 struct cm_dreq_msg *dreq_msg;
2107 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002108 int ret;
2109
2110 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2111 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2112 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002113 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002114 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2115 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002116 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002117 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002118 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002119
2120 work->cm_event.private_data = &dreq_msg->private_data;
2121
Sean Hefty24be6e82007-06-18 11:09:36 -07002122 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002123 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2124 goto unlock;
2125
2126 switch (cm_id_priv->id.state) {
2127 case IB_CM_REP_SENT:
2128 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002129 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002130 break;
2131 case IB_CM_ESTABLISHED:
2132 case IB_CM_MRA_REP_RCVD:
2133 break;
2134 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002135 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2136 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002137 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2138 goto unlock;
2139
2140 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2141 cm_id_priv->private_data,
2142 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002143 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002144
Sean Hefty34816ad2005-10-25 10:51:39 -07002145 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002146 cm_free_msg(msg);
2147 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002148 case IB_CM_DREQ_RCVD:
2149 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2150 counter[CM_DREQ_COUNTER]);
2151 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002152 default:
2153 goto unlock;
2154 }
2155 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2156 cm_id_priv->tid = dreq_msg->hdr.tid;
2157 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2158 if (!ret)
2159 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002160 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002161
2162 if (ret)
2163 cm_process_work(cm_id_priv, work);
2164 else
2165 cm_deref_id(cm_id_priv);
2166 return 0;
2167
Sean Hefty24be6e82007-06-18 11:09:36 -07002168unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002169deref: cm_deref_id(cm_id_priv);
2170 return -EINVAL;
2171}
2172
2173static int cm_drep_handler(struct cm_work *work)
2174{
2175 struct cm_id_private *cm_id_priv;
2176 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002177 int ret;
2178
2179 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2180 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2181 drep_msg->local_comm_id);
2182 if (!cm_id_priv)
2183 return -EINVAL;
2184
2185 work->cm_event.private_data = &drep_msg->private_data;
2186
Sean Hefty24be6e82007-06-18 11:09:36 -07002187 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002188 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2189 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002190 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002191 goto out;
2192 }
2193 cm_enter_timewait(cm_id_priv);
2194
Sean Hefty34816ad2005-10-25 10:51:39 -07002195 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002196 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2197 if (!ret)
2198 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002199 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002200
2201 if (ret)
2202 cm_process_work(cm_id_priv, work);
2203 else
2204 cm_deref_id(cm_id_priv);
2205 return 0;
2206out:
2207 cm_deref_id(cm_id_priv);
2208 return -EINVAL;
2209}
2210
2211int ib_send_cm_rej(struct ib_cm_id *cm_id,
2212 enum ib_cm_rej_reason reason,
2213 void *ari,
2214 u8 ari_length,
2215 const void *private_data,
2216 u8 private_data_len)
2217{
2218 struct cm_id_private *cm_id_priv;
2219 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002220 unsigned long flags;
2221 int ret;
2222
2223 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2224 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2225 return -EINVAL;
2226
2227 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2228
2229 spin_lock_irqsave(&cm_id_priv->lock, flags);
2230 switch (cm_id->state) {
2231 case IB_CM_REQ_SENT:
2232 case IB_CM_MRA_REQ_RCVD:
2233 case IB_CM_REQ_RCVD:
2234 case IB_CM_MRA_REQ_SENT:
2235 case IB_CM_REP_RCVD:
2236 case IB_CM_MRA_REP_SENT:
2237 ret = cm_alloc_msg(cm_id_priv, &msg);
2238 if (!ret)
2239 cm_format_rej((struct cm_rej_msg *) msg->mad,
2240 cm_id_priv, reason, ari, ari_length,
2241 private_data, private_data_len);
2242
2243 cm_reset_to_idle(cm_id_priv);
2244 break;
2245 case IB_CM_REP_SENT:
2246 case IB_CM_MRA_REP_RCVD:
2247 ret = cm_alloc_msg(cm_id_priv, &msg);
2248 if (!ret)
2249 cm_format_rej((struct cm_rej_msg *) msg->mad,
2250 cm_id_priv, reason, ari, ari_length,
2251 private_data, private_data_len);
2252
2253 cm_enter_timewait(cm_id_priv);
2254 break;
2255 default:
2256 ret = -EINVAL;
2257 goto out;
2258 }
2259
2260 if (ret)
2261 goto out;
2262
Sean Hefty34816ad2005-10-25 10:51:39 -07002263 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002264 if (ret)
2265 cm_free_msg(msg);
2266
2267out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2268 return ret;
2269}
2270EXPORT_SYMBOL(ib_send_cm_rej);
2271
2272static void cm_format_rej_event(struct cm_work *work)
2273{
2274 struct cm_rej_msg *rej_msg;
2275 struct ib_cm_rej_event_param *param;
2276
2277 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2278 param = &work->cm_event.param.rej_rcvd;
2279 param->ari = rej_msg->ari;
2280 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002281 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002282 work->cm_event.private_data = &rej_msg->private_data;
2283}
2284
2285static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2286{
2287 struct cm_timewait_info *timewait_info;
2288 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002289 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002290
2291 remote_id = rej_msg->local_comm_id;
2292
Sean Hefty97f52eb2005-08-13 21:05:57 -07002293 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002294 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002295 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002296 remote_id);
2297 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002298 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002299 return NULL;
2300 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002301 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2302 (timewait_info->work.local_id ^
2303 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002304 if (cm_id_priv) {
2305 if (cm_id_priv->id.remote_id == remote_id)
2306 atomic_inc(&cm_id_priv->refcount);
2307 else
2308 cm_id_priv = NULL;
2309 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002310 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002311 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2312 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2313 else
2314 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2315
2316 return cm_id_priv;
2317}
2318
2319static int cm_rej_handler(struct cm_work *work)
2320{
2321 struct cm_id_private *cm_id_priv;
2322 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002323 int ret;
2324
2325 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2326 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2327 if (!cm_id_priv)
2328 return -EINVAL;
2329
2330 cm_format_rej_event(work);
2331
Sean Hefty24be6e82007-06-18 11:09:36 -07002332 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002333 switch (cm_id_priv->id.state) {
2334 case IB_CM_REQ_SENT:
2335 case IB_CM_MRA_REQ_RCVD:
2336 case IB_CM_REP_SENT:
2337 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002338 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002339 /* fall through */
2340 case IB_CM_REQ_RCVD:
2341 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002342 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002343 cm_enter_timewait(cm_id_priv);
2344 else
2345 cm_reset_to_idle(cm_id_priv);
2346 break;
2347 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002348 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002349 /* fall through */
2350 case IB_CM_REP_RCVD:
2351 case IB_CM_MRA_REP_SENT:
2352 case IB_CM_ESTABLISHED:
2353 cm_enter_timewait(cm_id_priv);
2354 break;
2355 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002356 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002357 ret = -EINVAL;
2358 goto out;
2359 }
2360
2361 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2362 if (!ret)
2363 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002364 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002365
2366 if (ret)
2367 cm_process_work(cm_id_priv, work);
2368 else
2369 cm_deref_id(cm_id_priv);
2370 return 0;
2371out:
2372 cm_deref_id(cm_id_priv);
2373 return -EINVAL;
2374}
2375
2376int ib_send_cm_mra(struct ib_cm_id *cm_id,
2377 u8 service_timeout,
2378 const void *private_data,
2379 u8 private_data_len)
2380{
2381 struct cm_id_private *cm_id_priv;
2382 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002383 enum ib_cm_state cm_state;
2384 enum ib_cm_lap_state lap_state;
2385 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002386 void *data;
2387 unsigned long flags;
2388 int ret;
2389
2390 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2391 return -EINVAL;
2392
2393 data = cm_copy_private_data(private_data, private_data_len);
2394 if (IS_ERR(data))
2395 return PTR_ERR(data);
2396
2397 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2398
2399 spin_lock_irqsave(&cm_id_priv->lock, flags);
2400 switch(cm_id_priv->id.state) {
2401 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002402 cm_state = IB_CM_MRA_REQ_SENT;
2403 lap_state = cm_id->lap_state;
2404 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002405 break;
2406 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002407 cm_state = IB_CM_MRA_REP_SENT;
2408 lap_state = cm_id->lap_state;
2409 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002410 break;
2411 case IB_CM_ESTABLISHED:
Sean Heftyde98b692007-08-01 13:49:53 -07002412 cm_state = cm_id->state;
2413 lap_state = IB_CM_MRA_LAP_SENT;
2414 msg_response = CM_MSG_RESPONSE_OTHER;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002415 break;
2416 default:
2417 ret = -EINVAL;
2418 goto error1;
2419 }
Sean Heftyde98b692007-08-01 13:49:53 -07002420
2421 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2422 ret = cm_alloc_msg(cm_id_priv, &msg);
2423 if (ret)
2424 goto error1;
2425
2426 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2427 msg_response, service_timeout,
2428 private_data, private_data_len);
2429 ret = ib_post_send_mad(msg, NULL);
2430 if (ret)
2431 goto error2;
2432 }
2433
2434 cm_id->state = cm_state;
2435 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002436 cm_id_priv->service_timeout = service_timeout;
2437 cm_set_private_data(cm_id_priv, data, private_data_len);
2438 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2439 return 0;
2440
2441error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2442 kfree(data);
2443 return ret;
2444
2445error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2446 kfree(data);
2447 cm_free_msg(msg);
2448 return ret;
2449}
2450EXPORT_SYMBOL(ib_send_cm_mra);
2451
2452static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2453{
2454 switch (cm_mra_get_msg_mraed(mra_msg)) {
2455 case CM_MSG_RESPONSE_REQ:
2456 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2457 case CM_MSG_RESPONSE_REP:
2458 case CM_MSG_RESPONSE_OTHER:
2459 return cm_acquire_id(mra_msg->remote_comm_id,
2460 mra_msg->local_comm_id);
2461 default:
2462 return NULL;
2463 }
2464}
2465
2466static int cm_mra_handler(struct cm_work *work)
2467{
2468 struct cm_id_private *cm_id_priv;
2469 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002470 int timeout, ret;
2471
2472 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2473 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2474 if (!cm_id_priv)
2475 return -EINVAL;
2476
2477 work->cm_event.private_data = &mra_msg->private_data;
2478 work->cm_event.param.mra_rcvd.service_timeout =
2479 cm_mra_get_service_timeout(mra_msg);
2480 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002481 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002482
Sean Hefty24be6e82007-06-18 11:09:36 -07002483 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002484 switch (cm_id_priv->id.state) {
2485 case IB_CM_REQ_SENT:
2486 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2487 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002488 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002489 goto out;
2490 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2491 break;
2492 case IB_CM_REP_SENT:
2493 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2494 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002495 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002496 goto out;
2497 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2498 break;
2499 case IB_CM_ESTABLISHED:
2500 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2501 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2502 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002503 cm_id_priv->msg, timeout)) {
2504 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2505 atomic_long_inc(&work->port->
2506 counter_group[CM_RECV_DUPLICATES].
2507 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002508 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002509 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002510 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2511 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002512 case IB_CM_MRA_REQ_RCVD:
2513 case IB_CM_MRA_REP_RCVD:
2514 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2515 counter[CM_MRA_COUNTER]);
2516 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002517 default:
2518 goto out;
2519 }
2520
2521 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2522 cm_id_priv->id.state;
2523 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2524 if (!ret)
2525 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002526 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002527
2528 if (ret)
2529 cm_process_work(cm_id_priv, work);
2530 else
2531 cm_deref_id(cm_id_priv);
2532 return 0;
2533out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002534 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002535 cm_deref_id(cm_id_priv);
2536 return -EINVAL;
2537}
2538
2539static void cm_format_lap(struct cm_lap_msg *lap_msg,
2540 struct cm_id_private *cm_id_priv,
2541 struct ib_sa_path_rec *alternate_path,
2542 const void *private_data,
2543 u8 private_data_len)
2544{
2545 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2546 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2547 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2548 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2549 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2550 /* todo: need remote CM response timeout */
2551 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2552 lap_msg->alt_local_lid = alternate_path->slid;
2553 lap_msg->alt_remote_lid = alternate_path->dlid;
2554 lap_msg->alt_local_gid = alternate_path->sgid;
2555 lap_msg->alt_remote_gid = alternate_path->dgid;
2556 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2557 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2558 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2559 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2560 cm_lap_set_sl(lap_msg, alternate_path->sl);
2561 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2562 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002563 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2564 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002565
2566 if (private_data && private_data_len)
2567 memcpy(lap_msg->private_data, private_data, private_data_len);
2568}
2569
2570int ib_send_cm_lap(struct ib_cm_id *cm_id,
2571 struct ib_sa_path_rec *alternate_path,
2572 const void *private_data,
2573 u8 private_data_len)
2574{
2575 struct cm_id_private *cm_id_priv;
2576 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002577 unsigned long flags;
2578 int ret;
2579
2580 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2581 return -EINVAL;
2582
2583 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2584 spin_lock_irqsave(&cm_id_priv->lock, flags);
2585 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002586 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2587 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002588 ret = -EINVAL;
2589 goto out;
2590 }
2591
Sean Heftye1444b52006-11-28 14:57:13 -08002592 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2593 if (ret)
2594 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002595 cm_id_priv->alt_av.timeout =
2596 cm_ack_timeout(cm_id_priv->target_ack_delay,
2597 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002598
Hal Rosenstocka9770492005-07-27 11:45:40 -07002599 ret = cm_alloc_msg(cm_id_priv, &msg);
2600 if (ret)
2601 goto out;
2602
2603 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2604 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002605 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002606 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2607
Sean Hefty34816ad2005-10-25 10:51:39 -07002608 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002609 if (ret) {
2610 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2611 cm_free_msg(msg);
2612 return ret;
2613 }
2614
2615 cm_id->lap_state = IB_CM_LAP_SENT;
2616 cm_id_priv->msg = msg;
2617
2618out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2619 return ret;
2620}
2621EXPORT_SYMBOL(ib_send_cm_lap);
2622
Sean Heftye1444b52006-11-28 14:57:13 -08002623static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2624 struct ib_sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002625 struct cm_lap_msg *lap_msg)
2626{
2627 memset(path, 0, sizeof *path);
2628 path->dgid = lap_msg->alt_local_gid;
2629 path->sgid = lap_msg->alt_remote_gid;
2630 path->dlid = lap_msg->alt_local_lid;
2631 path->slid = lap_msg->alt_remote_lid;
2632 path->flow_label = cm_lap_get_flow_label(lap_msg);
2633 path->hop_limit = lap_msg->alt_hop_limit;
2634 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2635 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002636 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002637 path->sl = cm_lap_get_sl(lap_msg);
2638 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002639 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002640 path->rate_selector = IB_SA_EQ;
2641 path->rate = cm_lap_get_packet_rate(lap_msg);
2642 path->packet_life_time_selector = IB_SA_EQ;
2643 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2644 path->packet_life_time -= (path->packet_life_time > 0);
2645}
2646
2647static int cm_lap_handler(struct cm_work *work)
2648{
2649 struct cm_id_private *cm_id_priv;
2650 struct cm_lap_msg *lap_msg;
2651 struct ib_cm_lap_event_param *param;
2652 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002653 int ret;
2654
2655 /* todo: verify LAP request and send reject APR if invalid. */
2656 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2657 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2658 lap_msg->local_comm_id);
2659 if (!cm_id_priv)
2660 return -EINVAL;
2661
2662 param = &work->cm_event.param.lap_rcvd;
2663 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002664 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002665 work->cm_event.private_data = &lap_msg->private_data;
2666
Sean Hefty24be6e82007-06-18 11:09:36 -07002667 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002668 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2669 goto unlock;
2670
2671 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002672 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002673 case IB_CM_LAP_IDLE:
2674 break;
2675 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002676 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2677 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002678 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2679 goto unlock;
2680
2681 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2682 CM_MSG_RESPONSE_OTHER,
2683 cm_id_priv->service_timeout,
2684 cm_id_priv->private_data,
2685 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002686 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002687
Sean Hefty34816ad2005-10-25 10:51:39 -07002688 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002689 cm_free_msg(msg);
2690 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002691 case IB_CM_LAP_RCVD:
2692 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2693 counter[CM_LAP_COUNTER]);
2694 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002695 default:
2696 goto unlock;
2697 }
2698
2699 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2700 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08002701 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2702 work->mad_recv_wc->recv_buf.grh,
2703 &cm_id_priv->av);
2704 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002705 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2706 if (!ret)
2707 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002708 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002709
2710 if (ret)
2711 cm_process_work(cm_id_priv, work);
2712 else
2713 cm_deref_id(cm_id_priv);
2714 return 0;
2715
Sean Hefty24be6e82007-06-18 11:09:36 -07002716unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002717deref: cm_deref_id(cm_id_priv);
2718 return -EINVAL;
2719}
2720
2721static void cm_format_apr(struct cm_apr_msg *apr_msg,
2722 struct cm_id_private *cm_id_priv,
2723 enum ib_cm_apr_status status,
2724 void *info,
2725 u8 info_length,
2726 const void *private_data,
2727 u8 private_data_len)
2728{
2729 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2730 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2731 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2732 apr_msg->ap_status = (u8) status;
2733
2734 if (info && info_length) {
2735 apr_msg->info_length = info_length;
2736 memcpy(apr_msg->info, info, info_length);
2737 }
2738
2739 if (private_data && private_data_len)
2740 memcpy(apr_msg->private_data, private_data, private_data_len);
2741}
2742
2743int ib_send_cm_apr(struct ib_cm_id *cm_id,
2744 enum ib_cm_apr_status status,
2745 void *info,
2746 u8 info_length,
2747 const void *private_data,
2748 u8 private_data_len)
2749{
2750 struct cm_id_private *cm_id_priv;
2751 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002752 unsigned long flags;
2753 int ret;
2754
2755 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2756 (info && info_length > IB_CM_APR_INFO_LENGTH))
2757 return -EINVAL;
2758
2759 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2760 spin_lock_irqsave(&cm_id_priv->lock, flags);
2761 if (cm_id->state != IB_CM_ESTABLISHED ||
2762 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2763 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2764 ret = -EINVAL;
2765 goto out;
2766 }
2767
2768 ret = cm_alloc_msg(cm_id_priv, &msg);
2769 if (ret)
2770 goto out;
2771
2772 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2773 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002774 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002775 if (ret) {
2776 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2777 cm_free_msg(msg);
2778 return ret;
2779 }
2780
2781 cm_id->lap_state = IB_CM_LAP_IDLE;
2782out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2783 return ret;
2784}
2785EXPORT_SYMBOL(ib_send_cm_apr);
2786
2787static int cm_apr_handler(struct cm_work *work)
2788{
2789 struct cm_id_private *cm_id_priv;
2790 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002791 int ret;
2792
2793 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2794 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2795 apr_msg->local_comm_id);
2796 if (!cm_id_priv)
2797 return -EINVAL; /* Unmatched reply. */
2798
2799 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2800 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2801 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2802 work->cm_event.private_data = &apr_msg->private_data;
2803
Sean Hefty24be6e82007-06-18 11:09:36 -07002804 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002805 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2806 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2807 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002808 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002809 goto out;
2810 }
2811 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002812 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002813 cm_id_priv->msg = NULL;
2814
2815 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2816 if (!ret)
2817 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002818 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002819
2820 if (ret)
2821 cm_process_work(cm_id_priv, work);
2822 else
2823 cm_deref_id(cm_id_priv);
2824 return 0;
2825out:
2826 cm_deref_id(cm_id_priv);
2827 return -EINVAL;
2828}
2829
2830static int cm_timewait_handler(struct cm_work *work)
2831{
2832 struct cm_timewait_info *timewait_info;
2833 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002834 int ret;
2835
2836 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07002837 spin_lock_irq(&cm.lock);
2838 list_del(&timewait_info->list);
2839 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002840
2841 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2842 timewait_info->work.remote_id);
2843 if (!cm_id_priv)
2844 return -EINVAL;
2845
Sean Hefty85753292006-10-04 11:29:59 -07002846 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002847 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2848 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07002849 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002850 goto out;
2851 }
2852 cm_id_priv->id.state = IB_CM_IDLE;
2853 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2854 if (!ret)
2855 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07002856 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002857
2858 if (ret)
2859 cm_process_work(cm_id_priv, work);
2860 else
2861 cm_deref_id(cm_id_priv);
2862 return 0;
2863out:
2864 cm_deref_id(cm_id_priv);
2865 return -EINVAL;
2866}
2867
2868static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2869 struct cm_id_private *cm_id_priv,
2870 struct ib_cm_sidr_req_param *param)
2871{
2872 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2873 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2874 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Roland Dreiera88f4882008-04-16 21:01:07 -07002875 sidr_req_msg->pkey = param->path->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002876 sidr_req_msg->service_id = param->service_id;
2877
2878 if (param->private_data && param->private_data_len)
2879 memcpy(sidr_req_msg->private_data, param->private_data,
2880 param->private_data_len);
2881}
2882
2883int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2884 struct ib_cm_sidr_req_param *param)
2885{
2886 struct cm_id_private *cm_id_priv;
2887 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002888 unsigned long flags;
2889 int ret;
2890
2891 if (!param->path || (param->private_data &&
2892 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2893 return -EINVAL;
2894
2895 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2896 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2897 if (ret)
2898 goto out;
2899
2900 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002901 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002902 cm_id_priv->timeout_ms = param->timeout_ms;
2903 cm_id_priv->max_cm_retries = param->max_cm_retries;
2904 ret = cm_alloc_msg(cm_id_priv, &msg);
2905 if (ret)
2906 goto out;
2907
2908 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2909 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07002910 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002911 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2912
2913 spin_lock_irqsave(&cm_id_priv->lock, flags);
2914 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07002915 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002916 else
2917 ret = -EINVAL;
2918
2919 if (ret) {
2920 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2921 cm_free_msg(msg);
2922 goto out;
2923 }
2924 cm_id->state = IB_CM_SIDR_REQ_SENT;
2925 cm_id_priv->msg = msg;
2926 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2927out:
2928 return ret;
2929}
2930EXPORT_SYMBOL(ib_send_cm_sidr_req);
2931
2932static void cm_format_sidr_req_event(struct cm_work *work,
2933 struct ib_cm_id *listen_id)
2934{
2935 struct cm_sidr_req_msg *sidr_req_msg;
2936 struct ib_cm_sidr_req_event_param *param;
2937
2938 sidr_req_msg = (struct cm_sidr_req_msg *)
2939 work->mad_recv_wc->recv_buf.mad;
2940 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002941 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002942 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002943 param->port = work->port->port_num;
2944 work->cm_event.private_data = &sidr_req_msg->private_data;
2945}
2946
2947static int cm_sidr_req_handler(struct cm_work *work)
2948{
2949 struct ib_cm_id *cm_id;
2950 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2951 struct cm_sidr_req_msg *sidr_req_msg;
2952 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002953
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03002954 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002955 if (IS_ERR(cm_id))
2956 return PTR_ERR(cm_id);
2957 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2958
2959 /* Record SGID/SLID and request ID for lookup. */
2960 sidr_req_msg = (struct cm_sidr_req_msg *)
2961 work->mad_recv_wc->recv_buf.mad;
2962 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002963 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002964 cm_id_priv->av.dgid.global.interface_id = 0;
2965 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07002966 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002967 &cm_id_priv->av);
2968 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002969 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2970 atomic_inc(&cm_id_priv->work_count);
2971
Sean Hefty24be6e82007-06-18 11:09:36 -07002972 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002973 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2974 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002975 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07002976 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2977 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002978 goto out; /* Duplicate message. */
2979 }
Sean Hefty29c27312007-07-02 14:36:19 -07002980 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07002981 cur_cm_id_priv = cm_find_listen(cm_id->device,
Sean Hefty6e61d042006-06-17 20:37:28 -07002982 sidr_req_msg->service_id,
2983 sidr_req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002984 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002985 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07002986 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002987 goto out; /* No match. */
2988 }
2989 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07002990 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002991
2992 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2993 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2994 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002995 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002996
2997 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2998 cm_process_work(cm_id_priv, work);
2999 cm_deref_id(cur_cm_id_priv);
3000 return 0;
3001out:
3002 ib_destroy_cm_id(&cm_id_priv->id);
3003 return -EINVAL;
3004}
3005
3006static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3007 struct cm_id_private *cm_id_priv,
3008 struct ib_cm_sidr_rep_param *param)
3009{
3010 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3011 cm_id_priv->tid);
3012 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3013 sidr_rep_msg->status = param->status;
3014 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3015 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3016 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3017
3018 if (param->info && param->info_length)
3019 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3020
3021 if (param->private_data && param->private_data_len)
3022 memcpy(sidr_rep_msg->private_data, param->private_data,
3023 param->private_data_len);
3024}
3025
3026int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3027 struct ib_cm_sidr_rep_param *param)
3028{
3029 struct cm_id_private *cm_id_priv;
3030 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003031 unsigned long flags;
3032 int ret;
3033
3034 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3035 (param->private_data &&
3036 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3037 return -EINVAL;
3038
3039 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3040 spin_lock_irqsave(&cm_id_priv->lock, flags);
3041 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3042 ret = -EINVAL;
3043 goto error;
3044 }
3045
3046 ret = cm_alloc_msg(cm_id_priv, &msg);
3047 if (ret)
3048 goto error;
3049
3050 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3051 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003052 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003053 if (ret) {
3054 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3055 cm_free_msg(msg);
3056 return ret;
3057 }
3058 cm_id->state = IB_CM_IDLE;
3059 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3060
3061 spin_lock_irqsave(&cm.lock, flags);
3062 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3063 spin_unlock_irqrestore(&cm.lock, flags);
3064 return 0;
3065
3066error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3067 return ret;
3068}
3069EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3070
3071static void cm_format_sidr_rep_event(struct cm_work *work)
3072{
3073 struct cm_sidr_rep_msg *sidr_rep_msg;
3074 struct ib_cm_sidr_rep_event_param *param;
3075
3076 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3077 work->mad_recv_wc->recv_buf.mad;
3078 param = &work->cm_event.param.sidr_rep_rcvd;
3079 param->status = sidr_rep_msg->status;
3080 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3081 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3082 param->info = &sidr_rep_msg->info;
3083 param->info_len = sidr_rep_msg->info_length;
3084 work->cm_event.private_data = &sidr_rep_msg->private_data;
3085}
3086
3087static int cm_sidr_rep_handler(struct cm_work *work)
3088{
3089 struct cm_sidr_rep_msg *sidr_rep_msg;
3090 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003091
3092 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3093 work->mad_recv_wc->recv_buf.mad;
3094 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3095 if (!cm_id_priv)
3096 return -EINVAL; /* Unmatched reply. */
3097
Sean Hefty24be6e82007-06-18 11:09:36 -07003098 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003099 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003100 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003101 goto out;
3102 }
3103 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003104 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003105 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003106
3107 cm_format_sidr_rep_event(work);
3108 cm_process_work(cm_id_priv, work);
3109 return 0;
3110out:
3111 cm_deref_id(cm_id_priv);
3112 return -EINVAL;
3113}
3114
3115static void cm_process_send_error(struct ib_mad_send_buf *msg,
3116 enum ib_wc_status wc_status)
3117{
3118 struct cm_id_private *cm_id_priv;
3119 struct ib_cm_event cm_event;
3120 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003121 int ret;
3122
3123 memset(&cm_event, 0, sizeof cm_event);
3124 cm_id_priv = msg->context[0];
3125
3126 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003127 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003128 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3129 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3130 goto discard;
3131
3132 switch (state) {
3133 case IB_CM_REQ_SENT:
3134 case IB_CM_MRA_REQ_RCVD:
3135 cm_reset_to_idle(cm_id_priv);
3136 cm_event.event = IB_CM_REQ_ERROR;
3137 break;
3138 case IB_CM_REP_SENT:
3139 case IB_CM_MRA_REP_RCVD:
3140 cm_reset_to_idle(cm_id_priv);
3141 cm_event.event = IB_CM_REP_ERROR;
3142 break;
3143 case IB_CM_DREQ_SENT:
3144 cm_enter_timewait(cm_id_priv);
3145 cm_event.event = IB_CM_DREQ_ERROR;
3146 break;
3147 case IB_CM_SIDR_REQ_SENT:
3148 cm_id_priv->id.state = IB_CM_IDLE;
3149 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3150 break;
3151 default:
3152 goto discard;
3153 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003154 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003155 cm_event.param.send_status = wc_status;
3156
3157 /* No other events can occur on the cm_id at this point. */
3158 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3159 cm_free_msg(msg);
3160 if (ret)
3161 ib_destroy_cm_id(&cm_id_priv->id);
3162 return;
3163discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003164 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003165 cm_free_msg(msg);
3166}
3167
3168static void cm_send_handler(struct ib_mad_agent *mad_agent,
3169 struct ib_mad_send_wc *mad_send_wc)
3170{
Sean Hefty34816ad2005-10-25 10:51:39 -07003171 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003172 struct cm_port *port;
3173 u16 attr_index;
3174
3175 port = mad_agent->context;
3176 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3177 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3178
3179 /*
3180 * If the send was in response to a received message (context[0] is not
3181 * set to a cm_id), and is not a REJ, then it is a send that was
3182 * manually retried.
3183 */
3184 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3185 msg->retries = 1;
3186
3187 atomic_long_add(1 + msg->retries,
3188 &port->counter_group[CM_XMIT].counter[attr_index]);
3189 if (msg->retries)
3190 atomic_long_add(msg->retries,
3191 &port->counter_group[CM_XMIT_RETRIES].
3192 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003193
3194 switch (mad_send_wc->status) {
3195 case IB_WC_SUCCESS:
3196 case IB_WC_WR_FLUSH_ERR:
3197 cm_free_msg(msg);
3198 break;
3199 default:
3200 if (msg->context[0] && msg->context[1])
3201 cm_process_send_error(msg, mad_send_wc->status);
3202 else
3203 cm_free_msg(msg);
3204 break;
3205 }
3206}
3207
David Howellsc4028952006-11-22 14:57:56 +00003208static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003209{
David Howellsc4028952006-11-22 14:57:56 +00003210 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003211 int ret;
3212
3213 switch (work->cm_event.event) {
3214 case IB_CM_REQ_RECEIVED:
3215 ret = cm_req_handler(work);
3216 break;
3217 case IB_CM_MRA_RECEIVED:
3218 ret = cm_mra_handler(work);
3219 break;
3220 case IB_CM_REJ_RECEIVED:
3221 ret = cm_rej_handler(work);
3222 break;
3223 case IB_CM_REP_RECEIVED:
3224 ret = cm_rep_handler(work);
3225 break;
3226 case IB_CM_RTU_RECEIVED:
3227 ret = cm_rtu_handler(work);
3228 break;
3229 case IB_CM_USER_ESTABLISHED:
3230 ret = cm_establish_handler(work);
3231 break;
3232 case IB_CM_DREQ_RECEIVED:
3233 ret = cm_dreq_handler(work);
3234 break;
3235 case IB_CM_DREP_RECEIVED:
3236 ret = cm_drep_handler(work);
3237 break;
3238 case IB_CM_SIDR_REQ_RECEIVED:
3239 ret = cm_sidr_req_handler(work);
3240 break;
3241 case IB_CM_SIDR_REP_RECEIVED:
3242 ret = cm_sidr_rep_handler(work);
3243 break;
3244 case IB_CM_LAP_RECEIVED:
3245 ret = cm_lap_handler(work);
3246 break;
3247 case IB_CM_APR_RECEIVED:
3248 ret = cm_apr_handler(work);
3249 break;
3250 case IB_CM_TIMEWAIT_EXIT:
3251 ret = cm_timewait_handler(work);
3252 break;
3253 default:
3254 ret = -EINVAL;
3255 break;
3256 }
3257 if (ret)
3258 cm_free_work(work);
3259}
3260
Sean Heftye1444b52006-11-28 14:57:13 -08003261static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003262{
3263 struct cm_id_private *cm_id_priv;
3264 struct cm_work *work;
3265 unsigned long flags;
3266 int ret = 0;
3267
3268 work = kmalloc(sizeof *work, GFP_ATOMIC);
3269 if (!work)
3270 return -ENOMEM;
3271
3272 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3273 spin_lock_irqsave(&cm_id_priv->lock, flags);
3274 switch (cm_id->state)
3275 {
3276 case IB_CM_REP_SENT:
3277 case IB_CM_MRA_REP_RCVD:
3278 cm_id->state = IB_CM_ESTABLISHED;
3279 break;
3280 case IB_CM_ESTABLISHED:
3281 ret = -EISCONN;
3282 break;
3283 default:
3284 ret = -EINVAL;
3285 break;
3286 }
3287 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3288
3289 if (ret) {
3290 kfree(work);
3291 goto out;
3292 }
3293
3294 /*
3295 * The CM worker thread may try to destroy the cm_id before it
3296 * can execute this work item. To prevent potential deadlock,
3297 * we need to find the cm_id once we're in the context of the
3298 * worker thread, rather than holding a reference on it.
3299 */
David Howellsc4028952006-11-22 14:57:56 +00003300 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003301 work->local_id = cm_id->local_id;
3302 work->remote_id = cm_id->remote_id;
3303 work->mad_recv_wc = NULL;
3304 work->cm_event.event = IB_CM_USER_ESTABLISHED;
David Howellsc4028952006-11-22 14:57:56 +00003305 queue_delayed_work(cm.wq, &work->work, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003306out:
3307 return ret;
3308}
Sean Heftye1444b52006-11-28 14:57:13 -08003309
3310static int cm_migrate(struct ib_cm_id *cm_id)
3311{
3312 struct cm_id_private *cm_id_priv;
3313 unsigned long flags;
3314 int ret = 0;
3315
3316 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3317 spin_lock_irqsave(&cm_id_priv->lock, flags);
3318 if (cm_id->state == IB_CM_ESTABLISHED &&
3319 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3320 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3321 cm_id->lap_state = IB_CM_LAP_IDLE;
3322 cm_id_priv->av = cm_id_priv->alt_av;
3323 } else
3324 ret = -EINVAL;
3325 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3326
3327 return ret;
3328}
3329
3330int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3331{
3332 int ret;
3333
3334 switch (event) {
3335 case IB_EVENT_COMM_EST:
3336 ret = cm_establish(cm_id);
3337 break;
3338 case IB_EVENT_PATH_MIG:
3339 ret = cm_migrate(cm_id);
3340 break;
3341 default:
3342 ret = -EINVAL;
3343 }
3344 return ret;
3345}
3346EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003347
3348static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3349 struct ib_mad_recv_wc *mad_recv_wc)
3350{
Sean Hefty9af57b72007-07-16 21:49:35 -07003351 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003352 struct cm_work *work;
3353 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003354 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003355 int paths = 0;
3356
3357 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3358 case CM_REQ_ATTR_ID:
3359 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3360 alt_local_lid != 0);
3361 event = IB_CM_REQ_RECEIVED;
3362 break;
3363 case CM_MRA_ATTR_ID:
3364 event = IB_CM_MRA_RECEIVED;
3365 break;
3366 case CM_REJ_ATTR_ID:
3367 event = IB_CM_REJ_RECEIVED;
3368 break;
3369 case CM_REP_ATTR_ID:
3370 event = IB_CM_REP_RECEIVED;
3371 break;
3372 case CM_RTU_ATTR_ID:
3373 event = IB_CM_RTU_RECEIVED;
3374 break;
3375 case CM_DREQ_ATTR_ID:
3376 event = IB_CM_DREQ_RECEIVED;
3377 break;
3378 case CM_DREP_ATTR_ID:
3379 event = IB_CM_DREP_RECEIVED;
3380 break;
3381 case CM_SIDR_REQ_ATTR_ID:
3382 event = IB_CM_SIDR_REQ_RECEIVED;
3383 break;
3384 case CM_SIDR_REP_ATTR_ID:
3385 event = IB_CM_SIDR_REP_RECEIVED;
3386 break;
3387 case CM_LAP_ATTR_ID:
3388 paths = 1;
3389 event = IB_CM_LAP_RECEIVED;
3390 break;
3391 case CM_APR_ATTR_ID:
3392 event = IB_CM_APR_RECEIVED;
3393 break;
3394 default:
3395 ib_free_recv_mad(mad_recv_wc);
3396 return;
3397 }
3398
Sean Hefty9af57b72007-07-16 21:49:35 -07003399 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3400 atomic_long_inc(&port->counter_group[CM_RECV].
3401 counter[attr_id - CM_ATTR_ID_OFFSET]);
3402
Hal Rosenstocka9770492005-07-27 11:45:40 -07003403 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3404 GFP_KERNEL);
3405 if (!work) {
3406 ib_free_recv_mad(mad_recv_wc);
3407 return;
3408 }
3409
David Howellsc4028952006-11-22 14:57:56 +00003410 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003411 work->cm_event.event = event;
3412 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003413 work->port = port;
David Howellsc4028952006-11-22 14:57:56 +00003414 queue_delayed_work(cm.wq, &work->work, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003415}
3416
3417static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3418 struct ib_qp_attr *qp_attr,
3419 int *qp_attr_mask)
3420{
3421 unsigned long flags;
3422 int ret;
3423
3424 spin_lock_irqsave(&cm_id_priv->lock, flags);
3425 switch (cm_id_priv->id.state) {
3426 case IB_CM_REQ_SENT:
3427 case IB_CM_MRA_REQ_RCVD:
3428 case IB_CM_REQ_RCVD:
3429 case IB_CM_MRA_REQ_SENT:
3430 case IB_CM_REP_RCVD:
3431 case IB_CM_MRA_REP_SENT:
3432 case IB_CM_REP_SENT:
3433 case IB_CM_MRA_REP_RCVD:
3434 case IB_CM_ESTABLISHED:
3435 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3436 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353e2006-10-24 13:35:27 -07003437 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003438 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003439 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3440 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003441 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3442 qp_attr->port_num = cm_id_priv->av.port->port_num;
3443 ret = 0;
3444 break;
3445 default:
3446 ret = -EINVAL;
3447 break;
3448 }
3449 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3450 return ret;
3451}
3452
3453static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3454 struct ib_qp_attr *qp_attr,
3455 int *qp_attr_mask)
3456{
3457 unsigned long flags;
3458 int ret;
3459
3460 spin_lock_irqsave(&cm_id_priv->lock, flags);
3461 switch (cm_id_priv->id.state) {
3462 case IB_CM_REQ_RCVD:
3463 case IB_CM_MRA_REQ_SENT:
3464 case IB_CM_REP_RCVD:
3465 case IB_CM_MRA_REP_SENT:
3466 case IB_CM_REP_SENT:
3467 case IB_CM_MRA_REP_RCVD:
3468 case IB_CM_ESTABLISHED:
3469 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003470 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003471 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3472 qp_attr->path_mtu = cm_id_priv->path_mtu;
3473 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3474 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyae7971a2005-10-24 12:33:56 -07003475 if (cm_id_priv->qp_type == IB_QPT_RC) {
3476 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3477 IB_QP_MIN_RNR_TIMER;
3478 qp_attr->max_dest_rd_atomic =
3479 cm_id_priv->responder_resources;
3480 qp_attr->min_rnr_timer = 0;
3481 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003482 if (cm_id_priv->alt_av.ah_attr.dlid) {
3483 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003484 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003485 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003486 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003487 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3488 }
3489 ret = 0;
3490 break;
3491 default:
3492 ret = -EINVAL;
3493 break;
3494 }
3495 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3496 return ret;
3497}
3498
3499static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3500 struct ib_qp_attr *qp_attr,
3501 int *qp_attr_mask)
3502{
3503 unsigned long flags;
3504 int ret;
3505
3506 spin_lock_irqsave(&cm_id_priv->lock, flags);
3507 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003508 /* Allow transition to RTS before sending REP */
3509 case IB_CM_REQ_RCVD:
3510 case IB_CM_MRA_REQ_SENT:
3511
Hal Rosenstocka9770492005-07-27 11:45:40 -07003512 case IB_CM_REP_RCVD:
3513 case IB_CM_MRA_REP_SENT:
3514 case IB_CM_REP_SENT:
3515 case IB_CM_MRA_REP_RCVD:
3516 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003517 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3518 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3519 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3520 if (cm_id_priv->qp_type == IB_QPT_RC) {
3521 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3522 IB_QP_RNR_RETRY |
3523 IB_QP_MAX_QP_RD_ATOMIC;
Sean Hefty1d846122007-06-18 11:09:37 -07003524 qp_attr->timeout = cm_id_priv->av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003525 qp_attr->retry_cnt = cm_id_priv->retry_count;
3526 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3527 qp_attr->max_rd_atomic =
3528 cm_id_priv->initiator_depth;
3529 }
3530 if (cm_id_priv->alt_av.ah_attr.dlid) {
3531 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3532 qp_attr->path_mig_state = IB_MIG_REARM;
3533 }
3534 } else {
3535 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3536 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3537 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003538 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003539 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003540 qp_attr->path_mig_state = IB_MIG_REARM;
3541 }
3542 ret = 0;
3543 break;
3544 default:
3545 ret = -EINVAL;
3546 break;
3547 }
3548 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3549 return ret;
3550}
3551
3552int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3553 struct ib_qp_attr *qp_attr,
3554 int *qp_attr_mask)
3555{
3556 struct cm_id_private *cm_id_priv;
3557 int ret;
3558
3559 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3560 switch (qp_attr->qp_state) {
3561 case IB_QPS_INIT:
3562 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3563 break;
3564 case IB_QPS_RTR:
3565 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3566 break;
3567 case IB_QPS_RTS:
3568 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3569 break;
3570 default:
3571 ret = -EINVAL;
3572 break;
3573 }
3574 return ret;
3575}
3576EXPORT_SYMBOL(ib_cm_init_qp_attr);
3577
Roland Dreier454a01e2007-07-17 18:37:43 -07003578static void cm_get_ack_delay(struct cm_device *cm_dev)
Sean Hefty1d846122007-06-18 11:09:37 -07003579{
3580 struct ib_device_attr attr;
3581
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003582 if (ib_query_device(cm_dev->ib_device, &attr))
Sean Hefty1d846122007-06-18 11:09:37 -07003583 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3584 else
3585 cm_dev->ack_delay = attr.local_ca_ack_delay;
3586}
3587
Sean Hefty9af57b72007-07-16 21:49:35 -07003588static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3589 char *buf)
3590{
3591 struct cm_counter_group *group;
3592 struct cm_counter_attribute *cm_attr;
3593
3594 group = container_of(obj, struct cm_counter_group, obj);
3595 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3596
3597 return sprintf(buf, "%ld\n",
3598 atomic_long_read(&group->counter[cm_attr->index]));
3599}
3600
3601static struct sysfs_ops cm_counter_ops = {
3602 .show = cm_show_counter
3603};
3604
3605static struct kobj_type cm_counter_obj_type = {
3606 .sysfs_ops = &cm_counter_ops,
3607 .default_attrs = cm_counter_default_attrs
3608};
3609
3610static void cm_release_port_obj(struct kobject *obj)
3611{
3612 struct cm_port *cm_port;
3613
Sean Hefty9af57b72007-07-16 21:49:35 -07003614 cm_port = container_of(obj, struct cm_port, port_obj);
3615 kfree(cm_port);
3616}
3617
3618static struct kobj_type cm_port_obj_type = {
3619 .release = cm_release_port_obj
3620};
3621
Sean Hefty9af57b72007-07-16 21:49:35 -07003622struct class cm_class = {
3623 .name = "infiniband_cm",
3624};
3625EXPORT_SYMBOL(cm_class);
3626
Sean Hefty9af57b72007-07-16 21:49:35 -07003627static int cm_create_port_fs(struct cm_port *port)
3628{
3629 int i, ret;
3630
3631 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003632 &port->cm_dev->device->kobj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003633 "%d", port->port_num);
3634 if (ret) {
3635 kfree(port);
3636 return ret;
3637 }
3638
3639 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3640 ret = kobject_init_and_add(&port->counter_group[i].obj,
3641 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003642 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003643 "%s", counter_group_names[i]);
3644 if (ret)
3645 goto error;
3646 }
3647
3648 return 0;
3649
3650error:
3651 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003652 kobject_put(&port->counter_group[i].obj);
3653 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003654 return ret;
3655
3656}
3657
3658static void cm_remove_port_fs(struct cm_port *port)
3659{
3660 int i;
3661
3662 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003663 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003664
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003665 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003666}
3667
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003668static void cm_add_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003669{
3670 struct cm_device *cm_dev;
3671 struct cm_port *port;
3672 struct ib_mad_reg_req reg_req = {
3673 .mgmt_class = IB_MGMT_CLASS_CM,
3674 .mgmt_class_version = IB_CM_CLASS_VERSION
3675 };
3676 struct ib_port_modify port_modify = {
3677 .set_port_cap_mask = IB_PORT_CM_SUP
3678 };
3679 unsigned long flags;
3680 int ret;
3681 u8 i;
3682
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003683 if (rdma_node_get_transport(ib_device->node_type) != RDMA_TRANSPORT_IB)
Tom Tucker07ebafb2006-08-03 16:02:42 -05003684 return;
3685
Sean Hefty9af57b72007-07-16 21:49:35 -07003686 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003687 ib_device->phys_port_cnt, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003688 if (!cm_dev)
3689 return;
3690
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003691 cm_dev->ib_device = ib_device;
Sean Hefty1d846122007-06-18 11:09:37 -07003692 cm_get_ack_delay(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003693
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07003694 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3695 MKDEV(0, 0), NULL,
3696 "%s", ib_device->name);
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003697 if (!cm_dev->device) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003698 kfree(cm_dev);
3699 return;
3700 }
3701
Hal Rosenstocka9770492005-07-27 11:45:40 -07003702 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003703 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003704 port = kzalloc(sizeof *port, GFP_KERNEL);
3705 if (!port)
3706 goto error1;
3707
3708 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003709 port->cm_dev = cm_dev;
3710 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07003711
3712 ret = cm_create_port_fs(port);
3713 if (ret)
3714 goto error1;
3715
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003716 port->mad_agent = ib_register_mad_agent(ib_device, i,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003717 IB_QPT_GSI,
3718 &reg_req,
3719 0,
3720 cm_send_handler,
3721 cm_recv_handler,
3722 port);
3723 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07003724 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003725
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003726 ret = ib_modify_port(ib_device, i, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003727 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003728 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003729 }
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003730 ib_set_client_data(ib_device, &cm_client, cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003731
3732 write_lock_irqsave(&cm.device_lock, flags);
3733 list_add_tail(&cm_dev->list, &cm.device_list);
3734 write_unlock_irqrestore(&cm.device_lock, flags);
3735 return;
3736
Sean Hefty9af57b72007-07-16 21:49:35 -07003737error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08003738 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003739error2:
3740 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08003741error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003742 port_modify.set_port_cap_mask = 0;
3743 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3744 while (--i) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003745 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003746 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003747 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003748 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003749 }
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003750 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003751 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003752}
3753
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003754static void cm_remove_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003755{
3756 struct cm_device *cm_dev;
3757 struct cm_port *port;
3758 struct ib_port_modify port_modify = {
3759 .clr_port_cap_mask = IB_PORT_CM_SUP
3760 };
3761 unsigned long flags;
3762 int i;
3763
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003764 cm_dev = ib_get_client_data(ib_device, &cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003765 if (!cm_dev)
3766 return;
3767
3768 write_lock_irqsave(&cm.device_lock, flags);
3769 list_del(&cm_dev->list);
3770 write_unlock_irqrestore(&cm.device_lock, flags);
3771
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003772 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003773 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003774 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003775 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty84ba2842008-02-22 10:40:45 -08003776 flush_workqueue(cm.wq);
Sean Hefty9af57b72007-07-16 21:49:35 -07003777 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003778 }
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003779 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003780 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003781}
3782
3783static int __init ib_cm_init(void)
3784{
3785 int ret;
3786
3787 memset(&cm, 0, sizeof cm);
3788 INIT_LIST_HEAD(&cm.device_list);
3789 rwlock_init(&cm.device_lock);
3790 spin_lock_init(&cm.lock);
3791 cm.listen_service_table = RB_ROOT;
3792 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3793 cm.remote_id_table = RB_ROOT;
3794 cm.remote_qp_table = RB_ROOT;
3795 cm.remote_sidr_table = RB_ROOT;
3796 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07003797 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003798 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
Sean Hefty85753292006-10-04 11:29:59 -07003799 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003800
Sean Hefty9af57b72007-07-16 21:49:35 -07003801 ret = class_register(&cm_class);
3802 if (ret)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003803 return -ENOMEM;
3804
Sean Hefty9af57b72007-07-16 21:49:35 -07003805 cm.wq = create_workqueue("ib_cm");
3806 if (!cm.wq) {
3807 ret = -ENOMEM;
3808 goto error1;
3809 }
3810
Hal Rosenstocka9770492005-07-27 11:45:40 -07003811 ret = ib_register_client(&cm_client);
3812 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003813 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003814
3815 return 0;
Sean Hefty9af57b72007-07-16 21:49:35 -07003816error2:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003817 destroy_workqueue(cm.wq);
Sean Hefty9af57b72007-07-16 21:49:35 -07003818error1:
3819 class_unregister(&cm_class);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003820 return ret;
3821}
3822
3823static void __exit ib_cm_cleanup(void)
3824{
Sean Hefty85753292006-10-04 11:29:59 -07003825 struct cm_timewait_info *timewait_info, *tmp;
3826
3827 spin_lock_irq(&cm.lock);
3828 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3829 cancel_delayed_work(&timewait_info->work.work);
3830 spin_unlock_irq(&cm.lock);
3831
Sean Hefty84ba2842008-02-22 10:40:45 -08003832 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003833 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07003834
3835 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3836 list_del(&timewait_info->list);
3837 kfree(timewait_info);
3838 }
3839
Sean Hefty9af57b72007-07-16 21:49:35 -07003840 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07003841 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003842}
3843
3844module_init(ib_cm_init);
3845module_exit(ib_cm_cleanup);
3846