From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.171.202.116 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796056326; b=HIPvUe/Nxi7Z/6Dv7NtpWGwxS2sh8RzM6phdvkGDYZuAxZbDxlanwDcgL+qgXDRcgl/f CCJDb+dHzwJVYIIYzief+xe3h2mWTxy2fvP57HTcIGH+J2zMTaq3BmnAsPaDgZHL2l+ur z9a1sSZPwX1H/ib3JR/UUnrQvDPVVOzpDwR0wJtGlxXAOjUVgF/h3CSJhzZRsuOfH2fKH U20pDjbmuNJjf/nVFJGmjjHt4MppKjSUkWjGYd3WoYsOrISZePJVvWjWGmaCl+AiqxSS/ /YnOwKZM7nLlVUWNVUMTD8h9HCSfLJKiuqP5+1Ge3V2xw5PI7AWrRxVYcv31fW1eQ3g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796056326; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:Message-ID: Date:MIME-Version:User-Agent:Subject:To:Cc:References:From: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding: X-Last-TLS-Session-Version; bh=W7itOX9u9x6sqnn3B+P0bH8qmSwhrxhyQLcWecgC02I=; b=TS+au9g/T+9TO0dEbWi49MXjOtz/ZmxFu8JcypE1qk1BDkpNIewOT/lzte7Br8XFO9B6 AXz0F73qBkClu+9riecHdyMNg2KlkSkFhfmnKSg35ZCScYXEAAjs//oCzad99B3cQQVeP WpCiz8IFRc1BofJvpfSmJ0lmZtCe7vByzyZ1YRxaYeCNhzzDNJRKG05Hh+HJrv5g0uRWg FOvCwPyNWLVJUBnzbaGMzZQlXRJFQk8eBuQEG0ERO5U1S2AfNUcGgpVMlXKNSvav1ignd KQeNaf/gUFTbmxwqdTZoJFN2FFx9MxYxeyOHvMzB20RNs2OHBVSbvM6chTdBHlHrflA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=kkaAWrME; arc=none smtp.remote-ip=185.171.202.116 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1FDE606A33D; Mon, 30 Nov 2026 16:32:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F1FDE606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=kkaAWrME Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id EE0D0C2AC6A; Mon, 30 Nov 2026 16:32:13 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id C50BA6007C; Mon, 30 Nov 2026 16:32:03 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id EEB4F11EA04AF; Mon, 30 Nov 2026 17:31:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796056322; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:content-language:in-reply-to:references; bh=W7itOX9u9x6sqnn3B+P0bH8qmSwhrxhyQLcWecgC02I=; b=kkaAWrMEGB62vHKJVkurCfeAhyWqTGRctjE4VhlY4TNY6KELUTVrJssfFVLiPEt/lvMWnW LWpuHrhhcX7dRZbu7uzQffgelctsJBqBawJQBziqFYwlrUwCErxqHXkGFL9p6HZlRobXvH 230xbY9Gai0VqhOUY9/Ru/izs3soa4TlvbJ7aXrv+eoqbc2jCweOM52wQy+6Dkt2De6ITt dStT2JHik0bT/uMQLMa2IXICcwQXuVfDy6YA+4xkP1yzvOvoyTq/2a+KuGzWFTolc/oJVh J6ZgGugD3LtkNR9CIS1bDOlGKgyY/HAqVJ6dQiDiqYS+iojlO143VE7JSbcP8g== Message-ID: Date: Mon, 30 Nov 2026 17:31:55 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Betterbird (Linux) Subject: Re: [PATCH net-next v6 2/2] net: pse-pd: add LED trigger support via notification path To: Carlo Szelinsky , Oleksij Rempel Cc: Andrew Lunn , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Lee Jones , Pavel Machek , Jonas Jelonek , Ross Cawston , netdev@vger.kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org References: <20261128121525.3909836-1-github@szelinsky.de> <20261128121525.3909836-3-github@szelinsky.de> From: Kory Maincent Content-Language: en-US In-Reply-To: <20261128121525.3909836-3-github@szelinsky.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 On 11/28/26 13:15, Carlo Szelinsky wrote: > 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. Human readable commit message please. > Signed-off-by: Carlo Szelinsky > --- > drivers/net/pse-pd/pse_core.c | 307 ++++++++++++++++++++++++++++++++++ > include/linux/pse-pd/pse.h | 4 + > 2 files changed, 311 insertions(+) > > diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c > index 2fb10f5d8f8e..0e8584567042 100644 > --- a/drivers/net/pse-pd/pse_core.c > +++ b/drivers/net/pse-pd/pse_core.c > @@ -8,6 +8,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -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); I don't get why you separate the registration of the two led triggers. What is the reason for that? > + return 0; > +} > +#else > +static inline void pse_led_update(struct pse_controller_dev *pcdev, int id) {} Please no inline in C files. Let the compiler decide. > +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); > diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h > index e40ed754dbdb..c7253332ec48 100644 > --- a/include/linux/pse-pd/pse.h > +++ b/include/linux/pse-pd/pse.h > @@ -12,6 +12,8 @@ > #include > #include > > +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; There is one led trigger per PI isn't it? So why do you put it here instead of in the pse_pi structure? > }; > > /** -- Köry Maincent, Bootlin Embedded Linux and kernel engineering https://bootlin.com