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 Reviewed-by: Linus Walleij Reviewed-by: Alvin Šipraga Reviewed-by: Luiz Angelo Daros de Luca --- Changes in v3: - wrap lines at 80 columns, splitting long calls into local variables - keep the Reviewed-by tags: v3 only rewraps lines and adds local variables 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 | 172 +++++++++++++++++++++++++++++++ drivers/net/dsa/realtek/rtl8365mb_qos.h | 4 + drivers/net/dsa/realtek/rtl83xx-qos.c | 76 ++++++++++++++ drivers/net/dsa/realtek/rtl83xx-qos.h | 4 + 6 files changed, 276 insertions(+) diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h index c79a026c7999b1d0055f336dedff64f80e1d30ec..8d2293dc12b22257e5615e5d66e85f3d4653b5b0 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 @@ -175,6 +184,13 @@ struct realtek_ops { 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 e3d1dadf893646addd617b68013550e2f8ab8974..9be0842831486f6f90f94cc1862099b42d6f2225 100644 --- a/drivers/net/dsa/realtek/rtl8365mb_main.c +++ b/drivers/net/dsa/realtek/rtl8365mb_main.c @@ -3480,6 +3480,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, @@ -3526,6 +3528,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 bf11a5974d6c7cf56e2ccce47f364a7dee549304..e96a99f3fbf5fca0af40dff9be221d7ceb5e885c 100644 --- a/drivers/net/dsa/realtek/rtl8365mb_qos.c +++ b/drivers/net/dsa/realtek/rtl8365mb_qos.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -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); @@ -303,6 +336,15 @@ static int rtl8365mb_qos_set_pridec(struct realtek_priv *priv, int table, return rtl8365mb_qos_regmap_update_field(priv, reg, mask, weight); } +static int rtl8365mb_qos_get_pridec(struct realtek_priv *priv, int table, + int src) +{ + u32 reg = RTL8365MB_QOS_PRIDEC_REG(table, src); + u32 mask = rtl8365mb_qos_weight_mask(src); + + return rtl8365mb_qos_regmap_read_field(priv, reg, mask); +} + /* Program every source in both tables rather than relying on the reset state */ int rtl8365mb_qos_pridec_init(struct realtek_priv *priv) { @@ -327,3 +369,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 table = RTL8365MB_QOS_PRIDEC_TABLE_TRUSTED; + int i; + + for (i = 0; i < ARRAY_SIZE(rtl8365mb_qos_apptrust_src); i++) { + u8 src = rtl8365mb_qos_apptrust_src[i]; + int weight; + + weight = rtl8365mb_qos_get_pridec(priv, table, src); + 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..d08ffd585caf7d91f4ddff10b90d6eafd2a2a734 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 #include #include #include @@ -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,59 @@ 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