Skip to contentSkip to conversation

net: pse-pd: add poll path and LED trigger support

PATCH New Carlo Szelinsky 2 participants 5 messages Nov 28 – Nov 29

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v6 cover net: pse-pd: add poll path and LED trigger support

Some PSE controllers have no IRQ line, and boards want to show the PoE state of each port on an LED. This series adds both.

Patch 1 adds pse_poll_helper(). The core calls the driver's map_event() from a delayed work and handles the events like the IRQ path does.

Patch 2 adds two LED triggers per PI, "delivering" and "enabled". "delivering" needs events, so it exists only on a controller with an IRQ or polling. "enabled" works on every controller.

The poll helper has no in-tree user yet. The users I know of are out of tree: the IP804AR driver in OpenWrt and an HS104 driver (S600WP switch). Patch 2 is also useful without them: "enabled" works on every controller, and "delivering" on IRQ controllers such as tps23881.

Two questions:

1. The triggers are per PI, as phy_led_triggers.c does it per PHY. That is up to 96 triggers on a 48-port controller. Is this fine, or do you prefer one trigger with a per-LED port setting, like ledtrig-netdev?

2. Kory: with the "disabled" budget strategy, a polled controller keeps the power control in hardware. pse_pw_d_is_sw_pw_control() takes it over only for an IRQ controller, and polled drivers may not report the events that software control needs. Is that what you want, or should a driver opt in to software control explicitly?

A polled driver also needs Kory's fix "net: pse-pd: fix bogus budget evaluation strategy check in IRQ handler", as a poll often reports DETECTION together with other events. It is not a build dependency:
https://lore.kernel.org/netdev/[email protected]/

Testing: on an S600WP switch with the HS104 PSE chip, polled, with LEDs on both triggers, plugging and unplugging PDs, and unbinding and binding the driver. Also in QEMU arm64 with fake PSE controllers (polling every 1 ms, bind/unbind loops, fake LEDs), with KASAN, kmemleak and lockdep. Kory's fix was applied for the tests. Lockdep reports one inversion between pcdev->lock and the regulator supply lock. It is in net-next without this series too.

Changes in v6:
- Rebased on net-next, which now has the teardown fixes that v5 waited for.
- Poll helper:
- Renamed devm_pse_poll_helper() to pse_poll_helper(), as it adds no devres action.
- Must be called before the controller registers (-EBUSY and a WARN after). The register call allocates the poll buffers once nr_lines is final, and starts the work as its last step (Jakub, Oleksij).
- The work runs on system_freezable_power_efficient_wq.
- A failing map_event() is logged once when it starts failing and once when it works again.
- d->name is optional. The comments that said events need an IRQ now also cover polling.
- LED triggers:
- Registered before the PI regulators. A failure now fails the probe (Jakub, Oleksij).
- "delivering" only with an IRQ or polling.
- "enabled" shows the admin state as ethtool reports it, also under software power control.
- Also updated when the core switches a PI for the power budget.
- The PI state is read only while an LED is bound, and only the state that LED shows. Bound LEDs are counted in .activate and .deactivate. No hardware reads at probe.

v5: https://lore.kernel.org/all/[email protected]/

Carlo Szelinsky (2):
net: pse-pd: add pse_poll_helper()
net: pse-pd: add LED trigger support via notification path

 drivers/net/pse-pd/pse_core.c | 490 ++++++++++++++++++++++++++++++++--
 include/linux/pse-pd/pse.h    |  31 ++-
 2 files changed, 488 insertions(+), 33 deletions(-)

base-commit: f89726ffabef1b08c52e6163b7ab94b0d5f15503

Signature · 1 line
2.43.0
#1 lore raw

PATCH v6 1/2 net: pse-pd: add pse_poll_helper()

Some PSE controllers have no interrupt line, or the board does not wire it. These controllers can still report events if the core polls them.

Move the event loop of pse_isr() into pse_handle_events(), and add pse_poll_helper(). It uses the same pse_irq_desc as the IRQ path. A delayed work calls the driver's map_event() every poll_interval_ms (default 500 ms) and passes the events to pse_handle_events(). When map_event() starts or stops failing, the core logs it once, and the next poll runs as usual.

The helper must be called before the controller registers, and returns -EBUSY otherwise. It has no devm_ prefix, as it allocates nothing and adds no devres action. pse_controller_register() allocates the poll buffers once nr_lines is final, and starts the work at its end, so the work never runs on a controller that is not fully set up, or on one whose probe failed. pse_controller_unregister() stops it next to the IRQ, before the PI data and power domains are released.

The work runs on system_freezable_power_efficient_wq, so it does not access the bus during suspend, and it is not bound to one CPU when power efficient workqueues are enabled.

With the "disabled" budget strategy, a polled controller keeps the power control in hardware, as a controller without events does today. The core takes it over only for an IRQ controller, which it expects to report PD detection while a PI is off. With the static strategy, a polled controller uses the software power control, as an IRQ one does. Update the comments that say this needs an IRQ.

