linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: tegra: fix possible race condition after tx
@ 2011-08-11 20:19 Doug Anderson
       [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Doug Anderson @ 2011-08-11 20:19 UTC (permalink / raw)
  To: Ben Dooks, Stephen Warren, Vincent Palatin, Rhyland Klein
  Cc: Jean Delvare, Rakesh Iyer, Doug Anderson, Lucas De Marchi,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

In tegra_i2c_fill_tx_fifo, once we have finished pushing all the bytes
to the I2C hardware controller, the interrupt might happen before we
have updated i2c_dev->msg_buf_remaining at the end of the function.
Then, in tegra_i2c_isr, we will call again tegra_i2c_fill_tx_fifo
triggering weird behaviour. This has been shown to happen under real
conditions.

Signed-off-by: Doug Anderson <dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
---
 drivers/i2c/busses/i2c-tegra.c |   46 +++++++++++++++++++++++++++------------
 1 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index 2440b74..9c5fb75 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -270,14 +270,30 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
 
 	/* Rounds down to not include partial word at the end of buf */
 	words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
-	if (words_to_transfer > tx_fifo_avail)
-		words_to_transfer = tx_fifo_avail;
 
-	i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
-
-	buf += words_to_transfer * BYTES_PER_FIFO_WORD;
-	buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
-	tx_fifo_avail -= words_to_transfer;
+	/* It's very common to have < 4 bytes, so optimize that case. */
+	if (words_to_transfer) {
+		if (words_to_transfer > tx_fifo_avail)
+			words_to_transfer = tx_fifo_avail;
+
+		/*
+		 * Update state before writing to FIFO.  If this casues us
+		 * to finish writing all bytes (AKA buf_remaining goes to 0) we
+		 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
+		 * not maskable).  We need to make sure that the isr sees
+		 * buf_remaining as 0 and doesn't call us back re-entrantly.
+		 */
+		buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
+		tx_fifo_avail -= words_to_transfer;
+		i2c_dev->msg_buf_remaining = buf_remaining;
+		i2c_dev->msg_buf = buf +
+			words_to_transfer * BYTES_PER_FIFO_WORD;
+		barrier();
+
+		i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
+
+		buf += words_to_transfer * BYTES_PER_FIFO_WORD;
+	}
 
 	/*
 	 * If there is a partial word at the end of buf, handle it manually to
@@ -287,14 +303,15 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
 	if (tx_fifo_avail > 0 && buf_remaining > 0) {
 		BUG_ON(buf_remaining > 3);
 		memcpy(&val, buf, buf_remaining);
+
+		/* Again update before writing to FIFO to make sure isr sees. */
+		i2c_dev->msg_buf_remaining = 0;
+		i2c_dev->msg_buf = NULL;
+		barrier();
+
 		i2c_writel(i2c_dev, val, I2C_TX_FIFO);
-		buf_remaining = 0;
-		tx_fifo_avail--;
 	}
 
-	BUG_ON(tx_fifo_avail > 0 && buf_remaining > 0);
-	i2c_dev->msg_buf_remaining = buf_remaining;
-	i2c_dev->msg_buf = buf;
 	return 0;
 }
 
@@ -411,9 +428,10 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
 			tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
 	}
 
-	if ((status & I2C_INT_PACKET_XFER_COMPLETE) &&
-			!i2c_dev->msg_buf_remaining)
+	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
+		BUG_ON(i2c_dev->msg_buf_remaining);
 		complete(&i2c_dev->msg_complete);
+	}
 
 	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
 	if (i2c_dev->is_dvc)
-- 
1.7.3.1

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

