revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / drivers / infiniband / core / cm.c
blob2e39236d189ff88e3d919629c9f756e61a7d4d6b
1 /*
2 * Copyright (c) 2004-2006 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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
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.
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.
35 * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/err.h>
41 #include <linux/idr.h>
42 #include <linux/interrupt.h>
43 #include <linux/random.h>
44 #include <linux/rbtree.h>
45 #include <linux/spinlock.h>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_cache.h>
49 #include <rdma/ib_cm.h>
50 #include "cm_msgs.h"
52 MODULE_AUTHOR("Sean Hefty");
53 MODULE_DESCRIPTION("InfiniBand CM");
54 MODULE_LICENSE("Dual BSD/GPL");
56 static void cm_add_one(struct ib_device *device);
57 static void cm_remove_one(struct ib_device *device);
59 static struct ib_client cm_client = {
60 .name = "cm",
61 .add = cm_add_one,
62 .remove = cm_remove_one
65 static struct ib_cm {
66 spinlock_t lock;
67 struct list_head device_list;
68 rwlock_t device_lock;
69 struct rb_root listen_service_table;
70 u64 listen_service_id;
71 /* struct rb_root peer_service_table; todo: fix peer to peer */
72 struct rb_root remote_qp_table;
73 struct rb_root remote_id_table;
74 struct rb_root remote_sidr_table;
75 struct idr local_id_table;
76 __be32 random_id_operand;
77 struct list_head timewait_list;
78 struct workqueue_struct *wq;
79 } cm;
81 struct cm_port {
82 struct cm_device *cm_dev;
83 struct ib_mad_agent *mad_agent;
84 u8 port_num;
87 struct cm_device {
88 struct list_head list;
89 struct ib_device *device;
90 u8 ack_delay;
91 struct cm_port port[0];
94 struct cm_av {
95 struct cm_port *port;
96 union ib_gid dgid;
97 struct ib_ah_attr ah_attr;
98 u16 pkey_index;
99 u8 timeout;
102 struct cm_work {
103 struct delayed_work work;
104 struct list_head list;
105 struct cm_port *port;
106 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
107 __be32 local_id; /* Established / timewait */
108 __be32 remote_id;
109 struct ib_cm_event cm_event;
110 struct ib_sa_path_rec path[0];
113 struct cm_timewait_info {
114 struct cm_work work; /* Must be first. */
115 struct list_head list;
116 struct rb_node remote_qp_node;
117 struct rb_node remote_id_node;
118 __be64 remote_ca_guid;
119 __be32 remote_qpn;
120 u8 inserted_remote_qp;
121 u8 inserted_remote_id;
124 struct cm_id_private {
125 struct ib_cm_id id;
127 struct rb_node service_node;
128 struct rb_node sidr_id_node;
129 spinlock_t lock; /* Do not acquire inside cm.lock */
130 struct completion comp;
131 atomic_t refcount;
133 struct ib_mad_send_buf *msg;
134 struct cm_timewait_info *timewait_info;
135 /* todo: use alternate port on send failure */
136 struct cm_av av;
137 struct cm_av alt_av;
138 struct ib_cm_compare_data *compare_data;
140 void *private_data;
141 __be64 tid;
142 __be32 local_qpn;
143 __be32 remote_qpn;
144 enum ib_qp_type qp_type;
145 __be32 sq_psn;
146 __be32 rq_psn;
147 int timeout_ms;
148 enum ib_mtu path_mtu;
149 __be16 pkey;
150 u8 private_data_len;
151 u8 max_cm_retries;
152 u8 peer_to_peer;
153 u8 responder_resources;
154 u8 initiator_depth;
155 u8 retry_count;
156 u8 rnr_retry_count;
157 u8 service_timeout;
158 u8 target_ack_delay;
160 struct list_head work_list;
161 atomic_t work_count;
164 static void cm_work_handler(struct work_struct *work);
166 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
168 if (atomic_dec_and_test(&cm_id_priv->refcount))
169 complete(&cm_id_priv->comp);
172 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
173 struct ib_mad_send_buf **msg)
175 struct ib_mad_agent *mad_agent;
176 struct ib_mad_send_buf *m;
177 struct ib_ah *ah;
179 mad_agent = cm_id_priv->av.port->mad_agent;
180 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
181 if (IS_ERR(ah))
182 return PTR_ERR(ah);
184 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
185 cm_id_priv->av.pkey_index,
186 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
187 GFP_ATOMIC);
188 if (IS_ERR(m)) {
189 ib_destroy_ah(ah);
190 return PTR_ERR(m);
193 /* Timeout set by caller if response is expected. */
194 m->ah = ah;
195 m->retries = cm_id_priv->max_cm_retries;
197 atomic_inc(&cm_id_priv->refcount);
198 m->context[0] = cm_id_priv;
199 *msg = m;
200 return 0;
203 static int cm_alloc_response_msg(struct cm_port *port,
204 struct ib_mad_recv_wc *mad_recv_wc,
205 struct ib_mad_send_buf **msg)
207 struct ib_mad_send_buf *m;
208 struct ib_ah *ah;
210 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
211 mad_recv_wc->recv_buf.grh, port->port_num);
212 if (IS_ERR(ah))
213 return PTR_ERR(ah);
215 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
216 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
217 GFP_ATOMIC);
218 if (IS_ERR(m)) {
219 ib_destroy_ah(ah);
220 return PTR_ERR(m);
222 m->ah = ah;
223 *msg = m;
224 return 0;
227 static void cm_free_msg(struct ib_mad_send_buf *msg)
229 ib_destroy_ah(msg->ah);
230 if (msg->context[0])
231 cm_deref_id(msg->context[0]);
232 ib_free_send_mad(msg);
235 static void * cm_copy_private_data(const void *private_data,
236 u8 private_data_len)
238 void *data;
240 if (!private_data || !private_data_len)
241 return NULL;
243 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
244 if (!data)
245 return ERR_PTR(-ENOMEM);
247 return data;
250 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
251 void *private_data, u8 private_data_len)
253 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
254 kfree(cm_id_priv->private_data);
256 cm_id_priv->private_data = private_data;
257 cm_id_priv->private_data_len = private_data_len;
260 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
261 struct ib_grh *grh, struct cm_av *av)
263 av->port = port;
264 av->pkey_index = wc->pkey_index;
265 ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc,
266 grh, &av->ah_attr);
269 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
271 struct cm_device *cm_dev;
272 struct cm_port *port = NULL;
273 unsigned long flags;
274 int ret;
275 u8 p;
277 read_lock_irqsave(&cm.device_lock, flags);
278 list_for_each_entry(cm_dev, &cm.device_list, list) {
279 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
280 &p, NULL)) {
281 port = &cm_dev->port[p-1];
282 break;
285 read_unlock_irqrestore(&cm.device_lock, flags);
287 if (!port)
288 return -EINVAL;
290 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
291 be16_to_cpu(path->pkey), &av->pkey_index);
292 if (ret)
293 return ret;
295 av->port = port;
296 ib_init_ah_from_path(cm_dev->device, port->port_num, path,
297 &av->ah_attr);
298 av->timeout = path->packet_life_time + 1;
299 return 0;
302 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
304 unsigned long flags;
305 int ret, id;
306 static int next_id;
308 do {
309 spin_lock_irqsave(&cm.lock, flags);
310 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
311 next_id, &id);
312 if (!ret)
313 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
314 spin_unlock_irqrestore(&cm.lock, flags);
315 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
317 cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand);
318 return ret;
321 static void cm_free_id(__be32 local_id)
323 spin_lock_irq(&cm.lock);
324 idr_remove(&cm.local_id_table,
325 (__force int) (local_id ^ cm.random_id_operand));
326 spin_unlock_irq(&cm.lock);
329 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
331 struct cm_id_private *cm_id_priv;
333 cm_id_priv = idr_find(&cm.local_id_table,
334 (__force int) (local_id ^ cm.random_id_operand));
335 if (cm_id_priv) {
336 if (cm_id_priv->id.remote_id == remote_id)
337 atomic_inc(&cm_id_priv->refcount);
338 else
339 cm_id_priv = NULL;
342 return cm_id_priv;
345 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
347 struct cm_id_private *cm_id_priv;
349 spin_lock_irq(&cm.lock);
350 cm_id_priv = cm_get_id(local_id, remote_id);
351 spin_unlock_irq(&cm.lock);
353 return cm_id_priv;
356 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
358 int i;
360 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
361 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
362 ((unsigned long *) mask)[i];
365 static int cm_compare_data(struct ib_cm_compare_data *src_data,
366 struct ib_cm_compare_data *dst_data)
368 u8 src[IB_CM_COMPARE_SIZE];
369 u8 dst[IB_CM_COMPARE_SIZE];
371 if (!src_data || !dst_data)
372 return 0;
374 cm_mask_copy(src, src_data->data, dst_data->mask);
375 cm_mask_copy(dst, dst_data->data, src_data->mask);
376 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
379 static int cm_compare_private_data(u8 *private_data,
380 struct ib_cm_compare_data *dst_data)
382 u8 src[IB_CM_COMPARE_SIZE];
384 if (!dst_data)
385 return 0;
387 cm_mask_copy(src, private_data, dst_data->mask);
388 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
391 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
393 struct rb_node **link = &cm.listen_service_table.rb_node;
394 struct rb_node *parent = NULL;
395 struct cm_id_private *cur_cm_id_priv;
396 __be64 service_id = cm_id_priv->id.service_id;
397 __be64 service_mask = cm_id_priv->id.service_mask;
398 int data_cmp;
400 while (*link) {
401 parent = *link;
402 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
403 service_node);
404 data_cmp = cm_compare_data(cm_id_priv->compare_data,
405 cur_cm_id_priv->compare_data);
406 if ((cur_cm_id_priv->id.service_mask & service_id) ==
407 (service_mask & cur_cm_id_priv->id.service_id) &&
408 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
409 !data_cmp)
410 return cur_cm_id_priv;
412 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
413 link = &(*link)->rb_left;
414 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
415 link = &(*link)->rb_right;
416 else if (service_id < cur_cm_id_priv->id.service_id)
417 link = &(*link)->rb_left;
418 else if (service_id > cur_cm_id_priv->id.service_id)
419 link = &(*link)->rb_right;
420 else if (data_cmp < 0)
421 link = &(*link)->rb_left;
422 else
423 link = &(*link)->rb_right;
425 rb_link_node(&cm_id_priv->service_node, parent, link);
426 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
427 return NULL;
430 static struct cm_id_private * cm_find_listen(struct ib_device *device,
431 __be64 service_id,
432 u8 *private_data)
434 struct rb_node *node = cm.listen_service_table.rb_node;
435 struct cm_id_private *cm_id_priv;
436 int data_cmp;
438 while (node) {
439 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
440 data_cmp = cm_compare_private_data(private_data,
441 cm_id_priv->compare_data);
442 if ((cm_id_priv->id.service_mask & service_id) ==
443 cm_id_priv->id.service_id &&
444 (cm_id_priv->id.device == device) && !data_cmp)
445 return cm_id_priv;
447 if (device < cm_id_priv->id.device)
448 node = node->rb_left;
449 else if (device > cm_id_priv->id.device)
450 node = node->rb_right;
451 else if (service_id < cm_id_priv->id.service_id)
452 node = node->rb_left;
453 else if (service_id > cm_id_priv->id.service_id)
454 node = node->rb_right;
455 else if (data_cmp < 0)
456 node = node->rb_left;
457 else
458 node = node->rb_right;
460 return NULL;
463 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
464 *timewait_info)
466 struct rb_node **link = &cm.remote_id_table.rb_node;
467 struct rb_node *parent = NULL;
468 struct cm_timewait_info *cur_timewait_info;
469 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
470 __be32 remote_id = timewait_info->work.remote_id;
472 while (*link) {
473 parent = *link;
474 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
475 remote_id_node);
476 if (remote_id < cur_timewait_info->work.remote_id)
477 link = &(*link)->rb_left;
478 else if (remote_id > cur_timewait_info->work.remote_id)
479 link = &(*link)->rb_right;
480 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
481 link = &(*link)->rb_left;
482 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
483 link = &(*link)->rb_right;
484 else
485 return cur_timewait_info;
487 timewait_info->inserted_remote_id = 1;
488 rb_link_node(&timewait_info->remote_id_node, parent, link);
489 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
490 return NULL;
493 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
494 __be32 remote_id)
496 struct rb_node *node = cm.remote_id_table.rb_node;
497 struct cm_timewait_info *timewait_info;
499 while (node) {
500 timewait_info = rb_entry(node, struct cm_timewait_info,
501 remote_id_node);
502 if (remote_id < timewait_info->work.remote_id)
503 node = node->rb_left;
504 else if (remote_id > timewait_info->work.remote_id)
505 node = node->rb_right;
506 else if (remote_ca_guid < timewait_info->remote_ca_guid)
507 node = node->rb_left;
508 else if (remote_ca_guid > timewait_info->remote_ca_guid)
509 node = node->rb_right;
510 else
511 return timewait_info;
513 return NULL;
516 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
517 *timewait_info)
519 struct rb_node **link = &cm.remote_qp_table.rb_node;
520 struct rb_node *parent = NULL;
521 struct cm_timewait_info *cur_timewait_info;
522 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
523 __be32 remote_qpn = timewait_info->remote_qpn;
525 while (*link) {
526 parent = *link;
527 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
528 remote_qp_node);
529 if (remote_qpn < cur_timewait_info->remote_qpn)
530 link = &(*link)->rb_left;
531 else if (remote_qpn > cur_timewait_info->remote_qpn)
532 link = &(*link)->rb_right;
533 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
534 link = &(*link)->rb_left;
535 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
536 link = &(*link)->rb_right;
537 else
538 return cur_timewait_info;
540 timewait_info->inserted_remote_qp = 1;
541 rb_link_node(&timewait_info->remote_qp_node, parent, link);
542 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
543 return NULL;
546 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
547 *cm_id_priv)
549 struct rb_node **link = &cm.remote_sidr_table.rb_node;
550 struct rb_node *parent = NULL;
551 struct cm_id_private *cur_cm_id_priv;
552 union ib_gid *port_gid = &cm_id_priv->av.dgid;
553 __be32 remote_id = cm_id_priv->id.remote_id;
555 while (*link) {
556 parent = *link;
557 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
558 sidr_id_node);
559 if (remote_id < cur_cm_id_priv->id.remote_id)
560 link = &(*link)->rb_left;
561 else if (remote_id > cur_cm_id_priv->id.remote_id)
562 link = &(*link)->rb_right;
563 else {
564 int cmp;
565 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
566 sizeof *port_gid);
567 if (cmp < 0)
568 link = &(*link)->rb_left;
569 else if (cmp > 0)
570 link = &(*link)->rb_right;
571 else
572 return cur_cm_id_priv;
575 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
576 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
577 return NULL;
580 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
581 enum ib_cm_sidr_status status)
583 struct ib_cm_sidr_rep_param param;
585 memset(&param, 0, sizeof param);
586 param.status = status;
587 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
590 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
591 ib_cm_handler cm_handler,
592 void *context)
594 struct cm_id_private *cm_id_priv;
595 int ret;
597 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
598 if (!cm_id_priv)
599 return ERR_PTR(-ENOMEM);
601 cm_id_priv->id.state = IB_CM_IDLE;
602 cm_id_priv->id.device = device;
603 cm_id_priv->id.cm_handler = cm_handler;
604 cm_id_priv->id.context = context;
605 cm_id_priv->id.remote_cm_qpn = 1;
606 ret = cm_alloc_id(cm_id_priv);
607 if (ret)
608 goto error;
610 spin_lock_init(&cm_id_priv->lock);
611 init_completion(&cm_id_priv->comp);
612 INIT_LIST_HEAD(&cm_id_priv->work_list);
613 atomic_set(&cm_id_priv->work_count, -1);
614 atomic_set(&cm_id_priv->refcount, 1);
615 return &cm_id_priv->id;
617 error:
618 kfree(cm_id_priv);
619 return ERR_PTR(-ENOMEM);
621 EXPORT_SYMBOL(ib_create_cm_id);
623 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
625 struct cm_work *work;
627 if (list_empty(&cm_id_priv->work_list))
628 return NULL;
630 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
631 list_del(&work->list);
632 return work;
635 static void cm_free_work(struct cm_work *work)
637 if (work->mad_recv_wc)
638 ib_free_recv_mad(work->mad_recv_wc);
639 kfree(work);
642 static inline int cm_convert_to_ms(int iba_time)
644 /* approximate conversion to ms from 4.096us x 2^iba_time */
645 return 1 << max(iba_time - 8, 0);
649 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
650 * Because of how ack_timeout is stored, adding one doubles the timeout.
651 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
652 * increment it (round up) only if the other is within 50%.
654 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
656 int ack_timeout = packet_life_time + 1;
658 if (ack_timeout >= ca_ack_delay)
659 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
660 else
661 ack_timeout = ca_ack_delay +
662 (ack_timeout >= (ca_ack_delay - 1));
664 return min(31, ack_timeout);
667 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
669 if (timewait_info->inserted_remote_id) {
670 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
671 timewait_info->inserted_remote_id = 0;
674 if (timewait_info->inserted_remote_qp) {
675 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
676 timewait_info->inserted_remote_qp = 0;
680 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
682 struct cm_timewait_info *timewait_info;
684 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
685 if (!timewait_info)
686 return ERR_PTR(-ENOMEM);
688 timewait_info->work.local_id = local_id;
689 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
690 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
691 return timewait_info;
694 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
696 int wait_time;
697 unsigned long flags;
699 spin_lock_irqsave(&cm.lock, flags);
700 cm_cleanup_timewait(cm_id_priv->timewait_info);
701 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
702 spin_unlock_irqrestore(&cm.lock, flags);
705 * The cm_id could be destroyed by the user before we exit timewait.
706 * To protect against this, we search for the cm_id after exiting
707 * timewait before notifying the user that we've exited timewait.
709 cm_id_priv->id.state = IB_CM_TIMEWAIT;
710 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
711 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
712 msecs_to_jiffies(wait_time));
713 cm_id_priv->timewait_info = NULL;
716 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
718 unsigned long flags;
720 cm_id_priv->id.state = IB_CM_IDLE;
721 if (cm_id_priv->timewait_info) {
722 spin_lock_irqsave(&cm.lock, flags);
723 cm_cleanup_timewait(cm_id_priv->timewait_info);
724 spin_unlock_irqrestore(&cm.lock, flags);
725 kfree(cm_id_priv->timewait_info);
726 cm_id_priv->timewait_info = NULL;
730 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
732 struct cm_id_private *cm_id_priv;
733 struct cm_work *work;
735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
736 retest:
737 spin_lock_irq(&cm_id_priv->lock);
738 switch (cm_id->state) {
739 case IB_CM_LISTEN:
740 cm_id->state = IB_CM_IDLE;
741 spin_unlock_irq(&cm_id_priv->lock);
742 spin_lock_irq(&cm.lock);
743 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
744 spin_unlock_irq(&cm.lock);
745 break;
746 case IB_CM_SIDR_REQ_SENT:
747 cm_id->state = IB_CM_IDLE;
748 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
749 spin_unlock_irq(&cm_id_priv->lock);
750 break;
751 case IB_CM_SIDR_REQ_RCVD:
752 spin_unlock_irq(&cm_id_priv->lock);
753 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
754 break;
755 case IB_CM_REQ_SENT:
756 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
757 spin_unlock_irq(&cm_id_priv->lock);
758 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
759 &cm_id_priv->id.device->node_guid,
760 sizeof cm_id_priv->id.device->node_guid,
761 NULL, 0);
762 break;
763 case IB_CM_REQ_RCVD:
764 if (err == -ENOMEM) {
765 /* Do not reject to allow future retries. */
766 cm_reset_to_idle(cm_id_priv);
767 spin_unlock_irq(&cm_id_priv->lock);
768 } else {
769 spin_unlock_irq(&cm_id_priv->lock);
770 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
771 NULL, 0, NULL, 0);
773 break;
774 case IB_CM_MRA_REQ_RCVD:
775 case IB_CM_REP_SENT:
776 case IB_CM_MRA_REP_RCVD:
777 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
778 /* Fall through */
779 case IB_CM_MRA_REQ_SENT:
780 case IB_CM_REP_RCVD:
781 case IB_CM_MRA_REP_SENT:
782 spin_unlock_irq(&cm_id_priv->lock);
783 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
784 NULL, 0, NULL, 0);
785 break;
786 case IB_CM_ESTABLISHED:
787 spin_unlock_irq(&cm_id_priv->lock);
788 ib_send_cm_dreq(cm_id, NULL, 0);
789 goto retest;
790 case IB_CM_DREQ_SENT:
791 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
792 cm_enter_timewait(cm_id_priv);
793 spin_unlock_irq(&cm_id_priv->lock);
794 break;
795 case IB_CM_DREQ_RCVD:
796 spin_unlock_irq(&cm_id_priv->lock);
797 ib_send_cm_drep(cm_id, NULL, 0);
798 break;
799 default:
800 spin_unlock_irq(&cm_id_priv->lock);
801 break;
804 cm_free_id(cm_id->local_id);
805 cm_deref_id(cm_id_priv);
806 wait_for_completion(&cm_id_priv->comp);
807 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
808 cm_free_work(work);
809 kfree(cm_id_priv->compare_data);
810 kfree(cm_id_priv->private_data);
811 kfree(cm_id_priv);
814 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
816 cm_destroy_id(cm_id, 0);
818 EXPORT_SYMBOL(ib_destroy_cm_id);
820 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
821 struct ib_cm_compare_data *compare_data)
823 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
824 unsigned long flags;
825 int ret = 0;
827 service_mask = service_mask ? service_mask :
828 __constant_cpu_to_be64(~0ULL);
829 service_id &= service_mask;
830 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
831 (service_id != IB_CM_ASSIGN_SERVICE_ID))
832 return -EINVAL;
834 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
835 if (cm_id->state != IB_CM_IDLE)
836 return -EINVAL;
838 if (compare_data) {
839 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
840 GFP_KERNEL);
841 if (!cm_id_priv->compare_data)
842 return -ENOMEM;
843 cm_mask_copy(cm_id_priv->compare_data->data,
844 compare_data->data, compare_data->mask);
845 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
846 IB_CM_COMPARE_SIZE);
849 cm_id->state = IB_CM_LISTEN;
851 spin_lock_irqsave(&cm.lock, flags);
852 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
853 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
854 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
855 } else {
856 cm_id->service_id = service_id;
857 cm_id->service_mask = service_mask;
859 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
860 spin_unlock_irqrestore(&cm.lock, flags);
862 if (cur_cm_id_priv) {
863 cm_id->state = IB_CM_IDLE;
864 kfree(cm_id_priv->compare_data);
865 cm_id_priv->compare_data = NULL;
866 ret = -EBUSY;
868 return ret;
870 EXPORT_SYMBOL(ib_cm_listen);
872 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
873 enum cm_msg_sequence msg_seq)
875 u64 hi_tid, low_tid;
877 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
878 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
879 (msg_seq << 30));
880 return cpu_to_be64(hi_tid | low_tid);
883 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
884 __be16 attr_id, __be64 tid)
886 hdr->base_version = IB_MGMT_BASE_VERSION;
887 hdr->mgmt_class = IB_MGMT_CLASS_CM;
888 hdr->class_version = IB_CM_CLASS_VERSION;
889 hdr->method = IB_MGMT_METHOD_SEND;
890 hdr->attr_id = attr_id;
891 hdr->tid = tid;
894 static void cm_format_req(struct cm_req_msg *req_msg,
895 struct cm_id_private *cm_id_priv,
896 struct ib_cm_req_param *param)
898 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
899 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
901 req_msg->local_comm_id = cm_id_priv->id.local_id;
902 req_msg->service_id = param->service_id;
903 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
904 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
905 cm_req_set_resp_res(req_msg, param->responder_resources);
906 cm_req_set_init_depth(req_msg, param->initiator_depth);
907 cm_req_set_remote_resp_timeout(req_msg,
908 param->remote_cm_response_timeout);
909 cm_req_set_qp_type(req_msg, param->qp_type);
910 cm_req_set_flow_ctrl(req_msg, param->flow_control);
911 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
912 cm_req_set_local_resp_timeout(req_msg,
913 param->local_cm_response_timeout);
914 cm_req_set_retry_count(req_msg, param->retry_count);
915 req_msg->pkey = param->primary_path->pkey;
916 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
917 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
918 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
919 cm_req_set_srq(req_msg, param->srq);
921 req_msg->primary_local_lid = param->primary_path->slid;
922 req_msg->primary_remote_lid = param->primary_path->dlid;
923 req_msg->primary_local_gid = param->primary_path->sgid;
924 req_msg->primary_remote_gid = param->primary_path->dgid;
925 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
926 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
927 req_msg->primary_traffic_class = param->primary_path->traffic_class;
928 req_msg->primary_hop_limit = param->primary_path->hop_limit;
929 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
930 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
931 cm_req_set_primary_local_ack_timeout(req_msg,
932 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
933 param->primary_path->packet_life_time));
935 if (param->alternate_path) {
936 req_msg->alt_local_lid = param->alternate_path->slid;
937 req_msg->alt_remote_lid = param->alternate_path->dlid;
938 req_msg->alt_local_gid = param->alternate_path->sgid;
939 req_msg->alt_remote_gid = param->alternate_path->dgid;
940 cm_req_set_alt_flow_label(req_msg,
941 param->alternate_path->flow_label);
942 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
943 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
944 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
945 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
946 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
947 cm_req_set_alt_local_ack_timeout(req_msg,
948 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
949 param->alternate_path->packet_life_time));
952 if (param->private_data && param->private_data_len)
953 memcpy(req_msg->private_data, param->private_data,
954 param->private_data_len);
957 static int cm_validate_req_param(struct ib_cm_req_param *param)
959 /* peer-to-peer not supported */
960 if (param->peer_to_peer)
961 return -EINVAL;
963 if (!param->primary_path)
964 return -EINVAL;
966 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
967 return -EINVAL;
969 if (param->private_data &&
970 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
971 return -EINVAL;
973 if (param->alternate_path &&
974 (param->alternate_path->pkey != param->primary_path->pkey ||
975 param->alternate_path->mtu != param->primary_path->mtu))
976 return -EINVAL;
978 return 0;
981 int ib_send_cm_req(struct ib_cm_id *cm_id,
982 struct ib_cm_req_param *param)
984 struct cm_id_private *cm_id_priv;
985 struct cm_req_msg *req_msg;
986 unsigned long flags;
987 int ret;
989 ret = cm_validate_req_param(param);
990 if (ret)
991 return ret;
993 /* Verify that we're not in timewait. */
994 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
995 spin_lock_irqsave(&cm_id_priv->lock, flags);
996 if (cm_id->state != IB_CM_IDLE) {
997 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
998 ret = -EINVAL;
999 goto out;
1001 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1003 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1004 id.local_id);
1005 if (IS_ERR(cm_id_priv->timewait_info)) {
1006 ret = PTR_ERR(cm_id_priv->timewait_info);
1007 goto out;
1010 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1011 if (ret)
1012 goto error1;
1013 if (param->alternate_path) {
1014 ret = cm_init_av_by_path(param->alternate_path,
1015 &cm_id_priv->alt_av);
1016 if (ret)
1017 goto error1;
1019 cm_id->service_id = param->service_id;
1020 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
1021 cm_id_priv->timeout_ms = cm_convert_to_ms(
1022 param->primary_path->packet_life_time) * 2 +
1023 cm_convert_to_ms(
1024 param->remote_cm_response_timeout);
1025 cm_id_priv->max_cm_retries = param->max_cm_retries;
1026 cm_id_priv->initiator_depth = param->initiator_depth;
1027 cm_id_priv->responder_resources = param->responder_resources;
1028 cm_id_priv->retry_count = param->retry_count;
1029 cm_id_priv->path_mtu = param->primary_path->mtu;
1030 cm_id_priv->pkey = param->primary_path->pkey;
1031 cm_id_priv->qp_type = param->qp_type;
1033 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1034 if (ret)
1035 goto error1;
1037 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1038 cm_format_req(req_msg, cm_id_priv, param);
1039 cm_id_priv->tid = req_msg->hdr.tid;
1040 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1041 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1043 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1044 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1046 spin_lock_irqsave(&cm_id_priv->lock, flags);
1047 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1048 if (ret) {
1049 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1050 goto error2;
1052 BUG_ON(cm_id->state != IB_CM_IDLE);
1053 cm_id->state = IB_CM_REQ_SENT;
1054 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1055 return 0;
1057 error2: cm_free_msg(cm_id_priv->msg);
1058 error1: kfree(cm_id_priv->timewait_info);
1059 out: return ret;
1061 EXPORT_SYMBOL(ib_send_cm_req);
1063 static int cm_issue_rej(struct cm_port *port,
1064 struct ib_mad_recv_wc *mad_recv_wc,
1065 enum ib_cm_rej_reason reason,
1066 enum cm_msg_response msg_rejected,
1067 void *ari, u8 ari_length)
1069 struct ib_mad_send_buf *msg = NULL;
1070 struct cm_rej_msg *rej_msg, *rcv_msg;
1071 int ret;
1073 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1074 if (ret)
1075 return ret;
1077 /* We just need common CM header information. Cast to any message. */
1078 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1079 rej_msg = (struct cm_rej_msg *) msg->mad;
1081 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1082 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1083 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1084 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1085 rej_msg->reason = cpu_to_be16(reason);
1087 if (ari && ari_length) {
1088 cm_rej_set_reject_info_len(rej_msg, ari_length);
1089 memcpy(rej_msg->ari, ari, ari_length);
1092 ret = ib_post_send_mad(msg, NULL);
1093 if (ret)
1094 cm_free_msg(msg);
1096 return ret;
1099 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1100 __be32 local_qpn, __be32 remote_qpn)
1102 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1103 ((local_ca_guid == remote_ca_guid) &&
1104 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1107 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1108 struct ib_sa_path_rec *primary_path,
1109 struct ib_sa_path_rec *alt_path)
1111 memset(primary_path, 0, sizeof *primary_path);
1112 primary_path->dgid = req_msg->primary_local_gid;
1113 primary_path->sgid = req_msg->primary_remote_gid;
1114 primary_path->dlid = req_msg->primary_local_lid;
1115 primary_path->slid = req_msg->primary_remote_lid;
1116 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1117 primary_path->hop_limit = req_msg->primary_hop_limit;
1118 primary_path->traffic_class = req_msg->primary_traffic_class;
1119 primary_path->reversible = 1;
1120 primary_path->pkey = req_msg->pkey;
1121 primary_path->sl = cm_req_get_primary_sl(req_msg);
1122 primary_path->mtu_selector = IB_SA_EQ;
1123 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1124 primary_path->rate_selector = IB_SA_EQ;
1125 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1126 primary_path->packet_life_time_selector = IB_SA_EQ;
1127 primary_path->packet_life_time =
1128 cm_req_get_primary_local_ack_timeout(req_msg);
1129 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1131 if (req_msg->alt_local_lid) {
1132 memset(alt_path, 0, sizeof *alt_path);
1133 alt_path->dgid = req_msg->alt_local_gid;
1134 alt_path->sgid = req_msg->alt_remote_gid;
1135 alt_path->dlid = req_msg->alt_local_lid;
1136 alt_path->slid = req_msg->alt_remote_lid;
1137 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1138 alt_path->hop_limit = req_msg->alt_hop_limit;
1139 alt_path->traffic_class = req_msg->alt_traffic_class;
1140 alt_path->reversible = 1;
1141 alt_path->pkey = req_msg->pkey;
1142 alt_path->sl = cm_req_get_alt_sl(req_msg);
1143 alt_path->mtu_selector = IB_SA_EQ;
1144 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1145 alt_path->rate_selector = IB_SA_EQ;
1146 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1147 alt_path->packet_life_time_selector = IB_SA_EQ;
1148 alt_path->packet_life_time =
1149 cm_req_get_alt_local_ack_timeout(req_msg);
1150 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1154 static void cm_format_req_event(struct cm_work *work,
1155 struct cm_id_private *cm_id_priv,
1156 struct ib_cm_id *listen_id)
1158 struct cm_req_msg *req_msg;
1159 struct ib_cm_req_event_param *param;
1161 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1162 param = &work->cm_event.param.req_rcvd;
1163 param->listen_id = listen_id;
1164 param->port = cm_id_priv->av.port->port_num;
1165 param->primary_path = &work->path[0];
1166 if (req_msg->alt_local_lid)
1167 param->alternate_path = &work->path[1];
1168 else
1169 param->alternate_path = NULL;
1170 param->remote_ca_guid = req_msg->local_ca_guid;
1171 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1172 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1173 param->qp_type = cm_req_get_qp_type(req_msg);
1174 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1175 param->responder_resources = cm_req_get_init_depth(req_msg);
1176 param->initiator_depth = cm_req_get_resp_res(req_msg);
1177 param->local_cm_response_timeout =
1178 cm_req_get_remote_resp_timeout(req_msg);
1179 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1180 param->remote_cm_response_timeout =
1181 cm_req_get_local_resp_timeout(req_msg);
1182 param->retry_count = cm_req_get_retry_count(req_msg);
1183 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1184 param->srq = cm_req_get_srq(req_msg);
1185 work->cm_event.private_data = &req_msg->private_data;
1188 static void cm_process_work(struct cm_id_private *cm_id_priv,
1189 struct cm_work *work)
1191 int ret;
1193 /* We will typically only have the current event to report. */
1194 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1195 cm_free_work(work);
1197 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1198 spin_lock_irq(&cm_id_priv->lock);
1199 work = cm_dequeue_work(cm_id_priv);
1200 spin_unlock_irq(&cm_id_priv->lock);
1201 BUG_ON(!work);
1202 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1203 &work->cm_event);
1204 cm_free_work(work);
1206 cm_deref_id(cm_id_priv);
1207 if (ret)
1208 cm_destroy_id(&cm_id_priv->id, ret);
1211 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1212 struct cm_id_private *cm_id_priv,
1213 enum cm_msg_response msg_mraed, u8 service_timeout,
1214 const void *private_data, u8 private_data_len)
1216 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1217 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1218 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1219 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1220 cm_mra_set_service_timeout(mra_msg, service_timeout);
1222 if (private_data && private_data_len)
1223 memcpy(mra_msg->private_data, private_data, private_data_len);
1226 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1227 struct cm_id_private *cm_id_priv,
1228 enum ib_cm_rej_reason reason,
1229 void *ari,
1230 u8 ari_length,
1231 const void *private_data,
1232 u8 private_data_len)
1234 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1235 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1237 switch(cm_id_priv->id.state) {
1238 case IB_CM_REQ_RCVD:
1239 rej_msg->local_comm_id = 0;
1240 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1241 break;
1242 case IB_CM_MRA_REQ_SENT:
1243 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1244 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1245 break;
1246 case IB_CM_REP_RCVD:
1247 case IB_CM_MRA_REP_SENT:
1248 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1249 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1250 break;
1251 default:
1252 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1253 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1254 break;
1257 rej_msg->reason = cpu_to_be16(reason);
1258 if (ari && ari_length) {
1259 cm_rej_set_reject_info_len(rej_msg, ari_length);
1260 memcpy(rej_msg->ari, ari, ari_length);
1263 if (private_data && private_data_len)
1264 memcpy(rej_msg->private_data, private_data, private_data_len);
1267 static void cm_dup_req_handler(struct cm_work *work,
1268 struct cm_id_private *cm_id_priv)
1270 struct ib_mad_send_buf *msg = NULL;
1271 int ret;
1273 /* Quick state check to discard duplicate REQs. */
1274 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1275 return;
1277 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1278 if (ret)
1279 return;
1281 spin_lock_irq(&cm_id_priv->lock);
1282 switch (cm_id_priv->id.state) {
1283 case IB_CM_MRA_REQ_SENT:
1284 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1285 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1286 cm_id_priv->private_data,
1287 cm_id_priv->private_data_len);
1288 break;
1289 case IB_CM_TIMEWAIT:
1290 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1291 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1292 break;
1293 default:
1294 goto unlock;
1296 spin_unlock_irq(&cm_id_priv->lock);
1298 ret = ib_post_send_mad(msg, NULL);
1299 if (ret)
1300 goto free;
1301 return;
1303 unlock: spin_unlock_irq(&cm_id_priv->lock);
1304 free: cm_free_msg(msg);
1307 static struct cm_id_private * cm_match_req(struct cm_work *work,
1308 struct cm_id_private *cm_id_priv)
1310 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1311 struct cm_timewait_info *timewait_info;
1312 struct cm_req_msg *req_msg;
1314 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1316 /* Check for possible duplicate REQ. */
1317 spin_lock_irq(&cm.lock);
1318 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1319 if (timewait_info) {
1320 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1321 timewait_info->work.remote_id);
1322 spin_unlock_irq(&cm.lock);
1323 if (cur_cm_id_priv) {
1324 cm_dup_req_handler(work, cur_cm_id_priv);
1325 cm_deref_id(cur_cm_id_priv);
1327 return NULL;
1330 /* Check for stale connections. */
1331 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1332 if (timewait_info) {
1333 cm_cleanup_timewait(cm_id_priv->timewait_info);
1334 spin_unlock_irq(&cm.lock);
1335 cm_issue_rej(work->port, work->mad_recv_wc,
1336 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1337 NULL, 0);
1338 return NULL;
1341 /* Find matching listen request. */
1342 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1343 req_msg->service_id,
1344 req_msg->private_data);
1345 if (!listen_cm_id_priv) {
1346 cm_cleanup_timewait(cm_id_priv->timewait_info);
1347 spin_unlock_irq(&cm.lock);
1348 cm_issue_rej(work->port, work->mad_recv_wc,
1349 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1350 NULL, 0);
1351 goto out;
1353 atomic_inc(&listen_cm_id_priv->refcount);
1354 atomic_inc(&cm_id_priv->refcount);
1355 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1356 atomic_inc(&cm_id_priv->work_count);
1357 spin_unlock_irq(&cm.lock);
1358 out:
1359 return listen_cm_id_priv;
1362 static int cm_req_handler(struct cm_work *work)
1364 struct ib_cm_id *cm_id;
1365 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1366 struct cm_req_msg *req_msg;
1367 int ret;
1369 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1371 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
1372 if (IS_ERR(cm_id))
1373 return PTR_ERR(cm_id);
1375 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1376 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1377 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1378 work->mad_recv_wc->recv_buf.grh,
1379 &cm_id_priv->av);
1380 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1381 id.local_id);
1382 if (IS_ERR(cm_id_priv->timewait_info)) {
1383 ret = PTR_ERR(cm_id_priv->timewait_info);
1384 goto destroy;
1386 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1387 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1388 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1390 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1391 if (!listen_cm_id_priv) {
1392 ret = -EINVAL;
1393 kfree(cm_id_priv->timewait_info);
1394 goto destroy;
1397 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1398 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1399 cm_id_priv->id.service_id = req_msg->service_id;
1400 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
1402 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1403 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1404 if (ret) {
1405 ib_get_cached_gid(work->port->cm_dev->device,
1406 work->port->port_num, 0, &work->path[0].sgid);
1407 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1408 &work->path[0].sgid, sizeof work->path[0].sgid,
1409 NULL, 0);
1410 goto rejected;
1412 if (req_msg->alt_local_lid) {
1413 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1414 if (ret) {
1415 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1416 &work->path[0].sgid,
1417 sizeof work->path[0].sgid, NULL, 0);
1418 goto rejected;
1421 cm_id_priv->tid = req_msg->hdr.tid;
1422 cm_id_priv->timeout_ms = cm_convert_to_ms(
1423 cm_req_get_local_resp_timeout(req_msg));
1424 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1425 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1426 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1427 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1428 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1429 cm_id_priv->pkey = req_msg->pkey;
1430 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1431 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1432 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1433 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1435 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1436 cm_process_work(cm_id_priv, work);
1437 cm_deref_id(listen_cm_id_priv);
1438 return 0;
1440 rejected:
1441 atomic_dec(&cm_id_priv->refcount);
1442 cm_deref_id(listen_cm_id_priv);
1443 destroy:
1444 ib_destroy_cm_id(cm_id);
1445 return ret;
1448 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1449 struct cm_id_private *cm_id_priv,
1450 struct ib_cm_rep_param *param)
1452 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1453 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1454 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1455 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1456 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1457 rep_msg->resp_resources = param->responder_resources;
1458 rep_msg->initiator_depth = param->initiator_depth;
1459 cm_rep_set_target_ack_delay(rep_msg,
1460 cm_id_priv->av.port->cm_dev->ack_delay);
1461 cm_rep_set_failover(rep_msg, param->failover_accepted);
1462 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1463 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1464 cm_rep_set_srq(rep_msg, param->srq);
1465 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1467 if (param->private_data && param->private_data_len)
1468 memcpy(rep_msg->private_data, param->private_data,
1469 param->private_data_len);
1472 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1473 struct ib_cm_rep_param *param)
1475 struct cm_id_private *cm_id_priv;
1476 struct ib_mad_send_buf *msg;
1477 struct cm_rep_msg *rep_msg;
1478 unsigned long flags;
1479 int ret;
1481 if (param->private_data &&
1482 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1483 return -EINVAL;
1485 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1486 spin_lock_irqsave(&cm_id_priv->lock, flags);
1487 if (cm_id->state != IB_CM_REQ_RCVD &&
1488 cm_id->state != IB_CM_MRA_REQ_SENT) {
1489 ret = -EINVAL;
1490 goto out;
1493 ret = cm_alloc_msg(cm_id_priv, &msg);
1494 if (ret)
1495 goto out;
1497 rep_msg = (struct cm_rep_msg *) msg->mad;
1498 cm_format_rep(rep_msg, cm_id_priv, param);
1499 msg->timeout_ms = cm_id_priv->timeout_ms;
1500 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1502 ret = ib_post_send_mad(msg, NULL);
1503 if (ret) {
1504 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1505 cm_free_msg(msg);
1506 return ret;
1509 cm_id->state = IB_CM_REP_SENT;
1510 cm_id_priv->msg = msg;
1511 cm_id_priv->initiator_depth = param->initiator_depth;
1512 cm_id_priv->responder_resources = param->responder_resources;
1513 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1514 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1516 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1517 return ret;
1519 EXPORT_SYMBOL(ib_send_cm_rep);
1521 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1522 struct cm_id_private *cm_id_priv,
1523 const void *private_data,
1524 u8 private_data_len)
1526 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1527 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1528 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1530 if (private_data && private_data_len)
1531 memcpy(rtu_msg->private_data, private_data, private_data_len);
1534 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1535 const void *private_data,
1536 u8 private_data_len)
1538 struct cm_id_private *cm_id_priv;
1539 struct ib_mad_send_buf *msg;
1540 unsigned long flags;
1541 void *data;
1542 int ret;
1544 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1545 return -EINVAL;
1547 data = cm_copy_private_data(private_data, private_data_len);
1548 if (IS_ERR(data))
1549 return PTR_ERR(data);
1551 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1552 spin_lock_irqsave(&cm_id_priv->lock, flags);
1553 if (cm_id->state != IB_CM_REP_RCVD &&
1554 cm_id->state != IB_CM_MRA_REP_SENT) {
1555 ret = -EINVAL;
1556 goto error;
1559 ret = cm_alloc_msg(cm_id_priv, &msg);
1560 if (ret)
1561 goto error;
1563 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1564 private_data, private_data_len);
1566 ret = ib_post_send_mad(msg, NULL);
1567 if (ret) {
1568 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1569 cm_free_msg(msg);
1570 kfree(data);
1571 return ret;
1574 cm_id->state = IB_CM_ESTABLISHED;
1575 cm_set_private_data(cm_id_priv, data, private_data_len);
1576 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1577 return 0;
1579 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1580 kfree(data);
1581 return ret;
1583 EXPORT_SYMBOL(ib_send_cm_rtu);
1585 static void cm_format_rep_event(struct cm_work *work)
1587 struct cm_rep_msg *rep_msg;
1588 struct ib_cm_rep_event_param *param;
1590 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1591 param = &work->cm_event.param.rep_rcvd;
1592 param->remote_ca_guid = rep_msg->local_ca_guid;
1593 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1594 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1595 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1596 param->responder_resources = rep_msg->initiator_depth;
1597 param->initiator_depth = rep_msg->resp_resources;
1598 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1599 param->failover_accepted = cm_rep_get_failover(rep_msg);
1600 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1601 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1602 param->srq = cm_rep_get_srq(rep_msg);
1603 work->cm_event.private_data = &rep_msg->private_data;
1606 static void cm_dup_rep_handler(struct cm_work *work)
1608 struct cm_id_private *cm_id_priv;
1609 struct cm_rep_msg *rep_msg;
1610 struct ib_mad_send_buf *msg = NULL;
1611 int ret;
1613 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1614 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1615 rep_msg->local_comm_id);
1616 if (!cm_id_priv)
1617 return;
1619 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1620 if (ret)
1621 goto deref;
1623 spin_lock_irq(&cm_id_priv->lock);
1624 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1625 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1626 cm_id_priv->private_data,
1627 cm_id_priv->private_data_len);
1628 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1629 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1630 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1631 cm_id_priv->private_data,
1632 cm_id_priv->private_data_len);
1633 else
1634 goto unlock;
1635 spin_unlock_irq(&cm_id_priv->lock);
1637 ret = ib_post_send_mad(msg, NULL);
1638 if (ret)
1639 goto free;
1640 goto deref;
1642 unlock: spin_unlock_irq(&cm_id_priv->lock);
1643 free: cm_free_msg(msg);
1644 deref: cm_deref_id(cm_id_priv);
1647 static int cm_rep_handler(struct cm_work *work)
1649 struct cm_id_private *cm_id_priv;
1650 struct cm_rep_msg *rep_msg;
1651 int ret;
1653 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1654 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1655 if (!cm_id_priv) {
1656 cm_dup_rep_handler(work);
1657 return -EINVAL;
1660 cm_format_rep_event(work);
1662 spin_lock_irq(&cm_id_priv->lock);
1663 switch (cm_id_priv->id.state) {
1664 case IB_CM_REQ_SENT:
1665 case IB_CM_MRA_REQ_RCVD:
1666 break;
1667 default:
1668 spin_unlock_irq(&cm_id_priv->lock);
1669 ret = -EINVAL;
1670 goto error;
1673 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1674 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1675 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1677 spin_lock(&cm.lock);
1678 /* Check for duplicate REP. */
1679 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1680 spin_unlock(&cm.lock);
1681 spin_unlock_irq(&cm_id_priv->lock);
1682 ret = -EINVAL;
1683 goto error;
1685 /* Check for a stale connection. */
1686 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1687 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1688 &cm.remote_id_table);
1689 cm_id_priv->timewait_info->inserted_remote_id = 0;
1690 spin_unlock(&cm.lock);
1691 spin_unlock_irq(&cm_id_priv->lock);
1692 cm_issue_rej(work->port, work->mad_recv_wc,
1693 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1694 NULL, 0);
1695 ret = -EINVAL;
1696 goto error;
1698 spin_unlock(&cm.lock);
1700 cm_id_priv->id.state = IB_CM_REP_RCVD;
1701 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1702 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1703 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1704 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1705 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1706 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1707 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1708 cm_id_priv->av.timeout =
1709 cm_ack_timeout(cm_id_priv->target_ack_delay,
1710 cm_id_priv->av.timeout - 1);
1711 cm_id_priv->alt_av.timeout =
1712 cm_ack_timeout(cm_id_priv->target_ack_delay,
1713 cm_id_priv->alt_av.timeout - 1);
1715 /* todo: handle peer_to_peer */
1717 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1718 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1719 if (!ret)
1720 list_add_tail(&work->list, &cm_id_priv->work_list);
1721 spin_unlock_irq(&cm_id_priv->lock);
1723 if (ret)
1724 cm_process_work(cm_id_priv, work);
1725 else
1726 cm_deref_id(cm_id_priv);
1727 return 0;
1729 error:
1730 cm_deref_id(cm_id_priv);
1731 return ret;
1734 static int cm_establish_handler(struct cm_work *work)
1736 struct cm_id_private *cm_id_priv;
1737 int ret;
1739 /* See comment in cm_establish about lookup. */
1740 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1741 if (!cm_id_priv)
1742 return -EINVAL;
1744 spin_lock_irq(&cm_id_priv->lock);
1745 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1746 spin_unlock_irq(&cm_id_priv->lock);
1747 goto out;
1750 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1751 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1752 if (!ret)
1753 list_add_tail(&work->list, &cm_id_priv->work_list);
1754 spin_unlock_irq(&cm_id_priv->lock);
1756 if (ret)
1757 cm_process_work(cm_id_priv, work);
1758 else
1759 cm_deref_id(cm_id_priv);
1760 return 0;
1761 out:
1762 cm_deref_id(cm_id_priv);
1763 return -EINVAL;
1766 static int cm_rtu_handler(struct cm_work *work)
1768 struct cm_id_private *cm_id_priv;
1769 struct cm_rtu_msg *rtu_msg;
1770 int ret;
1772 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1773 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1774 rtu_msg->local_comm_id);
1775 if (!cm_id_priv)
1776 return -EINVAL;
1778 work->cm_event.private_data = &rtu_msg->private_data;
1780 spin_lock_irq(&cm_id_priv->lock);
1781 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1782 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1783 spin_unlock_irq(&cm_id_priv->lock);
1784 goto out;
1786 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1788 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1789 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1790 if (!ret)
1791 list_add_tail(&work->list, &cm_id_priv->work_list);
1792 spin_unlock_irq(&cm_id_priv->lock);
1794 if (ret)
1795 cm_process_work(cm_id_priv, work);
1796 else
1797 cm_deref_id(cm_id_priv);
1798 return 0;
1799 out:
1800 cm_deref_id(cm_id_priv);
1801 return -EINVAL;
1804 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1805 struct cm_id_private *cm_id_priv,
1806 const void *private_data,
1807 u8 private_data_len)
1809 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1810 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1811 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1812 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1813 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1815 if (private_data && private_data_len)
1816 memcpy(dreq_msg->private_data, private_data, private_data_len);
1819 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1820 const void *private_data,
1821 u8 private_data_len)
1823 struct cm_id_private *cm_id_priv;
1824 struct ib_mad_send_buf *msg;
1825 unsigned long flags;
1826 int ret;
1828 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1829 return -EINVAL;
1831 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1832 spin_lock_irqsave(&cm_id_priv->lock, flags);
1833 if (cm_id->state != IB_CM_ESTABLISHED) {
1834 ret = -EINVAL;
1835 goto out;
1838 ret = cm_alloc_msg(cm_id_priv, &msg);
1839 if (ret) {
1840 cm_enter_timewait(cm_id_priv);
1841 goto out;
1844 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1845 private_data, private_data_len);
1846 msg->timeout_ms = cm_id_priv->timeout_ms;
1847 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1849 ret = ib_post_send_mad(msg, NULL);
1850 if (ret) {
1851 cm_enter_timewait(cm_id_priv);
1852 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1853 cm_free_msg(msg);
1854 return ret;
1857 cm_id->state = IB_CM_DREQ_SENT;
1858 cm_id_priv->msg = msg;
1859 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1860 return ret;
1862 EXPORT_SYMBOL(ib_send_cm_dreq);
1864 static void cm_format_drep(struct cm_drep_msg *drep_msg,
1865 struct cm_id_private *cm_id_priv,
1866 const void *private_data,
1867 u8 private_data_len)
1869 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1870 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1871 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1873 if (private_data && private_data_len)
1874 memcpy(drep_msg->private_data, private_data, private_data_len);
1877 int ib_send_cm_drep(struct ib_cm_id *cm_id,
1878 const void *private_data,
1879 u8 private_data_len)
1881 struct cm_id_private *cm_id_priv;
1882 struct ib_mad_send_buf *msg;
1883 unsigned long flags;
1884 void *data;
1885 int ret;
1887 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1888 return -EINVAL;
1890 data = cm_copy_private_data(private_data, private_data_len);
1891 if (IS_ERR(data))
1892 return PTR_ERR(data);
1894 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1895 spin_lock_irqsave(&cm_id_priv->lock, flags);
1896 if (cm_id->state != IB_CM_DREQ_RCVD) {
1897 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1898 kfree(data);
1899 return -EINVAL;
1902 cm_set_private_data(cm_id_priv, data, private_data_len);
1903 cm_enter_timewait(cm_id_priv);
1905 ret = cm_alloc_msg(cm_id_priv, &msg);
1906 if (ret)
1907 goto out;
1909 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1910 private_data, private_data_len);
1912 ret = ib_post_send_mad(msg, NULL);
1913 if (ret) {
1914 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1915 cm_free_msg(msg);
1916 return ret;
1919 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1920 return ret;
1922 EXPORT_SYMBOL(ib_send_cm_drep);
1924 static int cm_issue_drep(struct cm_port *port,
1925 struct ib_mad_recv_wc *mad_recv_wc)
1927 struct ib_mad_send_buf *msg = NULL;
1928 struct cm_dreq_msg *dreq_msg;
1929 struct cm_drep_msg *drep_msg;
1930 int ret;
1932 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1933 if (ret)
1934 return ret;
1936 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
1937 drep_msg = (struct cm_drep_msg *) msg->mad;
1939 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
1940 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
1941 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
1943 ret = ib_post_send_mad(msg, NULL);
1944 if (ret)
1945 cm_free_msg(msg);
1947 return ret;
1950 static int cm_dreq_handler(struct cm_work *work)
1952 struct cm_id_private *cm_id_priv;
1953 struct cm_dreq_msg *dreq_msg;
1954 struct ib_mad_send_buf *msg = NULL;
1955 int ret;
1957 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1958 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1959 dreq_msg->local_comm_id);
1960 if (!cm_id_priv) {
1961 cm_issue_drep(work->port, work->mad_recv_wc);
1962 return -EINVAL;
1965 work->cm_event.private_data = &dreq_msg->private_data;
1967 spin_lock_irq(&cm_id_priv->lock);
1968 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1969 goto unlock;
1971 switch (cm_id_priv->id.state) {
1972 case IB_CM_REP_SENT:
1973 case IB_CM_DREQ_SENT:
1974 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1975 break;
1976 case IB_CM_ESTABLISHED:
1977 case IB_CM_MRA_REP_RCVD:
1978 break;
1979 case IB_CM_TIMEWAIT:
1980 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1981 goto unlock;
1983 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1984 cm_id_priv->private_data,
1985 cm_id_priv->private_data_len);
1986 spin_unlock_irq(&cm_id_priv->lock);
1988 if (ib_post_send_mad(msg, NULL))
1989 cm_free_msg(msg);
1990 goto deref;
1991 default:
1992 goto unlock;
1994 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1995 cm_id_priv->tid = dreq_msg->hdr.tid;
1996 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1997 if (!ret)
1998 list_add_tail(&work->list, &cm_id_priv->work_list);
1999 spin_unlock_irq(&cm_id_priv->lock);
2001 if (ret)
2002 cm_process_work(cm_id_priv, work);
2003 else
2004 cm_deref_id(cm_id_priv);
2005 return 0;
2007 unlock: spin_unlock_irq(&cm_id_priv->lock);
2008 deref: cm_deref_id(cm_id_priv);
2009 return -EINVAL;
2012 static int cm_drep_handler(struct cm_work *work)
2014 struct cm_id_private *cm_id_priv;
2015 struct cm_drep_msg *drep_msg;
2016 int ret;
2018 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2019 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2020 drep_msg->local_comm_id);
2021 if (!cm_id_priv)
2022 return -EINVAL;
2024 work->cm_event.private_data = &drep_msg->private_data;
2026 spin_lock_irq(&cm_id_priv->lock);
2027 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2028 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2029 spin_unlock_irq(&cm_id_priv->lock);
2030 goto out;
2032 cm_enter_timewait(cm_id_priv);
2034 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2035 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2036 if (!ret)
2037 list_add_tail(&work->list, &cm_id_priv->work_list);
2038 spin_unlock_irq(&cm_id_priv->lock);
2040 if (ret)
2041 cm_process_work(cm_id_priv, work);
2042 else
2043 cm_deref_id(cm_id_priv);
2044 return 0;
2045 out:
2046 cm_deref_id(cm_id_priv);
2047 return -EINVAL;
2050 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2051 enum ib_cm_rej_reason reason,
2052 void *ari,
2053 u8 ari_length,
2054 const void *private_data,
2055 u8 private_data_len)
2057 struct cm_id_private *cm_id_priv;
2058 struct ib_mad_send_buf *msg;
2059 unsigned long flags;
2060 int ret;
2062 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2063 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2064 return -EINVAL;
2066 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2068 spin_lock_irqsave(&cm_id_priv->lock, flags);
2069 switch (cm_id->state) {
2070 case IB_CM_REQ_SENT:
2071 case IB_CM_MRA_REQ_RCVD:
2072 case IB_CM_REQ_RCVD:
2073 case IB_CM_MRA_REQ_SENT:
2074 case IB_CM_REP_RCVD:
2075 case IB_CM_MRA_REP_SENT:
2076 ret = cm_alloc_msg(cm_id_priv, &msg);
2077 if (!ret)
2078 cm_format_rej((struct cm_rej_msg *) msg->mad,
2079 cm_id_priv, reason, ari, ari_length,
2080 private_data, private_data_len);
2082 cm_reset_to_idle(cm_id_priv);
2083 break;
2084 case IB_CM_REP_SENT:
2085 case IB_CM_MRA_REP_RCVD:
2086 ret = cm_alloc_msg(cm_id_priv, &msg);
2087 if (!ret)
2088 cm_format_rej((struct cm_rej_msg *) msg->mad,
2089 cm_id_priv, reason, ari, ari_length,
2090 private_data, private_data_len);
2092 cm_enter_timewait(cm_id_priv);
2093 break;
2094 default:
2095 ret = -EINVAL;
2096 goto out;
2099 if (ret)
2100 goto out;
2102 ret = ib_post_send_mad(msg, NULL);
2103 if (ret)
2104 cm_free_msg(msg);
2106 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2107 return ret;
2109 EXPORT_SYMBOL(ib_send_cm_rej);
2111 static void cm_format_rej_event(struct cm_work *work)
2113 struct cm_rej_msg *rej_msg;
2114 struct ib_cm_rej_event_param *param;
2116 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2117 param = &work->cm_event.param.rej_rcvd;
2118 param->ari = rej_msg->ari;
2119 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2120 param->reason = __be16_to_cpu(rej_msg->reason);
2121 work->cm_event.private_data = &rej_msg->private_data;
2124 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2126 struct cm_timewait_info *timewait_info;
2127 struct cm_id_private *cm_id_priv;
2128 __be32 remote_id;
2130 remote_id = rej_msg->local_comm_id;
2132 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2133 spin_lock_irq(&cm.lock);
2134 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2135 remote_id);
2136 if (!timewait_info) {
2137 spin_unlock_irq(&cm.lock);
2138 return NULL;
2140 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2141 (timewait_info->work.local_id ^
2142 cm.random_id_operand));
2143 if (cm_id_priv) {
2144 if (cm_id_priv->id.remote_id == remote_id)
2145 atomic_inc(&cm_id_priv->refcount);
2146 else
2147 cm_id_priv = NULL;
2149 spin_unlock_irq(&cm.lock);
2150 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2151 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2152 else
2153 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2155 return cm_id_priv;
2158 static int cm_rej_handler(struct cm_work *work)
2160 struct cm_id_private *cm_id_priv;
2161 struct cm_rej_msg *rej_msg;
2162 int ret;
2164 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2165 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2166 if (!cm_id_priv)
2167 return -EINVAL;
2169 cm_format_rej_event(work);
2171 spin_lock_irq(&cm_id_priv->lock);
2172 switch (cm_id_priv->id.state) {
2173 case IB_CM_REQ_SENT:
2174 case IB_CM_MRA_REQ_RCVD:
2175 case IB_CM_REP_SENT:
2176 case IB_CM_MRA_REP_RCVD:
2177 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2178 /* fall through */
2179 case IB_CM_REQ_RCVD:
2180 case IB_CM_MRA_REQ_SENT:
2181 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2182 cm_enter_timewait(cm_id_priv);
2183 else
2184 cm_reset_to_idle(cm_id_priv);
2185 break;
2186 case IB_CM_DREQ_SENT:
2187 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2188 /* fall through */
2189 case IB_CM_REP_RCVD:
2190 case IB_CM_MRA_REP_SENT:
2191 case IB_CM_ESTABLISHED:
2192 cm_enter_timewait(cm_id_priv);
2193 break;
2194 default:
2195 spin_unlock_irq(&cm_id_priv->lock);
2196 ret = -EINVAL;
2197 goto out;
2200 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2201 if (!ret)
2202 list_add_tail(&work->list, &cm_id_priv->work_list);
2203 spin_unlock_irq(&cm_id_priv->lock);
2205 if (ret)
2206 cm_process_work(cm_id_priv, work);
2207 else
2208 cm_deref_id(cm_id_priv);
2209 return 0;
2210 out:
2211 cm_deref_id(cm_id_priv);
2212 return -EINVAL;
2215 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2216 u8 service_timeout,
2217 const void *private_data,
2218 u8 private_data_len)
2220 struct cm_id_private *cm_id_priv;
2221 struct ib_mad_send_buf *msg;
2222 enum ib_cm_state cm_state;
2223 enum ib_cm_lap_state lap_state;
2224 enum cm_msg_response msg_response;
2225 void *data;
2226 unsigned long flags;
2227 int ret;
2229 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2230 return -EINVAL;
2232 data = cm_copy_private_data(private_data, private_data_len);
2233 if (IS_ERR(data))
2234 return PTR_ERR(data);
2236 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2238 spin_lock_irqsave(&cm_id_priv->lock, flags);
2239 switch(cm_id_priv->id.state) {
2240 case IB_CM_REQ_RCVD:
2241 cm_state = IB_CM_MRA_REQ_SENT;
2242 lap_state = cm_id->lap_state;
2243 msg_response = CM_MSG_RESPONSE_REQ;
2244 break;
2245 case IB_CM_REP_RCVD:
2246 cm_state = IB_CM_MRA_REP_SENT;
2247 lap_state = cm_id->lap_state;
2248 msg_response = CM_MSG_RESPONSE_REP;
2249 break;
2250 case IB_CM_ESTABLISHED:
2251 cm_state = cm_id->state;
2252 lap_state = IB_CM_MRA_LAP_SENT;
2253 msg_response = CM_MSG_RESPONSE_OTHER;
2254 break;
2255 default:
2256 ret = -EINVAL;
2257 goto error1;
2260 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2261 ret = cm_alloc_msg(cm_id_priv, &msg);
2262 if (ret)
2263 goto error1;
2265 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2266 msg_response, service_timeout,
2267 private_data, private_data_len);
2268 ret = ib_post_send_mad(msg, NULL);
2269 if (ret)
2270 goto error2;
2273 cm_id->state = cm_state;
2274 cm_id->lap_state = lap_state;
2275 cm_id_priv->service_timeout = service_timeout;
2276 cm_set_private_data(cm_id_priv, data, private_data_len);
2277 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2278 return 0;
2280 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2281 kfree(data);
2282 return ret;
2284 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2285 kfree(data);
2286 cm_free_msg(msg);
2287 return ret;
2289 EXPORT_SYMBOL(ib_send_cm_mra);
2291 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2293 switch (cm_mra_get_msg_mraed(mra_msg)) {
2294 case CM_MSG_RESPONSE_REQ:
2295 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2296 case CM_MSG_RESPONSE_REP:
2297 case CM_MSG_RESPONSE_OTHER:
2298 return cm_acquire_id(mra_msg->remote_comm_id,
2299 mra_msg->local_comm_id);
2300 default:
2301 return NULL;
2305 static int cm_mra_handler(struct cm_work *work)
2307 struct cm_id_private *cm_id_priv;
2308 struct cm_mra_msg *mra_msg;
2309 int timeout, ret;
2311 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2312 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2313 if (!cm_id_priv)
2314 return -EINVAL;
2316 work->cm_event.private_data = &mra_msg->private_data;
2317 work->cm_event.param.mra_rcvd.service_timeout =
2318 cm_mra_get_service_timeout(mra_msg);
2319 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2320 cm_convert_to_ms(cm_id_priv->av.timeout);
2322 spin_lock_irq(&cm_id_priv->lock);
2323 switch (cm_id_priv->id.state) {
2324 case IB_CM_REQ_SENT:
2325 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2326 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2327 cm_id_priv->msg, timeout))
2328 goto out;
2329 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2330 break;
2331 case IB_CM_REP_SENT:
2332 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2333 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2334 cm_id_priv->msg, timeout))
2335 goto out;
2336 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2337 break;
2338 case IB_CM_ESTABLISHED:
2339 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2340 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2341 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2342 cm_id_priv->msg, timeout))
2343 goto out;
2344 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2345 break;
2346 default:
2347 goto out;
2350 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2351 cm_id_priv->id.state;
2352 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2353 if (!ret)
2354 list_add_tail(&work->list, &cm_id_priv->work_list);
2355 spin_unlock_irq(&cm_id_priv->lock);
2357 if (ret)
2358 cm_process_work(cm_id_priv, work);
2359 else
2360 cm_deref_id(cm_id_priv);
2361 return 0;
2362 out:
2363 spin_unlock_irq(&cm_id_priv->lock);
2364 cm_deref_id(cm_id_priv);
2365 return -EINVAL;
2368 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2369 struct cm_id_private *cm_id_priv,
2370 struct ib_sa_path_rec *alternate_path,
2371 const void *private_data,
2372 u8 private_data_len)
2374 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2375 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2376 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2377 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2378 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2379 /* todo: need remote CM response timeout */
2380 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2381 lap_msg->alt_local_lid = alternate_path->slid;
2382 lap_msg->alt_remote_lid = alternate_path->dlid;
2383 lap_msg->alt_local_gid = alternate_path->sgid;
2384 lap_msg->alt_remote_gid = alternate_path->dgid;
2385 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2386 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2387 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2388 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2389 cm_lap_set_sl(lap_msg, alternate_path->sl);
2390 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2391 cm_lap_set_local_ack_timeout(lap_msg,
2392 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2393 alternate_path->packet_life_time));
2395 if (private_data && private_data_len)
2396 memcpy(lap_msg->private_data, private_data, private_data_len);
2399 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2400 struct ib_sa_path_rec *alternate_path,
2401 const void *private_data,
2402 u8 private_data_len)
2404 struct cm_id_private *cm_id_priv;
2405 struct ib_mad_send_buf *msg;
2406 unsigned long flags;
2407 int ret;
2409 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2410 return -EINVAL;
2412 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2413 spin_lock_irqsave(&cm_id_priv->lock, flags);
2414 if (cm_id->state != IB_CM_ESTABLISHED ||
2415 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2416 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2417 ret = -EINVAL;
2418 goto out;
2421 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2422 if (ret)
2423 goto out;
2424 cm_id_priv->alt_av.timeout =
2425 cm_ack_timeout(cm_id_priv->target_ack_delay,
2426 cm_id_priv->alt_av.timeout - 1);
2428 ret = cm_alloc_msg(cm_id_priv, &msg);
2429 if (ret)
2430 goto out;
2432 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2433 alternate_path, private_data, private_data_len);
2434 msg->timeout_ms = cm_id_priv->timeout_ms;
2435 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2437 ret = ib_post_send_mad(msg, NULL);
2438 if (ret) {
2439 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2440 cm_free_msg(msg);
2441 return ret;
2444 cm_id->lap_state = IB_CM_LAP_SENT;
2445 cm_id_priv->msg = msg;
2447 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2448 return ret;
2450 EXPORT_SYMBOL(ib_send_cm_lap);
2452 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2453 struct ib_sa_path_rec *path,
2454 struct cm_lap_msg *lap_msg)
2456 memset(path, 0, sizeof *path);
2457 path->dgid = lap_msg->alt_local_gid;
2458 path->sgid = lap_msg->alt_remote_gid;
2459 path->dlid = lap_msg->alt_local_lid;
2460 path->slid = lap_msg->alt_remote_lid;
2461 path->flow_label = cm_lap_get_flow_label(lap_msg);
2462 path->hop_limit = lap_msg->alt_hop_limit;
2463 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2464 path->reversible = 1;
2465 path->pkey = cm_id_priv->pkey;
2466 path->sl = cm_lap_get_sl(lap_msg);
2467 path->mtu_selector = IB_SA_EQ;
2468 path->mtu = cm_id_priv->path_mtu;
2469 path->rate_selector = IB_SA_EQ;
2470 path->rate = cm_lap_get_packet_rate(lap_msg);
2471 path->packet_life_time_selector = IB_SA_EQ;
2472 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2473 path->packet_life_time -= (path->packet_life_time > 0);
2476 static int cm_lap_handler(struct cm_work *work)
2478 struct cm_id_private *cm_id_priv;
2479 struct cm_lap_msg *lap_msg;
2480 struct ib_cm_lap_event_param *param;
2481 struct ib_mad_send_buf *msg = NULL;
2482 int ret;
2484 /* todo: verify LAP request and send reject APR if invalid. */
2485 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2486 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2487 lap_msg->local_comm_id);
2488 if (!cm_id_priv)
2489 return -EINVAL;
2491 param = &work->cm_event.param.lap_rcvd;
2492 param->alternate_path = &work->path[0];
2493 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2494 work->cm_event.private_data = &lap_msg->private_data;
2496 spin_lock_irq(&cm_id_priv->lock);
2497 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2498 goto unlock;
2500 switch (cm_id_priv->id.lap_state) {
2501 case IB_CM_LAP_UNINIT:
2502 case IB_CM_LAP_IDLE:
2503 break;
2504 case IB_CM_MRA_LAP_SENT:
2505 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2506 goto unlock;
2508 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2509 CM_MSG_RESPONSE_OTHER,
2510 cm_id_priv->service_timeout,
2511 cm_id_priv->private_data,
2512 cm_id_priv->private_data_len);
2513 spin_unlock_irq(&cm_id_priv->lock);
2515 if (ib_post_send_mad(msg, NULL))
2516 cm_free_msg(msg);
2517 goto deref;
2518 default:
2519 goto unlock;
2522 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2523 cm_id_priv->tid = lap_msg->hdr.tid;
2524 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2525 work->mad_recv_wc->recv_buf.grh,
2526 &cm_id_priv->av);
2527 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2528 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2529 if (!ret)
2530 list_add_tail(&work->list, &cm_id_priv->work_list);
2531 spin_unlock_irq(&cm_id_priv->lock);
2533 if (ret)
2534 cm_process_work(cm_id_priv, work);
2535 else
2536 cm_deref_id(cm_id_priv);
2537 return 0;
2539 unlock: spin_unlock_irq(&cm_id_priv->lock);
2540 deref: cm_deref_id(cm_id_priv);
2541 return -EINVAL;
2544 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2545 struct cm_id_private *cm_id_priv,
2546 enum ib_cm_apr_status status,
2547 void *info,
2548 u8 info_length,
2549 const void *private_data,
2550 u8 private_data_len)
2552 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2553 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2554 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2555 apr_msg->ap_status = (u8) status;
2557 if (info && info_length) {
2558 apr_msg->info_length = info_length;
2559 memcpy(apr_msg->info, info, info_length);
2562 if (private_data && private_data_len)
2563 memcpy(apr_msg->private_data, private_data, private_data_len);
2566 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2567 enum ib_cm_apr_status status,
2568 void *info,
2569 u8 info_length,
2570 const void *private_data,
2571 u8 private_data_len)
2573 struct cm_id_private *cm_id_priv;
2574 struct ib_mad_send_buf *msg;
2575 unsigned long flags;
2576 int ret;
2578 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2579 (info && info_length > IB_CM_APR_INFO_LENGTH))
2580 return -EINVAL;
2582 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2583 spin_lock_irqsave(&cm_id_priv->lock, flags);
2584 if (cm_id->state != IB_CM_ESTABLISHED ||
2585 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2586 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2587 ret = -EINVAL;
2588 goto out;
2591 ret = cm_alloc_msg(cm_id_priv, &msg);
2592 if (ret)
2593 goto out;
2595 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2596 info, info_length, private_data, private_data_len);
2597 ret = ib_post_send_mad(msg, NULL);
2598 if (ret) {
2599 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2600 cm_free_msg(msg);
2601 return ret;
2604 cm_id->lap_state = IB_CM_LAP_IDLE;
2605 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2606 return ret;
2608 EXPORT_SYMBOL(ib_send_cm_apr);
2610 static int cm_apr_handler(struct cm_work *work)
2612 struct cm_id_private *cm_id_priv;
2613 struct cm_apr_msg *apr_msg;
2614 int ret;
2616 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2617 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2618 apr_msg->local_comm_id);
2619 if (!cm_id_priv)
2620 return -EINVAL; /* Unmatched reply. */
2622 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2623 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2624 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2625 work->cm_event.private_data = &apr_msg->private_data;
2627 spin_lock_irq(&cm_id_priv->lock);
2628 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2629 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2630 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2631 spin_unlock_irq(&cm_id_priv->lock);
2632 goto out;
2634 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2635 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2636 cm_id_priv->msg = NULL;
2638 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2639 if (!ret)
2640 list_add_tail(&work->list, &cm_id_priv->work_list);
2641 spin_unlock_irq(&cm_id_priv->lock);
2643 if (ret)
2644 cm_process_work(cm_id_priv, work);
2645 else
2646 cm_deref_id(cm_id_priv);
2647 return 0;
2648 out:
2649 cm_deref_id(cm_id_priv);
2650 return -EINVAL;
2653 static int cm_timewait_handler(struct cm_work *work)
2655 struct cm_timewait_info *timewait_info;
2656 struct cm_id_private *cm_id_priv;
2657 int ret;
2659 timewait_info = (struct cm_timewait_info *)work;
2660 spin_lock_irq(&cm.lock);
2661 list_del(&timewait_info->list);
2662 spin_unlock_irq(&cm.lock);
2664 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2665 timewait_info->work.remote_id);
2666 if (!cm_id_priv)
2667 return -EINVAL;
2669 spin_lock_irq(&cm_id_priv->lock);
2670 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2671 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2672 spin_unlock_irq(&cm_id_priv->lock);
2673 goto out;
2675 cm_id_priv->id.state = IB_CM_IDLE;
2676 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2677 if (!ret)
2678 list_add_tail(&work->list, &cm_id_priv->work_list);
2679 spin_unlock_irq(&cm_id_priv->lock);
2681 if (ret)
2682 cm_process_work(cm_id_priv, work);
2683 else
2684 cm_deref_id(cm_id_priv);
2685 return 0;
2686 out:
2687 cm_deref_id(cm_id_priv);
2688 return -EINVAL;
2691 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2692 struct cm_id_private *cm_id_priv,
2693 struct ib_cm_sidr_req_param *param)
2695 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2696 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2697 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2698 sidr_req_msg->pkey = cpu_to_be16(param->path->pkey);
2699 sidr_req_msg->service_id = param->service_id;
2701 if (param->private_data && param->private_data_len)
2702 memcpy(sidr_req_msg->private_data, param->private_data,
2703 param->private_data_len);
2706 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2707 struct ib_cm_sidr_req_param *param)
2709 struct cm_id_private *cm_id_priv;
2710 struct ib_mad_send_buf *msg;
2711 unsigned long flags;
2712 int ret;
2714 if (!param->path || (param->private_data &&
2715 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2716 return -EINVAL;
2718 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2719 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2720 if (ret)
2721 goto out;
2723 cm_id->service_id = param->service_id;
2724 cm_id->service_mask = __constant_cpu_to_be64(~0ULL);
2725 cm_id_priv->timeout_ms = param->timeout_ms;
2726 cm_id_priv->max_cm_retries = param->max_cm_retries;
2727 ret = cm_alloc_msg(cm_id_priv, &msg);
2728 if (ret)
2729 goto out;
2731 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2732 param);
2733 msg->timeout_ms = cm_id_priv->timeout_ms;
2734 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2736 spin_lock_irqsave(&cm_id_priv->lock, flags);
2737 if (cm_id->state == IB_CM_IDLE)
2738 ret = ib_post_send_mad(msg, NULL);
2739 else
2740 ret = -EINVAL;
2742 if (ret) {
2743 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2744 cm_free_msg(msg);
2745 goto out;
2747 cm_id->state = IB_CM_SIDR_REQ_SENT;
2748 cm_id_priv->msg = msg;
2749 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2750 out:
2751 return ret;
2753 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2755 static void cm_format_sidr_req_event(struct cm_work *work,
2756 struct ib_cm_id *listen_id)
2758 struct cm_sidr_req_msg *sidr_req_msg;
2759 struct ib_cm_sidr_req_event_param *param;
2761 sidr_req_msg = (struct cm_sidr_req_msg *)
2762 work->mad_recv_wc->recv_buf.mad;
2763 param = &work->cm_event.param.sidr_req_rcvd;
2764 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2765 param->listen_id = listen_id;
2766 param->port = work->port->port_num;
2767 work->cm_event.private_data = &sidr_req_msg->private_data;
2770 static int cm_sidr_req_handler(struct cm_work *work)
2772 struct ib_cm_id *cm_id;
2773 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2774 struct cm_sidr_req_msg *sidr_req_msg;
2775 struct ib_wc *wc;
2777 cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
2778 if (IS_ERR(cm_id))
2779 return PTR_ERR(cm_id);
2780 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2782 /* Record SGID/SLID and request ID for lookup. */
2783 sidr_req_msg = (struct cm_sidr_req_msg *)
2784 work->mad_recv_wc->recv_buf.mad;
2785 wc = work->mad_recv_wc->wc;
2786 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2787 cm_id_priv->av.dgid.global.interface_id = 0;
2788 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2789 work->mad_recv_wc->recv_buf.grh,
2790 &cm_id_priv->av);
2791 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2792 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2793 atomic_inc(&cm_id_priv->work_count);
2795 spin_lock_irq(&cm.lock);
2796 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2797 if (cur_cm_id_priv) {
2798 spin_unlock_irq(&cm.lock);
2799 goto out; /* Duplicate message. */
2801 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2802 cur_cm_id_priv = cm_find_listen(cm_id->device,
2803 sidr_req_msg->service_id,
2804 sidr_req_msg->private_data);
2805 if (!cur_cm_id_priv) {
2806 spin_unlock_irq(&cm.lock);
2807 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
2808 goto out; /* No match. */
2810 atomic_inc(&cur_cm_id_priv->refcount);
2811 spin_unlock_irq(&cm.lock);
2813 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2814 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2815 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2816 cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL);
2818 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2819 cm_process_work(cm_id_priv, work);
2820 cm_deref_id(cur_cm_id_priv);
2821 return 0;
2822 out:
2823 ib_destroy_cm_id(&cm_id_priv->id);
2824 return -EINVAL;
2827 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2828 struct cm_id_private *cm_id_priv,
2829 struct ib_cm_sidr_rep_param *param)
2831 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2832 cm_id_priv->tid);
2833 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2834 sidr_rep_msg->status = param->status;
2835 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2836 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2837 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2839 if (param->info && param->info_length)
2840 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2842 if (param->private_data && param->private_data_len)
2843 memcpy(sidr_rep_msg->private_data, param->private_data,
2844 param->private_data_len);
2847 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2848 struct ib_cm_sidr_rep_param *param)
2850 struct cm_id_private *cm_id_priv;
2851 struct ib_mad_send_buf *msg;
2852 unsigned long flags;
2853 int ret;
2855 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2856 (param->private_data &&
2857 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2858 return -EINVAL;
2860 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2861 spin_lock_irqsave(&cm_id_priv->lock, flags);
2862 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2863 ret = -EINVAL;
2864 goto error;
2867 ret = cm_alloc_msg(cm_id_priv, &msg);
2868 if (ret)
2869 goto error;
2871 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2872 param);
2873 ret = ib_post_send_mad(msg, NULL);
2874 if (ret) {
2875 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2876 cm_free_msg(msg);
2877 return ret;
2879 cm_id->state = IB_CM_IDLE;
2880 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2882 spin_lock_irqsave(&cm.lock, flags);
2883 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2884 spin_unlock_irqrestore(&cm.lock, flags);
2885 return 0;
2887 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2888 return ret;
2890 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2892 static void cm_format_sidr_rep_event(struct cm_work *work)
2894 struct cm_sidr_rep_msg *sidr_rep_msg;
2895 struct ib_cm_sidr_rep_event_param *param;
2897 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2898 work->mad_recv_wc->recv_buf.mad;
2899 param = &work->cm_event.param.sidr_rep_rcvd;
2900 param->status = sidr_rep_msg->status;
2901 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2902 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2903 param->info = &sidr_rep_msg->info;
2904 param->info_len = sidr_rep_msg->info_length;
2905 work->cm_event.private_data = &sidr_rep_msg->private_data;
2908 static int cm_sidr_rep_handler(struct cm_work *work)
2910 struct cm_sidr_rep_msg *sidr_rep_msg;
2911 struct cm_id_private *cm_id_priv;
2913 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2914 work->mad_recv_wc->recv_buf.mad;
2915 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2916 if (!cm_id_priv)
2917 return -EINVAL; /* Unmatched reply. */
2919 spin_lock_irq(&cm_id_priv->lock);
2920 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2921 spin_unlock_irq(&cm_id_priv->lock);
2922 goto out;
2924 cm_id_priv->id.state = IB_CM_IDLE;
2925 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2926 spin_unlock_irq(&cm_id_priv->lock);
2928 cm_format_sidr_rep_event(work);
2929 cm_process_work(cm_id_priv, work);
2930 return 0;
2931 out:
2932 cm_deref_id(cm_id_priv);
2933 return -EINVAL;
2936 static void cm_process_send_error(struct ib_mad_send_buf *msg,
2937 enum ib_wc_status wc_status)
2939 struct cm_id_private *cm_id_priv;
2940 struct ib_cm_event cm_event;
2941 enum ib_cm_state state;
2942 int ret;
2944 memset(&cm_event, 0, sizeof cm_event);
2945 cm_id_priv = msg->context[0];
2947 /* Discard old sends or ones without a response. */
2948 spin_lock_irq(&cm_id_priv->lock);
2949 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2950 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2951 goto discard;
2953 switch (state) {
2954 case IB_CM_REQ_SENT:
2955 case IB_CM_MRA_REQ_RCVD:
2956 cm_reset_to_idle(cm_id_priv);
2957 cm_event.event = IB_CM_REQ_ERROR;
2958 break;
2959 case IB_CM_REP_SENT:
2960 case IB_CM_MRA_REP_RCVD:
2961 cm_reset_to_idle(cm_id_priv);
2962 cm_event.event = IB_CM_REP_ERROR;
2963 break;
2964 case IB_CM_DREQ_SENT:
2965 cm_enter_timewait(cm_id_priv);
2966 cm_event.event = IB_CM_DREQ_ERROR;
2967 break;
2968 case IB_CM_SIDR_REQ_SENT:
2969 cm_id_priv->id.state = IB_CM_IDLE;
2970 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2971 break;
2972 default:
2973 goto discard;
2975 spin_unlock_irq(&cm_id_priv->lock);
2976 cm_event.param.send_status = wc_status;
2978 /* No other events can occur on the cm_id at this point. */
2979 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2980 cm_free_msg(msg);
2981 if (ret)
2982 ib_destroy_cm_id(&cm_id_priv->id);
2983 return;
2984 discard:
2985 spin_unlock_irq(&cm_id_priv->lock);
2986 cm_free_msg(msg);
2989 static void cm_send_handler(struct ib_mad_agent *mad_agent,
2990 struct ib_mad_send_wc *mad_send_wc)
2992 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
2994 switch (mad_send_wc->status) {
2995 case IB_WC_SUCCESS:
2996 case IB_WC_WR_FLUSH_ERR:
2997 cm_free_msg(msg);
2998 break;
2999 default:
3000 if (msg->context[0] && msg->context[1])
3001 cm_process_send_error(msg, mad_send_wc->status);
3002 else
3003 cm_free_msg(msg);
3004 break;
3008 static void cm_work_handler(struct work_struct *_work)
3010 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3011 int ret;
3013 switch (work->cm_event.event) {
3014 case IB_CM_REQ_RECEIVED:
3015 ret = cm_req_handler(work);
3016 break;
3017 case IB_CM_MRA_RECEIVED:
3018 ret = cm_mra_handler(work);
3019 break;
3020 case IB_CM_REJ_RECEIVED:
3021 ret = cm_rej_handler(work);
3022 break;
3023 case IB_CM_REP_RECEIVED:
3024 ret = cm_rep_handler(work);
3025 break;
3026 case IB_CM_RTU_RECEIVED:
3027 ret = cm_rtu_handler(work);
3028 break;
3029 case IB_CM_USER_ESTABLISHED:
3030 ret = cm_establish_handler(work);
3031 break;
3032 case IB_CM_DREQ_RECEIVED:
3033 ret = cm_dreq_handler(work);
3034 break;
3035 case IB_CM_DREP_RECEIVED:
3036 ret = cm_drep_handler(work);
3037 break;
3038 case IB_CM_SIDR_REQ_RECEIVED:
3039 ret = cm_sidr_req_handler(work);
3040 break;
3041 case IB_CM_SIDR_REP_RECEIVED:
3042 ret = cm_sidr_rep_handler(work);
3043 break;
3044 case IB_CM_LAP_RECEIVED:
3045 ret = cm_lap_handler(work);
3046 break;
3047 case IB_CM_APR_RECEIVED:
3048 ret = cm_apr_handler(work);
3049 break;
3050 case IB_CM_TIMEWAIT_EXIT:
3051 ret = cm_timewait_handler(work);
3052 break;
3053 default:
3054 ret = -EINVAL;
3055 break;
3057 if (ret)
3058 cm_free_work(work);
3061 static int cm_establish(struct ib_cm_id *cm_id)
3063 struct cm_id_private *cm_id_priv;
3064 struct cm_work *work;
3065 unsigned long flags;
3066 int ret = 0;
3068 work = kmalloc(sizeof *work, GFP_ATOMIC);
3069 if (!work)
3070 return -ENOMEM;
3072 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3073 spin_lock_irqsave(&cm_id_priv->lock, flags);
3074 switch (cm_id->state)
3076 case IB_CM_REP_SENT:
3077 case IB_CM_MRA_REP_RCVD:
3078 cm_id->state = IB_CM_ESTABLISHED;
3079 break;
3080 case IB_CM_ESTABLISHED:
3081 ret = -EISCONN;
3082 break;
3083 default:
3084 ret = -EINVAL;
3085 break;
3087 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3089 if (ret) {
3090 kfree(work);
3091 goto out;
3095 * The CM worker thread may try to destroy the cm_id before it
3096 * can execute this work item. To prevent potential deadlock,
3097 * we need to find the cm_id once we're in the context of the
3098 * worker thread, rather than holding a reference on it.
3100 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3101 work->local_id = cm_id->local_id;
3102 work->remote_id = cm_id->remote_id;
3103 work->mad_recv_wc = NULL;
3104 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3105 queue_delayed_work(cm.wq, &work->work, 0);
3106 out:
3107 return ret;
3110 static int cm_migrate(struct ib_cm_id *cm_id)
3112 struct cm_id_private *cm_id_priv;
3113 unsigned long flags;
3114 int ret = 0;
3116 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3117 spin_lock_irqsave(&cm_id_priv->lock, flags);
3118 if (cm_id->state == IB_CM_ESTABLISHED &&
3119 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3120 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3121 cm_id->lap_state = IB_CM_LAP_IDLE;
3122 cm_id_priv->av = cm_id_priv->alt_av;
3123 } else
3124 ret = -EINVAL;
3125 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3127 return ret;
3130 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3132 int ret;
3134 switch (event) {
3135 case IB_EVENT_COMM_EST:
3136 ret = cm_establish(cm_id);
3137 break;
3138 case IB_EVENT_PATH_MIG:
3139 ret = cm_migrate(cm_id);
3140 break;
3141 default:
3142 ret = -EINVAL;
3144 return ret;
3146 EXPORT_SYMBOL(ib_cm_notify);
3148 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3149 struct ib_mad_recv_wc *mad_recv_wc)
3151 struct cm_work *work;
3152 enum ib_cm_event_type event;
3153 int paths = 0;
3155 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3156 case CM_REQ_ATTR_ID:
3157 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3158 alt_local_lid != 0);
3159 event = IB_CM_REQ_RECEIVED;
3160 break;
3161 case CM_MRA_ATTR_ID:
3162 event = IB_CM_MRA_RECEIVED;
3163 break;
3164 case CM_REJ_ATTR_ID:
3165 event = IB_CM_REJ_RECEIVED;
3166 break;
3167 case CM_REP_ATTR_ID:
3168 event = IB_CM_REP_RECEIVED;
3169 break;
3170 case CM_RTU_ATTR_ID:
3171 event = IB_CM_RTU_RECEIVED;
3172 break;
3173 case CM_DREQ_ATTR_ID:
3174 event = IB_CM_DREQ_RECEIVED;
3175 break;
3176 case CM_DREP_ATTR_ID:
3177 event = IB_CM_DREP_RECEIVED;
3178 break;
3179 case CM_SIDR_REQ_ATTR_ID:
3180 event = IB_CM_SIDR_REQ_RECEIVED;
3181 break;
3182 case CM_SIDR_REP_ATTR_ID:
3183 event = IB_CM_SIDR_REP_RECEIVED;
3184 break;
3185 case CM_LAP_ATTR_ID:
3186 paths = 1;
3187 event = IB_CM_LAP_RECEIVED;
3188 break;
3189 case CM_APR_ATTR_ID:
3190 event = IB_CM_APR_RECEIVED;
3191 break;
3192 default:
3193 ib_free_recv_mad(mad_recv_wc);
3194 return;
3197 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3198 GFP_KERNEL);
3199 if (!work) {
3200 ib_free_recv_mad(mad_recv_wc);
3201 return;
3204 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3205 work->cm_event.event = event;
3206 work->mad_recv_wc = mad_recv_wc;
3207 work->port = (struct cm_port *)mad_agent->context;
3208 queue_delayed_work(cm.wq, &work->work, 0);
3211 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3212 struct ib_qp_attr *qp_attr,
3213 int *qp_attr_mask)
3215 unsigned long flags;
3216 int ret;
3218 spin_lock_irqsave(&cm_id_priv->lock, flags);
3219 switch (cm_id_priv->id.state) {
3220 case IB_CM_REQ_SENT:
3221 case IB_CM_MRA_REQ_RCVD:
3222 case IB_CM_REQ_RCVD:
3223 case IB_CM_MRA_REQ_SENT:
3224 case IB_CM_REP_RCVD:
3225 case IB_CM_MRA_REP_SENT:
3226 case IB_CM_REP_SENT:
3227 case IB_CM_MRA_REP_RCVD:
3228 case IB_CM_ESTABLISHED:
3229 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3230 IB_QP_PKEY_INDEX | IB_QP_PORT;
3231 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3232 if (cm_id_priv->responder_resources)
3233 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3234 IB_ACCESS_REMOTE_ATOMIC;
3235 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3236 qp_attr->port_num = cm_id_priv->av.port->port_num;
3237 ret = 0;
3238 break;
3239 default:
3240 ret = -EINVAL;
3241 break;
3243 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3244 return ret;
3247 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3248 struct ib_qp_attr *qp_attr,
3249 int *qp_attr_mask)
3251 unsigned long flags;
3252 int ret;
3254 spin_lock_irqsave(&cm_id_priv->lock, flags);
3255 switch (cm_id_priv->id.state) {
3256 case IB_CM_REQ_RCVD:
3257 case IB_CM_MRA_REQ_SENT:
3258 case IB_CM_REP_RCVD:
3259 case IB_CM_MRA_REP_SENT:
3260 case IB_CM_REP_SENT:
3261 case IB_CM_MRA_REP_RCVD:
3262 case IB_CM_ESTABLISHED:
3263 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3264 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3265 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3266 qp_attr->path_mtu = cm_id_priv->path_mtu;
3267 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3268 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3269 if (cm_id_priv->qp_type == IB_QPT_RC) {
3270 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3271 IB_QP_MIN_RNR_TIMER;
3272 qp_attr->max_dest_rd_atomic =
3273 cm_id_priv->responder_resources;
3274 qp_attr->min_rnr_timer = 0;
3276 if (cm_id_priv->alt_av.ah_attr.dlid) {
3277 *qp_attr_mask |= IB_QP_ALT_PATH;
3278 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3279 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3280 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3281 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3283 ret = 0;
3284 break;
3285 default:
3286 ret = -EINVAL;
3287 break;
3289 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3290 return ret;
3293 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3294 struct ib_qp_attr *qp_attr,
3295 int *qp_attr_mask)
3297 unsigned long flags;
3298 int ret;
3300 spin_lock_irqsave(&cm_id_priv->lock, flags);
3301 switch (cm_id_priv->id.state) {
3302 /* Allow transition to RTS before sending REP */
3303 case IB_CM_REQ_RCVD:
3304 case IB_CM_MRA_REQ_SENT:
3306 case IB_CM_REP_RCVD:
3307 case IB_CM_MRA_REP_SENT:
3308 case IB_CM_REP_SENT:
3309 case IB_CM_MRA_REP_RCVD:
3310 case IB_CM_ESTABLISHED:
3311 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3312 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3313 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3314 if (cm_id_priv->qp_type == IB_QPT_RC) {
3315 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3316 IB_QP_RNR_RETRY |
3317 IB_QP_MAX_QP_RD_ATOMIC;
3318 qp_attr->timeout = cm_id_priv->av.timeout;
3319 qp_attr->retry_cnt = cm_id_priv->retry_count;
3320 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3321 qp_attr->max_rd_atomic =
3322 cm_id_priv->initiator_depth;
3324 if (cm_id_priv->alt_av.ah_attr.dlid) {
3325 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3326 qp_attr->path_mig_state = IB_MIG_REARM;
3328 } else {
3329 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3330 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3331 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3332 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3333 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3334 qp_attr->path_mig_state = IB_MIG_REARM;
3336 ret = 0;
3337 break;
3338 default:
3339 ret = -EINVAL;
3340 break;
3342 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3343 return ret;
3346 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3347 struct ib_qp_attr *qp_attr,
3348 int *qp_attr_mask)
3350 struct cm_id_private *cm_id_priv;
3351 int ret;
3353 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3354 switch (qp_attr->qp_state) {
3355 case IB_QPS_INIT:
3356 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3357 break;
3358 case IB_QPS_RTR:
3359 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3360 break;
3361 case IB_QPS_RTS:
3362 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3363 break;
3364 default:
3365 ret = -EINVAL;
3366 break;
3368 return ret;
3370 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3372 static void cm_get_ack_delay(struct cm_device *cm_dev)
3374 struct ib_device_attr attr;
3376 if (ib_query_device(cm_dev->device, &attr))
3377 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3378 else
3379 cm_dev->ack_delay = attr.local_ca_ack_delay;
3382 static void cm_add_one(struct ib_device *device)
3384 struct cm_device *cm_dev;
3385 struct cm_port *port;
3386 struct ib_mad_reg_req reg_req = {
3387 .mgmt_class = IB_MGMT_CLASS_CM,
3388 .mgmt_class_version = IB_CM_CLASS_VERSION
3390 struct ib_port_modify port_modify = {
3391 .set_port_cap_mask = IB_PORT_CM_SUP
3393 unsigned long flags;
3394 int ret;
3395 u8 i;
3397 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3398 return;
3400 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3401 device->phys_port_cnt, GFP_KERNEL);
3402 if (!cm_dev)
3403 return;
3405 cm_dev->device = device;
3406 cm_get_ack_delay(cm_dev);
3408 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3409 for (i = 1; i <= device->phys_port_cnt; i++) {
3410 port = &cm_dev->port[i-1];
3411 port->cm_dev = cm_dev;
3412 port->port_num = i;
3413 port->mad_agent = ib_register_mad_agent(device, i,
3414 IB_QPT_GSI,
3415 &reg_req,
3417 cm_send_handler,
3418 cm_recv_handler,
3419 port);
3420 if (IS_ERR(port->mad_agent))
3421 goto error1;
3423 ret = ib_modify_port(device, i, 0, &port_modify);
3424 if (ret)
3425 goto error2;
3427 ib_set_client_data(device, &cm_client, cm_dev);
3429 write_lock_irqsave(&cm.device_lock, flags);
3430 list_add_tail(&cm_dev->list, &cm.device_list);
3431 write_unlock_irqrestore(&cm.device_lock, flags);
3432 return;
3434 error2:
3435 ib_unregister_mad_agent(port->mad_agent);
3436 error1:
3437 port_modify.set_port_cap_mask = 0;
3438 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3439 while (--i) {
3440 port = &cm_dev->port[i-1];
3441 ib_modify_port(device, port->port_num, 0, &port_modify);
3442 ib_unregister_mad_agent(port->mad_agent);
3444 kfree(cm_dev);
3447 static void cm_remove_one(struct ib_device *device)
3449 struct cm_device *cm_dev;
3450 struct cm_port *port;
3451 struct ib_port_modify port_modify = {
3452 .clr_port_cap_mask = IB_PORT_CM_SUP
3454 unsigned long flags;
3455 int i;
3457 cm_dev = ib_get_client_data(device, &cm_client);
3458 if (!cm_dev)
3459 return;
3461 write_lock_irqsave(&cm.device_lock, flags);
3462 list_del(&cm_dev->list);
3463 write_unlock_irqrestore(&cm.device_lock, flags);
3465 for (i = 1; i <= device->phys_port_cnt; i++) {
3466 port = &cm_dev->port[i-1];
3467 ib_modify_port(device, port->port_num, 0, &port_modify);
3468 ib_unregister_mad_agent(port->mad_agent);
3470 kfree(cm_dev);
3473 static int __init ib_cm_init(void)
3475 int ret;
3477 memset(&cm, 0, sizeof cm);
3478 INIT_LIST_HEAD(&cm.device_list);
3479 rwlock_init(&cm.device_lock);
3480 spin_lock_init(&cm.lock);
3481 cm.listen_service_table = RB_ROOT;
3482 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3483 cm.remote_id_table = RB_ROOT;
3484 cm.remote_qp_table = RB_ROOT;
3485 cm.remote_sidr_table = RB_ROOT;
3486 idr_init(&cm.local_id_table);
3487 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3488 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3489 INIT_LIST_HEAD(&cm.timewait_list);
3491 cm.wq = create_workqueue("ib_cm");
3492 if (!cm.wq)
3493 return -ENOMEM;
3495 ret = ib_register_client(&cm_client);
3496 if (ret)
3497 goto error;
3499 return 0;
3500 error:
3501 destroy_workqueue(cm.wq);
3502 return ret;
3505 static void __exit ib_cm_cleanup(void)
3507 struct cm_timewait_info *timewait_info, *tmp;
3509 spin_lock_irq(&cm.lock);
3510 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3511 cancel_delayed_work(&timewait_info->work.work);
3512 spin_unlock_irq(&cm.lock);
3514 destroy_workqueue(cm.wq);
3516 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3517 list_del(&timewait_info->list);
3518 kfree(timewait_info);
3521 ib_unregister_client(&cm_client);
3522 idr_destroy(&cm.local_id_table);
3525 module_init(ib_cm_init);
3526 module_exit(ib_cm_cleanup);