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