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@xxxxxxxxx> --- drivers/pci/pci-sysfs.c | 12 ++---------- drivers/pci/pci.c | 20 ++++++++++++++++++++ include/linux/pci.h | 1 + 3 files changed, 23 insertions(+), 10 deletions(-) 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);