blob: 31b74f5e61adbd37535b636b1499c384bdd992f5 [file] [log] [blame]
Andy Groverec162272009-02-24 15:30:30 +00001/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
34#include <linux/in.h>
Tejun 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>
Andy Groverec162272009-02-24 15:30:30 +000038
39#include "rds.h"
40#include "ib.h"
41
Zach Brown1bde04a2010-07-14 14:01:21 -070042static char *rds_ib_event_type_strings[] = {
Zach Brown59f740a2010-08-03 13:52:47 -070043#define RDS_IB_EVENT_STRING(foo) \
44 [IB_EVENT_##foo] = __stringify(IB_EVENT_##foo)
Zach Brown1bde04a2010-07-14 14:01:21 -070045 RDS_IB_EVENT_STRING(CQ_ERR),
46 RDS_IB_EVENT_STRING(QP_FATAL),
47 RDS_IB_EVENT_STRING(QP_REQ_ERR),
48 RDS_IB_EVENT_STRING(QP_ACCESS_ERR),
49 RDS_IB_EVENT_STRING(COMM_EST),
50 RDS_IB_EVENT_STRING(SQ_DRAINED),
51 RDS_IB_EVENT_STRING(PATH_MIG),
52 RDS_IB_EVENT_STRING(PATH_MIG_ERR),
53 RDS_IB_EVENT_STRING(DEVICE_FATAL),
54 RDS_IB_EVENT_STRING(PORT_ACTIVE),
55 RDS_IB_EVENT_STRING(PORT_ERR),
56 RDS_IB_EVENT_STRING(LID_CHANGE),
57 RDS_IB_EVENT_STRING(PKEY_CHANGE),
58 RDS_IB_EVENT_STRING(SM_CHANGE),
59 RDS_IB_EVENT_STRING(SRQ_ERR),
60 RDS_IB_EVENT_STRING(SRQ_LIMIT_REACHED),
61 RDS_IB_EVENT_STRING(QP_LAST_WQE_REACHED),
62 RDS_IB_EVENT_STRING(CLIENT_REREGISTER),
63#undef RDS_IB_EVENT_STRING
64};
65
66static char *rds_ib_event_str(enum ib_event_type type)
67{
Zach Brown59f740a2010-08-03 13:52:47 -070068 return rds_str_array(rds_ib_event_type_strings,
69 ARRAY_SIZE(rds_ib_event_type_strings), type);
Zach Brown1bde04a2010-07-14 14:01:21 -070070};
71
Andy Groverec162272009-02-24 15:30:30 +000072/*
73 * Set the selected protocol version
74 */
75static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version)
76{
77 conn->c_version = version;
78}
79
80/*
81 * Set up flow control
82 */
83static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits)
84{
85 struct rds_ib_connection *ic = conn->c_transport_data;
86
87 if (rds_ib_sysctl_flow_control && credits != 0) {
88 /* We're doing flow control */
89 ic->i_flowctl = 1;
90 rds_ib_send_add_credits(conn, credits);
91 } else {
92 ic->i_flowctl = 0;
93 }
94}
95
96/*
97 * Tune RNR behavior. Without flow control, we use a rather
98 * low timeout, but not the absolute minimum - this should
99 * be tunable.
100 *
101 * We already set the RNR retry count to 7 (which is the
102 * smallest infinite number :-) above.
103 * If flow control is off, we want to change this back to 0
104 * so that we learn quickly when our credit accounting is
105 * buggy.
106 *
107 * Caller passes in a qp_attr pointer - don't waste stack spacv
108 * by allocation this twice.
109 */
110static void
111rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr)
112{
113 int ret;
114
115 attr->min_rnr_timer = IB_RNR_TIMER_000_32;
116 ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER);
117 if (ret)
118 printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret);
119}
120
121/*
122 * Connection established.
123 * We get here for both outgoing and incoming connection.
124 */
125void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
126{
127 const struct rds_ib_connect_private *dp = NULL;
128 struct rds_ib_connection *ic = conn->c_transport_data;
Andy Groverec162272009-02-24 15:30:30 +0000129 struct ib_qp_attr qp_attr;
130 int err;
131
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000132 if (event->param.conn.private_data_len >= sizeof(*dp)) {
Andy Groverec162272009-02-24 15:30:30 +0000133 dp = event->param.conn.private_data;
134
Andy Grover02a6a252009-07-17 13:13:24 +0000135 /* make sure it isn't empty data */
136 if (dp->dp_protocol_major) {
137 rds_ib_set_protocol(conn,
Andy Groverec162272009-02-24 15:30:30 +0000138 RDS_PROTOCOL(dp->dp_protocol_major,
Andy Grover02a6a252009-07-17 13:13:24 +0000139 dp->dp_protocol_minor));
140 rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
141 }
Andy Groverec162272009-02-24 15:30:30 +0000142 }
143
Andy Groverf147dd92010-01-13 15:50:09 -0800144 if (conn->c_version < RDS_PROTOCOL(3,1)) {
145 printk(KERN_NOTICE "RDS/IB: Connection to %pI4 version %u.%u failed,"
146 " no longer supported\n",
147 &conn->c_faddr,
148 RDS_PROTOCOL_MAJOR(conn->c_version),
149 RDS_PROTOCOL_MINOR(conn->c_version));
150 rds_conn_destroy(conn);
151 return;
152 } else {
153 printk(KERN_NOTICE "RDS/IB: connected to %pI4 version %u.%u%s\n",
154 &conn->c_faddr,
155 RDS_PROTOCOL_MAJOR(conn->c_version),
156 RDS_PROTOCOL_MINOR(conn->c_version),
157 ic->i_flowctl ? ", flow control" : "");
158 }
Andy Groverec162272009-02-24 15:30:30 +0000159
Andy Grovere11d9122009-07-17 13:13:29 +0000160 /*
161 * Init rings and fill recv. this needs to wait until protocol negotiation
162 * is complete, since ring layout is different from 3.0 to 3.1.
163 */
164 rds_ib_send_init_ring(ic);
165 rds_ib_recv_init_ring(ic);
166 /* Post receive buffers - as a side effect, this will update
167 * the posted credit count. */
Andy Groverf17a1a52010-03-18 17:19:52 -0700168 rds_ib_recv_refill(conn, 1);
Andy Grovere11d9122009-07-17 13:13:29 +0000169
Andy Groverec162272009-02-24 15:30:30 +0000170 /* Tune RNR behavior */
171 rds_ib_tune_rnr(ic, &qp_attr);
172
173 qp_attr.qp_state = IB_QPS_RTS;
174 err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
175 if (err)
176 printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
177
Zach Brown3e0249f2010-05-18 15:48:51 -0700178 /* update ib_device with this local ipaddr */
179 err = rds_ib_update_ipaddr(ic->rds_ibdev, conn->c_laddr);
Andy Groverec162272009-02-24 15:30:30 +0000180 if (err)
Zach Brown3e0249f2010-05-18 15:48:51 -0700181 printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n",
182 err);
Andy Groverec162272009-02-24 15:30:30 +0000183
184 /* If the peer gave us the last packet it saw, process this as if
185 * we had received a regular ACK. */
186 if (dp && dp->dp_ack_seq)
187 rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
188
189 rds_connect_complete(conn);
190}
191
192static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
193 struct rdma_conn_param *conn_param,
194 struct rds_ib_connect_private *dp,
Andy Grover40589e72010-01-12 10:50:48 -0800195 u32 protocol_version,
196 u32 max_responder_resources,
197 u32 max_initiator_depth)
Andy Groverec162272009-02-24 15:30:30 +0000198{
Andy Grover40589e72010-01-12 10:50:48 -0800199 struct rds_ib_connection *ic = conn->c_transport_data;
Zach Brown3e0249f2010-05-18 15:48:51 -0700200 struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
Andy Grover40589e72010-01-12 10:50:48 -0800201
Andy Groverec162272009-02-24 15:30:30 +0000202 memset(conn_param, 0, sizeof(struct rdma_conn_param));
Andy Grover40589e72010-01-12 10:50:48 -0800203
Andy Grover40589e72010-01-12 10:50:48 -0800204 conn_param->responder_resources =
205 min_t(u32, rds_ibdev->max_responder_resources, max_responder_resources);
206 conn_param->initiator_depth =
207 min_t(u32, rds_ibdev->max_initiator_depth, max_initiator_depth);
Andy Grover3ba23ad2009-07-17 13:13:22 +0000208 conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7);
Andy Groverec162272009-02-24 15:30:30 +0000209 conn_param->rnr_retry_count = 7;
210
211 if (dp) {
Andy Groverec162272009-02-24 15:30:30 +0000212 memset(dp, 0, sizeof(*dp));
213 dp->dp_saddr = conn->c_laddr;
214 dp->dp_daddr = conn->c_faddr;
215 dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
216 dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
217 dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
218 dp->dp_ack_seq = rds_ib_piggyb_ack(ic);
219
220 /* Advertise flow control */
221 if (ic->i_flowctl) {
222 unsigned int credits;
223
224 credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
225 dp->dp_credit = cpu_to_be32(credits);
226 atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
227 }
228
229 conn_param->private_data = dp;
230 conn_param->private_data_len = sizeof(*dp);
231 }
232}
233
234static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
235{
Zach Brown1bde04a2010-07-14 14:01:21 -0700236 rdsdebug("event %u (%s) data %p\n",
237 event->event, rds_ib_event_str(event->event), data);
Andy Groverec162272009-02-24 15:30:30 +0000238}
239
240static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
241{
242 struct rds_connection *conn = data;
243 struct rds_ib_connection *ic = conn->c_transport_data;
244
Zach Brown1bde04a2010-07-14 14:01:21 -0700245 rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event,
246 rds_ib_event_str(event->event));
Andy Groverec162272009-02-24 15:30:30 +0000247
248 switch (event->event) {
249 case IB_EVENT_COMM_EST:
250 rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
251 break;
252 default:
Zach Brown1bde04a2010-07-14 14:01:21 -0700253 rdsdebug("Fatal QP Event %u (%s) "
Andy Groverfdf6e6b2009-07-17 13:13:31 +0000254 "- connection %pI4->%pI4, reconnecting\n",
Zach Brown1bde04a2010-07-14 14:01:21 -0700255 event->event, rds_ib_event_str(event->event),
256 &conn->c_laddr, &conn->c_faddr);
Andy Grover97069782010-03-11 13:50:02 +0000257 rds_conn_drop(conn);
Andy Groverec162272009-02-24 15:30:30 +0000258 break;
259 }
260}
261
262/*
263 * This needs to be very careful to not leave IS_ERR pointers around for
264 * cleanup to trip over.
265 */
266static int rds_ib_setup_qp(struct rds_connection *conn)
267{
268 struct rds_ib_connection *ic = conn->c_transport_data;
269 struct ib_device *dev = ic->i_cm_id->device;
270 struct ib_qp_init_attr attr;
271 struct rds_ib_device *rds_ibdev;
272 int ret;
273
Zach Brown3e0249f2010-05-18 15:48:51 -0700274 /*
275 * It's normal to see a null device if an incoming connection races
276 * with device removal, so we don't print a warning.
Andy Groverec162272009-02-24 15:30:30 +0000277 */
Zach Brown3e0249f2010-05-18 15:48:51 -0700278 rds_ibdev = rds_ib_get_client_data(dev);
279 if (!rds_ibdev)
Andy Groverec162272009-02-24 15:30:30 +0000280 return -EOPNOTSUPP;
Zach Brown3e0249f2010-05-18 15:48:51 -0700281
282 /* add the conn now so that connection establishment has the dev */
283 rds_ib_add_conn(rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +0000284
285 if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
286 rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
287 if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
288 rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
289
290 /* Protection domain and memory range */
291 ic->i_pd = rds_ibdev->pd;
292 ic->i_mr = rds_ibdev->mr;
293
294 ic->i_send_cq = ib_create_cq(dev, rds_ib_send_cq_comp_handler,
295 rds_ib_cq_event_handler, conn,
296 ic->i_send_ring.w_nr + 1, 0);
297 if (IS_ERR(ic->i_send_cq)) {
298 ret = PTR_ERR(ic->i_send_cq);
299 ic->i_send_cq = NULL;
300 rdsdebug("ib_create_cq send failed: %d\n", ret);
301 goto out;
302 }
303
304 ic->i_recv_cq = ib_create_cq(dev, rds_ib_recv_cq_comp_handler,
305 rds_ib_cq_event_handler, conn,
306 ic->i_recv_ring.w_nr, 0);
307 if (IS_ERR(ic->i_recv_cq)) {
308 ret = PTR_ERR(ic->i_recv_cq);
309 ic->i_recv_cq = NULL;
310 rdsdebug("ib_create_cq recv failed: %d\n", ret);
311 goto out;
312 }
313
314 ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
315 if (ret) {
316 rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
317 goto out;
318 }
319
320 ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
321 if (ret) {
322 rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
323 goto out;
324 }
325
326 /* XXX negotiate max send/recv with remote? */
327 memset(&attr, 0, sizeof(attr));
328 attr.event_handler = rds_ib_qp_event_handler;
329 attr.qp_context = conn;
330 /* + 1 to allow for the single ack message */
331 attr.cap.max_send_wr = ic->i_send_ring.w_nr + 1;
332 attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
333 attr.cap.max_send_sge = rds_ibdev->max_sge;
334 attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
335 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
336 attr.qp_type = IB_QPT_RC;
337 attr.send_cq = ic->i_send_cq;
338 attr.recv_cq = ic->i_recv_cq;
339
340 /*
341 * XXX this can fail if max_*_wr is too large? Are we supposed
342 * to back off until we get a value that the hardware can support?
343 */
344 ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
345 if (ret) {
346 rdsdebug("rdma_create_qp failed: %d\n", ret);
347 goto out;
348 }
349
350 ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
351 ic->i_send_ring.w_nr *
352 sizeof(struct rds_header),
353 &ic->i_send_hdrs_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800354 if (!ic->i_send_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000355 ret = -ENOMEM;
356 rdsdebug("ib_dma_alloc_coherent send failed\n");
357 goto out;
358 }
359
360 ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
361 ic->i_recv_ring.w_nr *
362 sizeof(struct rds_header),
363 &ic->i_recv_hdrs_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800364 if (!ic->i_recv_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000365 ret = -ENOMEM;
366 rdsdebug("ib_dma_alloc_coherent recv failed\n");
367 goto out;
368 }
369
370 ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
371 &ic->i_ack_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800372 if (!ic->i_ack) {
Andy Groverec162272009-02-24 15:30:30 +0000373 ret = -ENOMEM;
374 rdsdebug("ib_dma_alloc_coherent ack failed\n");
375 goto out;
376 }
377
Joe Perches3dbd4432011-05-28 10:36:35 -0700378 ic->i_sends = vzalloc_node(ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work),
Andy Grovere4c52c92010-04-23 10:49:53 -0700379 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800380 if (!ic->i_sends) {
Andy Groverec162272009-02-24 15:30:30 +0000381 ret = -ENOMEM;
382 rdsdebug("send allocation failed\n");
383 goto out;
384 }
Andy Groverec162272009-02-24 15:30:30 +0000385
Joe Perches3dbd4432011-05-28 10:36:35 -0700386 ic->i_recvs = vzalloc_node(ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work),
Andy Grovere4c52c92010-04-23 10:49:53 -0700387 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800388 if (!ic->i_recvs) {
Andy Groverec162272009-02-24 15:30:30 +0000389 ret = -ENOMEM;
390 rdsdebug("recv allocation failed\n");
391 goto out;
392 }
393
Andy Groverec162272009-02-24 15:30:30 +0000394 rds_ib_recv_init_ack(ic);
395
Andy Groverec162272009-02-24 15:30:30 +0000396 rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
397 ic->i_send_cq, ic->i_recv_cq);
398
399out:
Zach Brown3e0249f2010-05-18 15:48:51 -0700400 rds_ib_dev_put(rds_ibdev);
Andy Groverec162272009-02-24 15:30:30 +0000401 return ret;
402}
403
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000404static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event)
Andy Groverec162272009-02-24 15:30:30 +0000405{
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000406 const struct rds_ib_connect_private *dp = event->param.conn.private_data;
Andy Groverec162272009-02-24 15:30:30 +0000407 u16 common;
408 u32 version = 0;
409
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000410 /*
411 * rdma_cm private data is odd - when there is any private data in the
Andy Groverec162272009-02-24 15:30:30 +0000412 * request, we will be given a pretty large buffer without telling us the
413 * original size. The only way to tell the difference is by looking at
414 * the contents, which are initialized to zero.
415 * If the protocol version fields aren't set, this is a connection attempt
416 * from an older version. This could could be 3.0 or 2.0 - we can't tell.
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000417 * We really should have changed this for OFED 1.3 :-(
418 */
419
420 /* Be paranoid. RDS always has privdata */
421 if (!event->param.conn.private_data_len) {
422 printk(KERN_NOTICE "RDS incoming connection has no private data, "
423 "rejecting\n");
424 return 0;
425 }
426
427 /* Even if len is crap *now* I still want to check it. -ASG */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800428 if (event->param.conn.private_data_len < sizeof (*dp) ||
429 dp->dp_protocol_major == 0)
Andy Groverec162272009-02-24 15:30:30 +0000430 return RDS_PROTOCOL_3_0;
431
432 common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
433 if (dp->dp_protocol_major == 3 && common) {
434 version = RDS_PROTOCOL_3_0;
435 while ((common >>= 1) != 0)
436 version++;
Marciniszyn, Mikea4967592012-12-21 08:01:54 +0000437 } else
438 printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using incompatible protocol version %u.%u\n",
439 &dp->dp_saddr,
440 dp->dp_protocol_major,
441 dp->dp_protocol_minor);
Andy Groverec162272009-02-24 15:30:30 +0000442 return version;
443}
444
445int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
446 struct rdma_cm_event *event)
447{
448 __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
449 __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
450 const struct rds_ib_connect_private *dp = event->param.conn.private_data;
451 struct rds_ib_connect_private dp_rep;
452 struct rds_connection *conn = NULL;
453 struct rds_ib_connection *ic = NULL;
454 struct rdma_conn_param conn_param;
455 u32 version;
Zach Browna46ca942010-05-24 13:14:59 -0700456 int err = 1, destroy = 1;
Andy Groverec162272009-02-24 15:30:30 +0000457
458 /* Check whether the remote protocol version matches ours. */
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000459 version = rds_ib_protocol_compatible(event);
Andy Groverec162272009-02-24 15:30:30 +0000460 if (!version)
461 goto out;
462
463 rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
464 "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
465 RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
466 (unsigned long long)be64_to_cpu(lguid),
467 (unsigned long long)be64_to_cpu(fguid));
468
469 conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_ib_transport,
470 GFP_KERNEL);
471 if (IS_ERR(conn)) {
472 rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
473 conn = NULL;
474 goto out;
475 }
476
477 /*
478 * The connection request may occur while the
479 * previous connection exist, e.g. in case of failover.
480 * But as connections may be initiated simultaneously
481 * by both hosts, we have a random backoff mechanism -
482 * see the comment above rds_queue_reconnect()
483 */
484 mutex_lock(&conn->c_cm_lock);
485 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
486 if (rds_conn_state(conn) == RDS_CONN_UP) {
487 rdsdebug("incoming connect while connecting\n");
488 rds_conn_drop(conn);
489 rds_ib_stats_inc(s_ib_listen_closed_stale);
490 } else
491 if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
492 /* Wait and see - our connect may still be succeeding */
493 rds_ib_stats_inc(s_ib_connect_raced);
494 }
Andy Groverec162272009-02-24 15:30:30 +0000495 goto out;
496 }
497
498 ic = conn->c_transport_data;
499
500 rds_ib_set_protocol(conn, version);
501 rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
502
503 /* If the peer gave us the last packet it saw, process this as if
504 * we had received a regular ACK. */
505 if (dp->dp_ack_seq)
506 rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
507
508 BUG_ON(cm_id->context);
509 BUG_ON(ic->i_cm_id);
510
511 ic->i_cm_id = cm_id;
512 cm_id->context = conn;
513
514 /* We got halfway through setting up the ib_connection, if we
515 * fail now, we have to take the long route out of this mess. */
516 destroy = 0;
517
518 err = rds_ib_setup_qp(conn);
519 if (err) {
520 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
521 goto out;
522 }
523
Andy Grover40589e72010-01-12 10:50:48 -0800524 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version,
525 event->param.conn.responder_resources,
526 event->param.conn.initiator_depth);
Andy Groverec162272009-02-24 15:30:30 +0000527
528 /* rdma_accept() calls rdma_reject() internally if it fails */
529 err = rdma_accept(cm_id, &conn_param);
Zach Browna46ca942010-05-24 13:14:59 -0700530 if (err)
Andy Groverec162272009-02-24 15:30:30 +0000531 rds_ib_conn_error(conn, "rdma_accept failed (%d)\n", err);
Andy Groverec162272009-02-24 15:30:30 +0000532
533out:
Zach Browna46ca942010-05-24 13:14:59 -0700534 if (conn)
535 mutex_unlock(&conn->c_cm_lock);
536 if (err)
537 rdma_reject(cm_id, NULL, 0);
Andy Groverec162272009-02-24 15:30:30 +0000538 return destroy;
539}
540
541
542int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
543{
544 struct rds_connection *conn = cm_id->context;
545 struct rds_ib_connection *ic = conn->c_transport_data;
546 struct rdma_conn_param conn_param;
547 struct rds_ib_connect_private dp;
548 int ret;
549
550 /* If the peer doesn't do protocol negotiation, we must
551 * default to RDSv3.0 */
552 rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
553 ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
554
555 ret = rds_ib_setup_qp(conn);
556 if (ret) {
557 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
558 goto out;
559 }
560
Andy Grover40589e72010-01-12 10:50:48 -0800561 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION,
562 UINT_MAX, UINT_MAX);
Andy Groverec162272009-02-24 15:30:30 +0000563 ret = rdma_connect(cm_id, &conn_param);
564 if (ret)
565 rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
566
567out:
568 /* Beware - returning non-zero tells the rdma_cm to destroy
569 * the cm_id. We should certainly not do it as long as we still
570 * "own" the cm_id. */
571 if (ret) {
572 if (ic->i_cm_id == cm_id)
573 ret = 0;
574 }
575 return ret;
576}
577
578int rds_ib_conn_connect(struct rds_connection *conn)
579{
580 struct rds_ib_connection *ic = conn->c_transport_data;
581 struct sockaddr_in src, dest;
582 int ret;
583
584 /* XXX I wonder what affect the port space has */
585 /* delegate cm event handler to rdma_transport */
586 ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000587 RDMA_PS_TCP, IB_QPT_RC);
Andy Groverec162272009-02-24 15:30:30 +0000588 if (IS_ERR(ic->i_cm_id)) {
589 ret = PTR_ERR(ic->i_cm_id);
590 ic->i_cm_id = NULL;
591 rdsdebug("rdma_create_id() failed: %d\n", ret);
592 goto out;
593 }
594
595 rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
596
597 src.sin_family = AF_INET;
598 src.sin_addr.s_addr = (__force u32)conn->c_laddr;
599 src.sin_port = (__force u16)htons(0);
600
601 dest.sin_family = AF_INET;
602 dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
603 dest.sin_port = (__force u16)htons(RDS_PORT);
604
605 ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
606 (struct sockaddr *)&dest,
607 RDS_RDMA_RESOLVE_TIMEOUT_MS);
608 if (ret) {
609 rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
610 ret);
611 rdma_destroy_id(ic->i_cm_id);
612 ic->i_cm_id = NULL;
613 }
614
615out:
616 return ret;
617}
618
619/*
620 * This is so careful about only cleaning up resources that were built up
621 * so that it can be called at any point during startup. In fact it
622 * can be called multiple times for a given connection.
623 */
624void rds_ib_conn_shutdown(struct rds_connection *conn)
625{
626 struct rds_ib_connection *ic = conn->c_transport_data;
627 int err = 0;
628
629 rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
630 ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
631 ic->i_cm_id ? ic->i_cm_id->qp : NULL);
632
633 if (ic->i_cm_id) {
634 struct ib_device *dev = ic->i_cm_id->device;
635
636 rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
637 err = rdma_disconnect(ic->i_cm_id);
638 if (err) {
639 /* Actually this may happen quite frequently, when
640 * an outgoing connect raced with an incoming connect.
641 */
642 rdsdebug("failed to disconnect, cm: %p err %d\n",
643 ic->i_cm_id, err);
644 }
645
Andy Grovere32b4a72010-03-03 19:25:21 -0800646 /*
Zach Brownf0460112010-07-14 13:55:35 -0700647 * We want to wait for tx and rx completion to finish
648 * before we tear down the connection, but we have to be
649 * careful not to get stuck waiting on a send ring that
650 * only has unsignaled sends in it. We've shutdown new
651 * sends before getting here so by waiting for signaled
652 * sends to complete we're ensured that there will be no
653 * more tx processing.
Andy Grovere32b4a72010-03-03 19:25:21 -0800654 */
Andy Groverec162272009-02-24 15:30:30 +0000655 wait_event(rds_ib_ring_empty_wait,
Zach Brownf0460112010-07-14 13:55:35 -0700656 rds_ib_ring_empty(&ic->i_recv_ring) &&
657 (atomic_read(&ic->i_signaled_sends) == 0));
658 tasklet_kill(&ic->i_recv_tasklet);
Andy Groverec162272009-02-24 15:30:30 +0000659
660 if (ic->i_send_hdrs)
661 ib_dma_free_coherent(dev,
662 ic->i_send_ring.w_nr *
663 sizeof(struct rds_header),
664 ic->i_send_hdrs,
665 ic->i_send_hdrs_dma);
666
667 if (ic->i_recv_hdrs)
668 ib_dma_free_coherent(dev,
669 ic->i_recv_ring.w_nr *
670 sizeof(struct rds_header),
671 ic->i_recv_hdrs,
672 ic->i_recv_hdrs_dma);
673
674 if (ic->i_ack)
675 ib_dma_free_coherent(dev, sizeof(struct rds_header),
676 ic->i_ack, ic->i_ack_dma);
677
678 if (ic->i_sends)
679 rds_ib_send_clear_ring(ic);
680 if (ic->i_recvs)
681 rds_ib_recv_clear_ring(ic);
682
683 if (ic->i_cm_id->qp)
684 rdma_destroy_qp(ic->i_cm_id);
685 if (ic->i_send_cq)
686 ib_destroy_cq(ic->i_send_cq);
687 if (ic->i_recv_cq)
688 ib_destroy_cq(ic->i_recv_cq);
689 rdma_destroy_id(ic->i_cm_id);
690
691 /*
692 * Move connection back to the nodev list.
693 */
Andy Grover745cbcc2009-04-01 08:20:19 +0000694 if (ic->rds_ibdev)
695 rds_ib_remove_conn(ic->rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +0000696
697 ic->i_cm_id = NULL;
698 ic->i_pd = NULL;
699 ic->i_mr = NULL;
700 ic->i_send_cq = NULL;
701 ic->i_recv_cq = NULL;
702 ic->i_send_hdrs = NULL;
703 ic->i_recv_hdrs = NULL;
704 ic->i_ack = NULL;
705 }
706 BUG_ON(ic->rds_ibdev);
707
708 /* Clear pending transmit */
Andy Groverff3d7d32010-03-01 14:03:09 -0800709 if (ic->i_data_op) {
710 struct rds_message *rm;
711
712 rm = container_of(ic->i_data_op, struct rds_message, data);
713 rds_message_put(rm);
714 ic->i_data_op = NULL;
Andy Groverec162272009-02-24 15:30:30 +0000715 }
716
717 /* Clear the ACK state */
718 clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
Andy Grover8cbd9602009-04-01 08:20:20 +0000719#ifdef KERNEL_HAS_ATOMIC64
720 atomic64_set(&ic->i_ack_next, 0);
721#else
722 ic->i_ack_next = 0;
723#endif
Andy Groverec162272009-02-24 15:30:30 +0000724 ic->i_ack_recv = 0;
725
726 /* Clear flow control state */
727 ic->i_flowctl = 0;
728 atomic_set(&ic->i_credits, 0);
729
730 rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
731 rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
732
733 if (ic->i_ibinc) {
734 rds_inc_put(&ic->i_ibinc->ii_inc);
735 ic->i_ibinc = NULL;
736 }
737
738 vfree(ic->i_sends);
739 ic->i_sends = NULL;
740 vfree(ic->i_recvs);
741 ic->i_recvs = NULL;
742}
743
744int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
745{
746 struct rds_ib_connection *ic;
747 unsigned long flags;
Chris Mason33244122010-05-26 22:05:37 -0700748 int ret;
Andy Groverec162272009-02-24 15:30:30 +0000749
750 /* XXX too lazy? */
Dan Carpenterf0229ea2012-03-21 20:44:09 +0000751 ic = kzalloc(sizeof(struct rds_ib_connection), gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800752 if (!ic)
Andy Groverec162272009-02-24 15:30:30 +0000753 return -ENOMEM;
754
Chris Mason33244122010-05-26 22:05:37 -0700755 ret = rds_ib_recv_alloc_caches(ic);
756 if (ret) {
757 kfree(ic);
758 return ret;
759 }
760
Andy Groverec162272009-02-24 15:30:30 +0000761 INIT_LIST_HEAD(&ic->ib_node);
Andy Groverd521b632009-10-30 08:51:57 +0000762 tasklet_init(&ic->i_recv_tasklet, rds_ib_recv_tasklet_fn,
763 (unsigned long) ic);
Andy Groverec162272009-02-24 15:30:30 +0000764 mutex_init(&ic->i_recv_mutex);
Andy Grover8cbd9602009-04-01 08:20:20 +0000765#ifndef KERNEL_HAS_ATOMIC64
766 spin_lock_init(&ic->i_ack_lock);
767#endif
Zach Brownf0460112010-07-14 13:55:35 -0700768 atomic_set(&ic->i_signaled_sends, 0);
Andy Groverec162272009-02-24 15:30:30 +0000769
770 /*
771 * rds_ib_conn_shutdown() waits for these to be emptied so they
772 * must be initialized before it can be called.
773 */
774 rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
775 rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
776
777 ic->conn = conn;
778 conn->c_transport_data = ic;
779
780 spin_lock_irqsave(&ib_nodev_conns_lock, flags);
781 list_add_tail(&ic->ib_node, &ib_nodev_conns);
782 spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
783
784
785 rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
786 return 0;
787}
788
Andy Grover745cbcc2009-04-01 08:20:19 +0000789/*
790 * Free a connection. Connection must be shut down and not set for reconnect.
791 */
Andy Groverec162272009-02-24 15:30:30 +0000792void rds_ib_conn_free(void *arg)
793{
794 struct rds_ib_connection *ic = arg;
Andy Grover745cbcc2009-04-01 08:20:19 +0000795 spinlock_t *lock_ptr;
796
Andy Groverec162272009-02-24 15:30:30 +0000797 rdsdebug("ic %p\n", ic);
Andy Grover745cbcc2009-04-01 08:20:19 +0000798
799 /*
800 * Conn is either on a dev's list or on the nodev list.
801 * A race with shutdown() or connect() would cause problems
802 * (since rds_ibdev would change) but that should never happen.
803 */
804 lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
805
806 spin_lock_irq(lock_ptr);
Andy Groverec162272009-02-24 15:30:30 +0000807 list_del(&ic->ib_node);
Andy Grover745cbcc2009-04-01 08:20:19 +0000808 spin_unlock_irq(lock_ptr);
809
Chris Mason33244122010-05-26 22:05:37 -0700810 rds_ib_recv_free_caches(ic);
811
Andy Groverec162272009-02-24 15:30:30 +0000812 kfree(ic);
813}
814
815
816/*
817 * An error occurred on the connection
818 */
819void
820__rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
821{
822 va_list ap;
823
824 rds_conn_drop(conn);
825
826 va_start(ap, fmt);
827 vprintk(fmt, ap);
828 va_end(ap);
829}