---
 drivers/net/pse-pd/pse_core.c | 183 ++++++++++++++++++++++++++++------
 include/linux/pse-pd/pse.h    |  27 ++++-
 2 files changed, 177 insertions(+), 33 deletions(-)
drivers/net/pse-pd/pse_core.c +154 -29
index 0477cf6787bd..2fb10f5d8f8e 100644
@@ -14,10 +14,14 @@
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
 #include <linux/rtnetlink.h>
+#include <linux/workqueue.h>
 #include <net/net_trackers.h>
 
 #define PSE_PW_D_LIMIT INT_MAX
 
+/* Default poll interval: fast enough for PD events, light on the I2C bus */
+#define PSE_DEFAULT_POLL_INTERVAL_MS 500
+
 static DEFINE_MUTEX(pse_list_mutex);
 static LIST_HEAD(pse_controller_list);
 static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
@@ -392,8 +396,8 @@ static void pse_pw_d_retry_power_delivery(struct pse_controller_dev *pcdev,
  * @pw_d: a pointer to the PSE power domain
  *
  * This function determines whether the power control for a specific power
- * domain is managed by software in the interrupt handler rather than directly
- * by hardware.
+ * domain is managed by software in the event handler (IRQ or poll) rather
+ * than directly by hardware.
  *
  * Software power control is active in the following cases:
  * - When the budget evaluation strategy is set to static
@@ -1119,6 +1123,19 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 		goto free_kfifo;
 	}
 
+	if (pcdev->polling) {
+		pcdev->poll_notifs = devm_kzalloc_objs(pcdev->dev,
+						       *pcdev->poll_notifs,
+						       pcdev->nr_lines);
+		pcdev->poll_notifs_mask = devm_bitmap_zalloc(pcdev->dev,
+							     pcdev->nr_lines,
+							     GFP_KERNEL);
+		if (!pcdev->poll_notifs || !pcdev->poll_notifs_mask) {
+			ret = -ENOMEM;
+			goto free_kfifo;
+		}
+	}
+
 	ret = of_load_pse_pis(pcdev);
 	if (ret)
 		goto free_kfifo;
@@ -1171,6 +1188,14 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 	list_add(&pcdev->list, &pse_controller_list);
 	mutex_unlock(&pse_list_mutex);
 
+	/* Start polling only now, so the worker never sees a controller
+	 * that is not fully set up.
+	 */
+	if (pcdev->polling)
+		queue_delayed_work(system_freezable_power_efficient_wq,
+				   &pcdev->poll_work,
+				   msecs_to_jiffies(pcdev->poll_interval_ms));
+
 	return 0;
 
 free_kfifo:
@@ -1193,12 +1218,15 @@ void pse_controller_unregister(struct pse_controller_dev *pcdev)
 	pcdev->unregistering = true;
 	mutex_unlock(&pcdev->lock);
 
-	/* Stop the IRQ and the notification worker first: pse_isr() uses
-	 * pcdev->pi and the power domains that pse_flush_pw_ds() drops,
-	 * and the worker reads the kfifo that is freed below.
+	/* Stop the IRQ, the poll work and the notification worker first:
+	 * pse_isr() and pse_poll_worker() use pcdev->pi and the power
+	 * domains that pse_flush_pw_ds() drops, and the worker reads the
+	 * kfifo that is freed below.
 	 */
 	if (pcdev->irq)
 		disable_irq(pcdev->irq);
+	if (pcdev->polling)
+		cancel_delayed_work_sync(&pcdev->poll_work);
 	/* Unlink before the PI array is freed: of_pse_control_get() walks
 	 * the list and reads pcdev->pi[].
 	 */
@@ -1326,48 +1354,39 @@ static int pse_set_config_isr(struct pse_controller_dev *pcdev, int id,
 }
 
 /**
- * pse_isr - IRQ handler for PSE
- * @irq: irq number
- * @data: pointer to user interrupt structure
+ * pse_handle_events - Process PSE events for all PIs
+ * @pcdev: a pointer to the PSE controller device
+ * @pi_notifs: per-PI notification array
+ * @notifs_mask: bitmap of PIs with events (sized for pcdev->nr_lines)
  *
- * Return: irqreturn_t - status of IRQ
+ * Common event handling shared between IRQ and poll paths.
+ * Caller must hold pcdev->lock.
  */
