blob: 4115be54ba3b32626dc75c4529a3aa186b6f02c1 [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>
Paul Gortmakerfec14d22011-08-30 12:32:52 -040037#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Eli Cohen40ca1982008-04-16 21:09:27 -070041#include <linux/ip.h>
42#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "ipoib.h"
45
46#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
47static int data_debug_level;
48
49module_param(data_debug_level, int, 0644);
50MODULE_PARM_DESC(data_debug_level,
51 "Enable data path debug tracing if > 0");
52#endif
53
Ingo Molnar95ed6442006-01-13 14:51:39 -080054static DEFINE_MUTEX(pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
57 struct ib_pd *pd, struct ib_ah_attr *attr)
58{
59 struct ipoib_ah *ah;
Mike Marciniszyn38743972011-11-21 08:43:54 -050060 struct ib_ah *vah;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 ah = kmalloc(sizeof *ah, GFP_KERNEL);
63 if (!ah)
Mike Marciniszyn38743972011-11-21 08:43:54 -050064 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 ah->dev = dev;
67 ah->last_send = 0;
68 kref_init(&ah->ref);
69
Mike Marciniszyn38743972011-11-21 08:43:54 -050070 vah = ib_create_ah(pd, attr);
71 if (IS_ERR(vah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 kfree(ah);
Mike Marciniszyn38743972011-11-21 08:43:54 -050073 ah = (struct ipoib_ah *)vah;
74 } else {
75 ah->ah = vah;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
Mike Marciniszyn38743972011-11-21 08:43:54 -050077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 return ah;
80}
81
82void ipoib_free_ah(struct kref *kref)
83{
84 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
85 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
86
87 unsigned long flags;
88
Roland Dreier31c02e22006-06-17 20:37:34 -070089 spin_lock_irqsave(&priv->lock, flags);
90 list_add_tail(&ah->list, &priv->dead_ahs);
91 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Shirley Mabc7b3a32008-04-23 11:55:45 -070094static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
95 u64 mapping[IPOIB_UD_RX_SG])
96{
97 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
98 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
99 DMA_FROM_DEVICE);
100 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
101 DMA_FROM_DEVICE);
102 } else
103 ib_dma_unmap_single(priv->ca, mapping[0],
104 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
105 DMA_FROM_DEVICE);
106}
107
108static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
109 struct sk_buff *skb,
110 unsigned int length)
111{
112 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
113 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
114 unsigned int size;
115 /*
116 * There is only two buffers needed for max_payload = 4K,
117 * first buf size is IPOIB_UD_HEAD_SIZE
118 */
119 skb->tail += IPOIB_UD_HEAD_SIZE;
120 skb->len += length;
121
122 size = length - IPOIB_UD_HEAD_SIZE;
123
Eric Dumazet9e903e02011-10-18 21:00:24 +0000124 skb_frag_size_set(frag, size);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700125 skb->data_len += size;
126 skb->truesize += size;
127 } else
128 skb_put(skb, length);
129
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int ipoib_ib_post_receive(struct net_device *dev, int id)
133{
134 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700135 struct ib_recv_wr *bad_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int ret;
137
Shirley Mabc7b3a32008-04-23 11:55:45 -0700138 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
139 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
140 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Shirley Mabc7b3a32008-04-23 11:55:45 -0700143 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
Roland Dreier1993d682005-10-28 15:30:34 -0700144 if (unlikely(ret)) {
145 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700146 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
Roland Dreier1993d682005-10-28 15:30:34 -0700147 dev_kfree_skb_any(priv->rx_ring[id].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 priv->rx_ring[id].skb = NULL;
149 }
150
151 return ret;
152}
153
Shirley Mabc7b3a32008-04-23 11:55:45 -0700154static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
Roland Dreier1993d682005-10-28 15:30:34 -0700155{
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700158 int buf_size;
159 u64 *mapping;
Roland Dreier1993d682005-10-28 15:30:34 -0700160
Shirley Mabc7b3a32008-04-23 11:55:45 -0700161 if (ipoib_ud_need_sg(priv->max_ib_mtu))
162 buf_size = IPOIB_UD_HEAD_SIZE;
163 else
164 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
165
166 skb = dev_alloc_skb(buf_size + 4);
167 if (unlikely(!skb))
168 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700169
170 /*
171 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
172 * header. So we need 4 more bytes to get to 48 and align the
173 * IP header to a multiple of 16.
174 */
175 skb_reserve(skb, 4);
176
Shirley Mabc7b3a32008-04-23 11:55:45 -0700177 mapping = priv->rx_ring[id].mapping;
178 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
179 DMA_FROM_DEVICE);
180 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
181 goto error;
182
183 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
184 struct page *page = alloc_page(GFP_ATOMIC);
185 if (!page)
186 goto partial_error;
187 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
188 mapping[1] =
Ian Campbell5581be32011-08-24 22:28:10 +0000189 ib_dma_map_page(priv->ca, page,
Shirley Mabc7b3a32008-04-23 11:55:45 -0700190 0, PAGE_SIZE, DMA_FROM_DEVICE);
191 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
192 goto partial_error;
Roland Dreier1993d682005-10-28 15:30:34 -0700193 }
194
Shirley Mabc7b3a32008-04-23 11:55:45 -0700195 priv->rx_ring[id].skb = skb;
196 return skb;
Roland Dreier1993d682005-10-28 15:30:34 -0700197
Shirley Mabc7b3a32008-04-23 11:55:45 -0700198partial_error:
199 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
200error:
201 dev_kfree_skb_any(skb);
202 return NULL;
Roland Dreier1993d682005-10-28 15:30:34 -0700203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int ipoib_ib_post_receives(struct net_device *dev)
206{
207 struct ipoib_dev_priv *priv = netdev_priv(dev);
208 int i;
209
Shirley Ma0f485252006-04-10 09:43:58 -0700210 for (i = 0; i < ipoib_recvq_size; ++i) {
Shirley Mabc7b3a32008-04-23 11:55:45 -0700211 if (!ipoib_alloc_rx_skb(dev, i)) {
Roland Dreier1993d682005-10-28 15:30:34 -0700212 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
213 return -ENOMEM;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (ipoib_ib_post_receive(dev, i)) {
216 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
217 return -EIO;
218 }
219 }
220
221 return 0;
222}
223
Roland Dreier2439a6e2006-09-22 15:22:52 -0700224static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
225{
226 struct ipoib_dev_priv *priv = netdev_priv(dev);
227 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
228 struct sk_buff *skb;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700229 u64 mapping[IPOIB_UD_RX_SG];
Christoph Lameterfed1db32010-08-27 08:29:38 -0500230 union ib_gid *dgid;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700231
Roland Dreiera89875f2007-04-18 20:20:53 -0700232 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
233 wr_id, wc->status);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700234
235 if (unlikely(wr_id >= ipoib_recvq_size)) {
236 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
237 wr_id, ipoib_recvq_size);
238 return;
239 }
240
241 skb = priv->rx_ring[wr_id].skb;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700242
243 if (unlikely(wc->status != IB_WC_SUCCESS)) {
244 if (wc->status != IB_WC_WR_FLUSH_ERR)
245 ipoib_warn(priv, "failed recv event "
246 "(status=%d, wrid=%d vend_err %x)\n",
247 wc->status, wr_id, wc->vendor_err);
Shirley Mabc7b3a32008-04-23 11:55:45 -0700248 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700249 dev_kfree_skb_any(skb);
250 priv->rx_ring[wr_id].skb = NULL;
251 return;
252 }
253
254 /*
Roland Dreier1b844af2007-07-10 13:43:53 -0700255 * Drop packets that this interface sent, ie multicast packets
256 * that the HCA has replicated.
257 */
258 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
259 goto repost;
260
Shirley Mabc7b3a32008-04-23 11:55:45 -0700261 memcpy(mapping, priv->rx_ring[wr_id].mapping,
262 IPOIB_UD_RX_SG * sizeof *mapping);
263
Roland Dreier1b844af2007-07-10 13:43:53 -0700264 /*
Roland Dreier2439a6e2006-09-22 15:22:52 -0700265 * If we can't allocate a new RX buffer, dump
266 * this packet and reuse the old buffer.
267 */
Shirley Mabc7b3a32008-04-23 11:55:45 -0700268 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
Roland Dreierde903512007-09-28 15:33:51 -0700269 ++dev->stats.rx_dropped;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700270 goto repost;
271 }
272
273 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
274 wc->byte_len, wc->slid);
275
Shirley Mabc7b3a32008-04-23 11:55:45 -0700276 ipoib_ud_dma_unmap_rx(priv, mapping);
277 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700278
Christoph Lameterfed1db32010-08-27 08:29:38 -0500279 /* First byte of dgid signals multicast when 0xff */
280 dgid = &((struct ib_grh *)skb->data)->dgid;
281
282 if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
283 skb->pkt_type = PACKET_HOST;
284 else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
285 skb->pkt_type = PACKET_BROADCAST;
286 else
287 skb->pkt_type = PACKET_MULTICAST;
288
Roland Dreier2439a6e2006-09-22 15:22:52 -0700289 skb_pull(skb, IB_GRH_BYTES);
290
Roland Dreier1b844af2007-07-10 13:43:53 -0700291 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
292 skb_reset_mac_header(skb);
293 skb_pull(skb, IPOIB_ENCAP_LEN);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700294
Roland Dreierde903512007-09-28 15:33:51 -0700295 ++dev->stats.rx_packets;
296 dev->stats.rx_bytes += skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700297
Roland Dreier1b844af2007-07-10 13:43:53 -0700298 skb->dev = dev;
Michał Mirosław3d96c742011-04-19 00:43:20 +0000299 if ((dev->features & NETIF_F_RXCSUM) && likely(wc->csum_ok))
Eli Cohen60461362008-04-16 21:01:10 -0700300 skb->ip_summed = CHECKSUM_UNNECESSARY;
301
Or Gerlitz8ae31e52011-01-10 17:41:55 -0800302 napi_gro_receive(&priv->napi, skb);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700303
304repost:
305 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
306 ipoib_warn(priv, "ipoib_ib_post_receive failed "
307 "for buf %d\n", wr_id);
308}
309
Eli Cohen71437402008-01-30 18:30:53 +0200310static int ipoib_dma_map_tx(struct ib_device *ca,
311 struct ipoib_tx_buf *tx_req)
312{
313 struct sk_buff *skb = tx_req->skb;
314 u64 *mapping = tx_req->mapping;
315 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700316 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200317
Eli Cohen40ca1982008-04-16 21:09:27 -0700318 if (skb_headlen(skb)) {
319 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
320 DMA_TO_DEVICE);
321 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
322 return -EIO;
323
324 off = 1;
325 } else
326 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200327
328 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000329 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Ian Campbell5581be32011-08-24 22:28:10 +0000330 mapping[i + off] = ib_dma_map_page(ca,
331 skb_frag_page(frag),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000332 frag->page_offset, skb_frag_size(frag),
Eli Cohen71437402008-01-30 18:30:53 +0200333 DMA_TO_DEVICE);
Eli Cohen40ca1982008-04-16 21:09:27 -0700334 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
Eli Cohen71437402008-01-30 18:30:53 +0200335 goto partial_error;
336 }
337 return 0;
338
339partial_error:
Eli Cohen71437402008-01-30 18:30:53 +0200340 for (; i > 0; --i) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000341 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
342
343 ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
Eli Cohen71437402008-01-30 18:30:53 +0200344 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700345
346 if (off)
347 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
348
Eli Cohen71437402008-01-30 18:30:53 +0200349 return -EIO;
350}
351
352static void ipoib_dma_unmap_tx(struct ib_device *ca,
353 struct ipoib_tx_buf *tx_req)
354{
355 struct sk_buff *skb = tx_req->skb;
356 u64 *mapping = tx_req->mapping;
357 int i;
Eli Cohen40ca1982008-04-16 21:09:27 -0700358 int off;
Eli Cohen71437402008-01-30 18:30:53 +0200359
Eli Cohen40ca1982008-04-16 21:09:27 -0700360 if (skb_headlen(skb)) {
361 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
362 off = 1;
363 } else
364 off = 0;
Eli Cohen71437402008-01-30 18:30:53 +0200365
366 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000367 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
368
369 ib_dma_unmap_page(ca, mapping[i + off], skb_frag_size(frag),
Eli Cohen71437402008-01-30 18:30:53 +0200370 DMA_TO_DEVICE);
371 }
372}
373
Roland Dreier2439a6e2006-09-22 15:22:52 -0700374static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct ipoib_dev_priv *priv = netdev_priv(dev);
377 unsigned int wr_id = wc->wr_id;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700378 struct ipoib_tx_buf *tx_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Roland Dreiera89875f2007-04-18 20:20:53 -0700380 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
381 wr_id, wc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Roland Dreier2439a6e2006-09-22 15:22:52 -0700383 if (unlikely(wr_id >= ipoib_sendq_size)) {
384 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
385 wr_id, ipoib_sendq_size);
386 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Roland Dreier2439a6e2006-09-22 15:22:52 -0700388
389 tx_req = &priv->tx_ring[wr_id];
390
Eli Cohen71437402008-01-30 18:30:53 +0200391 ipoib_dma_unmap_tx(priv->ca, tx_req);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700392
Roland Dreierde903512007-09-28 15:33:51 -0700393 ++dev->stats.tx_packets;
394 dev->stats.tx_bytes += tx_req->skb->len;
Roland Dreier2439a6e2006-09-22 15:22:52 -0700395
396 dev_kfree_skb_any(tx_req->skb);
397
Roland Dreier2439a6e2006-09-22 15:22:52 -0700398 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300399 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
400 netif_queue_stopped(dev) &&
401 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Roland Dreier2439a6e2006-09-22 15:22:52 -0700402 netif_wake_queue(dev);
Roland Dreier2439a6e2006-09-22 15:22:52 -0700403
404 if (wc->status != IB_WC_SUCCESS &&
405 wc->status != IB_WC_WR_FLUSH_ERR)
406 ipoib_warn(priv, "failed send event "
407 "(status=%d, wrid=%d vend_err %x)\n",
408 wc->status, wr_id, wc->vendor_err);
409}
410
Eli Cohenf56bcd82008-04-29 13:46:53 -0700411static int poll_tx(struct ipoib_dev_priv *priv)
412{
413 int n, i;
414
415 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
416 for (i = 0; i < n; ++i)
417 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
418
419 return n == MAX_SEND_CQE;
420}
421
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700422int ipoib_poll(struct napi_struct *napi, int budget)
Roland Dreier2439a6e2006-09-22 15:22:52 -0700423{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700424 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
425 struct net_device *dev = priv->dev;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700426 int done;
427 int t;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700428 int n, i;
429
430 done = 0;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700431
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700432poll_more:
433 while (done < budget) {
434 int max = (budget - done);
435
Roland Dreier8d1cc862007-05-06 21:05:32 -0700436 t = min(IPOIB_NUM_WC, max);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700437 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700438
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700439 for (i = 0; i < n; i++) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700440 struct ib_wc *wc = priv->ibwc + i;
441
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300442 if (wc->wr_id & IPOIB_OP_RECV) {
Roland Dreier8d1cc862007-05-06 21:05:32 -0700443 ++done;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300444 if (wc->wr_id & IPOIB_OP_CM)
445 ipoib_cm_handle_rx_wc(dev, wc);
446 else
447 ipoib_ib_handle_rx_wc(dev, wc);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700448 } else
449 ipoib_cm_handle_tx_wc(priv->dev, wc);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700450 }
451
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700452 if (n != t)
Roland Dreier8d1cc862007-05-06 21:05:32 -0700453 break;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700454 }
455
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700456 if (done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800457 napi_complete(napi);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700458 if (unlikely(ib_req_notify_cq(priv->recv_cq,
Roland Dreier8d1cc862007-05-06 21:05:32 -0700459 IB_CQ_NEXT_COMP |
460 IB_CQ_REPORT_MISSED_EVENTS)) &&
Ben Hutchings288379f2009-01-19 16:43:59 -0800461 napi_reschedule(napi))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700462 goto poll_more;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700463 }
464
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700465 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
469{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700470 struct net_device *dev = dev_ptr;
471 struct ipoib_dev_priv *priv = netdev_priv(dev);
472
Ben Hutchings288379f2009-01-19 16:43:59 -0800473 napi_schedule(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Eli Cohen57ce41d2008-04-30 20:02:45 -0700476static void drain_tx_cq(struct net_device *dev)
477{
478 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700479
Roland Dreier943c2462008-09-30 10:36:21 -0700480 netif_tx_lock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700481 while (poll_tx(priv))
482 ; /* nothing */
483
484 if (netif_queue_stopped(dev))
485 mod_timer(&priv->poll_timer, jiffies + 1);
486
Roland Dreier943c2462008-09-30 10:36:21 -0700487 netif_tx_unlock(dev);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700488}
489
490void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
491{
Roland Dreier943c2462008-09-30 10:36:21 -0700492 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
493
494 mod_timer(&priv->poll_timer, jiffies);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static inline int post_send(struct ipoib_dev_priv *priv,
498 unsigned int wr_id,
499 struct ib_ah *address, u32 qpn,
Eli Cohen40ca1982008-04-16 21:09:27 -0700500 struct ipoib_tx_buf *tx_req,
501 void *head, int hlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct ib_send_wr *bad_wr;
Eli Cohen40ca1982008-04-16 21:09:27 -0700504 int i, off;
505 struct sk_buff *skb = tx_req->skb;
506 skb_frag_t *frags = skb_shinfo(skb)->frags;
507 int nr_frags = skb_shinfo(skb)->nr_frags;
508 u64 *mapping = tx_req->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Eli Cohen40ca1982008-04-16 21:09:27 -0700510 if (skb_headlen(skb)) {
511 priv->tx_sge[0].addr = mapping[0];
512 priv->tx_sge[0].length = skb_headlen(skb);
513 off = 1;
514 } else
515 off = 0;
516
Eli Cohen71437402008-01-30 18:30:53 +0200517 for (i = 0; i < nr_frags; ++i) {
Eli Cohen40ca1982008-04-16 21:09:27 -0700518 priv->tx_sge[i + off].addr = mapping[i + off];
Eric Dumazet9e903e02011-10-18 21:00:24 +0000519 priv->tx_sge[i + off].length = skb_frag_size(&frags[i]);
Eli Cohen71437402008-01-30 18:30:53 +0200520 }
Eli Cohen40ca1982008-04-16 21:09:27 -0700521 priv->tx_wr.num_sge = nr_frags + off;
Eli Cohen71437402008-01-30 18:30:53 +0200522 priv->tx_wr.wr_id = wr_id;
523 priv->tx_wr.wr.ud.remote_qpn = qpn;
524 priv->tx_wr.wr.ud.ah = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Eli Cohen40ca1982008-04-16 21:09:27 -0700526 if (head) {
527 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
528 priv->tx_wr.wr.ud.header = head;
529 priv->tx_wr.wr.ud.hlen = hlen;
530 priv->tx_wr.opcode = IB_WR_LSO;
531 } else
532 priv->tx_wr.opcode = IB_WR_SEND;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
535}
536
537void ipoib_send(struct net_device *dev, struct sk_buff *skb,
538 struct ipoib_ah *address, u32 qpn)
539{
540 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1993d682005-10-28 15:30:34 -0700541 struct ipoib_tx_buf *tx_req;
Or Gerlitza48f5092010-03-04 13:17:37 +0000542 int hlen, rc;
Eli Cohen40ca1982008-04-16 21:09:27 -0700543 void *phead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Eli Cohen40ca1982008-04-16 21:09:27 -0700545 if (skb_is_gso(skb)) {
546 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
547 phead = skb->data;
548 if (unlikely(!skb_pull(skb, hlen))) {
549 ipoib_warn(priv, "linear data too small\n");
550 ++dev->stats.tx_dropped;
551 ++dev->stats.tx_errors;
552 dev_kfree_skb_any(skb);
553 return;
554 }
555 } else {
556 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
557 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
558 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
559 ++dev->stats.tx_dropped;
560 ++dev->stats.tx_errors;
561 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
562 return;
563 }
564 phead = NULL;
565 hlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
569 skb->len, address, qpn);
570
571 /*
572 * We put the skb into the tx_ring _before_ we call post_send()
573 * because it's entirely possible that the completion handler will
574 * run before we execute anything after the post_send(). That
575 * means we have to make sure everything is properly recorded and
576 * our state is consistent before we call post_send().
577 */
Shirley Ma0f485252006-04-10 09:43:58 -0700578 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 tx_req->skb = skb;
Eli Cohen71437402008-01-30 18:30:53 +0200580 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700581 ++dev->stats.tx_errors;
Roland Dreier73fbe8b2006-10-10 12:50:38 -0700582 dev_kfree_skb_any(skb);
583 return;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Eli Cohen60461362008-04-16 21:01:10 -0700586 if (skb->ip_summed == CHECKSUM_PARTIAL)
587 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
588 else
589 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
590
Eli Cohen57ce41d2008-04-30 20:02:45 -0700591 if (++priv->tx_outstanding == ipoib_sendq_size) {
592 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
593 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
594 ipoib_warn(priv, "request notify on send CQ failed\n");
595 netif_stop_queue(dev);
596 }
597
Or Gerlitza48f5092010-03-04 13:17:37 +0000598 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
599 address->ah, qpn, tx_req, phead, hlen);
600 if (unlikely(rc)) {
601 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700602 ++dev->stats.tx_errors;
Eli Cohen57ce41d2008-04-30 20:02:45 -0700603 --priv->tx_outstanding;
Eli Cohen71437402008-01-30 18:30:53 +0200604 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev_kfree_skb_any(skb);
Eli Cohen57ce41d2008-04-30 20:02:45 -0700606 if (netif_queue_stopped(dev))
607 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else {
609 dev->trans_start = jiffies;
610
611 address->last_send = priv->tx_head;
612 ++priv->tx_head;
Eli Cohenf56bcd82008-04-29 13:46:53 -0700613 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Eli Cohenf56bcd82008-04-29 13:46:53 -0700616
617 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
Eli Cohen57ce41d2008-04-30 20:02:45 -0700618 while (poll_tx(priv))
619 ; /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static void __ipoib_reap_ah(struct net_device *dev)
623{
624 struct ipoib_dev_priv *priv = netdev_priv(dev);
625 struct ipoib_ah *ah, *tah;
626 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700627 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Roland Dreier943c2462008-09-30 10:36:21 -0700629 netif_tx_lock_bh(dev);
630 spin_lock_irqsave(&priv->lock, flags);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
Roland Dreier21818582005-07-27 14:41:32 -0700633 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 list_del(&ah->list);
Roland Dreier31c02e22006-06-17 20:37:34 -0700635 ib_destroy_ah(ah->ah);
636 kfree(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Roland Dreier943c2462008-09-30 10:36:21 -0700638
639 spin_unlock_irqrestore(&priv->lock, flags);
640 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
David Howellsc4028952006-11-22 14:57:56 +0000643void ipoib_reap_ah(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
David Howellsc4028952006-11-22 14:57:56 +0000645 struct ipoib_dev_priv *priv =
646 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
647 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 __ipoib_reap_ah(dev);
650
651 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
Anton Blanchard69fc5072007-10-15 00:50:56 -0500652 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
653 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Eli Cohen57ce41d2008-04-30 20:02:45 -0700656static void ipoib_ib_tx_timer_func(unsigned long ctx)
657{
658 drain_tx_cq((struct net_device *)ctx);
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661int ipoib_ib_dev_open(struct net_device *dev)
662{
663 struct ipoib_dev_priv *priv = netdev_priv(dev);
664 int ret;
665
Yosef Etigin26bbf132007-05-19 08:51:54 -0700666 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
667 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
668 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
669 return -1;
670 }
671 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
672
Roland Dreier5b6810e2005-10-11 11:08:24 -0700673 ret = ipoib_init_qp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ret) {
Roland Dreier5b6810e2005-10-11 11:08:24 -0700675 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -1;
677 }
678
679 ret = ipoib_ib_post_receives(dev);
680 if (ret) {
681 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700682 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -1;
684 }
685
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200686 ret = ipoib_cm_dev_open(dev);
687 if (ret) {
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300688 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700689 ipoib_ib_dev_stop(dev, 1);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200690 return -1;
691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
Anton Blanchard69fc5072007-10-15 00:50:56 -0500694 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
695 round_jiffies_relative(HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Yossi Etigine028cc52009-04-20 13:58:08 -0700697 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
698 napi_enable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
Leonid Arsh7a343d42006-03-23 19:52:51 +0200703static void ipoib_pkey_dev_check_presence(struct net_device *dev)
704{
705 struct ipoib_dev_priv *priv = netdev_priv(dev);
706 u16 pkey_index = 0;
707
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700708 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
Leonid Arsh7a343d42006-03-23 19:52:51 +0200709 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
710 else
711 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714int ipoib_ib_dev_up(struct net_device *dev)
715{
716 struct ipoib_dev_priv *priv = netdev_priv(dev);
717
Leonid Arsh7a343d42006-03-23 19:52:51 +0200718 ipoib_pkey_dev_check_presence(dev);
719
720 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
721 ipoib_dbg(priv, "PKEY is not assigned.\n");
722 return 0;
723 }
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
726
727 return ipoib_mcast_start_thread(dev);
728}
729
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800730int ipoib_ib_dev_down(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct ipoib_dev_priv *priv = netdev_priv(dev);
733
734 ipoib_dbg(priv, "downing ib_dev\n");
735
736 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
737 netif_carrier_off(dev);
738
739 /* Shutdown the P_Key thread if still active */
740 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -0800741 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 set_bit(IPOIB_PKEY_STOP, &priv->flags);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700743 cancel_delayed_work(&priv->pkey_poll_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800744 mutex_unlock(&pkey_mutex);
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800745 if (flush)
746 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800749 ipoib_mcast_stop_thread(dev, flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ipoib_mcast_dev_flush(dev);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ipoib_flush_paths(dev);
753
754 return 0;
755}
756
757static int recvs_pending(struct net_device *dev)
758{
759 struct ipoib_dev_priv *priv = netdev_priv(dev);
760 int pending = 0;
761 int i;
762
Shirley Ma0f485252006-04-10 09:43:58 -0700763 for (i = 0; i < ipoib_recvq_size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (priv->rx_ring[i].skb)
765 ++pending;
766
767 return pending;
768}
769
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300770void ipoib_drain_cq(struct net_device *dev)
771{
772 struct ipoib_dev_priv *priv = netdev_priv(dev);
773 int i, n;
Roland Dreier943c2462008-09-30 10:36:21 -0700774
775 /*
776 * We call completion handling routines that expect to be
777 * called from the BH-disabled NAPI poll context, so disable
778 * BHs here too.
779 */
780 local_bh_disable();
781
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300782 do {
Eli Cohenf56bcd82008-04-29 13:46:53 -0700783 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300784 for (i = 0; i < n; ++i) {
Roland Dreierce423ef2007-10-09 19:59:04 -0700785 /*
786 * Convert any successful completions to flush
787 * errors to avoid passing packets up the
788 * stack after bringing the device down.
789 */
790 if (priv->ibwc[i].status == IB_WC_SUCCESS)
791 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
792
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300793 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
794 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
795 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
796 else
797 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700798 } else
799 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300800 }
801 } while (n == IPOIB_NUM_WC);
Eli Cohenf56bcd82008-04-29 13:46:53 -0700802
803 while (poll_tx(priv))
804 ; /* nothing */
Roland Dreier943c2462008-09-30 10:36:21 -0700805
806 local_bh_enable();
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300807}
808
Yosef Etigin26bbf132007-05-19 08:51:54 -0700809int ipoib_ib_dev_stop(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct ipoib_dev_priv *priv = netdev_priv(dev);
812 struct ib_qp_attr qp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned long begin;
Roland Dreier1993d682005-10-28 15:30:34 -0700814 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300815 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Yossi Etigine028cc52009-04-20 13:58:08 -0700817 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
818 napi_disable(&priv->napi);
Leonid Arsh7a343d42006-03-23 19:52:51 +0200819
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200820 ipoib_cm_dev_stop(dev);
821
Roland Dreier3bc12e72005-10-30 13:20:09 -0800822 /*
823 * Move our QP to the error state and then reinitialize in
824 * when all work requests have completed or have been flushed.
825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 qp_attr.qp_state = IB_QPS_ERR;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800827 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
829
830 /* Wait for all sends and receives to complete */
831 begin = jiffies;
832
833 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
834 if (time_after(jiffies, begin + 5 * HZ)) {
835 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
836 priv->tx_head - priv->tx_tail, recvs_pending(dev));
837
838 /*
839 * assume the HW is wedged and just free up
840 * all our pending work requests.
841 */
Roland Dreier21818582005-07-27 14:41:32 -0700842 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tx_req = &priv->tx_ring[priv->tx_tail &
Shirley Ma0f485252006-04-10 09:43:58 -0700844 (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +0200845 ipoib_dma_unmap_tx(priv->ca, tx_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 dev_kfree_skb_any(tx_req->skb);
847 ++priv->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300848 --priv->tx_outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800851 for (i = 0; i < ipoib_recvq_size; ++i) {
852 struct ipoib_rx_buf *rx_req;
853
854 rx_req = &priv->rx_ring[i];
855 if (!rx_req->skb)
856 continue;
Shirley Mabc7b3a32008-04-23 11:55:45 -0700857 ipoib_ud_dma_unmap_rx(priv,
858 priv->rx_ring[i].mapping);
Ralph Campbell37ccf9d2006-12-12 14:30:48 -0800859 dev_kfree_skb_any(rx_req->skb);
860 rx_req->skb = NULL;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 goto timeout;
864 }
865
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300866 ipoib_drain_cq(dev);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 msleep(1);
869 }
870
871 ipoib_dbg(priv, "All sends and receives done.\n");
872
873timeout:
Eli Cohen57ce41d2008-04-30 20:02:45 -0700874 del_timer_sync(&priv->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 qp_attr.qp_state = IB_QPS_RESET;
Roland Dreier3bc12e72005-10-30 13:20:09 -0800876 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
878
879 /* Wait for all AHs to be reaped */
880 set_bit(IPOIB_STOP_REAPER, &priv->flags);
881 cancel_delayed_work(&priv->ah_reap_task);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700882 if (flush)
883 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 begin = jiffies;
886
887 while (!list_empty(&priv->dead_ahs)) {
888 __ipoib_reap_ah(dev);
889
890 if (time_after(jiffies, begin + HZ)) {
891 ipoib_warn(priv, "timing out; will leak address handles\n");
892 break;
893 }
894
895 msleep(1);
896 }
897
Eli Cohenf56bcd82008-04-29 13:46:53 -0700898 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreier8d1cc862007-05-06 21:05:32 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return 0;
901}
902
903int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
904{
905 struct ipoib_dev_priv *priv = netdev_priv(dev);
906
907 priv->ca = ca;
908 priv->port = port;
909 priv->qp = NULL;
910
911 if (ipoib_transport_dev_init(dev, ca)) {
912 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
913 return -ENODEV;
914 }
915
Roland Dreier27678402008-10-10 15:58:52 -0700916 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
917 (unsigned long) dev);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (dev->flags & IFF_UP) {
920 if (ipoib_ib_dev_open(dev)) {
921 ipoib_transport_dev_cleanup(dev);
922 return -ENODEV;
923 }
924 }
925
926 return 0;
927}
928
Moni Shouaee1e2c82008-07-14 23:48:49 -0700929static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
930 enum ipoib_flush_level level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Yosef Etigin26bbf132007-05-19 08:51:54 -0700932 struct ipoib_dev_priv *cpriv;
David Howellsc4028952006-11-22 14:57:56 +0000933 struct net_device *dev = priv->dev;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700934 u16 new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Yosef Etigin26bbf132007-05-19 08:51:54 -0700936 mutex_lock(&priv->vlan_mutex);
937
938 /*
939 * Flush any child interfaces too -- they might be up even if
940 * the parent is down.
941 */
942 list_for_each_entry(cpriv, &priv->child_intfs, list)
Moni Shouaee1e2c82008-07-14 23:48:49 -0700943 __ipoib_ib_dev_flush(cpriv, level);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700944
945 mutex_unlock(&priv->vlan_mutex);
946
947 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
Leonid Arsh7a343d42006-03-23 19:52:51 +0200948 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return;
Leonid Arsh7a343d42006-03-23 19:52:51 +0200950 }
951
952 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
953 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
954 return;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Moni Shouaee1e2c82008-07-14 23:48:49 -0700957 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700958 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
959 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
960 ipoib_ib_dev_down(dev, 0);
Jack Morgenstein167c4262008-02-13 16:23:50 +0200961 ipoib_ib_dev_stop(dev, 0);
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700962 if (ipoib_pkey_dev_delay_open(dev))
963 return;
Yosef Etigin26bbf132007-05-19 08:51:54 -0700964 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700965
966 /* restart QP only if P_Key index is changed */
Roland Dreier9fdd5e52008-04-16 21:09:35 -0700967 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
968 new_index == priv->pkey_index) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700969 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
970 return;
971 }
972 priv->pkey_index = new_index;
973 }
974
Moni Shouaee1e2c82008-07-14 23:48:49 -0700975 if (level == IPOIB_FLUSH_LIGHT) {
976 ipoib_mark_paths_invalid(dev);
977 ipoib_mcast_dev_flush(dev);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Moni Shouaee1e2c82008-07-14 23:48:49 -0700980 if (level >= IPOIB_FLUSH_NORMAL)
981 ipoib_ib_dev_down(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Moni Shouaee1e2c82008-07-14 23:48:49 -0700983 if (level == IPOIB_FLUSH_HEAVY) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700984 ipoib_ib_dev_stop(dev, 0);
985 ipoib_ib_dev_open(dev);
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /*
989 * The device could have been brought down between the start and when
990 * we get here, don't bring it back up if it's not configured up
991 */
Eli Cohen5ccd0252006-09-22 15:22:56 -0700992 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700993 if (level >= IPOIB_FLUSH_NORMAL)
994 ipoib_ib_dev_up(dev);
David Howellsc4028952006-11-22 14:57:56 +0000995 ipoib_mcast_restart_task(&priv->restart_task);
Eli Cohen5ccd0252006-09-22 15:22:56 -0700996 }
Yosef Etigin26bbf132007-05-19 08:51:54 -0700997}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Moni Shouaee1e2c82008-07-14 23:48:49 -0700999void ipoib_ib_dev_flush_light(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -07001000{
1001 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -07001002 container_of(work, struct ipoib_dev_priv, flush_light);
Michael S. Tsirkin4f710552005-11-29 10:53:30 -08001003
Moni Shouaee1e2c82008-07-14 23:48:49 -07001004 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
Yosef Etigin26bbf132007-05-19 08:51:54 -07001005}
Michael S. Tsirkin4f710552005-11-29 10:53:30 -08001006
Moni Shouaee1e2c82008-07-14 23:48:49 -07001007void ipoib_ib_dev_flush_normal(struct work_struct *work)
Yosef Etigin26bbf132007-05-19 08:51:54 -07001008{
1009 struct ipoib_dev_priv *priv =
Moni Shouaee1e2c82008-07-14 23:48:49 -07001010 container_of(work, struct ipoib_dev_priv, flush_normal);
Yosef Etigin26bbf132007-05-19 08:51:54 -07001011
Moni Shouaee1e2c82008-07-14 23:48:49 -07001012 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1013}
1014
1015void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1016{
1017 struct ipoib_dev_priv *priv =
1018 container_of(work, struct ipoib_dev_priv, flush_heavy);
1019
1020 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023void ipoib_ib_dev_cleanup(struct net_device *dev)
1024{
1025 struct ipoib_dev_priv *priv = netdev_priv(dev);
1026
1027 ipoib_dbg(priv, "cleaning up ib_dev\n");
1028
Roland Dreier8d2cae02005-09-20 10:52:04 -07001029 ipoib_mcast_stop_thread(dev, 1);
Eli Cohen988bd502006-01-12 14:32:20 -08001030 ipoib_mcast_dev_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 ipoib_transport_dev_cleanup(dev);
1033}
1034
1035/*
1036 * Delayed P_Key Assigment Interim Support
1037 *
1038 * The following is initial implementation of delayed P_Key assigment
1039 * mechanism. It is using the same approach implemented for the multicast
1040 * group join. The single goal of this implementation is to quickly address
1041 * Bug #2507. This implementation will probably be removed when the P_Key
1042 * change async notification is available.
1043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
David Howellsc4028952006-11-22 14:57:56 +00001045void ipoib_pkey_poll(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
David Howellsc4028952006-11-22 14:57:56 +00001047 struct ipoib_dev_priv *priv =
Yosef Etigin26bbf132007-05-19 08:51:54 -07001048 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
David Howellsc4028952006-11-22 14:57:56 +00001049 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 ipoib_pkey_dev_check_presence(dev);
1052
1053 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1054 ipoib_open(dev);
1055 else {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001056 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1058 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001059 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001061 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063}
1064
1065int ipoib_pkey_dev_delay_open(struct net_device *dev)
1066{
1067 struct ipoib_dev_priv *priv = netdev_priv(dev);
1068
1069 /* Look for the interface pkey value in the IB Port P_Key table and */
1070 /* set the interface pkey assigment flag */
1071 ipoib_pkey_dev_check_presence(dev);
1072
1073 /* P_Key value not assigned yet - start polling */
1074 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
Ingo Molnar95ed6442006-01-13 14:51:39 -08001075 mutex_lock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1077 queue_delayed_work(ipoib_workqueue,
Yosef Etigin26bbf132007-05-19 08:51:54 -07001078 &priv->pkey_poll_task,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001080 mutex_unlock(&pkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 1;
1082 }
1083
1084 return 0;
1085}