net: dsa: sja1105: Use correct dsa_8021q VIDs for FDB commands
[linux-2.6-microblaze.git] / drivers / net / dsa / sja1105 / sja1105_main.c
index 9395e8f..cadee76 100644 (file)
@@ -80,7 +80,7 @@ static int sja1105_init_mac_settings(struct sja1105_private *priv)
                .maxage = 0xFF,
                /* Internal VLAN (pvid) to apply to untagged ingress */
                .vlanprio = 0,
-               .vlanid = 0,
+               .vlanid = 1,
                .ing_mirr = false,
                .egr_mirr = false,
                /* Don't drop traffic with other EtherType than ETH_P_IP */
@@ -203,6 +203,7 @@ static int sja1105_init_static_fdb(struct sja1105_private *priv)
 static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
 {
        struct sja1105_table *table;
+       u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
        struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
                /* Learned FDB entries are forgotten after 300 seconds */
                .maxage = SJA1105_AGEING_TIME_MS(300000),
@@ -210,6 +211,8 @@ static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
                .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
                /* And the P/Q/R/S equivalent setting: */
                .start_dynspc = 0,
+               .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
+                            max_fdb_entries, max_fdb_entries, },
                /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
                .poly = 0x97,
                /* This selects between Independent VLAN Learning (IVL) and
@@ -264,20 +267,15 @@ static int sja1105_init_static_vlan(struct sja1105_private *priv)
                .vmemb_port = 0,
                .vlan_bc = 0,
                .tag_port = 0,
-               .vlanid = 0,
+               .vlanid = 1,
        };
        int i;
 
        table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
 
-       /* The static VLAN table will only contain the initial pvid of 0.
+       /* The static VLAN table will only contain the initial pvid of 1.
         * All other VLANs are to be configured through dynamic entries,
         * and kept in the static configuration table as backing memory.
-        * The pvid of 0 is sufficient to pass traffic while the ports are
-        * standalone and when vlan_filtering is disabled. When filtering
-        * gets enabled, the switchdev core sets up the VLAN ID 1 and sets
-        * it as the new pvid. Actually 'pvid 1' still comes up in 'bridge
-        * vlan' even when vlan_filtering is off, but it has no effect.
         */
        if (table->entry_count) {
                kfree(table->entries);
@@ -291,7 +289,7 @@ static int sja1105_init_static_vlan(struct sja1105_private *priv)
 
        table->entry_count = 1;
 
-       /* VLAN ID 0: all DT-defined ports are members; no restrictions on
+       /* VLAN 1: all DT-defined ports are members; no restrictions on
         * forwarding; always transmit priority-tagged frames as untagged.
         */
        for (i = 0; i < SJA1105_NUM_PORTS; i++) {
@@ -818,6 +816,77 @@ static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
                   __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static int
+sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
+                             const struct sja1105_l2_lookup_entry *requested)
+{
+       struct sja1105_l2_lookup_entry *l2_lookup;
+       struct sja1105_table *table;
+       int i;
+
+       table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
+       l2_lookup = table->entries;
+
+       for (i = 0; i < table->entry_count; i++)
+               if (l2_lookup[i].macaddr == requested->macaddr &&
+                   l2_lookup[i].vlanid == requested->vlanid &&
+                   l2_lookup[i].destports & BIT(port))
+                       return i;
+
+       return -1;
+}
+
+/* We want FDB entries added statically through the bridge command to persist
+ * across switch resets, which are a common thing during normal SJA1105
+ * operation. So we have to back them up in the static configuration tables
+ * and hence apply them on next static config upload... yay!
+ */
+static int
+sja1105_static_fdb_change(struct sja1105_private *priv, int port,
+                         const struct sja1105_l2_lookup_entry *requested,
+                         bool keep)
+{
+       struct sja1105_l2_lookup_entry *l2_lookup;
+       struct sja1105_table *table;
+       int rc, match;
+
+       table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
+
+       match = sja1105_find_static_fdb_entry(priv, port, requested);
+       if (match < 0) {
+               /* Can't delete a missing entry. */
+               if (!keep)
+                       return 0;
+
+               /* No match => new entry */
+               rc = sja1105_table_resize(table, table->entry_count + 1);
+               if (rc)
+                       return rc;
+
+               match = table->entry_count - 1;
+       }
+
+       /* Assign pointer after the resize (it may be new memory) */
+       l2_lookup = table->entries;
+
+       /* We have a match.
+        * If the job was to add this FDB entry, it's already done (mostly
+        * anyway, since the port forwarding mask may have changed, case in
+        * which we update it).
+        * Otherwise we have to delete it.
+        */
+       if (keep) {
+               l2_lookup[match] = *requested;
+               return 0;
+       }
+
+       /* To remove, the strategy is to overwrite the element with
+        * the last one, and then reduce the array size by 1
+        */
+       l2_lookup[match] = l2_lookup[table->entry_count - 1];
+       return sja1105_table_resize(table, table->entry_count - 1);
+}
+
 /* First-generation switches have a 4-way set associative TCAM that
  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
@@ -868,7 +937,7 @@ int sja1105et_fdb_add(struct dsa_switch *ds, int port,
        struct sja1105_private *priv = ds->priv;
        struct device *dev = ds->dev;
        int last_unused = -1;
-       int bin, way;
+       int bin, way, rc;
 
        bin = sja1105et_fdb_hash(priv, addr, vid);
 
@@ -912,9 +981,13 @@ int sja1105et_fdb_add(struct dsa_switch *ds, int port,
        }
        l2_lookup.index = sja1105et_fdb_index(bin, way);
 
-       return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
-                                           l2_lookup.index, &l2_lookup,
-                                           true);
+       rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
+                                         l2_lookup.index, &l2_lookup,
+                                         true);
+       if (rc < 0)
+               return rc;
+
+       return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
 }
 
 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
@@ -922,7 +995,7 @@ int sja1105et_fdb_del(struct dsa_switch *ds, int port,
 {
        struct sja1105_l2_lookup_entry l2_lookup = {0};
        struct sja1105_private *priv = ds->priv;
-       int index, bin, way;
+       int index, bin, way, rc;
        bool keep;
 
        bin = sja1105et_fdb_hash(priv, addr, vid);
@@ -944,8 +1017,12 @@ int sja1105et_fdb_del(struct dsa_switch *ds, int port,
        else
                keep = false;
 
-       return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
-                                           index, &l2_lookup, keep);
+       rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
+                                         index, &l2_lookup, keep);
+       if (rc < 0)
+               return rc;
+
+       return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
 }
 
 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
@@ -993,12 +1070,17 @@ int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
                dev_err(ds->dev, "FDB is full, cannot add entry.\n");
                return -EINVAL;
        }
+       l2_lookup.lockeds = true;
        l2_lookup.index = i;
 
 skip_finding_an_index:
-       return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
-                                           l2_lookup.index, &l2_lookup,
-                                           true);
+       rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
+                                         l2_lookup.index, &l2_lookup,
+                                         true);
+       if (rc < 0)
+               return rc;
+
+       return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
 }
 
 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
@@ -1032,52 +1114,72 @@ int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
        else
                keep = false;
 
-       return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
-                                           l2_lookup.index, &l2_lookup, keep);
+       rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
+                                         l2_lookup.index, &l2_lookup, keep);
+       if (rc < 0)
+               return rc;
+
+       return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
 }
 
 static int sja1105_fdb_add(struct dsa_switch *ds, int port,
                           const unsigned char *addr, u16 vid)
 {
        struct sja1105_private *priv = ds->priv;
-       int rc;
+       u16 rx_vid, tx_vid;
+       int rc, i;
+
+       if (dsa_port_is_vlan_filtering(&ds->ports[port]))
+               return priv->info->fdb_add_cmd(ds, port, addr, vid);
 
        /* Since we make use of VLANs even when the bridge core doesn't tell us
         * to, translate these FDB entries into the correct dsa_8021q ones.
+        * The basic idea (also repeats for removal below) is:
+        * - Each of the other front-panel ports needs to be able to forward a
+        *   pvid-tagged (aka tagged with their rx_vid) frame that matches this
+        *   DMAC.
+        * - The CPU port (aka the tx_vid of this port) needs to be able to
+        *   send a frame matching this DMAC to the specified port.
+        * For a better picture see net/dsa/tag_8021q.c.
         */
-       if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
-               unsigned int upstream = dsa_upstream_port(priv->ds, port);
-               u16 tx_vid = dsa_8021q_tx_vid(ds, port);
-               u16 rx_vid = dsa_8021q_rx_vid(ds, port);
+       for (i = 0; i < SJA1105_NUM_PORTS; i++) {
+               if (i == port)
+                       continue;
+               if (i == dsa_upstream_port(priv->ds, port))
+                       continue;
 
-               rc = priv->info->fdb_add_cmd(ds, port, addr, tx_vid);
+               rx_vid = dsa_8021q_rx_vid(ds, i);
+               rc = priv->info->fdb_add_cmd(ds, port, addr, rx_vid);
                if (rc < 0)
                        return rc;
-               return priv->info->fdb_add_cmd(ds, upstream, addr, rx_vid);
        }
-       return priv->info->fdb_add_cmd(ds, port, addr, vid);
+       tx_vid = dsa_8021q_tx_vid(ds, port);
+       return priv->info->fdb_add_cmd(ds, port, addr, tx_vid);
 }
 
 static int sja1105_fdb_del(struct dsa_switch *ds, int port,
                           const unsigned char *addr, u16 vid)
 {
        struct sja1105_private *priv = ds->priv;
-       int rc;
+       u16 rx_vid, tx_vid;
+       int rc, i;
 
-       /* Since we make use of VLANs even when the bridge core doesn't tell us
-        * to, translate these FDB entries into the correct dsa_8021q ones.
-        */
-       if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
-               unsigned int upstream = dsa_upstream_port(priv->ds, port);
-               u16 tx_vid = dsa_8021q_tx_vid(ds, port);
-               u16 rx_vid = dsa_8021q_rx_vid(ds, port);
+       if (dsa_port_is_vlan_filtering(&ds->ports[port]))
+               return priv->info->fdb_del_cmd(ds, port, addr, vid);
+
+       for (i = 0; i < SJA1105_NUM_PORTS; i++) {
+               if (i == port)
+                       continue;
+               if (i == dsa_upstream_port(priv->ds, port))
+                       continue;
 
-               rc = priv->info->fdb_del_cmd(ds, port, addr, tx_vid);
+               rx_vid = dsa_8021q_rx_vid(ds, i);
+               rc = priv->info->fdb_del_cmd(ds, port, addr, rx_vid);
                if (rc < 0)
                        return rc;
-               return priv->info->fdb_del_cmd(ds, upstream, addr, rx_vid);
        }
-       return priv->info->fdb_del_cmd(ds, port, addr, vid);
+       tx_vid = dsa_8021q_tx_vid(ds, port);
+       return priv->info->fdb_del_cmd(ds, port, addr, tx_vid);
 }
 
 static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
