[PATCH] serial: imx: fix tx statemachine deadlock

Paul Geurts posted 1 patch 2 years, 2 months ago
There is a newer version of this series
drivers/tty/serial/imx.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years, 2 months ago
When using the serial port as RS485 port, the tx statemachine is used to
control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
TTY port is closed in the middle of a transmission (for instance during
userland application crash), imx_uart_shutdown disables the interface
and disables the Transmission Complete interrupt. afer that,
imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
by the TC interrupt. This interrupt is disabled and therefore the tx
statemachine never transitions out of SEND. The statemachine is in
deadlock now, and the TX_EN remains low, making the interface useless.

imx_uart_stop_tx now checks for incomplete transmission AND whether TC
interrupts are enabled before bailing to be retriggered. This makes sure
the state machine handling is reached, and is properly set to
WAIT_AFTER_SEND.

Signed-off-by: Paul Geurts <paul_geurts@live.nl>
---
 drivers/tty/serial/imx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 13cb78340709..90a4b7841030 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -421,13 +421,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
 	ucr1 = imx_uart_readl(sport, UCR1);
 	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
 
+	ucr4 = imx_uart_readl(sport, UCR4);
 	usr2 = imx_uart_readl(sport, USR2);
-	if (!(usr2 & USR2_TXDC)) {
+	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
 		/* The shifter is still busy, so retry once TC triggers */
 		return;
 	}
 
-	ucr4 = imx_uart_readl(sport, UCR4);
 	ucr4 &= ~UCR4_TCEN;
 	imx_uart_writel(sport, ucr4, UCR4);
 
-- 
2.20.1
Re: [PATCH] serial: imx: fix tx statemachine deadlock
Posted by Greg KH 2 years, 2 months ago
On Wed, Oct 04, 2023 at 11:57:22AM +0200, Paul Geurts wrote:
> When using the serial port as RS485 port, the tx statemachine is used to
> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> TTY port is closed in the middle of a transmission (for instance during
> userland application crash), imx_uart_shutdown disables the interface
> and disables the Transmission Complete interrupt. afer that,
> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> by the TC interrupt. This interrupt is disabled and therefore the tx
> statemachine never transitions out of SEND. The statemachine is in
> deadlock now, and the TX_EN remains low, making the interface useless.
> 
> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> interrupts are enabled before bailing to be retriggered. This makes sure
> the state machine handling is reached, and is properly set to
> WAIT_AFTER_SEND.
> 
> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
> ---
>  drivers/tty/serial/imx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

What commit id does this fix?

thanks,

greg k-h
[PATCH v2] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years ago
When using the serial port as RS485 port, the tx statemachine is used to
control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
TTY port is closed in the middle of a transmission (for instance during
userland application crash), imx_uart_shutdown disables the interface
and disables the Transmission Complete interrupt. afer that,
imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
by the TC interrupt. This interrupt is disabled and therefore the tx
statemachine never transitions out of SEND. The statemachine is in
deadlock now, and the TX_EN remains low, making the interface useless.

imx_uart_stop_tx now checks for incomplete transmission AND whether TC
interrupts are enabled before bailing to be retriggered. This makes sure
the state machine handling is reached, and is properly set to
WAIT_AFTER_SEND.

Fixes: cb1a60923609 serial: imx: implement rts delaying for rs485

Signed-off-by: Paul Geurts <paul_geurts@live.nl>
---
V1 -> V2: Added fixes line to the commit message

 drivers/tty/serial/imx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 708b9852a575..ad36c49c7898 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -415,13 +415,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
 	ucr1 = imx_uart_readl(sport, UCR1);
 	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
 
+	ucr4 = imx_uart_readl(sport, UCR4);
 	usr2 = imx_uart_readl(sport, USR2);
-	if (!(usr2 & USR2_TXDC)) {
+	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
 		/* The shifter is still busy, so retry once TC triggers */
 		return;
 	}
 
-	ucr4 = imx_uart_readl(sport, UCR4);
 	ucr4 &= ~UCR4_TCEN;
 	imx_uart_writel(sport, ucr4, UCR4);
 
-- 
2.20.1
Re: [PATCH v2] serial: imx: fix tx statemachine deadlock
Posted by Fabio Estevam 2 years ago
Hi Paul,