-static irqreturn_t pse_isr(int irq, void *data)
+static void pse_handle_events(struct pse_controller_dev *pcdev,
+			      unsigned long *pi_notifs,
+			      unsigned long *notifs_mask)
 {
-	struct pse_controller_dev *pcdev;
-	struct pse_irq_desc *desc;
-	struct pse_irq *h = data;
-	int ret, i;
-
-	desc = &h->desc;
-	pcdev = h->pcdev;
+	int i;
 
-	/* Clear notifs mask */
-	memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs));
-	bitmap_zero(h->notifs_mask, pcdev->nr_lines);
-	mutex_lock(&pcdev->lock);
-	ret = desc->map_event(irq, pcdev, h->notifs, h->notifs_mask);
-	if (ret || bitmap_empty(h->notifs_mask, pcdev->nr_lines)) {
-		mutex_unlock(&pcdev->lock);
-		return IRQ_NONE;
-	}
+	lockdep_assert_held(&pcdev->lock);
 
-	for_each_set_bit(i, h->notifs_mask, pcdev->nr_lines) {
+	for_each_set_bit(i, notifs_mask, pcdev->nr_lines) {
 		unsigned long notifs, rnotifs;
 		struct pse_ntf ntf = {};
+		int ret;
 
 		/* Do nothing PI not described */
 		if (!pcdev->pi[i].rdev)
 			continue;
 
-		notifs = h->notifs[i];
+		notifs = pi_notifs[i];
 		if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[i].pw_d)) {
 			ret = pse_set_config_isr(pcdev, i, notifs);
 			if (ret)
 				notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR;
 		}
 
-		dev_dbg(h->pcdev->dev,
+		dev_dbg(pcdev->dev,
 			"Sending PSE notification EVT 0x%lx\n", notifs);
 
 		ntf.notifs = notifs;
@@ -1380,12 +1399,73 @@ static irqreturn_t pse_isr(int irq, void *data)
 		regulator_notifier_call_chain(pcdev->pi[i].rdev, rnotifs,
 					      NULL);
 	}
+}
+
+/**
+ * pse_isr - IRQ handler for PSE
+ * @irq: irq number
+ * @data: pointer to user interrupt structure
+ *
+ * Return: irqreturn_t - status of IRQ
+ */
+static irqreturn_t pse_isr(int irq, void *data)
+{
+	struct pse_controller_dev *pcdev;
+	struct pse_irq *h = data;
+	int ret;
+
+	pcdev = h->pcdev;
+
+	/* Clear notifs mask */
+	memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs));
+	bitmap_zero(h->notifs_mask, pcdev->nr_lines);
+	mutex_lock(&pcdev->lock);
+	ret = h->desc.map_event(irq, pcdev, h->notifs, h->notifs_mask);
+	if (ret || bitmap_empty(h->notifs_mask, pcdev->nr_lines)) {
+		mutex_unlock(&pcdev->lock);
+		return IRQ_NONE;
+	}
 
+	pse_handle_events(pcdev, h->notifs, h->notifs_mask);
 	mutex_unlock(&pcdev->lock);
 
 	return IRQ_HANDLED;
 }
 
+static void pse_poll_worker(struct work_struct *work)
+{
+	struct pse_controller_dev *pcdev =
+		container_of(work, struct pse_controller_dev,
+			     poll_work.work);
+	int ret;
+
+	memset(pcdev->poll_notifs, 0,
+	       pcdev->nr_lines * sizeof(*pcdev->poll_notifs));
+	bitmap_zero(pcdev->poll_notifs_mask, pcdev->nr_lines);
+	mutex_lock(&pcdev->lock);
+	ret = pcdev->poll_desc.map_event(0, pcdev, pcdev->poll_notifs,
+					 pcdev->poll_notifs_mask);
+	/* Log only changes: a controller that stopped answering would
+	 * otherwise log an error at every poll.
+	 */
+	if (ret != pcdev->poll_err) {
+		if (ret)
+			dev_err(pcdev->dev, "Failed to poll events: %pe\n",
+				ERR_PTR(ret));
+		else
+			dev_info(pcdev->dev, "Polling events works again\n");
+		pcdev->poll_err = ret;
+	}
+	if (!ret && !bitmap_empty(pcdev->poll_notifs_mask, pcdev->nr_lines))
+		pse_handle_events(pcdev, pcdev->poll_notifs,
+				  pcdev->poll_notifs_mask);
+	mutex_unlock(&pcdev->lock);
+
+	queue_delayed_work(system_freezable_power_efficient_wq,
+			   &pcdev->poll_work,
+			   msecs_to_jiffies(pcdev->poll_interval_ms));
+}
+
 /**
  * devm_pse_irq_helper - Register IRQ based PSE event notifier
  * @pcdev: a pointer to the PSE
@@ -1448,6 +1528,51 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 }
 EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
 
+/**
+ * pse_poll_helper - Set up a poll-based PSE event notifier
+ * @pcdev: a pointer to the PSE controller device
+ * @d: PSE event description, the same as for devm_pse_irq_helper().
+ *     d->name is not used.
+ *
+ * For PSE controllers without a usable IRQ. Every poll_interval_ms
+ * (default 500 ms), a delayed work calls d->map_event() with irq 0 and
+ * handles the events like pse_isr() does. The work runs on a freezable
+ * workqueue, so it does not touch the hardware during suspend.
+ *
+ * With the "disabled" budget strategy, the hardware keeps the power
+ * control of a polled controller. The PSE core takes it over only for
+ * an IRQ controller, see pse_pw_d_is_sw_pw_control().
+ *
+ * Must be called before devm_pse_controller_register(), which allocates
+ * the poll buffers and starts the polling once the controller is
+ * registered.
+ *
+ * Return: 0 on success, -EBUSY if the controller is already registered,
+ *	   or another errno on failure
+ */
+int pse_poll_helper(struct pse_controller_dev *pcdev,
+		    const struct pse_irq_desc *d)
+{
+	if (!d || !d->map_event)
+		return -EINVAL;
+
+	/* pcdev->pi is set by pse_controller_register(), which must come
+	 * after this call to start the polling.
+	 */
+	if (WARN_ON(pcdev->pi))
+		return -EBUSY;
+
+	pcdev->poll_desc = *d;
+	if (!pcdev->poll_interval_ms)
+		pcdev->poll_interval_ms = PSE_DEFAULT_POLL_INTERVAL_MS;
+
+	INIT_DELAYED_WORK(&pcdev->poll_work, pse_poll_worker);
+	pcdev->polling = true;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pse_poll_helper);
+
 /* PSE control section */
 
 static void __pse_control_release(struct kref *kref)
