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