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