1 From: Felix Fietkau <nbd@openwrt.org>
2 Date: Sun, 12 Apr 2015 22:23:07 +0200
3 Subject: [PATCH] bgmac: simplify/optimize rx DMA error handling
5 Allocate a new buffer before processing the completed one. If allocation
6 fails, reuse the old buffer.
8 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
11 --- a/drivers/net/ethernet/broadcom/bgmac.c
12 +++ b/drivers/net/ethernet/broadcom/bgmac.c
13 @@ -386,6 +386,19 @@ static void bgmac_dma_rx_setup_desc(stru
14 dma_desc->ctl1 = cpu_to_le32(ctl1);
17 +static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
18 + struct bgmac_slot_info *slot)
20 + struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
22 + dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
24 + rx->len = cpu_to_le16(0xdead);
25 + rx->flags = cpu_to_le16(0xbeef);
26 + dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
30 static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
33 @@ -406,53 +419,35 @@ static int bgmac_dma_rx_read(struct bgma
34 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
36 void *buf = slot->buf;
37 + dma_addr_t dma_addr = slot->dma_addr;
40 - /* Unmap buffer to make it accessible to the CPU */
41 - dma_sync_single_for_cpu(dma_dev, slot->dma_addr,
42 - BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
44 - /* Get info from the header */
45 - len = le16_to_cpu(rx->len);
46 - flags = le16_to_cpu(rx->flags);
49 - dma_addr_t old_dma_addr = slot->dma_addr;
51 + /* Prepare new skb as replacement */
52 + if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
53 + bgmac_dma_rx_poison_buf(dma_dev, slot);
57 + /* Unmap buffer to make it accessible to the CPU */
58 + dma_unmap_single(dma_dev, dma_addr,
59 + BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
61 + /* Get info from the header */
62 + len = le16_to_cpu(rx->len);
63 + flags = le16_to_cpu(rx->flags);
65 /* Check for poison and drop or pass the packet */
66 if (len == 0xdead && flags == 0xbeef) {
67 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
69 - dma_sync_single_for_device(dma_dev,
73 + put_page(virt_to_head_page(buf));
80 - /* Prepare new skb as replacement */
81 - err = bgmac_dma_rx_skb_for_slot(bgmac, slot);
83 - /* Poison the old skb */
84 - rx->len = cpu_to_le16(0xdead);
85 - rx->flags = cpu_to_le16(0xbeef);
87 - dma_sync_single_for_device(dma_dev,
93 - bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
95 - /* Unmap old skb, we'll pass it to the netfif */
96 - dma_unmap_single(dma_dev, old_dma_addr,
97 - BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
99 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
100 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
101 BGMAC_RX_BUF_OFFSET + len);
102 @@ -465,6 +460,8 @@ static int bgmac_dma_rx_read(struct bgma
106 + bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
108 if (++ring->start >= BGMAC_RX_RING_SLOTS)
111 @@ -532,14 +529,14 @@ static void bgmac_dma_rx_ring_free(struc
113 for (i = 0; i < ring->num_slots; i++) {
114 slot = &ring->slots[i];
116 + if (!slot->dma_addr)
119 - if (slot->dma_addr)
120 - dma_unmap_single(dma_dev, slot->dma_addr,
123 + dma_unmap_single(dma_dev, slot->dma_addr,
126 put_page(virt_to_head_page(slot->buf));
127 + slot->dma_addr = 0;