[PATCH 2/3] watchdog: add watchdog ping support

Sascha Hauer s.hauer at pengutronix.de
Tue Nov 12 01:16:05 PST 2024


The barebox watchdog API is quite limited. the set_timeout() hook is an
all purpose function for enabling and disabling the watchdog and also to
configure the timeout.
This is not suitable for watchdogs which for example can't change the
timeout anymore once started.
This patch introduces a watchdog_ping() function which hooks into the
drivers with a struct watchdog::ping function. With this the watchdog
can be pinged without caring about the timeout.  The ping hook is
optional. When not implemented watchdog_ping() falls back to call
watchdog_set_timeout() with the current watchdog timeout.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/wd.c              | 17 ++++++++++++++++-
 drivers/watchdog/wd_core.c | 28 ++++++++++++++++++++++++----
 include/watchdog.h         |  7 +++++++
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/commands/wd.c b/commands/wd.c
index 11f888133a..18e8e03378 100644
--- a/commands/wd.c
+++ b/commands/wd.c
@@ -17,14 +17,18 @@ static int do_wd(int argc, char *argv[])
 	struct watchdog *wd = watchdog_get_default();
 	int opt;
 	int rc;
+	bool do_ping = false;
 
-	while ((opt = getopt(argc, argv, "d:x")) > 0) {
+	while ((opt = getopt(argc, argv, "d:xp")) > 0) {
 		switch (opt) {
 		case 'd':
 			wd = watchdog_get_by_name(optarg);
 			break;
 		case 'x':
 			return watchdog_inhibit_all();
+		case 'p':
+			do_ping = true;
+			break;
 		default:
 			return COMMAND_ERROR_USAGE;
 		}
@@ -39,6 +43,16 @@ static int do_wd(int argc, char *argv[])
 		}
 	}
 
+	if (do_ping) {
+		rc = watchdog_ping(wd);
+		if (rc) {
+			printf("watchdog ping failed: %pe (%d)\n", ERR_PTR(rc), rc);
+			return COMMAND_ERROR;
+		}
+
+		return 0;
+	}
+
 	rc = watchdog_set_timeout(wd, timeout);
 	if (rc < 0) {
 		switch (rc) {
@@ -68,6 +82,7 @@ BAREBOX_CMD_HELP_TEXT("When TIME is 0, the watchdog gets disabled,")
 BAREBOX_CMD_HELP_TEXT("Without a parameter the watchdog will be re-triggered.")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT("-d DEVICE\t", "watchdog name (default is highest priority watchdog)")
+BAREBOX_CMD_HELP_OPT("-p\t", "ping watchdog")
 BAREBOX_CMD_HELP_OPT("-x\t", "inhibit all watchdogs (i.e. disable or autopoll if possible)")
 BAREBOX_CMD_HELP_END
 
diff --git a/drivers/watchdog/wd_core.c b/drivers/watchdog/wd_core.c
index 663a6e5c75..27371672f3 100644
--- a/drivers/watchdog/wd_core.c
+++ b/drivers/watchdog/wd_core.c
@@ -57,6 +57,29 @@ int watchdog_set_timeout(struct watchdog *wd, unsigned timeout)
 }
 EXPORT_SYMBOL(watchdog_set_timeout);
 
+int watchdog_ping(struct watchdog *wd)
+{
+	int ret;
+
+	if (!wd)
+		return -ENODEV;
+
+	if (!watchdog_hw_running(wd))
+		return 0;
+
+	if (wd->ping) {
+		ret = wd->ping(wd);
+		if (!ret)
+			wd->last_ping = get_time_ns();
+	} else {
+		if (!wd->timeout_cur)
+			wd->timeout_cur = wd->timeout_max;
+		ret = watchdog_set_timeout(wd, wd->timeout_cur);
+	}
+
+	return ret;
+}
+
 static int watchdog_set_priority(struct param_d *param, void *priv)
 {
 	struct watchdog *wd = priv;
@@ -78,11 +101,8 @@ static void watchdog_poller_cb(void *priv);
 
 static void watchdog_poller_start(struct watchdog *wd)
 {
-	unsigned int timeout_s;
-
-	timeout_s = wd->timeout_cur ?: wd->timeout_max;
+	watchdog_ping(wd);
 
-	watchdog_set_timeout(wd, timeout_s);
 	poller_call_async(&wd->poller, 500 * MSECOND,
 			watchdog_poller_cb, wd);
 
diff --git a/include/watchdog.h b/include/watchdog.h
index 8084f27596..c70883fbd0 100644
--- a/include/watchdog.h
+++ b/include/watchdog.h
@@ -17,6 +17,7 @@ struct device_node;
 
 struct watchdog {
 	int (*set_timeout)(struct watchdog *, unsigned);
+	int (*ping)(struct watchdog *);
 	const char *name;
 	struct device *hwdev;
 	struct device dev;
@@ -49,6 +50,7 @@ struct watchdog *watchdog_get_default(void);
 struct watchdog *watchdog_get_by_name(const char *name);
 int watchdog_get_alias_id_from(struct watchdog *, struct device_node *);
 int watchdog_set_timeout(struct watchdog*, unsigned);
+int watchdog_ping(struct watchdog *);
 int watchdog_inhibit_all(void);
 #else
 static inline int watchdog_register(struct watchdog *w)
@@ -76,6 +78,11 @@ static inline int watchdog_set_timeout(struct watchdog*w, unsigned t)
 	return 0;
 }
 
+static inline int watchdog_ping(struct watchdog *)
+{
+	return 0;
+}
+
 static inline int watchdog_inhibit_all(void)
 {
 	return -ENOSYS;

-- 
2.39.5




More information about the barebox mailing list