Skip to contentSkip to conversation

tty: serial: 8250_omap: fix tx with dma

PATCH Superseded Mans Rullgard 4 participants 4 messages Apr 30, 2025 – May 6, 2025 TTY layer and serial drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: serial: 8250_omap: fix tx with dma

Commit 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo") introduced two errors in the TX DMA handling for 8250_omap.

Firstly, kfifo_dma_out_prepare_mapped() needs a scatterlist with two entries whereas only one is provided. The same error was fixed for 8250_dma in 59449c9dbdaa ("tty: serial: 8250_dma: use sgl with 2 nents to take care of buffer wrap").

Secondly, when the OMAP_DMA_TX_KICK flag is set, one byte is pulled from the kfifo and emitted directly in order to start the DMA. This is done without updating DMA tx_size which leads to uart_xmit_advance() called in the DMA complete callback advancing the kfifo by one too much.

In practice, transmitting N bytes has been seen to result in the last N-1 bytes being sent repeatedly.

This change fixes both problems.

Fixes 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
---
 drivers/tty/serial/8250/8250_omap.c | 35 +++++++++++++++--------------
 1 file changed, 18 insertions(+), 17 deletions(-)
drivers/tty/serial/8250/8250_omap.c +18 -17
index f1aee915bc02..84a2f013015e 100644
@@ -1152,9 +1152,11 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 	struct omap8250_priv		*priv = p->port.private_data;
 	struct tty_port			*tport = &p->port.state->port;
 	struct dma_async_tx_descriptor	*desc;
-	struct scatterlist sg;
+	struct scatterlist *sg;
+	struct scatterlist sgl[2];
 	int skip_byte = -1;
 	int ret;
+	int i;
 
 	if (dma->tx_running)
 		return 0;
@@ -1173,16 +1175,6 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 		return 0;
 	}
 
-	sg_init_table(&sg, 1);
-	ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-					   UART_XMIT_SIZE, dma->tx_addr);
-	if (ret != 1) {
-		serial8250_clear_THRI(p);
-		return 0;
-	}
-
-	dma->tx_size = sg_dma_len(&sg);
-
 	if (priv->habit & OMAP_DMA_TX_KICK) {
 		unsigned char c;
 		u8 tx_lvl;
@@ -1207,7 +1199,7 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 			ret = -EBUSY;
 			goto err;
 		}
-		if (dma->tx_size < 4) {
+		if (kfifo_len(&tport->xmit_fifo) < 4) {
 			ret = -EINVAL;
 			goto err;
 		}
@@ -1216,12 +1208,19 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 			goto err;
 		}
 		skip_byte = c;
-		/* now we need to recompute due to kfifo_get */
-		kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-				UART_XMIT_SIZE, dma->tx_addr);
 	}
 
-	desc = dmaengine_prep_slave_sg(dma->txchan, &sg, 1, DMA_MEM_TO_DEV,
+	sg_init_table(sgl, ARRAY_SIZE(sgl));
+
+	ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, sgl, ARRAY_SIZE(sgl),
+					   UART_XMIT_SIZE, dma->tx_addr);
+
+	dma->tx_size = 0;
+
+	for_each_sg(sgl, sg, ret, i)
+		dma->tx_size += sg_dma_len(sg);
+
+	desc = dmaengine_prep_slave_sg(dma->txchan, sgl, ret, DMA_MEM_TO_DEV,
 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 	if (!desc) {
 		ret = -EBUSY;
@@ -1248,8 +1247,10 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 err:
 	dma->tx_err = 1;
 out_skip:
-	if (skip_byte >= 0)
+	if (skip_byte >= 0) {
 		serial_out(p, UART_TX, skip_byte);
+		p->port.icount.tx++;
+	}
 	return ret;
 }
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 106 lines · Mans Rullgard

Commit 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo") introduced two errors in the TX DMA handling for 8250_omap.

Firstly, kfifo_dma_out_prepare_mapped() needs a scatterlist with two entries whereas only one is provided. The same error was fixed for 8250_dma in 59449c9dbdaa ("tty: serial: 8250_dma: use sgl with 2 nents to take care of buffer wrap").

Secondly, when the OMAP_DMA_TX_KICK flag is set, one byte is pulled from the kfifo and emitted directly in order to start the DMA. This is done without updating DMA tx_size which leads to uart_xmit_advance() called in the DMA complete callback advancing the kfifo by one too much.

In practice, transmitting N bytes has been seen to result in the last N-1 bytes being sent repeatedly.

This change fixes both problems.

Fixes 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
---
 drivers/tty/serial/8250/8250_omap.c | 35 +++++++++++++++--------------
 1 file changed, 18 insertions(+), 17 deletions(-)
drivers/tty/serial/8250/8250_omap.c +18 -17
index f1aee915bc02..84a2f013015e 100644
@@ -1152,9 +1152,11 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 	struct omap8250_priv		*priv = p->port.private_data;
 	struct tty_port			*tport = &p->port.state->port;
 	struct dma_async_tx_descriptor	*desc;
-	struct scatterlist sg;
+	struct scatterlist *sg;
+	struct scatterlist sgl[2];
 	int skip_byte = -1;
 	int ret;
+	int i;
 
 	if (dma->tx_running)
 		return 0;
@@ -1173,16 +1175,6 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 		return 0;
 	}
 
