diff mbox series

[10/18] PCI: Export pcie_get_speed() using the code from sysfs PCI link speed show function

Message ID 167571665075.587790.11513782507200128278.stgit@djiang5-mobl3.local
State Superseded
Headers show
Series cxl: Add support for QTG ID retrieval for CXL subsystem | expand

Commit Message

Dave Jiang Feb. 6, 2023, 8:50 p.m. UTC
Move the logic in current_link_speed_show() to a common function and export
that functiuon as pcie_get_speed() to allow other drivers to to retrieve
the current negotiated link speed.

Signed-off-by: Dave Jiang <dave.jiang@intel.com>
---
 drivers/pci/pci-sysfs.c |   12 ++----------
 drivers/pci/pci.c       |   20 ++++++++++++++++++++
 include/linux/pci.h     |    1 +
 3 files changed, 23 insertions(+), 10 deletions(-)

Comments

Lukas Wunner Feb. 6, 2023, 10:27 p.m. UTC | #1
On Mon, Feb 06, 2023 at 01:50:52PM -0700, Dave Jiang wrote:
> Move the logic in current_link_speed_show() to a common function and export
> that functiuon as pcie_get_speed() to allow other drivers to to retrieve
> the current negotiated link speed.
[...]
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -6215,6 +6215,26 @@ enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
>  }
>  EXPORT_SYMBOL(pcie_get_width_cap);
>  
> +/**
> + * pcie_get_speed - query for the PCI device's current link speed
> + * @dev: PCI device to query
> + *
> + * Query the PCI device current link speed.
> + */
> +enum pci_bus_speed pcie_get_speed(struct pci_dev *dev)
> +{
> +	u16 linkstat, cls;
> +	int err;
> +
> +	err = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat);
> +	if (err)
> +		return PCI_SPEED_UNKNOWN;
> +
> +	cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, linkstat);
> +	return pcie_link_speed[cls];
> +}
> +EXPORT_SYMBOL(pcie_get_speed);

It seems we're already caching the current speed in dev->bus->cur_bus_speed.
Is that not sufficient?  If it isn't, that should be explained in the
commit message.

Thanks,

Lukas
Dave Jiang Feb. 7, 2023, 8:29 p.m. UTC | #2
On 2/6/23 3:27 PM, Lukas Wunner wrote:
> On Mon, Feb 06, 2023 at 01:50:52PM -0700, Dave Jiang wrote:
>> Move the logic in current_link_speed_show() to a common function and export
>> that functiuon as pcie_get_speed() to allow other drivers to to retrieve
>> the current negotiated link speed.
> [...]
>> --- a/drivers/pci/pci.c
>> +++ b/drivers/pci/pci.c
>> @@ -6215,6 +6215,26 @@ enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
>>   }
>>   EXPORT_SYMBOL(pcie_get_width_cap);
>>   
>> +/**
>> + * pcie_get_speed - query for the PCI device's current link speed
>> + * @dev: PCI device to query
>> + *
>> + * Query the PCI device current link speed.
>> + */
>> +enum pci_bus_speed pcie_get_speed(struct pci_dev *dev)
>> +{
>> +	u16 linkstat, cls;
>> +	int err;
>> +
>> +	err = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat);
>> +	if (err)
>> +		return PCI_SPEED_UNKNOWN;
>> +
>> +	cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, linkstat);
>> +	return pcie_link_speed[cls];
>> +}
>> +EXPORT_SYMBOL(pcie_get_speed);
> 
> It seems we're already caching the current speed in dev->bus->cur_bus_speed.
> Is that not sufficient?  If it isn't, that should be explained in the
> commit message.

I did not realize. That should work. Thanks. I'll drop patch.

> 
> Thanks,
> 
> Lukas
diff mbox series

Patch

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index dd0d9d9bc509..0217bb5ca8fa 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -205,17 +205,9 @@  static ssize_t current_link_speed_show(struct device *dev,
 				       struct device_attribute *attr, char *buf)
 {
 	struct pci_dev *pci_dev = to_pci_dev(dev);
-	u16 linkstat;
-	int err;
-	enum pci_bus_speed speed;
-
-	err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
-	if (err)
-		return -EINVAL;
 
-	speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
-
-	return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
+	return sysfs_emit(buf, "%s\n",
+			  pci_speed_string(pcie_get_speed(pci_dev)));
 }
 static DEVICE_ATTR_RO(current_link_speed);
 
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index fba95486caaf..d0131b5623b1 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -6215,6 +6215,26 @@  enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
 }
 EXPORT_SYMBOL(pcie_get_width_cap);
 
+/**
+ * pcie_get_speed - query for the PCI device's current link speed
+ * @dev: PCI device to query
+ *
+ * Query the PCI device current link speed.
+ */
+enum pci_bus_speed pcie_get_speed(struct pci_dev *dev)
+{
+	u16 linkstat, cls;
+	int err;
+
+	err = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat);
+	if (err)
+		return PCI_SPEED_UNKNOWN;
+
+	cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, linkstat);
+	return pcie_link_speed[cls];
+}
+EXPORT_SYMBOL(pcie_get_speed);
+
 /**
  * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability
  * @dev: PCI device
diff --git a/include/linux/pci.h b/include/linux/pci.h
index adffd65e84b4..6a065986ff8f 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -303,6 +303,7 @@  enum pci_bus_speed {
 	PCI_SPEED_UNKNOWN		= 0xff,
 };
 
+enum pci_bus_speed pcie_get_speed(struct pci_dev *dev);
 enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev);
 enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev);