include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
index 8238766..ec6b4fb 100644 (file)
@@ -1,5 +1,8 @@
 /*
  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
+ * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
  */
 
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
+#include <linux/slab.h>
 
-#include <ib_cache.h>
+#include <linux/ip.h>
+#include <linux/tcp.h>
 
 #include "ipoib.h"
 
@@ -47,9 +50,7 @@ MODULE_PARM_DESC(data_debug_level,
                 "Enable data path debug tracing if > 0");
 #endif
 
-#define        IPOIB_OP_RECV   (1ul << 31)
-
-static DECLARE_MUTEX(pkey_sem);
+static DEFINE_MUTEX(pkey_mutex);
 
 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
                                 struct ib_pd *pd, struct ib_ah_attr *attr)
@@ -81,76 +82,132 @@ void ipoib_free_ah(struct kref *kref)
 
        unsigned long flags;
 
-       if (ah->last_send <= priv->tx_tail) {
-               ipoib_dbg(priv, "Freeing ah %p\n", ah->ah);
-               ib_destroy_ah(ah->ah);
-               kfree(ah);
-       } else {
-               spin_lock_irqsave(&priv->lock, flags);
-               list_add_tail(&ah->list, &priv->dead_ahs);
-               spin_unlock_irqrestore(&priv->lock, flags);
-       }
-}
-
-static inline int ipoib_ib_receive(struct ipoib_dev_priv *priv,
-                                  unsigned int wr_id,
-                                  dma_addr_t addr)
-{
-       struct ib_sge list = {
-               .addr    = addr,
-               .length  = IPOIB_BUF_SIZE,
-               .lkey    = priv->mr->lkey,
-       };
-       struct ib_recv_wr param = {
-               .wr_id      = wr_id | IPOIB_OP_RECV,
-               .sg_list    = &list,
-               .num_sge    = 1,
-       };
-       struct ib_recv_wr *bad_wr;
+       spin_lock_irqsave(&priv->lock, flags);
+       list_add_tail(&ah->list, &priv->dead_ahs);
+       spin_unlock_irqrestore(&priv->lock, flags);
+}
+
+static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
+                                 u64 mapping[IPOIB_UD_RX_SG])
+{
+       if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
+               ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
+                                   DMA_FROM_DEVICE);
+               ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
+                                 DMA_FROM_DEVICE);
+       } else
+               ib_dma_unmap_single(priv->ca, mapping[0],
+                                   IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
+                                   DMA_FROM_DEVICE);
+}
+
+static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
+                                  struct sk_buff *skb,
+                                  unsigned int length)
+{
+       if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
+               skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
+               unsigned int size;
+               /*
+                * There is only two buffers needed for max_payload = 4K,
+                * first buf size is IPOIB_UD_HEAD_SIZE
+                */
+               skb->tail += IPOIB_UD_HEAD_SIZE;
+               skb->len  += length;
+
+               size = length - IPOIB_UD_HEAD_SIZE;
+
+               frag->size     = size;
+               skb->data_len += size;
+               skb->truesize += size;
+       } else
+               skb_put(skb, length);
 
-       return ib_post_recv(priv->qp, &param, &bad_wr);
 }
 
 static int ipoib_ib_post_receive(struct net_device *dev, int id)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct sk_buff *skb;
-       dma_addr_t addr;
+       struct ib_recv_wr *bad_wr;
        int ret;
 
-       skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
-       if (!skb) {
-               ipoib_warn(priv, "failed to allocate receive buffer\n");
+       priv->rx_wr.wr_id   = id | IPOIB_OP_RECV;
+       priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
+       priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
 
-               priv->rx_ring[id].skb = NULL;
-               return -ENOMEM;
-       }
-       skb_reserve(skb, 4);    /* 16 byte align IP header */
-       priv->rx_ring[id].skb = skb;
-       addr = dma_map_single(priv->ca->dma_device,
-                             skb->data, IPOIB_BUF_SIZE,
-                             DMA_FROM_DEVICE);
-       pci_unmap_addr_set(&priv->rx_ring[id], mapping, addr);
 
