[PATCH v2 3/4] serial: max310x: hold TX data until the software RTS send phase

From: Tapio Reijonen

Date: Tue Nov 17 2026 - 01:14:36 EST


On the software-timed RS-485 path, start_tx() begins an envelope by
setting MAX310X_TX_WAIT_BEFORE_SEND and queuing the RTS worker; the
data may only go to the chip once the before-send timer has moved the
envelope to MAX310X_TX_SEND and scheduled tx_work. Nothing enforced
that: max310x_handle_tx() pushed whatever was queued whenever it ran.

The chip latches its TX-empty interrupt when the last byte of the
previous envelope leaves the FIFO. Under load the IRQ thread reads that
bit late, after the next write has started a new envelope, and
schedules tx_work for it. That run found the new data and pushed it
into the FIFO while RTS was still down or the setup delay still
running; the chip transmitted at once, the head of the write left
through a disabled transceiver and the receiver saw the rest misframed.

Seen on a MAX14830 with a 20 ms before-send delay: with 25-byte writes
every 20 ms, 33 to 88 such losses per port in five minutes remained
after the after-send race was fixed, several of them as misframed
bytes.

Hold the data while the envelope is not in its send phase. The
before-send timer schedules tx_work when it moves the envelope to
MAX310X_TX_SEND, and so does a write arriving during a send, so the
data leaves as soon as RTS is up.

One more path enters MAX310X_TX_SEND, and it does not schedule
tx_work: a set_termios() that moves a running transfer from chip-timed
to driver-timed RTS adopts it in max310x_adopt_hw_tx(). A tx_work that
runs after the path has changed and before the adoption is now held,
and with the chip FIFO empty nothing sends the queued data until the
serial core's runtime PM calls start_tx() about 0.5 s later. With only
the hold in place, 10 of 338 baud rate changes made near the end of a
chip FIFO load stalled for 370 to 490 ms. Restart the transmission at
the end of set_termios() the way rs_work does after an RS485
reconfigure, through a helper for both. The helper leaves stopped
output alone, as serial core does.

A software envelope between two FIFO loads looks the same as an
adopted transfer, so a set_termios() that lands there restarts it as
well: RTS stays asserted and the rest of the data waits for one more
before-send delay. rs_work has always done that for TIOCSRS485.

The comment in the IRQ handler said that a stale TX-empty interrupt
cannot push data during the setup delay; correct it. The hold does
not apply to the hardware RTS path, which does not use tx_state.

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 | 72 ++++++++++++++++++++++++++++++++------------
1 file changed, 52 insertions(+), 20 deletions(-)

diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index 7ddfe8773faf9bc51621c49fa5d8e7d6b1e4bc34..d92bf241558862cd83073c1d12ec9f0aaf88f090 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -942,6 +942,16 @@ static void max310x_handle_tx(struct uart_port *port)
return;
}

+ /*
+ * Outside the send phase of a software envelope RTS is down, still
+ * in its setup delay, or about to drop: a late TX-empty interrupt
+ * must not push the data out now. start_tx() opens an envelope and
+ * tmr_tx() reschedules us at MAX310X_TX_SEND.
+ */
+ if (READ_ONCE(one->sw_rts_during_tx) &&
+ READ_ONCE(one->tx_state) != MAX310X_TX_SEND)
+ return;
+
while (!kfifo_is_empty(&tport->xmit_fifo)) {
unsigned int txlen, to_send;
unsigned long flags;
@@ -1077,8 +1087,8 @@ static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno)
if (rxlen)
max310x_handle_rx(port, rxlen);
/*
- * TXEMPTY latches on the FIFO becoming empty, so a stale
- * interrupt cannot pump data during an RTS setup delay.
+ * A TX-empty read late can belong to the previous envelope;
+ * max310x_handle_tx() holds the data until the send phase.
*/
if (ists & MAX310X_IRQ_TXEMPTY_BIT)
schedule_work(&s->p[portno].tx_work);
@@ -1314,6 +1324,28 @@ static void max310x_set_rts_ctl_params(struct max310x_one *one)
MAX310X_IRDA_RTSINVERT_BIT, irda);
}

