Skip to contentSkip to conversation

skd/mtip32xx: Prefer pcie_capability_read_word()

PATCH In review Frederick Lawler 2 participants 10 messages Nov 18, 2019 – Nov 19, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover skd/mtip32xx: Prefer pcie_capability_read_word()

Hi,

These changes are in reference to Bjorn's suggestions here:
https://lore.kernel.org/lkml/CAErSpo6BmWDxi3Vckm03=fZHEUosTgkMUjqCvYRA186jv8XbFw@mail.gmail.com/
https://lore.kernel.org/lkml/CAErSpo5TZC3iM09SB1td+F7b-+aiu9EHwPYa1ayiU-i1tseV5w@mail.gmail.com/

And the patches are rebased ontop of 54ecb8f7028c ("Linux 5.4-rc1")

Changes since v1:
* Replace magic numbers constants

This is a long time coming, apologies for dragging my feet on getting these trivial patches out.

Frederick Lawler (4):
skd: Prefer pcie_capability_read_word()
skd: Replace magic numbers with PCI constants
mtip32xx: Prefer pcie_capability_read_word()
mtip32xx: Replace magic numbers with PCI constants

 drivers/block/mtip32xx/mtip32xx.c | 28 ++++++++++++----------------
 drivers/block/skd_main.c          | 14 ++++++--------
 2 files changed, 18 insertions(+), 24 deletions(-)

--
2.20.1

#1 lore raw

PATCH v2 1/4 skd: Prefer pcie_capability_read_word()

Commit 8c0d3a02c130 ("PCI: Add accessors for PCI Express Capability") added accessors for the PCI Express Capability so that drivers didn't need to be aware of differences between v1 and v2 of the PCI Express Capability.

Replace pci_read_config_word() and pci_write_config_word() calls with pcie_capability_read_word() and pcie_capability_write_word().

---
 drivers/block/skd_main.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/skd_main.c +2 -6
