Patchwork i2c-davinci: Handle signals gracefully

login
register
mail settings
Submitter Mike Looijmans
Date Jan. 9, 2014, 11:11 a.m.
Message ID <1389265885-26777-1-git-send-email-mike.looijmans@topic.nl>
Download mbox | patch
Permalink /patch/308576/
State Changes Requested
Headers show

Comments

Mike Looijmans - Jan. 9, 2014, 11:11 a.m.
When a signal is caught while the i2c-davinci bus driver is transferring,
the drive just "abandons" the transfer and leaves the controller to fend
for itself. The next I2C transaction will find the controller in an
undefined state and often results in a stream of "initiating i2c bus recovery"
messages until the controller arrives in a defined state. This behaviour
also sends out "half" or possibly even mixed messages to I2C client
devices which may put them in an undesired state as well.

This patch fixes this issue by always attempting to finish the current
transaction, and then check on a pending signal. It either reports
success if all data has been transferred, or it returns failure when
the transaction was aborted. This keeps the controller in a defined
state, and is also much friendlier towards client devices, because
it will only send complete messages.

Before this patch, reading an I2C device in a loop and interrupting it
often resulted in a "initiating i2c bus recovery" storm and not being
able to communicate via I2C for several seconds. With this patch, the
userspace call simply returns EINTR and the next I2C transaction
succeeds without errors.

Signed-off-by: Mike Looijmans <mike.looijmans@topic.nl>
---
 drivers/i2c/busses/i2c-davinci.c |   23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
Wolfram Sang - March 9, 2014, 8:21 p.m.
On Thu, Jan 09, 2014 at 12:11:25PM +0100, Mike Looijmans wrote:
> When a signal is caught while the i2c-davinci bus driver is transferring,
> the drive just "abandons" the transfer and leaves the controller to fend
> for itself. The next I2C transaction will find the controller in an
> undefined state and often results in a stream of "initiating i2c bus recovery"
> messages until the controller arrives in a defined state. This behaviour
> also sends out "half" or possibly even mixed messages to I2C client
> devices which may put them in an undesired state as well.
> 
> This patch fixes this issue by always attempting to finish the current
> transaction, and then check on a pending signal. It either reports
> success if all data has been transferred, or it returns failure when
> the transaction was aborted. This keeps the controller in a defined
> state, and is also much friendlier towards client devices, because
> it will only send complete messages.

Even more, you should complete the whole transfer. There are devices
where things can really go wrong if you send a half-complete command and
then start with the next one. So, not checking signals at all is the way
to go for I2C drivers. There is some cruft left, so I am happy about
patches fixing that, with testing on real HW. Like yours here.
Mike Looijmans - March 10, 2014, 10:24 a.m.
On 03/09/2014 09:21 PM, Wolfram Sang wrote:
> On Thu, Jan 09, 2014 at 12:11:25PM +0100, Mike Looijmans wrote:
>> When a signal is caught while the i2c-davinci bus driver is transferring,
>> the drive just "abandons" the transfer and leaves the controller to fend
>> for itself. The next I2C transaction will find the controller in an
>> undefined state and often results in a stream of "initiating i2c bus recovery"
>> messages until the controller arrives in a defined state. This behaviour
>> also sends out "half" or possibly even mixed messages to I2C client
>> devices which may put them in an undesired state as well.
>>
>> This patch fixes this issue by always attempting to finish the current
>> transaction, and then check on a pending signal. It either reports
>> success if all data has been transferred, or it returns failure when
>> the transaction was aborted. This keeps the controller in a defined
>> state, and is also much friendlier towards client devices, because
>> it will only send complete messages.
>
> Even more, you should complete the whole transfer. There are devices
> where things can really go wrong if you send a half-complete command and
> then start with the next one. So, not checking signals at all is the way
> to go for I2C drivers. There is some cruft left, so I am happy about
> patches fixing that, with testing on real HW. Like yours here.

I agree.

I know the Zynq (using a cadence controller) also lets signals interrupt I2C 
transfers, so I'll propose a patch to Xilinx and CC to you and linux-i2c to 
completely remove signal handling from that driver as well.

Mike


Met vriendelijke groet / kind regards,

Mike Looijmans

TOPIC Embedded Systems
Eindhovenseweg 32-C, NL-5683 KH Best
Postbus 440, NL-5680 AK Best
Telefoon: (+31) (0) 499 33 69 79
Telefax:  (+31) (0) 499 33 69 70
E-mail: mike.looijmans@topic.nl
Website: www.topic.nl

Please consider the environment before printing this e-mail

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wolfram Sang - March 10, 2014, 10:59 a.m.
> >Even more, you should complete the whole transfer. There are devices
> >where things can really go wrong if you send a half-complete command and
> >then start with the next one. So, not checking signals at all is the way
> >to go for I2C drivers. There is some cruft left, so I am happy about
> >patches fixing that, with testing on real HW. Like yours here.
> 
> I agree.
> 
> I know the Zynq (using a cadence controller) also lets signals
> interrupt I2C transfers, so I'll propose a patch to Xilinx and CC to
> you and linux-i2c to completely remove signal handling from that
> driver as well.

Cool, thanks!
Wolfram Sang - March 10, 2014, 3:24 p.m.
> > >Even more, you should complete the whole transfer. There are devices
> > >where things can really go wrong if you send a half-complete command and
> > >then start with the next one. So, not checking signals at all is the way
> > >to go for I2C drivers. There is some cruft left, so I am happy about
> > >patches fixing that, with testing on real HW. Like yours here.
> > 
> > I agree.
> > 
> > I know the Zynq (using a cadence controller) also lets signals
> > interrupt I2C transfers, so I'll propose a patch to Xilinx and CC to
> > you and linux-i2c to completely remove signal handling from that
> > driver as well.
> 
> Cool, thanks!

Are you going to update the davinci patch as well?
Mike Looijmans - March 14, 2014, 6:43 a.m.
On 03/10/2014 04:24 PM, Wolfram Sang wrote:
>
>>>> Even more, you should complete the whole transfer. There are devices
>>>> where things can really go wrong if you send a half-complete command and
>>>> then start with the next one. So, not checking signals at all is the way
>>>> to go for I2C drivers. There is some cruft left, so I am happy about
>>>> patches fixing that, with testing on real HW. Like yours here.
>>>
>>> I agree.
>>>
>>> I know the Zynq (using a cadence controller) also lets signals
>>> interrupt I2C transfers, so I'll propose a patch to Xilinx and CC to
>>> you and linux-i2c to completely remove signal handling from that
>>> driver as well.
>>
>> Cool, thanks!
>
> Are you going to update the davinci patch as well?

An amended patch is on its way now. I forgot to set the subject to "PATCHv2" 
though.

Mike.



Met vriendelijke groet / kind regards,

Mike Looijmans

TOPIC Embedded Systems
Eindhovenseweg 32-C, NL-5683 KH Best
Postbus 440, NL-5680 AK Best
Telefoon: (+31) (0) 499 33 69 79
Telefax:  (+31) (0) 499 33 69 70
E-mail: mike.looijmans@topic.nl
Website: www.topic.nl

Please consider the environment before printing this e-mail

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/drivers/i2c/busses/i2c-davinci.c b/drivers/i2c/busses/i2c-davinci.c
index 132369f..102673b 100644
--- a/drivers/i2c/busses/i2c-davinci.c
+++ b/drivers/i2c/busses/i2c-davinci.c
@@ -372,9 +372,9 @@  i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
 		flag |= DAVINCI_I2C_MDR_STP;
 	davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
 
-	r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
+	r = wait_for_completion_timeout(&dev->cmd_complete,
 						      dev->adapter.timeout);
-	if (r == 0) {
+	if (unlikely(r == 0)) {
 		dev_err(dev->dev, "controller timed out\n");
 		davinci_i2c_recover_bus(dev);
 		i2c_davinci_init(dev);
@@ -384,7 +384,6 @@  i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
 	if (dev->buf_len) {
 		/* This should be 0 if all bytes were transferred
 		 * or dev->cmd_err denotes an error.
-		 * A signal may have aborted the transfer.
 		 */
 		if (r >= 0) {
 			dev_err(dev->dev, "abnormal termination buf_len=%i\n",
@@ -436,22 +435,30 @@  i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 	ret = i2c_davinci_wait_bus_not_busy(dev, 1);
 	if (ret < 0) {
 		dev_warn(dev->dev, "timeout waiting for bus ready\n");
-		return ret;
+		goto error;
 	}
 
 	for (i = 0; i < num; i++) {
+		if (signal_pending(current)) {
+			dev_dbg(dev->dev, "%s [%d/%d] %#x ERESTARTSYS\n",
+					__func__, i + 1, num, msgs[i].addr);
+			ret = -ERESTARTSYS;
+			goto error;
+		}
 		ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
-		dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
-			ret);
+		dev_dbg(dev->dev, "%s [%d/%d] %#x ret: %d\n", __func__, i + 1,
+				num, msgs[i].addr, ret);
 		if (ret < 0)
-			return ret;
+			goto error;
 	}
+	ret = num;
 
+error:
 #ifdef CONFIG_CPU_FREQ
 	complete(&dev->xfr_complete);
 #endif
 
-	return num;
+	return ret;
 }
 
 static u32 i2c_davinci_func(struct i2c_adapter *adap)