[PATCH v2 2/4] serial: max310x: re-check the TX queue before the after-send hold
From: Tapio Reijonen
Date: Tue Nov 17 2026 - 01:14:26 EST
max310x_delayed_stop_tx() decides that a software-timed RS-485 envelope
is over when the transmit queue is empty and the chip's TX FIFO is
empty. It tests the queue first, then reads TXFIFOLVL over the bus, and
only then takes the port lock and arms the after-send hold. The bus read
sleeps, under load for tens of milliseconds, and write() queues data
under the port lock in that window. start_tx() finds the envelope in
MAX310X_TX_SEND and does nothing but reschedule tx_work.
When the stop routine resumes it sees an empty chip FIFO and arms the
hold on a queue that was empty before the read. The hold expires, the
RTS release is scheduled, the rescheduled tx_work pushes the new data
into the chip FIFO, and RTS drops while the chip is still shifting it
out. The tail of the write leaves through a disabled transceiver. No
FIFO overflows, so no counter moves; the loss is silent.
Seen on a MAX14830 behind a slow SPI path with 20 ms of before-send
delay: 25-byte writes every 20 ms lost 1600 to 2200 bytes per port in
five minutes in RS-485 mode and nothing in RS-232 mode under the same
traffic, overrun counters equal; 96-byte writes every 60 ms lost
partial chunks of 14 to 75 bytes a few times per run.
Re-check the queue under the port lock before arming the hold. Data
queued before the check keeps the envelope open and the pending tx_work
ends it once that data has drained; data queued after the hold is
armed is already handled by start_tx(), which cancels the hold.
Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>
---
drivers/tty/serial/max310x.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index e6a740295b96653281a92f8d94999f06c2c74685..7ddfe8773faf9bc51621c49fa5d8e7d6b1e4bc34 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -897,6 +897,14 @@ static void max310x_delayed_stop_tx(struct uart_port *port)
READ_ONCE(one->tx_state) != MAX310X_TX_SEND)
return;
+ /*
+ * Data queued while TXFIFOLVL was read: start_tx() saw SEND and only
+ * rescheduled tx_work, so arming the hold now would drop RTS under it.
+ */
+ if (!kfifo_is_empty(&port->state->port.xmit_fifo) &&
+ !uart_tx_stopped(port))
+ return;
+
if (!hrtimer_active(&one->tx_delay_tmr)) {
/*
* Add one character for the byte still in the shift register -
--
2.47.3