Skip to contentSkip to conversation

pci: Enable bus master till the FLR completes

PATCH Superseded Vipul Pandya 3 participants 4 messages Jul 1, 2013 – Jul 26, 2013 PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: Enable bus master till the FLR completes

From: Casey Leedom <[email protected]>

T4 can wedge if there are DMAs in flight within the chip and Bus master has been disabled. We need to have it on till the Function Level Reset completes. T4 can also suffer a Head Of Line blocking problem if MSI-X interrupts are disabled before the FLR has completed.

---
 drivers/pci/quirks.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
drivers/pci/quirks.c +91 -0
index e85d230..6357ba1 100644
@@ -3208,6 +3208,95 @@ reset_complete:
 	return 0;
 }
 
+/*
+ * Device-specific reset method for Chelsio T4-based adapters.
+ */
+static int reset_chelsio_generic_dev(struct pci_dev *dev, int probe)
+{
+	u16 old_command;
+	u16 status, msix_flags;
+	int i, rc, msix_pos;
+
+	/*
+	 * If this isn't a Chelsio T4-based device, return -ENOTTY indicating
+	 * that we have no device-specific reset method.
+	 */
+	if ((dev->device & 0xf000) != 0x4000)
+		return -ENOTTY;
+
+	/*
+	 * If this is the "probe" phase, return 0 indicating that we can
+	 * reset this device.
+	 */
+	if (probe)
+		return 0;
+
+	/*
+	 * T4 can wedge if their are DMAs in flight within the chip and Bus
+	 * master has been disabled.  We need to have it on till the Function
+	 * Level Reset completes.
+	 */
+	pci_read_config_word(dev, PCI_COMMAND, &old_command);
+	pci_write_config_word(dev, PCI_COMMAND,
+			      old_command | PCI_COMMAND_MASTER);
+
+	/*
+	 * Perform the actual device function reset, saving and restoring
+	 * configuration information around the reset.
+	 */
+	pci_save_state(dev);
+
+	/*
+	 * T4 also suffers a Head-Of-Line blocking problem if MSI-X interrupts
+	 * are disabled when an MSI-X interrupt message needs to be delivered.
+	 * So we briefly re-enable MSI-X interrupts for the duration of the
+	 * FLR.  The pci_restore_state() below will restore the original
+	 * MSI-X state.
+	 */
+	msix_pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
+	pci_read_config_word(dev, msix_pos+PCI_MSIX_FLAGS, &msix_flags);
+	if ((msix_flags & PCI_MSIX_FLAGS_ENABLE) == 0)
+		pci_write_config_word(dev, msix_pos+PCI_MSIX_FLAGS,
+				      msix_flags |
+				      PCI_MSIX_FLAGS_ENABLE |
+				      PCI_MSIX_FLAGS_MASKALL);
+
+	/*
+	 * This reset code is a copy of the guts of pcie_flr() because that's
+	 * not an exported function.
+	 */
+
+	/* Wait for Transaction Pending bit clean */
+	for (i = 0; i < 4; i++) {
+		if (i)
+			msleep((1 << (i - 1)) * 100);
+
+		pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
+		if (!(status & PCI_EXP_DEVSTA_TRPND))
+			goto clear;
+	}
+
+	dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
+
+clear:
+	pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
+	msleep(100);
+
+	/*
+	 * End of pcie_flr() code sequence.
+	 */
+
+	rc = 0;
+	pci_restore_state(dev);
+
+	/*
+	 * Restore the original PCI Configuration Space Command word (which
+	 * probably had Bus Master disabled).
+	 */
+	pci_write_config_word(dev, PCI_COMMAND, old_command);
+	return rc;
+}
+
 #define PCI_DEVICE_ID_INTEL_82599_SFP_VF   0x10ed
 #define PCI_DEVICE_ID_INTEL_IVB_M_VGA      0x0156
 #define PCI_DEVICE_ID_INTEL_IVB_M2_VGA     0x0166
@@ -3221,6 +3310,8 @@ static const struct pci_dev_reset_methods pci_dev_reset_methods[] = {
 		reset_ivb_igd },
 	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
 		reset_intel_generic_dev },
+	{ PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
+		reset_chelsio_generic_dev },
 	{ 0 }
 };
 
Signature · 1 line
1.8.0
maintainer
#1 lore raw
Quoted 56 lines · Vipul Pandya

From: Casey Leedom <[email protected]>

