ARM: 7989/1: Delete asm/system.h
[linux/fpc-iii.git] / drivers / infiniband / core / cm.c
blob0601b9daf8407ae31c4ccc6c99ee64372a5a896f
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>
50 #include <linux/etherdevice.h>
52 #include <rdma/ib_cache.h>
53 #include <rdma/ib_cm.h>
54 #include "cm_msgs.h"
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("InfiniBand CM");
58 MODULE_LICENSE("Dual BSD/GPL");
60 static void cm_add_one(struct ib_device *device);
61 static void cm_remove_one(struct ib_device *device);
63 static struct ib_client cm_client = {
64 .name = "cm",
65 .add = cm_add_one,
66 .remove = cm_remove_one
69 static struct ib_cm {
70 spinlock_t lock;
71 struct list_head device_list;
72 rwlock_t device_lock;
73 struct rb_root listen_service_table;
74 u64 listen_service_id;
75 /* struct rb_root peer_service_table; todo: fix peer to peer */
76 struct rb_root remote_qp_table;
77 struct rb_root remote_id_table;
78 struct rb_root remote_sidr_table;
79 struct idr local_id_table;
80 __be32 random_id_operand;
81 struct list_head timewait_list;
82 struct workqueue_struct *wq;
83 } cm;
85 /* Counter indexes ordered by attribute ID */
86 enum {
87 CM_REQ_COUNTER,
88 CM_MRA_COUNTER,
89 CM_REJ_COUNTER,
90 CM_REP_COUNTER,
91 CM_RTU_COUNTER,
92 CM_DREQ_COUNTER,
93 CM_DREP_COUNTER,
94 CM_SIDR_REQ_COUNTER,
95 CM_SIDR_REP_COUNTER,
96 CM_LAP_COUNTER,
97 CM_APR_COUNTER,
98 CM_ATTR_COUNT,
99 CM_ATTR_ID_OFFSET = 0x0010,
102 enum {
103 CM_XMIT,
104 CM_XMIT_RETRIES,
105 CM_RECV,
106 CM_RECV_DUPLICATES,
107 CM_COUNTER_GROUPS
110 static char const counter_group_names[CM_COUNTER_GROUPS]
111 [sizeof("cm_rx_duplicates")] = {
112 "cm_tx_msgs", "cm_tx_retries",
113 "cm_rx_msgs", "cm_rx_duplicates"
116 struct cm_counter_group {
117 struct kobject obj;
118 atomic_long_t counter[CM_ATTR_COUNT];
121 struct cm_counter_attribute {
122 struct attribute attr;
123 int index;
126 #define CM_COUNTER_ATTR(_name, _index) \
127 struct cm_counter_attribute cm_##_name##_counter_attr = { \
128 .attr = { .name = __stringify(_name), .mode = 0444 }, \
129 .index = _index \
132 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
133 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
134 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
135 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
136 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
137 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
138 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
139 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
140 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
141 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
142 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
144 static struct attribute *cm_counter_default_attrs[] = {
145 &cm_req_counter_attr.attr,
146 &cm_mra_counter_attr.attr,
147 &cm_rej_counter_attr.attr,
148 &cm_rep_counter_attr.attr,
149 &cm_rtu_counter_attr.attr,
150 &cm_dreq_counter_attr.attr,
151 &cm_drep_counter_attr.attr,
152 &cm_sidr_req_counter_attr.attr,
153 &cm_sidr_rep_counter_attr.attr,
154 &cm_lap_counter_attr.attr,
155 &cm_apr_counter_attr.attr,
156 NULL
159 struct cm_port {
160 struct cm_device *cm_dev;
161 struct ib_mad_agent *mad_agent;
162 struct kobject port_obj;
163 u8 port_num;
164 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
167 struct cm_device {
168 struct list_head list;
169 struct ib_device *ib_device;
170 struct device *device;
171 u8 ack_delay;
172 struct cm_port *port[0];
175 struct cm_av {
176 struct cm_port *port;
177 union ib_gid dgid;
178 struct ib_ah_attr ah_attr;
179 u16 pkey_index;
180 u8 timeout;
181 u8 valid;
182 u8 smac[ETH_ALEN];
185 struct cm_work {
186 struct delayed_work work;
187 struct list_head list;
188 struct cm_port *port;
189 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
190 __be32 local_id; /* Established / timewait */
191 __be32 remote_id;
192 struct ib_cm_event cm_event;
193 struct ib_sa_path_rec path[0];
196 struct cm_timewait_info {
197 struct cm_work work; /* Must be first. */
198 struct list_head list;
199 struct rb_node remote_qp_node;
200 struct rb_node remote_id_node;
201 __be64 remote_ca_guid;
202 __be32 remote_qpn;
203 u8 inserted_remote_qp;
204 u8 inserted_remote_id;
207 struct cm_id_private {
208 struct ib_cm_id id;
210 struct rb_node service_node;
211 struct rb_node sidr_id_node;
212 spinlock_t lock; /* Do not acquire inside cm.lock */
213 struct completion comp;
214 atomic_t refcount;
216 struct ib_mad_send_buf *msg;
217 struct cm_timewait_info *timewait_info;
218 /* todo: use alternate port on send failure */
219 struct cm_av av;
220 struct cm_av alt_av;
221 struct ib_cm_compare_data *compare_data;
223 void *private_data;
224 __be64 tid;
225 __be32 local_qpn;
226 __be32 remote_qpn;
227 enum ib_qp_type qp_type;
228 __be32 sq_psn;
229 __be32 rq_psn;
230 int timeout_ms;
231 enum ib_mtu path_mtu;
232 __be16 pkey;
233 u8 private_data_len;
234 u8 max_cm_retries;
235 u8 peer_to_peer;
236 u8 responder_resources;
237 u8 initiator_depth;
238 u8 retry_count;
239 u8 rnr_retry_count;
240 u8 service_timeout;
241 u8 target_ack_delay;
243 struct list_head work_list;
244 atomic_t work_count;
247 static void cm_work_handler(struct work_struct *work);
249 static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
251 if (atomic_dec_and_test(&cm_id_priv->refcount))
252 complete(&cm_id_priv->comp);
255 static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
256 struct ib_mad_send_buf **msg)
258 struct ib_mad_agent *mad_agent;
259 struct ib_mad_send_buf *m;
260 struct ib_ah *ah;
262 mad_agent = cm_id_priv->av.port->mad_agent;
263 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
264 if (IS_ERR(ah))
265 return PTR_ERR(ah);
267 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
268 cm_id_priv->av.pkey_index,
269 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
270 GFP_ATOMIC);
271 if (IS_ERR(m)) {
272 ib_destroy_ah(ah);
273 return PTR_ERR(m);
276 /* Timeout set by caller if response is expected. */
277 m->ah = ah;
278 m->retries = cm_id_priv->max_cm_retries;
280 atomic_inc(&cm_id_priv->refcount);
281 m->context[0] = cm_id_priv;
282 *msg = m;
283 return 0;
286 static int cm_alloc_response_msg(struct cm_port *port,
287 struct ib_mad_recv_wc *mad_recv_wc,
288 struct ib_mad_send_buf **msg)
290 struct ib_mad_send_buf *m;
291 struct ib_ah *ah;
293 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
294 mad_recv_wc->recv_buf.grh, port->port_num);
295 if (IS_ERR(ah))
296 return PTR_ERR(ah);
298 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
299 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
300 GFP_ATOMIC);
301 if (IS_ERR(m)) {
302 ib_destroy_ah(ah);
303 return PTR_ERR(m);
305 m->ah = ah;
306 *msg = m;
307 return 0;
310 static void cm_free_msg(struct ib_mad_send_buf *msg)
312 ib_destroy_ah(msg->ah);
313 if (msg->context[0])
314 cm_deref_id(msg->context[0]);
315 ib_free_send_mad(msg);
318 static void * cm_copy_private_data(const void *private_data,
319 u8 private_data_len)
321 void *data;
323 if (!private_data || !private_data_len)
324 return NULL;
326 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
327 if (!data)
328 return ERR_PTR(-ENOMEM);
330 return data;
333 static void cm_set_private_data(struct cm_id_private *cm_id_priv,
334 void *private_data, u8 private_data_len)
336 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
337 kfree(cm_id_priv->private_data);
339 cm_id_priv->private_data = private_data;
340 cm_id_priv->private_data_len = private_data_len;
343 static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
344 struct ib_grh *grh, struct cm_av *av)
346 av->port = port;
347 av->pkey_index = wc->pkey_index;
348 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
349 grh, &av->ah_attr);
352 int ib_update_cm_av(struct ib_cm_id *id, const u8 *smac, const u8 *alt_smac)
354 struct cm_id_private *cm_id_priv;
356 cm_id_priv = container_of(id, struct cm_id_private, id);
358 if (smac != NULL)
359 memcpy(cm_id_priv->av.smac, smac, sizeof(cm_id_priv->av.smac));
361 if (alt_smac != NULL)
362 memcpy(cm_id_priv->alt_av.smac, alt_smac,
363 sizeof(cm_id_priv->alt_av.smac));
365 return 0;
367 EXPORT_SYMBOL(ib_update_cm_av);
369 static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
371 struct cm_device *cm_dev;
372 struct cm_port *port = NULL;
373 unsigned long flags;
374 int ret;
375 u8 p;
377 read_lock_irqsave(&cm.device_lock, flags);
378 list_for_each_entry(cm_dev, &cm.device_list, list) {
379 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
380 &p, NULL)) {
381 port = cm_dev->port[p-1];
382 break;
385 read_unlock_irqrestore(&cm.device_lock, flags);
387 if (!port)
388 return -EINVAL;
390 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
391 be16_to_cpu(path->pkey), &av->pkey_index);
392 if (ret)
393 return ret;
395 av->port = port;
396 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
397 &av->ah_attr);
398 av->timeout = path->packet_life_time + 1;
399 memcpy(av->smac, path->smac, sizeof(av->smac));
401 av->valid = 1;
402 return 0;
405 static int cm_alloc_id(struct cm_id_private *cm_id_priv)
407 unsigned long flags;
408 int id;
410 idr_preload(GFP_KERNEL);
411 spin_lock_irqsave(&cm.lock, flags);
413 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
415 spin_unlock_irqrestore(&cm.lock, flags);
416 idr_preload_end();
418 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
419 return id < 0 ? id : 0;
422 static void cm_free_id(__be32 local_id)
424 spin_lock_irq(&cm.lock);
425 idr_remove(&cm.local_id_table,
426 (__force int) (local_id ^ cm.random_id_operand));
427 spin_unlock_irq(&cm.lock);
430 static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
432 struct cm_id_private *cm_id_priv;
434 cm_id_priv = idr_find(&cm.local_id_table,
435 (__force int) (local_id ^ cm.random_id_operand));
436 if (cm_id_priv) {
437 if (cm_id_priv->id.remote_id == remote_id)
438 atomic_inc(&cm_id_priv->refcount);
439 else
440 cm_id_priv = NULL;
443 return cm_id_priv;
446 static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
448 struct cm_id_private *cm_id_priv;
450 spin_lock_irq(&cm.lock);
451 cm_id_priv = cm_get_id(local_id, remote_id);
452 spin_unlock_irq(&cm.lock);
454 return cm_id_priv;
457 static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
459 int i;
461 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
462 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
463 ((unsigned long *) mask)[i];
466 static int cm_compare_data(struct ib_cm_compare_data *src_data,
467 struct ib_cm_compare_data *dst_data)
469 u8 src[IB_CM_COMPARE_SIZE];
470 u8 dst[IB_CM_COMPARE_SIZE];
472 if (!src_data || !dst_data)
473 return 0;
475 cm_mask_copy(src, src_data->data, dst_data->mask);
476 cm_mask_copy(dst, dst_data->data, src_data->mask);
477 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
480 static int cm_compare_private_data(u8 *private_data,
481 struct ib_cm_compare_data *dst_data)
483 u8 src[IB_CM_COMPARE_SIZE];
485 if (!dst_data)
486 return 0;
488 cm_mask_copy(src, private_data, dst_data->mask);
489 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
493 * Trivial helpers to strip endian annotation and compare; the
494 * endianness doesn't actually matter since we just need a stable
495 * order for the RB tree.
497 static int be32_lt(__be32 a, __be32 b)
499 return (__force u32) a < (__force u32) b;
502 static int be32_gt(__be32 a, __be32 b)
504 return (__force u32) a > (__force u32) b;
507 static int be64_lt(__be64 a, __be64 b)
509 return (__force u64) a < (__force u64) b;
512 static int be64_gt(__be64 a, __be64 b)
514 return (__force u64) a > (__force u64) b;
517 static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
519 struct rb_node **link = &cm.listen_service_table.rb_node;
520 struct rb_node *parent = NULL;
521 struct cm_id_private *cur_cm_id_priv;
522 __be64 service_id = cm_id_priv->id.service_id;
523 __be64 service_mask = cm_id_priv->id.service_mask;
524 int data_cmp;
526 while (*link) {
527 parent = *link;
528 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
529 service_node);
530 data_cmp = cm_compare_data(cm_id_priv->compare_data,
531 cur_cm_id_priv->compare_data);
532 if ((cur_cm_id_priv->id.service_mask & service_id) ==
533 (service_mask & cur_cm_id_priv->id.service_id) &&
534 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
535 !data_cmp)
536 return cur_cm_id_priv;
538 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
539 link = &(*link)->rb_left;
540 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
541 link = &(*link)->rb_right;
542 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
543 link = &(*link)->rb_left;
544 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
545 link = &(*link)->rb_right;
546 else if (data_cmp < 0)
547 link = &(*link)->rb_left;
548 else
549 link = &(*link)->rb_right;
551 rb_link_node(&cm_id_priv->service_node, parent, link);
552 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
553 return NULL;
556 static struct cm_id_private * cm_find_listen(struct ib_device *device,
557 __be64 service_id,
558 u8 *private_data)
560 struct rb_node *node = cm.listen_service_table.rb_node;
561 struct cm_id_private *cm_id_priv;
562 int data_cmp;
564 while (node) {
565 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
566 data_cmp = cm_compare_private_data(private_data,
567 cm_id_priv->compare_data);
568 if ((cm_id_priv->id.service_mask & service_id) ==
569 cm_id_priv->id.service_id &&
570 (cm_id_priv->id.device == device) && !data_cmp)
571 return cm_id_priv;
573 if (device < cm_id_priv->id.device)
574 node = node->rb_left;
575 else if (device > cm_id_priv->id.device)
576 node = node->rb_right;
577 else if (be64_lt(service_id, cm_id_priv->id.service_id))
578 node = node->rb_left;
579 else if (be64_gt(service_id, cm_id_priv->id.service_id))
580 node = node->rb_right;
581 else if (data_cmp < 0)
582 node = node->rb_left;
583 else
584 node = node->rb_right;
586 return NULL;
589 static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
590 *timewait_info)
592 struct rb_node **link = &cm.remote_id_table.rb_node;
593 struct rb_node *parent = NULL;
594 struct cm_timewait_info *cur_timewait_info;
595 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
596 __be32 remote_id = timewait_info->work.remote_id;
598 while (*link) {
599 parent = *link;
600 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
601 remote_id_node);
602 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
603 link = &(*link)->rb_left;
604 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
605 link = &(*link)->rb_right;
606 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
607 link = &(*link)->rb_left;
608 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
609 link = &(*link)->rb_right;
610 else
611 return cur_timewait_info;
613 timewait_info->inserted_remote_id = 1;
614 rb_link_node(&timewait_info->remote_id_node, parent, link);
615 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
616 return NULL;
619 static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
620 __be32 remote_id)
622 struct rb_node *node = cm.remote_id_table.rb_node;
623 struct cm_timewait_info *timewait_info;
625 while (node) {
626 timewait_info = rb_entry(node, struct cm_timewait_info,
627 remote_id_node);
628 if (be32_lt(remote_id, timewait_info->work.remote_id))
629 node = node->rb_left;
630 else if (be32_gt(remote_id, timewait_info->work.remote_id))
631 node = node->rb_right;
632 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
633 node = node->rb_left;
634 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
635 node = node->rb_right;
636 else
637 return timewait_info;
639 return NULL;
642 static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
643 *timewait_info)
645 struct rb_node **link = &cm.remote_qp_table.rb_node;
646 struct rb_node *parent = NULL;
647 struct cm_timewait_info *cur_timewait_info;
648 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
649 __be32 remote_qpn = timewait_info->remote_qpn;
651 while (*link) {
652 parent = *link;
653 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
654 remote_qp_node);
655 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
656 link = &(*link)->rb_left;
657 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
658 link = &(*link)->rb_right;
659 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
660 link = &(*link)->rb_left;
661 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
662 link = &(*link)->rb_right;
663 else
664 return cur_timewait_info;
666 timewait_info->inserted_remote_qp = 1;
667 rb_link_node(&timewait_info->remote_qp_node, parent, link);
668 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
669 return NULL;
672 static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
673 *cm_id_priv)
675 struct rb_node **link = &cm.remote_sidr_table.rb_node;
676 struct rb_node *parent = NULL;
677 struct cm_id_private *cur_cm_id_priv;
678 union ib_gid *port_gid = &cm_id_priv->av.dgid;
679 __be32 remote_id = cm_id_priv->id.remote_id;
681 while (*link) {
682 parent = *link;
683 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
684 sidr_id_node);
685 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
686 link = &(*link)->rb_left;
687 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
688 link = &(*link)->rb_right;
689 else {
690 int cmp;
691 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
692 sizeof *port_gid);
693 if (cmp < 0)
694 link = &(*link)->rb_left;
695 else if (cmp > 0)
696 link = &(*link)->rb_right;
697 else
698 return cur_cm_id_priv;
701 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
702 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
703 return NULL;
706 static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
707 enum ib_cm_sidr_status status)
709 struct ib_cm_sidr_rep_param param;
711 memset(&param, 0, sizeof param);
712 param.status = status;
713 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
716 struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
717 ib_cm_handler cm_handler,
718 void *context)
720 struct cm_id_private *cm_id_priv;
721 int ret;
723 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
724 if (!cm_id_priv)
725 return ERR_PTR(-ENOMEM);
727 cm_id_priv->id.state = IB_CM_IDLE;
728 cm_id_priv->id.device = device;
729 cm_id_priv->id.cm_handler = cm_handler;
730 cm_id_priv->id.context = context;
731 cm_id_priv->id.remote_cm_qpn = 1;
732 ret = cm_alloc_id(cm_id_priv);
733 if (ret)
734 goto error;
736 spin_lock_init(&cm_id_priv->lock);
737 init_completion(&cm_id_priv->comp);
738 INIT_LIST_HEAD(&cm_id_priv->work_list);
739 atomic_set(&cm_id_priv->work_count, -1);
740 atomic_set(&cm_id_priv->refcount, 1);
741 return &cm_id_priv->id;
743 error:
744 kfree(cm_id_priv);
745 return ERR_PTR(-ENOMEM);
747 EXPORT_SYMBOL(ib_create_cm_id);
749 static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
751 struct cm_work *work;
753 if (list_empty(&cm_id_priv->work_list))
754 return NULL;
756 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
757 list_del(&work->list);
758 return work;
761 static void cm_free_work(struct cm_work *work)
763 if (work->mad_recv_wc)
764 ib_free_recv_mad(work->mad_recv_wc);
765 kfree(work);
768 static inline int cm_convert_to_ms(int iba_time)
770 /* approximate conversion to ms from 4.096us x 2^iba_time */
771 return 1 << max(iba_time - 8, 0);
775 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
776 * Because of how ack_timeout is stored, adding one doubles the timeout.
777 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
778 * increment it (round up) only if the other is within 50%.
780 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
782 int ack_timeout = packet_life_time + 1;
784 if (ack_timeout >= ca_ack_delay)
785 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
786 else
787 ack_timeout = ca_ack_delay +
788 (ack_timeout >= (ca_ack_delay - 1));
790 return min(31, ack_timeout);
793 static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
795 if (timewait_info->inserted_remote_id) {
796 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
797 timewait_info->inserted_remote_id = 0;
800 if (timewait_info->inserted_remote_qp) {
801 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
802 timewait_info->inserted_remote_qp = 0;
806 static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
808 struct cm_timewait_info *timewait_info;
810 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
811 if (!timewait_info)
812 return ERR_PTR(-ENOMEM);
814 timewait_info->work.local_id = local_id;
815 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
816 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
817 return timewait_info;
820 static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
822 int wait_time;
823 unsigned long flags;
825 spin_lock_irqsave(&cm.lock, flags);
826 cm_cleanup_timewait(cm_id_priv->timewait_info);
827 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
828 spin_unlock_irqrestore(&cm.lock, flags);
831 * The cm_id could be destroyed by the user before we exit timewait.
832 * To protect against this, we search for the cm_id after exiting
833 * timewait before notifying the user that we've exited timewait.
835 cm_id_priv->id.state = IB_CM_TIMEWAIT;
836 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
837 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
838 msecs_to_jiffies(wait_time));
839 cm_id_priv->timewait_info = NULL;
842 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
844 unsigned long flags;
846 cm_id_priv->id.state = IB_CM_IDLE;
847 if (cm_id_priv->timewait_info) {
848 spin_lock_irqsave(&cm.lock, flags);
849 cm_cleanup_timewait(cm_id_priv->timewait_info);
850 spin_unlock_irqrestore(&cm.lock, flags);
851 kfree(cm_id_priv->timewait_info);
852 cm_id_priv->timewait_info = NULL;
856 static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
858 struct cm_id_private *cm_id_priv;
859 struct cm_work *work;
861 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
862 retest:
863 spin_lock_irq(&cm_id_priv->lock);
864 switch (cm_id->state) {
865 case IB_CM_LISTEN:
866 cm_id->state = IB_CM_IDLE;
867 spin_unlock_irq(&cm_id_priv->lock);
868 spin_lock_irq(&cm.lock);
869 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
870 spin_unlock_irq(&cm.lock);
871 break;
872 case IB_CM_SIDR_REQ_SENT:
873 cm_id->state = IB_CM_IDLE;
874 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
875 spin_unlock_irq(&cm_id_priv->lock);
876 break;
877 case IB_CM_SIDR_REQ_RCVD:
878 spin_unlock_irq(&cm_id_priv->lock);
879 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
880 break;
881 case IB_CM_REQ_SENT:
882 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
883 spin_unlock_irq(&cm_id_priv->lock);
884 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
885 &cm_id_priv->id.device->node_guid,
886 sizeof cm_id_priv->id.device->node_guid,
887 NULL, 0);
888 break;
889 case IB_CM_REQ_RCVD:
890 if (err == -ENOMEM) {
891 /* Do not reject to allow future retries. */
892 cm_reset_to_idle(cm_id_priv);
893 spin_unlock_irq(&cm_id_priv->lock);
894 } else {
895 spin_unlock_irq(&cm_id_priv->lock);
896 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
897 NULL, 0, NULL, 0);
899 break;
900 case IB_CM_MRA_REQ_RCVD:
901 case IB_CM_REP_SENT:
902 case IB_CM_MRA_REP_RCVD:
903 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
904 /* Fall through */
905 case IB_CM_MRA_REQ_SENT:
906 case IB_CM_REP_RCVD:
907 case IB_CM_MRA_REP_SENT:
908 spin_unlock_irq(&cm_id_priv->lock);
909 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
910 NULL, 0, NULL, 0);
911 break;
912 case IB_CM_ESTABLISHED:
913 spin_unlock_irq(&cm_id_priv->lock);
914 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
915 break;
916 ib_send_cm_dreq(cm_id, NULL, 0);
917 goto retest;
918 case IB_CM_DREQ_SENT:
919 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
920 cm_enter_timewait(cm_id_priv);
921 spin_unlock_irq(&cm_id_priv->lock);
922 break;
923 case IB_CM_DREQ_RCVD:
924 spin_unlock_irq(&cm_id_priv->lock);
925 ib_send_cm_drep(cm_id, NULL, 0);
926 break;
927 default:
928 spin_unlock_irq(&cm_id_priv->lock);
929 break;
932 cm_free_id(cm_id->local_id);
933 cm_deref_id(cm_id_priv);
934 wait_for_completion(&cm_id_priv->comp);
935 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
936 cm_free_work(work);
937 kfree(cm_id_priv->compare_data);
938 kfree(cm_id_priv->private_data);
939 kfree(cm_id_priv);
942 void ib_destroy_cm_id(struct ib_cm_id *cm_id)
944 cm_destroy_id(cm_id, 0);
946 EXPORT_SYMBOL(ib_destroy_cm_id);
948 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
949 struct ib_cm_compare_data *compare_data)
951 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
952 unsigned long flags;
953 int ret = 0;
955 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
956 service_id &= service_mask;
957 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
958 (service_id != IB_CM_ASSIGN_SERVICE_ID))
959 return -EINVAL;
961 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
962 if (cm_id->state != IB_CM_IDLE)
963 return -EINVAL;
965 if (compare_data) {
966 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
967 GFP_KERNEL);
968 if (!cm_id_priv->compare_data)
969 return -ENOMEM;
970 cm_mask_copy(cm_id_priv->compare_data->data,
971 compare_data->data, compare_data->mask);
972 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
973 IB_CM_COMPARE_SIZE);
976 cm_id->state = IB_CM_LISTEN;
978 spin_lock_irqsave(&cm.lock, flags);
979 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
980 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
981 cm_id->service_mask = ~cpu_to_be64(0);
982 } else {
983 cm_id->service_id = service_id;
984 cm_id->service_mask = service_mask;
986 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
987 spin_unlock_irqrestore(&cm.lock, flags);
989 if (cur_cm_id_priv) {
990 cm_id->state = IB_CM_IDLE;
991 kfree(cm_id_priv->compare_data);
992 cm_id_priv->compare_data = NULL;
993 ret = -EBUSY;
995 return ret;
997 EXPORT_SYMBOL(ib_cm_listen);
999 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1000 enum cm_msg_sequence msg_seq)
1002 u64 hi_tid, low_tid;
1004 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1005 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1006 (msg_seq << 30));
1007 return cpu_to_be64(hi_tid | low_tid);
1010 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1011 __be16 attr_id, __be64 tid)
1013 hdr->base_version = IB_MGMT_BASE_VERSION;
1014 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1015 hdr->class_version = IB_CM_CLASS_VERSION;
1016 hdr->method = IB_MGMT_METHOD_SEND;
1017 hdr->attr_id = attr_id;
1018 hdr->tid = tid;
1021 static void cm_format_req(struct cm_req_msg *req_msg,
1022 struct cm_id_private *cm_id_priv,
1023 struct ib_cm_req_param *param)
1025 struct ib_sa_path_rec *pri_path = param->primary_path;
1026 struct ib_sa_path_rec *alt_path = param->alternate_path;
1028 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1029 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1031 req_msg->local_comm_id = cm_id_priv->id.local_id;
1032 req_msg->service_id = param->service_id;
1033 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1034 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1035 cm_req_set_init_depth(req_msg, param->initiator_depth);
1036 cm_req_set_remote_resp_timeout(req_msg,
1037 param->remote_cm_response_timeout);
1038 cm_req_set_qp_type(req_msg, param->qp_type);
1039 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1040 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1041 cm_req_set_local_resp_timeout(req_msg,
1042 param->local_cm_response_timeout);
1043 req_msg->pkey = param->primary_path->pkey;
1044 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1045 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1047 if (param->qp_type != IB_QPT_XRC_INI) {
1048 cm_req_set_resp_res(req_msg, param->responder_resources);
1049 cm_req_set_retry_count(req_msg, param->retry_count);
1050 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1051 cm_req_set_srq(req_msg, param->srq);
1054 if (pri_path->hop_limit <= 1) {
1055 req_msg->primary_local_lid = pri_path->slid;
1056 req_msg->primary_remote_lid = pri_path->dlid;
1057 } else {
1058 /* Work-around until there's a way to obtain remote LID info */
1059 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1060 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1062 req_msg->primary_local_gid = pri_path->sgid;
1063 req_msg->primary_remote_gid = pri_path->dgid;
1064 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1065 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1066 req_msg->primary_traffic_class = pri_path->traffic_class;
1067 req_msg->primary_hop_limit = pri_path->hop_limit;
1068 cm_req_set_primary_sl(req_msg, pri_path->sl);
1069 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1070 cm_req_set_primary_local_ack_timeout(req_msg,
1071 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1072 pri_path->packet_life_time));
1074 if (alt_path) {
1075 if (alt_path->hop_limit <= 1) {
1076 req_msg->alt_local_lid = alt_path->slid;
1077 req_msg->alt_remote_lid = alt_path->dlid;
1078 } else {
1079 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1080 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1082 req_msg->alt_local_gid = alt_path->sgid;
1083 req_msg->alt_remote_gid = alt_path->dgid;
1084 cm_req_set_alt_flow_label(req_msg,
1085 alt_path->flow_label);
1086 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1087 req_msg->alt_traffic_class = alt_path->traffic_class;
1088 req_msg->alt_hop_limit = alt_path->hop_limit;
1089 cm_req_set_alt_sl(req_msg, alt_path->sl);
1090 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1091 cm_req_set_alt_local_ack_timeout(req_msg,
1092 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1093 alt_path->packet_life_time));
1096 if (param->private_data && param->private_data_len)
1097 memcpy(req_msg->private_data, param->private_data,
1098 param->private_data_len);
1101 static int cm_validate_req_param(struct ib_cm_req_param *param)
1103 /* peer-to-peer not supported */
1104 if (param->peer_to_peer)
1105 return -EINVAL;
1107 if (!param->primary_path)
1108 return -EINVAL;
1110 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1111 param->qp_type != IB_QPT_XRC_INI)
1112 return -EINVAL;
1114 if (param->private_data &&
1115 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1116 return -EINVAL;
1118 if (param->alternate_path &&
1119 (param->alternate_path->pkey != param->primary_path->pkey ||
1120 param->alternate_path->mtu != param->primary_path->mtu))
1121 return -EINVAL;
1123 return 0;
1126 int ib_send_cm_req(struct ib_cm_id *cm_id,
1127 struct ib_cm_req_param *param)
1129 struct cm_id_private *cm_id_priv;
1130 struct cm_req_msg *req_msg;
1131 unsigned long flags;
1132 int ret;
1134 ret = cm_validate_req_param(param);
1135 if (ret)
1136 return ret;
1138 /* Verify that we're not in timewait. */
1139 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1140 spin_lock_irqsave(&cm_id_priv->lock, flags);
1141 if (cm_id->state != IB_CM_IDLE) {
1142 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1143 ret = -EINVAL;
1144 goto out;
1146 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1148 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1149 id.local_id);
1150 if (IS_ERR(cm_id_priv->timewait_info)) {
1151 ret = PTR_ERR(cm_id_priv->timewait_info);
1152 goto out;
1155 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1156 if (ret)
1157 goto error1;
1158 if (param->alternate_path) {
1159 ret = cm_init_av_by_path(param->alternate_path,
1160 &cm_id_priv->alt_av);
1161 if (ret)
1162 goto error1;
1164 cm_id->service_id = param->service_id;
1165 cm_id->service_mask = ~cpu_to_be64(0);
1166 cm_id_priv->timeout_ms = cm_convert_to_ms(
1167 param->primary_path->packet_life_time) * 2 +
1168 cm_convert_to_ms(
1169 param->remote_cm_response_timeout);
1170 cm_id_priv->max_cm_retries = param->max_cm_retries;
1171 cm_id_priv->initiator_depth = param->initiator_depth;
1172 cm_id_priv->responder_resources = param->responder_resources;
1173 cm_id_priv->retry_count = param->retry_count;
1174 cm_id_priv->path_mtu = param->primary_path->mtu;
1175 cm_id_priv->pkey = param->primary_path->pkey;
1176 cm_id_priv->qp_type = param->qp_type;
1178 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1179 if (ret)
1180 goto error1;
1182 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1183 cm_format_req(req_msg, cm_id_priv, param);
1184 cm_id_priv->tid = req_msg->hdr.tid;
1185 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1186 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1188 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1189 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1191 spin_lock_irqsave(&cm_id_priv->lock, flags);
1192 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1193 if (ret) {
1194 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1195 goto error2;
1197 BUG_ON(cm_id->state != IB_CM_IDLE);
1198 cm_id->state = IB_CM_REQ_SENT;
1199 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1200 return 0;
1202 error2: cm_free_msg(cm_id_priv->msg);
1203 error1: kfree(cm_id_priv->timewait_info);
1204 out: return ret;
1206 EXPORT_SYMBOL(ib_send_cm_req);
1208 static int cm_issue_rej(struct cm_port *port,
1209 struct ib_mad_recv_wc *mad_recv_wc,
1210 enum ib_cm_rej_reason reason,
1211 enum cm_msg_response msg_rejected,
1212 void *ari, u8 ari_length)
1214 struct ib_mad_send_buf *msg = NULL;
1215 struct cm_rej_msg *rej_msg, *rcv_msg;
1216 int ret;
1218 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1219 if (ret)
1220 return ret;
1222 /* We just need common CM header information. Cast to any message. */
1223 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1224 rej_msg = (struct cm_rej_msg *) msg->mad;
1226 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1227 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1228 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1229 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1230 rej_msg->reason = cpu_to_be16(reason);
1232 if (ari && ari_length) {
1233 cm_rej_set_reject_info_len(rej_msg, ari_length);
1234 memcpy(rej_msg->ari, ari, ari_length);
1237 ret = ib_post_send_mad(msg, NULL);
1238 if (ret)
1239 cm_free_msg(msg);
1241 return ret;
1244 static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1245 __be32 local_qpn, __be32 remote_qpn)
1247 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1248 ((local_ca_guid == remote_ca_guid) &&
1249 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1252 static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1253 struct ib_sa_path_rec *primary_path,
1254 struct ib_sa_path_rec *alt_path)
1256 memset(primary_path, 0, sizeof *primary_path);
1257 primary_path->dgid = req_msg->primary_local_gid;
1258 primary_path->sgid = req_msg->primary_remote_gid;
1259 primary_path->dlid = req_msg->primary_local_lid;
1260 primary_path->slid = req_msg->primary_remote_lid;
1261 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1262 primary_path->hop_limit = req_msg->primary_hop_limit;
1263 primary_path->traffic_class = req_msg->primary_traffic_class;
1264 primary_path->reversible = 1;
1265 primary_path->pkey = req_msg->pkey;
1266 primary_path->sl = cm_req_get_primary_sl(req_msg);
1267 primary_path->mtu_selector = IB_SA_EQ;
1268 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1269 primary_path->rate_selector = IB_SA_EQ;
1270 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1271 primary_path->packet_life_time_selector = IB_SA_EQ;
1272 primary_path->packet_life_time =
1273 cm_req_get_primary_local_ack_timeout(req_msg);
1274 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1276 if (req_msg->alt_local_lid) {
1277 memset(alt_path, 0, sizeof *alt_path);
1278 alt_path->dgid = req_msg->alt_local_gid;
1279 alt_path->sgid = req_msg->alt_remote_gid;
1280 alt_path->dlid = req_msg->alt_local_lid;
1281 alt_path->slid = req_msg->alt_remote_lid;
1282 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1283 alt_path->hop_limit = req_msg->alt_hop_limit;
1284 alt_path->traffic_class = req_msg->alt_traffic_class;
1285 alt_path->reversible = 1;
1286 alt_path->pkey = req_msg->pkey;
1287 alt_path->sl = cm_req_get_alt_sl(req_msg);
1288 alt_path->mtu_selector = IB_SA_EQ;
1289 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1290 alt_path->rate_selector = IB_SA_EQ;
1291 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1292 alt_path->packet_life_time_selector = IB_SA_EQ;
1293 alt_path->packet_life_time =
1294 cm_req_get_alt_local_ack_timeout(req_msg);
1295 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1299 static void cm_format_req_event(struct cm_work *work,
1300 struct cm_id_private *cm_id_priv,
1301 struct ib_cm_id *listen_id)
1303 struct cm_req_msg *req_msg;
1304 struct ib_cm_req_event_param *param;
1306 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1307 param = &work->cm_event.param.req_rcvd;
1308 param->listen_id = listen_id;
1309 param->port = cm_id_priv->av.port->port_num;
1310 param->primary_path = &work->path[0];
1311 if (req_msg->alt_local_lid)
1312 param->alternate_path = &work->path[1];
1313 else
1314 param->alternate_path = NULL;
1315 param->remote_ca_guid = req_msg->local_ca_guid;
1316 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1317 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1318 param->qp_type = cm_req_get_qp_type(req_msg);
1319 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1320 param->responder_resources = cm_req_get_init_depth(req_msg);
1321 param->initiator_depth = cm_req_get_resp_res(req_msg);
1322 param->local_cm_response_timeout =
1323 cm_req_get_remote_resp_timeout(req_msg);
1324 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1325 param->remote_cm_response_timeout =
1326 cm_req_get_local_resp_timeout(req_msg);
1327 param->retry_count = cm_req_get_retry_count(req_msg);
1328 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1329 param->srq = cm_req_get_srq(req_msg);
1330 work->cm_event.private_data = &req_msg->private_data;
1333 static void cm_process_work(struct cm_id_private *cm_id_priv,
1334 struct cm_work *work)
1336 int ret;
1338 /* We will typically only have the current event to report. */
1339 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1340 cm_free_work(work);
1342 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1343 spin_lock_irq(&cm_id_priv->lock);
1344 work = cm_dequeue_work(cm_id_priv);
1345 spin_unlock_irq(&cm_id_priv->lock);
1346 BUG_ON(!work);
1347 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1348 &work->cm_event);
1349 cm_free_work(work);
1351 cm_deref_id(cm_id_priv);
1352 if (ret)
1353 cm_destroy_id(&cm_id_priv->id, ret);
1356 static void cm_format_mra(struct cm_mra_msg *mra_msg,
1357 struct cm_id_private *cm_id_priv,
1358 enum cm_msg_response msg_mraed, u8 service_timeout,
1359 const void *private_data, u8 private_data_len)
1361 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1362 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1363 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1364 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1365 cm_mra_set_service_timeout(mra_msg, service_timeout);
1367 if (private_data && private_data_len)
1368 memcpy(mra_msg->private_data, private_data, private_data_len);
1371 static void cm_format_rej(struct cm_rej_msg *rej_msg,
1372 struct cm_id_private *cm_id_priv,
1373 enum ib_cm_rej_reason reason,
1374 void *ari,
1375 u8 ari_length,
1376 const void *private_data,
1377 u8 private_data_len)
1379 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1380 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1382 switch(cm_id_priv->id.state) {
1383 case IB_CM_REQ_RCVD:
1384 rej_msg->local_comm_id = 0;
1385 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1386 break;
1387 case IB_CM_MRA_REQ_SENT:
1388 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1389 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1390 break;
1391 case IB_CM_REP_RCVD:
1392 case IB_CM_MRA_REP_SENT:
1393 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1394 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1395 break;
1396 default:
1397 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1398 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1399 break;
1402 rej_msg->reason = cpu_to_be16(reason);
1403 if (ari && ari_length) {
1404 cm_rej_set_reject_info_len(rej_msg, ari_length);
1405 memcpy(rej_msg->ari, ari, ari_length);
1408 if (private_data && private_data_len)
1409 memcpy(rej_msg->private_data, private_data, private_data_len);
1412 static void cm_dup_req_handler(struct cm_work *work,
1413 struct cm_id_private *cm_id_priv)
1415 struct ib_mad_send_buf *msg = NULL;
1416 int ret;
1418 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1419 counter[CM_REQ_COUNTER]);
1421 /* Quick state check to discard duplicate REQs. */
1422 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1423 return;
1425 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1426 if (ret)
1427 return;
1429 spin_lock_irq(&cm_id_priv->lock);
1430 switch (cm_id_priv->id.state) {
1431 case IB_CM_MRA_REQ_SENT:
1432 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1433 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1434 cm_id_priv->private_data,
1435 cm_id_priv->private_data_len);
1436 break;
1437 case IB_CM_TIMEWAIT:
1438 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1439 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1440 break;
1441 default:
1442 goto unlock;
1444 spin_unlock_irq(&cm_id_priv->lock);
1446 ret = ib_post_send_mad(msg, NULL);
1447 if (ret)
1448 goto free;
1449 return;
1451 unlock: spin_unlock_irq(&cm_id_priv->lock);
1452 free: cm_free_msg(msg);
1455 static struct cm_id_private * cm_match_req(struct cm_work *work,
1456 struct cm_id_private *cm_id_priv)
1458 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1459 struct cm_timewait_info *timewait_info;
1460 struct cm_req_msg *req_msg;
1462 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1464 /* Check for possible duplicate REQ. */
1465 spin_lock_irq(&cm.lock);
1466 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1467 if (timewait_info) {
1468 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1469 timewait_info->work.remote_id);
1470 spin_unlock_irq(&cm.lock);
1471 if (cur_cm_id_priv) {
1472 cm_dup_req_handler(work, cur_cm_id_priv);
1473 cm_deref_id(cur_cm_id_priv);
1475 return NULL;
1478 /* Check for stale connections. */
1479 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1480 if (timewait_info) {
1481 cm_cleanup_timewait(cm_id_priv->timewait_info);
1482 spin_unlock_irq(&cm.lock);
1483 cm_issue_rej(work->port, work->mad_recv_wc,
1484 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1485 NULL, 0);
1486 return NULL;
1489 /* Find matching listen request. */
1490 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1491 req_msg->service_id,
1492 req_msg->private_data);
1493 if (!listen_cm_id_priv) {
1494 cm_cleanup_timewait(cm_id_priv->timewait_info);
1495 spin_unlock_irq(&cm.lock);
1496 cm_issue_rej(work->port, work->mad_recv_wc,
1497 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1498 NULL, 0);
1499 goto out;
1501 atomic_inc(&listen_cm_id_priv->refcount);
1502 atomic_inc(&cm_id_priv->refcount);
1503 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1504 atomic_inc(&cm_id_priv->work_count);
1505 spin_unlock_irq(&cm.lock);
1506 out:
1507 return listen_cm_id_priv;
1511 * Work-around for inter-subnet connections. If the LIDs are permissive,
1512 * we need to override the LID/SL data in the REQ with the LID information
1513 * in the work completion.
1515 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1517 if (!cm_req_get_primary_subnet_local(req_msg)) {
1518 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1519 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1520 cm_req_set_primary_sl(req_msg, wc->sl);
1523 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1524 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1527 if (!cm_req_get_alt_subnet_local(req_msg)) {
1528 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1529 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1530 cm_req_set_alt_sl(req_msg, wc->sl);
1533 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1534 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1538 static int cm_req_handler(struct cm_work *work)
1540 struct ib_cm_id *cm_id;
1541 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1542 struct cm_req_msg *req_msg;
1543 int ret;
1545 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1547 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1548 if (IS_ERR(cm_id))
1549 return PTR_ERR(cm_id);
1551 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1552 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1553 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1554 work->mad_recv_wc->recv_buf.grh,
1555 &cm_id_priv->av);
1556 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1557 id.local_id);
1558 if (IS_ERR(cm_id_priv->timewait_info)) {
1559 ret = PTR_ERR(cm_id_priv->timewait_info);
1560 goto destroy;
1562 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1563 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1564 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1566 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1567 if (!listen_cm_id_priv) {
1568 ret = -EINVAL;
1569 kfree(cm_id_priv->timewait_info);
1570 goto destroy;
1573 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1574 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1575 cm_id_priv->id.service_id = req_msg->service_id;
1576 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1578 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1579 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1581 memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
1582 work->path[0].vlan_id = cm_id_priv->av.ah_attr.vlan_id;
1583 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1584 if (ret) {
1585 ib_get_cached_gid(work->port->cm_dev->ib_device,
1586 work->port->port_num, 0, &work->path[0].sgid);
1587 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1588 &work->path[0].sgid, sizeof work->path[0].sgid,
1589 NULL, 0);
1590 goto rejected;
1592 if (req_msg->alt_local_lid) {
1593 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1594 if (ret) {
1595 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1596 &work->path[0].sgid,
1597 sizeof work->path[0].sgid, NULL, 0);
1598 goto rejected;
1601 cm_id_priv->tid = req_msg->hdr.tid;
1602 cm_id_priv->timeout_ms = cm_convert_to_ms(
1603 cm_req_get_local_resp_timeout(req_msg));
1604 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1605 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1606 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1607 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1608 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1609 cm_id_priv->pkey = req_msg->pkey;
1610 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1611 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1612 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1613 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1615 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1616 cm_process_work(cm_id_priv, work);
1617 cm_deref_id(listen_cm_id_priv);
1618 return 0;
1620 rejected:
1621 atomic_dec(&cm_id_priv->refcount);
1622 cm_deref_id(listen_cm_id_priv);
1623 destroy:
1624 ib_destroy_cm_id(cm_id);
1625 return ret;
1628 static void cm_format_rep(struct cm_rep_msg *rep_msg,
1629 struct cm_id_private *cm_id_priv,
1630 struct ib_cm_rep_param *param)
1632 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1633 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1634 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1635 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1636 rep_msg->resp_resources = param->responder_resources;
1637 cm_rep_set_target_ack_delay(rep_msg,
1638 cm_id_priv->av.port->cm_dev->ack_delay);
1639 cm_rep_set_failover(rep_msg, param->failover_accepted);
1640 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1641 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1643 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1644 rep_msg->initiator_depth = param->initiator_depth;
1645 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1646 cm_rep_set_srq(rep_msg, param->srq);
1647 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1648 } else {
1649 cm_rep_set_srq(rep_msg, 1);
1650 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
1653 if (param->private_data && param->private_data_len)
1654 memcpy(rep_msg->private_data, param->private_data,
1655 param->private_data_len);
1658 int ib_send_cm_rep(struct ib_cm_id *cm_id,
1659 struct ib_cm_rep_param *param)
1661 struct cm_id_private *cm_id_priv;
1662 struct ib_mad_send_buf *msg;
1663 struct cm_rep_msg *rep_msg;
1664 unsigned long flags;
1665 int ret;
1667 if (param->private_data &&
1668 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1669 return -EINVAL;
1671 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1672 spin_lock_irqsave(&cm_id_priv->lock, flags);
1673 if (cm_id->state != IB_CM_REQ_RCVD &&
1674 cm_id->state != IB_CM_MRA_REQ_SENT) {
1675 ret = -EINVAL;
1676 goto out;
1679 ret = cm_alloc_msg(cm_id_priv, &msg);
1680 if (ret)
1681 goto out;
1683 rep_msg = (struct cm_rep_msg *) msg->mad;
1684 cm_format_rep(rep_msg, cm_id_priv, param);
1685 msg->timeout_ms = cm_id_priv->timeout_ms;
1686 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1688 ret = ib_post_send_mad(msg, NULL);
1689 if (ret) {
1690 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1691 cm_free_msg(msg);
1692 return ret;
1695 cm_id->state = IB_CM_REP_SENT;
1696 cm_id_priv->msg = msg;
1697 cm_id_priv->initiator_depth = param->initiator_depth;
1698 cm_id_priv->responder_resources = param->responder_resources;
1699 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1700 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
1702 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1703 return ret;
1705 EXPORT_SYMBOL(ib_send_cm_rep);
1707 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1708 struct cm_id_private *cm_id_priv,
1709 const void *private_data,
1710 u8 private_data_len)
1712 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1713 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1714 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1716 if (private_data && private_data_len)
1717 memcpy(rtu_msg->private_data, private_data, private_data_len);
1720 int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1721 const void *private_data,
1722 u8 private_data_len)
1724 struct cm_id_private *cm_id_priv;
1725 struct ib_mad_send_buf *msg;
1726 unsigned long flags;
1727 void *data;
1728 int ret;
1730 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1731 return -EINVAL;
1733 data = cm_copy_private_data(private_data, private_data_len);
1734 if (IS_ERR(data))
1735 return PTR_ERR(data);
1737 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1738 spin_lock_irqsave(&cm_id_priv->lock, flags);
1739 if (cm_id->state != IB_CM_REP_RCVD &&
1740 cm_id->state != IB_CM_MRA_REP_SENT) {
1741 ret = -EINVAL;
1742 goto error;
1745 ret = cm_alloc_msg(cm_id_priv, &msg);
1746 if (ret)
1747 goto error;
1749 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1750 private_data, private_data_len);
1752 ret = ib_post_send_mad(msg, NULL);
1753 if (ret) {
1754 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1755 cm_free_msg(msg);
1756 kfree(data);
1757 return ret;
1760 cm_id->state = IB_CM_ESTABLISHED;
1761 cm_set_private_data(cm_id_priv, data, private_data_len);
1762 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1763 return 0;
1765 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1766 kfree(data);
1767 return ret;
1769 EXPORT_SYMBOL(ib_send_cm_rtu);
1771 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
1773 struct cm_rep_msg *rep_msg;
1774 struct ib_cm_rep_event_param *param;
1776 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1777 param = &work->cm_event.param.rep_rcvd;
1778 param->remote_ca_guid = rep_msg->local_ca_guid;
1779 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1780 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
1781 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1782 param->responder_resources = rep_msg->initiator_depth;
1783 param->initiator_depth = rep_msg->resp_resources;
1784 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1785 param->failover_accepted = cm_rep_get_failover(rep_msg);
1786 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1787 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1788 param->srq = cm_rep_get_srq(rep_msg);
1789 work->cm_event.private_data = &rep_msg->private_data;
1792 static void cm_dup_rep_handler(struct cm_work *work)
1794 struct cm_id_private *cm_id_priv;
1795 struct cm_rep_msg *rep_msg;
1796 struct ib_mad_send_buf *msg = NULL;
1797 int ret;
1799 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1800 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1801 rep_msg->local_comm_id);
1802 if (!cm_id_priv)
1803 return;
1805 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1806 counter[CM_REP_COUNTER]);
1807 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1808 if (ret)
1809 goto deref;
1811 spin_lock_irq(&cm_id_priv->lock);
1812 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1813 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1814 cm_id_priv->private_data,
1815 cm_id_priv->private_data_len);
1816 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1817 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1818 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1819 cm_id_priv->private_data,
1820 cm_id_priv->private_data_len);
1821 else
1822 goto unlock;
1823 spin_unlock_irq(&cm_id_priv->lock);
1825 ret = ib_post_send_mad(msg, NULL);
1826 if (ret)
1827 goto free;
1828 goto deref;
1830 unlock: spin_unlock_irq(&cm_id_priv->lock);
1831 free: cm_free_msg(msg);
1832 deref: cm_deref_id(cm_id_priv);
1835 static int cm_rep_handler(struct cm_work *work)
1837 struct cm_id_private *cm_id_priv;
1838 struct cm_rep_msg *rep_msg;
1839 int ret;
1841 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1842 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1843 if (!cm_id_priv) {
1844 cm_dup_rep_handler(work);
1845 return -EINVAL;
1848 cm_format_rep_event(work, cm_id_priv->qp_type);
1850 spin_lock_irq(&cm_id_priv->lock);
1851 switch (cm_id_priv->id.state) {
1852 case IB_CM_REQ_SENT:
1853 case IB_CM_MRA_REQ_RCVD:
1854 break;
1855 default:
1856 spin_unlock_irq(&cm_id_priv->lock);
1857 ret = -EINVAL;
1858 goto error;
1861 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1862 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1863 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
1865 spin_lock(&cm.lock);
1866 /* Check for duplicate REP. */
1867 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1868 spin_unlock(&cm.lock);
1869 spin_unlock_irq(&cm_id_priv->lock);
1870 ret = -EINVAL;
1871 goto error;
1873 /* Check for a stale connection. */
1874 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1875 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1876 &cm.remote_id_table);
1877 cm_id_priv->timewait_info->inserted_remote_id = 0;
1878 spin_unlock(&cm.lock);
1879 spin_unlock_irq(&cm_id_priv->lock);
1880 cm_issue_rej(work->port, work->mad_recv_wc,
1881 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1882 NULL, 0);
1883 ret = -EINVAL;
1884 goto error;
1886 spin_unlock(&cm.lock);
1888 cm_id_priv->id.state = IB_CM_REP_RCVD;
1889 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1890 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
1891 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1892 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1893 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1894 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1895 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1896 cm_id_priv->av.timeout =
1897 cm_ack_timeout(cm_id_priv->target_ack_delay,
1898 cm_id_priv->av.timeout - 1);
1899 cm_id_priv->alt_av.timeout =
1900 cm_ack_timeout(cm_id_priv->target_ack_delay,
1901 cm_id_priv->alt_av.timeout - 1);
1903 /* todo: handle peer_to_peer */
1905 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1906 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1907 if (!ret)
1908 list_add_tail(&work->list, &cm_id_priv->work_list);
1909 spin_unlock_irq(&cm_id_priv->lock);
1911 if (ret)
1912 cm_process_work(cm_id_priv, work);
1913 else
1914 cm_deref_id(cm_id_priv);
1915 return 0;
1917 error:
1918 cm_deref_id(cm_id_priv);
1919 return ret;
1922 static int cm_establish_handler(struct cm_work *work)
1924 struct cm_id_private *cm_id_priv;
1925 int ret;
1927 /* See comment in cm_establish about lookup. */
1928 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1929 if (!cm_id_priv)
1930 return -EINVAL;
1932 spin_lock_irq(&cm_id_priv->lock);
1933 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1934 spin_unlock_irq(&cm_id_priv->lock);
1935 goto out;
1938 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1939 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1940 if (!ret)
1941 list_add_tail(&work->list, &cm_id_priv->work_list);
1942 spin_unlock_irq(&cm_id_priv->lock);
1944 if (ret)
1945 cm_process_work(cm_id_priv, work);
1946 else
1947 cm_deref_id(cm_id_priv);
1948 return 0;
1949 out:
1950 cm_deref_id(cm_id_priv);
1951 return -EINVAL;
1954 static int cm_rtu_handler(struct cm_work *work)
1956 struct cm_id_private *cm_id_priv;
1957 struct cm_rtu_msg *rtu_msg;
1958 int ret;
1960 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1961 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1962 rtu_msg->local_comm_id);
1963 if (!cm_id_priv)
1964 return -EINVAL;
1966 work->cm_event.private_data = &rtu_msg->private_data;
1968 spin_lock_irq(&cm_id_priv->lock);
1969 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1970 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1971 spin_unlock_irq(&cm_id_priv->lock);
1972 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1973 counter[CM_RTU_COUNTER]);
1974 goto out;
1976 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1978 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1979 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1980 if (!ret)
1981 list_add_tail(&work->list, &cm_id_priv->work_list);
1982 spin_unlock_irq(&cm_id_priv->lock);
1984 if (ret)
1985 cm_process_work(cm_id_priv, work);
1986 else
1987 cm_deref_id(cm_id_priv);
1988 return 0;
1989 out:
1990 cm_deref_id(cm_id_priv);
1991 return -EINVAL;
1994 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1995 struct cm_id_private *cm_id_priv,
1996 const void *private_data,
1997 u8 private_data_len)
1999 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2000 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2001 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2002 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2003 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2005 if (private_data && private_data_len)
2006 memcpy(dreq_msg->private_data, private_data, private_data_len);
2009 int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2010 const void *private_data,
2011 u8 private_data_len)
2013 struct cm_id_private *cm_id_priv;
2014 struct ib_mad_send_buf *msg;
2015 unsigned long flags;
2016 int ret;
2018 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2019 return -EINVAL;
2021 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2022 spin_lock_irqsave(&cm_id_priv->lock, flags);
2023 if (cm_id->state != IB_CM_ESTABLISHED) {
2024 ret = -EINVAL;
2025 goto out;
2028 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2029 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2030 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2032 ret = cm_alloc_msg(cm_id_priv, &msg);
2033 if (ret) {
2034 cm_enter_timewait(cm_id_priv);
2035 goto out;
2038 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2039 private_data, private_data_len);
2040 msg->timeout_ms = cm_id_priv->timeout_ms;
2041 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2043 ret = ib_post_send_mad(msg, NULL);
2044 if (ret) {
2045 cm_enter_timewait(cm_id_priv);
2046 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2047 cm_free_msg(msg);
2048 return ret;
2051 cm_id->state = IB_CM_DREQ_SENT;
2052 cm_id_priv->msg = msg;
2053 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2054 return ret;
2056 EXPORT_SYMBOL(ib_send_cm_dreq);
2058 static void cm_format_drep(struct cm_drep_msg *drep_msg,
2059 struct cm_id_private *cm_id_priv,
2060 const void *private_data,
2061 u8 private_data_len)
2063 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2064 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2065 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2067 if (private_data && private_data_len)
2068 memcpy(drep_msg->private_data, private_data, private_data_len);
2071 int ib_send_cm_drep(struct ib_cm_id *cm_id,
2072 const void *private_data,
2073 u8 private_data_len)
2075 struct cm_id_private *cm_id_priv;
2076 struct ib_mad_send_buf *msg;
2077 unsigned long flags;
2078 void *data;
2079 int ret;
2081 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2082 return -EINVAL;
2084 data = cm_copy_private_data(private_data, private_data_len);
2085 if (IS_ERR(data))
2086 return PTR_ERR(data);
2088 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2089 spin_lock_irqsave(&cm_id_priv->lock, flags);
2090 if (cm_id->state != IB_CM_DREQ_RCVD) {
2091 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2092 kfree(data);
2093 return -EINVAL;
2096 cm_set_private_data(cm_id_priv, data, private_data_len);
2097 cm_enter_timewait(cm_id_priv);
2099 ret = cm_alloc_msg(cm_id_priv, &msg);
2100 if (ret)
2101 goto out;
2103 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2104 private_data, private_data_len);
2106 ret = ib_post_send_mad(msg, NULL);
2107 if (ret) {
2108 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2109 cm_free_msg(msg);
2110 return ret;
2113 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2114 return ret;
2116 EXPORT_SYMBOL(ib_send_cm_drep);
2118 static int cm_issue_drep(struct cm_port *port,
2119 struct ib_mad_recv_wc *mad_recv_wc)
2121 struct ib_mad_send_buf *msg = NULL;
2122 struct cm_dreq_msg *dreq_msg;
2123 struct cm_drep_msg *drep_msg;
2124 int ret;
2126 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2127 if (ret)
2128 return ret;
2130 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2131 drep_msg = (struct cm_drep_msg *) msg->mad;
2133 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2134 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2135 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2137 ret = ib_post_send_mad(msg, NULL);
2138 if (ret)
2139 cm_free_msg(msg);
2141 return ret;
2144 static int cm_dreq_handler(struct cm_work *work)
2146 struct cm_id_private *cm_id_priv;
2147 struct cm_dreq_msg *dreq_msg;
2148 struct ib_mad_send_buf *msg = NULL;
2149 int ret;
2151 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2152 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2153 dreq_msg->local_comm_id);
2154 if (!cm_id_priv) {
2155 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2156 counter[CM_DREQ_COUNTER]);
2157 cm_issue_drep(work->port, work->mad_recv_wc);
2158 return -EINVAL;
2161 work->cm_event.private_data = &dreq_msg->private_data;
2163 spin_lock_irq(&cm_id_priv->lock);
2164 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2165 goto unlock;
2167 switch (cm_id_priv->id.state) {
2168 case IB_CM_REP_SENT:
2169 case IB_CM_DREQ_SENT:
2170 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2171 break;
2172 case IB_CM_ESTABLISHED:
2173 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2174 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2175 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2176 break;
2177 case IB_CM_MRA_REP_RCVD:
2178 break;
2179 case IB_CM_TIMEWAIT:
2180 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2181 counter[CM_DREQ_COUNTER]);
2182 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2183 goto unlock;
2185 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2186 cm_id_priv->private_data,
2187 cm_id_priv->private_data_len);
2188 spin_unlock_irq(&cm_id_priv->lock);
2190 if (ib_post_send_mad(msg, NULL))
2191 cm_free_msg(msg);
2192 goto deref;
2193 case IB_CM_DREQ_RCVD:
2194 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2195 counter[CM_DREQ_COUNTER]);
2196 goto unlock;
2197 default:
2198 goto unlock;
2200 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2201 cm_id_priv->tid = dreq_msg->hdr.tid;
2202 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2203 if (!ret)
2204 list_add_tail(&work->list, &cm_id_priv->work_list);
2205 spin_unlock_irq(&cm_id_priv->lock);
2207 if (ret)
2208 cm_process_work(cm_id_priv, work);
2209 else
2210 cm_deref_id(cm_id_priv);
2211 return 0;
2213 unlock: spin_unlock_irq(&cm_id_priv->lock);
2214 deref: cm_deref_id(cm_id_priv);
2215 return -EINVAL;
2218 static int cm_drep_handler(struct cm_work *work)
2220 struct cm_id_private *cm_id_priv;
2221 struct cm_drep_msg *drep_msg;
2222 int ret;
2224 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2225 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2226 drep_msg->local_comm_id);
2227 if (!cm_id_priv)
2228 return -EINVAL;
2230 work->cm_event.private_data = &drep_msg->private_data;
2232 spin_lock_irq(&cm_id_priv->lock);
2233 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2234 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2235 spin_unlock_irq(&cm_id_priv->lock);
2236 goto out;
2238 cm_enter_timewait(cm_id_priv);
2240 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2241 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2242 if (!ret)
2243 list_add_tail(&work->list, &cm_id_priv->work_list);
2244 spin_unlock_irq(&cm_id_priv->lock);
2246 if (ret)
2247 cm_process_work(cm_id_priv, work);
2248 else
2249 cm_deref_id(cm_id_priv);
2250 return 0;
2251 out:
2252 cm_deref_id(cm_id_priv);
2253 return -EINVAL;
2256 int ib_send_cm_rej(struct ib_cm_id *cm_id,
2257 enum ib_cm_rej_reason reason,
2258 void *ari,
2259 u8 ari_length,
2260 const void *private_data,
2261 u8 private_data_len)
2263 struct cm_id_private *cm_id_priv;
2264 struct ib_mad_send_buf *msg;
2265 unsigned long flags;
2266 int ret;
2268 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2269 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2270 return -EINVAL;
2272 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2274 spin_lock_irqsave(&cm_id_priv->lock, flags);
2275 switch (cm_id->state) {
2276 case IB_CM_REQ_SENT:
2277 case IB_CM_MRA_REQ_RCVD:
2278 case IB_CM_REQ_RCVD:
2279 case IB_CM_MRA_REQ_SENT:
2280 case IB_CM_REP_RCVD:
2281 case IB_CM_MRA_REP_SENT:
2282 ret = cm_alloc_msg(cm_id_priv, &msg);
2283 if (!ret)
2284 cm_format_rej((struct cm_rej_msg *) msg->mad,
2285 cm_id_priv, reason, ari, ari_length,
2286 private_data, private_data_len);
2288 cm_reset_to_idle(cm_id_priv);
2289 break;
2290 case IB_CM_REP_SENT:
2291 case IB_CM_MRA_REP_RCVD:
2292 ret = cm_alloc_msg(cm_id_priv, &msg);
2293 if (!ret)
2294 cm_format_rej((struct cm_rej_msg *) msg->mad,
2295 cm_id_priv, reason, ari, ari_length,
2296 private_data, private_data_len);
2298 cm_enter_timewait(cm_id_priv);
2299 break;
2300 default:
2301 ret = -EINVAL;
2302 goto out;
2305 if (ret)
2306 goto out;
2308 ret = ib_post_send_mad(msg, NULL);
2309 if (ret)
2310 cm_free_msg(msg);
2312 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2313 return ret;
2315 EXPORT_SYMBOL(ib_send_cm_rej);
2317 static void cm_format_rej_event(struct cm_work *work)
2319 struct cm_rej_msg *rej_msg;
2320 struct ib_cm_rej_event_param *param;
2322 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2323 param = &work->cm_event.param.rej_rcvd;
2324 param->ari = rej_msg->ari;
2325 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2326 param->reason = __be16_to_cpu(rej_msg->reason);
2327 work->cm_event.private_data = &rej_msg->private_data;
2330 static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2332 struct cm_timewait_info *timewait_info;
2333 struct cm_id_private *cm_id_priv;
2334 __be32 remote_id;
2336 remote_id = rej_msg->local_comm_id;
2338 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2339 spin_lock_irq(&cm.lock);
2340 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2341 remote_id);
2342 if (!timewait_info) {
2343 spin_unlock_irq(&cm.lock);
2344 return NULL;
2346 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2347 (timewait_info->work.local_id ^
2348 cm.random_id_operand));
2349 if (cm_id_priv) {
2350 if (cm_id_priv->id.remote_id == remote_id)
2351 atomic_inc(&cm_id_priv->refcount);
2352 else
2353 cm_id_priv = NULL;
2355 spin_unlock_irq(&cm.lock);
2356 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2357 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2358 else
2359 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2361 return cm_id_priv;
2364 static int cm_rej_handler(struct cm_work *work)
2366 struct cm_id_private *cm_id_priv;
2367 struct cm_rej_msg *rej_msg;
2368 int ret;
2370 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2371 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2372 if (!cm_id_priv)
2373 return -EINVAL;
2375 cm_format_rej_event(work);
2377 spin_lock_irq(&cm_id_priv->lock);
2378 switch (cm_id_priv->id.state) {
2379 case IB_CM_REQ_SENT:
2380 case IB_CM_MRA_REQ_RCVD:
2381 case IB_CM_REP_SENT:
2382 case IB_CM_MRA_REP_RCVD:
2383 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2384 /* fall through */
2385 case IB_CM_REQ_RCVD:
2386 case IB_CM_MRA_REQ_SENT:
2387 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2388 cm_enter_timewait(cm_id_priv);
2389 else
2390 cm_reset_to_idle(cm_id_priv);
2391 break;
2392 case IB_CM_DREQ_SENT:
2393 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2394 /* fall through */
2395 case IB_CM_REP_RCVD:
2396 case IB_CM_MRA_REP_SENT:
2397 cm_enter_timewait(cm_id_priv);
2398 break;
2399 case IB_CM_ESTABLISHED:
2400 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2401 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2402 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2403 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2404 cm_id_priv->msg);
2405 cm_enter_timewait(cm_id_priv);
2406 break;
2408 /* fall through */
2409 default:
2410 spin_unlock_irq(&cm_id_priv->lock);
2411 ret = -EINVAL;
2412 goto out;
2415 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2416 if (!ret)
2417 list_add_tail(&work->list, &cm_id_priv->work_list);
2418 spin_unlock_irq(&cm_id_priv->lock);
2420 if (ret)
2421 cm_process_work(cm_id_priv, work);
2422 else
2423 cm_deref_id(cm_id_priv);
2424 return 0;
2425 out:
2426 cm_deref_id(cm_id_priv);
2427 return -EINVAL;
2430 int ib_send_cm_mra(struct ib_cm_id *cm_id,
2431 u8 service_timeout,
2432 const void *private_data,
2433 u8 private_data_len)
2435 struct cm_id_private *cm_id_priv;
2436 struct ib_mad_send_buf *msg;
2437 enum ib_cm_state cm_state;
2438 enum ib_cm_lap_state lap_state;
2439 enum cm_msg_response msg_response;
2440 void *data;
2441 unsigned long flags;
2442 int ret;
2444 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2445 return -EINVAL;
2447 data = cm_copy_private_data(private_data, private_data_len);
2448 if (IS_ERR(data))
2449 return PTR_ERR(data);
2451 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2453 spin_lock_irqsave(&cm_id_priv->lock, flags);
2454 switch(cm_id_priv->id.state) {
2455 case IB_CM_REQ_RCVD:
2456 cm_state = IB_CM_MRA_REQ_SENT;
2457 lap_state = cm_id->lap_state;
2458 msg_response = CM_MSG_RESPONSE_REQ;
2459 break;
2460 case IB_CM_REP_RCVD:
2461 cm_state = IB_CM_MRA_REP_SENT;
2462 lap_state = cm_id->lap_state;
2463 msg_response = CM_MSG_RESPONSE_REP;
2464 break;
2465 case IB_CM_ESTABLISHED:
2466 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2467 cm_state = cm_id->state;
2468 lap_state = IB_CM_MRA_LAP_SENT;
2469 msg_response = CM_MSG_RESPONSE_OTHER;
2470 break;
2472 default:
2473 ret = -EINVAL;
2474 goto error1;
2477 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2478 ret = cm_alloc_msg(cm_id_priv, &msg);
2479 if (ret)
2480 goto error1;
2482 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2483 msg_response, service_timeout,
2484 private_data, private_data_len);
2485 ret = ib_post_send_mad(msg, NULL);
2486 if (ret)
2487 goto error2;
2490 cm_id->state = cm_state;
2491 cm_id->lap_state = lap_state;
2492 cm_id_priv->service_timeout = service_timeout;
2493 cm_set_private_data(cm_id_priv, data, private_data_len);
2494 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2495 return 0;
2497 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2498 kfree(data);
2499 return ret;
2501 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2502 kfree(data);
2503 cm_free_msg(msg);
2504 return ret;
2506 EXPORT_SYMBOL(ib_send_cm_mra);
2508 static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2510 switch (cm_mra_get_msg_mraed(mra_msg)) {
2511 case CM_MSG_RESPONSE_REQ:
2512 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2513 case CM_MSG_RESPONSE_REP:
2514 case CM_MSG_RESPONSE_OTHER:
2515 return cm_acquire_id(mra_msg->remote_comm_id,
2516 mra_msg->local_comm_id);
2517 default:
2518 return NULL;
2522 static int cm_mra_handler(struct cm_work *work)
2524 struct cm_id_private *cm_id_priv;
2525 struct cm_mra_msg *mra_msg;
2526 int timeout, ret;
2528 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2529 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2530 if (!cm_id_priv)
2531 return -EINVAL;
2533 work->cm_event.private_data = &mra_msg->private_data;
2534 work->cm_event.param.mra_rcvd.service_timeout =
2535 cm_mra_get_service_timeout(mra_msg);
2536 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2537 cm_convert_to_ms(cm_id_priv->av.timeout);
2539 spin_lock_irq(&cm_id_priv->lock);
2540 switch (cm_id_priv->id.state) {
2541 case IB_CM_REQ_SENT:
2542 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2543 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2544 cm_id_priv->msg, timeout))
2545 goto out;
2546 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2547 break;
2548 case IB_CM_REP_SENT:
2549 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2550 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2551 cm_id_priv->msg, timeout))
2552 goto out;
2553 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2554 break;
2555 case IB_CM_ESTABLISHED:
2556 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2557 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2558 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2559 cm_id_priv->msg, timeout)) {
2560 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2561 atomic_long_inc(&work->port->
2562 counter_group[CM_RECV_DUPLICATES].
2563 counter[CM_MRA_COUNTER]);
2564 goto out;
2566 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2567 break;
2568 case IB_CM_MRA_REQ_RCVD:
2569 case IB_CM_MRA_REP_RCVD:
2570 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2571 counter[CM_MRA_COUNTER]);
2572 /* fall through */
2573 default:
2574 goto out;
2577 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2578 cm_id_priv->id.state;
2579 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2580 if (!ret)
2581 list_add_tail(&work->list, &cm_id_priv->work_list);
2582 spin_unlock_irq(&cm_id_priv->lock);
2584 if (ret)
2585 cm_process_work(cm_id_priv, work);
2586 else
2587 cm_deref_id(cm_id_priv);
2588 return 0;
2589 out:
2590 spin_unlock_irq(&cm_id_priv->lock);
2591 cm_deref_id(cm_id_priv);
2592 return -EINVAL;
2595 static void cm_format_lap(struct cm_lap_msg *lap_msg,
2596 struct cm_id_private *cm_id_priv,
2597 struct ib_sa_path_rec *alternate_path,
2598 const void *private_data,
2599 u8 private_data_len)
2601 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2602 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2603 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2604 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2605 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2606 /* todo: need remote CM response timeout */
2607 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2608 lap_msg->alt_local_lid = alternate_path->slid;
2609 lap_msg->alt_remote_lid = alternate_path->dlid;
2610 lap_msg->alt_local_gid = alternate_path->sgid;
2611 lap_msg->alt_remote_gid = alternate_path->dgid;
2612 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2613 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2614 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2615 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2616 cm_lap_set_sl(lap_msg, alternate_path->sl);
2617 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2618 cm_lap_set_local_ack_timeout(lap_msg,
2619 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2620 alternate_path->packet_life_time));
2622 if (private_data && private_data_len)
2623 memcpy(lap_msg->private_data, private_data, private_data_len);
2626 int ib_send_cm_lap(struct ib_cm_id *cm_id,
2627 struct ib_sa_path_rec *alternate_path,
2628 const void *private_data,
2629 u8 private_data_len)
2631 struct cm_id_private *cm_id_priv;
2632 struct ib_mad_send_buf *msg;
2633 unsigned long flags;
2634 int ret;
2636 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2637 return -EINVAL;
2639 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2640 spin_lock_irqsave(&cm_id_priv->lock, flags);
2641 if (cm_id->state != IB_CM_ESTABLISHED ||
2642 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2643 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2644 ret = -EINVAL;
2645 goto out;
2648 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2649 if (ret)
2650 goto out;
2651 cm_id_priv->alt_av.timeout =
2652 cm_ack_timeout(cm_id_priv->target_ack_delay,
2653 cm_id_priv->alt_av.timeout - 1);
2655 ret = cm_alloc_msg(cm_id_priv, &msg);
2656 if (ret)
2657 goto out;
2659 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2660 alternate_path, private_data, private_data_len);
2661 msg->timeout_ms = cm_id_priv->timeout_ms;
2662 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2664 ret = ib_post_send_mad(msg, NULL);
2665 if (ret) {
2666 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2667 cm_free_msg(msg);
2668 return ret;
2671 cm_id->lap_state = IB_CM_LAP_SENT;
2672 cm_id_priv->msg = msg;
2674 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2675 return ret;
2677 EXPORT_SYMBOL(ib_send_cm_lap);
2679 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2680 struct ib_sa_path_rec *path,
2681 struct cm_lap_msg *lap_msg)
2683 memset(path, 0, sizeof *path);
2684 path->dgid = lap_msg->alt_local_gid;
2685 path->sgid = lap_msg->alt_remote_gid;
2686 path->dlid = lap_msg->alt_local_lid;
2687 path->slid = lap_msg->alt_remote_lid;
2688 path->flow_label = cm_lap_get_flow_label(lap_msg);
2689 path->hop_limit = lap_msg->alt_hop_limit;
2690 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2691 path->reversible = 1;
2692 path->pkey = cm_id_priv->pkey;
2693 path->sl = cm_lap_get_sl(lap_msg);
2694 path->mtu_selector = IB_SA_EQ;
2695 path->mtu = cm_id_priv->path_mtu;
2696 path->rate_selector = IB_SA_EQ;
2697 path->rate = cm_lap_get_packet_rate(lap_msg);
2698 path->packet_life_time_selector = IB_SA_EQ;
2699 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2700 path->packet_life_time -= (path->packet_life_time > 0);
2703 static int cm_lap_handler(struct cm_work *work)
2705 struct cm_id_private *cm_id_priv;
2706 struct cm_lap_msg *lap_msg;
2707 struct ib_cm_lap_event_param *param;
2708 struct ib_mad_send_buf *msg = NULL;
2709 int ret;
2711 /* todo: verify LAP request and send reject APR if invalid. */
2712 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2713 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2714 lap_msg->local_comm_id);
2715 if (!cm_id_priv)
2716 return -EINVAL;
2718 param = &work->cm_event.param.lap_rcvd;
2719 param->alternate_path = &work->path[0];
2720 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2721 work->cm_event.private_data = &lap_msg->private_data;
2723 spin_lock_irq(&cm_id_priv->lock);
2724 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2725 goto unlock;
2727 switch (cm_id_priv->id.lap_state) {
2728 case IB_CM_LAP_UNINIT:
2729 case IB_CM_LAP_IDLE:
2730 break;
2731 case IB_CM_MRA_LAP_SENT:
2732 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2733 counter[CM_LAP_COUNTER]);
2734 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2735 goto unlock;
2737 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2738 CM_MSG_RESPONSE_OTHER,
2739 cm_id_priv->service_timeout,
2740 cm_id_priv->private_data,
2741 cm_id_priv->private_data_len);
2742 spin_unlock_irq(&cm_id_priv->lock);
2744 if (ib_post_send_mad(msg, NULL))
2745 cm_free_msg(msg);
2746 goto deref;
2747 case IB_CM_LAP_RCVD:
2748 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2749 counter[CM_LAP_COUNTER]);
2750 goto unlock;
2751 default:
2752 goto unlock;
2755 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2756 cm_id_priv->tid = lap_msg->hdr.tid;
2757 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2758 work->mad_recv_wc->recv_buf.grh,
2759 &cm_id_priv->av);
2760 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2761 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2762 if (!ret)
2763 list_add_tail(&work->list, &cm_id_priv->work_list);
2764 spin_unlock_irq(&cm_id_priv->lock);
2766 if (ret)
2767 cm_process_work(cm_id_priv, work);
2768 else
2769 cm_deref_id(cm_id_priv);
2770 return 0;
2772 unlock: spin_unlock_irq(&cm_id_priv->lock);
2773 deref: cm_deref_id(cm_id_priv);
2774 return -EINVAL;
2777 static void cm_format_apr(struct cm_apr_msg *apr_msg,
2778 struct cm_id_private *cm_id_priv,
2779 enum ib_cm_apr_status status,
2780 void *info,
2781 u8 info_length,
2782 const void *private_data,
2783 u8 private_data_len)
2785 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2786 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2787 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2788 apr_msg->ap_status = (u8) status;
2790 if (info && info_length) {
2791 apr_msg->info_length = info_length;
2792 memcpy(apr_msg->info, info, info_length);
2795 if (private_data && private_data_len)
2796 memcpy(apr_msg->private_data, private_data, private_data_len);
2799 int ib_send_cm_apr(struct ib_cm_id *cm_id,
2800 enum ib_cm_apr_status status,
2801 void *info,
2802 u8 info_length,
2803 const void *private_data,
2804 u8 private_data_len)
2806 struct cm_id_private *cm_id_priv;
2807 struct ib_mad_send_buf *msg;
2808 unsigned long flags;
2809 int ret;
2811 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2812 (info && info_length > IB_CM_APR_INFO_LENGTH))
2813 return -EINVAL;
2815 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2816 spin_lock_irqsave(&cm_id_priv->lock, flags);
2817 if (cm_id->state != IB_CM_ESTABLISHED ||
2818 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2819 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2820 ret = -EINVAL;
2821 goto out;
2824 ret = cm_alloc_msg(cm_id_priv, &msg);
2825 if (ret)
2826 goto out;
2828 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2829 info, info_length, private_data, private_data_len);
2830 ret = ib_post_send_mad(msg, NULL);
2831 if (ret) {
2832 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2833 cm_free_msg(msg);
2834 return ret;
2837 cm_id->lap_state = IB_CM_LAP_IDLE;
2838 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2839 return ret;
2841 EXPORT_SYMBOL(ib_send_cm_apr);
2843 static int cm_apr_handler(struct cm_work *work)
2845 struct cm_id_private *cm_id_priv;
2846 struct cm_apr_msg *apr_msg;
2847 int ret;
2849 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2850 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2851 apr_msg->local_comm_id);
2852 if (!cm_id_priv)
2853 return -EINVAL; /* Unmatched reply. */
2855 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2856 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2857 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2858 work->cm_event.private_data = &apr_msg->private_data;
2860 spin_lock_irq(&cm_id_priv->lock);
2861 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2862 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2863 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2864 spin_unlock_irq(&cm_id_priv->lock);
2865 goto out;
2867 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2868 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2869 cm_id_priv->msg = NULL;
2871 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2872 if (!ret)
2873 list_add_tail(&work->list, &cm_id_priv->work_list);
2874 spin_unlock_irq(&cm_id_priv->lock);
2876 if (ret)
2877 cm_process_work(cm_id_priv, work);
2878 else
2879 cm_deref_id(cm_id_priv);
2880 return 0;
2881 out:
2882 cm_deref_id(cm_id_priv);
2883 return -EINVAL;
2886 static int cm_timewait_handler(struct cm_work *work)
2888 struct cm_timewait_info *timewait_info;
2889 struct cm_id_private *cm_id_priv;
2890 int ret;
2892 timewait_info = (struct cm_timewait_info *)work;
2893 spin_lock_irq(&cm.lock);
2894 list_del(&timewait_info->list);
2895 spin_unlock_irq(&cm.lock);
2897 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2898 timewait_info->work.remote_id);
2899 if (!cm_id_priv)
2900 return -EINVAL;
2902 spin_lock_irq(&cm_id_priv->lock);
2903 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2904 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2905 spin_unlock_irq(&cm_id_priv->lock);
2906 goto out;
2908 cm_id_priv->id.state = IB_CM_IDLE;
2909 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2910 if (!ret)
2911 list_add_tail(&work->list, &cm_id_priv->work_list);
2912 spin_unlock_irq(&cm_id_priv->lock);
2914 if (ret)
2915 cm_process_work(cm_id_priv, work);
2916 else
2917 cm_deref_id(cm_id_priv);
2918 return 0;
2919 out:
2920 cm_deref_id(cm_id_priv);
2921 return -EINVAL;
2924 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2925 struct cm_id_private *cm_id_priv,
2926 struct ib_cm_sidr_req_param *param)
2928 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2929 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2930 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2931 sidr_req_msg->pkey = param->path->pkey;
2932 sidr_req_msg->service_id = param->service_id;
2934 if (param->private_data && param->private_data_len)
2935 memcpy(sidr_req_msg->private_data, param->private_data,
2936 param->private_data_len);
2939 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2940 struct ib_cm_sidr_req_param *param)
2942 struct cm_id_private *cm_id_priv;
2943 struct ib_mad_send_buf *msg;
2944 unsigned long flags;
2945 int ret;
2947 if (!param->path || (param->private_data &&
2948 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2949 return -EINVAL;
2951 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2952 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2953 if (ret)
2954 goto out;
2956 cm_id->service_id = param->service_id;
2957 cm_id->service_mask = ~cpu_to_be64(0);
2958 cm_id_priv->timeout_ms = param->timeout_ms;
2959 cm_id_priv->max_cm_retries = param->max_cm_retries;
2960 ret = cm_alloc_msg(cm_id_priv, &msg);
2961 if (ret)
2962 goto out;
2964 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2965 param);
2966 msg->timeout_ms = cm_id_priv->timeout_ms;
2967 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2969 spin_lock_irqsave(&cm_id_priv->lock, flags);
2970 if (cm_id->state == IB_CM_IDLE)
2971 ret = ib_post_send_mad(msg, NULL);
2972 else
2973 ret = -EINVAL;
2975 if (ret) {
2976 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2977 cm_free_msg(msg);
2978 goto out;
2980 cm_id->state = IB_CM_SIDR_REQ_SENT;
2981 cm_id_priv->msg = msg;
2982 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2983 out:
2984 return ret;
2986 EXPORT_SYMBOL(ib_send_cm_sidr_req);
2988 static void cm_format_sidr_req_event(struct cm_work *work,
2989 struct ib_cm_id *listen_id)
2991 struct cm_sidr_req_msg *sidr_req_msg;
2992 struct ib_cm_sidr_req_event_param *param;
2994 sidr_req_msg = (struct cm_sidr_req_msg *)
2995 work->mad_recv_wc->recv_buf.mad;
2996 param = &work->cm_event.param.sidr_req_rcvd;
2997 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2998 param->listen_id = listen_id;
2999 param->port = work->port->port_num;
3000 work->cm_event.private_data = &sidr_req_msg->private_data;
3003 static int cm_sidr_req_handler(struct cm_work *work)
3005 struct ib_cm_id *cm_id;
3006 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3007 struct cm_sidr_req_msg *sidr_req_msg;
3008 struct ib_wc *wc;
3010 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
3011 if (IS_ERR(cm_id))
3012 return PTR_ERR(cm_id);
3013 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3015 /* Record SGID/SLID and request ID for lookup. */
3016 sidr_req_msg = (struct cm_sidr_req_msg *)
3017 work->mad_recv_wc->recv_buf.mad;
3018 wc = work->mad_recv_wc->wc;
3019 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
3020 cm_id_priv->av.dgid.global.interface_id = 0;
3021 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3022 work->mad_recv_wc->recv_buf.grh,
3023 &cm_id_priv->av);
3024 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
3025 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3026 atomic_inc(&cm_id_priv->work_count);
3028 spin_lock_irq(&cm.lock);
3029 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3030 if (cur_cm_id_priv) {
3031 spin_unlock_irq(&cm.lock);
3032 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3033 counter[CM_SIDR_REQ_COUNTER]);
3034 goto out; /* Duplicate message. */
3036 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3037 cur_cm_id_priv = cm_find_listen(cm_id->device,
3038 sidr_req_msg->service_id,
3039 sidr_req_msg->private_data);
3040 if (!cur_cm_id_priv) {
3041 spin_unlock_irq(&cm.lock);
3042 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3043 goto out; /* No match. */
3045 atomic_inc(&cur_cm_id_priv->refcount);
3046 atomic_inc(&cm_id_priv->refcount);
3047 spin_unlock_irq(&cm.lock);
3049 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3050 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3051 cm_id_priv->id.service_id = sidr_req_msg->service_id;
3052 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3054 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3055 cm_process_work(cm_id_priv, work);
3056 cm_deref_id(cur_cm_id_priv);
3057 return 0;
3058 out:
3059 ib_destroy_cm_id(&cm_id_priv->id);
3060 return -EINVAL;
3063 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3064 struct cm_id_private *cm_id_priv,
3065 struct ib_cm_sidr_rep_param *param)
3067 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3068 cm_id_priv->tid);
3069 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3070 sidr_rep_msg->status = param->status;
3071 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3072 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3073 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3075 if (param->info && param->info_length)
3076 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3078 if (param->private_data && param->private_data_len)
3079 memcpy(sidr_rep_msg->private_data, param->private_data,
3080 param->private_data_len);
3083 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3084 struct ib_cm_sidr_rep_param *param)
3086 struct cm_id_private *cm_id_priv;
3087 struct ib_mad_send_buf *msg;
3088 unsigned long flags;
3089 int ret;
3091 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3092 (param->private_data &&
3093 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3094 return -EINVAL;
3096 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3097 spin_lock_irqsave(&cm_id_priv->lock, flags);
3098 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3099 ret = -EINVAL;
3100 goto error;
3103 ret = cm_alloc_msg(cm_id_priv, &msg);
3104 if (ret)
3105 goto error;
3107 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3108 param);
3109 ret = ib_post_send_mad(msg, NULL);
3110 if (ret) {
3111 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3112 cm_free_msg(msg);
3113 return ret;
3115 cm_id->state = IB_CM_IDLE;
3116 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3118 spin_lock_irqsave(&cm.lock, flags);
3119 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3120 spin_unlock_irqrestore(&cm.lock, flags);
3121 return 0;
3123 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3124 return ret;
3126 EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3128 static void cm_format_sidr_rep_event(struct cm_work *work)
3130 struct cm_sidr_rep_msg *sidr_rep_msg;
3131 struct ib_cm_sidr_rep_event_param *param;
3133 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3134 work->mad_recv_wc->recv_buf.mad;
3135 param = &work->cm_event.param.sidr_rep_rcvd;
3136 param->status = sidr_rep_msg->status;
3137 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3138 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3139 param->info = &sidr_rep_msg->info;
3140 param->info_len = sidr_rep_msg->info_length;
3141 work->cm_event.private_data = &sidr_rep_msg->private_data;
3144 static int cm_sidr_rep_handler(struct cm_work *work)
3146 struct cm_sidr_rep_msg *sidr_rep_msg;
3147 struct cm_id_private *cm_id_priv;
3149 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3150 work->mad_recv_wc->recv_buf.mad;
3151 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3152 if (!cm_id_priv)
3153 return -EINVAL; /* Unmatched reply. */
3155 spin_lock_irq(&cm_id_priv->lock);
3156 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3157 spin_unlock_irq(&cm_id_priv->lock);
3158 goto out;
3160 cm_id_priv->id.state = IB_CM_IDLE;
3161 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3162 spin_unlock_irq(&cm_id_priv->lock);
3164 cm_format_sidr_rep_event(work);
3165 cm_process_work(cm_id_priv, work);
3166 return 0;
3167 out:
3168 cm_deref_id(cm_id_priv);
3169 return -EINVAL;
3172 static void cm_process_send_error(struct ib_mad_send_buf *msg,
3173 enum ib_wc_status wc_status)
3175 struct cm_id_private *cm_id_priv;
3176 struct ib_cm_event cm_event;
3177 enum ib_cm_state state;
3178 int ret;
3180 memset(&cm_event, 0, sizeof cm_event);
3181 cm_id_priv = msg->context[0];
3183 /* Discard old sends or ones without a response. */
3184 spin_lock_irq(&cm_id_priv->lock);
3185 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3186 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3187 goto discard;
3189 switch (state) {
3190 case IB_CM_REQ_SENT:
3191 case IB_CM_MRA_REQ_RCVD:
3192 cm_reset_to_idle(cm_id_priv);
3193 cm_event.event = IB_CM_REQ_ERROR;
3194 break;
3195 case IB_CM_REP_SENT:
3196 case IB_CM_MRA_REP_RCVD:
3197 cm_reset_to_idle(cm_id_priv);
3198 cm_event.event = IB_CM_REP_ERROR;
3199 break;
3200 case IB_CM_DREQ_SENT:
3201 cm_enter_timewait(cm_id_priv);
3202 cm_event.event = IB_CM_DREQ_ERROR;
3203 break;
3204 case IB_CM_SIDR_REQ_SENT:
3205 cm_id_priv->id.state = IB_CM_IDLE;
3206 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3207 break;
3208 default:
3209 goto discard;
3211 spin_unlock_irq(&cm_id_priv->lock);
3212 cm_event.param.send_status = wc_status;
3214 /* No other events can occur on the cm_id at this point. */
3215 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3216 cm_free_msg(msg);
3217 if (ret)
3218 ib_destroy_cm_id(&cm_id_priv->id);
3219 return;
3220 discard:
3221 spin_unlock_irq(&cm_id_priv->lock);
3222 cm_free_msg(msg);
3225 static void cm_send_handler(struct ib_mad_agent *mad_agent,
3226 struct ib_mad_send_wc *mad_send_wc)
3228 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3229 struct cm_port *port;
3230 u16 attr_index;
3232 port = mad_agent->context;
3233 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3234 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3237 * If the send was in response to a received message (context[0] is not
3238 * set to a cm_id), and is not a REJ, then it is a send that was
3239 * manually retried.
3241 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3242 msg->retries = 1;
3244 atomic_long_add(1 + msg->retries,
3245 &port->counter_group[CM_XMIT].counter[attr_index]);
3246 if (msg->retries)
3247 atomic_long_add(msg->retries,
3248 &port->counter_group[CM_XMIT_RETRIES].
3249 counter[attr_index]);
3251 switch (mad_send_wc->status) {
3252 case IB_WC_SUCCESS:
3253 case IB_WC_WR_FLUSH_ERR:
3254 cm_free_msg(msg);
3255 break;
3256 default:
3257 if (msg->context[0] && msg->context[1])
3258 cm_process_send_error(msg, mad_send_wc->status);
3259 else
3260 cm_free_msg(msg);
3261 break;
3265 static void cm_work_handler(struct work_struct *_work)
3267 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3268 int ret;
3270 switch (work->cm_event.event) {
3271 case IB_CM_REQ_RECEIVED:
3272 ret = cm_req_handler(work);
3273 break;
3274 case IB_CM_MRA_RECEIVED:
3275 ret = cm_mra_handler(work);
3276 break;
3277 case IB_CM_REJ_RECEIVED:
3278 ret = cm_rej_handler(work);
3279 break;
3280 case IB_CM_REP_RECEIVED:
3281 ret = cm_rep_handler(work);
3282 break;
3283 case IB_CM_RTU_RECEIVED:
3284 ret = cm_rtu_handler(work);
3285 break;
3286 case IB_CM_USER_ESTABLISHED:
3287 ret = cm_establish_handler(work);
3288 break;
3289 case IB_CM_DREQ_RECEIVED:
3290 ret = cm_dreq_handler(work);
3291 break;
3292 case IB_CM_DREP_RECEIVED:
3293 ret = cm_drep_handler(work);
3294 break;
3295 case IB_CM_SIDR_REQ_RECEIVED:
3296 ret = cm_sidr_req_handler(work);
3297 break;
3298 case IB_CM_SIDR_REP_RECEIVED:
3299 ret = cm_sidr_rep_handler(work);
3300 break;
3301 case IB_CM_LAP_RECEIVED:
3302 ret = cm_lap_handler(work);
3303 break;
3304 case IB_CM_APR_RECEIVED:
3305 ret = cm_apr_handler(work);
3306 break;
3307 case IB_CM_TIMEWAIT_EXIT:
3308 ret = cm_timewait_handler(work);
3309 break;
3310 default:
3311 ret = -EINVAL;
3312 break;
3314 if (ret)
3315 cm_free_work(work);
3318 static int cm_establish(struct ib_cm_id *cm_id)
3320 struct cm_id_private *cm_id_priv;
3321 struct cm_work *work;
3322 unsigned long flags;
3323 int ret = 0;
3325 work = kmalloc(sizeof *work, GFP_ATOMIC);
3326 if (!work)
3327 return -ENOMEM;
3329 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3330 spin_lock_irqsave(&cm_id_priv->lock, flags);
3331 switch (cm_id->state)
3333 case IB_CM_REP_SENT:
3334 case IB_CM_MRA_REP_RCVD:
3335 cm_id->state = IB_CM_ESTABLISHED;
3336 break;
3337 case IB_CM_ESTABLISHED:
3338 ret = -EISCONN;
3339 break;
3340 default:
3341 ret = -EINVAL;
3342 break;
3344 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3346 if (ret) {
3347 kfree(work);
3348 goto out;
3352 * The CM worker thread may try to destroy the cm_id before it
3353 * can execute this work item. To prevent potential deadlock,
3354 * we need to find the cm_id once we're in the context of the
3355 * worker thread, rather than holding a reference on it.
3357 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3358 work->local_id = cm_id->local_id;
3359 work->remote_id = cm_id->remote_id;
3360 work->mad_recv_wc = NULL;
3361 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3362 queue_delayed_work(cm.wq, &work->work, 0);
3363 out:
3364 return ret;
3367 static int cm_migrate(struct ib_cm_id *cm_id)
3369 struct cm_id_private *cm_id_priv;
3370 unsigned long flags;
3371 int ret = 0;
3373 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3374 spin_lock_irqsave(&cm_id_priv->lock, flags);
3375 if (cm_id->state == IB_CM_ESTABLISHED &&
3376 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3377 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3378 cm_id->lap_state = IB_CM_LAP_IDLE;
3379 cm_id_priv->av = cm_id_priv->alt_av;
3380 } else
3381 ret = -EINVAL;
3382 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3384 return ret;
3387 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3389 int ret;
3391 switch (event) {
3392 case IB_EVENT_COMM_EST:
3393 ret = cm_establish(cm_id);
3394 break;
3395 case IB_EVENT_PATH_MIG:
3396 ret = cm_migrate(cm_id);
3397 break;
3398 default:
3399 ret = -EINVAL;
3401 return ret;
3403 EXPORT_SYMBOL(ib_cm_notify);
3405 static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3406 struct ib_mad_recv_wc *mad_recv_wc)
3408 struct cm_port *port = mad_agent->context;
3409 struct cm_work *work;
3410 enum ib_cm_event_type event;
3411 u16 attr_id;
3412 int paths = 0;
3414 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3415 case CM_REQ_ATTR_ID:
3416 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3417 alt_local_lid != 0);
3418 event = IB_CM_REQ_RECEIVED;
3419 break;
3420 case CM_MRA_ATTR_ID:
3421 event = IB_CM_MRA_RECEIVED;
3422 break;
3423 case CM_REJ_ATTR_ID:
3424 event = IB_CM_REJ_RECEIVED;
3425 break;
3426 case CM_REP_ATTR_ID:
3427 event = IB_CM_REP_RECEIVED;
3428 break;
3429 case CM_RTU_ATTR_ID:
3430 event = IB_CM_RTU_RECEIVED;
3431 break;
3432 case CM_DREQ_ATTR_ID:
3433 event = IB_CM_DREQ_RECEIVED;
3434 break;
3435 case CM_DREP_ATTR_ID:
3436 event = IB_CM_DREP_RECEIVED;
3437 break;
3438 case CM_SIDR_REQ_ATTR_ID:
3439 event = IB_CM_SIDR_REQ_RECEIVED;
3440 break;
3441 case CM_SIDR_REP_ATTR_ID:
3442 event = IB_CM_SIDR_REP_RECEIVED;
3443 break;
3444 case CM_LAP_ATTR_ID:
3445 paths = 1;
3446 event = IB_CM_LAP_RECEIVED;
3447 break;
3448 case CM_APR_ATTR_ID:
3449 event = IB_CM_APR_RECEIVED;
3450 break;
3451 default:
3452 ib_free_recv_mad(mad_recv_wc);
3453 return;
3456 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3457 atomic_long_inc(&port->counter_group[CM_RECV].
3458 counter[attr_id - CM_ATTR_ID_OFFSET]);
3460 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3461 GFP_KERNEL);
3462 if (!work) {
3463 ib_free_recv_mad(mad_recv_wc);
3464 return;
3467 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3468 work->cm_event.event = event;
3469 work->mad_recv_wc = mad_recv_wc;
3470 work->port = port;
3471 queue_delayed_work(cm.wq, &work->work, 0);
3474 static int cm_init_qp_init_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_SENT:
3484 case IB_CM_MRA_REQ_RCVD:
3485 case IB_CM_REQ_RCVD:
3486 case IB_CM_MRA_REQ_SENT:
3487 case IB_CM_REP_RCVD:
3488 case IB_CM_MRA_REP_SENT:
3489 case IB_CM_REP_SENT:
3490 case IB_CM_MRA_REP_RCVD:
3491 case IB_CM_ESTABLISHED:
3492 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3493 IB_QP_PKEY_INDEX | IB_QP_PORT;
3494 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3495 if (cm_id_priv->responder_resources)
3496 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3497 IB_ACCESS_REMOTE_ATOMIC;
3498 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3499 qp_attr->port_num = cm_id_priv->av.port->port_num;
3500 ret = 0;
3501 break;
3502 default:
3503 ret = -EINVAL;
3504 break;
3506 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3507 return ret;
3510 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3511 struct ib_qp_attr *qp_attr,
3512 int *qp_attr_mask)
3514 unsigned long flags;
3515 int ret;
3517 spin_lock_irqsave(&cm_id_priv->lock, flags);
3518 switch (cm_id_priv->id.state) {
3519 case IB_CM_REQ_RCVD:
3520 case IB_CM_MRA_REQ_SENT:
3521 case IB_CM_REP_RCVD:
3522 case IB_CM_MRA_REP_SENT:
3523 case IB_CM_REP_SENT:
3524 case IB_CM_MRA_REP_RCVD:
3525 case IB_CM_ESTABLISHED:
3526 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3527 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3528 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3529 if (!cm_id_priv->av.valid) {
3530 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3531 return -EINVAL;
3533 if (cm_id_priv->av.ah_attr.vlan_id != 0xffff) {
3534 qp_attr->vlan_id = cm_id_priv->av.ah_attr.vlan_id;
3535 *qp_attr_mask |= IB_QP_VID;
3537 if (!is_zero_ether_addr(cm_id_priv->av.smac)) {
3538 memcpy(qp_attr->smac, cm_id_priv->av.smac,
3539 sizeof(qp_attr->smac));
3540 *qp_attr_mask |= IB_QP_SMAC;
3542 if (cm_id_priv->alt_av.valid) {
3543 if (cm_id_priv->alt_av.ah_attr.vlan_id != 0xffff) {
3544 qp_attr->alt_vlan_id =
3545 cm_id_priv->alt_av.ah_attr.vlan_id;
3546 *qp_attr_mask |= IB_QP_ALT_VID;
3548 if (!is_zero_ether_addr(cm_id_priv->alt_av.smac)) {
3549 memcpy(qp_attr->alt_smac,
3550 cm_id_priv->alt_av.smac,
3551 sizeof(qp_attr->alt_smac));
3552 *qp_attr_mask |= IB_QP_ALT_SMAC;
3555 qp_attr->path_mtu = cm_id_priv->path_mtu;
3556 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3557 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3558 if (cm_id_priv->qp_type == IB_QPT_RC ||
3559 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
3560 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3561 IB_QP_MIN_RNR_TIMER;
3562 qp_attr->max_dest_rd_atomic =
3563 cm_id_priv->responder_resources;
3564 qp_attr->min_rnr_timer = 0;
3566 if (cm_id_priv->alt_av.ah_attr.dlid) {
3567 *qp_attr_mask |= IB_QP_ALT_PATH;
3568 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3569 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3570 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3571 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3573 ret = 0;
3574 break;
3575 default:
3576 ret = -EINVAL;
3577 break;
3579 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3580 return ret;
3583 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3584 struct ib_qp_attr *qp_attr,
3585 int *qp_attr_mask)
3587 unsigned long flags;
3588 int ret;
3590 spin_lock_irqsave(&cm_id_priv->lock, flags);
3591 switch (cm_id_priv->id.state) {
3592 /* Allow transition to RTS before sending REP */
3593 case IB_CM_REQ_RCVD:
3594 case IB_CM_MRA_REQ_SENT:
3596 case IB_CM_REP_RCVD:
3597 case IB_CM_MRA_REP_SENT:
3598 case IB_CM_REP_SENT:
3599 case IB_CM_MRA_REP_RCVD:
3600 case IB_CM_ESTABLISHED:
3601 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3602 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3603 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3604 switch (cm_id_priv->qp_type) {
3605 case IB_QPT_RC:
3606 case IB_QPT_XRC_INI:
3607 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3608 IB_QP_MAX_QP_RD_ATOMIC;
3609 qp_attr->retry_cnt = cm_id_priv->retry_count;
3610 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3611 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3612 /* fall through */
3613 case IB_QPT_XRC_TGT:
3614 *qp_attr_mask |= IB_QP_TIMEOUT;
3615 qp_attr->timeout = cm_id_priv->av.timeout;
3616 break;
3617 default:
3618 break;
3620 if (cm_id_priv->alt_av.ah_attr.dlid) {
3621 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3622 qp_attr->path_mig_state = IB_MIG_REARM;
3624 } else {
3625 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3626 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3627 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3628 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3629 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3630 qp_attr->path_mig_state = IB_MIG_REARM;
3632 ret = 0;
3633 break;
3634 default:
3635 ret = -EINVAL;
3636 break;
3638 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3639 return ret;
3642 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3643 struct ib_qp_attr *qp_attr,
3644 int *qp_attr_mask)
3646 struct cm_id_private *cm_id_priv;
3647 int ret;
3649 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3650 switch (qp_attr->qp_state) {
3651 case IB_QPS_INIT:
3652 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3653 break;
3654 case IB_QPS_RTR:
3655 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3656 break;
3657 case IB_QPS_RTS:
3658 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3659 break;
3660 default:
3661 ret = -EINVAL;
3662 break;
3664 return ret;
3666 EXPORT_SYMBOL(ib_cm_init_qp_attr);
3668 static void cm_get_ack_delay(struct cm_device *cm_dev)
3670 struct ib_device_attr attr;
3672 if (ib_query_device(cm_dev->ib_device, &attr))
3673 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3674 else
3675 cm_dev->ack_delay = attr.local_ca_ack_delay;
3678 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3679 char *buf)
3681 struct cm_counter_group *group;
3682 struct cm_counter_attribute *cm_attr;
3684 group = container_of(obj, struct cm_counter_group, obj);
3685 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3687 return sprintf(buf, "%ld\n",
3688 atomic_long_read(&group->counter[cm_attr->index]));
3691 static const struct sysfs_ops cm_counter_ops = {
3692 .show = cm_show_counter
3695 static struct kobj_type cm_counter_obj_type = {
3696 .sysfs_ops = &cm_counter_ops,
3697 .default_attrs = cm_counter_default_attrs
3700 static void cm_release_port_obj(struct kobject *obj)
3702 struct cm_port *cm_port;
3704 cm_port = container_of(obj, struct cm_port, port_obj);
3705 kfree(cm_port);
3708 static struct kobj_type cm_port_obj_type = {
3709 .release = cm_release_port_obj
3712 static char *cm_devnode(struct device *dev, umode_t *mode)
3714 if (mode)
3715 *mode = 0666;
3716 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3719 struct class cm_class = {
3720 .owner = THIS_MODULE,
3721 .name = "infiniband_cm",
3722 .devnode = cm_devnode,
3724 EXPORT_SYMBOL(cm_class);
3726 static int cm_create_port_fs(struct cm_port *port)
3728 int i, ret;
3730 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3731 &port->cm_dev->device->kobj,
3732 "%d", port->port_num);
3733 if (ret) {
3734 kfree(port);
3735 return ret;
3738 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3739 ret = kobject_init_and_add(&port->counter_group[i].obj,
3740 &cm_counter_obj_type,
3741 &port->port_obj,
3742 "%s", counter_group_names[i]);
3743 if (ret)
3744 goto error;
3747 return 0;
3749 error:
3750 while (i--)
3751 kobject_put(&port->counter_group[i].obj);
3752 kobject_put(&port->port_obj);
3753 return ret;
3757 static void cm_remove_port_fs(struct cm_port *port)
3759 int i;
3761 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3762 kobject_put(&port->counter_group[i].obj);
3764 kobject_put(&port->port_obj);
3767 static void cm_add_one(struct ib_device *ib_device)
3769 struct cm_device *cm_dev;
3770 struct cm_port *port;
3771 struct ib_mad_reg_req reg_req = {
3772 .mgmt_class = IB_MGMT_CLASS_CM,
3773 .mgmt_class_version = IB_CM_CLASS_VERSION
3775 struct ib_port_modify port_modify = {
3776 .set_port_cap_mask = IB_PORT_CM_SUP
3778 unsigned long flags;
3779 int ret;
3780 u8 i;
3782 if (rdma_node_get_transport(ib_device->node_type) != RDMA_TRANSPORT_IB)
3783 return;
3785 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3786 ib_device->phys_port_cnt, GFP_KERNEL);
3787 if (!cm_dev)
3788 return;
3790 cm_dev->ib_device = ib_device;
3791 cm_get_ack_delay(cm_dev);
3793 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3794 MKDEV(0, 0), NULL,
3795 "%s", ib_device->name);
3796 if (IS_ERR(cm_dev->device)) {
3797 kfree(cm_dev);
3798 return;
3801 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3802 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3803 port = kzalloc(sizeof *port, GFP_KERNEL);
3804 if (!port)
3805 goto error1;
3807 cm_dev->port[i-1] = port;
3808 port->cm_dev = cm_dev;
3809 port->port_num = i;
3811 ret = cm_create_port_fs(port);
3812 if (ret)
3813 goto error1;
3815 port->mad_agent = ib_register_mad_agent(ib_device, i,
3816 IB_QPT_GSI,
3817 &reg_req,
3819 cm_send_handler,
3820 cm_recv_handler,
3821 port);
3822 if (IS_ERR(port->mad_agent))
3823 goto error2;
3825 ret = ib_modify_port(ib_device, i, 0, &port_modify);
3826 if (ret)
3827 goto error3;
3829 ib_set_client_data(ib_device, &cm_client, cm_dev);
3831 write_lock_irqsave(&cm.device_lock, flags);
3832 list_add_tail(&cm_dev->list, &cm.device_list);
3833 write_unlock_irqrestore(&cm.device_lock, flags);
3834 return;
3836 error3:
3837 ib_unregister_mad_agent(port->mad_agent);
3838 error2:
3839 cm_remove_port_fs(port);
3840 error1:
3841 port_modify.set_port_cap_mask = 0;
3842 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3843 while (--i) {
3844 port = cm_dev->port[i-1];
3845 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3846 ib_unregister_mad_agent(port->mad_agent);
3847 cm_remove_port_fs(port);
3849 device_unregister(cm_dev->device);
3850 kfree(cm_dev);
3853 static void cm_remove_one(struct ib_device *ib_device)
3855 struct cm_device *cm_dev;
3856 struct cm_port *port;
3857 struct ib_port_modify port_modify = {
3858 .clr_port_cap_mask = IB_PORT_CM_SUP
3860 unsigned long flags;
3861 int i;
3863 cm_dev = ib_get_client_data(ib_device, &cm_client);
3864 if (!cm_dev)
3865 return;
3867 write_lock_irqsave(&cm.device_lock, flags);
3868 list_del(&cm_dev->list);
3869 write_unlock_irqrestore(&cm.device_lock, flags);
3871 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3872 port = cm_dev->port[i-1];
3873 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3874 ib_unregister_mad_agent(port->mad_agent);
3875 flush_workqueue(cm.wq);
3876 cm_remove_port_fs(port);
3878 device_unregister(cm_dev->device);
3879 kfree(cm_dev);
3882 static int __init ib_cm_init(void)
3884 int ret;
3886 memset(&cm, 0, sizeof cm);
3887 INIT_LIST_HEAD(&cm.device_list);
3888 rwlock_init(&cm.device_lock);
3889 spin_lock_init(&cm.lock);
3890 cm.listen_service_table = RB_ROOT;
3891 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3892 cm.remote_id_table = RB_ROOT;
3893 cm.remote_qp_table = RB_ROOT;
3894 cm.remote_sidr_table = RB_ROOT;
3895 idr_init(&cm.local_id_table);
3896 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3897 INIT_LIST_HEAD(&cm.timewait_list);
3899 ret = class_register(&cm_class);
3900 if (ret) {
3901 ret = -ENOMEM;
3902 goto error1;
3905 cm.wq = create_workqueue("ib_cm");
3906 if (!cm.wq) {
3907 ret = -ENOMEM;
3908 goto error2;
3911 ret = ib_register_client(&cm_client);
3912 if (ret)
3913 goto error3;
3915 return 0;
3916 error3:
3917 destroy_workqueue(cm.wq);
3918 error2:
3919 class_unregister(&cm_class);
3920 error1:
3921 idr_destroy(&cm.local_id_table);
3922 return ret;
3925 static void __exit ib_cm_cleanup(void)
3927 struct cm_timewait_info *timewait_info, *tmp;
3929 spin_lock_irq(&cm.lock);
3930 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3931 cancel_delayed_work(&timewait_info->work.work);
3932 spin_unlock_irq(&cm.lock);
3934 ib_unregister_client(&cm_client);
3935 destroy_workqueue(cm.wq);
3937 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3938 list_del(&timewait_info->list);
3939 kfree(timewait_info);
3942 class_unregister(&cm_class);
3943 idr_destroy(&cm.local_id_table);
3946 module_init(ib_cm_init);
3947 module_exit(ib_cm_cleanup);