blob: a1c3ad380ec81fb43b9900dc4dbd9fff7e37cad1 [file] [log] [blame]
Andy Groverec162272009-02-24 15:30:30 +00001/*
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -07002 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
Andy Groverec162272009-02-24 15:30:30 +00003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Andy Groverec162272009-02-24 15:30:30 +000036#include <linux/vmalloc.h>
Manuel Zerpiescb0a6052011-06-16 02:09:57 +000037#include <linux/ratelimit.h>
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070038#include <net/addrconf.h>
Andy Groverec162272009-02-24 15:30:30 +000039
Sowmini Varadhan0cb43962016-06-13 09:44:26 -070040#include "rds_single_path.h"
Andy Groverec162272009-02-24 15:30:30 +000041#include "rds.h"
42#include "ib.h"
43
44/*
45 * Set the selected protocol version
46 */
47static 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 */
55static 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 */
82static void
83rds_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 */
97void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
98{
Andy Groverec162272009-02-24 15:30:30 +000099 struct rds_ib_connection *ic = conn->c_transport_data;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700100 const union rds_ib_conn_priv *dp = NULL;
Andy Groverec162272009-02-24 15:30:30 +0000101 struct ib_qp_attr qp_attr;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700102 __be64 ack_seq = 0;
103 __be32 credit = 0;
104 u8 major = 0;
105 u8 minor = 0;
Andy Groverec162272009-02-24 15:30:30 +0000106 int err;
107
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700108 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 Grover02a6a252009-07-17 13:13:24 +0000121 }
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700122 } 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 Groverec162272009-02-24 15:30:30 +0000134 }
135
Santosh Shilimkarcdc306a2018-10-13 20:34:42 +0800136 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 Groverf147dd92010-01-13 15:50:09 -0800145 }
Andy Groverec162272009-02-24 15:30:30 +0000146
Santosh Shilimkarcdc306a2018-10-13 20:34:42 +0800147 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 Shilimkarcf657262016-09-29 11:07:11 -0700154 atomic_set(&ic->i_cq_quiesce, 0);
155
Santosh Shilimkar581d53c2016-07-09 18:31:38 -0700156 /* 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 Grovere11d9122009-07-17 13:13:29 +0000159 */
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.com73ce4312015-08-22 15:45:26 -0700164 rds_ib_recv_refill(conn, 1, GFP_KERNEL);
Andy Grovere11d9122009-07-17 13:13:29 +0000165
Andy Groverec162272009-02-24 15:30:30 +0000166 /* 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 Brown3e0249f2010-05-18 15:48:51 -0700174 /* update ib_device with this local ipaddr */
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700175 err = rds_ib_update_ipaddr(ic->rds_ibdev, &conn->c_laddr);
Andy Groverec162272009-02-24 15:30:30 +0000176 if (err)
Zach Brown3e0249f2010-05-18 15:48:51 -0700177 printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n",
178 err);
Andy Groverec162272009-02-24 15:30:30 +0000179
180 /* If the peer gave us the last packet it saw, process this as if
181 * we had received a regular ACK. */
shamir rabinovitchc0adf542015-04-30 20:58:07 -0400182 if (dp) {
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700183 if (ack_seq)
184 rds_send_drop_acked(conn, be64_to_cpu(ack_seq),
shamir rabinovitchc0adf542015-04-30 20:58:07 -0400185 NULL);
186 }
Andy Groverec162272009-02-24 15:30:30 +0000187
Santosh Shilimkarcdc306a2018-10-13 20:34:42 +0800188 conn->c_proposed_version = conn->c_version;
Andy Groverec162272009-02-24 15:30:30 +0000189 rds_connect_complete(conn);
190}
191
192static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700193 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 Groverec162272009-02-24 15:30:30 +0000199{
Andy Grover40589e72010-01-12 10:50:48 -0800200 struct rds_ib_connection *ic = conn->c_transport_data;
Zach Brown3e0249f2010-05-18 15:48:51 -0700201 struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
Andy Grover40589e72010-01-12 10:50:48 -0800202
Andy Groverec162272009-02-24 15:30:30 +0000203 memset(conn_param, 0, sizeof(struct rdma_conn_param));
Andy Grover40589e72010-01-12 10:50:48 -0800204
Andy Grover40589e72010-01-12 10:50:48 -0800205 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 Grover3ba23ad2009-07-17 13:13:22 +0000209 conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7);
Andy Groverec162272009-02-24 15:30:30 +0000210 conn_param->rnr_retry_count = 7;
211
212 if (dp) {
Andy Groverec162272009-02-24 15:30:30 +0000213 memset(dp, 0, sizeof(*dp));
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700214 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 Groverec162272009-02-24 15:30:30 +0000243
244 /* Advertise flow control */
245 if (ic->i_flowctl) {
246 unsigned int credits;
247
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700248 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 Groverec162272009-02-24 15:30:30 +0000256 }
Andy Groverec162272009-02-24 15:30:30 +0000257 }
258}
259
260static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
261{
Zach Brown1bde04a2010-07-14 14:01:21 -0700262 rdsdebug("event %u (%s) data %p\n",
Sagi Grimberg3c88f3d2015-05-18 13:40:33 +0300263 event->event, ib_event_msg(event->event), data);
Andy Groverec162272009-02-24 15:30:30 +0000264}
265
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400266/* 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 */
274static 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.comdcfd0412016-03-01 15:20:45 -0800286static void poll_scq(struct rds_ib_connection *ic, struct ib_cq *cq,
287 struct ib_wc *wcs)
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400288{
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800289 int nr, i;
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400290 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 Shilimkar0c28c042015-09-06 02:18:51 -0400298
Avinash Repaka16591852016-03-01 15:20:54 -0800299 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 Shilimkarf4f943c2015-09-06 02:18:51 -0400305 }
306 }
307}
308
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400309static 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 Shilimkar0c28c042015-09-06 02:18:51 -0400313
314 rds_ib_stats_inc(s_ib_tasklet_call);
315
Santosh Shilimkarcf657262016-09-29 11:07:11 -0700316 /* if cq has been already reaped, ignore incoming cq event */
317 if (atomic_read(&ic->i_cq_quiesce))
318 return;
319
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800320 poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400321 ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800322 poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400323
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 Varadhan1f9ecd72016-06-13 09:44:34 -0700327 rds_send_xmit(&ic->conn->c_path[0]);
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400328}
329
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800330static 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 Shilimkarf4f943c2015-09-06 02:18:51 -0400349static 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 Shilimkar9441c972015-09-19 14:01:09 -0400356 if (!rds_ibdev)
357 rds_conn_drop(conn);
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400358
359 rds_ib_stats_inc(s_ib_tasklet_call);
360
Santosh Shilimkarcf657262016-09-29 11:07:11 -0700361 /* if cq has been already reaped, ignore incoming cq event */
362 if (atomic_read(&ic->i_cq_quiesce))
363 return;
364
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400365 memset(&state, 0, sizeof(state));
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800366 poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400367 ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
santosh.shilimkar@oracle.comdcfd0412016-03-01 15:20:45 -0800368 poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400369
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 Groverec162272009-02-24 15:30:30 +0000381static 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 Brown1bde04a2010-07-14 14:01:21 -0700386 rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event,
Sagi Grimberg3c88f3d2015-05-18 13:40:33 +0300387 ib_event_msg(event->event));
Andy Groverec162272009-02-24 15:30:30 +0000388
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 Brown1bde04a2010-07-14 14:01:21 -0700394 rdsdebug("Fatal QP Event %u (%s) "
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700395 "- connection %pI6c->%pI6c, reconnecting\n",
Sagi Grimberg3c88f3d2015-05-18 13:40:33 +0300396 event->event, ib_event_msg(event->event),
Zach Brown1bde04a2010-07-14 14:01:21 -0700397 &conn->c_laddr, &conn->c_faddr);
Andy Grover97069782010-03-11 13:50:02 +0000398 rds_conn_drop(conn);
Andy Groverec162272009-02-24 15:30:30 +0000399 break;
400 }
401}
402
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400403static 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 Shilimkarbe2f76e2016-07-04 16:16:36 -0700415static 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
432static inline void ibdev_put_vector(struct rds_ib_device *rds_ibdev, int index)
433{
434 rds_ibdev->vector_load[index]--;
435}
436
Andy Groverec162272009-02-24 15:30:30 +0000437/*
438 * This needs to be very careful to not leave IS_ERR pointers around for
439 * cleanup to trip over.
440 */
441static 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 Barak8e372102015-06-11 16:35:21 +0300446 struct ib_cq_init_attr cq_attr = {};
Andy Groverec162272009-02-24 15:30:30 +0000447 struct rds_ib_device *rds_ibdev;
santosh.shilimkar@oracle.comad6832f2016-03-01 15:20:53 -0800448 int ret, fr_queue_space;
Andy Groverec162272009-02-24 15:30:30 +0000449
Zach Brown3e0249f2010-05-18 15:48:51 -0700450 /*
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 Groverec162272009-02-24 15:30:30 +0000453 */
Zach Brown3e0249f2010-05-18 15:48:51 -0700454 rds_ibdev = rds_ib_get_client_data(dev);
455 if (!rds_ibdev)
Andy Groverec162272009-02-24 15:30:30 +0000456 return -EOPNOTSUPP;
Zach Brown3e0249f2010-05-18 15:48:51 -0700457
santosh.shilimkar@oracle.comad6832f2016-03-01 15:20:53 -0800458 /* 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 Shilimkar56012452016-03-08 09:19:01 -0800462 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.comad6832f2016-03-01 15:20:53 -0800466
Zach Brown3e0249f2010-05-18 15:48:51 -0700467 /* add the conn now so that connection establishment has the dev */
468 rds_ib_add_conn(rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +0000469
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 Groverec162272009-02-24 15:30:30 +0000477
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700478 ic->i_scq_vector = ibdev_get_unused_vector(rds_ibdev);
santosh.shilimkar@oracle.comad6832f2016-03-01 15:20:53 -0800479 cq_attr.cqe = ic->i_send_ring.w_nr + fr_queue_space + 1;
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700480 cq_attr.comp_vector = ic->i_scq_vector;
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400481 ic->i_send_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_send,
Andy Groverec162272009-02-24 15:30:30 +0000482 rds_ib_cq_event_handler, conn,
Matan Barak8e372102015-06-11 16:35:21 +0300483 &cq_attr);
Andy Groverec162272009-02-24 15:30:30 +0000484 if (IS_ERR(ic->i_send_cq)) {
485 ret = PTR_ERR(ic->i_send_cq);
486 ic->i_send_cq = NULL;
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700487 ibdev_put_vector(rds_ibdev, ic->i_scq_vector);
Andy Groverec162272009-02-24 15:30:30 +0000488 rdsdebug("ib_create_cq send failed: %d\n", ret);
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500489 goto rds_ibdev_out;
Andy Groverec162272009-02-24 15:30:30 +0000490 }
491
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700492 ic->i_rcq_vector = ibdev_get_unused_vector(rds_ibdev);
Matan Barak8e372102015-06-11 16:35:21 +0300493 cq_attr.cqe = ic->i_recv_ring.w_nr;
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700494 cq_attr.comp_vector = ic->i_rcq_vector;
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -0400495 ic->i_recv_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_recv,
Andy Groverec162272009-02-24 15:30:30 +0000496 rds_ib_cq_event_handler, conn,
Matan Barak8e372102015-06-11 16:35:21 +0300497 &cq_attr);
Andy Groverec162272009-02-24 15:30:30 +0000498 if (IS_ERR(ic->i_recv_cq)) {
499 ret = PTR_ERR(ic->i_recv_cq);
500 ic->i_recv_cq = NULL;
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -0700501 ibdev_put_vector(rds_ibdev, ic->i_rcq_vector);
Andy Groverec162272009-02-24 15:30:30 +0000502 rdsdebug("ib_create_cq recv failed: %d\n", ret);
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500503 goto send_cq_out;
Andy Groverec162272009-02-24 15:30:30 +0000504 }
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 Yanjun3b12f732017-03-07 02:48:36 -0500509 goto recv_cq_out;
Andy Groverec162272009-02-24 15:30:30 +0000510 }
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 Yanjun3b12f732017-03-07 02:48:36 -0500515 goto recv_cq_out;
Andy Groverec162272009-02-24 15:30:30 +0000516 }
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.comad6832f2016-03-01 15:20:53 -0800523 attr.cap.max_send_wr = ic->i_send_ring.w_nr + fr_queue_space + 1;
Andy Groverec162272009-02-24 15:30:30 +0000524 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.comad6832f2016-03-01 15:20:53 -0800531 atomic_set(&ic->i_fastreg_wrs, RDS_IB_DEFAULT_FR_WR);
Santosh Shilimkar56012452016-03-08 09:19:01 -0800532 atomic_set(&ic->i_fastunreg_wrs, RDS_IB_DEFAULT_FR_INV_WR);
Andy Groverec162272009-02-24 15:30:30 +0000533
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 Yanjun3b12f732017-03-07 02:48:36 -0500541 goto recv_cq_out;
Andy Groverec162272009-02-24 15:30:30 +0000542 }
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 Grover8690bfa2010-01-12 11:56:44 -0800548 if (!ic->i_send_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000549 ret = -ENOMEM;
550 rdsdebug("ib_dma_alloc_coherent send failed\n");
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500551 goto qp_out;
Andy Groverec162272009-02-24 15:30:30 +0000552 }
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 Grover8690bfa2010-01-12 11:56:44 -0800558 if (!ic->i_recv_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000559 ret = -ENOMEM;
560 rdsdebug("ib_dma_alloc_coherent recv failed\n");
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500561 goto send_hdrs_dma_out;
Andy Groverec162272009-02-24 15:30:30 +0000562 }
563
564 ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
565 &ic->i_ack_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800566 if (!ic->i_ack) {
Andy Groverec162272009-02-24 15:30:30 +0000567 ret = -ENOMEM;
568 rdsdebug("ib_dma_alloc_coherent ack failed\n");
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500569 goto recv_hdrs_dma_out;
Andy Groverec162272009-02-24 15:30:30 +0000570 }
571
Kees Cookfd7bece2018-06-12 14:27:52 -0700572 ic->i_sends = vzalloc_node(array_size(sizeof(struct rds_ib_send_work),
573 ic->i_send_ring.w_nr),
Andy Grovere4c52c92010-04-23 10:49:53 -0700574 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800575 if (!ic->i_sends) {
Andy Groverec162272009-02-24 15:30:30 +0000576 ret = -ENOMEM;
577 rdsdebug("send allocation failed\n");
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500578 goto ack_dma_out;
Andy Groverec162272009-02-24 15:30:30 +0000579 }
Andy Groverec162272009-02-24 15:30:30 +0000580
Kees Cookfd7bece2018-06-12 14:27:52 -0700581 ic->i_recvs = vzalloc_node(array_size(sizeof(struct rds_ib_recv_work),
582 ic->i_recv_ring.w_nr),
Andy Grovere4c52c92010-04-23 10:49:53 -0700583 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800584 if (!ic->i_recvs) {
Andy Groverec162272009-02-24 15:30:30 +0000585 ret = -ENOMEM;
586 rdsdebug("recv allocation failed\n");
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500587 goto sends_out;
Andy Groverec162272009-02-24 15:30:30 +0000588 }
589
Andy Groverec162272009-02-24 15:30:30 +0000590 rds_ib_recv_init_ack(ic);
591
Jason Gunthorpee5580242015-07-30 17:22:26 -0600592 rdsdebug("conn %p pd %p cq %p %p\n", conn, ic->i_pd,
Andy Groverec162272009-02-24 15:30:30 +0000593 ic->i_send_cq, ic->i_recv_cq);
594
Dag Moxnes91a82522018-04-25 13:22:01 +0200595 goto out;
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500596
597sends_out:
598 vfree(ic->i_sends);
599ack_dma_out:
600 ib_dma_free_coherent(dev, sizeof(struct rds_header),
601 ic->i_ack, ic->i_ack_dma);
602recv_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);
606send_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);
610qp_out:
611 rdma_destroy_qp(ic->i_cm_id);
612recv_cq_out:
613 if (!ib_destroy_cq(ic->i_recv_cq))
614 ic->i_recv_cq = NULL;
615send_cq_out:
616 if (!ib_destroy_cq(ic->i_send_cq))
617 ic->i_send_cq = NULL;
618rds_ibdev_out:
619 rds_ib_remove_conn(rds_ibdev, conn);
Dag Moxnes91a82522018-04-25 13:22:01 +0200620out:
Zach Brown3e0249f2010-05-18 15:48:51 -0700621 rds_ib_dev_put(rds_ibdev);
Zhu Yanjun3b12f732017-03-07 02:48:36 -0500622
Andy Groverec162272009-02-24 15:30:30 +0000623 return ret;
624}
625
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700626static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event, bool isv6)
Andy Groverec162272009-02-24 15:30:30 +0000627{
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700628 const union rds_ib_conn_priv *dp = event->param.conn.private_data;
629 u8 data_len, major, minor;
Andy Groverec162272009-02-24 15:30:30 +0000630 u32 version = 0;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700631 __be16 mask;
632 u16 common;
Andy Groverec162272009-02-24 15:30:30 +0000633
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000634 /*
635 * rdma_cm private data is odd - when there is any private data in the
Andy Groverec162272009-02-24 15:30:30 +0000636 * 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 Grover9ddbcfa2009-07-17 13:13:23 +0000641 * 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 Pooneee2fa62018-07-23 20:51:21 -0700651 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 Grover9ddbcfa2009-07-17 13:13:23 +0000663 /* Even if len is crap *now* I still want to check it. -ASG */
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700664 if (event->param.conn.private_data_len < data_len || major == 0)
Andy Groverec162272009-02-24 15:30:30 +0000665 return RDS_PROTOCOL_3_0;
666
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700667 common = be16_to_cpu(mask) & RDS_IB_SUPPORTED_PROTOCOLS;
668 if (major == 3 && common) {
Andy Groverec162272009-02-24 15:30:30 +0000669 version = RDS_PROTOCOL_3_0;
670 while ((common >>= 1) != 0)
671 version++;
Santosh Shilimkarcdc306a2018-10-13 20:34:42 +0800672 } else if (RDS_PROTOCOL_COMPAT_VERSION ==
673 RDS_PROTOCOL(major, minor)) {
674 version = RDS_PROTOCOL_COMPAT_VERSION;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700675 } 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 Groverec162272009-02-24 15:30:30 +0000683 return version;
684}
685
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700686#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700687/* Given an IPv6 address, find the net_device which hosts that address and
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700688 * 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 */
697static 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 Poon1e2b44e2018-07-23 20:51:22 -0700704 if (ipv6_chk_addr(net, addr, dev, 1)) {
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700705 idx = dev->ifindex;
706 break;
707 }
708 }
709 rcu_read_unlock();
710
711 return idx;
712}
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700713#endif
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700714
Andy Groverec162272009-02-24 15:30:30 +0000715int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700716 struct rdma_cm_event *event, bool isv6)
Andy Groverec162272009-02-24 15:30:30 +0000717{
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 Pooneee2fa62018-07-23 20:51:21 -0700720 const struct rds_ib_conn_priv_cmn *dp_cmn;
Andy Groverec162272009-02-24 15:30:30 +0000721 struct rds_connection *conn = NULL;
722 struct rds_ib_connection *ic = NULL;
723 struct rdma_conn_param conn_param;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700724 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 Groverec162272009-02-24 15:30:30 +0000732 u32 version;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700733 int err = 1;
Andy Groverec162272009-02-24 15:30:30 +0000734
735 /* Check whether the remote protocol version matches ours. */
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700736 version = rds_ib_protocol_compatible(event, isv6);
Santosh Shilimkard021fab2018-10-23 23:09:00 -0400737 if (!version) {
738 err = RDS_RDMA_REJ_INCOMPAT;
Andy Groverec162272009-02-24 15:30:30 +0000739 goto out;
Santosh Shilimkard021fab2018-10-23 23:09:00 -0400740 }
Andy Groverec162272009-02-24 15:30:30 +0000741
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700742 dp = event->param.conn.private_data;
743 if (isv6) {
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700744#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700745 dp_cmn = &dp->ricp_v6.dp_cmn;
746 saddr6 = &dp->ricp_v6.dp_saddr;
747 daddr6 = &dp->ricp_v6.dp_daddr;
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700748 /* If either address is link local, need to find the
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700749 * 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 Poon1e2b44e2018-07-23 20:51:22 -0700760 } 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 Pooneee2fa62018-07-23 20:51:21 -0700768 }
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700769#else
770 err = -EOPNOTSUPP;
771 goto out;
772#endif
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700773 } 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 Groverec162272009-02-24 15:30:30 +0000783 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 Varadhand5a8ac22015-08-05 01:43:25 -0400787 /* RDS/IB is not currently netns aware, thus init_net */
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700788 conn = rds_conn_create(&init_net, daddr6, saddr6,
789 &rds_ib_transport, GFP_KERNEL, ifindex);
Andy Groverec162272009-02-24 15:30:30 +0000790 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 Groverec162272009-02-24 15:30:30 +0000814 goto out;
815 }
816
817 ic = conn->c_transport_data;
818
819 rds_ib_set_protocol(conn, version);
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700820 rds_ib_set_flow_control(conn, be32_to_cpu(dp_cmn->ricpc_credit));
Andy Groverec162272009-02-24 15:30:30 +0000821
822 /* If the peer gave us the last packet it saw, process this as if
823 * we had received a regular ACK. */
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700824 if (dp_cmn->ricpc_ack_seq)
825 rds_send_drop_acked(conn, be64_to_cpu(dp_cmn->ricpc_ack_seq),
826 NULL);
Andy Groverec162272009-02-24 15:30:30 +0000827
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 Grover40589e72010-01-12 10:50:48 -0800844 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version,
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700845 event->param.conn.responder_resources,
846 event->param.conn.initiator_depth, isv6);
Andy Groverec162272009-02-24 15:30:30 +0000847
848 /* rdma_accept() calls rdma_reject() internally if it fails */
Zhu Yanjunb418c522017-03-13 01:43:45 -0400849 if (rdma_accept(cm_id, &conn_param))
850 rds_ib_conn_error(conn, "rdma_accept failed\n");
Andy Groverec162272009-02-24 15:30:30 +0000851
852out:
Zach Browna46ca942010-05-24 13:14:59 -0700853 if (conn)
854 mutex_unlock(&conn->c_cm_lock);
855 if (err)
Santosh Shilimkard021fab2018-10-23 23:09:00 -0400856 rdma_reject(cm_id, &err, sizeof(int));
Andy Groverec162272009-02-24 15:30:30 +0000857 return destroy;
858}
859
860
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700861int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6)
Andy Groverec162272009-02-24 15:30:30 +0000862{
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 Pooneee2fa62018-07-23 20:51:21 -0700866 union rds_ib_conn_priv dp;
Andy Groverec162272009-02-24 15:30:30 +0000867 int ret;
868
869 /* If the peer doesn't do protocol negotiation, we must
870 * default to RDSv3.0 */
Santosh Shilimkarcdc306a2018-10-13 20:34:42 +0800871 rds_ib_set_protocol(conn, RDS_PROTOCOL_VERSION);
Andy Groverec162272009-02-24 15:30:30 +0000872 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 Shilimkarcdc306a2018-10-13 20:34:42 +0800880 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp,
881 conn->c_proposed_version,
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700882 UINT_MAX, UINT_MAX, isv6);
Andy Groverec162272009-02-24 15:30:30 +0000883 ret = rdma_connect(cm_id, &conn_param);
884 if (ret)
885 rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
886
887out:
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 Shilimkar581d53c2016-07-09 18:31:38 -0700895 ic->i_active_side = true;
Andy Groverec162272009-02-24 15:30:30 +0000896 return ret;
897}
898
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700899int rds_ib_conn_path_connect(struct rds_conn_path *cp)
Andy Groverec162272009-02-24 15:30:30 +0000900{
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700901 struct rds_connection *conn = cp->cp_conn;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700902 struct sockaddr_storage src, dest;
903 rdma_cm_event_handler handler;
904 struct rds_ib_connection *ic;
Andy Groverec162272009-02-24 15:30:30 +0000905 int ret;
906
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700907 ic = conn->c_transport_data;
908
Andy Groverec162272009-02-24 15:30:30 +0000909 /* XXX I wonder what affect the port space has */
910 /* delegate cm event handler to rdma_transport */
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700911#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700912 if (conn->c_isv6)
913 handler = rds6_rdma_cm_event_handler;
914 else
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700915#endif
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700916 handler = rds_rdma_cm_event_handler;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700917 ic->i_cm_id = rdma_create_id(&init_net, handler, conn,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000918 RDMA_PS_TCP, IB_QPT_RC);
Andy Groverec162272009-02-24 15:30:30 +0000919 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 Pooneee2fa62018-07-23 20:51:21 -0700928 if (ipv6_addr_v4mapped(&conn->c_faddr)) {
929 struct sockaddr_in *sin;
Andy Groverec162272009-02-24 15:30:30 +0000930
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700931 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 Groverec162272009-02-24 15:30:30 +0000955
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
966out:
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 Varadhan226f7a72016-06-30 16:11:10 -0700975void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
Andy Groverec162272009-02-24 15:30:30 +0000976{
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700977 struct rds_connection *conn = cp->cp_conn;
Andy Groverec162272009-02-24 15:30:30 +0000978 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 Grovere32b4a72010-03-03 19:25:21 -0800998 /*
Zach Brownf0460112010-07-14 13:55:35 -0700999 * 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 Grovere32b4a72010-03-03 19:25:21 -08001006 */
Andy Groverec162272009-02-24 15:30:30 +00001007 wait_event(rds_ib_ring_empty_wait,
Zach Brownf0460112010-07-14 13:55:35 -07001008 rds_ib_ring_empty(&ic->i_recv_ring) &&
santosh.shilimkar@oracle.comad6832f2016-03-01 15:20:53 -08001009 (atomic_read(&ic->i_signaled_sends) == 0) &&
Santosh Shilimkar56012452016-03-08 09:19:01 -08001010 (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 Shilimkar0c28c042015-09-06 02:18:51 -04001012 tasklet_kill(&ic->i_send_tasklet);
Zach Brownf0460112010-07-14 13:55:35 -07001013 tasklet_kill(&ic->i_recv_tasklet);
Andy Groverec162272009-02-24 15:30:30 +00001014
Santosh Shilimkarcf657262016-09-29 11:07:11 -07001015 atomic_set(&ic->i_cq_quiesce, 1);
1016
santosh.shilimkar@oracle.com1bc7b8632015-08-22 15:45:24 -07001017 /* first destroy the ib state that generates callbacks */
1018 if (ic->i_cm_id->qp)
1019 rdma_destroy_qp(ic->i_cm_id);
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -07001020 if (ic->i_send_cq) {
1021 if (ic->rds_ibdev)
1022 ibdev_put_vector(ic->rds_ibdev, ic->i_scq_vector);
santosh.shilimkar@oracle.com1bc7b8632015-08-22 15:45:24 -07001023 ib_destroy_cq(ic->i_send_cq);
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -07001024 }
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.com1bc7b8632015-08-22 15:45:24 -07001029 ib_destroy_cq(ic->i_recv_cq);
Santosh Shilimkarbe2f76e2016-07-04 16:16:36 -07001030 }
santosh.shilimkar@oracle.com1bc7b8632015-08-22 15:45:24 -07001031
1032 /* then free the resources that ib callbacks use */
Andy Groverec162272009-02-24 15:30:30 +00001033 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 Shilimkar1c3be622015-08-22 15:45:32 -07001056 rdma_destroy_id(ic->i_cm_id);
1057
Andy Groverec162272009-02-24 15:30:30 +00001058 /*
1059 * Move connection back to the nodev list.
1060 */
Andy Grover745cbcc2009-04-01 08:20:19 +00001061 if (ic->rds_ibdev)
1062 rds_ib_remove_conn(ic->rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +00001063
1064 ic->i_cm_id = NULL;
1065 ic->i_pd = NULL;
Andy Groverec162272009-02-24 15:30:30 +00001066 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 Groverff3d7d32010-03-01 14:03:09 -08001075 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 Groverec162272009-02-24 15:30:30 +00001081 }
1082
1083 /* Clear the ACK state */
1084 clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
Andy Grover8cbd9602009-04-01 08:20:20 +00001085#ifdef KERNEL_HAS_ATOMIC64
1086 atomic64_set(&ic->i_ack_next, 0);
1087#else
1088 ic->i_ack_next = 0;
1089#endif
Andy Groverec162272009-02-24 15:30:30 +00001090 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 Shilimkar581d53c2016-07-09 18:31:38 -07001108 ic->i_active_side = false;
Andy Groverec162272009-02-24 15:30:30 +00001109}
1110
1111int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
1112{
1113 struct rds_ib_connection *ic;
1114 unsigned long flags;
Chris Mason33244122010-05-26 22:05:37 -07001115 int ret;
Andy Groverec162272009-02-24 15:30:30 +00001116
1117 /* XXX too lazy? */
Dan Carpenterf0229ea2012-03-21 20:44:09 +00001118 ic = kzalloc(sizeof(struct rds_ib_connection), gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -08001119 if (!ic)
Andy Groverec162272009-02-24 15:30:30 +00001120 return -ENOMEM;
1121
Ka-Cheong Poonf394ad22018-07-30 22:48:41 -07001122 ret = rds_ib_recv_alloc_caches(ic, gfp);
Chris Mason33244122010-05-26 22:05:37 -07001123 if (ret) {
1124 kfree(ic);
1125 return ret;
1126 }
1127
Andy Groverec162272009-02-24 15:30:30 +00001128 INIT_LIST_HEAD(&ic->ib_node);
Santosh Shilimkar0c28c042015-09-06 02:18:51 -04001129 tasklet_init(&ic->i_send_tasklet, rds_ib_tasklet_fn_send,
1130 (unsigned long)ic);
Santosh Shilimkarf4f943c2015-09-06 02:18:51 -04001131 tasklet_init(&ic->i_recv_tasklet, rds_ib_tasklet_fn_recv,
Santosh Shilimkar0c28c042015-09-06 02:18:51 -04001132 (unsigned long)ic);
Andy Groverec162272009-02-24 15:30:30 +00001133 mutex_init(&ic->i_recv_mutex);
Andy Grover8cbd9602009-04-01 08:20:20 +00001134#ifndef KERNEL_HAS_ATOMIC64
1135 spin_lock_init(&ic->i_ack_lock);
1136#endif
Zach Brownf0460112010-07-14 13:55:35 -07001137 atomic_set(&ic->i_signaled_sends, 0);
Andy Groverec162272009-02-24 15:30:30 +00001138
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 Grover745cbcc2009-04-01 08:20:19 +00001158/*
1159 * Free a connection. Connection must be shut down and not set for reconnect.
1160 */
Andy Groverec162272009-02-24 15:30:30 +00001161void rds_ib_conn_free(void *arg)
1162{
1163 struct rds_ib_connection *ic = arg;
Andy Grover745cbcc2009-04-01 08:20:19 +00001164 spinlock_t *lock_ptr;
1165
Andy Groverec162272009-02-24 15:30:30 +00001166 rdsdebug("ic %p\n", ic);
Andy Grover745cbcc2009-04-01 08:20:19 +00001167
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 Groverec162272009-02-24 15:30:30 +00001176 list_del(&ic->ib_node);
Andy Grover745cbcc2009-04-01 08:20:19 +00001177 spin_unlock_irq(lock_ptr);
1178
Chris Mason33244122010-05-26 22:05:37 -07001179 rds_ib_recv_free_caches(ic);
1180
Andy Groverec162272009-02-24 15:30:30 +00001181 kfree(ic);
1182}
1183
1184
1185/*
1186 * An error occurred on the connection
1187 */
1188void
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}