T4 can wedge if there are DMAs in flight within the chip and Bus master has been disabled. We need to have it on till the Function Level Reset completes. T4 can also suffer a Head Of Line blocking problem if MSI-X interrupts are disabled before the FLR has completed.

---
 drivers/pci/quirks.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
drivers/pci/quirks.c +36 -0
index e85d230..6357ba1 100644
@@ -3208,6 +3208,95 @@ reset_complete:
        return 0;
 }
 
+/*
+ * Device-specific reset method for Chelsio T4-based adapters.
+ */
+static int reset_chelsio_generic_dev(struct pci_dev *dev, int probe)
+{
+       u16 old_command;
+       u16 status, msix_flags;
+       int i, rc, msix_pos;
+
+       /*
+        * If this isn't a Chelsio T4-based device, return -ENOTTY indicating
+        * that we have no device-specific reset method.
+        */
+       if ((dev->device & 0xf000) != 0x4000)
+               return -ENOTTY;
+
+       /*
+        * If this is the "probe" phase, return 0 indicating that we can
+        * reset this device.
+        */
+       if (probe)
+               return 0;
+
+       /*
+        * T4 can wedge if their are DMAs in flight within the chip and Bus
+        * master has been disabled.  We need to have it on till the Function
+        * Level Reset completes.
+        */
+       pci_read_config_word(dev, PCI_COMMAND, &old_command);
+       pci_write_config_word(dev, PCI_COMMAND,
+                             old_command | PCI_COMMAND_MASTER);
+
+       /*
+        * Perform the actual device function reset, saving and restoring
+        * configuration information around the reset.
+        */

drivers/pci/quirks.c reset_complete: · line 3256

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+       pci_save_state(dev);
+
+       /*
+        * T4 also suffers a Head-Of-Line blocking problem if MSI-X interrupts
+        * are disabled when an MSI-X interrupt message needs to be delivered.
+        * So we briefly re-enable MSI-X interrupts for the duration of the
+        * FLR.  The pci_restore_state() below will restore the original
+        * MSI-X state.
+        */
+       msix_pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);

dev->msix_cap contains the capability offset already.

Quoted 11 lines
drivers/pci/quirks.c +11 -0
+       pci_read_config_word(dev, msix_pos+PCI_MSIX_FLAGS, &msix_flags);
+       if ((msix_flags & PCI_MSIX_FLAGS_ENABLE) == 0)
+               pci_write_config_word(dev, msix_pos+PCI_MSIX_FLAGS,
+                                     msix_flags |
+                                     PCI_MSIX_FLAGS_ENABLE |
+                                     PCI_MSIX_FLAGS_MASKALL);
+
+       /*
+        * This reset code is a copy of the guts of pcie_flr() because that's
+        * not an exported function.
+        */

drivers/pci/quirks.c reset_complete: · line 3277

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+
+       /* Wait for Transaction Pending bit clean */
+       for (i = 0; i < 4; i++) {
+               if (i)
+                       msleep((1 << (i - 1)) * 100);
+
+               pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
+               if (!(status & PCI_EXP_DEVSTA_TRPND))
+                       goto clear;
+       }

This is at least the fifth copy of this loop:

pcie_flr()
pci_af_flr()
bnx2x_do_flr()
reset_intel_82599_sfp_virtfn()

Can we factor this out and just implement it once? Maybe even make it smart enough to look at the PCIe DEVSTA if it exists, and fall back to using PCI_AF_STATUS register if *it* exists?

Quoted 7 lines
drivers/pci/quirks.c +7 -0
+
+       dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
+
+clear:
+       pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
+       msleep(100);
+

drivers/pci/quirks.c reset_complete: · line 3294

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+       /*
+        * End of pcie_flr() code sequence.
+        */
+
+       rc = 0;
+       pci_restore_state(dev);
+
+       /*
+        * Restore the original PCI Configuration Space Command word (which
+        * probably had Bus Master disabled).

Why was Bus Master probably originally disabled? I don't see anything in the pci_reset_function() path that would have disabled it before getting to this function.

But I don't think you need the comment anyway. "Probably had Bus Master disabled" isn't anything one can rely on.

drivers/pci/quirks.c reset_complete: · line 3297

Quoted 3 lines
drivers/pci/quirks.c +3 -0
+        */
+       pci_write_config_word(dev, PCI_COMMAND, old_command);
+       return rc;

"rc" appears useless.

