blob: 712cf2d1f28ef36e3bf567540b036ca41179faaf [file] [log] [blame]
Andy Groverfcd8b7c2009-02-24 15:30:36 +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 Groverfcd8b7c2009-02-24 15:30:36 +000036#include <linux/vmalloc.h>
37
38#include "rds.h"
39#include "iw.h"
40
41/*
42 * Set the selected protocol version
43 */
44static 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 */
52static 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 */
69void 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);
Andy Grover745cbcc2009-04-01 08:20:19 +000090 rds_iw_add_conn(rds_iwdev, conn);
Andy Groverfcd8b7c2009-02-24 15:30:36 +000091
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
106static 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
141static 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
146static 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:
Andy Grover97069782010-03-11 13:50:02 +0000160 rdsdebug("Fatal QP Event %u "
161 "- connection %pI4->%pI4, reconnecting\n",
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000162 event->event, &conn->c_laddr,
163 &conn->c_faddr);
Andy Grover97069782010-03-11 13:50:02 +0000164 rds_conn_drop(conn);
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000165 break;
166 }
167}
168
169/*
170 * Create a QP
171 */
172static int rds_iw_init_qp_attrs(struct ib_qp_init_attr *attr,
173 struct rds_iw_device *rds_iwdev,
174 struct rds_iw_work_ring *send_ring,
175 void (*send_cq_handler)(struct ib_cq *, void *),
176 struct rds_iw_work_ring *recv_ring,
177 void (*recv_cq_handler)(struct ib_cq *, void *),
178 void *context)
179{
180 struct ib_device *dev = rds_iwdev->dev;
181 unsigned int send_size, recv_size;
182 int ret;
183
184 /* The offset of 1 is to accomodate the additional ACK WR. */
185 send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1);
186 recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1);
187 rds_iw_ring_resize(send_ring, send_size - 1);
188 rds_iw_ring_resize(recv_ring, recv_size - 1);
189
190 memset(attr, 0, sizeof(*attr));
191 attr->event_handler = rds_iw_qp_event_handler;
192 attr->qp_context = context;
193 attr->cap.max_send_wr = send_size;
194 attr->cap.max_recv_wr = recv_size;
195 attr->cap.max_send_sge = rds_iwdev->max_sge;
196 attr->cap.max_recv_sge = RDS_IW_RECV_SGE;
197 attr->sq_sig_type = IB_SIGNAL_REQ_WR;
198 attr->qp_type = IB_QPT_RC;
199
200 attr->send_cq = ib_create_cq(dev, send_cq_handler,
201 rds_iw_cq_event_handler,
202 context, send_size, 0);
203 if (IS_ERR(attr->send_cq)) {
204 ret = PTR_ERR(attr->send_cq);
205 attr->send_cq = NULL;
206 rdsdebug("ib_create_cq send failed: %d\n", ret);
207 goto out;
208 }
209
210 attr->recv_cq = ib_create_cq(dev, recv_cq_handler,
211 rds_iw_cq_event_handler,
212 context, recv_size, 0);
213 if (IS_ERR(attr->recv_cq)) {
214 ret = PTR_ERR(attr->recv_cq);
215 attr->recv_cq = NULL;
216 rdsdebug("ib_create_cq send failed: %d\n", ret);
217 goto out;
218 }
219
220 ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP);
221 if (ret) {
222 rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
223 goto out;
224 }
225
226 ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED);
227 if (ret) {
228 rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
229 goto out;
230 }
231
232out:
233 if (ret) {
234 if (attr->send_cq)
235 ib_destroy_cq(attr->send_cq);
236 if (attr->recv_cq)
237 ib_destroy_cq(attr->recv_cq);
238 }
239 return ret;
240}
241
242/*
243 * This needs to be very careful to not leave IS_ERR pointers around for
244 * cleanup to trip over.
245 */
246static int rds_iw_setup_qp(struct rds_connection *conn)
247{
248 struct rds_iw_connection *ic = conn->c_transport_data;
249 struct ib_device *dev = ic->i_cm_id->device;
250 struct ib_qp_init_attr attr;
251 struct rds_iw_device *rds_iwdev;
252 int ret;
253
254 /* rds_iw_add_one creates a rds_iw_device object per IB device,
255 * and allocates a protection domain, memory range and MR pool
256 * for each. If that fails for any reason, it will not register
257 * the rds_iwdev at all.
258 */
259 rds_iwdev = ib_get_client_data(dev, &rds_iw_client);
Andy Grover8690bfa2010-01-12 11:56:44 -0800260 if (!rds_iwdev) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000261 if (printk_ratelimit())
262 printk(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);
Andy Grover8690bfa2010-01-12 11:56:44 -0800295 if (!ic->i_send_hdrs) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000296 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);
Andy Grover8690bfa2010-01-12 11:56:44 -0800305 if (!ic->i_recv_hdrs) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000306 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);
Andy Grover8690bfa2010-01-12 11:56:44 -0800313 if (!ic->i_ack) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000314 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));
Andy Grover8690bfa2010-01-12 11:56:44 -0800320 if (!ic->i_sends) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000321 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));
Andy Grover8690bfa2010-01-12 11:56:44 -0800328 if (!ic->i_recvs) {
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000329 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
344out:
345 return ret;
346}
347
348static 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 } else if (printk_ratelimit()) {
369 printk(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 }
375 return version;
376}
377
378int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id,
379 struct rdma_cm_event *event)
380{
381 const struct rds_iw_connect_private *dp = event->param.conn.private_data;
382 struct rds_iw_connect_private dp_rep;
383 struct rds_connection *conn = NULL;
384 struct rds_iw_connection *ic = NULL;
385 struct rdma_conn_param conn_param;
386 struct rds_iw_device *rds_iwdev;
387 u32 version;
388 int err, destroy = 1;
389
390 /* Check whether the remote protocol version matches ours. */
391 version = rds_iw_protocol_compatible(dp);
392 if (!version)
393 goto out;
394
395 rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u\n",
396 &dp->dp_saddr, &dp->dp_daddr,
397 RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version));
398
399 conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_iw_transport,
400 GFP_KERNEL);
401 if (IS_ERR(conn)) {
402 rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
403 conn = NULL;
404 goto out;
405 }
406
407 /*
408 * The connection request may occur while the
409 * previous connection exist, e.g. in case of failover.
410 * But as connections may be initiated simultaneously
411 * by both hosts, we have a random backoff mechanism -
412 * see the comment above rds_queue_reconnect()
413 */
414 mutex_lock(&conn->c_cm_lock);
415 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
416 if (rds_conn_state(conn) == RDS_CONN_UP) {
417 rdsdebug("incoming connect while connecting\n");
418 rds_conn_drop(conn);
419 rds_iw_stats_inc(s_iw_listen_closed_stale);
420 } else
421 if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
422 /* Wait and see - our connect may still be succeeding */
423 rds_iw_stats_inc(s_iw_connect_raced);
424 }
425 mutex_unlock(&conn->c_cm_lock);
426 goto out;
427 }
428
429 ic = conn->c_transport_data;
430
431 rds_iw_set_protocol(conn, version);
432 rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
433
434 /* If the peer gave us the last packet it saw, process this as if
435 * we had received a regular ACK. */
436 if (dp->dp_ack_seq)
437 rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
438
439 BUG_ON(cm_id->context);
440 BUG_ON(ic->i_cm_id);
441
442 ic->i_cm_id = cm_id;
443 cm_id->context = conn;
444
445 rds_iwdev = ib_get_client_data(cm_id->device, &rds_iw_client);
446 ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
447
448 /* We got halfway through setting up the ib_connection, if we
449 * fail now, we have to take the long route out of this mess. */
450 destroy = 0;
451
452 err = rds_iw_setup_qp(conn);
453 if (err) {
454 rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", err);
Julia Lawall5daf47b2010-05-26 05:54:21 +0000455 mutex_unlock(&conn->c_cm_lock);
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000456 goto out;
457 }
458
459 rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
460
461 /* rdma_accept() calls rdma_reject() internally if it fails */
462 err = rdma_accept(cm_id, &conn_param);
463 mutex_unlock(&conn->c_cm_lock);
464 if (err) {
465 rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err);
466 goto out;
467 }
468
469 return 0;
470
471out:
472 rdma_reject(cm_id, NULL, 0);
473 return destroy;
474}
475
476
477int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id)
478{
479 struct rds_connection *conn = cm_id->context;
480 struct rds_iw_connection *ic = conn->c_transport_data;
481 struct rdma_conn_param conn_param;
482 struct rds_iw_connect_private dp;
483 int ret;
484
485 /* If the peer doesn't do protocol negotiation, we must
486 * default to RDSv3.0 */
487 rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0);
488 ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */
489
490 ret = rds_iw_setup_qp(conn);
491 if (ret) {
492 rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret);
493 goto out;
494 }
495
496 rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
497
498 ret = rdma_connect(cm_id, &conn_param);
499 if (ret)
500 rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret);
501
502out:
503 /* Beware - returning non-zero tells the rdma_cm to destroy
504 * the cm_id. We should certainly not do it as long as we still
505 * "own" the cm_id. */
506 if (ret) {
507 struct rds_iw_connection *ic = conn->c_transport_data;
508
509 if (ic->i_cm_id == cm_id)
510 ret = 0;
511 }
512 return ret;
513}
514
515int rds_iw_conn_connect(struct rds_connection *conn)
516{
517 struct rds_iw_connection *ic = conn->c_transport_data;
518 struct rds_iw_device *rds_iwdev;
519 struct sockaddr_in src, dest;
520 int ret;
521
522 /* XXX I wonder what affect the port space has */
523 /* delegate cm event handler to rdma_transport */
524 ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
525 RDMA_PS_TCP);
526 if (IS_ERR(ic->i_cm_id)) {
527 ret = PTR_ERR(ic->i_cm_id);
528 ic->i_cm_id = NULL;
529 rdsdebug("rdma_create_id() failed: %d\n", ret);
530 goto out;
531 }
532
533 rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
534
535 src.sin_family = AF_INET;
536 src.sin_addr.s_addr = (__force u32)conn->c_laddr;
537 src.sin_port = (__force u16)htons(0);
538
539 /* First, bind to the local address and device. */
540 ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src);
541 if (ret) {
542 rdsdebug("rdma_bind_addr(%pI4) failed: %d\n",
543 &conn->c_laddr, ret);
544 rdma_destroy_id(ic->i_cm_id);
545 ic->i_cm_id = NULL;
546 goto out;
547 }
548
549 rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
550 ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
551
552 dest.sin_family = AF_INET;
553 dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
554 dest.sin_port = (__force u16)htons(RDS_PORT);
555
556 ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
557 (struct sockaddr *)&dest,
558 RDS_RDMA_RESOLVE_TIMEOUT_MS);
559 if (ret) {
560 rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
561 ret);
562 rdma_destroy_id(ic->i_cm_id);
563 ic->i_cm_id = NULL;
564 }
565
566out:
567 return ret;
568}
569
570/*
571 * This is so careful about only cleaning up resources that were built up
572 * so that it can be called at any point during startup. In fact it
573 * can be called multiple times for a given connection.
574 */
575void rds_iw_conn_shutdown(struct rds_connection *conn)
576{
577 struct rds_iw_connection *ic = conn->c_transport_data;
578 int err = 0;
579 struct ib_qp_attr qp_attr;
580
581 rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
582 ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
583 ic->i_cm_id ? ic->i_cm_id->qp : NULL);
584
585 if (ic->i_cm_id) {
586 struct ib_device *dev = ic->i_cm_id->device;
587
588 rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
589 err = rdma_disconnect(ic->i_cm_id);
590 if (err) {
591 /* Actually this may happen quite frequently, when
592 * an outgoing connect raced with an incoming connect.
593 */
594 rdsdebug("rds_iw_conn_shutdown: failed to disconnect,"
595 " cm: %p err %d\n", ic->i_cm_id, err);
596 }
597
598 if (ic->i_cm_id->qp) {
599 qp_attr.qp_state = IB_QPS_ERR;
600 ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
601 }
602
603 wait_event(rds_iw_ring_empty_wait,
604 rds_iw_ring_empty(&ic->i_send_ring) &&
605 rds_iw_ring_empty(&ic->i_recv_ring));
606
607 if (ic->i_send_hdrs)
608 ib_dma_free_coherent(dev,
609 ic->i_send_ring.w_nr *
610 sizeof(struct rds_header),
611 ic->i_send_hdrs,
612 ic->i_send_hdrs_dma);
613
614 if (ic->i_recv_hdrs)
615 ib_dma_free_coherent(dev,
616 ic->i_recv_ring.w_nr *
617 sizeof(struct rds_header),
618 ic->i_recv_hdrs,
619 ic->i_recv_hdrs_dma);
620
621 if (ic->i_ack)
622 ib_dma_free_coherent(dev, sizeof(struct rds_header),
623 ic->i_ack, ic->i_ack_dma);
624
625 if (ic->i_sends)
626 rds_iw_send_clear_ring(ic);
627 if (ic->i_recvs)
628 rds_iw_recv_clear_ring(ic);
629
630 if (ic->i_cm_id->qp)
631 rdma_destroy_qp(ic->i_cm_id);
632 if (ic->i_send_cq)
633 ib_destroy_cq(ic->i_send_cq);
634 if (ic->i_recv_cq)
635 ib_destroy_cq(ic->i_recv_cq);
636
637 /*
638 * If associated with an rds_iw_device:
639 * Move connection back to the nodev list.
640 * Remove cm_id from the device cm_id list.
641 */
Andy Grover745cbcc2009-04-01 08:20:19 +0000642 if (ic->rds_iwdev)
643 rds_iw_remove_conn(ic->rds_iwdev, conn);
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000644
645 rdma_destroy_id(ic->i_cm_id);
646
647 ic->i_cm_id = NULL;
648 ic->i_pd = NULL;
649 ic->i_mr = NULL;
650 ic->i_send_cq = NULL;
651 ic->i_recv_cq = NULL;
652 ic->i_send_hdrs = NULL;
653 ic->i_recv_hdrs = NULL;
654 ic->i_ack = NULL;
655 }
656 BUG_ON(ic->rds_iwdev);
657
658 /* Clear pending transmit */
659 if (ic->i_rm) {
660 rds_message_put(ic->i_rm);
661 ic->i_rm = NULL;
662 }
663
664 /* Clear the ACK state */
665 clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
Andy Grover8cbd9602009-04-01 08:20:20 +0000666#ifdef KERNEL_HAS_ATOMIC64
667 atomic64_set(&ic->i_ack_next, 0);
668#else
669 ic->i_ack_next = 0;
670#endif
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000671 ic->i_ack_recv = 0;
672
673 /* Clear flow control state */
674 ic->i_flowctl = 0;
675 atomic_set(&ic->i_credits, 0);
676
677 rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
678 rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
679
680 if (ic->i_iwinc) {
681 rds_inc_put(&ic->i_iwinc->ii_inc);
682 ic->i_iwinc = NULL;
683 }
684
685 vfree(ic->i_sends);
686 ic->i_sends = NULL;
687 vfree(ic->i_recvs);
688 ic->i_recvs = NULL;
689 rdsdebug("shutdown complete\n");
690}
691
692int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp)
693{
694 struct rds_iw_connection *ic;
695 unsigned long flags;
696
697 /* XXX too lazy? */
698 ic = kzalloc(sizeof(struct rds_iw_connection), GFP_KERNEL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800699 if (!ic)
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000700 return -ENOMEM;
701
702 INIT_LIST_HEAD(&ic->iw_node);
Andy Groverd521b632009-10-30 08:51:57 +0000703 tasklet_init(&ic->i_recv_tasklet, rds_iw_recv_tasklet_fn,
704 (unsigned long) ic);
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000705 mutex_init(&ic->i_recv_mutex);
Andy Grover8cbd9602009-04-01 08:20:20 +0000706#ifndef KERNEL_HAS_ATOMIC64
707 spin_lock_init(&ic->i_ack_lock);
708#endif
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000709
710 /*
711 * rds_iw_conn_shutdown() waits for these to be emptied so they
712 * must be initialized before it can be called.
713 */
714 rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
715 rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
716
717 ic->conn = conn;
718 conn->c_transport_data = ic;
719
720 spin_lock_irqsave(&iw_nodev_conns_lock, flags);
721 list_add_tail(&ic->iw_node, &iw_nodev_conns);
722 spin_unlock_irqrestore(&iw_nodev_conns_lock, flags);
723
724
725 rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
726 return 0;
727}
728
Andy Grover745cbcc2009-04-01 08:20:19 +0000729/*
730 * Free a connection. Connection must be shut down and not set for reconnect.
731 */
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000732void rds_iw_conn_free(void *arg)
733{
734 struct rds_iw_connection *ic = arg;
Andy Grover745cbcc2009-04-01 08:20:19 +0000735 spinlock_t *lock_ptr;
736
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000737 rdsdebug("ic %p\n", ic);
Andy Grover745cbcc2009-04-01 08:20:19 +0000738
739 /*
740 * Conn is either on a dev's list or on the nodev list.
741 * A race with shutdown() or connect() would cause problems
742 * (since rds_iwdev would change) but that should never happen.
743 */
744 lock_ptr = ic->rds_iwdev ? &ic->rds_iwdev->spinlock : &iw_nodev_conns_lock;
745
746 spin_lock_irq(lock_ptr);
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000747 list_del(&ic->iw_node);
Andy Grover745cbcc2009-04-01 08:20:19 +0000748 spin_unlock_irq(lock_ptr);
749
Andy Groverfcd8b7c2009-02-24 15:30:36 +0000750 kfree(ic);
751}
752
753/*
754 * An error occurred on the connection
755 */
756void
757__rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...)
758{
759 va_list ap;
760
761 rds_conn_drop(conn);
762
763 va_start(ap, fmt);
764 vprintk(fmt, ap);
765 va_end(ap);
766}