From 0fe3abb9797f6bdfd1a9308e7b9fb2727b7ae262 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Sat, 5 Dec 2026 09:30:10 +0800 Subject: [PATCH] w1: masters: w1-uart: Retry a faulted HDQ link every minute Since the consecutive-timeout cap, a guarded HDQ link that misses five reads in a row stops for good, until a reboot or rebind. A gauge that goes quiet only for a while, around suspend and resume for example, then takes the battery away for the rest of the session. Let a faulted link try again: once W1_UART_HDQ_RETRY_SECS (60 s) have passed since the fault, the next reset or read goes to the wire. A good read clears the fault count; one more exhausted read, or any other error, faults the link again at once for another minute. A dead gauge so costs one short attempt a minute, and only the first fault is logged as an error. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/w1/masters/w1-uart.c | 45 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/drivers/w1/masters/w1-uart.c b/drivers/w1/masters/w1-uart.c index 7a54d883f7c536..11803fe9e51f9b 100644 --- a/drivers/w1/masters/w1-uart.c +++ b/drivers/w1/masters/w1-uart.c @@ -37,6 +37,8 @@ #define W1_UART_HDQ_TRIES 3 /* Consecutive exhausted reads before faulting a guarded HDQ link. */ #define W1_UART_HDQ_MAX_TIMEOUTS 5 +/* A faulted link tries one transfer again after this long. */ +#define W1_UART_HDQ_RETRY_SECS 60 #define W1_UART_HDQ_BREAK_MIN_US 250 #define W1_UART_HDQ_BREAK_MAX_US 500 #define W1_UART_HDQ_RECOVERY_MIN_US 150 @@ -89,6 +91,8 @@ struct w1_uart_config { * @rx_err: indicates an error in serdev-receive * @rx_byte: result byte from serdev-receive * @hdq_lock: serializes HDQ bus transactions and fault state + * @hdq_faulted_at: when the link faulted, for the next retry + * @hdq_retrying: whether the link faulted before and has not answered since * @hdq_rx_lock: protects HDQ receive state from the UART callback * @hdq_command: encoded HDQ command and expected UART echo * @hdq_response: decoded HDQ response byte @@ -103,6 +107,8 @@ struct w1_uart_device { bool hdq_faulted; bool hdq_fail_stop; unsigned int hdq_consecutive_timeouts; + unsigned long hdq_faulted_at; + bool hdq_retrying; const u8 *hdq_read_regs; size_t hdq_num_read_regs; unsigned long hdq_deadline; @@ -358,8 +364,33 @@ static void w1_uart_hdq_stop_on_error(struct w1_uart_device *w1dev, w1_uart_hdq_cancel_receive(w1dev); w1dev->hdq_faulted = true; - dev_err(&w1dev->serdev->dev, "HDQ %s failed (%d), stopping transfers\n", - operation, ret); + w1dev->hdq_faulted_at = jiffies; + if (w1dev->hdq_retrying) + dev_dbg(&w1dev->serdev->dev, "HDQ %s failed again (%d)\n", + operation, ret); + else + dev_err(&w1dev->serdev->dev, + "HDQ %s failed (%d), stopping transfers, retrying every %ds\n", + operation, ret, W1_UART_HDQ_RETRY_SECS); +} + +/* + * Whether the link is still faulted. A gauge can go quiet for a while (around + * suspend, for example), so a faulted link lets one transfer through every + * W1_UART_HDQ_RETRY_SECS. One more exhausted read faults it again at + * once; a good one clears the count. Called with hdq_lock held. + */ +static bool w1_uart_hdq_faulted(struct w1_uart_device *w1dev) +{ + if (!w1dev->hdq_faulted) + return false; + if (time_before(jiffies, w1dev->hdq_faulted_at + W1_UART_HDQ_RETRY_SECS * HZ)) + return true; + + w1dev->hdq_faulted = false; + w1dev->hdq_retrying = true; + w1dev->hdq_consecutive_timeouts = W1_UART_HDQ_MAX_TIMEOUTS - 1; + return false; } static int w1_uart_hdq_break(struct w1_uart_device *w1dev) @@ -509,7 +540,7 @@ static int w1_uart_hdq_xfer(struct w1_uart_device *w1dev, u8 command, mutex_lock(&w1dev->hdq_lock); w1_uart_hdq_cancel_receive(w1dev); - if (w1dev->hdq_faulted) { + if (w1_uart_hdq_faulted(w1dev)) { ret = -ESHUTDOWN; goto out_unlock; } @@ -581,8 +612,12 @@ static int w1_uart_hdq_xfer(struct w1_uart_device *w1dev, u8 command, } } } else { - if (!ret) + if (!ret) { + if (w1dev->hdq_retrying) + dev_info(&w1dev->serdev->dev, "HDQ link answers again\n"); + w1dev->hdq_retrying = false; w1dev->hdq_consecutive_timeouts = 0; + } w1_uart_hdq_stop_on_error(w1dev, "read", ret); } @@ -788,7 +823,7 @@ static u8 w1_uart_hdq_reset_bus(void *data) int ret; mutex_lock(&w1dev->hdq_lock); - if (w1dev->hdq_faulted) { + if (w1_uart_hdq_faulted(w1dev)) { mutex_unlock(&w1dev->hdq_lock); return 1; }