]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/infiniband/ulp/ipoib/ipoib_ib.c
IPoIB: Handle 4K IB MTU for UD (datagram) mode
[linux-2.6-omap-h63xx.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
index 1a77e79f6b432748accdbb9a813f9a257aa4c1c6..7cf1fa7074abfa28ca31e298c8c2c04a53b75ab8 100644 (file)
@@ -39,6 +39,8 @@
 #include <linux/dma-mapping.h>
 
 #include <rdma/ib_cache.h>
+#include <linux/ip.h>
+#include <linux/tcp.h>
 
 #include "ipoib.h"
 
@@ -87,28 +89,59 @@ void ipoib_free_ah(struct kref *kref)
        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);
+
+}
+
 static int ipoib_ib_post_receive(struct net_device *dev, int id)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct ib_sge list;
-       struct ib_recv_wr param;
        struct ib_recv_wr *bad_wr;
        int ret;
 
-       list.addr     = priv->rx_ring[id].mapping;
-       list.length   = IPOIB_BUF_SIZE;
-       list.lkey     = priv->mr->lkey;
+       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];
 
-       param.next    = NULL;
-       param.wr_id   = id | IPOIB_OP_RECV;
-       param.sg_list = &list;
-       param.num_sge = 1;
 
-       ret = ib_post_recv(priv->qp, &param, &bad_wr);
+       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);
-               ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
-                                   IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+               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;
        }
@@ -116,15 +149,21 @@ static int ipoib_ib_post_receive(struct net_device *dev, int id)
        return ret;
 }
 
-static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
+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;
-       u64 addr;
+       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(IPOIB_BUF_SIZE + 4);
-       if (!skb)
-               return -ENOMEM;
+       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
@@ -133,17 +172,32 @@ static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
         */
        skb_reserve(skb, 4);
 
-       addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
-                                DMA_FROM_DEVICE);
-       if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
-               dev_kfree_skb_any(skb);
-               return -EIO;
+       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;
-       priv->rx_ring[id].mapping = addr;
+       priv->rx_ring[id].skb = skb;
+       return skb;
 
-       return 0;
+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)
@@ -152,7 +206,7 @@ static int ipoib_ib_post_receives(struct net_device *dev)
        int i;
 
        for (i = 0; i < ipoib_recvq_size; ++i) {
-               if (ipoib_alloc_rx_skb(dev, i)) {
+               if (!ipoib_alloc_rx_skb(dev, i)) {
                        ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
                        return -ENOMEM;
                }
@@ -170,7 +224,7 @@ 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 addr;
+       u64 mapping[IPOIB_UD_RX_SG];
 
        ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
                       wr_id, wc->status);
@@ -182,15 +236,13 @@ static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
        }
 
        skb  = priv->rx_ring[wr_id].skb;
-       addr = priv->rx_ring[wr_id].mapping;
 
        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);
-               ib_dma_unmap_single(priv->ca, addr,
-                                   IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+               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;
@@ -203,11 +255,14 @@ static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
        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))) {
+       if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
                ++dev->stats.rx_dropped;
                goto repost;
        }
@@ -215,9 +270,9 @@ static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
        ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
                       wc->byte_len, wc->slid);
 
-       ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+       ipoib_ud_dma_unmap_rx(priv, mapping);
+       ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
 
-       skb_put(skb, wc->byte_len);
        skb_pull(skb, IB_GRH_BYTES);
 
        skb->protocol = ((struct ipoib_header *) skb->data)->proto;
@@ -231,6 +286,10 @@ static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
        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;
+
        netif_receive_skb(skb);
 
 repost:
@@ -239,6 +298,67 @@ repost:
                           "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);
@@ -257,8 +377,7 @@ static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
 
        tx_req = &priv->tx_ring[wr_id];
 
-       ib_dma_unmap_single(priv->ca, tx_req->mapping,
-                           tx_req->skb->len, DMA_TO_DEVICE);
+       ipoib_dma_unmap_tx(priv->ca, tx_req);
 
        ++dev->stats.tx_packets;
        dev->stats.tx_bytes += tx_req->skb->len;
@@ -267,11 +386,10 @@ static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
 
        spin_lock_irqsave(&priv->tx_lock, flags);
        ++priv->tx_tail;