On Fri, Nov 24, 2023 at 9:55 AM Paul Geurts <paul_geurts@live.nl> wrote:
>
> When using the serial port as RS485 port, the tx statemachine is used to
> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> TTY port is closed in the middle of a transmission (for instance during
> userland application crash), imx_uart_shutdown disables the interface
> and disables the Transmission Complete interrupt. afer that,
> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> by the TC interrupt. This interrupt is disabled and therefore the tx
> statemachine never transitions out of SEND. The statemachine is in
> deadlock now, and the TX_EN remains low, making the interface useless.
>
> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> interrupts are enabled before bailing to be retriggered. This makes sure
> the state machine handling is reached, and is properly set to
> WAIT_AFTER_SEND.
>
> Fixes: cb1a60923609 serial: imx: implement rts delaying for rs485

One nit: the correct format for the Fixes tag is:

Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")

And no blank line is needed between the Fixes and Signed-off-by line.

> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
Re: [PATCH v2] serial: imx: fix tx statemachine deadlock
Posted by Greg KH 2 years ago
On Fri, Nov 24, 2023 at 09:58:19AM -0300, Fabio Estevam wrote:
> Hi Paul,
> 
> On Fri, Nov 24, 2023 at 9:55 AM Paul Geurts <paul_geurts@live.nl> wrote:
> >
> > When using the serial port as RS485 port, the tx statemachine is used to
> > control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> > TTY port is closed in the middle of a transmission (for instance during
> > userland application crash), imx_uart_shutdown disables the interface
> > and disables the Transmission Complete interrupt. afer that,
> > imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> > by the TC interrupt. This interrupt is disabled and therefore the tx
> > statemachine never transitions out of SEND. The statemachine is in
> > deadlock now, and the TX_EN remains low, making the interface useless.
> >
> > imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> > interrupts are enabled before bailing to be retriggered. This makes sure
> > the state machine handling is reached, and is properly set to
> > WAIT_AFTER_SEND.
> >
> > Fixes: cb1a60923609 serial: imx: implement rts delaying for rs485
> 
> One nit: the correct format for the Fixes tag is:
> 
> Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
> 
> And no blank line is needed between the Fixes and Signed-off-by line.

It's not really a "nit", our tools will complain if this is in the wrong
format as so many things depend on this being correct.

Paul, can you make a v3 with this change?

And really, this should have been v3 anyway, as the difference being you
added a changelog from v2 :)

thanks,

greg k-h
[PATCH v3] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years ago
When using the serial port as RS485 port, the tx statemachine is used to
control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
TTY port is closed in the middle of a transmission (for instance during
userland application crash), imx_uart_shutdown disables the interface
and disables the Transmission Complete interrupt. afer that,
imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
by the TC interrupt. This interrupt is disabled and therefore the tx
statemachine never transitions out of SEND. The statemachine is in
deadlock now, and the TX_EN remains low, making the interface useless.

imx_uart_stop_tx now checks for incomplete transmission AND whether TC
interrupts are enabled before bailing to be retriggered. This makes sure
the state machine handling is reached, and is properly set to
WAIT_AFTER_SEND.

Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
Signed-off-by: Paul Geurts <paul_geurts@live.nl>
---
V1 -> V2: Added fixes line to the commit message
V2 -> V3: Fixed up the fixes line by using the correct format

 drivers/tty/serial/imx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 708b9852a575..ad36c49c7898 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -415,13 +415,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
 	ucr1 = imx_uart_readl(sport, UCR1);
 	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
 
+	ucr4 = imx_uart_readl(sport, UCR4);
 	usr2 = imx_uart_readl(sport, USR2);
-	if (!(usr2 & USR2_TXDC)) {
+	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
 		/* The shifter is still busy, so retry once TC triggers */
 		return;
 	}
 
-	ucr4 = imx_uart_readl(sport, UCR4);
 	ucr4 &= ~UCR4_TCEN;
 	imx_uart_writel(sport, ucr4, UCR4);
 
