Skip to contentSkip to conversation

i2c-i801: Make interrupt mode more robust

PATCH Superseded Jean Delvare 2 participants 12 messages Nov 10, 2014 – Nov 11, 2014 5 messages not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover i2c-i801: Make interrupt mode more robust

Hi all,

This is a patch series aiming at making the interrupt mode of the i2c-i801 driver more robust. A number of driver lock-ups have been reported on a few systems over the past few years, all related to interrupt mode. So let's make it more tolerant to unreliable hardware or whatever the actual problem is.

i2c-i801: Use wait_event_timeout to wait for interrupts
i2c-i801: Fallback to polling if request_irq() fails
i2c-i801: Check if interrupts are disabled
i2c-i801: Drop useless debug message

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

PATCH 1/4 i2c-i801: Use wait_event_timeout to wait for interrupts

In reply to a message not in the archive

Some systems have been reported to have trouble with interrupts. Use wait_event_timeout() instead of wait_event() so we don't get stuck in that case, and log the problem.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |   25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-i801.c +22 -3
@@ -2,7 +2,7 @@
     Copyright (c) 1998 - 2002  Frodo Looijaard <[email protected]>,
     Philip Edelbrock <[email protected]>, and Mark D. Studebaker
     <mdsxyz123-/[email protected]>
-    Copyright (C) 2007 - 2012  Jean Delvare <[email protected]>
+    Copyright (C) 2007 - 2014  Jean Delvare <[email protected]>
     Copyright (C) 2010         Intel Corporation,
                                David Woodhouse <[email protected]>
 
