mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] PCI: Correct PCIe port type field encoding
@ 2026-11-19 12:19 Gokulkrishna Sekar
  2026-11-19 17:33 ` Bjorn Helgaas
  0 siblings, 1 reply; 2+ messages in thread
From: Gokulkrishna Sekar @ 2026-11-19 12:19 UTC (permalink / raw)
  To: linux-pci
  Cc: bhelgaas, mika.westerberg, linux-kernel, Gokulkrishna Sekar,
	Gowthami Thiagarajan

set_pcie_port_type() validates PCIe port topology and corrects
invalid upstream/downstream port type information in the cached
pcie_flags_reg value.

When performing these corrections, the code updates
PCI_EXP_FLAGS_TYPE using PCI_EXP_TYPE_UPSTREAM or
PCI_EXP_TYPE_DOWNSTREAM directly. The PCI_EXP_TYPE_* values are
decoded port type numbers, not values encoded for the
PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the
wrong bit position.

As a result, a device whose type has been corrected may subsequently
be reported as PCI_EXP_TYPE_ENDPOINT instead of the intended
upstream or downstream port type.

Use FIELD_MODIFY() to update PCI_EXP_FLAGS_TYPE so the corrected
port type is encoded consistently with the format used in the PCI
Express Capabilities Register.

Fixes: ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")
Link: https://lkml.org/lkml/2024/12/13/198
Reported-by: Gowthami Thiagarajan <gthiagarajan@marvell.com>
Signed-off-by: Gokulkrishna Sekar <Gokulkrishna.Sekar@harman.com>
---
v2:
 - Use FIELD_MODIFY() as suggested by Bjorn Helgaas
 - Clarify impact by describing the resulting PCI_EXP_TYPE_ENDPOINT
 - Add Fixes tag
 - Add Link tag to previous discussion
 - Add Reported-by

 drivers/pci/probe.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..93e2e1f688ca 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1673,8 +1673,7 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		 */
 		if (pcie_downstream_port(parent)) {
 			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
-			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
+			FIELD_MODIFY(PCI_EXP_FLAGS_TYPE, &pdev->pcie_flags_reg, PCI_EXP_TYPE_UPSTREAM);
 		}
 	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
 		/*
@@ -1684,8 +1683,7 @@ void set_pcie_port_type(struct pci_dev *pdev)
 		 */
 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
 			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
-			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
-			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
+			FIELD_MODIFY(PCI_EXP_FLAGS_TYPE, &pdev->pcie_flags_reg, PCI_EXP_TYPE_DOWNSTREAM);
 		}
 	}
 }
-- 
2.54.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] PCI: Correct PCIe port type field encoding
  2026-11-19 12:19 [PATCH v2] PCI: Correct PCIe port type field encoding Gokulkrishna Sekar
@ 2026-11-19 17:33 ` Bjorn Helgaas
  0 siblings, 0 replies; 2+ messages in thread
From: Bjorn Helgaas @ 2026-11-19 17:33 UTC (permalink / raw)
  To: Gokulkrishna Sekar
  Cc: linux-pci, bhelgaas, mika.westerberg, linux-kernel,
	Gokulkrishna Sekar, Gowthami Thiagarajan

On Thu, Nov 19, 2026 at 12:19:47PM +0000, Gokulkrishna Sekar wrote:
> set_pcie_port_type() validates PCIe port topology and corrects
> invalid upstream/downstream port type information in the cached
> pcie_flags_reg value.
> 
> When performing these corrections, the code updates
> PCI_EXP_FLAGS_TYPE using PCI_EXP_TYPE_UPSTREAM or
> PCI_EXP_TYPE_DOWNSTREAM directly. The PCI_EXP_TYPE_* values are
> decoded port type numbers, not values encoded for the
> PCI_EXP_FLAGS_TYPE bitfield, so the corrected type is stored in the
> wrong bit position.
> 
> As a result, a device whose type has been corrected may subsequently
> be reported as PCI_EXP_TYPE_ENDPOINT instead of the intended
> upstream or downstream port type.
> 
> Use FIELD_MODIFY() to update PCI_EXP_FLAGS_TYPE so the corrected
> port type is encoded consistently with the format used in the PCI
> Express Capabilities Register.
> 
> Fixes: ca78410403dd ("PCI: Get rid of dev->has_secondary_link flag")
> Link: https://lkml.org/lkml/2024/12/13/198

Patch looks good, but this link doesn't work for me.  Probably a
temporary issue with lkml.org, but https://lore.kernel.org links are
always better.  Looks like it might be
https://lore.kernel.org/all/20241213070241.3334854-1-gthiagarajan@marvell.com/?

> Reported-by: Gowthami Thiagarajan <gthiagarajan@marvell.com>
> Signed-off-by: Gokulkrishna Sekar <Gokulkrishna.Sekar@harman.com>
> ---
> v2:
>  - Use FIELD_MODIFY() as suggested by Bjorn Helgaas
>  - Clarify impact by describing the resulting PCI_EXP_TYPE_ENDPOINT
>  - Add Fixes tag
>  - Add Link tag to previous discussion
>  - Add Reported-by
> 
>  drivers/pci/probe.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 9a178e0e5f7f..93e2e1f688ca 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -1673,8 +1673,7 @@ void set_pcie_port_type(struct pci_dev *pdev)
>  		 */
>  		if (pcie_downstream_port(parent)) {
>  			pci_info(pdev, "claims to be downstream port but is acting as upstream port, correcting type\n");
> -			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
> -			pdev->pcie_flags_reg |= PCI_EXP_TYPE_UPSTREAM;
> +			FIELD_MODIFY(PCI_EXP_FLAGS_TYPE, &pdev->pcie_flags_reg, PCI_EXP_TYPE_UPSTREAM);
>  		}
>  	} else if (type == PCI_EXP_TYPE_UPSTREAM) {
>  		/*
> @@ -1684,8 +1683,7 @@ void set_pcie_port_type(struct pci_dev *pdev)
>  		 */
>  		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) {
>  			pci_info(pdev, "claims to be upstream port but is acting as downstream port, correcting type\n");
> -			pdev->pcie_flags_reg &= ~PCI_EXP_FLAGS_TYPE;
> -			pdev->pcie_flags_reg |= PCI_EXP_TYPE_DOWNSTREAM;
> +			FIELD_MODIFY(PCI_EXP_FLAGS_TYPE, &pdev->pcie_flags_reg, PCI_EXP_TYPE_DOWNSTREAM);
>  		}
>  	}
>  }
> -- 
> 2.54.0
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19 17:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 12:19 [PATCH v2] PCI: Correct PCIe port type field encoding Gokulkrishna Sekar
2026-11-19 17:33 ` Bjorn Helgaas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®