ar8216: add swconfig attributes for ARL table flushing
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / ar8216.c
index 8266003c172d953dc2101836ea83ff1a97a957db..75db1f120b6e3ee52cb45d054a3eca48cb8cd900 100644 (file)
@@ -205,7 +205,7 @@ ar8xxx_phy_init(struct ar8xxx_priv *priv)
 }
 
 u32
-mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
+ar8xxx_mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
 {
        struct mii_bus *bus = priv->mii_bus;
        u16 lo, hi;
@@ -217,7 +217,7 @@ mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
 }
 
 void
-mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val)
+ar8xxx_mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val)
 {
        struct mii_bus *bus = priv->mii_bus;
        u16 lo, hi;
@@ -248,7 +248,7 @@ ar8xxx_read(struct ar8xxx_priv *priv, int reg)
 
        bus->write(bus, 0x18, 0, page);
        wait_for_page_switch();
-       val = mii_read32(priv, 0x10 | r2, r1);
+       val = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
 
        mutex_unlock(&bus->mdio_lock);
 
@@ -267,7 +267,7 @@ ar8xxx_write(struct ar8xxx_priv *priv, int reg, u32 val)
 
        bus->write(bus, 0x18, 0, page);
        wait_for_page_switch();
-       mii_write32(priv, 0x10 | r2, r1, val);
+       ar8xxx_mii_write32(priv, 0x10 | r2, r1, val);
 
        mutex_unlock(&bus->mdio_lock);
 }
@@ -286,10 +286,10 @@ ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
        bus->write(bus, 0x18, 0, page);
        wait_for_page_switch();
 
-       ret = mii_read32(priv, 0x10 | r2, r1);
+       ret = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
        ret &= ~mask;
        ret |= val;
-       mii_write32(priv, 0x10 | r2, r1, ret);
+       ar8xxx_mii_write32(priv, 0x10 | r2, r1, ret);
 
        mutex_unlock(&bus->mdio_lock);
 
@@ -598,10 +598,26 @@ ar8216_atu_flush(struct ar8xxx_priv *priv)
 {
        int ret;
 
-       ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
+       ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
        if (!ret)
-               ar8xxx_write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH |
-                                                  AR8216_ATU_ACTIVE);
+               ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_OP_FLUSH |
+                                                        AR8216_ATU_ACTIVE);
+
+       return ret;
+}
+
+static int
+ar8216_atu_flush_port(struct ar8xxx_priv *priv, int port)
+{
+       u32 t;
+       int ret;
+
+       ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
+       if (!ret) {
+               t = (port << AR8216_ATU_PORT_NUM_S) | AR8216_ATU_OP_FLUSH_PORT;
+               t |= AR8216_ATU_ACTIVE;
+               ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, t);
+       }
 
        return ret;
 }
@@ -701,6 +717,77 @@ ar8216_init_port(struct ar8xxx_priv *priv, int port)
        }
 }
 
