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