Quoted 17 lines
drivers/pci/quirks.c +4 -1
+}
+
 #define PCI_DEVICE_ID_INTEL_82599_SFP_VF   0x10ed
 #define PCI_DEVICE_ID_INTEL_IVB_M_VGA      0x0156
 #define PCI_DEVICE_ID_INTEL_IVB_M2_VGA     0x0166
@@ -3221,6 +3310,8 @@ static const struct pci_dev_reset_methods pci_dev_reset_methods[] = {
                reset_ivb_igd },
        { PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
                reset_intel_generic_dev },
+       { PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
+               reset_chelsio_generic_dev },
        { 0 }
 };
 
--

1.8.0

#2 lore raw

Thanks for the review comments. I'll work with Vipul to get these properly incorporated. Answers to your comments are inline below:

Quoted 60 lines · Bjorn Helgaas
Quoted 59 lines · Vipul Pandya

From: Casey Leedom <[email protected]>

T4 can wedge if there are DMAs in flight within the chip and Bus master has been disabled. We need to have it on till the Function Level Reset completes. T4 can also suffer a Head Of Line blocking problem if MSI-X interrupts are disabled before the FLR has completed.

---
  drivers/pci/quirks.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 91 insertions(+)
drivers/pci/quirks.c +39 -0
index e85d230..6357ba1 100644
@@ -3208,6 +3208,95 @@ reset_complete:
         return 0;
  }
 
+/*
+ * Device-specific reset method for Chelsio T4-based adapters.
+ */
+static int reset_chelsio_generic_dev(struct pci_dev *dev, int probe)
+{
+       u16 old_command;
+       u16 status, msix_flags;
+       int i, rc, msix_pos;
+
+       /*
+        * If this isn't a Chelsio T4-based device, return -ENOTTY indicating
+        * that we have no device-specific reset method.
+        */
+       if ((dev->device & 0xf000) != 0x4000)
+               return -ENOTTY;
+
+       /*
+        * If this is the "probe" phase, return 0 indicating that we can
+        * reset this device.
+        */
+       if (probe)
+               return 0;
+
+       /*
+        * T4 can wedge if their are DMAs in flight within the chip and Bus
+        * master has been disabled.  We need to have it on till the Function
+        * Level Reset completes.
+        */
+       pci_read_config_word(dev, PCI_COMMAND, &old_command);
+       pci_write_config_word(dev, PCI_COMMAND,
+                             old_command | PCI_COMMAND_MASTER);
+
+       /*
+        * Perform the actual device function reset, saving and restoring
+        * configuration information around the reset.
+        */
+       pci_save_state(dev);
+
+       /*
Quoted 8 lines

drivers/pci/quirks.c reset_complete: · line 3256

Quoted 7 lines
drivers/pci/quirks.c +7 -0
+        * T4 also suffers a Head-Of-Line blocking problem if MSI-X interrupts
+        * are disabled when an MSI-X interrupt message needs to be delivered.
+        * So we briefly re-enable MSI-X interrupts for the duration of the
+        * FLR.  The pci_restore_state() below will restore the original
+        * MSI-X state.
+        */
+       msix_pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);

dev->msix_cap contains the capability offset already.

Thanks. I didn't know about that.

Quoted 23 lines
Quoted 11 lines
drivers/pci/quirks.c +11 -0
+       pci_read_config_word(dev, msix_pos+PCI_MSIX_FLAGS, &msix_flags);
+       if ((msix_flags & PCI_MSIX_FLAGS_ENABLE) == 0)
+               pci_write_config_word(dev, msix_pos+PCI_MSIX_FLAGS,
+                                     msix_flags |
+                                     PCI_MSIX_FLAGS_ENABLE |
+                                     PCI_MSIX_FLAGS_MASKALL);
+
+       /*
+        * This reset code is a copy of the guts of pcie_flr() because that's
+        * not an exported function.
+        */

drivers/pci/quirks.c reset_complete: · line 3277

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+
+       /* Wait for Transaction Pending bit clean */
+       for (i = 0; i < 4; i++) {
+               if (i)
+                       msleep((1 << (i - 1)) * 100);
+
+               pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
+               if (!(status & PCI_EXP_DEVSTA_TRPND))
+                       goto clear;
+       }

This is at least the fifth copy of this loop:

Quoted 8 lines
   pcie_flr()
   pci_af_flr()
   bnx2x_do_flr()
   reset_intel_82599_sfp_virtfn()