@@ -373,6 +373,7 @@ static int i801_transaction(struct i801_
 {
 	int status;
 	int result;
+	const struct i2c_adapter *adap = &priv->adapter;
 
 	result = i801_check_pre(priv);
 	if (result < 0)
@@ -381,7 +382,14 @@ static int i801_transaction(struct i801_
 	if (priv->features & FEATURE_IRQ) {
 		outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
 		       SMBHSTCNT(priv));
-		wait_event(priv->waitq, (status = priv->status));
+		result = wait_event_timeout(priv->waitq,
+					    (status = priv->status),
+					    adap->timeout);
+		if (!result) {
+			status = -ETIMEDOUT;
+			dev_warn(&priv->pci_dev->dev,
+				 "Timeout waiting for interrupt!\n");
+		}
 		priv->status = 0;
 		return i801_check_post(priv, status);
 	}
@@ -529,6 +537,7 @@ static int i801_block_transaction_byte_b
 	int smbcmd;
 	int status;
 	int result;
+	const struct i2c_adapter *adap = &priv->adapter;
 
 	result = i801_check_pre(priv);
 	if (result < 0)
@@ -557,7 +566,14 @@ static int i801_block_transaction_byte_b
 		priv->data = &data->block[1];
 
 		outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
-		wait_event(priv->waitq, (status = priv->status));
+		result = wait_event_timeout(priv->waitq,
+					    (status = priv->status),
+					    adap->timeout);
+		if (!result) {
+			status = -ETIMEDOUT;
+			dev_warn(&priv->pci_dev->dev,
+				 "Timeout waiting for interrupt!\n");
+		}
 		priv->status = 0;
 		return i801_check_post(priv, status);
 	}
@@ -1215,6 +1231,9 @@ static int i801_probe(struct pci_dev *de
 		outb_p(inb_p(SMBAUXCTL(priv)) &
 		       ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
 
+	/* Default timeout in interrupt mode: 200 ms */
+	priv->adapter.timeout = HZ / 5;
+
 	if (priv->features & FEATURE_IRQ) {
 		init_waitqueue_head(&priv->waitq);
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#3 lore raw

PATCH 2/4 i2c-i801: Fallback to polling if request_irq() fails

In reply to a message not in the archive

The i2c-i801 driver can work without interrupts, so there is no reason to make a request_irq failure fatal. Instead we can simply fallback to polling.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-i801.c +4 -3
@@ -1242,9 +1242,11 @@ static int i801_probe(struct pci_dev *de
 		if (err) {
 			dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
 				dev->irq, err);
-			goto exit_release;
+			priv->features &= ~FEATURE_IRQ;
+			dev_info(&dev->dev, "SMBus using polling\n");
+		} else {
+			dev_info(&dev->dev, "SMBus using PCI interrupt\n");
 		}
-		dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
 	}
 
 	/* set up the sysfs linkage to our parent device */
@@ -1272,7 +1274,6 @@ static int i801_probe(struct pci_dev *de
 exit_free_irq:
 	if (priv->features & FEATURE_IRQ)
 		free_irq(dev->irq, priv);
-exit_release:
 	pci_release_region(dev, SMBBAR);
 exit:
 	kfree(priv);
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#7 lore raw

PATCH 3/4 i2c-i801: Check if interrupts are disabled

In reply to a message not in the archive

There is a control bit in the PCI configuration space which disables interrupts. If this bit is set, the driver should not try to make use of interrupts, it won't receive any.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/i2c/busses/i2c-i801.c +21 -0
@@ -110,12 +110,16 @@
 
 /* PCI Address Constants */
 #define SMBBAR		4
+#define SMBPCICTL	0x004
 #define SMBPCISTS	0x006
 #define SMBHSTCFG	0x040
 
 /* Host status bits for SMBPCISTS */
 #define SMBPCISTS_INTS		0x08
 
+/* Control bits for SMBPCICTL */
+#define SMBPCICTL_INTDIS	0x0400
+
 /* Host configuration bits for SMBHSTCFG */
 #define SMBHSTCFG_HST_EN	1
 #define SMBHSTCFG_SMB_SMI_EN	2
@@ -1235,6 +1239,23 @@ static int i801_probe(struct pci_dev *de
 	priv->adapter.timeout = HZ / 5;
 
 	if (priv->features & FEATURE_IRQ) {
+		u16 pcictl, pcists;
+
+		/* Complain if an interrupt is already pending */
+		pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
+		if (pcists & SMBPCISTS_INTS)
+			dev_warn(&dev->dev, "An interrupt is pending!\n");
+
+		/* Check if interrupts have been disabled */
+		pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
+		if (pcictl & SMBPCICTL_INTDIS) {
+			dev_warn(&dev->dev,
+				 "Interrupts are disabled, will use polling\n");
+			priv->features &= ~FEATURE_IRQ;
+		}
+	}
+
+	if (priv->features & FEATURE_IRQ) {
 		init_waitqueue_head(&priv->waitq);
 
 		err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#16 lore raw

PATCH 4/4 i2c-i801: Drop useless debug message

In reply to a message not in the archive

Don't log the host status register value in i801_isr(), it has very little value and fills up the log when debugging is enabled.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |    3 ---
 1 file changed, 3 deletions(-)
drivers/i2c/busses/i2c-i801.c +0 -3
@@ -507,9 +507,6 @@ static irqreturn_t i801_isr(int irq, voi
 		return IRQ_NONE;
 
 	status = inb_p(SMBHSTSTS(priv));
-	if (status != 0x42)
-		dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
-
 	if (status & SMBHSTSTS_BYTE_DONE)
 		i801_isr_byte_done(priv);
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#5 lore raw

Re: [PATCH 2/4] i2c-i801: Fallback to polling if request_irq() fails

In reply to a message not in the archive
Quoted 13 lines · Jean Delvare

The i2c-i801 driver can work without interrupts, so there is no reason to make a request_irq failure fatal. Instead we can simply fallback to polling.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1246

Quoted 6 lines
drivers/i2c/busses/i2c-i801.c +2 -1
@@ -1242,9 +1242,11 @@ static int i801_probe(struct pci_dev *de
 		if (err) {
 			dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
 				dev->irq, err);
-			goto exit_release;
+			priv->features &= ~FEATURE_IRQ;
+			dev_info(&dev->dev, "SMBus using polling\n");

Shouldn't this message also be printed for !FEATURE_IRQ? I'd think it should be moved to another place. We can also deduce from the dev_err above that polling will be used, no?

Quoted 19 lines
drivers/i2c/busses/i2c-i801.c +2 -2
+		} else {
+			dev_info(&dev->dev, "SMBus using PCI interrupt\n");
 		}
-		dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
 	}
 
 	/* set up the sysfs linkage to our parent device */
@@ -1272,7 +1274,6 @@ static int i801_probe(struct pci_dev *de
 exit_free_irq:
 	if (priv->features & FEATURE_IRQ)
 		free_irq(dev->irq, priv);
-exit_release:
 	pci_release_region(dev, SMBBAR);
 exit:
 	kfree(priv);
Signature · 3 lines
-- 
Jean Delvare
SUSE L3 Support
#9 lore raw

Re: [PATCH 3/4] i2c-i801: Check if interrupts are disabled

In reply to a message not in the archive
Quoted 30 lines · Jean Delvare

There is a control bit in the PCI configuration space which disables interrupts. If this bit is set, the driver should not try to make use of interrupts, it won't receive any.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/i2c/busses/i2c-i801.c +4 -0
@@ -110,12 +110,16 @@
 
 /* PCI Address Constants */
 #define SMBBAR		4
+#define SMBPCICTL	0x004
 #define SMBPCISTS	0x006
 #define SMBHSTCFG	0x040
 
 /* Host status bits for SMBPCISTS */
 #define SMBPCISTS_INTS		0x08
 
+/* Control bits for SMBPCICTL */
+#define SMBPCICTL_INTDIS	0x0400
+
 /* Host configuration bits for SMBHSTCFG */
 #define SMBHSTCFG_HST_EN	1
 #define SMBHSTCFG_SMB_SMI_EN	2

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1247

Quoted 9 lines
drivers/i2c/busses/i2c-i801.c +6 -0
@@ -1235,6 +1239,23 @@ static int i801_probe(struct pci_dev *de
 	priv->adapter.timeout = HZ / 5;
 
 	if (priv->features & FEATURE_IRQ) {
+		u16 pcictl, pcists;
+
+		/* Complain if an interrupt is already pending */
+		pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
+		if (pcists & SMBPCISTS_INTS)
+			dev_warn(&dev->dev, "An interrupt is pending!\n");

You think it is better to not clear it?

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1253

Quoted 6 lines
drivers/i2c/busses/i2c-i801.c +6 -0
+
+		/* Check if interrupts have been disabled */
+		pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
+		if (pcictl & SMBPCICTL_INTDIS) {
+			dev_warn(&dev->dev,
+				 "Interrupts are disabled, will use polling\n");

I'd say it is more info than warning?

Quoted 12 lines
drivers/i2c/busses/i2c-i801.c +5 -0
+			priv->features &= ~FEATURE_IRQ;
+		}
+	}
+
+	if (priv->features & FEATURE_IRQ) {
 		init_waitqueue_head(&priv->waitq);
 
 		err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
Signature · 3 lines
-- 
Jean Delvare
SUSE L3 Support
#6 lore raw

Re: [PATCH 2/4] i2c-i801: Fallback to polling if request_irq() fails

In reply to Wolfram Sang

Hi Wolfram,

Thanks for the quick review, very appreciated.

Quoted 11 lines · Wolfram Sang
Quoted 10 lines · Jean Delvare

The i2c-i801 driver can work without interrupts, so there is no reason to make a request_irq failure fatal. Instead we can simply fallback to polling.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Quoted 12 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1246

Quoted 9 lines
drivers/i2c/busses/i2c-i801.c +2 -1
@@ -1242,9 +1242,11 @@ static int i801_probe(struct pci_dev *de
 		if (err) {
 			dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
 				dev->irq, err);
-			goto exit_release;
+			priv->features &= ~FEATURE_IRQ;
+			dev_info(&dev->dev, "SMBus using polling\n");

Shouldn't this message also be printed for !FEATURE_IRQ? I'd think it should be moved to another place.

Good point, I'll rework that part of the code and resubmit.

Quoted 2 lines

We can also deduce from the dev_err
above that polling will be used, no?

Having to deduce things doesn't make my supporter's life easy. Given the problems that have been reported by the i2c-i801 driver users lately, I'd rather be verbose during initialization to make bug reports easier to analyze.

Quoted 16 lines
Quoted 16 lines
drivers/i2c/busses/i2c-i801.c +2 -2
+		} else {
+			dev_info(&dev->dev, "SMBus using PCI interrupt\n");
 		}
-		dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
 	}
 
 	/* set up the sysfs linkage to our parent device */
@@ -1272,7 +1274,6 @@ static int i801_probe(struct pci_dev *de
 exit_free_irq:
 	if (priv->features & FEATURE_IRQ)
 		free_irq(dev->irq, priv);
-exit_release:
 	pci_release_region(dev, SMBBAR);
 exit:
 	kfree(priv);
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#10 lore raw

Re: [PATCH 3/4] i2c-i801: Check if interrupts are disabled

In reply to Wolfram Sang

Hi Wolfram,

Quoted 33 lines · Wolfram Sang
Quoted 32 lines · Jean Delvare

There is a control bit in the PCI configuration space which disables interrupts. If this bit is set, the driver should not try to make use of interrupts, it won't receive any.

Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/busses/i2c-i801.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/i2c/busses/i2c-i801.c +4 -0
@@ -110,12 +110,16 @@
 
 /* PCI Address Constants */
 #define SMBBAR		4
+#define SMBPCICTL	0x004
 #define SMBPCISTS	0x006
 #define SMBHSTCFG	0x040
 
 /* Host status bits for SMBPCISTS */
 #define SMBPCISTS_INTS		0x08
 
+/* Control bits for SMBPCICTL */
+#define SMBPCICTL_INTDIS	0x0400
+
 /* Host configuration bits for SMBHSTCFG */
 #define SMBHSTCFG_HST_EN	1
 #define SMBHSTCFG_SMB_SMI_EN	2
@@ -1235,6 +1239,23 @@ static int i801_probe(struct pci_dev *de
 	priv->adapter.timeout = HZ / 5;
 
Quoted 9 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1247

Quoted 7 lines
drivers/i2c/busses/i2c-i801.c +6 -0
 	if (priv->features & FEATURE_IRQ) {
+		u16 pcictl, pcists;
+
+		/* Complain if an interrupt is already pending */
+		pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
+		if (pcists & SMBPCISTS_INTS)
+			dev_warn(&dev->dev, "An interrupt is pending!\n");

You think it is better to not clear it?

I admit I did not think about it. As I am trying to better understand the few mysterious failure cases that have been reported to me, I just wanted to log everything out of the ordinary. I have no idea what's considered the right thing to do in such a situation. Do you believe that clearing the interrupt is the appropriate action in that case?

Quoted 8 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1253

Quoted 6 lines
drivers/i2c/busses/i2c-i801.c +6 -0
+
+		/* Check if interrupts have been disabled */
+		pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
+		if (pcictl & SMBPCICTL_INTDIS) {
+			dev_warn(&dev->dev,
+				 "Interrupts are disabled, will use polling\n");

I'd say it is more info than warning?

Correct, I'll change it.

Quoted 8 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1261

Quoted 8 lines
drivers/i2c/busses/i2c-i801.c +5 -0
+			priv->features &= ~FEATURE_IRQ;
+		}
+	}
+
+	if (priv->features & FEATURE_IRQ) {
 		init_waitqueue_head(&priv->waitq);
 
 		err = request_irq(dev->irq, i801_isr, IRQF_SHARED,

Thanks again,

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

Re: [PATCH 3/4] i2c-i801: Check if interrupts are disabled

In reply to a message not in the archive
Quoted 11 lines
Quoted 5 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1247

Quoted 3 lines
drivers/i2c/busses/i2c-i801.c +2 -0
+		if (pcists & SMBPCISTS_INTS)
+			dev_warn(&dev->dev, "An interrupt is pending!\n");

You think it is better to not clear it?

I admit I did not think about it. As I am trying to better understand the few mysterious failure cases that have been reported to me, I just wanted to log everything out of the ordinary. I have no idea what's considered the right thing to do in such a situation. Do you believe that clearing the interrupt is the appropriate action in that case?

Depends on the driver, I'd say (which I haven't looked at in detail). If this causes the irq handler to be called as soon as the irq is registered, and if the state machine gets confused then, then it should be cleared beforehand, of course.

If this is not the case, then it might be better to be less intrusive, spit the warning, and wait for somebody to show up with this message in the logs.

#13 lore raw

Re: [PATCH 3/4] i2c-i801: Check if interrupts are disabled

Quoted 12 lines · Wolfram Sang
Quoted 11 lines
Quoted 5 lines

drivers/i2c/busses/i2c-i801.c static int i801_probe(struct pci_dev *de · line 1247

Quoted 3 lines
drivers/i2c/busses/i2c-i801.c +2 -0
+		if (pcists & SMBPCISTS_INTS)
+			dev_warn(&dev->dev, "An interrupt is pending!\n");

You think it is better to not clear it?

I admit I did not think about it. As I am trying to better understand the few mysterious failure cases that have been reported to me, I just wanted to log everything out of the ordinary. I have no idea what's considered the right thing to do in such a situation. Do you believe that clearing the interrupt is the appropriate action in that case?

Quoted 4 lines

Depends on the driver, I'd say (which I haven't looked at in detail). If this causes the irq handler to be called as soon as the irq is registered, and if the state machine gets confused then, then it should be cleared beforehand, of course.

The driver should survive just fine if the irq handler is called early, but lacking the context, it won't do anything useful. Essentially it will clear the interrupt and wake up an empty queue.

My actual concern is that I don't know what that would mean if an interrupt is pending at driver load time. If this is a leftover from a previous driver removal, or from the machine initialization (BIOS) then clearing it would be the right thing to do. But this could also mean that something (e.g. ACPI) is using the hardware and we should not. That being said, hitting the very moment where an interrupt is pending would be pretty random, so we can hardly rely on that anyway. Or this could mean that the interrupt setup is wrong somehow and we can't trust the SMBPCISTS_INTS because it is always set. At this point I just don't know.

Another thing I am wondering about, and thinking about it again (I wrote this code several months ago) this is probably the reason why I added the test in the first place: can a new interrupt be triggered as long as the previous one has not been cleared? If not, that could explain why the driver sometimes didn't work at all in interrupt mode on some systems.

Quoted 3 lines

If this is not the case, then it might be better to be less intrusive, spit the warning, and wait for somebody to show up with this message in the logs.

That was my intent, yes. We can always add an action later when we understand the situation better. If we ever get such a report, which might in fact never happen.

Thanks,

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

Re: [PATCH 3/4] i2c-i801: Check if interrupts are disabled

In reply to a message not in the archive
Quoted 7 lines
Quoted 3 lines

If this is not the case, then it might be better to be less intrusive, spit the warning, and wait for somebody to show up with this message in the logs.

That was my intent, yes. We can always add an action later when we understand the situation better. If we ever get such a report, which might in fact never happen.

Let's do it then!