blob: dfa71903d6e467c59071e3cc01b04fea98c256b3 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eli Cohen40ca1982008-04-16 21:09:27 -070040#include <linux/ip.h>
41#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "ipoib.h"
44
45#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
46static int data_debug_level;
47
48module_param(data_debug_level, int, 0644);
49MODULE_PARM_DESC(data_debug_level,
50 "Enable data path debug tracing if > 0");
51#endif
52
Ingo Molnar95ed6442006-01-13 14:51:39 -080053static DEFINE_MUTEX(pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
56 struct ib_pd *pd, struct ib_ah_attr *attr)
57{
58 struct ipoib_ah *ah;
59
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
61 if (!ah)
62 return NULL;
63
64 ah->dev = dev;
65 ah->last_send = 0;
66 kref_init(&ah->ref);
67
68 ah->ah = ib_create_ah(pd, attr);
69 if (IS_ERR(ah->ah)) {
70 kfree(ah);
71 ah = NULL;
72 } else
73 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
74
75 return ah;
76}
77
78void ipoib_free_ah(struct kref *kref)
79{
80 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
81 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
82
83 unsigned long flags;
84
Roland Dreier31c02e22006-06-17 20:37:34 -070085 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Shirley Mabc7b3a32008-04-23 11:55:45 -070090static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
91 u64 mapping[IPOIB_UD_RX_SG])
92{
93 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
94 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
95 DMA_FROM_DEVICE);
96 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
97 DMA_FROM_DEVICE);
98 } else
99 ib_dma_unmap_single(priv->ca, mapping[0],
100 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
101 DMA_FROM_DEVICE);
102}
103
104static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
105 struct sk_buff *skb,
106 unsigned int length)
107{
108 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
109 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
110 unsigned int size;
111 /*
112 * There is only two buffers needed for max_payload = 4K,
113 * first buf size is IPOIB_UD_HEAD_SIZE
114 */
115 skb->tail += IPOIB_UD_HEAD_SIZE;
116 skb->len += length;
117
118 size = length - IPOIB_UD_HEAD_SIZE;
119
120 frag->size = size;
121 skb->data_len += size;
122 skb->truesize += size;
123 } else
124 skb_put(skb, length);
125
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int ipoib_ib_post_receive(struct net_device *dev, int id)
129{
130 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700131 struct ib_recv_wr *bad_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int ret;
133
Shirley Mabc7b3a32008-04-23 11:55:45 -0700134 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
135 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
136 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Shirley Mabc7b3a32008-04-23 11:55:45 -0700139 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
Roland Dreier1993d682005-10-28 15:30:34 -0700140 if (unlikely(ret)) {
141 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700142 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
Roland Dreier1993d682005-10-28 15:30:34 -0700143 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 priv->rx_ring[id].skb = NULL;
145 }
146
147 return ret;
148}
149
Shirley Mabc7b3a32008-04-23 11:55:45 -0700150static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
Roland Dreier1993d682005-10-28 15:30:34 -0700151{
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
153 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700154 int buf_size;
155 u64 *mapping;
Roland Dreier1993d682005-10-28 15:30:34 -0700156
Shirley Mabc7b3a32008-04-23 11:55:45 -0700157 if (ipoib_ud_need_sg(priv->max_ib_mtu))
158 buf_size = IPOIB_UD_HEAD_SIZE;
159 else
160 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
161
162 skb = dev_alloc_skb(buf_size + 4);
163 if (unlikely(!skb))
164 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700165
166 /*
167 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
168 * header. So we need 4 more bytes to get to 48 and align the
169 * IP header to a multiple of 16.
170 */
171 skb_reserve(skb, 4);
172
Shirley Mabc7b3a32008-04-23 11:55:45 -0700173 mapping = priv->rx_ring[id].mapping;
174 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
175 DMA_FROM_DEVICE);
176 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
177 goto error;
178
179 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
180 struct page *page = alloc_page(GFP_ATOMIC);
181 if (!page)
182 goto partial_error;
183 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
184 mapping[1] =
185 ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
186 0, PAGE_SIZE, DMA_FROM_DEVICE);
187 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
188 goto partial_error;
Roland Dreier1993d682005-10-28 15:30:34 -0700189 }
190
Shirley Mabc7b3a32008-04-23 11:55:45 -0700191 priv->rx_ring[id].skb = skb;
192 return skb;
Roland Dreier1993d682005-10-28 15:30:34 -0700193
Shirley Mabc7b3a32008-04-23 11:55:45 -0700194partial_error:
195 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
196error:
197 dev_kfree_skb_any(skb);
198 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int ipoib_ib_post_receives(struct net_device *dev)
202{
203 struct ipoib_dev_priv *priv = netdev_priv(dev);
204 int i;
205
Shirley Ma0f485252006-04-10 09:43:58 -0700206 for (i = 0; i < ipoib_recvq_size; ++i) {
Shirley Mabc7b3a32008-04-23 11:55:45 -0700207 if (!ipoib_alloc_rx_skb(dev, i)) {
Roland Dreier1993d682005-10-28 15:30:34 -0700208 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
209 return -ENOMEM;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (ipoib_ib_post_receive(dev, i)) {
212 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
213 return -EIO;
214 }
215 }
216
217 return 0;
218}
219
Roland Dreier2439a6e2006-09-22 15:22:52 -0700220static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
221{
222 struct ipoib_dev_priv *priv = netdev_priv(dev);
223 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
224 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700225 u64 mapping[IPOIB_UD_RX_SG];
Christoph Lameterfed1db32010-08-27 08:29:38 -0500226 union ib_gid *dgid;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700227
Roland Dreiera89875f2007-04-18 20:20:53 -0700228 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
229 wr_id, wc->status);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700230
231 if (unlikely(wr_id >= ipoib_recvq_size)) {
232 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
233 wr_id, ipoib_recvq_size);
234 return;
235 }
236
237 skb = priv->rx_ring[wr_id].skb;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700238
239 if (unlikely(wc->status != IB_WC_SUCCESS)) {
240 if (wc->status != IB_WC_WR_FLUSH_ERR)
241 ipoib_warn(priv, "failed recv event "
242 "(status=%d, wrid=%d vend_err %x)\n",
243 wc->status, wr_id, wc->vendor_err);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700244 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700245 dev_kfree_skb_any(skb);
246 priv->rx_ring[wr_id].skb = NULL;
247 return;
248 }
249
250 /*
Roland Dreier1b844af2007-07-10 13:43:53 -0700251 * Drop packets that this interface sent, ie multicast packets
252 * that the HCA has replicated.
253 */
254 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
255 goto repost;
256
Shirley Mabc7b3a32008-04-23 11:55:45 -0700257 memcpy(mapping, priv->rx_ring[wr_id].mapping,
258 IPOIB_UD_RX_SG * sizeof *mapping);
259
Roland Dreier1b844af2007-07-10 13:43:53 -0700260 /*
Roland Dreier2439a6e2006-09-22 15:22:52 -0700261 * If we can't allocate a new RX buffer, dump
262 * this packet and reuse the old buffer.
263 */
Shirley Mabc7b3a32008-04-23 11:55:45 -0700264 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
Roland Dreierde903512007-09-28 15:33:51 -0700265 ++dev->stats.rx_dropped;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700266 goto repost;
267 }
268
269 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
270 wc->byte_len, wc->slid);
271
Shirley Mabc7b3a32008-04-23 11:55:45 -0700272 ipoib_ud_dma_unmap_rx(priv, mapping);
273 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700274
Christoph Lameterfed1db32010-08-27 08:29:38 -0500275 /* First byte of dgid signals multicast when 0xff */
276 dgid = &((struct ib_grh *)skb->data)->dgid;
277
278 if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
279 skb->pkt_type = PACKET_HOST;
280 else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
281 skb->pkt_type = PACKET_BROADCAST;
282 else
283 skb->pkt_type = PACKET_MULTICAST;
284
Roland Dreier2439a6e2006-09-22 15:22:52 -0700285 skb_pull(skb, IB_GRH_BYTES);
286
Roland Dreier1b844af2007-07-10 13:43:53 -0700287 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
288 skb_reset_mac_header(skb);
289 skb_pull(skb, IPOIB_ENCAP_LEN);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700290
Roland Dreierde903512007-09-28 15:33:51 -0700291 ++dev->stats.rx_packets;
292 dev->stats.rx_bytes += skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700293
Roland Dreier1b844af2007-07-10 13:43:53 -0700294 skb->dev = dev;
Eli Cohen60461362008-04-16 21:01:10 -0700295 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
296 skb->ip_summed = CHECKSUM_UNNECESSARY;
297
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -0700298 if (dev->features & NETIF_F_LRO)
299 lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
300 else
301 netif_receive_skb(skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700302
303repost:
304 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
305 ipoib_warn(priv, "ipoib_ib_post_receive failed "
306 "for buf %d\n", wr_id);
307}
308
Eli Cohen71437402008-01-30 18:30:53 +0200309static int ipoib_dma_map_tx(struct ib_device *ca,
310 struct ipoib_tx_buf *tx_req)
311{
312 struct sk_buff *skb = tx_req->skb;
313 u64 *mapping = tx_req->mapping;
314 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700315 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200316
Eli Cohen40ca1982008-04-16 21:09:27 -0700317 if (skb_headlen(skb)) {
318 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
319 DMA_TO_DEVICE);
320 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
321 return -EIO;
322
323 off = 1;
324 } else
325 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200326
327 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
328 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Eli Cohen40ca1982008-04-16 21:09:27 -0700329 mapping[i + off] = ib_dma_map_page(ca, frag->page,
Eli Cohen71437402008-01-30 18:30:53 +0200330 frag->page_offset, frag->size,
331 DMA_TO_DEVICE);
Eli Cohen40ca1982008-04-16 21:09:27 -0700332 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
Eli Cohen71437402008-01-30 18:30:53 +0200333 goto partial_error;
334 }
335 return 0;
336
337partial_error:
Eli Cohen71437402008-01-30 18:30:53 +0200338 for (; i > 0; --i) {
339 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
Eli Cohen40ca1982008-04-16 21:09:27 -0700340 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
Eli Cohen71437402008-01-30 18:30:53 +0200341 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700342
343 if (off)
344 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
345
Eli Cohen71437402008-01-30 18:30:53 +0200346 return -EIO;
347}
348
349static void ipoib_dma_unmap_tx(struct ib_device *ca,
350 struct ipoib_tx_buf *tx_req)
351{
352 struct sk_buff *skb = tx_req->skb;
353 u64 *mapping = tx_req->mapping;
354 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700355 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200356
Eli Cohen40ca1982008-04-16 21:09:27 -0700357 if (skb_headlen(skb)) {
358 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
359 off = 1;
360 } else
361 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200362
363 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
364 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Eli Cohen40ca1982008-04-16 21:09:27 -0700365 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
Eli Cohen71437402008-01-30 18:30:53 +0200366 DMA_TO_DEVICE);
367 }
368}
369
Roland Dreier2439a6e2006-09-22 15:22:52 -0700370static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct ipoib_dev_priv *priv = netdev_priv(dev);
373 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700374 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Roland Dreiera89875f2007-04-18 20:20:53 -0700376 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
377 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Roland Dreier2439a6e2006-09-22 15:22:52 -0700379 if (unlikely(wr_id >= ipoib_sendq_size)) {
380 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
381 wr_id, ipoib_sendq_size);
382 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700384
385 tx_req = &priv->tx_ring[wr_id];
386
Eli Cohen71437402008-01-30 18:30:53 +0200387 ipoib_dma_unmap_tx(priv->ca, tx_req);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700388
Roland Dreierde903512007-09-28 15:33:51 -0700389 ++dev->stats.tx_packets;
390 dev->stats.tx_bytes += tx_req->skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700391
392 dev_kfree_skb_any(tx_req->skb);
393
Roland Dreier2439a6e2006-09-22 15:22:52 -0700394 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300395 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
396 netif_queue_stopped(dev) &&
397 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Roland Dreier2439a6e2006-09-22 15:22:52 -0700398 netif_wake_queue(dev);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700399
400 if (wc->status != IB_WC_SUCCESS &&
401 wc->status != IB_WC_WR_FLUSH_ERR)
402 ipoib_warn(priv, "failed send event "
403 "(status=%d, wrid=%d vend_err %x)\n",
404 wc->status, wr_id, wc->vendor_err);
405}
406
Eli Cohenf56bcd82008-04-29 13:46:53 -0700407static int poll_tx(struct ipoib_dev_priv *priv)
408{
409 int n, i;
410
411 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
412 for (i = 0; i < n; ++i)
413 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
414
415 return n == MAX_SEND_CQE;
416}
417
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700418int ipoib_poll(struct napi_struct *napi, int budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700419{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700420 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
421 struct net_device *dev = priv->dev;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700422 int done;
423 int t;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700424 int n, i;
425
426 done = 0;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700427
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700428poll_more:
429 while (done < budget) {
430 int max = (budget - done);
431
Roland Dreier8d1cc862007-05-06 21:05:32 -0700432 t = min(IPOIB_NUM_WC, max);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700433 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700434
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700435 for (i = 0; i < n; i++) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700436 struct ib_wc *wc = priv->ibwc + i;
437
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300438 if (wc->wr_id & IPOIB_OP_RECV) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700439 ++done;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300440 if (wc->wr_id & IPOIB_OP_CM)
441 ipoib_cm_handle_rx_wc(dev, wc);
442 else
443 ipoib_ib_handle_rx_wc(dev, wc);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700444 } else
445 ipoib_cm_handle_tx_wc(priv->dev, wc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700446 }
447
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700448 if (n != t)
Roland Dreier8d1cc862007-05-06 21:05:32 -0700449 break;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700450 }
451
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700452 if (done < budget) {
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -0700453 if (dev->features & NETIF_F_LRO)
454 lro_flush_all(&priv->lro.lro_mgr);
455
Ben Hutchings288379f2009-01-19 16:43:59 -0800456 napi_complete(napi);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700457 if (unlikely(ib_req_notify_cq(priv->recv_cq,
Roland Dreier8d1cc862007-05-06 21:05:32 -0700458 IB_CQ_NEXT_COMP |
459 IB_CQ_REPORT_MISSED_EVENTS)) &&
Ben Hutchings288379f2009-01-19 16:43:59 -0800460 napi_reschedule(napi))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700461 goto poll_more;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700462 }
463
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700464 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
468{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700469 struct net_device *dev = dev_ptr;
470 struct ipoib_dev_priv *priv = netdev_priv(dev);
471
Ben Hutchings288379f2009-01-19 16:43:59 -0800472 napi_schedule(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Eli Cohen57ce41d2008-04-30 20:02:45 -0700475static void drain_tx_cq(struct net_device *dev)
476{
477 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700478
Roland Dreier943c2462008-09-30 10:36:21 -0700479 netif_tx_lock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700480 while (poll_tx(priv))
481 ; /* nothing */
482
483 if (netif_queue_stopped(dev))
484 mod_timer(&priv->poll_timer, jiffies + 1);
485
Roland Dreier943c2462008-09-30 10:36:21 -0700486 netif_tx_unlock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700487}
488
489void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
490{
Roland Dreier943c2462008-09-30 10:36:21 -0700491 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
492
493 mod_timer(&priv->poll_timer, jiffies);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static inline int post_send(struct ipoib_dev_priv *priv,
497 unsigned int wr_id,
498 struct ib_ah *address, u32 qpn,
Eli Cohen40ca1982008-04-16 21:09:27 -0700499 struct ipoib_tx_buf *tx_req,
500 void *head, int hlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct ib_send_wr *bad_wr;
Eli Cohen40ca1982008-04-16 21:09:27 -0700503 int i, off;
504 struct sk_buff *skb = tx_req->skb;
505 skb_frag_t *frags = skb_shinfo(skb)->frags;
506 int nr_frags = skb_shinfo(skb)->nr_frags;
507 u64 *mapping = tx_req->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Eli Cohen40ca1982008-04-16 21:09:27 -0700509 if (skb_headlen(skb)) {
510 priv->tx_sge[0].addr = mapping[0];
511 priv->tx_sge[0].length = skb_headlen(skb);
512 off = 1;
513 } else
514 off = 0;
515
Eli Cohen71437402008-01-30 18:30:53 +0200516 for (i = 0; i < nr_frags; ++i) {
Eli Cohen40ca1982008-04-16 21:09:27 -0700517 priv->tx_sge[i + off].addr = mapping[i + off];
518 priv->tx_sge[i + off].length = frags[i].size;
Eli Cohen71437402008-01-30 18:30:53 +0200519 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700520 priv->tx_wr.num_sge = nr_frags + off;
Eli Cohen71437402008-01-30 18:30:53 +0200521 priv->tx_wr.wr_id = wr_id;
522 priv->tx_wr.wr.ud.remote_qpn = qpn;
523 priv->tx_wr.wr.ud.ah = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Eli Cohen40ca1982008-04-16 21:09:27 -0700525 if (head) {
526 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
527 priv->tx_wr.wr.ud.header = head;
528 priv->tx_wr.wr.ud.hlen = hlen;
529 priv->tx_wr.opcode = IB_WR_LSO;
530 } else
531 priv->tx_wr.opcode = IB_WR_SEND;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
534}
535
536void ipoib_send(struct net_device *dev, struct sk_buff *skb,
537 struct ipoib_ah *address, u32 qpn)
538{
539 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700540 struct ipoib_tx_buf *tx_req;
Or Gerlitza48f5092010-03-04 13:17:37 +0000541 int hlen, rc;
Eli Cohen40ca1982008-04-16 21:09:27 -0700542 void *phead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Eli Cohen40ca1982008-04-16 21:09:27 -0700544 if (skb_is_gso(skb)) {
545 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
546 phead = skb->data;
547 if (unlikely(!skb_pull(skb, hlen))) {
548 ipoib_warn(priv, "linear data too small\n");
549 ++dev->stats.tx_dropped;
550 ++dev->stats.tx_errors;
551 dev_kfree_skb_any(skb);
552 return;
553 }
554 } else {
555 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
556 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
557 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
558 ++dev->stats.tx_dropped;
559 ++dev->stats.tx_errors;
560 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
561 return;
562 }
563 phead = NULL;
564 hlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
568 skb->len, address, qpn);
569
570 /*
571 * We put the skb into the tx_ring _before_ we call post_send()
572 * because it's entirely possible that the completion handler will
573 * run before we execute anything after the post_send(). That
574 * means we have to make sure everything is properly recorded and
575 * our state is consistent before we call post_send().
576 */
Shirley Ma0f485252006-04-10 09:43:58 -0700577 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 tx_req->skb = skb;
Eli Cohen71437402008-01-30 18:30:53 +0200579 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700580 ++dev->stats.tx_errors;
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700581 dev_kfree_skb_any(skb);
582 return;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Eli Cohen60461362008-04-16 21:01:10 -0700585 if (skb->ip_summed == CHECKSUM_PARTIAL)
586 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
587 else
588 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
589
Eli Cohen57ce41d2008-04-30 20:02:45 -0700590 if (++priv->tx_outstanding == ipoib_sendq_size) {
591 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
592 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
593 ipoib_warn(priv, "request notify on send CQ failed\n");
594 netif_stop_queue(dev);
595 }
596
Or Gerlitza48f5092010-03-04 13:17:37 +0000597 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
598 address->ah, qpn, tx_req, phead, hlen);
599 if (unlikely(rc)) {
600 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700601 ++dev->stats.tx_errors;
Eli Cohen57ce41d2008-04-30 20:02:45 -0700602 --priv->tx_outstanding;
Eli Cohen71437402008-01-30 18:30:53 +0200603 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 dev_kfree_skb_any(skb);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700605 if (netif_queue_stopped(dev))
606 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 } else {
608 dev->trans_start = jiffies;
609
610 address->last_send = priv->tx_head;
611 ++priv->tx_head;
Eli Cohenf56bcd82008-04-29 13:46:53 -0700612 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Eli Cohenf56bcd82008-04-29 13:46:53 -0700615
616 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
Eli Cohen57ce41d2008-04-30 20:02:45 -0700617 while (poll_tx(priv))
618 ; /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static void __ipoib_reap_ah(struct net_device *dev)
622{
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 struct ipoib_ah *ah, *tah;
625 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700626 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Roland Dreier943c2462008-09-30 10:36:21 -0700628 netif_tx_lock_bh(dev);
629 spin_lock_irqsave(&priv->lock, flags);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700632 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700634 ib_destroy_ah(ah->ah);
635 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Roland Dreier943c2462008-09-30 10:36:21 -0700637
638 spin_unlock_irqrestore(&priv->lock, flags);
639 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
David Howellsc4028952006-11-22 14:57:56 +0000642void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
David Howellsc4028952006-11-22 14:57:56 +0000644 struct ipoib_dev_priv *priv =
645 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
646 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 __ipoib_reap_ah(dev);
649
650 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
Anton Blanchard69fc5072007-10-15 00:50:56 -0500651 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
652 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Eli Cohen57ce41d2008-04-30 20:02:45 -0700655static void ipoib_ib_tx_timer_func(unsigned long ctx)
656{
657 drain_tx_cq((struct net_device *)ctx);
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660int ipoib_ib_dev_open(struct net_device *dev)
661{
662 struct ipoib_dev_priv *priv = netdev_priv(dev);
663 int ret;
664
Yosef Etigin26bbf132007-05-19 08:51:54 -0700665 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
666 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
667 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
668 return -1;
669 }
670 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
671
Roland Dreier5b6810e2005-10-11 11:08:24 -0700672 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700674 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -1;
676 }
677
678 ret = ipoib_ib_post_receives(dev);
679 if (ret) {
680 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700681 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -1;
683 }
684
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200685 ret = ipoib_cm_dev_open(dev);
686 if (ret) {
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300687 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700688 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200689 return -1;
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
Anton Blanchard69fc5072007-10-15 00:50:56 -0500693 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
694 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Yossi Etigine028cc52009-04-20 13:58:08 -0700696 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
697 napi_enable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700}
701
Leonid Arsh7a343d42006-03-23 19:52:51 +0200702static void ipoib_pkey_dev_check_presence(struct net_device *dev)
703{
704 struct ipoib_dev_priv *priv = netdev_priv(dev);
705 u16 pkey_index = 0;
706
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700707 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
Leonid Arsh7a343d42006-03-23 19:52:51 +0200708 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
709 else
710 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713int ipoib_ib_dev_up(struct net_device *dev)
714{
715 struct ipoib_dev_priv *priv = netdev_priv(dev);
716
Leonid Arsh7a343d42006-03-23 19:52:51 +0200717 ipoib_pkey_dev_check_presence(dev);
718
719 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
720 ipoib_dbg(priv, "PKEY is not assigned.\n");
721 return 0;
722 }
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
725
726 return ipoib_mcast_start_thread(dev);
727}
728
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800729int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct ipoib_dev_priv *priv = netdev_priv(dev);
732
733 ipoib_dbg(priv, "downing ib_dev\n");
734
735 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
736 netif_carrier_off(dev);
737
738 /* Shutdown the P_Key thread if still active */
739 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800740 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700742 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800743 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800744 if (flush)
745 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800748 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ipoib_mcast_dev_flush(dev);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ipoib_flush_paths(dev);
752
753 return 0;
754}
755
756static int recvs_pending(struct net_device *dev)
757{
758 struct ipoib_dev_priv *priv = netdev_priv(dev);
759 int pending = 0;
760 int i;
761
Shirley Ma0f485252006-04-10 09:43:58 -0700762 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (priv->rx_ring[i].skb)
764 ++pending;
765
766 return pending;
767}
768
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300769void ipoib_drain_cq(struct net_device *dev)
770{
771 struct ipoib_dev_priv *priv = netdev_priv(dev);
772 int i, n;
Roland Dreier943c2462008-09-30 10:36:21 -0700773
774 /*
775 * We call completion handling routines that expect to be
776 * called from the BH-disabled NAPI poll context, so disable
777 * BHs here too.
778 */
779 local_bh_disable();
780
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300781 do {
Eli Cohenf56bcd82008-04-29 13:46:53 -0700782 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300783 for (i = 0; i < n; ++i) {
Roland Dreierce423ef2007-10-09 19:59:04 -0700784 /*
785 * Convert any successful completions to flush
786 * errors to avoid passing packets up the
787 * stack after bringing the device down.
788 */
789 if (priv->ibwc[i].status == IB_WC_SUCCESS)
790 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
791
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300792 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
793 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
794 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
795 else
796 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700797 } else
798 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300799 }
800 } while (n == IPOIB_NUM_WC);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700801
802 while (poll_tx(priv))
803 ; /* nothing */
Roland Dreier943c2462008-09-30 10:36:21 -0700804
805 local_bh_enable();
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300806}
807
Yosef Etigin26bbf132007-05-19 08:51:54 -0700808int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct ipoib_dev_priv *priv = netdev_priv(dev);
811 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700813 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300814 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Yossi Etigine028cc52009-04-20 13:58:08 -0700816 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
817 napi_disable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200818
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200819 ipoib_cm_dev_stop(dev);
820
Roland Dreier3bc12e72005-10-30 13:20:09 -0800821 /*
822 * Move our QP to the error state and then reinitialize in
823 * when all work requests have completed or have been flushed.
824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800826 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
828
829 /* Wait for all sends and receives to complete */
830 begin = jiffies;
831
832 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
833 if (time_after(jiffies, begin + 5 * HZ)) {
834 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
835 priv->tx_head - priv->tx_tail, recvs_pending(dev));
836
837 /*
838 * assume the HW is wedged and just free up
839 * all our pending work requests.
840 */
Roland Dreier21818582005-07-27 14:41:32 -0700841 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700843 (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +0200844 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 dev_kfree_skb_any(tx_req->skb);
846 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300847 --priv->tx_outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800850 for (i = 0; i < ipoib_recvq_size; ++i) {
851 struct ipoib_rx_buf *rx_req;
852
853 rx_req = &priv->rx_ring[i];
854 if (!rx_req->skb)
855 continue;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700856 ipoib_ud_dma_unmap_rx(priv,
857 priv->rx_ring[i].mapping);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800858 dev_kfree_skb_any(rx_req->skb);
859 rx_req->skb = NULL;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 goto timeout;
863 }
864
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300865 ipoib_drain_cq(dev);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 msleep(1);
868 }
869
870 ipoib_dbg(priv, "All sends and receives done.\n");
871
872timeout:
Eli Cohen57ce41d2008-04-30 20:02:45 -0700873 del_timer_sync(&priv->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800875 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
877
878 /* Wait for all AHs to be reaped */
879 set_bit(IPOIB_STOP_REAPER, &priv->flags);
880 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700881 if (flush)
882 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 begin = jiffies;
885
886 while (!list_empty(&priv->dead_ahs)) {
887 __ipoib_reap_ah(dev);
888
889 if (time_after(jiffies, begin + HZ)) {
890 ipoib_warn(priv, "timing out; will leak address handles\n");
891 break;
892 }
893
894 msleep(1);
895 }
896
Eli Cohenf56bcd82008-04-29 13:46:53 -0700897 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
900}
901
902int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
903{
904 struct ipoib_dev_priv *priv = netdev_priv(dev);
905
906 priv->ca = ca;
907 priv->port = port;
908 priv->qp = NULL;
909
910 if (ipoib_transport_dev_init(dev, ca)) {
911 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
912 return -ENODEV;
913 }
914
Roland Dreier27678402008-10-10 15:58:52 -0700915 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
916 (unsigned long) dev);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (dev->flags & IFF_UP) {
919 if (ipoib_ib_dev_open(dev)) {
920 ipoib_transport_dev_cleanup(dev);
921 return -ENODEV;
922 }
923 }
924
925 return 0;
926}
927
Moni Shouaee1e2c82008-07-14 23:48:49 -0700928static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
929 enum ipoib_flush_level level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700931 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000932 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700933 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Yosef Etigin26bbf132007-05-19 08:51:54 -0700935 mutex_lock(&priv->vlan_mutex);
936
937 /*
938 * Flush any child interfaces too -- they might be up even if
939 * the parent is down.
940 */
941 list_for_each_entry(cpriv, &priv->child_intfs, list)
Moni Shouaee1e2c82008-07-14 23:48:49 -0700942 __ipoib_ib_dev_flush(cpriv, level);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700943
944 mutex_unlock(&priv->vlan_mutex);
945
946 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200947 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200949 }
950
951 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
952 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
953 return;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Moni Shouaee1e2c82008-07-14 23:48:49 -0700956 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700957 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
958 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
959 ipoib_ib_dev_down(dev, 0);
Jack Morgenstein167c4262008-02-13 16:23:50 +0200960 ipoib_ib_dev_stop(dev, 0);
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700961 if (ipoib_pkey_dev_delay_open(dev))
962 return;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700963 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700964
965 /* restart QP only if P_Key index is changed */
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700966 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
967 new_index == priv->pkey_index) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700968 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
969 return;
970 }
971 priv->pkey_index = new_index;
972 }
973
Moni Shouaee1e2c82008-07-14 23:48:49 -0700974 if (level == IPOIB_FLUSH_LIGHT) {
975 ipoib_mark_paths_invalid(dev);
976 ipoib_mcast_dev_flush(dev);
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Moni Shouaee1e2c82008-07-14 23:48:49 -0700979 if (level >= IPOIB_FLUSH_NORMAL)
980 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Moni Shouaee1e2c82008-07-14 23:48:49 -0700982 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700983 ipoib_ib_dev_stop(dev, 0);
984 ipoib_ib_dev_open(dev);
985 }
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /*
988 * The device could have been brought down between the start and when
989 * we get here, don't bring it back up if it's not configured up
990 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700991 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700992 if (level >= IPOIB_FLUSH_NORMAL)
993 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000994 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700995 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700996}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Moni Shouaee1e2c82008-07-14 23:48:49 -0700998void ipoib_ib_dev_flush_light(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -0700999{
1000 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -07001001 container_of(work, struct ipoib_dev_priv, flush_light);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -08001002
Moni Shouaee1e2c82008-07-14 23:48:49 -07001003 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
Yosef Etigin26bbf132007-05-19 08:51:54 -07001004}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -08001005
Moni Shouaee1e2c82008-07-14 23:48:49 -07001006void ipoib_ib_dev_flush_normal(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -07001007{
1008 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -07001009 container_of(work, struct ipoib_dev_priv, flush_normal);
Yosef Etigin26bbf132007-05-19 08:51:54 -07001010
Moni Shouaee1e2c82008-07-14 23:48:49 -07001011 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1012}
1013
1014void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1015{
1016 struct ipoib_dev_priv *priv =
1017 container_of(work, struct ipoib_dev_priv, flush_heavy);
1018
1019 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022void ipoib_ib_dev_cleanup(struct net_device *dev)
1023{
1024 struct ipoib_dev_priv *priv = netdev_priv(dev);
1025
1026 ipoib_dbg(priv, "cleaning up ib_dev\n");
1027
Roland Dreier8d2cae02005-09-20 10:52:04 -07001028 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -08001029 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 ipoib_transport_dev_cleanup(dev);
1032}
1033
1034/*
1035 * Delayed P_Key Assigment Interim Support
1036 *
1037 * The following is initial implementation of delayed P_Key assigment
1038 * mechanism. It is using the same approach implemented for the multicast
1039 * group join. The single goal of this implementation is to quickly address
1040 * Bug #2507. This implementation will probably be removed when the P_Key
1041 * change async notification is available.
1042 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
David Howellsc4028952006-11-22 14:57:56 +00001044void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
David Howellsc4028952006-11-22 14:57:56 +00001046 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -07001047 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +00001048 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 ipoib_pkey_dev_check_presence(dev);
1051
1052 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1053 ipoib_open(dev);
1054 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001055 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1057 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001058 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001060 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062}
1063
1064int ipoib_pkey_dev_delay_open(struct net_device *dev)
1065{
1066 struct ipoib_dev_priv *priv = netdev_priv(dev);
1067
1068 /* Look for the interface pkey value in the IB Port P_Key table and */
1069 /* set the interface pkey assigment flag */
1070 ipoib_pkey_dev_check_presence(dev);
1071
1072 /* P_Key value not assigned yet - start polling */
1073 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001074 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1076 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001077 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001079 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 1;
1081 }
1082
1083 return 0;
1084}