1 From: Felix Fietkau <nbd@openwrt.org>
2 Date: Sun, 12 Apr 2015 09:58:56 +0200
3 Subject: [PATCH] bgmac: simplify tx ring index handling
5 Keep incrementing ring->start and ring->end instead of pointing it to
6 the actual ring slot entry. This simplifies the calculation of the
9 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
12 --- a/drivers/net/ethernet/broadcom/bgmac.c
13 +++ b/drivers/net/ethernet/broadcom/bgmac.c
14 @@ -142,11 +142,10 @@ static netdev_tx_t bgmac_dma_tx_add(stru
16 struct device *dma_dev = bgmac->core->dma_dev;
17 struct net_device *net_dev = bgmac->net_dev;
18 - struct bgmac_slot_info *slot = &ring->slots[ring->end];
20 + int index = ring->end % BGMAC_TX_RING_SLOTS;
21 + struct bgmac_slot_info *slot = &ring->slots[index];
24 - int index = ring->end;
27 if (skb->len > BGMAC_DESC_CTL1_LEN) {
28 @@ -159,12 +158,10 @@ static netdev_tx_t bgmac_dma_tx_add(stru
30 nr_frags = skb_shinfo(skb)->nr_frags;
32 - if (ring->start <= ring->end)
33 - free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
35 - free_slots = ring->start - ring->end;
37 - if (free_slots <= nr_frags + 1) {
38 + /* ring->end - ring->start will return the number of valid slots,
39 + * even when ring->end overflows
41 + if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
42 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
43 netif_stop_queue(net_dev);
44 return NETDEV_TX_BUSY;
45 @@ -200,7 +197,7 @@ static netdev_tx_t bgmac_dma_tx_add(stru
50 + ring->end += nr_frags + 1;
51 netdev_sent_queue(net_dev, skb->len);
54 @@ -208,13 +205,12 @@ static netdev_tx_t bgmac_dma_tx_add(stru
55 /* Increase ring->end to point empty slot. We tell hardware the first
56 * slot it should *not* read.
58 - ring->end = (index + 1) % BGMAC_TX_RING_SLOTS;
59 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
61 - ring->end * sizeof(struct bgmac_dma_desc));
62 + (ring->end % BGMAC_TX_RING_SLOTS) *
63 + sizeof(struct bgmac_dma_desc));
65 - free_slots -= nr_frags + 1;
67 + if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
68 netif_stop_queue(net_dev);
71 @@ -256,17 +252,17 @@ static void bgmac_dma_tx_free(struct bgm
72 empty_slot &= BGMAC_DMA_TX_STATDPTR;
73 empty_slot /= sizeof(struct bgmac_dma_desc);
75 - while (ring->start != empty_slot) {
76 - struct bgmac_slot_info *slot = &ring->slots[ring->start];
77 - u32 ctl1 = le32_to_cpu(ring->cpu_base[ring->start].ctl1);
78 - int len = ctl1 & BGMAC_DESC_CTL1_LEN;
79 + while (ring->start != ring->end) {
80 + int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
81 + struct bgmac_slot_info *slot = &ring->slots[slot_idx];
85 - if (!slot->dma_addr) {
86 - bgmac_err(bgmac, "Hardware reported transmission for empty TX ring slot %d! End of ring: %d\n",
87 - ring->start, ring->end);
90 + if (slot_idx == empty_slot)
93 + ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
94 + len = ctl1 & BGMAC_DESC_CTL1_LEN;
95 if (ctl1 & BGMAC_DESC_CTL0_SOF)
96 /* Unmap no longer used buffer */
97 dma_unmap_single(dma_dev, slot->dma_addr, len,
98 @@ -284,10 +280,8 @@ static void bgmac_dma_tx_free(struct bgm
104 - if (++ring->start >= BGMAC_TX_RING_SLOTS)
110 --- a/drivers/net/ethernet/broadcom/bgmac.h
111 +++ b/drivers/net/ethernet/broadcom/bgmac.h
112 @@ -414,10 +414,10 @@ enum bgmac_dma_ring_type {
115 struct bgmac_dma_ring {
124 struct bgmac_dma_desc *cpu_base;