diff mbox series

[net-next,v1,3/8] net: dsa: microchip: ksz8: Refactor ksz8_fdb_dump()

Message ID 20240402131339.1525330-4-o.rempel@pengutronix.de (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: dsa: microchip: ksz8: refactor FDB dump path | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 943 this patch: 943
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 954 this patch: 954
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 954 this patch: 954
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 55 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 1 this patch: 1
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-03--09-00 (tests: 950)

Commit Message

Oleksij Rempel April 2, 2024, 1:13 p.m. UTC
Refactor ksz8_fdb_dump() to address potential issues:
- Limit the number of iterations to avoid endless loops.
- Handle error codes returned by ksz8_r_dyn_mac_table(), with
  an exception for -ENXIO when no more dynamic entries are detected.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/net/dsa/microchip/ksz8795.c     | 38 ++++++++++++++-----------
 drivers/net/dsa/microchip/ksz8795_reg.h |  1 +
 2 files changed, 23 insertions(+), 16 deletions(-)

Comments

Arun Ramadoss April 2, 2024, 3:37 p.m. UTC | #1
Hi Oleksij,

On Tue, 2024-04-02 at 15:13 +0200, Oleksij Rempel wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you
> know the content is safe
> 
> Refactor ksz8_fdb_dump() to address potential issues:
> - Limit the number of iterations to avoid endless loops.
> - Handle error codes returned by ksz8_r_dyn_mac_table(), with
>   an exception for -ENXIO when no more dynamic entries are detected.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  drivers/net/dsa/microchip/ksz8795.c     | 38 ++++++++++++++---------
> --
>  drivers/net/dsa/microchip/ksz8795_reg.h |  1 +
>  2 files changed, 23 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/net/dsa/microchip/ksz8795.c
> b/drivers/net/dsa/microchip/ksz8795.c
> index e407111db6637..b70aa2c0a85ec 100644
> --- a/drivers/net/dsa/microchip/ksz8795.c
> +++ b/drivers/net/dsa/microchip/ksz8795.c
> @@ -1191,27 +1191,33 @@ void ksz8_flush_dyn_mac_table(struct
> ksz_device *dev, int port)
>  int ksz8_fdb_dump(struct ksz_device *dev, int port,
>                   dsa_fdb_dump_cb_t *cb, void *data)
>  {
> -       int ret = 0;
> -       u16 i = 0;
>         u16 entries = 0;
> -       u8 fid;
> -       u8 src_port;
> -       u8 mac[ETH_ALEN];
> +       int ret, i;
> +
> +       for (i = 0; i < KSZ8_DYN_MAC_ENTRIES; i++) {
> +               u8 mac[ETH_ALEN];
> +               u8 src_port;
> +               u8 fid;

IMO:Since there is no code other than for loop in this function,
variable declaraion can be as before.

> 
> -       do {
>                 ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid,
> &src_port,
>                                            &entries);
> -               if (!ret && port == src_port) {
> -                       ret = cb(mac, fid, false, data);
> -                       if (ret)
> -                               break;
> -               }
> -               i++;
> -       } while (i < entries);
> -       if (i >= entries)
> -               ret = 0;
> +               if (ret == -ENXIO)
> +                       return 0;
> +               if (ret)
> +                       return ret;
> 
> -       return ret;
> +               if (i >= entries)
> +                       return 0;
> +
> +               if (port != src_port)
> +                       continue;

IMO: since already many returns in function, if above statement
replaced as below will increase readability.

	if (port == src_port) {
		ret = cb(mac, fid, false, data);
		if (ret)
			return ret;
	}
> +
> +               ret = cb(mac, fid, false, data);
> +               if (ret)
> +                       return ret;
> +       }
> +
> +       return 0;
>  }
> 
>  static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
> diff --git a/drivers/net/dsa/microchip/ksz8795_reg.h
> b/drivers/net/dsa/microchip/ksz8795_reg.h
> index 7c9341ef73b03..0d13a6e29b0e6 100644
> --- a/drivers/net/dsa/microchip/ksz8795_reg.h
> +++ b/drivers/net/dsa/microchip/ksz8795_reg.h
> @@ -794,5 +794,6 @@
>  #define TAIL_TAG_LOOKUP                        BIT(7)
> 
>  #define FID_ENTRIES                    128
> +#define KSZ8_DYN_MAC_ENTRIES           1024
> 
>  #endif
> --
> 2.39.2
>
diff mbox series

Patch

diff --git a/drivers/net/dsa/microchip/ksz8795.c b/drivers/net/dsa/microchip/ksz8795.c
index e407111db6637..b70aa2c0a85ec 100644
--- a/drivers/net/dsa/microchip/ksz8795.c
+++ b/drivers/net/dsa/microchip/ksz8795.c
@@ -1191,27 +1191,33 @@  void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
 int ksz8_fdb_dump(struct ksz_device *dev, int port,
 		  dsa_fdb_dump_cb_t *cb, void *data)
 {
-	int ret = 0;
-	u16 i = 0;
 	u16 entries = 0;
-	u8 fid;
-	u8 src_port;
-	u8 mac[ETH_ALEN];
+	int ret, i;
+
+	for (i = 0; i < KSZ8_DYN_MAC_ENTRIES; i++) {
+		u8 mac[ETH_ALEN];
+		u8 src_port;
+		u8 fid;
 
-	do {
 		ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid, &src_port,
 					   &entries);
-		if (!ret && port == src_port) {
-			ret = cb(mac, fid, false, data);
-			if (ret)
-				break;
-		}
-		i++;
-	} while (i < entries);
-	if (i >= entries)
-		ret = 0;
+		if (ret == -ENXIO)
+			return 0;
+		if (ret)
+			return ret;
 
-	return ret;
+		if (i >= entries)
+			return 0;
+
+		if (port != src_port)
+			continue;
+
+		ret = cb(mac, fid, false, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
diff --git a/drivers/net/dsa/microchip/ksz8795_reg.h b/drivers/net/dsa/microchip/ksz8795_reg.h
index 7c9341ef73b03..0d13a6e29b0e6 100644
--- a/drivers/net/dsa/microchip/ksz8795_reg.h
+++ b/drivers/net/dsa/microchip/ksz8795_reg.h
@@ -794,5 +794,6 @@ 
 #define TAIL_TAG_LOOKUP			BIT(7)
 
 #define FID_ENTRIES			128
+#define KSZ8_DYN_MAC_ENTRIES		1024
 
 #endif