Skip to content

Commit 9f4ca05

Browse files
ffainellidavem330
authored andcommitted
net: bcmgenet: Add support for adaptive RX coalescing
Unlike the moder modern SYSTEMPORT hardware, we do not have a configurable TDMA timeout, which limits us to implement adaptive RX interrupt coalescing only. We have each of our RX rings implement a bcmgenet_net_dim structure which holds an interrupt counter, number of packets, bytes, and a container for a net_dim instance. Signed-off-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b6e0e87 commit 9f4ca05

File tree

2 files changed

+103
-18
lines changed

2 files changed

+103
-18
lines changed

drivers/net/ethernet/broadcom/genet/bcmgenet.c

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ static int bcmgenet_get_coalesce(struct net_device *dev,
603603
struct ethtool_coalesce *ec)
604604
{
605605
struct bcmgenet_priv *priv = netdev_priv(dev);
606+
struct bcmgenet_rx_ring *ring;
607+
unsigned int i;
606608

607609
ec->tx_max_coalesced_frames =
608610
bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
@@ -613,15 +615,37 @@ static int bcmgenet_get_coalesce(struct net_device *dev,
613615
ec->rx_coalesce_usecs =
614616
bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
615617

618+
for (i = 0; i < priv->hw_params->rx_queues; i++) {
619+
ring = &priv->rx_rings[i];
620+
ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
621+
}
622+
ring = &priv->rx_rings[DESC_INDEX];
623+
ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
624+
616625
return 0;
617626
}
618627

628+
static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring)
629+
{
630+
struct bcmgenet_priv *priv = ring->priv;
631+
unsigned int i = ring->index;
632+
u32 reg;
633+
634+
bcmgenet_rdma_ring_writel(priv, i, ring->dim.coal_pkts,
635+
DMA_MBUF_DONE_THRESH);
636+
637+
reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
638+
reg &= ~DMA_TIMEOUT_MASK;
639+
reg |= DIV_ROUND_UP(ring->dim.coal_usecs * 1000, 8192);
640+
bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
641+
}
642+
619643
static int bcmgenet_set_coalesce(struct net_device *dev,
620644
struct ethtool_coalesce *ec)
621645
{
622646
struct bcmgenet_priv *priv = netdev_priv(dev);
647+
struct bcmgenet_rx_ring *ring;
623648
unsigned int i;
624-
u32 reg;
625649

626650
/* Base system clock is 125Mhz, DMA timeout is this reference clock
627651
* divided by 1024, which yields roughly 8.192us, our maximum value
@@ -641,7 +665,8 @@ static int bcmgenet_set_coalesce(struct net_device *dev,
641665
* transmitted, or when the ring is empty.
642666
*/
643667
if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
644-
ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low)
668+
ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low ||
669+
ec->use_adaptive_tx_coalesce)
645670
return -EOPNOTSUPP;
646671

647672
/* Program all TX queues with the same values, as there is no
@@ -656,24 +681,26 @@ static int bcmgenet_set_coalesce(struct net_device *dev,
656681
DMA_MBUF_DONE_THRESH);
657682

658683
for (i = 0; i < priv->hw_params->rx_queues; i++) {
659-
bcmgenet_rdma_ring_writel(priv, i,
660-
ec->rx_max_coalesced_frames,
661-
DMA_MBUF_DONE_THRESH);
662-
663-
reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
664-
reg &= ~DMA_TIMEOUT_MASK;
665-
reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
666-
bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
684+
ring = &priv->rx_rings[i];
685+
ring->dim.coal_usecs = ec->rx_coalesce_usecs;
686+
ring->dim.coal_pkts = ec->rx_max_coalesced_frames;
687+
if (!ec->use_adaptive_rx_coalesce && ring->dim.use_dim) {
688+
ring->dim.coal_pkts = 1;
689+
ring->dim.coal_usecs = 0;
690+
}
691+
ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
692+
bcmgenet_set_rx_coalesce(ring);
667693
}
668694

669-
bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
670-
ec->rx_max_coalesced_frames,
671-
DMA_MBUF_DONE_THRESH);
672-
673-
reg = bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT);
674-
reg &= ~DMA_TIMEOUT_MASK;
675-
reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192);
676-
bcmgenet_rdma_writel(priv, reg, DMA_RING16_TIMEOUT);
695+
ring = &priv->rx_rings[DESC_INDEX];
696+
ring->dim.coal_usecs = ec->rx_coalesce_usecs;
697+
ring->dim.coal_pkts = ec->rx_max_coalesced_frames;
698+
if (!ec->use_adaptive_rx_coalesce && ring->dim.use_dim) {
699+
ring->dim.coal_pkts = 1;
700+
ring->dim.coal_usecs = 0;
701+
}
702+
ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
703+
bcmgenet_set_rx_coalesce(ring);
677704

678705
return 0;
679706
}
@@ -1713,6 +1740,7 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
17131740
unsigned long dma_flag;
17141741
int len;
17151742
unsigned int rxpktprocessed = 0, rxpkttoprocess;
1743+
unsigned int bytes_processed = 0;
17161744
unsigned int p_index, mask;
17171745
unsigned int discards;
17181746
unsigned int chksum_ok = 0;
@@ -1832,6 +1860,8 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
18321860
len -= ETH_FCS_LEN;
18331861
}
18341862

1863+
bytes_processed += len;
1864+
18351865
/*Finish setting up the received SKB and send it to the kernel*/
18361866
skb->protocol = eth_type_trans(skb, priv->dev);
18371867
ring->packets++;
@@ -1854,6 +1884,9 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
18541884
bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
18551885
}
18561886

