Netdev List
 help / color / mirror / Atom feed
From: Carlo Szelinsky <github@szelinsky.de>
To: Oleksij Rempel <o.rempel@pengutronix.de>,
	Kory Maincent <kory.maincent@bootlin.com>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Jonas Jelonek <jelonek.jonas@gmail.com>,
	Ross Cawston <ross@r-sc.ca>,
	netdev@vger.kernel.org, linux-leds@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH net-next v7 1/2] net: pse-pd: add devm_pse_poll_helper()
Date: Sat,  5 Dec 2026 14:37:41 +0100	[thread overview]
Message-ID: <20261205133742.905114-2-github@szelinsky.de> (raw)
In-Reply-To: <20261205133742.905114-1-github@szelinsky.de>

Some PSE controllers have no interrupt line, or the board does not wire
it. The PSE core then never learns when a PD is plugged in or removed.
Userspace gets no ethtool notifications, and the static budget strategy
does not work: it only powers a port after the IRQ handler has reported
a PD.

Let these controllers report the same events by polling. The driver
passes the same pse_irq_desc as for devm_pse_irq_helper(). The core
calls its map_event() about every 500 ms and handles the events like
the IRQ handler does. The interval is fixed, as no driver needs another
one yet.

With the "disabled" budget strategy, a polled controller keeps the
power control in hardware, like a controller without events does today.
The core only takes it over for an IRQ controller, which it expects to
report a PD while the port is off.

Update the comments that say events need an IRQ.

Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
---
 drivers/net/pse-pd/pse_core.c | 209 ++++++++++++++++++++++++++++------
 include/linux/pse-pd/pse.h    |  18 ++-
 2 files changed, 185 insertions(+), 42 deletions(-)

diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 0477cf6787bd..07cbc585a898 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -14,10 +14,33 @@
 #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
 
+/* Fast enough for PD events, low load on the I2C bus */
+#define PSE_POLL_INTERVAL_MS 500
+
+/**
+ * struct pse_poll - poll state of a PSE controller
+ * @pcdev: the polled PSE controller
+ * @work: delayed work that polls the controller
+ * @desc: copy of the driver's event description
+ * @notifs: per-PI events of the current poll
+ * @notifs_mask: PIs with events in the current poll
+ * @err: 0 if the last poll worked, else the first error since then, to
+ *	 log only changes
+ */
+struct pse_poll {
+	struct pse_controller_dev *pcdev;
+	struct delayed_work work;
+	struct pse_irq_desc desc;
+	unsigned long *notifs;
+	unsigned long *notifs_mask;
+	int err;
+};
+
 static DEFINE_MUTEX(pse_list_mutex);
 static LIST_HEAD(pse_controller_list);
 static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
@@ -392,8 +415,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
@@ -623,9 +646,9 @@ static int _pse_pi_delivery_power_sw_pw_ctrl(struct pse_controller_dev *pcdev,
 	struct pse_pi *pi = &pcdev->pi[id];
 	int ret, pw_req;
 
-	/* pse_isr(), a priority change and the retry after a disable all
-	 * power a PI through here, so stop them here once unregistering
-	 * has started.
+	/* The event handler, a priority change and the retry after a
+	 * disable all power a PI through here, so stop them here once
+	 * unregistering has started.
 	 */
 	if (pcdev->unregistering)
 		return -ENODEV;
@@ -907,7 +930,7 @@ static void pse_pw_d_release(struct kref *kref)
 }
 
 /* Clear pi[id].pw_d before the domain's reference is dropped. The PI
- * regulator ops and pse_isr() hold pcdev->lock while they use it.
+ * regulator ops and the event handler hold pcdev->lock while they use it.
  */
 static struct pse_power_domain *
 pse_pi_take_pw_d(struct pse_controller_dev *pcdev, int id)
@@ -1119,6 +1142,20 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 		goto free_kfifo;
 	}
 
+	if (pcdev->poll) {
+		struct pse_poll *poll = pcdev->poll;
+
+		poll->notifs = devm_kzalloc_objs(pcdev->dev, *poll->notifs,
+						 pcdev->nr_lines);
+		poll->notifs_mask = devm_bitmap_zalloc(pcdev->dev,
+						       pcdev->nr_lines,
+						       GFP_KERNEL);
+		if (!poll->notifs || !poll->notifs_mask) {
+			ret = -ENOMEM;
+			goto free_kfifo;
+		}
+	}
+
 	ret = of_load_pse_pis(pcdev);
 	if (ret)
 		goto free_kfifo;
