]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/infiniband/ulp/ipoib/ipoib_ib.c
net: add skb frag size accessors
[mirror_ubuntu-artful-kernel.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * 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.
1da177e4
LT
6 *
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.
1da177e4
LT
34 */
35
36#include <linux/delay.h>
37#include <linux/dma-mapping.h>
5a0e3ad6 38#include <linux/slab.h>
1da177e4 39
40ca1988
EC
40#include <linux/ip.h>
41#include <linux/tcp.h>
1da177e4
LT
42
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
95ed644f 53static DEFINE_MUTEX(pkey_mutex);
1da177e4
LT
54
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
31c02e21
RD
85 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
88}
89
bc7b3a36
SM
90static 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
9e903e08 120 skb_frag_size_set(frag, size);
bc7b3a36
SM
121 skb->data_len += size;
122 skb->truesize += size;
123 } else
124 skb_put(skb, length);
125
126}
127
1993d683 128static int ipoib_ib_post_receive(struct net_device *dev, int id)
1da177e4 129{
1993d683 130 struct ipoib_dev_priv *priv = netdev_priv(dev);
1da177e4 131 struct ib_recv_wr *bad_wr;
1993d683
RD
132 int ret;
133
bc7b3a36
SM
134 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];
1993d683 137
1993d683 138
bc7b3a36 139 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
1993d683
RD
140 if (unlikely(ret)) {
141 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
bc7b3a36 142 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
1993d683
RD
143 dev_kfree_skb_any(priv->rx_ring[id].skb);
144 priv->rx_ring[id].skb = NULL;
145 }
1da177e4 146
1993d683 147 return ret;
1da177e4
LT
148}
149
bc7b3a36 150static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
1da177e4
LT
151{
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
153 struct sk_buff *skb;
bc7b3a36
SM
154 int buf_size;
155 u64 *mapping;
1da177e4 156
bc7b3a36
SM
157 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;
1993d683
RD
165
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
bc7b3a36
SM
173 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] =
5581be3b 185 ib_dma_map_page(priv->ca, page,
bc7b3a36
SM
186 0, PAGE_SIZE, DMA_FROM_DEVICE);
187 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
188 goto partial_error;
1da177e4
LT
189 }
190
bc7b3a36
SM
191 priv->rx_ring[id].skb = skb;
192 return skb;
1993d683 193
bc7b3a36
SM
194partial_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;
1da177e4
LT
199}
200
201static int ipoib_ib_post_receives(struct net_device *dev)
202{
203 struct ipoib_dev_priv *priv = netdev_priv(dev);
204 int i;
205
0f485251 206 for (i = 0; i < ipoib_recvq_size; ++i) {
bc7b3a36 207 if (!ipoib_alloc_rx_skb(dev, i)) {
1993d683
RD
208 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
209 return -ENOMEM;
210 }
1da177e4
LT
211 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
2439a6e6 220static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
1da177e4
LT
221{
222 struct ipoib_dev_priv *priv = netdev_priv(dev);
2439a6e6
RD
223 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
224 struct sk_buff *skb;
bc7b3a36 225 u64 mapping[IPOIB_UD_RX_SG];
fed1db33 226 union ib_gid *dgid;
1da177e4 227
a89875fc
RD
228 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
229 wr_id, wc->status);
1da177e4 230
2439a6e6
RD
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;
2439a6e6
RD
238
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);
bc7b3a36 244 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
2439a6e6
RD
245 dev_kfree_skb_any(skb);
246 priv->rx_ring[wr_id].skb = NULL;
247 return;
248 }
1da177e4 249
1b844afe
RD
250 /*
251 * 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
bc7b3a36
SM
257 memcpy(mapping, priv->rx_ring[wr_id].mapping,
258 IPOIB_UD_RX_SG * sizeof *mapping);
259
2439a6e6
RD
260 /*
261 * If we can't allocate a new RX buffer, dump
262 * this packet and reuse the old buffer.
263 */
bc7b3a36 264 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
de903512 265 ++dev->stats.rx_dropped;
2439a6e6
RD
266 goto repost;
267 }
268
269 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
270 wc->byte_len, wc->slid);
271
bc7b3a36
SM
272 ipoib_ud_dma_unmap_rx(priv, mapping);
273 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
2439a6e6 274
fed1db33
CL
275 /* 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
2439a6e6
RD
285 skb_pull(skb, IB_GRH_BYTES);
286
1b844afe
RD
287 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
288 skb_reset_mac_header(skb);
289 skb_pull(skb, IPOIB_ENCAP_LEN);
290
de903512
RD
291 ++dev->stats.rx_packets;
292 dev->stats.rx_bytes += skb->len;
1b844afe
RD
293
294 skb->dev = dev;
3d96c74d 295 if ((dev->features & NETIF_F_RXCSUM) && likely(wc->csum_ok))
6046136c
EC
296 skb->ip_summed = CHECKSUM_UNNECESSARY;
297
8ae31e5b 298 napi_gro_receive(&priv->napi, skb);
1da177e4 299
2439a6e6
RD
300repost:
301 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
302 ipoib_warn(priv, "ipoib_ib_post_receive failed "
303 "for buf %d\n", wr_id);
304}
1da177e4 305
7143740d
EC
306static int ipoib_dma_map_tx(struct ib_device *ca,
307 struct ipoib_tx_buf *tx_req)
308{
309 struct sk_buff *skb = tx_req->skb;
310 u64 *mapping = tx_req->mapping;
311 int i;
40ca1988 312 int off;
7143740d 313
40ca1988
EC
314 if (skb_headlen(skb)) {
315 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
316 DMA_TO_DEVICE);
317 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
318 return -EIO;
319
320 off = 1;
321 } else
322 off = 0;
7143740d
EC
323
324 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
9e903e08 325 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5581be3b
IC
326 mapping[i + off] = ib_dma_map_page(ca,
327 skb_frag_page(frag),
9e903e08 328 frag->page_offset, skb_frag_size(frag),
7143740d 329 DMA_TO_DEVICE);
40ca1988 330 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
7143740d
EC
331 goto partial_error;
332 }
333 return 0;
334
335partial_error:
7143740d 336 for (; i > 0; --i) {
9e903e08
ED
337 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
338
339 ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
7143740d 340 }
40ca1988
EC
341
342 if (off)
343 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
344
7143740d
EC
345 return -EIO;
346}
347
348static void ipoib_dma_unmap_tx(struct ib_device *ca,
349 struct ipoib_tx_buf *tx_req)
350{
351 struct sk_buff *skb = tx_req->skb;
352 u64 *mapping = tx_req->mapping;
353 int i;
40ca1988 354 int off;
7143740d 355
40ca1988
EC
356 if (skb_headlen(skb)) {
357 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
358 off = 1;
359 } else
360 off = 0;
7143740d
EC
361
362 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
9e903e08
ED
363 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
364
365 ib_dma_unmap_page(ca, mapping[i + off], skb_frag_size(frag),
7143740d
EC
366 DMA_TO_DEVICE);
367 }
368}
369
2439a6e6
RD
370static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
371{
372 struct ipoib_dev_priv *priv = netdev_priv(dev);
373 unsigned int wr_id = wc->wr_id;
374 struct ipoib_tx_buf *tx_req;
1da177e4 375
a89875fc
RD
376 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
377 wr_id, wc->status);
1da177e4 378
2439a6e6
RD
379 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;
1da177e4 383 }
2439a6e6
RD
384
385 tx_req = &priv->tx_ring[wr_id];
386
7143740d 387 ipoib_dma_unmap_tx(priv->ca, tx_req);
2439a6e6 388
de903512
RD
389 ++dev->stats.tx_packets;
390 dev->stats.tx_bytes += tx_req->skb->len;
2439a6e6
RD
391
392 dev_kfree_skb_any(tx_req->skb);
393
2439a6e6 394 ++priv->tx_tail;
1b524963
MT
395 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
396 netif_queue_stopped(dev) &&
397 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
2439a6e6 398 netif_wake_queue(dev);
2439a6e6
RD
399
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
f56bcd80
EC
407static 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
bea3348e 418int ipoib_poll(struct napi_struct *napi, int budget)
2439a6e6 419{
bea3348e
SH
420 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
421 struct net_device *dev = priv->dev;
8d1cc86a
RD
422 int done;
423 int t;
8d1cc86a
RD
424 int n, i;
425
426 done = 0;
8d1cc86a 427
bea3348e
SH
428poll_more:
429 while (done < budget) {
430 int max = (budget - done);
431
8d1cc86a 432 t = min(IPOIB_NUM_WC, max);
f56bcd80 433 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
8d1cc86a 434
bea3348e 435 for (i = 0; i < n; i++) {
8d1cc86a
RD
436 struct ib_wc *wc = priv->ibwc + i;
437
1b524963 438 if (wc->wr_id & IPOIB_OP_RECV) {
8d1cc86a 439 ++done;
1b524963
MT
440 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);
f56bcd80
EC
444 } else
445 ipoib_cm_handle_tx_wc(priv->dev, wc);
8d1cc86a
RD
446 }
447
bea3348e 448 if (n != t)
8d1cc86a 449 break;
8d1cc86a
RD
450 }
451
bea3348e 452 if (done < budget) {
288379f0 453 napi_complete(napi);
f56bcd80 454 if (unlikely(ib_req_notify_cq(priv->recv_cq,
8d1cc86a
RD
455 IB_CQ_NEXT_COMP |
456 IB_CQ_REPORT_MISSED_EVENTS)) &&
288379f0 457 napi_reschedule(napi))
bea3348e 458 goto poll_more;
8d1cc86a
RD
459 }
460
bea3348e 461 return done;
1da177e4
LT
462}
463
464void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
465{
bea3348e
SH
466 struct net_device *dev = dev_ptr;
467 struct ipoib_dev_priv *priv = netdev_priv(dev);
468
288379f0 469 napi_schedule(&priv->napi);
1da177e4
LT
470}
471
57ce41d1
EC
472static void drain_tx_cq(struct net_device *dev)
473{
474 struct ipoib_dev_priv *priv = netdev_priv(dev);
57ce41d1 475
943c246e 476 netif_tx_lock(dev);
57ce41d1
EC
477 while (poll_tx(priv))
478 ; /* nothing */
479
480 if (netif_queue_stopped(dev))
481 mod_timer(&priv->poll_timer, jiffies + 1);
482
943c246e 483 netif_tx_unlock(dev);
57ce41d1
EC
484}
485
486void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
487{
943c246e
RD
488 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
489
490 mod_timer(&priv->poll_timer, jiffies);
57ce41d1
EC
491}
492
1da177e4
LT
493static inline int post_send(struct ipoib_dev_priv *priv,
494 unsigned int wr_id,
495 struct ib_ah *address, u32 qpn,
40ca1988
EC
496 struct ipoib_tx_buf *tx_req,
497 void *head, int hlen)
1da177e4
LT
498{
499 struct ib_send_wr *bad_wr;
40ca1988
EC
500 int i, off;
501 struct sk_buff *skb = tx_req->skb;
502 skb_frag_t *frags = skb_shinfo(skb)->frags;
503 int nr_frags = skb_shinfo(skb)->nr_frags;
504 u64 *mapping = tx_req->mapping;
505
506 if (skb_headlen(skb)) {
507 priv->tx_sge[0].addr = mapping[0];
508 priv->tx_sge[0].length = skb_headlen(skb);
509 off = 1;
510 } else
511 off = 0;
1da177e4 512
7143740d 513 for (i = 0; i < nr_frags; ++i) {
40ca1988 514 priv->tx_sge[i + off].addr = mapping[i + off];
9e903e08 515 priv->tx_sge[i + off].length = skb_frag_size(&frags[i]);
7143740d 516 }
40ca1988 517 priv->tx_wr.num_sge = nr_frags + off;
7143740d
EC
518 priv->tx_wr.wr_id = wr_id;
519 priv->tx_wr.wr.ud.remote_qpn = qpn;
520 priv->tx_wr.wr.ud.ah = address;
1da177e4 521
40ca1988
EC
522 if (head) {
523 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
524 priv->tx_wr.wr.ud.header = head;
525 priv->tx_wr.wr.ud.hlen = hlen;
526 priv->tx_wr.opcode = IB_WR_LSO;
527 } else
528 priv->tx_wr.opcode = IB_WR_SEND;
529
1da177e4
LT
530 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
531}
532
533void ipoib_send(struct net_device *dev, struct sk_buff *skb,
534 struct ipoib_ah *address, u32 qpn)
535{
536 struct ipoib_dev_priv *priv = netdev_priv(dev);
1993d683 537 struct ipoib_tx_buf *tx_req;
a48f509b 538 int hlen, rc;
40ca1988
EC
539 void *phead;
540
541 if (skb_is_gso(skb)) {
542 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
543 phead = skb->data;
544 if (unlikely(!skb_pull(skb, hlen))) {
545 ipoib_warn(priv, "linear data too small\n");
546 ++dev->stats.tx_dropped;
547 ++dev->stats.tx_errors;
548 dev_kfree_skb_any(skb);
549 return;
550 }
551 } else {
552 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
553 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
554 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
555 ++dev->stats.tx_dropped;
556 ++dev->stats.tx_errors;
557 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
558 return;
559 }
560 phead = NULL;
561 hlen = 0;
1da177e4
LT
562 }
563
564 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
565 skb->len, address, qpn);
566
567 /*
568 * We put the skb into the tx_ring _before_ we call post_send()
569 * because it's entirely possible that the completion handler will
570 * run before we execute anything after the post_send(). That
571 * means we have to make sure everything is properly recorded and
572 * our state is consistent before we call post_send().
573 */
0f485251 574 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
1da177e4 575 tx_req->skb = skb;
7143740d 576 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
de903512 577 ++dev->stats.tx_errors;
73fbe8be
RD
578 dev_kfree_skb_any(skb);
579 return;
580 }
1da177e4 581
6046136c
EC
582 if (skb->ip_summed == CHECKSUM_PARTIAL)
583 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
584 else
585 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
586
57ce41d1
EC
587 if (++priv->tx_outstanding == ipoib_sendq_size) {
588 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
589 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
590 ipoib_warn(priv, "request notify on send CQ failed\n");
591 netif_stop_queue(dev);
592 }
593
a48f509b
OG
594 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
595 address->ah, qpn, tx_req, phead, hlen);
596 if (unlikely(rc)) {
597 ipoib_warn(priv, "post_send failed, error %d\n", rc);
de903512 598 ++dev->stats.tx_errors;
57ce41d1 599 --priv->tx_outstanding;
7143740d 600 ipoib_dma_unmap_tx(priv->ca, tx_req);
1da177e4 601 dev_kfree_skb_any(skb);
57ce41d1
EC
602 if (netif_queue_stopped(dev))
603 netif_wake_queue(dev);
1da177e4
LT
604 } else {
605 dev->trans_start = jiffies;
606
607 address->last_send = priv->tx_head;
608 ++priv->tx_head;
f56bcd80 609 skb_orphan(skb);
1da177e4 610
1da177e4 611 }
f56bcd80
EC
612
613 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
57ce41d1
EC
614 while (poll_tx(priv))
615 ; /* nothing */
1da177e4
LT
616}
617
618static void __ipoib_reap_ah(struct net_device *dev)
619{
620 struct ipoib_dev_priv *priv = netdev_priv(dev);
621 struct ipoib_ah *ah, *tah;
622 LIST_HEAD(remove_list);
943c246e
RD
623 unsigned long flags;
624
625 netif_tx_lock_bh(dev);
626 spin_lock_irqsave(&priv->lock, flags);
1da177e4 627
1da177e4 628 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
2181858b 629 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
1da177e4 630 list_del(&ah->list);
31c02e21
RD
631 ib_destroy_ah(ah->ah);
632 kfree(ah);
1da177e4 633 }
943c246e
RD
634
635 spin_unlock_irqrestore(&priv->lock, flags);
636 netif_tx_unlock_bh(dev);
1da177e4
LT
637}
638
c4028958 639void ipoib_reap_ah(struct work_struct *work)
1da177e4 640{
c4028958
DH
641 struct ipoib_dev_priv *priv =
642 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
643 struct net_device *dev = priv->dev;
1da177e4
LT
644
645 __ipoib_reap_ah(dev);
646
647 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
69fc507a
AB
648 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
649 round_jiffies_relative(HZ));
1da177e4
LT
650}
651
57ce41d1
EC
652static void ipoib_ib_tx_timer_func(unsigned long ctx)
653{
654 drain_tx_cq((struct net_device *)ctx);
655}
656
1da177e4
LT
657int ipoib_ib_dev_open(struct net_device *dev)
658{
659 struct ipoib_dev_priv *priv = netdev_priv(dev);
660 int ret;
661
26bbf13c
YE
662 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
663 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
664 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
665 return -1;
666 }
667 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
668
5b6810e0 669 ret = ipoib_init_qp(dev);
1da177e4 670 if (ret) {
5b6810e0 671 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
1da177e4
LT
672 return -1;
673 }
674
675 ret = ipoib_ib_post_receives(dev);
676 if (ret) {
677 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
26bbf13c 678 ipoib_ib_dev_stop(dev, 1);
1da177e4
LT
679 return -1;
680 }
681
839fcaba
MT
682 ret = ipoib_cm_dev_open(dev);
683 if (ret) {
24bd1e4e 684 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
26bbf13c 685 ipoib_ib_dev_stop(dev, 1);
839fcaba
MT
686 return -1;
687 }
688
1da177e4 689 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
69fc507a
AB
690 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
691 round_jiffies_relative(HZ));
1da177e4 692
e028cc55
YE
693 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
694 napi_enable(&priv->napi);
7a343d4c 695
1da177e4
LT
696 return 0;
697}
698
7a343d4c
LA
699static void ipoib_pkey_dev_check_presence(struct net_device *dev)
700{
701 struct ipoib_dev_priv *priv = netdev_priv(dev);
702 u16 pkey_index = 0;
703
9fdd5e5b 704 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
7a343d4c
LA
705 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
706 else
707 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
708}
709
1da177e4
LT
710int ipoib_ib_dev_up(struct net_device *dev)
711{
712 struct ipoib_dev_priv *priv = netdev_priv(dev);
713
7a343d4c
LA
714 ipoib_pkey_dev_check_presence(dev);
715
716 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
717 ipoib_dbg(priv, "PKEY is not assigned.\n");
718 return 0;
719 }
720
1da177e4
LT
721 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
722
723 return ipoib_mcast_start_thread(dev);
724}
725
0b3ea082 726int ipoib_ib_dev_down(struct net_device *dev, int flush)
1da177e4
LT
727{
728 struct ipoib_dev_priv *priv = netdev_priv(dev);
729
730 ipoib_dbg(priv, "downing ib_dev\n");
731
732 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
733 netif_carrier_off(dev);
734
735 /* Shutdown the P_Key thread if still active */
736 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 737 mutex_lock(&pkey_mutex);
1da177e4 738 set_bit(IPOIB_PKEY_STOP, &priv->flags);
26bbf13c 739 cancel_delayed_work(&priv->pkey_poll_task);
95ed644f 740 mutex_unlock(&pkey_mutex);
0b3ea082
JM
741 if (flush)
742 flush_workqueue(ipoib_workqueue);
1da177e4
LT
743 }
744
0b3ea082 745 ipoib_mcast_stop_thread(dev, flush);
1da177e4
LT
746 ipoib_mcast_dev_flush(dev);
747
1da177e4
LT
748 ipoib_flush_paths(dev);
749
750 return 0;
751}
752
753static int recvs_pending(struct net_device *dev)
754{
755 struct ipoib_dev_priv *priv = netdev_priv(dev);
756 int pending = 0;
757 int i;
758
0f485251 759 for (i = 0; i < ipoib_recvq_size; ++i)
1da177e4
LT
760 if (priv->rx_ring[i].skb)
761 ++pending;
762
763 return pending;
764}
765
2dfbfc37
MT
766void ipoib_drain_cq(struct net_device *dev)
767{
768 struct ipoib_dev_priv *priv = netdev_priv(dev);
769 int i, n;
943c246e
RD
770
771 /*
772 * We call completion handling routines that expect to be
773 * called from the BH-disabled NAPI poll context, so disable
774 * BHs here too.
775 */
776 local_bh_disable();
777
2dfbfc37 778 do {
f56bcd80 779 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
2dfbfc37 780 for (i = 0; i < n; ++i) {
ce423ef5
RD
781 /*
782 * Convert any successful completions to flush
783 * errors to avoid passing packets up the
784 * stack after bringing the device down.
785 */
786 if (priv->ibwc[i].status == IB_WC_SUCCESS)
787 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
788
1b524963
MT
789 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
790 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
791 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
792 else
793 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
f56bcd80
EC
794 } else
795 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
2dfbfc37
MT
796 }
797 } while (n == IPOIB_NUM_WC);
f56bcd80
EC
798
799 while (poll_tx(priv))
800 ; /* nothing */
943c246e
RD
801
802 local_bh_enable();
2dfbfc37
MT
803}
804
26bbf13c 805int ipoib_ib_dev_stop(struct net_device *dev, int flush)
1da177e4
LT
806{
807 struct ipoib_dev_priv *priv = netdev_priv(dev);
808 struct ib_qp_attr qp_attr;
1da177e4 809 unsigned long begin;
1993d683 810 struct ipoib_tx_buf *tx_req;
2dfbfc37 811 int i;
1da177e4 812
e028cc55
YE
813 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
814 napi_disable(&priv->napi);
7a343d4c 815
839fcaba
MT
816 ipoib_cm_dev_stop(dev);
817
3bc12e75
RD
818 /*
819 * Move our QP to the error state and then reinitialize in
820 * when all work requests have completed or have been flushed.
821 */
1da177e4 822 qp_attr.qp_state = IB_QPS_ERR;
3bc12e75 823 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
824 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
825
826 /* Wait for all sends and receives to complete */
827 begin = jiffies;
828
829 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
830 if (time_after(jiffies, begin + 5 * HZ)) {
831 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
832 priv->tx_head - priv->tx_tail, recvs_pending(dev));
833
834 /*
835 * assume the HW is wedged and just free up
836 * all our pending work requests.
837 */
2181858b 838 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
1da177e4 839 tx_req = &priv->tx_ring[priv->tx_tail &
0f485251 840 (ipoib_sendq_size - 1)];
7143740d 841 ipoib_dma_unmap_tx(priv->ca, tx_req);
1da177e4
LT
842 dev_kfree_skb_any(tx_req->skb);
843 ++priv->tx_tail;
1b524963 844 --priv->tx_outstanding;
1da177e4
LT
845 }
846
37ccf9df
RC
847 for (i = 0; i < ipoib_recvq_size; ++i) {
848 struct ipoib_rx_buf *rx_req;
849
850 rx_req = &priv->rx_ring[i];
851 if (!rx_req->skb)
852 continue;
bc7b3a36
SM
853 ipoib_ud_dma_unmap_rx(priv,
854 priv->rx_ring[i].mapping);
37ccf9df
RC
855 dev_kfree_skb_any(rx_req->skb);
856 rx_req->skb = NULL;
857 }
1da177e4
LT
858
859 goto timeout;
860 }
861
2dfbfc37 862 ipoib_drain_cq(dev);
8d1cc86a 863
1da177e4
LT
864 msleep(1);
865 }
866
867 ipoib_dbg(priv, "All sends and receives done.\n");
868
869timeout:
57ce41d1 870 del_timer_sync(&priv->poll_timer);
1da177e4 871 qp_attr.qp_state = IB_QPS_RESET;
3bc12e75 872 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
873 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
874
875 /* Wait for all AHs to be reaped */
876 set_bit(IPOIB_STOP_REAPER, &priv->flags);
877 cancel_delayed_work(&priv->ah_reap_task);
26bbf13c
YE
878 if (flush)
879 flush_workqueue(ipoib_workqueue);
1da177e4
LT
880
881 begin = jiffies;
882
883 while (!list_empty(&priv->dead_ahs)) {
884 __ipoib_reap_ah(dev);
885
886 if (time_after(jiffies, begin + HZ)) {
887 ipoib_warn(priv, "timing out; will leak address handles\n");
888 break;
889 }
890
891 msleep(1);
892 }
893
f56bcd80 894 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
8d1cc86a 895
1da177e4
LT
896 return 0;
897}
898
899int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
900{
901 struct ipoib_dev_priv *priv = netdev_priv(dev);
902
903 priv->ca = ca;
904 priv->port = port;
905 priv->qp = NULL;
906
907 if (ipoib_transport_dev_init(dev, ca)) {
908 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
909 return -ENODEV;
910 }
911
2767840a
RD
912 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
913 (unsigned long) dev);
914
1da177e4
LT
915 if (dev->flags & IFF_UP) {
916 if (ipoib_ib_dev_open(dev)) {
917 ipoib_transport_dev_cleanup(dev);
918 return -ENODEV;
919 }
920 }
921
922 return 0;
923}
924
ee1e2c82
MS
925static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
926 enum ipoib_flush_level level)
1da177e4 927{
26bbf13c 928 struct ipoib_dev_priv *cpriv;
c4028958 929 struct net_device *dev = priv->dev;
26bbf13c
YE
930 u16 new_index;
931
932 mutex_lock(&priv->vlan_mutex);
1da177e4 933
26bbf13c
YE
934 /*
935 * Flush any child interfaces too -- they might be up even if
936 * the parent is down.
937 */
938 list_for_each_entry(cpriv, &priv->child_intfs, list)
ee1e2c82 939 __ipoib_ib_dev_flush(cpriv, level);
26bbf13c
YE
940
941 mutex_unlock(&priv->vlan_mutex);
942
943 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
7a343d4c
LA
944 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
945 return;
946 }
947
948 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
949 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
1da177e4 950 return;
7a343d4c 951 }
1da177e4 952
ee1e2c82 953 if (level == IPOIB_FLUSH_HEAVY) {
26bbf13c
YE
954 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
955 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
956 ipoib_ib_dev_down(dev, 0);
167c4265 957 ipoib_ib_dev_stop(dev, 0);
9fdd5e5b
RD
958 if (ipoib_pkey_dev_delay_open(dev))
959 return;
26bbf13c 960 }
26bbf13c
YE
961
962 /* restart QP only if P_Key index is changed */
9fdd5e5b
RD
963 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
964 new_index == priv->pkey_index) {
26bbf13c
YE
965 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
966 return;
967 }
968 priv->pkey_index = new_index;
969 }
970
ee1e2c82
MS
971 if (level == IPOIB_FLUSH_LIGHT) {
972 ipoib_mark_paths_invalid(dev);
973 ipoib_mcast_dev_flush(dev);
974 }
1da177e4 975
ee1e2c82
MS
976 if (level >= IPOIB_FLUSH_NORMAL)
977 ipoib_ib_dev_down(dev, 0);
1da177e4 978
ee1e2c82 979 if (level == IPOIB_FLUSH_HEAVY) {
26bbf13c
YE
980 ipoib_ib_dev_stop(dev, 0);
981 ipoib_ib_dev_open(dev);
982 }
983
1da177e4
LT
984 /*
985 * The device could have been brought down between the start and when
986 * we get here, don't bring it back up if it's not configured up
987 */
5ccd0255 988 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
ee1e2c82
MS
989 if (level >= IPOIB_FLUSH_NORMAL)
990 ipoib_ib_dev_up(dev);
c4028958 991 ipoib_mcast_restart_task(&priv->restart_task);
5ccd0255 992 }
26bbf13c 993}
1da177e4 994
ee1e2c82
MS
995void ipoib_ib_dev_flush_light(struct work_struct *work)
996{
997 struct ipoib_dev_priv *priv =
998 container_of(work, struct ipoib_dev_priv, flush_light);
999
1000 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
1001}
1002
1003void ipoib_ib_dev_flush_normal(struct work_struct *work)
26bbf13c
YE
1004{
1005 struct ipoib_dev_priv *priv =
ee1e2c82 1006 container_of(work, struct ipoib_dev_priv, flush_normal);
4f71055a 1007
ee1e2c82 1008 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
26bbf13c 1009}
4f71055a 1010
ee1e2c82 1011void ipoib_ib_dev_flush_heavy(struct work_struct *work)
26bbf13c
YE
1012{
1013 struct ipoib_dev_priv *priv =
ee1e2c82 1014 container_of(work, struct ipoib_dev_priv, flush_heavy);
26bbf13c 1015
ee1e2c82 1016 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
1da177e4
LT
1017}
1018
1019void ipoib_ib_dev_cleanup(struct net_device *dev)
1020{
1021 struct ipoib_dev_priv *priv = netdev_priv(dev);
1022
1023 ipoib_dbg(priv, "cleaning up ib_dev\n");
1024
8d2cae06 1025 ipoib_mcast_stop_thread(dev, 1);
988bd503 1026 ipoib_mcast_dev_flush(dev);
1da177e4
LT
1027
1028 ipoib_transport_dev_cleanup(dev);
1029}
1030
1031/*
1032 * Delayed P_Key Assigment Interim Support
1033 *
1034 * The following is initial implementation of delayed P_Key assigment
1035 * mechanism. It is using the same approach implemented for the multicast
1036 * group join. The single goal of this implementation is to quickly address
1037 * Bug #2507. This implementation will probably be removed when the P_Key
1038 * change async notification is available.
1039 */
1da177e4 1040
c4028958 1041void ipoib_pkey_poll(struct work_struct *work)
1da177e4 1042{
c4028958 1043 struct ipoib_dev_priv *priv =
26bbf13c 1044 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
c4028958 1045 struct net_device *dev = priv->dev;
1da177e4
LT
1046
1047 ipoib_pkey_dev_check_presence(dev);
1048
1049 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1050 ipoib_open(dev);
1051 else {
95ed644f 1052 mutex_lock(&pkey_mutex);
1da177e4
LT
1053 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1054 queue_delayed_work(ipoib_workqueue,
26bbf13c 1055 &priv->pkey_poll_task,
1da177e4 1056 HZ);
95ed644f 1057 mutex_unlock(&pkey_mutex);
1da177e4
LT
1058 }
1059}
1060
1061int ipoib_pkey_dev_delay_open(struct net_device *dev)
1062{
1063 struct ipoib_dev_priv *priv = netdev_priv(dev);
1064
1065 /* Look for the interface pkey value in the IB Port P_Key table and */
1066 /* set the interface pkey assigment flag */
1067 ipoib_pkey_dev_check_presence(dev);
1068
1069 /* P_Key value not assigned yet - start polling */
1070 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 1071 mutex_lock(&pkey_mutex);
1da177e4
LT
1072 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1073 queue_delayed_work(ipoib_workqueue,
26bbf13c 1074 &priv->pkey_poll_task,
1da177e4 1075 HZ);
95ed644f 1076 mutex_unlock(&pkey_mutex);
1da177e4
LT
1077 return 1;
1078 }
1079
1080 return 0;
1081}