ipoib_cm.c revision fd312561adcc90e924f35d3032d5493aeb4c3017
1/*
2 * Copyright (c) 2006 Mellanox Technologies. 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 * $Id$
33 */
34
35#include <rdma/ib_cm.h>
36#include <rdma/ib_cache.h>
37#include <net/dst.h>
38#include <net/icmp.h>
39#include <linux/icmpv6.h>
40#include <linux/delay.h>
41
42#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
43static int data_debug_level;
44
45module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
46MODULE_PARM_DESC(cm_data_debug_level,
47		 "Enable data path debug tracing for connected mode if > 0");
48#endif
49
50#include "ipoib.h"
51
52#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
53
54#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
55#define IPOIB_CM_RX_TIMEOUT     (2 * 256 * HZ)
56#define IPOIB_CM_RX_DELAY       (3 * 256 * HZ)
57#define IPOIB_CM_RX_UPDATE_MASK (0x3)
58
59static struct ib_qp_attr ipoib_cm_err_attr = {
60	.qp_state = IB_QPS_ERR
61};
62
63#define IPOIB_CM_RX_DRAIN_WRID 0x7fffffff
64
65static struct ib_send_wr ipoib_cm_rx_drain_wr = {
66	.wr_id = IPOIB_CM_RX_DRAIN_WRID,
67	.opcode = IB_WR_SEND,
68};
69
70static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
71			       struct ib_cm_event *event);
72
73static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
74				  u64 mapping[IPOIB_CM_RX_SG])
75{
76	int i;
77
78	ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
79
80	for (i = 0; i < frags; ++i)
81		ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
82}
83
84static int ipoib_cm_post_receive(struct net_device *dev, int id)
85{
86	struct ipoib_dev_priv *priv = netdev_priv(dev);
87	struct ib_recv_wr *bad_wr;
88	int i, ret;
89
90	priv->cm.rx_wr.wr_id = id | IPOIB_CM_OP_SRQ;
91
92	for (i = 0; i < IPOIB_CM_RX_SG; ++i)
93		priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
94
95	ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
96	if (unlikely(ret)) {
97		ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
98		ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
99				      priv->cm.srq_ring[id].mapping);
100		dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
101		priv->cm.srq_ring[id].skb = NULL;
102	}
103
104	return ret;
105}
106
107static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, int id, int frags,
108					     u64 mapping[IPOIB_CM_RX_SG])
109{
110	struct ipoib_dev_priv *priv = netdev_priv(dev);
111	struct sk_buff *skb;
112	int i;
113
114	skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
115	if (unlikely(!skb))
116		return NULL;
117
118	/*
119	 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
120	 * IP header to a multiple of 16.
121	 */
122	skb_reserve(skb, 12);
123
124	mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
125				       DMA_FROM_DEVICE);
126	if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
127		dev_kfree_skb_any(skb);
128		return NULL;
129	}
130
131	for (i = 0; i < frags; i++) {
132		struct page *page = alloc_page(GFP_ATOMIC);
133
134		if (!page)
135			goto partial_error;
136		skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
137
138		mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
139						 0, PAGE_SIZE, DMA_FROM_DEVICE);
140		if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
141			goto partial_error;
142	}
143
144	priv->cm.srq_ring[id].skb = skb;
145	return skb;
146
147partial_error:
148
149	ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
150
151	for (; i > 0; --i)
152		ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
153
154	dev_kfree_skb_any(skb);
155	return NULL;
156}
157
158static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv* priv)
159{
160	struct ib_send_wr *bad_wr;
161	struct ipoib_cm_rx *p;
162
163	/* We only reserved 1 extra slot in CQ for drain WRs, so
164	 * make sure we have at most 1 outstanding WR. */
165	if (list_empty(&priv->cm.rx_flush_list) ||
166	    !list_empty(&priv->cm.rx_drain_list))
167		return;
168
169	/*
170	 * QPs on flush list are error state.  This way, a "flush
171	 * error" WC will be immediately generated for each WR we post.
172	 */
173	p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
174	if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
175		ipoib_warn(priv, "failed to post drain wr\n");
176
177	list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
178}
179
180static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
181{
182	struct ipoib_cm_rx *p = ctx;
183	struct ipoib_dev_priv *priv = netdev_priv(p->dev);
184	unsigned long flags;
185
186	if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
187		return;
188
189	spin_lock_irqsave(&priv->lock, flags);
190	list_move(&p->list, &priv->cm.rx_flush_list);
191	p->state = IPOIB_CM_RX_FLUSH;
192	ipoib_cm_start_rx_drain(priv);
193	spin_unlock_irqrestore(&priv->lock, flags);
194}
195
196static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
197					   struct ipoib_cm_rx *p)
198{
199	struct ipoib_dev_priv *priv = netdev_priv(dev);
200	struct ib_qp_init_attr attr = {
201		.event_handler = ipoib_cm_rx_event_handler,
202		.send_cq = priv->cq, /* For drain WR */
203		.recv_cq = priv->cq,
204		.srq = priv->cm.srq,
205		.cap.max_send_wr = 1, /* For drain WR */
206		.cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
207		.sq_sig_type = IB_SIGNAL_ALL_WR,
208		.qp_type = IB_QPT_RC,
209		.qp_context = p,
210	};
211	return ib_create_qp(priv->pd, &attr);
212}
213
214static int ipoib_cm_modify_rx_qp(struct net_device *dev,
215				  struct ib_cm_id *cm_id, struct ib_qp *qp,
216				  unsigned psn)
217{
218	struct ipoib_dev_priv *priv = netdev_priv(dev);
219	struct ib_qp_attr qp_attr;
220	int qp_attr_mask, ret;
221
222	qp_attr.qp_state = IB_QPS_INIT;
223	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
224	if (ret) {
225		ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
226		return ret;
227	}
228	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
229	if (ret) {
230		ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
231		return ret;
232	}
233	qp_attr.qp_state = IB_QPS_RTR;
234	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
235	if (ret) {
236		ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
237		return ret;
238	}
239	qp_attr.rq_psn = psn;
240	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
241	if (ret) {
242		ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
243		return ret;
244	}
245
246	/*
247	 * Current Mellanox HCA firmware won't generate completions
248	 * with error for drain WRs unless the QP has been moved to
249	 * RTS first. This work-around leaves a window where a QP has
250	 * moved to error asynchronously, but this will eventually get
251	 * fixed in firmware, so let's not error out if modify QP
252	 * fails.
253	 */
254	qp_attr.qp_state = IB_QPS_RTS;
255	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
256	if (ret) {
257		ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
258		return 0;
259	}
260	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
261	if (ret) {
262		ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
263		return 0;
264	}
265
266	return 0;
267}
268
269static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
270			     struct ib_qp *qp, struct ib_cm_req_event_param *req,
271			     unsigned psn)
272{
273	struct ipoib_dev_priv *priv = netdev_priv(dev);
274	struct ipoib_cm_data data = {};
275	struct ib_cm_rep_param rep = {};
276
277	data.qpn = cpu_to_be32(priv->qp->qp_num);
278	data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
279
280	rep.private_data = &data;
281	rep.private_data_len = sizeof data;
282	rep.flow_control = 0;
283	rep.rnr_retry_count = req->rnr_retry_count;
284	rep.srq = 1;
285	rep.qp_num = qp->qp_num;
286	rep.starting_psn = psn;
287	return ib_send_cm_rep(cm_id, &rep);
288}
289
290static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
291{
292	struct net_device *dev = cm_id->context;
293	struct ipoib_dev_priv *priv = netdev_priv(dev);
294	struct ipoib_cm_rx *p;
295	unsigned psn;
296	int ret;
297
298	ipoib_dbg(priv, "REQ arrived\n");
299	p = kzalloc(sizeof *p, GFP_KERNEL);
300	if (!p)
301		return -ENOMEM;
302	p->dev = dev;
303	p->id = cm_id;
304	cm_id->context = p;
305	p->state = IPOIB_CM_RX_LIVE;
306	p->jiffies = jiffies;
307	INIT_LIST_HEAD(&p->list);
308
309	p->qp = ipoib_cm_create_rx_qp(dev, p);
310	if (IS_ERR(p->qp)) {
311		ret = PTR_ERR(p->qp);
312		goto err_qp;
313	}
314
315	psn = random32() & 0xffffff;
316	ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
317	if (ret)
318		goto err_modify;
319
320	spin_lock_irq(&priv->lock);
321	queue_delayed_work(ipoib_workqueue,
322			   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
323	/* Add this entry to passive ids list head, but do not re-add it
324	 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
325	p->jiffies = jiffies;
326	if (p->state == IPOIB_CM_RX_LIVE)
327		list_move(&p->list, &priv->cm.passive_ids);
328	spin_unlock_irq(&priv->lock);
329
330	ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
331	if (ret) {
332		ipoib_warn(priv, "failed to send REP: %d\n", ret);
333		if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
334			ipoib_warn(priv, "unable to move qp to error state\n");
335	}
336	return 0;
337
338err_modify:
339	ib_destroy_qp(p->qp);
340err_qp:
341	kfree(p);
342	return ret;
343}
344
345static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
346			       struct ib_cm_event *event)
347{
348	struct ipoib_cm_rx *p;
349	struct ipoib_dev_priv *priv;
350
351	switch (event->event) {
352	case IB_CM_REQ_RECEIVED:
353		return ipoib_cm_req_handler(cm_id, event);
354	case IB_CM_DREQ_RECEIVED:
355		p = cm_id->context;
356		ib_send_cm_drep(cm_id, NULL, 0);
357		/* Fall through */
358	case IB_CM_REJ_RECEIVED:
359		p = cm_id->context;
360		priv = netdev_priv(p->dev);
361		if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
362			ipoib_warn(priv, "unable to move qp to error state\n");
363		/* Fall through */
364	default:
365		return 0;
366	}
367}
368/* Adjust length of skb with fragments to match received data */
369static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
370			  unsigned int length, struct sk_buff *toskb)
371{
372	int i, num_frags;
373	unsigned int size;
374
375	/* put header into skb */
376	size = min(length, hdr_space);
377	skb->tail += size;
378	skb->len += size;
379	length -= size;
380
381	num_frags = skb_shinfo(skb)->nr_frags;
382	for (i = 0; i < num_frags; i++) {
383		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
384
385		if (length == 0) {
386			/* don't need this page */
387			skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
388			--skb_shinfo(skb)->nr_frags;
389		} else {
390			size = min(length, (unsigned) PAGE_SIZE);
391
392			frag->size = size;
393			skb->data_len += size;
394			skb->truesize += size;
395			skb->len += size;
396			length -= size;
397		}
398	}
399}
400
401void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
402{
403	struct ipoib_dev_priv *priv = netdev_priv(dev);
404	unsigned int wr_id = wc->wr_id & ~IPOIB_CM_OP_SRQ;
405	struct sk_buff *skb, *newskb;
406	struct ipoib_cm_rx *p;
407	unsigned long flags;
408	u64 mapping[IPOIB_CM_RX_SG];
409	int frags;
410
411	ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
412		       wr_id, wc->status);
413
414	if (unlikely(wr_id >= ipoib_recvq_size)) {
415		if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~IPOIB_CM_OP_SRQ)) {
416			spin_lock_irqsave(&priv->lock, flags);
417			list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
418			ipoib_cm_start_rx_drain(priv);
419			queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
420			spin_unlock_irqrestore(&priv->lock, flags);
421		} else
422			ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
423				   wr_id, ipoib_recvq_size);
424		return;
425	}
426
427	skb  = priv->cm.srq_ring[wr_id].skb;
428
429	if (unlikely(wc->status != IB_WC_SUCCESS)) {
430		ipoib_dbg(priv, "cm recv error "
431			   "(status=%d, wrid=%d vend_err %x)\n",
432			   wc->status, wr_id, wc->vendor_err);
433		++dev->stats.rx_dropped;
434		goto repost;
435	}
436
437	if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
438		p = wc->qp->qp_context;
439		if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
440			spin_lock_irqsave(&priv->lock, flags);
441			p->jiffies = jiffies;
442			/* Move this entry to list head, but do not re-add it
443			 * if it has been moved out of list. */
444			if (p->state == IPOIB_CM_RX_LIVE)
445				list_move(&p->list, &priv->cm.passive_ids);
446			spin_unlock_irqrestore(&priv->lock, flags);
447		}
448	}
449
450	frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
451					      (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
452
453	newskb = ipoib_cm_alloc_rx_skb(dev, wr_id, frags, mapping);
454	if (unlikely(!newskb)) {
455		/*
456		 * If we can't allocate a new RX buffer, dump
457		 * this packet and reuse the old buffer.
458		 */
459		ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
460		++dev->stats.rx_dropped;
461		goto repost;
462	}
463
464	ipoib_cm_dma_unmap_rx(priv, frags, priv->cm.srq_ring[wr_id].mapping);
465	memcpy(priv->cm.srq_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
466
467	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
468		       wc->byte_len, wc->slid);
469
470	skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
471
472	skb->protocol = ((struct ipoib_header *) skb->data)->proto;
473	skb_reset_mac_header(skb);
474	skb_pull(skb, IPOIB_ENCAP_LEN);
475
476	dev->last_rx = jiffies;
477	++dev->stats.rx_packets;
478	dev->stats.rx_bytes += skb->len;
479
480	skb->dev = dev;
481	/* XXX get correct PACKET_ type here */
482	skb->pkt_type = PACKET_HOST;
483	netif_receive_skb(skb);
484
485repost:
486	if (unlikely(ipoib_cm_post_receive(dev, wr_id)))
487		ipoib_warn(priv, "ipoib_cm_post_receive failed "
488			   "for buf %d\n", wr_id);
489}
490
491static inline int post_send(struct ipoib_dev_priv *priv,
492			    struct ipoib_cm_tx *tx,
493			    unsigned int wr_id,
494			    u64 addr, int len)
495{
496	struct ib_send_wr *bad_wr;
497
498	priv->tx_sge.addr             = addr;
499	priv->tx_sge.length           = len;
500
501	priv->tx_wr.wr_id 	      = wr_id;
502
503	return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
504}
505
506void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
507{
508	struct ipoib_dev_priv *priv = netdev_priv(dev);
509	struct ipoib_tx_buf *tx_req;
510	u64 addr;
511
512	if (unlikely(skb->len > tx->mtu)) {
513		ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
514			   skb->len, tx->mtu);
515		++dev->stats.tx_dropped;
516		++dev->stats.tx_errors;
517		ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
518		return;
519	}
520
521	ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
522		       tx->tx_head, skb->len, tx->qp->qp_num);
523
524	/*
525	 * We put the skb into the tx_ring _before_ we call post_send()
526	 * because it's entirely possible that the completion handler will
527	 * run before we execute anything after the post_send().  That
528	 * means we have to make sure everything is properly recorded and
529	 * our state is consistent before we call post_send().
530	 */
531	tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
532	tx_req->skb = skb;
533	addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
534	if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
535		++dev->stats.tx_errors;
536		dev_kfree_skb_any(skb);
537		return;
538	}
539
540	tx_req->mapping = addr;
541
542	if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
543			        addr, skb->len))) {
544		ipoib_warn(priv, "post_send failed\n");
545		++dev->stats.tx_errors;
546		ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
547		dev_kfree_skb_any(skb);
548	} else {
549		dev->trans_start = jiffies;
550		++tx->tx_head;
551
552		if (tx->tx_head - tx->tx_tail == ipoib_sendq_size) {
553			ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
554				  tx->qp->qp_num);
555			netif_stop_queue(dev);
556			set_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
557		}
558	}
559}
560
561static void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ipoib_cm_tx *tx,
562				  struct ib_wc *wc)
563{
564	struct ipoib_dev_priv *priv = netdev_priv(dev);
565	unsigned int wr_id = wc->wr_id;
566	struct ipoib_tx_buf *tx_req;
567	unsigned long flags;
568
569	ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
570		       wr_id, wc->status);
571
572	if (unlikely(wr_id >= ipoib_sendq_size)) {
573		ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
574			   wr_id, ipoib_sendq_size);
575		return;
576	}
577
578	tx_req = &tx->tx_ring[wr_id];
579
580	ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
581
582	/* FIXME: is this right? Shouldn't we only increment on success? */
583	++dev->stats.tx_packets;
584	dev->stats.tx_bytes += tx_req->skb->len;
585
586	dev_kfree_skb_any(tx_req->skb);
587
588	spin_lock_irqsave(&priv->tx_lock, flags);
589	++tx->tx_tail;
590	if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags)) &&
591	    tx->tx_head - tx->tx_tail <= ipoib_sendq_size >> 1) {
592		clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
593		netif_wake_queue(dev);
594	}
595
596	if (wc->status != IB_WC_SUCCESS &&
597	    wc->status != IB_WC_WR_FLUSH_ERR) {
598		struct ipoib_neigh *neigh;
599
600		ipoib_dbg(priv, "failed cm send event "
601			   "(status=%d, wrid=%d vend_err %x)\n",
602			   wc->status, wr_id, wc->vendor_err);
603
604		spin_lock(&priv->lock);
605		neigh = tx->neigh;
606
607		if (neigh) {
608			neigh->cm = NULL;
609			list_del(&neigh->list);
610			if (neigh->ah)
611				ipoib_put_ah(neigh->ah);
612			ipoib_neigh_free(dev, neigh);
613
614			tx->neigh = NULL;
615		}
616
617		/* queue would be re-started anyway when TX is destroyed,
618		 * but it makes sense to do it ASAP here. */
619		if (test_and_clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags))
620			netif_wake_queue(dev);
621
622		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
623			list_move(&tx->list, &priv->cm.reap_list);
624			queue_work(ipoib_workqueue, &priv->cm.reap_task);
625		}
626
627		clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
628
629		spin_unlock(&priv->lock);
630	}
631
632	spin_unlock_irqrestore(&priv->tx_lock, flags);
633}
634
635static void ipoib_cm_tx_completion(struct ib_cq *cq, void *tx_ptr)
636{
637	struct ipoib_cm_tx *tx = tx_ptr;
638	int n, i;
639
640	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
641	do {
642		n = ib_poll_cq(cq, IPOIB_NUM_WC, tx->ibwc);
643		for (i = 0; i < n; ++i)
644			ipoib_cm_handle_tx_wc(tx->dev, tx, tx->ibwc + i);
645	} while (n == IPOIB_NUM_WC);
646}
647
648int ipoib_cm_dev_open(struct net_device *dev)
649{
650	struct ipoib_dev_priv *priv = netdev_priv(dev);
651	int ret;
652
653	if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
654		return 0;
655
656	priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
657	if (IS_ERR(priv->cm.id)) {
658		printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
659		ret = PTR_ERR(priv->cm.id);
660		goto err_cm;
661	}
662
663	ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
664			   0, NULL);
665	if (ret) {
666		printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
667		       IPOIB_CM_IETF_ID | priv->qp->qp_num);
668		goto err_listen;
669	}
670
671	return 0;
672
673err_listen:
674	ib_destroy_cm_id(priv->cm.id);
675err_cm:
676	priv->cm.id = NULL;
677	return ret;
678}
679
680void ipoib_cm_dev_stop(struct net_device *dev)
681{
682	struct ipoib_dev_priv *priv = netdev_priv(dev);
683	struct ipoib_cm_rx *p, *n;
684	unsigned long begin;
685	LIST_HEAD(list);
686	int ret;
687
688	if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
689		return;
690
691	ib_destroy_cm_id(priv->cm.id);
692	priv->cm.id = NULL;
693
694	spin_lock_irq(&priv->lock);
695	while (!list_empty(&priv->cm.passive_ids)) {
696		p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
697		list_move(&p->list, &priv->cm.rx_error_list);
698		p->state = IPOIB_CM_RX_ERROR;
699		spin_unlock_irq(&priv->lock);
700		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
701		if (ret)
702			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
703		spin_lock_irq(&priv->lock);
704	}
705
706	/* Wait for all RX to be drained */
707	begin = jiffies;
708
709	while (!list_empty(&priv->cm.rx_error_list) ||
710	       !list_empty(&priv->cm.rx_flush_list) ||
711	       !list_empty(&priv->cm.rx_drain_list)) {
712		if (time_after(jiffies, begin + 5 * HZ)) {
713			ipoib_warn(priv, "RX drain timing out\n");
714
715			/*
716			 * assume the HW is wedged and just free up everything.
717			 */
718			list_splice_init(&priv->cm.rx_flush_list, &list);
719			list_splice_init(&priv->cm.rx_error_list, &list);
720			list_splice_init(&priv->cm.rx_drain_list, &list);
721			break;
722		}
723		spin_unlock_irq(&priv->lock);
724		msleep(1);
725		ipoib_drain_cq(dev);
726		spin_lock_irq(&priv->lock);
727	}
728
729	list_splice_init(&priv->cm.rx_reap_list, &list);
730
731	spin_unlock_irq(&priv->lock);
732
733	list_for_each_entry_safe(p, n, &list, list) {
734		ib_destroy_cm_id(p->id);
735		ib_destroy_qp(p->qp);
736		kfree(p);
737	}
738
739	cancel_delayed_work(&priv->cm.stale_task);
740}
741
742static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
743{
744	struct ipoib_cm_tx *p = cm_id->context;
745	struct ipoib_dev_priv *priv = netdev_priv(p->dev);
746	struct ipoib_cm_data *data = event->private_data;
747	struct sk_buff_head skqueue;
748	struct ib_qp_attr qp_attr;
749	int qp_attr_mask, ret;
750	struct sk_buff *skb;
751
752	p->mtu = be32_to_cpu(data->mtu);
753
754	if (p->mtu <= IPOIB_ENCAP_LEN) {
755		ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
756			   p->mtu, IPOIB_ENCAP_LEN);
757		return -EINVAL;
758	}
759
760	qp_attr.qp_state = IB_QPS_RTR;
761	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
762	if (ret) {
763		ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
764		return ret;
765	}
766
767	qp_attr.rq_psn = 0 /* FIXME */;
768	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
769	if (ret) {
770		ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
771		return ret;
772	}
773
774	qp_attr.qp_state = IB_QPS_RTS;
775	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
776	if (ret) {
777		ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
778		return ret;
779	}
780	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
781	if (ret) {
782		ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
783		return ret;
784	}
785
786	skb_queue_head_init(&skqueue);
787
788	spin_lock_irq(&priv->lock);
789	set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
790	if (p->neigh)
791		while ((skb = __skb_dequeue(&p->neigh->queue)))
792			__skb_queue_tail(&skqueue, skb);
793	spin_unlock_irq(&priv->lock);
794
795	while ((skb = __skb_dequeue(&skqueue))) {
796		skb->dev = p->dev;
797		if (dev_queue_xmit(skb))
798			ipoib_warn(priv, "dev_queue_xmit failed "
799				   "to requeue packet\n");
800	}
801
802	ret = ib_send_cm_rtu(cm_id, NULL, 0);
803	if (ret) {
804		ipoib_warn(priv, "failed to send RTU: %d\n", ret);
805		return ret;
806	}
807	return 0;
808}
809
810static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ib_cq *cq)
811{
812	struct ipoib_dev_priv *priv = netdev_priv(dev);
813	struct ib_qp_init_attr attr = {
814		.send_cq		= cq,
815		.recv_cq		= priv->cq,
816		.srq			= priv->cm.srq,
817		.cap.max_send_wr	= ipoib_sendq_size,
818		.cap.max_send_sge	= 1,
819		.sq_sig_type		= IB_SIGNAL_ALL_WR,
820		.qp_type		= IB_QPT_RC,
821        };
822
823	return ib_create_qp(priv->pd, &attr);
824}
825
826static int ipoib_cm_send_req(struct net_device *dev,
827			     struct ib_cm_id *id, struct ib_qp *qp,
828			     u32 qpn,
829			     struct ib_sa_path_rec *pathrec)
830{
831	struct ipoib_dev_priv *priv = netdev_priv(dev);
832	struct ipoib_cm_data data = {};
833	struct ib_cm_req_param req = {};
834
835	data.qpn = cpu_to_be32(priv->qp->qp_num);
836	data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
837
838	req.primary_path 	      = pathrec;
839	req.alternate_path 	      = NULL;
840	req.service_id                = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
841	req.qp_num 		      = qp->qp_num;
842	req.qp_type 		      = qp->qp_type;
843	req.private_data 	      = &data;
844	req.private_data_len 	      = sizeof data;
845	req.flow_control 	      = 0;
846
847	req.starting_psn              = 0; /* FIXME */
848
849	/*
850	 * Pick some arbitrary defaults here; we could make these
851	 * module parameters if anyone cared about setting them.
852	 */
853	req.responder_resources	      = 4;
854	req.remote_cm_response_timeout = 20;
855	req.local_cm_response_timeout  = 20;
856	req.retry_count 	      = 0; /* RFC draft warns against retries */
857	req.rnr_retry_count 	      = 0; /* RFC draft warns against retries */
858	req.max_cm_retries 	      = 15;
859	req.srq 	              = 1;
860	return ib_send_cm_req(id, &req);
861}
862
863static int ipoib_cm_modify_tx_init(struct net_device *dev,
864				  struct ib_cm_id *cm_id, struct ib_qp *qp)
865{
866	struct ipoib_dev_priv *priv = netdev_priv(dev);
867	struct ib_qp_attr qp_attr;
868	int qp_attr_mask, ret;
869	ret = ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
870	if (ret) {
871		ipoib_warn(priv, "pkey 0x%x not in cache: %d\n", priv->pkey, ret);
872		return ret;
873	}
874
875	qp_attr.qp_state = IB_QPS_INIT;
876	qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
877	qp_attr.port_num = priv->port;
878	qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
879
880	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
881	if (ret) {
882		ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
883		return ret;
884	}
885	return 0;
886}
887
888static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
889			    struct ib_sa_path_rec *pathrec)
890{
891	struct ipoib_dev_priv *priv = netdev_priv(p->dev);
892	int ret;
893
894	p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring,
895				GFP_KERNEL);
896	if (!p->tx_ring) {
897		ipoib_warn(priv, "failed to allocate tx ring\n");
898		ret = -ENOMEM;
899		goto err_tx;
900	}
901
902	p->cq = ib_create_cq(priv->ca, ipoib_cm_tx_completion, NULL, p,
903			     ipoib_sendq_size + 1, 0);
904	if (IS_ERR(p->cq)) {
905		ret = PTR_ERR(p->cq);
906		ipoib_warn(priv, "failed to allocate tx cq: %d\n", ret);
907		goto err_cq;
908	}
909
910	ret = ib_req_notify_cq(p->cq, IB_CQ_NEXT_COMP);
911	if (ret) {
912		ipoib_warn(priv, "failed to request completion notification: %d\n", ret);
913		goto err_req_notify;
914	}
915
916	p->qp = ipoib_cm_create_tx_qp(p->dev, p->cq);
917	if (IS_ERR(p->qp)) {
918		ret = PTR_ERR(p->qp);
919		ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
920		goto err_qp;
921	}
922
923	p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
924	if (IS_ERR(p->id)) {
925		ret = PTR_ERR(p->id);
926		ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
927		goto err_id;
928	}
929
930	ret = ipoib_cm_modify_tx_init(p->dev, p->id,  p->qp);
931	if (ret) {
932		ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
933		goto err_modify;
934	}
935
936	ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
937	if (ret) {
938		ipoib_warn(priv, "failed to send cm req: %d\n", ret);
939		goto err_send_cm;
940	}
941
942	ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
943		  p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
944
945	return 0;
946
947err_send_cm:
948err_modify:
949	ib_destroy_cm_id(p->id);
950err_id:
951	p->id = NULL;
952	ib_destroy_qp(p->qp);
953err_req_notify:
954err_qp:
955	p->qp = NULL;
956	ib_destroy_cq(p->cq);
957err_cq:
958	p->cq = NULL;
959err_tx:
960	return ret;
961}
962
963static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
964{
965	struct ipoib_dev_priv *priv = netdev_priv(p->dev);
966	struct ipoib_tx_buf *tx_req;
967
968	ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
969		  p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
970
971	if (p->id)
972		ib_destroy_cm_id(p->id);
973
974	if (p->qp)
975		ib_destroy_qp(p->qp);
976
977	if (p->cq)
978		ib_destroy_cq(p->cq);
979
980	if (test_bit(IPOIB_FLAG_NETIF_STOPPED, &p->flags))
981		netif_wake_queue(p->dev);
982
983	if (p->tx_ring) {
984		while ((int) p->tx_tail - (int) p->tx_head < 0) {
985			tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
986			ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
987					 DMA_TO_DEVICE);
988			dev_kfree_skb_any(tx_req->skb);
989			++p->tx_tail;
990		}
991
992		kfree(p->tx_ring);
993	}
994
995	kfree(p);
996}
997
998static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
999			       struct ib_cm_event *event)
1000{
1001	struct ipoib_cm_tx *tx = cm_id->context;
1002	struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1003	struct net_device *dev = priv->dev;
1004	struct ipoib_neigh *neigh;
1005	int ret;
1006
1007	switch (event->event) {
1008	case IB_CM_DREQ_RECEIVED:
1009		ipoib_dbg(priv, "DREQ received.\n");
1010		ib_send_cm_drep(cm_id, NULL, 0);
1011		break;
1012	case IB_CM_REP_RECEIVED:
1013		ipoib_dbg(priv, "REP received.\n");
1014		ret = ipoib_cm_rep_handler(cm_id, event);
1015		if (ret)
1016			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1017				       NULL, 0, NULL, 0);
1018		break;
1019	case IB_CM_REQ_ERROR:
1020	case IB_CM_REJ_RECEIVED:
1021	case IB_CM_TIMEWAIT_EXIT:
1022		ipoib_dbg(priv, "CM error %d.\n", event->event);
1023		spin_lock_irq(&priv->tx_lock);
1024		spin_lock(&priv->lock);
1025		neigh = tx->neigh;
1026
1027		if (neigh) {
1028			neigh->cm = NULL;
1029			list_del(&neigh->list);
1030			if (neigh->ah)
1031				ipoib_put_ah(neigh->ah);
1032			ipoib_neigh_free(dev, neigh);
1033
1034			tx->neigh = NULL;
1035		}
1036
1037		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1038			list_move(&tx->list, &priv->cm.reap_list);
1039			queue_work(ipoib_workqueue, &priv->cm.reap_task);
1040		}
1041
1042		spin_unlock(&priv->lock);
1043		spin_unlock_irq(&priv->tx_lock);
1044		break;
1045	default:
1046		break;
1047	}
1048
1049	return 0;
1050}
1051
1052struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1053				       struct ipoib_neigh *neigh)
1054{
1055	struct ipoib_dev_priv *priv = netdev_priv(dev);
1056	struct ipoib_cm_tx *tx;
1057
1058	tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1059	if (!tx)
1060		return NULL;
1061
1062	neigh->cm = tx;
1063	tx->neigh = neigh;
1064	tx->path = path;
1065	tx->dev = dev;
1066	list_add(&tx->list, &priv->cm.start_list);
1067	set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1068	queue_work(ipoib_workqueue, &priv->cm.start_task);
1069	return tx;
1070}
1071
1072void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1073{
1074	struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1075	if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1076		list_move(&tx->list, &priv->cm.reap_list);
1077		queue_work(ipoib_workqueue, &priv->cm.reap_task);
1078		ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1079			  IPOIB_GID_ARG(tx->neigh->dgid));
1080		tx->neigh = NULL;
1081	}
1082}
1083
1084static void ipoib_cm_tx_start(struct work_struct *work)
1085{
1086	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1087						   cm.start_task);
1088	struct net_device *dev = priv->dev;
1089	struct ipoib_neigh *neigh;
1090	struct ipoib_cm_tx *p;
1091	unsigned long flags;
1092	int ret;
1093
1094	struct ib_sa_path_rec pathrec;
1095	u32 qpn;
1096
1097	spin_lock_irqsave(&priv->tx_lock, flags);
1098	spin_lock(&priv->lock);
1099	while (!list_empty(&priv->cm.start_list)) {
1100		p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1101		list_del_init(&p->list);
1102		neigh = p->neigh;
1103		qpn = IPOIB_QPN(neigh->neighbour->ha);
1104		memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1105		spin_unlock(&priv->lock);
1106		spin_unlock_irqrestore(&priv->tx_lock, flags);
1107		ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1108		spin_lock_irqsave(&priv->tx_lock, flags);
1109		spin_lock(&priv->lock);
1110		if (ret) {
1111			neigh = p->neigh;
1112			if (neigh) {
1113				neigh->cm = NULL;
1114				list_del(&neigh->list);
1115				if (neigh->ah)
1116					ipoib_put_ah(neigh->ah);
1117				ipoib_neigh_free(dev, neigh);
1118			}
1119			list_del(&p->list);
1120			kfree(p);
1121		}
1122	}
1123	spin_unlock(&priv->lock);
1124	spin_unlock_irqrestore(&priv->tx_lock, flags);
1125}
1126
1127static void ipoib_cm_tx_reap(struct work_struct *work)
1128{
1129	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1130						   cm.reap_task);
1131	struct ipoib_cm_tx *p;
1132
1133	spin_lock_irq(&priv->tx_lock);
1134	spin_lock(&priv->lock);
1135	while (!list_empty(&priv->cm.reap_list)) {
1136		p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1137		list_del(&p->list);
1138		spin_unlock(&priv->lock);
1139		spin_unlock_irq(&priv->tx_lock);
1140		ipoib_cm_tx_destroy(p);
1141		spin_lock_irq(&priv->tx_lock);
1142		spin_lock(&priv->lock);
1143	}
1144	spin_unlock(&priv->lock);
1145	spin_unlock_irq(&priv->tx_lock);
1146}
1147
1148static void ipoib_cm_skb_reap(struct work_struct *work)
1149{
1150	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1151						   cm.skb_task);
1152	struct sk_buff *skb;
1153
1154	unsigned mtu = priv->mcast_mtu;
1155
1156	spin_lock_irq(&priv->tx_lock);
1157	spin_lock(&priv->lock);
1158	while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1159		spin_unlock(&priv->lock);
1160		spin_unlock_irq(&priv->tx_lock);
1161		if (skb->protocol == htons(ETH_P_IP))
1162			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1163#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1164		else if (skb->protocol == htons(ETH_P_IPV6))
1165			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
1166#endif
1167		dev_kfree_skb_any(skb);
1168		spin_lock_irq(&priv->tx_lock);
1169		spin_lock(&priv->lock);
1170	}
1171	spin_unlock(&priv->lock);
1172	spin_unlock_irq(&priv->tx_lock);
1173}
1174
1175void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb,
1176			   unsigned int mtu)
1177{
1178	struct ipoib_dev_priv *priv = netdev_priv(dev);
1179	int e = skb_queue_empty(&priv->cm.skb_queue);
1180
1181	if (skb->dst)
1182		skb->dst->ops->update_pmtu(skb->dst, mtu);
1183
1184	skb_queue_tail(&priv->cm.skb_queue, skb);
1185	if (e)
1186		queue_work(ipoib_workqueue, &priv->cm.skb_task);
1187}
1188
1189static void ipoib_cm_rx_reap(struct work_struct *work)
1190{
1191	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1192						   cm.rx_reap_task);
1193	struct ipoib_cm_rx *p, *n;
1194	LIST_HEAD(list);
1195
1196	spin_lock_irq(&priv->lock);
1197	list_splice_init(&priv->cm.rx_reap_list, &list);
1198	spin_unlock_irq(&priv->lock);
1199
1200	list_for_each_entry_safe(p, n, &list, list) {
1201		ib_destroy_cm_id(p->id);
1202		ib_destroy_qp(p->qp);
1203		kfree(p);
1204	}
1205}
1206
1207static void ipoib_cm_stale_task(struct work_struct *work)
1208{
1209	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1210						   cm.stale_task.work);
1211	struct ipoib_cm_rx *p;
1212	int ret;
1213
1214	spin_lock_irq(&priv->lock);
1215	while (!list_empty(&priv->cm.passive_ids)) {
1216		/* List is sorted by LRU, start from tail,
1217		 * stop when we see a recently used entry */
1218		p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1219		if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1220			break;
1221		list_move(&p->list, &priv->cm.rx_error_list);
1222		p->state = IPOIB_CM_RX_ERROR;
1223		spin_unlock_irq(&priv->lock);
1224		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1225		if (ret)
1226			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1227		spin_lock_irq(&priv->lock);
1228	}
1229
1230	if (!list_empty(&priv->cm.passive_ids))
1231		queue_delayed_work(ipoib_workqueue,
1232				   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1233	spin_unlock_irq(&priv->lock);
1234}
1235
1236
1237static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1238			 char *buf)
1239{
1240	struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1241
1242	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1243		return sprintf(buf, "connected\n");
1244	else
1245		return sprintf(buf, "datagram\n");
1246}
1247
1248static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1249			const char *buf, size_t count)
1250{
1251	struct net_device *dev = to_net_dev(d);
1252	struct ipoib_dev_priv *priv = netdev_priv(dev);
1253
1254	/* flush paths if we switch modes so that connections are restarted */
1255	if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1256		set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1257		ipoib_warn(priv, "enabling connected mode "
1258			   "will cause multicast packet drops\n");
1259		ipoib_flush_paths(dev);
1260		return count;
1261	}
1262
1263	if (!strcmp(buf, "datagram\n")) {
1264		clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1265		dev->mtu = min(priv->mcast_mtu, dev->mtu);
1266		ipoib_flush_paths(dev);
1267		return count;
1268	}
1269
1270	return -EINVAL;
1271}
1272
1273static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1274
1275int ipoib_cm_add_mode_attr(struct net_device *dev)
1276{
1277	return device_create_file(&dev->dev, &dev_attr_mode);
1278}
1279
1280int ipoib_cm_dev_init(struct net_device *dev)
1281{
1282	struct ipoib_dev_priv *priv = netdev_priv(dev);
1283	struct ib_srq_init_attr srq_init_attr = {
1284		.attr = {
1285			.max_wr  = ipoib_recvq_size,
1286			.max_sge = IPOIB_CM_RX_SG
1287		}
1288	};
1289	int ret, i;
1290
1291	INIT_LIST_HEAD(&priv->cm.passive_ids);
1292	INIT_LIST_HEAD(&priv->cm.reap_list);
1293	INIT_LIST_HEAD(&priv->cm.start_list);
1294	INIT_LIST_HEAD(&priv->cm.rx_error_list);
1295	INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1296	INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1297	INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1298	INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1299	INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1300	INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1301	INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1302	INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1303
1304	skb_queue_head_init(&priv->cm.skb_queue);
1305
1306	priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1307	if (IS_ERR(priv->cm.srq)) {
1308		ret = PTR_ERR(priv->cm.srq);
1309		priv->cm.srq = NULL;
1310		return ret;
1311	}
1312
1313	priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1314				    GFP_KERNEL);
1315	if (!priv->cm.srq_ring) {
1316		printk(KERN_WARNING "%s: failed to allocate CM ring (%d entries)\n",
1317		       priv->ca->name, ipoib_recvq_size);
1318		ipoib_cm_dev_cleanup(dev);
1319		return -ENOMEM;
1320	}
1321
1322	for (i = 0; i < IPOIB_CM_RX_SG; ++i)
1323		priv->cm.rx_sge[i].lkey	= priv->mr->lkey;
1324
1325	priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
1326	for (i = 1; i < IPOIB_CM_RX_SG; ++i)
1327		priv->cm.rx_sge[i].length = PAGE_SIZE;
1328	priv->cm.rx_wr.next = NULL;
1329	priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
1330	priv->cm.rx_wr.num_sge = IPOIB_CM_RX_SG;
1331
1332	for (i = 0; i < ipoib_recvq_size; ++i) {
1333		if (!ipoib_cm_alloc_rx_skb(dev, i, IPOIB_CM_RX_SG - 1,
1334					   priv->cm.srq_ring[i].mapping)) {
1335			ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
1336			ipoib_cm_dev_cleanup(dev);
1337			return -ENOMEM;
1338		}
1339		if (ipoib_cm_post_receive(dev, i)) {
1340			ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
1341			ipoib_cm_dev_cleanup(dev);
1342			return -EIO;
1343		}
1344	}
1345
1346	priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1347	return 0;
1348}
1349
1350void ipoib_cm_dev_cleanup(struct net_device *dev)
1351{
1352	struct ipoib_dev_priv *priv = netdev_priv(dev);
1353	int i, ret;
1354
1355	if (!priv->cm.srq)
1356		return;
1357
1358	ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1359
1360	ret = ib_destroy_srq(priv->cm.srq);
1361	if (ret)
1362		ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1363
1364	priv->cm.srq = NULL;
1365	if (!priv->cm.srq_ring)
1366		return;
1367	for (i = 0; i < ipoib_recvq_size; ++i)
1368		if (priv->cm.srq_ring[i].skb) {
1369			ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
1370					      priv->cm.srq_ring[i].mapping);
1371			dev_kfree_skb_any(priv->cm.srq_ring[i].skb);
1372			priv->cm.srq_ring[i].skb = NULL;
1373		}
1374	kfree(priv->cm.srq_ring);
1375	priv->cm.srq_ring = NULL;
1376}
1377