include/linux/pse-pd/pse.h +23 -4
index 17738ffcd224..e40ed754dbdb 100644
@@ -43,10 +43,12 @@ struct ethtool_c33_pse_pw_limit_range {
 };
 
 /**
- * struct pse_irq_desc - notification sender description for IRQ based events.
+ * struct pse_irq_desc - notification sender description for IRQ or poll
+ *			  based events.
  *
- * @name: the visible name for the IRQ
+ * @name: the visible name for the IRQ, not used for polling
  * @map_event: driver callback to map IRQ status into PSE devices with events.
+ *	       irq is 0 when it is called from the poll work.
  */
 struct pse_irq_desc {
 	const char *name;
@@ -186,8 +188,8 @@ struct ethtool_pse_control_status {
  * @pi_get_prio: Get the PSE PI priority.
  * @pi_set_prio: Configure the PSE PI priority.
  * @pi_get_pw_req: Get the power requested by a PD before enabling the PSE PI.
- *		   This is only relevant when an interrupt is registered using
- *		   devm_pse_irq_helper helper.
+ *		   This is only relevant when events are reported, using the
+ *		   devm_pse_irq_helper() or pse_poll_helper() helper.
  */
 struct pse_controller_ops {
 	int (*setup_pi_matrix)(struct pse_controller_dev *pcdev);
@@ -294,6 +296,14 @@ struct pse_ntf {
  * @pi: table of PSE PIs described in this controller device
  * @no_of_pse_pi: flag set if the pse_pis devicetree node is not used
  * @irq: PSE interrupt
+ * @polling: flag indicating poll-based event detection is active
+ * @poll_interval_ms: poll interval in milliseconds, a driver may set it
+ *		      before pse_poll_helper(), 0 means 500 ms
+ * @poll_work: delayed work for poll-based event detection
+ * @poll_desc: copy of the driver's event descriptor for polling
+ * @poll_notifs: per-PI notification scratch space for poll worker
+ * @poll_notifs_mask: bitmap of PIs with events for poll worker
+ * @poll_err: result of the last poll, to log only changes
  * @pis_prio_max: Maximum value allowed for the PSE PIs priority
  * @supp_budget_eval_strategies: budget evaluation strategies supported
  *				 by the PSE
@@ -316,6 +326,13 @@ struct pse_controller_dev {
 	struct pse_pi *pi;
 	bool no_of_pse_pi;
 	int irq;
+	bool polling;
+	unsigned int poll_interval_ms;
+	struct delayed_work poll_work;
+	struct pse_irq_desc poll_desc;
+	unsigned long *poll_notifs;
+	unsigned long *poll_notifs_mask;
+	int poll_err;
 	unsigned int pis_prio_max;
 	u32 supp_budget_eval_strategies;
 	struct work_struct ntf_work;
@@ -350,6 +367,8 @@ int devm_pse_controller_register(struct device *dev,
 				 struct pse_controller_dev *pcdev);
 int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 			int irq_flags, const struct pse_irq_desc *d);
+int pse_poll_helper(struct pse_controller_dev *pcdev,
+		    const struct pse_irq_desc *d);
 
 struct pse_control *of_pse_control_get(struct device_node *node,
 				       struct phy_device *phydev);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v6 2/2 net: pse-pd: add LED trigger support via notification path

In reply to Carlo Szelinsky

Add two LED triggers per PI, "delivering" and "enabled", so a board can show the PoE state of each port on its LEDs.

The core updates the triggers when the IRQ or poll path reports an event, when a PI is enabled or disabled through its regulator, for example from ethtool, and when the core itself switches a PI on or off to share the power budget. "enabled" shows the admin state as ethtool reports it, also under software power control, where the hardware stays off until a PD is detected.

"delivering" changes when a PD is plugged in or removed, and only an IRQ or the poll helper reports that. So the core registers it only for a controller with one of them. For a driver that calls devm_pse_irq_helper() after registering, the helper adds it.

Reading the PI state can be slow, pd692x0 for example waits 30 ms per command. So the core reads a state only while an LED uses its trigger. It counts the bound LEDs in the .activate and .deactivate callbacks. When an LED is bound, at registration or later from sysfs, .activate also reads the state of that trigger and shows it.

The triggers are registered before the PI regulators. So a consumer that enables a regulator right away finds the triggers ready, and devres removes the triggers only after the regulators are gone. A "delivering" trigger that the IRQ helper added goes away before the IRQ. Once no LED is bound to it, the core no longer touches it.

The triggers are per PI, as phy_led_triggers.c does it per PHY, so a board can bind a port LED with linux,default-trigger alone. One trigger with a per-LED port setting, like ledtrig-netdev, can be added later if the number of triggers becomes a problem.

---
 drivers/net/pse-pd/pse_core.c | 307 ++++++++++++++++++++++++++++++++++
 include/linux/pse-pd/pse.h    |   4 +
 2 files changed, 311 insertions(+)
drivers/net/pse-pd/pse_core.c +307 -0
index 2fb10f5d8f8e..0e8584567042 100644
@@ -8,6 +8,7 @@
 #include <linux/device.h>
 #include <linux/ethtool.h>
 #include <linux/ethtool_netlink.h>
+#include <linux/leds.h>
 #include <linux/of.h>
 #include <linux/phy.h>
 #include <linux/pse-pd/pse.h>
@@ -350,6 +351,7 @@ pse_pi_is_admin_enable_pending(struct pse_controller_dev *pcdev, int id)
 static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev,
 					     int id,
 					     struct netlink_ext_ack *extack);
+static void pse_led_update(struct pse_controller_dev *pcdev, int id);
 
 /**
  * pse_pw_d_retry_power_delivery - Retry power delivery for pending ports in a
@@ -387,6 +389,9 @@ static void pse_pw_d_retry_power_delivery(struct pse_controller_dev *pcdev,
 		ret = _pse_pi_delivery_power_sw_pw_ctrl(pcdev, i, &extack);
 		if (ret == -ERANGE)
 			prio_max = pcdev->pi[i].prio;
+		/* The core switched this PI on without an event for it */
+		if (!ret)
+			pse_led_update(pcdev, i);
 	}
 }
 
@@ -501,6 +506,7 @@ static int pse_disable_pi_pol(struct pse_controller_dev *pcdev, int id)
 	ret = _pse_pi_disable(pcdev, id);
 	if (ret)
 		notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR;
+	pse_led_update(pcdev, id);
 
 	ntf.notifs = notifs;
 	ntf.id = id;
@@ -678,6 +684,287 @@ static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev,
 	return 0;
 }
 