1887+
ring->dim.bytes = bytes_processed;
1888+
ring->dim.packets = rxpktprocessed;
1889+
18571890
return rxpktprocessed;
18581891
}
18591892

@@ -1862,6 +1895,7 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
18621895
{
18631896
struct bcmgenet_rx_ring *ring = container_of(napi,
18641897
struct bcmgenet_rx_ring, napi);
1898+
struct net_dim_sample dim_sample;
18651899
unsigned int work_done;
18661900

18671901
work_done = bcmgenet_desc_rx(ring, budget);
@@ -1871,9 +1905,32 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
18711905
ring->int_enable(ring);
18721906
}
18731907

1908+
if (ring->dim.use_dim) {
1909+
net_dim_sample(ring->dim.event_ctr, ring->dim.packets,
1910+
ring->dim.bytes, &dim_sample);
1911+
net_dim(&ring->dim.dim, dim_sample);
1912+
}
1913+
18741914
return work_done;
18751915
}
18761916

1917+
static void bcmgenet_dim_work(struct work_struct *work)
1918+
{
1919+
struct net_dim *dim = container_of(work, struct net_dim, work);
1920+
struct bcmgenet_net_dim *ndim =
1921+
container_of(dim, struct bcmgenet_net_dim, dim);
1922+
struct bcmgenet_rx_ring *ring =
1923+
container_of(ndim, struct bcmgenet_rx_ring, dim);
1924+
struct net_dim_cq_moder cur_profile =
1925+
net_dim_get_profile(dim->mode, dim->profile_ix);
1926+
1927+
ring->dim.coal_usecs = cur_profile.usec;
1928+
ring->dim.coal_pkts = cur_profile.pkts;
1929+
1930+
bcmgenet_set_rx_coalesce(ring);
1931+
dim->state = NET_DIM_START_MEASURE;
1932+
}
1933+
18771934
/* Assign skb to RX DMA descriptor. */
18781935
static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
18791936
struct bcmgenet_rx_ring *ring)
@@ -2022,6 +2079,16 @@ static void init_umac(struct bcmgenet_priv *priv)
20222079
dev_dbg(kdev, "done init umac\n");
20232080
}
20242081

2082+
static void bcmgenet_init_dim(struct bcmgenet_net_dim *dim,
2083+
void (*cb)(struct work_struct *work))
2084+
{
2085+
INIT_WORK(&dim->dim.work, cb);
2086+
dim->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2087+
dim->event_ctr = 0;
2088+
dim->packets = 0;
2089+
dim->bytes = 0;
2090+
}
2091+
20252092
/* Initialize a Tx ring along with corresponding hardware registers */
20262093
static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
20272094
unsigned int index, unsigned int size,
@@ -2111,6 +2178,8 @@ static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
21112178
if (ret)
21122179
return ret;
21132180

2181+
bcmgenet_init_dim(&ring->dim, bcmgenet_dim_work);
2182+
21142183
/* Initialize Rx NAPI */
21152184
netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
21162185
NAPI_POLL_WEIGHT);
@@ -2276,10 +2345,12 @@ static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
22762345
for (i = 0; i < priv->hw_params->rx_queues; ++i) {
22772346
ring = &priv->rx_rings[i];
22782347
napi_disable(&ring->napi);
2348+
cancel_work_sync(&ring->dim.dim.work);
22792349
}
22802350

22812351
ring = &priv->rx_rings[DESC_INDEX];
22822352
napi_disable(&ring->napi);
2353+
cancel_work_sync(&ring->dim.dim.work);
22832354
}
22842355

22852356
static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
@@ -2557,6 +2628,7 @@ static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
25572628
continue;
25582629

25592630
rx_ring = &priv->rx_rings[index];
2631+
rx_ring->dim.event_ctr++;
25602632

25612633
if (likely(napi_schedule_prep(&rx_ring->napi))) {
25622634
rx_ring->int_disable(rx_ring);
@@ -2601,6 +2673,7 @@ static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
26012673

26022674
if (status & UMAC_IRQ_RXDMA_DONE) {
26032675
rx_ring = &priv->rx_rings[DESC_INDEX];
2676+
rx_ring->dim.event_ctr++;
26042677

26052678
if (likely(napi_schedule_prep(&rx_ring->napi))) {
26062679
rx_ring->int_disable(rx_ring);

drivers/net/ethernet/broadcom/genet/bcmgenet.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/mii.h>
1717
#include <linux/if_vlan.h>
1818
#include <linux/phy.h>
19+
#include <linux/net_dim.h>
1920

2021
/* total number of Buffer Descriptors, same for Rx/Tx */
2122
#define TOTAL_DESC 256
@@ -572,6 +573,16 @@ struct bcmgenet_tx_ring {
572573
struct bcmgenet_priv *priv;
573574
};
574575

576+
struct bcmgenet_net_dim {
577+
u16 use_dim;
578+
u16 event_ctr;
579+
unsigned long packets;
580+
unsigned long bytes;
581+
u32 coal_usecs;
582+
u32 coal_pkts;
583+
struct net_dim dim;
584+
};
585+
575586
struct bcmgenet_rx_ring {
576587
struct napi_struct napi; /* Rx NAPI struct */
577588
unsigned long bytes;
@@ -586,6 +597,7 @@ struct bcmgenet_rx_ring {
586597
unsigned int cb_ptr; /* Rx ring initial CB ptr */
587598
unsigned int end_ptr; /* Rx ring end CB ptr */
588599
unsigned int old_discards;
600+
struct bcmgenet_net_dim dim;
589601
void (*int_enable)(struct bcmgenet_rx_ring *);
590602
void (*int_disable)(struct bcmgenet_rx_ring *);
591603
struct bcmgenet_priv *priv;

0 commit comments

Comments
 (0)