Commit 37c86c4a authored by David S. Miller's avatar David S. Miller

Merge branch 'ks8795-vlan-fixes'

Ben Hutchings says:

====================
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.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 31782a01 411d466d
...@@ -687,8 +687,8 @@ static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) ...@@ -687,8 +687,8 @@ static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
shifts = ksz8->shifts; shifts = ksz8->shifts;
ksz8_r_table(dev, TABLE_VLAN, addr, &data); ksz8_r_table(dev, TABLE_VLAN, addr, &data);
addr *= dev->phy_port_cnt; addr *= 4;
for (i = 0; i < dev->phy_port_cnt; i++) { for (i = 0; i < 4; i++) {
dev->vlan_cache[addr + i].table[0] = (u16)data; dev->vlan_cache[addr + i].table[0] = (u16)data;
data >>= shifts[VLAN_TABLE]; data >>= shifts[VLAN_TABLE];
} }
...@@ -702,7 +702,7 @@ static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) ...@@ -702,7 +702,7 @@ static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
u64 buf; u64 buf;
data = (u16 *)&buf; data = (u16 *)&buf;
addr = vid / dev->phy_port_cnt; addr = vid / 4;
index = vid & 3; index = vid & 3;
ksz8_r_table(dev, TABLE_VLAN, addr, &buf); ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
*vlan = data[index]; *vlan = data[index];
...@@ -716,7 +716,7 @@ static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) ...@@ -716,7 +716,7 @@ static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
u64 buf; u64 buf;
data = (u16 *)&buf; data = (u16 *)&buf;
addr = vid / dev->phy_port_cnt; addr = vid / 4;
index = vid & 3; index = vid & 3;
ksz8_r_table(dev, TABLE_VLAN, addr, &buf); ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
data[index] = vlan; data[index] = vlan;
...@@ -1119,24 +1119,67 @@ static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, ...@@ -1119,24 +1119,67 @@ static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag,
if (ksz_is_ksz88x3(dev)) if (ksz_is_ksz88x3(dev))
return -ENOTSUPP; return -ENOTSUPP;
/* Discard packets with VID not enabled on the switch */
ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 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; 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, static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,
const struct switchdev_obj_port_vlan *vlan, const struct switchdev_obj_port_vlan *vlan,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
struct ksz_device *dev = ds->priv; struct ksz_device *dev = ds->priv;
struct ksz_port *p = &dev->ports[port];
u16 data, new_pvid = 0; u16 data, new_pvid = 0;
u8 fid, member, valid; u8 fid, member, valid;
if (ksz_is_ksz88x3(dev)) if (ksz_is_ksz88x3(dev))
return -ENOTSUPP; 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.
* Ignore CPU port, which will always be tagged.
*/
if (untagged != p->remove_tag && vlan->vid != 0 &&
port != dev->cpu_port) {
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_r_vlan_table(dev, vlan->vid, &data);
ksz8_from_vlan(dev, data, &fid, &member, &valid); ksz8_from_vlan(dev, data, &fid, &member, &valid);
...@@ -1160,9 +1203,11 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, ...@@ -1160,9 +1203,11 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,
u16 vid; u16 vid;
ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
vid &= 0xfff; vid &= ~VLAN_VID_MASK;
vid |= new_pvid; vid |= new_pvid;
ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
ksz8_port_enable_pvid(dev, port, true);
} }
return 0; return 0;
...@@ -1171,9 +1216,8 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, ...@@ -1171,9 +1216,8 @@ static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,
static int ksz8_port_vlan_del(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) const struct switchdev_obj_port_vlan *vlan)
{ {
bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
struct ksz_device *dev = ds->priv; struct ksz_device *dev = ds->priv;
u16 data, pvid, new_pvid = 0; u16 data, pvid;
u8 fid, member, valid; u8 fid, member, valid;
if (ksz_is_ksz88x3(dev)) if (ksz_is_ksz88x3(dev))
...@@ -1182,8 +1226,6 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, ...@@ -1182,8 +1226,6 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port,
ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
pvid = pvid & 0xFFF; pvid = pvid & 0xFFF;
ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
ksz8_r_vlan_table(dev, vlan->vid, &data); ksz8_r_vlan_table(dev, vlan->vid, &data);
ksz8_from_vlan(dev, data, &fid, &member, &valid); ksz8_from_vlan(dev, data, &fid, &member, &valid);
...@@ -1195,14 +1237,11 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, ...@@ -1195,14 +1237,11 @@ static int ksz8_port_vlan_del(struct dsa_switch *ds, int port,
valid = 0; valid = 0;
} }
if (pvid == vlan->vid)
new_pvid = 1;
ksz8_to_vlan(dev, fid, member, valid, &data); ksz8_to_vlan(dev, fid, member, valid, &data);
ksz8_w_vlan_table(dev, vlan->vid, data); ksz8_w_vlan_table(dev, vlan->vid, data);
if (new_pvid != pvid) if (pvid == vlan->vid)
ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid); ksz8_port_enable_pvid(dev, port, false);
return 0; return 0;
} }
...@@ -1435,6 +1474,9 @@ static int ksz8_setup(struct dsa_switch *ds) ...@@ -1435,6 +1474,9 @@ static int ksz8_setup(struct dsa_switch *ds)
ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 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 */ /* set broadcast storm protection 10% rate */
regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL,
BROADCAST_STORM_RATE, BROADCAST_STORM_RATE,
...@@ -1717,6 +1759,16 @@ static int ksz8_switch_init(struct ksz_device *dev) ...@@ -1717,6 +1759,16 @@ static int ksz8_switch_init(struct ksz_device *dev)
/* set the real number of ports */ /* set the real number of ports */
dev->ds->num_ports = dev->port_cnt; 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;
/* VLAN filtering is partly controlled by the global VLAN
* Enable flag
*/
dev->ds->vlan_filtering_is_global = true;
return 0; return 0;
} }
......
...@@ -631,6 +631,10 @@ ...@@ -631,6 +631,10 @@
#define REG_PORT_4_OUT_RATE_3 0xEE #define REG_PORT_4_OUT_RATE_3 0xEE
#define REG_PORT_5_OUT_RATE_3 0xFE #define REG_PORT_5_OUT_RATE_3 0xFE
/* 88x3 specific */
#define REG_SW_INSERT_SRC_PVID 0xC2
/* PME */ /* PME */
#define SW_PME_OUTPUT_ENABLE BIT(1) #define SW_PME_OUTPUT_ENABLE BIT(1)
......
...@@ -27,6 +27,7 @@ struct ksz_port_mib { ...@@ -27,6 +27,7 @@ struct ksz_port_mib {
struct ksz_port { struct ksz_port {
u16 member; u16 member;
u16 vid_member; u16 vid_member;
bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
int stp_state; int stp_state;
struct phy_device phydev; struct phy_device phydev;
...@@ -205,12 +206,8 @@ static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val) ...@@ -205,12 +206,8 @@ static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
int ret; int ret;
ret = regmap_bulk_read(dev->regmap[2], reg, value, 2); ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
if (!ret) { if (!ret)
/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */ *val = (u64)value[0] << 32 | value[1];
value[0] = swab32(value[0]);
value[1] = swab32(value[1]);
*val = swab64((u64)*value);
}
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment