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