@@ -1171,6 +1208,11 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 	list_add(&pcdev->list, &pse_controller_list);
 	mutex_unlock(&pse_list_mutex);
 
+	if (pcdev->poll)
+		queue_delayed_work(system_freezable_power_efficient_wq,
+				   &pcdev->poll->work,
+				   msecs_to_jiffies(PSE_POLL_INTERVAL_MS));
+
 	return 0;
 
 free_kfifo:
@@ -1193,12 +1235,11 @@ 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 all event handling first, it uses what is freed below */
 	if (pcdev->irq)
 		disable_irq(pcdev->irq);
+	if (pcdev->poll)
+		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 +1367,38 @@ 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 - Handle the events of the PIs in @notifs_mask
+ * @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
+ * Shared by the IRQ and the poll path. 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;
+	int i;
 
-	desc = &h->desc;
-	pcdev = h->pcdev;
+	lockdep_assert_held(&pcdev->lock);
 
-	/* 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;
-	}
-
-	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 +1411,67 @@ 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_poll *poll = container_of(work, struct pse_poll,
+					     work.work);
+	struct pse_controller_dev *pcdev = poll->pcdev;
+	int ret;
+
+	memset(poll->notifs, 0, pcdev->nr_lines * sizeof(*poll->notifs));
+	bitmap_zero(poll->notifs_mask, pcdev->nr_lines);
+	mutex_lock(&pcdev->lock);
+	ret = poll->desc.map_event(0, pcdev, poll->notifs, poll->notifs_mask);
+	/* Log only changes, not an error at every poll */
+	if (!ret != !poll->err) {
+		if (ret)
+			dev_err(pcdev->dev, "Failed to poll events: %pe\n",
+				ERR_PTR(ret));
+		else
+			dev_info(pcdev->dev, "Event polling works again\n");
+		poll->err = ret;
+	}
+	if (!ret && !bitmap_empty(poll->notifs_mask, pcdev->nr_lines))
+		pse_handle_events(pcdev, poll->notifs, poll->notifs_mask);
+	mutex_unlock(&pcdev->lock);
+
+	queue_delayed_work(system_freezable_power_efficient_wq, &poll->work,
+			   msecs_to_jiffies(PSE_POLL_INTERVAL_MS));
+}
+
 /**
  * devm_pse_irq_helper - Register IRQ based PSE event notifier
  * @pcdev: a pointer to the PSE
@@ -1393,7 +1479,8 @@ static irqreturn_t pse_isr(int irq, void *data)
  * @irq_flags: the flags to be passed to request_irq
  * @d: PSE interrupt description
  *
- * Return: 0 on success and errno on failure
+ * Return: 0 on success, -EBUSY if the controller already has an event
+ * source, or another errno on failure
  */
 int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 			int irq_flags, const struct pse_irq_desc *d)
@@ -1407,6 +1494,10 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 	if (!d || !d->map_event || !d->name)
 		return -EINVAL;
 
+	/* Only one event source per controller */
+	if (WARN_ON(pcdev->poll || pcdev->irq))
+		return -EBUSY;
+
 	h = devm_kzalloc_obj(dev, *h);
 	if (!h)
 		return -ENOMEM;
@@ -1448,6 +1539,50 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
 }
 EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
 