-       if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
-           priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
-               clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
+       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);
-       }
        spin_unlock_irqrestore(&priv->tx_lock, flags);
 
        if (wc->status != IB_WC_SUCCESS &&
@@ -301,14 +419,18 @@ poll_more:
                for (i = 0; i < n; i++) {
                        struct ib_wc *wc = priv->ibwc + i;
 
-                       if (wc->wr_id & IPOIB_CM_OP_SRQ) {
+                       if (wc->wr_id & IPOIB_OP_RECV) {
                                ++done;
-                               ipoib_cm_handle_rx_wc(dev, wc);
-                       } else if (wc->wr_id & IPOIB_OP_RECV) {
-                               ++done;
-                               ipoib_ib_handle_rx_wc(dev, wc);
-                       } else
-                               ipoib_ib_handle_tx_wc(dev, wc);
+                               if (wc->wr_id & IPOIB_OP_CM)
+                                       ipoib_cm_handle_rx_wc(dev, wc);
+                               else
+                                       ipoib_ib_handle_rx_wc(dev, wc);
+                       } else {
+                               if (wc->wr_id & IPOIB_OP_CM)
+                                       ipoib_cm_handle_tx_wc(dev, wc);
+                               else
+                                       ipoib_ib_handle_tx_wc(dev, wc);
+                       }
                }
 
                if (n != t)
@@ -338,16 +460,39 @@ void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
 static inline int post_send(struct ipoib_dev_priv *priv,
                            unsigned int wr_id,
                            struct ib_ah *address, u32 qpn,
-                           u64 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);
 }
@@ -357,15 +502,30 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ipoib_tx_buf *tx_req;
-       u64 addr;
-
-       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;
+       int hlen;
+       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",
@@ -380,20 +540,22 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
         */
        tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
        tx_req->skb = skb;
-       addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
-                                DMA_TO_DEVICE);
-       if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
+       if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
                ++dev->stats.tx_errors;
                dev_kfree_skb_any(skb);
                return;
        }
-       tx_req->mapping = addr;
+
+       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 (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
-                              address->ah, qpn, addr, skb->len))) {
+                              address->ah, qpn, tx_req, phead, hlen))) {
                ipoib_warn(priv, "post_send failed\n");
                ++dev->stats.tx_errors;
-               ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
+               ipoib_dma_unmap_tx(priv->ca, tx_req);
                dev_kfree_skb_any(skb);
        } else {
                dev->trans_start = jiffies;
@@ -401,10 +563,9 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
                address->last_send = priv->tx_head;
                ++priv->tx_head;
 
-               if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
+               if (++priv->tx_outstanding == ipoib_sendq_size) {
                        ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
                        netif_stop_queue(dev);
-                       set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
                }
        }
 }
@@ -436,7 +597,8 @@ void ipoib_reap_ah(struct work_struct *work)
        __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));
 }
 
 int ipoib_ib_dev_open(struct net_device *dev)
@@ -472,7 +634,8 @@ int ipoib_ib_dev_open(struct net_device *dev)
        }
 
        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));
 
        set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
 
@@ -484,7 +647,7 @@ 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))
+       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);
@@ -561,12 +724,17 @@ void ipoib_drain_cq(struct net_device *dev)
                        if (priv->ibwc[i].status == IB_WC_SUCCESS)
                                priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
 
-                       if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
-                               ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
-                       else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
-                               ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
-                       else
-                               ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
+                       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 {
+                               if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
+                                       ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
+                               else
+                                       ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
+                       }
                }
        } while (n == IPOIB_NUM_WC);
 }
@@ -606,12 +774,10 @@ int ipoib_ib_dev_stop(struct net_device *dev, int flush)
                        while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
                                tx_req = &priv->tx_ring[priv->tx_tail &
                                                        (ipoib_sendq_size - 1)];
-                               ib_dma_unmap_single(priv->ca,
-                                                   tx_req->mapping,
-                                                   tx_req->skb->len,
-                                                   DMA_TO_DEVICE);
+                               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_recvq_size; ++i) {
@@ -620,10 +786,8 @@ int ipoib_ib_dev_stop(struct net_device *dev, int flush)
                                rx_req = &priv->rx_ring[i];
                                if (!rx_req->skb)
                                        continue;
-                               ib_dma_unmap_single(priv->ca,
-                                                   rx_req->mapping,
-                                                   IPOIB_BUF_SIZE,
-                                                   DMA_FROM_DEVICE);
+                               ipoib_ud_dma_unmap_rx(priv,
+                                                     priv->rx_ring[i].mapping);
                                dev_kfree_skb_any(rx_req->skb);
                                rx_req->skb = NULL;
                        }
@@ -721,13 +885,14 @@ static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
                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_pkey_dev_delay_open(dev);
-                       return;
+                       ipoib_ib_dev_stop(dev, 0);
+                       if (ipoib_pkey_dev_delay_open(dev))
+                               return;
                }
-               set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
 
                /* restart QP only if P_Key index is changed */
-               if (new_index == priv->pkey_index) {
+               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;
                }