Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1 | /* |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2 | * Copyright (c) 2004-2007 Intel Corporation. All rights reserved. |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3 | * Copyright (c) 2004 Topspin Corporation. All rights reserved. |
| 4 | * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved. |
| 5 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
| 6 | * |
| 7 | * This software is available to you under a choice of one of two |
| 8 | * licenses. You may choose to be licensed under the terms of the GNU |
| 9 | * General Public License (GPL) Version 2, available from the file |
| 10 | * COPYING in the main directory of this source tree, or the |
| 11 | * OpenIB.org BSD license below: |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or |
| 14 | * without modification, are permitted provided that the following |
| 15 | * conditions are met: |
| 16 | * |
| 17 | * - Redistributions of source code must retain the above |
| 18 | * copyright notice, this list of conditions and the following |
| 19 | * disclaimer. |
| 20 | * |
| 21 | * - Redistributions in binary form must reproduce the above |
| 22 | * copyright notice, this list of conditions and the following |
| 23 | * disclaimer in the documentation and/or other materials |
| 24 | * provided with the distribution. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 30 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 31 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 | * SOFTWARE. |
| 34 | * |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 35 | * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $ |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 36 | */ |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 37 | |
| 38 | #include <linux/completion.h> |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 39 | #include <linux/dma-mapping.h> |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 40 | #include <linux/device.h> |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 41 | #include <linux/err.h> |
| 42 | #include <linux/idr.h> |
| 43 | #include <linux/interrupt.h> |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 44 | #include <linux/random.h> |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 45 | #include <linux/rbtree.h> |
| 46 | #include <linux/spinlock.h> |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 47 | #include <linux/sysfs.h> |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 48 | #include <linux/workqueue.h> |
| 49 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 50 | #include <rdma/ib_cache.h> |
| 51 | #include <rdma/ib_cm.h> |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 52 | #include "cm_msgs.h" |
| 53 | |
| 54 | MODULE_AUTHOR("Sean Hefty"); |
| 55 | MODULE_DESCRIPTION("InfiniBand CM"); |
| 56 | MODULE_LICENSE("Dual BSD/GPL"); |
| 57 | |
| 58 | static void cm_add_one(struct ib_device *device); |
| 59 | static void cm_remove_one(struct ib_device *device); |
| 60 | |
| 61 | static struct ib_client cm_client = { |
| 62 | .name = "cm", |
| 63 | .add = cm_add_one, |
| 64 | .remove = cm_remove_one |
| 65 | }; |
| 66 | |
| 67 | static struct ib_cm { |
| 68 | spinlock_t lock; |
| 69 | struct list_head device_list; |
| 70 | rwlock_t device_lock; |
| 71 | struct rb_root listen_service_table; |
| 72 | u64 listen_service_id; |
| 73 | /* struct rb_root peer_service_table; todo: fix peer to peer */ |
| 74 | struct rb_root remote_qp_table; |
| 75 | struct rb_root remote_id_table; |
| 76 | struct rb_root remote_sidr_table; |
| 77 | struct idr local_id_table; |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 78 | __be32 random_id_operand; |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 79 | struct list_head timewait_list; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 80 | struct workqueue_struct *wq; |
| 81 | } cm; |
| 82 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 83 | /* Counter indexes ordered by attribute ID */ |
| 84 | enum { |
| 85 | CM_REQ_COUNTER, |
| 86 | CM_MRA_COUNTER, |
| 87 | CM_REJ_COUNTER, |
| 88 | CM_REP_COUNTER, |
| 89 | CM_RTU_COUNTER, |
| 90 | CM_DREQ_COUNTER, |
| 91 | CM_DREP_COUNTER, |
| 92 | CM_SIDR_REQ_COUNTER, |
| 93 | CM_SIDR_REP_COUNTER, |
| 94 | CM_LAP_COUNTER, |
| 95 | CM_APR_COUNTER, |
| 96 | CM_ATTR_COUNT, |
| 97 | CM_ATTR_ID_OFFSET = 0x0010, |
| 98 | }; |
| 99 | |
| 100 | enum { |
| 101 | CM_XMIT, |
| 102 | CM_XMIT_RETRIES, |
| 103 | CM_RECV, |
| 104 | CM_RECV_DUPLICATES, |
| 105 | CM_COUNTER_GROUPS |
| 106 | }; |
| 107 | |
| 108 | static char const counter_group_names[CM_COUNTER_GROUPS] |
| 109 | [sizeof("cm_rx_duplicates")] = { |
| 110 | "cm_tx_msgs", "cm_tx_retries", |
| 111 | "cm_rx_msgs", "cm_rx_duplicates" |
| 112 | }; |
| 113 | |
| 114 | struct cm_counter_group { |
| 115 | struct kobject obj; |
| 116 | atomic_long_t counter[CM_ATTR_COUNT]; |
| 117 | }; |
| 118 | |
| 119 | struct cm_counter_attribute { |
| 120 | struct attribute attr; |
| 121 | int index; |
| 122 | }; |
| 123 | |
| 124 | #define CM_COUNTER_ATTR(_name, _index) \ |
| 125 | struct cm_counter_attribute cm_##_name##_counter_attr = { \ |
| 126 | .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \ |
| 127 | .index = _index \ |
| 128 | } |
| 129 | |
| 130 | static CM_COUNTER_ATTR(req, CM_REQ_COUNTER); |
| 131 | static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER); |
| 132 | static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER); |
| 133 | static CM_COUNTER_ATTR(rep, CM_REP_COUNTER); |
| 134 | static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER); |
| 135 | static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER); |
| 136 | static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER); |
| 137 | static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER); |
| 138 | static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER); |
| 139 | static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER); |
| 140 | static CM_COUNTER_ATTR(apr, CM_APR_COUNTER); |
| 141 | |
| 142 | static struct attribute *cm_counter_default_attrs[] = { |
| 143 | &cm_req_counter_attr.attr, |
| 144 | &cm_mra_counter_attr.attr, |
| 145 | &cm_rej_counter_attr.attr, |
| 146 | &cm_rep_counter_attr.attr, |
| 147 | &cm_rtu_counter_attr.attr, |
| 148 | &cm_dreq_counter_attr.attr, |
| 149 | &cm_drep_counter_attr.attr, |
| 150 | &cm_sidr_req_counter_attr.attr, |
| 151 | &cm_sidr_rep_counter_attr.attr, |
| 152 | &cm_lap_counter_attr.attr, |
| 153 | &cm_apr_counter_attr.attr, |
| 154 | NULL |
| 155 | }; |
| 156 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 157 | struct cm_port { |
| 158 | struct cm_device *cm_dev; |
| 159 | struct ib_mad_agent *mad_agent; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 160 | struct kobject port_obj; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 161 | u8 port_num; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 162 | struct cm_counter_group counter_group[CM_COUNTER_GROUPS]; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | struct cm_device { |
| 166 | struct list_head list; |
| 167 | struct ib_device *device; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 168 | struct kobject dev_obj; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 169 | u8 ack_delay; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 170 | struct cm_port *port[0]; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | struct cm_av { |
| 174 | struct cm_port *port; |
| 175 | union ib_gid dgid; |
| 176 | struct ib_ah_attr ah_attr; |
| 177 | u16 pkey_index; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 178 | u8 timeout; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | struct cm_work { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 182 | struct delayed_work work; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 183 | struct list_head list; |
| 184 | struct cm_port *port; |
| 185 | struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */ |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 186 | __be32 local_id; /* Established / timewait */ |
| 187 | __be32 remote_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 188 | struct ib_cm_event cm_event; |
| 189 | struct ib_sa_path_rec path[0]; |
| 190 | }; |
| 191 | |
| 192 | struct cm_timewait_info { |
| 193 | struct cm_work work; /* Must be first. */ |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 194 | struct list_head list; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 195 | struct rb_node remote_qp_node; |
| 196 | struct rb_node remote_id_node; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 197 | __be64 remote_ca_guid; |
| 198 | __be32 remote_qpn; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 199 | u8 inserted_remote_qp; |
| 200 | u8 inserted_remote_id; |
| 201 | }; |
| 202 | |
| 203 | struct cm_id_private { |
| 204 | struct ib_cm_id id; |
| 205 | |
| 206 | struct rb_node service_node; |
| 207 | struct rb_node sidr_id_node; |
Sean Hefty | 87fd1a1 | 2006-03-02 16:50:37 -0800 | [diff] [blame] | 208 | spinlock_t lock; /* Do not acquire inside cm.lock */ |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 209 | struct completion comp; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 210 | atomic_t refcount; |
| 211 | |
| 212 | struct ib_mad_send_buf *msg; |
| 213 | struct cm_timewait_info *timewait_info; |
| 214 | /* todo: use alternate port on send failure */ |
| 215 | struct cm_av av; |
| 216 | struct cm_av alt_av; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 217 | struct ib_cm_compare_data *compare_data; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 218 | |
| 219 | void *private_data; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 220 | __be64 tid; |
| 221 | __be32 local_qpn; |
| 222 | __be32 remote_qpn; |
Sean Hefty | ae7971a | 2005-10-24 12:33:56 -0700 | [diff] [blame] | 223 | enum ib_qp_type qp_type; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 224 | __be32 sq_psn; |
| 225 | __be32 rq_psn; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 226 | int timeout_ms; |
| 227 | enum ib_mtu path_mtu; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 228 | __be16 pkey; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 229 | u8 private_data_len; |
| 230 | u8 max_cm_retries; |
| 231 | u8 peer_to_peer; |
| 232 | u8 responder_resources; |
| 233 | u8 initiator_depth; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 234 | u8 retry_count; |
| 235 | u8 rnr_retry_count; |
| 236 | u8 service_timeout; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 237 | u8 target_ack_delay; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 238 | |
| 239 | struct list_head work_list; |
| 240 | atomic_t work_count; |
| 241 | }; |
| 242 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 243 | static void cm_work_handler(struct work_struct *work); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 244 | |
| 245 | static inline void cm_deref_id(struct cm_id_private *cm_id_priv) |
| 246 | { |
| 247 | if (atomic_dec_and_test(&cm_id_priv->refcount)) |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 248 | complete(&cm_id_priv->comp); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int cm_alloc_msg(struct cm_id_private *cm_id_priv, |
| 252 | struct ib_mad_send_buf **msg) |
| 253 | { |
| 254 | struct ib_mad_agent *mad_agent; |
| 255 | struct ib_mad_send_buf *m; |
| 256 | struct ib_ah *ah; |
| 257 | |
| 258 | mad_agent = cm_id_priv->av.port->mad_agent; |
| 259 | ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr); |
| 260 | if (IS_ERR(ah)) |
| 261 | return PTR_ERR(ah); |
| 262 | |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 263 | m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn, |
John Kingman | 354ba39 | 2005-09-09 18:23:32 -0700 | [diff] [blame] | 264 | cm_id_priv->av.pkey_index, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 265 | 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 266 | GFP_ATOMIC); |
| 267 | if (IS_ERR(m)) { |
| 268 | ib_destroy_ah(ah); |
| 269 | return PTR_ERR(m); |
| 270 | } |
| 271 | |
| 272 | /* Timeout set by caller if response is expected. */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 273 | m->ah = ah; |
| 274 | m->retries = cm_id_priv->max_cm_retries; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 275 | |
| 276 | atomic_inc(&cm_id_priv->refcount); |
| 277 | m->context[0] = cm_id_priv; |
| 278 | *msg = m; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int cm_alloc_response_msg(struct cm_port *port, |
| 283 | struct ib_mad_recv_wc *mad_recv_wc, |
| 284 | struct ib_mad_send_buf **msg) |
| 285 | { |
| 286 | struct ib_mad_send_buf *m; |
| 287 | struct ib_ah *ah; |
| 288 | |
| 289 | ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc, |
| 290 | mad_recv_wc->recv_buf.grh, port->port_num); |
| 291 | if (IS_ERR(ah)) |
| 292 | return PTR_ERR(ah); |
| 293 | |
| 294 | m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 295 | 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 296 | GFP_ATOMIC); |
| 297 | if (IS_ERR(m)) { |
| 298 | ib_destroy_ah(ah); |
| 299 | return PTR_ERR(m); |
| 300 | } |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 301 | m->ah = ah; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 302 | *msg = m; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void cm_free_msg(struct ib_mad_send_buf *msg) |
| 307 | { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 308 | ib_destroy_ah(msg->ah); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 309 | if (msg->context[0]) |
| 310 | cm_deref_id(msg->context[0]); |
| 311 | ib_free_send_mad(msg); |
| 312 | } |
| 313 | |
| 314 | static void * cm_copy_private_data(const void *private_data, |
| 315 | u8 private_data_len) |
| 316 | { |
| 317 | void *data; |
| 318 | |
| 319 | if (!private_data || !private_data_len) |
| 320 | return NULL; |
| 321 | |
Eric Sesterhenn | bed8bdf | 2006-10-23 22:17:21 +0200 | [diff] [blame] | 322 | data = kmemdup(private_data, private_data_len, GFP_KERNEL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 323 | if (!data) |
| 324 | return ERR_PTR(-ENOMEM); |
| 325 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 326 | return data; |
| 327 | } |
| 328 | |
| 329 | static void cm_set_private_data(struct cm_id_private *cm_id_priv, |
| 330 | void *private_data, u8 private_data_len) |
| 331 | { |
| 332 | if (cm_id_priv->private_data && cm_id_priv->private_data_len) |
| 333 | kfree(cm_id_priv->private_data); |
| 334 | |
| 335 | cm_id_priv->private_data = private_data; |
| 336 | cm_id_priv->private_data_len = private_data_len; |
| 337 | } |
| 338 | |
Sean Hefty | ca222c6 | 2006-06-17 20:37:40 -0700 | [diff] [blame] | 339 | static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc, |
| 340 | struct ib_grh *grh, struct cm_av *av) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 341 | { |
| 342 | av->port = port; |
| 343 | av->pkey_index = wc->pkey_index; |
Sean Hefty | ca222c6 | 2006-06-17 20:37:40 -0700 | [diff] [blame] | 344 | ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc, |
| 345 | grh, &av->ah_attr); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av) |
| 349 | { |
| 350 | struct cm_device *cm_dev; |
| 351 | struct cm_port *port = NULL; |
| 352 | unsigned long flags; |
| 353 | int ret; |
| 354 | u8 p; |
| 355 | |
| 356 | read_lock_irqsave(&cm.device_lock, flags); |
| 357 | list_for_each_entry(cm_dev, &cm.device_list, list) { |
| 358 | if (!ib_find_cached_gid(cm_dev->device, &path->sgid, |
| 359 | &p, NULL)) { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 360 | port = cm_dev->port[p-1]; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | } |
| 364 | read_unlock_irqrestore(&cm.device_lock, flags); |
| 365 | |
| 366 | if (!port) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | ret = ib_find_cached_pkey(cm_dev->device, port->port_num, |
| 370 | be16_to_cpu(path->pkey), &av->pkey_index); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | |
| 374 | av->port = port; |
Sean Hefty | ca222c6 | 2006-06-17 20:37:40 -0700 | [diff] [blame] | 375 | ib_init_ah_from_path(cm_dev->device, port->port_num, path, |
| 376 | &av->ah_attr); |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 377 | av->timeout = path->packet_life_time + 1; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int cm_alloc_id(struct cm_id_private *cm_id_priv) |
| 382 | { |
| 383 | unsigned long flags; |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 384 | int ret, id; |
Sean Hefty | de1bb1a | 2005-11-30 10:01:13 -0800 | [diff] [blame] | 385 | static int next_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 386 | |
| 387 | do { |
| 388 | spin_lock_irqsave(&cm.lock, flags); |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 389 | ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, |
Michael S. Tsirkin | 9f81036c | 2007-05-21 19:06:54 +0300 | [diff] [blame] | 390 | next_id, &id); |
| 391 | if (!ret) |
| 392 | next_id = ((unsigned) id + 1) & MAX_ID_MASK; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 393 | spin_unlock_irqrestore(&cm.lock, flags); |
| 394 | } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) ); |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 395 | |
| 396 | cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 397 | return ret; |
| 398 | } |
| 399 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 400 | static void cm_free_id(__be32 local_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 401 | { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 402 | spin_lock_irq(&cm.lock); |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 403 | idr_remove(&cm.local_id_table, |
| 404 | (__force int) (local_id ^ cm.random_id_operand)); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 405 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 408 | static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 409 | { |
| 410 | struct cm_id_private *cm_id_priv; |
| 411 | |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 412 | cm_id_priv = idr_find(&cm.local_id_table, |
| 413 | (__force int) (local_id ^ cm.random_id_operand)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 414 | if (cm_id_priv) { |
| 415 | if (cm_id_priv->id.remote_id == remote_id) |
| 416 | atomic_inc(&cm_id_priv->refcount); |
| 417 | else |
| 418 | cm_id_priv = NULL; |
| 419 | } |
| 420 | |
| 421 | return cm_id_priv; |
| 422 | } |
| 423 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 424 | static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 425 | { |
| 426 | struct cm_id_private *cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 427 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 428 | spin_lock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 429 | cm_id_priv = cm_get_id(local_id, remote_id); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 430 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 431 | |
| 432 | return cm_id_priv; |
| 433 | } |
| 434 | |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 435 | static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask) |
| 436 | { |
| 437 | int i; |
| 438 | |
| 439 | for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++) |
| 440 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] & |
| 441 | ((unsigned long *) mask)[i]; |
| 442 | } |
| 443 | |
| 444 | static int cm_compare_data(struct ib_cm_compare_data *src_data, |
| 445 | struct ib_cm_compare_data *dst_data) |
| 446 | { |
| 447 | u8 src[IB_CM_COMPARE_SIZE]; |
| 448 | u8 dst[IB_CM_COMPARE_SIZE]; |
| 449 | |
| 450 | if (!src_data || !dst_data) |
| 451 | return 0; |
| 452 | |
| 453 | cm_mask_copy(src, src_data->data, dst_data->mask); |
| 454 | cm_mask_copy(dst, dst_data->data, src_data->mask); |
| 455 | return memcmp(src, dst, IB_CM_COMPARE_SIZE); |
| 456 | } |
| 457 | |
| 458 | static int cm_compare_private_data(u8 *private_data, |
| 459 | struct ib_cm_compare_data *dst_data) |
| 460 | { |
| 461 | u8 src[IB_CM_COMPARE_SIZE]; |
| 462 | |
| 463 | if (!dst_data) |
| 464 | return 0; |
| 465 | |
| 466 | cm_mask_copy(src, private_data, dst_data->mask); |
| 467 | return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE); |
| 468 | } |
| 469 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 470 | static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv) |
| 471 | { |
| 472 | struct rb_node **link = &cm.listen_service_table.rb_node; |
| 473 | struct rb_node *parent = NULL; |
| 474 | struct cm_id_private *cur_cm_id_priv; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 475 | __be64 service_id = cm_id_priv->id.service_id; |
| 476 | __be64 service_mask = cm_id_priv->id.service_mask; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 477 | int data_cmp; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 478 | |
| 479 | while (*link) { |
| 480 | parent = *link; |
| 481 | cur_cm_id_priv = rb_entry(parent, struct cm_id_private, |
| 482 | service_node); |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 483 | data_cmp = cm_compare_data(cm_id_priv->compare_data, |
| 484 | cur_cm_id_priv->compare_data); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 485 | if ((cur_cm_id_priv->id.service_mask & service_id) == |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 486 | (service_mask & cur_cm_id_priv->id.service_id) && |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 487 | (cm_id_priv->id.device == cur_cm_id_priv->id.device) && |
| 488 | !data_cmp) |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 489 | return cur_cm_id_priv; |
| 490 | |
| 491 | if (cm_id_priv->id.device < cur_cm_id_priv->id.device) |
| 492 | link = &(*link)->rb_left; |
| 493 | else if (cm_id_priv->id.device > cur_cm_id_priv->id.device) |
| 494 | link = &(*link)->rb_right; |
| 495 | else if (service_id < cur_cm_id_priv->id.service_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 496 | link = &(*link)->rb_left; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 497 | else if (service_id > cur_cm_id_priv->id.service_id) |
| 498 | link = &(*link)->rb_right; |
| 499 | else if (data_cmp < 0) |
| 500 | link = &(*link)->rb_left; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 501 | else |
| 502 | link = &(*link)->rb_right; |
| 503 | } |
| 504 | rb_link_node(&cm_id_priv->service_node, parent, link); |
| 505 | rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 509 | static struct cm_id_private * cm_find_listen(struct ib_device *device, |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 510 | __be64 service_id, |
| 511 | u8 *private_data) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 512 | { |
| 513 | struct rb_node *node = cm.listen_service_table.rb_node; |
| 514 | struct cm_id_private *cm_id_priv; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 515 | int data_cmp; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 516 | |
| 517 | while (node) { |
| 518 | cm_id_priv = rb_entry(node, struct cm_id_private, service_node); |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 519 | data_cmp = cm_compare_private_data(private_data, |
| 520 | cm_id_priv->compare_data); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 521 | if ((cm_id_priv->id.service_mask & service_id) == |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 522 | cm_id_priv->id.service_id && |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 523 | (cm_id_priv->id.device == device) && !data_cmp) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 524 | return cm_id_priv; |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 525 | |
| 526 | if (device < cm_id_priv->id.device) |
| 527 | node = node->rb_left; |
| 528 | else if (device > cm_id_priv->id.device) |
| 529 | node = node->rb_right; |
| 530 | else if (service_id < cm_id_priv->id.service_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 531 | node = node->rb_left; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 532 | else if (service_id > cm_id_priv->id.service_id) |
| 533 | node = node->rb_right; |
| 534 | else if (data_cmp < 0) |
| 535 | node = node->rb_left; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 536 | else |
| 537 | node = node->rb_right; |
| 538 | } |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info |
| 543 | *timewait_info) |
| 544 | { |
| 545 | struct rb_node **link = &cm.remote_id_table.rb_node; |
| 546 | struct rb_node *parent = NULL; |
| 547 | struct cm_timewait_info *cur_timewait_info; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 548 | __be64 remote_ca_guid = timewait_info->remote_ca_guid; |
| 549 | __be32 remote_id = timewait_info->work.remote_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 550 | |
| 551 | while (*link) { |
| 552 | parent = *link; |
| 553 | cur_timewait_info = rb_entry(parent, struct cm_timewait_info, |
| 554 | remote_id_node); |
| 555 | if (remote_id < cur_timewait_info->work.remote_id) |
| 556 | link = &(*link)->rb_left; |
| 557 | else if (remote_id > cur_timewait_info->work.remote_id) |
| 558 | link = &(*link)->rb_right; |
| 559 | else if (remote_ca_guid < cur_timewait_info->remote_ca_guid) |
| 560 | link = &(*link)->rb_left; |
| 561 | else if (remote_ca_guid > cur_timewait_info->remote_ca_guid) |
| 562 | link = &(*link)->rb_right; |
| 563 | else |
| 564 | return cur_timewait_info; |
| 565 | } |
| 566 | timewait_info->inserted_remote_id = 1; |
| 567 | rb_link_node(&timewait_info->remote_id_node, parent, link); |
| 568 | rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 572 | static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid, |
| 573 | __be32 remote_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 574 | { |
| 575 | struct rb_node *node = cm.remote_id_table.rb_node; |
| 576 | struct cm_timewait_info *timewait_info; |
| 577 | |
| 578 | while (node) { |
| 579 | timewait_info = rb_entry(node, struct cm_timewait_info, |
| 580 | remote_id_node); |
| 581 | if (remote_id < timewait_info->work.remote_id) |
| 582 | node = node->rb_left; |
| 583 | else if (remote_id > timewait_info->work.remote_id) |
| 584 | node = node->rb_right; |
| 585 | else if (remote_ca_guid < timewait_info->remote_ca_guid) |
| 586 | node = node->rb_left; |
| 587 | else if (remote_ca_guid > timewait_info->remote_ca_guid) |
| 588 | node = node->rb_right; |
| 589 | else |
| 590 | return timewait_info; |
| 591 | } |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info |
| 596 | *timewait_info) |
| 597 | { |
| 598 | struct rb_node **link = &cm.remote_qp_table.rb_node; |
| 599 | struct rb_node *parent = NULL; |
| 600 | struct cm_timewait_info *cur_timewait_info; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 601 | __be64 remote_ca_guid = timewait_info->remote_ca_guid; |
| 602 | __be32 remote_qpn = timewait_info->remote_qpn; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 603 | |
| 604 | while (*link) { |
| 605 | parent = *link; |
| 606 | cur_timewait_info = rb_entry(parent, struct cm_timewait_info, |
| 607 | remote_qp_node); |
| 608 | if (remote_qpn < cur_timewait_info->remote_qpn) |
| 609 | link = &(*link)->rb_left; |
| 610 | else if (remote_qpn > cur_timewait_info->remote_qpn) |
| 611 | link = &(*link)->rb_right; |
| 612 | else if (remote_ca_guid < cur_timewait_info->remote_ca_guid) |
| 613 | link = &(*link)->rb_left; |
| 614 | else if (remote_ca_guid > cur_timewait_info->remote_ca_guid) |
| 615 | link = &(*link)->rb_right; |
| 616 | else |
| 617 | return cur_timewait_info; |
| 618 | } |
| 619 | timewait_info->inserted_remote_qp = 1; |
| 620 | rb_link_node(&timewait_info->remote_qp_node, parent, link); |
| 621 | rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private |
| 626 | *cm_id_priv) |
| 627 | { |
| 628 | struct rb_node **link = &cm.remote_sidr_table.rb_node; |
| 629 | struct rb_node *parent = NULL; |
| 630 | struct cm_id_private *cur_cm_id_priv; |
| 631 | union ib_gid *port_gid = &cm_id_priv->av.dgid; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 632 | __be32 remote_id = cm_id_priv->id.remote_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 633 | |
| 634 | while (*link) { |
| 635 | parent = *link; |
| 636 | cur_cm_id_priv = rb_entry(parent, struct cm_id_private, |
| 637 | sidr_id_node); |
| 638 | if (remote_id < cur_cm_id_priv->id.remote_id) |
| 639 | link = &(*link)->rb_left; |
| 640 | else if (remote_id > cur_cm_id_priv->id.remote_id) |
| 641 | link = &(*link)->rb_right; |
| 642 | else { |
| 643 | int cmp; |
| 644 | cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid, |
| 645 | sizeof *port_gid); |
| 646 | if (cmp < 0) |
| 647 | link = &(*link)->rb_left; |
| 648 | else if (cmp > 0) |
| 649 | link = &(*link)->rb_right; |
| 650 | else |
| 651 | return cur_cm_id_priv; |
| 652 | } |
| 653 | } |
| 654 | rb_link_node(&cm_id_priv->sidr_id_node, parent, link); |
| 655 | rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv, |
| 660 | enum ib_cm_sidr_status status) |
| 661 | { |
| 662 | struct ib_cm_sidr_rep_param param; |
| 663 | |
| 664 | memset(¶m, 0, sizeof param); |
| 665 | param.status = status; |
| 666 | ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m); |
| 667 | } |
| 668 | |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 669 | struct ib_cm_id *ib_create_cm_id(struct ib_device *device, |
| 670 | ib_cm_handler cm_handler, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 671 | void *context) |
| 672 | { |
| 673 | struct cm_id_private *cm_id_priv; |
| 674 | int ret; |
| 675 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 676 | cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 677 | if (!cm_id_priv) |
| 678 | return ERR_PTR(-ENOMEM); |
| 679 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 680 | cm_id_priv->id.state = IB_CM_IDLE; |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 681 | cm_id_priv->id.device = device; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 682 | cm_id_priv->id.cm_handler = cm_handler; |
| 683 | cm_id_priv->id.context = context; |
John Kingman | 354ba39 | 2005-09-09 18:23:32 -0700 | [diff] [blame] | 684 | cm_id_priv->id.remote_cm_qpn = 1; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 685 | ret = cm_alloc_id(cm_id_priv); |
| 686 | if (ret) |
| 687 | goto error; |
| 688 | |
| 689 | spin_lock_init(&cm_id_priv->lock); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 690 | init_completion(&cm_id_priv->comp); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 691 | INIT_LIST_HEAD(&cm_id_priv->work_list); |
| 692 | atomic_set(&cm_id_priv->work_count, -1); |
| 693 | atomic_set(&cm_id_priv->refcount, 1); |
| 694 | return &cm_id_priv->id; |
| 695 | |
| 696 | error: |
| 697 | kfree(cm_id_priv); |
| 698 | return ERR_PTR(-ENOMEM); |
| 699 | } |
| 700 | EXPORT_SYMBOL(ib_create_cm_id); |
| 701 | |
| 702 | static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv) |
| 703 | { |
| 704 | struct cm_work *work; |
| 705 | |
| 706 | if (list_empty(&cm_id_priv->work_list)) |
| 707 | return NULL; |
| 708 | |
| 709 | work = list_entry(cm_id_priv->work_list.next, struct cm_work, list); |
| 710 | list_del(&work->list); |
| 711 | return work; |
| 712 | } |
| 713 | |
| 714 | static void cm_free_work(struct cm_work *work) |
| 715 | { |
| 716 | if (work->mad_recv_wc) |
| 717 | ib_free_recv_mad(work->mad_recv_wc); |
| 718 | kfree(work); |
| 719 | } |
| 720 | |
| 721 | static inline int cm_convert_to_ms(int iba_time) |
| 722 | { |
| 723 | /* approximate conversion to ms from 4.096us x 2^iba_time */ |
| 724 | return 1 << max(iba_time - 8, 0); |
| 725 | } |
| 726 | |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 727 | /* |
| 728 | * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time |
| 729 | * Because of how ack_timeout is stored, adding one doubles the timeout. |
| 730 | * To avoid large timeouts, select the max(ack_delay, life_time + 1), and |
| 731 | * increment it (round up) only if the other is within 50%. |
| 732 | */ |
| 733 | static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time) |
| 734 | { |
| 735 | int ack_timeout = packet_life_time + 1; |
| 736 | |
| 737 | if (ack_timeout >= ca_ack_delay) |
| 738 | ack_timeout += (ca_ack_delay >= (ack_timeout - 1)); |
| 739 | else |
| 740 | ack_timeout = ca_ack_delay + |
| 741 | (ack_timeout >= (ca_ack_delay - 1)); |
| 742 | |
| 743 | return min(31, ack_timeout); |
| 744 | } |
| 745 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 746 | static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info) |
| 747 | { |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 748 | if (timewait_info->inserted_remote_id) { |
| 749 | rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table); |
| 750 | timewait_info->inserted_remote_id = 0; |
| 751 | } |
| 752 | |
| 753 | if (timewait_info->inserted_remote_qp) { |
| 754 | rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table); |
| 755 | timewait_info->inserted_remote_qp = 0; |
| 756 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 759 | static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 760 | { |
| 761 | struct cm_timewait_info *timewait_info; |
| 762 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 763 | timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 764 | if (!timewait_info) |
| 765 | return ERR_PTR(-ENOMEM); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 766 | |
| 767 | timewait_info->work.local_id = local_id; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 768 | INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 769 | timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT; |
| 770 | return timewait_info; |
| 771 | } |
| 772 | |
| 773 | static void cm_enter_timewait(struct cm_id_private *cm_id_priv) |
| 774 | { |
| 775 | int wait_time; |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 776 | unsigned long flags; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 777 | |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 778 | spin_lock_irqsave(&cm.lock, flags); |
Michael S. Tsirkin | a70d059 | 2006-08-28 16:32:50 +0300 | [diff] [blame] | 779 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 780 | list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list); |
| 781 | spin_unlock_irqrestore(&cm.lock, flags); |
Michael S. Tsirkin | a70d059 | 2006-08-28 16:32:50 +0300 | [diff] [blame] | 782 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 783 | /* |
| 784 | * The cm_id could be destroyed by the user before we exit timewait. |
| 785 | * To protect against this, we search for the cm_id after exiting |
| 786 | * timewait before notifying the user that we've exited timewait. |
| 787 | */ |
| 788 | cm_id_priv->id.state = IB_CM_TIMEWAIT; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 789 | wait_time = cm_convert_to_ms(cm_id_priv->av.timeout); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 790 | queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work, |
| 791 | msecs_to_jiffies(wait_time)); |
| 792 | cm_id_priv->timewait_info = NULL; |
| 793 | } |
| 794 | |
| 795 | static void cm_reset_to_idle(struct cm_id_private *cm_id_priv) |
| 796 | { |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 797 | unsigned long flags; |
| 798 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 799 | cm_id_priv->id.state = IB_CM_IDLE; |
| 800 | if (cm_id_priv->timewait_info) { |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 801 | spin_lock_irqsave(&cm.lock, flags); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 802 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 803 | spin_unlock_irqrestore(&cm.lock, flags); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 804 | kfree(cm_id_priv->timewait_info); |
| 805 | cm_id_priv->timewait_info = NULL; |
| 806 | } |
| 807 | } |
| 808 | |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 809 | static void cm_destroy_id(struct ib_cm_id *cm_id, int err) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 810 | { |
| 811 | struct cm_id_private *cm_id_priv; |
| 812 | struct cm_work *work; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 813 | |
| 814 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 815 | retest: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 816 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 817 | switch (cm_id->state) { |
| 818 | case IB_CM_LISTEN: |
| 819 | cm_id->state = IB_CM_IDLE; |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 820 | spin_unlock_irq(&cm_id_priv->lock); |
| 821 | spin_lock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 822 | rb_erase(&cm_id_priv->service_node, &cm.listen_service_table); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 823 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 824 | break; |
| 825 | case IB_CM_SIDR_REQ_SENT: |
| 826 | cm_id->state = IB_CM_IDLE; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 827 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 828 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 829 | break; |
| 830 | case IB_CM_SIDR_REQ_RCVD: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 831 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 832 | cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT); |
| 833 | break; |
| 834 | case IB_CM_REQ_SENT: |
Sean Hefty | 227eca8 | 2005-11-30 10:00:25 -0800 | [diff] [blame] | 835 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 836 | spin_unlock_irq(&cm_id_priv->lock); |
Sean Hefty | 227eca8 | 2005-11-30 10:00:25 -0800 | [diff] [blame] | 837 | ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT, |
Sean Hefty | e971b8c | 2007-02-22 11:37:38 -0800 | [diff] [blame] | 838 | &cm_id_priv->id.device->node_guid, |
| 839 | sizeof cm_id_priv->id.device->node_guid, |
Sean Hefty | 227eca8 | 2005-11-30 10:00:25 -0800 | [diff] [blame] | 840 | NULL, 0); |
| 841 | break; |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 842 | case IB_CM_REQ_RCVD: |
| 843 | if (err == -ENOMEM) { |
| 844 | /* Do not reject to allow future retries. */ |
| 845 | cm_reset_to_idle(cm_id_priv); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 846 | spin_unlock_irq(&cm_id_priv->lock); |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 847 | } else { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 848 | spin_unlock_irq(&cm_id_priv->lock); |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 849 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, |
| 850 | NULL, 0, NULL, 0); |
| 851 | } |
| 852 | break; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 853 | case IB_CM_MRA_REQ_RCVD: |
| 854 | case IB_CM_REP_SENT: |
| 855 | case IB_CM_MRA_REP_RCVD: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 856 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 857 | /* Fall through */ |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 858 | case IB_CM_MRA_REQ_SENT: |
| 859 | case IB_CM_REP_RCVD: |
| 860 | case IB_CM_MRA_REP_SENT: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 861 | spin_unlock_irq(&cm_id_priv->lock); |
Sean Hefty | 227eca8 | 2005-11-30 10:00:25 -0800 | [diff] [blame] | 862 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, |
| 863 | NULL, 0, NULL, 0); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 864 | break; |
| 865 | case IB_CM_ESTABLISHED: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 866 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 867 | ib_send_cm_dreq(cm_id, NULL, 0); |
| 868 | goto retest; |
| 869 | case IB_CM_DREQ_SENT: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 870 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 871 | cm_enter_timewait(cm_id_priv); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 872 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 873 | break; |
| 874 | case IB_CM_DREQ_RCVD: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 875 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 876 | ib_send_cm_drep(cm_id, NULL, 0); |
| 877 | break; |
| 878 | default: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 879 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | |
| 883 | cm_free_id(cm_id->local_id); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 884 | cm_deref_id(cm_id_priv); |
| 885 | wait_for_completion(&cm_id_priv->comp); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 886 | while ((work = cm_dequeue_work(cm_id_priv)) != NULL) |
| 887 | cm_free_work(work); |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 888 | kfree(cm_id_priv->compare_data); |
| 889 | kfree(cm_id_priv->private_data); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 890 | kfree(cm_id_priv); |
| 891 | } |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 892 | |
| 893 | void ib_destroy_cm_id(struct ib_cm_id *cm_id) |
| 894 | { |
| 895 | cm_destroy_id(cm_id, 0); |
| 896 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 897 | EXPORT_SYMBOL(ib_destroy_cm_id); |
| 898 | |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 899 | int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask, |
| 900 | struct ib_cm_compare_data *compare_data) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 901 | { |
| 902 | struct cm_id_private *cm_id_priv, *cur_cm_id_priv; |
| 903 | unsigned long flags; |
| 904 | int ret = 0; |
| 905 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 906 | service_mask = service_mask ? service_mask : |
| 907 | __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 908 | service_id &= service_mask; |
| 909 | if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID && |
| 910 | (service_id != IB_CM_ASSIGN_SERVICE_ID)) |
| 911 | return -EINVAL; |
| 912 | |
| 913 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 914 | if (cm_id->state != IB_CM_IDLE) |
| 915 | return -EINVAL; |
| 916 | |
| 917 | if (compare_data) { |
| 918 | cm_id_priv->compare_data = kzalloc(sizeof *compare_data, |
| 919 | GFP_KERNEL); |
| 920 | if (!cm_id_priv->compare_data) |
| 921 | return -ENOMEM; |
| 922 | cm_mask_copy(cm_id_priv->compare_data->data, |
| 923 | compare_data->data, compare_data->mask); |
| 924 | memcpy(cm_id_priv->compare_data->mask, compare_data->mask, |
| 925 | IB_CM_COMPARE_SIZE); |
| 926 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 927 | |
| 928 | cm_id->state = IB_CM_LISTEN; |
| 929 | |
| 930 | spin_lock_irqsave(&cm.lock, flags); |
| 931 | if (service_id == IB_CM_ASSIGN_SERVICE_ID) { |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 932 | cm_id->service_id = cpu_to_be64(cm.listen_service_id++); |
| 933 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 934 | } else { |
| 935 | cm_id->service_id = service_id; |
| 936 | cm_id->service_mask = service_mask; |
| 937 | } |
| 938 | cur_cm_id_priv = cm_insert_listen(cm_id_priv); |
| 939 | spin_unlock_irqrestore(&cm.lock, flags); |
| 940 | |
| 941 | if (cur_cm_id_priv) { |
| 942 | cm_id->state = IB_CM_IDLE; |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 943 | kfree(cm_id_priv->compare_data); |
| 944 | cm_id_priv->compare_data = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 945 | ret = -EBUSY; |
| 946 | } |
| 947 | return ret; |
| 948 | } |
| 949 | EXPORT_SYMBOL(ib_cm_listen); |
| 950 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 951 | static __be64 cm_form_tid(struct cm_id_private *cm_id_priv, |
| 952 | enum cm_msg_sequence msg_seq) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 953 | { |
| 954 | u64 hi_tid, low_tid; |
| 955 | |
| 956 | hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 957 | low_tid = (u64) ((__force u32)cm_id_priv->id.local_id | |
| 958 | (msg_seq << 30)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 959 | return cpu_to_be64(hi_tid | low_tid); |
| 960 | } |
| 961 | |
| 962 | static void cm_format_mad_hdr(struct ib_mad_hdr *hdr, |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 963 | __be16 attr_id, __be64 tid) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 964 | { |
| 965 | hdr->base_version = IB_MGMT_BASE_VERSION; |
| 966 | hdr->mgmt_class = IB_MGMT_CLASS_CM; |
| 967 | hdr->class_version = IB_CM_CLASS_VERSION; |
| 968 | hdr->method = IB_MGMT_METHOD_SEND; |
| 969 | hdr->attr_id = attr_id; |
| 970 | hdr->tid = tid; |
| 971 | } |
| 972 | |
| 973 | static void cm_format_req(struct cm_req_msg *req_msg, |
| 974 | struct cm_id_private *cm_id_priv, |
| 975 | struct ib_cm_req_param *param) |
| 976 | { |
| 977 | cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID, |
| 978 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ)); |
| 979 | |
| 980 | req_msg->local_comm_id = cm_id_priv->id.local_id; |
| 981 | req_msg->service_id = param->service_id; |
Sean Hefty | e971b8c | 2007-02-22 11:37:38 -0800 | [diff] [blame] | 982 | req_msg->local_ca_guid = cm_id_priv->id.device->node_guid; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 983 | cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num)); |
| 984 | cm_req_set_resp_res(req_msg, param->responder_resources); |
| 985 | cm_req_set_init_depth(req_msg, param->initiator_depth); |
| 986 | cm_req_set_remote_resp_timeout(req_msg, |
| 987 | param->remote_cm_response_timeout); |
| 988 | cm_req_set_qp_type(req_msg, param->qp_type); |
| 989 | cm_req_set_flow_ctrl(req_msg, param->flow_control); |
| 990 | cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn)); |
| 991 | cm_req_set_local_resp_timeout(req_msg, |
| 992 | param->local_cm_response_timeout); |
| 993 | cm_req_set_retry_count(req_msg, param->retry_count); |
| 994 | req_msg->pkey = param->primary_path->pkey; |
| 995 | cm_req_set_path_mtu(req_msg, param->primary_path->mtu); |
| 996 | cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count); |
| 997 | cm_req_set_max_cm_retries(req_msg, param->max_cm_retries); |
| 998 | cm_req_set_srq(req_msg, param->srq); |
| 999 | |
| 1000 | req_msg->primary_local_lid = param->primary_path->slid; |
| 1001 | req_msg->primary_remote_lid = param->primary_path->dlid; |
| 1002 | req_msg->primary_local_gid = param->primary_path->sgid; |
| 1003 | req_msg->primary_remote_gid = param->primary_path->dgid; |
| 1004 | cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label); |
| 1005 | cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate); |
| 1006 | req_msg->primary_traffic_class = param->primary_path->traffic_class; |
| 1007 | req_msg->primary_hop_limit = param->primary_path->hop_limit; |
| 1008 | cm_req_set_primary_sl(req_msg, param->primary_path->sl); |
| 1009 | cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */ |
| 1010 | cm_req_set_primary_local_ack_timeout(req_msg, |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 1011 | cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay, |
| 1012 | param->primary_path->packet_life_time)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1013 | |
| 1014 | if (param->alternate_path) { |
| 1015 | req_msg->alt_local_lid = param->alternate_path->slid; |
| 1016 | req_msg->alt_remote_lid = param->alternate_path->dlid; |
| 1017 | req_msg->alt_local_gid = param->alternate_path->sgid; |
| 1018 | req_msg->alt_remote_gid = param->alternate_path->dgid; |
| 1019 | cm_req_set_alt_flow_label(req_msg, |
| 1020 | param->alternate_path->flow_label); |
| 1021 | cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate); |
| 1022 | req_msg->alt_traffic_class = param->alternate_path->traffic_class; |
| 1023 | req_msg->alt_hop_limit = param->alternate_path->hop_limit; |
| 1024 | cm_req_set_alt_sl(req_msg, param->alternate_path->sl); |
| 1025 | cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */ |
| 1026 | cm_req_set_alt_local_ack_timeout(req_msg, |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 1027 | cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay, |
| 1028 | param->alternate_path->packet_life_time)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | if (param->private_data && param->private_data_len) |
| 1032 | memcpy(req_msg->private_data, param->private_data, |
| 1033 | param->private_data_len); |
| 1034 | } |
| 1035 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1036 | static int cm_validate_req_param(struct ib_cm_req_param *param) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1037 | { |
| 1038 | /* peer-to-peer not supported */ |
| 1039 | if (param->peer_to_peer) |
| 1040 | return -EINVAL; |
| 1041 | |
| 1042 | if (!param->primary_path) |
| 1043 | return -EINVAL; |
| 1044 | |
| 1045 | if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC) |
| 1046 | return -EINVAL; |
| 1047 | |
| 1048 | if (param->private_data && |
| 1049 | param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE) |
| 1050 | return -EINVAL; |
| 1051 | |
| 1052 | if (param->alternate_path && |
| 1053 | (param->alternate_path->pkey != param->primary_path->pkey || |
| 1054 | param->alternate_path->mtu != param->primary_path->mtu)) |
| 1055 | return -EINVAL; |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | int ib_send_cm_req(struct ib_cm_id *cm_id, |
| 1061 | struct ib_cm_req_param *param) |
| 1062 | { |
| 1063 | struct cm_id_private *cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1064 | struct cm_req_msg *req_msg; |
| 1065 | unsigned long flags; |
| 1066 | int ret; |
| 1067 | |
| 1068 | ret = cm_validate_req_param(param); |
| 1069 | if (ret) |
| 1070 | return ret; |
| 1071 | |
| 1072 | /* Verify that we're not in timewait. */ |
| 1073 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1074 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 1075 | if (cm_id->state != IB_CM_IDLE) { |
| 1076 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1077 | ret = -EINVAL; |
| 1078 | goto out; |
| 1079 | } |
| 1080 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1081 | |
| 1082 | cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> |
| 1083 | id.local_id); |
Sean Hefty | 75df23e | 2006-07-25 19:52:01 +0300 | [diff] [blame] | 1084 | if (IS_ERR(cm_id_priv->timewait_info)) { |
| 1085 | ret = PTR_ERR(cm_id_priv->timewait_info); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1086 | goto out; |
Sean Hefty | 75df23e | 2006-07-25 19:52:01 +0300 | [diff] [blame] | 1087 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1088 | |
| 1089 | ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av); |
| 1090 | if (ret) |
| 1091 | goto error1; |
| 1092 | if (param->alternate_path) { |
| 1093 | ret = cm_init_av_by_path(param->alternate_path, |
| 1094 | &cm_id_priv->alt_av); |
| 1095 | if (ret) |
| 1096 | goto error1; |
| 1097 | } |
| 1098 | cm_id->service_id = param->service_id; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 1099 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1100 | cm_id_priv->timeout_ms = cm_convert_to_ms( |
| 1101 | param->primary_path->packet_life_time) * 2 + |
| 1102 | cm_convert_to_ms( |
| 1103 | param->remote_cm_response_timeout); |
| 1104 | cm_id_priv->max_cm_retries = param->max_cm_retries; |
| 1105 | cm_id_priv->initiator_depth = param->initiator_depth; |
| 1106 | cm_id_priv->responder_resources = param->responder_resources; |
| 1107 | cm_id_priv->retry_count = param->retry_count; |
| 1108 | cm_id_priv->path_mtu = param->primary_path->mtu; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 1109 | cm_id_priv->pkey = param->primary_path->pkey; |
Sean Hefty | ae7971a | 2005-10-24 12:33:56 -0700 | [diff] [blame] | 1110 | cm_id_priv->qp_type = param->qp_type; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1111 | |
| 1112 | ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg); |
| 1113 | if (ret) |
| 1114 | goto error1; |
| 1115 | |
| 1116 | req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad; |
| 1117 | cm_format_req(req_msg, cm_id_priv, param); |
| 1118 | cm_id_priv->tid = req_msg->hdr.tid; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1119 | cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1120 | cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT; |
| 1121 | |
| 1122 | cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg); |
| 1123 | cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1124 | |
| 1125 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1126 | ret = ib_post_send_mad(cm_id_priv->msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1127 | if (ret) { |
| 1128 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1129 | goto error2; |
| 1130 | } |
| 1131 | BUG_ON(cm_id->state != IB_CM_IDLE); |
| 1132 | cm_id->state = IB_CM_REQ_SENT; |
| 1133 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1134 | return 0; |
| 1135 | |
| 1136 | error2: cm_free_msg(cm_id_priv->msg); |
| 1137 | error1: kfree(cm_id_priv->timewait_info); |
| 1138 | out: return ret; |
| 1139 | } |
| 1140 | EXPORT_SYMBOL(ib_send_cm_req); |
| 1141 | |
| 1142 | static int cm_issue_rej(struct cm_port *port, |
| 1143 | struct ib_mad_recv_wc *mad_recv_wc, |
| 1144 | enum ib_cm_rej_reason reason, |
| 1145 | enum cm_msg_response msg_rejected, |
| 1146 | void *ari, u8 ari_length) |
| 1147 | { |
| 1148 | struct ib_mad_send_buf *msg = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1149 | struct cm_rej_msg *rej_msg, *rcv_msg; |
| 1150 | int ret; |
| 1151 | |
| 1152 | ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); |
| 1153 | if (ret) |
| 1154 | return ret; |
| 1155 | |
| 1156 | /* We just need common CM header information. Cast to any message. */ |
| 1157 | rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad; |
| 1158 | rej_msg = (struct cm_rej_msg *) msg->mad; |
| 1159 | |
| 1160 | cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid); |
| 1161 | rej_msg->remote_comm_id = rcv_msg->local_comm_id; |
| 1162 | rej_msg->local_comm_id = rcv_msg->remote_comm_id; |
| 1163 | cm_rej_set_msg_rejected(rej_msg, msg_rejected); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 1164 | rej_msg->reason = cpu_to_be16(reason); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1165 | |
| 1166 | if (ari && ari_length) { |
| 1167 | cm_rej_set_reject_info_len(rej_msg, ari_length); |
| 1168 | memcpy(rej_msg->ari, ari, ari_length); |
| 1169 | } |
| 1170 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1171 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1172 | if (ret) |
| 1173 | cm_free_msg(msg); |
| 1174 | |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 1178 | static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid, |
| 1179 | __be32 local_qpn, __be32 remote_qpn) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1180 | { |
| 1181 | return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) || |
| 1182 | ((local_ca_guid == remote_ca_guid) && |
| 1183 | (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn)))); |
| 1184 | } |
| 1185 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1186 | static void cm_format_paths_from_req(struct cm_req_msg *req_msg, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1187 | struct ib_sa_path_rec *primary_path, |
| 1188 | struct ib_sa_path_rec *alt_path) |
| 1189 | { |
| 1190 | memset(primary_path, 0, sizeof *primary_path); |
| 1191 | primary_path->dgid = req_msg->primary_local_gid; |
| 1192 | primary_path->sgid = req_msg->primary_remote_gid; |
| 1193 | primary_path->dlid = req_msg->primary_local_lid; |
| 1194 | primary_path->slid = req_msg->primary_remote_lid; |
| 1195 | primary_path->flow_label = cm_req_get_primary_flow_label(req_msg); |
| 1196 | primary_path->hop_limit = req_msg->primary_hop_limit; |
| 1197 | primary_path->traffic_class = req_msg->primary_traffic_class; |
| 1198 | primary_path->reversible = 1; |
| 1199 | primary_path->pkey = req_msg->pkey; |
| 1200 | primary_path->sl = cm_req_get_primary_sl(req_msg); |
| 1201 | primary_path->mtu_selector = IB_SA_EQ; |
| 1202 | primary_path->mtu = cm_req_get_path_mtu(req_msg); |
| 1203 | primary_path->rate_selector = IB_SA_EQ; |
| 1204 | primary_path->rate = cm_req_get_primary_packet_rate(req_msg); |
| 1205 | primary_path->packet_life_time_selector = IB_SA_EQ; |
| 1206 | primary_path->packet_life_time = |
| 1207 | cm_req_get_primary_local_ack_timeout(req_msg); |
| 1208 | primary_path->packet_life_time -= (primary_path->packet_life_time > 0); |
| 1209 | |
| 1210 | if (req_msg->alt_local_lid) { |
| 1211 | memset(alt_path, 0, sizeof *alt_path); |
| 1212 | alt_path->dgid = req_msg->alt_local_gid; |
| 1213 | alt_path->sgid = req_msg->alt_remote_gid; |
| 1214 | alt_path->dlid = req_msg->alt_local_lid; |
| 1215 | alt_path->slid = req_msg->alt_remote_lid; |
| 1216 | alt_path->flow_label = cm_req_get_alt_flow_label(req_msg); |
| 1217 | alt_path->hop_limit = req_msg->alt_hop_limit; |
| 1218 | alt_path->traffic_class = req_msg->alt_traffic_class; |
| 1219 | alt_path->reversible = 1; |
| 1220 | alt_path->pkey = req_msg->pkey; |
| 1221 | alt_path->sl = cm_req_get_alt_sl(req_msg); |
| 1222 | alt_path->mtu_selector = IB_SA_EQ; |
| 1223 | alt_path->mtu = cm_req_get_path_mtu(req_msg); |
| 1224 | alt_path->rate_selector = IB_SA_EQ; |
| 1225 | alt_path->rate = cm_req_get_alt_packet_rate(req_msg); |
| 1226 | alt_path->packet_life_time_selector = IB_SA_EQ; |
| 1227 | alt_path->packet_life_time = |
| 1228 | cm_req_get_alt_local_ack_timeout(req_msg); |
| 1229 | alt_path->packet_life_time -= (alt_path->packet_life_time > 0); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | static void cm_format_req_event(struct cm_work *work, |
| 1234 | struct cm_id_private *cm_id_priv, |
| 1235 | struct ib_cm_id *listen_id) |
| 1236 | { |
| 1237 | struct cm_req_msg *req_msg; |
| 1238 | struct ib_cm_req_event_param *param; |
| 1239 | |
| 1240 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1241 | param = &work->cm_event.param.req_rcvd; |
| 1242 | param->listen_id = listen_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1243 | param->port = cm_id_priv->av.port->port_num; |
| 1244 | param->primary_path = &work->path[0]; |
| 1245 | if (req_msg->alt_local_lid) |
| 1246 | param->alternate_path = &work->path[1]; |
| 1247 | else |
| 1248 | param->alternate_path = NULL; |
| 1249 | param->remote_ca_guid = req_msg->local_ca_guid; |
| 1250 | param->remote_qkey = be32_to_cpu(req_msg->local_qkey); |
| 1251 | param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg)); |
| 1252 | param->qp_type = cm_req_get_qp_type(req_msg); |
| 1253 | param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg)); |
| 1254 | param->responder_resources = cm_req_get_init_depth(req_msg); |
| 1255 | param->initiator_depth = cm_req_get_resp_res(req_msg); |
| 1256 | param->local_cm_response_timeout = |
| 1257 | cm_req_get_remote_resp_timeout(req_msg); |
| 1258 | param->flow_control = cm_req_get_flow_ctrl(req_msg); |
| 1259 | param->remote_cm_response_timeout = |
| 1260 | cm_req_get_local_resp_timeout(req_msg); |
| 1261 | param->retry_count = cm_req_get_retry_count(req_msg); |
| 1262 | param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg); |
| 1263 | param->srq = cm_req_get_srq(req_msg); |
| 1264 | work->cm_event.private_data = &req_msg->private_data; |
| 1265 | } |
| 1266 | |
| 1267 | static void cm_process_work(struct cm_id_private *cm_id_priv, |
| 1268 | struct cm_work *work) |
| 1269 | { |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1270 | int ret; |
| 1271 | |
| 1272 | /* We will typically only have the current event to report. */ |
| 1273 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event); |
| 1274 | cm_free_work(work); |
| 1275 | |
| 1276 | while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1277 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1278 | work = cm_dequeue_work(cm_id_priv); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1279 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1280 | BUG_ON(!work); |
| 1281 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, |
| 1282 | &work->cm_event); |
| 1283 | cm_free_work(work); |
| 1284 | } |
| 1285 | cm_deref_id(cm_id_priv); |
| 1286 | if (ret) |
Michael S. Tsirkin | 04c3354 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 1287 | cm_destroy_id(&cm_id_priv->id, ret); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static void cm_format_mra(struct cm_mra_msg *mra_msg, |
| 1291 | struct cm_id_private *cm_id_priv, |
| 1292 | enum cm_msg_response msg_mraed, u8 service_timeout, |
| 1293 | const void *private_data, u8 private_data_len) |
| 1294 | { |
| 1295 | cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid); |
| 1296 | cm_mra_set_msg_mraed(mra_msg, msg_mraed); |
| 1297 | mra_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1298 | mra_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1299 | cm_mra_set_service_timeout(mra_msg, service_timeout); |
| 1300 | |
| 1301 | if (private_data && private_data_len) |
| 1302 | memcpy(mra_msg->private_data, private_data, private_data_len); |
| 1303 | } |
| 1304 | |
| 1305 | static void cm_format_rej(struct cm_rej_msg *rej_msg, |
| 1306 | struct cm_id_private *cm_id_priv, |
| 1307 | enum ib_cm_rej_reason reason, |
| 1308 | void *ari, |
| 1309 | u8 ari_length, |
| 1310 | const void *private_data, |
| 1311 | u8 private_data_len) |
| 1312 | { |
| 1313 | cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid); |
| 1314 | rej_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1315 | |
| 1316 | switch(cm_id_priv->id.state) { |
| 1317 | case IB_CM_REQ_RCVD: |
| 1318 | rej_msg->local_comm_id = 0; |
| 1319 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ); |
| 1320 | break; |
| 1321 | case IB_CM_MRA_REQ_SENT: |
| 1322 | rej_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1323 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ); |
| 1324 | break; |
| 1325 | case IB_CM_REP_RCVD: |
| 1326 | case IB_CM_MRA_REP_SENT: |
| 1327 | rej_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1328 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP); |
| 1329 | break; |
| 1330 | default: |
| 1331 | rej_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1332 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER); |
| 1333 | break; |
| 1334 | } |
| 1335 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 1336 | rej_msg->reason = cpu_to_be16(reason); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1337 | if (ari && ari_length) { |
| 1338 | cm_rej_set_reject_info_len(rej_msg, ari_length); |
| 1339 | memcpy(rej_msg->ari, ari, ari_length); |
| 1340 | } |
| 1341 | |
| 1342 | if (private_data && private_data_len) |
| 1343 | memcpy(rej_msg->private_data, private_data, private_data_len); |
| 1344 | } |
| 1345 | |
| 1346 | static void cm_dup_req_handler(struct cm_work *work, |
| 1347 | struct cm_id_private *cm_id_priv) |
| 1348 | { |
| 1349 | struct ib_mad_send_buf *msg = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1350 | int ret; |
| 1351 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 1352 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 1353 | counter[CM_REQ_COUNTER]); |
| 1354 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1355 | /* Quick state check to discard duplicate REQs. */ |
| 1356 | if (cm_id_priv->id.state == IB_CM_REQ_RCVD) |
| 1357 | return; |
| 1358 | |
| 1359 | ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); |
| 1360 | if (ret) |
| 1361 | return; |
| 1362 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1363 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1364 | switch (cm_id_priv->id.state) { |
| 1365 | case IB_CM_MRA_REQ_SENT: |
| 1366 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, |
| 1367 | CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout, |
| 1368 | cm_id_priv->private_data, |
| 1369 | cm_id_priv->private_data_len); |
| 1370 | break; |
| 1371 | case IB_CM_TIMEWAIT: |
| 1372 | cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv, |
| 1373 | IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0); |
| 1374 | break; |
| 1375 | default: |
| 1376 | goto unlock; |
| 1377 | } |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1378 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1379 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1380 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1381 | if (ret) |
| 1382 | goto free; |
| 1383 | return; |
| 1384 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1385 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1386 | free: cm_free_msg(msg); |
| 1387 | } |
| 1388 | |
| 1389 | static struct cm_id_private * cm_match_req(struct cm_work *work, |
| 1390 | struct cm_id_private *cm_id_priv) |
| 1391 | { |
| 1392 | struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv; |
| 1393 | struct cm_timewait_info *timewait_info; |
| 1394 | struct cm_req_msg *req_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1395 | |
| 1396 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1397 | |
Sean Hefty | d998ccc | 2007-05-21 17:38:02 -0700 | [diff] [blame] | 1398 | /* Check for possible duplicate REQ. */ |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1399 | spin_lock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1400 | timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1401 | if (timewait_info) { |
| 1402 | cur_cm_id_priv = cm_get_id(timewait_info->work.local_id, |
| 1403 | timewait_info->work.remote_id); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1404 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1405 | if (cur_cm_id_priv) { |
| 1406 | cm_dup_req_handler(work, cur_cm_id_priv); |
| 1407 | cm_deref_id(cur_cm_id_priv); |
Sean Hefty | d998ccc | 2007-05-21 17:38:02 -0700 | [diff] [blame] | 1408 | } |
| 1409 | return NULL; |
| 1410 | } |
| 1411 | |
| 1412 | /* Check for stale connections. */ |
| 1413 | timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info); |
| 1414 | if (timewait_info) { |
| 1415 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1416 | spin_unlock_irq(&cm.lock); |
Sean Hefty | d998ccc | 2007-05-21 17:38:02 -0700 | [diff] [blame] | 1417 | cm_issue_rej(work->port, work->mad_recv_wc, |
| 1418 | IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ, |
| 1419 | NULL, 0); |
| 1420 | return NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* Find matching listen request. */ |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 1424 | listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device, |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 1425 | req_msg->service_id, |
| 1426 | req_msg->private_data); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1427 | if (!listen_cm_id_priv) { |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 1428 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1429 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1430 | cm_issue_rej(work->port, work->mad_recv_wc, |
| 1431 | IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ, |
| 1432 | NULL, 0); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 1433 | goto out; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1434 | } |
| 1435 | atomic_inc(&listen_cm_id_priv->refcount); |
| 1436 | atomic_inc(&cm_id_priv->refcount); |
| 1437 | cm_id_priv->id.state = IB_CM_REQ_RCVD; |
| 1438 | atomic_inc(&cm_id_priv->work_count); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1439 | spin_unlock_irq(&cm.lock); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 1440 | out: |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1441 | return listen_cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int cm_req_handler(struct cm_work *work) |
| 1445 | { |
| 1446 | struct ib_cm_id *cm_id; |
| 1447 | struct cm_id_private *cm_id_priv, *listen_cm_id_priv; |
| 1448 | struct cm_req_msg *req_msg; |
| 1449 | int ret; |
| 1450 | |
| 1451 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1452 | |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 1453 | cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1454 | if (IS_ERR(cm_id)) |
| 1455 | return PTR_ERR(cm_id); |
| 1456 | |
| 1457 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1458 | cm_id_priv->id.remote_id = req_msg->local_comm_id; |
| 1459 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, |
Sean Hefty | ca222c6 | 2006-06-17 20:37:40 -0700 | [diff] [blame] | 1460 | work->mad_recv_wc->recv_buf.grh, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1461 | &cm_id_priv->av); |
| 1462 | cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> |
| 1463 | id.local_id); |
| 1464 | if (IS_ERR(cm_id_priv->timewait_info)) { |
| 1465 | ret = PTR_ERR(cm_id_priv->timewait_info); |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1466 | goto destroy; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1467 | } |
| 1468 | cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id; |
| 1469 | cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid; |
| 1470 | cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg); |
| 1471 | |
| 1472 | listen_cm_id_priv = cm_match_req(work, cm_id_priv); |
| 1473 | if (!listen_cm_id_priv) { |
| 1474 | ret = -EINVAL; |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1475 | kfree(cm_id_priv->timewait_info); |
| 1476 | goto destroy; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler; |
| 1480 | cm_id_priv->id.context = listen_cm_id_priv->id.context; |
| 1481 | cm_id_priv->id.service_id = req_msg->service_id; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 1482 | cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1483 | |
| 1484 | cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]); |
| 1485 | ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av); |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1486 | if (ret) { |
| 1487 | ib_get_cached_gid(work->port->cm_dev->device, |
| 1488 | work->port->port_num, 0, &work->path[0].sgid); |
| 1489 | ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID, |
| 1490 | &work->path[0].sgid, sizeof work->path[0].sgid, |
| 1491 | NULL, 0); |
| 1492 | goto rejected; |
| 1493 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1494 | if (req_msg->alt_local_lid) { |
| 1495 | ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av); |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1496 | if (ret) { |
| 1497 | ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID, |
| 1498 | &work->path[0].sgid, |
| 1499 | sizeof work->path[0].sgid, NULL, 0); |
| 1500 | goto rejected; |
| 1501 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1502 | } |
| 1503 | cm_id_priv->tid = req_msg->hdr.tid; |
| 1504 | cm_id_priv->timeout_ms = cm_convert_to_ms( |
| 1505 | cm_req_get_local_resp_timeout(req_msg)); |
| 1506 | cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg); |
| 1507 | cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg); |
| 1508 | cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg); |
| 1509 | cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg); |
| 1510 | cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg); |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 1511 | cm_id_priv->pkey = req_msg->pkey; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1512 | cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1513 | cm_id_priv->retry_count = cm_req_get_retry_count(req_msg); |
| 1514 | cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg); |
Sean Hefty | ae7971a | 2005-10-24 12:33:56 -0700 | [diff] [blame] | 1515 | cm_id_priv->qp_type = cm_req_get_qp_type(req_msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1516 | |
| 1517 | cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id); |
| 1518 | cm_process_work(cm_id_priv, work); |
| 1519 | cm_deref_id(listen_cm_id_priv); |
| 1520 | return 0; |
| 1521 | |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1522 | rejected: |
| 1523 | atomic_dec(&cm_id_priv->refcount); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1524 | cm_deref_id(listen_cm_id_priv); |
Sean Hefty | 7684240 | 2006-08-28 11:57:42 -0700 | [diff] [blame] | 1525 | destroy: |
| 1526 | ib_destroy_cm_id(cm_id); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | static void cm_format_rep(struct cm_rep_msg *rep_msg, |
| 1531 | struct cm_id_private *cm_id_priv, |
| 1532 | struct ib_cm_rep_param *param) |
| 1533 | { |
| 1534 | cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid); |
| 1535 | rep_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1536 | rep_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1537 | cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num)); |
| 1538 | cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn)); |
| 1539 | rep_msg->resp_resources = param->responder_resources; |
| 1540 | rep_msg->initiator_depth = param->initiator_depth; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 1541 | cm_rep_set_target_ack_delay(rep_msg, |
| 1542 | cm_id_priv->av.port->cm_dev->ack_delay); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1543 | cm_rep_set_failover(rep_msg, param->failover_accepted); |
| 1544 | cm_rep_set_flow_ctrl(rep_msg, param->flow_control); |
| 1545 | cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count); |
| 1546 | cm_rep_set_srq(rep_msg, param->srq); |
Sean Hefty | e971b8c | 2007-02-22 11:37:38 -0800 | [diff] [blame] | 1547 | rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1548 | |
| 1549 | if (param->private_data && param->private_data_len) |
| 1550 | memcpy(rep_msg->private_data, param->private_data, |
| 1551 | param->private_data_len); |
| 1552 | } |
| 1553 | |
| 1554 | int ib_send_cm_rep(struct ib_cm_id *cm_id, |
| 1555 | struct ib_cm_rep_param *param) |
| 1556 | { |
| 1557 | struct cm_id_private *cm_id_priv; |
| 1558 | struct ib_mad_send_buf *msg; |
| 1559 | struct cm_rep_msg *rep_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1560 | unsigned long flags; |
| 1561 | int ret; |
| 1562 | |
| 1563 | if (param->private_data && |
| 1564 | param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE) |
| 1565 | return -EINVAL; |
| 1566 | |
| 1567 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1568 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 1569 | if (cm_id->state != IB_CM_REQ_RCVD && |
| 1570 | cm_id->state != IB_CM_MRA_REQ_SENT) { |
| 1571 | ret = -EINVAL; |
| 1572 | goto out; |
| 1573 | } |
| 1574 | |
| 1575 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 1576 | if (ret) |
| 1577 | goto out; |
| 1578 | |
| 1579 | rep_msg = (struct cm_rep_msg *) msg->mad; |
| 1580 | cm_format_rep(rep_msg, cm_id_priv, param); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1581 | msg->timeout_ms = cm_id_priv->timeout_ms; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1582 | msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT; |
| 1583 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1584 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1585 | if (ret) { |
| 1586 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1587 | cm_free_msg(msg); |
| 1588 | return ret; |
| 1589 | } |
| 1590 | |
| 1591 | cm_id->state = IB_CM_REP_SENT; |
| 1592 | cm_id_priv->msg = msg; |
| 1593 | cm_id_priv->initiator_depth = param->initiator_depth; |
| 1594 | cm_id_priv->responder_resources = param->responder_resources; |
| 1595 | cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg); |
| 1596 | cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg); |
| 1597 | |
| 1598 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1599 | return ret; |
| 1600 | } |
| 1601 | EXPORT_SYMBOL(ib_send_cm_rep); |
| 1602 | |
| 1603 | static void cm_format_rtu(struct cm_rtu_msg *rtu_msg, |
| 1604 | struct cm_id_private *cm_id_priv, |
| 1605 | const void *private_data, |
| 1606 | u8 private_data_len) |
| 1607 | { |
| 1608 | cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid); |
| 1609 | rtu_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1610 | rtu_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1611 | |
| 1612 | if (private_data && private_data_len) |
| 1613 | memcpy(rtu_msg->private_data, private_data, private_data_len); |
| 1614 | } |
| 1615 | |
| 1616 | int ib_send_cm_rtu(struct ib_cm_id *cm_id, |
| 1617 | const void *private_data, |
| 1618 | u8 private_data_len) |
| 1619 | { |
| 1620 | struct cm_id_private *cm_id_priv; |
| 1621 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1622 | unsigned long flags; |
| 1623 | void *data; |
| 1624 | int ret; |
| 1625 | |
| 1626 | if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE) |
| 1627 | return -EINVAL; |
| 1628 | |
| 1629 | data = cm_copy_private_data(private_data, private_data_len); |
| 1630 | if (IS_ERR(data)) |
| 1631 | return PTR_ERR(data); |
| 1632 | |
| 1633 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1634 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 1635 | if (cm_id->state != IB_CM_REP_RCVD && |
| 1636 | cm_id->state != IB_CM_MRA_REP_SENT) { |
| 1637 | ret = -EINVAL; |
| 1638 | goto error; |
| 1639 | } |
| 1640 | |
| 1641 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 1642 | if (ret) |
| 1643 | goto error; |
| 1644 | |
| 1645 | cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, |
| 1646 | private_data, private_data_len); |
| 1647 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1648 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1649 | if (ret) { |
| 1650 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1651 | cm_free_msg(msg); |
| 1652 | kfree(data); |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | cm_id->state = IB_CM_ESTABLISHED; |
| 1657 | cm_set_private_data(cm_id_priv, data, private_data_len); |
| 1658 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1659 | return 0; |
| 1660 | |
| 1661 | error: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1662 | kfree(data); |
| 1663 | return ret; |
| 1664 | } |
| 1665 | EXPORT_SYMBOL(ib_send_cm_rtu); |
| 1666 | |
| 1667 | static void cm_format_rep_event(struct cm_work *work) |
| 1668 | { |
| 1669 | struct cm_rep_msg *rep_msg; |
| 1670 | struct ib_cm_rep_event_param *param; |
| 1671 | |
| 1672 | rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1673 | param = &work->cm_event.param.rep_rcvd; |
| 1674 | param->remote_ca_guid = rep_msg->local_ca_guid; |
| 1675 | param->remote_qkey = be32_to_cpu(rep_msg->local_qkey); |
| 1676 | param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg)); |
| 1677 | param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg)); |
| 1678 | param->responder_resources = rep_msg->initiator_depth; |
| 1679 | param->initiator_depth = rep_msg->resp_resources; |
| 1680 | param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg); |
| 1681 | param->failover_accepted = cm_rep_get_failover(rep_msg); |
| 1682 | param->flow_control = cm_rep_get_flow_ctrl(rep_msg); |
| 1683 | param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg); |
| 1684 | param->srq = cm_rep_get_srq(rep_msg); |
| 1685 | work->cm_event.private_data = &rep_msg->private_data; |
| 1686 | } |
| 1687 | |
| 1688 | static void cm_dup_rep_handler(struct cm_work *work) |
| 1689 | { |
| 1690 | struct cm_id_private *cm_id_priv; |
| 1691 | struct cm_rep_msg *rep_msg; |
| 1692 | struct ib_mad_send_buf *msg = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1693 | int ret; |
| 1694 | |
| 1695 | rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad; |
| 1696 | cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, |
| 1697 | rep_msg->local_comm_id); |
| 1698 | if (!cm_id_priv) |
| 1699 | return; |
| 1700 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 1701 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 1702 | counter[CM_REP_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1703 | ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); |
| 1704 | if (ret) |
| 1705 | goto deref; |
| 1706 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1707 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1708 | if (cm_id_priv->id.state == IB_CM_ESTABLISHED) |
| 1709 | cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, |
| 1710 | cm_id_priv->private_data, |
| 1711 | cm_id_priv->private_data_len); |
| 1712 | else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT) |
| 1713 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, |
| 1714 | CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout, |
| 1715 | cm_id_priv->private_data, |
| 1716 | cm_id_priv->private_data_len); |
| 1717 | else |
| 1718 | goto unlock; |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1719 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1720 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1721 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1722 | if (ret) |
| 1723 | goto free; |
| 1724 | goto deref; |
| 1725 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1726 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1727 | free: cm_free_msg(msg); |
| 1728 | deref: cm_deref_id(cm_id_priv); |
| 1729 | } |
| 1730 | |
| 1731 | static int cm_rep_handler(struct cm_work *work) |
| 1732 | { |
| 1733 | struct cm_id_private *cm_id_priv; |
| 1734 | struct cm_rep_msg *rep_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1735 | int ret; |
| 1736 | |
| 1737 | rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1738 | cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0); |
| 1739 | if (!cm_id_priv) { |
| 1740 | cm_dup_rep_handler(work); |
| 1741 | return -EINVAL; |
| 1742 | } |
| 1743 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1744 | cm_format_rep_event(work); |
| 1745 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1746 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1747 | switch (cm_id_priv->id.state) { |
| 1748 | case IB_CM_REQ_SENT: |
| 1749 | case IB_CM_MRA_REQ_RCVD: |
| 1750 | break; |
| 1751 | default: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1752 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1753 | ret = -EINVAL; |
| 1754 | goto error; |
| 1755 | } |
Sean Hefty | 87fd1a1 | 2006-03-02 16:50:37 -0800 | [diff] [blame] | 1756 | |
| 1757 | cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id; |
| 1758 | cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid; |
| 1759 | cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg); |
| 1760 | |
| 1761 | spin_lock(&cm.lock); |
| 1762 | /* Check for duplicate REP. */ |
| 1763 | if (cm_insert_remote_id(cm_id_priv->timewait_info)) { |
| 1764 | spin_unlock(&cm.lock); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1765 | spin_unlock_irq(&cm_id_priv->lock); |
Sean Hefty | 87fd1a1 | 2006-03-02 16:50:37 -0800 | [diff] [blame] | 1766 | ret = -EINVAL; |
| 1767 | goto error; |
| 1768 | } |
| 1769 | /* Check for a stale connection. */ |
| 1770 | if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) { |
| 1771 | rb_erase(&cm_id_priv->timewait_info->remote_id_node, |
| 1772 | &cm.remote_id_table); |
| 1773 | cm_id_priv->timewait_info->inserted_remote_id = 0; |
| 1774 | spin_unlock(&cm.lock); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1775 | spin_unlock_irq(&cm_id_priv->lock); |
Sean Hefty | 87fd1a1 | 2006-03-02 16:50:37 -0800 | [diff] [blame] | 1776 | cm_issue_rej(work->port, work->mad_recv_wc, |
| 1777 | IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP, |
| 1778 | NULL, 0); |
| 1779 | ret = -EINVAL; |
| 1780 | goto error; |
| 1781 | } |
| 1782 | spin_unlock(&cm.lock); |
| 1783 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1784 | cm_id_priv->id.state = IB_CM_REP_RCVD; |
| 1785 | cm_id_priv->id.remote_id = rep_msg->local_comm_id; |
| 1786 | cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg); |
| 1787 | cm_id_priv->initiator_depth = rep_msg->resp_resources; |
| 1788 | cm_id_priv->responder_resources = rep_msg->initiator_depth; |
| 1789 | cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg); |
| 1790 | cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg); |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 1791 | cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg); |
| 1792 | cm_id_priv->av.timeout = |
| 1793 | cm_ack_timeout(cm_id_priv->target_ack_delay, |
| 1794 | cm_id_priv->av.timeout - 1); |
| 1795 | cm_id_priv->alt_av.timeout = |
| 1796 | cm_ack_timeout(cm_id_priv->target_ack_delay, |
| 1797 | cm_id_priv->alt_av.timeout - 1); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1798 | |
| 1799 | /* todo: handle peer_to_peer */ |
| 1800 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1801 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1802 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 1803 | if (!ret) |
| 1804 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1805 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1806 | |
| 1807 | if (ret) |
| 1808 | cm_process_work(cm_id_priv, work); |
| 1809 | else |
| 1810 | cm_deref_id(cm_id_priv); |
| 1811 | return 0; |
| 1812 | |
Sean Hefty | 87fd1a1 | 2006-03-02 16:50:37 -0800 | [diff] [blame] | 1813 | error: |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1814 | cm_deref_id(cm_id_priv); |
| 1815 | return ret; |
| 1816 | } |
| 1817 | |
| 1818 | static int cm_establish_handler(struct cm_work *work) |
| 1819 | { |
| 1820 | struct cm_id_private *cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1821 | int ret; |
| 1822 | |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 1823 | /* See comment in cm_establish about lookup. */ |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1824 | cm_id_priv = cm_acquire_id(work->local_id, work->remote_id); |
| 1825 | if (!cm_id_priv) |
| 1826 | return -EINVAL; |
| 1827 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1828 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1829 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1830 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1831 | goto out; |
| 1832 | } |
| 1833 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1834 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1835 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 1836 | if (!ret) |
| 1837 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1838 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1839 | |
| 1840 | if (ret) |
| 1841 | cm_process_work(cm_id_priv, work); |
| 1842 | else |
| 1843 | cm_deref_id(cm_id_priv); |
| 1844 | return 0; |
| 1845 | out: |
| 1846 | cm_deref_id(cm_id_priv); |
| 1847 | return -EINVAL; |
| 1848 | } |
| 1849 | |
| 1850 | static int cm_rtu_handler(struct cm_work *work) |
| 1851 | { |
| 1852 | struct cm_id_private *cm_id_priv; |
| 1853 | struct cm_rtu_msg *rtu_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1854 | int ret; |
| 1855 | |
| 1856 | rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad; |
| 1857 | cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id, |
| 1858 | rtu_msg->local_comm_id); |
| 1859 | if (!cm_id_priv) |
| 1860 | return -EINVAL; |
| 1861 | |
| 1862 | work->cm_event.private_data = &rtu_msg->private_data; |
| 1863 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1864 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1865 | if (cm_id_priv->id.state != IB_CM_REP_SENT && |
| 1866 | cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1867 | spin_unlock_irq(&cm_id_priv->lock); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 1868 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 1869 | counter[CM_RTU_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1870 | goto out; |
| 1871 | } |
| 1872 | cm_id_priv->id.state = IB_CM_ESTABLISHED; |
| 1873 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1874 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1875 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 1876 | if (!ret) |
| 1877 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 1878 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1879 | |
| 1880 | if (ret) |
| 1881 | cm_process_work(cm_id_priv, work); |
| 1882 | else |
| 1883 | cm_deref_id(cm_id_priv); |
| 1884 | return 0; |
| 1885 | out: |
| 1886 | cm_deref_id(cm_id_priv); |
| 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | |
| 1890 | static void cm_format_dreq(struct cm_dreq_msg *dreq_msg, |
| 1891 | struct cm_id_private *cm_id_priv, |
| 1892 | const void *private_data, |
| 1893 | u8 private_data_len) |
| 1894 | { |
| 1895 | cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID, |
| 1896 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ)); |
| 1897 | dreq_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1898 | dreq_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1899 | cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn); |
| 1900 | |
| 1901 | if (private_data && private_data_len) |
| 1902 | memcpy(dreq_msg->private_data, private_data, private_data_len); |
| 1903 | } |
| 1904 | |
| 1905 | int ib_send_cm_dreq(struct ib_cm_id *cm_id, |
| 1906 | const void *private_data, |
| 1907 | u8 private_data_len) |
| 1908 | { |
| 1909 | struct cm_id_private *cm_id_priv; |
| 1910 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1911 | unsigned long flags; |
| 1912 | int ret; |
| 1913 | |
| 1914 | if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE) |
| 1915 | return -EINVAL; |
| 1916 | |
| 1917 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1918 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 1919 | if (cm_id->state != IB_CM_ESTABLISHED) { |
| 1920 | ret = -EINVAL; |
| 1921 | goto out; |
| 1922 | } |
| 1923 | |
| 1924 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 1925 | if (ret) { |
| 1926 | cm_enter_timewait(cm_id_priv); |
| 1927 | goto out; |
| 1928 | } |
| 1929 | |
| 1930 | cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv, |
| 1931 | private_data, private_data_len); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1932 | msg->timeout_ms = cm_id_priv->timeout_ms; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1933 | msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT; |
| 1934 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1935 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1936 | if (ret) { |
| 1937 | cm_enter_timewait(cm_id_priv); |
| 1938 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1939 | cm_free_msg(msg); |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | cm_id->state = IB_CM_DREQ_SENT; |
| 1944 | cm_id_priv->msg = msg; |
| 1945 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1946 | return ret; |
| 1947 | } |
| 1948 | EXPORT_SYMBOL(ib_send_cm_dreq); |
| 1949 | |
| 1950 | static void cm_format_drep(struct cm_drep_msg *drep_msg, |
| 1951 | struct cm_id_private *cm_id_priv, |
| 1952 | const void *private_data, |
| 1953 | u8 private_data_len) |
| 1954 | { |
| 1955 | cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid); |
| 1956 | drep_msg->local_comm_id = cm_id_priv->id.local_id; |
| 1957 | drep_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 1958 | |
| 1959 | if (private_data && private_data_len) |
| 1960 | memcpy(drep_msg->private_data, private_data, private_data_len); |
| 1961 | } |
| 1962 | |
| 1963 | int ib_send_cm_drep(struct ib_cm_id *cm_id, |
| 1964 | const void *private_data, |
| 1965 | u8 private_data_len) |
| 1966 | { |
| 1967 | struct cm_id_private *cm_id_priv; |
| 1968 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1969 | unsigned long flags; |
| 1970 | void *data; |
| 1971 | int ret; |
| 1972 | |
| 1973 | if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE) |
| 1974 | return -EINVAL; |
| 1975 | |
| 1976 | data = cm_copy_private_data(private_data, private_data_len); |
| 1977 | if (IS_ERR(data)) |
| 1978 | return PTR_ERR(data); |
| 1979 | |
| 1980 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 1981 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 1982 | if (cm_id->state != IB_CM_DREQ_RCVD) { |
| 1983 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 1984 | kfree(data); |
| 1985 | return -EINVAL; |
| 1986 | } |
| 1987 | |
| 1988 | cm_set_private_data(cm_id_priv, data, private_data_len); |
| 1989 | cm_enter_timewait(cm_id_priv); |
| 1990 | |
| 1991 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 1992 | if (ret) |
| 1993 | goto out; |
| 1994 | |
| 1995 | cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, |
| 1996 | private_data, private_data_len); |
| 1997 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1998 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 1999 | if (ret) { |
| 2000 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2001 | cm_free_msg(msg); |
| 2002 | return ret; |
| 2003 | } |
| 2004 | |
| 2005 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2006 | return ret; |
| 2007 | } |
| 2008 | EXPORT_SYMBOL(ib_send_cm_drep); |
| 2009 | |
Sean Hefty | 82a9c16 | 2006-10-04 11:37:25 -0700 | [diff] [blame] | 2010 | static int cm_issue_drep(struct cm_port *port, |
| 2011 | struct ib_mad_recv_wc *mad_recv_wc) |
| 2012 | { |
| 2013 | struct ib_mad_send_buf *msg = NULL; |
| 2014 | struct cm_dreq_msg *dreq_msg; |
| 2015 | struct cm_drep_msg *drep_msg; |
| 2016 | int ret; |
| 2017 | |
| 2018 | ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); |
| 2019 | if (ret) |
| 2020 | return ret; |
| 2021 | |
| 2022 | dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad; |
| 2023 | drep_msg = (struct cm_drep_msg *) msg->mad; |
| 2024 | |
| 2025 | cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid); |
| 2026 | drep_msg->remote_comm_id = dreq_msg->local_comm_id; |
| 2027 | drep_msg->local_comm_id = dreq_msg->remote_comm_id; |
| 2028 | |
| 2029 | ret = ib_post_send_mad(msg, NULL); |
| 2030 | if (ret) |
| 2031 | cm_free_msg(msg); |
| 2032 | |
| 2033 | return ret; |
| 2034 | } |
| 2035 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2036 | static int cm_dreq_handler(struct cm_work *work) |
| 2037 | { |
| 2038 | struct cm_id_private *cm_id_priv; |
| 2039 | struct cm_dreq_msg *dreq_msg; |
| 2040 | struct ib_mad_send_buf *msg = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2041 | int ret; |
| 2042 | |
| 2043 | dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2044 | cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id, |
| 2045 | dreq_msg->local_comm_id); |
Sean Hefty | 82a9c16 | 2006-10-04 11:37:25 -0700 | [diff] [blame] | 2046 | if (!cm_id_priv) { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2047 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2048 | counter[CM_DREQ_COUNTER]); |
Sean Hefty | 82a9c16 | 2006-10-04 11:37:25 -0700 | [diff] [blame] | 2049 | cm_issue_drep(work->port, work->mad_recv_wc); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2050 | return -EINVAL; |
Sean Hefty | 82a9c16 | 2006-10-04 11:37:25 -0700 | [diff] [blame] | 2051 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2052 | |
| 2053 | work->cm_event.private_data = &dreq_msg->private_data; |
| 2054 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2055 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2056 | if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg)) |
| 2057 | goto unlock; |
| 2058 | |
| 2059 | switch (cm_id_priv->id.state) { |
| 2060 | case IB_CM_REP_SENT: |
| 2061 | case IB_CM_DREQ_SENT: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2062 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | case IB_CM_ESTABLISHED: |
| 2065 | case IB_CM_MRA_REP_RCVD: |
| 2066 | break; |
| 2067 | case IB_CM_TIMEWAIT: |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2068 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2069 | counter[CM_DREQ_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2070 | if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) |
| 2071 | goto unlock; |
| 2072 | |
| 2073 | cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, |
| 2074 | cm_id_priv->private_data, |
| 2075 | cm_id_priv->private_data_len); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2076 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2077 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2078 | if (ib_post_send_mad(msg, NULL)) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2079 | cm_free_msg(msg); |
| 2080 | goto deref; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2081 | case IB_CM_DREQ_RCVD: |
| 2082 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2083 | counter[CM_DREQ_COUNTER]); |
| 2084 | goto unlock; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2085 | default: |
| 2086 | goto unlock; |
| 2087 | } |
| 2088 | cm_id_priv->id.state = IB_CM_DREQ_RCVD; |
| 2089 | cm_id_priv->tid = dreq_msg->hdr.tid; |
| 2090 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2091 | if (!ret) |
| 2092 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2093 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2094 | |
| 2095 | if (ret) |
| 2096 | cm_process_work(cm_id_priv, work); |
| 2097 | else |
| 2098 | cm_deref_id(cm_id_priv); |
| 2099 | return 0; |
| 2100 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2101 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2102 | deref: cm_deref_id(cm_id_priv); |
| 2103 | return -EINVAL; |
| 2104 | } |
| 2105 | |
| 2106 | static int cm_drep_handler(struct cm_work *work) |
| 2107 | { |
| 2108 | struct cm_id_private *cm_id_priv; |
| 2109 | struct cm_drep_msg *drep_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2110 | int ret; |
| 2111 | |
| 2112 | drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2113 | cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id, |
| 2114 | drep_msg->local_comm_id); |
| 2115 | if (!cm_id_priv) |
| 2116 | return -EINVAL; |
| 2117 | |
| 2118 | work->cm_event.private_data = &drep_msg->private_data; |
| 2119 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2120 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2121 | if (cm_id_priv->id.state != IB_CM_DREQ_SENT && |
| 2122 | cm_id_priv->id.state != IB_CM_DREQ_RCVD) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2123 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2124 | goto out; |
| 2125 | } |
| 2126 | cm_enter_timewait(cm_id_priv); |
| 2127 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2128 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2129 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2130 | if (!ret) |
| 2131 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2132 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2133 | |
| 2134 | if (ret) |
| 2135 | cm_process_work(cm_id_priv, work); |
| 2136 | else |
| 2137 | cm_deref_id(cm_id_priv); |
| 2138 | return 0; |
| 2139 | out: |
| 2140 | cm_deref_id(cm_id_priv); |
| 2141 | return -EINVAL; |
| 2142 | } |
| 2143 | |
| 2144 | int ib_send_cm_rej(struct ib_cm_id *cm_id, |
| 2145 | enum ib_cm_rej_reason reason, |
| 2146 | void *ari, |
| 2147 | u8 ari_length, |
| 2148 | const void *private_data, |
| 2149 | u8 private_data_len) |
| 2150 | { |
| 2151 | struct cm_id_private *cm_id_priv; |
| 2152 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2153 | unsigned long flags; |
| 2154 | int ret; |
| 2155 | |
| 2156 | if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) || |
| 2157 | (ari && ari_length > IB_CM_REJ_ARI_LENGTH)) |
| 2158 | return -EINVAL; |
| 2159 | |
| 2160 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2161 | |
| 2162 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2163 | switch (cm_id->state) { |
| 2164 | case IB_CM_REQ_SENT: |
| 2165 | case IB_CM_MRA_REQ_RCVD: |
| 2166 | case IB_CM_REQ_RCVD: |
| 2167 | case IB_CM_MRA_REQ_SENT: |
| 2168 | case IB_CM_REP_RCVD: |
| 2169 | case IB_CM_MRA_REP_SENT: |
| 2170 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2171 | if (!ret) |
| 2172 | cm_format_rej((struct cm_rej_msg *) msg->mad, |
| 2173 | cm_id_priv, reason, ari, ari_length, |
| 2174 | private_data, private_data_len); |
| 2175 | |
| 2176 | cm_reset_to_idle(cm_id_priv); |
| 2177 | break; |
| 2178 | case IB_CM_REP_SENT: |
| 2179 | case IB_CM_MRA_REP_RCVD: |
| 2180 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2181 | if (!ret) |
| 2182 | cm_format_rej((struct cm_rej_msg *) msg->mad, |
| 2183 | cm_id_priv, reason, ari, ari_length, |
| 2184 | private_data, private_data_len); |
| 2185 | |
| 2186 | cm_enter_timewait(cm_id_priv); |
| 2187 | break; |
| 2188 | default: |
| 2189 | ret = -EINVAL; |
| 2190 | goto out; |
| 2191 | } |
| 2192 | |
| 2193 | if (ret) |
| 2194 | goto out; |
| 2195 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2196 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2197 | if (ret) |
| 2198 | cm_free_msg(msg); |
| 2199 | |
| 2200 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2201 | return ret; |
| 2202 | } |
| 2203 | EXPORT_SYMBOL(ib_send_cm_rej); |
| 2204 | |
| 2205 | static void cm_format_rej_event(struct cm_work *work) |
| 2206 | { |
| 2207 | struct cm_rej_msg *rej_msg; |
| 2208 | struct ib_cm_rej_event_param *param; |
| 2209 | |
| 2210 | rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2211 | param = &work->cm_event.param.rej_rcvd; |
| 2212 | param->ari = rej_msg->ari; |
| 2213 | param->ari_length = cm_rej_get_reject_info_len(rej_msg); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2214 | param->reason = __be16_to_cpu(rej_msg->reason); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2215 | work->cm_event.private_data = &rej_msg->private_data; |
| 2216 | } |
| 2217 | |
| 2218 | static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg) |
| 2219 | { |
| 2220 | struct cm_timewait_info *timewait_info; |
| 2221 | struct cm_id_private *cm_id_priv; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2222 | __be32 remote_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2223 | |
| 2224 | remote_id = rej_msg->local_comm_id; |
| 2225 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2226 | if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2227 | spin_lock_irq(&cm.lock); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2228 | timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari), |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2229 | remote_id); |
| 2230 | if (!timewait_info) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2231 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2232 | return NULL; |
| 2233 | } |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 2234 | cm_id_priv = idr_find(&cm.local_id_table, (__force int) |
| 2235 | (timewait_info->work.local_id ^ |
| 2236 | cm.random_id_operand)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2237 | if (cm_id_priv) { |
| 2238 | if (cm_id_priv->id.remote_id == remote_id) |
| 2239 | atomic_inc(&cm_id_priv->refcount); |
| 2240 | else |
| 2241 | cm_id_priv = NULL; |
| 2242 | } |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2243 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2244 | } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ) |
| 2245 | cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0); |
| 2246 | else |
| 2247 | cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id); |
| 2248 | |
| 2249 | return cm_id_priv; |
| 2250 | } |
| 2251 | |
| 2252 | static int cm_rej_handler(struct cm_work *work) |
| 2253 | { |
| 2254 | struct cm_id_private *cm_id_priv; |
| 2255 | struct cm_rej_msg *rej_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2256 | int ret; |
| 2257 | |
| 2258 | rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2259 | cm_id_priv = cm_acquire_rejected_id(rej_msg); |
| 2260 | if (!cm_id_priv) |
| 2261 | return -EINVAL; |
| 2262 | |
| 2263 | cm_format_rej_event(work); |
| 2264 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2265 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2266 | switch (cm_id_priv->id.state) { |
| 2267 | case IB_CM_REQ_SENT: |
| 2268 | case IB_CM_MRA_REQ_RCVD: |
| 2269 | case IB_CM_REP_SENT: |
| 2270 | case IB_CM_MRA_REP_RCVD: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2271 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2272 | /* fall through */ |
| 2273 | case IB_CM_REQ_RCVD: |
| 2274 | case IB_CM_MRA_REQ_SENT: |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2275 | if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2276 | cm_enter_timewait(cm_id_priv); |
| 2277 | else |
| 2278 | cm_reset_to_idle(cm_id_priv); |
| 2279 | break; |
| 2280 | case IB_CM_DREQ_SENT: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2281 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2282 | /* fall through */ |
| 2283 | case IB_CM_REP_RCVD: |
| 2284 | case IB_CM_MRA_REP_SENT: |
| 2285 | case IB_CM_ESTABLISHED: |
| 2286 | cm_enter_timewait(cm_id_priv); |
| 2287 | break; |
| 2288 | default: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2289 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2290 | ret = -EINVAL; |
| 2291 | goto out; |
| 2292 | } |
| 2293 | |
| 2294 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2295 | if (!ret) |
| 2296 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2297 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2298 | |
| 2299 | if (ret) |
| 2300 | cm_process_work(cm_id_priv, work); |
| 2301 | else |
| 2302 | cm_deref_id(cm_id_priv); |
| 2303 | return 0; |
| 2304 | out: |
| 2305 | cm_deref_id(cm_id_priv); |
| 2306 | return -EINVAL; |
| 2307 | } |
| 2308 | |
| 2309 | int ib_send_cm_mra(struct ib_cm_id *cm_id, |
| 2310 | u8 service_timeout, |
| 2311 | const void *private_data, |
| 2312 | u8 private_data_len) |
| 2313 | { |
| 2314 | struct cm_id_private *cm_id_priv; |
| 2315 | struct ib_mad_send_buf *msg; |
Sean Hefty | de98b69 | 2007-08-01 13:49:53 -0700 | [diff] [blame] | 2316 | enum ib_cm_state cm_state; |
| 2317 | enum ib_cm_lap_state lap_state; |
| 2318 | enum cm_msg_response msg_response; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2319 | void *data; |
| 2320 | unsigned long flags; |
| 2321 | int ret; |
| 2322 | |
| 2323 | if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE) |
| 2324 | return -EINVAL; |
| 2325 | |
| 2326 | data = cm_copy_private_data(private_data, private_data_len); |
| 2327 | if (IS_ERR(data)) |
| 2328 | return PTR_ERR(data); |
| 2329 | |
| 2330 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2331 | |
| 2332 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2333 | switch(cm_id_priv->id.state) { |
| 2334 | case IB_CM_REQ_RCVD: |
Sean Hefty | de98b69 | 2007-08-01 13:49:53 -0700 | [diff] [blame] | 2335 | cm_state = IB_CM_MRA_REQ_SENT; |
| 2336 | lap_state = cm_id->lap_state; |
| 2337 | msg_response = CM_MSG_RESPONSE_REQ; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2338 | break; |
| 2339 | case IB_CM_REP_RCVD: |
Sean Hefty | de98b69 | 2007-08-01 13:49:53 -0700 | [diff] [blame] | 2340 | cm_state = IB_CM_MRA_REP_SENT; |
| 2341 | lap_state = cm_id->lap_state; |
| 2342 | msg_response = CM_MSG_RESPONSE_REP; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | case IB_CM_ESTABLISHED: |
Sean Hefty | de98b69 | 2007-08-01 13:49:53 -0700 | [diff] [blame] | 2345 | cm_state = cm_id->state; |
| 2346 | lap_state = IB_CM_MRA_LAP_SENT; |
| 2347 | msg_response = CM_MSG_RESPONSE_OTHER; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2348 | break; |
| 2349 | default: |
| 2350 | ret = -EINVAL; |
| 2351 | goto error1; |
| 2352 | } |
Sean Hefty | de98b69 | 2007-08-01 13:49:53 -0700 | [diff] [blame] | 2353 | |
| 2354 | if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) { |
| 2355 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2356 | if (ret) |
| 2357 | goto error1; |
| 2358 | |
| 2359 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, |
| 2360 | msg_response, service_timeout, |
| 2361 | private_data, private_data_len); |
| 2362 | ret = ib_post_send_mad(msg, NULL); |
| 2363 | if (ret) |
| 2364 | goto error2; |
| 2365 | } |
| 2366 | |
| 2367 | cm_id->state = cm_state; |
| 2368 | cm_id->lap_state = lap_state; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2369 | cm_id_priv->service_timeout = service_timeout; |
| 2370 | cm_set_private_data(cm_id_priv, data, private_data_len); |
| 2371 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2372 | return 0; |
| 2373 | |
| 2374 | error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2375 | kfree(data); |
| 2376 | return ret; |
| 2377 | |
| 2378 | error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2379 | kfree(data); |
| 2380 | cm_free_msg(msg); |
| 2381 | return ret; |
| 2382 | } |
| 2383 | EXPORT_SYMBOL(ib_send_cm_mra); |
| 2384 | |
| 2385 | static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg) |
| 2386 | { |
| 2387 | switch (cm_mra_get_msg_mraed(mra_msg)) { |
| 2388 | case CM_MSG_RESPONSE_REQ: |
| 2389 | return cm_acquire_id(mra_msg->remote_comm_id, 0); |
| 2390 | case CM_MSG_RESPONSE_REP: |
| 2391 | case CM_MSG_RESPONSE_OTHER: |
| 2392 | return cm_acquire_id(mra_msg->remote_comm_id, |
| 2393 | mra_msg->local_comm_id); |
| 2394 | default: |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | static int cm_mra_handler(struct cm_work *work) |
| 2400 | { |
| 2401 | struct cm_id_private *cm_id_priv; |
| 2402 | struct cm_mra_msg *mra_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2403 | int timeout, ret; |
| 2404 | |
| 2405 | mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2406 | cm_id_priv = cm_acquire_mraed_id(mra_msg); |
| 2407 | if (!cm_id_priv) |
| 2408 | return -EINVAL; |
| 2409 | |
| 2410 | work->cm_event.private_data = &mra_msg->private_data; |
| 2411 | work->cm_event.param.mra_rcvd.service_timeout = |
| 2412 | cm_mra_get_service_timeout(mra_msg); |
| 2413 | timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) + |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 2414 | cm_convert_to_ms(cm_id_priv->av.timeout); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2415 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2416 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2417 | switch (cm_id_priv->id.state) { |
| 2418 | case IB_CM_REQ_SENT: |
| 2419 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ || |
| 2420 | ib_modify_mad(cm_id_priv->av.port->mad_agent, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2421 | cm_id_priv->msg, timeout)) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2422 | goto out; |
| 2423 | cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD; |
| 2424 | break; |
| 2425 | case IB_CM_REP_SENT: |
| 2426 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP || |
| 2427 | ib_modify_mad(cm_id_priv->av.port->mad_agent, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2428 | cm_id_priv->msg, timeout)) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2429 | goto out; |
| 2430 | cm_id_priv->id.state = IB_CM_MRA_REP_RCVD; |
| 2431 | break; |
| 2432 | case IB_CM_ESTABLISHED: |
| 2433 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER || |
| 2434 | cm_id_priv->id.lap_state != IB_CM_LAP_SENT || |
| 2435 | ib_modify_mad(cm_id_priv->av.port->mad_agent, |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2436 | cm_id_priv->msg, timeout)) { |
| 2437 | if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD) |
| 2438 | atomic_long_inc(&work->port-> |
| 2439 | counter_group[CM_RECV_DUPLICATES]. |
| 2440 | counter[CM_MRA_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2441 | goto out; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2442 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2443 | cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD; |
| 2444 | break; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2445 | case IB_CM_MRA_REQ_RCVD: |
| 2446 | case IB_CM_MRA_REP_RCVD: |
| 2447 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2448 | counter[CM_MRA_COUNTER]); |
| 2449 | /* fall through */ |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2450 | default: |
| 2451 | goto out; |
| 2452 | } |
| 2453 | |
| 2454 | cm_id_priv->msg->context[1] = (void *) (unsigned long) |
| 2455 | cm_id_priv->id.state; |
| 2456 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2457 | if (!ret) |
| 2458 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2459 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2460 | |
| 2461 | if (ret) |
| 2462 | cm_process_work(cm_id_priv, work); |
| 2463 | else |
| 2464 | cm_deref_id(cm_id_priv); |
| 2465 | return 0; |
| 2466 | out: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2467 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2468 | cm_deref_id(cm_id_priv); |
| 2469 | return -EINVAL; |
| 2470 | } |
| 2471 | |
| 2472 | static void cm_format_lap(struct cm_lap_msg *lap_msg, |
| 2473 | struct cm_id_private *cm_id_priv, |
| 2474 | struct ib_sa_path_rec *alternate_path, |
| 2475 | const void *private_data, |
| 2476 | u8 private_data_len) |
| 2477 | { |
| 2478 | cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID, |
| 2479 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP)); |
| 2480 | lap_msg->local_comm_id = cm_id_priv->id.local_id; |
| 2481 | lap_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 2482 | cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn); |
| 2483 | /* todo: need remote CM response timeout */ |
| 2484 | cm_lap_set_remote_resp_timeout(lap_msg, 0x1F); |
| 2485 | lap_msg->alt_local_lid = alternate_path->slid; |
| 2486 | lap_msg->alt_remote_lid = alternate_path->dlid; |
| 2487 | lap_msg->alt_local_gid = alternate_path->sgid; |
| 2488 | lap_msg->alt_remote_gid = alternate_path->dgid; |
| 2489 | cm_lap_set_flow_label(lap_msg, alternate_path->flow_label); |
| 2490 | cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class); |
| 2491 | lap_msg->alt_hop_limit = alternate_path->hop_limit; |
| 2492 | cm_lap_set_packet_rate(lap_msg, alternate_path->rate); |
| 2493 | cm_lap_set_sl(lap_msg, alternate_path->sl); |
| 2494 | cm_lap_set_subnet_local(lap_msg, 1); /* local only... */ |
| 2495 | cm_lap_set_local_ack_timeout(lap_msg, |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 2496 | cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay, |
| 2497 | alternate_path->packet_life_time)); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2498 | |
| 2499 | if (private_data && private_data_len) |
| 2500 | memcpy(lap_msg->private_data, private_data, private_data_len); |
| 2501 | } |
| 2502 | |
| 2503 | int ib_send_cm_lap(struct ib_cm_id *cm_id, |
| 2504 | struct ib_sa_path_rec *alternate_path, |
| 2505 | const void *private_data, |
| 2506 | u8 private_data_len) |
| 2507 | { |
| 2508 | struct cm_id_private *cm_id_priv; |
| 2509 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2510 | unsigned long flags; |
| 2511 | int ret; |
| 2512 | |
| 2513 | if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE) |
| 2514 | return -EINVAL; |
| 2515 | |
| 2516 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2517 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2518 | if (cm_id->state != IB_CM_ESTABLISHED || |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2519 | (cm_id->lap_state != IB_CM_LAP_UNINIT && |
| 2520 | cm_id->lap_state != IB_CM_LAP_IDLE)) { |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2521 | ret = -EINVAL; |
| 2522 | goto out; |
| 2523 | } |
| 2524 | |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2525 | ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av); |
| 2526 | if (ret) |
| 2527 | goto out; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 2528 | cm_id_priv->alt_av.timeout = |
| 2529 | cm_ack_timeout(cm_id_priv->target_ack_delay, |
| 2530 | cm_id_priv->alt_av.timeout - 1); |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2531 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2532 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2533 | if (ret) |
| 2534 | goto out; |
| 2535 | |
| 2536 | cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv, |
| 2537 | alternate_path, private_data, private_data_len); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2538 | msg->timeout_ms = cm_id_priv->timeout_ms; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2539 | msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED; |
| 2540 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2541 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2542 | if (ret) { |
| 2543 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2544 | cm_free_msg(msg); |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | cm_id->lap_state = IB_CM_LAP_SENT; |
| 2549 | cm_id_priv->msg = msg; |
| 2550 | |
| 2551 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2552 | return ret; |
| 2553 | } |
| 2554 | EXPORT_SYMBOL(ib_send_cm_lap); |
| 2555 | |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2556 | static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv, |
| 2557 | struct ib_sa_path_rec *path, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2558 | struct cm_lap_msg *lap_msg) |
| 2559 | { |
| 2560 | memset(path, 0, sizeof *path); |
| 2561 | path->dgid = lap_msg->alt_local_gid; |
| 2562 | path->sgid = lap_msg->alt_remote_gid; |
| 2563 | path->dlid = lap_msg->alt_local_lid; |
| 2564 | path->slid = lap_msg->alt_remote_lid; |
| 2565 | path->flow_label = cm_lap_get_flow_label(lap_msg); |
| 2566 | path->hop_limit = lap_msg->alt_hop_limit; |
| 2567 | path->traffic_class = cm_lap_get_traffic_class(lap_msg); |
| 2568 | path->reversible = 1; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2569 | path->pkey = cm_id_priv->pkey; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2570 | path->sl = cm_lap_get_sl(lap_msg); |
| 2571 | path->mtu_selector = IB_SA_EQ; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2572 | path->mtu = cm_id_priv->path_mtu; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2573 | path->rate_selector = IB_SA_EQ; |
| 2574 | path->rate = cm_lap_get_packet_rate(lap_msg); |
| 2575 | path->packet_life_time_selector = IB_SA_EQ; |
| 2576 | path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg); |
| 2577 | path->packet_life_time -= (path->packet_life_time > 0); |
| 2578 | } |
| 2579 | |
| 2580 | static int cm_lap_handler(struct cm_work *work) |
| 2581 | { |
| 2582 | struct cm_id_private *cm_id_priv; |
| 2583 | struct cm_lap_msg *lap_msg; |
| 2584 | struct ib_cm_lap_event_param *param; |
| 2585 | struct ib_mad_send_buf *msg = NULL; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2586 | int ret; |
| 2587 | |
| 2588 | /* todo: verify LAP request and send reject APR if invalid. */ |
| 2589 | lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2590 | cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id, |
| 2591 | lap_msg->local_comm_id); |
| 2592 | if (!cm_id_priv) |
| 2593 | return -EINVAL; |
| 2594 | |
| 2595 | param = &work->cm_event.param.lap_rcvd; |
| 2596 | param->alternate_path = &work->path[0]; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2597 | cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2598 | work->cm_event.private_data = &lap_msg->private_data; |
| 2599 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2600 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2601 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED) |
| 2602 | goto unlock; |
| 2603 | |
| 2604 | switch (cm_id_priv->id.lap_state) { |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2605 | case IB_CM_LAP_UNINIT: |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2606 | case IB_CM_LAP_IDLE: |
| 2607 | break; |
| 2608 | case IB_CM_MRA_LAP_SENT: |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2609 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2610 | counter[CM_LAP_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2611 | if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) |
| 2612 | goto unlock; |
| 2613 | |
| 2614 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, |
| 2615 | CM_MSG_RESPONSE_OTHER, |
| 2616 | cm_id_priv->service_timeout, |
| 2617 | cm_id_priv->private_data, |
| 2618 | cm_id_priv->private_data_len); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2619 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2620 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2621 | if (ib_post_send_mad(msg, NULL)) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2622 | cm_free_msg(msg); |
| 2623 | goto deref; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2624 | case IB_CM_LAP_RCVD: |
| 2625 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2626 | counter[CM_LAP_COUNTER]); |
| 2627 | goto unlock; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2628 | default: |
| 2629 | goto unlock; |
| 2630 | } |
| 2631 | |
| 2632 | cm_id_priv->id.lap_state = IB_CM_LAP_RCVD; |
| 2633 | cm_id_priv->tid = lap_msg->hdr.tid; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 2634 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, |
| 2635 | work->mad_recv_wc->recv_buf.grh, |
| 2636 | &cm_id_priv->av); |
| 2637 | cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2638 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2639 | if (!ret) |
| 2640 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2641 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2642 | |
| 2643 | if (ret) |
| 2644 | cm_process_work(cm_id_priv, work); |
| 2645 | else |
| 2646 | cm_deref_id(cm_id_priv); |
| 2647 | return 0; |
| 2648 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2649 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2650 | deref: cm_deref_id(cm_id_priv); |
| 2651 | return -EINVAL; |
| 2652 | } |
| 2653 | |
| 2654 | static void cm_format_apr(struct cm_apr_msg *apr_msg, |
| 2655 | struct cm_id_private *cm_id_priv, |
| 2656 | enum ib_cm_apr_status status, |
| 2657 | void *info, |
| 2658 | u8 info_length, |
| 2659 | const void *private_data, |
| 2660 | u8 private_data_len) |
| 2661 | { |
| 2662 | cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid); |
| 2663 | apr_msg->local_comm_id = cm_id_priv->id.local_id; |
| 2664 | apr_msg->remote_comm_id = cm_id_priv->id.remote_id; |
| 2665 | apr_msg->ap_status = (u8) status; |
| 2666 | |
| 2667 | if (info && info_length) { |
| 2668 | apr_msg->info_length = info_length; |
| 2669 | memcpy(apr_msg->info, info, info_length); |
| 2670 | } |
| 2671 | |
| 2672 | if (private_data && private_data_len) |
| 2673 | memcpy(apr_msg->private_data, private_data, private_data_len); |
| 2674 | } |
| 2675 | |
| 2676 | int ib_send_cm_apr(struct ib_cm_id *cm_id, |
| 2677 | enum ib_cm_apr_status status, |
| 2678 | void *info, |
| 2679 | u8 info_length, |
| 2680 | const void *private_data, |
| 2681 | u8 private_data_len) |
| 2682 | { |
| 2683 | struct cm_id_private *cm_id_priv; |
| 2684 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2685 | unsigned long flags; |
| 2686 | int ret; |
| 2687 | |
| 2688 | if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) || |
| 2689 | (info && info_length > IB_CM_APR_INFO_LENGTH)) |
| 2690 | return -EINVAL; |
| 2691 | |
| 2692 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2693 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2694 | if (cm_id->state != IB_CM_ESTABLISHED || |
| 2695 | (cm_id->lap_state != IB_CM_LAP_RCVD && |
| 2696 | cm_id->lap_state != IB_CM_MRA_LAP_SENT)) { |
| 2697 | ret = -EINVAL; |
| 2698 | goto out; |
| 2699 | } |
| 2700 | |
| 2701 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2702 | if (ret) |
| 2703 | goto out; |
| 2704 | |
| 2705 | cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status, |
| 2706 | info, info_length, private_data, private_data_len); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2707 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2708 | if (ret) { |
| 2709 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2710 | cm_free_msg(msg); |
| 2711 | return ret; |
| 2712 | } |
| 2713 | |
| 2714 | cm_id->lap_state = IB_CM_LAP_IDLE; |
| 2715 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2716 | return ret; |
| 2717 | } |
| 2718 | EXPORT_SYMBOL(ib_send_cm_apr); |
| 2719 | |
| 2720 | static int cm_apr_handler(struct cm_work *work) |
| 2721 | { |
| 2722 | struct cm_id_private *cm_id_priv; |
| 2723 | struct cm_apr_msg *apr_msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2724 | int ret; |
| 2725 | |
| 2726 | apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad; |
| 2727 | cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id, |
| 2728 | apr_msg->local_comm_id); |
| 2729 | if (!cm_id_priv) |
| 2730 | return -EINVAL; /* Unmatched reply. */ |
| 2731 | |
| 2732 | work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status; |
| 2733 | work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info; |
| 2734 | work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length; |
| 2735 | work->cm_event.private_data = &apr_msg->private_data; |
| 2736 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2737 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2738 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED || |
| 2739 | (cm_id_priv->id.lap_state != IB_CM_LAP_SENT && |
| 2740 | cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2741 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2742 | goto out; |
| 2743 | } |
| 2744 | cm_id_priv->id.lap_state = IB_CM_LAP_IDLE; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2745 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2746 | cm_id_priv->msg = NULL; |
| 2747 | |
| 2748 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2749 | if (!ret) |
| 2750 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2751 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2752 | |
| 2753 | if (ret) |
| 2754 | cm_process_work(cm_id_priv, work); |
| 2755 | else |
| 2756 | cm_deref_id(cm_id_priv); |
| 2757 | return 0; |
| 2758 | out: |
| 2759 | cm_deref_id(cm_id_priv); |
| 2760 | return -EINVAL; |
| 2761 | } |
| 2762 | |
| 2763 | static int cm_timewait_handler(struct cm_work *work) |
| 2764 | { |
| 2765 | struct cm_timewait_info *timewait_info; |
| 2766 | struct cm_id_private *cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2767 | int ret; |
| 2768 | |
| 2769 | timewait_info = (struct cm_timewait_info *)work; |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 2770 | spin_lock_irq(&cm.lock); |
| 2771 | list_del(&timewait_info->list); |
| 2772 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2773 | |
| 2774 | cm_id_priv = cm_acquire_id(timewait_info->work.local_id, |
| 2775 | timewait_info->work.remote_id); |
| 2776 | if (!cm_id_priv) |
| 2777 | return -EINVAL; |
| 2778 | |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 2779 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2780 | if (cm_id_priv->id.state != IB_CM_TIMEWAIT || |
| 2781 | cm_id_priv->remote_qpn != timewait_info->remote_qpn) { |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 2782 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2783 | goto out; |
| 2784 | } |
| 2785 | cm_id_priv->id.state = IB_CM_IDLE; |
| 2786 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
| 2787 | if (!ret) |
| 2788 | list_add_tail(&work->list, &cm_id_priv->work_list); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 2789 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2790 | |
| 2791 | if (ret) |
| 2792 | cm_process_work(cm_id_priv, work); |
| 2793 | else |
| 2794 | cm_deref_id(cm_id_priv); |
| 2795 | return 0; |
| 2796 | out: |
| 2797 | cm_deref_id(cm_id_priv); |
| 2798 | return -EINVAL; |
| 2799 | } |
| 2800 | |
| 2801 | static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg, |
| 2802 | struct cm_id_private *cm_id_priv, |
| 2803 | struct ib_cm_sidr_req_param *param) |
| 2804 | { |
| 2805 | cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID, |
| 2806 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR)); |
| 2807 | sidr_req_msg->request_id = cm_id_priv->id.local_id; |
Sean Hefty | 75af908 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 2808 | sidr_req_msg->pkey = cpu_to_be16(param->path->pkey); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2809 | sidr_req_msg->service_id = param->service_id; |
| 2810 | |
| 2811 | if (param->private_data && param->private_data_len) |
| 2812 | memcpy(sidr_req_msg->private_data, param->private_data, |
| 2813 | param->private_data_len); |
| 2814 | } |
| 2815 | |
| 2816 | int ib_send_cm_sidr_req(struct ib_cm_id *cm_id, |
| 2817 | struct ib_cm_sidr_req_param *param) |
| 2818 | { |
| 2819 | struct cm_id_private *cm_id_priv; |
| 2820 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2821 | unsigned long flags; |
| 2822 | int ret; |
| 2823 | |
| 2824 | if (!param->path || (param->private_data && |
| 2825 | param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE)) |
| 2826 | return -EINVAL; |
| 2827 | |
| 2828 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2829 | ret = cm_init_av_by_path(param->path, &cm_id_priv->av); |
| 2830 | if (ret) |
| 2831 | goto out; |
| 2832 | |
| 2833 | cm_id->service_id = param->service_id; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2834 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2835 | cm_id_priv->timeout_ms = param->timeout_ms; |
| 2836 | cm_id_priv->max_cm_retries = param->max_cm_retries; |
| 2837 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2838 | if (ret) |
| 2839 | goto out; |
| 2840 | |
| 2841 | cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv, |
| 2842 | param); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2843 | msg->timeout_ms = cm_id_priv->timeout_ms; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2844 | msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT; |
| 2845 | |
| 2846 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2847 | if (cm_id->state == IB_CM_IDLE) |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2848 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2849 | else |
| 2850 | ret = -EINVAL; |
| 2851 | |
| 2852 | if (ret) { |
| 2853 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2854 | cm_free_msg(msg); |
| 2855 | goto out; |
| 2856 | } |
| 2857 | cm_id->state = IB_CM_SIDR_REQ_SENT; |
| 2858 | cm_id_priv->msg = msg; |
| 2859 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2860 | out: |
| 2861 | return ret; |
| 2862 | } |
| 2863 | EXPORT_SYMBOL(ib_send_cm_sidr_req); |
| 2864 | |
| 2865 | static void cm_format_sidr_req_event(struct cm_work *work, |
| 2866 | struct ib_cm_id *listen_id) |
| 2867 | { |
| 2868 | struct cm_sidr_req_msg *sidr_req_msg; |
| 2869 | struct ib_cm_sidr_req_event_param *param; |
| 2870 | |
| 2871 | sidr_req_msg = (struct cm_sidr_req_msg *) |
| 2872 | work->mad_recv_wc->recv_buf.mad; |
| 2873 | param = &work->cm_event.param.sidr_req_rcvd; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2874 | param->pkey = __be16_to_cpu(sidr_req_msg->pkey); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2875 | param->listen_id = listen_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2876 | param->port = work->port->port_num; |
| 2877 | work->cm_event.private_data = &sidr_req_msg->private_data; |
| 2878 | } |
| 2879 | |
| 2880 | static int cm_sidr_req_handler(struct cm_work *work) |
| 2881 | { |
| 2882 | struct ib_cm_id *cm_id; |
| 2883 | struct cm_id_private *cm_id_priv, *cur_cm_id_priv; |
| 2884 | struct cm_sidr_req_msg *sidr_req_msg; |
| 2885 | struct ib_wc *wc; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2886 | |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 2887 | cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2888 | if (IS_ERR(cm_id)) |
| 2889 | return PTR_ERR(cm_id); |
| 2890 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2891 | |
| 2892 | /* Record SGID/SLID and request ID for lookup. */ |
| 2893 | sidr_req_msg = (struct cm_sidr_req_msg *) |
| 2894 | work->mad_recv_wc->recv_buf.mad; |
| 2895 | wc = work->mad_recv_wc->wc; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2896 | cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2897 | cm_id_priv->av.dgid.global.interface_id = 0; |
| 2898 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, |
Sean Hefty | ca222c6 | 2006-06-17 20:37:40 -0700 | [diff] [blame] | 2899 | work->mad_recv_wc->recv_buf.grh, |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2900 | &cm_id_priv->av); |
| 2901 | cm_id_priv->id.remote_id = sidr_req_msg->request_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2902 | cm_id_priv->tid = sidr_req_msg->hdr.tid; |
| 2903 | atomic_inc(&cm_id_priv->work_count); |
| 2904 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2905 | spin_lock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2906 | cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv); |
| 2907 | if (cur_cm_id_priv) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2908 | spin_unlock_irq(&cm.lock); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 2909 | atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. |
| 2910 | counter[CM_SIDR_REQ_COUNTER]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2911 | goto out; /* Duplicate message. */ |
| 2912 | } |
Sean Hefty | 29c2731 | 2007-07-02 14:36:19 -0700 | [diff] [blame] | 2913 | cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD; |
Sean Hefty | 07d357d | 2005-10-17 15:37:43 -0700 | [diff] [blame] | 2914 | cur_cm_id_priv = cm_find_listen(cm_id->device, |
Sean Hefty | 6e61d04 | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 2915 | sidr_req_msg->service_id, |
| 2916 | sidr_req_msg->private_data); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2917 | if (!cur_cm_id_priv) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2918 | spin_unlock_irq(&cm.lock); |
Sean Hefty | 6164c8c | 2007-07-02 16:14:14 -0700 | [diff] [blame] | 2919 | cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2920 | goto out; /* No match. */ |
| 2921 | } |
| 2922 | atomic_inc(&cur_cm_id_priv->refcount); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 2923 | spin_unlock_irq(&cm.lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2924 | |
| 2925 | cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler; |
| 2926 | cm_id_priv->id.context = cur_cm_id_priv->id.context; |
| 2927 | cm_id_priv->id.service_id = sidr_req_msg->service_id; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2928 | cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2929 | |
| 2930 | cm_format_sidr_req_event(work, &cur_cm_id_priv->id); |
| 2931 | cm_process_work(cm_id_priv, work); |
| 2932 | cm_deref_id(cur_cm_id_priv); |
| 2933 | return 0; |
| 2934 | out: |
| 2935 | ib_destroy_cm_id(&cm_id_priv->id); |
| 2936 | return -EINVAL; |
| 2937 | } |
| 2938 | |
| 2939 | static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg, |
| 2940 | struct cm_id_private *cm_id_priv, |
| 2941 | struct ib_cm_sidr_rep_param *param) |
| 2942 | { |
| 2943 | cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID, |
| 2944 | cm_id_priv->tid); |
| 2945 | sidr_rep_msg->request_id = cm_id_priv->id.remote_id; |
| 2946 | sidr_rep_msg->status = param->status; |
| 2947 | cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num)); |
| 2948 | sidr_rep_msg->service_id = cm_id_priv->id.service_id; |
| 2949 | sidr_rep_msg->qkey = cpu_to_be32(param->qkey); |
| 2950 | |
| 2951 | if (param->info && param->info_length) |
| 2952 | memcpy(sidr_rep_msg->info, param->info, param->info_length); |
| 2953 | |
| 2954 | if (param->private_data && param->private_data_len) |
| 2955 | memcpy(sidr_rep_msg->private_data, param->private_data, |
| 2956 | param->private_data_len); |
| 2957 | } |
| 2958 | |
| 2959 | int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id, |
| 2960 | struct ib_cm_sidr_rep_param *param) |
| 2961 | { |
| 2962 | struct cm_id_private *cm_id_priv; |
| 2963 | struct ib_mad_send_buf *msg; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2964 | unsigned long flags; |
| 2965 | int ret; |
| 2966 | |
| 2967 | if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) || |
| 2968 | (param->private_data && |
| 2969 | param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE)) |
| 2970 | return -EINVAL; |
| 2971 | |
| 2972 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 2973 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 2974 | if (cm_id->state != IB_CM_SIDR_REQ_RCVD) { |
| 2975 | ret = -EINVAL; |
| 2976 | goto error; |
| 2977 | } |
| 2978 | |
| 2979 | ret = cm_alloc_msg(cm_id_priv, &msg); |
| 2980 | if (ret) |
| 2981 | goto error; |
| 2982 | |
| 2983 | cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv, |
| 2984 | param); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2985 | ret = ib_post_send_mad(msg, NULL); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 2986 | if (ret) { |
| 2987 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2988 | cm_free_msg(msg); |
| 2989 | return ret; |
| 2990 | } |
| 2991 | cm_id->state = IB_CM_IDLE; |
| 2992 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 2993 | |
| 2994 | spin_lock_irqsave(&cm.lock, flags); |
| 2995 | rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); |
| 2996 | spin_unlock_irqrestore(&cm.lock, flags); |
| 2997 | return 0; |
| 2998 | |
| 2999 | error: spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3000 | return ret; |
| 3001 | } |
| 3002 | EXPORT_SYMBOL(ib_send_cm_sidr_rep); |
| 3003 | |
| 3004 | static void cm_format_sidr_rep_event(struct cm_work *work) |
| 3005 | { |
| 3006 | struct cm_sidr_rep_msg *sidr_rep_msg; |
| 3007 | struct ib_cm_sidr_rep_event_param *param; |
| 3008 | |
| 3009 | sidr_rep_msg = (struct cm_sidr_rep_msg *) |
| 3010 | work->mad_recv_wc->recv_buf.mad; |
| 3011 | param = &work->cm_event.param.sidr_rep_rcvd; |
| 3012 | param->status = sidr_rep_msg->status; |
| 3013 | param->qkey = be32_to_cpu(sidr_rep_msg->qkey); |
| 3014 | param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg)); |
| 3015 | param->info = &sidr_rep_msg->info; |
| 3016 | param->info_len = sidr_rep_msg->info_length; |
| 3017 | work->cm_event.private_data = &sidr_rep_msg->private_data; |
| 3018 | } |
| 3019 | |
| 3020 | static int cm_sidr_rep_handler(struct cm_work *work) |
| 3021 | { |
| 3022 | struct cm_sidr_rep_msg *sidr_rep_msg; |
| 3023 | struct cm_id_private *cm_id_priv; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3024 | |
| 3025 | sidr_rep_msg = (struct cm_sidr_rep_msg *) |
| 3026 | work->mad_recv_wc->recv_buf.mad; |
| 3027 | cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0); |
| 3028 | if (!cm_id_priv) |
| 3029 | return -EINVAL; /* Unmatched reply. */ |
| 3030 | |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3031 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3032 | if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) { |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3033 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3034 | goto out; |
| 3035 | } |
| 3036 | cm_id_priv->id.state = IB_CM_IDLE; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 3037 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3038 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3039 | |
| 3040 | cm_format_sidr_rep_event(work); |
| 3041 | cm_process_work(cm_id_priv, work); |
| 3042 | return 0; |
| 3043 | out: |
| 3044 | cm_deref_id(cm_id_priv); |
| 3045 | return -EINVAL; |
| 3046 | } |
| 3047 | |
| 3048 | static void cm_process_send_error(struct ib_mad_send_buf *msg, |
| 3049 | enum ib_wc_status wc_status) |
| 3050 | { |
| 3051 | struct cm_id_private *cm_id_priv; |
| 3052 | struct ib_cm_event cm_event; |
| 3053 | enum ib_cm_state state; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3054 | int ret; |
| 3055 | |
| 3056 | memset(&cm_event, 0, sizeof cm_event); |
| 3057 | cm_id_priv = msg->context[0]; |
| 3058 | |
| 3059 | /* Discard old sends or ones without a response. */ |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3060 | spin_lock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3061 | state = (enum ib_cm_state) (unsigned long) msg->context[1]; |
| 3062 | if (msg != cm_id_priv->msg || state != cm_id_priv->id.state) |
| 3063 | goto discard; |
| 3064 | |
| 3065 | switch (state) { |
| 3066 | case IB_CM_REQ_SENT: |
| 3067 | case IB_CM_MRA_REQ_RCVD: |
| 3068 | cm_reset_to_idle(cm_id_priv); |
| 3069 | cm_event.event = IB_CM_REQ_ERROR; |
| 3070 | break; |
| 3071 | case IB_CM_REP_SENT: |
| 3072 | case IB_CM_MRA_REP_RCVD: |
| 3073 | cm_reset_to_idle(cm_id_priv); |
| 3074 | cm_event.event = IB_CM_REP_ERROR; |
| 3075 | break; |
| 3076 | case IB_CM_DREQ_SENT: |
| 3077 | cm_enter_timewait(cm_id_priv); |
| 3078 | cm_event.event = IB_CM_DREQ_ERROR; |
| 3079 | break; |
| 3080 | case IB_CM_SIDR_REQ_SENT: |
| 3081 | cm_id_priv->id.state = IB_CM_IDLE; |
| 3082 | cm_event.event = IB_CM_SIDR_REQ_ERROR; |
| 3083 | break; |
| 3084 | default: |
| 3085 | goto discard; |
| 3086 | } |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3087 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3088 | cm_event.param.send_status = wc_status; |
| 3089 | |
| 3090 | /* No other events can occur on the cm_id at this point. */ |
| 3091 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event); |
| 3092 | cm_free_msg(msg); |
| 3093 | if (ret) |
| 3094 | ib_destroy_cm_id(&cm_id_priv->id); |
| 3095 | return; |
| 3096 | discard: |
Sean Hefty | 24be6e8 | 2007-06-18 11:09:36 -0700 | [diff] [blame] | 3097 | spin_unlock_irq(&cm_id_priv->lock); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3098 | cm_free_msg(msg); |
| 3099 | } |
| 3100 | |
| 3101 | static void cm_send_handler(struct ib_mad_agent *mad_agent, |
| 3102 | struct ib_mad_send_wc *mad_send_wc) |
| 3103 | { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 3104 | struct ib_mad_send_buf *msg = mad_send_wc->send_buf; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3105 | struct cm_port *port; |
| 3106 | u16 attr_index; |
| 3107 | |
| 3108 | port = mad_agent->context; |
| 3109 | attr_index = be16_to_cpu(((struct ib_mad_hdr *) |
| 3110 | msg->mad)->attr_id) - CM_ATTR_ID_OFFSET; |
| 3111 | |
| 3112 | /* |
| 3113 | * If the send was in response to a received message (context[0] is not |
| 3114 | * set to a cm_id), and is not a REJ, then it is a send that was |
| 3115 | * manually retried. |
| 3116 | */ |
| 3117 | if (!msg->context[0] && (attr_index != CM_REJ_COUNTER)) |
| 3118 | msg->retries = 1; |
| 3119 | |
| 3120 | atomic_long_add(1 + msg->retries, |
| 3121 | &port->counter_group[CM_XMIT].counter[attr_index]); |
| 3122 | if (msg->retries) |
| 3123 | atomic_long_add(msg->retries, |
| 3124 | &port->counter_group[CM_XMIT_RETRIES]. |
| 3125 | counter[attr_index]); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3126 | |
| 3127 | switch (mad_send_wc->status) { |
| 3128 | case IB_WC_SUCCESS: |
| 3129 | case IB_WC_WR_FLUSH_ERR: |
| 3130 | cm_free_msg(msg); |
| 3131 | break; |
| 3132 | default: |
| 3133 | if (msg->context[0] && msg->context[1]) |
| 3134 | cm_process_send_error(msg, mad_send_wc->status); |
| 3135 | else |
| 3136 | cm_free_msg(msg); |
| 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3141 | static void cm_work_handler(struct work_struct *_work) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3142 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3143 | struct cm_work *work = container_of(_work, struct cm_work, work.work); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3144 | int ret; |
| 3145 | |
| 3146 | switch (work->cm_event.event) { |
| 3147 | case IB_CM_REQ_RECEIVED: |
| 3148 | ret = cm_req_handler(work); |
| 3149 | break; |
| 3150 | case IB_CM_MRA_RECEIVED: |
| 3151 | ret = cm_mra_handler(work); |
| 3152 | break; |
| 3153 | case IB_CM_REJ_RECEIVED: |
| 3154 | ret = cm_rej_handler(work); |
| 3155 | break; |
| 3156 | case IB_CM_REP_RECEIVED: |
| 3157 | ret = cm_rep_handler(work); |
| 3158 | break; |
| 3159 | case IB_CM_RTU_RECEIVED: |
| 3160 | ret = cm_rtu_handler(work); |
| 3161 | break; |
| 3162 | case IB_CM_USER_ESTABLISHED: |
| 3163 | ret = cm_establish_handler(work); |
| 3164 | break; |
| 3165 | case IB_CM_DREQ_RECEIVED: |
| 3166 | ret = cm_dreq_handler(work); |
| 3167 | break; |
| 3168 | case IB_CM_DREP_RECEIVED: |
| 3169 | ret = cm_drep_handler(work); |
| 3170 | break; |
| 3171 | case IB_CM_SIDR_REQ_RECEIVED: |
| 3172 | ret = cm_sidr_req_handler(work); |
| 3173 | break; |
| 3174 | case IB_CM_SIDR_REP_RECEIVED: |
| 3175 | ret = cm_sidr_rep_handler(work); |
| 3176 | break; |
| 3177 | case IB_CM_LAP_RECEIVED: |
| 3178 | ret = cm_lap_handler(work); |
| 3179 | break; |
| 3180 | case IB_CM_APR_RECEIVED: |
| 3181 | ret = cm_apr_handler(work); |
| 3182 | break; |
| 3183 | case IB_CM_TIMEWAIT_EXIT: |
| 3184 | ret = cm_timewait_handler(work); |
| 3185 | break; |
| 3186 | default: |
| 3187 | ret = -EINVAL; |
| 3188 | break; |
| 3189 | } |
| 3190 | if (ret) |
| 3191 | cm_free_work(work); |
| 3192 | } |
| 3193 | |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3194 | static int cm_establish(struct ib_cm_id *cm_id) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3195 | { |
| 3196 | struct cm_id_private *cm_id_priv; |
| 3197 | struct cm_work *work; |
| 3198 | unsigned long flags; |
| 3199 | int ret = 0; |
| 3200 | |
| 3201 | work = kmalloc(sizeof *work, GFP_ATOMIC); |
| 3202 | if (!work) |
| 3203 | return -ENOMEM; |
| 3204 | |
| 3205 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 3206 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 3207 | switch (cm_id->state) |
| 3208 | { |
| 3209 | case IB_CM_REP_SENT: |
| 3210 | case IB_CM_MRA_REP_RCVD: |
| 3211 | cm_id->state = IB_CM_ESTABLISHED; |
| 3212 | break; |
| 3213 | case IB_CM_ESTABLISHED: |
| 3214 | ret = -EISCONN; |
| 3215 | break; |
| 3216 | default: |
| 3217 | ret = -EINVAL; |
| 3218 | break; |
| 3219 | } |
| 3220 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3221 | |
| 3222 | if (ret) { |
| 3223 | kfree(work); |
| 3224 | goto out; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * The CM worker thread may try to destroy the cm_id before it |
| 3229 | * can execute this work item. To prevent potential deadlock, |
| 3230 | * we need to find the cm_id once we're in the context of the |
| 3231 | * worker thread, rather than holding a reference on it. |
| 3232 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3233 | INIT_DELAYED_WORK(&work->work, cm_work_handler); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3234 | work->local_id = cm_id->local_id; |
| 3235 | work->remote_id = cm_id->remote_id; |
| 3236 | work->mad_recv_wc = NULL; |
| 3237 | work->cm_event.event = IB_CM_USER_ESTABLISHED; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3238 | queue_delayed_work(cm.wq, &work->work, 0); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3239 | out: |
| 3240 | return ret; |
| 3241 | } |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3242 | |
| 3243 | static int cm_migrate(struct ib_cm_id *cm_id) |
| 3244 | { |
| 3245 | struct cm_id_private *cm_id_priv; |
| 3246 | unsigned long flags; |
| 3247 | int ret = 0; |
| 3248 | |
| 3249 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 3250 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 3251 | if (cm_id->state == IB_CM_ESTABLISHED && |
| 3252 | (cm_id->lap_state == IB_CM_LAP_UNINIT || |
| 3253 | cm_id->lap_state == IB_CM_LAP_IDLE)) { |
| 3254 | cm_id->lap_state = IB_CM_LAP_IDLE; |
| 3255 | cm_id_priv->av = cm_id_priv->alt_av; |
| 3256 | } else |
| 3257 | ret = -EINVAL; |
| 3258 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3259 | |
| 3260 | return ret; |
| 3261 | } |
| 3262 | |
| 3263 | int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event) |
| 3264 | { |
| 3265 | int ret; |
| 3266 | |
| 3267 | switch (event) { |
| 3268 | case IB_EVENT_COMM_EST: |
| 3269 | ret = cm_establish(cm_id); |
| 3270 | break; |
| 3271 | case IB_EVENT_PATH_MIG: |
| 3272 | ret = cm_migrate(cm_id); |
| 3273 | break; |
| 3274 | default: |
| 3275 | ret = -EINVAL; |
| 3276 | } |
| 3277 | return ret; |
| 3278 | } |
| 3279 | EXPORT_SYMBOL(ib_cm_notify); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3280 | |
| 3281 | static void cm_recv_handler(struct ib_mad_agent *mad_agent, |
| 3282 | struct ib_mad_recv_wc *mad_recv_wc) |
| 3283 | { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3284 | struct cm_port *port = mad_agent->context; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3285 | struct cm_work *work; |
| 3286 | enum ib_cm_event_type event; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3287 | u16 attr_id; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3288 | int paths = 0; |
| 3289 | |
| 3290 | switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) { |
| 3291 | case CM_REQ_ATTR_ID: |
| 3292 | paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)-> |
| 3293 | alt_local_lid != 0); |
| 3294 | event = IB_CM_REQ_RECEIVED; |
| 3295 | break; |
| 3296 | case CM_MRA_ATTR_ID: |
| 3297 | event = IB_CM_MRA_RECEIVED; |
| 3298 | break; |
| 3299 | case CM_REJ_ATTR_ID: |
| 3300 | event = IB_CM_REJ_RECEIVED; |
| 3301 | break; |
| 3302 | case CM_REP_ATTR_ID: |
| 3303 | event = IB_CM_REP_RECEIVED; |
| 3304 | break; |
| 3305 | case CM_RTU_ATTR_ID: |
| 3306 | event = IB_CM_RTU_RECEIVED; |
| 3307 | break; |
| 3308 | case CM_DREQ_ATTR_ID: |
| 3309 | event = IB_CM_DREQ_RECEIVED; |
| 3310 | break; |
| 3311 | case CM_DREP_ATTR_ID: |
| 3312 | event = IB_CM_DREP_RECEIVED; |
| 3313 | break; |
| 3314 | case CM_SIDR_REQ_ATTR_ID: |
| 3315 | event = IB_CM_SIDR_REQ_RECEIVED; |
| 3316 | break; |
| 3317 | case CM_SIDR_REP_ATTR_ID: |
| 3318 | event = IB_CM_SIDR_REP_RECEIVED; |
| 3319 | break; |
| 3320 | case CM_LAP_ATTR_ID: |
| 3321 | paths = 1; |
| 3322 | event = IB_CM_LAP_RECEIVED; |
| 3323 | break; |
| 3324 | case CM_APR_ATTR_ID: |
| 3325 | event = IB_CM_APR_RECEIVED; |
| 3326 | break; |
| 3327 | default: |
| 3328 | ib_free_recv_mad(mad_recv_wc); |
| 3329 | return; |
| 3330 | } |
| 3331 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3332 | attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id); |
| 3333 | atomic_long_inc(&port->counter_group[CM_RECV]. |
| 3334 | counter[attr_id - CM_ATTR_ID_OFFSET]); |
| 3335 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3336 | work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths, |
| 3337 | GFP_KERNEL); |
| 3338 | if (!work) { |
| 3339 | ib_free_recv_mad(mad_recv_wc); |
| 3340 | return; |
| 3341 | } |
| 3342 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3343 | INIT_DELAYED_WORK(&work->work, cm_work_handler); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3344 | work->cm_event.event = event; |
| 3345 | work->mad_recv_wc = mad_recv_wc; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3346 | work->port = port; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3347 | queue_delayed_work(cm.wq, &work->work, 0); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv, |
| 3351 | struct ib_qp_attr *qp_attr, |
| 3352 | int *qp_attr_mask) |
| 3353 | { |
| 3354 | unsigned long flags; |
| 3355 | int ret; |
| 3356 | |
| 3357 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 3358 | switch (cm_id_priv->id.state) { |
| 3359 | case IB_CM_REQ_SENT: |
| 3360 | case IB_CM_MRA_REQ_RCVD: |
| 3361 | case IB_CM_REQ_RCVD: |
| 3362 | case IB_CM_MRA_REQ_SENT: |
| 3363 | case IB_CM_REP_RCVD: |
| 3364 | case IB_CM_MRA_REP_SENT: |
| 3365 | case IB_CM_REP_SENT: |
| 3366 | case IB_CM_MRA_REP_RCVD: |
| 3367 | case IB_CM_ESTABLISHED: |
| 3368 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | |
| 3369 | IB_QP_PKEY_INDEX | IB_QP_PORT; |
Dotan Barak | e31353e | 2006-10-24 13:35:27 -0700 | [diff] [blame] | 3370 | qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3371 | if (cm_id_priv->responder_resources) |
Sean Hefty | c1f250c | 2006-08-28 11:55:52 -0700 | [diff] [blame] | 3372 | qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ | |
| 3373 | IB_ACCESS_REMOTE_ATOMIC; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3374 | qp_attr->pkey_index = cm_id_priv->av.pkey_index; |
| 3375 | qp_attr->port_num = cm_id_priv->av.port->port_num; |
| 3376 | ret = 0; |
| 3377 | break; |
| 3378 | default: |
| 3379 | ret = -EINVAL; |
| 3380 | break; |
| 3381 | } |
| 3382 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3383 | return ret; |
| 3384 | } |
| 3385 | |
| 3386 | static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv, |
| 3387 | struct ib_qp_attr *qp_attr, |
| 3388 | int *qp_attr_mask) |
| 3389 | { |
| 3390 | unsigned long flags; |
| 3391 | int ret; |
| 3392 | |
| 3393 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 3394 | switch (cm_id_priv->id.state) { |
| 3395 | case IB_CM_REQ_RCVD: |
| 3396 | case IB_CM_MRA_REQ_SENT: |
| 3397 | case IB_CM_REP_RCVD: |
| 3398 | case IB_CM_MRA_REP_SENT: |
| 3399 | case IB_CM_REP_SENT: |
| 3400 | case IB_CM_MRA_REP_RCVD: |
| 3401 | case IB_CM_ESTABLISHED: |
| 3402 | *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | |
Sean Hefty | ae7971a | 2005-10-24 12:33:56 -0700 | [diff] [blame] | 3403 | IB_QP_DEST_QPN | IB_QP_RQ_PSN; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3404 | qp_attr->ah_attr = cm_id_priv->av.ah_attr; |
| 3405 | qp_attr->path_mtu = cm_id_priv->path_mtu; |
| 3406 | qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn); |
| 3407 | qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn); |
Sean Hefty | ae7971a | 2005-10-24 12:33:56 -0700 | [diff] [blame] | 3408 | if (cm_id_priv->qp_type == IB_QPT_RC) { |
| 3409 | *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC | |
| 3410 | IB_QP_MIN_RNR_TIMER; |
| 3411 | qp_attr->max_dest_rd_atomic = |
| 3412 | cm_id_priv->responder_resources; |
| 3413 | qp_attr->min_rnr_timer = 0; |
| 3414 | } |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3415 | if (cm_id_priv->alt_av.ah_attr.dlid) { |
| 3416 | *qp_attr_mask |= IB_QP_ALT_PATH; |
Sean Hefty | 0d8fdfd | 2006-06-30 14:10:14 -0700 | [diff] [blame] | 3417 | qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3418 | qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 3419 | qp_attr->alt_timeout = cm_id_priv->alt_av.timeout; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3420 | qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; |
| 3421 | } |
| 3422 | ret = 0; |
| 3423 | break; |
| 3424 | default: |
| 3425 | ret = -EINVAL; |
| 3426 | break; |
| 3427 | } |
| 3428 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3429 | return ret; |
| 3430 | } |
| 3431 | |
| 3432 | static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv, |
| 3433 | struct ib_qp_attr *qp_attr, |
| 3434 | int *qp_attr_mask) |
| 3435 | { |
| 3436 | unsigned long flags; |
| 3437 | int ret; |
| 3438 | |
| 3439 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
| 3440 | switch (cm_id_priv->id.state) { |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 3441 | /* Allow transition to RTS before sending REP */ |
| 3442 | case IB_CM_REQ_RCVD: |
| 3443 | case IB_CM_MRA_REQ_SENT: |
| 3444 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3445 | case IB_CM_REP_RCVD: |
| 3446 | case IB_CM_MRA_REP_SENT: |
| 3447 | case IB_CM_REP_SENT: |
| 3448 | case IB_CM_MRA_REP_RCVD: |
| 3449 | case IB_CM_ESTABLISHED: |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3450 | if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) { |
| 3451 | *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN; |
| 3452 | qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn); |
| 3453 | if (cm_id_priv->qp_type == IB_QPT_RC) { |
| 3454 | *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | |
| 3455 | IB_QP_RNR_RETRY | |
| 3456 | IB_QP_MAX_QP_RD_ATOMIC; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 3457 | qp_attr->timeout = cm_id_priv->av.timeout; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3458 | qp_attr->retry_cnt = cm_id_priv->retry_count; |
| 3459 | qp_attr->rnr_retry = cm_id_priv->rnr_retry_count; |
| 3460 | qp_attr->max_rd_atomic = |
| 3461 | cm_id_priv->initiator_depth; |
| 3462 | } |
| 3463 | if (cm_id_priv->alt_av.ah_attr.dlid) { |
| 3464 | *qp_attr_mask |= IB_QP_PATH_MIG_STATE; |
| 3465 | qp_attr->path_mig_state = IB_MIG_REARM; |
| 3466 | } |
| 3467 | } else { |
| 3468 | *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE; |
| 3469 | qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; |
| 3470 | qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 3471 | qp_attr->alt_timeout = cm_id_priv->alt_av.timeout; |
Sean Hefty | e1444b5 | 2006-11-28 14:57:13 -0800 | [diff] [blame] | 3472 | qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3473 | qp_attr->path_mig_state = IB_MIG_REARM; |
| 3474 | } |
| 3475 | ret = 0; |
| 3476 | break; |
| 3477 | default: |
| 3478 | ret = -EINVAL; |
| 3479 | break; |
| 3480 | } |
| 3481 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
| 3482 | return ret; |
| 3483 | } |
| 3484 | |
| 3485 | int ib_cm_init_qp_attr(struct ib_cm_id *cm_id, |
| 3486 | struct ib_qp_attr *qp_attr, |
| 3487 | int *qp_attr_mask) |
| 3488 | { |
| 3489 | struct cm_id_private *cm_id_priv; |
| 3490 | int ret; |
| 3491 | |
| 3492 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); |
| 3493 | switch (qp_attr->qp_state) { |
| 3494 | case IB_QPS_INIT: |
| 3495 | ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask); |
| 3496 | break; |
| 3497 | case IB_QPS_RTR: |
| 3498 | ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask); |
| 3499 | break; |
| 3500 | case IB_QPS_RTS: |
| 3501 | ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask); |
| 3502 | break; |
| 3503 | default: |
| 3504 | ret = -EINVAL; |
| 3505 | break; |
| 3506 | } |
| 3507 | return ret; |
| 3508 | } |
| 3509 | EXPORT_SYMBOL(ib_cm_init_qp_attr); |
| 3510 | |
Roland Dreier | 454a01e | 2007-07-17 18:37:43 -0700 | [diff] [blame] | 3511 | static void cm_get_ack_delay(struct cm_device *cm_dev) |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 3512 | { |
| 3513 | struct ib_device_attr attr; |
| 3514 | |
| 3515 | if (ib_query_device(cm_dev->device, &attr)) |
| 3516 | cm_dev->ack_delay = 0; /* acks will rely on packet life time */ |
| 3517 | else |
| 3518 | cm_dev->ack_delay = attr.local_ca_ack_delay; |
| 3519 | } |
| 3520 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3521 | static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr, |
| 3522 | char *buf) |
| 3523 | { |
| 3524 | struct cm_counter_group *group; |
| 3525 | struct cm_counter_attribute *cm_attr; |
| 3526 | |
| 3527 | group = container_of(obj, struct cm_counter_group, obj); |
| 3528 | cm_attr = container_of(attr, struct cm_counter_attribute, attr); |
| 3529 | |
| 3530 | return sprintf(buf, "%ld\n", |
| 3531 | atomic_long_read(&group->counter[cm_attr->index])); |
| 3532 | } |
| 3533 | |
| 3534 | static struct sysfs_ops cm_counter_ops = { |
| 3535 | .show = cm_show_counter |
| 3536 | }; |
| 3537 | |
| 3538 | static struct kobj_type cm_counter_obj_type = { |
| 3539 | .sysfs_ops = &cm_counter_ops, |
| 3540 | .default_attrs = cm_counter_default_attrs |
| 3541 | }; |
| 3542 | |
| 3543 | static void cm_release_port_obj(struct kobject *obj) |
| 3544 | { |
| 3545 | struct cm_port *cm_port; |
| 3546 | |
| 3547 | printk(KERN_ERR "free cm port\n"); |
| 3548 | |
| 3549 | cm_port = container_of(obj, struct cm_port, port_obj); |
| 3550 | kfree(cm_port); |
| 3551 | } |
| 3552 | |
| 3553 | static struct kobj_type cm_port_obj_type = { |
| 3554 | .release = cm_release_port_obj |
| 3555 | }; |
| 3556 | |
| 3557 | static void cm_release_dev_obj(struct kobject *obj) |
| 3558 | { |
| 3559 | struct cm_device *cm_dev; |
| 3560 | |
| 3561 | printk(KERN_ERR "free cm dev\n"); |
| 3562 | |
| 3563 | cm_dev = container_of(obj, struct cm_device, dev_obj); |
| 3564 | kfree(cm_dev); |
| 3565 | } |
| 3566 | |
| 3567 | static struct kobj_type cm_dev_obj_type = { |
| 3568 | .release = cm_release_dev_obj |
| 3569 | }; |
| 3570 | |
| 3571 | struct class cm_class = { |
| 3572 | .name = "infiniband_cm", |
| 3573 | }; |
| 3574 | EXPORT_SYMBOL(cm_class); |
| 3575 | |
| 3576 | static void cm_remove_fs_obj(struct kobject *obj) |
| 3577 | { |
| 3578 | kobject_put(obj->parent); |
| 3579 | kobject_put(obj); |
| 3580 | } |
| 3581 | |
| 3582 | static int cm_create_port_fs(struct cm_port *port) |
| 3583 | { |
| 3584 | int i, ret; |
| 3585 | |
| 3586 | ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type, |
| 3587 | kobject_get(&port->cm_dev->dev_obj), |
| 3588 | "%d", port->port_num); |
| 3589 | if (ret) { |
| 3590 | kfree(port); |
| 3591 | return ret; |
| 3592 | } |
| 3593 | |
| 3594 | for (i = 0; i < CM_COUNTER_GROUPS; i++) { |
| 3595 | ret = kobject_init_and_add(&port->counter_group[i].obj, |
| 3596 | &cm_counter_obj_type, |
| 3597 | kobject_get(&port->port_obj), |
| 3598 | "%s", counter_group_names[i]); |
| 3599 | if (ret) |
| 3600 | goto error; |
| 3601 | } |
| 3602 | |
| 3603 | return 0; |
| 3604 | |
| 3605 | error: |
| 3606 | while (i--) |
| 3607 | cm_remove_fs_obj(&port->counter_group[i].obj); |
| 3608 | cm_remove_fs_obj(&port->port_obj); |
| 3609 | return ret; |
| 3610 | |
| 3611 | } |
| 3612 | |
| 3613 | static void cm_remove_port_fs(struct cm_port *port) |
| 3614 | { |
| 3615 | int i; |
| 3616 | |
| 3617 | for (i = 0; i < CM_COUNTER_GROUPS; i++) |
| 3618 | cm_remove_fs_obj(&port->counter_group[i].obj); |
| 3619 | |
| 3620 | cm_remove_fs_obj(&port->port_obj); |
| 3621 | } |
| 3622 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3623 | static void cm_add_one(struct ib_device *device) |
| 3624 | { |
| 3625 | struct cm_device *cm_dev; |
| 3626 | struct cm_port *port; |
| 3627 | struct ib_mad_reg_req reg_req = { |
| 3628 | .mgmt_class = IB_MGMT_CLASS_CM, |
| 3629 | .mgmt_class_version = IB_CM_CLASS_VERSION |
| 3630 | }; |
| 3631 | struct ib_port_modify port_modify = { |
| 3632 | .set_port_cap_mask = IB_PORT_CM_SUP |
| 3633 | }; |
| 3634 | unsigned long flags; |
| 3635 | int ret; |
| 3636 | u8 i; |
| 3637 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 3638 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
| 3639 | return; |
| 3640 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3641 | cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) * |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3642 | device->phys_port_cnt, GFP_KERNEL); |
| 3643 | if (!cm_dev) |
| 3644 | return; |
| 3645 | |
| 3646 | cm_dev->device = device; |
Sean Hefty | 1d84612 | 2007-06-18 11:09:37 -0700 | [diff] [blame] | 3647 | cm_get_ack_delay(cm_dev); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3648 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3649 | ret = kobject_init_and_add(&cm_dev->dev_obj, &cm_dev_obj_type, |
| 3650 | &cm_class.subsys.kobj, "%s", device->name); |
| 3651 | if (ret) { |
| 3652 | kfree(cm_dev); |
| 3653 | return; |
| 3654 | } |
| 3655 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3656 | set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask); |
| 3657 | for (i = 1; i <= device->phys_port_cnt; i++) { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3658 | port = kzalloc(sizeof *port, GFP_KERNEL); |
| 3659 | if (!port) |
| 3660 | goto error1; |
| 3661 | |
| 3662 | cm_dev->port[i-1] = port; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3663 | port->cm_dev = cm_dev; |
| 3664 | port->port_num = i; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3665 | |
| 3666 | ret = cm_create_port_fs(port); |
| 3667 | if (ret) |
| 3668 | goto error1; |
| 3669 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3670 | port->mad_agent = ib_register_mad_agent(device, i, |
| 3671 | IB_QPT_GSI, |
| 3672 | ®_req, |
| 3673 | 0, |
| 3674 | cm_send_handler, |
| 3675 | cm_recv_handler, |
| 3676 | port); |
| 3677 | if (IS_ERR(port->mad_agent)) |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3678 | goto error2; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3679 | |
| 3680 | ret = ib_modify_port(device, i, 0, &port_modify); |
| 3681 | if (ret) |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3682 | goto error3; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3683 | } |
| 3684 | ib_set_client_data(device, &cm_client, cm_dev); |
| 3685 | |
| 3686 | write_lock_irqsave(&cm.device_lock, flags); |
| 3687 | list_add_tail(&cm_dev->list, &cm.device_list); |
| 3688 | write_unlock_irqrestore(&cm.device_lock, flags); |
| 3689 | return; |
| 3690 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3691 | error3: |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 3692 | ib_unregister_mad_agent(port->mad_agent); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3693 | error2: |
| 3694 | cm_remove_port_fs(port); |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 3695 | error1: |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3696 | port_modify.set_port_cap_mask = 0; |
| 3697 | port_modify.clr_port_cap_mask = IB_PORT_CM_SUP; |
| 3698 | while (--i) { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3699 | port = cm_dev->port[i-1]; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3700 | ib_modify_port(device, port->port_num, 0, &port_modify); |
| 3701 | ib_unregister_mad_agent(port->mad_agent); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3702 | cm_remove_port_fs(port); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3703 | } |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3704 | cm_remove_fs_obj(&cm_dev->dev_obj); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | static void cm_remove_one(struct ib_device *device) |
| 3708 | { |
| 3709 | struct cm_device *cm_dev; |
| 3710 | struct cm_port *port; |
| 3711 | struct ib_port_modify port_modify = { |
| 3712 | .clr_port_cap_mask = IB_PORT_CM_SUP |
| 3713 | }; |
| 3714 | unsigned long flags; |
| 3715 | int i; |
| 3716 | |
| 3717 | cm_dev = ib_get_client_data(device, &cm_client); |
| 3718 | if (!cm_dev) |
| 3719 | return; |
| 3720 | |
| 3721 | write_lock_irqsave(&cm.device_lock, flags); |
| 3722 | list_del(&cm_dev->list); |
| 3723 | write_unlock_irqrestore(&cm.device_lock, flags); |
| 3724 | |
| 3725 | for (i = 1; i <= device->phys_port_cnt; i++) { |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3726 | port = cm_dev->port[i-1]; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3727 | ib_modify_port(device, port->port_num, 0, &port_modify); |
| 3728 | ib_unregister_mad_agent(port->mad_agent); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3729 | cm_remove_port_fs(port); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3730 | } |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3731 | cm_remove_fs_obj(&cm_dev->dev_obj); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | static int __init ib_cm_init(void) |
| 3735 | { |
| 3736 | int ret; |
| 3737 | |
| 3738 | memset(&cm, 0, sizeof cm); |
| 3739 | INIT_LIST_HEAD(&cm.device_list); |
| 3740 | rwlock_init(&cm.device_lock); |
| 3741 | spin_lock_init(&cm.lock); |
| 3742 | cm.listen_service_table = RB_ROOT; |
| 3743 | cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID); |
| 3744 | cm.remote_id_table = RB_ROOT; |
| 3745 | cm.remote_qp_table = RB_ROOT; |
| 3746 | cm.remote_sidr_table = RB_ROOT; |
| 3747 | idr_init(&cm.local_id_table); |
Sean Hefty | f06d265 | 2006-08-28 15:15:18 -0700 | [diff] [blame] | 3748 | get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3749 | idr_pre_get(&cm.local_id_table, GFP_KERNEL); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 3750 | INIT_LIST_HEAD(&cm.timewait_list); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3751 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3752 | ret = class_register(&cm_class); |
| 3753 | if (ret) |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3754 | return -ENOMEM; |
| 3755 | |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3756 | cm.wq = create_workqueue("ib_cm"); |
| 3757 | if (!cm.wq) { |
| 3758 | ret = -ENOMEM; |
| 3759 | goto error1; |
| 3760 | } |
| 3761 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3762 | ret = ib_register_client(&cm_client); |
| 3763 | if (ret) |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3764 | goto error2; |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3765 | |
| 3766 | return 0; |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3767 | error2: |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3768 | destroy_workqueue(cm.wq); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3769 | error1: |
| 3770 | class_unregister(&cm_class); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3771 | return ret; |
| 3772 | } |
| 3773 | |
| 3774 | static void __exit ib_cm_cleanup(void) |
| 3775 | { |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 3776 | struct cm_timewait_info *timewait_info, *tmp; |
| 3777 | |
| 3778 | spin_lock_irq(&cm.lock); |
| 3779 | list_for_each_entry(timewait_info, &cm.timewait_list, list) |
| 3780 | cancel_delayed_work(&timewait_info->work.work); |
| 3781 | spin_unlock_irq(&cm.lock); |
| 3782 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3783 | destroy_workqueue(cm.wq); |
Sean Hefty | 8575329 | 2006-10-04 11:29:59 -0700 | [diff] [blame] | 3784 | |
| 3785 | list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) { |
| 3786 | list_del(&timewait_info->list); |
| 3787 | kfree(timewait_info); |
| 3788 | } |
| 3789 | |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3790 | ib_unregister_client(&cm_client); |
Sean Hefty | 9af57b7 | 2007-07-16 21:49:35 -0700 | [diff] [blame] | 3791 | class_unregister(&cm_class); |
Roland Dreier | 5d7edb3 | 2005-10-24 10:53:25 -0700 | [diff] [blame] | 3792 | idr_destroy(&cm.local_id_table); |
Hal Rosenstock | a977049 | 2005-07-27 11:45:40 -0700 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | module_init(ib_cm_init); |
| 3796 | module_exit(ib_cm_cleanup); |
| 3797 | |