-       ret = ipoib_ib_receive(priv, id, addr);
-       if (ret) {
-               ipoib_warn(priv, "ipoib_ib_receive failed for buf %d (%d)\n",
-                          id, ret);
-               dma_unmap_single(priv->ca->dma_device, addr,
-                                IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
-               dev_kfree_skb_any(skb);
+       ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
+       if (unlikely(ret)) {
+               ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
+               ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
+               dev_kfree_skb_any(priv->rx_ring[id].skb);
                priv->rx_ring[id].skb = NULL;
        }
 
        return ret;
 }
 
+static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct sk_buff *skb;
+       int buf_size;
+       u64 *mapping;
+
+       if (ipoib_ud_need_sg(priv->max_ib_mtu))
+               buf_size = IPOIB_UD_HEAD_SIZE;
+       else
+               buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
+
+       skb = dev_alloc_skb(buf_size + 4);
+       if (unlikely(!skb))
+               return NULL;
+
+       /*
+        * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
+        * header.  So we need 4 more bytes to get to 48 and align the
+        * IP header to a multiple of 16.
+        */
+       skb_reserve(skb, 4);
+
+       mapping = priv->rx_ring[id].mapping;
+       mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
+                                      DMA_FROM_DEVICE);
+       if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
+               goto error;
+
+       if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
+               struct page *page = alloc_page(GFP_ATOMIC);
+               if (!page)
+                       goto partial_error;
+               skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
+               mapping[1] =
+                       ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
+                                       0, PAGE_SIZE, DMA_FROM_DEVICE);
+               if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
+                       goto partial_error;
+       }
+
+       priv->rx_ring[id].skb = skb;
+       return skb;
+
+partial_error:
+       ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
+error:
+       dev_kfree_skb_any(skb);
+       return NULL;
+}
+
 static int ipoib_ib_post_receives(struct net_device *dev)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        int i;
 
-       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) {
+       for (i = 0; i < ipoib_recvq_size; ++i) {
+               if (!ipoib_alloc_rx_skb(dev, i)) {
+                       ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
+                       return -ENOMEM;
+               }
                if (ipoib_ib_post_receive(dev, i)) {
                        ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
                        return -EIO;
@@ -160,137 +217,310 @@ static int ipoib_ib_post_receives(struct net_device *dev)
        return 0;
 }
 
-static void ipoib_ib_handle_wc(struct net_device *dev,
-                              struct ib_wc *wc)
+static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
+       struct sk_buff *skb;
+       u64 mapping[IPOIB_UD_RX_SG];
+
+       ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
+                      wr_id, wc->status);
+
+       if (unlikely(wr_id >= ipoib_recvq_size)) {
+               ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
+                          wr_id, ipoib_recvq_size);
+               return;
+       }
+
+       skb  = priv->rx_ring[wr_id].skb;
+
+       if (unlikely(wc->status != IB_WC_SUCCESS)) {
+               if (wc->status != IB_WC_WR_FLUSH_ERR)
+                       ipoib_warn(priv, "failed recv event "
+                                  "(status=%d, wrid=%d vend_err %x)\n",
+                                  wc->status, wr_id, wc->vendor_err);
+               ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
+               dev_kfree_skb_any(skb);
+               priv->rx_ring[wr_id].skb = NULL;
+               return;
+       }
+
+       /*
+        * Drop packets that this interface sent, ie multicast packets
+        * that the HCA has replicated.
+        */
+       if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
+               goto repost;
+
+       memcpy(mapping, priv->rx_ring[wr_id].mapping,
+              IPOIB_UD_RX_SG * sizeof *mapping);
+
+       /*
+        * If we can't allocate a new RX buffer, dump
+        * this packet and reuse the old buffer.
+        */
+       if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
+               ++dev->stats.rx_dropped;
+               goto repost;
+       }
+
+       ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
+                      wc->byte_len, wc->slid);
+
+       ipoib_ud_dma_unmap_rx(priv, mapping);
+       ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
+
+       skb_pull(skb, IB_GRH_BYTES);
+
+       skb->protocol = ((struct ipoib_header *) skb->data)->proto;
+       skb_reset_mac_header(skb);
+       skb_pull(skb, IPOIB_ENCAP_LEN);
+
+       ++dev->stats.rx_packets;
+       dev->stats.rx_bytes += skb->len;
+
+       skb->dev = dev;
+       /* XXX get correct PACKET_ type here */
+       skb->pkt_type = PACKET_HOST;
+
+       if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
+               skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+       if (dev->features & NETIF_F_LRO)
+               lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
+       else
+               netif_receive_skb(skb);
+
+repost:
+       if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
+               ipoib_warn(priv, "ipoib_ib_post_receive failed "
+                          "for buf %d\n", wr_id);
+}
+
+static int ipoib_dma_map_tx(struct ib_device *ca,
+                           struct ipoib_tx_buf *tx_req)
+{
+       struct sk_buff *skb = tx_req->skb;
+       u64 *mapping = tx_req->mapping;
+       int i;
+       int off;
+
+       if (skb_headlen(skb)) {
+               mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
+                                              DMA_TO_DEVICE);
+               if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
+                       return -EIO;
+
+               off = 1;
+       } else
+               off = 0;
+
+       for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
+               skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
+               mapping[i + off] = ib_dma_map_page(ca, frag->page,
+                                                frag->page_offset, frag->size,
+                                                DMA_TO_DEVICE);
+               if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
+                       goto partial_error;
+       }
+       return 0;
+
+partial_error:
+       for (; i > 0; --i) {
+               skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
+               ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
+       }
+
+       if (off)
+               ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
+
+       return -EIO;
+}
+
+static void ipoib_dma_unmap_tx(struct ib_device *ca,
+                              struct ipoib_tx_buf *tx_req)
+{
+       struct sk_buff *skb = tx_req->skb;
+       u64 *mapping = tx_req->mapping;
+       int i;
+       int off;
+
+       if (skb_headlen(skb)) {
+               ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
+               off = 1;
+       } else
+               off = 0;
+
+       for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
+               skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
+               ib_dma_unmap_page(ca, mapping[i + off], frag->size,
+                                 DMA_TO_DEVICE);
+       }
+}
+
+static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        unsigned int wr_id = wc->wr_id;
+       struct ipoib_tx_buf *tx_req;
 