Can we factor this out and just implement it once? Maybe even make it smart enough to look at the PCIe DEVSTA if it exists, and fall back to using PCI_AF_STATUS register if *it* exists?

Should such a task be taken up in the patch we're trying to submit or should it be a follow on patch? I had thought that, in general, patches were supposed to essentially do "one thing." Thanks for your insight on this.

Quoted 21 lines
Quoted 7 lines
drivers/pci/quirks.c +7 -0
+
+       dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
+
+clear:
+       pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
+       msleep(100);
+

drivers/pci/quirks.c reset_complete: · line 3294

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+       /*
+        * End of pcie_flr() code sequence.
+        */
+
+       rc = 0;
+       pci_restore_state(dev);
+
+       /*
+        * Restore the original PCI Configuration Space Command word (which
+        * probably had Bus Master disabled).

Why was Bus Master probably originally disabled? I don't see anything in the pci_reset_function() path that would have disabled it before getting to this function.

Quoted 2 lines

But I don't think you need the comment anyway. "Probably had Bus Master disabled" isn't anything one can rely on.

Sorry, I probably should have included our example. In this case it's coming from KVM's kvm_free_assigned_device() routine which calls pci_reset_function() where we do:

/*
* both INTx and MSI are disabled after the Interrupt Disable bit
* is set and the Bus Master bit is cleared.
*/
pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);

rc = pci_dev_reset(dev, 0);

The pci_dev_reset(dev, 0) eventually dives into pcie_flr() where the Function Level Reset is performed — with Bus Master cleared by pci_reset_function() as above.

I'm happy to enhance the comment with this information since I'm a comment maven.

Quoted 4 lines

drivers/pci/quirks.c reset_complete: · line 3297

Quoted 3 lines
drivers/pci/quirks.c +3 -0
+        */
+       pci_write_config_word(dev, PCI_COMMAND, old_command);
+       return rc;

"rc" appears useless.

I agree. I was probably trying to follow coding styles as slavishly as possible with some intermediate coding efforts and didn't see that the final result had made "rc" pointless.

Casey

Quoted 17 lines
Quoted 17 lines
drivers/pci/quirks.c +4 -1
+}
+
  #define PCI_DEVICE_ID_INTEL_82599_SFP_VF   0x10ed
  #define PCI_DEVICE_ID_INTEL_IVB_M_VGA      0x0156
  #define PCI_DEVICE_ID_INTEL_IVB_M2_VGA     0x0166
@@ -3221,6 +3310,8 @@ static const struct pci_dev_reset_methods pci_dev_reset_methods[] = {
                 reset_ivb_igd },
         { PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
                 reset_intel_generic_dev },
+       { PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
+               reset_chelsio_generic_dev },
         { 0 }
  };
 
--

1.8.0

maintainer
#3 lore raw
Quoted 122 lines · Casey Leedom

Thanks for the review comments. I'll work with Vipul to get these properly incorporated. Answers to your comments are inline below:

Quoted 73 lines · Bjorn Helgaas
Quoted 71 lines · Vipul Pandya

From: Casey Leedom <[email protected]>

T4 can wedge if there are DMAs in flight within the chip and Bus master has
been disabled. We need to have it on till the Function Level Reset completes.
T4 can also suffer a Head Of Line blocking problem if MSI-X interrupts are
disabled before the FLR has completed.

---
  drivers/pci/quirks.c | 91
++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 91 insertions(+)
drivers/pci/quirks.c +11 -0
index e85d230..6357ba1 100644
@@ -3208,6 +3208,95 @@ reset_complete:
         return 0;
  }
 
