Merge branch 'nfs-for-2.6.32'
[pandora-kernel.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
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.
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.
34  */
35
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41
42 #include "ipoib.h"
43
44 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
45 static int data_debug_level;
46
47 module_param(data_debug_level, int, 0644);
48 MODULE_PARM_DESC(data_debug_level,
49                  "Enable data path debug tracing if > 0");
50 #endif
51
52 static DEFINE_MUTEX(pkey_mutex);
53
54 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
55                                  struct ib_pd *pd, struct ib_ah_attr *attr)
56 {
57         struct ipoib_ah *ah;
58
59         ah = kmalloc(sizeof *ah, GFP_KERNEL);
60         if (!ah)
61                 return NULL;
62
63         ah->dev       = dev;
64         ah->last_send = 0;
65         kref_init(&ah->ref);
66
67         ah->ah = ib_create_ah(pd, attr);
68         if (IS_ERR(ah->ah)) {
69                 kfree(ah);
70                 ah = NULL;
71         } else
72                 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
73
74         return ah;
75 }
76
77 void ipoib_free_ah(struct kref *kref)
78 {
79         struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
80         struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
81
82         unsigned long flags;
83
84         spin_lock_irqsave(&priv->lock, flags);
85         list_add_tail(&ah->list, &priv->dead_ahs);
86         spin_unlock_irqrestore(&priv->lock, flags);
87 }
88
89 static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
90                                   u64 mapping[IPOIB_UD_RX_SG])
91 {
92         if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
93                 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
94                                     DMA_FROM_DEVICE);
95                 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
96                                   DMA_FROM_DEVICE);
97         } else
98                 ib_dma_unmap_single(priv->ca, mapping[0],
99                                     IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
100                                     DMA_FROM_DEVICE);
101 }
102
103 static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
104                                    struct sk_buff *skb,
105                                    unsigned int length)
106 {
107         if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
108                 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
109                 unsigned int size;
110                 /*
111                  * There is only two buffers needed for max_payload = 4K,
112                  * first buf size is IPOIB_UD_HEAD_SIZE
113                  */
114                 skb->tail += IPOIB_UD_HEAD_SIZE;
115                 skb->len  += length;
116
117                 size = length - IPOIB_UD_HEAD_SIZE;
118
119                 frag->size     = size;
120                 skb->data_len += size;
121                 skb->truesize += size;
122         } else
123                 skb_put(skb, length);
124
125 }
126
127 static int ipoib_ib_post_receive(struct net_device *dev, int id)
128 {
129         struct ipoib_dev_priv *priv = netdev_priv(dev);
130         struct ib_recv_wr *bad_wr;
131         int ret;
132
133         priv->rx_wr.wr_id   = id | IPOIB_OP_RECV;
134         priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
135         priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
136
137
138         ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
139         if (unlikely(ret)) {
140                 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
141                 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
142                 dev_kfree_skb_any(priv->rx_ring[id].skb);
143                 priv->rx_ring[id].skb = NULL;
144         }
145
146         return ret;
147 }
148
149 static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
150 {
151         struct ipoib_dev_priv *priv = netdev_priv(dev);
152         struct sk_buff *skb;
153         int buf_size;
154         u64 *mapping;
155
156         if (ipoib_ud_need_sg(priv->max_ib_mtu))
157                 buf_size = IPOIB_UD_HEAD_SIZE;
158         else
159                 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
160
161         skb = dev_alloc_skb(buf_size + 4);
162         if (unlikely(!skb))
163                 return NULL;
164
165         /*
166          * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
167          * header.  So we need 4 more bytes to get to 48 and align the
168          * IP header to a multiple of 16.
169          */
170         skb_reserve(skb, 4);
171
172         mapping = priv->rx_ring[id].mapping;
173         mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
174                                        DMA_FROM_DEVICE);
175         if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
176                 goto error;
177
178         if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
179                 struct page *page = alloc_page(GFP_ATOMIC);
180                 if (!page)
181                         goto partial_error;
182                 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
183                 mapping[1] =
184                         ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
185                                         0, PAGE_SIZE, DMA_FROM_DEVICE);
186                 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
187                         goto partial_error;
188         }
189
190         priv->rx_ring[id].skb = skb;
191         return skb;
192
193 partial_error:
194         ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
195 error:
196         dev_kfree_skb_any(skb);
197         return NULL;
198 }
199
200 static int ipoib_ib_post_receives(struct net_device *dev)
201 {
202         struct ipoib_dev_priv *priv = netdev_priv(dev);
203         int i;
204
205         for (i = 0; i < ipoib_recvq_size; ++i) {
206                 if (!ipoib_alloc_rx_skb(dev, i)) {
207                         ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
208                         return -ENOMEM;
209                 }
210                 if (ipoib_ib_post_receive(dev, i)) {
211                         ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
212                         return -EIO;
213                 }
214         }
215
216         return 0;
217 }
218
219 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
220 {
221         struct ipoib_dev_priv *priv = netdev_priv(dev);
222         unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
223         struct sk_buff *skb;
224         u64 mapping[IPOIB_UD_RX_SG];
225
226         ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
227                        wr_id, wc->status);
228
229         if (unlikely(wr_id >= ipoib_recvq_size)) {
230                 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
231                            wr_id, ipoib_recvq_size);
232                 return;
233         }
234
235         skb  = priv->rx_ring[wr_id].skb;
236
237         if (unlikely(wc->status != IB_WC_SUCCESS)) {
238                 if (wc->status != IB_WC_WR_FLUSH_ERR)
239                         ipoib_warn(priv, "failed recv event "
240                                    "(status=%d, wrid=%d vend_err %x)\n",
241                                    wc->status, wr_id, wc->vendor_err);
242                 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
243                 dev_kfree_skb_any(skb);
244                 priv->rx_ring[wr_id].skb = NULL;
245                 return;
246         }
247
248         /*
249          * Drop packets that this interface sent, ie multicast packets
250          * that the HCA has replicated.
251          */
252         if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
253                 goto repost;
254
255         memcpy(mapping, priv->rx_ring[wr_id].mapping,
256                IPOIB_UD_RX_SG * sizeof *mapping);
257
258         /*
259          * If we can't allocate a new RX buffer, dump
260          * this packet and reuse the old buffer.
261          */
262         if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
263                 ++dev->stats.rx_dropped;
264                 goto repost;
265         }
266
267         ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
268                        wc->byte_len, wc->slid);
269
270         ipoib_ud_dma_unmap_rx(priv, mapping);
271         ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
272
273         skb_pull(skb, IB_GRH_BYTES);
274
275         skb->protocol = ((struct ipoib_header *) skb->data)->proto;
276         skb_reset_mac_header(skb);
277         skb_pull(skb, IPOIB_ENCAP_LEN);
278
279         dev->last_rx = jiffies;
280         ++dev->stats.rx_packets;
281         dev->stats.rx_bytes += skb->len;
282
283         skb->dev = dev;
284         /* XXX get correct PACKET_ type here */
285         skb->pkt_type = PACKET_HOST;
286
287         if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
288                 skb->ip_summed = CHECKSUM_UNNECESSARY;
289
290         if (dev->features & NETIF_F_LRO)
291                 lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
292         else
293                 netif_receive_skb(skb);
294
295 repost:
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 }
300
301 static 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;
307         int off;
308
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;
318
319         for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
320                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
321                 mapping[i + off] = ib_dma_map_page(ca, frag->page,
322                                                  frag->page_offset, frag->size,
323                                                  DMA_TO_DEVICE);
324                 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
325                         goto partial_error;
326         }
327         return 0;
328
329 partial_error:
330         for (; i > 0; --i) {
331                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
332                 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
333         }
334
335         if (off)
336                 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
337
338         return -EIO;
339 }
340
341 static 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;
347         int off;
348
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;
354
355         for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
356                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
357                 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
358                                   DMA_TO_DEVICE);
359         }
360 }
361
362 static 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;
367
368         ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
369                        wr_id, wc->status);
370
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;
375         }
376
377         tx_req = &priv->tx_ring[wr_id];
378
379         ipoib_dma_unmap_tx(priv->ca, tx_req);
380
381         ++dev->stats.tx_packets;
382         dev->stats.tx_bytes += tx_req->skb->len;
383
384         dev_kfree_skb_any(tx_req->skb);
385
386         ++priv->tx_tail;
387         if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
388             netif_queue_stopped(dev) &&
389             test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
390                 netif_wake_queue(dev);
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
399 static 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
410 int ipoib_poll(struct napi_struct *napi, int budget)
411 {
412         struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
413         struct net_device *dev = priv->dev;
414         int done;
415         int t;
416         int n, i;
417
418         done  = 0;
419
420 poll_more:
421         while (done < budget) {
422                 int max = (budget - done);
423
424                 t = min(IPOIB_NUM_WC, max);
425                 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
426
427                 for (i = 0; i < n; i++) {
428                         struct ib_wc *wc = priv->ibwc + i;
429
430                         if (wc->wr_id & IPOIB_OP_RECV) {
431                                 ++done;
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);
436                         } else
437                                 ipoib_cm_handle_tx_wc(priv->dev, wc);
438                 }
439
440                 if (n != t)
441                         break;
442         }
443
444         if (done < budget) {
445                 if (dev->features & NETIF_F_LRO)
446                         lro_flush_all(&priv->lro.lro_mgr);
447
448                 napi_complete(napi);
449                 if (unlikely(ib_req_notify_cq(priv->recv_cq,
450                                               IB_CQ_NEXT_COMP |
451                                               IB_CQ_REPORT_MISSED_EVENTS)) &&
452                     napi_reschedule(napi))
453                         goto poll_more;
454         }
455
456         return done;
457 }
458
459 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
460 {
461         struct net_device *dev = dev_ptr;
462         struct ipoib_dev_priv *priv = netdev_priv(dev);
463
464         napi_schedule(&priv->napi);
465 }
466
467 static void drain_tx_cq(struct net_device *dev)
468 {
469         struct ipoib_dev_priv *priv = netdev_priv(dev);
470
471         netif_tx_lock(dev);
472         while (poll_tx(priv))
473                 ; /* nothing */
474
475         if (netif_queue_stopped(dev))
476                 mod_timer(&priv->poll_timer, jiffies + 1);
477
478         netif_tx_unlock(dev);
479 }
480
481 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
482 {
483         struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
484
485         mod_timer(&priv->poll_timer, jiffies);
486 }
487
488 static inline int post_send(struct ipoib_dev_priv *priv,
489                             unsigned int wr_id,
490                             struct ib_ah *address, u32 qpn,
491                             struct ipoib_tx_buf *tx_req,
492                             void *head, int hlen)
493 {
494         struct ib_send_wr *bad_wr;
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;
507
508         for (i = 0; i < nr_frags; ++i) {
509                 priv->tx_sge[i + off].addr = mapping[i + off];
510                 priv->tx_sge[i + off].length = frags[i].size;
511         }
512         priv->tx_wr.num_sge          = nr_frags + off;
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;
516
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
525         return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
526 }
527
528 void 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);
532         struct ipoib_tx_buf *tx_req;
533         int hlen;
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;
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          */
569         tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
570         tx_req->skb = skb;
571         if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
572                 ++dev->stats.tx_errors;
573                 dev_kfree_skb_any(skb);
574                 return;
575         }
576
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
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
589         if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
590                                address->ah, qpn, tx_req, phead, hlen))) {
591                 ipoib_warn(priv, "post_send failed\n");
592                 ++dev->stats.tx_errors;
593                 --priv->tx_outstanding;
594                 ipoib_dma_unmap_tx(priv->ca, tx_req);
595                 dev_kfree_skb_any(skb);
596                 if (netif_queue_stopped(dev))
597                         netif_wake_queue(dev);
598         } else {
599                 dev->trans_start = jiffies;
600
601                 address->last_send = priv->tx_head;
602                 ++priv->tx_head;
603                 skb_orphan(skb);
604
605         }
606
607         if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
608                 while (poll_tx(priv))
609                         ; /* nothing */
610 }
611
612 static void __ipoib_reap_ah(struct net_device *dev)
613 {
614         struct ipoib_dev_priv *priv = netdev_priv(dev);
615         struct ipoib_ah *ah, *tah;
616         LIST_HEAD(remove_list);
617         unsigned long flags;
618
619         netif_tx_lock_bh(dev);
620         spin_lock_irqsave(&priv->lock, flags);
621
622         list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
623                 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
624                         list_del(&ah->list);
625                         ib_destroy_ah(ah->ah);
626                         kfree(ah);
627                 }
628
629         spin_unlock_irqrestore(&priv->lock, flags);
630         netif_tx_unlock_bh(dev);
631 }
632
633 void ipoib_reap_ah(struct work_struct *work)
634 {
635         struct ipoib_dev_priv *priv =
636                 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
637         struct net_device *dev = priv->dev;
638
639         __ipoib_reap_ah(dev);
640
641         if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
642                 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
643                                    round_jiffies_relative(HZ));
644 }
645
646 static void ipoib_ib_tx_timer_func(unsigned long ctx)
647 {
648         drain_tx_cq((struct net_device *)ctx);
649 }
650
651 int ipoib_ib_dev_open(struct net_device *dev)
652 {
653         struct ipoib_dev_priv *priv = netdev_priv(dev);
654         int ret;
655
656         if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
657                 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
658                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
659                 return -1;
660         }
661         set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
662
663         ret = ipoib_init_qp(dev);
664         if (ret) {
665                 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
666                 return -1;
667         }
668
669         ret = ipoib_ib_post_receives(dev);
670         if (ret) {
671                 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
672                 ipoib_ib_dev_stop(dev, 1);
673                 return -1;
674         }
675
676         ret = ipoib_cm_dev_open(dev);
677         if (ret) {
678                 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
679                 ipoib_ib_dev_stop(dev, 1);
680                 return -1;
681         }
682
683         clear_bit(IPOIB_STOP_REAPER, &priv->flags);
684         queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
685                            round_jiffies_relative(HZ));
686
687         if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
688                 napi_enable(&priv->napi);
689
690         return 0;
691 }
692
693 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
694 {
695         struct ipoib_dev_priv *priv = netdev_priv(dev);
696         u16 pkey_index = 0;
697
698         if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
699                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
700         else
701                 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
702 }
703
704 int ipoib_ib_dev_up(struct net_device *dev)
705 {
706         struct ipoib_dev_priv *priv = netdev_priv(dev);
707
708         ipoib_pkey_dev_check_presence(dev);
709
710         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
711                 ipoib_dbg(priv, "PKEY is not assigned.\n");
712                 return 0;
713         }
714
715         set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
716
717         return ipoib_mcast_start_thread(dev);
718 }
719
720 int ipoib_ib_dev_down(struct net_device *dev, int flush)
721 {
722         struct ipoib_dev_priv *priv = netdev_priv(dev);
723
724         ipoib_dbg(priv, "downing ib_dev\n");
725
726         clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
727         netif_carrier_off(dev);
728
729         /* Shutdown the P_Key thread if still active */
730         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
731                 mutex_lock(&pkey_mutex);
732                 set_bit(IPOIB_PKEY_STOP, &priv->flags);
733                 cancel_delayed_work(&priv->pkey_poll_task);
734                 mutex_unlock(&pkey_mutex);
735                 if (flush)
736                         flush_workqueue(ipoib_workqueue);
737         }
738
739         ipoib_mcast_stop_thread(dev, flush);
740         ipoib_mcast_dev_flush(dev);
741
742         ipoib_flush_paths(dev);
743
744         return 0;
745 }
746
747 static int recvs_pending(struct net_device *dev)
748 {
749         struct ipoib_dev_priv *priv = netdev_priv(dev);
750         int pending = 0;
751         int i;
752
753         for (i = 0; i < ipoib_recvq_size; ++i)
754                 if (priv->rx_ring[i].skb)
755                         ++pending;
756
757         return pending;
758 }
759
760 void ipoib_drain_cq(struct net_device *dev)
761 {
762         struct ipoib_dev_priv *priv = netdev_priv(dev);
763         int i, n;
764
765         /*
766          * We call completion handling routines that expect to be
767          * called from the BH-disabled NAPI poll context, so disable
768          * BHs here too.
769          */
770         local_bh_disable();
771
772         do {
773                 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
774                 for (i = 0; i < n; ++i) {
775                         /*
776                          * Convert any successful completions to flush
777                          * errors to avoid passing packets up the
778                          * stack after bringing the device down.
779                          */
780                         if (priv->ibwc[i].status == IB_WC_SUCCESS)
781                                 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
782
783                         if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
784                                 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
785                                         ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
786                                 else
787                                         ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
788                         } else
789                                 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
790                 }
791         } while (n == IPOIB_NUM_WC);
792
793         while (poll_tx(priv))
794                 ; /* nothing */
795
796         local_bh_enable();
797 }
798
799 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
800 {
801         struct ipoib_dev_priv *priv = netdev_priv(dev);
802         struct ib_qp_attr qp_attr;
803         unsigned long begin;
804         struct ipoib_tx_buf *tx_req;
805         int i;
806
807         if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
808                 napi_disable(&priv->napi);
809
810         ipoib_cm_dev_stop(dev);
811
812         /*
813          * Move our QP to the error state and then reinitialize in
814          * when all work requests have completed or have been flushed.
815          */
816         qp_attr.qp_state = IB_QPS_ERR;
817         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
818                 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
819
820         /* Wait for all sends and receives to complete */
821         begin = jiffies;
822
823         while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
824                 if (time_after(jiffies, begin + 5 * HZ)) {
825                         ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
826                                    priv->tx_head - priv->tx_tail, recvs_pending(dev));
827
828                         /*
829                          * assume the HW is wedged and just free up
830                          * all our pending work requests.
831                          */
832                         while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
833                                 tx_req = &priv->tx_ring[priv->tx_tail &
834                                                         (ipoib_sendq_size - 1)];
835                                 ipoib_dma_unmap_tx(priv->ca, tx_req);
836                                 dev_kfree_skb_any(tx_req->skb);
837                                 ++priv->tx_tail;
838                                 --priv->tx_outstanding;
839                         }
840
841                         for (i = 0; i < ipoib_recvq_size; ++i) {
842                                 struct ipoib_rx_buf *rx_req;
843
844                                 rx_req = &priv->rx_ring[i];
845                                 if (!rx_req->skb)
846                                         continue;
847                                 ipoib_ud_dma_unmap_rx(priv,
848                                                       priv->rx_ring[i].mapping);
849                                 dev_kfree_skb_any(rx_req->skb);
850                                 rx_req->skb = NULL;
851                         }
852
853                         goto timeout;
854                 }
855
856                 ipoib_drain_cq(dev);
857
858                 msleep(1);
859         }
860
861         ipoib_dbg(priv, "All sends and receives done.\n");
862
863 timeout:
864         del_timer_sync(&priv->poll_timer);
865         qp_attr.qp_state = IB_QPS_RESET;
866         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
867                 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
868
869         /* Wait for all AHs to be reaped */
870         set_bit(IPOIB_STOP_REAPER, &priv->flags);
871         cancel_delayed_work(&priv->ah_reap_task);
872         if (flush)
873                 flush_workqueue(ipoib_workqueue);
874
875         begin = jiffies;
876
877         while (!list_empty(&priv->dead_ahs)) {
878                 __ipoib_reap_ah(dev);
879
880                 if (time_after(jiffies, begin + HZ)) {
881                         ipoib_warn(priv, "timing out; will leak address handles\n");
882                         break;
883                 }
884
885                 msleep(1);
886         }
887
888         ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
889
890         return 0;
891 }
892
893 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
894 {
895         struct ipoib_dev_priv *priv = netdev_priv(dev);
896
897         priv->ca = ca;
898         priv->port = port;
899         priv->qp = NULL;
900
901         if (ipoib_transport_dev_init(dev, ca)) {
902                 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
903                 return -ENODEV;
904         }
905
906         setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
907                     (unsigned long) dev);
908
909         if (dev->flags & IFF_UP) {
910                 if (ipoib_ib_dev_open(dev)) {
911                         ipoib_transport_dev_cleanup(dev);
912                         return -ENODEV;
913                 }
914         }
915
916         return 0;
917 }
918
919 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
920                                 enum ipoib_flush_level level)
921 {
922         struct ipoib_dev_priv *cpriv;
923         struct net_device *dev = priv->dev;
924         u16 new_index;
925
926         mutex_lock(&priv->vlan_mutex);
927
928         /*
929          * Flush any child interfaces too -- they might be up even if
930          * the parent is down.
931          */
932         list_for_each_entry(cpriv, &priv->child_intfs, list)
933                 __ipoib_ib_dev_flush(cpriv, level);
934
935         mutex_unlock(&priv->vlan_mutex);
936
937         if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
938                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
939                 return;
940         }
941
942         if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
943                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
944                 return;
945         }
946
947         if (level == IPOIB_FLUSH_HEAVY) {
948                 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
949                         clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
950                         ipoib_ib_dev_down(dev, 0);
951                         ipoib_ib_dev_stop(dev, 0);
952                         if (ipoib_pkey_dev_delay_open(dev))
953                                 return;
954                 }
955
956                 /* restart QP only if P_Key index is changed */
957                 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
958                     new_index == priv->pkey_index) {
959                         ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
960                         return;
961                 }
962                 priv->pkey_index = new_index;
963         }
964
965         if (level == IPOIB_FLUSH_LIGHT) {
966                 ipoib_mark_paths_invalid(dev);
967                 ipoib_mcast_dev_flush(dev);
968         }
969
970         if (level >= IPOIB_FLUSH_NORMAL)
971                 ipoib_ib_dev_down(dev, 0);
972
973         if (level == IPOIB_FLUSH_HEAVY) {
974                 ipoib_ib_dev_stop(dev, 0);
975                 ipoib_ib_dev_open(dev);
976         }
977
978         /*
979          * The device could have been brought down between the start and when
980          * we get here, don't bring it back up if it's not configured up
981          */
982         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
983                 if (level >= IPOIB_FLUSH_NORMAL)
984                         ipoib_ib_dev_up(dev);
985                 ipoib_mcast_restart_task(&priv->restart_task);
986         }
987 }
988
989 void ipoib_ib_dev_flush_light(struct work_struct *work)
990 {
991         struct ipoib_dev_priv *priv =
992                 container_of(work, struct ipoib_dev_priv, flush_light);
993
994         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
995 }
996
997 void ipoib_ib_dev_flush_normal(struct work_struct *work)
998 {
999         struct ipoib_dev_priv *priv =
1000                 container_of(work, struct ipoib_dev_priv, flush_normal);
1001
1002         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1003 }
1004
1005 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1006 {
1007         struct ipoib_dev_priv *priv =
1008                 container_of(work, struct ipoib_dev_priv, flush_heavy);
1009
1010         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
1011 }
1012
1013 void ipoib_ib_dev_cleanup(struct net_device *dev)
1014 {
1015         struct ipoib_dev_priv *priv = netdev_priv(dev);
1016
1017         ipoib_dbg(priv, "cleaning up ib_dev\n");
1018
1019         ipoib_mcast_stop_thread(dev, 1);
1020         ipoib_mcast_dev_flush(dev);
1021
1022         ipoib_transport_dev_cleanup(dev);
1023 }
1024
1025 /*
1026  * Delayed P_Key Assigment Interim Support
1027  *
1028  * The following is initial implementation of delayed P_Key assigment
1029  * mechanism. It is using the same approach implemented for the multicast
1030  * group join. The single goal of this implementation is to quickly address
1031  * Bug #2507. This implementation will probably be removed when the P_Key
1032  * change async notification is available.
1033  */
1034
1035 void ipoib_pkey_poll(struct work_struct *work)
1036 {
1037         struct ipoib_dev_priv *priv =
1038                 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
1039         struct net_device *dev = priv->dev;
1040
1041         ipoib_pkey_dev_check_presence(dev);
1042
1043         if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1044                 ipoib_open(dev);
1045         else {
1046                 mutex_lock(&pkey_mutex);
1047                 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1048                         queue_delayed_work(ipoib_workqueue,
1049                                            &priv->pkey_poll_task,
1050                                            HZ);
1051                 mutex_unlock(&pkey_mutex);
1052         }
1053 }
1054
1055 int ipoib_pkey_dev_delay_open(struct net_device *dev)
1056 {
1057         struct ipoib_dev_priv *priv = netdev_priv(dev);
1058
1059         /* Look for the interface pkey value in the IB Port P_Key table and */
1060         /* set the interface pkey assigment flag                            */
1061         ipoib_pkey_dev_check_presence(dev);
1062
1063         /* P_Key value not assigned yet - start polling */
1064         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
1065                 mutex_lock(&pkey_mutex);
1066                 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1067                 queue_delayed_work(ipoib_workqueue,
1068                                    &priv->pkey_poll_task,
1069                                    HZ);
1070                 mutex_unlock(&pkey_mutex);
1071                 return 1;
1072         }
1073
1074         return 0;
1075 }