linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: ocores: generate stop condition after timeout in polling mode
@ 2023-02-20 16:16 Matthias Schiffer
  2023-04-12  9:49 ` Matthias Schiffer
  2023-04-12 12:43 ` Andrew Lunn
  0 siblings, 2 replies; 4+ messages in thread
From: Matthias Schiffer @ 2023-02-20 16:16 UTC (permalink / raw)
  To: Peter Korsgaard, Andrew Lunn
  Cc: Federico Vaga, Wolfram Sang, linux-i2c, linux-kernel, linux

From: Gregor Herburger <gregor.herburger@tq-group.com>

In polling mode, no stop condition is generated after a timeout. This
causes SCL to remain low and thereby block the bus. If this happens
during a transfer it can cause slaves to misinterpret the subsequent
transfer and return wrong values.

To solve this, pass the ETIMEDOUT error up from ocores_process_polling()
instead of setting STATE_ERROR directly. The caller is adjusted to call
ocores_process_timeout() on error both in polling and in IRQ mode, which
will set STATE_ERROR and generate a stop condition.

Fixes: 69c8c0c0efa8 ("i2c: ocores: add polling interface")
Signed-off-by: Gregor Herburger <gregor.herburger@tq-group.com>
Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>
---
 drivers/i2c/busses/i2c-ocores.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
index a0af027db04c1..28bcda3f7040a 100644
--- a/drivers/i2c/busses/i2c-ocores.c
+++ b/drivers/i2c/busses/i2c-ocores.c
@@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
  * ocores_isr(), we just add our polling code around it.
  *
  * It can run in atomic context
+ *
+ * Return: 0 on success, -ETIMEDOUT on timeout
  */
-static void ocores_process_polling(struct ocores_i2c *i2c)
+static int ocores_process_polling(struct ocores_i2c *i2c)
 {
 	while (1) {
 		irqreturn_t ret;
 		int err;
 
 		err = ocores_poll_wait(i2c);
-		if (err) {
-			i2c->state = STATE_ERROR;
-			break; /* timeout */
-		}
+		if (err)
+			return err;
 
 		ret = ocores_isr(-1, i2c);
 		if (ret == IRQ_NONE)
@@ -364,6 +364,8 @@ static void ocores_process_polling(struct ocores_i2c *i2c)
 					break;
 		}
 	}
