On Fri, 18 Jul 2025 12:32:29 -0700 Matthew Wood <thepacketgeek@xxxxxxxxx> wrote: > Add a single sysfs read-only interface for reading PCIe device serial > numbers from userspace in a programmatic way. This device attribute > uses the same hexadecimal 1-byte dashed formatting as lspci serial number > capability output. If a device doesn't support the serial number > capability, the serial_number sysfs attribute will not be visible. > > Signed-off-by: Matthew Wood <thepacketgeek@xxxxxxxxx> > Reviewed-by: Mario Limonciello <superm1@xxxxxxxxxx> Nice. The %8phD that Thomas pointed out was new to me. Very useful! Reviewed-by: Jonathan Cameron <jonathan.cameron@xxxxxxxxxx> > --- > Documentation/ABI/testing/sysfs-bus-pci | 9 +++++++++ > drivers/pci/pci-sysfs.c | 27 ++++++++++++++++++++++--- > 2 files changed, 33 insertions(+), 3 deletions(-) > > diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci > index 69f952fffec7..0a2580cdd58c 100644 > --- a/Documentation/ABI/testing/sysfs-bus-pci > +++ b/Documentation/ABI/testing/sysfs-bus-pci > @@ -612,3 +612,12 @@ Description: > > # ls doe_features > 0001:01 0001:02 doe_discovery > + > +What: /sys/bus/pci/devices/.../serial_number > +Date: October 2025 > +Contact: Matthew Wood <thepacketgeek@xxxxxxxxx> > +Description: > + This is visible only for PCIe devices that support the serial > + number extended capability. The file is read only and due to > + the possible sensitivity of accessible serial numbers, admin > + only. > diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c > index 268c69daa4d5..1d26e4336f1b 100644 > --- a/drivers/pci/pci-sysfs.c > +++ b/drivers/pci/pci-sysfs.c > @@ -30,6 +30,7 @@ > #include <linux/msi.h> > #include <linux/of.h> > #include <linux/aperture.h> > +#include <linux/unaligned.h> > #include "pci.h" > > #ifndef ARCH_PCI_DEV_GROUPS > @@ -239,6 +240,22 @@ static ssize_t current_link_width_show(struct device *dev, > } > static DEVICE_ATTR_RO(current_link_width); > > +static ssize_t serial_number_show(struct device *dev, > + struct device_attribute *attr, char *buf) > +{ > + struct pci_dev *pci_dev = to_pci_dev(dev); > + u64 dsn; > + u8 bytes[8]; > + > + dsn = pci_get_dsn(pci_dev); > + if (!dsn) > + return -EIO; > + put_unaligned_be64(dsn, bytes); > + > + return sysfs_emit(buf, "%8phD\n", bytes); > +} > +static DEVICE_ATTR_ADMIN_RO(serial_number); > + > static ssize_t secondary_bus_number_show(struct device *dev, > struct device_attribute *attr, > char *buf) > @@ -660,6 +677,7 @@ static struct attribute *pcie_dev_attrs[] = { > &dev_attr_current_link_width.attr, > &dev_attr_max_link_width.attr, > &dev_attr_max_link_speed.attr, > + &dev_attr_serial_number.attr, > NULL, > }; > > @@ -1749,10 +1767,13 @@ static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, > struct device *dev = kobj_to_dev(kobj); > struct pci_dev *pdev = to_pci_dev(dev); > > - if (pci_is_pcie(pdev)) > - return a->mode; > + if (!pci_is_pcie(pdev)) > + return 0; > > - return 0; > + if (a == &dev_attr_serial_number.attr && !pci_get_dsn(pdev)) > + return 0; > + > + return a->mode; > } > > static const struct attribute_group pci_dev_group = {