index 51569c199a6c..f25f6ef6b4c7 100644
@@ -3134,18 +3134,14 @@ MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
 
 static char *skd_pci_info(struct skd_device *skdev, char *str)
 {
-	int pcie_reg;
-
 	strcpy(str, "PCIe (");
-	pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
 
-	if (pcie_reg) {
+	if (pci_is_pcie(skdev->pdev)) {
 
 		char lwstr[6];
 		uint16_t pcie_lstat, lspeed, lwidth;
 
-		pcie_reg += 0x12;
-		pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
+		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
 		lspeed = pcie_lstat & (0xF);
 		lwidth = (pcie_lstat & 0x3F0) >> 4;
 
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v2 1/4] skd: Prefer pcie_capability_read_word()

Quoted 18 lines · Frederick Lawler

Commit 8c0d3a02c130 ("PCI: Add accessors for PCI Express Capability") added accessors for the PCI Express Capability so that drivers didn't need to be aware of differences between v1 and v2 of the PCI Express Capability.

Replace pci_read_config_word() and pci_write_config_word() calls with pcie_capability_read_word() and pcie_capability_write_word().

---
 drivers/block/skd_main.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/skd_main.c +0 -0
index 51569c199a6c..f25f6ef6b4c7 100644

drivers/block/skd_main.c MODULE_DEVICE_TABLE(pci, skd_pci_tbl); · line 3139

Quoted 10 lines
drivers/block/skd_main.c +1 -4
@@ -3134,18 +3134,14 @@ MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
 
 static char *skd_pci_info(struct skd_device *skdev, char *str)
 {
-	int pcie_reg;
-
 	strcpy(str, "PCIe (");
-	pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
 
-	if (pcie_reg) {
+	if (pci_is_pcie(skdev->pdev)) {

Maybe return early here ? The reason is that if pci_is_pcie() is false, then the string "PCIe (" is not terminated with a closing parenthesis. So something like:

	if (!pci_is_pcie(skdev->pdev)) {
		strcat(str, ")");
		return str;
	}

May be better. Note that the "return str" is also rather pointless since it is not used by the caller of skd_pci_info().

Quoted 11 lines
drivers/block/skd_main.c +1 -2
 		char lwstr[6];
 		uint16_t pcie_lstat, lspeed, lwidth;
 
-		pcie_reg += 0x12;
-		pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
+		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
 		lspeed = pcie_lstat & (0xF);
 		lwidth = (pcie_lstat & 0x3F0) >> 4;
 
 
Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw

Re: [PATCH v2 1/4] skd: Prefer pcie_capability_read_word()

Damien Le Moal wrote on 11/17/19 7:45 PM:

Quoted 34 lines · Frederick Lawler
Quoted 18 lines

Commit 8c0d3a02c130 ("PCI: Add accessors for PCI Express Capability") added accessors for the PCI Express Capability so that drivers didn't need to be aware of differences between v1 and v2 of the PCI Express Capability.

Replace pci_read_config_word() and pci_write_config_word() calls with pcie_capability_read_word() and pcie_capability_write_word().

---
  drivers/block/skd_main.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/skd_main.c +0 -0
index 51569c199a6c..f25f6ef6b4c7 100644

drivers/block/skd_main.c MODULE_DEVICE_TABLE(pci, skd_pci_tbl); · line 3139

Quoted 10 lines
drivers/block/skd_main.c +1 -4
@@ -3134,18 +3134,14 @@ MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
 
  static char *skd_pci_info(struct skd_device *skdev, char *str)
  {
-	int pcie_reg;
-
  	strcpy(str, "PCIe (");
-	pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
 
-	if (pcie_reg) {
+	if (pci_is_pcie(skdev->pdev)) {

Maybe return early here ? The reason is that if pci_is_pcie() is false, then the string "PCIe (" is not terminated with a closing parenthesis. So something like:

Quoted 8 lines
	if (!pci_is_pcie(skdev->pdev)) {
		strcat(str, ")");
		return str;
	}

May be better. Note that the "return str" is also rather pointless since it is not used by the caller of skd_pci_info().

I agree, but it might be better to include a note that this is not a PCIe device. Reading "PCIe ()" seems weird to me.

Quoted 4 lines
Quoted 4 lines
drivers/block/skd_main.c +0 -0
  		char lwstr[6];
  		uint16_t pcie_lstat, lspeed, lwidth;
 
Quoted 9 lines

drivers/block/skd_main.c MODULE_DEVICE_TABLE(pci, skd_pci_tbl);

Quoted 7 lines
drivers/block/skd_main.c +1 -2
-		pcie_reg += 0x12;
-		pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
+		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
  		lspeed = pcie_lstat & (0xF);
  		lwidth = (pcie_lstat & 0x3F0) >> 4;
 
 

Thanks,
Frederick Lawler

#4 lore raw

Re: [PATCH v2 1/4] skd: Prefer pcie_capability_read_word()

Quoted 40 lines · Frederick Lawler

Damien Le Moal wrote on 11/17/19 7:45 PM:

Quoted 39 lines · Frederick Lawler
Quoted 18 lines

Commit 8c0d3a02c130 ("PCI: Add accessors for PCI Express Capability") added accessors for the PCI Express Capability so that drivers didn't need to be aware of differences between v1 and v2 of the PCI Express Capability.

Replace pci_read_config_word() and pci_write_config_word() calls with pcie_capability_read_word() and pcie_capability_write_word().

---
  drivers/block/skd_main.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/skd_main.c +0 -0
index 51569c199a6c..f25f6ef6b4c7 100644

drivers/block/skd_main.c MODULE_DEVICE_TABLE(pci, skd_pci_tbl); · line 3139

Quoted 10 lines
drivers/block/skd_main.c +1 -4
@@ -3134,18 +3134,14 @@ MODULE_DEVICE_TABLE(pci, skd_pci_tbl);
 
  static char *skd_pci_info(struct skd_device *skdev, char *str)
  {
-	int pcie_reg;
-
  	strcpy(str, "PCIe (");
-	pcie_reg = pci_find_capability(skdev->pdev, PCI_CAP_ID_EXP);
 
-	if (pcie_reg) {
+	if (pci_is_pcie(skdev->pdev)) {

Maybe return early here ? The reason is that if pci_is_pcie() is false, then the string "PCIe (" is not terminated with a closing parenthesis. So something like:

	if (!pci_is_pcie(skdev->pdev)) {
		strcat(str, ")");
		return str;
	}
Quoted 6 lines
Quoted 3 lines

May be better. Note that the "return str" is also rather pointless since it is not used by the caller of skd_pci_info().

I agree, but it might be better to include a note that this is not a PCIe device. Reading "PCIe ()" seems weird to me.

Yes indeed. But since this device is PCIe only, if pci_is_pcie(skdev->pdev) == false, we should probably fail the driver initialization.

Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
drivers/block/skd_main.c +0 -0
  		char lwstr[6];
  		uint16_t pcie_lstat, lspeed, lwidth;
 
Quoted 9 lines

drivers/block/skd_main.c MODULE_DEVICE_TABLE(pci, skd_pci_tbl);

Quoted 7 lines
drivers/block/skd_main.c +1 -2
-		pcie_reg += 0x12;
-		pci_read_config_word(skdev->pdev, pcie_reg, &pcie_lstat);
+		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
  		lspeed = pcie_lstat & (0xF);
  		lwidth = (pcie_lstat & 0x3F0) >> 4;
 
 

Thanks,
Frederick Lawler

Signature · 2 lines
Damien Le Moal
Western Digital Research
#5 lore raw

PATCH v2 2/4 skd: Replace magic numbers with PCI constants

In reply to Frederick Lawler

Readability was improved by replacing pci_read_config_word() with pcie_capability_read_word(). Take that a step further by replacing magic numbers with PCI reg constants.

No functional change intended.

---
v2
- Added this patch

---
 drivers/block/skd_main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/skd_main.c +5 -3
index f25f6ef6b4c7..7f8243573ad9 100644
@@ -3141,9 +3141,11 @@ static char *skd_pci_info(struct skd_device *skdev, char *str)
 		char lwstr[6];
 		uint16_t pcie_lstat, lspeed, lwidth;
 
-		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
-		lspeed = pcie_lstat & (0xF);
-		lwidth = (pcie_lstat & 0x3F0) >> 4;
+		pcie_capability_read_word(skdev->pdev, PCI_EXP_LNKSTA,
+					  &pcie_lstat);
+		lspeed = pcie_lstat & PCI_EXP_LNKSTA_CLS;
+		lwidth = (pcie_lstat & PCI_EXP_LNKSTA_NLW) >>
+			 PCI_EXP_LNKSTA_NLW_SHIFT;
 
 		if (lspeed == 1)
 			strcat(str, "2.5GT/s ");
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH v2 2/4] skd: Replace magic numbers with PCI constants

Quoted 24 lines · Frederick Lawler

Readability was improved by replacing pci_read_config_word() with pcie_capability_read_word(). Take that a step further by replacing magic numbers with PCI reg constants.

No functional change intended.

---
v2
- Added this patch

---
 drivers/block/skd_main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/skd_main.c +0 -1
index f25f6ef6b4c7..7f8243573ad9 100644
@@ -3141,9 +3141,11 @@ static char *skd_pci_info(struct skd_device *skdev, char *str)
 		char lwstr[6];
 		uint16_t pcie_lstat, lspeed, lwidth;
 
-		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);

drivers/block/skd_main.c static char *skd_pci_info(struct skd_device *skdev, char *str) · line 3151

Quoted 10 lines
drivers/block/skd_main.c +5 -2
-		lspeed = pcie_lstat & (0xF);
-		lwidth = (pcie_lstat & 0x3F0) >> 4;
+		pcie_capability_read_word(skdev->pdev, PCI_EXP_LNKSTA,
+					  &pcie_lstat);
+		lspeed = pcie_lstat & PCI_EXP_LNKSTA_CLS;
+		lwidth = (pcie_lstat & PCI_EXP_LNKSTA_NLW) >>
+			 PCI_EXP_LNKSTA_NLW_SHIFT;
 
 		if (lspeed == 1)
 			strcat(str, "2.5GT/s ");

Looks OK to me. But since this is changing again one line that is added by patch 1/4, why not make patch 1 and this patch changes a single patch ?

Signature · 2 lines
Damien Le Moal
Western Digital Research
#7 lore raw

Re: [PATCH v2 2/4] skd: Replace magic numbers with PCI constants

Hi,

Damien Le Moal wrote on 11/17/19 7:45 PM:

Quoted 32 lines · Frederick Lawler
Quoted 31 lines

Readability was improved by replacing pci_read_config_word() with pcie_capability_read_word(). Take that a step further by replacing magic numbers with PCI reg constants.

No functional change intended.

---
v2
- Added this patch

---
  drivers/block/skd_main.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/skd_main.c +5 -3
index f25f6ef6b4c7..7f8243573ad9 100644
@@ -3141,9 +3141,11 @@ static char *skd_pci_info(struct skd_device *skdev, char *str)
  		char lwstr[6];
  		uint16_t pcie_lstat, lspeed, lwidth;
 
-		pcie_capability_read_word(skdev->pdev, 0x12, &pcie_lstat);
-		lspeed = pcie_lstat & (0xF);
-		lwidth = (pcie_lstat & 0x3F0) >> 4;
+		pcie_capability_read_word(skdev->pdev, PCI_EXP_LNKSTA,
+					  &pcie_lstat);
+		lspeed = pcie_lstat & PCI_EXP_LNKSTA_CLS;
+		lwidth = (pcie_lstat & PCI_EXP_LNKSTA_NLW) >>
+			 PCI_EXP_LNKSTA_NLW_SHIFT;
 
Quoted 7 lines

drivers/block/skd_main.c static char *skd_pci_info(struct skd_device *skdev, char *str) · line 3151

Quoted 3 lines
drivers/block/skd_main.c +0 -0
  		if (lspeed == 1)
  			strcat(str, "2.5GT/s ");

Looks OK to me. But since this is changing again one line that is added by patch 1/4, why not make patch 1 and this patch changes a single patch ?

Works for me.

Thanks,
Frederick Lawler

#8 lore raw

PATCH v2 3/4 mtip32xx: Prefer pcie_capability_read_word()

In reply to Frederick Lawler

Commit 8c0d3a02c130 ("PCI: Add accessors for PCI Express Capability") added accessors for the PCI Express Capability so that drivers didn't need to be aware of differences between v1 and v2 of the PCI Express Capability.

Replace pci_read_config_word() and pci_write_config_word() calls with pcie_capability_read_word() and pcie_capability_write_word().

---
 drivers/block/mtip32xx/mtip32xx.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
drivers/block/mtip32xx/mtip32xx.c +12 -16
index 964f78cfffa0..35703dc98e25 100644
@@ -3938,22 +3938,18 @@ static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
 	int pos;
 	unsigned short pcie_dev_ctrl;
 
-	pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
-	if (pos) {
-		pci_read_config_word(pdev,
-			pos + PCI_EXP_DEVCTL,
-			&pcie_dev_ctrl);
-		if (pcie_dev_ctrl & (1 << 11) ||
-		    pcie_dev_ctrl & (1 << 4)) {
-			dev_info(&dd->pdev->dev,
-				"Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
-					pdev->vendor, pdev->device);
-			pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
-						PCI_EXP_DEVCTL_RELAX_EN);
-			pci_write_config_word(pdev,
-				pos + PCI_EXP_DEVCTL,
-				pcie_dev_ctrl);
-		}
+	if (!pci_is_pcie(pdev))
+		return;
+
+	pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &pcie_dev_ctrl);
+	if (pcie_dev_ctrl & (1 << 11) ||
+	    pcie_dev_ctrl & (1 << 4)) {
+		dev_info(&dd->pdev->dev,
+			 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
+			 pdev->vendor, pdev->device);
+		pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
+					PCI_EXP_DEVCTL_RELAX_EN);
+		pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, pcie_dev_ctrl);
 	}
 }
 
Signature · 1 line
2.20.1
#9 lore raw

PATCH v2 4/4 mtip32xx: Replace magic numbers with PCI constants

In reply to Frederick Lawler

Readability was improved by replacing pci_read_config_word() with pcie_capability_read_word(). Take that a step further by replacing magic numbers with PCI reg constants.

No functional change intended.

---
v2:
- Added this patch

---
 drivers/block/mtip32xx/mtip32xx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/mtip32xx/mtip32xx.c +2 -2
index 35703dc98e25..225c6ae62385 100644
@@ -3942,8 +3942,8 @@ static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
 		return;
 
 	pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &pcie_dev_ctrl);
-	if (pcie_dev_ctrl & (1 << 11) ||
-	    pcie_dev_ctrl & (1 << 4)) {
+	if (pcie_dev_ctrl & PCI_EXP_DEVCTL_NOSNOOP_EN ||
+	    pcie_dev_ctrl & PCI_EXP_DEVCTL_RELAX_EN) {
 		dev_info(&dd->pdev->dev,
 			 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
 			 pdev->vendor, pdev->device);
Signature · 1 line
2.20.1