Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | * |
| 32 | */ |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/in.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 36 | #include <linux/vmalloc.h> |
Manuel Zerpies | cb0a605 | 2011-06-16 02:09:57 +0000 | [diff] [blame] | 37 | #include <linux/ratelimit.h> |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 38 | |
| 39 | #include "rds.h" |
| 40 | #include "iw.h" |
| 41 | |
| 42 | /* |
| 43 | * Set the selected protocol version |
| 44 | */ |
| 45 | static void rds_iw_set_protocol(struct rds_connection *conn, unsigned int version) |
| 46 | { |
| 47 | conn->c_version = version; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Set up flow control |
| 52 | */ |
| 53 | static void rds_iw_set_flow_control(struct rds_connection *conn, u32 credits) |
| 54 | { |
| 55 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 56 | |
| 57 | if (rds_iw_sysctl_flow_control && credits != 0) { |
| 58 | /* We're doing flow control */ |
| 59 | ic->i_flowctl = 1; |
| 60 | rds_iw_send_add_credits(conn, credits); |
| 61 | } else { |
| 62 | ic->i_flowctl = 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Connection established. |
| 68 | * We get here for both outgoing and incoming connection. |
| 69 | */ |
| 70 | void rds_iw_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event) |
| 71 | { |
| 72 | const struct rds_iw_connect_private *dp = NULL; |
| 73 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 74 | struct rds_iw_device *rds_iwdev; |
| 75 | int err; |
| 76 | |
| 77 | if (event->param.conn.private_data_len) { |
| 78 | dp = event->param.conn.private_data; |
| 79 | |
| 80 | rds_iw_set_protocol(conn, |
| 81 | RDS_PROTOCOL(dp->dp_protocol_major, |
| 82 | dp->dp_protocol_minor)); |
| 83 | rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit)); |
| 84 | } |
| 85 | |
| 86 | /* update ib_device with this local ipaddr & conn */ |
| 87 | rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client); |
| 88 | err = rds_iw_update_cm_id(rds_iwdev, ic->i_cm_id); |
| 89 | if (err) |
| 90 | printk(KERN_ERR "rds_iw_update_ipaddr failed (%d)\n", err); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 91 | rds_iw_add_conn(rds_iwdev, conn); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 92 | |
| 93 | /* If the peer gave us the last packet it saw, process this as if |
| 94 | * we had received a regular ACK. */ |
| 95 | if (dp && dp->dp_ack_seq) |
| 96 | rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL); |
| 97 | |
| 98 | printk(KERN_NOTICE "RDS/IW: connected to %pI4<->%pI4 version %u.%u%s\n", |
| 99 | &conn->c_laddr, &conn->c_faddr, |
| 100 | RDS_PROTOCOL_MAJOR(conn->c_version), |
| 101 | RDS_PROTOCOL_MINOR(conn->c_version), |
| 102 | ic->i_flowctl ? ", flow control" : ""); |
| 103 | |
| 104 | rds_connect_complete(conn); |
| 105 | } |
| 106 | |
| 107 | static void rds_iw_cm_fill_conn_param(struct rds_connection *conn, |
| 108 | struct rdma_conn_param *conn_param, |
| 109 | struct rds_iw_connect_private *dp, |
| 110 | u32 protocol_version) |
| 111 | { |
| 112 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 113 | |
| 114 | memset(conn_param, 0, sizeof(struct rdma_conn_param)); |
| 115 | /* XXX tune these? */ |
| 116 | conn_param->responder_resources = 1; |
| 117 | conn_param->initiator_depth = 1; |
| 118 | |
| 119 | if (dp) { |
| 120 | memset(dp, 0, sizeof(*dp)); |
| 121 | dp->dp_saddr = conn->c_laddr; |
| 122 | dp->dp_daddr = conn->c_faddr; |
| 123 | dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version); |
| 124 | dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version); |
| 125 | dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IW_SUPPORTED_PROTOCOLS); |
| 126 | dp->dp_ack_seq = rds_iw_piggyb_ack(ic); |
| 127 | |
| 128 | /* Advertise flow control */ |
| 129 | if (ic->i_flowctl) { |
| 130 | unsigned int credits; |
| 131 | |
| 132 | credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits)); |
| 133 | dp->dp_credit = cpu_to_be32(credits); |
| 134 | atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits); |
| 135 | } |
| 136 | |
| 137 | conn_param->private_data = dp; |
| 138 | conn_param->private_data_len = sizeof(*dp); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void rds_iw_cq_event_handler(struct ib_event *event, void *data) |
| 143 | { |
| 144 | rdsdebug("event %u data %p\n", event->event, data); |
| 145 | } |
| 146 | |
| 147 | static void rds_iw_qp_event_handler(struct ib_event *event, void *data) |
| 148 | { |
| 149 | struct rds_connection *conn = data; |
| 150 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 151 | |
| 152 | rdsdebug("conn %p ic %p event %u\n", conn, ic, event->event); |
| 153 | |
| 154 | switch (event->event) { |
| 155 | case IB_EVENT_COMM_EST: |
| 156 | rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST); |
| 157 | break; |
| 158 | case IB_EVENT_QP_REQ_ERR: |
| 159 | case IB_EVENT_QP_FATAL: |
| 160 | default: |
Andy Grover | 9706978 | 2010-03-11 13:50:02 +0000 | [diff] [blame] | 161 | rdsdebug("Fatal QP Event %u " |
| 162 | "- connection %pI4->%pI4, reconnecting\n", |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 163 | event->event, &conn->c_laddr, |
| 164 | &conn->c_faddr); |
Andy Grover | 9706978 | 2010-03-11 13:50:02 +0000 | [diff] [blame] | 165 | rds_conn_drop(conn); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Create a QP |
| 172 | */ |
| 173 | static int rds_iw_init_qp_attrs(struct ib_qp_init_attr *attr, |
| 174 | struct rds_iw_device *rds_iwdev, |
| 175 | struct rds_iw_work_ring *send_ring, |
| 176 | void (*send_cq_handler)(struct ib_cq *, void *), |
| 177 | struct rds_iw_work_ring *recv_ring, |
| 178 | void (*recv_cq_handler)(struct ib_cq *, void *), |
| 179 | void *context) |
| 180 | { |
| 181 | struct ib_device *dev = rds_iwdev->dev; |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 182 | struct ib_cq_init_attr cq_attr = {}; |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 183 | unsigned int send_size, recv_size; |
| 184 | int ret; |
| 185 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 186 | /* The offset of 1 is to accommodate the additional ACK WR. */ |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 187 | send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1); |
| 188 | recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1); |
| 189 | rds_iw_ring_resize(send_ring, send_size - 1); |
| 190 | rds_iw_ring_resize(recv_ring, recv_size - 1); |
| 191 | |
| 192 | memset(attr, 0, sizeof(*attr)); |
| 193 | attr->event_handler = rds_iw_qp_event_handler; |
| 194 | attr->qp_context = context; |
| 195 | attr->cap.max_send_wr = send_size; |
| 196 | attr->cap.max_recv_wr = recv_size; |
| 197 | attr->cap.max_send_sge = rds_iwdev->max_sge; |
| 198 | attr->cap.max_recv_sge = RDS_IW_RECV_SGE; |
| 199 | attr->sq_sig_type = IB_SIGNAL_REQ_WR; |
| 200 | attr->qp_type = IB_QPT_RC; |
| 201 | |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 202 | cq_attr.cqe = send_size; |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 203 | attr->send_cq = ib_create_cq(dev, send_cq_handler, |
| 204 | rds_iw_cq_event_handler, |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 205 | context, &cq_attr); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 206 | if (IS_ERR(attr->send_cq)) { |
| 207 | ret = PTR_ERR(attr->send_cq); |
| 208 | attr->send_cq = NULL; |
| 209 | rdsdebug("ib_create_cq send failed: %d\n", ret); |
| 210 | goto out; |
| 211 | } |
| 212 | |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 213 | cq_attr.cqe = recv_size; |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 214 | attr->recv_cq = ib_create_cq(dev, recv_cq_handler, |
| 215 | rds_iw_cq_event_handler, |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 216 | context, &cq_attr); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 217 | if (IS_ERR(attr->recv_cq)) { |
| 218 | ret = PTR_ERR(attr->recv_cq); |
| 219 | attr->recv_cq = NULL; |
| 220 | rdsdebug("ib_create_cq send failed: %d\n", ret); |
| 221 | goto out; |
| 222 | } |
| 223 | |
| 224 | ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP); |
| 225 | if (ret) { |
| 226 | rdsdebug("ib_req_notify_cq send failed: %d\n", ret); |
| 227 | goto out; |
| 228 | } |
| 229 | |
| 230 | ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED); |
| 231 | if (ret) { |
| 232 | rdsdebug("ib_req_notify_cq recv failed: %d\n", ret); |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | out: |
| 237 | if (ret) { |
| 238 | if (attr->send_cq) |
| 239 | ib_destroy_cq(attr->send_cq); |
| 240 | if (attr->recv_cq) |
| 241 | ib_destroy_cq(attr->recv_cq); |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * This needs to be very careful to not leave IS_ERR pointers around for |
| 248 | * cleanup to trip over. |
| 249 | */ |
| 250 | static int rds_iw_setup_qp(struct rds_connection *conn) |
| 251 | { |
| 252 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 253 | struct ib_device *dev = ic->i_cm_id->device; |
| 254 | struct ib_qp_init_attr attr; |
| 255 | struct rds_iw_device *rds_iwdev; |
| 256 | int ret; |
| 257 | |
| 258 | /* rds_iw_add_one creates a rds_iw_device object per IB device, |
| 259 | * and allocates a protection domain, memory range and MR pool |
| 260 | * for each. If that fails for any reason, it will not register |
| 261 | * the rds_iwdev at all. |
| 262 | */ |
| 263 | rds_iwdev = ib_get_client_data(dev, &rds_iw_client); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 264 | if (!rds_iwdev) { |
Manuel Zerpies | cb0a605 | 2011-06-16 02:09:57 +0000 | [diff] [blame] | 265 | printk_ratelimited(KERN_NOTICE "RDS/IW: No client_data for device %s\n", |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 266 | dev->name); |
| 267 | return -EOPNOTSUPP; |
| 268 | } |
| 269 | |
| 270 | /* Protection domain and memory range */ |
| 271 | ic->i_pd = rds_iwdev->pd; |
| 272 | ic->i_mr = rds_iwdev->mr; |
| 273 | |
| 274 | ret = rds_iw_init_qp_attrs(&attr, rds_iwdev, |
| 275 | &ic->i_send_ring, rds_iw_send_cq_comp_handler, |
| 276 | &ic->i_recv_ring, rds_iw_recv_cq_comp_handler, |
| 277 | conn); |
| 278 | if (ret < 0) |
| 279 | goto out; |
| 280 | |
| 281 | ic->i_send_cq = attr.send_cq; |
| 282 | ic->i_recv_cq = attr.recv_cq; |
| 283 | |
| 284 | /* |
| 285 | * XXX this can fail if max_*_wr is too large? Are we supposed |
| 286 | * to back off until we get a value that the hardware can support? |
| 287 | */ |
| 288 | ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr); |
| 289 | if (ret) { |
| 290 | rdsdebug("rdma_create_qp failed: %d\n", ret); |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | ic->i_send_hdrs = ib_dma_alloc_coherent(dev, |
| 295 | ic->i_send_ring.w_nr * |
| 296 | sizeof(struct rds_header), |
| 297 | &ic->i_send_hdrs_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 298 | if (!ic->i_send_hdrs) { |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 299 | ret = -ENOMEM; |
| 300 | rdsdebug("ib_dma_alloc_coherent send failed\n"); |
| 301 | goto out; |
| 302 | } |
| 303 | |
| 304 | ic->i_recv_hdrs = ib_dma_alloc_coherent(dev, |
| 305 | ic->i_recv_ring.w_nr * |
| 306 | sizeof(struct rds_header), |
| 307 | &ic->i_recv_hdrs_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 308 | if (!ic->i_recv_hdrs) { |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 309 | ret = -ENOMEM; |
| 310 | rdsdebug("ib_dma_alloc_coherent recv failed\n"); |
| 311 | goto out; |
| 312 | } |
| 313 | |
| 314 | ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header), |
| 315 | &ic->i_ack_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 316 | if (!ic->i_ack) { |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 317 | ret = -ENOMEM; |
| 318 | rdsdebug("ib_dma_alloc_coherent ack failed\n"); |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_iw_send_work)); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 323 | if (!ic->i_sends) { |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 324 | ret = -ENOMEM; |
| 325 | rdsdebug("send allocation failed\n"); |
| 326 | goto out; |
| 327 | } |
| 328 | rds_iw_send_init_ring(ic); |
| 329 | |
| 330 | ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_iw_recv_work)); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 331 | if (!ic->i_recvs) { |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 332 | ret = -ENOMEM; |
| 333 | rdsdebug("recv allocation failed\n"); |
| 334 | goto out; |
| 335 | } |
| 336 | |
| 337 | rds_iw_recv_init_ring(ic); |
| 338 | rds_iw_recv_init_ack(ic); |
| 339 | |
| 340 | /* Post receive buffers - as a side effect, this will update |
| 341 | * the posted credit count. */ |
| 342 | rds_iw_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1); |
| 343 | |
| 344 | rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr, |
| 345 | ic->i_send_cq, ic->i_recv_cq); |
| 346 | |
| 347 | out: |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | static u32 rds_iw_protocol_compatible(const struct rds_iw_connect_private *dp) |
| 352 | { |
| 353 | u16 common; |
| 354 | u32 version = 0; |
| 355 | |
| 356 | /* rdma_cm private data is odd - when there is any private data in the |
| 357 | * request, we will be given a pretty large buffer without telling us the |
| 358 | * original size. The only way to tell the difference is by looking at |
| 359 | * the contents, which are initialized to zero. |
| 360 | * If the protocol version fields aren't set, this is a connection attempt |
| 361 | * from an older version. This could could be 3.0 or 2.0 - we can't tell. |
| 362 | * We really should have changed this for OFED 1.3 :-( */ |
| 363 | if (dp->dp_protocol_major == 0) |
| 364 | return RDS_PROTOCOL_3_0; |
| 365 | |
| 366 | common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IW_SUPPORTED_PROTOCOLS; |
| 367 | if (dp->dp_protocol_major == 3 && common) { |
| 368 | version = RDS_PROTOCOL_3_0; |
| 369 | while ((common >>= 1) != 0) |
| 370 | version++; |
Manuel Zerpies | cb0a605 | 2011-06-16 02:09:57 +0000 | [diff] [blame] | 371 | } |
| 372 | printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using " |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 373 | "incompatible protocol version %u.%u\n", |
| 374 | &dp->dp_saddr, |
| 375 | dp->dp_protocol_major, |
| 376 | dp->dp_protocol_minor); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 377 | return version; |
| 378 | } |
| 379 | |
| 380 | int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id, |
| 381 | struct rdma_cm_event *event) |
| 382 | { |
| 383 | const struct rds_iw_connect_private *dp = event->param.conn.private_data; |
| 384 | struct rds_iw_connect_private dp_rep; |
| 385 | struct rds_connection *conn = NULL; |
| 386 | struct rds_iw_connection *ic = NULL; |
| 387 | struct rdma_conn_param conn_param; |
| 388 | struct rds_iw_device *rds_iwdev; |
| 389 | u32 version; |
| 390 | int err, destroy = 1; |
| 391 | |
| 392 | /* Check whether the remote protocol version matches ours. */ |
| 393 | version = rds_iw_protocol_compatible(dp); |
| 394 | if (!version) |
| 395 | goto out; |
| 396 | |
| 397 | rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u\n", |
| 398 | &dp->dp_saddr, &dp->dp_daddr, |
| 399 | RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version)); |
| 400 | |
| 401 | conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_iw_transport, |
| 402 | GFP_KERNEL); |
| 403 | if (IS_ERR(conn)) { |
| 404 | rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn)); |
| 405 | conn = NULL; |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * The connection request may occur while the |
| 411 | * previous connection exist, e.g. in case of failover. |
| 412 | * But as connections may be initiated simultaneously |
| 413 | * by both hosts, we have a random backoff mechanism - |
| 414 | * see the comment above rds_queue_reconnect() |
| 415 | */ |
| 416 | mutex_lock(&conn->c_cm_lock); |
| 417 | if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { |
| 418 | if (rds_conn_state(conn) == RDS_CONN_UP) { |
| 419 | rdsdebug("incoming connect while connecting\n"); |
| 420 | rds_conn_drop(conn); |
| 421 | rds_iw_stats_inc(s_iw_listen_closed_stale); |
| 422 | } else |
| 423 | if (rds_conn_state(conn) == RDS_CONN_CONNECTING) { |
| 424 | /* Wait and see - our connect may still be succeeding */ |
| 425 | rds_iw_stats_inc(s_iw_connect_raced); |
| 426 | } |
| 427 | mutex_unlock(&conn->c_cm_lock); |
| 428 | goto out; |
| 429 | } |
| 430 | |
| 431 | ic = conn->c_transport_data; |
| 432 | |
| 433 | rds_iw_set_protocol(conn, version); |
| 434 | rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit)); |
| 435 | |
| 436 | /* If the peer gave us the last packet it saw, process this as if |
| 437 | * we had received a regular ACK. */ |
| 438 | if (dp->dp_ack_seq) |
| 439 | rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL); |
| 440 | |
| 441 | BUG_ON(cm_id->context); |
| 442 | BUG_ON(ic->i_cm_id); |
| 443 | |
| 444 | ic->i_cm_id = cm_id; |
| 445 | cm_id->context = conn; |
| 446 | |
| 447 | rds_iwdev = ib_get_client_data(cm_id->device, &rds_iw_client); |
| 448 | ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey; |
| 449 | |
| 450 | /* We got halfway through setting up the ib_connection, if we |
| 451 | * fail now, we have to take the long route out of this mess. */ |
| 452 | destroy = 0; |
| 453 | |
| 454 | err = rds_iw_setup_qp(conn); |
| 455 | if (err) { |
| 456 | rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", err); |
Julia Lawall | 5daf47b | 2010-05-26 05:54:21 +0000 | [diff] [blame] | 457 | mutex_unlock(&conn->c_cm_lock); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version); |
| 462 | |
| 463 | /* rdma_accept() calls rdma_reject() internally if it fails */ |
| 464 | err = rdma_accept(cm_id, &conn_param); |
| 465 | mutex_unlock(&conn->c_cm_lock); |
| 466 | if (err) { |
| 467 | rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err); |
| 468 | goto out; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | |
| 473 | out: |
| 474 | rdma_reject(cm_id, NULL, 0); |
| 475 | return destroy; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id) |
| 480 | { |
| 481 | struct rds_connection *conn = cm_id->context; |
| 482 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 483 | struct rdma_conn_param conn_param; |
| 484 | struct rds_iw_connect_private dp; |
| 485 | int ret; |
| 486 | |
| 487 | /* If the peer doesn't do protocol negotiation, we must |
| 488 | * default to RDSv3.0 */ |
| 489 | rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0); |
| 490 | ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */ |
| 491 | |
| 492 | ret = rds_iw_setup_qp(conn); |
| 493 | if (ret) { |
| 494 | rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret); |
| 495 | goto out; |
| 496 | } |
| 497 | |
| 498 | rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION); |
| 499 | |
| 500 | ret = rdma_connect(cm_id, &conn_param); |
| 501 | if (ret) |
| 502 | rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret); |
| 503 | |
| 504 | out: |
| 505 | /* Beware - returning non-zero tells the rdma_cm to destroy |
| 506 | * the cm_id. We should certainly not do it as long as we still |
| 507 | * "own" the cm_id. */ |
| 508 | if (ret) { |
| 509 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 510 | |
| 511 | if (ic->i_cm_id == cm_id) |
| 512 | ret = 0; |
| 513 | } |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | int rds_iw_conn_connect(struct rds_connection *conn) |
| 518 | { |
| 519 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 520 | struct rds_iw_device *rds_iwdev; |
| 521 | struct sockaddr_in src, dest; |
| 522 | int ret; |
| 523 | |
| 524 | /* XXX I wonder what affect the port space has */ |
| 525 | /* delegate cm event handler to rdma_transport */ |
| 526 | ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 527 | RDMA_PS_TCP, IB_QPT_RC); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 528 | if (IS_ERR(ic->i_cm_id)) { |
| 529 | ret = PTR_ERR(ic->i_cm_id); |
| 530 | ic->i_cm_id = NULL; |
| 531 | rdsdebug("rdma_create_id() failed: %d\n", ret); |
| 532 | goto out; |
| 533 | } |
| 534 | |
| 535 | rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn); |
| 536 | |
| 537 | src.sin_family = AF_INET; |
| 538 | src.sin_addr.s_addr = (__force u32)conn->c_laddr; |
| 539 | src.sin_port = (__force u16)htons(0); |
| 540 | |
| 541 | /* First, bind to the local address and device. */ |
| 542 | ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src); |
| 543 | if (ret) { |
| 544 | rdsdebug("rdma_bind_addr(%pI4) failed: %d\n", |
| 545 | &conn->c_laddr, ret); |
| 546 | rdma_destroy_id(ic->i_cm_id); |
| 547 | ic->i_cm_id = NULL; |
| 548 | goto out; |
| 549 | } |
| 550 | |
| 551 | rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client); |
| 552 | ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey; |
| 553 | |
| 554 | dest.sin_family = AF_INET; |
| 555 | dest.sin_addr.s_addr = (__force u32)conn->c_faddr; |
| 556 | dest.sin_port = (__force u16)htons(RDS_PORT); |
| 557 | |
| 558 | ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src, |
| 559 | (struct sockaddr *)&dest, |
| 560 | RDS_RDMA_RESOLVE_TIMEOUT_MS); |
| 561 | if (ret) { |
| 562 | rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id, |
| 563 | ret); |
| 564 | rdma_destroy_id(ic->i_cm_id); |
| 565 | ic->i_cm_id = NULL; |
| 566 | } |
| 567 | |
| 568 | out: |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * This is so careful about only cleaning up resources that were built up |
| 574 | * so that it can be called at any point during startup. In fact it |
| 575 | * can be called multiple times for a given connection. |
| 576 | */ |
| 577 | void rds_iw_conn_shutdown(struct rds_connection *conn) |
| 578 | { |
| 579 | struct rds_iw_connection *ic = conn->c_transport_data; |
| 580 | int err = 0; |
| 581 | struct ib_qp_attr qp_attr; |
| 582 | |
| 583 | rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id, |
| 584 | ic->i_pd, ic->i_send_cq, ic->i_recv_cq, |
| 585 | ic->i_cm_id ? ic->i_cm_id->qp : NULL); |
| 586 | |
| 587 | if (ic->i_cm_id) { |
| 588 | struct ib_device *dev = ic->i_cm_id->device; |
| 589 | |
| 590 | rdsdebug("disconnecting cm %p\n", ic->i_cm_id); |
| 591 | err = rdma_disconnect(ic->i_cm_id); |
| 592 | if (err) { |
| 593 | /* Actually this may happen quite frequently, when |
| 594 | * an outgoing connect raced with an incoming connect. |
| 595 | */ |
Rasmus Villemoes | 11ac119 | 2015-02-05 23:17:20 +0100 | [diff] [blame] | 596 | rdsdebug("failed to disconnect, cm: %p err %d\n", |
| 597 | ic->i_cm_id, err); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (ic->i_cm_id->qp) { |
| 601 | qp_attr.qp_state = IB_QPS_ERR; |
| 602 | ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE); |
| 603 | } |
| 604 | |
| 605 | wait_event(rds_iw_ring_empty_wait, |
| 606 | rds_iw_ring_empty(&ic->i_send_ring) && |
| 607 | rds_iw_ring_empty(&ic->i_recv_ring)); |
| 608 | |
| 609 | if (ic->i_send_hdrs) |
| 610 | ib_dma_free_coherent(dev, |
| 611 | ic->i_send_ring.w_nr * |
| 612 | sizeof(struct rds_header), |
| 613 | ic->i_send_hdrs, |
| 614 | ic->i_send_hdrs_dma); |
| 615 | |
| 616 | if (ic->i_recv_hdrs) |
| 617 | ib_dma_free_coherent(dev, |
| 618 | ic->i_recv_ring.w_nr * |
| 619 | sizeof(struct rds_header), |
| 620 | ic->i_recv_hdrs, |
| 621 | ic->i_recv_hdrs_dma); |
| 622 | |
| 623 | if (ic->i_ack) |
| 624 | ib_dma_free_coherent(dev, sizeof(struct rds_header), |
| 625 | ic->i_ack, ic->i_ack_dma); |
| 626 | |
| 627 | if (ic->i_sends) |
| 628 | rds_iw_send_clear_ring(ic); |
| 629 | if (ic->i_recvs) |
| 630 | rds_iw_recv_clear_ring(ic); |
| 631 | |
| 632 | if (ic->i_cm_id->qp) |
| 633 | rdma_destroy_qp(ic->i_cm_id); |
| 634 | if (ic->i_send_cq) |
| 635 | ib_destroy_cq(ic->i_send_cq); |
| 636 | if (ic->i_recv_cq) |
| 637 | ib_destroy_cq(ic->i_recv_cq); |
| 638 | |
| 639 | /* |
| 640 | * If associated with an rds_iw_device: |
| 641 | * Move connection back to the nodev list. |
| 642 | * Remove cm_id from the device cm_id list. |
| 643 | */ |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 644 | if (ic->rds_iwdev) |
| 645 | rds_iw_remove_conn(ic->rds_iwdev, conn); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 646 | |
| 647 | rdma_destroy_id(ic->i_cm_id); |
| 648 | |
| 649 | ic->i_cm_id = NULL; |
| 650 | ic->i_pd = NULL; |
| 651 | ic->i_mr = NULL; |
| 652 | ic->i_send_cq = NULL; |
| 653 | ic->i_recv_cq = NULL; |
| 654 | ic->i_send_hdrs = NULL; |
| 655 | ic->i_recv_hdrs = NULL; |
| 656 | ic->i_ack = NULL; |
| 657 | } |
| 658 | BUG_ON(ic->rds_iwdev); |
| 659 | |
| 660 | /* Clear pending transmit */ |
| 661 | if (ic->i_rm) { |
| 662 | rds_message_put(ic->i_rm); |
| 663 | ic->i_rm = NULL; |
| 664 | } |
| 665 | |
| 666 | /* Clear the ACK state */ |
| 667 | clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags); |
Andy Grover | 8cbd960 | 2009-04-01 08:20:20 +0000 | [diff] [blame] | 668 | #ifdef KERNEL_HAS_ATOMIC64 |
| 669 | atomic64_set(&ic->i_ack_next, 0); |
| 670 | #else |
| 671 | ic->i_ack_next = 0; |
| 672 | #endif |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 673 | ic->i_ack_recv = 0; |
| 674 | |
| 675 | /* Clear flow control state */ |
| 676 | ic->i_flowctl = 0; |
| 677 | atomic_set(&ic->i_credits, 0); |
| 678 | |
| 679 | rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr); |
| 680 | rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr); |
| 681 | |
| 682 | if (ic->i_iwinc) { |
| 683 | rds_inc_put(&ic->i_iwinc->ii_inc); |
| 684 | ic->i_iwinc = NULL; |
| 685 | } |
| 686 | |
| 687 | vfree(ic->i_sends); |
| 688 | ic->i_sends = NULL; |
| 689 | vfree(ic->i_recvs); |
| 690 | ic->i_recvs = NULL; |
| 691 | rdsdebug("shutdown complete\n"); |
| 692 | } |
| 693 | |
| 694 | int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp) |
| 695 | { |
| 696 | struct rds_iw_connection *ic; |
| 697 | unsigned long flags; |
| 698 | |
| 699 | /* XXX too lazy? */ |
Dan Carpenter | f0229ea | 2012-03-21 20:44:09 +0000 | [diff] [blame] | 700 | ic = kzalloc(sizeof(struct rds_iw_connection), gfp); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 701 | if (!ic) |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 702 | return -ENOMEM; |
| 703 | |
| 704 | INIT_LIST_HEAD(&ic->iw_node); |
Andy Grover | d521b63 | 2009-10-30 08:51:57 +0000 | [diff] [blame] | 705 | tasklet_init(&ic->i_recv_tasklet, rds_iw_recv_tasklet_fn, |
| 706 | (unsigned long) ic); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 707 | mutex_init(&ic->i_recv_mutex); |
Andy Grover | 8cbd960 | 2009-04-01 08:20:20 +0000 | [diff] [blame] | 708 | #ifndef KERNEL_HAS_ATOMIC64 |
| 709 | spin_lock_init(&ic->i_ack_lock); |
| 710 | #endif |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | * rds_iw_conn_shutdown() waits for these to be emptied so they |
| 714 | * must be initialized before it can be called. |
| 715 | */ |
| 716 | rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr); |
| 717 | rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr); |
| 718 | |
| 719 | ic->conn = conn; |
| 720 | conn->c_transport_data = ic; |
| 721 | |
| 722 | spin_lock_irqsave(&iw_nodev_conns_lock, flags); |
| 723 | list_add_tail(&ic->iw_node, &iw_nodev_conns); |
| 724 | spin_unlock_irqrestore(&iw_nodev_conns_lock, flags); |
| 725 | |
| 726 | |
| 727 | rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data); |
| 728 | return 0; |
| 729 | } |
| 730 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 731 | /* |
| 732 | * Free a connection. Connection must be shut down and not set for reconnect. |
| 733 | */ |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 734 | void rds_iw_conn_free(void *arg) |
| 735 | { |
| 736 | struct rds_iw_connection *ic = arg; |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 737 | spinlock_t *lock_ptr; |
| 738 | |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 739 | rdsdebug("ic %p\n", ic); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * Conn is either on a dev's list or on the nodev list. |
| 743 | * A race with shutdown() or connect() would cause problems |
| 744 | * (since rds_iwdev would change) but that should never happen. |
| 745 | */ |
| 746 | lock_ptr = ic->rds_iwdev ? &ic->rds_iwdev->spinlock : &iw_nodev_conns_lock; |
| 747 | |
| 748 | spin_lock_irq(lock_ptr); |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 749 | list_del(&ic->iw_node); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 750 | spin_unlock_irq(lock_ptr); |
| 751 | |
Andy Grover | fcd8b7c | 2009-02-24 15:30:36 +0000 | [diff] [blame] | 752 | kfree(ic); |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * An error occurred on the connection |
| 757 | */ |
| 758 | void |
| 759 | __rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...) |
| 760 | { |
| 761 | va_list ap; |
| 762 | |
| 763 | rds_conn_drop(conn); |
| 764 | |
| 765 | va_start(ap, fmt); |
| 766 | vprintk(fmt, ap); |
| 767 | va_end(ap); |
| 768 | } |