All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] net: sfp: support assigning status LEDs to SFP connectors
@ 2022-05-09 12:29 Josua Mayer
  2022-05-09 12:49 ` Andrew Lunn
  2022-05-09 15:54 ` Russell King (Oracle)
  0 siblings, 2 replies; 16+ messages in thread
From: Josua Mayer @ 2022-05-09 12:29 UTC (permalink / raw)
  To: netdev
  Cc: Josua Mayer, David S. Miller, Jakub Kicinski, Rob Herring,
	Russell King, Andrew Lunn, Heiner Kallweit

Dear Maintainers,

I am working on a new device based on the LX2160A platform, that exposes
16 sfp connectors, each with its own led controlled by gpios intended
to show link status.
This patch intends to illustrate in code what we want to achieve,
so that I can better ask you all:
How can this work with the generic led framework, and how was it meant to work?

The paragraphs below are a discussion of paths I have explored without success.
Please let me know if you have any opinions and ideas.

Describing in device-tree that an led node belongs to a particular network
device (dpmac) however seems impossible. Instead the standard appears to work
through triggers, where in device-tree one only annotates that the led should
show a trigger "netdev" or "phy". Both of these make no sense when multiple
network interfaces exist - raising the first question:
How can device-tree indicate that an individual led should show the events of
a particular network interface?

We have found that there is a way in sysfs to echo the name of the network
device to the api of the led driver, and it will start showing link status.
However this has to be done at runtime by the user.
But network interface names are unstable. They depend on probe order and
can be changed at will. Further they can be moved to different namespaces,
which will allow e.g. two instances of "eth0" to coexist.
On the Layerscape platform in particular these devices are created dynamically
by the networkign coprocessor, which supports complex functions such as
creating one network interface that spans multiple ports.
It seems to me that the netdev trigger therefore can not properly reflect
the relation between an LED (which is hard-wired to an sfp cage), and the
link state reported by e.g. a phy inside an sfp module.

There exists also a phy trigger for leds.
When invoking the phy_attach or phy_connect functions from the generic phy
framework to connect an instance of net_device with an instance of phy_device,
a trigger providing the link and speed events is registered.
You may notice that again leds are tied to existence of a particular logical
network interface, which may or may not exist, and may span multiple
physical interfaces in case of layerscape.
This is a dead end though, simply because the dpaa2 driver does not even use
objects of phy_device, so this registering of triggers never happens.

In addition the dpmac nodes in device-tree don't really have a phy modeled.
One end are the serdes which are managed by the networking coprocessor.
The other end has removable sfp modules, which may or may not contain a phy.

The serdes are modeled as phy in device-tree though, perhaps the dpaa2 driver
could be extended to instantiate phy_device objects for the serdes?
However I feel like this would especially not solve when mutliple physical
ports are used as one logical interface.

It seems to me that there should be a way to explicitly link gpio-driven LEDs to
either specific phy nodes, or specific sfp connectors - and have them receive
link events from the respective phy, fully independent even from whether there
is a logical network interface.

If you got here, thank you very much for reading!
Ay comments?

sincerely
Josua Mayer

Signed-off-by: Josua Mayer <josua@solid-run.com>
---
 .../devicetree/bindings/net/sff,sfp.txt       |  4 +++
 drivers/net/phy/sfp.c                         | 36 +++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/Documentation/devicetree/bindings/net/sff,sfp.txt b/Documentation/devicetree/bindings/net/sff,sfp.txt
index 832139919f20..d46df1300d28 100644
--- a/Documentation/devicetree/bindings/net/sff,sfp.txt
+++ b/Documentation/devicetree/bindings/net/sff,sfp.txt
@@ -37,6 +37,10 @@ Optional Properties:
   Specifies the maximum power consumption allowable by a module in the
   slot, in milli-Watts.  Presently, modules can be up to 1W, 1.5W or 2W.
 
