* [PATCH v3 1/5] PCI: Use pcie_get_link_speed() in pci.c
2026-11-12 2:24 [PATCH v3 0/5] PCI: Encapsulate pcie_link_speed array access Hans Zhang
@ 2026-11-12 2:24 ` Hans Zhang
2026-11-12 10:25 ` Ilpo Järvinen
2026-11-12 2:24 ` [PATCH v3 2/5] PCI: Convert remaining core pcie_link_speed[] users Hans Zhang
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Hans Zhang @ 2026-11-12 2:24 UTC (permalink / raw)
To: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh
Cc: robh, linux-pci, linux-kernel, linux-mediatek, linux-tegra, Hans Zhang
Replace the to_pcie_link_speed() wrapper with pcie_get_link_speed() in
pcie_link_bandwidth_mbps() and pcie_bandwidth_available(), and remove
the now-unused wrapper. pcie_get_link_speed() performs the same lookup
with bounds checking and is already exported.
Signed-off-by: Hans Zhang <18255117159@163.com>
---
drivers/pci/pci.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7c6f2aba8144..594ee7aa4cad 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -6025,11 +6025,6 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
}
EXPORT_SYMBOL(pcie_set_mps);
-static enum pci_bus_speed to_pcie_link_speed(u16 lnksta)
-{
- return pcie_link_speed[FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta)];
-}
-
/**
* pcie_link_bandwidth_mbps - get the current bandwidth of a single PCIe link
* @pdev: PCI device to query
@@ -6041,14 +6036,15 @@ static enum pci_bus_speed to_pcie_link_speed(u16 lnksta)
*/
int pcie_link_bandwidth_mbps(struct pci_dev *pdev)
{
- u16 lnksta;
+ u16 lnksta, lnksta_cls;
int speed, err;
err = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
if (err)
return pcibios_err_to_errno(err);
- speed = pcie_dev_speed_mbps(to_pcie_link_speed(lnksta));
+ lnksta_cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
+ speed = pcie_dev_speed_mbps(pcie_get_link_speed(lnksta_cls));
if (speed < 0)
return speed;
@@ -6074,7 +6070,7 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
enum pci_bus_speed *speed,
enum pcie_link_width *width)
{
- u16 lnksta;
+ u16 lnksta, lnksta_cls;
enum pci_bus_speed next_speed;
enum pcie_link_width next_width;
u32 bw, next_bw;
@@ -6089,7 +6085,8 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
while (dev) {
pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
- next_speed = to_pcie_link_speed(lnksta);
+ lnksta_cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
+ next_speed = pcie_get_link_speed(lnksta_cls);
next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v3 1/5] PCI: Use pcie_get_link_speed() in pci.c
2026-11-12 2:24 ` [PATCH v3 1/5] PCI: Use pcie_get_link_speed() in pci.c Hans Zhang
@ 2026-11-12 10:25 ` Ilpo Järvinen
0 siblings, 0 replies; 7+ messages in thread
From: Ilpo Järvinen @ 2026-11-12 10:25 UTC (permalink / raw)
To: Hans Zhang
Cc: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh, robh, linux-pci, LKML, linux-mediatek,
linux-tegra
[-- Attachment #1: Type: text/plain, Size: 2612 bytes --]
On Thu, 12 Nov 2026, Hans Zhang wrote:
> Replace the to_pcie_link_speed() wrapper with pcie_get_link_speed() in
> pcie_link_bandwidth_mbps() and pcie_bandwidth_available(), and remove
> the now-unused wrapper. pcie_get_link_speed() performs the same lookup
now unused
I suppose Bjorn can take care of that while applying so no need to send
another version because of this alone.
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
--
i.
> with bounds checking and is already exported.
>
> Signed-off-by: Hans Zhang <18255117159@163.com>
> ---
> drivers/pci/pci.c | 15 ++++++---------
> 1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7c6f2aba8144..594ee7aa4cad 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -6025,11 +6025,6 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
> }
> EXPORT_SYMBOL(pcie_set_mps);
>
> -static enum pci_bus_speed to_pcie_link_speed(u16 lnksta)
> -{
> - return pcie_link_speed[FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta)];
> -}
> -
> /**
> * pcie_link_bandwidth_mbps - get the current bandwidth of a single PCIe link
> * @pdev: PCI device to query
> @@ -6041,14 +6036,15 @@ static enum pci_bus_speed to_pcie_link_speed(u16 lnksta)
> */
> int pcie_link_bandwidth_mbps(struct pci_dev *pdev)
> {
> - u16 lnksta;
> + u16 lnksta, lnksta_cls;
> int speed, err;
>
> err = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
> if (err)
> return pcibios_err_to_errno(err);
>
> - speed = pcie_dev_speed_mbps(to_pcie_link_speed(lnksta));
> + lnksta_cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
> + speed = pcie_dev_speed_mbps(pcie_get_link_speed(lnksta_cls));
> if (speed < 0)
> return speed;
>
> @@ -6074,7 +6070,7 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
> enum pci_bus_speed *speed,
> enum pcie_link_width *width)
> {
> - u16 lnksta;
> + u16 lnksta, lnksta_cls;
> enum pci_bus_speed next_speed;
> enum pcie_link_width next_width;
> u32 bw, next_bw;
> @@ -6089,7 +6085,8 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
> while (dev) {
> pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
>
> - next_speed = to_pcie_link_speed(lnksta);
> + lnksta_cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
> + next_speed = pcie_get_link_speed(lnksta_cls);
> next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
>
> next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 2/5] PCI: Convert remaining core pcie_link_speed[] users
2026-11-12 2:24 [PATCH v3 0/5] PCI: Encapsulate pcie_link_speed array access Hans Zhang
2026-11-12 2:24 ` [PATCH v3 1/5] PCI: Use pcie_get_link_speed() in pci.c Hans Zhang
@ 2026-11-12 2:24 ` Hans Zhang
2026-11-12 2:24 ` [PATCH v3 3/5] PCI: tegra264: Use pcie_get_link_speed() Hans Zhang
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Hans Zhang @ 2026-11-12 2:24 UTC (permalink / raw)
To: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh
Cc: robh, linux-pci, linux-kernel, linux-mediatek, linux-tegra, Hans Zhang
Convert the remaining direct pcie_link_speed[] accesses in pci-sysfs.c,
the __pcie_update_link_speed() inline helper, and pci_set_bus_speed() in
probe.c to pcie_get_link_speed(). This includes the max_bus_speed
lookup that was missed in v1.
Signed-off-by: Hans Zhang <18255117159@163.com>
---
drivers/pci/pci-sysfs.c | 2 +-
drivers/pci/pci.h | 2 +-
drivers/pci/probe.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 6669e9ad5bd3..30db3349a44c 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -234,7 +234,7 @@ static ssize_t current_link_speed_show(struct device *dev,
if (err)
return -EINVAL;
- speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
+ speed = pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKSTA_CLS, linkstat));
return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960..5441741bc330 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -692,7 +692,7 @@ static inline void __pcie_update_link_speed(struct pci_bus *bus,
enum pcie_link_change_reason reason,
u16 linksta, u16 linksta2)
{
- bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
+ bus->cur_bus_speed = pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKSTA_CLS, linksta));
bus->flit_mode = (linksta2 & PCI_EXP_LNKSTA2_FLIT) ? 1 : 0;
trace_pcie_link_event(bus,
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..d0283a8158f2 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -924,7 +924,7 @@ static void pci_set_bus_speed(struct pci_bus *bus)
u32 linkcap;
pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
- bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
+ bus->max_bus_speed = pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKCAP_SLS, linkcap));
pcie_update_link_speed(bus, PCIE_ADD_BUS);
}
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 3/5] PCI: tegra264: Use pcie_get_link_speed()
2026-11-12 2:24 [PATCH v3 0/5] PCI: Encapsulate pcie_link_speed array access Hans Zhang
2026-11-12 2:24 ` [PATCH v3 1/5] PCI: Use pcie_get_link_speed() in pci.c Hans Zhang
2026-11-12 2:24 ` [PATCH v3 2/5] PCI: Convert remaining core pcie_link_speed[] users Hans Zhang
@ 2026-11-12 2:24 ` Hans Zhang
2026-11-12 2:24 ` [PATCH v3 4/5] PCI: mediatek: " Hans Zhang
2026-11-12 2:24 ` [PATCH v3 5/5] PCI: Make pcie_link_speed[] private to probe.c Hans Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Hans Zhang @ 2026-11-12 2:24 UTC (permalink / raw)
To: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh
Cc: robh, linux-pci, linux-kernel, linux-mediatek, linux-tegra, Hans Zhang
Tegra264 was added after pcie_get_link_speed() was introduced and still
indexes pcie_link_speed[] directly. Convert it to the helper so it does
not depend on the array being exported and cannot go out of bounds on
invalid speed values.
Signed-off-by: Hans Zhang <18255117159@163.com>
---
drivers/pci/controller/pcie-tegra264.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/controller/pcie-tegra264.c b/drivers/pci/controller/pcie-tegra264.c
index c97576ab4a31..711b66287681 100644
--- a/drivers/pci/controller/pcie-tegra264.c
+++ b/drivers/pci/controller/pcie-tegra264.c
@@ -126,7 +126,7 @@ static void tegra264_pcie_icc_set(struct tegra264_pcie *pcie)
width = FIELD_GET(PCI_EXP_LNKCAP_MLW, value);
}
- value = Mbps_to_icc(width * PCIE_SPEED2MBS_ENC(pcie_link_speed[speed]));
+ value = Mbps_to_icc(width * PCIE_SPEED2MBS_ENC(pcie_get_link_speed(speed)));
/*
* We don't want to error out here because a boot-critical device
@@ -266,8 +266,8 @@ static bool tegra264_pcie_link_up(struct tegra264_pcie *pcie,
if (value & PCI_EXP_LNKSTA_DLLLA) {
if (speed)
- *speed = pcie_link_speed[FIELD_GET(PCI_EXP_LNKSTA_CLS,
- value)];
+ *speed = pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKSTA_CLS,
+ value));
return true;
}
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 4/5] PCI: mediatek: Use pcie_get_link_speed()
2026-11-12 2:24 [PATCH v3 0/5] PCI: Encapsulate pcie_link_speed array access Hans Zhang
` (2 preceding siblings ...)
2026-11-12 2:24 ` [PATCH v3 3/5] PCI: tegra264: Use pcie_get_link_speed() Hans Zhang
@ 2026-11-12 2:24 ` Hans Zhang
2026-11-12 2:24 ` [PATCH v3 5/5] PCI: Make pcie_link_speed[] private to probe.c Hans Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Hans Zhang @ 2026-11-12 2:24 UTC (permalink / raw)
To: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh
Cc: robh, linux-pci, linux-kernel, linux-mediatek, linux-tegra, Hans Zhang
MediaTek was added after pcie_get_link_speed() was introduced and still
indexes pcie_link_speed[] directly. Convert it to the helper so it does
not depend on the array being exported and cannot go out of bounds on
invalid speed values.
Signed-off-by: Hans Zhang <18255117159@163.com>
---
drivers/pci/controller/pcie-mediatek.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
index 3a3e91cd0c21..ffde365c987f 100644
--- a/drivers/pci/controller/pcie-mediatek.c
+++ b/drivers/pci/controller/pcie-mediatek.c
@@ -888,7 +888,7 @@ static int mtk_pcie_retrain(struct device *dev, struct pci_host_bridge *host)
speed = lnksta & PCI_EXP_LNKSTA_CLS;
pci_info(rp, "link retrained, speed %s\n",
- pci_speed_string(pcie_link_speed[speed]));
+ pci_speed_string(pcie_get_link_speed(speed)));
}
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v3 5/5] PCI: Make pcie_link_speed[] private to probe.c
2026-11-12 2:24 [PATCH v3 0/5] PCI: Encapsulate pcie_link_speed array access Hans Zhang
` (3 preceding siblings ...)
2026-11-12 2:24 ` [PATCH v3 4/5] PCI: mediatek: " Hans Zhang
@ 2026-11-12 2:24 ` Hans Zhang
4 siblings, 0 replies; 7+ messages in thread
From: Hans Zhang @ 2026-11-12 2:24 UTC (permalink / raw)
To: bhelgaas, mani, lpieralisi, kwilczynski, thierry.reding,
ryder.lee, jonathanh
Cc: robh, linux-pci, linux-kernel, linux-mediatek, linux-tegra, Hans Zhang
pcie_get_link_speed() is now the only accessor for pcie_link_speed[].
Make the array static and remove its extern declaration and export so no
other code can index it directly.
Signed-off-by: Hans Zhang <18255117159@163.com>
---
drivers/pci/pci.h | 1 -
drivers/pci/probe.c | 4 +---
2 files changed, 1 insertion(+), 4 deletions(-)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 5441741bc330..04bdf3b86a56 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -118,7 +118,6 @@ struct pcie_tlp_log;
#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
-extern const unsigned char pcie_link_speed[];
unsigned char pcie_get_link_speed(unsigned int speed);
extern bool pci_early_dump;
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index d0283a8158f2..b2d69de00041 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -756,7 +756,7 @@ static const unsigned char pcix_bus_speed[] = {
};
/* Indexed by PCI_EXP_LNKCAP_SLS, PCI_EXP_LNKSTA_CLS */
-const unsigned char pcie_link_speed[] = {
+static const unsigned char pcie_link_speed[] = {
PCI_SPEED_UNKNOWN, /* 0 */
PCIE_SPEED_2_5GT, /* 1 */
PCIE_SPEED_5_0GT, /* 2 */
@@ -774,8 +774,6 @@ const unsigned char pcie_link_speed[] = {
PCI_SPEED_UNKNOWN, /* E */
PCI_SPEED_UNKNOWN /* F */
};
-EXPORT_SYMBOL_GPL(pcie_link_speed);
-
/**
* pcie_get_link_speed - Get speed value from PCIe generation number
* @speed: PCIe speed (1-based: 1 = 2.5GT, 2 = 5GT, ...)
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread