Commit | Line | Data |
---|---|---|
fcd8b7c0 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> | |
5a0e3ad6 | 35 | #include <linux/slab.h> |
fcd8b7c0 AG |
36 | #include <linux/vmalloc.h> |
37 | ||
38 | #include "rds.h" | |
39 | #include "iw.h" | |
40 | ||
41 | /* | |
42 | * Set the selected protocol version | |
43 | */ | |
44 | static void rds_iw_set_protocol(struct rds_connection *conn, unsigned int version) | |
45 | { | |
46 | conn->c_version = version; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Set up flow control | |
51 | */ | |
52 | static void rds_iw_set_flow_control(struct rds_connection *conn, u32 credits) | |
53 | { | |
54 | struct rds_iw_connection *ic = conn->c_transport_data; | |
55 | ||
56 | if (rds_iw_sysctl_flow_control && credits != 0) { | |
57 | /* We're doing flow control */ | |
58 | ic->i_flowctl = 1; | |
59 | rds_iw_send_add_credits(conn, credits); | |
60 | } else { | |
61 | ic->i_flowctl = 0; | |
62 | } | |
63 | } | |
64 | ||
65 | /* | |
66 | * Connection established. | |
67 | * We get here for both outgoing and incoming connection. | |
68 | */ | |
69 | void rds_iw_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event) | |
70 | { | |
71 | const struct rds_iw_connect_private *dp = NULL; | |
72 | struct rds_iw_connection *ic = conn->c_transport_data; | |
73 | struct rds_iw_device *rds_iwdev; | |
74 | int err; | |
75 | ||
76 | if (event->param.conn.private_data_len) { | |
77 | dp = event->param.conn.private_data; | |
78 | ||
79 | rds_iw_set_protocol(conn, | |
80 | RDS_PROTOCOL(dp->dp_protocol_major, | |
81 | dp->dp_protocol_minor)); | |
82 | rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit)); | |
83 | } | |
84 | ||
85 | /* update ib_device with this local ipaddr & conn */ | |
86 | rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client); | |
87 | err = rds_iw_update_cm_id(rds_iwdev, ic->i_cm_id); | |
88 | if (err) | |
89 | printk(KERN_ERR "rds_iw_update_ipaddr failed (%d)\n", err); | |
745cbcca | 90 | rds_iw_add_conn(rds_iwdev, conn); |
fcd8b7c0 AG |
91 | |
92 | /* If the peer gave us the last packet it saw, process this as if | |
93 | * we had received a regular ACK. */ | |
94 | if (dp && dp->dp_ack_seq) | |
95 | rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL); | |
96 | ||
97 | printk(KERN_NOTICE "RDS/IW: connected to %pI4<->%pI4 version %u.%u%s\n", | |
98 | &conn->c_laddr, &conn->c_faddr, | |
99 | RDS_PROTOCOL_MAJOR(conn->c_version), | |
100 | RDS_PROTOCOL_MINOR(conn->c_version), | |
101 | ic->i_flowctl ? ", flow control" : ""); | |
102 | ||
103 | rds_connect_complete(conn); | |
104 | } | |
105 | ||
106 | static void rds_iw_cm_fill_conn_param(struct rds_connection *conn, | |
107 | struct rdma_conn_param *conn_param, | |
108 | struct rds_iw_connect_private *dp, | |
109 | u32 protocol_version) | |
110 | { | |
111 | struct rds_iw_connection *ic = conn->c_transport_data; | |
112 | ||
113 | memset(conn_param, 0, sizeof(struct rdma_conn_param)); | |
114 | /* XXX tune these? */ | |
115 | conn_param->responder_resources = 1; | |
116 | conn_param->initiator_depth = 1; | |
117 | ||
118 | if (dp) { | |
119 | memset(dp, 0, sizeof(*dp)); | |
120 | dp->dp_saddr = conn->c_laddr; | |
121 | dp->dp_daddr = conn->c_faddr; | |
122 | dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version); | |
123 | dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version); | |
124 | dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IW_SUPPORTED_PROTOCOLS); | |
125 | dp->dp_ack_seq = rds_iw_piggyb_ack(ic); | |
126 | ||
127 | /* Advertise flow control */ | |
128 | if (ic->i_flowctl) { | |
129 | unsigned int credits; | |
130 | ||
131 | credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits)); | |
132 | dp->dp_credit = cpu_to_be32(credits); | |
133 | atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits); | |
134 | } | |
135 | ||
136 | conn_param->private_data = dp; | |
137 | conn_param->private_data_len = sizeof(*dp); | |
138 | } | |
139 | } | |
140 | ||
141 | static void rds_iw_cq_event_handler(struct ib_event *event, void *data) | |
142 | { | |
143 | rdsdebug("event %u data %p\n", event->event, data); | |
144 | } | |
145 | ||
146 | static void rds_iw_qp_event_handler(struct ib_event *event, void *data) | |
147 | { | |
148 | struct rds_connection *conn = data; | |
149 | struct rds_iw_connection *ic = conn->c_transport_data; | |
150 | ||
151 | rdsdebug("conn %p ic %p event %u\n", conn, ic, event->event); | |
152 | ||
153 | switch (event->event) { | |
154 | case IB_EVENT_COMM_EST: | |
155 | rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST); | |
156 | break; | |
157 | case IB_EVENT_QP_REQ_ERR: | |
158 | case IB_EVENT_QP_FATAL: | |
159 | default: | |
160 | rds_iw_conn_error(conn, "RDS/IW: Fatal QP Event %u - connection %pI4->%pI4...reconnecting\n", | |
161 | event->event, &conn->c_laddr, | |
162 | &conn->c_faddr); | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | /* | |
168 | * Create a QP | |
169 | */ | |
170 | static int rds_iw_init_qp_attrs(struct ib_qp_init_attr *attr, | |
171 | struct rds_iw_device *rds_iwdev, | |
172 | struct rds_iw_work_ring *send_ring, | |
173 | void (*send_cq_handler)(struct ib_cq *, void *), | |
174 | struct rds_iw_work_ring *recv_ring, | |
175 | void (*recv_cq_handler)(struct ib_cq *, void *), | |
176 | void *context) | |
177 | { | |
178 | struct ib_device *dev = rds_iwdev->dev; | |
179 | unsigned int send_size, recv_size; | |
180 | int ret; | |
181 | ||
182 | /* The offset of 1 is to accomodate the additional ACK WR. */ | |
183 | send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1); | |
184 | recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1); | |
185 | rds_iw_ring_resize(send_ring, send_size - 1); | |
186 | rds_iw_ring_resize(recv_ring, recv_size - 1); | |
187 | ||
188 | memset(attr, 0, sizeof(*attr)); | |
189 | attr->event_handler = rds_iw_qp_event_handler; | |
190 | attr->qp_context = context; | |
191 | attr->cap.max_send_wr = send_size; | |
192 | attr->cap.max_recv_wr = recv_size; | |
193 | attr->cap.max_send_sge = rds_iwdev->max_sge; | |
194 | attr->cap.max_recv_sge = RDS_IW_RECV_SGE; | |
195 | attr->sq_sig_type = IB_SIGNAL_REQ_WR; | |
196 | attr->qp_type = IB_QPT_RC; | |
197 | ||
198 | attr->send_cq = ib_create_cq(dev, send_cq_handler, | |
199 | rds_iw_cq_event_handler, | |
200 | context, send_size, 0); | |
201 | if (IS_ERR(attr->send_cq)) { | |
202 | ret = PTR_ERR(attr->send_cq); | |
203 | attr->send_cq = NULL; | |
204 | rdsdebug("ib_create_cq send failed: %d\n", ret); | |
205 | goto out; | |
206 | } | |
207 | ||
208 | attr->recv_cq = ib_create_cq(dev, recv_cq_handler, | |
209 | rds_iw_cq_event_handler, | |
210 | context, recv_size, 0); | |
211 | if (IS_ERR(attr->recv_cq)) { | |
212 | ret = PTR_ERR(attr->recv_cq); | |
213 | attr->recv_cq = NULL; | |
214 | rdsdebug("ib_create_cq send failed: %d\n", ret); | |
215 | goto out; | |
216 | } | |
217 | ||
218 | ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP); | |
219 | if (ret) { | |
220 | rdsdebug("ib_req_notify_cq send failed: %d\n", ret); | |
221 | goto out; | |
222 | } | |
223 | ||
224 | ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED); | |
225 | if (ret) { | |
226 | rdsdebug("ib_req_notify_cq recv failed: %d\n", ret); | |
227 | goto out; | |
228 | } | |
229 | ||
230 | out: | |
231 | if (ret) { | |
232 | if (attr->send_cq) | |
233 | ib_destroy_cq(attr->send_cq); | |
234 | if (attr->recv_cq) | |
235 | ib_destroy_cq(attr->recv_cq); | |
236 | } | |
237 | return ret; | |
238 | } | |
239 | ||
240 | /* | |
241 | * This needs to be very careful to not leave IS_ERR pointers around for | |
242 | * cleanup to trip over. | |
243 | */ | |
244 | static int rds_iw_setup_qp(struct rds_connection *conn) | |
245 | { | |
246 | struct rds_iw_connection *ic = conn->c_transport_data; | |
247 | struct ib_device *dev = ic->i_cm_id->device; | |
248 | struct ib_qp_init_attr attr; | |
249 | struct rds_iw_device *rds_iwdev; | |
250 | int ret; | |
251 | ||
252 | /* rds_iw_add_one creates a rds_iw_device object per IB device, | |
253 | * and allocates a protection domain, memory range and MR pool | |
254 | * for each. If that fails for any reason, it will not register | |
255 | * the rds_iwdev at all. | |
256 | */ | |
257 | rds_iwdev = ib_get_client_data(dev, &rds_iw_client); | |
258 | if (rds_iwdev == NULL) { | |
259 | if (printk_ratelimit()) | |
260 | printk(KERN_NOTICE "RDS/IW: No client_data for device %s\n", | |
261 | dev->name); | |
262 | return -EOPNOTSUPP; | |
263 | } | |
264 | ||
265 | /* Protection domain and memory range */ | |
266 | ic->i_pd = rds_iwdev->pd; | |
267 | ic->i_mr = rds_iwdev->mr; | |
268 | ||
269 | ret = rds_iw_init_qp_attrs(&attr, rds_iwdev, | |
270 | &ic->i_send_ring, rds_iw_send_cq_comp_handler, | |
271 | &ic->i_recv_ring, rds_iw_recv_cq_comp_handler, | |
272 | conn); | |
273 | if (ret < 0) | |
274 | goto out; | |
275 | ||
276 | ic->i_send_cq = attr.send_cq; | |
277 | ic->i_recv_cq = attr.recv_cq; | |
278 | ||
279 | /* | |
280 | * XXX this can fail if max_*_wr is too large? Are we supposed | |
281 | * to back off until we get a value that the hardware can support? | |
282 | */ | |
283 | ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr); | |
284 | if (ret) { | |
285 | rdsdebug("rdma_create_qp failed: %d\n", ret); | |
286 | goto out; | |
287 | } | |
288 | ||
289 | ic->i_send_hdrs = ib_dma_alloc_coherent(dev, | |
290 | ic->i_send_ring.w_nr * | |
291 | sizeof(struct rds_header), | |
292 | &ic->i_send_hdrs_dma, GFP_KERNEL); | |
293 | if (ic->i_send_hdrs == NULL) { | |
294 | ret = -ENOMEM; | |
295 | rdsdebug("ib_dma_alloc_coherent send failed\n"); | |
296 | goto out; | |
297 | } | |
298 | ||
299 | ic->i_recv_hdrs = ib_dma_alloc_coherent(dev, | |
300 | ic->i_recv_ring.w_nr * | |
301 | sizeof(struct rds_header), | |
302 | &ic->i_recv_hdrs_dma, GFP_KERNEL); | |
303 | if (ic->i_recv_hdrs == NULL) { | |
304 | ret = -ENOMEM; | |
305 | rdsdebug("ib_dma_alloc_coherent recv failed\n"); | |
306 | goto out; | |
307 | } | |
308 | ||
309 | ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header), | |
310 | &ic->i_ack_dma, GFP_KERNEL); | |
311 | if (ic->i_ack == NULL) { | |
312 | ret = -ENOMEM; | |
313 | rdsdebug("ib_dma_alloc_coherent ack failed\n"); | |
314 | goto out; | |
315 | } | |
316 | ||
317 | ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_iw_send_work)); | |
318 | if (ic->i_sends == NULL) { | |
319 | ret = -ENOMEM; | |
320 | rdsdebug("send allocation failed\n"); | |
321 | goto out; | |
322 | } | |
323 | rds_iw_send_init_ring(ic); | |
324 | ||
325 | ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_iw_recv_work)); | |
326 | if (ic->i_recvs == NULL) { | |
327 | ret = -ENOMEM; | |
328 | rdsdebug("recv allocation failed\n"); | |
329 | goto out; | |
330 | } | |
331 | ||
332 | rds_iw_recv_init_ring(ic); | |
333 | rds_iw_recv_init_ack(ic); | |
334 | ||
335 | /* Post receive buffers - as a side effect, this will update | |
336 | * the posted credit count. */ | |
337 | rds_iw_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1); | |
338 | ||
339 | rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr, | |
340 | ic->i_send_cq, ic->i_recv_cq); | |
341 | ||
342 | out: | |
343 | return ret; | |
344 | } | |
345 | ||
346 | static u32 rds_iw_protocol_compatible(const struct rds_iw_connect_private *dp) | |
347 | { | |
348 | u16 common; | |
349 | u32 version = 0; | |
350 | ||
351 | /* rdma_cm private data is odd - when there is any private data in the | |
352 | * request, we will be given a pretty large buffer without telling us the | |
353 | * original size. The only way to tell the difference is by looking at | |
354 | * the contents, which are initialized to zero. | |
355 | * If the protocol version fields aren't set, this is a connection attempt | |
356 | * from an older version. This could could be 3.0 or 2.0 - we can't tell. | |
357 | * We really should have changed this for OFED 1.3 :-( */ | |
358 | if (dp->dp_protocol_major == 0) | |
359 | return RDS_PROTOCOL_3_0; | |
360 | ||
361 | common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IW_SUPPORTED_PROTOCOLS; | |
362 | if (dp->dp_protocol_major == 3 && common) { | |
363 | version = RDS_PROTOCOL_3_0; | |
364 | while ((common >>= 1) != 0) | |
365 | version++; | |
366 | } else if (printk_ratelimit()) { | |
367 | printk(KERN_NOTICE "RDS: Connection from %pI4 using " | |
368 | "incompatible protocol version %u.%u\n", | |
369 | &dp->dp_saddr, | |
370 | dp->dp_protocol_major, | |
371 | dp->dp_protocol_minor); | |
372 | } | |
373 | return version; | |
374 | } | |
375 | ||
376 | int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id, | |
377 | struct rdma_cm_event *event) | |
378 | { | |
379 | const struct rds_iw_connect_private *dp = event->param.conn.private_data; | |
380 | struct rds_iw_connect_private dp_rep; | |
381 | struct rds_connection *conn = NULL; | |
382 | struct rds_iw_connection *ic = NULL; | |
383 | struct rdma_conn_param conn_param; | |
384 | struct rds_iw_device *rds_iwdev; | |
385 | u32 version; | |
386 | int err, destroy = 1; | |
387 | ||
388 | /* Check whether the remote protocol version matches ours. */ | |
389 | version = rds_iw_protocol_compatible(dp); | |
390 | if (!version) | |
391 | goto out; | |
392 | ||
393 | rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u\n", | |
394 | &dp->dp_saddr, &dp->dp_daddr, | |
395 | RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version)); | |
396 | ||
397 | conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_iw_transport, | |
398 | GFP_KERNEL); | |
399 | if (IS_ERR(conn)) { | |
400 | rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn)); | |
401 | conn = NULL; | |
402 | goto out; | |
403 | } | |
404 | ||
405 | /* | |
406 | * The connection request may occur while the | |
407 | * previous connection exist, e.g. in case of failover. | |
408 | * But as connections may be initiated simultaneously | |
409 | * by both hosts, we have a random backoff mechanism - | |
410 | * see the comment above rds_queue_reconnect() | |
411 | */ | |
412 | mutex_lock(&conn->c_cm_lock); | |
413 | if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { | |
414 | if (rds_conn_state(conn) == RDS_CONN_UP) { | |
415 | rdsdebug("incoming connect while connecting\n"); | |
416 | rds_conn_drop(conn); | |
417 | rds_iw_stats_inc(s_iw_listen_closed_stale); | |
418 | } else | |
419 | if (rds_conn_state(conn) == RDS_CONN_CONNECTING) { | |
420 | /* Wait and see - our connect may still be succeeding */ | |
421 | rds_iw_stats_inc(s_iw_connect_raced); | |
422 | } | |
423 | mutex_unlock(&conn->c_cm_lock); | |
424 | goto out; | |
425 | } | |
426 | ||
427 | ic = conn->c_transport_data; | |
428 | ||
429 | rds_iw_set_protocol(conn, version); | |
430 | rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit)); | |
431 | ||
432 | /* If the peer gave us the last packet it saw, process this as if | |
433 | * we had received a regular ACK. */ | |
434 | if (dp->dp_ack_seq) | |
435 | rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL); | |
436 | ||
437 | BUG_ON(cm_id->context); | |
438 | BUG_ON(ic->i_cm_id); | |
439 | ||
440 | ic->i_cm_id = cm_id; | |
441 | cm_id->context = conn; | |
442 | ||
443 | rds_iwdev = ib_get_client_data(cm_id->device, &rds_iw_client); | |
444 | ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey; | |
445 | ||
446 | /* We got halfway through setting up the ib_connection, if we | |
447 | * fail now, we have to take the long route out of this mess. */ | |
448 | destroy = 0; | |
449 | ||
450 | err = rds_iw_setup_qp(conn); | |
451 | if (err) { | |
452 | rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", err); | |
453 | goto out; | |
454 | } | |
455 | ||
456 | rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version); | |
457 | ||
458 | /* rdma_accept() calls rdma_reject() internally if it fails */ | |
459 | err = rdma_accept(cm_id, &conn_param); | |
460 | mutex_unlock(&conn->c_cm_lock); | |
461 | if (err) { | |
462 | rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err); | |
463 | goto out; | |
464 | } | |
465 | ||
466 | return 0; | |
467 | ||
468 | out: | |
469 | rdma_reject(cm_id, NULL, 0); | |
470 | return destroy; | |
471 | } | |
472 | ||
473 | ||
474 | int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id) | |
475 | { | |
476 | struct rds_connection *conn = cm_id->context; | |
477 | struct rds_iw_connection *ic = conn->c_transport_data; | |
478 | struct rdma_conn_param conn_param; | |
479 | struct rds_iw_connect_private dp; | |
480 | int ret; | |
481 | ||
482 | /* If the peer doesn't do protocol negotiation, we must | |
483 | * default to RDSv3.0 */ | |
484 | rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0); | |
485 | ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */ | |
486 | ||
487 | ret = rds_iw_setup_qp(conn); | |
488 | if (ret) { | |
489 | rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret); | |
490 | goto out; | |
491 | } | |
492 | ||
493 | rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION); | |
494 | ||
495 | ret = rdma_connect(cm_id, &conn_param); | |
496 | if (ret) | |
497 | rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret); | |
498 | ||
499 | out: | |
500 | /* Beware - returning non-zero tells the rdma_cm to destroy | |
501 | * the cm_id. We should certainly not do it as long as we still | |
502 | * "own" the cm_id. */ | |
503 | if (ret) { | |
504 | struct rds_iw_connection *ic = conn->c_transport_data; | |
505 | ||
506 | if (ic->i_cm_id == cm_id) | |
507 | ret = 0; | |
508 | } | |
509 | return ret; | |
510 | } | |
511 | ||
512 | int rds_iw_conn_connect(struct rds_connection *conn) | |
513 | { | |
514 | struct rds_iw_connection *ic = conn->c_transport_data; | |
515 | struct rds_iw_device *rds_iwdev; | |
516 | struct sockaddr_in src, dest; | |
517 | int ret; | |
518 | ||
519 | /* XXX I wonder what affect the port space has */ | |
520 | /* delegate cm event handler to rdma_transport */ | |
521 | ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn, | |
522 | RDMA_PS_TCP); | |
523 | if (IS_ERR(ic->i_cm_id)) { | |
524 | ret = PTR_ERR(ic->i_cm_id); | |
525 | ic->i_cm_id = NULL; | |
526 | rdsdebug("rdma_create_id() failed: %d\n", ret); | |
527 | goto out; | |
528 | } | |
529 | ||
530 | rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn); | |
531 | ||
532 | src.sin_family = AF_INET; | |
533 | src.sin_addr.s_addr = (__force u32)conn->c_laddr; | |
534 | src.sin_port = (__force u16)htons(0); | |
535 | ||
536 | /* First, bind to the local address and device. */ | |
537 | ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src); | |
538 | if (ret) { | |
539 | rdsdebug("rdma_bind_addr(%pI4) failed: %d\n", | |
540 | &conn->c_laddr, ret); | |
541 | rdma_destroy_id(ic->i_cm_id); | |
542 | ic->i_cm_id = NULL; | |
543 | goto out; | |
544 | } | |
545 | ||
546 | rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client); | |
547 | ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey; | |
548 | ||
549 | dest.sin_family = AF_INET; | |
550 | dest.sin_addr.s_addr = (__force u32)conn->c_faddr; | |
551 | dest.sin_port = (__force u16)htons(RDS_PORT); | |
552 | ||
553 | ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src, | |
554 | (struct sockaddr *)&dest, | |
555 | RDS_RDMA_RESOLVE_TIMEOUT_MS); | |
556 | if (ret) { | |
557 | rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id, | |
558 | ret); | |
559 | rdma_destroy_id(ic->i_cm_id); | |
560 | ic->i_cm_id = NULL; | |
561 | } | |
562 | ||
563 | out: | |
564 | return ret; | |
565 | } | |
566 | ||
567 | /* | |
568 | * This is so careful about only cleaning up resources that were built up | |
569 | * so that it can be called at any point during startup. In fact it | |
570 | * can be called multiple times for a given connection. | |
571 | */ | |
572 | void rds_iw_conn_shutdown(struct rds_connection *conn) | |
573 | { | |
574 | struct rds_iw_connection *ic = conn->c_transport_data; | |
575 | int err = 0; | |
576 | struct ib_qp_attr qp_attr; | |
577 | ||
578 | rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id, | |
579 | ic->i_pd, ic->i_send_cq, ic->i_recv_cq, | |
580 | ic->i_cm_id ? ic->i_cm_id->qp : NULL); | |
581 | ||
582 | if (ic->i_cm_id) { | |
583 | struct ib_device *dev = ic->i_cm_id->device; | |
584 | ||
585 | rdsdebug("disconnecting cm %p\n", ic->i_cm_id); | |
586 | err = rdma_disconnect(ic->i_cm_id); | |
587 | if (err) { | |
588 | /* Actually this may happen quite frequently, when | |
589 | * an outgoing connect raced with an incoming connect. | |
590 | */ | |
591 | rdsdebug("rds_iw_conn_shutdown: failed to disconnect," | |
592 | " cm: %p err %d\n", ic->i_cm_id, err); | |
593 | } | |
594 | ||
595 | if (ic->i_cm_id->qp) { | |
596 | qp_attr.qp_state = IB_QPS_ERR; | |
597 | ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE); | |
598 | } | |
599 | ||
600 | wait_event(rds_iw_ring_empty_wait, | |
601 | rds_iw_ring_empty(&ic->i_send_ring) && | |
602 | rds_iw_ring_empty(&ic->i_recv_ring)); | |
603 | ||
604 | if (ic->i_send_hdrs) | |
605 | ib_dma_free_coherent(dev, | |
606 | ic->i_send_ring.w_nr * | |
607 | sizeof(struct rds_header), | |
608 | ic->i_send_hdrs, | |
609 | ic->i_send_hdrs_dma); | |
610 | ||
611 | if (ic->i_recv_hdrs) | |
612 | ib_dma_free_coherent(dev, | |
613 | ic->i_recv_ring.w_nr * | |
614 | sizeof(struct rds_header), | |
615 | ic->i_recv_hdrs, | |
616 | ic->i_recv_hdrs_dma); | |
617 | ||
618 | if (ic->i_ack) | |
619 | ib_dma_free_coherent(dev, sizeof(struct rds_header), | |
620 | ic->i_ack, ic->i_ack_dma); | |
621 | ||
622 | if (ic->i_sends) | |
623 | rds_iw_send_clear_ring(ic); | |
624 | if (ic->i_recvs) | |
625 | rds_iw_recv_clear_ring(ic); | |
626 | ||
627 | if (ic->i_cm_id->qp) | |
628 | rdma_destroy_qp(ic->i_cm_id); | |
629 | if (ic->i_send_cq) | |
630 | ib_destroy_cq(ic->i_send_cq); | |
631 | if (ic->i_recv_cq) | |
632 | ib_destroy_cq(ic->i_recv_cq); | |
633 | ||
634 | /* | |
635 | * If associated with an rds_iw_device: | |
636 | * Move connection back to the nodev list. | |
637 | * Remove cm_id from the device cm_id list. | |
638 | */ | |
745cbcca AG |
639 | if (ic->rds_iwdev) |
640 | rds_iw_remove_conn(ic->rds_iwdev, conn); | |
fcd8b7c0 AG |
641 | |
642 | rdma_destroy_id(ic->i_cm_id); | |
643 | ||
644 | ic->i_cm_id = NULL; | |
645 | ic->i_pd = NULL; | |
646 | ic->i_mr = NULL; | |
647 | ic->i_send_cq = NULL; | |
648 | ic->i_recv_cq = NULL; | |
649 | ic->i_send_hdrs = NULL; | |
650 | ic->i_recv_hdrs = NULL; | |
651 | ic->i_ack = NULL; | |
652 | } | |
653 | BUG_ON(ic->rds_iwdev); | |
654 | ||
655 | /* Clear pending transmit */ | |
656 | if (ic->i_rm) { | |
657 | rds_message_put(ic->i_rm); | |
658 | ic->i_rm = NULL; | |
659 | } | |
660 | ||
661 | /* Clear the ACK state */ | |
662 | clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags); | |
8cbd9606 AG |
663 | #ifdef KERNEL_HAS_ATOMIC64 |
664 | atomic64_set(&ic->i_ack_next, 0); | |
665 | #else | |
666 | ic->i_ack_next = 0; | |
667 | #endif | |
fcd8b7c0 AG |
668 | ic->i_ack_recv = 0; |
669 | ||
670 | /* Clear flow control state */ | |
671 | ic->i_flowctl = 0; | |
672 | atomic_set(&ic->i_credits, 0); | |
673 | ||
674 | rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr); | |
675 | rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr); | |
676 | ||
677 | if (ic->i_iwinc) { | |
678 | rds_inc_put(&ic->i_iwinc->ii_inc); | |
679 | ic->i_iwinc = NULL; | |
680 | } | |
681 | ||
682 | vfree(ic->i_sends); | |
683 | ic->i_sends = NULL; | |
684 | vfree(ic->i_recvs); | |
685 | ic->i_recvs = NULL; | |
686 | rdsdebug("shutdown complete\n"); | |
687 | } | |
688 | ||
689 | int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp) | |
690 | { | |
691 | struct rds_iw_connection *ic; | |
692 | unsigned long flags; | |
693 | ||
694 | /* XXX too lazy? */ | |
695 | ic = kzalloc(sizeof(struct rds_iw_connection), GFP_KERNEL); | |
696 | if (ic == NULL) | |
697 | return -ENOMEM; | |
698 | ||
699 | INIT_LIST_HEAD(&ic->iw_node); | |
d521b63b AG |
700 | tasklet_init(&ic->i_recv_tasklet, rds_iw_recv_tasklet_fn, |
701 | (unsigned long) ic); | |
fcd8b7c0 | 702 | mutex_init(&ic->i_recv_mutex); |
8cbd9606 AG |
703 | #ifndef KERNEL_HAS_ATOMIC64 |
704 | spin_lock_init(&ic->i_ack_lock); | |
705 | #endif | |
fcd8b7c0 AG |
706 | |
707 | /* | |
708 | * rds_iw_conn_shutdown() waits for these to be emptied so they | |
709 | * must be initialized before it can be called. | |
710 | */ | |
711 | rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr); | |
712 | rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr); | |
713 | ||
714 | ic->conn = conn; | |
715 | conn->c_transport_data = ic; | |
716 | ||
717 | spin_lock_irqsave(&iw_nodev_conns_lock, flags); | |
718 | list_add_tail(&ic->iw_node, &iw_nodev_conns); | |
719 | spin_unlock_irqrestore(&iw_nodev_conns_lock, flags); | |
720 | ||
721 | ||
722 | rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data); | |
723 | return 0; | |
724 | } | |
725 | ||
745cbcca AG |
726 | /* |
727 | * Free a connection. Connection must be shut down and not set for reconnect. | |
728 | */ | |
fcd8b7c0 AG |
729 | void rds_iw_conn_free(void *arg) |
730 | { | |
731 | struct rds_iw_connection *ic = arg; | |
745cbcca AG |
732 | spinlock_t *lock_ptr; |
733 | ||
fcd8b7c0 | 734 | rdsdebug("ic %p\n", ic); |
745cbcca AG |
735 | |
736 | /* | |
737 | * Conn is either on a dev's list or on the nodev list. | |
738 | * A race with shutdown() or connect() would cause problems | |
739 | * (since rds_iwdev would change) but that should never happen. | |
740 | */ | |
741 | lock_ptr = ic->rds_iwdev ? &ic->rds_iwdev->spinlock : &iw_nodev_conns_lock; | |
742 | ||
743 | spin_lock_irq(lock_ptr); | |
fcd8b7c0 | 744 | list_del(&ic->iw_node); |
745cbcca AG |
745 | spin_unlock_irq(lock_ptr); |
746 | ||
fcd8b7c0 AG |
747 | kfree(ic); |
748 | } | |
749 | ||
750 | /* | |
751 | * An error occurred on the connection | |
752 | */ | |
753 | void | |
754 | __rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...) | |
755 | { | |
756 | va_list ap; | |
757 | ||
758 | rds_conn_drop(conn); | |
759 | ||
760 | va_start(ap, fmt); | |
761 | vprintk(fmt, ap); | |
762 | va_end(ap); | |
763 | } |