diff --git a/drivers/w1/masters/w1-uart.c b/drivers/w1/masters/w1-uart.c index 304af3a4808960..7a54d883f7c536 100644 --- a/drivers/w1/masters/w1-uart.c +++ b/drivers/w1/masters/w1-uart.c @@ -35,6 +35,8 @@ /* A transaction takes a few ms; do not hold the bus for long on a miss. */ #define W1_UART_HDQ_TIMEOUT msecs_to_jiffies(100) #define W1_UART_HDQ_TRIES 3 +/* Consecutive exhausted reads before faulting a guarded HDQ link. */ +#define W1_UART_HDQ_MAX_TIMEOUTS 5 #define W1_UART_HDQ_BREAK_MIN_US 250 #define W1_UART_HDQ_BREAK_MAX_US 500 #define W1_UART_HDQ_RECOVERY_MIN_US 150 @@ -86,18 +88,21 @@ struct w1_uart_config { * @rx_mutex: mutex to protect rx_err and rx_byte * @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_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 * @hdq_rx_bit: number of accepted echo and response symbols * @hdq_receiving: whether an HDQ transaction is receiving symbols * @hdq_response_valid: whether @hdq_response is ready for the W1 client + * @hdq_rx_on_time: whether the completed HDQ response arrived before @hdq_deadline * @hdq_raw: first symbols received in the current HDQ cycle, for debugging * @hdq_raw_len: number of symbols in @hdq_raw */ struct w1_uart_device { bool hdq_faulted; bool hdq_fail_stop; + unsigned int hdq_consecutive_timeouts; const u8 *hdq_read_regs; size_t hdq_num_read_regs; unsigned long hdq_deadline; @@ -106,6 +111,7 @@ struct w1_uart_device { struct w1_bus_master bus; enum w1_uart_mode mode; struct mux_state *hdq_mux; + struct mutex hdq_lock; struct w1_uart_config cfg_reset; struct w1_uart_config cfg_touch_0; @@ -126,6 +132,7 @@ struct w1_uart_device { u8 hdq_rx_bit; bool hdq_receiving; bool hdq_response_valid; + bool hdq_rx_on_time; bool voltage_diagnostic; unsigned long diagnostic_attempted; u8 hdq_raw[W1_UART_HDQ_RAW_MAX]; @@ -339,6 +346,7 @@ static void w1_uart_hdq_cancel_receive(struct w1_uart_device *w1dev) spin_lock_irqsave(&w1dev->hdq_rx_lock, flags); w1dev->hdq_receiving = false; w1dev->hdq_response_valid = false; + w1dev->hdq_rx_on_time = false; spin_unlock_irqrestore(&w1dev->hdq_rx_lock, flags); } @@ -392,14 +400,21 @@ static unsigned long w1_uart_hdq_wait_budget(struct w1_uart_device *w1dev, return min(legacy, w1dev->hdq_deadline - now); } -static int w1_uart_hdq_xfer_once(struct w1_uart_device *w1dev) +static int w1_uart_hdq_xfer_once(struct w1_uart_device *w1dev, + bool *wire_timeout) { struct serdev_device *serdev = w1dev->serdev; + u8 raw[W1_UART_HDQ_RAW_MAX]; unsigned long flags; unsigned long timeout; + bool waited = false; + bool on_time, late_rx; ssize_t written; + u8 len, bit; int ret; + *wire_timeout = false; + if (!w1_uart_hdq_wait_budget(w1dev, HZ)) return -ETIMEDOUT; ret = w1_uart_hdq_break(w1dev); @@ -413,6 +428,7 @@ static int w1_uart_hdq_xfer_once(struct w1_uart_device *w1dev) w1dev->hdq_raw_len = 0; w1dev->hdq_receiving = true; w1dev->hdq_response_valid = false; + w1dev->hdq_rx_on_time = false; spin_unlock_irqrestore(&w1dev->hdq_rx_lock, flags); timeout = w1_uart_hdq_wait_budget(w1dev, HZ); @@ -432,28 +448,27 @@ static int w1_uart_hdq_xfer_once(struct w1_uart_device *w1dev) } timeout = w1_uart_hdq_wait_budget(w1dev, HZ); - if (!timeout) { - ret = -ETIMEDOUT; - goto cancel_receive; - } - serdev_device_wait_until_sent(serdev, timeout); + if (timeout) + serdev_device_wait_until_sent(serdev, timeout); timeout = w1_uart_hdq_wait_budget(w1dev, W1_UART_HDQ_TIMEOUT); - if (!timeout) { - ret = -ETIMEDOUT; - goto cancel_receive; + if (timeout) { + wait_for_completion_timeout(&w1dev->rx_byte_received, timeout); + waited = true; } - timeout = wait_for_completion_timeout(&w1dev->rx_byte_received, - timeout); - if (!timeout || !w1_uart_hdq_wait_budget(w1dev, HZ)) { - u8 raw[W1_UART_HDQ_RAW_MAX]; - u8 len, bit; - spin_lock_irqsave(&w1dev->hdq_rx_lock, flags); + spin_lock_irqsave(&w1dev->hdq_rx_lock, flags); + on_time = w1dev->hdq_response_valid && w1dev->hdq_rx_on_time; + late_rx = w1dev->hdq_response_valid && !w1dev->hdq_rx_on_time; + if (!on_time) { len = w1dev->hdq_raw_len; bit = w1dev->hdq_rx_bit; memcpy(raw, w1dev->hdq_raw, len); - spin_unlock_irqrestore(&w1dev->hdq_rx_lock, flags); + } + spin_unlock_irqrestore(&w1dev->hdq_rx_lock, flags); + + if (!on_time) { + *wire_timeout = waited || late_rx; /* What the line gave back: no echo, a bent echo, or no answer */ dev_dbg(&serdev->dev, "HDQ cycle: %u of %u symbols accepted, %u received: %*ph\n", @@ -488,20 +503,26 @@ static int w1_uart_hdq_xfer(struct w1_uart_device *w1dev, u8 command, { unsigned int bit; unsigned int try; + bool all_wire_timeouts = true; int ret; int deselect_ret; + mutex_lock(&w1dev->hdq_lock); w1_uart_hdq_cancel_receive(w1dev); - if (w1dev->hdq_faulted) - return -ESHUTDOWN; - if (!w1_uart_hdq_command_allowed(w1dev, command)) - return -EACCES; + if (w1dev->hdq_faulted) { + ret = -ESHUTDOWN; + goto out_unlock; + } + if (!w1_uart_hdq_command_allowed(w1dev, command)) { + ret = -EACCES; + goto out_unlock; + } if (w1dev->hdq_mux) { ret = mux_state_select(w1dev->hdq_mux); if (ret) { w1_uart_hdq_stop_on_error(w1dev, "mux select", ret); - return ret; + goto out_unlock; } } @@ -516,12 +537,15 @@ static int w1_uart_hdq_xfer(struct w1_uart_device *w1dev, u8 command, * starve the retries. */ for (try = 0; try < tries; try++) { + bool wire_timeout; + if (w1dev->hdq_budget_ms) w1dev->hdq_deadline = jiffies + msecs_to_jiffies(w1dev->hdq_budget_ms); - ret = w1_uart_hdq_xfer_once(w1dev); + ret = w1_uart_hdq_xfer_once(w1dev, &wire_timeout); if (ret != -ETIMEDOUT) break; + all_wire_timeouts &= wire_timeout; dev_dbg(&w1dev->serdev->dev, "HDQ command %02x timed out (try %u/%u)\n", command, try + 1, tries); @@ -536,14 +560,34 @@ static int w1_uart_hdq_xfer(struct w1_uart_device *w1dev, u8 command, } if (ret == -ETIMEDOUT) { - if (w1dev->hdq_fail_stop) - dev_warn_ratelimited(&w1dev->serdev->dev, - "HDQ read %02x timed out after %u %s\n", - command, tries, - tries == 1 ? "try" : "tries"); + if (w1dev->hdq_fail_stop) { + if (!all_wire_timeouts) { + dev_warn_ratelimited(&w1dev->serdev->dev, + "HDQ read %02x exhausted host budget after %u %s\n", + command, tries, + tries == 1 ? "try" : "tries"); + } else { + w1dev->hdq_consecutive_timeouts++; + if (w1dev->hdq_consecutive_timeouts >= + W1_UART_HDQ_MAX_TIMEOUTS) + w1_uart_hdq_stop_on_error(w1dev, "read", ret); + else + dev_warn_ratelimited(&w1dev->serdev->dev, + "HDQ read %02x timed out after %u %s (%u/%u)\n", + command, tries, + tries == 1 ? "try" : "tries", + w1dev->hdq_consecutive_timeouts, + W1_UART_HDQ_MAX_TIMEOUTS); + } + } } else { + if (!ret) + w1dev->hdq_consecutive_timeouts = 0; w1_uart_hdq_stop_on_error(w1dev, "read", ret); } + +out_unlock: + mutex_unlock(&w1dev->hdq_lock); return ret; } @@ -640,6 +684,8 @@ static size_t w1_uart_serdev_receive_buf(struct serdev_device *serdev, if (w1dev->hdq_rx_bit == W1_UART_HDQ_RX_BITS) { w1dev->hdq_receiving = false; w1dev->hdq_response_valid = true; + w1dev->hdq_rx_on_time = !w1dev->hdq_budget_ms || + time_before(jiffies, w1dev->hdq_deadline); complete(&w1dev->rx_byte_received); } } @@ -741,13 +787,17 @@ static u8 w1_uart_hdq_reset_bus(void *data) struct w1_uart_device *w1dev = data; int ret; - if (w1dev->hdq_faulted) + mutex_lock(&w1dev->hdq_lock); + if (w1dev->hdq_faulted) { + mutex_unlock(&w1dev->hdq_lock); return 1; + } w1_uart_hdq_cancel_receive(w1dev); if (w1dev->hdq_mux) { ret = mux_state_select(w1dev->hdq_mux); if (ret) { w1_uart_hdq_stop_on_error(w1dev, "reset mux select", ret); + mutex_unlock(&w1dev->hdq_lock); return 1; } } @@ -757,6 +807,7 @@ static u8 w1_uart_hdq_reset_bus(void *data) ret = -EIO; w1_uart_hdq_stop_on_error(w1dev, "reset", ret); + mutex_unlock(&w1dev->hdq_lock); return ret ? 1 : 0; } @@ -860,6 +911,7 @@ static int w1_uart_probe(struct serdev_device *serdev) init_completion(&w1dev->rx_byte_received); mutex_init(&w1dev->rx_mutex); + mutex_init(&w1dev->hdq_lock); spin_lock_init(&w1dev->hdq_rx_lock); serdev_device_set_drvdata(serdev, w1dev);