🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Luiz Angelo Daros de Luca Sun, 18 Oct 2026 22:40
Re: [PATCH net-next v6 3/9] net: dsa: realtek: rtl8365mb: set speed for family D
#4671
Author: Luiz Angelo Daros de Luca
Date: Sun, 18 Oct 2026 22:40
128 lines
14765 bytes
> diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
> index 840db5a..2dabc4d 100644
> --- a/drivers/net/dsa/realtek/rtl8365mb_main.c
> +++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
> @@ -344,6 +344,7 @@
>  #define RTL8365MB_PORT_SPEED_10M       0
>  #define RTL8365MB_PORT_SPEED_100M      1
>  #define RTL8365MB_PORT_SPEED_1000M     2
> +#define RTL8365MB_D_PORT_SPEED_2500M   5

I also used the same _<FAMILY>_ tag trick previously, but it gets
confusing quickly. Does RTL8365MB_D_xxx mean something exclusive to
family D, or family D and later? I hope a more seasoned reviewer can
weigh in on this.

When I implemented support for the RTL8367R (pre-family B), I ran into
this exact problem of macro definitions (and code conditions) turning
into unmaintainable spaghetti.

In this particular case, as 5 does not exist in Family C and the
semantics of the existing values still hold, you can omit the _D_. A
simple comment will be enough if the semantics change for a future
family.

>  /* External interface force configuration registers 0~2 */
>  #define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0         0x1310 /* EXT0 */
> @@ -361,6 +362,28 @@
>  #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_LINK_MASK          BIT(4)
>  #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_DUPLEX_MASK                BIT(2)
>  #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK         GENMASK(1, 0)
> +/* Width of the FORCE_SPEED field, bits[1:0] */
> +#define   RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_WIDTH                2

This is what bitfield masks/macros are for; there is no need for an
explicit WIDTH define. More comments below on the code using it.

> +/* bits[13:12]; bit 13 reserved, no defined speed value uses it yet */
> +#define   RTL8365MB_D_DIGITAL_INTERFACE_FORCE_SPEED2_MASK      GENMASK(13, 12)
> +
> +#define RTL8365MB_D_DIGITAL_INTERFACE_FORCE_REG_BASE           0x12c0
> +#define RTL8365MB_D_DIGITAL_INTERFACE_FORCE_REG(_port) \
> +               (RTL8365MB_D_DIGITAL_INTERFACE_FORCE_REG_BASE + (_port))
> +
> +#define RTL8365MB_D_DIGITAL_INTERFACE_FORCE_EN_BASE            0x12c8
> +#define RTL8365MB_D_DIGITAL_INTERFACE_FORCE_REG_EN(_port) \
> +               (RTL8365MB_D_DIGITAL_INTERFACE_FORCE_EN_BASE + (_port))
> +
> +/* Per-port force-mode enable. The vendor RTL8367D API
> + * (dal_rtl8367d_port_macForceLink_set()) treats it as an on/off switch for
> + * the whole forced configuration: all bits set when the MAC is forced, zero
> + * when it is not. The meaning of the individual bits is not documented.
> + * This driver always forces the MAC, as on family C, so all bits are set
> + * on every call, including link down.
> + */
> +#define RTL8365MB_D_DIGITAL_INTERFACE_FORCE_EN_ALL_MASK        GENMASK(15, 0)
>
>  /* CPU port mask register - controls which ports are treated as CPU ports */
>  #define RTL8365MB_CPU_PORT_MASK_REG    0x1219
> @@ -1646,23 +1669,32 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
>         u32 r_duplex;
>         u32 r_speed;
>         u32 r_link;
> +       bool is_d;

Thinking about future families like E, F, G..., a boolean flag like
is_d might not scale well.
You can still keep the family id in a variable to keep lines short and clean:

        enum rtl8365mb_family family;

>         int val;
>         int ret;

[...]

>               FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_LINK_MASK, r_link) |
>               FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_DUPLEX_MASK,
>                          r_duplex) |
> -             FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK, r_speed);
> -       ret = regmap_write(priv->map,
> -                          RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(extint->id),
> -                          val);
> -       if (ret)
> -               return ret;
> +             FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK,
> +                        r_speed &
> +                        RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK);

Isn't the point of FIELD_GET/FIELD_PREP to avoid bitwise operations
like this? For fields split between bit ranges, you can use the same
trick as in RTL8365MB_CVLAN_MBR_LO_MASK, RTL8365MB_CVLAN_MBR_HI_MASK.

