[PATCH v2 1/5] PCI: Use pcie_get_link_speed() in pci.c

Hans Zhang 18255117159 at 163.com
Mon Nov 9 07:06:36 PST 2026



On 11/9/26 20:03, Ilpo Järvinen wrote:
> On Sun, 8 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
>> with bounds checking and is already exported.
>>
>> Signed-off-by: Hans Zhang <18255117159 at 163.com>
>> ---
>>   drivers/pci/pci.c | 9 ++-------
>>   1 file changed, 2 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
>> index 7c6f2aba8144..92ede1768caf 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
>> @@ -6048,7 +6043,7 @@ int pcie_link_bandwidth_mbps(struct pci_dev *pdev)
>>   	if (err)
>>   		return pcibios_err_to_errno(err);
>>   
>> -	speed = pcie_dev_speed_mbps(to_pcie_link_speed(lnksta));
>> +	speed = pcie_dev_speed_mbps(pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta)));
> 
> Readability seems worse due to cramming everything into a single
> statement.

Hello Ilpo,

Agreed. I'll split the link speed class into a separate local variable
at both call sites, keeping lnksta intact for the link width extraction.

I'll send v3 of the series with this change.


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);




Best regards,
Hans



> 
>>   	if (speed < 0)
>>   		return speed;
>>   
>> @@ -6089,7 +6084,7 @@ 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);
>> +		next_speed = pcie_get_link_speed(FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta));
>>   		next_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
>>   
>>   		next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
>>
> 




More information about the Linux-mediatek mailing list