+static void
+ar8216_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
+{
+       int timeout = 20;
+
+       while (ar8xxx_mii_read32(priv, r2, r1) & AR8216_ATU_ACTIVE && --timeout)
+                udelay(10);
+
+       if (!timeout)
+               pr_err("ar8216: timeout waiting for atu to become ready\n");
+}
+
+static void ar8216_get_arl_entry(struct ar8xxx_priv *priv,
+                                struct arl_entry *a, u32 *status, enum arl_op op)
+{
+       struct mii_bus *bus = priv->mii_bus;
+       u16 r2, page;
+       u16 r1_func0, r1_func1, r1_func2;
+       u32 t, val0, val1, val2;
+       int i;
+
+       split_addr(AR8216_REG_ATU_FUNC0, &r1_func0, &r2, &page);
+       r2 |= 0x10;
+
+       r1_func1 = (AR8216_REG_ATU_FUNC1 >> 1) & 0x1e;
+       r1_func2 = (AR8216_REG_ATU_FUNC2 >> 1) & 0x1e;
+
+       switch (op) {
+       case AR8XXX_ARL_INITIALIZE:
+               /* all ATU registers are on the same page
+               * therefore set page only once
+               */
+               bus->write(bus, 0x18, 0, page);
+               wait_for_page_switch();
+
+               ar8216_wait_atu_ready(priv, r2, r1_func0);
+
+               ar8xxx_mii_write32(priv, r2, r1_func0, AR8216_ATU_OP_GET_NEXT);
+               ar8xxx_mii_write32(priv, r2, r1_func1, 0);
+               ar8xxx_mii_write32(priv, r2, r1_func2, 0);
+               break;
+       case AR8XXX_ARL_GET_NEXT:
+               t = ar8xxx_mii_read32(priv, r2, r1_func0);
+               t |= AR8216_ATU_ACTIVE;
+               ar8xxx_mii_write32(priv, r2, r1_func0, t);
+               ar8216_wait_atu_ready(priv, r2, r1_func0);
+
+               val0 = ar8xxx_mii_read32(priv, r2, r1_func0);
+               val1 = ar8xxx_mii_read32(priv, r2, r1_func1);
+               val2 = ar8xxx_mii_read32(priv, r2, r1_func2);
+
+               *status = (val2 & AR8216_ATU_STATUS) >> AR8216_ATU_STATUS_S;
+               if (!*status)
+                       break;
+
+               i = 0;
+               t = AR8216_ATU_PORT0;
+               while (!(val2 & t) && ++i < priv->dev.ports)
+                       t <<= 1;
+
+               a->port = i;
+               a->mac[0] = (val0 & AR8216_ATU_ADDR5) >> AR8216_ATU_ADDR5_S;
+               a->mac[1] = (val0 & AR8216_ATU_ADDR4) >> AR8216_ATU_ADDR4_S;
+               a->mac[2] = (val1 & AR8216_ATU_ADDR3) >> AR8216_ATU_ADDR3_S;
+               a->mac[3] = (val1 & AR8216_ATU_ADDR2) >> AR8216_ATU_ADDR2_S;
+               a->mac[4] = (val1 & AR8216_ATU_ADDR1) >> AR8216_ATU_ADDR1_S;
+               a->mac[5] = (val1 & AR8216_ATU_ADDR0) >> AR8216_ATU_ADDR0_S;
+               break;
+       }
+}
+
 static void
 ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
 {
@@ -1349,6 +1436,39 @@ ar8xxx_sw_get_arl_table(struct switch_dev *dev,
        return 0;
 }
 
+int
+ar8xxx_sw_set_flush_arl_table(struct switch_dev *dev,
+                             const struct switch_attr *attr,
+                             struct switch_val *val)
+{
+       struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
+       int ret;
+
+       mutex_lock(&priv->reg_mutex);
+       ret = priv->chip->atu_flush(priv);
+       mutex_unlock(&priv->reg_mutex);
+
+       return ret;
+}
+
+int
+ar8xxx_sw_set_flush_port_arl_table(struct switch_dev *dev,
+                                  const struct switch_attr *attr,
+                                  struct switch_val *val)
+{
+       struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
+       int port, ret;
+
+       port = val->port_vlan;
+       if (port >= dev->ports)
+               return -EINVAL;
+
+       mutex_lock(&priv->reg_mutex);
+       ret = priv->chip->atu_flush_port(priv, port);
+       mutex_unlock(&priv->reg_mutex);
+
+       return ret;
+}
 
 static const struct switch_attr ar8xxx_sw_attr_globals[] = {
        {
@@ -1404,9 +1524,15 @@ static const struct switch_attr ar8xxx_sw_attr_globals[] = {
                .set = NULL,
                .get = ar8xxx_sw_get_arl_table,
        },
+       {
+               .type = SWITCH_TYPE_NOVAL,
+               .name = "flush_arl_table",
+               .description = "Flush ARL table",
+               .set = ar8xxx_sw_set_flush_arl_table,
+       },
 };
 
-const struct switch_attr ar8xxx_sw_attr_port[2] = {
+const struct switch_attr ar8xxx_sw_attr_port[] = {
        {
                .type = SWITCH_TYPE_NOVAL,
                .name = "reset_mib",
@@ -1420,6 +1546,12 @@ const struct switch_attr ar8xxx_sw_attr_port[2] = {
                .set = NULL,
                .get = ar8xxx_sw_get_port_mib,
        },
+       {
+               .type = SWITCH_TYPE_NOVAL,
+               .name = "flush_arl_table",
+               .description = "Flush port's ARL table entries",
+               .set = ar8xxx_sw_set_flush_port_arl_table,
+       },
 };
 
 const struct switch_attr ar8xxx_sw_attr_vlan[1] = {
@@ -1472,9 +1604,11 @@ static const struct ar8xxx_chip ar8216_chip = {
        .setup_port = ar8216_setup_port,
        .read_port_status = ar8216_read_port_status,
        .atu_flush = ar8216_atu_flush,
+       .atu_flush_port = ar8216_atu_flush_port,
        .vtu_flush = ar8216_vtu_flush,
        .vtu_load_vlan = ar8216_vtu_load_vlan,
        .set_mirror_regs = ar8216_set_mirror_regs,
+       .get_arl_entry = ar8216_get_arl_entry,
        .sw_hw_apply = ar8xxx_sw_hw_apply,
 
        .num_mibs = ARRAY_SIZE(ar8216_mibs),
@@ -1499,9 +1633,11 @@ static const struct ar8xxx_chip ar8236_chip = {
        .setup_port = ar8236_setup_port,
        .read_port_status = ar8216_read_port_status,
        .atu_flush = ar8216_atu_flush,
+       .atu_flush_port = ar8216_atu_flush_port,
        .vtu_flush = ar8216_vtu_flush,
        .vtu_load_vlan = ar8216_vtu_load_vlan,
        .set_mirror_regs = ar8216_set_mirror_regs,
+       .get_arl_entry = ar8216_get_arl_entry,
        .sw_hw_apply = ar8xxx_sw_hw_apply,
 
        .num_mibs = ARRAY_SIZE(ar8236_mibs),
@@ -1526,9 +1662,11 @@ static const struct ar8xxx_chip ar8316_chip = {
        .setup_port = ar8216_setup_port,
        .read_port_status = ar8216_read_port_status,
        .atu_flush = ar8216_atu_flush,
+       .atu_flush_port = ar8216_atu_flush_port,
        .vtu_flush = ar8216_vtu_flush,
        .vtu_load_vlan = ar8216_vtu_load_vlan,
        .set_mirror_regs = ar8216_set_mirror_regs,
+       .get_arl_entry = ar8216_get_arl_entry,
        .sw_hw_apply = ar8xxx_sw_hw_apply,
 
        .num_mibs = ARRAY_SIZE(ar8236_mibs),
@@ -1790,13 +1928,13 @@ ar8xxx_check_link_states(struct ar8xxx_priv *priv)
 
                priv->link_up[i] = link_new;
                changed = true;
+               /* flush ARL entries for this port if it went down*/
+               if (!link_new)
+                       priv->chip->atu_flush_port(priv, i);
                dev_info(&priv->phy->dev, "Port %d is %s\n",
                         i, link_new ? "up" : "down");
        }
 
-       if (changed)
-               priv->chip->atu_flush(priv);
-
        mutex_unlock(&priv->reg_mutex);
 
        return changed;
@@ -1808,9 +1946,7 @@ ar8xxx_phy_read_status(struct phy_device *phydev)
        struct ar8xxx_priv *priv = phydev->priv;
        struct switch_port_link link;
 
-       /* check for link changes and flush ATU
-        * if a change was detected
-        */
+       /* check for switch port link changes */
        if (phydev->state == PHY_CHANGELINK)
                ar8xxx_check_link_states(priv);