-       ipoib_dbg_data(priv, "called: id %d, op %d, status: %d\n",
-                      wr_id, wc->opcode, wc->status);
+       ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
+                      wr_id, wc->status);
 
-       if (wr_id & IPOIB_OP_RECV) {
-               wr_id &= ~IPOIB_OP_RECV;
+       if (unlikely(wr_id >= ipoib_sendq_size)) {
+               ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
+                          wr_id, ipoib_sendq_size);
+               return;
+       }
 
-               if (wr_id < IPOIB_RX_RING_SIZE) {
-                       struct sk_buff *skb = priv->rx_ring[wr_id].skb;
+       tx_req = &priv->tx_ring[wr_id];
 
-                       priv->rx_ring[wr_id].skb = NULL;
+       ipoib_dma_unmap_tx(priv->ca, tx_req);
 
-                       dma_unmap_single(priv->ca->dma_device,
-                                        pci_unmap_addr(&priv->rx_ring[wr_id],
-                                                       mapping),
-                                        IPOIB_BUF_SIZE,
-                                        DMA_FROM_DEVICE);
+       ++dev->stats.tx_packets;
+       dev->stats.tx_bytes += tx_req->skb->len;
 
-                       if (wc->status != IB_WC_SUCCESS) {
-                               if (wc->status != IB_WC_WR_FLUSH_ERR)
-                                       ipoib_warn(priv, "failed recv event "
-                                                  "(status=%d, wrid=%d vend_err %x)\n",
-                                                  wc->status, wr_id, wc->vendor_err);
-                               dev_kfree_skb_any(skb);
-                               return;
-                       }
+       dev_kfree_skb_any(tx_req->skb);
 
-                       ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
-                                      wc->byte_len, wc->slid);
-
-                       skb_put(skb, wc->byte_len);
-                       skb_pull(skb, IB_GRH_BYTES);
-
-                       if (wc->slid != priv->local_lid ||
-                           wc->src_qp != priv->qp->qp_num) {
-                               skb->protocol = ((struct ipoib_header *) skb->data)->proto;
-                               skb->mac.raw = skb->data;
-                               skb_pull(skb, IPOIB_ENCAP_LEN);
-
-                               dev->last_rx = jiffies;
-                               ++priv->stats.rx_packets;
-                               priv->stats.rx_bytes += skb->len;
-
-                               skb->dev = dev;
-                               /* XXX get correct PACKET_ type here */
-                               skb->pkt_type = PACKET_HOST;
-                               netif_rx_ni(skb);
-                       } else {
-                               ipoib_dbg_data(priv, "dropping loopback packet\n");
-                               dev_kfree_skb_any(skb);
-                       }
+       ++priv->tx_tail;
+       if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
+           netif_queue_stopped(dev) &&
+           test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
+               netif_wake_queue(dev);
 
-                       /* repost receive */
-                       if (ipoib_ib_post_receive(dev, wr_id))
-                               ipoib_warn(priv, "ipoib_ib_post_receive failed "
-                                          "for buf %d\n", wr_id);
-               } else
-                       ipoib_warn(priv, "completion event with wrid %d\n",
-                                  wr_id);
+       if (wc->status != IB_WC_SUCCESS &&
+           wc->status != IB_WC_WR_FLUSH_ERR)
+               ipoib_warn(priv, "failed send event "
+                          "(status=%d, wrid=%d vend_err %x)\n",
+                          wc->status, wr_id, wc->vendor_err);
+}
 
