Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1 | /* |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved. |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 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 | ec16227 | 2009-02-24 15:30:30 +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> |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 38 | #include <net/addrconf.h> |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 39 | |
Sowmini Varadhan | 0cb4396 | 2016-06-13 09:44:26 -0700 | [diff] [blame] | 40 | #include "rds_single_path.h" |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 41 | #include "rds.h" |
| 42 | #include "ib.h" |
| 43 | |
| 44 | /* |
| 45 | * Set the selected protocol version |
| 46 | */ |
| 47 | static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version) |
| 48 | { |
| 49 | conn->c_version = version; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Set up flow control |
| 54 | */ |
| 55 | static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits) |
| 56 | { |
| 57 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 58 | |
| 59 | if (rds_ib_sysctl_flow_control && credits != 0) { |
| 60 | /* We're doing flow control */ |
| 61 | ic->i_flowctl = 1; |
| 62 | rds_ib_send_add_credits(conn, credits); |
| 63 | } else { |
| 64 | ic->i_flowctl = 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Tune RNR behavior. Without flow control, we use a rather |
| 70 | * low timeout, but not the absolute minimum - this should |
| 71 | * be tunable. |
| 72 | * |
| 73 | * We already set the RNR retry count to 7 (which is the |
| 74 | * smallest infinite number :-) above. |
| 75 | * If flow control is off, we want to change this back to 0 |
| 76 | * so that we learn quickly when our credit accounting is |
| 77 | * buggy. |
| 78 | * |
| 79 | * Caller passes in a qp_attr pointer - don't waste stack spacv |
| 80 | * by allocation this twice. |
| 81 | */ |
| 82 | static void |
| 83 | rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr) |
| 84 | { |
| 85 | int ret; |
| 86 | |
| 87 | attr->min_rnr_timer = IB_RNR_TIMER_000_32; |
| 88 | ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER); |
| 89 | if (ret) |
| 90 | printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Connection established. |
| 95 | * We get here for both outgoing and incoming connection. |
| 96 | */ |
| 97 | void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event) |
| 98 | { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 99 | struct rds_ib_connection *ic = conn->c_transport_data; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 100 | const union rds_ib_conn_priv *dp = NULL; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 101 | struct ib_qp_attr qp_attr; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 102 | __be64 ack_seq = 0; |
| 103 | __be32 credit = 0; |
| 104 | u8 major = 0; |
| 105 | u8 minor = 0; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 106 | int err; |
| 107 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 108 | dp = event->param.conn.private_data; |
| 109 | if (conn->c_isv6) { |
| 110 | if (event->param.conn.private_data_len >= |
| 111 | sizeof(struct rds6_ib_connect_private)) { |
| 112 | major = dp->ricp_v6.dp_protocol_major; |
| 113 | minor = dp->ricp_v6.dp_protocol_minor; |
| 114 | credit = dp->ricp_v6.dp_credit; |
| 115 | /* dp structure start is not guaranteed to be 8 bytes |
| 116 | * aligned. Since dp_ack_seq is 64-bit extended load |
| 117 | * operations can be used so go through get_unaligned |
| 118 | * to avoid unaligned errors. |
| 119 | */ |
| 120 | ack_seq = get_unaligned(&dp->ricp_v6.dp_ack_seq); |
Andy Grover | 02a6a25 | 2009-07-17 13:13:24 +0000 | [diff] [blame] | 121 | } |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 122 | } else if (event->param.conn.private_data_len >= |
| 123 | sizeof(struct rds_ib_connect_private)) { |
| 124 | major = dp->ricp_v4.dp_protocol_major; |
| 125 | minor = dp->ricp_v4.dp_protocol_minor; |
| 126 | credit = dp->ricp_v4.dp_credit; |
| 127 | ack_seq = get_unaligned(&dp->ricp_v4.dp_ack_seq); |
| 128 | } |
| 129 | |
| 130 | /* make sure it isn't empty data */ |
| 131 | if (major) { |
| 132 | rds_ib_set_protocol(conn, RDS_PROTOCOL(major, minor)); |
| 133 | rds_ib_set_flow_control(conn, be32_to_cpu(credit)); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 136 | if (conn->c_version < RDS_PROTOCOL_VERSION) { |
| 137 | if (conn->c_version != RDS_PROTOCOL_COMPAT_VERSION) { |
| 138 | pr_notice("RDS/IB: Connection <%pI6c,%pI6c> version %u.%u no longer supported\n", |
| 139 | &conn->c_laddr, &conn->c_faddr, |
| 140 | RDS_PROTOCOL_MAJOR(conn->c_version), |
| 141 | RDS_PROTOCOL_MINOR(conn->c_version)); |
| 142 | rds_conn_destroy(conn); |
| 143 | return; |
| 144 | } |
Andy Grover | f147dd9 | 2010-01-13 15:50:09 -0800 | [diff] [blame] | 145 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 146 | |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 147 | pr_notice("RDS/IB: %s conn connected <%pI6c,%pI6c> version %u.%u%s\n", |
| 148 | ic->i_active_side ? "Active" : "Passive", |
| 149 | &conn->c_laddr, &conn->c_faddr, |
| 150 | RDS_PROTOCOL_MAJOR(conn->c_version), |
| 151 | RDS_PROTOCOL_MINOR(conn->c_version), |
| 152 | ic->i_flowctl ? ", flow control" : ""); |
| 153 | |
Santosh Shilimkar | cf65726 | 2016-09-29 11:07:11 -0700 | [diff] [blame] | 154 | atomic_set(&ic->i_cq_quiesce, 0); |
| 155 | |
Santosh Shilimkar | 581d53c | 2016-07-09 18:31:38 -0700 | [diff] [blame] | 156 | /* Init rings and fill recv. this needs to wait until protocol |
| 157 | * negotiation is complete, since ring layout is different |
| 158 | * from 3.1 to 4.1. |
Andy Grover | e11d912 | 2009-07-17 13:13:29 +0000 | [diff] [blame] | 159 | */ |
| 160 | rds_ib_send_init_ring(ic); |
| 161 | rds_ib_recv_init_ring(ic); |
| 162 | /* Post receive buffers - as a side effect, this will update |
| 163 | * the posted credit count. */ |
santosh.shilimkar@oracle.com | 73ce431 | 2015-08-22 15:45:26 -0700 | [diff] [blame] | 164 | rds_ib_recv_refill(conn, 1, GFP_KERNEL); |
Andy Grover | e11d912 | 2009-07-17 13:13:29 +0000 | [diff] [blame] | 165 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 166 | /* Tune RNR behavior */ |
| 167 | rds_ib_tune_rnr(ic, &qp_attr); |
| 168 | |
| 169 | qp_attr.qp_state = IB_QPS_RTS; |
| 170 | err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE); |
| 171 | if (err) |
| 172 | printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err); |
| 173 | |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 174 | /* update ib_device with this local ipaddr */ |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 175 | err = rds_ib_update_ipaddr(ic->rds_ibdev, &conn->c_laddr); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 176 | if (err) |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 177 | printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n", |
| 178 | err); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 179 | |
| 180 | /* If the peer gave us the last packet it saw, process this as if |
| 181 | * we had received a regular ACK. */ |
shamir rabinovitch | c0adf54 | 2015-04-30 20:58:07 -0400 | [diff] [blame] | 182 | if (dp) { |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 183 | if (ack_seq) |
| 184 | rds_send_drop_acked(conn, be64_to_cpu(ack_seq), |
shamir rabinovitch | c0adf54 | 2015-04-30 20:58:07 -0400 | [diff] [blame] | 185 | NULL); |
| 186 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 187 | |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 188 | conn->c_proposed_version = conn->c_version; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 189 | rds_connect_complete(conn); |
| 190 | } |
| 191 | |
| 192 | static void rds_ib_cm_fill_conn_param(struct rds_connection *conn, |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 193 | struct rdma_conn_param *conn_param, |
| 194 | union rds_ib_conn_priv *dp, |
| 195 | u32 protocol_version, |
| 196 | u32 max_responder_resources, |
| 197 | u32 max_initiator_depth, |
| 198 | bool isv6) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 199 | { |
Andy Grover | 40589e7 | 2010-01-12 10:50:48 -0800 | [diff] [blame] | 200 | struct rds_ib_connection *ic = conn->c_transport_data; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 201 | struct rds_ib_device *rds_ibdev = ic->rds_ibdev; |
Andy Grover | 40589e7 | 2010-01-12 10:50:48 -0800 | [diff] [blame] | 202 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 203 | memset(conn_param, 0, sizeof(struct rdma_conn_param)); |
Andy Grover | 40589e7 | 2010-01-12 10:50:48 -0800 | [diff] [blame] | 204 | |
Andy Grover | 40589e7 | 2010-01-12 10:50:48 -0800 | [diff] [blame] | 205 | conn_param->responder_resources = |
| 206 | min_t(u32, rds_ibdev->max_responder_resources, max_responder_resources); |
| 207 | conn_param->initiator_depth = |
| 208 | min_t(u32, rds_ibdev->max_initiator_depth, max_initiator_depth); |
Andy Grover | 3ba23ad | 2009-07-17 13:13:22 +0000 | [diff] [blame] | 209 | conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 210 | conn_param->rnr_retry_count = 7; |
| 211 | |
| 212 | if (dp) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 213 | memset(dp, 0, sizeof(*dp)); |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 214 | if (isv6) { |
| 215 | dp->ricp_v6.dp_saddr = conn->c_laddr; |
| 216 | dp->ricp_v6.dp_daddr = conn->c_faddr; |
| 217 | dp->ricp_v6.dp_protocol_major = |
| 218 | RDS_PROTOCOL_MAJOR(protocol_version); |
| 219 | dp->ricp_v6.dp_protocol_minor = |
| 220 | RDS_PROTOCOL_MINOR(protocol_version); |
| 221 | dp->ricp_v6.dp_protocol_minor_mask = |
| 222 | cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS); |
| 223 | dp->ricp_v6.dp_ack_seq = |
| 224 | cpu_to_be64(rds_ib_piggyb_ack(ic)); |
| 225 | |
| 226 | conn_param->private_data = &dp->ricp_v6; |
| 227 | conn_param->private_data_len = sizeof(dp->ricp_v6); |
| 228 | } else { |
| 229 | dp->ricp_v4.dp_saddr = conn->c_laddr.s6_addr32[3]; |
| 230 | dp->ricp_v4.dp_daddr = conn->c_faddr.s6_addr32[3]; |
| 231 | dp->ricp_v4.dp_protocol_major = |
| 232 | RDS_PROTOCOL_MAJOR(protocol_version); |
| 233 | dp->ricp_v4.dp_protocol_minor = |
| 234 | RDS_PROTOCOL_MINOR(protocol_version); |
| 235 | dp->ricp_v4.dp_protocol_minor_mask = |
| 236 | cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS); |
| 237 | dp->ricp_v4.dp_ack_seq = |
| 238 | cpu_to_be64(rds_ib_piggyb_ack(ic)); |
| 239 | |
| 240 | conn_param->private_data = &dp->ricp_v4; |
| 241 | conn_param->private_data_len = sizeof(dp->ricp_v4); |
| 242 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 243 | |
| 244 | /* Advertise flow control */ |
| 245 | if (ic->i_flowctl) { |
| 246 | unsigned int credits; |
| 247 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 248 | credits = IB_GET_POST_CREDITS |
| 249 | (atomic_read(&ic->i_credits)); |
| 250 | if (isv6) |
| 251 | dp->ricp_v6.dp_credit = cpu_to_be32(credits); |
| 252 | else |
| 253 | dp->ricp_v4.dp_credit = cpu_to_be32(credits); |
| 254 | atomic_sub(IB_SET_POST_CREDITS(credits), |
| 255 | &ic->i_credits); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 256 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | static void rds_ib_cq_event_handler(struct ib_event *event, void *data) |
| 261 | { |
Zach Brown | 1bde04a | 2010-07-14 14:01:21 -0700 | [diff] [blame] | 262 | rdsdebug("event %u (%s) data %p\n", |
Sagi Grimberg | 3c88f3d | 2015-05-18 13:40:33 +0300 | [diff] [blame] | 263 | event->event, ib_event_msg(event->event), data); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 266 | /* Plucking the oldest entry from the ring can be done concurrently with |
| 267 | * the thread refilling the ring. Each ring operation is protected by |
| 268 | * spinlocks and the transient state of refilling doesn't change the |
| 269 | * recording of which entry is oldest. |
| 270 | * |
| 271 | * This relies on IB only calling one cq comp_handler for each cq so that |
| 272 | * there will only be one caller of rds_recv_incoming() per RDS connection. |
| 273 | */ |
| 274 | static void rds_ib_cq_comp_handler_recv(struct ib_cq *cq, void *context) |
| 275 | { |
| 276 | struct rds_connection *conn = context; |
| 277 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 278 | |
| 279 | rdsdebug("conn %p cq %p\n", conn, cq); |
| 280 | |
| 281 | rds_ib_stats_inc(s_ib_evt_handler_call); |
| 282 | |
| 283 | tasklet_schedule(&ic->i_recv_tasklet); |
| 284 | } |
| 285 | |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 286 | static void poll_scq(struct rds_ib_connection *ic, struct ib_cq *cq, |
| 287 | struct ib_wc *wcs) |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 288 | { |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 289 | int nr, i; |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 290 | struct ib_wc *wc; |
| 291 | |
| 292 | while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) { |
| 293 | for (i = 0; i < nr; i++) { |
| 294 | wc = wcs + i; |
| 295 | rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n", |
| 296 | (unsigned long long)wc->wr_id, wc->status, |
| 297 | wc->byte_len, be32_to_cpu(wc->ex.imm_data)); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 298 | |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 299 | if (wc->wr_id <= ic->i_send_ring.w_nr || |
| 300 | wc->wr_id == RDS_IB_ACK_WR_ID) |
| 301 | rds_ib_send_cqe_handler(ic, wc); |
| 302 | else |
| 303 | rds_ib_mr_cqe_handler(ic, wc); |
| 304 | |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 309 | static void rds_ib_tasklet_fn_send(unsigned long data) |
| 310 | { |
| 311 | struct rds_ib_connection *ic = (struct rds_ib_connection *)data; |
| 312 | struct rds_connection *conn = ic->conn; |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 313 | |
| 314 | rds_ib_stats_inc(s_ib_tasklet_call); |
| 315 | |
Santosh Shilimkar | cf65726 | 2016-09-29 11:07:11 -0700 | [diff] [blame] | 316 | /* if cq has been already reaped, ignore incoming cq event */ |
| 317 | if (atomic_read(&ic->i_cq_quiesce)) |
| 318 | return; |
| 319 | |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 320 | poll_scq(ic, ic->i_send_cq, ic->i_send_wc); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 321 | ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP); |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 322 | poll_scq(ic, ic->i_send_cq, ic->i_send_wc); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 323 | |
| 324 | if (rds_conn_up(conn) && |
| 325 | (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags) || |
| 326 | test_bit(0, &conn->c_map_queued))) |
Sowmini Varadhan | 1f9ecd7 | 2016-06-13 09:44:34 -0700 | [diff] [blame] | 327 | rds_send_xmit(&ic->conn->c_path[0]); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 328 | } |
| 329 | |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 330 | static void poll_rcq(struct rds_ib_connection *ic, struct ib_cq *cq, |
| 331 | struct ib_wc *wcs, |
| 332 | struct rds_ib_ack_state *ack_state) |
| 333 | { |
| 334 | int nr, i; |
| 335 | struct ib_wc *wc; |
| 336 | |
| 337 | while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) { |
| 338 | for (i = 0; i < nr; i++) { |
| 339 | wc = wcs + i; |
| 340 | rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n", |
| 341 | (unsigned long long)wc->wr_id, wc->status, |
| 342 | wc->byte_len, be32_to_cpu(wc->ex.imm_data)); |
| 343 | |
| 344 | rds_ib_recv_cqe_handler(ic, wc, ack_state); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 349 | static void rds_ib_tasklet_fn_recv(unsigned long data) |
| 350 | { |
| 351 | struct rds_ib_connection *ic = (struct rds_ib_connection *)data; |
| 352 | struct rds_connection *conn = ic->conn; |
| 353 | struct rds_ib_device *rds_ibdev = ic->rds_ibdev; |
| 354 | struct rds_ib_ack_state state; |
| 355 | |
Santosh Shilimkar | 9441c97 | 2015-09-19 14:01:09 -0400 | [diff] [blame] | 356 | if (!rds_ibdev) |
| 357 | rds_conn_drop(conn); |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 358 | |
| 359 | rds_ib_stats_inc(s_ib_tasklet_call); |
| 360 | |
Santosh Shilimkar | cf65726 | 2016-09-29 11:07:11 -0700 | [diff] [blame] | 361 | /* if cq has been already reaped, ignore incoming cq event */ |
| 362 | if (atomic_read(&ic->i_cq_quiesce)) |
| 363 | return; |
| 364 | |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 365 | memset(&state, 0, sizeof(state)); |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 366 | poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state); |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 367 | ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED); |
santosh.shilimkar@oracle.com | dcfd041 | 2016-03-01 15:20:45 -0800 | [diff] [blame] | 368 | poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state); |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 369 | |
| 370 | if (state.ack_next_valid) |
| 371 | rds_ib_set_ack(ic, state.ack_next, state.ack_required); |
| 372 | if (state.ack_recv_valid && state.ack_recv > ic->i_ack_recv) { |
| 373 | rds_send_drop_acked(conn, state.ack_recv, NULL); |
| 374 | ic->i_ack_recv = state.ack_recv; |
| 375 | } |
| 376 | |
| 377 | if (rds_conn_up(conn)) |
| 378 | rds_ib_attempt_ack(ic); |
| 379 | } |
| 380 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 381 | static void rds_ib_qp_event_handler(struct ib_event *event, void *data) |
| 382 | { |
| 383 | struct rds_connection *conn = data; |
| 384 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 385 | |
Zach Brown | 1bde04a | 2010-07-14 14:01:21 -0700 | [diff] [blame] | 386 | rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event, |
Sagi Grimberg | 3c88f3d | 2015-05-18 13:40:33 +0300 | [diff] [blame] | 387 | ib_event_msg(event->event)); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 388 | |
| 389 | switch (event->event) { |
| 390 | case IB_EVENT_COMM_EST: |
| 391 | rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST); |
| 392 | break; |
| 393 | default: |
Zach Brown | 1bde04a | 2010-07-14 14:01:21 -0700 | [diff] [blame] | 394 | rdsdebug("Fatal QP Event %u (%s) " |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 395 | "- connection %pI6c->%pI6c, reconnecting\n", |
Sagi Grimberg | 3c88f3d | 2015-05-18 13:40:33 +0300 | [diff] [blame] | 396 | event->event, ib_event_msg(event->event), |
Zach Brown | 1bde04a | 2010-07-14 14:01:21 -0700 | [diff] [blame] | 397 | &conn->c_laddr, &conn->c_faddr); |
Andy Grover | 9706978 | 2010-03-11 13:50:02 +0000 | [diff] [blame] | 398 | rds_conn_drop(conn); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 403 | static void rds_ib_cq_comp_handler_send(struct ib_cq *cq, void *context) |
| 404 | { |
| 405 | struct rds_connection *conn = context; |
| 406 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 407 | |
| 408 | rdsdebug("conn %p cq %p\n", conn, cq); |
| 409 | |
| 410 | rds_ib_stats_inc(s_ib_evt_handler_call); |
| 411 | |
| 412 | tasklet_schedule(&ic->i_send_tasklet); |
| 413 | } |
| 414 | |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 415 | static inline int ibdev_get_unused_vector(struct rds_ib_device *rds_ibdev) |
| 416 | { |
| 417 | int min = rds_ibdev->vector_load[rds_ibdev->dev->num_comp_vectors - 1]; |
| 418 | int index = rds_ibdev->dev->num_comp_vectors - 1; |
| 419 | int i; |
| 420 | |
| 421 | for (i = rds_ibdev->dev->num_comp_vectors - 1; i >= 0; i--) { |
| 422 | if (rds_ibdev->vector_load[i] < min) { |
| 423 | index = i; |
| 424 | min = rds_ibdev->vector_load[i]; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | rds_ibdev->vector_load[index]++; |
| 429 | return index; |
| 430 | } |
| 431 | |
| 432 | static inline void ibdev_put_vector(struct rds_ib_device *rds_ibdev, int index) |
| 433 | { |
| 434 | rds_ibdev->vector_load[index]--; |
| 435 | } |
| 436 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 437 | /* |
| 438 | * This needs to be very careful to not leave IS_ERR pointers around for |
| 439 | * cleanup to trip over. |
| 440 | */ |
| 441 | static int rds_ib_setup_qp(struct rds_connection *conn) |
| 442 | { |
| 443 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 444 | struct ib_device *dev = ic->i_cm_id->device; |
| 445 | struct ib_qp_init_attr attr; |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 446 | struct ib_cq_init_attr cq_attr = {}; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 447 | struct rds_ib_device *rds_ibdev; |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 448 | int ret, fr_queue_space; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 449 | |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 450 | /* |
| 451 | * It's normal to see a null device if an incoming connection races |
| 452 | * with device removal, so we don't print a warning. |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 453 | */ |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 454 | rds_ibdev = rds_ib_get_client_data(dev); |
| 455 | if (!rds_ibdev) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 456 | return -EOPNOTSUPP; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 457 | |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 458 | /* The fr_queue_space is currently set to 512, to add extra space on |
| 459 | * completion queue and send queue. This extra space is used for FRMR |
| 460 | * registration and invalidation work requests |
| 461 | */ |
Santosh Shilimkar | 5601245 | 2016-03-08 09:19:01 -0800 | [diff] [blame] | 462 | fr_queue_space = rds_ibdev->use_fastreg ? |
| 463 | (RDS_IB_DEFAULT_FR_WR + 1) + |
| 464 | (RDS_IB_DEFAULT_FR_INV_WR + 1) |
| 465 | : 0; |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 466 | |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 467 | /* add the conn now so that connection establishment has the dev */ |
| 468 | rds_ib_add_conn(rds_ibdev, conn); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 469 | |
| 470 | if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1) |
| 471 | rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1); |
| 472 | if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1) |
| 473 | rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1); |
| 474 | |
| 475 | /* Protection domain and memory range */ |
| 476 | ic->i_pd = rds_ibdev->pd; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 477 | |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 478 | ic->i_scq_vector = ibdev_get_unused_vector(rds_ibdev); |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 479 | cq_attr.cqe = ic->i_send_ring.w_nr + fr_queue_space + 1; |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 480 | cq_attr.comp_vector = ic->i_scq_vector; |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 481 | ic->i_send_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_send, |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 482 | rds_ib_cq_event_handler, conn, |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 483 | &cq_attr); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 484 | if (IS_ERR(ic->i_send_cq)) { |
| 485 | ret = PTR_ERR(ic->i_send_cq); |
| 486 | ic->i_send_cq = NULL; |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 487 | ibdev_put_vector(rds_ibdev, ic->i_scq_vector); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 488 | rdsdebug("ib_create_cq send failed: %d\n", ret); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 489 | goto rds_ibdev_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 492 | ic->i_rcq_vector = ibdev_get_unused_vector(rds_ibdev); |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 493 | cq_attr.cqe = ic->i_recv_ring.w_nr; |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 494 | cq_attr.comp_vector = ic->i_rcq_vector; |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 495 | ic->i_recv_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_recv, |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 496 | rds_ib_cq_event_handler, conn, |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 497 | &cq_attr); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 498 | if (IS_ERR(ic->i_recv_cq)) { |
| 499 | ret = PTR_ERR(ic->i_recv_cq); |
| 500 | ic->i_recv_cq = NULL; |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 501 | ibdev_put_vector(rds_ibdev, ic->i_rcq_vector); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 502 | rdsdebug("ib_create_cq recv failed: %d\n", ret); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 503 | goto send_cq_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP); |
| 507 | if (ret) { |
| 508 | rdsdebug("ib_req_notify_cq send failed: %d\n", ret); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 509 | goto recv_cq_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED); |
| 513 | if (ret) { |
| 514 | rdsdebug("ib_req_notify_cq recv failed: %d\n", ret); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 515 | goto recv_cq_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* XXX negotiate max send/recv with remote? */ |
| 519 | memset(&attr, 0, sizeof(attr)); |
| 520 | attr.event_handler = rds_ib_qp_event_handler; |
| 521 | attr.qp_context = conn; |
| 522 | /* + 1 to allow for the single ack message */ |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 523 | attr.cap.max_send_wr = ic->i_send_ring.w_nr + fr_queue_space + 1; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 524 | attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1; |
| 525 | attr.cap.max_send_sge = rds_ibdev->max_sge; |
| 526 | attr.cap.max_recv_sge = RDS_IB_RECV_SGE; |
| 527 | attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 528 | attr.qp_type = IB_QPT_RC; |
| 529 | attr.send_cq = ic->i_send_cq; |
| 530 | attr.recv_cq = ic->i_recv_cq; |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 531 | atomic_set(&ic->i_fastreg_wrs, RDS_IB_DEFAULT_FR_WR); |
Santosh Shilimkar | 5601245 | 2016-03-08 09:19:01 -0800 | [diff] [blame] | 532 | atomic_set(&ic->i_fastunreg_wrs, RDS_IB_DEFAULT_FR_INV_WR); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * XXX this can fail if max_*_wr is too large? Are we supposed |
| 536 | * to back off until we get a value that the hardware can support? |
| 537 | */ |
| 538 | ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr); |
| 539 | if (ret) { |
| 540 | rdsdebug("rdma_create_qp failed: %d\n", ret); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 541 | goto recv_cq_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | ic->i_send_hdrs = ib_dma_alloc_coherent(dev, |
| 545 | ic->i_send_ring.w_nr * |
| 546 | sizeof(struct rds_header), |
| 547 | &ic->i_send_hdrs_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 548 | if (!ic->i_send_hdrs) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 549 | ret = -ENOMEM; |
| 550 | rdsdebug("ib_dma_alloc_coherent send failed\n"); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 551 | goto qp_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | ic->i_recv_hdrs = ib_dma_alloc_coherent(dev, |
| 555 | ic->i_recv_ring.w_nr * |
| 556 | sizeof(struct rds_header), |
| 557 | &ic->i_recv_hdrs_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 558 | if (!ic->i_recv_hdrs) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 559 | ret = -ENOMEM; |
| 560 | rdsdebug("ib_dma_alloc_coherent recv failed\n"); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 561 | goto send_hdrs_dma_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header), |
| 565 | &ic->i_ack_dma, GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 566 | if (!ic->i_ack) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 567 | ret = -ENOMEM; |
| 568 | rdsdebug("ib_dma_alloc_coherent ack failed\n"); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 569 | goto recv_hdrs_dma_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Kees Cook | fd7bece | 2018-06-12 14:27:52 -0700 | [diff] [blame] | 572 | ic->i_sends = vzalloc_node(array_size(sizeof(struct rds_ib_send_work), |
| 573 | ic->i_send_ring.w_nr), |
Andy Grover | e4c52c9 | 2010-04-23 10:49:53 -0700 | [diff] [blame] | 574 | ibdev_to_node(dev)); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 575 | if (!ic->i_sends) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 576 | ret = -ENOMEM; |
| 577 | rdsdebug("send allocation failed\n"); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 578 | goto ack_dma_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 579 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 580 | |
Kees Cook | fd7bece | 2018-06-12 14:27:52 -0700 | [diff] [blame] | 581 | ic->i_recvs = vzalloc_node(array_size(sizeof(struct rds_ib_recv_work), |
| 582 | ic->i_recv_ring.w_nr), |
Andy Grover | e4c52c9 | 2010-04-23 10:49:53 -0700 | [diff] [blame] | 583 | ibdev_to_node(dev)); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 584 | if (!ic->i_recvs) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 585 | ret = -ENOMEM; |
| 586 | rdsdebug("recv allocation failed\n"); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 587 | goto sends_out; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 590 | rds_ib_recv_init_ack(ic); |
| 591 | |
Jason Gunthorpe | e558024 | 2015-07-30 17:22:26 -0600 | [diff] [blame] | 592 | rdsdebug("conn %p pd %p cq %p %p\n", conn, ic->i_pd, |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 593 | ic->i_send_cq, ic->i_recv_cq); |
| 594 | |
Dag Moxnes | 91a8252 | 2018-04-25 13:22:01 +0200 | [diff] [blame] | 595 | goto out; |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 596 | |
| 597 | sends_out: |
| 598 | vfree(ic->i_sends); |
| 599 | ack_dma_out: |
| 600 | ib_dma_free_coherent(dev, sizeof(struct rds_header), |
| 601 | ic->i_ack, ic->i_ack_dma); |
| 602 | recv_hdrs_dma_out: |
| 603 | ib_dma_free_coherent(dev, ic->i_recv_ring.w_nr * |
| 604 | sizeof(struct rds_header), |
| 605 | ic->i_recv_hdrs, ic->i_recv_hdrs_dma); |
| 606 | send_hdrs_dma_out: |
| 607 | ib_dma_free_coherent(dev, ic->i_send_ring.w_nr * |
| 608 | sizeof(struct rds_header), |
| 609 | ic->i_send_hdrs, ic->i_send_hdrs_dma); |
| 610 | qp_out: |
| 611 | rdma_destroy_qp(ic->i_cm_id); |
| 612 | recv_cq_out: |
| 613 | if (!ib_destroy_cq(ic->i_recv_cq)) |
| 614 | ic->i_recv_cq = NULL; |
| 615 | send_cq_out: |
| 616 | if (!ib_destroy_cq(ic->i_send_cq)) |
| 617 | ic->i_send_cq = NULL; |
| 618 | rds_ibdev_out: |
| 619 | rds_ib_remove_conn(rds_ibdev, conn); |
Dag Moxnes | 91a8252 | 2018-04-25 13:22:01 +0200 | [diff] [blame] | 620 | out: |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 621 | rds_ib_dev_put(rds_ibdev); |
Zhu Yanjun | 3b12f73 | 2017-03-07 02:48:36 -0500 | [diff] [blame] | 622 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 623 | return ret; |
| 624 | } |
| 625 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 626 | static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event, bool isv6) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 627 | { |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 628 | const union rds_ib_conn_priv *dp = event->param.conn.private_data; |
| 629 | u8 data_len, major, minor; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 630 | u32 version = 0; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 631 | __be16 mask; |
| 632 | u16 common; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 633 | |
Andy Grover | 9ddbcfa | 2009-07-17 13:13:23 +0000 | [diff] [blame] | 634 | /* |
| 635 | * rdma_cm private data is odd - when there is any private data in the |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 636 | * request, we will be given a pretty large buffer without telling us the |
| 637 | * original size. The only way to tell the difference is by looking at |
| 638 | * the contents, which are initialized to zero. |
| 639 | * If the protocol version fields aren't set, this is a connection attempt |
| 640 | * from an older version. This could could be 3.0 or 2.0 - we can't tell. |
Andy Grover | 9ddbcfa | 2009-07-17 13:13:23 +0000 | [diff] [blame] | 641 | * We really should have changed this for OFED 1.3 :-( |
| 642 | */ |
| 643 | |
| 644 | /* Be paranoid. RDS always has privdata */ |
| 645 | if (!event->param.conn.private_data_len) { |
| 646 | printk(KERN_NOTICE "RDS incoming connection has no private data, " |
| 647 | "rejecting\n"); |
| 648 | return 0; |
| 649 | } |
| 650 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 651 | if (isv6) { |
| 652 | data_len = sizeof(struct rds6_ib_connect_private); |
| 653 | major = dp->ricp_v6.dp_protocol_major; |
| 654 | minor = dp->ricp_v6.dp_protocol_minor; |
| 655 | mask = dp->ricp_v6.dp_protocol_minor_mask; |
| 656 | } else { |
| 657 | data_len = sizeof(struct rds_ib_connect_private); |
| 658 | major = dp->ricp_v4.dp_protocol_major; |
| 659 | minor = dp->ricp_v4.dp_protocol_minor; |
| 660 | mask = dp->ricp_v4.dp_protocol_minor_mask; |
| 661 | } |
| 662 | |
Andy Grover | 9ddbcfa | 2009-07-17 13:13:23 +0000 | [diff] [blame] | 663 | /* Even if len is crap *now* I still want to check it. -ASG */ |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 664 | if (event->param.conn.private_data_len < data_len || major == 0) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 665 | return RDS_PROTOCOL_3_0; |
| 666 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 667 | common = be16_to_cpu(mask) & RDS_IB_SUPPORTED_PROTOCOLS; |
| 668 | if (major == 3 && common) { |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 669 | version = RDS_PROTOCOL_3_0; |
| 670 | while ((common >>= 1) != 0) |
| 671 | version++; |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 672 | } else if (RDS_PROTOCOL_COMPAT_VERSION == |
| 673 | RDS_PROTOCOL(major, minor)) { |
| 674 | version = RDS_PROTOCOL_COMPAT_VERSION; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 675 | } else { |
| 676 | if (isv6) |
| 677 | printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI6c using incompatible protocol version %u.%u\n", |
| 678 | &dp->ricp_v6.dp_saddr, major, minor); |
| 679 | else |
| 680 | printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using incompatible protocol version %u.%u\n", |
| 681 | &dp->ricp_v4.dp_saddr, major, minor); |
| 682 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 683 | return version; |
| 684 | } |
| 685 | |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 686 | #if IS_ENABLED(CONFIG_IPV6) |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 687 | /* Given an IPv6 address, find the net_device which hosts that address and |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 688 | * return its index. This is used by the rds_ib_cm_handle_connect() code to |
| 689 | * find the interface index of where an incoming request comes from when |
| 690 | * the request is using a link local address. |
| 691 | * |
| 692 | * Note one problem in this search. It is possible that two interfaces have |
| 693 | * the same link local address. Unfortunately, this cannot be solved unless |
| 694 | * the underlying layer gives us the interface which an incoming RDMA connect |
| 695 | * request comes from. |
| 696 | */ |
| 697 | static u32 __rds_find_ifindex(struct net *net, const struct in6_addr *addr) |
| 698 | { |
| 699 | struct net_device *dev; |
| 700 | int idx = 0; |
| 701 | |
| 702 | rcu_read_lock(); |
| 703 | for_each_netdev_rcu(net, dev) { |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 704 | if (ipv6_chk_addr(net, addr, dev, 1)) { |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 705 | idx = dev->ifindex; |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | rcu_read_unlock(); |
| 710 | |
| 711 | return idx; |
| 712 | } |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 713 | #endif |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 714 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 715 | int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id, |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 716 | struct rdma_cm_event *event, bool isv6) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 717 | { |
| 718 | __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id; |
| 719 | __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 720 | const struct rds_ib_conn_priv_cmn *dp_cmn; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 721 | struct rds_connection *conn = NULL; |
| 722 | struct rds_ib_connection *ic = NULL; |
| 723 | struct rdma_conn_param conn_param; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 724 | const union rds_ib_conn_priv *dp; |
| 725 | union rds_ib_conn_priv dp_rep; |
| 726 | struct in6_addr s_mapped_addr; |
| 727 | struct in6_addr d_mapped_addr; |
| 728 | const struct in6_addr *saddr6; |
| 729 | const struct in6_addr *daddr6; |
| 730 | int destroy = 1; |
| 731 | u32 ifindex = 0; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 732 | u32 version; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 733 | int err = 1; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 734 | |
| 735 | /* Check whether the remote protocol version matches ours. */ |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 736 | version = rds_ib_protocol_compatible(event, isv6); |
Santosh Shilimkar | d021fab | 2018-10-23 23:09:00 -0400 | [diff] [blame^] | 737 | if (!version) { |
| 738 | err = RDS_RDMA_REJ_INCOMPAT; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 739 | goto out; |
Santosh Shilimkar | d021fab | 2018-10-23 23:09:00 -0400 | [diff] [blame^] | 740 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 741 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 742 | dp = event->param.conn.private_data; |
| 743 | if (isv6) { |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 744 | #if IS_ENABLED(CONFIG_IPV6) |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 745 | dp_cmn = &dp->ricp_v6.dp_cmn; |
| 746 | saddr6 = &dp->ricp_v6.dp_saddr; |
| 747 | daddr6 = &dp->ricp_v6.dp_daddr; |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 748 | /* If either address is link local, need to find the |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 749 | * interface index in order to create a proper RDS |
| 750 | * connection. |
| 751 | */ |
| 752 | if (ipv6_addr_type(daddr6) & IPV6_ADDR_LINKLOCAL) { |
| 753 | /* Using init_net for now .. */ |
| 754 | ifindex = __rds_find_ifindex(&init_net, daddr6); |
| 755 | /* No index found... Need to bail out. */ |
| 756 | if (ifindex == 0) { |
| 757 | err = -EOPNOTSUPP; |
| 758 | goto out; |
| 759 | } |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 760 | } else if (ipv6_addr_type(saddr6) & IPV6_ADDR_LINKLOCAL) { |
| 761 | /* Use our address to find the correct index. */ |
| 762 | ifindex = __rds_find_ifindex(&init_net, daddr6); |
| 763 | /* No index found... Need to bail out. */ |
| 764 | if (ifindex == 0) { |
| 765 | err = -EOPNOTSUPP; |
| 766 | goto out; |
| 767 | } |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 768 | } |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 769 | #else |
| 770 | err = -EOPNOTSUPP; |
| 771 | goto out; |
| 772 | #endif |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 773 | } else { |
| 774 | dp_cmn = &dp->ricp_v4.dp_cmn; |
| 775 | ipv6_addr_set_v4mapped(dp->ricp_v4.dp_saddr, &s_mapped_addr); |
| 776 | ipv6_addr_set_v4mapped(dp->ricp_v4.dp_daddr, &d_mapped_addr); |
| 777 | saddr6 = &s_mapped_addr; |
| 778 | daddr6 = &d_mapped_addr; |
| 779 | } |
| 780 | |
| 781 | rdsdebug("saddr %pI6c daddr %pI6c RDSv%u.%u lguid 0x%llx fguid " |
| 782 | "0x%llx\n", saddr6, daddr6, |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 783 | RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version), |
| 784 | (unsigned long long)be64_to_cpu(lguid), |
| 785 | (unsigned long long)be64_to_cpu(fguid)); |
| 786 | |
Sowmini Varadhan | d5a8ac2 | 2015-08-05 01:43:25 -0400 | [diff] [blame] | 787 | /* RDS/IB is not currently netns aware, thus init_net */ |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 788 | conn = rds_conn_create(&init_net, daddr6, saddr6, |
| 789 | &rds_ib_transport, GFP_KERNEL, ifindex); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 790 | if (IS_ERR(conn)) { |
| 791 | rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn)); |
| 792 | conn = NULL; |
| 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * The connection request may occur while the |
| 798 | * previous connection exist, e.g. in case of failover. |
| 799 | * But as connections may be initiated simultaneously |
| 800 | * by both hosts, we have a random backoff mechanism - |
| 801 | * see the comment above rds_queue_reconnect() |
| 802 | */ |
| 803 | mutex_lock(&conn->c_cm_lock); |
| 804 | if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { |
| 805 | if (rds_conn_state(conn) == RDS_CONN_UP) { |
| 806 | rdsdebug("incoming connect while connecting\n"); |
| 807 | rds_conn_drop(conn); |
| 808 | rds_ib_stats_inc(s_ib_listen_closed_stale); |
| 809 | } else |
| 810 | if (rds_conn_state(conn) == RDS_CONN_CONNECTING) { |
| 811 | /* Wait and see - our connect may still be succeeding */ |
| 812 | rds_ib_stats_inc(s_ib_connect_raced); |
| 813 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 814 | goto out; |
| 815 | } |
| 816 | |
| 817 | ic = conn->c_transport_data; |
| 818 | |
| 819 | rds_ib_set_protocol(conn, version); |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 820 | rds_ib_set_flow_control(conn, be32_to_cpu(dp_cmn->ricpc_credit)); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 821 | |
| 822 | /* If the peer gave us the last packet it saw, process this as if |
| 823 | * we had received a regular ACK. */ |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 824 | if (dp_cmn->ricpc_ack_seq) |
| 825 | rds_send_drop_acked(conn, be64_to_cpu(dp_cmn->ricpc_ack_seq), |
| 826 | NULL); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 827 | |
| 828 | BUG_ON(cm_id->context); |
| 829 | BUG_ON(ic->i_cm_id); |
| 830 | |
| 831 | ic->i_cm_id = cm_id; |
| 832 | cm_id->context = conn; |
| 833 | |
| 834 | /* We got halfway through setting up the ib_connection, if we |
| 835 | * fail now, we have to take the long route out of this mess. */ |
| 836 | destroy = 0; |
| 837 | |
| 838 | err = rds_ib_setup_qp(conn); |
| 839 | if (err) { |
| 840 | rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err); |
| 841 | goto out; |
| 842 | } |
| 843 | |
Andy Grover | 40589e7 | 2010-01-12 10:50:48 -0800 | [diff] [blame] | 844 | rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version, |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 845 | event->param.conn.responder_resources, |
| 846 | event->param.conn.initiator_depth, isv6); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 847 | |
| 848 | /* rdma_accept() calls rdma_reject() internally if it fails */ |
Zhu Yanjun | b418c52 | 2017-03-13 01:43:45 -0400 | [diff] [blame] | 849 | if (rdma_accept(cm_id, &conn_param)) |
| 850 | rds_ib_conn_error(conn, "rdma_accept failed\n"); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 851 | |
| 852 | out: |
Zach Brown | a46ca94 | 2010-05-24 13:14:59 -0700 | [diff] [blame] | 853 | if (conn) |
| 854 | mutex_unlock(&conn->c_cm_lock); |
| 855 | if (err) |
Santosh Shilimkar | d021fab | 2018-10-23 23:09:00 -0400 | [diff] [blame^] | 856 | rdma_reject(cm_id, &err, sizeof(int)); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 857 | return destroy; |
| 858 | } |
| 859 | |
| 860 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 861 | int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 862 | { |
| 863 | struct rds_connection *conn = cm_id->context; |
| 864 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 865 | struct rdma_conn_param conn_param; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 866 | union rds_ib_conn_priv dp; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 867 | int ret; |
| 868 | |
| 869 | /* If the peer doesn't do protocol negotiation, we must |
| 870 | * default to RDSv3.0 */ |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 871 | rds_ib_set_protocol(conn, RDS_PROTOCOL_VERSION); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 872 | ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */ |
| 873 | |
| 874 | ret = rds_ib_setup_qp(conn); |
| 875 | if (ret) { |
| 876 | rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret); |
| 877 | goto out; |
| 878 | } |
| 879 | |
Santosh Shilimkar | cdc306a | 2018-10-13 20:34:42 +0800 | [diff] [blame] | 880 | rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, |
| 881 | conn->c_proposed_version, |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 882 | UINT_MAX, UINT_MAX, isv6); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 883 | ret = rdma_connect(cm_id, &conn_param); |
| 884 | if (ret) |
| 885 | rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret); |
| 886 | |
| 887 | out: |
| 888 | /* Beware - returning non-zero tells the rdma_cm to destroy |
| 889 | * the cm_id. We should certainly not do it as long as we still |
| 890 | * "own" the cm_id. */ |
| 891 | if (ret) { |
| 892 | if (ic->i_cm_id == cm_id) |
| 893 | ret = 0; |
| 894 | } |
Santosh Shilimkar | 581d53c | 2016-07-09 18:31:38 -0700 | [diff] [blame] | 895 | ic->i_active_side = true; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 896 | return ret; |
| 897 | } |
| 898 | |
Sowmini Varadhan | b04e855 | 2016-06-30 16:11:16 -0700 | [diff] [blame] | 899 | int rds_ib_conn_path_connect(struct rds_conn_path *cp) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 900 | { |
Sowmini Varadhan | b04e855 | 2016-06-30 16:11:16 -0700 | [diff] [blame] | 901 | struct rds_connection *conn = cp->cp_conn; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 902 | struct sockaddr_storage src, dest; |
| 903 | rdma_cm_event_handler handler; |
| 904 | struct rds_ib_connection *ic; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 905 | int ret; |
| 906 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 907 | ic = conn->c_transport_data; |
| 908 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 909 | /* XXX I wonder what affect the port space has */ |
| 910 | /* delegate cm event handler to rdma_transport */ |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 911 | #if IS_ENABLED(CONFIG_IPV6) |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 912 | if (conn->c_isv6) |
| 913 | handler = rds6_rdma_cm_event_handler; |
| 914 | else |
Ka-Cheong Poon | e65d4d9 | 2018-07-30 22:48:42 -0700 | [diff] [blame] | 915 | #endif |
Ka-Cheong Poon | 1e2b44e | 2018-07-23 20:51:22 -0700 | [diff] [blame] | 916 | handler = rds_rdma_cm_event_handler; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 917 | ic->i_cm_id = rdma_create_id(&init_net, handler, conn, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 918 | RDMA_PS_TCP, IB_QPT_RC); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 919 | if (IS_ERR(ic->i_cm_id)) { |
| 920 | ret = PTR_ERR(ic->i_cm_id); |
| 921 | ic->i_cm_id = NULL; |
| 922 | rdsdebug("rdma_create_id() failed: %d\n", ret); |
| 923 | goto out; |
| 924 | } |
| 925 | |
| 926 | rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn); |
| 927 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 928 | if (ipv6_addr_v4mapped(&conn->c_faddr)) { |
| 929 | struct sockaddr_in *sin; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 930 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 931 | sin = (struct sockaddr_in *)&src; |
| 932 | sin->sin_family = AF_INET; |
| 933 | sin->sin_addr.s_addr = conn->c_laddr.s6_addr32[3]; |
| 934 | sin->sin_port = 0; |
| 935 | |
| 936 | sin = (struct sockaddr_in *)&dest; |
| 937 | sin->sin_family = AF_INET; |
| 938 | sin->sin_addr.s_addr = conn->c_faddr.s6_addr32[3]; |
| 939 | sin->sin_port = htons(RDS_PORT); |
| 940 | } else { |
| 941 | struct sockaddr_in6 *sin6; |
| 942 | |
| 943 | sin6 = (struct sockaddr_in6 *)&src; |
| 944 | sin6->sin6_family = AF_INET6; |
| 945 | sin6->sin6_addr = conn->c_laddr; |
| 946 | sin6->sin6_port = 0; |
| 947 | sin6->sin6_scope_id = conn->c_dev_if; |
| 948 | |
| 949 | sin6 = (struct sockaddr_in6 *)&dest; |
| 950 | sin6->sin6_family = AF_INET6; |
| 951 | sin6->sin6_addr = conn->c_faddr; |
| 952 | sin6->sin6_port = htons(RDS_CM_PORT); |
| 953 | sin6->sin6_scope_id = conn->c_dev_if; |
| 954 | } |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 955 | |
| 956 | ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src, |
| 957 | (struct sockaddr *)&dest, |
| 958 | RDS_RDMA_RESOLVE_TIMEOUT_MS); |
| 959 | if (ret) { |
| 960 | rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id, |
| 961 | ret); |
| 962 | rdma_destroy_id(ic->i_cm_id); |
| 963 | ic->i_cm_id = NULL; |
| 964 | } |
| 965 | |
| 966 | out: |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * This is so careful about only cleaning up resources that were built up |
| 972 | * so that it can be called at any point during startup. In fact it |
| 973 | * can be called multiple times for a given connection. |
| 974 | */ |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 975 | void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 976 | { |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 977 | struct rds_connection *conn = cp->cp_conn; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 978 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 979 | int err = 0; |
| 980 | |
| 981 | rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id, |
| 982 | ic->i_pd, ic->i_send_cq, ic->i_recv_cq, |
| 983 | ic->i_cm_id ? ic->i_cm_id->qp : NULL); |
| 984 | |
| 985 | if (ic->i_cm_id) { |
| 986 | struct ib_device *dev = ic->i_cm_id->device; |
| 987 | |
| 988 | rdsdebug("disconnecting cm %p\n", ic->i_cm_id); |
| 989 | err = rdma_disconnect(ic->i_cm_id); |
| 990 | if (err) { |
| 991 | /* Actually this may happen quite frequently, when |
| 992 | * an outgoing connect raced with an incoming connect. |
| 993 | */ |
| 994 | rdsdebug("failed to disconnect, cm: %p err %d\n", |
| 995 | ic->i_cm_id, err); |
| 996 | } |
| 997 | |
Andy Grover | e32b4a7 | 2010-03-03 19:25:21 -0800 | [diff] [blame] | 998 | /* |
Zach Brown | f046011 | 2010-07-14 13:55:35 -0700 | [diff] [blame] | 999 | * We want to wait for tx and rx completion to finish |
| 1000 | * before we tear down the connection, but we have to be |
| 1001 | * careful not to get stuck waiting on a send ring that |
| 1002 | * only has unsignaled sends in it. We've shutdown new |
| 1003 | * sends before getting here so by waiting for signaled |
| 1004 | * sends to complete we're ensured that there will be no |
| 1005 | * more tx processing. |
Andy Grover | e32b4a7 | 2010-03-03 19:25:21 -0800 | [diff] [blame] | 1006 | */ |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1007 | wait_event(rds_ib_ring_empty_wait, |
Zach Brown | f046011 | 2010-07-14 13:55:35 -0700 | [diff] [blame] | 1008 | rds_ib_ring_empty(&ic->i_recv_ring) && |
santosh.shilimkar@oracle.com | ad6832f | 2016-03-01 15:20:53 -0800 | [diff] [blame] | 1009 | (atomic_read(&ic->i_signaled_sends) == 0) && |
Santosh Shilimkar | 5601245 | 2016-03-08 09:19:01 -0800 | [diff] [blame] | 1010 | (atomic_read(&ic->i_fastreg_wrs) == RDS_IB_DEFAULT_FR_WR) && |
| 1011 | (atomic_read(&ic->i_fastunreg_wrs) == RDS_IB_DEFAULT_FR_INV_WR)); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 1012 | tasklet_kill(&ic->i_send_tasklet); |
Zach Brown | f046011 | 2010-07-14 13:55:35 -0700 | [diff] [blame] | 1013 | tasklet_kill(&ic->i_recv_tasklet); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1014 | |
Santosh Shilimkar | cf65726 | 2016-09-29 11:07:11 -0700 | [diff] [blame] | 1015 | atomic_set(&ic->i_cq_quiesce, 1); |
| 1016 | |
santosh.shilimkar@oracle.com | 1bc7b863 | 2015-08-22 15:45:24 -0700 | [diff] [blame] | 1017 | /* first destroy the ib state that generates callbacks */ |
| 1018 | if (ic->i_cm_id->qp) |
| 1019 | rdma_destroy_qp(ic->i_cm_id); |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 1020 | if (ic->i_send_cq) { |
| 1021 | if (ic->rds_ibdev) |
| 1022 | ibdev_put_vector(ic->rds_ibdev, ic->i_scq_vector); |
santosh.shilimkar@oracle.com | 1bc7b863 | 2015-08-22 15:45:24 -0700 | [diff] [blame] | 1023 | ib_destroy_cq(ic->i_send_cq); |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if (ic->i_recv_cq) { |
| 1027 | if (ic->rds_ibdev) |
| 1028 | ibdev_put_vector(ic->rds_ibdev, ic->i_rcq_vector); |
santosh.shilimkar@oracle.com | 1bc7b863 | 2015-08-22 15:45:24 -0700 | [diff] [blame] | 1029 | ib_destroy_cq(ic->i_recv_cq); |
Santosh Shilimkar | be2f76e | 2016-07-04 16:16:36 -0700 | [diff] [blame] | 1030 | } |
santosh.shilimkar@oracle.com | 1bc7b863 | 2015-08-22 15:45:24 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* then free the resources that ib callbacks use */ |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1033 | if (ic->i_send_hdrs) |
| 1034 | ib_dma_free_coherent(dev, |
| 1035 | ic->i_send_ring.w_nr * |
| 1036 | sizeof(struct rds_header), |
| 1037 | ic->i_send_hdrs, |
| 1038 | ic->i_send_hdrs_dma); |
| 1039 | |
| 1040 | if (ic->i_recv_hdrs) |
| 1041 | ib_dma_free_coherent(dev, |
| 1042 | ic->i_recv_ring.w_nr * |
| 1043 | sizeof(struct rds_header), |
| 1044 | ic->i_recv_hdrs, |
| 1045 | ic->i_recv_hdrs_dma); |
| 1046 | |
| 1047 | if (ic->i_ack) |
| 1048 | ib_dma_free_coherent(dev, sizeof(struct rds_header), |
| 1049 | ic->i_ack, ic->i_ack_dma); |
| 1050 | |
| 1051 | if (ic->i_sends) |
| 1052 | rds_ib_send_clear_ring(ic); |
| 1053 | if (ic->i_recvs) |
| 1054 | rds_ib_recv_clear_ring(ic); |
| 1055 | |
Santosh Shilimkar | 1c3be62 | 2015-08-22 15:45:32 -0700 | [diff] [blame] | 1056 | rdma_destroy_id(ic->i_cm_id); |
| 1057 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1058 | /* |
| 1059 | * Move connection back to the nodev list. |
| 1060 | */ |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 1061 | if (ic->rds_ibdev) |
| 1062 | rds_ib_remove_conn(ic->rds_ibdev, conn); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1063 | |
| 1064 | ic->i_cm_id = NULL; |
| 1065 | ic->i_pd = NULL; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1066 | ic->i_send_cq = NULL; |
| 1067 | ic->i_recv_cq = NULL; |
| 1068 | ic->i_send_hdrs = NULL; |
| 1069 | ic->i_recv_hdrs = NULL; |
| 1070 | ic->i_ack = NULL; |
| 1071 | } |
| 1072 | BUG_ON(ic->rds_ibdev); |
| 1073 | |
| 1074 | /* Clear pending transmit */ |
Andy Grover | ff3d7d3 | 2010-03-01 14:03:09 -0800 | [diff] [blame] | 1075 | if (ic->i_data_op) { |
| 1076 | struct rds_message *rm; |
| 1077 | |
| 1078 | rm = container_of(ic->i_data_op, struct rds_message, data); |
| 1079 | rds_message_put(rm); |
| 1080 | ic->i_data_op = NULL; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /* Clear the ACK state */ |
| 1084 | clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags); |
Andy Grover | 8cbd960 | 2009-04-01 08:20:20 +0000 | [diff] [blame] | 1085 | #ifdef KERNEL_HAS_ATOMIC64 |
| 1086 | atomic64_set(&ic->i_ack_next, 0); |
| 1087 | #else |
| 1088 | ic->i_ack_next = 0; |
| 1089 | #endif |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1090 | ic->i_ack_recv = 0; |
| 1091 | |
| 1092 | /* Clear flow control state */ |
| 1093 | ic->i_flowctl = 0; |
| 1094 | atomic_set(&ic->i_credits, 0); |
| 1095 | |
| 1096 | rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr); |
| 1097 | rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr); |
| 1098 | |
| 1099 | if (ic->i_ibinc) { |
| 1100 | rds_inc_put(&ic->i_ibinc->ii_inc); |
| 1101 | ic->i_ibinc = NULL; |
| 1102 | } |
| 1103 | |
| 1104 | vfree(ic->i_sends); |
| 1105 | ic->i_sends = NULL; |
| 1106 | vfree(ic->i_recvs); |
| 1107 | ic->i_recvs = NULL; |
Santosh Shilimkar | 581d53c | 2016-07-09 18:31:38 -0700 | [diff] [blame] | 1108 | ic->i_active_side = false; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp) |
| 1112 | { |
| 1113 | struct rds_ib_connection *ic; |
| 1114 | unsigned long flags; |
Chris Mason | 3324412 | 2010-05-26 22:05:37 -0700 | [diff] [blame] | 1115 | int ret; |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* XXX too lazy? */ |
Dan Carpenter | f0229ea | 2012-03-21 20:44:09 +0000 | [diff] [blame] | 1118 | ic = kzalloc(sizeof(struct rds_ib_connection), gfp); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 1119 | if (!ic) |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1120 | return -ENOMEM; |
| 1121 | |
Ka-Cheong Poon | f394ad2 | 2018-07-30 22:48:41 -0700 | [diff] [blame] | 1122 | ret = rds_ib_recv_alloc_caches(ic, gfp); |
Chris Mason | 3324412 | 2010-05-26 22:05:37 -0700 | [diff] [blame] | 1123 | if (ret) { |
| 1124 | kfree(ic); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1128 | INIT_LIST_HEAD(&ic->ib_node); |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 1129 | tasklet_init(&ic->i_send_tasklet, rds_ib_tasklet_fn_send, |
| 1130 | (unsigned long)ic); |
Santosh Shilimkar | f4f943c | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 1131 | tasklet_init(&ic->i_recv_tasklet, rds_ib_tasklet_fn_recv, |
Santosh Shilimkar | 0c28c04 | 2015-09-06 02:18:51 -0400 | [diff] [blame] | 1132 | (unsigned long)ic); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1133 | mutex_init(&ic->i_recv_mutex); |
Andy Grover | 8cbd960 | 2009-04-01 08:20:20 +0000 | [diff] [blame] | 1134 | #ifndef KERNEL_HAS_ATOMIC64 |
| 1135 | spin_lock_init(&ic->i_ack_lock); |
| 1136 | #endif |
Zach Brown | f046011 | 2010-07-14 13:55:35 -0700 | [diff] [blame] | 1137 | atomic_set(&ic->i_signaled_sends, 0); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* |
| 1140 | * rds_ib_conn_shutdown() waits for these to be emptied so they |
| 1141 | * must be initialized before it can be called. |
| 1142 | */ |
| 1143 | rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr); |
| 1144 | rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr); |
| 1145 | |
| 1146 | ic->conn = conn; |
| 1147 | conn->c_transport_data = ic; |
| 1148 | |
| 1149 | spin_lock_irqsave(&ib_nodev_conns_lock, flags); |
| 1150 | list_add_tail(&ic->ib_node, &ib_nodev_conns); |
| 1151 | spin_unlock_irqrestore(&ib_nodev_conns_lock, flags); |
| 1152 | |
| 1153 | |
| 1154 | rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 1158 | /* |
| 1159 | * Free a connection. Connection must be shut down and not set for reconnect. |
| 1160 | */ |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1161 | void rds_ib_conn_free(void *arg) |
| 1162 | { |
| 1163 | struct rds_ib_connection *ic = arg; |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 1164 | spinlock_t *lock_ptr; |
| 1165 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1166 | rdsdebug("ic %p\n", ic); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* |
| 1169 | * Conn is either on a dev's list or on the nodev list. |
| 1170 | * A race with shutdown() or connect() would cause problems |
| 1171 | * (since rds_ibdev would change) but that should never happen. |
| 1172 | */ |
| 1173 | lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock; |
| 1174 | |
| 1175 | spin_lock_irq(lock_ptr); |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1176 | list_del(&ic->ib_node); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 1177 | spin_unlock_irq(lock_ptr); |
| 1178 | |
Chris Mason | 3324412 | 2010-05-26 22:05:37 -0700 | [diff] [blame] | 1179 | rds_ib_recv_free_caches(ic); |
| 1180 | |
Andy Grover | ec16227 | 2009-02-24 15:30:30 +0000 | [diff] [blame] | 1181 | kfree(ic); |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | /* |
| 1186 | * An error occurred on the connection |
| 1187 | */ |
| 1188 | void |
| 1189 | __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...) |
| 1190 | { |
| 1191 | va_list ap; |
| 1192 | |
| 1193 | rds_conn_drop(conn); |
| 1194 | |
| 1195 | va_start(ap, fmt); |
| 1196 | vprintk(fmt, ap); |
| 1197 | va_end(ap); |
| 1198 | } |