-	sg_init_table(&sg, 1);
-	ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-					   UART_XMIT_SIZE, dma->tx_addr);
-	if (ret != 1) {
-		serial8250_clear_THRI(p);
-		return 0;
-	}
-
-	dma->tx_size = sg_dma_len(&sg);
-
 	if (priv->habit & OMAP_DMA_TX_KICK) {
 		unsigned char c;
 		u8 tx_lvl;
@@ -1207,7 +1199,7 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 			ret = -EBUSY;
 			goto err;
 		}
-		if (dma->tx_size < 4) {
+		if (kfifo_len(&tport->xmit_fifo) < 4) {
 			ret = -EINVAL;
 			goto err;
 		}
@@ -1216,12 +1208,19 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 			goto err;
 		}
 		skip_byte = c;
-		/* now we need to recompute due to kfifo_get */
-		kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-				UART_XMIT_SIZE, dma->tx_addr);
 	}
 
-	desc = dmaengine_prep_slave_sg(dma->txchan, &sg, 1, DMA_MEM_TO_DEV,
+	sg_init_table(sgl, ARRAY_SIZE(sgl));
+
+	ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, sgl, ARRAY_SIZE(sgl),
+					   UART_XMIT_SIZE, dma->tx_addr);
+
+	dma->tx_size = 0;
+
+	for_each_sg(sgl, sg, ret, i)
+		dma->tx_size += sg_dma_len(sg);
+
+	desc = dmaengine_prep_slave_sg(dma->txchan, sgl, ret, DMA_MEM_TO_DEV,
 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 	if (!desc) {
 		ret = -EBUSY;
@@ -1248,8 +1247,10 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 err:
 	dma->tx_err = 1;
 out_skip:
-	if (skip_byte >= 0)
+	if (skip_byte >= 0) {
 		serial_out(p, UART_TX, skip_byte);
+		p->port.icount.tx++;
+	}
 	return ret;
 }
 
Signature · 1 line
2.49.0

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#2 lore raw
In reply to Mans Rullgard

CC omap

Quoted 104 lines · Mans Rullgard

Commit 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo") introduced two errors in the TX DMA handling for 8250_omap.

Firstly, kfifo_dma_out_prepare_mapped() needs a scatterlist with two entries whereas only one is provided. The same error was fixed for 8250_dma in 59449c9dbdaa ("tty: serial: 8250_dma: use sgl with 2 nents to take care of buffer wrap").

Secondly, when the OMAP_DMA_TX_KICK flag is set, one byte is pulled from the kfifo and emitted directly in order to start the DMA. This is done without updating DMA tx_size which leads to uart_xmit_advance() called in the DMA complete callback advancing the kfifo by one too much.

In practice, transmitting N bytes has been seen to result in the last N-1 bytes being sent repeatedly.

This change fixes both problems.

Fixes 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
---
 drivers/tty/serial/8250/8250_omap.c | 35 +++++++++++++++--------------
 1 file changed, 18 insertions(+), 17 deletions(-)
drivers/tty/serial/8250/8250_omap.c +18 -18
index f1aee915bc02..84a2f013015e 100644
@@ -1152,9 +1152,11 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
        struct omap8250_priv            *priv = p->port.private_data;
        struct tty_port                 *tport = &p->port.state->port;
        struct dma_async_tx_descriptor  *desc;
-       struct scatterlist sg;
+       struct scatterlist *sg;
+       struct scatterlist sgl[2];
        int skip_byte = -1;
        int ret;
+       int i;
 
        if (dma->tx_running)
                return 0;
@@ -1173,16 +1175,6 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
                return 0;
        }
 