@@ -1085,8 +1187,12 @@ static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
 {
        struct sja1105_private *priv = ds->priv;
        struct device *dev = ds->dev;
+       u16 rx_vid, tx_vid;
        int i;
 
+       rx_vid = dsa_8021q_rx_vid(ds, port);
+       tx_vid = dsa_8021q_tx_vid(ds, port);
+
        for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
                struct sja1105_l2_lookup_entry l2_lookup = {0};
                u8 macaddr[ETH_ALEN];
@@ -1112,15 +1218,25 @@ static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
                        continue;
                u64_to_ether_addr(l2_lookup.macaddr, macaddr);
 
-               /* We need to hide the dsa_8021q VLAN from the user.
-                * Convert the TX VID into the pvid that is active in
-                * standalone and non-vlan_filtering modes, aka 1.
-                * The RX VID is applied on the CPU port, which is not seen by
-                * the bridge core anyway, so there's nothing to hide.
+               /* We need to hide the dsa_8021q VLANs from the user. This
+                * basically means hiding the duplicates and only showing
+                * the pvid that is supposed to be active in standalone and
+                * non-vlan_filtering modes (aka 1).
+                * - For statically added FDB entries (bridge fdb add), we
+                *   can convert the TX VID (coming from the CPU port) into the
+                *   pvid and ignore the RX VIDs of the other ports.
+                * - For dynamically learned FDB entries, a single entry with
+                *   no duplicates is learned - that which has the real port's
+                *   pvid, aka RX VID.
                 */
-               if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
-                       l2_lookup.vlanid = 1;
-               cb(macaddr, l2_lookup.vlanid, false, data);
+               if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
+                       if (l2_lookup.vlanid == tx_vid ||
+                           l2_lookup.vlanid == rx_vid)
+                               l2_lookup.vlanid = 1;
+                       else
+                               continue;
+               }
+               cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
        }
        return 0;
 }