blob: 0e2fea893a769ac9bb42cab3e293733d0cd70763 [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>
37
38#include "rds.h"
39#include "ib.h"
40
Zach Brown1bde04a2010-07-14 14:01:21 -070041static char *rds_ib_event_type_strings[] = {
42#define RDS_IB_EVENT_STRING(foo) [IB_EVENT_##foo] = __stringify(foo)
43 RDS_IB_EVENT_STRING(CQ_ERR),
44 RDS_IB_EVENT_STRING(QP_FATAL),
45 RDS_IB_EVENT_STRING(QP_REQ_ERR),
46 RDS_IB_EVENT_STRING(QP_ACCESS_ERR),
47 RDS_IB_EVENT_STRING(COMM_EST),
48 RDS_IB_EVENT_STRING(SQ_DRAINED),
49 RDS_IB_EVENT_STRING(PATH_MIG),
50 RDS_IB_EVENT_STRING(PATH_MIG_ERR),
51 RDS_IB_EVENT_STRING(DEVICE_FATAL),
52 RDS_IB_EVENT_STRING(PORT_ACTIVE),
53 RDS_IB_EVENT_STRING(PORT_ERR),
54 RDS_IB_EVENT_STRING(LID_CHANGE),
55 RDS_IB_EVENT_STRING(PKEY_CHANGE),
56 RDS_IB_EVENT_STRING(SM_CHANGE),
57 RDS_IB_EVENT_STRING(SRQ_ERR),
58 RDS_IB_EVENT_STRING(SRQ_LIMIT_REACHED),
59 RDS_IB_EVENT_STRING(QP_LAST_WQE_REACHED),
60 RDS_IB_EVENT_STRING(CLIENT_REREGISTER),
61#undef RDS_IB_EVENT_STRING
62};
63
64static char *rds_ib_event_str(enum ib_event_type type)
65{
66 if (type < ARRAY_SIZE(rds_ib_event_type_strings) &&
67 rds_ib_event_type_strings[type])
68 return rds_ib_event_type_strings[type];
69 else
70 return "unknown";
71};
72
Andy Groverec162272009-02-24 15:30:30 +000073/*
74 * Set the selected protocol version
75 */
76static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version)
77{
78 conn->c_version = version;
79}
80
81/*
82 * Set up flow control
83 */
84static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits)
85{
86 struct rds_ib_connection *ic = conn->c_transport_data;
87
88 if (rds_ib_sysctl_flow_control && credits != 0) {
89 /* We're doing flow control */
90 ic->i_flowctl = 1;
91 rds_ib_send_add_credits(conn, credits);
92 } else {
93 ic->i_flowctl = 0;
94 }
95}
96
97/*
98 * Tune RNR behavior. Without flow control, we use a rather
99 * low timeout, but not the absolute minimum - this should
100 * be tunable.
101 *
102 * We already set the RNR retry count to 7 (which is the
103 * smallest infinite number :-) above.
104 * If flow control is off, we want to change this back to 0
105 * so that we learn quickly when our credit accounting is
106 * buggy.
107 *
108 * Caller passes in a qp_attr pointer - don't waste stack spacv
109 * by allocation this twice.
110 */
111static void
112rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr)
113{
114 int ret;
115
116 attr->min_rnr_timer = IB_RNR_TIMER_000_32;
117 ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER);
118 if (ret)
119 printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret);
120}
121
122/*
123 * Connection established.
124 * We get here for both outgoing and incoming connection.
125 */
126void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
127{
128 const struct rds_ib_connect_private *dp = NULL;
129 struct rds_ib_connection *ic = conn->c_transport_data;
Andy Groverec162272009-02-24 15:30:30 +0000130 struct ib_qp_attr qp_attr;
131 int err;
132
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000133 if (event->param.conn.private_data_len >= sizeof(*dp)) {
Andy Groverec162272009-02-24 15:30:30 +0000134 dp = event->param.conn.private_data;
135
Andy Grover02a6a252009-07-17 13:13:24 +0000136 /* make sure it isn't empty data */
137 if (dp->dp_protocol_major) {
138 rds_ib_set_protocol(conn,
Andy Groverec162272009-02-24 15:30:30 +0000139 RDS_PROTOCOL(dp->dp_protocol_major,
Andy Grover02a6a252009-07-17 13:13:24 +0000140 dp->dp_protocol_minor));
141 rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
142 }
Andy Groverec162272009-02-24 15:30:30 +0000143 }
144
Andy Groverf147dd92010-01-13 15:50:09 -0800145 if (conn->c_version < RDS_PROTOCOL(3,1)) {
146 printk(KERN_NOTICE "RDS/IB: Connection to %pI4 version %u.%u failed,"
147 " no longer supported\n",
148 &conn->c_faddr,
149 RDS_PROTOCOL_MAJOR(conn->c_version),
150 RDS_PROTOCOL_MINOR(conn->c_version));
151 rds_conn_destroy(conn);
152 return;
153 } else {
154 printk(KERN_NOTICE "RDS/IB: connected to %pI4 version %u.%u%s\n",
155 &conn->c_faddr,
156 RDS_PROTOCOL_MAJOR(conn->c_version),
157 RDS_PROTOCOL_MINOR(conn->c_version),
158 ic->i_flowctl ? ", flow control" : "");
159 }
Andy Groverec162272009-02-24 15:30:30 +0000160
Andy Grovere11d9122009-07-17 13:13:29 +0000161 /*
162 * Init rings and fill recv. this needs to wait until protocol negotiation
163 * is complete, since ring layout is different from 3.0 to 3.1.
164 */
165 rds_ib_send_init_ring(ic);
166 rds_ib_recv_init_ring(ic);
167 /* Post receive buffers - as a side effect, this will update
168 * the posted credit count. */
Andy Groverf17a1a52010-03-18 17:19:52 -0700169 rds_ib_recv_refill(conn, 1);
Andy Grovere11d9122009-07-17 13:13:29 +0000170
Andy Groverec162272009-02-24 15:30:30 +0000171 /* Tune RNR behavior */
172 rds_ib_tune_rnr(ic, &qp_attr);
173
174 qp_attr.qp_state = IB_QPS_RTS;
175 err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
176 if (err)
177 printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
178
Zach Brown3e0249f2010-05-18 15:48:51 -0700179 /* update ib_device with this local ipaddr */
180 err = rds_ib_update_ipaddr(ic->rds_ibdev, conn->c_laddr);
Andy Groverec162272009-02-24 15:30:30 +0000181 if (err)
Zach Brown3e0249f2010-05-18 15:48:51 -0700182 printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n",
183 err);
Andy Groverec162272009-02-24 15:30:30 +0000184
185 /* If the peer gave us the last packet it saw, process this as if
186 * we had received a regular ACK. */
187 if (dp && dp->dp_ack_seq)
188 rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
189
190 rds_connect_complete(conn);
191}
192
193static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
194 struct rdma_conn_param *conn_param,
195 struct rds_ib_connect_private *dp,
Andy Grover40589e72010-01-12 10:50:48 -0800196 u32 protocol_version,
197 u32 max_responder_resources,
198 u32 max_initiator_depth)
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));
214 dp->dp_saddr = conn->c_laddr;
215 dp->dp_daddr = conn->c_faddr;
216 dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
217 dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
218 dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
219 dp->dp_ack_seq = rds_ib_piggyb_ack(ic);
220
221 /* Advertise flow control */
222 if (ic->i_flowctl) {
223 unsigned int credits;
224
225 credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
226 dp->dp_credit = cpu_to_be32(credits);
227 atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
228 }
229
230 conn_param->private_data = dp;
231 conn_param->private_data_len = sizeof(*dp);
232 }
233}
234
235static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
236{
Zach Brown1bde04a2010-07-14 14:01:21 -0700237 rdsdebug("event %u (%s) data %p\n",
238 event->event, rds_ib_event_str(event->event), data);
Andy Groverec162272009-02-24 15:30:30 +0000239}
240
241static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
242{
243 struct rds_connection *conn = data;
244 struct rds_ib_connection *ic = conn->c_transport_data;
245
Zach Brown1bde04a2010-07-14 14:01:21 -0700246 rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event,
247 rds_ib_event_str(event->event));
Andy Groverec162272009-02-24 15:30:30 +0000248
249 switch (event->event) {
250 case IB_EVENT_COMM_EST:
251 rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
252 break;
253 default:
Zach Brown1bde04a2010-07-14 14:01:21 -0700254 rdsdebug("Fatal QP Event %u (%s) "
Andy Groverfdf6e6b2009-07-17 13:13:31 +0000255 "- connection %pI4->%pI4, reconnecting\n",
Zach Brown1bde04a2010-07-14 14:01:21 -0700256 event->event, rds_ib_event_str(event->event),
257 &conn->c_laddr, &conn->c_faddr);
Andy Grover97069782010-03-11 13:50:02 +0000258 rds_conn_drop(conn);
Andy Groverec162272009-02-24 15:30:30 +0000259 break;
260 }
261}
262
263/*
264 * This needs to be very careful to not leave IS_ERR pointers around for
265 * cleanup to trip over.
266 */
267static int rds_ib_setup_qp(struct rds_connection *conn)
268{
269 struct rds_ib_connection *ic = conn->c_transport_data;
270 struct ib_device *dev = ic->i_cm_id->device;
271 struct ib_qp_init_attr attr;
272 struct rds_ib_device *rds_ibdev;
273 int ret;
274
Zach Brown3e0249f2010-05-18 15:48:51 -0700275 /*
276 * It's normal to see a null device if an incoming connection races
277 * with device removal, so we don't print a warning.
Andy Groverec162272009-02-24 15:30:30 +0000278 */
Zach Brown3e0249f2010-05-18 15:48:51 -0700279 rds_ibdev = rds_ib_get_client_data(dev);
280 if (!rds_ibdev)
Andy Groverec162272009-02-24 15:30:30 +0000281 return -EOPNOTSUPP;
Zach Brown3e0249f2010-05-18 15:48:51 -0700282
283 /* add the conn now so that connection establishment has the dev */
284 rds_ib_add_conn(rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +0000285
286 if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
287 rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
288 if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
289 rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
290
291 /* Protection domain and memory range */
292 ic->i_pd = rds_ibdev->pd;
293 ic->i_mr = rds_ibdev->mr;
294
295 ic->i_send_cq = ib_create_cq(dev, rds_ib_send_cq_comp_handler,
296 rds_ib_cq_event_handler, conn,
297 ic->i_send_ring.w_nr + 1, 0);
298 if (IS_ERR(ic->i_send_cq)) {
299 ret = PTR_ERR(ic->i_send_cq);
300 ic->i_send_cq = NULL;
301 rdsdebug("ib_create_cq send failed: %d\n", ret);
302 goto out;
303 }
304
305 ic->i_recv_cq = ib_create_cq(dev, rds_ib_recv_cq_comp_handler,
306 rds_ib_cq_event_handler, conn,
307 ic->i_recv_ring.w_nr, 0);
308 if (IS_ERR(ic->i_recv_cq)) {
309 ret = PTR_ERR(ic->i_recv_cq);
310 ic->i_recv_cq = NULL;
311 rdsdebug("ib_create_cq recv failed: %d\n", ret);
312 goto out;
313 }
314
315 ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
316 if (ret) {
317 rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
318 goto out;
319 }
320
321 ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
322 if (ret) {
323 rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
324 goto out;
325 }
326
327 /* XXX negotiate max send/recv with remote? */
328 memset(&attr, 0, sizeof(attr));
329 attr.event_handler = rds_ib_qp_event_handler;
330 attr.qp_context = conn;
331 /* + 1 to allow for the single ack message */
332 attr.cap.max_send_wr = ic->i_send_ring.w_nr + 1;
333 attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
334 attr.cap.max_send_sge = rds_ibdev->max_sge;
335 attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
336 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
337 attr.qp_type = IB_QPT_RC;
338 attr.send_cq = ic->i_send_cq;
339 attr.recv_cq = ic->i_recv_cq;
340
341 /*
342 * XXX this can fail if max_*_wr is too large? Are we supposed
343 * to back off until we get a value that the hardware can support?
344 */
345 ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
346 if (ret) {
347 rdsdebug("rdma_create_qp failed: %d\n", ret);
348 goto out;
349 }
350
351 ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
352 ic->i_send_ring.w_nr *
353 sizeof(struct rds_header),
354 &ic->i_send_hdrs_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800355 if (!ic->i_send_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000356 ret = -ENOMEM;
357 rdsdebug("ib_dma_alloc_coherent send failed\n");
358 goto out;
359 }
360
361 ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
362 ic->i_recv_ring.w_nr *
363 sizeof(struct rds_header),
364 &ic->i_recv_hdrs_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800365 if (!ic->i_recv_hdrs) {
Andy Groverec162272009-02-24 15:30:30 +0000366 ret = -ENOMEM;
367 rdsdebug("ib_dma_alloc_coherent recv failed\n");
368 goto out;
369 }
370
371 ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
372 &ic->i_ack_dma, GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800373 if (!ic->i_ack) {
Andy Groverec162272009-02-24 15:30:30 +0000374 ret = -ENOMEM;
375 rdsdebug("ib_dma_alloc_coherent ack failed\n");
376 goto out;
377 }
378
Andy Grovere4c52c92010-04-23 10:49:53 -0700379 ic->i_sends = vmalloc_node(ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work),
380 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800381 if (!ic->i_sends) {
Andy Groverec162272009-02-24 15:30:30 +0000382 ret = -ENOMEM;
383 rdsdebug("send allocation failed\n");
384 goto out;
385 }
Andy Grovere11d9122009-07-17 13:13:29 +0000386 memset(ic->i_sends, 0, ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work));
Andy Groverec162272009-02-24 15:30:30 +0000387
Andy Grovere4c52c92010-04-23 10:49:53 -0700388 ic->i_recvs = vmalloc_node(ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work),
389 ibdev_to_node(dev));
Andy Grover8690bfa2010-01-12 11:56:44 -0800390 if (!ic->i_recvs) {
Andy Groverec162272009-02-24 15:30:30 +0000391 ret = -ENOMEM;
392 rdsdebug("recv allocation failed\n");
393 goto out;
394 }
Andy Grovere11d9122009-07-17 13:13:29 +0000395 memset(ic->i_recvs, 0, ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work));
Andy Groverec162272009-02-24 15:30:30 +0000396
Andy Groverec162272009-02-24 15:30:30 +0000397 rds_ib_recv_init_ack(ic);
398
Andy Groverec162272009-02-24 15:30:30 +0000399 rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
400 ic->i_send_cq, ic->i_recv_cq);
401
402out:
Zach Brown3e0249f2010-05-18 15:48:51 -0700403 rds_ib_dev_put(rds_ibdev);
Andy Groverec162272009-02-24 15:30:30 +0000404 return ret;
405}
406
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000407static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event)
Andy Groverec162272009-02-24 15:30:30 +0000408{
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000409 const struct rds_ib_connect_private *dp = event->param.conn.private_data;
Andy Groverec162272009-02-24 15:30:30 +0000410 u16 common;
411 u32 version = 0;
412
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000413 /*
414 * rdma_cm private data is odd - when there is any private data in the
Andy Groverec162272009-02-24 15:30:30 +0000415 * request, we will be given a pretty large buffer without telling us the
416 * original size. The only way to tell the difference is by looking at
417 * the contents, which are initialized to zero.
418 * If the protocol version fields aren't set, this is a connection attempt
419 * from an older version. This could could be 3.0 or 2.0 - we can't tell.
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000420 * We really should have changed this for OFED 1.3 :-(
421 */
422
423 /* Be paranoid. RDS always has privdata */
424 if (!event->param.conn.private_data_len) {
425 printk(KERN_NOTICE "RDS incoming connection has no private data, "
426 "rejecting\n");
427 return 0;
428 }
429
430 /* Even if len is crap *now* I still want to check it. -ASG */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800431 if (event->param.conn.private_data_len < sizeof (*dp) ||
432 dp->dp_protocol_major == 0)
Andy Groverec162272009-02-24 15:30:30 +0000433 return RDS_PROTOCOL_3_0;
434
435 common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
436 if (dp->dp_protocol_major == 3 && common) {
437 version = RDS_PROTOCOL_3_0;
438 while ((common >>= 1) != 0)
439 version++;
440 } else if (printk_ratelimit()) {
441 printk(KERN_NOTICE "RDS: Connection from %pI4 using "
442 "incompatible protocol version %u.%u\n",
443 &dp->dp_saddr,
444 dp->dp_protocol_major,
445 dp->dp_protocol_minor);
446 }
447 return version;
448}
449
450int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
451 struct rdma_cm_event *event)
452{
453 __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
454 __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
455 const struct rds_ib_connect_private *dp = event->param.conn.private_data;
456 struct rds_ib_connect_private dp_rep;
457 struct rds_connection *conn = NULL;
458 struct rds_ib_connection *ic = NULL;
459 struct rdma_conn_param conn_param;
460 u32 version;
Zach Browna46ca942010-05-24 13:14:59 -0700461 int err = 1, destroy = 1;
Andy Groverec162272009-02-24 15:30:30 +0000462
463 /* Check whether the remote protocol version matches ours. */
Andy Grover9ddbcfa2009-07-17 13:13:23 +0000464 version = rds_ib_protocol_compatible(event);
Andy Groverec162272009-02-24 15:30:30 +0000465 if (!version)
466 goto out;
467
468 rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
469 "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
470 RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
471 (unsigned long long)be64_to_cpu(lguid),
472 (unsigned long long)be64_to_cpu(fguid));
473
474 conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_ib_transport,
475 GFP_KERNEL);
476 if (IS_ERR(conn)) {
477 rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
478 conn = NULL;
479 goto out;
480 }
481
482 /*
483 * The connection request may occur while the
484 * previous connection exist, e.g. in case of failover.
485 * But as connections may be initiated simultaneously
486 * by both hosts, we have a random backoff mechanism -
487 * see the comment above rds_queue_reconnect()
488 */
489 mutex_lock(&conn->c_cm_lock);
490 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
491 if (rds_conn_state(conn) == RDS_CONN_UP) {
492 rdsdebug("incoming connect while connecting\n");
493 rds_conn_drop(conn);
494 rds_ib_stats_inc(s_ib_listen_closed_stale);
495 } else
496 if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
497 /* Wait and see - our connect may still be succeeding */
498 rds_ib_stats_inc(s_ib_connect_raced);
499 }
Andy Groverec162272009-02-24 15:30:30 +0000500 goto out;
501 }
502
503 ic = conn->c_transport_data;
504
505 rds_ib_set_protocol(conn, version);
506 rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
507
508 /* If the peer gave us the last packet it saw, process this as if
509 * we had received a regular ACK. */
510 if (dp->dp_ack_seq)
511 rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
512
513 BUG_ON(cm_id->context);
514 BUG_ON(ic->i_cm_id);
515
516 ic->i_cm_id = cm_id;
517 cm_id->context = conn;
518
519 /* We got halfway through setting up the ib_connection, if we
520 * fail now, we have to take the long route out of this mess. */
521 destroy = 0;
522
523 err = rds_ib_setup_qp(conn);
524 if (err) {
525 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
Julia Lawall5daf47b2010-05-26 05:54:21 +0000526 mutex_unlock(&conn->c_cm_lock);
Andy Groverec162272009-02-24 15:30:30 +0000527 goto out;
528 }
529
Andy Grover40589e72010-01-12 10:50:48 -0800530 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version,
531 event->param.conn.responder_resources,
532 event->param.conn.initiator_depth);
Andy Groverec162272009-02-24 15:30:30 +0000533
534 /* rdma_accept() calls rdma_reject() internally if it fails */
535 err = rdma_accept(cm_id, &conn_param);
Zach Browna46ca942010-05-24 13:14:59 -0700536 if (err)
Andy Groverec162272009-02-24 15:30:30 +0000537 rds_ib_conn_error(conn, "rdma_accept failed (%d)\n", err);
Andy Groverec162272009-02-24 15:30:30 +0000538
539out:
Zach Browna46ca942010-05-24 13:14:59 -0700540 if (conn)
541 mutex_unlock(&conn->c_cm_lock);
542 if (err)
543 rdma_reject(cm_id, NULL, 0);
Andy Groverec162272009-02-24 15:30:30 +0000544 return destroy;
545}
546
547
548int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
549{
550 struct rds_connection *conn = cm_id->context;
551 struct rds_ib_connection *ic = conn->c_transport_data;
552 struct rdma_conn_param conn_param;
553 struct rds_ib_connect_private dp;
554 int ret;
555
556 /* If the peer doesn't do protocol negotiation, we must
557 * default to RDSv3.0 */
558 rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
559 ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
560
561 ret = rds_ib_setup_qp(conn);
562 if (ret) {
563 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
564 goto out;
565 }
566
Andy Grover40589e72010-01-12 10:50:48 -0800567 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION,
568 UINT_MAX, UINT_MAX);
Andy Groverec162272009-02-24 15:30:30 +0000569 ret = rdma_connect(cm_id, &conn_param);
570 if (ret)
571 rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
572
573out:
574 /* Beware - returning non-zero tells the rdma_cm to destroy
575 * the cm_id. We should certainly not do it as long as we still
576 * "own" the cm_id. */
577 if (ret) {
578 if (ic->i_cm_id == cm_id)
579 ret = 0;
580 }
581 return ret;
582}
583
584int rds_ib_conn_connect(struct rds_connection *conn)
585{
586 struct rds_ib_connection *ic = conn->c_transport_data;
587 struct sockaddr_in src, dest;
588 int ret;
589
590 /* XXX I wonder what affect the port space has */
591 /* delegate cm event handler to rdma_transport */
592 ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
593 RDMA_PS_TCP);
594 if (IS_ERR(ic->i_cm_id)) {
595 ret = PTR_ERR(ic->i_cm_id);
596 ic->i_cm_id = NULL;
597 rdsdebug("rdma_create_id() failed: %d\n", ret);
598 goto out;
599 }
600
601 rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
602
603 src.sin_family = AF_INET;
604 src.sin_addr.s_addr = (__force u32)conn->c_laddr;
605 src.sin_port = (__force u16)htons(0);
606
607 dest.sin_family = AF_INET;
608 dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
609 dest.sin_port = (__force u16)htons(RDS_PORT);
610
611 ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
612 (struct sockaddr *)&dest,
613 RDS_RDMA_RESOLVE_TIMEOUT_MS);
614 if (ret) {
615 rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
616 ret);
617 rdma_destroy_id(ic->i_cm_id);
618 ic->i_cm_id = NULL;
619 }
620
621out:
622 return ret;
623}
624
625/*
626 * This is so careful about only cleaning up resources that were built up
627 * so that it can be called at any point during startup. In fact it
628 * can be called multiple times for a given connection.
629 */
630void rds_ib_conn_shutdown(struct rds_connection *conn)
631{
632 struct rds_ib_connection *ic = conn->c_transport_data;
633 int err = 0;
634
635 rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
636 ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
637 ic->i_cm_id ? ic->i_cm_id->qp : NULL);
638
639 if (ic->i_cm_id) {
640 struct ib_device *dev = ic->i_cm_id->device;
641
642 rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
643 err = rdma_disconnect(ic->i_cm_id);
644 if (err) {
645 /* Actually this may happen quite frequently, when
646 * an outgoing connect raced with an incoming connect.
647 */
648 rdsdebug("failed to disconnect, cm: %p err %d\n",
649 ic->i_cm_id, err);
650 }
651
Andy Grovere32b4a72010-03-03 19:25:21 -0800652 /*
Zach Brownf0460112010-07-14 13:55:35 -0700653 * We want to wait for tx and rx completion to finish
654 * before we tear down the connection, but we have to be
655 * careful not to get stuck waiting on a send ring that
656 * only has unsignaled sends in it. We've shutdown new
657 * sends before getting here so by waiting for signaled
658 * sends to complete we're ensured that there will be no
659 * more tx processing.
Andy Grovere32b4a72010-03-03 19:25:21 -0800660 */
Andy Groverec162272009-02-24 15:30:30 +0000661 wait_event(rds_ib_ring_empty_wait,
Zach Brownf0460112010-07-14 13:55:35 -0700662 rds_ib_ring_empty(&ic->i_recv_ring) &&
663 (atomic_read(&ic->i_signaled_sends) == 0));
664 tasklet_kill(&ic->i_recv_tasklet);
Andy Groverec162272009-02-24 15:30:30 +0000665
666 if (ic->i_send_hdrs)
667 ib_dma_free_coherent(dev,
668 ic->i_send_ring.w_nr *
669 sizeof(struct rds_header),
670 ic->i_send_hdrs,
671 ic->i_send_hdrs_dma);
672
673 if (ic->i_recv_hdrs)
674 ib_dma_free_coherent(dev,
675 ic->i_recv_ring.w_nr *
676 sizeof(struct rds_header),
677 ic->i_recv_hdrs,
678 ic->i_recv_hdrs_dma);
679
680 if (ic->i_ack)
681 ib_dma_free_coherent(dev, sizeof(struct rds_header),
682 ic->i_ack, ic->i_ack_dma);
683
684 if (ic->i_sends)
685 rds_ib_send_clear_ring(ic);
686 if (ic->i_recvs)
687 rds_ib_recv_clear_ring(ic);
688
689 if (ic->i_cm_id->qp)
690 rdma_destroy_qp(ic->i_cm_id);
691 if (ic->i_send_cq)
692 ib_destroy_cq(ic->i_send_cq);
693 if (ic->i_recv_cq)
694 ib_destroy_cq(ic->i_recv_cq);
695 rdma_destroy_id(ic->i_cm_id);
696
697 /*
698 * Move connection back to the nodev list.
699 */
Andy Grover745cbcc2009-04-01 08:20:19 +0000700 if (ic->rds_ibdev)
701 rds_ib_remove_conn(ic->rds_ibdev, conn);
Andy Groverec162272009-02-24 15:30:30 +0000702
703 ic->i_cm_id = NULL;
704 ic->i_pd = NULL;
705 ic->i_mr = NULL;
706 ic->i_send_cq = NULL;
707 ic->i_recv_cq = NULL;
708 ic->i_send_hdrs = NULL;
709 ic->i_recv_hdrs = NULL;
710 ic->i_ack = NULL;
711 }
712 BUG_ON(ic->rds_ibdev);
713
714 /* Clear pending transmit */
Andy Groverff3d7d32010-03-01 14:03:09 -0800715 if (ic->i_data_op) {
716 struct rds_message *rm;
717
718 rm = container_of(ic->i_data_op, struct rds_message, data);
719 rds_message_put(rm);
720 ic->i_data_op = NULL;
Andy Groverec162272009-02-24 15:30:30 +0000721 }
722
723 /* Clear the ACK state */
724 clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
Andy Grover8cbd9602009-04-01 08:20:20 +0000725#ifdef KERNEL_HAS_ATOMIC64
726 atomic64_set(&ic->i_ack_next, 0);
727#else
728 ic->i_ack_next = 0;
729#endif
Andy Groverec162272009-02-24 15:30:30 +0000730 ic->i_ack_recv = 0;
731
732 /* Clear flow control state */
733 ic->i_flowctl = 0;
734 atomic_set(&ic->i_credits, 0);
735
736 rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
737 rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
738
739 if (ic->i_ibinc) {
740 rds_inc_put(&ic->i_ibinc->ii_inc);
741 ic->i_ibinc = NULL;
742 }
743
744 vfree(ic->i_sends);
745 ic->i_sends = NULL;
746 vfree(ic->i_recvs);
747 ic->i_recvs = NULL;
748}
749
750int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
751{
752 struct rds_ib_connection *ic;
753 unsigned long flags;
Chris Mason33244122010-05-26 22:05:37 -0700754 int ret;
Andy Groverec162272009-02-24 15:30:30 +0000755
756 /* XXX too lazy? */
757 ic = kzalloc(sizeof(struct rds_ib_connection), GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800758 if (!ic)
Andy Groverec162272009-02-24 15:30:30 +0000759 return -ENOMEM;
760
Chris Mason33244122010-05-26 22:05:37 -0700761 ret = rds_ib_recv_alloc_caches(ic);
762 if (ret) {
763 kfree(ic);
764 return ret;
765 }
766
Andy Groverec162272009-02-24 15:30:30 +0000767 INIT_LIST_HEAD(&ic->ib_node);
Andy Groverd521b632009-10-30 08:51:57 +0000768 tasklet_init(&ic->i_recv_tasklet, rds_ib_recv_tasklet_fn,
769 (unsigned long) ic);
Andy Groverec162272009-02-24 15:30:30 +0000770 mutex_init(&ic->i_recv_mutex);
Andy Grover8cbd9602009-04-01 08:20:20 +0000771#ifndef KERNEL_HAS_ATOMIC64
772 spin_lock_init(&ic->i_ack_lock);
773#endif
Zach Brownf0460112010-07-14 13:55:35 -0700774 atomic_set(&ic->i_signaled_sends, 0);
Andy Groverec162272009-02-24 15:30:30 +0000775
776 /*
777 * rds_ib_conn_shutdown() waits for these to be emptied so they
778 * must be initialized before it can be called.
779 */
780 rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
781 rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
782
783 ic->conn = conn;
784 conn->c_transport_data = ic;
785
786 spin_lock_irqsave(&ib_nodev_conns_lock, flags);
787 list_add_tail(&ic->ib_node, &ib_nodev_conns);
788 spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
789
790
791 rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
792 return 0;
793}
794
Andy Grover745cbcc2009-04-01 08:20:19 +0000795/*
796 * Free a connection. Connection must be shut down and not set for reconnect.
797 */
Andy Groverec162272009-02-24 15:30:30 +0000798void rds_ib_conn_free(void *arg)
799{
800 struct rds_ib_connection *ic = arg;
Andy Grover745cbcc2009-04-01 08:20:19 +0000801 spinlock_t *lock_ptr;
802
Andy Groverec162272009-02-24 15:30:30 +0000803 rdsdebug("ic %p\n", ic);
Andy Grover745cbcc2009-04-01 08:20:19 +0000804
805 /*
806 * Conn is either on a dev's list or on the nodev list.
807 * A race with shutdown() or connect() would cause problems
808 * (since rds_ibdev would change) but that should never happen.
809 */
810 lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
811
812 spin_lock_irq(lock_ptr);
Andy Groverec162272009-02-24 15:30:30 +0000813 list_del(&ic->ib_node);
Andy Grover745cbcc2009-04-01 08:20:19 +0000814 spin_unlock_irq(lock_ptr);
815
Chris Mason33244122010-05-26 22:05:37 -0700816 rds_ib_recv_free_caches(ic);
817
Andy Groverec162272009-02-24 15:30:30 +0000818 kfree(ic);
819}
820
821
822/*
823 * An error occurred on the connection
824 */
825void
826__rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
827{
828 va_list ap;
829
830 rds_conn_drop(conn);
831
832 va_start(ap, fmt);
833 vprintk(fmt, ap);
834 va_end(ap);
835}