blob: 86fee43502cd2a977030d48fad69810fc7f0ac0f [file] [log] [blame]
Hal Rosenstocka9770492005-07-27 11:45:40 -07001/*
2 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 * $Id: cm.c 2821 2005-07-08 17:07:28Z sean.hefty $
36 */
Sean Hefty1b52fa982006-05-12 14:57:52 -070037
38#include <linux/completion.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070039#include <linux/dma-mapping.h>
40#include <linux/err.h>
41#include <linux/idr.h>
42#include <linux/interrupt.h>
43#include <linux/pci.h>
44#include <linux/rbtree.h>
45#include <linux/spinlock.h>
46#include <linux/workqueue.h>
47
Roland Dreiera4d61e82005-08-25 13:40:04 -070048#include <rdma/ib_cache.h>
49#include <rdma/ib_cm.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070050#include "cm_msgs.h"
51
52MODULE_AUTHOR("Sean Hefty");
53MODULE_DESCRIPTION("InfiniBand CM");
54MODULE_LICENSE("Dual BSD/GPL");
55
56static void cm_add_one(struct ib_device *device);
57static void cm_remove_one(struct ib_device *device);
58
59static struct ib_client cm_client = {
60 .name = "cm",
61 .add = cm_add_one,
62 .remove = cm_remove_one
63};
64
65static struct ib_cm {
66 spinlock_t lock;
67 struct list_head device_list;
68 rwlock_t device_lock;
69 struct rb_root listen_service_table;
70 u64 listen_service_id;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table;
73 struct rb_root remote_id_table;
74 struct rb_root remote_sidr_table;
75 struct idr local_id_table;
76 struct workqueue_struct *wq;
77} cm;
78
79struct cm_port {
80 struct cm_device *cm_dev;
81 struct ib_mad_agent *mad_agent;
82 u8 port_num;
83};
84
85struct cm_device {
86 struct list_head list;
87 struct ib_device *device;
Sean Hefty97f52eb2005-08-13 21:05:57 -070088 __be64 ca_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -070089 struct cm_port port[0];
90};
91
92struct cm_av {
93 struct cm_port *port;
94 union ib_gid dgid;
95 struct ib_ah_attr ah_attr;
96 u16 pkey_index;
97 u8 packet_life_time;
98};
99
100struct cm_work {
101 struct work_struct work;
102 struct list_head list;
103 struct cm_port *port;
104 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700105 __be32 local_id; /* Established / timewait */
106 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700107 struct ib_cm_event cm_event;
108 struct ib_sa_path_rec path[0];
109};
110
111struct cm_timewait_info {
112 struct cm_work work; /* Must be first. */
113 struct rb_node remote_qp_node;
114 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700115 __be64 remote_ca_guid;
116 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700117 u8 inserted_remote_qp;
118 u8 inserted_remote_id;
119};
120
121struct cm_id_private {
122 struct ib_cm_id id;
123
124 struct rb_node service_node;
125 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800126 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700127 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700128 atomic_t refcount;
129
130 struct ib_mad_send_buf *msg;
131 struct cm_timewait_info *timewait_info;
132 /* todo: use alternate port on send failure */
133 struct cm_av av;
134 struct cm_av alt_av;
135
136 void *private_data;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700137 __be64 tid;
138 __be32 local_qpn;
139 __be32 remote_qpn;
Sean Heftyae7971a2005-10-24 12:33:56 -0700140 enum ib_qp_type qp_type;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700141 __be32 sq_psn;
142 __be32 rq_psn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700143 int timeout_ms;
144 enum ib_mtu path_mtu;
145 u8 private_data_len;
146 u8 max_cm_retries;
147 u8 peer_to_peer;
148 u8 responder_resources;
149 u8 initiator_depth;
150 u8 local_ack_timeout;
151 u8 retry_count;
152 u8 rnr_retry_count;
153 u8 service_timeout;
154
155 struct list_head work_list;
156 atomic_t work_count;
157};
158
159static void cm_work_handler(void *data);
160
161static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
162{
163 if (atomic_dec_and_test(&cm_id_priv->refcount))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700164 complete(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700165}
166
167static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
168 struct ib_mad_send_buf **msg)
169{
170 struct ib_mad_agent *mad_agent;
171 struct ib_mad_send_buf *m;
172 struct ib_ah *ah;
173
174 mad_agent = cm_id_priv->av.port->mad_agent;
175 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
176 if (IS_ERR(ah))
177 return PTR_ERR(ah);
178
John Kingman354ba392005-09-09 18:23:32 -0700179 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
180 cm_id_priv->av.pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700181 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700182 GFP_ATOMIC);
183 if (IS_ERR(m)) {
184 ib_destroy_ah(ah);
185 return PTR_ERR(m);
186 }
187
188 /* Timeout set by caller if response is expected. */
Sean Hefty34816ad2005-10-25 10:51:39 -0700189 m->ah = ah;
190 m->retries = cm_id_priv->max_cm_retries;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700191
192 atomic_inc(&cm_id_priv->refcount);
193 m->context[0] = cm_id_priv;
194 *msg = m;
195 return 0;
196}
197
198static int cm_alloc_response_msg(struct cm_port *port,
199 struct ib_mad_recv_wc *mad_recv_wc,
200 struct ib_mad_send_buf **msg)
201{
202 struct ib_mad_send_buf *m;
203 struct ib_ah *ah;
204
205 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
206 mad_recv_wc->recv_buf.grh, port->port_num);
207 if (IS_ERR(ah))
208 return PTR_ERR(ah);
209
210 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700211 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700212 GFP_ATOMIC);
213 if (IS_ERR(m)) {
214 ib_destroy_ah(ah);
215 return PTR_ERR(m);
216 }
Sean Hefty34816ad2005-10-25 10:51:39 -0700217 m->ah = ah;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700218 *msg = m;
219 return 0;
220}
221
222static void cm_free_msg(struct ib_mad_send_buf *msg)
223{
Sean Hefty34816ad2005-10-25 10:51:39 -0700224 ib_destroy_ah(msg->ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700225 if (msg->context[0])
226 cm_deref_id(msg->context[0]);
227 ib_free_send_mad(msg);
228}
229
230static void * cm_copy_private_data(const void *private_data,
231 u8 private_data_len)
232{
233 void *data;
234
235 if (!private_data || !private_data_len)
236 return NULL;
237
238 data = kmalloc(private_data_len, GFP_KERNEL);
239 if (!data)
240 return ERR_PTR(-ENOMEM);
241
242 memcpy(data, private_data, private_data_len);
243 return data;
244}
245
246static void cm_set_private_data(struct cm_id_private *cm_id_priv,
247 void *private_data, u8 private_data_len)
248{
249 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
250 kfree(cm_id_priv->private_data);
251
252 cm_id_priv->private_data = private_data;
253 cm_id_priv->private_data_len = private_data_len;
254}
255
256static void cm_set_ah_attr(struct ib_ah_attr *ah_attr, u8 port_num,
257 u16 dlid, u8 sl, u16 src_path_bits)
258{
259 memset(ah_attr, 0, sizeof ah_attr);
Sean Hefty97f52eb2005-08-13 21:05:57 -0700260 ah_attr->dlid = dlid;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700261 ah_attr->sl = sl;
262 ah_attr->src_path_bits = src_path_bits;
263 ah_attr->port_num = port_num;
264}
265
266static void cm_init_av_for_response(struct cm_port *port,
267 struct ib_wc *wc, struct cm_av *av)
268{
269 av->port = port;
270 av->pkey_index = wc->pkey_index;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700271 cm_set_ah_attr(&av->ah_attr, port->port_num, wc->slid,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700272 wc->sl, wc->dlid_path_bits);
273}
274
275static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
276{
277 struct cm_device *cm_dev;
278 struct cm_port *port = NULL;
279 unsigned long flags;
280 int ret;
281 u8 p;
282
283 read_lock_irqsave(&cm.device_lock, flags);
284 list_for_each_entry(cm_dev, &cm.device_list, list) {
285 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
286 &p, NULL)) {
287 port = &cm_dev->port[p-1];
288 break;
289 }
290 }
291 read_unlock_irqrestore(&cm.device_lock, flags);
292
293 if (!port)
294 return -EINVAL;
295
296 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
297 be16_to_cpu(path->pkey), &av->pkey_index);
298 if (ret)
299 return ret;
300
301 av->port = port;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700302 cm_set_ah_attr(&av->ah_attr, av->port->port_num,
303 be16_to_cpu(path->dlid), path->sl,
304 be16_to_cpu(path->slid) & 0x7F);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700305 av->packet_life_time = path->packet_life_time;
306 return 0;
307}
308
309static int cm_alloc_id(struct cm_id_private *cm_id_priv)
310{
311 unsigned long flags;
312 int ret;
Sean Heftyde1bb1a2005-11-30 10:01:13 -0800313 static int next_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700314
315 do {
316 spin_lock_irqsave(&cm.lock, flags);
Sean Heftyde1bb1a2005-11-30 10:01:13 -0800317 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, next_id++,
Sean Hefty97f52eb2005-08-13 21:05:57 -0700318 (__force int *) &cm_id_priv->id.local_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700319 spin_unlock_irqrestore(&cm.lock, flags);
320 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
321 return ret;
322}
323
Sean Hefty97f52eb2005-08-13 21:05:57 -0700324static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700325{
326 unsigned long flags;
327
328 spin_lock_irqsave(&cm.lock, flags);
Sean Hefty97f52eb2005-08-13 21:05:57 -0700329 idr_remove(&cm.local_id_table, (__force int) local_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700330 spin_unlock_irqrestore(&cm.lock, flags);
331}
332
Sean Hefty97f52eb2005-08-13 21:05:57 -0700333static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700334{
335 struct cm_id_private *cm_id_priv;
336
Sean Hefty97f52eb2005-08-13 21:05:57 -0700337 cm_id_priv = idr_find(&cm.local_id_table, (__force int) local_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700338 if (cm_id_priv) {
339 if (cm_id_priv->id.remote_id == remote_id)
340 atomic_inc(&cm_id_priv->refcount);
341 else
342 cm_id_priv = NULL;
343 }
344
345 return cm_id_priv;
346}
347
Sean Hefty97f52eb2005-08-13 21:05:57 -0700348static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700349{
350 struct cm_id_private *cm_id_priv;
351 unsigned long flags;
352
353 spin_lock_irqsave(&cm.lock, flags);
354 cm_id_priv = cm_get_id(local_id, remote_id);
355 spin_unlock_irqrestore(&cm.lock, flags);
356
357 return cm_id_priv;
358}
359
360static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
361{
362 struct rb_node **link = &cm.listen_service_table.rb_node;
363 struct rb_node *parent = NULL;
364 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700365 __be64 service_id = cm_id_priv->id.service_id;
366 __be64 service_mask = cm_id_priv->id.service_mask;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700367
368 while (*link) {
369 parent = *link;
370 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
371 service_node);
372 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700373 (service_mask & cur_cm_id_priv->id.service_id) &&
374 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
375 return cur_cm_id_priv;
376
377 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
378 link = &(*link)->rb_left;
379 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
380 link = &(*link)->rb_right;
381 else if (service_id < cur_cm_id_priv->id.service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700382 link = &(*link)->rb_left;
383 else
384 link = &(*link)->rb_right;
385 }
386 rb_link_node(&cm_id_priv->service_node, parent, link);
387 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
388 return NULL;
389}
390
Sean Hefty07d357d2005-10-17 15:37:43 -0700391static struct cm_id_private * cm_find_listen(struct ib_device *device,
392 __be64 service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700393{
394 struct rb_node *node = cm.listen_service_table.rb_node;
395 struct cm_id_private *cm_id_priv;
396
397 while (node) {
398 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
399 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700400 cm_id_priv->id.service_id &&
401 (cm_id_priv->id.device == device))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700402 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700403
404 if (device < cm_id_priv->id.device)
405 node = node->rb_left;
406 else if (device > cm_id_priv->id.device)
407 node = node->rb_right;
408 else if (service_id < cm_id_priv->id.service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700409 node = node->rb_left;
410 else
411 node = node->rb_right;
412 }
413 return NULL;
414}
415
416static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
417 *timewait_info)
418{
419 struct rb_node **link = &cm.remote_id_table.rb_node;
420 struct rb_node *parent = NULL;
421 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700422 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
423 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700424
425 while (*link) {
426 parent = *link;
427 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
428 remote_id_node);
429 if (remote_id < cur_timewait_info->work.remote_id)
430 link = &(*link)->rb_left;
431 else if (remote_id > cur_timewait_info->work.remote_id)
432 link = &(*link)->rb_right;
433 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
434 link = &(*link)->rb_left;
435 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
436 link = &(*link)->rb_right;
437 else
438 return cur_timewait_info;
439 }
440 timewait_info->inserted_remote_id = 1;
441 rb_link_node(&timewait_info->remote_id_node, parent, link);
442 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
443 return NULL;
444}
445
Sean Hefty97f52eb2005-08-13 21:05:57 -0700446static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
447 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700448{
449 struct rb_node *node = cm.remote_id_table.rb_node;
450 struct cm_timewait_info *timewait_info;
451
452 while (node) {
453 timewait_info = rb_entry(node, struct cm_timewait_info,
454 remote_id_node);
455 if (remote_id < timewait_info->work.remote_id)
456 node = node->rb_left;
457 else if (remote_id > timewait_info->work.remote_id)
458 node = node->rb_right;
459 else if (remote_ca_guid < timewait_info->remote_ca_guid)
460 node = node->rb_left;
461 else if (remote_ca_guid > timewait_info->remote_ca_guid)
462 node = node->rb_right;
463 else
464 return timewait_info;
465 }
466 return NULL;
467}
468
469static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
470 *timewait_info)
471{
472 struct rb_node **link = &cm.remote_qp_table.rb_node;
473 struct rb_node *parent = NULL;
474 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700475 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
476 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700477
478 while (*link) {
479 parent = *link;
480 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
481 remote_qp_node);
482 if (remote_qpn < cur_timewait_info->remote_qpn)
483 link = &(*link)->rb_left;
484 else if (remote_qpn > cur_timewait_info->remote_qpn)
485 link = &(*link)->rb_right;
486 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
487 link = &(*link)->rb_left;
488 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
489 link = &(*link)->rb_right;
490 else
491 return cur_timewait_info;
492 }
493 timewait_info->inserted_remote_qp = 1;
494 rb_link_node(&timewait_info->remote_qp_node, parent, link);
495 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
496 return NULL;
497}
498
499static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
500 *cm_id_priv)
501{
502 struct rb_node **link = &cm.remote_sidr_table.rb_node;
503 struct rb_node *parent = NULL;
504 struct cm_id_private *cur_cm_id_priv;
505 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700506 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700507
508 while (*link) {
509 parent = *link;
510 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
511 sidr_id_node);
512 if (remote_id < cur_cm_id_priv->id.remote_id)
513 link = &(*link)->rb_left;
514 else if (remote_id > cur_cm_id_priv->id.remote_id)
515 link = &(*link)->rb_right;
516 else {
517 int cmp;
518 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
519 sizeof *port_gid);
520 if (cmp < 0)
521 link = &(*link)->rb_left;
522 else if (cmp > 0)
523 link = &(*link)->rb_right;
524 else
525 return cur_cm_id_priv;
526 }
527 }
528 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
529 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
530 return NULL;
531}
532
533static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
534 enum ib_cm_sidr_status status)
535{
536 struct ib_cm_sidr_rep_param param;
537
538 memset(&param, 0, sizeof param);
539 param.status = status;
540 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
541}
542
Sean Hefty07d357d2005-10-17 15:37:43 -0700543struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
544 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700545 void *context)
546{
547 struct cm_id_private *cm_id_priv;
548 int ret;
549
Roland Dreierde6eb662005-11-02 07:23:14 -0800550 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700551 if (!cm_id_priv)
552 return ERR_PTR(-ENOMEM);
553
Hal Rosenstocka9770492005-07-27 11:45:40 -0700554 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700555 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700556 cm_id_priv->id.cm_handler = cm_handler;
557 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700558 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700559 ret = cm_alloc_id(cm_id_priv);
560 if (ret)
561 goto error;
562
563 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700564 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700565 INIT_LIST_HEAD(&cm_id_priv->work_list);
566 atomic_set(&cm_id_priv->work_count, -1);
567 atomic_set(&cm_id_priv->refcount, 1);
568 return &cm_id_priv->id;
569
570error:
571 kfree(cm_id_priv);
572 return ERR_PTR(-ENOMEM);
573}
574EXPORT_SYMBOL(ib_create_cm_id);
575
576static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
577{
578 struct cm_work *work;
579
580 if (list_empty(&cm_id_priv->work_list))
581 return NULL;
582
583 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
584 list_del(&work->list);
585 return work;
586}
587
588static void cm_free_work(struct cm_work *work)
589{
590 if (work->mad_recv_wc)
591 ib_free_recv_mad(work->mad_recv_wc);
592 kfree(work);
593}
594
595static inline int cm_convert_to_ms(int iba_time)
596{
597 /* approximate conversion to ms from 4.096us x 2^iba_time */
598 return 1 << max(iba_time - 8, 0);
599}
600
601static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
602{
603 unsigned long flags;
604
605 if (!timewait_info->inserted_remote_id &&
606 !timewait_info->inserted_remote_qp)
607 return;
608
609 spin_lock_irqsave(&cm.lock, flags);
610 if (timewait_info->inserted_remote_id) {
611 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
612 timewait_info->inserted_remote_id = 0;
613 }
614
615 if (timewait_info->inserted_remote_qp) {
616 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
617 timewait_info->inserted_remote_qp = 0;
618 }
619 spin_unlock_irqrestore(&cm.lock, flags);
620}
621
Sean Hefty97f52eb2005-08-13 21:05:57 -0700622static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700623{
624 struct cm_timewait_info *timewait_info;
625
Roland Dreierde6eb662005-11-02 07:23:14 -0800626 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700627 if (!timewait_info)
628 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700629
630 timewait_info->work.local_id = local_id;
631 INIT_WORK(&timewait_info->work.work, cm_work_handler,
632 &timewait_info->work);
633 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
634 return timewait_info;
635}
636
637static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
638{
639 int wait_time;
640
641 /*
642 * The cm_id could be destroyed by the user before we exit timewait.
643 * To protect against this, we search for the cm_id after exiting
644 * timewait before notifying the user that we've exited timewait.
645 */
646 cm_id_priv->id.state = IB_CM_TIMEWAIT;
647 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
648 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
649 msecs_to_jiffies(wait_time));
650 cm_id_priv->timewait_info = NULL;
651}
652
653static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
654{
655 cm_id_priv->id.state = IB_CM_IDLE;
656 if (cm_id_priv->timewait_info) {
657 cm_cleanup_timewait(cm_id_priv->timewait_info);
658 kfree(cm_id_priv->timewait_info);
659 cm_id_priv->timewait_info = NULL;
660 }
661}
662
663void ib_destroy_cm_id(struct ib_cm_id *cm_id)
664{
665 struct cm_id_private *cm_id_priv;
666 struct cm_work *work;
667 unsigned long flags;
668
669 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
670retest:
671 spin_lock_irqsave(&cm_id_priv->lock, flags);
672 switch (cm_id->state) {
673 case IB_CM_LISTEN:
674 cm_id->state = IB_CM_IDLE;
675 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
676 spin_lock_irqsave(&cm.lock, flags);
677 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
678 spin_unlock_irqrestore(&cm.lock, flags);
679 break;
680 case IB_CM_SIDR_REQ_SENT:
681 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700682 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700683 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
684 break;
685 case IB_CM_SIDR_REQ_RCVD:
686 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
687 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
688 break;
689 case IB_CM_REQ_SENT:
Sean Hefty227eca82005-11-30 10:00:25 -0800690 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
691 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
692 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
693 &cm_id_priv->av.port->cm_dev->ca_guid,
694 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
695 NULL, 0);
696 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700697 case IB_CM_MRA_REQ_RCVD:
698 case IB_CM_REP_SENT:
699 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700700 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700701 /* Fall through */
702 case IB_CM_REQ_RCVD:
703 case IB_CM_MRA_REQ_SENT:
704 case IB_CM_REP_RCVD:
705 case IB_CM_MRA_REP_SENT:
706 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
Sean Hefty227eca82005-11-30 10:00:25 -0800707 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
708 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700709 break;
710 case IB_CM_ESTABLISHED:
711 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
712 ib_send_cm_dreq(cm_id, NULL, 0);
713 goto retest;
714 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700715 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700716 cm_enter_timewait(cm_id_priv);
717 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
718 break;
719 case IB_CM_DREQ_RCVD:
720 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
721 ib_send_cm_drep(cm_id, NULL, 0);
722 break;
723 default:
724 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
725 break;
726 }
727
728 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700729 cm_deref_id(cm_id_priv);
730 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700731 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
732 cm_free_work(work);
733 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
734 kfree(cm_id_priv->private_data);
735 kfree(cm_id_priv);
736}
737EXPORT_SYMBOL(ib_destroy_cm_id);
738
739int ib_cm_listen(struct ib_cm_id *cm_id,
Sean Hefty97f52eb2005-08-13 21:05:57 -0700740 __be64 service_id,
741 __be64 service_mask)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700742{
743 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
744 unsigned long flags;
745 int ret = 0;
746
Sean Hefty97f52eb2005-08-13 21:05:57 -0700747 service_mask = service_mask ? service_mask :
748 __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700749 service_id &= service_mask;
750 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
751 (service_id != IB_CM_ASSIGN_SERVICE_ID))
752 return -EINVAL;
753
754 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
755 BUG_ON(cm_id->state != IB_CM_IDLE);
756
757 cm_id->state = IB_CM_LISTEN;
758
759 spin_lock_irqsave(&cm.lock, flags);
760 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700761 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
762 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700763 } else {
764 cm_id->service_id = service_id;
765 cm_id->service_mask = service_mask;
766 }
767 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
768 spin_unlock_irqrestore(&cm.lock, flags);
769
770 if (cur_cm_id_priv) {
771 cm_id->state = IB_CM_IDLE;
772 ret = -EBUSY;
773 }
774 return ret;
775}
776EXPORT_SYMBOL(ib_cm_listen);
777
Sean Hefty97f52eb2005-08-13 21:05:57 -0700778static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
779 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700780{
781 u64 hi_tid, low_tid;
782
783 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700784 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
785 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700786 return cpu_to_be64(hi_tid | low_tid);
787}
788
789static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -0700790 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700791{
792 hdr->base_version = IB_MGMT_BASE_VERSION;
793 hdr->mgmt_class = IB_MGMT_CLASS_CM;
794 hdr->class_version = IB_CM_CLASS_VERSION;
795 hdr->method = IB_MGMT_METHOD_SEND;
796 hdr->attr_id = attr_id;
797 hdr->tid = tid;
798}
799
800static void cm_format_req(struct cm_req_msg *req_msg,
801 struct cm_id_private *cm_id_priv,
802 struct ib_cm_req_param *param)
803{
804 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
805 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
806
807 req_msg->local_comm_id = cm_id_priv->id.local_id;
808 req_msg->service_id = param->service_id;
809 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
810 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
811 cm_req_set_resp_res(req_msg, param->responder_resources);
812 cm_req_set_init_depth(req_msg, param->initiator_depth);
813 cm_req_set_remote_resp_timeout(req_msg,
814 param->remote_cm_response_timeout);
815 cm_req_set_qp_type(req_msg, param->qp_type);
816 cm_req_set_flow_ctrl(req_msg, param->flow_control);
817 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
818 cm_req_set_local_resp_timeout(req_msg,
819 param->local_cm_response_timeout);
820 cm_req_set_retry_count(req_msg, param->retry_count);
821 req_msg->pkey = param->primary_path->pkey;
822 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
823 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
824 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
825 cm_req_set_srq(req_msg, param->srq);
826
827 req_msg->primary_local_lid = param->primary_path->slid;
828 req_msg->primary_remote_lid = param->primary_path->dlid;
829 req_msg->primary_local_gid = param->primary_path->sgid;
830 req_msg->primary_remote_gid = param->primary_path->dgid;
831 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
832 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
833 req_msg->primary_traffic_class = param->primary_path->traffic_class;
834 req_msg->primary_hop_limit = param->primary_path->hop_limit;
835 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
836 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
837 cm_req_set_primary_local_ack_timeout(req_msg,
838 min(31, param->primary_path->packet_life_time + 1));
839
840 if (param->alternate_path) {
841 req_msg->alt_local_lid = param->alternate_path->slid;
842 req_msg->alt_remote_lid = param->alternate_path->dlid;
843 req_msg->alt_local_gid = param->alternate_path->sgid;
844 req_msg->alt_remote_gid = param->alternate_path->dgid;
845 cm_req_set_alt_flow_label(req_msg,
846 param->alternate_path->flow_label);
847 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
848 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
849 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
850 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
851 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
852 cm_req_set_alt_local_ack_timeout(req_msg,
853 min(31, param->alternate_path->packet_life_time + 1));
854 }
855
856 if (param->private_data && param->private_data_len)
857 memcpy(req_msg->private_data, param->private_data,
858 param->private_data_len);
859}
860
Arjan van de Ven858119e2006-01-14 13:20:43 -0800861static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700862{
863 /* peer-to-peer not supported */
864 if (param->peer_to_peer)
865 return -EINVAL;
866
867 if (!param->primary_path)
868 return -EINVAL;
869
870 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
871 return -EINVAL;
872
873 if (param->private_data &&
874 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
875 return -EINVAL;
876
877 if (param->alternate_path &&
878 (param->alternate_path->pkey != param->primary_path->pkey ||
879 param->alternate_path->mtu != param->primary_path->mtu))
880 return -EINVAL;
881
882 return 0;
883}
884
885int ib_send_cm_req(struct ib_cm_id *cm_id,
886 struct ib_cm_req_param *param)
887{
888 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700889 struct cm_req_msg *req_msg;
890 unsigned long flags;
891 int ret;
892
893 ret = cm_validate_req_param(param);
894 if (ret)
895 return ret;
896
897 /* Verify that we're not in timewait. */
898 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
899 spin_lock_irqsave(&cm_id_priv->lock, flags);
900 if (cm_id->state != IB_CM_IDLE) {
901 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
902 ret = -EINVAL;
903 goto out;
904 }
905 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
906
907 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
908 id.local_id);
909 if (IS_ERR(cm_id_priv->timewait_info))
910 goto out;
911
912 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
913 if (ret)
914 goto error1;
915 if (param->alternate_path) {
916 ret = cm_init_av_by_path(param->alternate_path,
917 &cm_id_priv->alt_av);
918 if (ret)
919 goto error1;
920 }
921 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700922 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700923 cm_id_priv->timeout_ms = cm_convert_to_ms(
924 param->primary_path->packet_life_time) * 2 +
925 cm_convert_to_ms(
926 param->remote_cm_response_timeout);
927 cm_id_priv->max_cm_retries = param->max_cm_retries;
928 cm_id_priv->initiator_depth = param->initiator_depth;
929 cm_id_priv->responder_resources = param->responder_resources;
930 cm_id_priv->retry_count = param->retry_count;
931 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftyae7971a2005-10-24 12:33:56 -0700932 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700933
934 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
935 if (ret)
936 goto error1;
937
938 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
939 cm_format_req(req_msg, cm_id_priv, param);
940 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -0700941 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700942 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
943
944 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
945 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
946 cm_id_priv->local_ack_timeout =
947 cm_req_get_primary_local_ack_timeout(req_msg);
948
949 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700950 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700951 if (ret) {
952 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
953 goto error2;
954 }
955 BUG_ON(cm_id->state != IB_CM_IDLE);
956 cm_id->state = IB_CM_REQ_SENT;
957 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
958 return 0;
959
960error2: cm_free_msg(cm_id_priv->msg);
961error1: kfree(cm_id_priv->timewait_info);
962out: return ret;
963}
964EXPORT_SYMBOL(ib_send_cm_req);
965
966static int cm_issue_rej(struct cm_port *port,
967 struct ib_mad_recv_wc *mad_recv_wc,
968 enum ib_cm_rej_reason reason,
969 enum cm_msg_response msg_rejected,
970 void *ari, u8 ari_length)
971{
972 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700973 struct cm_rej_msg *rej_msg, *rcv_msg;
974 int ret;
975
976 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
977 if (ret)
978 return ret;
979
980 /* We just need common CM header information. Cast to any message. */
981 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
982 rej_msg = (struct cm_rej_msg *) msg->mad;
983
984 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
985 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
986 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
987 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -0700988 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700989
990 if (ari && ari_length) {
991 cm_rej_set_reject_info_len(rej_msg, ari_length);
992 memcpy(rej_msg->ari, ari, ari_length);
993 }
994
Sean Hefty34816ad2005-10-25 10:51:39 -0700995 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700996 if (ret)
997 cm_free_msg(msg);
998
999 return ret;
1000}
1001
Sean Hefty97f52eb2005-08-13 21:05:57 -07001002static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1003 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001004{
1005 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1006 ((local_ca_guid == remote_ca_guid) &&
1007 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1008}
1009
Arjan van de Ven858119e2006-01-14 13:20:43 -08001010static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001011 struct ib_sa_path_rec *primary_path,
1012 struct ib_sa_path_rec *alt_path)
1013{
1014 memset(primary_path, 0, sizeof *primary_path);
1015 primary_path->dgid = req_msg->primary_local_gid;
1016 primary_path->sgid = req_msg->primary_remote_gid;
1017 primary_path->dlid = req_msg->primary_local_lid;
1018 primary_path->slid = req_msg->primary_remote_lid;
1019 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1020 primary_path->hop_limit = req_msg->primary_hop_limit;
1021 primary_path->traffic_class = req_msg->primary_traffic_class;
1022 primary_path->reversible = 1;
1023 primary_path->pkey = req_msg->pkey;
1024 primary_path->sl = cm_req_get_primary_sl(req_msg);
1025 primary_path->mtu_selector = IB_SA_EQ;
1026 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1027 primary_path->rate_selector = IB_SA_EQ;
1028 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1029 primary_path->packet_life_time_selector = IB_SA_EQ;
1030 primary_path->packet_life_time =
1031 cm_req_get_primary_local_ack_timeout(req_msg);
1032 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1033
1034 if (req_msg->alt_local_lid) {
1035 memset(alt_path, 0, sizeof *alt_path);
1036 alt_path->dgid = req_msg->alt_local_gid;
1037 alt_path->sgid = req_msg->alt_remote_gid;
1038 alt_path->dlid = req_msg->alt_local_lid;
1039 alt_path->slid = req_msg->alt_remote_lid;
1040 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1041 alt_path->hop_limit = req_msg->alt_hop_limit;
1042 alt_path->traffic_class = req_msg->alt_traffic_class;
1043 alt_path->reversible = 1;
1044 alt_path->pkey = req_msg->pkey;
1045 alt_path->sl = cm_req_get_alt_sl(req_msg);
1046 alt_path->mtu_selector = IB_SA_EQ;
1047 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1048 alt_path->rate_selector = IB_SA_EQ;
1049 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1050 alt_path->packet_life_time_selector = IB_SA_EQ;
1051 alt_path->packet_life_time =
1052 cm_req_get_alt_local_ack_timeout(req_msg);
1053 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1054 }
1055}
1056
1057static void cm_format_req_event(struct cm_work *work,
1058 struct cm_id_private *cm_id_priv,
1059 struct ib_cm_id *listen_id)
1060{
1061 struct cm_req_msg *req_msg;
1062 struct ib_cm_req_event_param *param;
1063
1064 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1065 param = &work->cm_event.param.req_rcvd;
1066 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001067 param->port = cm_id_priv->av.port->port_num;
1068 param->primary_path = &work->path[0];
1069 if (req_msg->alt_local_lid)
1070 param->alternate_path = &work->path[1];
1071 else
1072 param->alternate_path = NULL;
1073 param->remote_ca_guid = req_msg->local_ca_guid;
1074 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1075 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1076 param->qp_type = cm_req_get_qp_type(req_msg);
1077 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1078 param->responder_resources = cm_req_get_init_depth(req_msg);
1079 param->initiator_depth = cm_req_get_resp_res(req_msg);
1080 param->local_cm_response_timeout =
1081 cm_req_get_remote_resp_timeout(req_msg);
1082 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1083 param->remote_cm_response_timeout =
1084 cm_req_get_local_resp_timeout(req_msg);
1085 param->retry_count = cm_req_get_retry_count(req_msg);
1086 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1087 param->srq = cm_req_get_srq(req_msg);
1088 work->cm_event.private_data = &req_msg->private_data;
1089}
1090
1091static void cm_process_work(struct cm_id_private *cm_id_priv,
1092 struct cm_work *work)
1093{
1094 unsigned long flags;
1095 int ret;
1096
1097 /* We will typically only have the current event to report. */
1098 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1099 cm_free_work(work);
1100
1101 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1102 spin_lock_irqsave(&cm_id_priv->lock, flags);
1103 work = cm_dequeue_work(cm_id_priv);
1104 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1105 BUG_ON(!work);
1106 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1107 &work->cm_event);
1108 cm_free_work(work);
1109 }
1110 cm_deref_id(cm_id_priv);
1111 if (ret)
1112 ib_destroy_cm_id(&cm_id_priv->id);
1113}
1114
1115static void cm_format_mra(struct cm_mra_msg *mra_msg,
1116 struct cm_id_private *cm_id_priv,
1117 enum cm_msg_response msg_mraed, u8 service_timeout,
1118 const void *private_data, u8 private_data_len)
1119{
1120 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1121 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1122 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1123 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1124 cm_mra_set_service_timeout(mra_msg, service_timeout);
1125
1126 if (private_data && private_data_len)
1127 memcpy(mra_msg->private_data, private_data, private_data_len);
1128}
1129
1130static void cm_format_rej(struct cm_rej_msg *rej_msg,
1131 struct cm_id_private *cm_id_priv,
1132 enum ib_cm_rej_reason reason,
1133 void *ari,
1134 u8 ari_length,
1135 const void *private_data,
1136 u8 private_data_len)
1137{
1138 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1139 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1140
1141 switch(cm_id_priv->id.state) {
1142 case IB_CM_REQ_RCVD:
1143 rej_msg->local_comm_id = 0;
1144 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1145 break;
1146 case IB_CM_MRA_REQ_SENT:
1147 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1148 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1149 break;
1150 case IB_CM_REP_RCVD:
1151 case IB_CM_MRA_REP_SENT:
1152 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1153 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1154 break;
1155 default:
1156 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1157 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1158 break;
1159 }
1160
Sean Hefty97f52eb2005-08-13 21:05:57 -07001161 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001162 if (ari && ari_length) {
1163 cm_rej_set_reject_info_len(rej_msg, ari_length);
1164 memcpy(rej_msg->ari, ari, ari_length);
1165 }
1166
1167 if (private_data && private_data_len)
1168 memcpy(rej_msg->private_data, private_data, private_data_len);
1169}
1170
1171static void cm_dup_req_handler(struct cm_work *work,
1172 struct cm_id_private *cm_id_priv)
1173{
1174 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001175 unsigned long flags;
1176 int ret;
1177
1178 /* Quick state check to discard duplicate REQs. */
1179 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1180 return;
1181
1182 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1183 if (ret)
1184 return;
1185
1186 spin_lock_irqsave(&cm_id_priv->lock, flags);
1187 switch (cm_id_priv->id.state) {
1188 case IB_CM_MRA_REQ_SENT:
1189 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1190 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1191 cm_id_priv->private_data,
1192 cm_id_priv->private_data_len);
1193 break;
1194 case IB_CM_TIMEWAIT:
1195 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1196 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1197 break;
1198 default:
1199 goto unlock;
1200 }
1201 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1202
Sean Hefty34816ad2005-10-25 10:51:39 -07001203 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001204 if (ret)
1205 goto free;
1206 return;
1207
1208unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1209free: cm_free_msg(msg);
1210}
1211
1212static struct cm_id_private * cm_match_req(struct cm_work *work,
1213 struct cm_id_private *cm_id_priv)
1214{
1215 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1216 struct cm_timewait_info *timewait_info;
1217 struct cm_req_msg *req_msg;
1218 unsigned long flags;
1219
1220 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1221
1222 /* Check for duplicate REQ and stale connections. */
1223 spin_lock_irqsave(&cm.lock, flags);
1224 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1225 if (!timewait_info)
1226 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1227
1228 if (timewait_info) {
1229 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1230 timewait_info->work.remote_id);
1231 spin_unlock_irqrestore(&cm.lock, flags);
1232 if (cur_cm_id_priv) {
1233 cm_dup_req_handler(work, cur_cm_id_priv);
1234 cm_deref_id(cur_cm_id_priv);
1235 } else
1236 cm_issue_rej(work->port, work->mad_recv_wc,
1237 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1238 NULL, 0);
1239 goto error;
1240 }
1241
1242 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001243 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1244 req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001245 if (!listen_cm_id_priv) {
1246 spin_unlock_irqrestore(&cm.lock, flags);
1247 cm_issue_rej(work->port, work->mad_recv_wc,
1248 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1249 NULL, 0);
1250 goto error;
1251 }
1252 atomic_inc(&listen_cm_id_priv->refcount);
1253 atomic_inc(&cm_id_priv->refcount);
1254 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1255 atomic_inc(&cm_id_priv->work_count);
1256 spin_unlock_irqrestore(&cm.lock, flags);
1257 return listen_cm_id_priv;
1258
1259error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1260 return NULL;
1261}
1262
1263static int cm_req_handler(struct cm_work *work)
1264{
1265 struct ib_cm_id *cm_id;
1266 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1267 struct cm_req_msg *req_msg;
1268 int ret;
1269
1270 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1271
Sean Hefty07d357d2005-10-17 15:37:43 -07001272 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001273 if (IS_ERR(cm_id))
1274 return PTR_ERR(cm_id);
1275
1276 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1277 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1278 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1279 &cm_id_priv->av);
1280 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1281 id.local_id);
1282 if (IS_ERR(cm_id_priv->timewait_info)) {
1283 ret = PTR_ERR(cm_id_priv->timewait_info);
1284 goto error1;
1285 }
1286 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1287 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1288 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1289
1290 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1291 if (!listen_cm_id_priv) {
1292 ret = -EINVAL;
1293 goto error2;
1294 }
1295
1296 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1297 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1298 cm_id_priv->id.service_id = req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001299 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001300
1301 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1302 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1303 if (ret)
1304 goto error3;
1305 if (req_msg->alt_local_lid) {
1306 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1307 if (ret)
1308 goto error3;
1309 }
1310 cm_id_priv->tid = req_msg->hdr.tid;
1311 cm_id_priv->timeout_ms = cm_convert_to_ms(
1312 cm_req_get_local_resp_timeout(req_msg));
1313 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1314 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1315 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1316 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1317 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1318 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1319 cm_id_priv->local_ack_timeout =
1320 cm_req_get_primary_local_ack_timeout(req_msg);
1321 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1322 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001323 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001324
1325 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1326 cm_process_work(cm_id_priv, work);
1327 cm_deref_id(listen_cm_id_priv);
1328 return 0;
1329
1330error3: atomic_dec(&cm_id_priv->refcount);
1331 cm_deref_id(listen_cm_id_priv);
1332 cm_cleanup_timewait(cm_id_priv->timewait_info);
1333error2: kfree(cm_id_priv->timewait_info);
Roland Dreier1b205c22005-09-09 20:52:00 -07001334 cm_id_priv->timewait_info = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001335error1: ib_destroy_cm_id(&cm_id_priv->id);
1336 return ret;
1337}
1338
1339static void cm_format_rep(struct cm_rep_msg *rep_msg,
1340 struct cm_id_private *cm_id_priv,
1341 struct ib_cm_rep_param *param)
1342{
1343 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1344 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1345 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1346 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1347 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1348 rep_msg->resp_resources = param->responder_resources;
1349 rep_msg->initiator_depth = param->initiator_depth;
1350 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1351 cm_rep_set_failover(rep_msg, param->failover_accepted);
1352 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1353 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1354 cm_rep_set_srq(rep_msg, param->srq);
1355 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1356
1357 if (param->private_data && param->private_data_len)
1358 memcpy(rep_msg->private_data, param->private_data,
1359 param->private_data_len);
1360}
1361
1362int ib_send_cm_rep(struct ib_cm_id *cm_id,
1363 struct ib_cm_rep_param *param)
1364{
1365 struct cm_id_private *cm_id_priv;
1366 struct ib_mad_send_buf *msg;
1367 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001368 unsigned long flags;
1369 int ret;
1370
1371 if (param->private_data &&
1372 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1373 return -EINVAL;
1374
1375 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1376 spin_lock_irqsave(&cm_id_priv->lock, flags);
1377 if (cm_id->state != IB_CM_REQ_RCVD &&
1378 cm_id->state != IB_CM_MRA_REQ_SENT) {
1379 ret = -EINVAL;
1380 goto out;
1381 }
1382
1383 ret = cm_alloc_msg(cm_id_priv, &msg);
1384 if (ret)
1385 goto out;
1386
1387 rep_msg = (struct cm_rep_msg *) msg->mad;
1388 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001389 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001390 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1391
Sean Hefty34816ad2005-10-25 10:51:39 -07001392 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001393 if (ret) {
1394 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1395 cm_free_msg(msg);
1396 return ret;
1397 }
1398
1399 cm_id->state = IB_CM_REP_SENT;
1400 cm_id_priv->msg = msg;
1401 cm_id_priv->initiator_depth = param->initiator_depth;
1402 cm_id_priv->responder_resources = param->responder_resources;
1403 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1404 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1405
1406out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1407 return ret;
1408}
1409EXPORT_SYMBOL(ib_send_cm_rep);
1410
1411static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1412 struct cm_id_private *cm_id_priv,
1413 const void *private_data,
1414 u8 private_data_len)
1415{
1416 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1417 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1418 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1419
1420 if (private_data && private_data_len)
1421 memcpy(rtu_msg->private_data, private_data, private_data_len);
1422}
1423
1424int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1425 const void *private_data,
1426 u8 private_data_len)
1427{
1428 struct cm_id_private *cm_id_priv;
1429 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001430 unsigned long flags;
1431 void *data;
1432 int ret;
1433
1434 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1435 return -EINVAL;
1436
1437 data = cm_copy_private_data(private_data, private_data_len);
1438 if (IS_ERR(data))
1439 return PTR_ERR(data);
1440
1441 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1442 spin_lock_irqsave(&cm_id_priv->lock, flags);
1443 if (cm_id->state != IB_CM_REP_RCVD &&
1444 cm_id->state != IB_CM_MRA_REP_SENT) {
1445 ret = -EINVAL;
1446 goto error;
1447 }
1448
1449 ret = cm_alloc_msg(cm_id_priv, &msg);
1450 if (ret)
1451 goto error;
1452
1453 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1454 private_data, private_data_len);
1455
Sean Hefty34816ad2005-10-25 10:51:39 -07001456 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001457 if (ret) {
1458 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1459 cm_free_msg(msg);
1460 kfree(data);
1461 return ret;
1462 }
1463
1464 cm_id->state = IB_CM_ESTABLISHED;
1465 cm_set_private_data(cm_id_priv, data, private_data_len);
1466 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1467 return 0;
1468
1469error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1470 kfree(data);
1471 return ret;
1472}
1473EXPORT_SYMBOL(ib_send_cm_rtu);
1474
1475static void cm_format_rep_event(struct cm_work *work)
1476{
1477 struct cm_rep_msg *rep_msg;
1478 struct ib_cm_rep_event_param *param;
1479
1480 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1481 param = &work->cm_event.param.rep_rcvd;
1482 param->remote_ca_guid = rep_msg->local_ca_guid;
1483 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1484 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1485 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1486 param->responder_resources = rep_msg->initiator_depth;
1487 param->initiator_depth = rep_msg->resp_resources;
1488 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1489 param->failover_accepted = cm_rep_get_failover(rep_msg);
1490 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1491 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1492 param->srq = cm_rep_get_srq(rep_msg);
1493 work->cm_event.private_data = &rep_msg->private_data;
1494}
1495
1496static void cm_dup_rep_handler(struct cm_work *work)
1497{
1498 struct cm_id_private *cm_id_priv;
1499 struct cm_rep_msg *rep_msg;
1500 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001501 unsigned long flags;
1502 int ret;
1503
1504 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1505 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1506 rep_msg->local_comm_id);
1507 if (!cm_id_priv)
1508 return;
1509
1510 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1511 if (ret)
1512 goto deref;
1513
1514 spin_lock_irqsave(&cm_id_priv->lock, flags);
1515 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1516 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1517 cm_id_priv->private_data,
1518 cm_id_priv->private_data_len);
1519 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1520 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1521 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1522 cm_id_priv->private_data,
1523 cm_id_priv->private_data_len);
1524 else
1525 goto unlock;
1526 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1527
Sean Hefty34816ad2005-10-25 10:51:39 -07001528 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001529 if (ret)
1530 goto free;
1531 goto deref;
1532
1533unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1534free: cm_free_msg(msg);
1535deref: cm_deref_id(cm_id_priv);
1536}
1537
1538static int cm_rep_handler(struct cm_work *work)
1539{
1540 struct cm_id_private *cm_id_priv;
1541 struct cm_rep_msg *rep_msg;
1542 unsigned long flags;
1543 int ret;
1544
1545 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1546 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1547 if (!cm_id_priv) {
1548 cm_dup_rep_handler(work);
1549 return -EINVAL;
1550 }
1551
Hal Rosenstocka9770492005-07-27 11:45:40 -07001552 cm_format_rep_event(work);
1553
1554 spin_lock_irqsave(&cm_id_priv->lock, flags);
1555 switch (cm_id_priv->id.state) {
1556 case IB_CM_REQ_SENT:
1557 case IB_CM_MRA_REQ_RCVD:
1558 break;
1559 default:
1560 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1561 ret = -EINVAL;
1562 goto error;
1563 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08001564
1565 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1566 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1567 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1568
1569 spin_lock(&cm.lock);
1570 /* Check for duplicate REP. */
1571 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1572 spin_unlock(&cm.lock);
1573 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1574 ret = -EINVAL;
1575 goto error;
1576 }
1577 /* Check for a stale connection. */
1578 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1579 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1580 &cm.remote_id_table);
1581 cm_id_priv->timewait_info->inserted_remote_id = 0;
1582 spin_unlock(&cm.lock);
1583 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1584 cm_issue_rej(work->port, work->mad_recv_wc,
1585 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1586 NULL, 0);
1587 ret = -EINVAL;
1588 goto error;
1589 }
1590 spin_unlock(&cm.lock);
1591
Hal Rosenstocka9770492005-07-27 11:45:40 -07001592 cm_id_priv->id.state = IB_CM_REP_RCVD;
1593 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1594 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1595 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1596 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1597 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1598 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1599
1600 /* todo: handle peer_to_peer */
1601
Sean Hefty34816ad2005-10-25 10:51:39 -07001602 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001603 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1604 if (!ret)
1605 list_add_tail(&work->list, &cm_id_priv->work_list);
1606 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1607
1608 if (ret)
1609 cm_process_work(cm_id_priv, work);
1610 else
1611 cm_deref_id(cm_id_priv);
1612 return 0;
1613
Sean Hefty87fd1a12006-03-02 16:50:37 -08001614error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001615 cm_deref_id(cm_id_priv);
1616 return ret;
1617}
1618
1619static int cm_establish_handler(struct cm_work *work)
1620{
1621 struct cm_id_private *cm_id_priv;
1622 unsigned long flags;
1623 int ret;
1624
1625 /* See comment in ib_cm_establish about lookup. */
1626 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1627 if (!cm_id_priv)
1628 return -EINVAL;
1629
1630 spin_lock_irqsave(&cm_id_priv->lock, flags);
1631 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1632 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1633 goto out;
1634 }
1635
Sean Hefty34816ad2005-10-25 10:51:39 -07001636 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001637 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1638 if (!ret)
1639 list_add_tail(&work->list, &cm_id_priv->work_list);
1640 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1641
1642 if (ret)
1643 cm_process_work(cm_id_priv, work);
1644 else
1645 cm_deref_id(cm_id_priv);
1646 return 0;
1647out:
1648 cm_deref_id(cm_id_priv);
1649 return -EINVAL;
1650}
1651
1652static int cm_rtu_handler(struct cm_work *work)
1653{
1654 struct cm_id_private *cm_id_priv;
1655 struct cm_rtu_msg *rtu_msg;
1656 unsigned long flags;
1657 int ret;
1658
1659 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1660 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1661 rtu_msg->local_comm_id);
1662 if (!cm_id_priv)
1663 return -EINVAL;
1664
1665 work->cm_event.private_data = &rtu_msg->private_data;
1666
1667 spin_lock_irqsave(&cm_id_priv->lock, flags);
1668 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1669 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1670 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1671 goto out;
1672 }
1673 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1674
Sean Hefty34816ad2005-10-25 10:51:39 -07001675 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001676 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1677 if (!ret)
1678 list_add_tail(&work->list, &cm_id_priv->work_list);
1679 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1680
1681 if (ret)
1682 cm_process_work(cm_id_priv, work);
1683 else
1684 cm_deref_id(cm_id_priv);
1685 return 0;
1686out:
1687 cm_deref_id(cm_id_priv);
1688 return -EINVAL;
1689}
1690
1691static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1692 struct cm_id_private *cm_id_priv,
1693 const void *private_data,
1694 u8 private_data_len)
1695{
1696 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1697 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1698 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1699 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1700 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1701
1702 if (private_data && private_data_len)
1703 memcpy(dreq_msg->private_data, private_data, private_data_len);
1704}
1705
1706int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1707 const void *private_data,
1708 u8 private_data_len)
1709{
1710 struct cm_id_private *cm_id_priv;
1711 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001712 unsigned long flags;
1713 int ret;
1714
1715 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1716 return -EINVAL;
1717
1718 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1719 spin_lock_irqsave(&cm_id_priv->lock, flags);
1720 if (cm_id->state != IB_CM_ESTABLISHED) {
1721 ret = -EINVAL;
1722 goto out;
1723 }
1724
1725 ret = cm_alloc_msg(cm_id_priv, &msg);
1726 if (ret) {
1727 cm_enter_timewait(cm_id_priv);
1728 goto out;
1729 }
1730
1731 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1732 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07001733 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001734 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1735
Sean Hefty34816ad2005-10-25 10:51:39 -07001736 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001737 if (ret) {
1738 cm_enter_timewait(cm_id_priv);
1739 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1740 cm_free_msg(msg);
1741 return ret;
1742 }
1743
1744 cm_id->state = IB_CM_DREQ_SENT;
1745 cm_id_priv->msg = msg;
1746out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1747 return ret;
1748}
1749EXPORT_SYMBOL(ib_send_cm_dreq);
1750
1751static void cm_format_drep(struct cm_drep_msg *drep_msg,
1752 struct cm_id_private *cm_id_priv,
1753 const void *private_data,
1754 u8 private_data_len)
1755{
1756 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1757 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1758 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1759
1760 if (private_data && private_data_len)
1761 memcpy(drep_msg->private_data, private_data, private_data_len);
1762}
1763
1764int ib_send_cm_drep(struct ib_cm_id *cm_id,
1765 const void *private_data,
1766 u8 private_data_len)
1767{
1768 struct cm_id_private *cm_id_priv;
1769 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001770 unsigned long flags;
1771 void *data;
1772 int ret;
1773
1774 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1775 return -EINVAL;
1776
1777 data = cm_copy_private_data(private_data, private_data_len);
1778 if (IS_ERR(data))
1779 return PTR_ERR(data);
1780
1781 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1782 spin_lock_irqsave(&cm_id_priv->lock, flags);
1783 if (cm_id->state != IB_CM_DREQ_RCVD) {
1784 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1785 kfree(data);
1786 return -EINVAL;
1787 }
1788
1789 cm_set_private_data(cm_id_priv, data, private_data_len);
1790 cm_enter_timewait(cm_id_priv);
1791
1792 ret = cm_alloc_msg(cm_id_priv, &msg);
1793 if (ret)
1794 goto out;
1795
1796 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1797 private_data, private_data_len);
1798
Sean Hefty34816ad2005-10-25 10:51:39 -07001799 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001800 if (ret) {
1801 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1802 cm_free_msg(msg);
1803 return ret;
1804 }
1805
1806out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1807 return ret;
1808}
1809EXPORT_SYMBOL(ib_send_cm_drep);
1810
1811static int cm_dreq_handler(struct cm_work *work)
1812{
1813 struct cm_id_private *cm_id_priv;
1814 struct cm_dreq_msg *dreq_msg;
1815 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001816 unsigned long flags;
1817 int ret;
1818
1819 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1820 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1821 dreq_msg->local_comm_id);
1822 if (!cm_id_priv)
1823 return -EINVAL;
1824
1825 work->cm_event.private_data = &dreq_msg->private_data;
1826
1827 spin_lock_irqsave(&cm_id_priv->lock, flags);
1828 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1829 goto unlock;
1830
1831 switch (cm_id_priv->id.state) {
1832 case IB_CM_REP_SENT:
1833 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07001834 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001835 break;
1836 case IB_CM_ESTABLISHED:
1837 case IB_CM_MRA_REP_RCVD:
1838 break;
1839 case IB_CM_TIMEWAIT:
1840 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1841 goto unlock;
1842
1843 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1844 cm_id_priv->private_data,
1845 cm_id_priv->private_data_len);
1846 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1847
Sean Hefty34816ad2005-10-25 10:51:39 -07001848 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07001849 cm_free_msg(msg);
1850 goto deref;
1851 default:
1852 goto unlock;
1853 }
1854 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1855 cm_id_priv->tid = dreq_msg->hdr.tid;
1856 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1857 if (!ret)
1858 list_add_tail(&work->list, &cm_id_priv->work_list);
1859 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1860
1861 if (ret)
1862 cm_process_work(cm_id_priv, work);
1863 else
1864 cm_deref_id(cm_id_priv);
1865 return 0;
1866
1867unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1868deref: cm_deref_id(cm_id_priv);
1869 return -EINVAL;
1870}
1871
1872static int cm_drep_handler(struct cm_work *work)
1873{
1874 struct cm_id_private *cm_id_priv;
1875 struct cm_drep_msg *drep_msg;
1876 unsigned long flags;
1877 int ret;
1878
1879 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1880 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1881 drep_msg->local_comm_id);
1882 if (!cm_id_priv)
1883 return -EINVAL;
1884
1885 work->cm_event.private_data = &drep_msg->private_data;
1886
1887 spin_lock_irqsave(&cm_id_priv->lock, flags);
1888 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1889 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1890 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1891 goto out;
1892 }
1893 cm_enter_timewait(cm_id_priv);
1894
Sean Hefty34816ad2005-10-25 10:51:39 -07001895 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001896 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1897 if (!ret)
1898 list_add_tail(&work->list, &cm_id_priv->work_list);
1899 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1900
1901 if (ret)
1902 cm_process_work(cm_id_priv, work);
1903 else
1904 cm_deref_id(cm_id_priv);
1905 return 0;
1906out:
1907 cm_deref_id(cm_id_priv);
1908 return -EINVAL;
1909}
1910
1911int ib_send_cm_rej(struct ib_cm_id *cm_id,
1912 enum ib_cm_rej_reason reason,
1913 void *ari,
1914 u8 ari_length,
1915 const void *private_data,
1916 u8 private_data_len)
1917{
1918 struct cm_id_private *cm_id_priv;
1919 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001920 unsigned long flags;
1921 int ret;
1922
1923 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
1924 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
1925 return -EINVAL;
1926
1927 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1928
1929 spin_lock_irqsave(&cm_id_priv->lock, flags);
1930 switch (cm_id->state) {
1931 case IB_CM_REQ_SENT:
1932 case IB_CM_MRA_REQ_RCVD:
1933 case IB_CM_REQ_RCVD:
1934 case IB_CM_MRA_REQ_SENT:
1935 case IB_CM_REP_RCVD:
1936 case IB_CM_MRA_REP_SENT:
1937 ret = cm_alloc_msg(cm_id_priv, &msg);
1938 if (!ret)
1939 cm_format_rej((struct cm_rej_msg *) msg->mad,
1940 cm_id_priv, reason, ari, ari_length,
1941 private_data, private_data_len);
1942
1943 cm_reset_to_idle(cm_id_priv);
1944 break;
1945 case IB_CM_REP_SENT:
1946 case IB_CM_MRA_REP_RCVD:
1947 ret = cm_alloc_msg(cm_id_priv, &msg);
1948 if (!ret)
1949 cm_format_rej((struct cm_rej_msg *) msg->mad,
1950 cm_id_priv, reason, ari, ari_length,
1951 private_data, private_data_len);
1952
1953 cm_enter_timewait(cm_id_priv);
1954 break;
1955 default:
1956 ret = -EINVAL;
1957 goto out;
1958 }
1959
1960 if (ret)
1961 goto out;
1962
Sean Hefty34816ad2005-10-25 10:51:39 -07001963 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001964 if (ret)
1965 cm_free_msg(msg);
1966
1967out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1968 return ret;
1969}
1970EXPORT_SYMBOL(ib_send_cm_rej);
1971
1972static void cm_format_rej_event(struct cm_work *work)
1973{
1974 struct cm_rej_msg *rej_msg;
1975 struct ib_cm_rej_event_param *param;
1976
1977 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
1978 param = &work->cm_event.param.rej_rcvd;
1979 param->ari = rej_msg->ari;
1980 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001981 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001982 work->cm_event.private_data = &rej_msg->private_data;
1983}
1984
1985static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
1986{
1987 struct cm_timewait_info *timewait_info;
1988 struct cm_id_private *cm_id_priv;
1989 unsigned long flags;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001990 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001991
1992 remote_id = rej_msg->local_comm_id;
1993
Sean Hefty97f52eb2005-08-13 21:05:57 -07001994 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07001995 spin_lock_irqsave(&cm.lock, flags);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001996 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07001997 remote_id);
1998 if (!timewait_info) {
1999 spin_unlock_irqrestore(&cm.lock, flags);
2000 return NULL;
2001 }
2002 cm_id_priv = idr_find(&cm.local_id_table,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002003 (__force int) timewait_info->work.local_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002004 if (cm_id_priv) {
2005 if (cm_id_priv->id.remote_id == remote_id)
2006 atomic_inc(&cm_id_priv->refcount);
2007 else
2008 cm_id_priv = NULL;
2009 }
2010 spin_unlock_irqrestore(&cm.lock, flags);
2011 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2012 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2013 else
2014 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2015
2016 return cm_id_priv;
2017}
2018
2019static int cm_rej_handler(struct cm_work *work)
2020{
2021 struct cm_id_private *cm_id_priv;
2022 struct cm_rej_msg *rej_msg;
2023 unsigned long flags;
2024 int ret;
2025
2026 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2027 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2028 if (!cm_id_priv)
2029 return -EINVAL;
2030
2031 cm_format_rej_event(work);
2032
2033 spin_lock_irqsave(&cm_id_priv->lock, flags);
2034 switch (cm_id_priv->id.state) {
2035 case IB_CM_REQ_SENT:
2036 case IB_CM_MRA_REQ_RCVD:
2037 case IB_CM_REP_SENT:
2038 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002039 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002040 /* fall through */
2041 case IB_CM_REQ_RCVD:
2042 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002043 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002044 cm_enter_timewait(cm_id_priv);
2045 else
2046 cm_reset_to_idle(cm_id_priv);
2047 break;
2048 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002049 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002050 /* fall through */
2051 case IB_CM_REP_RCVD:
2052 case IB_CM_MRA_REP_SENT:
2053 case IB_CM_ESTABLISHED:
2054 cm_enter_timewait(cm_id_priv);
2055 break;
2056 default:
2057 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2058 ret = -EINVAL;
2059 goto out;
2060 }
2061
2062 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2063 if (!ret)
2064 list_add_tail(&work->list, &cm_id_priv->work_list);
2065 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2066
2067 if (ret)
2068 cm_process_work(cm_id_priv, work);
2069 else
2070 cm_deref_id(cm_id_priv);
2071 return 0;
2072out:
2073 cm_deref_id(cm_id_priv);
2074 return -EINVAL;
2075}
2076
2077int ib_send_cm_mra(struct ib_cm_id *cm_id,
2078 u8 service_timeout,
2079 const void *private_data,
2080 u8 private_data_len)
2081{
2082 struct cm_id_private *cm_id_priv;
2083 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002084 void *data;
2085 unsigned long flags;
2086 int ret;
2087
2088 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2089 return -EINVAL;
2090
2091 data = cm_copy_private_data(private_data, private_data_len);
2092 if (IS_ERR(data))
2093 return PTR_ERR(data);
2094
2095 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2096
2097 spin_lock_irqsave(&cm_id_priv->lock, flags);
2098 switch(cm_id_priv->id.state) {
2099 case IB_CM_REQ_RCVD:
2100 ret = cm_alloc_msg(cm_id_priv, &msg);
2101 if (ret)
2102 goto error1;
2103
2104 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2105 CM_MSG_RESPONSE_REQ, service_timeout,
2106 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002107 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002108 if (ret)
2109 goto error2;
2110 cm_id->state = IB_CM_MRA_REQ_SENT;
2111 break;
2112 case IB_CM_REP_RCVD:
2113 ret = cm_alloc_msg(cm_id_priv, &msg);
2114 if (ret)
2115 goto error1;
2116
2117 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2118 CM_MSG_RESPONSE_REP, service_timeout,
2119 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002120 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002121 if (ret)
2122 goto error2;
2123 cm_id->state = IB_CM_MRA_REP_SENT;
2124 break;
2125 case IB_CM_ESTABLISHED:
2126 ret = cm_alloc_msg(cm_id_priv, &msg);
2127 if (ret)
2128 goto error1;
2129
2130 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2131 CM_MSG_RESPONSE_OTHER, service_timeout,
2132 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002133 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002134 if (ret)
2135 goto error2;
2136 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2137 break;
2138 default:
2139 ret = -EINVAL;
2140 goto error1;
2141 }
2142 cm_id_priv->service_timeout = service_timeout;
2143 cm_set_private_data(cm_id_priv, data, private_data_len);
2144 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2145 return 0;
2146
2147error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2148 kfree(data);
2149 return ret;
2150
2151error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2152 kfree(data);
2153 cm_free_msg(msg);
2154 return ret;
2155}
2156EXPORT_SYMBOL(ib_send_cm_mra);
2157
2158static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2159{
2160 switch (cm_mra_get_msg_mraed(mra_msg)) {
2161 case CM_MSG_RESPONSE_REQ:
2162 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2163 case CM_MSG_RESPONSE_REP:
2164 case CM_MSG_RESPONSE_OTHER:
2165 return cm_acquire_id(mra_msg->remote_comm_id,
2166 mra_msg->local_comm_id);
2167 default:
2168 return NULL;
2169 }
2170}
2171
2172static int cm_mra_handler(struct cm_work *work)
2173{
2174 struct cm_id_private *cm_id_priv;
2175 struct cm_mra_msg *mra_msg;
2176 unsigned long flags;
2177 int timeout, ret;
2178
2179 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2180 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2181 if (!cm_id_priv)
2182 return -EINVAL;
2183
2184 work->cm_event.private_data = &mra_msg->private_data;
2185 work->cm_event.param.mra_rcvd.service_timeout =
2186 cm_mra_get_service_timeout(mra_msg);
2187 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2188 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2189
2190 spin_lock_irqsave(&cm_id_priv->lock, flags);
2191 switch (cm_id_priv->id.state) {
2192 case IB_CM_REQ_SENT:
2193 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2194 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002195 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002196 goto out;
2197 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2198 break;
2199 case IB_CM_REP_SENT:
2200 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2201 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002202 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002203 goto out;
2204 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2205 break;
2206 case IB_CM_ESTABLISHED:
2207 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2208 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2209 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002210 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002211 goto out;
2212 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2213 break;
2214 default:
2215 goto out;
2216 }
2217
2218 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2219 cm_id_priv->id.state;
2220 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2221 if (!ret)
2222 list_add_tail(&work->list, &cm_id_priv->work_list);
2223 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2224
2225 if (ret)
2226 cm_process_work(cm_id_priv, work);
2227 else
2228 cm_deref_id(cm_id_priv);
2229 return 0;
2230out:
2231 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2232 cm_deref_id(cm_id_priv);
2233 return -EINVAL;
2234}
2235
2236static void cm_format_lap(struct cm_lap_msg *lap_msg,
2237 struct cm_id_private *cm_id_priv,
2238 struct ib_sa_path_rec *alternate_path,
2239 const void *private_data,
2240 u8 private_data_len)
2241{
2242 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2243 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2244 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2245 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2246 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2247 /* todo: need remote CM response timeout */
2248 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2249 lap_msg->alt_local_lid = alternate_path->slid;
2250 lap_msg->alt_remote_lid = alternate_path->dlid;
2251 lap_msg->alt_local_gid = alternate_path->sgid;
2252 lap_msg->alt_remote_gid = alternate_path->dgid;
2253 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2254 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2255 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2256 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2257 cm_lap_set_sl(lap_msg, alternate_path->sl);
2258 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2259 cm_lap_set_local_ack_timeout(lap_msg,
2260 min(31, alternate_path->packet_life_time + 1));
2261
2262 if (private_data && private_data_len)
2263 memcpy(lap_msg->private_data, private_data, private_data_len);
2264}
2265
2266int ib_send_cm_lap(struct ib_cm_id *cm_id,
2267 struct ib_sa_path_rec *alternate_path,
2268 const void *private_data,
2269 u8 private_data_len)
2270{
2271 struct cm_id_private *cm_id_priv;
2272 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002273 unsigned long flags;
2274 int ret;
2275
2276 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2277 return -EINVAL;
2278
2279 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2280 spin_lock_irqsave(&cm_id_priv->lock, flags);
2281 if (cm_id->state != IB_CM_ESTABLISHED ||
2282 cm_id->lap_state != IB_CM_LAP_IDLE) {
2283 ret = -EINVAL;
2284 goto out;
2285 }
2286
2287 ret = cm_alloc_msg(cm_id_priv, &msg);
2288 if (ret)
2289 goto out;
2290
2291 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2292 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002293 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002294 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2295
Sean Hefty34816ad2005-10-25 10:51:39 -07002296 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002297 if (ret) {
2298 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2299 cm_free_msg(msg);
2300 return ret;
2301 }
2302
2303 cm_id->lap_state = IB_CM_LAP_SENT;
2304 cm_id_priv->msg = msg;
2305
2306out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2307 return ret;
2308}
2309EXPORT_SYMBOL(ib_send_cm_lap);
2310
2311static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2312 struct cm_lap_msg *lap_msg)
2313{
2314 memset(path, 0, sizeof *path);
2315 path->dgid = lap_msg->alt_local_gid;
2316 path->sgid = lap_msg->alt_remote_gid;
2317 path->dlid = lap_msg->alt_local_lid;
2318 path->slid = lap_msg->alt_remote_lid;
2319 path->flow_label = cm_lap_get_flow_label(lap_msg);
2320 path->hop_limit = lap_msg->alt_hop_limit;
2321 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2322 path->reversible = 1;
2323 /* pkey is same as in REQ */
2324 path->sl = cm_lap_get_sl(lap_msg);
2325 path->mtu_selector = IB_SA_EQ;
2326 /* mtu is same as in REQ */
2327 path->rate_selector = IB_SA_EQ;
2328 path->rate = cm_lap_get_packet_rate(lap_msg);
2329 path->packet_life_time_selector = IB_SA_EQ;
2330 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2331 path->packet_life_time -= (path->packet_life_time > 0);
2332}
2333
2334static int cm_lap_handler(struct cm_work *work)
2335{
2336 struct cm_id_private *cm_id_priv;
2337 struct cm_lap_msg *lap_msg;
2338 struct ib_cm_lap_event_param *param;
2339 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002340 unsigned long flags;
2341 int ret;
2342
2343 /* todo: verify LAP request and send reject APR if invalid. */
2344 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2345 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2346 lap_msg->local_comm_id);
2347 if (!cm_id_priv)
2348 return -EINVAL;
2349
2350 param = &work->cm_event.param.lap_rcvd;
2351 param->alternate_path = &work->path[0];
2352 cm_format_path_from_lap(param->alternate_path, lap_msg);
2353 work->cm_event.private_data = &lap_msg->private_data;
2354
2355 spin_lock_irqsave(&cm_id_priv->lock, flags);
2356 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2357 goto unlock;
2358
2359 switch (cm_id_priv->id.lap_state) {
2360 case IB_CM_LAP_IDLE:
2361 break;
2362 case IB_CM_MRA_LAP_SENT:
2363 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2364 goto unlock;
2365
2366 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2367 CM_MSG_RESPONSE_OTHER,
2368 cm_id_priv->service_timeout,
2369 cm_id_priv->private_data,
2370 cm_id_priv->private_data_len);
2371 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2372
Sean Hefty34816ad2005-10-25 10:51:39 -07002373 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002374 cm_free_msg(msg);
2375 goto deref;
2376 default:
2377 goto unlock;
2378 }
2379
2380 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2381 cm_id_priv->tid = lap_msg->hdr.tid;
2382 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2383 if (!ret)
2384 list_add_tail(&work->list, &cm_id_priv->work_list);
2385 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2386
2387 if (ret)
2388 cm_process_work(cm_id_priv, work);
2389 else
2390 cm_deref_id(cm_id_priv);
2391 return 0;
2392
2393unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2394deref: cm_deref_id(cm_id_priv);
2395 return -EINVAL;
2396}
2397
2398static void cm_format_apr(struct cm_apr_msg *apr_msg,
2399 struct cm_id_private *cm_id_priv,
2400 enum ib_cm_apr_status status,
2401 void *info,
2402 u8 info_length,
2403 const void *private_data,
2404 u8 private_data_len)
2405{
2406 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2407 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2408 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2409 apr_msg->ap_status = (u8) status;
2410
2411 if (info && info_length) {
2412 apr_msg->info_length = info_length;
2413 memcpy(apr_msg->info, info, info_length);
2414 }
2415
2416 if (private_data && private_data_len)
2417 memcpy(apr_msg->private_data, private_data, private_data_len);
2418}
2419
2420int ib_send_cm_apr(struct ib_cm_id *cm_id,
2421 enum ib_cm_apr_status status,
2422 void *info,
2423 u8 info_length,
2424 const void *private_data,
2425 u8 private_data_len)
2426{
2427 struct cm_id_private *cm_id_priv;
2428 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002429 unsigned long flags;
2430 int ret;
2431
2432 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2433 (info && info_length > IB_CM_APR_INFO_LENGTH))
2434 return -EINVAL;
2435
2436 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2437 spin_lock_irqsave(&cm_id_priv->lock, flags);
2438 if (cm_id->state != IB_CM_ESTABLISHED ||
2439 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2440 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2441 ret = -EINVAL;
2442 goto out;
2443 }
2444
2445 ret = cm_alloc_msg(cm_id_priv, &msg);
2446 if (ret)
2447 goto out;
2448
2449 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2450 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002451 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002452 if (ret) {
2453 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2454 cm_free_msg(msg);
2455 return ret;
2456 }
2457
2458 cm_id->lap_state = IB_CM_LAP_IDLE;
2459out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2460 return ret;
2461}
2462EXPORT_SYMBOL(ib_send_cm_apr);
2463
2464static int cm_apr_handler(struct cm_work *work)
2465{
2466 struct cm_id_private *cm_id_priv;
2467 struct cm_apr_msg *apr_msg;
2468 unsigned long flags;
2469 int ret;
2470
2471 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2472 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2473 apr_msg->local_comm_id);
2474 if (!cm_id_priv)
2475 return -EINVAL; /* Unmatched reply. */
2476
2477 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2478 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2479 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2480 work->cm_event.private_data = &apr_msg->private_data;
2481
2482 spin_lock_irqsave(&cm_id_priv->lock, flags);
2483 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2484 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2485 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2486 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2487 goto out;
2488 }
2489 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002490 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002491 cm_id_priv->msg = NULL;
2492
2493 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2494 if (!ret)
2495 list_add_tail(&work->list, &cm_id_priv->work_list);
2496 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2497
2498 if (ret)
2499 cm_process_work(cm_id_priv, work);
2500 else
2501 cm_deref_id(cm_id_priv);
2502 return 0;
2503out:
2504 cm_deref_id(cm_id_priv);
2505 return -EINVAL;
2506}
2507
2508static int cm_timewait_handler(struct cm_work *work)
2509{
2510 struct cm_timewait_info *timewait_info;
2511 struct cm_id_private *cm_id_priv;
2512 unsigned long flags;
2513 int ret;
2514
2515 timewait_info = (struct cm_timewait_info *)work;
2516 cm_cleanup_timewait(timewait_info);
2517
2518 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2519 timewait_info->work.remote_id);
2520 if (!cm_id_priv)
2521 return -EINVAL;
2522
2523 spin_lock_irqsave(&cm_id_priv->lock, flags);
2524 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2525 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2526 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2527 goto out;
2528 }
2529 cm_id_priv->id.state = IB_CM_IDLE;
2530 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2531 if (!ret)
2532 list_add_tail(&work->list, &cm_id_priv->work_list);
2533 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2534
2535 if (ret)
2536 cm_process_work(cm_id_priv, work);
2537 else
2538 cm_deref_id(cm_id_priv);
2539 return 0;
2540out:
2541 cm_deref_id(cm_id_priv);
2542 return -EINVAL;
2543}
2544
2545static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2546 struct cm_id_private *cm_id_priv,
2547 struct ib_cm_sidr_req_param *param)
2548{
2549 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2550 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2551 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002552 sidr_req_msg->pkey = cpu_to_be16(param->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002553 sidr_req_msg->service_id = param->service_id;
2554
2555 if (param->private_data && param->private_data_len)
2556 memcpy(sidr_req_msg->private_data, param->private_data,
2557 param->private_data_len);
2558}
2559
2560int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2561 struct ib_cm_sidr_req_param *param)
2562{
2563 struct cm_id_private *cm_id_priv;
2564 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002565 unsigned long flags;
2566 int ret;
2567
2568 if (!param->path || (param->private_data &&
2569 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2570 return -EINVAL;
2571
2572 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2573 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2574 if (ret)
2575 goto out;
2576
2577 cm_id->service_id = param->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002578 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002579 cm_id_priv->timeout_ms = param->timeout_ms;
2580 cm_id_priv->max_cm_retries = param->max_cm_retries;
2581 ret = cm_alloc_msg(cm_id_priv, &msg);
2582 if (ret)
2583 goto out;
2584
2585 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2586 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07002587 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002588 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2589
2590 spin_lock_irqsave(&cm_id_priv->lock, flags);
2591 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07002592 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002593 else
2594 ret = -EINVAL;
2595
2596 if (ret) {
2597 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2598 cm_free_msg(msg);
2599 goto out;
2600 }
2601 cm_id->state = IB_CM_SIDR_REQ_SENT;
2602 cm_id_priv->msg = msg;
2603 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2604out:
2605 return ret;
2606}
2607EXPORT_SYMBOL(ib_send_cm_sidr_req);
2608
2609static void cm_format_sidr_req_event(struct cm_work *work,
2610 struct ib_cm_id *listen_id)
2611{
2612 struct cm_sidr_req_msg *sidr_req_msg;
2613 struct ib_cm_sidr_req_event_param *param;
2614
2615 sidr_req_msg = (struct cm_sidr_req_msg *)
2616 work->mad_recv_wc->recv_buf.mad;
2617 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002618 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002619 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002620 param->port = work->port->port_num;
2621 work->cm_event.private_data = &sidr_req_msg->private_data;
2622}
2623
2624static int cm_sidr_req_handler(struct cm_work *work)
2625{
2626 struct ib_cm_id *cm_id;
2627 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2628 struct cm_sidr_req_msg *sidr_req_msg;
2629 struct ib_wc *wc;
2630 unsigned long flags;
2631
Sean Hefty07d357d2005-10-17 15:37:43 -07002632 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002633 if (IS_ERR(cm_id))
2634 return PTR_ERR(cm_id);
2635 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2636
2637 /* Record SGID/SLID and request ID for lookup. */
2638 sidr_req_msg = (struct cm_sidr_req_msg *)
2639 work->mad_recv_wc->recv_buf.mad;
2640 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002641 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002642 cm_id_priv->av.dgid.global.interface_id = 0;
2643 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2644 &cm_id_priv->av);
2645 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2646 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2647 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2648 atomic_inc(&cm_id_priv->work_count);
2649
2650 spin_lock_irqsave(&cm.lock, flags);
2651 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2652 if (cur_cm_id_priv) {
2653 spin_unlock_irqrestore(&cm.lock, flags);
2654 goto out; /* Duplicate message. */
2655 }
Sean Hefty07d357d2005-10-17 15:37:43 -07002656 cur_cm_id_priv = cm_find_listen(cm_id->device,
2657 sidr_req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002658 if (!cur_cm_id_priv) {
2659 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2660 spin_unlock_irqrestore(&cm.lock, flags);
2661 /* todo: reply with no match */
2662 goto out; /* No match. */
2663 }
2664 atomic_inc(&cur_cm_id_priv->refcount);
2665 spin_unlock_irqrestore(&cm.lock, flags);
2666
2667 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2668 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2669 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002670 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002671
2672 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2673 cm_process_work(cm_id_priv, work);
2674 cm_deref_id(cur_cm_id_priv);
2675 return 0;
2676out:
2677 ib_destroy_cm_id(&cm_id_priv->id);
2678 return -EINVAL;
2679}
2680
2681static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2682 struct cm_id_private *cm_id_priv,
2683 struct ib_cm_sidr_rep_param *param)
2684{
2685 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2686 cm_id_priv->tid);
2687 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2688 sidr_rep_msg->status = param->status;
2689 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2690 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2691 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2692
2693 if (param->info && param->info_length)
2694 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2695
2696 if (param->private_data && param->private_data_len)
2697 memcpy(sidr_rep_msg->private_data, param->private_data,
2698 param->private_data_len);
2699}
2700
2701int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2702 struct ib_cm_sidr_rep_param *param)
2703{
2704 struct cm_id_private *cm_id_priv;
2705 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002706 unsigned long flags;
2707 int ret;
2708
2709 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2710 (param->private_data &&
2711 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2712 return -EINVAL;
2713
2714 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2715 spin_lock_irqsave(&cm_id_priv->lock, flags);
2716 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2717 ret = -EINVAL;
2718 goto error;
2719 }
2720
2721 ret = cm_alloc_msg(cm_id_priv, &msg);
2722 if (ret)
2723 goto error;
2724
2725 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2726 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07002727 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002728 if (ret) {
2729 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2730 cm_free_msg(msg);
2731 return ret;
2732 }
2733 cm_id->state = IB_CM_IDLE;
2734 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2735
2736 spin_lock_irqsave(&cm.lock, flags);
2737 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2738 spin_unlock_irqrestore(&cm.lock, flags);
2739 return 0;
2740
2741error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2742 return ret;
2743}
2744EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2745
2746static void cm_format_sidr_rep_event(struct cm_work *work)
2747{
2748 struct cm_sidr_rep_msg *sidr_rep_msg;
2749 struct ib_cm_sidr_rep_event_param *param;
2750
2751 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2752 work->mad_recv_wc->recv_buf.mad;
2753 param = &work->cm_event.param.sidr_rep_rcvd;
2754 param->status = sidr_rep_msg->status;
2755 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2756 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2757 param->info = &sidr_rep_msg->info;
2758 param->info_len = sidr_rep_msg->info_length;
2759 work->cm_event.private_data = &sidr_rep_msg->private_data;
2760}
2761
2762static int cm_sidr_rep_handler(struct cm_work *work)
2763{
2764 struct cm_sidr_rep_msg *sidr_rep_msg;
2765 struct cm_id_private *cm_id_priv;
2766 unsigned long flags;
2767
2768 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2769 work->mad_recv_wc->recv_buf.mad;
2770 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2771 if (!cm_id_priv)
2772 return -EINVAL; /* Unmatched reply. */
2773
2774 spin_lock_irqsave(&cm_id_priv->lock, flags);
2775 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2776 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2777 goto out;
2778 }
2779 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002780 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002781 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2782
2783 cm_format_sidr_rep_event(work);
2784 cm_process_work(cm_id_priv, work);
2785 return 0;
2786out:
2787 cm_deref_id(cm_id_priv);
2788 return -EINVAL;
2789}
2790
2791static void cm_process_send_error(struct ib_mad_send_buf *msg,
2792 enum ib_wc_status wc_status)
2793{
2794 struct cm_id_private *cm_id_priv;
2795 struct ib_cm_event cm_event;
2796 enum ib_cm_state state;
2797 unsigned long flags;
2798 int ret;
2799
2800 memset(&cm_event, 0, sizeof cm_event);
2801 cm_id_priv = msg->context[0];
2802
2803 /* Discard old sends or ones without a response. */
2804 spin_lock_irqsave(&cm_id_priv->lock, flags);
2805 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2806 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2807 goto discard;
2808
2809 switch (state) {
2810 case IB_CM_REQ_SENT:
2811 case IB_CM_MRA_REQ_RCVD:
2812 cm_reset_to_idle(cm_id_priv);
2813 cm_event.event = IB_CM_REQ_ERROR;
2814 break;
2815 case IB_CM_REP_SENT:
2816 case IB_CM_MRA_REP_RCVD:
2817 cm_reset_to_idle(cm_id_priv);
2818 cm_event.event = IB_CM_REP_ERROR;
2819 break;
2820 case IB_CM_DREQ_SENT:
2821 cm_enter_timewait(cm_id_priv);
2822 cm_event.event = IB_CM_DREQ_ERROR;
2823 break;
2824 case IB_CM_SIDR_REQ_SENT:
2825 cm_id_priv->id.state = IB_CM_IDLE;
2826 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2827 break;
2828 default:
2829 goto discard;
2830 }
2831 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2832 cm_event.param.send_status = wc_status;
2833
2834 /* No other events can occur on the cm_id at this point. */
2835 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2836 cm_free_msg(msg);
2837 if (ret)
2838 ib_destroy_cm_id(&cm_id_priv->id);
2839 return;
2840discard:
2841 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2842 cm_free_msg(msg);
2843}
2844
2845static void cm_send_handler(struct ib_mad_agent *mad_agent,
2846 struct ib_mad_send_wc *mad_send_wc)
2847{
Sean Hefty34816ad2005-10-25 10:51:39 -07002848 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002849
2850 switch (mad_send_wc->status) {
2851 case IB_WC_SUCCESS:
2852 case IB_WC_WR_FLUSH_ERR:
2853 cm_free_msg(msg);
2854 break;
2855 default:
2856 if (msg->context[0] && msg->context[1])
2857 cm_process_send_error(msg, mad_send_wc->status);
2858 else
2859 cm_free_msg(msg);
2860 break;
2861 }
2862}
2863
2864static void cm_work_handler(void *data)
2865{
2866 struct cm_work *work = data;
2867 int ret;
2868
2869 switch (work->cm_event.event) {
2870 case IB_CM_REQ_RECEIVED:
2871 ret = cm_req_handler(work);
2872 break;
2873 case IB_CM_MRA_RECEIVED:
2874 ret = cm_mra_handler(work);
2875 break;
2876 case IB_CM_REJ_RECEIVED:
2877 ret = cm_rej_handler(work);
2878 break;
2879 case IB_CM_REP_RECEIVED:
2880 ret = cm_rep_handler(work);
2881 break;
2882 case IB_CM_RTU_RECEIVED:
2883 ret = cm_rtu_handler(work);
2884 break;
2885 case IB_CM_USER_ESTABLISHED:
2886 ret = cm_establish_handler(work);
2887 break;
2888 case IB_CM_DREQ_RECEIVED:
2889 ret = cm_dreq_handler(work);
2890 break;
2891 case IB_CM_DREP_RECEIVED:
2892 ret = cm_drep_handler(work);
2893 break;
2894 case IB_CM_SIDR_REQ_RECEIVED:
2895 ret = cm_sidr_req_handler(work);
2896 break;
2897 case IB_CM_SIDR_REP_RECEIVED:
2898 ret = cm_sidr_rep_handler(work);
2899 break;
2900 case IB_CM_LAP_RECEIVED:
2901 ret = cm_lap_handler(work);
2902 break;
2903 case IB_CM_APR_RECEIVED:
2904 ret = cm_apr_handler(work);
2905 break;
2906 case IB_CM_TIMEWAIT_EXIT:
2907 ret = cm_timewait_handler(work);
2908 break;
2909 default:
2910 ret = -EINVAL;
2911 break;
2912 }
2913 if (ret)
2914 cm_free_work(work);
2915}
2916
2917int ib_cm_establish(struct ib_cm_id *cm_id)
2918{
2919 struct cm_id_private *cm_id_priv;
2920 struct cm_work *work;
2921 unsigned long flags;
2922 int ret = 0;
2923
2924 work = kmalloc(sizeof *work, GFP_ATOMIC);
2925 if (!work)
2926 return -ENOMEM;
2927
2928 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2929 spin_lock_irqsave(&cm_id_priv->lock, flags);
2930 switch (cm_id->state)
2931 {
2932 case IB_CM_REP_SENT:
2933 case IB_CM_MRA_REP_RCVD:
2934 cm_id->state = IB_CM_ESTABLISHED;
2935 break;
2936 case IB_CM_ESTABLISHED:
2937 ret = -EISCONN;
2938 break;
2939 default:
2940 ret = -EINVAL;
2941 break;
2942 }
2943 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2944
2945 if (ret) {
2946 kfree(work);
2947 goto out;
2948 }
2949
2950 /*
2951 * The CM worker thread may try to destroy the cm_id before it
2952 * can execute this work item. To prevent potential deadlock,
2953 * we need to find the cm_id once we're in the context of the
2954 * worker thread, rather than holding a reference on it.
2955 */
2956 INIT_WORK(&work->work, cm_work_handler, work);
2957 work->local_id = cm_id->local_id;
2958 work->remote_id = cm_id->remote_id;
2959 work->mad_recv_wc = NULL;
2960 work->cm_event.event = IB_CM_USER_ESTABLISHED;
2961 queue_work(cm.wq, &work->work);
2962out:
2963 return ret;
2964}
2965EXPORT_SYMBOL(ib_cm_establish);
2966
2967static void cm_recv_handler(struct ib_mad_agent *mad_agent,
2968 struct ib_mad_recv_wc *mad_recv_wc)
2969{
2970 struct cm_work *work;
2971 enum ib_cm_event_type event;
2972 int paths = 0;
2973
2974 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
2975 case CM_REQ_ATTR_ID:
2976 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
2977 alt_local_lid != 0);
2978 event = IB_CM_REQ_RECEIVED;
2979 break;
2980 case CM_MRA_ATTR_ID:
2981 event = IB_CM_MRA_RECEIVED;
2982 break;
2983 case CM_REJ_ATTR_ID:
2984 event = IB_CM_REJ_RECEIVED;
2985 break;
2986 case CM_REP_ATTR_ID:
2987 event = IB_CM_REP_RECEIVED;
2988 break;
2989 case CM_RTU_ATTR_ID:
2990 event = IB_CM_RTU_RECEIVED;
2991 break;
2992 case CM_DREQ_ATTR_ID:
2993 event = IB_CM_DREQ_RECEIVED;
2994 break;
2995 case CM_DREP_ATTR_ID:
2996 event = IB_CM_DREP_RECEIVED;
2997 break;
2998 case CM_SIDR_REQ_ATTR_ID:
2999 event = IB_CM_SIDR_REQ_RECEIVED;
3000 break;
3001 case CM_SIDR_REP_ATTR_ID:
3002 event = IB_CM_SIDR_REP_RECEIVED;
3003 break;
3004 case CM_LAP_ATTR_ID:
3005 paths = 1;
3006 event = IB_CM_LAP_RECEIVED;
3007 break;
3008 case CM_APR_ATTR_ID:
3009 event = IB_CM_APR_RECEIVED;
3010 break;
3011 default:
3012 ib_free_recv_mad(mad_recv_wc);
3013 return;
3014 }
3015
3016 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3017 GFP_KERNEL);
3018 if (!work) {
3019 ib_free_recv_mad(mad_recv_wc);
3020 return;
3021 }
3022
3023 INIT_WORK(&work->work, cm_work_handler, work);
3024 work->cm_event.event = event;
3025 work->mad_recv_wc = mad_recv_wc;
3026 work->port = (struct cm_port *)mad_agent->context;
3027 queue_work(cm.wq, &work->work);
3028}
3029
3030static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3031 struct ib_qp_attr *qp_attr,
3032 int *qp_attr_mask)
3033{
3034 unsigned long flags;
3035 int ret;
3036
3037 spin_lock_irqsave(&cm_id_priv->lock, flags);
3038 switch (cm_id_priv->id.state) {
3039 case IB_CM_REQ_SENT:
3040 case IB_CM_MRA_REQ_RCVD:
3041 case IB_CM_REQ_RCVD:
3042 case IB_CM_MRA_REQ_SENT:
3043 case IB_CM_REP_RCVD:
3044 case IB_CM_MRA_REP_SENT:
3045 case IB_CM_REP_SENT:
3046 case IB_CM_MRA_REP_RCVD:
3047 case IB_CM_ESTABLISHED:
3048 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3049 IB_QP_PKEY_INDEX | IB_QP_PORT;
Sean Heftyae7971a2005-10-24 12:33:56 -07003050 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
3051 IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003052 if (cm_id_priv->responder_resources)
Sean Heftyae7971a2005-10-24 12:33:56 -07003053 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003054 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3055 qp_attr->port_num = cm_id_priv->av.port->port_num;
3056 ret = 0;
3057 break;
3058 default:
3059 ret = -EINVAL;
3060 break;
3061 }
3062 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3063 return ret;
3064}
3065
3066static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3067 struct ib_qp_attr *qp_attr,
3068 int *qp_attr_mask)
3069{
3070 unsigned long flags;
3071 int ret;
3072
3073 spin_lock_irqsave(&cm_id_priv->lock, flags);
3074 switch (cm_id_priv->id.state) {
3075 case IB_CM_REQ_RCVD:
3076 case IB_CM_MRA_REQ_SENT:
3077 case IB_CM_REP_RCVD:
3078 case IB_CM_MRA_REP_SENT:
3079 case IB_CM_REP_SENT:
3080 case IB_CM_MRA_REP_RCVD:
3081 case IB_CM_ESTABLISHED:
3082 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003083 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003084 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3085 qp_attr->path_mtu = cm_id_priv->path_mtu;
3086 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3087 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyae7971a2005-10-24 12:33:56 -07003088 if (cm_id_priv->qp_type == IB_QPT_RC) {
3089 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3090 IB_QP_MIN_RNR_TIMER;
3091 qp_attr->max_dest_rd_atomic =
3092 cm_id_priv->responder_resources;
3093 qp_attr->min_rnr_timer = 0;
3094 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003095 if (cm_id_priv->alt_av.ah_attr.dlid) {
3096 *qp_attr_mask |= IB_QP_ALT_PATH;
3097 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3098 }
3099 ret = 0;
3100 break;
3101 default:
3102 ret = -EINVAL;
3103 break;
3104 }
3105 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3106 return ret;
3107}
3108
3109static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3110 struct ib_qp_attr *qp_attr,
3111 int *qp_attr_mask)
3112{
3113 unsigned long flags;
3114 int ret;
3115
3116 spin_lock_irqsave(&cm_id_priv->lock, flags);
3117 switch (cm_id_priv->id.state) {
3118 case IB_CM_REP_RCVD:
3119 case IB_CM_MRA_REP_SENT:
3120 case IB_CM_REP_SENT:
3121 case IB_CM_MRA_REP_RCVD:
3122 case IB_CM_ESTABLISHED:
Sean Heftyae7971a2005-10-24 12:33:56 -07003123 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003124 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
Sean Heftyae7971a2005-10-24 12:33:56 -07003125 if (cm_id_priv->qp_type == IB_QPT_RC) {
3126 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3127 IB_QP_RNR_RETRY |
3128 IB_QP_MAX_QP_RD_ATOMIC;
3129 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3130 qp_attr->retry_cnt = cm_id_priv->retry_count;
3131 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3132 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3133 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003134 if (cm_id_priv->alt_av.ah_attr.dlid) {
3135 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3136 qp_attr->path_mig_state = IB_MIG_REARM;
3137 }
3138 ret = 0;
3139 break;
3140 default:
3141 ret = -EINVAL;
3142 break;
3143 }
3144 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3145 return ret;
3146}
3147
3148int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3149 struct ib_qp_attr *qp_attr,
3150 int *qp_attr_mask)
3151{
3152 struct cm_id_private *cm_id_priv;
3153 int ret;
3154
3155 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3156 switch (qp_attr->qp_state) {
3157 case IB_QPS_INIT:
3158 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3159 break;
3160 case IB_QPS_RTR:
3161 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3162 break;
3163 case IB_QPS_RTS:
3164 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3165 break;
3166 default:
3167 ret = -EINVAL;
3168 break;
3169 }
3170 return ret;
3171}
3172EXPORT_SYMBOL(ib_cm_init_qp_attr);
3173
Hal Rosenstocka9770492005-07-27 11:45:40 -07003174static void cm_add_one(struct ib_device *device)
3175{
3176 struct cm_device *cm_dev;
3177 struct cm_port *port;
3178 struct ib_mad_reg_req reg_req = {
3179 .mgmt_class = IB_MGMT_CLASS_CM,
3180 .mgmt_class_version = IB_CM_CLASS_VERSION
3181 };
3182 struct ib_port_modify port_modify = {
3183 .set_port_cap_mask = IB_PORT_CM_SUP
3184 };
3185 unsigned long flags;
3186 int ret;
3187 u8 i;
3188
3189 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3190 device->phys_port_cnt, GFP_KERNEL);
3191 if (!cm_dev)
3192 return;
3193
3194 cm_dev->device = device;
Sean Heftycf311cd2006-01-10 07:39:34 -08003195 cm_dev->ca_guid = device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003196
3197 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3198 for (i = 1; i <= device->phys_port_cnt; i++) {
3199 port = &cm_dev->port[i-1];
3200 port->cm_dev = cm_dev;
3201 port->port_num = i;
3202 port->mad_agent = ib_register_mad_agent(device, i,
3203 IB_QPT_GSI,
3204 &reg_req,
3205 0,
3206 cm_send_handler,
3207 cm_recv_handler,
3208 port);
3209 if (IS_ERR(port->mad_agent))
Sean Heftycf311cd2006-01-10 07:39:34 -08003210 goto error1;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003211
3212 ret = ib_modify_port(device, i, 0, &port_modify);
3213 if (ret)
Sean Heftycf311cd2006-01-10 07:39:34 -08003214 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003215 }
3216 ib_set_client_data(device, &cm_client, cm_dev);
3217
3218 write_lock_irqsave(&cm.device_lock, flags);
3219 list_add_tail(&cm_dev->list, &cm.device_list);
3220 write_unlock_irqrestore(&cm.device_lock, flags);
3221 return;
3222
Hal Rosenstocka9770492005-07-27 11:45:40 -07003223error2:
Sean Heftycf311cd2006-01-10 07:39:34 -08003224 ib_unregister_mad_agent(port->mad_agent);
3225error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003226 port_modify.set_port_cap_mask = 0;
3227 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3228 while (--i) {
3229 port = &cm_dev->port[i-1];
3230 ib_modify_port(device, port->port_num, 0, &port_modify);
3231 ib_unregister_mad_agent(port->mad_agent);
3232 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003233 kfree(cm_dev);
3234}
3235
3236static void cm_remove_one(struct ib_device *device)
3237{
3238 struct cm_device *cm_dev;
3239 struct cm_port *port;
3240 struct ib_port_modify port_modify = {
3241 .clr_port_cap_mask = IB_PORT_CM_SUP
3242 };
3243 unsigned long flags;
3244 int i;
3245
3246 cm_dev = ib_get_client_data(device, &cm_client);
3247 if (!cm_dev)
3248 return;
3249
3250 write_lock_irqsave(&cm.device_lock, flags);
3251 list_del(&cm_dev->list);
3252 write_unlock_irqrestore(&cm.device_lock, flags);
3253
3254 for (i = 1; i <= device->phys_port_cnt; i++) {
3255 port = &cm_dev->port[i-1];
3256 ib_modify_port(device, port->port_num, 0, &port_modify);
3257 ib_unregister_mad_agent(port->mad_agent);
3258 }
3259 kfree(cm_dev);
3260}
3261
3262static int __init ib_cm_init(void)
3263{
3264 int ret;
3265
3266 memset(&cm, 0, sizeof cm);
3267 INIT_LIST_HEAD(&cm.device_list);
3268 rwlock_init(&cm.device_lock);
3269 spin_lock_init(&cm.lock);
3270 cm.listen_service_table = RB_ROOT;
3271 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3272 cm.remote_id_table = RB_ROOT;
3273 cm.remote_qp_table = RB_ROOT;
3274 cm.remote_sidr_table = RB_ROOT;
3275 idr_init(&cm.local_id_table);
3276 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3277
3278 cm.wq = create_workqueue("ib_cm");
3279 if (!cm.wq)
3280 return -ENOMEM;
3281
3282 ret = ib_register_client(&cm_client);
3283 if (ret)
3284 goto error;
3285
3286 return 0;
3287error:
3288 destroy_workqueue(cm.wq);
3289 return ret;
3290}
3291
3292static void __exit ib_cm_cleanup(void)
3293{
3294 flush_workqueue(cm.wq);
3295 destroy_workqueue(cm.wq);
3296 ib_unregister_client(&cm_client);
Roland Dreier5d7edb32005-10-24 10:53:25 -07003297 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003298}
3299
3300module_init(ib_cm_init);
3301module_exit(ib_cm_cleanup);
3302