+/**
+ * struct pse_pi_led_triggers - LED triggers of a PSE PI
+ *
+ * @delivering: LED trigger for the power delivering state
+ * @enabled: LED trigger for the admin enabled state
+ * @pcdev: the PSE controller of the PI
+ * @id: PI index
+ * @nr_delivering: number of LEDs bound to @delivering
+ * @nr_enabled: number of LEDs bound to @enabled
+ * @last_delivering: last delivering state, for change detection
+ * @last_enabled: last enabled state, for change detection
+ *
+ * @nr_delivering and @nr_enabled are changed under pcdev->lock.
+ */
+struct pse_pi_led_triggers {
+	struct led_trigger delivering;
+	struct led_trigger enabled;
+	struct pse_controller_dev *pcdev;
+	int id;
+	unsigned int nr_delivering;
+	unsigned int nr_enabled;
+	bool last_delivering;
+	bool last_enabled;
+};
+
+#if IS_ENABLED(CONFIG_LEDS_TRIGGERS)
+/**
+ * pse_pi_get_states - Read the delivering and enabled state of a PI
+ * @pcdev: PSE controller device
+ * @id: PI index
+ * @delivering: out, true if the PI delivers power. NULL to not read it.
+ * @enabled: out, true if the PI is admin enabled, as ethtool reports it.
+ *	     NULL to not read it.
+ *
+ * Reading a state can be slow, so read only the ones asked for.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int pse_pi_get_states(struct pse_controller_dev *pcdev, int id,
+			     bool *delivering, bool *enabled)
+{
+	struct pse_admin_state admin_state = {};
+	struct pse_pw_status pw_status = {};
+	int ret;
+
+	lockdep_assert_held(&pcdev->lock);
+
+	if (delivering) {
+		ret = pcdev->ops->pi_get_pw_status(pcdev, id, &pw_status);
+		if (ret)
+			return ret;
+
+		*delivering = pw_status.c33_pw_status ==
+			      ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING ||
+			      pw_status.podl_pw_status ==
+			      ETHTOOL_PODL_PSE_PW_D_STATUS_DELIVERING;
+	}
+
+	if (!enabled)
+		return 0;
+
+	/* Under software power control, the hardware is off until a PD
+	 * is detected, so use the admin state the core keeps.
+	 */
+	if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) {
+		*enabled = pcdev->pi[id].admin_state_enabled;
+		return 0;
+	}
+
+	ret = pcdev->ops->pi_get_admin_state(pcdev, id, &admin_state);
+	if (ret)
+		return ret;
+
+	*enabled = admin_state.c33_admin_state ==
+		   ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED ||
+		   admin_state.podl_admin_state ==
+		   ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED;
+
+	return 0;
+}
+
+/**
+ * pse_led_update - Update the LED triggers of a PI
+ * @pcdev: PSE controller device
+ * @id: PI index
+ *
+ * Reads the PI state and fires the triggers whose state changed. Reading
+ * the state can be slow, so do nothing while no LED uses the triggers:
+ * pse_led_activate() reads it when an LED is bound.
+ */
+static void pse_led_update(struct pse_controller_dev *pcdev, int id)
+{
+	struct pse_pi_led_triggers *trigs;
+	bool delivering = false, enabled = false;
+
+	lockdep_assert_held(&pcdev->lock);
+
+	if (!pcdev->pi_led_trigs)
+		return;
+
+	trigs = &pcdev->pi_led_trigs[id];
+	/* No LED bound. This also covers a PI that is not described and
+	 * a trigger that is not registered.
+	 */
+	if (!trigs->nr_delivering && !trigs->nr_enabled)
+		return;
+
+	if (pse_pi_get_states(pcdev, id,
+			      trigs->nr_delivering ? &delivering : NULL,
+			      trigs->nr_enabled ? &enabled : NULL))
+		return;
+
+	if (trigs->nr_delivering && trigs->last_delivering != delivering) {
+		trigs->last_delivering = delivering;
+		led_trigger_event(&trigs->delivering,
+				  delivering ? LED_FULL : LED_OFF);
+	}
+
+	if (trigs->nr_enabled && trigs->last_enabled != enabled) {
+		trigs->last_enabled = enabled;
+		led_trigger_event(&trigs->enabled,
+				  enabled ? LED_FULL : LED_OFF);
+	}
+}
+
+/* An LED was bound to @trig, at registration or later from sysfs. Read
+ * its state, as pse_led_update() skips a trigger while no LED is bound
+ * to it. Only touch @trig: the other trigger may not be registered.
+ */
+static void pse_led_activate(struct pse_pi_led_triggers *trigs,
+			     struct led_trigger *trig, unsigned int *nr,
+			     bool *last, struct led_classdev *led_cdev)
+{
+	bool is_delivering = trig == &trigs->delivering;
+	struct pse_controller_dev *pcdev = trigs->pcdev;
+	bool state;
+
+	mutex_lock(&pcdev->lock);
+	(*nr)++;
+	if (!pse_pi_get_states(pcdev, trigs->id,
+			       is_delivering ? &state : NULL,
+			       is_delivering ? NULL : &state)) {
+		if (*last != state) {
+			*last = state;
+			led_trigger_event(trig, state ? LED_FULL : LED_OFF);
+		}
+	}
+	led_set_brightness(led_cdev, *last ? LED_FULL : LED_OFF);
+	mutex_unlock(&pcdev->lock);
+}
+
+static int pse_led_delivering_activate(struct led_classdev *led_cdev)
+{
+	struct pse_pi_led_triggers *trigs =
+		container_of(led_cdev->trigger, struct pse_pi_led_triggers,
+			     delivering);
+
+	pse_led_activate(trigs, &trigs->delivering, &trigs->nr_delivering,
+			 &trigs->last_delivering, led_cdev);
+	return 0;
+}
+
+static void pse_led_delivering_deactivate(struct led_classdev *led_cdev)
+{
+	struct pse_pi_led_triggers *trigs =
+		container_of(led_cdev->trigger, struct pse_pi_led_triggers,
+			     delivering);
+
+	mutex_lock(&trigs->pcdev->lock);
+	trigs->nr_delivering--;
+	mutex_unlock(&trigs->pcdev->lock);
+}
+
+static int pse_led_enabled_activate(struct led_classdev *led_cdev)
+{
+	struct pse_pi_led_triggers *trigs =
+		container_of(led_cdev->trigger, struct pse_pi_led_triggers,
+			     enabled);
+
+	pse_led_activate(trigs, &trigs->enabled, &trigs->nr_enabled,
+			 &trigs->last_enabled, led_cdev);
+	return 0;
+}
+
+static void pse_led_enabled_deactivate(struct led_classdev *led_cdev)
+{
+	struct pse_pi_led_triggers *trigs =
+		container_of(led_cdev->trigger, struct pse_pi_led_triggers,
+			     enabled);
+
+	mutex_lock(&trigs->pcdev->lock);
+	trigs->nr_enabled--;
+	mutex_unlock(&trigs->pcdev->lock);
+}
+
+/* "delivering" follows the PD, which only an event source reports. */
+static int pse_led_delivering_register(struct pse_controller_dev *pcdev)
+{
+	struct device *dev = pcdev->dev;
+	int i, ret;
+
+	for (i = 0; i < pcdev->nr_lines; i++) {
+		struct pse_pi_led_triggers *trigs = &pcdev->pi_led_trigs[i];
+
+		/* Skip PIs not described in device tree */
+		if (!trigs->pcdev)
+			continue;
+
+		trigs->delivering.name = devm_kasprintf(dev, GFP_KERNEL,
+							"pse-%s:port%d:delivering",
+							dev_name(dev), i);
+		if (!trigs->delivering.name)
+			return -ENOMEM;
+		trigs->delivering.activate = pse_led_delivering_activate;
+		trigs->delivering.deactivate = pse_led_delivering_deactivate;
+
+		ret = devm_led_trigger_register(dev, &trigs->delivering);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int pse_led_triggers_register(struct pse_controller_dev *pcdev)
+{
+	struct device *dev = pcdev->dev;
+	const char *dev_id;
+	int i, ret;
+
+	dev_id = dev_name(dev);
+
+	pcdev->pi_led_trigs = devm_kzalloc_objs(dev, *pcdev->pi_led_trigs,
+						pcdev->nr_lines);
+	if (!pcdev->pi_led_trigs)
+		return -ENOMEM;
+
+	for (i = 0; i < pcdev->nr_lines; i++) {
+		struct pse_pi_led_triggers *trigs = &pcdev->pi_led_trigs[i];
+
+		/* Skip PIs not described in device tree */
+		if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np)
+			continue;
+
+		trigs->pcdev = pcdev;
+		trigs->id = i;
+
+		trigs->enabled.name = devm_kasprintf(dev, GFP_KERNEL,
+						     "pse-%s:port%d:enabled",
+						     dev_id, i);
+		if (!trigs->enabled.name)
+			return -ENOMEM;
+		trigs->enabled.activate = pse_led_enabled_activate;
+		trigs->enabled.deactivate = pse_led_enabled_deactivate;
+
+		ret = devm_led_trigger_register(dev, &trigs->enabled);
+		if (ret)
+			return ret;
+	}
+
+	/* A driver that sets up its IRQ after registering gets "delivering"
+	 * from devm_pse_irq_helper().
+	 */
+	if (pcdev->irq || pcdev->polling)
+		return pse_led_delivering_register(pcdev);
+
+	return 0;
+}
+#else
+static inline void pse_led_update(struct pse_controller_dev *pcdev, int id) {}
+static int pse_led_triggers_register(struct pse_controller_dev *pcdev)
+{
+	return 0;
+}
+
+static int pse_led_delivering_register(struct pse_controller_dev *pcdev)
+{
+	return 0;
+}
+#endif /* CONFIG_LEDS_TRIGGERS */
+
 static int pse_pi_enable(struct regulator_dev *rdev)
 {
 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
@@ -708,6 +995,7 @@ static int pse_pi_enable(struct regulator_dev *rdev)
 			pcdev->pi[id].admin_state_enabled = 1;
 			ret = 0;
 		}
+		pse_led_update(pcdev, id);
 		mutex_unlock(&pcdev->lock);
 		return ret;
 	}
