blob: 5df40b128f81b1b13f8cd9d30133a4ea43148875 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
36#include <linux/delay.h>
37#include <linux/dma-mapping.h>
38
Eli Cohen40ca1982008-04-16 21:09:27 -070039#include <linux/ip.h>
40#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "ipoib.h"
43
44#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
45static int data_debug_level;
46
47module_param(data_debug_level, int, 0644);
48MODULE_PARM_DESC(data_debug_level,
49 "Enable data path debug tracing if > 0");
50#endif
51
Ingo Molnar95ed6442006-01-13 14:51:39 -080052static DEFINE_MUTEX(pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
55 struct ib_pd *pd, struct ib_ah_attr *attr)
56{
57 struct ipoib_ah *ah;
58
59 ah = kmalloc(sizeof *ah, GFP_KERNEL);
60 if (!ah)
61 return NULL;
62
63 ah->dev = dev;
64 ah->last_send = 0;
65 kref_init(&ah->ref);
66
67 ah->ah = ib_create_ah(pd, attr);
68 if (IS_ERR(ah->ah)) {
69 kfree(ah);
70 ah = NULL;
71 } else
72 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
73
74 return ah;
75}
76
77void ipoib_free_ah(struct kref *kref)
78{
79 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
80 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
81
82 unsigned long flags;
83
Roland Dreier31c02e22006-06-17 20:37:34 -070084 spin_lock_irqsave(&priv->lock, flags);
85 list_add_tail(&ah->list, &priv->dead_ahs);
86 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Shirley Mabc7b3a32008-04-23 11:55:45 -070089static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
90 u64 mapping[IPOIB_UD_RX_SG])
91{
92 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
93 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
94 DMA_FROM_DEVICE);
95 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
96 DMA_FROM_DEVICE);
97 } else
98 ib_dma_unmap_single(priv->ca, mapping[0],
99 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
100 DMA_FROM_DEVICE);
101}
102
103static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
104 struct sk_buff *skb,
105 unsigned int length)
106{
107 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
108 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
109 unsigned int size;
110 /*
111 * There is only two buffers needed for max_payload = 4K,
112 * first buf size is IPOIB_UD_HEAD_SIZE
113 */
114 skb->tail += IPOIB_UD_HEAD_SIZE;
115 skb->len += length;
116
117 size = length - IPOIB_UD_HEAD_SIZE;
118
119 frag->size = size;
120 skb->data_len += size;
121 skb->truesize += size;
122 } else
123 skb_put(skb, length);
124
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int ipoib_ib_post_receive(struct net_device *dev, int id)
128{
129 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700130 struct ib_recv_wr *bad_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int ret;
132
Shirley Mabc7b3a32008-04-23 11:55:45 -0700133 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
134 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
135 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Shirley Mabc7b3a32008-04-23 11:55:45 -0700138 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
Roland Dreier1993d682005-10-28 15:30:34 -0700139 if (unlikely(ret)) {
140 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700141 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
Roland Dreier1993d682005-10-28 15:30:34 -0700142 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 priv->rx_ring[id].skb = NULL;
144 }
145
146 return ret;
147}
148
Shirley Mabc7b3a32008-04-23 11:55:45 -0700149static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
Roland Dreier1993d682005-10-28 15:30:34 -0700150{
151 struct ipoib_dev_priv *priv = netdev_priv(dev);
152 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700153 int buf_size;
154 u64 *mapping;
Roland Dreier1993d682005-10-28 15:30:34 -0700155
Shirley Mabc7b3a32008-04-23 11:55:45 -0700156 if (ipoib_ud_need_sg(priv->max_ib_mtu))
157 buf_size = IPOIB_UD_HEAD_SIZE;
158 else
159 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
160
161 skb = dev_alloc_skb(buf_size + 4);
162 if (unlikely(!skb))
163 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700164
165 /*
166 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
167 * header. So we need 4 more bytes to get to 48 and align the
168 * IP header to a multiple of 16.
169 */
170 skb_reserve(skb, 4);
171
Shirley Mabc7b3a32008-04-23 11:55:45 -0700172 mapping = priv->rx_ring[id].mapping;
173 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
174 DMA_FROM_DEVICE);
175 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
176 goto error;
177
178 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
179 struct page *page = alloc_page(GFP_ATOMIC);
180 if (!page)
181 goto partial_error;
182 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
183 mapping[1] =
184 ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
185 0, PAGE_SIZE, DMA_FROM_DEVICE);
186 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
187 goto partial_error;
Roland Dreier1993d682005-10-28 15:30:34 -0700188 }
189
Shirley Mabc7b3a32008-04-23 11:55:45 -0700190 priv->rx_ring[id].skb = skb;
191 return skb;
Roland Dreier1993d682005-10-28 15:30:34 -0700192
Shirley Mabc7b3a32008-04-23 11:55:45 -0700193partial_error:
194 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
195error:
196 dev_kfree_skb_any(skb);
197 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int ipoib_ib_post_receives(struct net_device *dev)
201{
202 struct ipoib_dev_priv *priv = netdev_priv(dev);
203 int i;
204
Shirley Ma0f485252006-04-10 09:43:58 -0700205 for (i = 0; i < ipoib_recvq_size; ++i) {
Shirley Mabc7b3a32008-04-23 11:55:45 -0700206 if (!ipoib_alloc_rx_skb(dev, i)) {
Roland Dreier1993d682005-10-28 15:30:34 -0700207 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
208 return -ENOMEM;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (ipoib_ib_post_receive(dev, i)) {
211 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
212 return -EIO;
213 }
214 }
215
216 return 0;
217}
218
Roland Dreier2439a6e2006-09-22 15:22:52 -0700219static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
220{
221 struct ipoib_dev_priv *priv = netdev_priv(dev);
222 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
223 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700224 u64 mapping[IPOIB_UD_RX_SG];
Roland Dreier2439a6e2006-09-22 15:22:52 -0700225
Roland Dreiera89875f2007-04-18 20:20:53 -0700226 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
227 wr_id, wc->status);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700228
229 if (unlikely(wr_id >= ipoib_recvq_size)) {
230 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
231 wr_id, ipoib_recvq_size);
232 return;
233 }
234
235 skb = priv->rx_ring[wr_id].skb;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700236
237 if (unlikely(wc->status != IB_WC_SUCCESS)) {
238 if (wc->status != IB_WC_WR_FLUSH_ERR)
239 ipoib_warn(priv, "failed recv event "
240 "(status=%d, wrid=%d vend_err %x)\n",
241 wc->status, wr_id, wc->vendor_err);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700242 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700243 dev_kfree_skb_any(skb);
244 priv->rx_ring[wr_id].skb = NULL;
245 return;
246 }
247
248 /*
Roland Dreier1b844af2007-07-10 13:43:53 -0700249 * Drop packets that this interface sent, ie multicast packets
250 * that the HCA has replicated.
251 */
252 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
253 goto repost;
254
Shirley Mabc7b3a32008-04-23 11:55:45 -0700255 memcpy(mapping, priv->rx_ring[wr_id].mapping,
256 IPOIB_UD_RX_SG * sizeof *mapping);
257
Roland Dreier1b844af2007-07-10 13:43:53 -0700258 /*
Roland Dreier2439a6e2006-09-22 15:22:52 -0700259 * If we can't allocate a new RX buffer, dump
260 * this packet and reuse the old buffer.
261 */
Shirley Mabc7b3a32008-04-23 11:55:45 -0700262 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
Roland Dreierde903512007-09-28 15:33:51 -0700263 ++dev->stats.rx_dropped;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700264 goto repost;
265 }
266
267 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
268 wc->byte_len, wc->slid);
269
Shirley Mabc7b3a32008-04-23 11:55:45 -0700270 ipoib_ud_dma_unmap_rx(priv, mapping);
271 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700272
Roland Dreier2439a6e2006-09-22 15:22:52 -0700273 skb_pull(skb, IB_GRH_BYTES);
274
Roland Dreier1b844af2007-07-10 13:43:53 -0700275 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
276 skb_reset_mac_header(skb);
277 skb_pull(skb, IPOIB_ENCAP_LEN);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700278
Roland Dreierde903512007-09-28 15:33:51 -0700279 ++dev->stats.rx_packets;
280 dev->stats.rx_bytes += skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700281
Roland Dreier1b844af2007-07-10 13:43:53 -0700282 skb->dev = dev;
283 /* XXX get correct PACKET_ type here */
284 skb->pkt_type = PACKET_HOST;
Eli Cohen60461362008-04-16 21:01:10 -0700285
286 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
287 skb->ip_summed = CHECKSUM_UNNECESSARY;
288
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -0700289 if (dev->features & NETIF_F_LRO)
290 lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
291 else
292 netif_receive_skb(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700293
294repost:
295 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
296 ipoib_warn(priv, "ipoib_ib_post_receive failed "
297 "for buf %d\n", wr_id);
298}
299
Eli Cohen71437402008-01-30 18:30:53 +0200300static int ipoib_dma_map_tx(struct ib_device *ca,
301 struct ipoib_tx_buf *tx_req)
302{
303 struct sk_buff *skb = tx_req->skb;
304 u64 *mapping = tx_req->mapping;
305 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700306 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200307
Eli Cohen40ca1982008-04-16 21:09:27 -0700308 if (skb_headlen(skb)) {
309 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
310 DMA_TO_DEVICE);
311 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
312 return -EIO;
313
314 off = 1;
315 } else
316 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200317
318 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
319 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Eli Cohen40ca1982008-04-16 21:09:27 -0700320 mapping[i + off] = ib_dma_map_page(ca, frag->page,
Eli Cohen71437402008-01-30 18:30:53 +0200321 frag->page_offset, frag->size,
322 DMA_TO_DEVICE);
Eli Cohen40ca1982008-04-16 21:09:27 -0700323 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
Eli Cohen71437402008-01-30 18:30:53 +0200324 goto partial_error;
325 }
326 return 0;
327
328partial_error:
Eli Cohen71437402008-01-30 18:30:53 +0200329 for (; i > 0; --i) {
330 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
Eli Cohen40ca1982008-04-16 21:09:27 -0700331 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
Eli Cohen71437402008-01-30 18:30:53 +0200332 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700333
334 if (off)
335 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
336
Eli Cohen71437402008-01-30 18:30:53 +0200337 return -EIO;
338}
339
340static void ipoib_dma_unmap_tx(struct ib_device *ca,
341 struct ipoib_tx_buf *tx_req)
342{
343 struct sk_buff *skb = tx_req->skb;
344 u64 *mapping = tx_req->mapping;
345 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700346 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200347
Eli Cohen40ca1982008-04-16 21:09:27 -0700348 if (skb_headlen(skb)) {
349 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
350 off = 1;
351 } else
352 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200353
354 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
355 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Eli Cohen40ca1982008-04-16 21:09:27 -0700356 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
Eli Cohen71437402008-01-30 18:30:53 +0200357 DMA_TO_DEVICE);
358 }
359}
360
Roland Dreier2439a6e2006-09-22 15:22:52 -0700361static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct ipoib_dev_priv *priv = netdev_priv(dev);
364 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700365 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Roland Dreiera89875f2007-04-18 20:20:53 -0700367 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
368 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Roland Dreier2439a6e2006-09-22 15:22:52 -0700370 if (unlikely(wr_id >= ipoib_sendq_size)) {
371 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
372 wr_id, ipoib_sendq_size);
373 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700375
376 tx_req = &priv->tx_ring[wr_id];
377
Eli Cohen71437402008-01-30 18:30:53 +0200378 ipoib_dma_unmap_tx(priv->ca, tx_req);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700379
Roland Dreierde903512007-09-28 15:33:51 -0700380 ++dev->stats.tx_packets;
381 dev->stats.tx_bytes += tx_req->skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700382
383 dev_kfree_skb_any(tx_req->skb);
384
Roland Dreier2439a6e2006-09-22 15:22:52 -0700385 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300386 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
387 netif_queue_stopped(dev) &&
388 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Roland Dreier2439a6e2006-09-22 15:22:52 -0700389 netif_wake_queue(dev);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700390
391 if (wc->status != IB_WC_SUCCESS &&
392 wc->status != IB_WC_WR_FLUSH_ERR)
393 ipoib_warn(priv, "failed send event "
394 "(status=%d, wrid=%d vend_err %x)\n",
395 wc->status, wr_id, wc->vendor_err);
396}
397
Eli Cohenf56bcd82008-04-29 13:46:53 -0700398static int poll_tx(struct ipoib_dev_priv *priv)
399{
400 int n, i;
401
402 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
403 for (i = 0; i < n; ++i)
404 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
405
406 return n == MAX_SEND_CQE;
407}
408
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700409int ipoib_poll(struct napi_struct *napi, int budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700410{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700411 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
412 struct net_device *dev = priv->dev;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700413 int done;
414 int t;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700415 int n, i;
416
417 done = 0;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700418
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700419poll_more:
420 while (done < budget) {
421 int max = (budget - done);
422
Roland Dreier8d1cc862007-05-06 21:05:32 -0700423 t = min(IPOIB_NUM_WC, max);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700424 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700425
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700426 for (i = 0; i < n; i++) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700427 struct ib_wc *wc = priv->ibwc + i;
428
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300429 if (wc->wr_id & IPOIB_OP_RECV) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700430 ++done;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300431 if (wc->wr_id & IPOIB_OP_CM)
432 ipoib_cm_handle_rx_wc(dev, wc);
433 else
434 ipoib_ib_handle_rx_wc(dev, wc);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700435 } else
436 ipoib_cm_handle_tx_wc(priv->dev, wc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700437 }
438
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700439 if (n != t)
Roland Dreier8d1cc862007-05-06 21:05:32 -0700440 break;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700441 }
442
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700443 if (done < budget) {
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -0700444 if (dev->features & NETIF_F_LRO)
445 lro_flush_all(&priv->lro.lro_mgr);
446
Ben Hutchings288379f2009-01-19 16:43:59 -0800447 napi_complete(napi);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700448 if (unlikely(ib_req_notify_cq(priv->recv_cq,
Roland Dreier8d1cc862007-05-06 21:05:32 -0700449 IB_CQ_NEXT_COMP |
450 IB_CQ_REPORT_MISSED_EVENTS)) &&
Ben Hutchings288379f2009-01-19 16:43:59 -0800451 napi_reschedule(napi))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700452 goto poll_more;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700453 }
454
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700455 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
459{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700460 struct net_device *dev = dev_ptr;
461 struct ipoib_dev_priv *priv = netdev_priv(dev);
462
Ben Hutchings288379f2009-01-19 16:43:59 -0800463 napi_schedule(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Eli Cohen57ce41d2008-04-30 20:02:45 -0700466static void drain_tx_cq(struct net_device *dev)
467{
468 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700469
Roland Dreier943c2462008-09-30 10:36:21 -0700470 netif_tx_lock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700471 while (poll_tx(priv))
472 ; /* nothing */
473
474 if (netif_queue_stopped(dev))
475 mod_timer(&priv->poll_timer, jiffies + 1);
476
Roland Dreier943c2462008-09-30 10:36:21 -0700477 netif_tx_unlock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700478}
479
480void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
481{
Roland Dreier943c2462008-09-30 10:36:21 -0700482 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
483
484 mod_timer(&priv->poll_timer, jiffies);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static inline int post_send(struct ipoib_dev_priv *priv,
488 unsigned int wr_id,
489 struct ib_ah *address, u32 qpn,
Eli Cohen40ca1982008-04-16 21:09:27 -0700490 struct ipoib_tx_buf *tx_req,
491 void *head, int hlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct ib_send_wr *bad_wr;
Eli Cohen40ca1982008-04-16 21:09:27 -0700494 int i, off;
495 struct sk_buff *skb = tx_req->skb;
496 skb_frag_t *frags = skb_shinfo(skb)->frags;
497 int nr_frags = skb_shinfo(skb)->nr_frags;
498 u64 *mapping = tx_req->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Eli Cohen40ca1982008-04-16 21:09:27 -0700500 if (skb_headlen(skb)) {
501 priv->tx_sge[0].addr = mapping[0];
502 priv->tx_sge[0].length = skb_headlen(skb);
503 off = 1;
504 } else
505 off = 0;
506
Eli Cohen71437402008-01-30 18:30:53 +0200507 for (i = 0; i < nr_frags; ++i) {
Eli Cohen40ca1982008-04-16 21:09:27 -0700508 priv->tx_sge[i + off].addr = mapping[i + off];
509 priv->tx_sge[i + off].length = frags[i].size;
Eli Cohen71437402008-01-30 18:30:53 +0200510 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700511 priv->tx_wr.num_sge = nr_frags + off;
Eli Cohen71437402008-01-30 18:30:53 +0200512 priv->tx_wr.wr_id = wr_id;
513 priv->tx_wr.wr.ud.remote_qpn = qpn;
514 priv->tx_wr.wr.ud.ah = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eli Cohen40ca1982008-04-16 21:09:27 -0700516 if (head) {
517 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
518 priv->tx_wr.wr.ud.header = head;
519 priv->tx_wr.wr.ud.hlen = hlen;
520 priv->tx_wr.opcode = IB_WR_LSO;
521 } else
522 priv->tx_wr.opcode = IB_WR_SEND;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
525}
526
527void ipoib_send(struct net_device *dev, struct sk_buff *skb,
528 struct ipoib_ah *address, u32 qpn)
529{
530 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700531 struct ipoib_tx_buf *tx_req;
Or Gerlitza48f5092010-03-04 13:17:37 +0000532 int hlen, rc;
Eli Cohen40ca1982008-04-16 21:09:27 -0700533 void *phead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Eli Cohen40ca1982008-04-16 21:09:27 -0700535 if (skb_is_gso(skb)) {
536 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
537 phead = skb->data;
538 if (unlikely(!skb_pull(skb, hlen))) {
539 ipoib_warn(priv, "linear data too small\n");
540 ++dev->stats.tx_dropped;
541 ++dev->stats.tx_errors;
542 dev_kfree_skb_any(skb);
543 return;
544 }
545 } else {
546 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
547 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
548 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
549 ++dev->stats.tx_dropped;
550 ++dev->stats.tx_errors;
551 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
552 return;
553 }
554 phead = NULL;
555 hlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
559 skb->len, address, qpn);
560
561 /*
562 * We put the skb into the tx_ring _before_ we call post_send()
563 * because it's entirely possible that the completion handler will
564 * run before we execute anything after the post_send(). That
565 * means we have to make sure everything is properly recorded and
566 * our state is consistent before we call post_send().
567 */
Shirley Ma0f485252006-04-10 09:43:58 -0700568 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 tx_req->skb = skb;
Eli Cohen71437402008-01-30 18:30:53 +0200570 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700571 ++dev->stats.tx_errors;
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700572 dev_kfree_skb_any(skb);
573 return;
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Eli Cohen60461362008-04-16 21:01:10 -0700576 if (skb->ip_summed == CHECKSUM_PARTIAL)
577 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
578 else
579 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
580
Eli Cohen57ce41d2008-04-30 20:02:45 -0700581 if (++priv->tx_outstanding == ipoib_sendq_size) {
582 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
583 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
584 ipoib_warn(priv, "request notify on send CQ failed\n");
585 netif_stop_queue(dev);
586 }
587
Or Gerlitza48f5092010-03-04 13:17:37 +0000588 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
589 address->ah, qpn, tx_req, phead, hlen);
590 if (unlikely(rc)) {
591 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700592 ++dev->stats.tx_errors;
Eli Cohen57ce41d2008-04-30 20:02:45 -0700593 --priv->tx_outstanding;
Eli Cohen71437402008-01-30 18:30:53 +0200594 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 dev_kfree_skb_any(skb);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700596 if (netif_queue_stopped(dev))
597 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else {
599 dev->trans_start = jiffies;
600
601 address->last_send = priv->tx_head;
602 ++priv->tx_head;
Eli Cohenf56bcd82008-04-29 13:46:53 -0700603 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Eli Cohenf56bcd82008-04-29 13:46:53 -0700606
607 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
Eli Cohen57ce41d2008-04-30 20:02:45 -0700608 while (poll_tx(priv))
609 ; /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static void __ipoib_reap_ah(struct net_device *dev)
613{
614 struct ipoib_dev_priv *priv = netdev_priv(dev);
615 struct ipoib_ah *ah, *tah;
616 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700617 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Roland Dreier943c2462008-09-30 10:36:21 -0700619 netif_tx_lock_bh(dev);
620 spin_lock_irqsave(&priv->lock, flags);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700623 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700625 ib_destroy_ah(ah->ah);
626 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Roland Dreier943c2462008-09-30 10:36:21 -0700628
629 spin_unlock_irqrestore(&priv->lock, flags);
630 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
David Howellsc4028952006-11-22 14:57:56 +0000633void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
David Howellsc4028952006-11-22 14:57:56 +0000635 struct ipoib_dev_priv *priv =
636 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
637 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 __ipoib_reap_ah(dev);
640
641 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
Anton Blanchard69fc5072007-10-15 00:50:56 -0500642 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
643 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Eli Cohen57ce41d2008-04-30 20:02:45 -0700646static void ipoib_ib_tx_timer_func(unsigned long ctx)
647{
648 drain_tx_cq((struct net_device *)ctx);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651int ipoib_ib_dev_open(struct net_device *dev)
652{
653 struct ipoib_dev_priv *priv = netdev_priv(dev);
654 int ret;
655
Yosef Etigin26bbf132007-05-19 08:51:54 -0700656 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
657 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
658 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
659 return -1;
660 }
661 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
662
Roland Dreier5b6810e2005-10-11 11:08:24 -0700663 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700665 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -1;
667 }
668
669 ret = ipoib_ib_post_receives(dev);
670 if (ret) {
671 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700672 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -1;
674 }
675
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200676 ret = ipoib_cm_dev_open(dev);
677 if (ret) {
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300678 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700679 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200680 return -1;
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
Anton Blanchard69fc5072007-10-15 00:50:56 -0500684 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
685 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Yossi Etigine028cc52009-04-20 13:58:08 -0700687 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
688 napi_enable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691}
692
Leonid Arsh7a343d42006-03-23 19:52:51 +0200693static void ipoib_pkey_dev_check_presence(struct net_device *dev)
694{
695 struct ipoib_dev_priv *priv = netdev_priv(dev);
696 u16 pkey_index = 0;
697
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700698 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
Leonid Arsh7a343d42006-03-23 19:52:51 +0200699 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
700 else
701 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704int ipoib_ib_dev_up(struct net_device *dev)
705{
706 struct ipoib_dev_priv *priv = netdev_priv(dev);
707
Leonid Arsh7a343d42006-03-23 19:52:51 +0200708 ipoib_pkey_dev_check_presence(dev);
709
710 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
711 ipoib_dbg(priv, "PKEY is not assigned.\n");
712 return 0;
713 }
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
716
717 return ipoib_mcast_start_thread(dev);
718}
719
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800720int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct ipoib_dev_priv *priv = netdev_priv(dev);
723
724 ipoib_dbg(priv, "downing ib_dev\n");
725
726 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
727 netif_carrier_off(dev);
728
729 /* Shutdown the P_Key thread if still active */
730 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800731 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700733 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800734 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800735 if (flush)
736 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800739 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ipoib_mcast_dev_flush(dev);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ipoib_flush_paths(dev);
743
744 return 0;
745}
746
747static int recvs_pending(struct net_device *dev)
748{
749 struct ipoib_dev_priv *priv = netdev_priv(dev);
750 int pending = 0;
751 int i;
752
Shirley Ma0f485252006-04-10 09:43:58 -0700753 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (priv->rx_ring[i].skb)
755 ++pending;
756
757 return pending;
758}
759
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300760void ipoib_drain_cq(struct net_device *dev)
761{
762 struct ipoib_dev_priv *priv = netdev_priv(dev);
763 int i, n;
Roland Dreier943c2462008-09-30 10:36:21 -0700764
765 /*
766 * We call completion handling routines that expect to be
767 * called from the BH-disabled NAPI poll context, so disable
768 * BHs here too.
769 */
770 local_bh_disable();
771
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300772 do {
Eli Cohenf56bcd82008-04-29 13:46:53 -0700773 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300774 for (i = 0; i < n; ++i) {
Roland Dreierce423ef2007-10-09 19:59:04 -0700775 /*
776 * Convert any successful completions to flush
777 * errors to avoid passing packets up the
778 * stack after bringing the device down.
779 */
780 if (priv->ibwc[i].status == IB_WC_SUCCESS)
781 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
782
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300783 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
784 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
785 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
786 else
787 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700788 } else
789 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300790 }
791 } while (n == IPOIB_NUM_WC);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700792
793 while (poll_tx(priv))
794 ; /* nothing */
Roland Dreier943c2462008-09-30 10:36:21 -0700795
796 local_bh_enable();
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300797}
798
Yosef Etigin26bbf132007-05-19 08:51:54 -0700799int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct ipoib_dev_priv *priv = netdev_priv(dev);
802 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700804 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300805 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Yossi Etigine028cc52009-04-20 13:58:08 -0700807 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
808 napi_disable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200809
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200810 ipoib_cm_dev_stop(dev);
811
Roland Dreier3bc12e72005-10-30 13:20:09 -0800812 /*
813 * Move our QP to the error state and then reinitialize in
814 * when all work requests have completed or have been flushed.
815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800817 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
819
820 /* Wait for all sends and receives to complete */
821 begin = jiffies;
822
823 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
824 if (time_after(jiffies, begin + 5 * HZ)) {
825 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
826 priv->tx_head - priv->tx_tail, recvs_pending(dev));
827
828 /*
829 * assume the HW is wedged and just free up
830 * all our pending work requests.
831 */
Roland Dreier21818582005-07-27 14:41:32 -0700832 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700834 (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +0200835 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 dev_kfree_skb_any(tx_req->skb);
837 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300838 --priv->tx_outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800841 for (i = 0; i < ipoib_recvq_size; ++i) {
842 struct ipoib_rx_buf *rx_req;
843
844 rx_req = &priv->rx_ring[i];
845 if (!rx_req->skb)
846 continue;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700847 ipoib_ud_dma_unmap_rx(priv,
848 priv->rx_ring[i].mapping);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800849 dev_kfree_skb_any(rx_req->skb);
850 rx_req->skb = NULL;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 goto timeout;
854 }
855
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300856 ipoib_drain_cq(dev);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 msleep(1);
859 }
860
861 ipoib_dbg(priv, "All sends and receives done.\n");
862
863timeout:
Eli Cohen57ce41d2008-04-30 20:02:45 -0700864 del_timer_sync(&priv->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800866 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
868
869 /* Wait for all AHs to be reaped */
870 set_bit(IPOIB_STOP_REAPER, &priv->flags);
871 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700872 if (flush)
873 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 begin = jiffies;
876
877 while (!list_empty(&priv->dead_ahs)) {
878 __ipoib_reap_ah(dev);
879
880 if (time_after(jiffies, begin + HZ)) {
881 ipoib_warn(priv, "timing out; will leak address handles\n");
882 break;
883 }
884
885 msleep(1);
886 }
887
Eli Cohenf56bcd82008-04-29 13:46:53 -0700888 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 0;
891}
892
893int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
894{
895 struct ipoib_dev_priv *priv = netdev_priv(dev);
896
897 priv->ca = ca;
898 priv->port = port;
899 priv->qp = NULL;
900
901 if (ipoib_transport_dev_init(dev, ca)) {
902 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
903 return -ENODEV;
904 }
905
Roland Dreier27678402008-10-10 15:58:52 -0700906 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
907 (unsigned long) dev);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (dev->flags & IFF_UP) {
910 if (ipoib_ib_dev_open(dev)) {
911 ipoib_transport_dev_cleanup(dev);
912 return -ENODEV;
913 }
914 }
915
916 return 0;
917}
918
Moni Shouaee1e2c82008-07-14 23:48:49 -0700919static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
920 enum ipoib_flush_level level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700922 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000923 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700924 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Yosef Etigin26bbf132007-05-19 08:51:54 -0700926 mutex_lock(&priv->vlan_mutex);
927
928 /*
929 * Flush any child interfaces too -- they might be up even if
930 * the parent is down.
931 */
932 list_for_each_entry(cpriv, &priv->child_intfs, list)
Moni Shouaee1e2c82008-07-14 23:48:49 -0700933 __ipoib_ib_dev_flush(cpriv, level);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700934
935 mutex_unlock(&priv->vlan_mutex);
936
937 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200938 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200940 }
941
942 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
943 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
944 return;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Moni Shouaee1e2c82008-07-14 23:48:49 -0700947 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700948 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
949 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
950 ipoib_ib_dev_down(dev, 0);
Jack Morgenstein167c4262008-02-13 16:23:50 +0200951 ipoib_ib_dev_stop(dev, 0);
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700952 if (ipoib_pkey_dev_delay_open(dev))
953 return;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700954 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700955
956 /* restart QP only if P_Key index is changed */
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700957 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
958 new_index == priv->pkey_index) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700959 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
960 return;
961 }
962 priv->pkey_index = new_index;
963 }
964
Moni Shouaee1e2c82008-07-14 23:48:49 -0700965 if (level == IPOIB_FLUSH_LIGHT) {
966 ipoib_mark_paths_invalid(dev);
967 ipoib_mcast_dev_flush(dev);
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Moni Shouaee1e2c82008-07-14 23:48:49 -0700970 if (level >= IPOIB_FLUSH_NORMAL)
971 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Moni Shouaee1e2c82008-07-14 23:48:49 -0700973 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700974 ipoib_ib_dev_stop(dev, 0);
975 ipoib_ib_dev_open(dev);
976 }
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * The device could have been brought down between the start and when
980 * we get here, don't bring it back up if it's not configured up
981 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700982 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700983 if (level >= IPOIB_FLUSH_NORMAL)
984 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000985 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700986 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700987}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Moni Shouaee1e2c82008-07-14 23:48:49 -0700989void ipoib_ib_dev_flush_light(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -0700990{
991 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -0700992 container_of(work, struct ipoib_dev_priv, flush_light);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800993
Moni Shouaee1e2c82008-07-14 23:48:49 -0700994 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700995}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -0800996
Moni Shouaee1e2c82008-07-14 23:48:49 -0700997void ipoib_ib_dev_flush_normal(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -0700998{
999 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -07001000 container_of(work, struct ipoib_dev_priv, flush_normal);
Yosef Etigin26bbf132007-05-19 08:51:54 -07001001
Moni Shouaee1e2c82008-07-14 23:48:49 -07001002 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1003}
1004
1005void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1006{
1007 struct ipoib_dev_priv *priv =
1008 container_of(work, struct ipoib_dev_priv, flush_heavy);
1009
1010 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013void ipoib_ib_dev_cleanup(struct net_device *dev)
1014{
1015 struct ipoib_dev_priv *priv = netdev_priv(dev);
1016
1017 ipoib_dbg(priv, "cleaning up ib_dev\n");
1018
Roland Dreier8d2cae02005-09-20 10:52:04 -07001019 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -08001020 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 ipoib_transport_dev_cleanup(dev);
1023}
1024
1025/*
1026 * Delayed P_Key Assigment Interim Support
1027 *
1028 * The following is initial implementation of delayed P_Key assigment
1029 * mechanism. It is using the same approach implemented for the multicast
1030 * group join. The single goal of this implementation is to quickly address
1031 * Bug #2507. This implementation will probably be removed when the P_Key
1032 * change async notification is available.
1033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
David Howellsc4028952006-11-22 14:57:56 +00001035void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
David Howellsc4028952006-11-22 14:57:56 +00001037 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -07001038 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +00001039 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 ipoib_pkey_dev_check_presence(dev);
1042
1043 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1044 ipoib_open(dev);
1045 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001046 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1048 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001049 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001051 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
1054
1055int ipoib_pkey_dev_delay_open(struct net_device *dev)
1056{
1057 struct ipoib_dev_priv *priv = netdev_priv(dev);
1058
1059 /* Look for the interface pkey value in the IB Port P_Key table and */
1060 /* set the interface pkey assigment flag */
1061 ipoib_pkey_dev_check_presence(dev);
1062
1063 /* P_Key value not assigned yet - start polling */
1064 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001065 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1067 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001068 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001070 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 1;
1072 }
1073
1074 return 0;
1075}