This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / drivers / infiniband / core / cm.c
blob82013946efc99d13566fc1bbfcd68bea28351c21
1 /*
2 * Copyright (c) 2004-2007 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.
36 #include <linux/completion.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/device.h>
39 #include <linux/module.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/slab.h>
47 #include <linux/sysfs.h>
48 #include <linux/workqueue.h>
49 #include <linux/kdev_t.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_cm.h>
53 #include "cm_msgs.h"
55 MODULE_AUTHOR("Sean Hefty");
56 MODULE_DESCRIPTION("InfiniBand CM");
57 MODULE_LICENSE("Dual BSD/GPL");
59 static void cm_add_one(struct ib_device *device);
60 static void cm_remove_one(struct ib_device *device);
62 static struct ib_client cm_client = {
63 .name = "cm",
64 .add = cm_add_one,
65 .remove = cm_remove_one
68 static struct ib_cm {
69 spinlock_t lock;
70 struct list_head device_list;
71 rwlock_t device_lock;
72 struct rb_root listen_service_table;
73 u64 listen_service_id;
74 /* struct rb_root peer_service_table; todo: fix peer to peer */
75 struct rb_root remote_qp_table;
76 struct rb_root remote_id_table;
77 struct rb_root remote_sidr_table;
78 struct idr local_id_table;
79 __be32 random_id_operand;
80 struct list_head timewait_list;
81 struct workqueue_struct *wq;
82 } cm;
84 /* Counter indexes ordered by attribute ID */
85 enum {
86 CM_REQ_COUNTER,
87 CM_MRA_COUNTER,
88 CM_REJ_COUNTER,
89 CM_REP_COUNTER,
90 CM_RTU_COUNTER,
91 CM_DREQ_COUNTER,
92 CM_DREP_COUNTER,
93 CM_SIDR_REQ_COUNTER,
94 CM_SIDR_REP_COUNTER,
95 CM_LAP_COUNTER,
96 CM_APR_COUNTER,
97 CM_ATTR_COUNT,
98 CM_ATTR_ID_OFFSET = 0x0010,
101 enum {
102 CM_XMIT,
103 CM_XMIT_RETRIES,
104 CM_RECV,
105 CM_RECV_DUPLICATES,
106 CM_COUNTER_GROUPS
109 static char const counter_group_names[CM_COUNTER_GROUPS]
110 [sizeof("cm_rx_duplicates")] = {
111 "cm_tx_msgs", "cm_tx_retries",
112 "cm_rx_msgs", "cm_rx_duplicates"
115 struct cm_counter_group {
116 struct kobject obj;
117 atomic_long_t counter[CM_ATTR_COUNT];
120 struct cm_counter_attribute {
121 struct attribute attr;
122 int index;
125 #define CM_COUNTER_ATTR(_name, _index) \
126 struct cm_counter_attribute cm_##_name##_counter_attr = { \
127 .attr = { .name = __stringify(_name), .mode = 0444 }, \
128 .index = _index \
131 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
132 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
133 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
134 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
135 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
136 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
137 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
138 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
139 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
140 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
141 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
143 static struct attribute *cm_counter_default_attrs[] = {
144 &cm_req_counter_attr.attr,
145 &cm_mra_counter_attr.attr,
146 &cm_rej_counter_attr.attr,
147 &cm_rep_counter_attr.attr,
148 &cm_rtu_counter_attr.attr,
149 &cm_dreq_counter_attr.attr,
150 &cm_drep_counter_attr.attr,
151 &cm_sidr_req_counter_attr.attr,
152 &cm_sidr_rep_counter_attr.attr,
153 &cm_lap_counter_attr.attr,
154 &cm_apr_counter_attr.attr,
155 NULL
158 struct cm_port {
159 struct cm_device *cm_dev;
160 struct ib_mad_agent *mad_agent;
161 struct kobject port_obj;
162 u8 port_num;
163 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
166 struct cm_device {
167 struct list_head list;
168 struct ib_device *ib_device;
169 struct device *device;
170 u8 ack_delay;
171 struct cm_port *port[0];
174 struct cm_av {
175 struct cm_port *port;
176 union ib_gid dgid;
177 struct ib_ah_attr ah_attr;
178 u16 pkey_index;
179 u8 timeout;
182 struct cm_work {
183 struct delayed_work work;
184 struct list_head list;
185 struct cm_port *port;
186 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
187 __be32 local_id; /* Established / timewait */
188 __be32 remote_id;
189 struct ib_cm_event cm_event;
190 struct ib_sa_path_rec path[0];
193 struct cm_timewait_info {
194 struct cm_work work; /* Must be first. */
195 struct list_head list;
196 struct rb_node remote_qp_node;
197 struct rb_node remote_id_node;
198 __be64 remote_ca_guid;
199 __be32 remote_qpn;
200 u8 inserted_remote_qp;
201 u8 inserted_remote_id;
204 struct cm_id_private {
205 struct ib_cm_id id;
207 struct rb_node service_node;
208 struct rb_node sidr_id_node;
209 spinlock_t lock; /* Do not acquire inside cm.lock */
210 struct completion comp;
211 atomic_t refcount;
213 struct ib_mad_send_buf *msg;
214 struct cm_timewait_info *timewait_info;
215 /* todo: use alternate port on send failure */
216 struct cm_av av;
217 struct cm_av alt_av;
218 struct ib_cm_compare_data *compare_data;
220 void *private_data;
221 __be64 tid;
222 __be32 local_qpn;
223 __be32 remote_qpn;
224 enum ib_qp_type qp_type;
225 __be32 sq_psn;
226 __be32 rq_psn;
227 int timeout_ms;
228 enum ib_mtu path_mtu;
229 __be16 pkey;
230 u8 private_data_len;
231 u8 max_cm_retries;
232 u8 peer_to_peer;
233 u8 responder_resources;
234 u8 initiator_depth;
235 u8 retry_count;
236 u8 rnr_retry_count;
237 u8 service_timeout;
238 u8 target_ack_delay;
240 struct list_head work_list;
241 atomic_t work_count;
244 static void cm_work_handler(struct work_struct *work);
246 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
248 if (atomic_dec_and_test(&cm_id_priv->refcount))
249 complete(&cm_id_priv->comp);
252 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
253 struct ib_mad_send_buf **msg)
255 struct ib_mad_agent *mad_agent;
256 struct ib_mad_send_buf *m;
257 struct ib_ah *ah;
259 mad_agent = cm_id_priv->av.port->mad_agent;
260 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
261 if (IS_ERR(ah))
262 return PTR_ERR(ah);
264 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
265 cm_id_priv->av.pkey_index,
266 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
267 GFP_ATOMIC);
268 if (IS_ERR(m)) {
269 ib_destroy_ah(ah);
270 return PTR_ERR(m);
273 /* Timeout set by caller if response is expected. */
274 m->ah = ah;
275 m->retries = cm_id_priv->max_cm_retries;
277 atomic_inc(&cm_id_priv->refcount);
278 m->context[0] = cm_id_priv;
279 *msg = m;
280 return 0;
283 static int cm_alloc_response_msg(struct cm_port *port,
284 struct ib_mad_recv_wc *mad_recv_wc,
285 struct ib_mad_send_buf **msg)
287 struct ib_mad_send_buf *m;
288 struct ib_ah *ah;
290 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
291 mad_recv_wc->recv_buf.grh, port->port_num);
292 if (IS_ERR(ah))
293 return PTR_ERR(ah);
295 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
296 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
297 GFP_ATOMIC);
298 if (IS_ERR(m)) {
299 ib_destroy_ah(ah);
300 return PTR_ERR(m);
302 m->ah = ah;
303 *msg = m;
304 return 0;
307 static void cm_free_msg(struct ib_mad_send_buf *msg)
309 ib_destroy_ah(msg->ah);
310 if (msg->context[0])
311 cm_deref_id(msg->context[0]);
312 ib_free_send_mad(msg);
315 static void * cm_copy_private_data(const void *private_data,
316 u8 private_data_len)
318 void *data;
320 if (!private_data || !private_data_len)
321 return NULL;
323 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
324 if (!data)
325 return ERR_PTR(-ENOMEM);
327 return data;
330 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
331 void *private_data, u8 private_data_len)
333 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
334 kfree(cm_id_priv->private_data);
336 cm_id_priv->private_data = private_data;
337 cm_id_priv->private_data_len = private_data_len;
340 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
341 struct ib_grh *grh, struct cm_av *av)
343 av->port = port;
344 av->pkey_index = wc->pkey_index;
345 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
346 grh, &av->ah_attr);
349 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
351 struct cm_device *cm_dev;
352 struct cm_port *port = NULL;
353 unsigned long flags;
354 int ret;
355 u8 p;
357 read_lock_irqsave(&cm.device_lock, flags);
358 list_for_each_entry(cm_dev, &cm.device_list, list) {
359 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
360 &p, NULL)) {
361 port = cm_dev->port[p-1];
362 break;
365 read_unlock_irqrestore(&cm.device_lock, flags);
367 if (!port)
368 return -EINVAL;
370 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
371 be16_to_cpu(path->pkey), &av->pkey_index);
372 if (ret)
373 return ret;
375 av->port = port;
376 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
377 &av->ah_attr);
378 av->timeout = path->packet_life_time + 1;
379 return 0;
382 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
384 unsigned long flags;
385 int ret, id;
386 static int next_id;
388 do {
389 spin_lock_irqsave(&cm.lock, flags);
390 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
391 next_id, &id);
392 if (!ret)
393 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
394 spin_unlock_irqrestore(&cm.lock, flags);
395 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
397 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
398 return ret;
401 static void cm_free_id(__be32 local_id)
403 spin_lock_irq(&cm.lock);
404 idr_remove(&cm.local_id_table,
405 (__force int) (local_id ^ cm.random_id_operand));
406 spin_unlock_irq(&cm.lock);
409 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
411 struct cm_id_private *cm_id_priv;
413 cm_id_priv = idr_find(&cm.local_id_table,
414 (__force int) (local_id ^ cm.random_id_operand));
415 if (cm_id_priv) {
416 if (cm_id_priv->id.remote_id == remote_id)
417 atomic_inc(&cm_id_priv->refcount);
418 else
419 cm_id_priv = NULL;
422 return cm_id_priv;
425 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
427 struct cm_id_private *cm_id_priv;
429 spin_lock_irq(&cm.lock);
430 cm_id_priv = cm_get_id(local_id, remote_id);
431 spin_unlock_irq(&cm.lock);
433 return cm_id_priv;
436 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
438 int i;
440 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
441 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
442 ((unsigned long *) mask)[i];
445 static int cm_compare_data(struct ib_cm_compare_data *src_data,
446 struct ib_cm_compare_data *dst_data)
448 u8 src[IB_CM_COMPARE_SIZE];
449 u8 dst[IB_CM_COMPARE_SIZE];
451 if (!src_data || !dst_data)
452 return 0;
454 cm_mask_copy(src, src_data->data, dst_data->mask);
455 cm_mask_copy(dst, dst_data->data, src_data->mask);
456 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
459 static int cm_compare_private_data(u8 *private_data,
460 struct ib_cm_compare_data *dst_data)
462 u8 src[IB_CM_COMPARE_SIZE];
464 if (!dst_data)
465 return 0;
467 cm_mask_copy(src, private_data, dst_data->mask);
468 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
472 * Trivial helpers to strip endian annotation and compare; the
473 * endianness doesn't actually matter since we just need a stable
474 * order for the RB tree.
476 static int be32_lt(__be32 a, __be32 b)
478 return (__force u32) a < (__force u32) b;
481 static int be32_gt(__be32 a, __be32 b)
483 return (__force u32) a > (__force u32) b;
486 static int be64_lt(__be64 a, __be64 b)
488 return (__force u64) a < (__force u64) b;
491 static int be64_gt(__be64 a, __be64 b)
493 return (__force u64) a > (__force u64) b;
496 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
498 struct rb_node **link = &cm.listen_service_table.rb_node;
499 struct rb_node *parent = NULL;
500 struct cm_id_private *cur_cm_id_priv;
501 __be64 service_id = cm_id_priv->id.service_id;
502 __be64 service_mask = cm_id_priv->id.service_mask;
503 int data_cmp;
505 while (*link) {
506 parent = *link;
507 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
508 service_node);
509 data_cmp = cm_compare_data(cm_id_priv->compare_data,
510 cur_cm_id_priv->compare_data);
511 if ((cur_cm_id_priv->id.service_mask & service_id) ==
512 (service_mask & cur_cm_id_priv->id.service_id) &&
513 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
514 !data_cmp)
515 return cur_cm_id_priv;
517 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
518 link = &(*link)->rb_left;
519 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
520 link = &(*link)->rb_right;
521 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
522 link = &(*link)->rb_left;
523 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
524 link = &(*link)->rb_right;
525 else if (data_cmp < 0)
526 link = &(*link)->rb_left;
527 else
528 link = &(*link)->rb_right;
530 rb_link_node(&cm_id_priv->service_node, parent, link);
531 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
532 return NULL;
535 static struct cm_id_private * cm_find_listen(struct ib_device *device,
536 __be64 service_id,
537 u8 *private_data)
539 struct rb_node *node = cm.listen_service_table.rb_node;
540 struct cm_id_private *cm_id_priv;
541 int data_cmp;
543 while (node) {
544 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
545 data_cmp = cm_compare_private_data(private_data,
546 cm_id_priv->compare_data);
547 if ((cm_id_priv->id.service_mask & service_id) ==
548 cm_id_priv->id.service_id &&
549 (cm_id_priv->id.device == device) && !data_cmp)
550 return cm_id_priv;
552 if (device < cm_id_priv->id.device)
553 node = node->rb_left;
554 else if (device > cm_id_priv->id.device)
555 node = node->rb_right;
556 else if (be64_lt(service_id, cm_id_priv->id.service_id))
557 node = node->rb_left;
558 else if (be64_gt(service_id, cm_id_priv->id.service_id))
559 node = node->rb_right;
560 else if (data_cmp < 0)
561 node = node->rb_left;
562 else
563 node = node->rb_right;
565 return NULL;
568 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
569 *timewait_info)
571 struct rb_node **link = &cm.remote_id_table.rb_node;
572 struct rb_node *parent = NULL;
573 struct cm_timewait_info *cur_timewait_info;
574 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
575 __be32 remote_id = timewait_info->work.remote_id;
577 while (*link) {
578 parent = *link;
579 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
580 remote_id_node);
581 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
582 link = &(*link)->rb_left;
583 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
584 link = &(*link)->rb_right;
585 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
586 link = &(*link)->rb_left;
587 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
588 link = &(*link)->rb_right;
589 else
590 return cur_timewait_info;
592 timewait_info->inserted_remote_id = 1;
593 rb_link_node(&timewait_info->remote_id_node, parent, link);
594 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
595 return NULL;
598 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
599 __be32 remote_id)
601 struct rb_node *node = cm.remote_id_table.rb_node;
602 struct cm_timewait_info *timewait_info;
604 while (node) {
605 timewait_info = rb_entry(node, struct cm_timewait_info,
606 remote_id_node);
607 if (be32_lt(remote_id, timewait_info->work.remote_id))
608 node = node->rb_left;
609 else if (be32_gt(remote_id, timewait_info->work.remote_id))
610 node = node->rb_right;
611 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
612 node = node->rb_left;
613 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
614 node = node->rb_right;
615 else
616 return timewait_info;
618 return NULL;
621 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
622 *timewait_info)
624 struct rb_node **link = &cm.remote_qp_table.rb_node;
625 struct rb_node *parent = NULL;
626 struct cm_timewait_info *cur_timewait_info;
627 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
628 __be32 remote_qpn = timewait_info->remote_qpn;
630 while (*link) {
631 parent = *link;
632 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
633 remote_qp_node);
634 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
635 link = &(*link)->rb_left;
636 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
637 link = &(*link)->rb_right;
638 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
639 link = &(*link)->rb_left;
640 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
641 link = &(*link)->rb_right;
642 else
643 return cur_timewait_info;
645 timewait_info->inserted_remote_qp = 1;
646 rb_link_node(&timewait_info->remote_qp_node, parent, link);
647 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
648 return NULL;
651 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
652 *cm_id_priv)
654 struct rb_node **link = &cm.remote_sidr_table.rb_node;
655 struct rb_node *parent = NULL;
656 struct cm_id_private *cur_cm_id_priv;
657 union ib_gid *port_gid = &cm_id_priv->av.dgid;
658 __be32 remote_id = cm_id_priv->id.remote_id;
660 while (*link) {
661 parent = *link;
662 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
663 sidr_id_node);
664 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
665 link = &(*link)->rb_left;
666 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
667 link = &(*link)->rb_right;
668 else {
669 int cmp;
670 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
671 sizeof *port_gid);
672 if (cmp < 0)
673 link = &(*link)->rb_left;
674 else if (cmp > 0)
675 link = &(*link)->rb_right;
676 else
677 return cur_cm_id_priv;
680 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
681 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
682 return NULL;
685 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
686 enum ib_cm_sidr_status status)
688 struct ib_cm_sidr_rep_param param;
690 memset(&param, 0, sizeof param);
691 param.status = status;
692 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
695 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
696 ib_cm_handler cm_handler,
697 void *context)
699 struct cm_id_private *cm_id_priv;
700 int ret;
702 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
703 if (!cm_id_priv)
704 return ERR_PTR(-ENOMEM);
706 cm_id_priv->id.state = IB_CM_IDLE;
707 cm_id_priv->id.device = device;
708 cm_id_priv->id.cm_handler = cm_handler;
709 cm_id_priv->id.context = context;
710 cm_id_priv->id.remote_cm_qpn = 1;
711 ret = cm_alloc_id(cm_id_priv);
712 if (ret)
713 goto error;
715 spin_lock_init(&cm_id_priv->lock);
716 init_completion(&cm_id_priv->comp);
717 INIT_LIST_HEAD(&cm_id_priv->work_list);
718 atomic_set(&cm_id_priv->work_count, -1);
719 atomic_set(&cm_id_priv->refcount, 1);
720 return &cm_id_priv->id;
722 error:
723 kfree(cm_id_priv);
724 return ERR_PTR(-ENOMEM);
726 EXPORT_SYMBOL(ib_create_cm_id);
728 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
730 struct cm_work *work;
732 if (list_empty(&cm_id_priv->work_list))
733 return NULL;
735 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
736 list_del(&work->list);
737 return work;
740 static void cm_free_work(struct cm_work *work)
742 if (work->mad_recv_wc)
743 ib_free_recv_mad(work->mad_recv_wc);
744 kfree(work);
747 static inline int cm_convert_to_ms(int iba_time)
749 /* approximate conversion to ms from 4.096us x 2^iba_time */
750 return 1 << max(iba_time - 8, 0);
754 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
755 * Because of how ack_timeout is stored, adding one doubles the timeout.
756 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
757 * increment it (round up) only if the other is within 50%.
759 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
761 int ack_timeout = packet_life_time + 1;
763 if (ack_timeout >= ca_ack_delay)
764 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
765 else
766 ack_timeout = ca_ack_delay +
767 (ack_timeout >= (ca_ack_delay - 1));
769 return min(31, ack_timeout);
772 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
774 if (timewait_info->inserted_remote_id) {
775 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
776 timewait_info->inserted_remote_id = 0;
779 if (timewait_info->inserted_remote_qp) {
780 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
781 timewait_info->inserted_remote_qp = 0;
785 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
787 struct cm_timewait_info *timewait_info;
789 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
790 if (!timewait_info)
791 return ERR_PTR(-ENOMEM);
793 timewait_info->work.local_id = local_id;
794 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
795 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
796 return timewait_info;
799 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
801 int wait_time;
802 unsigned long flags;
804 spin_lock_irqsave(&cm.lock, flags);
805 cm_cleanup_timewait(cm_id_priv->timewait_info);
806 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
807 spin_unlock_irqrestore(&cm.lock, flags);
810 * The cm_id could be destroyed by the user before we exit timewait.
811 * To protect against this, we search for the cm_id after exiting
812 * timewait before notifying the user that we've exited timewait.
814 cm_id_priv->id.state = IB_CM_TIMEWAIT;
815 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
816 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
817 msecs_to_jiffies(wait_time));
818 cm_id_priv->timewait_info = NULL;
821 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
823 unsigned long flags;
825 cm_id_priv->id.state = IB_CM_IDLE;
826 if (cm_id_priv->timewait_info) {
827 spin_lock_irqsave(&cm.lock, flags);
828 cm_cleanup_timewait(cm_id_priv->timewait_info);
829 spin_unlock_irqrestore(&cm.lock, flags);
830 kfree(cm_id_priv->timewait_info);
831 cm_id_priv->timewait_info = NULL;
835 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
837 struct cm_id_private *cm_id_priv;
838 struct cm_work *work;
840 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
841 retest:
842 spin_lock_irq(&cm_id_priv->lock);
843 switch (cm_id->state) {
844 case IB_CM_LISTEN:
845 cm_id->state = IB_CM_IDLE;
846 spin_unlock_irq(&cm_id_priv->lock);
847 spin_lock_irq(&cm.lock);
848 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
849 spin_unlock_irq(&cm.lock);
850 break;
851 case IB_CM_SIDR_REQ_SENT:
852 cm_id->state = IB_CM_IDLE;
853 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
854 spin_unlock_irq(&cm_id_priv->lock);
855 break;
856 case IB_CM_SIDR_REQ_RCVD:
857 spin_unlock_irq(&cm_id_priv->lock);
858 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
859 break;
860 case IB_CM_REQ_SENT:
861 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
862 spin_unlock_irq(&cm_id_priv->lock);
863 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
864 &cm_id_priv->id.device->node_guid,
865 sizeof cm_id_priv->id.device->node_guid,
866 NULL, 0);
867 break;
868 case IB_CM_REQ_RCVD:
869 if (err == -ENOMEM) {
870 /* Do not reject to allow future retries. */
871 cm_reset_to_idle(cm_id_priv);
872 spin_unlock_irq(&cm_id_priv->lock);
873 } else {
874 spin_unlock_irq(&cm_id_priv->lock);
875 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
876 NULL, 0, NULL, 0);
878 break;
879 case IB_CM_MRA_REQ_RCVD:
880 case IB_CM_REP_SENT:
881 case IB_CM_MRA_REP_RCVD:
882 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
883 /* Fall through */
884 case IB_CM_MRA_REQ_SENT:
885 case IB_CM_REP_RCVD:
886 case IB_CM_MRA_REP_SENT:
887 spin_unlock_irq(&cm_id_priv->lock);
888 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
889 NULL, 0, NULL, 0);
890 break;
891 case IB_CM_ESTABLISHED:
892 spin_unlock_irq(&cm_id_priv->lock);
893 ib_send_cm_dreq(cm_id, NULL, 0);
894 goto retest;
895 case IB_CM_DREQ_SENT:
896 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
897 cm_enter_timewait(cm_id_priv);
898 spin_unlock_irq(&cm_id_priv->lock);
899 break;
900 case IB_CM_DREQ_RCVD:
901 spin_unlock_irq(&cm_id_priv->lock);
902 ib_send_cm_drep(cm_id, NULL, 0);
903 break;
904 default:
905 spin_unlock_irq(&cm_id_priv->lock);
906 break;
909 cm_free_id(cm_id->local_id);
910 cm_deref_id(cm_id_priv);
911 wait_for_completion(&cm_id_priv->comp);
912 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
913 cm_free_work(work);
914 kfree(cm_id_priv->compare_data);
915 kfree(cm_id_priv->private_data);
916 kfree(cm_id_priv);
919 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
921 cm_destroy_id(cm_id, 0);
923 EXPORT_SYMBOL(ib_destroy_cm_id);
925 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
926 struct ib_cm_compare_data *compare_data)
928 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
929 unsigned long flags;
930 int ret = 0;
932 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
933 service_id &= service_mask;
934 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
935 (service_id != IB_CM_ASSIGN_SERVICE_ID))
936 return -EINVAL;
938 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
939 if (cm_id->state != IB_CM_IDLE)
940 return -EINVAL;
942 if (compare_data) {
943 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
944 GFP_KERNEL);
945 if (!cm_id_priv->compare_data)
946 return -ENOMEM;
947 cm_mask_copy(cm_id_priv->compare_data->data,
948 compare_data->data, compare_data->mask);
949 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
950 IB_CM_COMPARE_SIZE);
953 cm_id->state = IB_CM_LISTEN;
955 spin_lock_irqsave(&cm.lock, flags);
956 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
957 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
958 cm_id->service_mask = ~cpu_to_be64(0);
959 } else {
960 cm_id->service_id = service_id;
961 cm_id->service_mask = service_mask;
963 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
964 spin_unlock_irqrestore(&cm.lock, flags);
966 if (cur_cm_id_priv) {
967 cm_id->state = IB_CM_IDLE;
968 kfree(cm_id_priv->compare_data);
969 cm_id_priv->compare_data = NULL;
970 ret = -EBUSY;
972 return ret;
974 EXPORT_SYMBOL(ib_cm_listen);
976 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
977 enum cm_msg_sequence msg_seq)
979 u64 hi_tid, low_tid;
981 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
982 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
983 (msg_seq << 30));
984 return cpu_to_be64(hi_tid | low_tid);
987 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
988 __be16 attr_id, __be64 tid)
990 hdr->base_version = IB_MGMT_BASE_VERSION;
991 hdr->mgmt_class = IB_MGMT_CLASS_CM;
992 hdr->class_version = IB_CM_CLASS_VERSION;
993 hdr->method = IB_MGMT_METHOD_SEND;
994 hdr->attr_id = attr_id;
995 hdr->tid = tid;
998 static void cm_format_req(struct cm_req_msg *req_msg,
999 struct cm_id_private *cm_id_priv,
1000 struct ib_cm_req_param *param)
1002 struct ib_sa_path_rec *pri_path = param->primary_path;
1003 struct ib_sa_path_rec *alt_path = param->alternate_path;
1005 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1006 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1008 req_msg->local_comm_id = cm_id_priv->id.local_id;
1009 req_msg->service_id = param->service_id;
1010 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1011 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1012 cm_req_set_resp_res(req_msg, param->responder_resources);
1013 cm_req_set_init_depth(req_msg, param->initiator_depth);
1014 cm_req_set_remote_resp_timeout(req_msg,
1015 param->remote_cm_response_timeout);
1016 cm_req_set_qp_type(req_msg, param->qp_type);
1017 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1018 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1019 cm_req_set_local_resp_timeout(req_msg,
1020 param->local_cm_response_timeout);
1021 cm_req_set_retry_count(req_msg, param->retry_count);
1022 req_msg->pkey = param->primary_path->pkey;
1023 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1024 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1025 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1026 cm_req_set_srq(req_msg, param->srq);
1028 if (pri_path->hop_limit <= 1) {
1029 req_msg->primary_local_lid = pri_path->slid;
1030 req_msg->primary_remote_lid = pri_path->dlid;
1031 } else {
1032 /* Work-around until there's a way to obtain remote LID info */
1033 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1034 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1036 req_msg->primary_local_gid = pri_path->sgid;
1037 req_msg->primary_remote_gid = pri_path->dgid;
1038 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1039 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1040 req_msg->primary_traffic_class = pri_path->traffic_class;
1041 req_msg->primary_hop_limit = pri_path->hop_limit;
1042 cm_req_set_primary_sl(req_msg, pri_path->sl);
1043 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1044 cm_req_set_primary_local_ack_timeout(req_msg,
1045 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1046 pri_path->packet_life_time));
1048 if (alt_path) {
1049 if (alt_path->hop_limit <= 1) {
1050 req_msg->alt_local_lid = alt_path->slid;
1051 req_msg->alt_remote_lid = alt_path->dlid;
1052 } else {
1053 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1054 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1056 req_msg->alt_local_gid = alt_path->sgid;
1057 req_msg->alt_remote_gid = alt_path->dgid;
1058 cm_req_set_alt_flow_label(req_msg,
1059 alt_path->flow_label);
1060 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1061 req_msg->alt_traffic_class = alt_path->traffic_class;
1062 req_msg->alt_hop_limit = alt_path->hop_limit;
1063 cm_req_set_alt_sl(req_msg, alt_path->sl);
1064 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1065 cm_req_set_alt_local_ack_timeout(req_msg,
1066 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1067 alt_path->packet_life_time));
1070 if (param->private_data && param->private_data_len)
1071 memcpy(req_msg->private_data, param->private_data,
1072 param->private_data_len);
1075 static int cm_validate_req_param(struct ib_cm_req_param *param)
1077 /* peer-to-peer not supported */
1078 if (param->peer_to_peer)
1079 return -EINVAL;
1081 if (!param->primary_path)
1082 return -EINVAL;
1084 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1085 return -EINVAL;
1087 if (param->private_data &&
1088 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1089 return -EINVAL;
1091 if (param->alternate_path &&
1092 (param->alternate_path->pkey != param->primary_path->pkey ||
1093 param->alternate_path->mtu != param->primary_path->mtu))
1094 return -EINVAL;
1096 return 0;
1099 int ib_send_cm_req(struct ib_cm_id *cm_id,
1100 struct ib_cm_req_param *param)
1102 struct cm_id_private *cm_id_priv;
1103 struct cm_req_msg *req_msg;
1104 unsigned long flags;
1105 int ret;
1107 ret = cm_validate_req_param(param);
1108 if (ret)
1109 return ret;
1111 /* Verify that we're not in timewait. */
1112 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1113 spin_lock_irqsave(&cm_id_priv->lock, flags);
1114 if (cm_id->state != IB_CM_IDLE) {
1115 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1116 ret = -EINVAL;
1117 goto out;
1119 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1121 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1122 id.local_id);
1123 if (IS_ERR(cm_id_priv->timewait_info)) {
1124 ret = PTR_ERR(cm_id_priv->timewait_info);
1125 goto out;
1128 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1129 if (ret)
1130 goto error1;
1131 if (param->alternate_path) {
1132 ret = cm_init_av_by_path(param->alternate_path,
1133 &cm_id_priv->alt_av);
1134 if (ret)
1135 goto error1;
1137 cm_id->service_id = param->service_id;
1138 cm_id->service_mask = ~cpu_to_be64(0);
1139 cm_id_priv->timeout_ms = cm_convert_to_ms(
1140 param->primary_path->packet_life_time) * 2 +
1141 cm_convert_to_ms(
1142 param->remote_cm_response_timeout);
1143 cm_id_priv->max_cm_retries = param->max_cm_retries;
1144 cm_id_priv->initiator_depth = param->initiator_depth;
1145 cm_id_priv->responder_resources = param->responder_resources;
1146 cm_id_priv->retry_count = param->retry_count;
1147 cm_id_priv->path_mtu = param->primary_path->mtu;
1148 cm_id_priv->pkey = param->primary_path->pkey;
1149 cm_id_priv->qp_type = param->qp_type;
1151 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1152 if (ret)
1153 goto error1;
1155 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1156 cm_format_req(req_msg, cm_id_priv, param);
1157 cm_id_priv->tid = req_msg->hdr.tid;
1158 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1159 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1161 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1162 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1164 spin_lock_irqsave(&cm_id_priv->lock, flags);
1165 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1166 if (ret) {
1167 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1168 goto error2;
1170 BUG_ON(cm_id->state != IB_CM_IDLE);
1171 cm_id->state = IB_CM_REQ_SENT;
1172 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1173 return 0;
1175 error2: cm_free_msg(cm_id_priv->msg);
1176 error1: kfree(cm_id_priv->timewait_info);
1177 out: return ret;
1179 EXPORT_SYMBOL(ib_send_cm_req);
1181 static int cm_issue_rej(struct cm_port *port,
1182 struct ib_mad_recv_wc *mad_recv_wc,
1183 enum ib_cm_rej_reason reason,
1184 enum cm_msg_response msg_rejected,
1185 void *ari, u8 ari_length)
1187 struct ib_mad_send_buf *msg = NULL;
1188 struct cm_rej_msg *rej_msg, *rcv_msg;
1189 int ret;
1191 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1192 if (ret)
1193 return ret;
1195 /* We just need common CM header information. Cast to any message. */
1196 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1197 rej_msg = (struct cm_rej_msg *) msg->mad;
1199 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1200 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1201 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1202 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1203 rej_msg->reason = cpu_to_be16(reason);
1205 if (ari && ari_length) {
1206 cm_rej_set_reject_info_len(rej_msg, ari_length);
1207 memcpy(rej_msg->ari, ari, ari_length);
1210 ret = ib_post_send_mad(msg, NULL);
1211 if (ret)
1212 cm_free_msg(msg);
1214 return ret;
1217 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1218 __be32 local_qpn, __be32 remote_qpn)
1220 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1221 ((local_ca_guid == remote_ca_guid) &&
1222 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1225 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1226 struct ib_sa_path_rec *primary_path,
1227 struct ib_sa_path_rec *alt_path)
1229 memset(primary_path, 0, sizeof *primary_path);
1230 primary_path->dgid = req_msg->primary_local_gid;
1231 primary_path->sgid = req_msg->primary_remote_gid;
1232 primary_path->dlid = req_msg->primary_local_lid;
1233 primary_path->slid = req_msg->primary_remote_lid;
1234 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1235 primary_path->hop_limit = req_msg->primary_hop_limit;
1236 primary_path->traffic_class = req_msg->primary_traffic_class;
1237 primary_path->reversible = 1;
1238 primary_path->pkey = req_msg->pkey;
1239 primary_path->sl = cm_req_get_primary_sl(req_msg);
1240 primary_path->mtu_selector = IB_SA_EQ;
1241 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1242 primary_path->rate_selector = IB_SA_EQ;
1243 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1244 primary_path->packet_life_time_selector = IB_SA_EQ;
1245 primary_path->packet_life_time =
1246 cm_req_get_primary_local_ack_timeout(req_msg);
1247 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1249 if (req_msg->alt_local_lid) {
1250 memset(alt_path, 0, sizeof *alt_path);
1251 alt_path->dgid = req_msg->alt_local_gid;
1252 alt_path->sgid = req_msg->alt_remote_gid;
1253 alt_path->dlid = req_msg->alt_local_lid;
1254 alt_path->slid = req_msg->alt_remote_lid;
1255 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1256 alt_path->hop_limit = req_msg->alt_hop_limit;
1257 alt_path->traffic_class = req_msg->alt_traffic_class;
1258 alt_path->reversible = 1;
1259 alt_path->pkey = req_msg->pkey;
1260 alt_path->sl = cm_req_get_alt_sl(req_msg);
1261 alt_path->mtu_selector = IB_SA_EQ;
1262 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1263 alt_path->rate_selector = IB_SA_EQ;
1264 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1265 alt_path->packet_life_time_selector = IB_SA_EQ;
1266 alt_path->packet_life_time =
1267 cm_req_get_alt_local_ack_timeout(req_msg);
1268 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1272 static void cm_format_req_event(struct cm_work *work,
1273 struct cm_id_private *cm_id_priv,
1274 struct ib_cm_id *listen_id)
1276 struct cm_req_msg *req_msg;
1277 struct ib_cm_req_event_param *param;
1279 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1280 param = &work->cm_event.param.req_rcvd;
1281 param->listen_id = listen_id;
1282 param->port = cm_id_priv->av.port->port_num;
1283 param->primary_path = &work->path[0];
1284 if (req_msg->alt_local_lid)
1285 param->alternate_path = &work->path[1];
1286 else
1287 param->alternate_path = NULL;
1288 param->remote_ca_guid = req_msg->local_ca_guid;
1289 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1290 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1291 param->qp_type = cm_req_get_qp_type(req_msg);
1292 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1293 param->responder_resources = cm_req_get_init_depth(req_msg);
1294 param->initiator_depth = cm_req_get_resp_res(req_msg);
1295 param->local_cm_response_timeout =
1296 cm_req_get_remote_resp_timeout(req_msg);
1297 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1298 param->remote_cm_response_timeout =
1299 cm_req_get_local_resp_timeout(req_msg);
1300 param->retry_count = cm_req_get_retry_count(req_msg);
1301 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1302 param->srq = cm_req_get_srq(req_msg);
1303 work->cm_event.private_data = &req_msg->private_data;
1306 static void cm_process_work(struct cm_id_private *cm_id_priv,
1307 struct cm_work *work)
1309 int ret;
1311 /* We will typically only have the current event to report. */
1312 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1313 cm_free_work(work);
1315 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1316 spin_lock_irq(&cm_id_priv->lock);
1317 work = cm_dequeue_work(cm_id_priv);
1318 spin_unlock_irq(&cm_id_priv->lock);
1319 BUG_ON(!work);
1320 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1321 &work->cm_event);
1322 cm_free_work(work);
1324 cm_deref_id(cm_id_priv);
1325 if (ret)
1326 cm_destroy_id(&cm_id_priv->id, ret);
1329 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1330 struct cm_id_private *cm_id_priv,
1331 enum cm_msg_response msg_mraed, u8 service_timeout,
1332 const void *private_data, u8 private_data_len)
1334 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1335 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1336 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1337 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1338 cm_mra_set_service_timeout(mra_msg, service_timeout);
1340 if (private_data && private_data_len)
1341 memcpy(mra_msg->private_data, private_data, private_data_len);
1344 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1345 struct cm_id_private *cm_id_priv,
1346 enum ib_cm_rej_reason reason,
1347 void *ari,
1348 u8 ari_length,
1349 const void *private_data,
1350 u8 private_data_len)
1352 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1353 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1355 switch(cm_id_priv->id.state) {
1356 case IB_CM_REQ_RCVD:
1357 rej_msg->local_comm_id = 0;
1358 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1359 break;
1360 case IB_CM_MRA_REQ_SENT:
1361 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1362 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1363 break;
1364 case IB_CM_REP_RCVD:
1365 case IB_CM_MRA_REP_SENT:
1366 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1367 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1368 break;
1369 default:
1370 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1371 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1372 break;
1375 rej_msg->reason = cpu_to_be16(reason);
1376 if (ari && ari_length) {
1377 cm_rej_set_reject_info_len(rej_msg, ari_length);
1378 memcpy(rej_msg->ari, ari, ari_length);
1381 if (private_data && private_data_len)
1382 memcpy(rej_msg->private_data, private_data, private_data_len);
1385 static void cm_dup_req_handler(struct cm_work *work,
1386 struct cm_id_private *cm_id_priv)
1388 struct ib_mad_send_buf *msg = NULL;
1389 int ret;
1391 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1392 counter[CM_REQ_COUNTER]);
1394 /* Quick state check to discard duplicate REQs. */
1395 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1396 return;
1398 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1399 if (ret)
1400 return;
1402 spin_lock_irq(&cm_id_priv->lock);
1403 switch (cm_id_priv->id.state) {
1404 case IB_CM_MRA_REQ_SENT:
1405 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1406 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1407 cm_id_priv->private_data,
1408 cm_id_priv->private_data_len);
1409 break;
1410 case IB_CM_TIMEWAIT:
1411 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1412 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1413 break;
1414 default:
1415 goto unlock;
1417 spin_unlock_irq(&cm_id_priv->lock);
1419 ret = ib_post_send_mad(msg, NULL);
1420 if (ret)
1421 goto free;
1422 return;
1424 unlock: spin_unlock_irq(&cm_id_priv->lock);
1425 free: cm_free_msg(msg);
1428 static struct cm_id_private * cm_match_req(struct cm_work *work,
1429 struct cm_id_private *cm_id_priv)
1431 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1432 struct cm_timewait_info *timewait_info;
1433 struct cm_req_msg *req_msg;
1435 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1437 /* Check for possible duplicate REQ. */
1438 spin_lock_irq(&cm.lock);
1439 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1440 if (timewait_info) {
1441 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1442 timewait_info->work.remote_id);
1443 spin_unlock_irq(&cm.lock);
1444 if (cur_cm_id_priv) {
1445 cm_dup_req_handler(work, cur_cm_id_priv);
1446 cm_deref_id(cur_cm_id_priv);
1448 return NULL;
1451 /* Check for stale connections. */
1452 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1453 if (timewait_info) {
1454 cm_cleanup_timewait(cm_id_priv->timewait_info);
1455 spin_unlock_irq(&cm.lock);
1456 cm_issue_rej(work->port, work->mad_recv_wc,
1457 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1458 NULL, 0);
1459 return NULL;
1462 /* Find matching listen request. */
1463 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1464 req_msg->service_id,
1465 req_msg->private_data);
1466 if (!listen_cm_id_priv) {
1467 cm_cleanup_timewait(cm_id_priv->timewait_info);
1468 spin_unlock_irq(&cm.lock);
1469 cm_issue_rej(work->port, work->mad_recv_wc,
1470 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1471 NULL, 0);
1472 goto out;
1474 atomic_inc(&listen_cm_id_priv->refcount);
1475 atomic_inc(&cm_id_priv->refcount);
1476 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1477 atomic_inc(&cm_id_priv->work_count);
1478 spin_unlock_irq(&cm.lock);
1479 out:
1480 return listen_cm_id_priv;
1484 * Work-around for inter-subnet connections. If the LIDs are permissive,
1485 * we need to override the LID/SL data in the REQ with the LID information
1486 * in the work completion.
1488 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1490 if (!cm_req_get_primary_subnet_local(req_msg)) {
1491 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1492 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1493 cm_req_set_primary_sl(req_msg, wc->sl);
1496 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1497 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1500 if (!cm_req_get_alt_subnet_local(req_msg)) {
1501 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1502 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1503 cm_req_set_alt_sl(req_msg, wc->sl);
1506 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1507 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1511 static int cm_req_handler(struct cm_work *work)
1513 struct ib_cm_id *cm_id;
1514 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1515 struct cm_req_msg *req_msg;
1516 int ret;
1518 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1520 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1521 if (IS_ERR(cm_id))
1522 return PTR_ERR(cm_id);
1524 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1525 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1526 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1527 work->mad_recv_wc->recv_buf.grh,
1528 &cm_id_priv->av);
1529 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1530 id.local_id);
1531 if (IS_ERR(cm_id_priv->timewait_info)) {
1532 ret = PTR_ERR(cm_id_priv->timewait_info);
1533 goto destroy;
1535 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1536 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1537 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1539 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1540 if (!listen_cm_id_priv) {
1541 ret = -EINVAL;
1542 kfree(cm_id_priv->timewait_info);
1543 goto destroy;
1546 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1547 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1548 cm_id_priv->id.service_id = req_msg->service_id;
1549 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1551 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1552 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1553 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1554 if (ret) {
1555 ib_get_cached_gid(work->port->cm_dev->ib_device,
1556 work->port->port_num, 0, &work->path[0].sgid);
1557 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1558 &work->path[0].sgid, sizeof work->path[0].sgid,
1559 NULL, 0);
1560 goto rejected;
1562 if (req_msg->alt_local_lid) {
1563 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1564 if (ret) {
1565 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1566 &work->path[0].sgid,
1567 sizeof work->path[0].sgid, NULL, 0);
1568 goto rejected;
1571 cm_id_priv->tid = req_msg->hdr.tid;
1572 cm_id_priv->timeout_ms = cm_convert_to_ms(
1573 cm_req_get_local_resp_timeout(req_msg));
1574 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1575 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1576 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1577 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1578 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1579 cm_id_priv->pkey = req_msg->pkey;
1580 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1581 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1582 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1583 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1585 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1586 cm_process_work(cm_id_priv, work);
1587 cm_deref_id(listen_cm_id_priv);
1588 return 0;
1590 rejected:
1591 atomic_dec(&cm_id_priv->refcount);
1592 cm_deref_id(listen_cm_id_priv);
1593 destroy:
1594 ib_destroy_cm_id(cm_id);
1595 return ret;
1598 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1599 struct cm_id_private *cm_id_priv,
1600 struct ib_cm_rep_param *param)
1602 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1603 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1604 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1605 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1606 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1607 rep_msg->resp_resources = param->responder_resources;
1608 rep_msg->initiator_depth = param->initiator_depth;
1609 cm_rep_set_target_ack_delay(rep_msg,
1610 cm_id_priv->av.port->cm_dev->ack_delay);
1611 cm_rep_set_failover(rep_msg, param->failover_accepted);
1612 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1613 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1614 cm_rep_set_srq(rep_msg, param->srq);
1615 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1617 if (param->private_data && param->private_data_len)
1618 memcpy(rep_msg->private_data, param->private_data,
1619 param->private_data_len);
1622 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1623 struct ib_cm_rep_param *param)
1625 struct cm_id_private *cm_id_priv;
1626 struct ib_mad_send_buf *msg;
1627 struct cm_rep_msg *rep_msg;
1628 unsigned long flags;
1629 int ret;
1631 if (param->private_data &&
1632 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1633 return -EINVAL;
1635 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1636 spin_lock_irqsave(&cm_id_priv->lock, flags);
1637 if (cm_id->state != IB_CM_REQ_RCVD &&
1638 cm_id->state != IB_CM_MRA_REQ_SENT) {
1639 ret = -EINVAL;
1640 goto out;
1643 ret = cm_alloc_msg(cm_id_priv, &msg);
1644 if (ret)
1645 goto out;
1647 rep_msg = (struct cm_rep_msg *) msg->mad;
1648 cm_format_rep(rep_msg, cm_id_priv, param);
1649 msg->timeout_ms = cm_id_priv->timeout_ms;
1650 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1652 ret = ib_post_send_mad(msg, NULL);
1653 if (ret) {
1654 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1655 cm_free_msg(msg);
1656 return ret;
1659 cm_id->state = IB_CM_REP_SENT;
1660 cm_id_priv->msg = msg;
1661 cm_id_priv->initiator_depth = param->initiator_depth;
1662 cm_id_priv->responder_resources = param->responder_resources;
1663 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1664 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1666 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1667 return ret;
1669 EXPORT_SYMBOL(ib_send_cm_rep);
1671 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1672 struct cm_id_private *cm_id_priv,
1673 const void *private_data,
1674 u8 private_data_len)
1676 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1677 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1678 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1680 if (private_data && private_data_len)
1681 memcpy(rtu_msg->private_data, private_data, private_data_len);
1684 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1685 const void *private_data,
1686 u8 private_data_len)
1688 struct cm_id_private *cm_id_priv;
1689 struct ib_mad_send_buf *msg;
1690 unsigned long flags;
1691 void *data;
1692 int ret;
1694 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1695 return -EINVAL;
1697 data = cm_copy_private_data(private_data, private_data_len);
1698 if (IS_ERR(data))
1699 return PTR_ERR(data);
1701 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1702 spin_lock_irqsave(&cm_id_priv->lock, flags);
1703 if (cm_id->state != IB_CM_REP_RCVD &&
1704 cm_id->state != IB_CM_MRA_REP_SENT) {
1705 ret = -EINVAL;
1706 goto error;
1709 ret = cm_alloc_msg(cm_id_priv, &msg);
1710 if (ret)
1711 goto error;
1713 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1714 private_data, private_data_len);
1716 ret = ib_post_send_mad(msg, NULL);
1717 if (ret) {
1718 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1719 cm_free_msg(msg);
1720 kfree(data);
1721 return ret;
1724 cm_id->state = IB_CM_ESTABLISHED;
1725 cm_set_private_data(cm_id_priv, data, private_data_len);
1726 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1727 return 0;
1729 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1730 kfree(data);
1731 return ret;
1733 EXPORT_SYMBOL(ib_send_cm_rtu);
1735 static void cm_format_rep_event(struct cm_work *work)
1737 struct cm_rep_msg *rep_msg;
1738 struct ib_cm_rep_event_param *param;
1740 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1741 param = &work->cm_event.param.rep_rcvd;
1742 param->remote_ca_guid = rep_msg->local_ca_guid;
1743 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1744 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1745 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1746 param->responder_resources = rep_msg->initiator_depth;
1747 param->initiator_depth = rep_msg->resp_resources;
1748 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1749 param->failover_accepted = cm_rep_get_failover(rep_msg);
1750 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1751 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1752 param->srq = cm_rep_get_srq(rep_msg);
1753 work->cm_event.private_data = &rep_msg->private_data;
1756 static void cm_dup_rep_handler(struct cm_work *work)
1758 struct cm_id_private *cm_id_priv;
1759 struct cm_rep_msg *rep_msg;
1760 struct ib_mad_send_buf *msg = NULL;
1761 int ret;
1763 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1764 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1765 rep_msg->local_comm_id);
1766 if (!cm_id_priv)
1767 return;
1769 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1770 counter[CM_REP_COUNTER]);
1771 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1772 if (ret)
1773 goto deref;
1775 spin_lock_irq(&cm_id_priv->lock);
1776 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1777 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1778 cm_id_priv->private_data,
1779 cm_id_priv->private_data_len);
1780 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1781 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1782 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1783 cm_id_priv->private_data,
1784 cm_id_priv->private_data_len);
1785 else
1786 goto unlock;
1787 spin_unlock_irq(&cm_id_priv->lock);
1789 ret = ib_post_send_mad(msg, NULL);
1790 if (ret)
1791 goto free;
1792 goto deref;
1794 unlock: spin_unlock_irq(&cm_id_priv->lock);
1795 free: cm_free_msg(msg);
1796 deref: cm_deref_id(cm_id_priv);
1799 static int cm_rep_handler(struct cm_work *work)
1801 struct cm_id_private *cm_id_priv;
1802 struct cm_rep_msg *rep_msg;
1803 int ret;
1805 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1806 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1807 if (!cm_id_priv) {
1808 cm_dup_rep_handler(work);
1809 return -EINVAL;
1812 cm_format_rep_event(work);
1814 spin_lock_irq(&cm_id_priv->lock);
1815 switch (cm_id_priv->id.state) {
1816 case IB_CM_REQ_SENT:
1817 case IB_CM_MRA_REQ_RCVD:
1818 break;
1819 default:
1820 spin_unlock_irq(&cm_id_priv->lock);
1821 ret = -EINVAL;
1822 goto error;
1825 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1826 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1827 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1829 spin_lock(&cm.lock);
1830 /* Check for duplicate REP. */
1831 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1832 spin_unlock(&cm.lock);
1833 spin_unlock_irq(&cm_id_priv->lock);
1834 ret = -EINVAL;
1835 goto error;
1837 /* Check for a stale connection. */
1838 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1839 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1840 &cm.remote_id_table);
1841 cm_id_priv->timewait_info->inserted_remote_id = 0;
1842 spin_unlock(&cm.lock);
1843 spin_unlock_irq(&cm_id_priv->lock);
1844 cm_issue_rej(work->port, work->mad_recv_wc,
1845 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1846 NULL, 0);
1847 ret = -EINVAL;
1848 goto error;
1850 spin_unlock(&cm.lock);
1852 cm_id_priv->id.state = IB_CM_REP_RCVD;
1853 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1854 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1855 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1856 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1857 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1858 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1859 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1860 cm_id_priv->av.timeout =
1861 cm_ack_timeout(cm_id_priv->target_ack_delay,
1862 cm_id_priv->av.timeout - 1);
1863 cm_id_priv->alt_av.timeout =
1864 cm_ack_timeout(cm_id_priv->target_ack_delay,
1865 cm_id_priv->alt_av.timeout - 1);
1867 /* todo: handle peer_to_peer */
1869 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1870 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1871 if (!ret)
1872 list_add_tail(&work->list, &cm_id_priv->work_list);
1873 spin_unlock_irq(&cm_id_priv->lock);
1875 if (ret)
1876 cm_process_work(cm_id_priv, work);
1877 else
1878 cm_deref_id(cm_id_priv);
1879 return 0;
1881 error:
1882 cm_deref_id(cm_id_priv);
1883 return ret;
1886 static int cm_establish_handler(struct cm_work *work)
1888 struct cm_id_private *cm_id_priv;
1889 int ret;
1891 /* See comment in cm_establish about lookup. */
1892 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1893 if (!cm_id_priv)
1894 return -EINVAL;
1896 spin_lock_irq(&cm_id_priv->lock);
1897 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1898 spin_unlock_irq(&cm_id_priv->lock);
1899 goto out;
1902 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1903 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1904 if (!ret)
1905 list_add_tail(&work->list, &cm_id_priv->work_list);
1906 spin_unlock_irq(&cm_id_priv->lock);
1908 if (ret)
1909 cm_process_work(cm_id_priv, work);
1910 else
1911 cm_deref_id(cm_id_priv);
1912 return 0;
1913 out:
1914 cm_deref_id(cm_id_priv);
1915 return -EINVAL;
1918 static int cm_rtu_handler(struct cm_work *work)
1920 struct cm_id_private *cm_id_priv;
1921 struct cm_rtu_msg *rtu_msg;
1922 int ret;
1924 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1925 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1926 rtu_msg->local_comm_id);
1927 if (!cm_id_priv)
1928 return -EINVAL;
1930 work->cm_event.private_data = &rtu_msg->private_data;
1932 spin_lock_irq(&cm_id_priv->lock);
1933 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1934 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1935 spin_unlock_irq(&cm_id_priv->lock);
1936 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1937 counter[CM_RTU_COUNTER]);
1938 goto out;
1940 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1942 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1943 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1944 if (!ret)
1945 list_add_tail(&work->list, &cm_id_priv->work_list);
1946 spin_unlock_irq(&cm_id_priv->lock);
1948 if (ret)
1949 cm_process_work(cm_id_priv, work);
1950 else
1951 cm_deref_id(cm_id_priv);
1952 return 0;
1953 out:
1954 cm_deref_id(cm_id_priv);
1955 return -EINVAL;
1958 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1959 struct cm_id_private *cm_id_priv,
1960 const void *private_data,
1961 u8 private_data_len)
1963 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1964 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1965 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1966 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1967 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1969 if (private_data && private_data_len)
1970 memcpy(dreq_msg->private_data, private_data, private_data_len);
1973 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1974 const void *private_data,
1975 u8 private_data_len)
1977 struct cm_id_private *cm_id_priv;
1978 struct ib_mad_send_buf *msg;
1979 unsigned long flags;
1980 int ret;
1982 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1983 return -EINVAL;
1985 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1986 spin_lock_irqsave(&cm_id_priv->lock, flags);
1987 if (cm_id->state != IB_CM_ESTABLISHED) {
1988 ret = -EINVAL;
1989 goto out;
1992 if (cm_id->lap_state == IB_CM_LAP_SENT ||
1993 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
1994 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1996 ret = cm_alloc_msg(cm_id_priv, &msg);
1997 if (ret) {
1998 cm_enter_timewait(cm_id_priv);
1999 goto out;
2002 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2003 private_data, private_data_len);
2004 msg->timeout_ms = cm_id_priv->timeout_ms;
2005 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2007 ret = ib_post_send_mad(msg, NULL);
2008 if (ret) {
2009 cm_enter_timewait(cm_id_priv);
2010 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2011 cm_free_msg(msg);
2012 return ret;
2015 cm_id->state = IB_CM_DREQ_SENT;
2016 cm_id_priv->msg = msg;
2017 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2018 return ret;
2020 EXPORT_SYMBOL(ib_send_cm_dreq);
2022 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2023 struct cm_id_private *cm_id_priv,
2024 const void *private_data,
2025 u8 private_data_len)
2027 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2028 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2029 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2031 if (private_data && private_data_len)
2032 memcpy(drep_msg->private_data, private_data, private_data_len);
2035 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2036 const void *private_data,
2037 u8 private_data_len)
2039 struct cm_id_private *cm_id_priv;
2040 struct ib_mad_send_buf *msg;
2041 unsigned long flags;
2042 void *data;
2043 int ret;
2045 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2046 return -EINVAL;
2048 data = cm_copy_private_data(private_data, private_data_len);
2049 if (IS_ERR(data))
2050 return PTR_ERR(data);
2052 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2053 spin_lock_irqsave(&cm_id_priv->lock, flags);
2054 if (cm_id->state != IB_CM_DREQ_RCVD) {
2055 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2056 kfree(data);
2057 return -EINVAL;
2060 cm_set_private_data(cm_id_priv, data, private_data_len);
2061 cm_enter_timewait(cm_id_priv);
2063 ret = cm_alloc_msg(cm_id_priv, &msg);
2064 if (ret)
2065 goto out;
2067 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2068 private_data, private_data_len);
2070 ret = ib_post_send_mad(msg, NULL);
2071 if (ret) {
2072 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2073 cm_free_msg(msg);
2074 return ret;
2077 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2078 return ret;
2080 EXPORT_SYMBOL(ib_send_cm_drep);
2082 static int cm_issue_drep(struct cm_port *port,
2083 struct ib_mad_recv_wc *mad_recv_wc)
2085 struct ib_mad_send_buf *msg = NULL;
2086 struct cm_dreq_msg *dreq_msg;
2087 struct cm_drep_msg *drep_msg;
2088 int ret;
2090 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2091 if (ret)
2092 return ret;
2094 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2095 drep_msg = (struct cm_drep_msg *) msg->mad;
2097 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2098 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2099 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2101 ret = ib_post_send_mad(msg, NULL);
2102 if (ret)
2103 cm_free_msg(msg);
2105 return ret;
2108 static int cm_dreq_handler(struct cm_work *work)
2110 struct cm_id_private *cm_id_priv;
2111 struct cm_dreq_msg *dreq_msg;
2112 struct ib_mad_send_buf *msg = NULL;
2113 int ret;
2115 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2116 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2117 dreq_msg->local_comm_id);
2118 if (!cm_id_priv) {
2119 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2120 counter[CM_DREQ_COUNTER]);
2121 cm_issue_drep(work->port, work->mad_recv_wc);
2122 return -EINVAL;
2125 work->cm_event.private_data = &dreq_msg->private_data;
2127 spin_lock_irq(&cm_id_priv->lock);
2128 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2129 goto unlock;
2131 switch (cm_id_priv->id.state) {
2132 case IB_CM_REP_SENT:
2133 case IB_CM_DREQ_SENT:
2134 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2135 break;
2136 case IB_CM_ESTABLISHED:
2137 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2138 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2139 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2140 break;
2141 case IB_CM_MRA_REP_RCVD:
2142 break;
2143 case IB_CM_TIMEWAIT:
2144 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2145 counter[CM_DREQ_COUNTER]);
2146 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2147 goto unlock;
2149 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2150 cm_id_priv->private_data,
2151 cm_id_priv->private_data_len);
2152 spin_unlock_irq(&cm_id_priv->lock);
2154 if (ib_post_send_mad(msg, NULL))
2155 cm_free_msg(msg);
2156 goto deref;
2157 case IB_CM_DREQ_RCVD:
2158 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2159 counter[CM_DREQ_COUNTER]);
2160 goto unlock;
2161 default:
2162 goto unlock;
2164 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2165 cm_id_priv->tid = dreq_msg->hdr.tid;
2166 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2167 if (!ret)
2168 list_add_tail(&work->list, &cm_id_priv->work_list);
2169 spin_unlock_irq(&cm_id_priv->lock);
2171 if (ret)
2172 cm_process_work(cm_id_priv, work);
2173 else
2174 cm_deref_id(cm_id_priv);
2175 return 0;
2177 unlock: spin_unlock_irq(&cm_id_priv->lock);
2178 deref: cm_deref_id(cm_id_priv);
2179 return -EINVAL;
2182 static int cm_drep_handler(struct cm_work *work)
2184 struct cm_id_private *cm_id_priv;
2185 struct cm_drep_msg *drep_msg;
2186 int ret;
2188 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2189 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2190 drep_msg->local_comm_id);
2191 if (!cm_id_priv)
2192 return -EINVAL;
2194 work->cm_event.private_data = &drep_msg->private_data;
2196 spin_lock_irq(&cm_id_priv->lock);
2197 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2198 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2199 spin_unlock_irq(&cm_id_priv->lock);
2200 goto out;
2202 cm_enter_timewait(cm_id_priv);
2204 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2205 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2206 if (!ret)
2207 list_add_tail(&work->list, &cm_id_priv->work_list);
2208 spin_unlock_irq(&cm_id_priv->lock);
2210 if (ret)
2211 cm_process_work(cm_id_priv, work);
2212 else
2213 cm_deref_id(cm_id_priv);
2214 return 0;
2215 out:
2216 cm_deref_id(cm_id_priv);
2217 return -EINVAL;
2220 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2221 enum ib_cm_rej_reason reason,
2222 void *ari,
2223 u8 ari_length,
2224 const void *private_data,
2225 u8 private_data_len)
2227 struct cm_id_private *cm_id_priv;
2228 struct ib_mad_send_buf *msg;
2229 unsigned long flags;
2230 int ret;
2232 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2233 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2234 return -EINVAL;
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->state) {
2240 case IB_CM_REQ_SENT:
2241 case IB_CM_MRA_REQ_RCVD:
2242 case IB_CM_REQ_RCVD:
2243 case IB_CM_MRA_REQ_SENT:
2244 case IB_CM_REP_RCVD:
2245 case IB_CM_MRA_REP_SENT:
2246 ret = cm_alloc_msg(cm_id_priv, &msg);
2247 if (!ret)
2248 cm_format_rej((struct cm_rej_msg *) msg->mad,
2249 cm_id_priv, reason, ari, ari_length,
2250 private_data, private_data_len);
2252 cm_reset_to_idle(cm_id_priv);
2253 break;
2254 case IB_CM_REP_SENT:
2255 case IB_CM_MRA_REP_RCVD:
2256 ret = cm_alloc_msg(cm_id_priv, &msg);
2257 if (!ret)
2258 cm_format_rej((struct cm_rej_msg *) msg->mad,
2259 cm_id_priv, reason, ari, ari_length,
2260 private_data, private_data_len);
2262 cm_enter_timewait(cm_id_priv);
2263 break;
2264 default:
2265 ret = -EINVAL;
2266 goto out;
2269 if (ret)
2270 goto out;
2272 ret = ib_post_send_mad(msg, NULL);
2273 if (ret)
2274 cm_free_msg(msg);
2276 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2277 return ret;
2279 EXPORT_SYMBOL(ib_send_cm_rej);
2281 static void cm_format_rej_event(struct cm_work *work)
2283 struct cm_rej_msg *rej_msg;
2284 struct ib_cm_rej_event_param *param;
2286 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2287 param = &work->cm_event.param.rej_rcvd;
2288 param->ari = rej_msg->ari;
2289 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2290 param->reason = __be16_to_cpu(rej_msg->reason);
2291 work->cm_event.private_data = &rej_msg->private_data;
2294 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2296 struct cm_timewait_info *timewait_info;
2297 struct cm_id_private *cm_id_priv;
2298 __be32 remote_id;
2300 remote_id = rej_msg->local_comm_id;
2302 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2303 spin_lock_irq(&cm.lock);
2304 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2305 remote_id);
2306 if (!timewait_info) {
2307 spin_unlock_irq(&cm.lock);
2308 return NULL;
2310 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2311 (timewait_info->work.local_id ^
2312 cm.random_id_operand));
2313 if (cm_id_priv) {
2314 if (cm_id_priv->id.remote_id == remote_id)
2315 atomic_inc(&cm_id_priv->refcount);
2316 else
2317 cm_id_priv = NULL;
2319 spin_unlock_irq(&cm.lock);
2320 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2321 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2322 else
2323 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2325 return cm_id_priv;
2328 static int cm_rej_handler(struct cm_work *work)
2330 struct cm_id_private *cm_id_priv;
2331 struct cm_rej_msg *rej_msg;
2332 int ret;
2334 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2335 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2336 if (!cm_id_priv)
2337 return -EINVAL;
2339 cm_format_rej_event(work);
2341 spin_lock_irq(&cm_id_priv->lock);
2342 switch (cm_id_priv->id.state) {
2343 case IB_CM_REQ_SENT:
2344 case IB_CM_MRA_REQ_RCVD:
2345 case IB_CM_REP_SENT:
2346 case IB_CM_MRA_REP_RCVD:
2347 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2348 /* fall through */
2349 case IB_CM_REQ_RCVD:
2350 case IB_CM_MRA_REQ_SENT:
2351 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2352 cm_enter_timewait(cm_id_priv);
2353 else
2354 cm_reset_to_idle(cm_id_priv);
2355 break;
2356 case IB_CM_DREQ_SENT:
2357 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2358 /* fall through */
2359 case IB_CM_REP_RCVD:
2360 case IB_CM_MRA_REP_SENT:
2361 cm_enter_timewait(cm_id_priv);
2362 break;
2363 case IB_CM_ESTABLISHED:
2364 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2365 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2366 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2367 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2368 cm_id_priv->msg);
2369 cm_enter_timewait(cm_id_priv);
2370 break;
2372 /* fall through */
2373 default:
2374 spin_unlock_irq(&cm_id_priv->lock);
2375 ret = -EINVAL;
2376 goto out;
2379 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2380 if (!ret)
2381 list_add_tail(&work->list, &cm_id_priv->work_list);
2382 spin_unlock_irq(&cm_id_priv->lock);
2384 if (ret)
2385 cm_process_work(cm_id_priv, work);
2386 else
2387 cm_deref_id(cm_id_priv);
2388 return 0;
2389 out:
2390 cm_deref_id(cm_id_priv);
2391 return -EINVAL;
2394 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2395 u8 service_timeout,
2396 const void *private_data,
2397 u8 private_data_len)
2399 struct cm_id_private *cm_id_priv;
2400 struct ib_mad_send_buf *msg;
2401 enum ib_cm_state cm_state;
2402 enum ib_cm_lap_state lap_state;
2403 enum cm_msg_response msg_response;
2404 void *data;
2405 unsigned long flags;
2406 int ret;
2408 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2409 return -EINVAL;
2411 data = cm_copy_private_data(private_data, private_data_len);
2412 if (IS_ERR(data))
2413 return PTR_ERR(data);
2415 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2417 spin_lock_irqsave(&cm_id_priv->lock, flags);
2418 switch(cm_id_priv->id.state) {
2419 case IB_CM_REQ_RCVD:
2420 cm_state = IB_CM_MRA_REQ_SENT;
2421 lap_state = cm_id->lap_state;
2422 msg_response = CM_MSG_RESPONSE_REQ;
2423 break;
2424 case IB_CM_REP_RCVD:
2425 cm_state = IB_CM_MRA_REP_SENT;
2426 lap_state = cm_id->lap_state;
2427 msg_response = CM_MSG_RESPONSE_REP;
2428 break;
2429 case IB_CM_ESTABLISHED:
2430 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2431 cm_state = cm_id->state;
2432 lap_state = IB_CM_MRA_LAP_SENT;
2433 msg_response = CM_MSG_RESPONSE_OTHER;
2434 break;
2436 default:
2437 ret = -EINVAL;
2438 goto error1;
2441 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2442 ret = cm_alloc_msg(cm_id_priv, &msg);
2443 if (ret)
2444 goto error1;
2446 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2447 msg_response, service_timeout,
2448 private_data, private_data_len);
2449 ret = ib_post_send_mad(msg, NULL);
2450 if (ret)
2451 goto error2;
2454 cm_id->state = cm_state;
2455 cm_id->lap_state = lap_state;
2456 cm_id_priv->service_timeout = service_timeout;
2457 cm_set_private_data(cm_id_priv, data, private_data_len);
2458 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2459 return 0;
2461 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2462 kfree(data);
2463 return ret;
2465 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2466 kfree(data);
2467 cm_free_msg(msg);
2468 return ret;
2470 EXPORT_SYMBOL(ib_send_cm_mra);
2472 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2474 switch (cm_mra_get_msg_mraed(mra_msg)) {
2475 case CM_MSG_RESPONSE_REQ:
2476 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2477 case CM_MSG_RESPONSE_REP:
2478 case CM_MSG_RESPONSE_OTHER:
2479 return cm_acquire_id(mra_msg->remote_comm_id,
2480 mra_msg->local_comm_id);
2481 default:
2482 return NULL;
2486 static int cm_mra_handler(struct cm_work *work)
2488 struct cm_id_private *cm_id_priv;
2489 struct cm_mra_msg *mra_msg;
2490 int timeout, ret;
2492 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2493 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2494 if (!cm_id_priv)
2495 return -EINVAL;
2497 work->cm_event.private_data = &mra_msg->private_data;
2498 work->cm_event.param.mra_rcvd.service_timeout =
2499 cm_mra_get_service_timeout(mra_msg);
2500 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2501 cm_convert_to_ms(cm_id_priv->av.timeout);
2503 spin_lock_irq(&cm_id_priv->lock);
2504 switch (cm_id_priv->id.state) {
2505 case IB_CM_REQ_SENT:
2506 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2507 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2508 cm_id_priv->msg, timeout))
2509 goto out;
2510 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2511 break;
2512 case IB_CM_REP_SENT:
2513 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2514 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2515 cm_id_priv->msg, timeout))
2516 goto out;
2517 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2518 break;
2519 case IB_CM_ESTABLISHED:
2520 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2521 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2522 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2523 cm_id_priv->msg, timeout)) {
2524 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2525 atomic_long_inc(&work->port->
2526 counter_group[CM_RECV_DUPLICATES].
2527 counter[CM_MRA_COUNTER]);
2528 goto out;
2530 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2531 break;
2532 case IB_CM_MRA_REQ_RCVD:
2533 case IB_CM_MRA_REP_RCVD:
2534 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2535 counter[CM_MRA_COUNTER]);
2536 /* fall through */
2537 default:
2538 goto out;
2541 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2542 cm_id_priv->id.state;
2543 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2544 if (!ret)
2545 list_add_tail(&work->list, &cm_id_priv->work_list);
2546 spin_unlock_irq(&cm_id_priv->lock);
2548 if (ret)
2549 cm_process_work(cm_id_priv, work);
2550 else
2551 cm_deref_id(cm_id_priv);
2552 return 0;
2553 out:
2554 spin_unlock_irq(&cm_id_priv->lock);
2555 cm_deref_id(cm_id_priv);
2556 return -EINVAL;
2559 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2560 struct cm_id_private *cm_id_priv,
2561 struct ib_sa_path_rec *alternate_path,
2562 const void *private_data,
2563 u8 private_data_len)
2565 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2566 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2567 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2568 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2569 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2570 /* todo: need remote CM response timeout */
2571 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2572 lap_msg->alt_local_lid = alternate_path->slid;
2573 lap_msg->alt_remote_lid = alternate_path->dlid;
2574 lap_msg->alt_local_gid = alternate_path->sgid;
2575 lap_msg->alt_remote_gid = alternate_path->dgid;
2576 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2577 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2578 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2579 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2580 cm_lap_set_sl(lap_msg, alternate_path->sl);
2581 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2582 cm_lap_set_local_ack_timeout(lap_msg,
2583 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2584 alternate_path->packet_life_time));
2586 if (private_data && private_data_len)
2587 memcpy(lap_msg->private_data, private_data, private_data_len);
2590 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2591 struct ib_sa_path_rec *alternate_path,
2592 const void *private_data,
2593 u8 private_data_len)
2595 struct cm_id_private *cm_id_priv;
2596 struct ib_mad_send_buf *msg;
2597 unsigned long flags;
2598 int ret;
2600 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2601 return -EINVAL;
2603 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2604 spin_lock_irqsave(&cm_id_priv->lock, flags);
2605 if (cm_id->state != IB_CM_ESTABLISHED ||
2606 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2607 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2608 ret = -EINVAL;
2609 goto out;
2612 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2613 if (ret)
2614 goto out;
2615 cm_id_priv->alt_av.timeout =
2616 cm_ack_timeout(cm_id_priv->target_ack_delay,
2617 cm_id_priv->alt_av.timeout - 1);
2619 ret = cm_alloc_msg(cm_id_priv, &msg);
2620 if (ret)
2621 goto out;
2623 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2624 alternate_path, private_data, private_data_len);
2625 msg->timeout_ms = cm_id_priv->timeout_ms;
2626 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2628 ret = ib_post_send_mad(msg, NULL);
2629 if (ret) {
2630 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2631 cm_free_msg(msg);
2632 return ret;
2635 cm_id->lap_state = IB_CM_LAP_SENT;
2636 cm_id_priv->msg = msg;
2638 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2639 return ret;
2641 EXPORT_SYMBOL(ib_send_cm_lap);
2643 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2644 struct ib_sa_path_rec *path,
2645 struct cm_lap_msg *lap_msg)
2647 memset(path, 0, sizeof *path);
2648 path->dgid = lap_msg->alt_local_gid;
2649 path->sgid = lap_msg->alt_remote_gid;
2650 path->dlid = lap_msg->alt_local_lid;
2651 path->slid = lap_msg->alt_remote_lid;
2652 path->flow_label = cm_lap_get_flow_label(lap_msg);
2653 path->hop_limit = lap_msg->alt_hop_limit;
2654 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2655 path->reversible = 1;
2656 path->pkey = cm_id_priv->pkey;
2657 path->sl = cm_lap_get_sl(lap_msg);
2658 path->mtu_selector = IB_SA_EQ;
2659 path->mtu = cm_id_priv->path_mtu;
2660 path->rate_selector = IB_SA_EQ;
2661 path->rate = cm_lap_get_packet_rate(lap_msg);
2662 path->packet_life_time_selector = IB_SA_EQ;
2663 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2664 path->packet_life_time -= (path->packet_life_time > 0);
2667 static int cm_lap_handler(struct cm_work *work)
2669 struct cm_id_private *cm_id_priv;
2670 struct cm_lap_msg *lap_msg;
2671 struct ib_cm_lap_event_param *param;
2672 struct ib_mad_send_buf *msg = NULL;
2673 int ret;
2675 /* todo: verify LAP request and send reject APR if invalid. */
2676 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2677 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2678 lap_msg->local_comm_id);
2679 if (!cm_id_priv)
2680 return -EINVAL;
2682 param = &work->cm_event.param.lap_rcvd;
2683 param->alternate_path = &work->path[0];
2684 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2685 work->cm_event.private_data = &lap_msg->private_data;
2687 spin_lock_irq(&cm_id_priv->lock);
2688 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2689 goto unlock;
2691 switch (cm_id_priv->id.lap_state) {
2692 case IB_CM_LAP_UNINIT:
2693 case IB_CM_LAP_IDLE:
2694 break;
2695 case IB_CM_MRA_LAP_SENT:
2696 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2697 counter[CM_LAP_COUNTER]);
2698 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2699 goto unlock;
2701 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2702 CM_MSG_RESPONSE_OTHER,
2703 cm_id_priv->service_timeout,
2704 cm_id_priv->private_data,
2705 cm_id_priv->private_data_len);
2706 spin_unlock_irq(&cm_id_priv->lock);
2708 if (ib_post_send_mad(msg, NULL))
2709 cm_free_msg(msg);
2710 goto deref;
2711 case IB_CM_LAP_RCVD:
2712 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2713 counter[CM_LAP_COUNTER]);
2714 goto unlock;
2715 default:
2716 goto unlock;
2719 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2720 cm_id_priv->tid = lap_msg->hdr.tid;
2721 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2722 work->mad_recv_wc->recv_buf.grh,
2723 &cm_id_priv->av);
2724 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2725 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2726 if (!ret)
2727 list_add_tail(&work->list, &cm_id_priv->work_list);
2728 spin_unlock_irq(&cm_id_priv->lock);
2730 if (ret)
2731 cm_process_work(cm_id_priv, work);
2732 else
2733 cm_deref_id(cm_id_priv);
2734 return 0;
2736 unlock: spin_unlock_irq(&cm_id_priv->lock);
2737 deref: cm_deref_id(cm_id_priv);
2738 return -EINVAL;
2741 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2742 struct cm_id_private *cm_id_priv,
2743 enum ib_cm_apr_status status,
2744 void *info,
2745 u8 info_length,
2746 const void *private_data,
2747 u8 private_data_len)
2749 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2750 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2751 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2752 apr_msg->ap_status = (u8) status;
2754 if (info && info_length) {
2755 apr_msg->info_length = info_length;
2756 memcpy(apr_msg->info, info, info_length);
2759 if (private_data && private_data_len)
2760 memcpy(apr_msg->private_data, private_data, private_data_len);
2763 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2764 enum ib_cm_apr_status status,
2765 void *info,
2766 u8 info_length,
2767 const void *private_data,
2768 u8 private_data_len)
2770 struct cm_id_private *cm_id_priv;
2771 struct ib_mad_send_buf *msg;
2772 unsigned long flags;
2773 int ret;
2775 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2776 (info && info_length > IB_CM_APR_INFO_LENGTH))
2777 return -EINVAL;
2779 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2780 spin_lock_irqsave(&cm_id_priv->lock, flags);
2781 if (cm_id->state != IB_CM_ESTABLISHED ||
2782 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2783 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2784 ret = -EINVAL;
2785 goto out;
2788 ret = cm_alloc_msg(cm_id_priv, &msg);
2789 if (ret)
2790 goto out;
2792 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2793 info, info_length, private_data, private_data_len);
2794 ret = ib_post_send_mad(msg, NULL);
2795 if (ret) {
2796 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2797 cm_free_msg(msg);
2798 return ret;
2801 cm_id->lap_state = IB_CM_LAP_IDLE;
2802 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2803 return ret;
2805 EXPORT_SYMBOL(ib_send_cm_apr);
2807 static int cm_apr_handler(struct cm_work *work)
2809 struct cm_id_private *cm_id_priv;
2810 struct cm_apr_msg *apr_msg;
2811 int ret;
2813 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2814 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2815 apr_msg->local_comm_id);
2816 if (!cm_id_priv)
2817 return -EINVAL; /* Unmatched reply. */
2819 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2820 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2821 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2822 work->cm_event.private_data = &apr_msg->private_data;
2824 spin_lock_irq(&cm_id_priv->lock);
2825 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2826 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2827 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2828 spin_unlock_irq(&cm_id_priv->lock);
2829 goto out;
2831 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2832 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2833 cm_id_priv->msg = NULL;
2835 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2836 if (!ret)
2837 list_add_tail(&work->list, &cm_id_priv->work_list);
2838 spin_unlock_irq(&cm_id_priv->lock);
2840 if (ret)
2841 cm_process_work(cm_id_priv, work);
2842 else
2843 cm_deref_id(cm_id_priv);
2844 return 0;
2845 out:
2846 cm_deref_id(cm_id_priv);
2847 return -EINVAL;
2850 static int cm_timewait_handler(struct cm_work *work)
2852 struct cm_timewait_info *timewait_info;
2853 struct cm_id_private *cm_id_priv;
2854 int ret;
2856 timewait_info = (struct cm_timewait_info *)work;
2857 spin_lock_irq(&cm.lock);
2858 list_del(&timewait_info->list);
2859 spin_unlock_irq(&cm.lock);
2861 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2862 timewait_info->work.remote_id);
2863 if (!cm_id_priv)
2864 return -EINVAL;
2866 spin_lock_irq(&cm_id_priv->lock);
2867 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2868 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2869 spin_unlock_irq(&cm_id_priv->lock);
2870 goto out;
2872 cm_id_priv->id.state = IB_CM_IDLE;
2873 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2874 if (!ret)
2875 list_add_tail(&work->list, &cm_id_priv->work_list);
2876 spin_unlock_irq(&cm_id_priv->lock);
2878 if (ret)
2879 cm_process_work(cm_id_priv, work);
2880 else
2881 cm_deref_id(cm_id_priv);
2882 return 0;
2883 out:
2884 cm_deref_id(cm_id_priv);
2885 return -EINVAL;
2888 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2889 struct cm_id_private *cm_id_priv,
2890 struct ib_cm_sidr_req_param *param)
2892 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2893 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2894 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2895 sidr_req_msg->pkey = param->path->pkey;
2896 sidr_req_msg->service_id = param->service_id;
2898 if (param->private_data && param->private_data_len)
2899 memcpy(sidr_req_msg->private_data, param->private_data,
2900 param->private_data_len);
2903 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2904 struct ib_cm_sidr_req_param *param)
2906 struct cm_id_private *cm_id_priv;
2907 struct ib_mad_send_buf *msg;
2908 unsigned long flags;
2909 int ret;
2911 if (!param->path || (param->private_data &&
2912 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2913 return -EINVAL;
2915 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2916 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2917 if (ret)
2918 goto out;
2920 cm_id->service_id = param->service_id;
2921 cm_id->service_mask = ~cpu_to_be64(0);
2922 cm_id_priv->timeout_ms = param->timeout_ms;
2923 cm_id_priv->max_cm_retries = param->max_cm_retries;
2924 ret = cm_alloc_msg(cm_id_priv, &msg);
2925 if (ret)
2926 goto out;
2928 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2929 param);
2930 msg->timeout_ms = cm_id_priv->timeout_ms;
2931 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2933 spin_lock_irqsave(&cm_id_priv->lock, flags);
2934 if (cm_id->state == IB_CM_IDLE)
2935 ret = ib_post_send_mad(msg, NULL);
2936 else
2937 ret = -EINVAL;
2939 if (ret) {
2940 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2941 cm_free_msg(msg);
2942 goto out;
2944 cm_id->state = IB_CM_SIDR_REQ_SENT;
2945 cm_id_priv->msg = msg;
2946 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2947 out:
2948 return ret;
2950 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2952 static void cm_format_sidr_req_event(struct cm_work *work,
2953 struct ib_cm_id *listen_id)
2955 struct cm_sidr_req_msg *sidr_req_msg;
2956 struct ib_cm_sidr_req_event_param *param;
2958 sidr_req_msg = (struct cm_sidr_req_msg *)
2959 work->mad_recv_wc->recv_buf.mad;
2960 param = &work->cm_event.param.sidr_req_rcvd;
2961 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2962 param->listen_id = listen_id;
2963 param->port = work->port->port_num;
2964 work->cm_event.private_data = &sidr_req_msg->private_data;
2967 static int cm_sidr_req_handler(struct cm_work *work)
2969 struct ib_cm_id *cm_id;
2970 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2971 struct cm_sidr_req_msg *sidr_req_msg;
2972 struct ib_wc *wc;
2974 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
2975 if (IS_ERR(cm_id))
2976 return PTR_ERR(cm_id);
2977 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2979 /* Record SGID/SLID and request ID for lookup. */
2980 sidr_req_msg = (struct cm_sidr_req_msg *)
2981 work->mad_recv_wc->recv_buf.mad;
2982 wc = work->mad_recv_wc->wc;
2983 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2984 cm_id_priv->av.dgid.global.interface_id = 0;
2985 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2986 work->mad_recv_wc->recv_buf.grh,
2987 &cm_id_priv->av);
2988 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2989 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2990 atomic_inc(&cm_id_priv->work_count);
2992 spin_lock_irq(&cm.lock);
2993 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2994 if (cur_cm_id_priv) {
2995 spin_unlock_irq(&cm.lock);
2996 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2997 counter[CM_SIDR_REQ_COUNTER]);
2998 goto out; /* Duplicate message. */
3000 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3001 cur_cm_id_priv = cm_find_listen(cm_id->device,
3002 sidr_req_msg->service_id,
3003 sidr_req_msg->private_data);
3004 if (!cur_cm_id_priv) {
3005 spin_unlock_irq(&cm.lock);
3006 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3007 goto out; /* No match. */
3009 atomic_inc(&cur_cm_id_priv->refcount);
3010 atomic_inc(&cm_id_priv->refcount);
3011 spin_unlock_irq(&cm.lock);
3013 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3014 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3015 cm_id_priv->id.service_id = sidr_req_msg->service_id;
3016 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3018 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3019 cm_process_work(cm_id_priv, work);
3020 cm_deref_id(cur_cm_id_priv);
3021 return 0;
3022 out:
3023 ib_destroy_cm_id(&cm_id_priv->id);
3024 return -EINVAL;
3027 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3028 struct cm_id_private *cm_id_priv,
3029 struct ib_cm_sidr_rep_param *param)
3031 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3032 cm_id_priv->tid);
3033 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3034 sidr_rep_msg->status = param->status;
3035 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3036 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3037 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3039 if (param->info && param->info_length)
3040 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3042 if (param->private_data && param->private_data_len)
3043 memcpy(sidr_rep_msg->private_data, param->private_data,
3044 param->private_data_len);
3047 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3048 struct ib_cm_sidr_rep_param *param)
3050 struct cm_id_private *cm_id_priv;
3051 struct ib_mad_send_buf *msg;
3052 unsigned long flags;
3053 int ret;
3055 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3056 (param->private_data &&
3057 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3058 return -EINVAL;
3060 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3061 spin_lock_irqsave(&cm_id_priv->lock, flags);
3062 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3063 ret = -EINVAL;
3064 goto error;
3067 ret = cm_alloc_msg(cm_id_priv, &msg);
3068 if (ret)
3069 goto error;
3071 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3072 param);
3073 ret = ib_post_send_mad(msg, NULL);
3074 if (ret) {
3075 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3076 cm_free_msg(msg);
3077 return ret;
3079 cm_id->state = IB_CM_IDLE;
3080 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3082 spin_lock_irqsave(&cm.lock, flags);
3083 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3084 spin_unlock_irqrestore(&cm.lock, flags);
3085 return 0;
3087 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3088 return ret;
3090 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3092 static void cm_format_sidr_rep_event(struct cm_work *work)
3094 struct cm_sidr_rep_msg *sidr_rep_msg;
3095 struct ib_cm_sidr_rep_event_param *param;
3097 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3098 work->mad_recv_wc->recv_buf.mad;
3099 param = &work->cm_event.param.sidr_rep_rcvd;
3100 param->status = sidr_rep_msg->status;
3101 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3102 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3103 param->info = &sidr_rep_msg->info;
3104 param->info_len = sidr_rep_msg->info_length;
3105 work->cm_event.private_data = &sidr_rep_msg->private_data;
3108 static int cm_sidr_rep_handler(struct cm_work *work)
3110 struct cm_sidr_rep_msg *sidr_rep_msg;
3111 struct cm_id_private *cm_id_priv;
3113 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3114 work->mad_recv_wc->recv_buf.mad;
3115 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3116 if (!cm_id_priv)
3117 return -EINVAL; /* Unmatched reply. */
3119 spin_lock_irq(&cm_id_priv->lock);
3120 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3121 spin_unlock_irq(&cm_id_priv->lock);
3122 goto out;
3124 cm_id_priv->id.state = IB_CM_IDLE;
3125 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3126 spin_unlock_irq(&cm_id_priv->lock);
3128 cm_format_sidr_rep_event(work);
3129 cm_process_work(cm_id_priv, work);
3130 return 0;
3131 out:
3132 cm_deref_id(cm_id_priv);
3133 return -EINVAL;
3136 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3137 enum ib_wc_status wc_status)
3139 struct cm_id_private *cm_id_priv;
3140 struct ib_cm_event cm_event;
3141 enum ib_cm_state state;
3142 int ret;
3144 memset(&cm_event, 0, sizeof cm_event);
3145 cm_id_priv = msg->context[0];
3147 /* Discard old sends or ones without a response. */
3148 spin_lock_irq(&cm_id_priv->lock);
3149 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3150 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3151 goto discard;
3153 switch (state) {
3154 case IB_CM_REQ_SENT:
3155 case IB_CM_MRA_REQ_RCVD:
3156 cm_reset_to_idle(cm_id_priv);
3157 cm_event.event = IB_CM_REQ_ERROR;
3158 break;
3159 case IB_CM_REP_SENT:
3160 case IB_CM_MRA_REP_RCVD:
3161 cm_reset_to_idle(cm_id_priv);
3162 cm_event.event = IB_CM_REP_ERROR;
3163 break;
3164 case IB_CM_DREQ_SENT:
3165 cm_enter_timewait(cm_id_priv);
3166 cm_event.event = IB_CM_DREQ_ERROR;
3167 break;
3168 case IB_CM_SIDR_REQ_SENT:
3169 cm_id_priv->id.state = IB_CM_IDLE;
3170 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3171 break;
3172 default:
3173 goto discard;
3175 spin_unlock_irq(&cm_id_priv->lock);
3176 cm_event.param.send_status = wc_status;
3178 /* No other events can occur on the cm_id at this point. */
3179 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3180 cm_free_msg(msg);
3181 if (ret)
3182 ib_destroy_cm_id(&cm_id_priv->id);
3183 return;
3184 discard:
3185 spin_unlock_irq(&cm_id_priv->lock);
3186 cm_free_msg(msg);
3189 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3190 struct ib_mad_send_wc *mad_send_wc)
3192 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3193 struct cm_port *port;
3194 u16 attr_index;
3196 port = mad_agent->context;
3197 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3198 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3201 * If the send was in response to a received message (context[0] is not
3202 * set to a cm_id), and is not a REJ, then it is a send that was
3203 * manually retried.
3205 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3206 msg->retries = 1;
3208 atomic_long_add(1 + msg->retries,
3209 &port->counter_group[CM_XMIT].counter[attr_index]);
3210 if (msg->retries)
3211 atomic_long_add(msg->retries,
3212 &port->counter_group[CM_XMIT_RETRIES].
3213 counter[attr_index]);
3215 switch (mad_send_wc->status) {
3216 case IB_WC_SUCCESS:
3217 case IB_WC_WR_FLUSH_ERR:
3218 cm_free_msg(msg);
3219 break;
3220 default:
3221 if (msg->context[0] && msg->context[1])
3222 cm_process_send_error(msg, mad_send_wc->status);
3223 else
3224 cm_free_msg(msg);
3225 break;
3229 static void cm_work_handler(struct work_struct *_work)
3231 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3232 int ret;
3234 switch (work->cm_event.event) {
3235 case IB_CM_REQ_RECEIVED:
3236 ret = cm_req_handler(work);
3237 break;
3238 case IB_CM_MRA_RECEIVED:
3239 ret = cm_mra_handler(work);
3240 break;
3241 case IB_CM_REJ_RECEIVED:
3242 ret = cm_rej_handler(work);
3243 break;
3244 case IB_CM_REP_RECEIVED:
3245 ret = cm_rep_handler(work);
3246 break;
3247 case IB_CM_RTU_RECEIVED:
3248 ret = cm_rtu_handler(work);
3249 break;
3250 case IB_CM_USER_ESTABLISHED:
3251 ret = cm_establish_handler(work);
3252 break;
3253 case IB_CM_DREQ_RECEIVED:
3254 ret = cm_dreq_handler(work);
3255 break;
3256 case IB_CM_DREP_RECEIVED:
3257 ret = cm_drep_handler(work);
3258 break;
3259 case IB_CM_SIDR_REQ_RECEIVED:
3260 ret = cm_sidr_req_handler(work);
3261 break;
3262 case IB_CM_SIDR_REP_RECEIVED:
3263 ret = cm_sidr_rep_handler(work);
3264 break;
3265 case IB_CM_LAP_RECEIVED:
3266 ret = cm_lap_handler(work);
3267 break;
3268 case IB_CM_APR_RECEIVED:
3269 ret = cm_apr_handler(work);
3270 break;
3271 case IB_CM_TIMEWAIT_EXIT:
3272 ret = cm_timewait_handler(work);
3273 break;
3274 default:
3275 ret = -EINVAL;
3276 break;
3278 if (ret)
3279 cm_free_work(work);
3282 static int cm_establish(struct ib_cm_id *cm_id)
3284 struct cm_id_private *cm_id_priv;
3285 struct cm_work *work;
3286 unsigned long flags;
3287 int ret = 0;
3289 work = kmalloc(sizeof *work, GFP_ATOMIC);
3290 if (!work)
3291 return -ENOMEM;
3293 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3294 spin_lock_irqsave(&cm_id_priv->lock, flags);
3295 switch (cm_id->state)
3297 case IB_CM_REP_SENT:
3298 case IB_CM_MRA_REP_RCVD:
3299 cm_id->state = IB_CM_ESTABLISHED;
3300 break;
3301 case IB_CM_ESTABLISHED:
3302 ret = -EISCONN;
3303 break;
3304 default:
3305 ret = -EINVAL;
3306 break;
3308 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3310 if (ret) {
3311 kfree(work);
3312 goto out;
3316 * The CM worker thread may try to destroy the cm_id before it
3317 * can execute this work item. To prevent potential deadlock,
3318 * we need to find the cm_id once we're in the context of the
3319 * worker thread, rather than holding a reference on it.
3321 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3322 work->local_id = cm_id->local_id;
3323 work->remote_id = cm_id->remote_id;
3324 work->mad_recv_wc = NULL;
3325 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3326 queue_delayed_work(cm.wq, &work->work, 0);
3327 out:
3328 return ret;
3331 static int cm_migrate(struct ib_cm_id *cm_id)
3333 struct cm_id_private *cm_id_priv;
3334 unsigned long flags;
3335 int ret = 0;
3337 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3338 spin_lock_irqsave(&cm_id_priv->lock, flags);
3339 if (cm_id->state == IB_CM_ESTABLISHED &&
3340 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3341 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3342 cm_id->lap_state = IB_CM_LAP_IDLE;
3343 cm_id_priv->av = cm_id_priv->alt_av;
3344 } else
3345 ret = -EINVAL;
3346 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3348 return ret;
3351 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3353 int ret;
3355 switch (event) {
3356 case IB_EVENT_COMM_EST:
3357 ret = cm_establish(cm_id);
3358 break;
3359 case IB_EVENT_PATH_MIG:
3360 ret = cm_migrate(cm_id);
3361 break;
3362 default:
3363 ret = -EINVAL;
3365 return ret;
3367 EXPORT_SYMBOL(ib_cm_notify);
3369 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3370 struct ib_mad_recv_wc *mad_recv_wc)
3372 struct cm_port *port = mad_agent->context;
3373 struct cm_work *work;
3374 enum ib_cm_event_type event;
3375 u16 attr_id;
3376 int paths = 0;
3378 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3379 case CM_REQ_ATTR_ID:
3380 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3381 alt_local_lid != 0);
3382 event = IB_CM_REQ_RECEIVED;
3383 break;
3384 case CM_MRA_ATTR_ID:
3385 event = IB_CM_MRA_RECEIVED;
3386 break;
3387 case CM_REJ_ATTR_ID:
3388 event = IB_CM_REJ_RECEIVED;
3389 break;
3390 case CM_REP_ATTR_ID:
3391 event = IB_CM_REP_RECEIVED;
3392 break;
3393 case CM_RTU_ATTR_ID:
3394 event = IB_CM_RTU_RECEIVED;
3395 break;
3396 case CM_DREQ_ATTR_ID:
3397 event = IB_CM_DREQ_RECEIVED;
3398 break;
3399 case CM_DREP_ATTR_ID:
3400 event = IB_CM_DREP_RECEIVED;
3401 break;
3402 case CM_SIDR_REQ_ATTR_ID:
3403 event = IB_CM_SIDR_REQ_RECEIVED;
3404 break;
3405 case CM_SIDR_REP_ATTR_ID:
3406 event = IB_CM_SIDR_REP_RECEIVED;
3407 break;
3408 case CM_LAP_ATTR_ID:
3409 paths = 1;
3410 event = IB_CM_LAP_RECEIVED;
3411 break;
3412 case CM_APR_ATTR_ID:
3413 event = IB_CM_APR_RECEIVED;
3414 break;
3415 default:
3416 ib_free_recv_mad(mad_recv_wc);
3417 return;
3420 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3421 atomic_long_inc(&port->counter_group[CM_RECV].
3422 counter[attr_id - CM_ATTR_ID_OFFSET]);
3424 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3425 GFP_KERNEL);
3426 if (!work) {
3427 ib_free_recv_mad(mad_recv_wc);
3428 return;
3431 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3432 work->cm_event.event = event;
3433 work->mad_recv_wc = mad_recv_wc;
3434 work->port = port;
3435 queue_delayed_work(cm.wq, &work->work, 0);
3438 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3439 struct ib_qp_attr *qp_attr,
3440 int *qp_attr_mask)
3442 unsigned long flags;
3443 int ret;
3445 spin_lock_irqsave(&cm_id_priv->lock, flags);
3446 switch (cm_id_priv->id.state) {
3447 case IB_CM_REQ_SENT:
3448 case IB_CM_MRA_REQ_RCVD:
3449 case IB_CM_REQ_RCVD:
3450 case IB_CM_MRA_REQ_SENT:
3451 case IB_CM_REP_RCVD:
3452 case IB_CM_MRA_REP_SENT:
3453 case IB_CM_REP_SENT:
3454 case IB_CM_MRA_REP_RCVD:
3455 case IB_CM_ESTABLISHED:
3456 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3457 IB_QP_PKEY_INDEX | IB_QP_PORT;
3458 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3459 if (cm_id_priv->responder_resources)
3460 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3461 IB_ACCESS_REMOTE_ATOMIC;
3462 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3463 qp_attr->port_num = cm_id_priv->av.port->port_num;
3464 ret = 0;
3465 break;
3466 default:
3467 ret = -EINVAL;
3468 break;
3470 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3471 return ret;
3474 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3475 struct ib_qp_attr *qp_attr,
3476 int *qp_attr_mask)
3478 unsigned long flags;
3479 int ret;
3481 spin_lock_irqsave(&cm_id_priv->lock, flags);
3482 switch (cm_id_priv->id.state) {
3483 case IB_CM_REQ_RCVD:
3484 case IB_CM_MRA_REQ_SENT:
3485 case IB_CM_REP_RCVD:
3486 case IB_CM_MRA_REP_SENT:
3487 case IB_CM_REP_SENT:
3488 case IB_CM_MRA_REP_RCVD:
3489 case IB_CM_ESTABLISHED:
3490 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3491 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3492 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3493 qp_attr->path_mtu = cm_id_priv->path_mtu;
3494 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3495 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3496 if (cm_id_priv->qp_type == IB_QPT_RC) {
3497 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3498 IB_QP_MIN_RNR_TIMER;
3499 qp_attr->max_dest_rd_atomic =
3500 cm_id_priv->responder_resources;
3501 qp_attr->min_rnr_timer = 0;
3503 if (cm_id_priv->alt_av.ah_attr.dlid) {
3504 *qp_attr_mask |= IB_QP_ALT_PATH;
3505 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3506 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3507 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3508 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3510 ret = 0;
3511 break;
3512 default:
3513 ret = -EINVAL;
3514 break;
3516 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3517 return ret;
3520 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3521 struct ib_qp_attr *qp_attr,
3522 int *qp_attr_mask)
3524 unsigned long flags;
3525 int ret;
3527 spin_lock_irqsave(&cm_id_priv->lock, flags);
3528 switch (cm_id_priv->id.state) {
3529 /* Allow transition to RTS before sending REP */
3530 case IB_CM_REQ_RCVD:
3531 case IB_CM_MRA_REQ_SENT:
3533 case IB_CM_REP_RCVD:
3534 case IB_CM_MRA_REP_SENT:
3535 case IB_CM_REP_SENT:
3536 case IB_CM_MRA_REP_RCVD:
3537 case IB_CM_ESTABLISHED:
3538 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3539 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3540 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3541 if (cm_id_priv->qp_type == IB_QPT_RC) {
3542 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3543 IB_QP_RNR_RETRY |
3544 IB_QP_MAX_QP_RD_ATOMIC;
3545 qp_attr->timeout = cm_id_priv->av.timeout;
3546 qp_attr->retry_cnt = cm_id_priv->retry_count;
3547 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3548 qp_attr->max_rd_atomic =
3549 cm_id_priv->initiator_depth;
3551 if (cm_id_priv->alt_av.ah_attr.dlid) {
3552 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3553 qp_attr->path_mig_state = IB_MIG_REARM;
3555 } else {
3556 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3557 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3558 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3559 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3560 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3561 qp_attr->path_mig_state = IB_MIG_REARM;
3563 ret = 0;
3564 break;
3565 default:
3566 ret = -EINVAL;
3567 break;
3569 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3570 return ret;
3573 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3574 struct ib_qp_attr *qp_attr,
3575 int *qp_attr_mask)
3577 struct cm_id_private *cm_id_priv;
3578 int ret;
3580 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3581 switch (qp_attr->qp_state) {
3582 case IB_QPS_INIT:
3583 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3584 break;
3585 case IB_QPS_RTR:
3586 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3587 break;
3588 case IB_QPS_RTS:
3589 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3590 break;
3591 default:
3592 ret = -EINVAL;
3593 break;
3595 return ret;
3597 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3599 static void cm_get_ack_delay(struct cm_device *cm_dev)
3601 struct ib_device_attr attr;
3603 if (ib_query_device(cm_dev->ib_device, &attr))
3604 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3605 else
3606 cm_dev->ack_delay = attr.local_ca_ack_delay;
3609 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3610 char *buf)
3612 struct cm_counter_group *group;
3613 struct cm_counter_attribute *cm_attr;
3615 group = container_of(obj, struct cm_counter_group, obj);
3616 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3618 return sprintf(buf, "%ld\n",
3619 atomic_long_read(&group->counter[cm_attr->index]));
3622 static const struct sysfs_ops cm_counter_ops = {
3623 .show = cm_show_counter
3626 static struct kobj_type cm_counter_obj_type = {
3627 .sysfs_ops = &cm_counter_ops,
3628 .default_attrs = cm_counter_default_attrs
3631 static void cm_release_port_obj(struct kobject *obj)
3633 struct cm_port *cm_port;
3635 cm_port = container_of(obj, struct cm_port, port_obj);
3636 kfree(cm_port);
3639 static struct kobj_type cm_port_obj_type = {
3640 .release = cm_release_port_obj
3643 static char *cm_devnode(struct device *dev, mode_t *mode)
3645 if (mode)
3646 *mode = 0666;
3647 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3650 struct class cm_class = {
3651 .owner = THIS_MODULE,
3652 .name = "infiniband_cm",
3653 .devnode = cm_devnode,
3655 EXPORT_SYMBOL(cm_class);
3657 static int cm_create_port_fs(struct cm_port *port)
3659 int i, ret;
3661 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3662 &port->cm_dev->device->kobj,
3663 "%d", port->port_num);
3664 if (ret) {
3665 kfree(port);
3666 return ret;
3669 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3670 ret = kobject_init_and_add(&port->counter_group[i].obj,
3671 &cm_counter_obj_type,
3672 &port->port_obj,
3673 "%s", counter_group_names[i]);
3674 if (ret)
3675 goto error;
3678 return 0;
3680 error:
3681 while (i--)
3682 kobject_put(&port->counter_group[i].obj);
3683 kobject_put(&port->port_obj);
3684 return ret;
3688 static void cm_remove_port_fs(struct cm_port *port)
3690 int i;
3692 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3693 kobject_put(&port->counter_group[i].obj);
3695 kobject_put(&port->port_obj);
3698 static void cm_add_one(struct ib_device *ib_device)
3700 struct cm_device *cm_dev;
3701 struct cm_port *port;
3702 struct ib_mad_reg_req reg_req = {
3703 .mgmt_class = IB_MGMT_CLASS_CM,
3704 .mgmt_class_version = IB_CM_CLASS_VERSION
3706 struct ib_port_modify port_modify = {
3707 .set_port_cap_mask = IB_PORT_CM_SUP
3709 unsigned long flags;
3710 int ret;
3711 u8 i;
3713 if (rdma_node_get_transport(ib_device->node_type) != RDMA_TRANSPORT_IB)
3714 return;
3716 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3717 ib_device->phys_port_cnt, GFP_KERNEL);
3718 if (!cm_dev)
3719 return;
3721 cm_dev->ib_device = ib_device;
3722 cm_get_ack_delay(cm_dev);
3724 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3725 MKDEV(0, 0), NULL,
3726 "%s", ib_device->name);
3727 if (IS_ERR(cm_dev->device)) {
3728 kfree(cm_dev);
3729 return;
3732 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3733 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3734 port = kzalloc(sizeof *port, GFP_KERNEL);
3735 if (!port)
3736 goto error1;
3738 cm_dev->port[i-1] = port;
3739 port->cm_dev = cm_dev;
3740 port->port_num = i;
3742 ret = cm_create_port_fs(port);
3743 if (ret)
3744 goto error1;
3746 port->mad_agent = ib_register_mad_agent(ib_device, i,
3747 IB_QPT_GSI,
3748 &reg_req,
3750 cm_send_handler,
3751 cm_recv_handler,
3752 port);
3753 if (IS_ERR(port->mad_agent))
3754 goto error2;
3756 ret = ib_modify_port(ib_device, i, 0, &port_modify);
3757 if (ret)
3758 goto error3;
3760 ib_set_client_data(ib_device, &cm_client, cm_dev);
3762 write_lock_irqsave(&cm.device_lock, flags);
3763 list_add_tail(&cm_dev->list, &cm.device_list);
3764 write_unlock_irqrestore(&cm.device_lock, flags);
3765 return;
3767 error3:
3768 ib_unregister_mad_agent(port->mad_agent);
3769 error2:
3770 cm_remove_port_fs(port);
3771 error1:
3772 port_modify.set_port_cap_mask = 0;
3773 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3774 while (--i) {
3775 port = cm_dev->port[i-1];
3776 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3777 ib_unregister_mad_agent(port->mad_agent);
3778 cm_remove_port_fs(port);
3780 device_unregister(cm_dev->device);
3781 kfree(cm_dev);
3784 static void cm_remove_one(struct ib_device *ib_device)
3786 struct cm_device *cm_dev;
3787 struct cm_port *port;
3788 struct ib_port_modify port_modify = {
3789 .clr_port_cap_mask = IB_PORT_CM_SUP
3791 unsigned long flags;
3792 int i;
3794 cm_dev = ib_get_client_data(ib_device, &cm_client);
3795 if (!cm_dev)
3796 return;
3798 write_lock_irqsave(&cm.device_lock, flags);
3799 list_del(&cm_dev->list);
3800 write_unlock_irqrestore(&cm.device_lock, flags);
3802 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3803 port = cm_dev->port[i-1];
3804 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3805 ib_unregister_mad_agent(port->mad_agent);
3806 flush_workqueue(cm.wq);
3807 cm_remove_port_fs(port);
3809 device_unregister(cm_dev->device);
3810 kfree(cm_dev);
3813 static int __init ib_cm_init(void)
3815 int ret;
3817 memset(&cm, 0, sizeof cm);
3818 INIT_LIST_HEAD(&cm.device_list);
3819 rwlock_init(&cm.device_lock);
3820 spin_lock_init(&cm.lock);
3821 cm.listen_service_table = RB_ROOT;
3822 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3823 cm.remote_id_table = RB_ROOT;
3824 cm.remote_qp_table = RB_ROOT;
3825 cm.remote_sidr_table = RB_ROOT;
3826 idr_init(&cm.local_id_table);
3827 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3828 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3829 INIT_LIST_HEAD(&cm.timewait_list);
3831 ret = class_register(&cm_class);
3832 if (ret)
3833 return -ENOMEM;
3835 cm.wq = create_workqueue("ib_cm");
3836 if (!cm.wq) {
3837 ret = -ENOMEM;
3838 goto error1;
3841 ret = ib_register_client(&cm_client);
3842 if (ret)
3843 goto error2;
3845 return 0;
3846 error2:
3847 destroy_workqueue(cm.wq);
3848 error1:
3849 class_unregister(&cm_class);
3850 return ret;
3853 static void __exit ib_cm_cleanup(void)
3855 struct cm_timewait_info *timewait_info, *tmp;
3857 spin_lock_irq(&cm.lock);
3858 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3859 cancel_delayed_work(&timewait_info->work.work);
3860 spin_unlock_irq(&cm.lock);
3862 ib_unregister_client(&cm_client);
3863 destroy_workqueue(cm.wq);
3865 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3866 list_del(&timewait_info->list);
3867 kfree(timewait_info);
3870 class_unregister(&cm_class);
3871 idr_destroy(&cm.local_id_table);
3874 module_init(ib_cm_init);
3875 module_exit(ib_cm_cleanup);