-       } else {
-               struct ipoib_buf *tx_req;
-               unsigned long flags;
+static int poll_tx(struct ipoib_dev_priv *priv)
+{
+       int n, i;
 
-               if (wr_id >= IPOIB_TX_RING_SIZE) {
-                       ipoib_warn(priv, "completion event with wrid %d (> %d)\n",
-                                  wr_id, IPOIB_TX_RING_SIZE);
-                       return;
-               }
+       n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
+       for (i = 0; i < n; ++i)
+               ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
 
-               ipoib_dbg_data(priv, "send complete, wrid %d\n", wr_id);
+       return n == MAX_SEND_CQE;
+}
 
-               tx_req = &priv->tx_ring[wr_id];
+int ipoib_poll(struct napi_struct *napi, int budget)
+{
+       struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
+       struct net_device *dev = priv->dev;
+       int done;
+       int t;
+       int n, i;
 
-               dma_unmap_single(priv->ca->dma_device,
-                                pci_unmap_addr(tx_req, mapping),
-                                tx_req->skb->len,
-                                DMA_TO_DEVICE);
+       done  = 0;
 
-               ++priv->stats.tx_packets;
-               priv->stats.tx_bytes += tx_req->skb->len;
+poll_more:
+       while (done < budget) {
+               int max = (budget - done);
 
-               dev_kfree_skb_any(tx_req->skb);
+               t = min(IPOIB_NUM_WC, max);
+               n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
 
-               spin_lock_irqsave(&priv->tx_lock, flags);
-               ++priv->tx_tail;
-               if (netif_queue_stopped(dev) &&
-                   priv->tx_head - priv->tx_tail <= IPOIB_TX_RING_SIZE / 2)
-                       netif_wake_queue(dev);
-               spin_unlock_irqrestore(&priv->tx_lock, flags);
+               for (i = 0; i < n; i++) {
+                       struct ib_wc *wc = priv->ibwc + i;
 
-               if (wc->status != IB_WC_SUCCESS &&
-                   wc->status != IB_WC_WR_FLUSH_ERR)
-                       ipoib_warn(priv, "failed send event "
-                                  "(status=%d, wrid=%d vend_err %x)\n",
-                                  wc->status, wr_id, wc->vendor_err);
+                       if (wc->wr_id & IPOIB_OP_RECV) {
+                               ++done;
+                               if (wc->wr_id & IPOIB_OP_CM)
+                                       ipoib_cm_handle_rx_wc(dev, wc);
+                               else
+                                       ipoib_ib_handle_rx_wc(dev, wc);
+                       } else
+                               ipoib_cm_handle_tx_wc(priv->dev, wc);
+               }
+
+               if (n != t)
+                       break;
+       }
+
+       if (done < budget) {
+               if (dev->features & NETIF_F_LRO)
+                       lro_flush_all(&priv->lro.lro_mgr);
+
+               napi_complete(napi);
+               if (unlikely(ib_req_notify_cq(priv->recv_cq,
+                                             IB_CQ_NEXT_COMP |
+                                             IB_CQ_REPORT_MISSED_EVENTS)) &&
+                   napi_reschedule(napi))
+                       goto poll_more;
        }
+
+       return done;
 }
 
 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
 {
-       struct net_device *dev = (struct net_device *) dev_ptr;
+       struct net_device *dev = dev_ptr;
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       int n, i;
 
-       ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
-       do {
-               n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
-               for (i = 0; i < n; ++i)
-                       ipoib_ib_handle_wc(dev, priv->ibwc + i);
-       } while (n == IPOIB_NUM_WC);
+       napi_schedule(&priv->napi);
+}
+
+static void drain_tx_cq(struct net_device *dev)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+
+       netif_tx_lock(dev);
+       while (poll_tx(priv))
+               ; /* nothing */
+
+       if (netif_queue_stopped(dev))
+               mod_timer(&priv->poll_timer, jiffies + 1);
+
+       netif_tx_unlock(dev);
+}
+
+void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
+
+       mod_timer(&priv->poll_timer, jiffies);
 }
 
 static inline int post_send(struct ipoib_dev_priv *priv,
                            unsigned int wr_id,
                            struct ib_ah *address, u32 qpn,
-                           dma_addr_t addr, int len)
+                           struct ipoib_tx_buf *tx_req,
+                           void *head, int hlen)
 {
        struct ib_send_wr *bad_wr;
+       int i, off;
+       struct sk_buff *skb = tx_req->skb;
+       skb_frag_t *frags = skb_shinfo(skb)->frags;
+       int nr_frags = skb_shinfo(skb)->nr_frags;
+       u64 *mapping = tx_req->mapping;
+
+       if (skb_headlen(skb)) {
+               priv->tx_sge[0].addr         = mapping[0];
+               priv->tx_sge[0].length       = skb_headlen(skb);
+               off = 1;
+       } else
+               off = 0;
 
-       priv->tx_sge.addr             = addr;
-       priv->tx_sge.length           = len;
-
-       priv->tx_wr.wr_id             = wr_id;
-       priv->tx_wr.wr.ud.remote_qpn  = qpn;
-       priv->tx_wr.wr.ud.ah          = address;
+       for (i = 0; i < nr_frags; ++i) {
+               priv->tx_sge[i + off].addr = mapping[i + off];
+               priv->tx_sge[i + off].length = frags[i].size;
+       }
+       priv->tx_wr.num_sge          = nr_frags + off;
+       priv->tx_wr.wr_id            = wr_id;
+       priv->tx_wr.wr.ud.remote_qpn = qpn;
+       priv->tx_wr.wr.ud.ah         = address;
+
+       if (head) {
+               priv->tx_wr.wr.ud.mss    = skb_shinfo(skb)->gso_size;
+               priv->tx_wr.wr.ud.header = head;
+               priv->tx_wr.wr.ud.hlen   = hlen;
+               priv->tx_wr.opcode       = IB_WR_LSO;
+       } else
+               priv->tx_wr.opcode       = IB_WR_SEND;
 
        return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
 }