+/*
+ * Device-specific reset method for Chelsio T4-based adapters.
+ */
+static int reset_chelsio_generic_dev(struct pci_dev *dev, int probe)
+{
+       u16 old_command;
+       u16 status, msix_flags;
+       int i, rc, msix_pos;
+
+       /*
+        * If this isn't a Chelsio T4-based device, return -ENOTTY

indicating

drivers/pci/quirks.c +14 -0
+        * that we have no device-specific reset method.
+        */
+       if ((dev->device & 0xf000) != 0x4000)
+               return -ENOTTY;
+
+       /*
+        * If this is the "probe" phase, return 0 indicating that we can
+        * reset this device.
+        */
+       if (probe)
+               return 0;
+
+       /*
+        * T4 can wedge if their are DMAs in flight within the chip and

Bus
+ * master has been disabled. We need to have it on till the
Function

drivers/pci/quirks.c +14 -0
+        * Level Reset completes.
+        */
+       pci_read_config_word(dev, PCI_COMMAND, &old_command);
+       pci_write_config_word(dev, PCI_COMMAND,
+                             old_command | PCI_COMMAND_MASTER);
+
+       /*
+        * Perform the actual device function reset, saving and restoring
+        * configuration information around the reset.
+        */
+       pci_save_state(dev);
+
+       /*
+        * T4 also suffers a Head-Of-Line blocking problem if MSI-X

interrupts
+ * are disabled when an MSI-X interrupt message needs to be
delivered.

Quoted 8 lines

drivers/pci/quirks.c reset_complete: · line 3256

Quoted 6 lines

+ * So we briefly re-enable MSI-X interrupts for the duration of
the

drivers/pci/quirks.c +4 -0
+        * FLR.  The pci_restore_state() below will restore the original
+        * MSI-X state.
+        */
+       msix_pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);

dev->msix_cap contains the capability offset already.

Thanks. I didn't know about that.

Quoted 30 lines
Quoted 12 lines
drivers/pci/quirks.c +9 -0
+       pci_read_config_word(dev, msix_pos+PCI_MSIX_FLAGS, &msix_flags);
+       if ((msix_flags & PCI_MSIX_FLAGS_ENABLE) == 0)
+               pci_write_config_word(dev, msix_pos+PCI_MSIX_FLAGS,
+                                     msix_flags |
+                                     PCI_MSIX_FLAGS_ENABLE |
+                                     PCI_MSIX_FLAGS_MASKALL);
+
+       /*
+        * This reset code is a copy of the guts of pcie_flr() because

that's

drivers/pci/quirks.c +2 -0
+        * not an exported function.
+        */

drivers/pci/quirks.c reset_complete: · line 3277

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+
+       /* Wait for Transaction Pending bit clean */
+       for (i = 0; i < 4; i++) {
+               if (i)
+                       msleep((1 << (i - 1)) * 100);
+
+               pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
+               if (!(status & PCI_EXP_DEVSTA_TRPND))
+                       goto clear;
+       }

This is at least the fifth copy of this loop:

   pcie_flr()
   pci_af_flr()
   bnx2x_do_flr()
   reset_intel_82599_sfp_virtfn()
Quoted 8 lines
Quoted 3 lines

Can we factor this out and just implement it once? Maybe even make it smart enough to look at the PCIe DEVSTA if it exists, and fall back to using PCI_AF_STATUS register if *it* exists?

Should such a task be taken up in the patch we're trying to submit or should it be a follow on patch? I had thought that, in general, patches were supposed to essentially do "one thing." Thanks for your insight on this.

You're right. The *ideal* thing would be a series like this:

1 add pci_wait_for_pending_transactions() and use it in drivers/pci 2 convert bnx2x to use it
3 chelsio patch (this one) using it

But I wouldn't hold your feet to the fire about the bnx2x one because that's outside of your control. It's just good citizenship if you can propose the patch.

Quoted 33 lines
Quoted 25 lines
Quoted 18 lines
drivers/pci/quirks.c +2 -0
+
+       dev_err(&dev->dev, "transaction is not cleared; proceeding with

reset anyway\n");

drivers/pci/quirks.c +3 -0
+
+clear:
+       pcie_capability_set_word(dev, PCI_EXP_DEVCTL,

PCI_EXP_DEVCTL_BCR_FLR);

drivers/pci/quirks.c +11 -0
+       msleep(100);
+
+       /*
+        * End of pcie_flr() code sequence.
+        */
+
+       rc = 0;
+       pci_restore_state(dev);
+
+       /*
+        * Restore the original PCI Configuration Space Command word
Quoted 2 lines

(which
+ * probably had Bus Master disabled).

Why was Bus Master probably originally disabled? I don't see anything in the pci_reset_function() path that would have disabled it before getting to this function.

Quoted 2 lines

But I don't think you need the comment anyway. "Probably had Bus Master disabled" isn't anything one can rely on.

Sorry, I probably should have included our example. In this case it's coming from KVM's kvm_free_assigned_device() routine which calls pci_reset_function() where we do:

Quoted 5 lines

/*
* both INTx and MSI are disabled after the Interrupt Disable bit
* is set and the Bus Master bit is cleared.
*/
pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);

Wow, I must be blind to have missed that :) Personally I would just drop the whole comment and not worry about it.

Bjorn