* Re: [PATCH] i2c: tegra: fix possible race condition after tx
       [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
@ 2011-08-11 20:25   ` Vincent Palatin
  2011-08-12  0:46   ` Ben Dooks
  1 sibling, 0 replies; 6+ messages in thread
From: Vincent Palatin @ 2011-08-11 20:25 UTC (permalink / raw)
  To: Doug Anderson
  Cc: Ben Dooks, Stephen Warren, Rhyland Klein, Jean Delvare,
	Rakesh Iyer, Lucas De Marchi, linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

Works nice on both real Tegra2 board and Qemu.

On Thu, Aug 11, 2011 at 13:19, Doug Anderson <dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org> wrote:
> In tegra_i2c_fill_tx_fifo, once we have finished pushing all the bytes
> to the I2C hardware controller, the interrupt might happen before we
> have updated i2c_dev->msg_buf_remaining at the end of the function.
> Then, in tegra_i2c_isr, we will call again tegra_i2c_fill_tx_fifo
> triggering weird behaviour. This has been shown to happen under real
> conditions.
>
> Signed-off-by: Doug Anderson <dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>

Tested-by: Vincent Palatin <vpalatin-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>

> ---
>  drivers/i2c/busses/i2c-tegra.c |   46 +++++++++++++++++++++++++++------------
>  1 files changed, 32 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
> index 2440b74..9c5fb75 100644
> --- a/drivers/i2c/busses/i2c-tegra.c
> +++ b/drivers/i2c/busses/i2c-tegra.c
> @@ -270,14 +270,30 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>
>        /* Rounds down to not include partial word at the end of buf */
>        words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
> -       if (words_to_transfer > tx_fifo_avail)
> -               words_to_transfer = tx_fifo_avail;
>
> -       i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> -
> -       buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> -       buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> -       tx_fifo_avail -= words_to_transfer;
> +       /* It's very common to have < 4 bytes, so optimize that case. */
> +       if (words_to_transfer) {
> +               if (words_to_transfer > tx_fifo_avail)
> +                       words_to_transfer = tx_fifo_avail;
> +
> +               /*
> +                * Update state before writing to FIFO.  If this casues us
> +                * to finish writing all bytes (AKA buf_remaining goes to 0) we
> +                * have a potential for an interrupt (PACKET_XFER_COMPLETE is
> +                * not maskable).  We need to make sure that the isr sees
> +                * buf_remaining as 0 and doesn't call us back re-entrantly.
> +                */
> +               buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> +               tx_fifo_avail -= words_to_transfer;
> +               i2c_dev->msg_buf_remaining = buf_remaining;
> +               i2c_dev->msg_buf = buf +
> +                       words_to_transfer * BYTES_PER_FIFO_WORD;
> +               barrier();
> +
> +               i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> +
> +               buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> +       }
>
>        /*
>         * If there is a partial word at the end of buf, handle it manually to
> @@ -287,14 +303,15 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>        if (tx_fifo_avail > 0 && buf_remaining > 0) {
>                BUG_ON(buf_remaining > 3);
>                memcpy(&val, buf, buf_remaining);
> +
> +               /* Again update before writing to FIFO to make sure isr sees. */
> +               i2c_dev->msg_buf_remaining = 0;
> +               i2c_dev->msg_buf = NULL;
> +               barrier();
> +
>                i2c_writel(i2c_dev, val, I2C_TX_FIFO);
> -               buf_remaining = 0;
> -               tx_fifo_avail--;
>        }
>
> -       BUG_ON(tx_fifo_avail > 0 && buf_remaining > 0);
> -       i2c_dev->msg_buf_remaining = buf_remaining;
> -       i2c_dev->msg_buf = buf;
>        return 0;
>  }
>
> @@ -411,9 +428,10 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>                        tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
>        }
>
> -       if ((status & I2C_INT_PACKET_XFER_COMPLETE) &&
> -                       !i2c_dev->msg_buf_remaining)
> +       if (status & I2C_INT_PACKET_XFER_COMPLETE) {
> +               BUG_ON(i2c_dev->msg_buf_remaining);
>                complete(&i2c_dev->msg_complete);
> +       }
>
>        i2c_writel(i2c_dev, status, I2C_INT_STATUS);
>        if (i2c_dev->is_dvc)
> --
> 1.7.3.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

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