@@ -299,16 +529,31 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
                struct ipoib_ah *address, u32 qpn)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct ipoib_buf *tx_req;
-       dma_addr_t addr;
-
-       if (skb->len > dev->mtu + INFINIBAND_ALEN) {
-               ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
-                          skb->len, dev->mtu + INFINIBAND_ALEN);
-               ++priv->stats.tx_dropped;
-               ++priv->stats.tx_errors;
-               dev_kfree_skb_any(skb);
-               return;
+       struct ipoib_tx_buf *tx_req;
+       int hlen, rc;
+       void *phead;
+
+       if (skb_is_gso(skb)) {
+               hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
+               phead = skb->data;
+               if (unlikely(!skb_pull(skb, hlen))) {
+                       ipoib_warn(priv, "linear data too small\n");
+                       ++dev->stats.tx_dropped;
+                       ++dev->stats.tx_errors;
+                       dev_kfree_skb_any(skb);
+                       return;
+               }
+       } else {
+               if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
+                       ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
+                                  skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
+                       ++dev->stats.tx_dropped;
+                       ++dev->stats.tx_errors;
+                       ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
+                       return;
+               }
+               phead = NULL;
+               hlen  = 0;
        }
 
        ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
@@ -321,30 +566,48 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
         * means we have to make sure everything is properly recorded and
         * our state is consistent before we call post_send().
         */
-       tx_req = &priv->tx_ring[priv->tx_head & (IPOIB_TX_RING_SIZE - 1)];
+       tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
        tx_req->skb = skb;
-       addr = dma_map_single(priv->ca->dma_device, skb->data, skb->len,
-                             DMA_TO_DEVICE);
-       pci_unmap_addr_set(tx_req, mapping, addr);
-
-       if (unlikely(post_send(priv, priv->tx_head & (IPOIB_TX_RING_SIZE - 1),
-                              address->ah, qpn, addr, skb->len))) {
-               ipoib_warn(priv, "post_send failed\n");
-               ++priv->stats.tx_errors;
-               dma_unmap_single(priv->ca->dma_device, addr, skb->len,
-                                DMA_TO_DEVICE);
+       if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
+               ++dev->stats.tx_errors;
+               dev_kfree_skb_any(skb);
+               return;
+       }
+
+       if (skb->ip_summed == CHECKSUM_PARTIAL)
+               priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
+       else
+               priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
+
+       if (++priv->tx_outstanding == ipoib_sendq_size) {
+               ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
+               if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
+                       ipoib_warn(priv, "request notify on send CQ failed\n");
+               netif_stop_queue(dev);
+       }
+
+       rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
+                      address->ah, qpn, tx_req, phead, hlen);
+       if (unlikely(rc)) {
+               ipoib_warn(priv, "post_send failed, error %d\n", rc);
+               ++dev->stats.tx_errors;
+               --priv->tx_outstanding;
+               ipoib_dma_unmap_tx(priv->ca, tx_req);
                dev_kfree_skb_any(skb);
+               if (netif_queue_stopped(dev))
+                       netif_wake_queue(dev);
        } else {
                dev->trans_start = jiffies;
 
                address->last_send = priv->tx_head;
                ++priv->tx_head;
+               skb_orphan(skb);
 
-               if (priv->tx_head - priv->tx_tail == IPOIB_TX_RING_SIZE) {
-                       ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
-                       netif_stop_queue(dev);
-               }
        }
+
+       if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
+               while (poll_tx(priv))
+                       ; /* nothing */
 }
 
 static void __ipoib_reap_ah(struct net_device *dev)
@@ -352,31 +615,38 @@ static void __ipoib_reap_ah(struct net_device *dev)
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ipoib_ah *ah, *tah;
        LIST_HEAD(remove_list);
+       unsigned long flags;
+
+       netif_tx_lock_bh(dev);
+       spin_lock_irqsave(&priv->lock, flags);
 