+
+	return 0;
 }
 
 static int ocores_xfer_core(struct ocores_i2c *i2c,
@@ -387,16 +389,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c,
 	oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
 	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
 
-	if (polling) {
-		ocores_process_polling(i2c);
-	} else {
+	if (polling)
+		ret = ocores_process_polling(i2c);
+	else
 		ret = wait_event_timeout(i2c->wait,
 					 (i2c->state == STATE_ERROR) ||
-					 (i2c->state == STATE_DONE), HZ);
-		if (ret == 0) {
-			ocores_process_timeout(i2c);
-			return -ETIMEDOUT;
-		}
+					 (i2c->state == STATE_DONE), HZ) ?
+						0 : -ETIMEDOUT;
+	if (ret) {
+		ocores_process_timeout(i2c);
+		return ret;
 	}
 
 	return (i2c->state == STATE_DONE) ? num : -EIO;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: ocores: generate stop condition after timeout in polling mode
  2023-02-20 16:16 [PATCH] i2c: ocores: generate stop condition after timeout in polling mode Matthias Schiffer
@ 2023-04-12  9:49 ` Matthias Schiffer
  2023-04-12 11:50   ` Federico Vaga
  2023-04-12 12:43 ` Andrew Lunn
  1 sibling, 1 reply; 4+ messages in thread
From: Matthias Schiffer @ 2023-04-12  9:49 UTC (permalink / raw)
  To: Peter Korsgaard, Andrew Lunn
  Cc: Federico Vaga, Wolfram Sang, linux-i2c, linux-kernel, linux

On Mon, 2023-02-20 at 16:17 +0000, Matthias Schiffer wrote:
> From: Gregor Herburger <gregor.herburger@tq-group.com>
> 
> In polling mode, no stop condition is generated after a timeout. This
> causes SCL to remain low and thereby block the bus. If this happens
> during a transfer it can cause slaves to misinterpret the subsequent
> transfer and return wrong values.
> 
> To solve this, pass the ETIMEDOUT error up from ocores_process_polling()
> instead of setting STATE_ERROR directly. The caller is adjusted to call
> ocores_process_timeout() on error both in polling and in IRQ mode, which
> will set STATE_ERROR and generate a stop condition.
> 
> Fixes: 69c8c0c0efa8 ("i2c: ocores: add polling interface")
> Signed-off-by: Gregor Herburger <gregor.herburger@tq-group.com>
> Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>


Any chance we can get someone to have a look at this patch?

Regards,
Matthias



> ---
>  drivers/i2c/busses/i2c-ocores.c | 28 +++++++++++++++-------------
>  1 file changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
> index a0af027db04c1..28bcda3f7040a 100644
> --- a/drivers/i2c/busses/i2c-ocores.c
> +++ b/drivers/i2c/busses/i2c-ocores.c
> @@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
>   * ocores_isr(), we just add our polling code around it.
>   *
>   * It can run in atomic context
> + *
> + * Return: 0 on success, -ETIMEDOUT on timeout
>   */
> -static void ocores_process_polling(struct ocores_i2c *i2c)
> +static int ocores_process_polling(struct ocores_i2c *i2c)
>  {
>  	while (1) {
>  		irqreturn_t ret;
>  		int err;
>  
>  		err = ocores_poll_wait(i2c);
> -		if (err) {
> -			i2c->state = STATE_ERROR;
> -			break; /* timeout */
> -		}
> +		if (err)
> +			return err;
>  
>  		ret = ocores_isr(-1, i2c);
>  		if (ret == IRQ_NONE)
> @@ -364,6 +364,8 @@ static void ocores_process_polling(struct ocores_i2c *i2c)
>  					break;
>  		}
>  	}
> +
> +	return 0;
>  }
>  
>  static int ocores_xfer_core(struct ocores_i2c *i2c,
> @@ -387,16 +389,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c,
>  	oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
>  	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
>  
> -	if (polling) {
> -		ocores_process_polling(i2c);
> -	} else {
> +	if (polling)
> +		ret = ocores_process_polling(i2c);
> +	else
>  		ret = wait_event_timeout(i2c->wait,
>  					 (i2c->state == STATE_ERROR) ||
> -					 (i2c->state == STATE_DONE), HZ);
> -		if (ret == 0) {
> -			ocores_process_timeout(i2c);
> -			return -ETIMEDOUT;
> -		}
> +					 (i2c->state == STATE_DONE), HZ) ?
> +						0 : -ETIMEDOUT;
> +	if (ret) {
> +		ocores_process_timeout(i2c);
> +		return ret;
>  	}
>  
>  	return (i2c->state == STATE_DONE) ? num : -EIO;

-- 
TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
Amtsgericht München, HRB 105018
Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
http://www.tq-group.com/


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: ocores: generate stop condition after timeout in polling mode
  2023-04-12  9:49 ` Matthias Schiffer
@ 2023-04-12 11:50   ` Federico Vaga
  0 siblings, 0 replies; 4+ messages in thread
From: Federico Vaga @ 2023-04-12 11:50 UTC (permalink / raw)
  To: Matthias Schiffer
  Cc: Peter Korsgaard, Andrew Lunn, Wolfram Sang, linux-i2c,
	linux-kernel, linux

On Wed, Apr 12, 2023 at 11:49:32AM +0200, Matthias Schiffer wrote:
>On Mon, 2023-02-20 at 16:17 +0000, Matthias Schiffer wrote:
>> From: Gregor Herburger <gregor.herburger@tq-group.com>
>>
>> In polling mode, no stop condition is generated after a timeout. This
>> causes SCL to remain low and thereby block the bus. If this happens
>> during a transfer it can cause slaves to misinterpret the subsequent
>> transfer and return wrong values.
>>
>> To solve this, pass the ETIMEDOUT error up from ocores_process_polling()
>> instead of setting STATE_ERROR directly. The caller is adjusted to call
>> ocores_process_timeout() on error both in polling and in IRQ mode, which
>> will set STATE_ERROR and generate a stop condition.

I can see and confirm the bug.

The patch seems fine to me, but I do not have the hardware at hand right now for a
real test.

Following a few cosmetic comments.

>> Fixes: 69c8c0c0efa8 ("i2c: ocores: add polling interface")
>> Signed-off-by: Gregor Herburger <gregor.herburger@tq-group.com>
>> Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>
>
>
>Any chance we can get someone to have a look at this patch?
>
>Regards,
>Matthias
>
>
>
>> ---
>>  drivers/i2c/busses/i2c-ocores.c | 28 +++++++++++++++-------------
>>  1 file changed, 15 insertions(+), 13 deletions(-)
>>
>> diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
>> index a0af027db04c1..28bcda3f7040a 100644
>> --- a/drivers/i2c/busses/i2c-ocores.c
>> +++ b/drivers/i2c/busses/i2c-ocores.c
>> @@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
>>   * ocores_isr(), we just add our polling code around it.
>>   *
>>   * It can run in atomic context
>> + *
>> + * Return: 0 on success, -ETIMEDOUT on timeout
>>   */
>> -static void ocores_process_polling(struct ocores_i2c *i2c)
>> +static int ocores_process_polling(struct ocores_i2c *i2c)
>>  {
>>  	while (1) {
>>  		irqreturn_t ret;
>>  		int err;
>>
>>  		err = ocores_poll_wait(i2c);
>> -		if (err) {
>> -			i2c->state = STATE_ERROR;
>> -			break; /* timeout */
>> -		}
>> +		if (err)
>> +			return err;
>>
>>  		ret = ocores_isr(-1, i2c);
>>  		if (ret == IRQ_NONE)
>> @@ -364,6 +364,8 @@ static void ocores_process_polling(struct ocores_i2c *i2c)
>>  					break;
>>  		}
>>  	}
>> +
>> +	return 0;
>>  }

I would try to have a single return point at the end `return err` and inside the
loop just a break.

>>
>>  static int ocores_xfer_core(struct ocores_i2c *i2c,
>> @@ -387,16 +389,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c,
>>  	oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
>>  	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
>>
>> -	if (polling) {
>> -		ocores_process_polling(i2c);
>> -	} else {
>> +	if (polling)
>> +		ret = ocores_process_polling(i2c);
>> +	else
>>  		ret = wait_event_timeout(i2c->wait,
>>  					 (i2c->state == STATE_ERROR) ||
>> -					 (i2c->state == STATE_DONE), HZ);
>> -		if (ret == 0) {
>> -			ocores_process_timeout(i2c);
>> -			return -ETIMEDOUT;
>> -		}
>> +					 (i2c->state == STATE_DONE), HZ) ?
>> +						0 : -ETIMEDOUT;

For readability, I would avoid the ternary operator here and do the assignement
with an `if` as it was before.

if (ret == 0)
     ret = -ETIMEDOUT;

>> +	if (ret) {
>> +		ocores_process_timeout(i2c);
>> +		return ret;
>>  	}
>>
>>  	return (i2c->state == STATE_DONE) ? num : -EIO;
>
>-- 
>TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
>Amtsgericht München, HRB 105018
>Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
>http://www.tq-group.com/
>

-- 
Federico Vaga - CERN BE-CEM-EDL

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: ocores: generate stop condition after timeout in polling mode
  2023-02-20 16:16 [PATCH] i2c: ocores: generate stop condition after timeout in polling mode Matthias Schiffer
  2023-04-12  9:49 ` Matthias Schiffer
@ 2023-04-12 12:43 ` Andrew Lunn
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2023-04-12 12:43 UTC (permalink / raw)
  To: Matthias Schiffer
  Cc: Peter Korsgaard, Federico Vaga, Wolfram Sang, linux-i2c,
	linux-kernel, linux

Hi Matthias

I also don't have access to the hardware, but the change looks O.K.

>  static int ocores_xfer_core(struct ocores_i2c *i2c,
> @@ -387,16 +389,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c,
>  	oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
>  	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
>  
> -	if (polling) {
> -		ocores_process_polling(i2c);
> -	} else {
> +	if (polling)
> +		ret = ocores_process_polling(i2c);
> +	else
>  		ret = wait_event_timeout(i2c->wait,
>  					 (i2c->state == STATE_ERROR) ||
> -					 (i2c->state == STATE_DONE), HZ);
> -		if (ret == 0) {
> -			ocores_process_timeout(i2c);
> -			return -ETIMEDOUT;
> -		}
> +					 (i2c->state == STATE_DONE), HZ) ?
> +						0 : -ETIMEDOUT;
> +	if (ret) {
> +		ocores_process_timeout(i2c);
> +		return ret;
>  	}

The ret == 0 becoming ret is not so obvious. Rather than having the
trinary, do

		if (ret == 0)
		   ret = -ETIMEDOUT;

and then fall into your new if clause. I think that makes it more
obvious that wait_event_timeout() returns 0 on timeout.

	Andrew

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-12 12:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-20 16:16 [PATCH] i2c: ocores: generate stop condition after timeout in polling mode Matthias Schiffer
2023-04-12  9:49 ` Matthias Schiffer
2023-04-12 11:50   ` Federico Vaga
2023-04-12 12:43 ` Andrew Lunn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).