* Re: [PATCH] i2c: tegra: fix possible race condition after tx
  2011-08-11 20:19 [PATCH] i2c: tegra: fix possible race condition after tx Doug Anderson
       [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
@ 2011-08-11 20:52 ` Rhyland Klein
  2011-08-11 20:54 ` Stephen Warren
  2 siblings, 0 replies; 6+ messages in thread
From: Rhyland Klein @ 2011-08-11 20:52 UTC (permalink / raw)
  To: Doug Anderson
  Cc: Vincent Palatin, Lucas De Marchi, linux-kernel@vger.kernel.org,
	linux-tegra@vger.kernel.org, Rakesh Iyer,
	linux-i2c@vger.kernel.org, Ben Dooks, Jean Delvare,
	Stephen Warren, linux-arm-kernel@lists.infradead.org

On Thu, 2011-08-11 at 13:19 -0700, Doug Anderson wrote:
> In tegra_i2c_fill_tx_fifo, once we have finished pushing all the bytes
> to the I2C hardware controller, the interrupt might happen before we
> have updated i2c_dev->msg_buf_remaining at the end of the function.
> Then, in tegra_i2c_isr, we will call again tegra_i2c_fill_tx_fifo
> triggering weird behaviour. This has been shown to happen under real
> conditions.
> 
> Signed-off-by: Doug Anderson <dianders@chromium.org>
> ---
>  drivers/i2c/busses/i2c-tegra.c |   46 +++++++++++++++++++++++++++------------
>  1 files changed, 32 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
> index 2440b74..9c5fb75 100644
> --- a/drivers/i2c/busses/i2c-tegra.c
> +++ b/drivers/i2c/busses/i2c-tegra.c
> @@ -270,14 +270,30 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>  
>  	/* Rounds down to not include partial word at the end of buf */
>  	words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
> -	if (words_to_transfer > tx_fifo_avail)
> -		words_to_transfer = tx_fifo_avail;
>  
> -	i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> -
> -	buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> -	buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> -	tx_fifo_avail -= words_to_transfer;
> +	/* It's very common to have < 4 bytes, so optimize that case. */
> +	if (words_to_transfer) {
> +		if (words_to_transfer > tx_fifo_avail)
> +			words_to_transfer = tx_fifo_avail;
> +
> +		/*
> +		 * Update state before writing to FIFO.  If this casues us
> +		 * to finish writing all bytes (AKA buf_remaining goes to 0) we
> +		 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
> +		 * not maskable).  We need to make sure that the isr sees
> +		 * buf_remaining as 0 and doesn't call us back re-entrantly.
> +		 */
> +		buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> +		tx_fifo_avail -= words_to_transfer;
> +		i2c_dev->msg_buf_remaining = buf_remaining;
> +		i2c_dev->msg_buf = buf +
> +			words_to_transfer * BYTES_PER_FIFO_WORD;
> +		barrier();
> +
> +		i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> +
> +		buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> +	}
>  
>  	/*
>  	 * If there is a partial word at the end of buf, handle it manually to
> @@ -287,14 +303,15 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>  	if (tx_fifo_avail > 0 && buf_remaining > 0) {
>  		BUG_ON(buf_remaining > 3);
>  		memcpy(&val, buf, buf_remaining);
> +
> +		/* Again update before writing to FIFO to make sure isr sees. */
> +		i2c_dev->msg_buf_remaining = 0;
> +		i2c_dev->msg_buf = NULL;
> +		barrier();
> +
>  		i2c_writel(i2c_dev, val, I2C_TX_FIFO);
> -		buf_remaining = 0;
> -		tx_fifo_avail--;
>  	}
>  
> -	BUG_ON(tx_fifo_avail > 0 && buf_remaining > 0);
> -	i2c_dev->msg_buf_remaining = buf_remaining;
> -	i2c_dev->msg_buf = buf;
>  	return 0;
>  }
>  
> @@ -411,9 +428,10 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>  			tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
>  	}
>  
> -	if ((status & I2C_INT_PACKET_XFER_COMPLETE) &&
> -			!i2c_dev->msg_buf_remaining)
> +	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
> +		BUG_ON(i2c_dev->msg_buf_remaining);
>  		complete(&i2c_dev->msg_complete);
> +	}
>  
>  	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
>  	if (i2c_dev->is_dvc)