+- link-status-led:
+    description: An LED node for showing link status.
+    $ref: /schemas/types.yaml#/definitions/phandle
+
 Example #1: Direct serdes to SFP connection
 
 sfp_eth3: sfp-eth3 {
diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index 2fff62695455..0f18e77b8b68 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -7,6 +7,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/jiffies.h>
+#include <linux/leds.h>
 #include <linux/mdio/mdio-i2c.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -258,6 +259,7 @@ struct sfp {
 	char *hwmon_name;
 #endif
 
+	struct led_classdev *link_status_led;
 };
 
 static bool sff_module_supported(const struct sfp_eeprom_id *id)
@@ -1490,6 +1492,8 @@ static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
 
 static void sfp_sm_link_up(struct sfp *sfp)
 {
+	if (sfp->link_status_led)
+		led_set_brightness(sfp->link_status_led, sfp->link_status_led->max_brightness);
 	sfp_link_up(sfp->sfp_bus);
 	sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
 }
@@ -1497,6 +1501,8 @@ static void sfp_sm_link_up(struct sfp *sfp)
 static void sfp_sm_link_down(struct sfp *sfp)
 {
 	sfp_link_down(sfp->sfp_bus);
+	if (sfp->link_status_led)
+		led_set_brightness(sfp->link_status_led, LED_OFF);
 }
 
 static void sfp_sm_link_check_los(struct sfp *sfp)
@@ -2425,6 +2429,23 @@ static int sfp_probe(struct platform_device *pdev)
 
 		i2c = of_find_i2c_adapter_by_node(np);
 		of_node_put(np);
+
+		np = of_parse_phandle(node, "link-status-led", 0);
+		sfp->link_status_led = of_led_get_hack(np);
+		of_node_put(np);
+
+		if (IS_ERR(sfp->link_status_led)) {
+			switch (PTR_ERR(sfp->link_status_led)) {
+			case -ENODEV:
+				sfp->link_status_led = NULL;
+				break;
+			default:
+				dev_err(sfp->dev, "failed to get link-statusled from 'link-status-led' property: %pe\n", sfp->link_status_led);
+				fallthrough;
+			case -EPROBE_DEFER:
+				return PTR_ERR(sfp->link_status_led);
+			};
+		}
 	} else if (has_acpi_companion(&pdev->dev)) {
 		struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
 		struct fwnode_handle *fw = acpi_fwnode_handle(adev);
@@ -2453,6 +2476,14 @@ static int sfp_probe(struct platform_device *pdev)
 		return err;
 	}
 
+	if (sfp->link_status_led) {
+		/* remove from sysfs to avoid userspce control */
+		led_sysfs_disable(sfp->link_status_led);
+
+		/* turn off initially */
+		led_set_brightness(sfp->link_status_led, LED_OFF);
+	}
+
 	for (i = 0; i < GPIO_MAX; i++)
 		if (sff->gpios & BIT(i)) {
 			sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
@@ -2545,6 +2576,11 @@ static int sfp_remove(struct platform_device *pdev)
 {
 	struct sfp *sfp = platform_get_drvdata(pdev);
 
+	if (sfp->link_status_led) {
+		/* re-enable sysfs interface */
+		led_sysfs_enable(sfp->link_status_led);
+	}
+
 	sfp_unregister_socket(sfp->sfp_bus);
 
 	rtnl_lock();
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-06-01 10:52 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09 12:29 [PATCH RFC] net: sfp: support assigning status LEDs to SFP connectors Josua Mayer
2022-05-09 12:49 ` Andrew Lunn
2022-05-10  8:56   ` Josua Mayer
2022-05-10 12:13     ` Andrew Lunn
2022-05-11 10:26       ` Russell King (Oracle)
2022-05-11 14:48         ` Ioana Ciornei
2022-05-11 10:12     ` Russell King (Oracle)
2022-05-11 15:48     ` Ioana Ciornei
2022-05-18  7:42       ` Josua Mayer
2022-05-09 15:54 ` Russell King (Oracle)
2022-05-10  9:44   ` Josua Mayer
2022-05-11 10:21     ` Russell King (Oracle)
2022-05-11 13:22     ` Ioana Ciornei
2022-05-11 13:39       ` Russell King (Oracle)
2022-06-01 10:18         ` Josua Mayer
2022-06-01 10:52           ` Russell King (Oracle)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.