[PATCH net-next v2 1/3] net: stmmac: separate the TC count from the queue count on XGMAC
From: Sagar Cheluvegowda
Date: Mon Nov 23 2026 - 19:04:50 EST
The driver assumes one traffic class per queue and bounds every TC
based register write by {rx,tx}_queues_to_use. Newer XGMAC hardware
has more queues than traffic classes (up to 16 queues and 8 TCs),
and the number of TCs is reported separately by the HW feature
register. The result is writes to TC registers that do not exist.
Add stmmac_num_tc(), which returns the number of TCs for the queues
in use and falls back to the number of TX queues on cores that do not
report it, and use it where the register is per TC:
- TX queue weight, CBS (replay on resume) and TX priority (TC_PRTY):
configure only the existing TCs.
- ETS: the loop was bound by MTL_MAX_TX_QUEUES, use the 8 TCs the
hardware can have. This also keeps it from walking off the TC
registers when the macro is raised.
Other registers are per queue and only exist for the first 8 queues
because they are per 802.1Q priority: Qx_TX_FLOW_CTRL and the PSRQ
fields. Bound them by STMMAC_NUM_PRIO_QUEUES instead of the TC count,
as the TC count is not the limit for them. Writing Qx_TX_FLOW_CTRL
past queue 7 would hit RX_FLOW_CTRL and MAC_RxQ_Ctrl4/5. Reject VLAN
priority filters that name a queue outside this range, and only walk
those queues when updating the PSRQ assignments.
The checks are in the callers, which can return an error to the user,
instead of in the callbacks, which are void.
Link: https://lore.kernel.org/netdev/20260601162537.553512-2-j.raczynski@xxxxxxxxxxx/
Signed-off-by: Sagar Cheluvegowda <sagar.cheluvegowda@xxxxxxxxxxxxxxxx>
---
.../net/ethernet/stmicro/stmmac/dwxgmac2.h | 1 +
.../ethernet/stmicro/stmmac/dwxgmac2_core.c | 2 +-
drivers/net/ethernet/stmicro/stmmac/stmmac.h | 17 ++++++++++++++
.../net/ethernet/stmicro/stmmac/stmmac_main.c | 22 ++++++++++++-------
.../net/ethernet/stmicro/stmmac/stmmac_tc.c | 21 +++++++++++-------
5 files changed, 46 insertions(+), 17 deletions(-)
diff --git a/drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h b/drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h
index f8ab347f7b5b..77a54792893b 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h
+++ b/drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h
@@ -291,6 +291,7 @@
#define XGMAC_TTC GENMASK(6, 4)
#define XGMAC_TXQEN GENMASK(3, 2)
#define XGMAC_TSF BIT(1)
+#define XGMAC_MAX_TC 8
#define XGMAC_MTL_TCx_ETS_CONTROL(x) (0x00001110 + (0x80 * (x)))
#define XGMAC_MTL_TCx_QUANTUM_WEIGHT(x) (0x00001118 + (0x80 * (x)))
#define XGMAC_MTL_TCx_SENDSLOPE(x) (0x0000111c + (0x80 * (x)))
diff --git a/drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c b/drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c
index 09194b9d8322..e50c0f4c482d 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c
@@ -235,7 +235,7 @@ static void dwxgmac2_prog_mtl_tx_algorithms(struct mac_device_info *hw,
writel(value, ioaddr + XGMAC_MTL_OPMODE);
/* Set ETS if desired */
- for (i = 0; i < MTL_MAX_TX_QUEUES; i++) {
+ for (i = 0; i < XGMAC_MAX_TC; i++) {
value = readl(ioaddr + XGMAC_MTL_TCx_ETS_CONTROL(i));
value &= ~XGMAC_TSA;
if (ets)
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
index fcc395321816..24bbe72e0071 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
@@ -57,6 +57,11 @@ struct stmmac_tx_info {
bool is_jumbo;
};
+/* Number of 802.1Q priorities. This is also the number of queues that have a
+ * per-priority register (PSRQ field, Qx_TX_FLOW_CTRL) on DWMAC4/XGMAC.
+ */
+#define STMMAC_NUM_PRIO_QUEUES 8
+
#define STMMAC_TBS_AVAIL BIT(0)
#define STMMAC_TBS_EN BIT(1)
@@ -441,6 +446,18 @@ static inline bool stmmac_check_timestamp_cap(struct stmmac_priv *priv)
return priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp;
}
+/* Most cores have one traffic class per queue, but newer XGMAC can have more
+ * queues than traffic classes (the number of TCs is reported by the HW
+ * feature register).
+ */
+static inline u32 stmmac_num_tc(const struct stmmac_priv *priv)
+{
+ if (!priv->dma_cap.numtc)
+ return priv->plat->tx_queues_to_use;
+
+ return min_t(u32, priv->dma_cap.numtc, priv->plat->tx_queues_to_use);
+}
+
static inline bool stmmac_xdp_is_enabled(struct stmmac_priv *priv)
{
return !!priv->xdp_prog;
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index ca4c7fb11b73..5d02f7034a7a 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -1023,7 +1023,11 @@ static int stmmac_legacy_serdes_power_up(struct stmmac_priv *priv)
static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex,
unsigned int flow_ctrl)
{
- u8 tx_cnt = priv->plat->tx_queues_to_use;
+ /* The per-queue flow control registers exist for the first
+ * STMMAC_NUM_PRIO_QUEUES queues only.
+ */
+ u8 tx_cnt = min_t(u8, priv->plat->tx_queues_to_use,
+ STMMAC_NUM_PRIO_QUEUES);
stmmac_flow_ctrl(priv, priv->hw, duplex, flow_ctrl, priv->pause_time,
tx_cnt);
@@ -3531,11 +3535,11 @@ static void stmmac_set_rings_length(struct stmmac_priv *priv)
*/
static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
{
- u8 tx_queues_count = priv->plat->tx_queues_to_use;
+ u8 num_tc = stmmac_num_tc(priv);
u32 weight;
u8 queue;
- for (queue = 0; queue < tx_queues_count; queue++) {
+ for (queue = 0; queue < num_tc; queue++) {
weight = priv->plat->tx_queues_cfg[queue].weight;
stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
}
@@ -3548,12 +3552,12 @@ static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
*/
void stmmac_configure_cbs(struct stmmac_priv *priv)
{
- u8 tx_queues_count = priv->plat->tx_queues_to_use;
+ u8 num_tc = stmmac_num_tc(priv);
u32 mode_to_use;
u8 queue;
/* queue 0 is reserved for legacy traffic */
- for (queue = 1; queue < tx_queues_count; queue++) {
+ for (queue = 1; queue < num_tc; queue++) {
mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
if (mode_to_use == MTL_QUEUE_DCB)
continue;
@@ -3591,7 +3595,8 @@ static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
*/
static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
{
- u8 rx_queues_count = priv->plat->rx_queues_to_use;
+ u8 rx_queues_count = min_t(u8, priv->plat->rx_queues_to_use,
+ STMMAC_NUM_PRIO_QUEUES);
u8 queue;
u32 prio;
@@ -3611,9 +3616,10 @@ static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
*/
static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
{
- int i;
+ u32 num_tc = stmmac_num_tc(priv);
+ u32 i;
- for (i = 0; i < priv->plat->tx_queues_to_use; i++)
+ for (i = 0; i < num_tc; i++)
stmmac_tx_queue_prio(priv, priv->hw,
priv->xmit_qdisc.prio[i], i);
}
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_tc.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_tc.c
index 5df6ef1dd438..d18d064b42ad 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_tc.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_tc.c
@@ -686,12 +686,16 @@ static struct stmmac_rfs_entry *tc_find_rfs(struct stmmac_priv *priv,
static void tc_update_vlan_prio(struct stmmac_priv *priv, u8 prio)
{
- u8 prios[MTL_MAX_RX_QUEUES] = {};
+ u8 prios[STMMAC_NUM_PRIO_QUEUES] = {};
struct stmmac_rfs_entry *entry;
+ unsigned int rx_cnt = min_t(unsigned int, priv->plat->rx_queues_to_use,
+ STMMAC_NUM_PRIO_QUEUES);
unsigned int queue, i;
- /* Restore platform assignments with the same precedence as setup. */
- for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++) {
+ /* Restore platform assignments with the same precedence as setup. Only
+ * the first STMMAC_NUM_PRIO_QUEUES queues have a PSRQ field.
+ */
+ for (queue = 0; queue < rx_cnt; queue++) {
if (!priv->plat->rx_queues_cfg[queue].use_prio)
continue;
@@ -706,19 +710,19 @@ static void tc_update_vlan_prio(struct stmmac_priv *priv, u8 prio)
*/
list_for_each_entry(entry, &priv->vlan_prio, vlan_node) {
/* The RX queue count may have been reduced with ethtool. */
- if (entry->tc >= priv->plat->rx_queues_to_use)
+ if (entry->tc >= rx_cnt)
continue;
- for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++)
+ for (queue = 0; queue < rx_cnt; queue++)
prios[queue] &= ~entry->vlan_prio;
prios[entry->tc] |= entry->vlan_prio;
}
/* Install the new assignment before clearing the previous queue. */
- for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++)
+ for (queue = 0; queue < rx_cnt; queue++)
if (prios[queue] & prio)
stmmac_rx_queue_prio(priv, priv->hw, prios[queue], queue);
- for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++)
+ for (queue = 0; queue < rx_cnt; queue++)
if (!(prios[queue] & prio))
stmmac_rx_queue_prio(priv, priv->hw, prios[queue], queue);
}
@@ -749,7 +753,8 @@ static int tc_add_vlan_flow(struct stmmac_priv *priv,
if (!dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_VLAN))
return -EINVAL;
- if (tc < 0 || tc >= priv->plat->rx_queues_to_use) {
+ if (tc < 0 || tc >= priv->plat->rx_queues_to_use ||
+ tc >= STMMAC_NUM_PRIO_QUEUES) {
netdev_err(priv->dev, "Invalid traffic class\n");
return -EINVAL;
}
--
2.34.1