-       sg_init_table(&sg, 1);
-       ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-                                          UART_XMIT_SIZE, dma->tx_addr);
-       if (ret != 1) {
-               serial8250_clear_THRI(p);
-               return 0;
-       }
-
-       dma->tx_size = sg_dma_len(&sg);
-
        if (priv->habit & OMAP_DMA_TX_KICK) {
                unsigned char c;
                u8 tx_lvl;
@@ -1207,7 +1199,7 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
                        ret = -EBUSY;
                        goto err;
                }
-               if (dma->tx_size < 4) {
+               if (kfifo_len(&tport->xmit_fifo) < 4) {
                        ret = -EINVAL;
                        goto err;
                }
@@ -1216,12 +1208,19 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
                        goto err;
                }
                skip_byte = c;
-               /* now we need to recompute due to kfifo_get */
-               kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1,
-                               UART_XMIT_SIZE, dma->tx_addr);
        }
 
-       desc = dmaengine_prep_slave_sg(dma->txchan, &sg, 1, DMA_MEM_TO_DEV,
+       sg_init_table(sgl, ARRAY_SIZE(sgl));
+
+       ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, sgl, ARRAY_SIZE(sgl),
+                                          UART_XMIT_SIZE, dma->tx_addr);
+
+       dma->tx_size = 0;
+
+       for_each_sg(sgl, sg, ret, i)
+               dma->tx_size += sg_dma_len(sg);
+
+       desc = dmaengine_prep_slave_sg(dma->txchan, sgl, ret, DMA_MEM_TO_DEV,
                        DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
        if (!desc) {
                ret = -EBUSY;
@@ -1248,8 +1247,10 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
 err:
        dma->tx_err = 1;
 out_skip:
-       if (skip_byte >= 0)
+       if (skip_byte >= 0) {
                serial_out(p, UART_TX, skip_byte);
+               p->port.icount.tx++;
+       }
        return ret;
 }
 
--
Quoted 1 line

2.49.0

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
maintainer
#3 lore raw
In reply to Mans Rullgard
Quoted 7 lines · Mans Rullgard

Commit 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo") introduced two errors in the TX DMA handling for 8250_omap.

Firstly, kfifo_dma_out_prepare_mapped() needs a scatterlist with two entries whereas only one is provided. The same error was fixed for 8250_dma in 59449c9dbdaa ("tty: serial: 8250_dma: use sgl with 2 nents to take care of buffer wrap").

It's not an error. This is how it used to work since ever. Providing two is an optimization, right?

Quoted 4 lines

Secondly, when the OMAP_DMA_TX_KICK flag is set, one byte is pulled from the kfifo and emitted directly in order to start the DMA.

This is done without updating DMA tx_size

Ah, that's an error, of course.

Quoted 7 lines

which leads to uart_xmit_advance() called
in the DMA complete callback advancing the kfifo by one too much.

In practice, transmitting N bytes has been seen to result in the last N-1 bytes being sent repeatedly.

This change fixes both problems.

I am not sure you want to mix fixups with features.

Quoted 2 lines
Fixes 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")

...

drivers/tty/serial/8250/8250_omap.c static int omap_8250_tx_dma(struct uart_8250_port *p) · line 1252

Quoted 8 lines
drivers/tty/serial/8250/8250_omap.c +2 -1
@@ -1248,8 +1247,10 @@ static int omap_8250_tx_dma(struct uart_8250_port *p)
  err:
  	dma->tx_err = 1;
  out_skip:
-	if (skip_byte >= 0)
+	if (skip_byte >= 0) {
  		serial_out(p, UART_TX, skip_byte);
+		p->port.icount.tx++;

This is unrelated (but correct) too.

thanks,

Signature · 2 lines
js
suse labs