-       spin_lock_irq(&priv->lock);
        list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
-               if (ah->last_send <= priv->tx_tail) {
+               if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
                        list_del(&ah->list);
-                       list_add_tail(&ah->list, &remove_list);
+                       ib_destroy_ah(ah->ah);
+                       kfree(ah);
                }
-       spin_unlock_irq(&priv->lock);
 
-       list_for_each_entry_safe(ah, tah, &remove_list, list) {
-               ipoib_dbg(priv, "Reaping ah %p\n", ah->ah);
-               ib_destroy_ah(ah->ah);
-               kfree(ah);
-       }
+       spin_unlock_irqrestore(&priv->lock, flags);
+       netif_tx_unlock_bh(dev);
 }
 
-void ipoib_reap_ah(void *dev_ptr)
+void ipoib_reap_ah(struct work_struct *work)
 {
-       struct net_device *dev = dev_ptr;
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
+       struct net_device *dev = priv->dev;
 
        __ipoib_reap_ah(dev);
 
        if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
-               queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
+               queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
+                                  round_jiffies_relative(HZ));
+}
+
+static void ipoib_ib_tx_timer_func(unsigned long ctx)
+{
+       drain_tx_cq((struct net_device *)ctx);
 }
 
 int ipoib_ib_dev_open(struct net_device *dev)
@@ -384,34 +654,71 @@ int ipoib_ib_dev_open(struct net_device *dev)
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        int ret;
 
-       ret = ipoib_qp_create(dev);
+       if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
+               ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
+               clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+               return -1;
+       }
+       set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+
+       ret = ipoib_init_qp(dev);
        if (ret) {
-               ipoib_warn(priv, "ipoib_qp_create returned %d\n", ret);
+               ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
                return -1;
        }
 
        ret = ipoib_ib_post_receives(dev);
        if (ret) {
                ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
+               ipoib_ib_dev_stop(dev, 1);
+               return -1;
+       }
+
+       ret = ipoib_cm_dev_open(dev);
+       if (ret) {
+               ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
+               ipoib_ib_dev_stop(dev, 1);
                return -1;
        }
 
        clear_bit(IPOIB_STOP_REAPER, &priv->flags);
-       queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
+       queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
+                          round_jiffies_relative(HZ));
+
+       if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
+               napi_enable(&priv->napi);
 
        return 0;
 }
 
+static void ipoib_pkey_dev_check_presence(struct net_device *dev)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       u16 pkey_index = 0;
+
+       if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
+               clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+       else
+               set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+}
+
 int ipoib_ib_dev_up(struct net_device *dev)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
 
+       ipoib_pkey_dev_check_presence(dev);
+
+       if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
+               ipoib_dbg(priv, "PKEY is not assigned.\n");
+               return 0;
+       }
+
        set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
 
        return ipoib_mcast_start_thread(dev);
 }
 
-int ipoib_ib_dev_down(struct net_device *dev)
+int ipoib_ib_dev_down(struct net_device *dev, int flush)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
 
@@ -422,25 +729,17 @@ int ipoib_ib_dev_down(struct net_device *dev)
 
        /* Shutdown the P_Key thread if still active */
        if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                set_bit(IPOIB_PKEY_STOP, &priv->flags);
-               cancel_delayed_work(&priv->pkey_task);
-               up(&pkey_sem);
-               flush_workqueue(ipoib_workqueue);
+               cancel_delayed_work(&priv->pkey_poll_task);
+               mutex_unlock(&pkey_mutex);
+               if (flush)
+                       flush_workqueue(ipoib_workqueue);
        }
 
-       ipoib_mcast_stop_thread(dev);
-
-       /*
-        * Flush the multicast groups first so we stop any multicast joins. The
-        * completion thread may have already died and we may deadlock waiting
-        * for the completion thread to finish some multicast joins.
-        */
+       ipoib_mcast_stop_thread(dev, flush);
        ipoib_mcast_dev_flush(dev);
 
-       /* Delete broadcast and local addresses since they will be recreated */
-       ipoib_mcast_dev_down(dev);
-
        ipoib_flush_paths(dev);
 
        return 0;
@@ -452,26 +751,71 @@ static int recvs_pending(struct net_device *dev)
        int pending = 0;
        int i;
 
-       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
+       for (i = 0; i < ipoib_recvq_size; ++i)
                if (priv->rx_ring[i].skb)
                        ++pending;
 
        return pending;
 }
 
