[][kernel][common][eth][Add debugfs support for the SFP]

[Description]
Add debugfs support for the SFP.

Users can utilize the following command to dump the SFP state.
- cat /sys/kernel/debug/sfp@X/state

[Example]
 # cat /sys/kernel/debug/sfp@1/state
Module state: present
Module probe attempts: 0 0
Device state: up
Main state: link_up
Fault recovery remaining retries: 5
PHY probe remaining retries: 12
moddef0: 1
rx_los: 0
tx_fault: 0
tx_disable: 0

Without this patch, users are unable to dump state from the SFP
framework.

[Release-log]
N/A


Change-Id: If347f24fb27afdd36ffaa11950e768baa8c6b999
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/9298300
diff --git a/21.02/files/target/linux/mediatek/patches-5.4/999-1717-v5.12-net-phy-sfp-add-debugfs-support.patch b/21.02/files/target/linux/mediatek/patches-5.4/999-1717-v5.12-net-phy-sfp-add-debugfs-support.patch
new file mode 100644
index 0000000..6b136a4
--- /dev/null
+++ b/21.02/files/target/linux/mediatek/patches-5.4/999-1717-v5.12-net-phy-sfp-add-debugfs-support.patch
@@ -0,0 +1,107 @@
+From 83e56d18e57fc46c3a25f917dbd42fb9b1599ab1 Mon Sep 17 00:00:00 2001
+From: Bo-Cun Chen <bc-bocun.chen@mediatek.com>
+Date: Mon, 1 Jul 2024 16:05:32 +0800
+Subject: [PATCH] 
+ [backport-networking-drivers][999-1717-v5.12-net-phy-sfp-add-debugfs-support.patch]
+
+---
+ drivers/net/phy/sfp.c | 55 +++++++++++++++++++++++++++++++++++++++++++
+ 1 file changed, 55 insertions(+)
+
+diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
+index d49a825..4bcc2bb 100644
+--- a/drivers/net/phy/sfp.c
++++ b/drivers/net/phy/sfp.c
+@@ -1,6 +1,7 @@
+ // SPDX-License-Identifier: GPL-2.0
+ #include <linux/acpi.h>
+ #include <linux/ctype.h>
++#include <linux/debugfs.h>
+ #include <linux/delay.h>
+ #include <linux/gpio/consumer.h>
+ #include <linux/hwmon.h>
+@@ -268,6 +269,9 @@ struct sfp {
+ 	char *hwmon_name;
+ #endif
+ 
++#if IS_ENABLED(CONFIG_DEBUG_FS)
++	struct dentry *debugfs_dir;
++#endif
+ };
+ 
+ static bool sff_module_supported(const struct sfp_eeprom_id *id)
+@@ -1617,6 +1621,54 @@ static void sfp_module_tx_enable(struct sfp *sfp)
+ 	sfp_set_state(sfp, sfp->state);
+ }
+ 
++#if IS_ENABLED(CONFIG_DEBUG_FS)
++static int sfp_debug_state_show(struct seq_file *s, void *data)
++{
++	struct sfp *sfp = s->private;
++
++	seq_printf(s, "Module state: %s\n",
++		   mod_state_to_str(sfp->sm_mod_state));
++	seq_printf(s, "Module probe attempts: %d %d\n",
++		   R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init,
++		   R_PROBE_RETRY_SLOW - sfp->sm_mod_tries);
++	seq_printf(s, "Device state: %s\n",
++		   dev_state_to_str(sfp->sm_dev_state));
++	seq_printf(s, "Main state: %s\n",
++		   sm_state_to_str(sfp->sm_state));
++	seq_printf(s, "Fault recovery remaining retries: %d\n",
++		   sfp->sm_fault_retries);
++	seq_printf(s, "PHY probe remaining retries: %d\n",
++		   sfp->sm_phy_retries);
++	seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT));
++	seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS));
++	seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT));
++	seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE));
++	return 0;
++}
++DEFINE_SHOW_ATTRIBUTE(sfp_debug_state);
++
++static void sfp_debugfs_init(struct sfp *sfp)
++{
++	sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL);
++
++	debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp,
++			    &sfp_debug_state_fops);
++}
++
++static void sfp_debugfs_exit(struct sfp *sfp)
++{
++	debugfs_remove_recursive(sfp->debugfs_dir);
++}
++#else
++static void sfp_debugfs_init(struct sfp *sfp)
++{
++}
++
++static void sfp_debugfs_exit(struct sfp *sfp)
++{
++}
++#endif
++
+ static void sfp_module_tx_fault_reset(struct sfp *sfp)
+ {
+ 	unsigned int state = sfp->state;
+@@ -2795,6 +2847,8 @@ static int sfp_probe(struct platform_device *pdev)
+ 	if (!sfp->sfp_bus)
+ 		return -ENOMEM;
+ 
++	sfp_debugfs_init(sfp);
++
+ 	return 0;
+ }
+ 
+@@ -2802,6 +2856,7 @@ static int sfp_remove(struct platform_device *pdev)
+ {
+ 	struct sfp *sfp = platform_get_drvdata(pdev);
+ 
++	sfp_debugfs_exit(sfp);
+ 	sfp_unregister_socket(sfp->sfp_bus);
+ 
+ 	rtnl_lock();
+-- 
+2.18.0
+