@@ -715,6 +1003,7 @@ static int pse_pi_enable(struct regulator_dev *rdev)
 	ret = ops->pi_enable(pcdev, id);
 	if (!ret)
 		pcdev->pi[id].admin_state_enabled = 1;
+	pse_led_update(pcdev, id);
 	mutex_unlock(&pcdev->lock);
 
 	return ret;
@@ -732,6 +1021,7 @@ static int pse_pi_disable(struct regulator_dev *rdev)
 	ret = _pse_pi_disable(pcdev, id);
 	if (!ret)
 		pi->admin_state_enabled = 0;
+	pse_led_update(pcdev, id);
 
 	mutex_unlock(&pcdev->lock);
 	return 0;
@@ -1153,6 +1443,14 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 			goto free_kfifo;
 	}
 
+	/* Register the LED triggers before the regulators: a consumer can
+	 * enable a regulator as soon as it exists, which updates the LEDs.
+	 * devres then also removes the triggers only after the regulators.
+	 */
+	ret = pse_led_triggers_register(pcdev);
+	if (ret)
+		goto free_kfifo;
+
 	/* Each regulator name len is pcdev dev name + 7 char +
 	 * int max digit number (10) + 1
 	 */
@@ -1386,6 +1684,9 @@ static void pse_handle_events(struct pse_controller_dev *pcdev,
 				notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR;
 		}
 