Looks good to me 

Acked-by: Rhyland Klein <rklein@nvidia.com>

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

* RE: [PATCH] i2c: tegra: fix possible race condition after tx
  2011-08-11 20:19 [PATCH] i2c: tegra: fix possible race condition after tx Doug Anderson
       [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
  2011-08-11 20:52 ` Rhyland Klein
@ 2011-08-11 20:54 ` Stephen Warren
       [not found]   ` <74CDBE0F657A3D45AFBB94109FB122FF04AEA24CFC-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Warren @ 2011-08-11 20:54 UTC (permalink / raw)
  To: Doug Anderson, Ben Dooks, Vincent Palatin, Rhyland Klein
  Cc: Jean Delvare, Rakesh Iyer, Lucas De Marchi,
	linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org

Doug Anderson wrote at Thursday, August 11, 2011 2:19 PM:
> In tegra_i2c_fill_tx_fifo, once we have finished pushing all the bytes
> to the I2C hardware controller, the interrupt might happen before we
> have updated i2c_dev->msg_buf_remaining at the end of the function.
> Then, in tegra_i2c_isr, we will call again tegra_i2c_fill_tx_fifo
> triggering weird behaviour. This has been shown to happen under real
> conditions.
> 
> Signed-off-by: Doug Anderson <dianders@chromium.org>

Acked-by: Stephen Warren <swarren@nvidia.com>

It's worth pointing out that this patch supersedes a previous attempt to fix
the same problem, originally posted in https://lkml.org/lkml/2011/4/19/514,
if I'm understanding correctly.

-- 
nvpublic

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

* Re: [PATCH] i2c: tegra: fix possible race condition after tx
       [not found]   ` <74CDBE0F657A3D45AFBB94109FB122FF04AEA24CFC-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
@ 2011-08-11 21:04     ` Doug Anderson
  0 siblings, 0 replies; 6+ messages in thread
From: Doug Anderson @ 2011-08-11 21:04 UTC (permalink / raw)
  To: Stephen Warren
  Cc: Ben Dooks, Vincent Palatin, Rhyland Klein, Jean Delvare,
	Rakesh Iyer, Lucas De Marchi,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org

On Thu, Aug 11, 2011 at 1:54 PM, Stephen Warren <swarren-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org> wrote:
> It's worth pointing out that this patch supersedes a previous attempt to fix
> the same problem, originally posted in https://lkml.org/lkml/2011/4/19/514,
> if I'm understanding correctly.

Yup, you're right.  :)  If people are interested, you can see some
discussion of the differences between the two patches on the Chromium
code review system: <http://gerrit.chromium.org/gerrit/5684>

-Doug

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

* Re: [PATCH] i2c: tegra: fix possible race condition after tx
       [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
  2011-08-11 20:25   ` Vincent Palatin
@ 2011-08-12  0:46   ` Ben Dooks
  1 sibling, 0 replies; 6+ messages in thread
From: Ben Dooks @ 2011-08-12  0:46 UTC (permalink / raw)
  To: Doug Anderson
  Cc: Ben Dooks, Stephen Warren, Vincent Palatin, Rhyland Klein,
	Jean Delvare, Rakesh Iyer, Lucas De Marchi,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-tegra-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r

On Thu, Aug 11, 2011 at 01:19:06PM -0700, Doug Anderson wrote:
> In tegra_i2c_fill_tx_fifo, once we have finished pushing all the bytes
> to the I2C hardware controller, the interrupt might happen before we
> have updated i2c_dev->msg_buf_remaining at the end of the function.
> Then, in tegra_i2c_isr, we will call again tegra_i2c_fill_tx_fifo
> triggering weird behaviour. This has been shown to happen under real
> conditions.
> 
> Signed-off-by: Doug Anderson <dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>

ok, it seems enough acks for this to make it in to the next i2c fixes
pull I'll send at the weekend.

> ---
>  drivers/i2c/busses/i2c-tegra.c |   46 +++++++++++++++++++++++++++------------
>  1 files changed, 32 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
> index 2440b74..9c5fb75 100644
> --- a/drivers/i2c/busses/i2c-tegra.c
> +++ b/drivers/i2c/busses/i2c-tegra.c
> @@ -270,14 +270,30 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>  
>  	/* Rounds down to not include partial word at the end of buf */
>  	words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
> -	if (words_to_transfer > tx_fifo_avail)
> -		words_to_transfer = tx_fifo_avail;
>  
> -	i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> -
> -	buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> -	buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> -	tx_fifo_avail -= words_to_transfer;
> +	/* It's very common to have < 4 bytes, so optimize that case. */
> +	if (words_to_transfer) {
> +		if (words_to_transfer > tx_fifo_avail)
> +			words_to_transfer = tx_fifo_avail;
> +
> +		/*
> +		 * Update state before writing to FIFO.  If this casues us
> +		 * to finish writing all bytes (AKA buf_remaining goes to 0) we
> +		 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
> +		 * not maskable).  We need to make sure that the isr sees
> +		 * buf_remaining as 0 and doesn't call us back re-entrantly.
> +		 */
> +		buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
> +		tx_fifo_avail -= words_to_transfer;
> +		i2c_dev->msg_buf_remaining = buf_remaining;
> +		i2c_dev->msg_buf = buf +
> +			words_to_transfer * BYTES_PER_FIFO_WORD;
> +		barrier();
> +
> +		i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
> +
> +		buf += words_to_transfer * BYTES_PER_FIFO_WORD;
> +	}
>  
>  	/*
>  	 * If there is a partial word at the end of buf, handle it manually to
> @@ -287,14 +303,15 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
>  	if (tx_fifo_avail > 0 && buf_remaining > 0) {
>  		BUG_ON(buf_remaining > 3);
>  		memcpy(&val, buf, buf_remaining);
> +
> +		/* Again update before writing to FIFO to make sure isr sees. */
> +		i2c_dev->msg_buf_remaining = 0;
> +		i2c_dev->msg_buf = NULL;
> +		barrier();
> +
>  		i2c_writel(i2c_dev, val, I2C_TX_FIFO);
> -		buf_remaining = 0;
> -		tx_fifo_avail--;
>  	}
>  
> -	BUG_ON(tx_fifo_avail > 0 && buf_remaining > 0);
> -	i2c_dev->msg_buf_remaining = buf_remaining;
> -	i2c_dev->msg_buf = buf;
>  	return 0;
>  }
>  
> @@ -411,9 +428,10 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>  			tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
>  	}
>  
> -	if ((status & I2C_INT_PACKET_XFER_COMPLETE) &&
> -			!i2c_dev->msg_buf_remaining)
> +	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
> +		BUG_ON(i2c_dev->msg_buf_remaining);
>  		complete(&i2c_dev->msg_complete);
> +	}
>  
>  	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
>  	if (i2c_dev->is_dvc)
> -- 
> 1.7.3.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
Ben Dooks, ben-elnMNo+KYs3YtjvyW6yDsg@public.gmane.org, http://www.fluff.org/ben/

Large Hadron Colada: A large Pina Colada that makes the universe disappear.

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

end of thread, other threads:[~2011-08-12  0:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-11 20:19 [PATCH] i2c: tegra: fix possible race condition after tx Doug Anderson
     [not found] ` <1313093946-24559-1-git-send-email-dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-08-11 20:25   ` Vincent Palatin
2011-08-12  0:46   ` Ben Dooks
2011-08-11 20:52 ` Rhyland Klein
2011-08-11 20:54 ` Stephen Warren
     [not found]   ` <74CDBE0F657A3D45AFBB94109FB122FF04AEA24CFC-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org>
2011-08-11 21:04     ` Doug Anderson

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).