Skip to content

Commit cf6ee09

Browse files
thepacketgeekbjorn-helgaas
authored andcommitted
PCI/sysfs: Expose PCI device serial number
Add a single sysfs read-only interface for reading PCI 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 <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Mario Limonciello <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Reviewed-by: Thomas Weißschuh <[email protected]> Reviewed-by: Keith Busch <[email protected]> Reviewed-by: Krzysztof Wilczyński <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent fac679d commit cf6ee09

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Documentation/ABI/testing/sysfs-bus-pci

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,12 @@ Description:
612612

613613
# ls doe_features
614614
0001:01 0001:02 doe_discovery
615+
616+
What: /sys/bus/pci/devices/.../serial_number
617+
Date: December 2025
618+
Contact: Matthew Wood <[email protected]>
619+
Description:
620+
This is visible only for PCI devices that support the serial
621+
number extended capability. The file is read only and due to
622+
the possible sensitivity of accessible serial numbers, admin
623+
only.

drivers/pci/pci-sysfs.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/msi.h>
3131
#include <linux/of.h>
3232
#include <linux/aperture.h>
33+
#include <linux/unaligned.h>
3334
#include "pci.h"
3435

3536
#ifndef ARCH_PCI_DEV_GROUPS
@@ -694,6 +695,22 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
694695
}
695696
static DEVICE_ATTR_RO(boot_vga);
696697

698+
static ssize_t serial_number_show(struct device *dev,
699+
struct device_attribute *attr, char *buf)
700+
{
701+
struct pci_dev *pci_dev = to_pci_dev(dev);
702+
u64 dsn;
703+
u8 bytes[8];
704+
705+
dsn = pci_get_dsn(pci_dev);
706+
if (!dsn)
707+
return -EIO;
708+
709+
put_unaligned_be64(dsn, bytes);
710+
return sysfs_emit(buf, "%8phD\n", bytes);
711+
}
712+
static DEVICE_ATTR_ADMIN_RO(serial_number);
713+
697714
static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
698715
const struct bin_attribute *bin_attr, char *buf,
699716
loff_t off, size_t count)
@@ -1698,6 +1715,7 @@ late_initcall(pci_sysfs_init);
16981715

16991716
static struct attribute *pci_dev_dev_attrs[] = {
17001717
&dev_attr_boot_vga.attr,
1718+
&dev_attr_serial_number.attr,
17011719
NULL,
17021720
};
17031721

@@ -1710,6 +1728,9 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
17101728
if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
17111729
return a->mode;
17121730

1731+
if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
1732+
return a->mode;
1733+
17131734
return 0;
17141735
}
17151736

0 commit comments

Comments
 (0)