> +
> +       if (is_d) {

For multiple/bigger differences, we will need to have different
functions for each family. In this case, for small differences, I
think is_d, is_e, is_f... are better handled by a switch statement
with the same indentation cost as an if:

            switch (family) {
            case RTL8365MB_FAMILY_D:
                 ...
                break;
            case RTL8365MB_FAMILY_C:
                 ...
            }

And, by omitting the default clause, when we add a future FAMILY_E and
forget to add a case for it, the compiler will warn us of the missing
check. You can even use a (commented) fallthrough when a family
incrementally expands a feature.

> +               /* Speed is 3 bits on family D: bits[1:0] go into FORCE_SPEED,
> +                * bit[2] goes into FORCE_SPEED2 (bit 12); only 2500M sets it,
> +                * bit 13 of the field is unused.
> +                */
> +               val |= FIELD_PREP(RTL8365MB_D_DIGITAL_INTERFACE_FORCE_SPEED2_MASK,
> +                                 r_speed >>
> +                                 RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_WIDTH);

Here is where FIELD_GET() with a HI/LO mask avoids the manual shift
and makes the WIDTH definition unnecessary.

Best regards,

Luiz
Re: [PATCH net-next v6 3/9] net: dsa: realtek: rtl8365mb: set speed for family D
#4676
Author: Andrew Lunn
Date: Sun, 18 Oct 2026 23:04
43 lines
7782 bytes
On Sun, Oct 18, 2026 at 07:40:47PM -0300, Luiz Angelo Daros de Luca wrote:
> > diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
> > index 840db5a..2dabc4d 100644
> > --- a/drivers/net/dsa/realtek/rtl8365mb_main.c
> > +++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
> > @@ -344,6 +344,7 @@
> >  #define RTL8365MB_PORT_SPEED_10M       0
> >  #define RTL8365MB_PORT_SPEED_100M      1
> >  #define RTL8365MB_PORT_SPEED_1000M     2
> > +#define RTL8365MB_D_PORT_SPEED_2500M   5
> 
> I also used the same _<FAMILY>_ tag trick previously, but it gets
> confusing quickly. Does RTL8365MB_D_xxx mean something exclusive to
> family D, or family D and later? I hope a more seasoned reviewer can
> weigh in on this.

The Marvell switch driver does something similar. There are a few
families of devices, and within each family, a few devices. Within a
family, the devices are register compatible, but they either have
different number of ports, or features are not present.

The naming of functions and defines is based around the highest family
member of the family which introduced the feature. A few examples:

int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
                               u8 out);
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
                               u8 out);

The feature was first introduced in mv88e6097, and this method is used
in all families until the mv88e6390 came along, which required a
different implementation because the registers changed.

So i would say RTL8365MB_D_xxx means D and later. And if you need
RTL8365MB_F_xxx, put it close by so other developers are aware it
exists.

What we also sometimes do is have a mv88e6xxx_foo function, which is
generic, but it calls mv88e6352_foo, mv88e6189_foo, mv88e6393_foo as
needed.

	Andrew
Re: [PATCH net-next v6 3/9] net: dsa: realtek: rtl8365mb: set speed for family D
#4782
Author: Linus Walleij
Date: Mon, 19 Oct 2026 08:28
64 lines
8628 bytes
On Mon, Oct 19, 2026 at 12:41 AM Luiz Angelo Daros de Luca
<luizluca@gmail.com> wrote:

> >  #define RTL8365MB_PORT_SPEED_10M       0
> >  #define RTL8365MB_PORT_SPEED_100M      1
> >  #define RTL8365MB_PORT_SPEED_1000M     2
> > +#define RTL8365MB_D_PORT_SPEED_2500M   5
>
> I also used the same _<FAMILY>_ tag trick previously, but it gets
> confusing quickly. Does RTL8365MB_D_xxx mean something exclusive to
> family D, or family D and later? I hope a more seasoned reviewer can
> weigh in on this.

I'm not seasoned.

But in the Microchip switches there are a ton of different
devices and it is managed by way of family-specific
files, includes etc. But we bake everything into one single
module in the end, the only optional part is PTP.

Here the family is somewhat and piecemal encoded in the
hardware registers so:

#define REG_CHIP_ID0                    0x00

#define SW_FAMILY_ID_M                  GENMASK(15, 8)
#define KSZ84_FAMILY_ID                 0x84
#define KSZ87_FAMILY_ID                 0x87
#define KSZ88_FAMILY_ID                 0x88
#define KSZ8895_FAMILY_ID               0x95

#define SW_CHIP_ID_M                    GENMASK(7, 4)
#define KSZ87_CHIP_ID_94                0x6
#define KSZ87_CHIP_ID_95                0x9
#define KSZ88_CHIP_ID_63                0x3
#define KSZ8895_CHIP_ID_95XA            0x0
#define KSZ8895_CHIP_ID_95              0x4
#define KSZ8895_CHIP_ID_95R             0x6

it all comes pretty naturally from this.

Then we go on to define boolean family deduction helpers:

static inline bool ksz_is_ksz87xx(struct ksz_device *dev)
{
        return dev->chip_id == KSZ8795_CHIP_ID ||
               dev->chip_id == KSZ8794_CHIP_ID ||
               dev->chip_id == KSZ8765_CHIP_ID;
}

static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
{
        return dev->chip_id == KSZ88X3_CHIP_ID;
}

This makes it pretty straight-forward to code it up.

The same can be achived even if you don't have any hardware
ID registersm with made-up family enums and using DT match
data obviously.

Yours,
Linus Walleij
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads