Skip to contentSkip to conversation

i2c: i801: Don't read back cleared status in i801_check_pre()

PATCH Applied Heiner Kallweit 3 participants 8 messages Dec 2, 2021 – Dec 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: i801: Don't read back cleared status in i801_check_pre()

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

Whilst at it, change involved syslog messages to use pci_dbg() et al. to simplify them.

---
 drivers/i2c/busses/i2c-i801.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
drivers/i2c/busses/i2c-i801.c +3 -19
index 720f7e9d0..a82aaef27 100644
@@ -328,22 +328,14 @@ static int i801_check_pre(struct i801_priv *priv)
 
 	status = inb_p(SMBHSTSTS(priv));
 	if (status & SMBHSTSTS_HOST_BUSY) {
-		dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
+		pci_err(priv->pci_dev, "SMBus is busy, can't use it!\n");
 		return -EBUSY;
 	}
 
 	status &= STATUS_FLAGS;
 	if (status) {
-		dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
-			status);
+		pci_dbg(priv->pci_dev, "Clearing status flags (%02x)\n", status);
 		outb_p(status, SMBHSTSTS(priv));
-		status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
-		if (status) {
-			dev_err(&priv->pci_dev->dev,
-				"Failed clearing status flags (%02x)\n",
-				status);
-			return -EBUSY;
-		}
 	}
 
 	/*
@@ -356,16 +348,8 @@ static int i801_check_pre(struct i801_priv *priv)
 	if (priv->features & FEATURE_SMBUS_PEC) {
 		status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
 		if (status) {
-			dev_dbg(&priv->pci_dev->dev,
-				"Clearing aux status flags (%02x)\n", status);
+			pci_dbg(priv->pci_dev, "Clearing aux status flags (%02x)\n", status);
 			outb_p(status, SMBAUXSTS(priv));
-			status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
-			if (status) {
-				dev_err(&priv->pci_dev->dev,
-					"Failed clearing aux status flags (%02x)\n",
-					status);
-				return -EBUSY;
-			}
 		}
 	}
 
Signature · 1 line
2.34.1
#1 lore raw

Hi Heiner,

Quoted 3 lines · Heiner Kallweit

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

This happened at least once in the past. See this archived message:

https://www.spinics.net/lists/linux-i2c/msg02651.html

This was in i801_check_post(), not i801_check_pre(), but that was the same code. Which was removed in 6cad93c4bbd62ecfa2e1b3a95c1ac4f6f27764c7 because there was little point in checking the same condition twice.

Unfortunately it seems that the error messages were copied manually so we lack the details of which status bit couldn't be cleared exactly.

Granted, it was caused by a driver bug, which was fixed since (commit c074c39d62306efa5ba7c69c1a1531bc7333d252) but this shows that the condition can actually trigger.

Quoted 2 lines

Whilst at it, change involved syslog messages to use pci_dbg() et al. to simplify them.

Fine with me.

Quoted 42 lines
---
 drivers/i2c/busses/i2c-i801.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/drivers/i2c/busses/i2c-i801.c index 720f7e9d0..a82aaef27 100644

drivers/i2c/busses/i2c-i801.c +3 -12
@@ -328,22 +328,14 @@ static int i801_check_pre(struct i801_priv
 *priv) 
 	status = inb_p(SMBHSTSTS(priv));
 	if (status & SMBHSTSTS_HOST_BUSY) {
-		dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
+		pci_err(priv->pci_dev, "SMBus is busy, can't use it!\n");
 		return -EBUSY;
 	}
 
 	status &= STATUS_FLAGS;
 	if (status) {
-		dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
-			status);
+		pci_dbg(priv->pci_dev, "Clearing status flags (%02x)\n", status);
 		outb_p(status, SMBHSTSTS(priv));
-		status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
-		if (status) {
-			dev_err(&priv->pci_dev->dev,
-				"Failed clearing status flags (%02x)\n",
-				status);
-			return -EBUSY;
-		}
 	}
 
 	/*
@@ -356,16 +348,8 @@ static int i801_check_pre(struct i801_priv *priv)
 	if (priv->features & FEATURE_SMBUS_PEC) {
 		status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
 		if (status) {
-			dev_dbg(&priv->pci_dev->dev,
-				"Clearing aux status flags (%02x)\n", status);
+			pci_dbg(priv->pci_dev, "Clearing aux status flags (%02x)\n", status);
 			outb_p(status, SMBAUXSTS(priv));

drivers/i2c/busses/i2c-i801.c static int i801_check_pre(struct i801_priv *priv) · line 355

Quoted 10 lines
drivers/i2c/busses/i2c-i801.c +0 -7
-			status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
-			if (status) {
-				dev_err(&priv->pci_dev->dev,
-					"Failed clearing aux status flags (%02x)\n",
-					status);
-				return -EBUSY;
-			}
 		}
 	}
 

So I'm not too sure what to do with this. On the one hand, the code you want to remove could be useful to catch and investigate future bugs. The rest of the code does assume that the status bits are properly cleared before starting a new transaction. On the other hand, it is slowing down the driver a bit when all is fine. Is that really worth optimizing?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw
Quoted 11 lines · Jean Delvare

Hi Heiner,

Quoted 3 lines · Heiner Kallweit

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

This happened at least once in the past. See this archived message:

https://www.spinics.net/lists/linux-i2c/msg02651.html

"My last attempt locked the SMBus, but I was able to recover by repeatedly writing to the HST_STS register, as may times as the block length."

OK, this was 11 yrs ago, so at least I wouldn't be able to recall in detail what happened back then ..

Question is how you did this "repeatedly writing to the HST_STS register". Something like the following?

while (status = in (STATUS))

	out(STATUS, status);

Or maybe the driver started the loop to process the next byte? I think it's not likely that when writing a status bit it remained set. As we now know E32B is ignored in I2C mode, therefore the chip can read/write only one byte in a row, and w/o setting SMBHSTCNT_START in between it wouldn't touch the next byte. Of course I may be wrong with my assumptions ..

Quoted 68 lines

This was in i801_check_post(), not i801_check_pre(), but that was the same code. Which was removed in 6cad93c4bbd62ecfa2e1b3a95c1ac4f6f27764c7 because there was little point in checking the same condition twice.

Unfortunately it seems that the error messages were copied manually so we lack the details of which status bit couldn't be cleared exactly.

Granted, it was caused by a driver bug, which was fixed since (commit c074c39d62306efa5ba7c69c1a1531bc7333d252) but this shows that the condition can actually trigger.

Quoted 2 lines

Whilst at it, change involved syslog messages to use pci_dbg() et al. to simplify them.

Fine with me.

Quoted 51 lines
---
 drivers/i2c/busses/i2c-i801.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/drivers/i2c/busses/i2c-i801.c index 720f7e9d0..a82aaef27 100644

drivers/i2c/busses/i2c-i801.c +3 -19
@@ -328,22 +328,14 @@ static int i801_check_pre(struct i801_priv
 *priv) 
 	status = inb_p(SMBHSTSTS(priv));
 	if (status & SMBHSTSTS_HOST_BUSY) {
-		dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
+		pci_err(priv->pci_dev, "SMBus is busy, can't use it!\n");
 		return -EBUSY;
 	}
 
 	status &= STATUS_FLAGS;
 	if (status) {
-		dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
-			status);
+		pci_dbg(priv->pci_dev, "Clearing status flags (%02x)\n", status);
 		outb_p(status, SMBHSTSTS(priv));
-		status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
-		if (status) {
-			dev_err(&priv->pci_dev->dev,
-				"Failed clearing status flags (%02x)\n",
-				status);
-			return -EBUSY;
-		}
 	}
 
 	/*
@@ -356,16 +348,8 @@ static int i801_check_pre(struct i801_priv *priv)
 	if (priv->features & FEATURE_SMBUS_PEC) {
 		status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
 		if (status) {
-			dev_dbg(&priv->pci_dev->dev,
-				"Clearing aux status flags (%02x)\n", status);
+			pci_dbg(priv->pci_dev, "Clearing aux status flags (%02x)\n", status);
 			outb_p(status, SMBAUXSTS(priv));
-			status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
-			if (status) {
-				dev_err(&priv->pci_dev->dev,
-					"Failed clearing aux status flags (%02x)\n",
-					status);
-				return -EBUSY;
-			}
 		}
 	}
Quoted 9 lines
Quoted 1 line

So I'm not too sure what to do with this. On the one hand, the code you want to remove could be useful to catch and investigate future bugs. The rest of the code does assume that the status bits are properly cleared before starting a new transaction. On the other hand, it is slowing down the driver a bit when all is fine. Is that really worth optimizing?

In a follow-up mail in the thread you mentioned is the following. I noticed the same (the 1ms delay is too short) and have related patches in my tree. However I'd like to finalize the cleanups first.

"While working on this issue, I noticed that the piece of code which is supposed to let the i2c-i801 driver recover in case of a transaction timeout, did not always work."

#3 lore raw
Quoted 23 lines · Heiner Kallweit
Quoted 11 lines · Jean Delvare

Hi Heiner,

Quoted 3 lines · Heiner Kallweit

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

This happened at least once in the past. See this archived message:

https://www.spinics.net/lists/linux-i2c/msg02651.html

"My last attempt locked the SMBus, but I was able to recover by repeatedly writing to the HST_STS register, as may times as the block length."

OK, this was 11 yrs ago, so at least I wouldn't be able to recall in detail what happened back then ..

Question is how you did this "repeatedly writing to the HST_STS register". Something like the following?

Quoted 8 lines

while (status = in (STATUS))

	out(STATUS, status);

Or maybe the driver started the loop to process the next byte? I think it's not likely that when writing a status bit it remained set. As we now know E32B is ignored in I2C mode, therefore the chip can read/write only one byte in a row, and w/o setting SMBHSTCNT_START in between it wouldn't touch the next byte.

I mixed something up, START is needed only once.

Quoted 89 lines

Of course I may be wrong with my assumptions ..

Quoted 68 lines

This was in i801_check_post(), not i801_check_pre(), but that was the same code. Which was removed in 6cad93c4bbd62ecfa2e1b3a95c1ac4f6f27764c7 because there was little point in checking the same condition twice.

Unfortunately it seems that the error messages were copied manually so we lack the details of which status bit couldn't be cleared exactly.

Granted, it was caused by a driver bug, which was fixed since (commit c074c39d62306efa5ba7c69c1a1531bc7333d252) but this shows that the condition can actually trigger.

Quoted 2 lines

Whilst at it, change involved syslog messages to use pci_dbg() et al. to simplify them.

Fine with me.

Quoted 51 lines
---
 drivers/i2c/busses/i2c-i801.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/drivers/i2c/busses/i2c-i801.c index 720f7e9d0..a82aaef27 100644

drivers/i2c/busses/i2c-i801.c +3 -19
@@ -328,22 +328,14 @@ static int i801_check_pre(struct i801_priv
 *priv) 
 	status = inb_p(SMBHSTSTS(priv));
 	if (status & SMBHSTSTS_HOST_BUSY) {
-		dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
+		pci_err(priv->pci_dev, "SMBus is busy, can't use it!\n");
 		return -EBUSY;
 	}
 
 	status &= STATUS_FLAGS;
 	if (status) {
-		dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
-			status);
+		pci_dbg(priv->pci_dev, "Clearing status flags (%02x)\n", status);
 		outb_p(status, SMBHSTSTS(priv));
-		status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
-		if (status) {
-			dev_err(&priv->pci_dev->dev,
-				"Failed clearing status flags (%02x)\n",
-				status);
-			return -EBUSY;
-		}
 	}
 
 	/*
@@ -356,16 +348,8 @@ static int i801_check_pre(struct i801_priv *priv)
 	if (priv->features & FEATURE_SMBUS_PEC) {
 		status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
 		if (status) {
-			dev_dbg(&priv->pci_dev->dev,
-				"Clearing aux status flags (%02x)\n", status);
+			pci_dbg(priv->pci_dev, "Clearing aux status flags (%02x)\n", status);
 			outb_p(status, SMBAUXSTS(priv));
-			status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
-			if (status) {
-				dev_err(&priv->pci_dev->dev,
-					"Failed clearing aux status flags (%02x)\n",
-					status);
-				return -EBUSY;
-			}
 		}
 	}
Quoted 9 lines
Quoted 1 line

So I'm not too sure what to do with this. On the one hand, the code you want to remove could be useful to catch and investigate future bugs. The rest of the code does assume that the status bits are properly cleared before starting a new transaction. On the other hand, it is slowing down the driver a bit when all is fine. Is that really worth optimizing?

In a follow-up mail in the thread you mentioned is the following. I noticed the same (the 1ms delay is too short) and have related patches in my tree. However I'd like to finalize the cleanups first.

"While working on this issue, I noticed that the piece of code which is supposed to let the i2c-i801 driver recover in case of a transaction timeout, did not always work."

#4 lore raw
In reply to Heiner Kallweit

Hi Heiner,

Quoted 8 lines · Heiner Kallweit
Quoted 7 lines · Jean Delvare
Quoted 3 lines · Heiner Kallweit

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

This happened at least once in the past. See this archived message:

Quoted 8 lines
Quoted 1 line

https://www.spinics.net/lists/linux-i2c/msg02651.html

"My last attempt locked the SMBus, but I was able to recover by repeatedly writing to the HST_STS register, as may times as the block length."

OK, this was 11 yrs ago, so at least I wouldn't be able to recall in detail what happened back then ..

I definitely do not remember, so all the information available now is what can be read in this archived thread.

Quoted 2 lines

Question is how you did this "repeatedly writing to the HST_STS register". Something like the following?

Sorry for the confusion, Felix was the guy hitting the bug, and I was helping him understand what was happening and how to fix it.

Quoted 2 lines

while (status = in (STATUS))

	out(STATUS, status);

Most probably yes.

Quoted 6 lines

Or maybe the driver started the loop to process the next byte? I think it's not likely that when writing a status bit it remained set. As we now know E32B is ignored in I2C mode, therefore the chip can read/write only one byte in a row, and w/o setting SMBHSTCNT_START in between it wouldn't touch the next byte. Of course I may be wrong with my assumptions ..

The important bit was probably SMBHSTSTS_BYTE_DONE. The driver set the block buffer mode, expecting the hardware to support that, but the hardware didn't and thus expected a byte-by-byte block transaction, which requires ack-ing every byte by clearing SMBHSTSTS_BYTE_DONE.

Quoted 7 lines
Quoted 7 lines

(...)
So I'm not too sure what to do with this. On the one hand, the code you want to remove could be useful to catch and investigate future bugs. The rest of the code does assume that the status bits are properly cleared before starting a new transaction. On the other hand, it is slowing down the driver a bit when all is fine. Is that really worth optimizing?

As I got some time to think about it, answering myself: I'm fine removing the checks. If we ever hit the problem (unable to clear the error flags), it means that something went wrong _before_, and we must have logged these problems already. As a matter of fact, that was exactly the situation for Felix, the message you want to remove was the 4th error message logged, so in fact it did not really add any value.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#5 lore raw
Quoted 7 lines

As I got some time to think about it, answering myself: I'm fine removing the checks. If we ever hit the problem (unable to clear the error flags), it means that something went wrong _before_, and we must have logged these problems already. As a matter of fact, that was exactly the situation for Felix, the message you want to remove was the 4th error message logged, so in fact it did not really add any value.

May I read this as an ack?

Reviewed-by Tested-by
#6 lore raw
Quoted 8 lines · Wolfram Sang
Quoted 6 lines

As I got some time to think about it, answering myself: I'm fine removing the checks. If we ever hit the problem (unable to clear the error flags), it means that something went wrong _before_, and we must have logged these problems already. As a matter of fact, that was exactly the situation for Felix, the message you want to remove was the 4th error message logged, so in fact it did not really add any value.

May I read this as an ack?

Yes, sorry ^^

Signature · 2 lines
Jean Delvare
SUSE L3 Support
Applied
#7 lore raw
In reply to Heiner Kallweit
Quoted 8 lines · Heiner Kallweit

I see no need to read back the registers to verify that the bits have actually been cleared. I can't imagine any scenario where the bits would remain set after a write to them.

Whilst at it, change involved syslog messages to use pci_dbg() et al. to simplify them.

Applied to for-next, thanks!