-- 
2.20.1
Re: [PATCH v3] serial: imx: fix tx statemachine deadlock
Posted by Rasmus Villemoes 2 years ago
On 24/11/2023 14.11, Paul Geurts wrote:
> When using the serial port as RS485 port, the tx statemachine is used to
> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> TTY port is closed in the middle of a transmission (for instance during
> userland application crash), imx_uart_shutdown disables the interface
> and disables the Transmission Complete interrupt. afer that,
> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> by the TC interrupt. This interrupt is disabled and therefore the tx
> statemachine never transitions out of SEND. The statemachine is in
> deadlock now, and the TX_EN remains low, making the interface useless.
> 
> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> interrupts are enabled before bailing to be retriggered. This makes sure
> the state machine handling is reached, and is properly set to
> WAIT_AFTER_SEND.
> 
> Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
> Signed-off-by: Paul Geurts <paul_geurts@live.nl>

Hi Paul

Interestingly, both Eberhard (cc'ed) and I have hit similar problems in
this driver recently. See the thread
https://lore.kernel.org/lkml/20231120132256.136625-1-rasmus.villemoes@prevas.dk/
.

It is possible that this also fixes the problems I/we saw, but I can't
get around to testing until sometime next week.

Rasmus
Re: [PATCH v3] serial: imx: fix tx statemachine deadlock
Posted by Rasmus Villemoes 2 years ago
On 24/11/2023 14.37, Rasmus Villemoes wrote:
> On 24/11/2023 14.11, Paul Geurts wrote:
>> When using the serial port as RS485 port, the tx statemachine is used to
>> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
>> TTY port is closed in the middle of a transmission (for instance during
>> userland application crash), imx_uart_shutdown disables the interface
>> and disables the Transmission Complete interrupt. afer that,
>> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
>> by the TC interrupt. This interrupt is disabled and therefore the tx
>> statemachine never transitions out of SEND. The statemachine is in
>> deadlock now, and the TX_EN remains low, making the interface useless.
>>
>> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
>> interrupts are enabled before bailing to be retriggered. This makes sure
>> the state machine handling is reached, and is properly set to
>> WAIT_AFTER_SEND.
>>
>> Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
>> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
> 
> Hi Paul
> 
> Interestingly, both Eberhard (cc'ed) and I have hit similar problems in
> this driver recently. See the thread
> https://lore.kernel.org/lkml/20231120132256.136625-1-rasmus.villemoes@prevas.dk/
> .
> 
> It is possible that this also fixes the problems I/we saw, but I can't
> get around to testing until sometime next week.

This also seems to fix the problem I had when switching to rs232 and
back to rs485, and I agree that it seems to be a cleaner fix than mine.

I also tried reproducing what Eberhard reported, and I think I managed
to do that, and at least my way of reproducing the tx lockup also seems
to be fixed by this patch. Eberhard, can you test this patch in your setup?

In any case,

Tested-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

Rasmus
Re: [PATCH v3] serial: imx: fix tx statemachine deadlock
Posted by Eberhard Stoll 2 years ago

On 30.11.23 10:09, Rasmus Villemoes wrote:
> On 24/11/2023 14.37, Rasmus Villemoes wrote:
>> On 24/11/2023 14.11, Paul Geurts wrote:
>>> When using the serial port as RS485 port, the tx statemachine is used to
>>> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
>>> TTY port is closed in the middle of a transmission (for instance during
>>> userland application crash), imx_uart_shutdown disables the interface
>>> and disables the Transmission Complete interrupt. afer that,
>>> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
>>> by the TC interrupt. This interrupt is disabled and therefore the tx
>>> statemachine never transitions out of SEND. The statemachine is in
>>> deadlock now, and the TX_EN remains low, making the interface useless.
>>>
>>> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
>>> interrupts are enabled before bailing to be retriggered. This makes sure
>>> the state machine handling is reached, and is properly set to
>>> WAIT_AFTER_SEND.
>>>
>>> Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
>>> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
>>
>> Hi Paul
>>
>> Interestingly, both Eberhard (cc'ed) and I have hit similar problems in
>> this driver recently. See the thread
>> https://lore.kernel.org/lkml/20231120132256.136625-1-rasmus.villemoes@prevas.dk/
>> .
>>
>> It is possible that this also fixes the problems I/we saw, but I can't
>> get around to testing until sometime next week.
>
> This also seems to fix the problem I had when switching to rs232 and
> back to rs485, and I agree that it seems to be a cleaner fix than mine.
>
> I also tried reproducing what Eberhard reported, and I think I managed
> to do that, and at least my way of reproducing the tx lockup also seems
> to be fixed by this patch. Eberhard, can you test this patch in your setup?
>
> In any case,
>
> Tested-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
>
> Rasmus
>

Yes, it also works for my test setup!

Tested-by: Eberhard Stoll <eberhard.stoll@gmx.de>

Best regards
Eberhard
Re: [PATCH v3] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years ago
> > When using the serial port as RS485 port, the tx statemachine is used to
> > control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> > TTY port is closed in the middle of a transmission (for instance during
> > userland application crash), imx_uart_shutdown disables the interface
> > and disables the Transmission Complete interrupt. afer that,
> > imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> > by the TC interrupt. This interrupt is disabled and therefore the tx
> > statemachine never transitions out of SEND. The statemachine is in
> > deadlock now, and the TX_EN remains low, making the interface useless.
> > 
> > imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> > interrupts are enabled before bailing to be retriggered. This makes sure
> > the state machine handling is reached, and is properly set to
> > WAIT_AFTER_SEND.
> > 
> > Fixes: cb1a60923609 ("serial: imx: implement rts delaying for rs485")
> > Signed-off-by: Paul Geurts <paul_geurts@live.nl>
> 
> Hi Paul
> 
> Interestingly, both Eberhard (cc'ed) and I have hit similar problems in
> this driver recently. See the thread
> https://lore.kernel.org/lkml/20231120132256.136625-1-rasmus.villemoes@prevas.dk/
> .
> 
> It is possible that this also fixes the problems I/we saw, but I can't
> get around to testing until sometime next week.
> 
> Rasmus

This might very well be a similar issue with the tx statemachine. The
patch you mention however pulls in the state machine behaviour into normal
RS232 mode, while it was (AFAIK) specifically designed for controlling the
TXEN in RS485 mode. Therefore, Whent his patch also fixes your problem, I
would suggest to use this instead of the beforementioned patch.

br,
Paul
[PATCH v2] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years ago
When using the serial port as RS485 port, the tx statemachine is used to
control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
TTY port is closed in the middle of a transmission (for instance during
userland application crash), imx_uart_shutdown disables the interface
and disables the Transmission Complete interrupt. afer that,
imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
by the TC interrupt. This interrupt is disabled and therefore the tx
statemachine never transitions out of SEND. The statemachine is in
deadlock now, and the TX_EN remains low, making the interface useless.

imx_uart_stop_tx now checks for incomplete transmission AND whether TC
interrupts are enabled before bailing to be retriggered. This makes sure
the state machine handling is reached, and is properly set to
WAIT_AFTER_SEND.

Fixes: cb1a60923609 serial: imx: implement rts delaying for rs485

Signed-off-by: Paul Geurts <paul_geurts@live.nl>
---
 drivers/tty/serial/imx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 708b9852a575..ad36c49c7898 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -415,13 +415,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
 	ucr1 = imx_uart_readl(sport, UCR1);
 	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
 
+	ucr4 = imx_uart_readl(sport, UCR4);
 	usr2 = imx_uart_readl(sport, USR2);
-	if (!(usr2 & USR2_TXDC)) {
+	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
 		/* The shifter is still busy, so retry once TC triggers */
 		return;
 	}
 
-	ucr4 = imx_uart_readl(sport, UCR4);
 	ucr4 &= ~UCR4_TCEN;
 	imx_uart_writel(sport, ucr4, UCR4);
 
-- 
2.20.1
Re: [PATCH v2] serial: imx: fix tx statemachine deadlock
Posted by Greg KH 2 years ago
On Fri, Nov 24, 2023 at 01:38:39PM +0100, Paul Geurts wrote:
> When using the serial port as RS485 port, the tx statemachine is used to
> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> TTY port is closed in the middle of a transmission (for instance during
> userland application crash), imx_uart_shutdown disables the interface
> and disables the Transmission Complete interrupt. afer that,
> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> by the TC interrupt. This interrupt is disabled and therefore the tx
> statemachine never transitions out of SEND. The statemachine is in
> deadlock now, and the TX_EN remains low, making the interface useless.
> 
> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> interrupts are enabled before bailing to be retriggered. This makes sure
> the state machine handling is reached, and is properly set to
> WAIT_AFTER_SEND.
> 
> Fixes: cb1a60923609 serial: imx: implement rts delaying for rs485
> 
> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
> ---
>  drivers/tty/serial/imx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
> index 708b9852a575..ad36c49c7898 100644
> --- a/drivers/tty/serial/imx.c
> +++ b/drivers/tty/serial/imx.c
> @@ -415,13 +415,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
>  	ucr1 = imx_uart_readl(sport, UCR1);
>  	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
>  
> +	ucr4 = imx_uart_readl(sport, UCR4);
>  	usr2 = imx_uart_readl(sport, USR2);
> -	if (!(usr2 & USR2_TXDC)) {
> +	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
>  		/* The shifter is still busy, so retry once TC triggers */
>  		return;
>  	}
>  
> -	ucr4 = imx_uart_readl(sport, UCR4);
>  	ucr4 &= ~UCR4_TCEN;
>  	imx_uart_writel(sport, ucr4, UCR4);
>  
> -- 
> 2.20.1
> 
> 

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:

- This looks like a new version of a previously submitted patch, but you
  did not list below the --- line any changes from the previous version.
  Please read the section entitled "The canonical patch format" in the
  kernel file, Documentation/process/submitting-patches.rst for what
  needs to be done here to properly describe 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
Re: [PATCH] serial: imx: fix tx statemachine deadlock
Posted by Uwe Kleine-König 2 years, 2 months ago
Hello Paul,

On Wed, Oct 04, 2023 at 11:57:22AM +0200, Paul Geurts wrote:
> When using the serial port as RS485 port, the tx statemachine is used to
> control the RTS pin to drive the RS485 transceiver TX_EN pin. When the
> TTY port is closed in the middle of a transmission (for instance during
> userland application crash), imx_uart_shutdown disables the interface
> and disables the Transmission Complete interrupt. afer that,
> imx_uart_stop_tx bails on an incomplete transmission, to be retriggered
> by the TC interrupt. This interrupt is disabled and therefore the tx
> statemachine never transitions out of SEND. The statemachine is in
> deadlock now, and the TX_EN remains low, making the interface useless.
> 
> imx_uart_stop_tx now checks for incomplete transmission AND whether TC
> interrupts are enabled before bailing to be retriggered. This makes sure
> the state machine handling is reached, and is properly set to
> WAIT_AFTER_SEND.

Sounds reasonable.

A Fixes: line would be nice.

> Signed-off-by: Paul Geurts <paul_geurts@live.nl>
> ---
>  drivers/tty/serial/imx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
> index 13cb78340709..90a4b7841030 100644
> --- a/drivers/tty/serial/imx.c
> +++ b/drivers/tty/serial/imx.c
> @@ -421,13 +421,13 @@ static void imx_uart_stop_tx(struct uart_port *port)
>  	ucr1 = imx_uart_readl(sport, UCR1);
>  	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
>  
> +	ucr4 = imx_uart_readl(sport, UCR4);
>  	usr2 = imx_uart_readl(sport, USR2);
> -	if (!(usr2 & USR2_TXDC)) {
> +	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
>  		/* The shifter is still busy, so retry once TC triggers */
>  		return;
>  	}

So the new thing is: If the hardware is still busy sending stuff but
/dev/ttymxcX isn't open any more (i.e. .shutdown was called), the
transmitter gets disabled. I wonder if in this case disabling the
transmitter should be delayed until the shifter is empty? Or maybe this
should be handled in .shutdown, that is only disable TCEN once the
shifter is empty?

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
Re: [PATCH] serial: imx: fix tx statemachine deadlock
Posted by Paul Geurts 2 years, 2 months ago
Hi Uwe,

> Sounds reasonable.
> 
> A Fixes: line would be nice.

Yes, I will add the Fixes line for sure.

> So the new thing is: If the hardware is still busy sending stuff but
> /dev/ttymxcX isn't open any more (i.e. .shutdown was called), the
> transmitter gets disabled. I wonder if in this case disabling the
> transmitter should be delayed until the shifter is empty? Or maybe this
> should be handled in .shutdown, that is only disable TCEN once the
> shifter is empty?

Good point. I am wondering whether this would be necessary. Writing to the
TTY is blocking until the shifter is done, so closing it before the shifter
is done is an error condition anyway, right? So if it already is an error
condition, the data is already unreliable. Making sure the shifter is
empty on shutdown would mean waiting for it, or doing it asynchronously,
which IMO is both not a great idea. Maybe we can just dump the buffer on
shutdown but I don't know whether the IP can do that.

Let me know what you think.

br,
Paul