[PATCH net-next v2 09/10] net: dsa: realtek: rtl8365mb: offload DCB apptrust
From: Oleksij Rempel
Date: Thu Nov 12 2026 - 04:00:55 EST
Wire up port_get_apptrust and port_set_apptrust. The admin can then
choose through dcbnl apptrust which ingress classification a port trusts
for its priority: 802.1p (PCP), DSCP or both. A port that trusts nothing
keeps the port default priority.
In the driver's model of the hardware, the trusted priority-decision
table is switch-wide, so all ports that trust something must trust the
same selectors. While another port trusts a set of selectors, a request
to trust a different set is refused with -EBUSY.
When both are trusted, DSCP must come first. When PCP is trusted, the
switch classifies an untagged frame by the PCP given to untagged
frames. A PCP source ranked above DSCP would then hide DSCP from
untagged IP frames. Refuse the order pcp dscp and any unknown selector
with -EOPNOTSUPP.
Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx>
---
Changes in v2:
- move selector and precedence validation into rtl83xx-qos.c behind new
qos_get/set_apptrust callbacks; weights and -EBUSY stay here (Linus)
- use one-hot decision weights; in the driver's model, the v1 linear
weights could tie PCP with DSCP (Linus)
- define the apptrust slot order once (enum rtl83xx_apptrust_slot) for
the shared selector table and the backend source table
- write the PCP and DSCP weights with one register update, so a failed
write cannot leave the trusted table half programmed (sashiko)
- check that all apptrust sources share the one register that is updated
- rank DSCP above PCP and refuse pcp dscp with -EOPNOTSUPP, because
with PCP first, untagged IP frames never reach DSCP
---
drivers/net/dsa/realtek/realtek.h | 16 +++
drivers/net/dsa/realtek/rtl8365mb_main.c | 4 +
drivers/net/dsa/realtek/rtl8365mb_qos.c | 171 +++++++++++++++++++++++++++++++
drivers/net/dsa/realtek/rtl8365mb_qos.h | 4 +
drivers/net/dsa/realtek/rtl83xx-qos.c | 75 ++++++++++++++
drivers/net/dsa/realtek/rtl83xx-qos.h | 4 +
6 files changed, 274 insertions(+)
diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h
index a9bdadbea3cbe19eeef0f33762318f022881e75f..0008ba9681cccb521d85d9bfc97425ad1b777aa6 100644
--- a/drivers/net/dsa/realtek/realtek.h
+++ b/drivers/net/dsa/realtek/realtek.h
@@ -100,6 +100,15 @@ struct realtek_priv {
void *chip_data; /* Per-chip extra variant data */
};
+/* apptrust selector slots: the index into the trust[] array of the
+ * qos_get/set_apptrust callbacks, in precedence order (highest first)
+ */
+enum rtl83xx_apptrust_slot {
+ RTL83XX_APPTRUST_DSCP,
+ RTL83XX_APPTRUST_PCP,
+ RTL83XX_APPTRUST_COUNT,
+};
+
/*
* struct realtek_ops - vtable for the per-SMI-chiptype operations
* @detect: detects the chiptype
@@ -174,6 +183,13 @@ struct realtek_ops {
int (*qos_set_pcp_prio)(struct realtek_priv *priv, u8 pcp, u8 iprio);
int (*qos_set_dscp_prio)(struct realtek_priv *priv, u8 dscp,
u8 iprio);
+ /* @trust is indexed by enum rtl83xx_apptrust_slot and has @n entries.
+ * The trust scope (per port or switch-wide) differs between chips.
+ */
+ int (*qos_set_apptrust)(struct realtek_priv *priv, int port,
+ const bool *trust, unsigned int n);
+ int (*qos_get_apptrust)(struct realtek_priv *priv, int port,
+ bool *trust, unsigned int n);
};
struct realtek_variant {
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index 512463be9212bb072380bc26c44b1bef23428917..d16e7c99217bdc01d1bdf7dbbcb8e7ed1441e6a8 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -3481,6 +3481,8 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
.port_bridge_flags = rtl83xx_port_bridge_flags,
.port_get_default_prio = rtl83xx_port_get_default_prio,
.port_set_default_prio = rtl83xx_port_set_default_prio,
+ .port_get_apptrust = rtl83xx_port_get_apptrust,
+ .port_set_apptrust = rtl83xx_port_set_apptrust,
.port_stp_state_set = rtl8365mb_port_stp_state_set,
.port_fast_age = rtl83xx_port_fast_age,
.port_fdb_add = rtl83xx_port_fdb_add,
@@ -3527,6 +3529,8 @@ static const struct realtek_ops rtl8365mb_ops = {
.qos_set_port_prio = rtl8365mb_qos_set_port_prio,
.qos_set_pcp_prio = rtl8365mb_qos_set_pcp_prio,
.qos_set_dscp_prio = rtl8365mb_qos_set_dscp_prio,
+ .qos_set_apptrust = rtl8365mb_qos_set_apptrust,
+ .qos_get_apptrust = rtl8365mb_qos_get_apptrust,
};
static const char *const rtl8365mb_supplies[] = {
diff --git a/drivers/net/dsa/realtek/rtl8365mb_qos.c b/drivers/net/dsa/realtek/rtl8365mb_qos.c
index 2a849d0e4898a712d8f4babd5cdd03fe8e207814..c465d639ee5e527a604498fb064d6757aac1fda4 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_qos.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_qos.c
@@ -12,6 +12,7 @@
#include <linux/build_bug.h>
#include <linux/dcbnl.h>
#include <linux/regmap.h>
+#include <linux/string.h>
#include <net/dsa.h>
#include <net/dscp.h>
#include <net/ieee8021q.h>
@@ -114,6 +115,8 @@ static_assert(RTL8365MB_QOS_WEIGHT_BITS * RTL8365MB_QOS_WEIGHTS_PER_REG <=
#define RTL8365MB_QOS_PRIDEC_TABLE_UNTRUSTED 0
#define RTL8365MB_QOS_PRIDEC_TABLE_TRUSTED 1
+static_assert(RTL8365MB_QOS_MAX_PORTS <= RTL8365MB_QOS_REG_BITS);
+
/* Priority-decision sources, in the hardware's numbering */
enum rtl8365mb_qos_pridec_src {
RTL8365MB_QOS_PRIDEC_PORT = 0,
@@ -141,6 +144,36 @@ static_assert(RTL8365MB_QOS_PRIDEC_REG(RTL8365MB_QOS_PRIDEC_TABLE_TRUSTED,
#define RTL8365MB_QOS_WEIGHT_DISABLED 0
#define RTL8365MB_QOS_WEIGHT_PORT BIT(0)
+/* Priority-decision source of each apptrust slot */
+static const u8 rtl8365mb_qos_apptrust_src[] = {
+ [RTL83XX_APPTRUST_DSCP] = RTL8365MB_QOS_PRIDEC_DSCP,
+ [RTL83XX_APPTRUST_PCP] = RTL8365MB_QOS_PRIDEC_1Q,
+};
+
+/* The 1Q source also classifies untagged frames, by the PCP given to untagged
+ * frames. Ranked above DSCP, it would hide DSCP from untagged IP frames.
+ */
+static_assert(RTL83XX_APPTRUST_DSCP < RTL83XX_APPTRUST_PCP);
+
+static_assert(ARRAY_SIZE(rtl8365mb_qos_apptrust_src) ==
+ RTL83XX_APPTRUST_COUNT);
+
+/* Register of source @_s in the trusted decision table */
+#define RTL8365MB_QOS_APPTRUST_REG(_s) \
+ RTL8365MB_QOS_PRIDEC_REG(RTL8365MB_QOS_PRIDEC_TABLE_TRUSTED, _s)
+
+/* The top trusted rank must fit in the weight field */
+static_assert((RTL8365MB_QOS_WEIGHT_PORT <<
+ ARRAY_SIZE(rtl8365mb_qos_apptrust_src)) <
+ BIT(RTL8365MB_QOS_WEIGHT_BITS));
+
+/* Each trusted slot outranks the port default and every later slot */
+static u8 rtl8365mb_qos_apptrust_weight(unsigned int slot)
+{
+ return RTL8365MB_QOS_WEIGHT_PORT <<
+ (ARRAY_SIZE(rtl8365mb_qos_apptrust_src) - slot);
+}
+
static u32 rtl8365mb_qos_sel_mask(unsigned int idx)
{
unsigned int off = RTL8365MB_QOS_SEL_OFFSET(idx);
@@ -304,6 +337,14 @@ static int rtl8365mb_qos_set_pridec(struct realtek_priv *priv, int table,
weight);
}
+static int rtl8365mb_qos_get_pridec(struct realtek_priv *priv, int table,
+ int src)
+{
+ return rtl8365mb_qos_regmap_read_field(priv,
+ RTL8365MB_QOS_PRIDEC_REG(table, src),
+ rtl8365mb_qos_weight_mask(src));
+}
+
/* Program every source in both tables rather than relying on the reset state */
int rtl8365mb_qos_pridec_init(struct realtek_priv *priv)
{
@@ -327,3 +368,133 @@ int rtl8365mb_qos_pridec_init(struct realtek_priv *priv)
/* In the driver's model, a clear bit selects the untrusted table */
return regmap_write(priv->map, RTL8365MB_QOS_PRIDEC_TBL_SEL_REG, 0);
}
+
+/* Read the trust state of each apptrust slot from the trusted table */
+static int rtl8365mb_qos_apptrust_read(struct realtek_priv *priv, bool *trust)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(rtl8365mb_qos_apptrust_src); i++) {
+ int weight;
+
+ weight = rtl8365mb_qos_get_pridec(priv,
+ RTL8365MB_QOS_PRIDEC_TABLE_TRUSTED,
+ rtl8365mb_qos_apptrust_src[i]);
+ if (weight < 0)
+ return weight;
+
+ trust[i] = weight != RTL8365MB_QOS_WEIGHT_DISABLED;
+ }
+
+ return 0;
+}
+
+int rtl8365mb_qos_get_apptrust(struct realtek_priv *priv, int port, bool *trust,
+ unsigned int n)
+{
+ u32 trusted_ports;
+ int ret;
+
+ if (n != ARRAY_SIZE(rtl8365mb_qos_apptrust_src))
+ return -EINVAL;
+
+ ret = regmap_read(priv->map, RTL8365MB_QOS_PRIDEC_TBL_SEL_REG,
+ &trusted_ports);
+ if (ret)
+ return ret;
+
+ /* On the untrusted table nothing but the port default is trusted. */
+ if (!(trusted_ports & BIT(port))) {
+ memset(trust, 0, n * sizeof(*trust));
+ return 0;
+ }
+
+ return rtl8365mb_qos_apptrust_read(priv, trust);
+}
+
+/* The trusted table is shared by all ports. A port may switch to it only if it
+ * trusts the same sources as the other ports already on it.
+ */
+static int rtl8365mb_qos_apptrust_check_shared(struct realtek_priv *priv,
+ int port, const bool *trust)
+{
+ bool other[ARRAY_SIZE(rtl8365mb_qos_apptrust_src)];
+ u32 trusted_ports;
+ int ret;
+
+ ret = regmap_read(priv->map, RTL8365MB_QOS_PRIDEC_TBL_SEL_REG,
+ &trusted_ports);
+ if (ret)
+ return ret;
+
+ if (!(trusted_ports & ~BIT(port)))
+ return 0;
+
+ ret = rtl8365mb_qos_apptrust_read(priv, other);
+ if (ret)
+ return ret;
+
+ if (memcmp(trust, other, sizeof(other))) {
+ dev_err(priv->dev,
+ "trust profile is switch-wide; another port already trusts different sources\n");
+ return -EBUSY;
+ }
+
+ return 0;
+}
+
+int rtl8365mb_qos_set_apptrust(struct realtek_priv *priv, int port,
+ const bool *trust, unsigned int n)
+{
+ u32 reg = RTL8365MB_QOS_APPTRUST_REG(rtl8365mb_qos_apptrust_src[0]);
+ u32 mask = 0, val = 0;
+ bool any = false;
+ int ret, i;
+
+ if (n != ARRAY_SIZE(rtl8365mb_qos_apptrust_src))
+ return -EINVAL;
+
+ ret = rtl8365mb_qos_check_port(priv, port);
+ if (ret)
+ return ret;
+
+ for (i = 0; i < n; i++)
+ any |= trust[i];
+
+ if (!any)
+ return regmap_clear_bits(priv->map,
+ RTL8365MB_QOS_PRIDEC_TBL_SEL_REG,
+ BIT(port));
+
+ ret = rtl8365mb_qos_apptrust_check_shared(priv, port, trust);
+ if (ret)
+ return ret;
+
+ /* Collect all slot weights into one mask and value and write them once,
+ * so a failed write cannot leave the trusted table half programmed.
+ */
+ for (i = 0; i < n; i++) {
+ u8 weight = trust[i] ? rtl8365mb_qos_apptrust_weight(i) :
+ RTL8365MB_QOS_WEIGHT_DISABLED;
+ u8 src = rtl8365mb_qos_apptrust_src[i];
+ u32 src_mask = rtl8365mb_qos_weight_mask(src);
+
+ /* A slot missing from rtl8365mb_qos_apptrust_src[] is 0, which
+ * is the port source. Reject it. Also reject a source whose
+ * weight is in a different register than reg.
+ */
+ if (WARN_ON_ONCE(src == RTL8365MB_QOS_PRIDEC_PORT ||
+ RTL8365MB_QOS_APPTRUST_REG(src) != reg))
+ return -EINVAL;
+
+ mask |= src_mask;
+ val |= field_prep(src_mask, weight);
+ }
+
+ ret = regmap_update_bits(priv->map, reg, mask, val);
+ if (ret)
+ return ret;
+
+ return regmap_set_bits(priv->map, RTL8365MB_QOS_PRIDEC_TBL_SEL_REG,
+ BIT(port));
+}
diff --git a/drivers/net/dsa/realtek/rtl8365mb_qos.h b/drivers/net/dsa/realtek/rtl8365mb_qos.h
index 71949ece4af8fae5c49cf105ddaa3db9bc622cf2..eed5ea5fa7f345ce6989e3e890d7cd755da9d0fc 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_qos.h
+++ b/drivers/net/dsa/realtek/rtl8365mb_qos.h
@@ -24,5 +24,9 @@ int rtl8365mb_qos_set_port_prio(struct realtek_priv *priv, int port, u8 iprio,
u8 untag_pcp);
int rtl8365mb_qos_set_pcp_prio(struct realtek_priv *priv, u8 pcp, u8 iprio);
int rtl8365mb_qos_set_dscp_prio(struct realtek_priv *priv, u8 dscp, u8 iprio);
+int rtl8365mb_qos_set_apptrust(struct realtek_priv *priv, int port,
+ const bool *trust, unsigned int n);
+int rtl8365mb_qos_get_apptrust(struct realtek_priv *priv, int port, bool *trust,
+ unsigned int n);
#endif /* _REALTEK_RTL8365MB_QOS_H */
diff --git a/drivers/net/dsa/realtek/rtl83xx-qos.c b/drivers/net/dsa/realtek/rtl83xx-qos.c
index 08091586901291e45ad1a2c64730cdcc30298b7a..b25a9dfb9138b5c2c176dbe68974cdd65e1643bb 100644
--- a/drivers/net/dsa/realtek/rtl83xx-qos.c
+++ b/drivers/net/dsa/realtek/rtl83xx-qos.c
@@ -10,6 +10,7 @@
* The backend maps internal priorities to its egress queues itself.
*/
+#include <linux/build_bug.h>
#include <linux/dcbnl.h>
#include <linux/errno.h>
#include <net/dscp.h>
@@ -19,6 +20,25 @@
#include "realtek.h"
#include "rtl83xx-qos.h"
+/* dcbnl selector of each apptrust slot */
+static const u8 rtl83xx_apptrust_sel[] = {
+ [RTL83XX_APPTRUST_DSCP] = IEEE_8021QAZ_APP_SEL_DSCP,
+ [RTL83XX_APPTRUST_PCP] = DCB_APP_SEL_PCP,
+};
+
+static_assert(ARRAY_SIZE(rtl83xx_apptrust_sel) == RTL83XX_APPTRUST_COUNT);
+
+static int rtl83xx_apptrust_sel_to_slot(u8 sel)
+{
+ int slot;
+
+ for (slot = 0; slot < ARRAY_SIZE(rtl83xx_apptrust_sel); slot++)
+ if (rtl83xx_apptrust_sel[slot] == sel)
+ return slot;
+
+ return -EINVAL;
+}
+
/* Seed the PCP and DSCP tables, although neither source is trusted yet */
int rtl83xx_qos_init(struct dsa_switch *ds)
{
@@ -113,3 +133,58 @@ int rtl83xx_port_set_default_prio(struct dsa_switch *ds, int port, u8 prio)
return priv->ops->qos_set_port_prio(priv, port, tt, prio);
}
EXPORT_SYMBOL_NS_GPL(rtl83xx_port_set_default_prio, "REALTEK_DSA");
+
+int rtl83xx_port_get_apptrust(struct dsa_switch *ds, int port, u8 *sel,
+ int *nsel)
+{
+ bool trust[ARRAY_SIZE(rtl83xx_apptrust_sel)];
+ struct realtek_priv *priv = ds->priv;
+ int i, ret;
+
+ if (!priv->ops->qos_get_apptrust)
+ return -EOPNOTSUPP;
+
+ ret = priv->ops->qos_get_apptrust(priv, port, trust,
+ ARRAY_SIZE(trust));
+ if (ret)
+ return ret;
+
+ *nsel = 0;
+
+ for (i = 0; i < ARRAY_SIZE(rtl83xx_apptrust_sel); i++)
+ if (trust[i])
+ sel[(*nsel)++] = rtl83xx_apptrust_sel[i];
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(rtl83xx_port_get_apptrust, "REALTEK_DSA");
+
+int rtl83xx_port_set_apptrust(struct dsa_switch *ds, int port, const u8 *sel,
+ int nsel)
+{
+ bool trust[ARRAY_SIZE(rtl83xx_apptrust_sel)] = { };
+ struct realtek_priv *priv = ds->priv;
+ int i, prev = -1;
+
+ if (!priv->ops->qos_set_apptrust)
+ return -EOPNOTSUPP;
+
+ /* The backends use a fixed precedence, so the selector list
+ * must follow the slot order.
+ */
+ for (i = 0; i < nsel; i++) {
+ int slot = rtl83xx_apptrust_sel_to_slot(sel[i]);
+
+ if (slot < 0 || slot <= prev) {
+ dev_err(priv->dev,
+ "unsupported apptrust order; supported: dscp, pcp, dscp pcp\n");
+ return -EOPNOTSUPP;
+ }
+
+ prev = slot;
+ trust[slot] = true;
+ }
+
+ return priv->ops->qos_set_apptrust(priv, port, trust, ARRAY_SIZE(trust));
+}
+EXPORT_SYMBOL_NS_GPL(rtl83xx_port_set_apptrust, "REALTEK_DSA");
diff --git a/drivers/net/dsa/realtek/rtl83xx-qos.h b/drivers/net/dsa/realtek/rtl83xx-qos.h
index 82ad9338d78db9062d8ceed06378b31e467ee04d..fbddc7b7d9dd37918423afc5373b684596637a05 100644
--- a/drivers/net/dsa/realtek/rtl83xx-qos.h
+++ b/drivers/net/dsa/realtek/rtl83xx-qos.h
@@ -11,5 +11,9 @@ int rtl83xx_qos_init(struct dsa_switch *ds);
int rtl83xx_qos_init_port(struct dsa_switch *ds, int port);
int rtl83xx_port_get_default_prio(struct dsa_switch *ds, int port);
int rtl83xx_port_set_default_prio(struct dsa_switch *ds, int port, u8 prio);
+int rtl83xx_port_get_apptrust(struct dsa_switch *ds, int port, u8 *sel,
+ int *nsel);
+int rtl83xx_port_set_apptrust(struct dsa_switch *ds, int port, const u8 *sel,
+ int nsel);
#endif /* _RTL83XX_QOS_H */
--
2.47.3