net: dsa: b53: Rework ARL bin logic
authorFlorian Fainelli <f.fainelli@gmail.com>
Tue, 21 Apr 2020 03:26:54 +0000 (20:26 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Apr 2020 14:33:10 +0000 (16:33 +0200)
[ Upstream commit 6344dbde6a27d10d16246d734b968f84887841e2 ]

When asking the ARL to read a MAC address, we will get a number of bins
returned in a single read. Out of those bins, there can essentially be 3
states:

- all bins are full, we have no space left, and we can either replace an
  existing address or return that full condition

- the MAC address was found, then we need to return its bin index and
  modify that one, and only that one

- the MAC address was not found and we have a least one bin free, we use
  that bin index location then

The code would unfortunately fail on all counts.

Fixes: 1da6df85c6fb ("net: dsa: b53: Implement ARL add/del/dump operations")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/net/dsa/b53/b53_common.c
drivers/net/dsa/b53/b53_regs.h

index ebbf03d..813f14a 100644 (file)
@@ -1450,6 +1450,7 @@ static int b53_arl_read(struct b53_device *dev, u64 mac,
                        u16 vid, struct b53_arl_entry *ent, u8 *idx,
                        bool is_valid)
 {
+       DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
        unsigned int i;
        int ret;
 
@@ -1457,6 +1458,8 @@ static int b53_arl_read(struct b53_device *dev, u64 mac,
        if (ret)
                return ret;
 
+       bitmap_zero(free_bins, dev->num_arl_entries);
+
        /* Read the bins */
        for (i = 0; i < dev->num_arl_entries; i++) {
                u64 mac_vid;
@@ -1468,16 +1471,24 @@ static int b53_arl_read(struct b53_device *dev, u64 mac,
                           B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
                b53_arl_to_entry(ent, mac_vid, fwd_entry);
 
-               if (!(fwd_entry & ARLTBL_VALID))
+               if (!(fwd_entry & ARLTBL_VALID)) {
+                       set_bit(i, free_bins);
                        continue;
+               }
                if ((mac_vid & ARLTBL_MAC_MASK) != mac)
                        continue;
                if (dev->vlan_enabled &&
                    ((mac_vid >> ARLTBL_VID_S) & ARLTBL_VID_MASK) != vid)
                        continue;
                *idx = i;
+               return 0;
        }
 
+       if (bitmap_weight(free_bins, dev->num_arl_entries) == 0)
+               return -ENOSPC;
+
+       *idx = find_first_bit(free_bins, dev->num_arl_entries);
+
        return -ENOENT;
 }
 
@@ -1507,10 +1518,21 @@ static int b53_arl_op(struct b53_device *dev, int op, int port,
        if (op)
                return ret;
 
-       /* We could not find a matching MAC, so reset to a new entry */
-       if (ret) {
+       switch (ret) {
+       case -ENOSPC:
+               dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
+                       addr, vid);
+               return is_valid ? ret : 0;
+       case -ENOENT:
+               /* We could not find a matching MAC, so reset to a new entry */
+               dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
+                       addr, vid, idx);
                fwd_entry = 0;
-               idx = 1;
+               break;
+       default:
+               dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
+                       addr, vid, idx);
+               break;
        }
 
        memset(&ent, 0, sizeof(ent));
index d914e75..14f617e 100644 (file)
 #define   ARLTBL_STATIC                        BIT(15)
 #define   ARLTBL_VALID                 BIT(16)
 
+/* Maximum number of bin entries in the ARL for all switches */
+#define B53_ARLTBL_MAX_BIN_ENTRIES     4
+
 /* ARL Search Control Register (8 bit) */
 #define B53_ARL_SRCH_CTL               0x50
 #define B53_ARL_SRCH_CTL_25            0x20