+/**
+ * devm_pse_poll_helper - Report PSE events by polling
+ * @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. 500 ms after each poll, the
+ * core calls d->map_event() again and handles the events like pse_isr()
+ * does. Polling pauses during suspend.
+ *
+ * With the "disabled" budget strategy, the hardware keeps the power
+ * control, see pse_pw_d_is_sw_pw_control().
+ *
+ * Call it after setting pcdev->dev and before
+ * devm_pse_controller_register(), which starts the polling.
+ *
+ * Return: 0 on success, -EBUSY if the controller is already registered
+ *	   or already has an event source, or another errno on failure
+ */
+int devm_pse_poll_helper(struct pse_controller_dev *pcdev,
+			 const struct pse_irq_desc *d)
+{
+	struct pse_poll *poll;
+
+	if (!d || !d->map_event)
+		return -EINVAL;
+
+	/* Call this once, and before registering, which sets pcdev->pi */
+	if (WARN_ON(pcdev->pi || pcdev->poll || pcdev->irq))
+		return -EBUSY;
+
+	poll = devm_kzalloc(pcdev->dev, sizeof(*poll), GFP_KERNEL);
+	if (!poll)
+		return -ENOMEM;
+
+	poll->pcdev = pcdev;
+	poll->desc = *d;
+	INIT_DELAYED_WORK(&poll->work, pse_poll_worker);
+	pcdev->poll = poll;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_pse_poll_helper);
+
 /* PSE control section */
 
 static void __pse_control_release(struct kref *kref)
@@ -1917,8 +2052,8 @@ static int pse_pi_update_pw_budget(struct pse_controller_dev *pcdev, int id,
 	 */
 	mutex_lock(&pcdev->lock);
 	/* Only a PI under software power control that is enabled and has a
-	 * PD detected gets a budget. Check this here, as pse_isr() changes
-	 * it under the same lock.
+	 * PD detected gets a budget. Check this here, as the event handler
+	 * changes it under the same lock.
 	 */
 	if (!pse_pw_d_is_sw_pw_control(pcdev, pi->pw_d) ||
 	    !pi->admin_state_enabled || !pi->isr_pd_detected)
diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
index 17738ffcd224..008e16b7f72d 100644
--- a/include/linux/pse-pd/pse.h
+++ b/include/linux/pse-pd/pse.h
@@ -43,10 +43,13 @@ 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.
+ *	       When polling, irq is 0, and the callback must report only
+ *	       the events since the last call, not the current state.
  */
 struct pse_irq_desc {
 	const char *name;
@@ -186,8 +189,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 by
+ *		   devm_pse_irq_helper() or devm_pse_poll_helper().
  */
 struct pse_controller_ops {
 	int (*setup_pi_matrix)(struct pse_controller_dev *pcdev);
@@ -218,6 +221,7 @@ struct module;
 struct device_node;
 struct of_phandle_args;
 struct pse_control;
+struct pse_poll;
 struct ethtool_pse_control_status;
 
 /* PSE PI pairset pinout can either be Alternative A or Alternative B */
@@ -249,7 +253,7 @@ struct pse_pi_pairset {
  * @pw_d: Power domain of the PSE PI
  * @pw_d_node: entry in the list of PIs that use @pw_d
  * @prio: Priority of the PSE PI. Used in static budget evaluation strategy
- * @isr_pd_detected: PSE PI detection status managed by the interruption
+ * @isr_pd_detected: PSE PI detection status managed by the event
  *		     handler. This variable is relevant when the power enabled
  *		     management is managed in software like the static
  *		     budget evaluation strategy.
@@ -294,6 +298,7 @@ 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
+ * @poll: poll state, set by devm_pse_poll_helper(), only used by the core
  * @pis_prio_max: Maximum value allowed for the PSE PIs priority
  * @supp_budget_eval_strategies: budget evaluation strategies supported
  *				 by the PSE
@@ -316,6 +321,7 @@ struct pse_controller_dev {
 	struct pse_pi *pi;
 	bool no_of_pse_pi;
 	int irq;
+	struct pse_poll *poll;
 	unsigned int pis_prio_max;
 	u32 supp_budget_eval_strategies;
 	struct work_struct ntf_work;
@@ -350,6 +356,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 devm_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);
-- 
2.43.0


  reply	other threads:[~2026-12-05 13:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:37 [PATCH net-next v7 0/2] net: pse-pd: add poll path and LED trigger support Carlo Szelinsky
2026-12-05 13:37 ` Carlo Szelinsky [this message]
2026-12-05 13:37 ` [PATCH net-next v7 2/2] net: pse-pd: add LED triggers for each PI Carlo Szelinsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205133742.905114-2-github@szelinsky.de \
    --to=github@szelinsky.de \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=jelonek.jonas@gmail.com \
    --cc=kory.maincent@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=pavel@kernel.org \
    --cc=ross@r-sc.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox