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