-int ipoib_ib_dev_stop(struct net_device *dev)
+void ipoib_drain_cq(struct net_device *dev)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       int i, n;
+
+       /*
+        * We call completion handling routines that expect to be
+        * called from the BH-disabled NAPI poll context, so disable
+        * BHs here too.
+        */
+       local_bh_disable();
+
+       do {
+               n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
+               for (i = 0; i < n; ++i) {
+                       /*
+                        * Convert any successful completions to flush
+                        * errors to avoid passing packets up the
+                        * stack after bringing the device down.
+                        */
+                       if (priv->ibwc[i].status == IB_WC_SUCCESS)
+                               priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
+
+                       if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
+                               if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
+                                       ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
+                               else
+                                       ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
+                       } else
+                               ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
+               }
+       } while (n == IPOIB_NUM_WC);
+
+       while (poll_tx(priv))
+               ; /* nothing */
+
+       local_bh_enable();
+}
+
+int ipoib_ib_dev_stop(struct net_device *dev, int flush)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ib_qp_attr qp_attr;
-       int attr_mask;
        unsigned long begin;
-       struct ipoib_buf *tx_req;
+       struct ipoib_tx_buf *tx_req;
        int i;
 
-       /* Kill the existing QP and allocate a new one */
+       if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
+               napi_disable(&priv->napi);
+
+       ipoib_cm_dev_stop(dev);
+
+       /*
+        * Move our QP to the error state and then reinitialize in
+        * when all work requests have completed or have been flushed.
+        */
        qp_attr.qp_state = IB_QPS_ERR;
-       attr_mask        = IB_QP_STATE;
-       if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
+       if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
                ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
 
        /* Wait for all sends and receives to complete */
@@ -486,46 +830,48 @@ int ipoib_ib_dev_stop(struct net_device *dev)
                         * assume the HW is wedged and just free up
                         * all our pending work requests.
                         */
-                       while (priv->tx_tail < priv->tx_head) {
+                       while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
                                tx_req = &priv->tx_ring[priv->tx_tail &
-                                                       (IPOIB_TX_RING_SIZE - 1)];
-                               dma_unmap_single(priv->ca->dma_device,
-                                                pci_unmap_addr(tx_req, mapping),
-                                                tx_req->skb->len,
-                                                DMA_TO_DEVICE);
+                                                       (ipoib_sendq_size - 1)];
+                               ipoib_dma_unmap_tx(priv->ca, tx_req);
                                dev_kfree_skb_any(tx_req->skb);
                                ++priv->tx_tail;
+                               --priv->tx_outstanding;
                        }
 
-                       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
-                               if (priv->rx_ring[i].skb) {
-                                       dma_unmap_single(priv->ca->dma_device,
-                                                        pci_unmap_addr(&priv->rx_ring[i],
-                                                                       mapping),
-                                                        IPOIB_BUF_SIZE,
-                                                        DMA_FROM_DEVICE);
-                                       dev_kfree_skb_any(priv->rx_ring[i].skb);
-                                       priv->rx_ring[i].skb = NULL;
-                               }
+                       for (i = 0; i < ipoib_recvq_size; ++i) {
+                               struct ipoib_rx_buf *rx_req;
+
+                               rx_req = &priv->rx_ring[i];
+                               if (!rx_req->skb)
+                                       continue;
+                               ipoib_ud_dma_unmap_rx(priv,
+                                                     priv->rx_ring[i].mapping);
+                               dev_kfree_skb_any(rx_req->skb);
+                               rx_req->skb = NULL;
+                       }
 
                        goto timeout;
                }
 
+               ipoib_drain_cq(dev);
+
                msleep(1);
        }
 
        ipoib_dbg(priv, "All sends and receives done.\n");
 
 timeout:
+       del_timer_sync(&priv->poll_timer);
        qp_attr.qp_state = IB_QPS_RESET;
-       attr_mask        = IB_QP_STATE;
-       if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
+       if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
                ipoib_warn(priv, "Failed to modify QP to RESET state\n");
 
        /* Wait for all AHs to be reaped */
        set_bit(IPOIB_STOP_REAPER, &priv->flags);
        cancel_delayed_work(&priv->ah_reap_task);
-       flush_workqueue(ipoib_workqueue);
+       if (flush)
+               flush_workqueue(ipoib_workqueue);
 
        begin = jiffies;
 
@@ -540,6 +886,8 @@ timeout:
                msleep(1);
        }
 
+       ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
+
        return 0;
 }
 
@@ -556,6 +904,9 @@ int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
                return -ENODEV;
        }
 
