diff mbox series

[net-next,v2,5/5] tsnep: Add PHY loopback selftests

Message ID 20250110144828.4943-6-gerhard@engleder-embedded.com (mailing list archive)
State New
Delegated to: Netdev Maintainers
Headers show
Series Support loopback mode speed selection | expand

Commit Message

Gerhard Engleder Jan. 10, 2025, 2:48 p.m. UTC
Add loopback selftests on PHY level. This enables quick testing of
loopback functionality to ensure working loopback for testing.

Signed-off-by: Gerhard Engleder <gerhard@engleder-embedded.com>
---
 .../net/ethernet/engleder/tsnep_selftests.c   | 102 ++++++++++++++++++
 1 file changed, 102 insertions(+)
diff mbox series

Patch

diff --git a/drivers/net/ethernet/engleder/tsnep_selftests.c b/drivers/net/ethernet/engleder/tsnep_selftests.c
index 8a9145f93147..0a3f92ac35cd 100644
--- a/drivers/net/ethernet/engleder/tsnep_selftests.c
+++ b/drivers/net/ethernet/engleder/tsnep_selftests.c
@@ -10,6 +10,7 @@  enum tsnep_test {
 	TSNEP_TEST_TAPRIO,
 	TSNEP_TEST_TAPRIO_CHANGE,
 	TSNEP_TEST_TAPRIO_EXTENSION,
+	TSNEP_TEST_PHY_LOOPBACK,
 };
 
 static const char tsnep_test_strings[][ETH_GSTRING_LEN] = {
@@ -17,6 +18,7 @@  static const char tsnep_test_strings[][ETH_GSTRING_LEN] = {
 	"TAPRIO                (offline)",
 	"TAPRIO change         (offline)",
 	"TAPRIO extension      (offline)",
+	"PHY loopback          (offline)",
 };
 
 #define TSNEP_TEST_COUNT (sizeof(tsnep_test_strings) / ETH_GSTRING_LEN)
@@ -754,6 +756,98 @@  static bool tsnep_test_taprio_extension(struct tsnep_adapter *adapter)
 	return false;
 }
 
+static bool loopback(struct tsnep_adapter *adapter, int speed)
+{
+	struct phy_device *phydev = adapter->phydev;
+	int retval;
+
+	retval = phy_loopback(phydev, true, speed);
+	if (retval || !phydev->loopback_enabled || !phydev->link || phydev->speed != speed)
+		return false;
+	retval = phy_loopback(phydev, false, 0);
+	if (retval || phydev->loopback_enabled)
+		return false;
+
+	return true;
+}
+
+static bool set_speed(struct tsnep_adapter *adapter, int speed)
+{
+	struct ethtool_link_ksettings cmd;
+	int retval;
+
+	retval = tsnep_ethtool_ops.get_link_ksettings(adapter->netdev, &cmd);
+	if (retval)
+		return false;
+
+	if (speed) {
+		cmd.base.speed = speed;
+		cmd.base.duplex = DUPLEX_FULL;
+		cmd.base.autoneg = AUTONEG_DISABLE;
+	} else {
+		cmd.base.autoneg = AUTONEG_ENABLE;
+	}
+
+	retval = tsnep_ethtool_ops.set_link_ksettings(adapter->netdev, &cmd);
+	if (retval)
+		return false;
+
+	return true;
+}
+
+static bool tsnep_test_phy_loopback(struct tsnep_adapter *adapter)
+{
+	/* 1000Mbps loopback */
+	if (!loopback(adapter, 1000))
+		goto failed;
+
+	/* 100Mbps loopback */
+	if (!loopback(adapter, 100))
+		goto failed;
+
+	/* 1000Mbps loopback after autonegotiation */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 1000))
+		goto failed;
+
+	/* 100Mbps loopback after autonegotiation */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 100))
+		goto failed;
+
+	/* 1000Mbps loopback after 100Mbps fixed speed */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 1000))
+		goto failed;
+
+	/* 100Mbps loopback after 100Mbps fixed speed */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 100))
+		goto failed;
+
+	/* 1000Mbps loopback after 1000Mbps fixed speed */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 1000))
+		goto failed;
+
+	/* 100Mbps loopback after 1000Mbps fixed speed */
+	if (!set_speed(adapter, 0))
+		goto failed;
+	if (!loopback(adapter, 100))
+		goto failed;
+
+	return true;
+
+failed:
+	phy_loopback(adapter->phydev, false, 0);
+	return false;
+}
+
 int tsnep_ethtool_get_test_count(void)
 {
 	return TSNEP_TEST_COUNT;
@@ -777,6 +871,7 @@  void tsnep_ethtool_self_test(struct net_device *netdev,
 		data[TSNEP_TEST_TAPRIO] = 0;
 		data[TSNEP_TEST_TAPRIO_CHANGE] = 0;
 		data[TSNEP_TEST_TAPRIO_EXTENSION] = 0;
+		data[TSNEP_TEST_PHY_LOOPBACK] = 0;
 
 		return;
 	}
@@ -808,4 +903,11 @@  void tsnep_ethtool_self_test(struct net_device *netdev,
 		eth_test->flags |= ETH_TEST_FL_FAILED;
 		data[TSNEP_TEST_TAPRIO_EXTENSION] = 1;
 	}
+
+	if (tsnep_test_phy_loopback(adapter)) {
+		data[TSNEP_TEST_PHY_LOOPBACK] = 0;
+	} else {
+		eth_test->flags |= ETH_TEST_FL_FAILED;
+		data[TSNEP_TEST_PHY_LOOPBACK] = 1;
+	}
 }