+		/* After pse_set_config_isr(), which can switch the PI */
+		pse_led_update(pcdev, i);
+
 		dev_dbg(pcdev->dev,
 			"Sending PSE notification EVT 0x%lx\n", notifs);
 
@@ -1524,6 +1825,12 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 		return ret;
 	}
 
+	/* The controller is already registered, so add the "delivering"
+	 * LED triggers now. See pse_led_triggers_register().
+	 */
+	if (pcdev->pi_led_trigs)
+		return pse_led_delivering_register(pcdev);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
include/linux/pse-pd/pse.h +4 -0
index e40ed754dbdb..c7253332ec48 100644
@@ -12,6 +12,8 @@
 #include <uapi/linux/ethtool_netlink_generated.h>
 #include <linux/regulator/driver.h>
 
+struct pse_pi_led_triggers;
+
 /* Maximum current in uA according to IEEE 802.3-2022 Table 145-1 */
 #define MAX_PI_CURRENT 1920000
 /* Maximum power in mW according to IEEE 802.3-2022 Table 145-16 */
@@ -312,6 +314,7 @@ struct pse_ntf {
  * @ntf_fifo_lock: protect @ntf_fifo writer
  * @unregistering: set under @lock when unregistering starts; the PSE
  *		   core switches no PI on after that
+ * @pi_led_trigs: per-PI LED trigger state array
  */
 struct pse_controller_dev {
 	const struct pse_controller_ops *ops;
@@ -339,6 +342,7 @@ struct pse_controller_dev {
 	DECLARE_KFIFO_PTR(ntf_fifo, struct pse_ntf);
 	spinlock_t ntf_fifo_lock; /* Protect @ntf_fifo writer */
 	bool unregistering;
+	struct pse_pi_led_triggers *pi_led_trigs;
 };
 
 /**
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH net-next v6 2/2] net: pse-pd: add LED trigger support via notification path

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: pse-pd: uninitialized or stale admin_state_enabled in pse_pi_get_states() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: pse-pd: uninitialized or stale admin_state_enabled in pse_pi_get_states()
--

commit 2a4f233cb7c3e4753e63b541d451694d1abc8a64
Author: Carlo Szelinsky <[email protected]>

net: pse-pd: add LED trigger support via notification path

This commit adds per-PI "delivering" and "enabled" LED triggers to the PSE core and updates them on PI state changes and notification events.

Quoted 4 lines
drivers/net/pse-pd/pse_core.c +0 -0
index 2fb10f5d8f8e1..0e8584567042b 100644

[ ... ]

Quoted 3 lines
drivers/net/pse-pd/pse_core.c +3 -0
+static int pse_pi_get_states(struct pse_controller_dev *pcdev, int id,
+			     bool *delivering, bool *enabled)
+{

[ ... ]

drivers/net/pse-pd/pse_core.c static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev, · line 754

Quoted 7 lines
drivers/net/pse-pd/pse_core.c +7 -0
+	/* Under software power control, the hardware is off until a PD
+	 * is detected, so use the admin state the core keeps.
+	 */
+	if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[id].pw_d)) {
+		*enabled = pcdev->pi[id].admin_state_enabled;
+		return 0;
+	}