+       setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
+                   (unsigned long) dev);
+
        if (dev->flags & IFF_UP) {
                if (ipoib_ib_dev_open(dev)) {
                        ipoib_transport_dev_cleanup(dev);
@@ -566,28 +917,98 @@ int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
        return 0;
 }
 
-void ipoib_ib_dev_flush(void *_dev)
+static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
+                               enum ipoib_flush_level level)
 {
-       struct net_device *dev = (struct net_device *)_dev;
-       struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv;
+       struct ipoib_dev_priv *cpriv;
+       struct net_device *dev = priv->dev;
+       u16 new_index;
+
+       mutex_lock(&priv->vlan_mutex);
+
+       /*
+        * Flush any child interfaces too -- they might be up even if
+        * the parent is down.
+        */
+       list_for_each_entry(cpriv, &priv->child_intfs, list)
+               __ipoib_ib_dev_flush(cpriv, level);
+
+       mutex_unlock(&priv->vlan_mutex);
 
-       if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
+       if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
+               ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
                return;
+       }
+
+       if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
+               ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
+               return;
+       }
+
+       if (level == IPOIB_FLUSH_HEAVY) {
+               if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
+                       clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+                       ipoib_ib_dev_down(dev, 0);
+                       ipoib_ib_dev_stop(dev, 0);
+                       if (ipoib_pkey_dev_delay_open(dev))
+                               return;
+               }
+
+               /* restart QP only if P_Key index is changed */
+               if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
+                   new_index == priv->pkey_index) {
+                       ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
+                       return;
+               }
+               priv->pkey_index = new_index;
+       }
 
-       ipoib_dbg(priv, "flushing\n");
+       if (level == IPOIB_FLUSH_LIGHT) {
+               ipoib_mark_paths_invalid(dev);
+               ipoib_mcast_dev_flush(dev);
+       }
 
-       ipoib_ib_dev_down(dev);
+       if (level >= IPOIB_FLUSH_NORMAL)
+               ipoib_ib_dev_down(dev, 0);
+
+       if (level == IPOIB_FLUSH_HEAVY) {
+               ipoib_ib_dev_stop(dev, 0);
+               ipoib_ib_dev_open(dev);
+       }
 
        /*
         * The device could have been brought down between the start and when
         * we get here, don't bring it back up if it's not configured up
         */
-       if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
-               ipoib_ib_dev_up(dev);
+       if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
+               if (level >= IPOIB_FLUSH_NORMAL)
+                       ipoib_ib_dev_up(dev);
+               ipoib_mcast_restart_task(&priv->restart_task);
+       }
+}
 
-       /* Flush any child interfaces too */
-       list_for_each_entry(cpriv, &priv->child_intfs, list)
-               ipoib_ib_dev_flush(&cpriv->dev);
+void ipoib_ib_dev_flush_light(struct work_struct *work)
+{
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, flush_light);
+
+       __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
+}
+
+void ipoib_ib_dev_flush_normal(struct work_struct *work)
+{
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, flush_normal);
+
+       __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
+}
+
+void ipoib_ib_dev_flush_heavy(struct work_struct *work)
+{
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, flush_heavy);
+
+       __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
 }
 
 void ipoib_ib_dev_cleanup(struct net_device *dev)
@@ -596,10 +1017,8 @@ void ipoib_ib_dev_cleanup(struct net_device *dev)
 
        ipoib_dbg(priv, "cleaning up ib_dev\n");
 
-       ipoib_mcast_stop_thread(dev);
-
-       /* Delete the broadcast address and the local address */
-       ipoib_mcast_dev_down(dev);
+       ipoib_mcast_stop_thread(dev, 1);
+       ipoib_mcast_dev_flush(dev);
 
        ipoib_transport_dev_cleanup(dev);
 }
@@ -613,35 +1032,24 @@ void ipoib_ib_dev_cleanup(struct net_device *dev)
  * Bug #2507. This implementation will probably be removed when the P_Key
  * change async notification is available.
  */
-int ipoib_open(struct net_device *dev);
-
-static void ipoib_pkey_dev_check_presence(struct net_device *dev)
-{
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
-       u16 pkey_index = 0;
-
-       if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
-               clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
-       else
-               set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
-}
 
-void ipoib_pkey_poll(void *dev_ptr)
+void ipoib_pkey_poll(struct work_struct *work)
 {
-       struct net_device *dev = dev_ptr;
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
+       struct net_device *dev = priv->dev;
 
        ipoib_pkey_dev_check_presence(dev);
 
        if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
                ipoib_open(dev);
        else {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
                        queue_delayed_work(ipoib_workqueue,
-                                          &priv->pkey_task,
+                                          &priv->pkey_poll_task,
                                           HZ);
-               up(&pkey_sem);
+               mutex_unlock(&pkey_mutex);
        }
 }
 
@@ -655,12 +1063,12 @@ int ipoib_pkey_dev_delay_open(struct net_device *dev)
 
        /* P_Key value not assigned yet - start polling */
        if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                clear_bit(IPOIB_PKEY_STOP, &priv->flags);
                queue_delayed_work(ipoib_workqueue,
-                                  &priv->pkey_task,
+                                  &priv->pkey_poll_task,
                                   HZ);
-               up(&pkey_sem);
+               mutex_unlock(&pkey_mutex);
                return 1;
        }