+/*
+ * A reconfigure is applied: restart a transmission that it left queued, on
+ * the path that is now configured. A send state without chip data has
+ * nothing in flight: restart it as a fresh envelope so the before-send delay
+ * is honoured. start_tx() can drop and retake the lock through the uart_port
+ * API - callers must take it with uart_port_lock*(), not a spinlock guard.
+ */
+static void max310x_restart_tx(struct max310x_one *one, bool chip_tx_empty)
+{
+ struct uart_port *port = &one->port;
+
+ lockdep_assert_held(&port->lock);
+
+ if (!port->x_char && (kfifo_is_empty(&port->state->port.xmit_fifo) ||
+ uart_tx_stopped(port)))
+ return;
+
+ if (chip_tx_empty && READ_ONCE(one->tx_state) == MAX310X_TX_SEND)
+ WRITE_ONCE(one->tx_state, MAX310X_TX_OFF);
+ max310x_start_tx(port);
+}
+
static void max310x_break_ctl(struct uart_port *port, int break_state)
{
struct max310x_one *one = to_max310x_port(port);
@@ -1363,7 +1395,10 @@ static void max310x_set_termios(struct uart_port *port,
const struct ktermios *old)
{
unsigned int frame_bits = tty_get_frame_size(termios->c_cflag);
+ struct max310x_one *one = to_max310x_port(port);
unsigned int lcr = 0, flow = 0;
+ unsigned long flags;
+ bool chip_tx_empty;
int baud;

/* Mask termios capabilities we don't support */
@@ -1482,10 +1517,19 @@ static void max310x_set_termios(struct uart_port *port,
* taking over an in-flight transfer arms the after-send hold from
* the character time.
*/
- to_max310x_port(port)->baud = baud;
- to_max310x_port(port)->char_time_us =
- DIV_ROUND_UP(USEC_PER_SEC * frame_bits, baud);
- max310x_set_rts_ctl_params(to_max310x_port(port));
+ one->baud = baud;
+ one->char_time_us = DIV_ROUND_UP(USEC_PER_SEC * frame_bits, baud);
+ max310x_set_rts_ctl_params(one);
+
+ /*
+ * A tx_work that ran while the RTS path was switched above has
+ * returned without sending, and nothing else pushes the data it left.
+ */
+ chip_tx_empty = !max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
+
+ uart_port_lock_irqsave(port, &flags);
+ max310x_restart_tx(one, chip_tx_empty);
+ uart_port_unlock_irqrestore(port, flags);
}

static void max310x_rs_proc(struct work_struct *ws)
@@ -1518,26 +1562,14 @@ static void max310x_rs_proc(struct work_struct *ws)
/*
* The configuration is applied: release any TX that start_tx()
* deferred while the reconfigure was pending, now on the right
- * path. start_tx() can drop and retake the lock through the
- * uart_port API - a raw spinlock guard here would unbalance it.
+ * path.
*/
chip_tx_empty = !max310x_port_read(&one->port,
MAX310X_TXFIFOLVL_REG);

uart_port_lock_irqsave(&one->port, &flags);
one->rs485_pending = false;
- if (one->port.x_char ||
- !kfifo_is_empty(&one->port.state->port.xmit_fifo)) {
- /*
- * A send state adopted without chip data is the deferred
- * write itself: restart it as a fresh envelope so the
- * before-send delay is honoured.
- */
- if (chip_tx_empty &&
- READ_ONCE(one->tx_state) == MAX310X_TX_SEND)
- WRITE_ONCE(one->tx_state, MAX310X_TX_OFF);
- max310x_start_tx(&one->port);
- }
+ max310x_restart_tx(one, chip_tx_empty);
uart_port_unlock_irqrestore(&one->port, flags);
}


--
2.47.3