ksz8795 VLAN fixes
cover ksz8795 VLAN fixes
This series fixes a number of bugs in the ksz8795 driver that affect VLAN filtering, tag insertion, and tag removal.
I've tested these on the KSZ8795CLXD evaluation board, and checked the register usage against the datasheets for the other supported chips.
Ben.
Ben Hutchings (7):
net: dsa: microchip: Fix ksz_read64()
net: dsa: microchip: ksz8795: Fix PVID tag insertion
net: dsa: microchip: ksz8795: Reject unsupported VLAN configuration
net: dsa: microchip: ksz8795: Fix VLAN untagged flag change on
deletion
net: dsa: microchip: ksz8795: Use software untagging on CPU port
net: dsa: microchip: ksz8795: Fix VLAN filtering
net: dsa: microchip: ksz8795: Don't use phy_port_cnt in VLAN table
lookup
drivers/net/dsa/microchip/ksz8795.c | 82 ++++++++++++++++++++----- drivers/net/dsa/microchip/ksz8795_reg.h | 4 ++ drivers/net/dsa/microchip/ksz_common.h | 9 +-- 3 files changed, 74 insertions(+), 21 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/7 net: dsa: microchip: Fix ksz_read64()
ksz_read64() currently does some dubious byte-swapping on the two halves of a 64-bit register, and then only returns the high bits. Replace this with a straightforward expression.
--- drivers/net/dsa/microchip/ksz_common.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/dsa/microchip/ksz_common.h +2 -6
@@ -205,12 +205,8 @@ static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)int ret; ret =regmap_bulk_read (dev->regmap[2 ], reg, value,2 ); -if (!ret) { -/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */ - value[0 ] =swab32 (value[0 ]); - value[1 ] =swab32 (value[1 ]); - *val =swab64 ((u64)*value); - } +if (!ret) + *val = (u64 )value[0 ] <<32 | value[1 ];return ret; }
Signature · 1 line
2.20.1
PATCH 2/7 net: dsa: microchip: ksz8795: Fix PVID tag insertion
In reply to Ben Hutchingsksz8795 has never actually enabled PVID tag insertion, and it also programmed the PVID incorrectly. To fix this:
* Allow tag insertion to be controlled per ingress port. On most chips, set bit 2 in Global Control 19. On KSZ88x3 this control flag doesn't exist.
* When adding a PVID:
- Set the appropriate register bits to enable tag insertion on egress at every other port if this was the packet's ingress port.
- Mask *out* the VID from the default tag, before or-ing in the new PVID.
* When removing a PVID:
- Clear the same control bits to disable tag insertion.
- Don't update the default tag. This wasn't doing anything useful.
--- drivers/net/dsa/microchip/ksz8795.c | 26 ++++++++++++++++++------- drivers/net/dsa/microchip/ksz8795_reg.h | 4 ++++ 2 files changed, 23 insertions(+), 7 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +19 -7
@@ -1124,6 +1124,16 @@ static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag,return 0 ; } +static void ksz8_port_enable_pvid (struct ksz_device *dev,int port,bool state) +{ +if (ksz_is_ksz88x3 (dev)) { +ksz_cfg (dev,REG_SW_INSERT_SRC_PVID , +0x03 << (4 -2 * port), state); + }else { +ksz_pwrite8 (dev, port,REG_PORT_CTRL_12 , state ?0x0f :0x00 ); + } +} +static int ksz8_port_vlan_add (struct dsa_switch *ds,int port,const struct switchdev_obj_port_vlan *vlan,struct netlink_ext_ack *extack)
@@ -1160,9 +1170,11 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,u16 vid;ksz_pread16 (dev, port,REG_PORT_CTRL_VID , &vid); - vid &=0xfff ; + vid &= ~VLAN_VID_MASK ; vid |= new_pvid;ksz_pwrite16 (dev, port,REG_PORT_CTRL_VID , vid); + +ksz8_port_enable_pvid (dev, port, true); }return 0 ;
@@ -1173,7 +1185,7 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, {bool untagged = vlan->flags &BRIDGE_VLAN_INFO_UNTAGGED ;struct ksz_device *dev = ds->priv; -u16 data, pvid, new_pvid =0 ; +u16 data, pvid;u8 fid, member, valid;if (ksz_is_ksz88x3 (dev))
@@ -1195,14 +1207,11 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, valid =0 ; } -if (pvid == vlan->vid) - new_pvid =1 ; -ksz8_to_vlan (dev, fid, member, valid, &data);ksz8_w_vlan_table (dev, vlan->vid, data); -if (new_pvid != pvid) -ksz_pwrite16 (dev, port,REG_PORT_CTRL_VID , pvid); +if (pvid == vlan->vid) +ksz8_port_enable_pvid (dev, port, false);return 0 ; }
@@ -1438,6 +1447,9 @@ static int ksz8_setup(struct dsa_switch *ds)ksz_cfg (dev,S_MIRROR_CTRL ,SW_MIRROR_RX_TX , false); +if (!ksz_is_ksz88x3 (dev)) +ksz_cfg (dev,REG_SW_CTRL_19 ,SW_INS_TAG_ENABLE , true); +/* set broadcast storm protection 10% rate */ regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, BROADCAST_STORM_RATE,
drivers/net/dsa/microchip/ksz8795_reg.h +4 -0
@@ -631,6 +631,10 @@#define REG_PORT_4_OUT_RATE_3 0xEE#define REG_PORT_5_OUT_RATE_3 0xFE +/* 88x3 specific */ + +#define REG_SW_INSERT_SRC_PVID 0xC2 +/* PME */ #define SW_PME_OUTPUT_ENABLE BIT(1)
Signature · 1 line
2.20.1
PATCH 3/7 net: dsa: microchip: ksz8795: Reject unsupported VLAN configuration
In reply to Ben HutchingsThe switches supported by ksz8795 only have a per-port flag for Tag Removal. This means it is not possible to support both tagged and untagged VLANs on the same port. Reject attempts to add a VLAN that requires the flag to be changed, unless there are no VLANs currently configured.
VID 0 is excluded from this check since it is untagged regardless of the state of the flag.
On the CPU port we could support tagged and untagged VLANs at the same time. This will be enabled by a later patch.
--- drivers/net/dsa/microchip/ksz8795.c | 27 +++++++++++++++++++++++++- drivers/net/dsa/microchip/ksz_common.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-)
drivers/net/dsa/microchip/ksz8795.c +26 -1
@@ -1140,13 +1140,38 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, {bool untagged = vlan->flags &BRIDGE_VLAN_INFO_UNTAGGED ;struct ksz_device *dev = ds->priv; +struct ksz_port *p = &dev->ports[port];u16 data, new_pvid =0 ;u8 fid, member, valid;if (ksz_is_ksz88x3 (dev))return -ENOTSUPP ; -ksz_port_cfg (dev, port,P_TAG_CTRL ,PORT_REMOVE_TAG , untagged); +/* If a VLAN is added with untagged flag different from the +* port's Remove Tag flag, we need to change the latter. +* Ignore VID 0, which is always untagged. +*/ +if (untagged != p->remove_tag && vlan->vid !=0 ) { +unsigned int vid; + +/* Reject attempts to add a VLAN that requires the +* Remove Tag flag to be changed, unless there are no +* other VLANs currently configured. +*/ +for (vid =1 ; vid < dev->num_vlans; ++vid) { +/* Skip the VID we are going to add or reconfigure */ +if (vid == vlan->vid) +continue ; + +ksz8_from_vlan (dev, dev->vlan_cache[vid].table[0 ], + &fid, &member, &valid); +if (valid && (member &BIT (port))) +return -EINVAL ; + } + +ksz_port_cfg (dev, port,P_TAG_CTRL ,PORT_REMOVE_TAG , untagged); + p->remove_tag = untagged; + }ksz8_r_vlan_table (dev, vlan->vid, &data);ksz8_from_vlan (dev, data, &fid, &member, &valid);
drivers/net/dsa/microchip/ksz_common.h +1 -0
@@ -27,6 +27,7 @@ struct ksz_port_mib {struct ksz_port {u16 member;u16 vid_member; +bool remove_tag;/* Remove Tag flag set, for ksz8795 only */ int stp_state;struct phy_device phydev;
Signature · 1 line
2.20.1
PATCH 4/7 net: dsa: microchip: ksz8795: Fix VLAN untagged flag change on deletion
In reply to Ben HutchingsWhen a VLAN is deleted from a port, the flags in struct switchdev_obj_port_vlan are always 0. ksz8_port_vlan_del() copies the BRIDGE_VLAN_INFO_UNTAGGED flag to the port's Tag Removal flag, and therefore always clears it.
In case there are multiple VLANs configured as untagged on this port - which seems useless, but is allowed - deleting one of them changes the remaining VLANs to be tagged.
It's only ever necessary to change this flag when a VLAN is added to the port, so leave it unchanged in ksz8_port_vlan_del().
--- drivers/net/dsa/microchip/ksz8795.c | 3 --- 1 file changed, 3 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +0 -3
@@ -1208,7 +1208,6 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,static int ksz8_port_vlan_del (struct dsa_switch *ds,int port,const struct switchdev_obj_port_vlan *vlan) { -bool untagged = vlan->flags &BRIDGE_VLAN_INFO_UNTAGGED ;struct ksz_device *dev = ds->priv;u16 data, pvid;u8 fid, member, valid;
@@ -1219,8 +1218,6 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port,ksz_pread16 (dev, port,REG_PORT_CTRL_VID , &pvid); pvid = pvid &0xFFF ; -ksz_port_cfg (dev, port,P_TAG_CTRL ,PORT_REMOVE_TAG , untagged); -ksz8_r_vlan_table (dev, vlan->vid, &data);ksz8_from_vlan (dev, data, &fid, &member, &valid);
Signature · 1 line
2.20.1
PATCH 5/7 net: dsa: microchip: ksz8795: Use software untagging on CPU port
In reply to Ben HutchingsOn the CPU port, we can support both tagged and untagged VLANs at the same time by doing any necessary untagging in software rather than hardware. To enable that, keep the CPU port's Remove Tag flag cleared and set the dsa_switch::untag_bridge_pvid flag.
--- drivers/net/dsa/microchip/ksz8795.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/net/dsa/microchip/ksz8795.c +8 -1
@@ -1150,8 +1150,10 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,/* If a VLAN is added with untagged flag different from the * port's Remove Tag flag, we need to change the latter. * Ignore VID 0, which is always untagged. +* Ignore CPU port, which will always be tagged. */ -if (untagged != p->remove_tag && vlan->vid !=0 ) { +if (untagged != p->remove_tag && vlan->vid !=0 && + port != dev->cpu_port) {unsigned int vid;/* Reject attempts to add a VLAN that requires the
@@ -1767,6 +1769,11 @@ static int ksz8_switch_init(struct ksz_device *dev)/* set the real number of ports */ dev->ds->num_ports = dev->port_cnt; +/* We rely on software untagging on the CPU port, so that we +* can support both tagged and untagged VLANs +*/ + dev->ds->untag_bridge_pvid = true; +return 0 ; }
Signature · 1 line
2.20.1
PATCH 6/7 net: dsa: microchip: ksz8795: Fix VLAN filtering
In reply to Ben HutchingsCurrently ksz8_port_vlan_filtering() sets or clears the VLAN Enable hardware flag. That controls discarding of packets with a VID that has not been enabled for any port on the switch.
Since it is a global flag, set the dsa_switch::vlan_filtering_is_global flag so that the DSA core understands this can't be controlled per port.
When VLAN filtering is enabled, the switch should also discard packets with a VID that's not enabled on the ingress port. Set or clear each external port's VLAN Ingress Filter flag in ksz8_port_vlan_filtering() to make that happen.
--- drivers/net/dsa/microchip/ksz8795.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/dsa/microchip/ksz8795.c +11 -0
@@ -1119,8 +1119,14 @@ static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag,if (ksz_is_ksz88x3 (dev))return -ENOTSUPP ; +/* Discard packets with VID not enabled on the switch */ ksz_cfg (dev,S_MIRROR_CTRL ,SW_VLAN_ENABLE , flag); +/* Discard packets with VID not enabled on the ingress port */ +for (port =0 ; port < dev->phy_port_cnt; ++port) +ksz_port_cfg (dev, port,REG_PORT_CTRL_2 ,PORT_INGRESS_FILTER , + flag); +return 0 ; }
@@ -1774,6 +1780,11 @@ static int ksz8_switch_init(struct ksz_device *dev)*/ dev->ds->untag_bridge_pvid = true; +/* VLAN filtering is partly controlled by the global VLAN +* Enable flag +*/ + dev->ds->vlan_filtering_is_global = true; +return 0 ; }
Signature · 1 line
2.20.1
PATCH 7/7 net: dsa: microchip: ksz8795: Don't use phy_port_cnt in VLAN table lookup
In reply to Ben HutchingsThe magic number 4 in VLAN table lookup was the number of entries we can read and write at once. Using phy_port_cnt here doesn't make sense and presumably broke VLAN filtering for 3-port switches. Change it back to 4.
--- drivers/net/dsa/microchip/ksz8795.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +4 -4
@@ -687,8 +687,8 @@ static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) shifts = ksz8->shifts;ksz8_r_table (dev,TABLE_VLAN , addr, &data); - addr *= dev->phy_port_cnt; -for (i =0 ; i < dev->phy_port_cnt; i++) { + addr *=4 ; +for (i =0 ; i <4 ; i++) { dev->vlan_cache[addr + i].table[0 ] = (u16 )data; data >>= shifts[VLAN_TABLE ]; }
@@ -702,7 +702,7 @@ static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)u64 buf; data = (u16 *)&buf; - addr = vid / dev->phy_port_cnt; + addr = vid /4 ; index = vid &3 ;ksz8_r_table (dev,TABLE_VLAN , addr, &buf); *vlan = data[index];
@@ -716,7 +716,7 @@ static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)u64 buf; data = (u16 *)&buf; - addr = vid / dev->phy_port_cnt; + addr = vid /4 ; index = vid &3 ;ksz8_r_table (dev,TABLE_VLAN , addr, &buf); data[index] = vlan;
Signature · 1 line
2.20.1
Jakub Kicinski
maintainerQuoted 5 lines · On Tue, 10 Aug 2021 00:57:54 +0200 Ben Hutchings wrote:
This series fixes a number of bugs in the ksz8795 driver that affect VLAN filtering, tag insertion, and tag removal.
I've tested these on the KSZ8795CLXD evaluation board, and checked the register usage against the datasheets for the other supported chips.
Let's remember to CC authors of patches under Fixes, adding them now.
This series was applied to netdev/net.git (refs/heads/master):
Hello:
This series was applied to netdev/net.git (refs/heads/master):
Quoted 9 lines · On Tue, 10 Aug 2021 00:57:54 +0200 you wrote:
This series fixes a number of bugs in the ksz8795 driver that affect VLAN filtering, tag insertion, and tag removal.
I've tested these on the KSZ8795CLXD evaluation board, and checked the register usage against the datasheets for the other supported chips.
Ben.
[...]
Here is the summary with links:
- [net,1/7] net: dsa: microchip: Fix ksz_read64()
https://git.kernel.org/netdev/net/c/c34f674c8875 - [net,2/7] net: dsa: microchip: ksz8795: Fix PVID tag insertion https://git.kernel.org/netdev/net/c/ef3b02a1d79b - [net,3/7] net: dsa: microchip: ksz8795: Reject unsupported VLAN configuration https://git.kernel.org/netdev/net/c/8f4f58f88fe0 - [net,4/7] net: dsa: microchip: ksz8795: Fix VLAN untagged flag change on deletion https://git.kernel.org/netdev/net/c/af01754f9e3c - [net,5/7] net: dsa: microchip: ksz8795: Use software untagging on CPU port https://git.kernel.org/netdev/net/c/9130c2d30c17 - [net,6/7] net: dsa: microchip: ksz8795: Fix VLAN filtering https://git.kernel.org/netdev/net/c/164844135a3f - [net,7/7] net: dsa: microchip: ksz8795: Don't use phy_port_cnt in VLAN table lookup https://git.kernel.org/netdev/net/c/411d466d94a6
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html