[Severity: Medium]
Can pse_pi_get_states() read an uninitialized pcdev->pi[id].pw_d or a stale pcdev->pi[id].admin_state_enabled under software power control?

In pse_controller_register(), pse_led_triggers_register() runs before pse_register_pw_ds() populates pcdev->pi[id].pw_d. If an LED with a matching default_trigger activates during trigger registration, pse_pw_d_is_sw_pw_control() sees pw_d == NULL and queries ops->pi_get_admin_state() instead of admin_state_enabled.

Even after pse_register_pw_ds() runs, pcdev->pi[id].admin_state_enabled remains zero-initialized until a consumer calls of_pse_control_get() -> pse_control_get_internal(), which then initializes admin_state_enabled without holding pcdev->lock or calling pse_led_update():

drivers/net/pse-pd/pse_core.c:pse_control_get_internal() {
    ...
	ret = pse_pi_is_hw_enabled(pcdev, index);
	if (ret < 0)
		goto put_module;

	pcdev->pi[index].admin_state_enabled = ret;
    ...
}

As a result, the "enabled" LED trigger can stay out of sync with the actual admin state.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw
In reply to Carlo Szelinsky
Sashiko has finished reviewing this 2-patch series:

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("net: pse-pd: add LED trigger support via notificatio..."): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]