* [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number
@ 2025-07-13 1:17 Matthew Wood
2025-07-13 1:17 ` [PATCH pci-next v1 1/1] " Matthew Wood
2025-07-15 11:19 ` [PATCH pci-next v1 0/1] " Jonathan Cameron
0 siblings, 2 replies; 5+ messages in thread
From: Matthew Wood @ 2025-07-13 1:17 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel
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 2-byte dashed formatting as lspci serial number capability
output:
more /sys/devices/pci0000:c0/0000:c0:01.1/0000:c1:00.0/0000:c2:1f.0/0000:cc:00.0/device_serial_number
00-80-ee-00-00-00-41-80
Accompanying lspci output:
sudo lspci -vvv -s cc:00.0
cc:00.0 Serial Attached SCSI controller: Broadcom / LSI PCIe Switch management endpoint (rev b0)
Subsystem: Broadcom / LSI Device 0144
...
Capabilities: [100 v1] Device Serial Number 00-80-ee-00-00-00-41-80
...
If a device doesn't support the serial number capability, userspace will receive
an empty read:
more /sys/devices/pci0000:00/0000:00:07.1/device_serial_number
echo $?
0
Matthew Wood (1):
PCI/sysfs: Expose PCIe device serial number
drivers/pci/pci-sysfs.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
--
2.50.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH pci-next v1 1/1] PCI/sysfs: Expose PCIe device serial number
2025-07-13 1:17 [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number Matthew Wood
@ 2025-07-13 1:17 ` Matthew Wood
2025-07-15 11:19 ` [PATCH pci-next v1 0/1] " Jonathan Cameron
1 sibling, 0 replies; 5+ messages in thread
From: Matthew Wood @ 2025-07-13 1:17 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel
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 2-byte dashed formatting as lspci serial number capability
output.
Signed-off-by: Matthew Wood <thepacketgeek@gmail.com>
---
drivers/pci/pci-sysfs.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 268c69daa4d5..72bc266388d4 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -239,6 +239,22 @@ static ssize_t current_link_width_show(struct device *dev,
}
static DEVICE_ATTR_RO(current_link_width);
+static ssize_t device_serial_number_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct pci_dev *pci_dev = to_pci_dev(dev);
+ u64 dsn;
+
+ dsn = pci_get_dsn(pci_dev);
+ if (!dsn)
+ return -EINVAL;
+
+ return sysfs_emit(buf, "%02llx-%02llx-%02llx-%02llx-%02llx-%02llx-%02llx-%02llx\n",
+ dsn >> 56, (dsn >> 48) & 0xff, (dsn >> 40) & 0xff, (dsn >> 32) & 0xff,
+ (dsn >> 24) & 0xff, (dsn >> 16) & 0xff, (dsn >> 8) & 0xff, dsn & 0xff);
+}
+static DEVICE_ATTR_RO(device_serial_number);
+
static ssize_t secondary_bus_number_show(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -660,6 +676,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_device_serial_number.attr,
NULL,
};
--
2.50.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number
2025-07-13 1:17 [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number Matthew Wood
2025-07-13 1:17 ` [PATCH pci-next v1 1/1] " Matthew Wood
@ 2025-07-15 11:19 ` Jonathan Cameron
2025-07-15 15:59 ` Matthew Wood
1 sibling, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2025-07-15 11:19 UTC (permalink / raw)
To: Matthew Wood; +Cc: Bjorn Helgaas, linux-pci, linux-kernel
On Sat, 12 Jul 2025 18:17:12 -0700
Matthew Wood <thepacketgeek@gmail.com> 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 2-byte dashed formatting as lspci serial number capability
> output:
>
> more /sys/devices/pci0000:c0/0000:c0:01.1/0000:c1:00.0/0000:c2:1f.0/0000:cc:00.0/device_serial_number
> 00-80-ee-00-00-00-41-80
>
What is the use case for this? I can think of some possibilities but good to
see why you care here.
> Accompanying lspci output:
>
> sudo lspci -vvv -s cc:00.0
> cc:00.0 Serial Attached SCSI controller: Broadcom / LSI PCIe Switch management endpoint (rev b0)
> Subsystem: Broadcom / LSI Device 0144
> ...
> Capabilities: [100 v1] Device Serial Number 00-80-ee-00-00-00-41-80
> ...
>
> If a device doesn't support the serial number capability, userspace will receive
> an empty read:
Better if possible to not expose the sysfs attribute if no such capability.
We already have pcie_dev_attrs_are_visible() so easy to extend that.
>
> more /sys/devices/pci0000:00/0000:00:07.1/device_serial_number
> echo $?
> 0
>
>
> Matthew Wood (1):
> PCI/sysfs: Expose PCIe device serial number
>
> drivers/pci/pci-sysfs.c | 17 +++++++++++++++++
> 1 file changed, 17 insertions(+)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number
2025-07-15 11:19 ` [PATCH pci-next v1 0/1] " Jonathan Cameron
@ 2025-07-15 15:59 ` Matthew Wood
2025-07-16 9:23 ` Jonathan Cameron
0 siblings, 1 reply; 5+ messages in thread
From: Matthew Wood @ 2025-07-15 15:59 UTC (permalink / raw)
To: Jonathan Cameron; +Cc: Bjorn Helgaas, linux-pci, linux-kernel
On Tue, Jul 15, 2025 at 4:19 AM Jonathan Cameron
<Jonathan.Cameron@huawei.com> wrote:
>
> On Sat, 12 Jul 2025 18:17:12 -0700
> Matthew Wood <thepacketgeek@gmail.com> 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 2-byte dashed formatting as lspci serial number capability
> > output:
> >
> > more /sys/devices/pci0000:c0/0000:c0:01.1/0000:c1:00.0/0000:c2:1f.0/0000:cc:00.0/device_serial_number
> > 00-80-ee-00-00-00-41-80
> >
>
> What is the use case for this? I can think of some possibilities but good to
> see why you care here.
Two primary use cases we have are for inventory tooling and health
check tooling; being able to
reliably collect device serial numbers for tracking unique devices
whose BDFs could change is
critical. Sometimes in the process of hardware troubleshooting, cards
are swapped and BDF idents
change but we want to track devices by serial number without possibly
fragile regexps.
>
>
> > Accompanying lspci output:
> >
> > sudo lspci -vvv -s cc:00.0
> > cc:00.0 Serial Attached SCSI controller: Broadcom / LSI PCIe Switch management endpoint (rev b0)
> > Subsystem: Broadcom / LSI Device 0144
> > ...
> > Capabilities: [100 v1] Device Serial Number 00-80-ee-00-00-00-41-80
> > ...
> >
> > If a device doesn't support the serial number capability, userspace will receive
> > an empty read:
>
> Better if possible to not expose the sysfs attribute if no such capability.
> We already have pcie_dev_attrs_are_visible() so easy to extend that.
That's a great point, it looks like I could match on the attribute
name to specifically hide device_serial_number
if the device does not support the cap, but I can't find any precedent
for matching on a->name in pci-sysfs.c.
Would something like this be alright after the check for pci_is_pcie(dev):
if (a->name == "device_serial_number") {
// check if device has serial, if not return 0
}
>
>
> >
> > more /sys/devices/pci0000:00/0000:00:07.1/device_serial_number
> > echo $?
> > 0
> >
> >
> > Matthew Wood (1):
> > PCI/sysfs: Expose PCIe device serial number
> >
> > drivers/pci/pci-sysfs.c | 17 +++++++++++++++++
> > 1 file changed, 17 insertions(+)
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number
2025-07-15 15:59 ` Matthew Wood
@ 2025-07-16 9:23 ` Jonathan Cameron
0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2025-07-16 9:23 UTC (permalink / raw)
To: Matthew Wood; +Cc: Bjorn Helgaas, linux-pci, linux-kernel
On Tue, 15 Jul 2025 08:59:42 -0700
Matthew Wood <thepacketgeek@gmail.com> wrote:
> On Tue, Jul 15, 2025 at 4:19 AM Jonathan Cameron
> <Jonathan.Cameron@huawei.com> wrote:
> >
> > On Sat, 12 Jul 2025 18:17:12 -0700
> > Matthew Wood <thepacketgeek@gmail.com> 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 2-byte dashed formatting as lspci serial number capability
> > > output:
> > >
> > > more /sys/devices/pci0000:c0/0000:c0:01.1/0000:c1:00.0/0000:c2:1f.0/0000:cc:00.0/device_serial_number
> > > 00-80-ee-00-00-00-41-80
> > >
> >
> > What is the use case for this? I can think of some possibilities but good to
> > see why you care here.
>
> Two primary use cases we have are for inventory tooling and health
> check tooling; being able to
> reliably collect device serial numbers for tracking unique devices
> whose BDFs could change is
> critical. Sometimes in the process of hardware troubleshooting, cards
> are swapped and BDF idents
> change but we want to track devices by serial number without possibly
> fragile regexps.
Ok. So you want to avoid having pull this from lspci output which
makes sense to me. Not sure what Bjorn and others think about this
though.
>
> >
> >
> > > Accompanying lspci output:
> > >
> > > sudo lspci -vvv -s cc:00.0
> > > cc:00.0 Serial Attached SCSI controller: Broadcom / LSI PCIe Switch management endpoint (rev b0)
> > > Subsystem: Broadcom / LSI Device 0144
> > > ...
> > > Capabilities: [100 v1] Device Serial Number 00-80-ee-00-00-00-41-80
> > > ...
> > >
> > > If a device doesn't support the serial number capability, userspace will receive
> > > an empty read:
> >
> > Better if possible to not expose the sysfs attribute if no such capability.
> > We already have pcie_dev_attrs_are_visible() so easy to extend that.
>
> That's a great point, it looks like I could match on the attribute
> name to specifically hide device_serial_number
> if the device does not support the cap, but I can't find any precedent
> for matching on a->name in pci-sysfs.c.
> Would something like this be alright after the check for pci_is_pcie(dev):
>
> if (a->name == "device_serial_number") {
> // check if device has serial, if not return 0
> }
if (a == &dev_attr_device_serial_number.attr)
or something like that.
No need for string matching.
Jonathan
>
> >
> >
> > >
> > > more /sys/devices/pci0000:00/0000:00:07.1/device_serial_number
> > > echo $?
> > > 0
> > >
> > >
> > > Matthew Wood (1):
> > > PCI/sysfs: Expose PCIe device serial number
> > >
> > > drivers/pci/pci-sysfs.c | 17 +++++++++++++++++
> > > 1 file changed, 17 insertions(+)
> > >
> >
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-07-16 9:23 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-13 1:17 [PATCH pci-next v1 0/1] PCI/sysfs: Expose PCIe device serial number Matthew Wood
2025-07-13 1:17 ` [PATCH pci-next v1 1/1] " Matthew Wood
2025-07-15 11:19 ` [PATCH pci-next v1 0/1] " Jonathan Cameron
2025-07-15 15:59 ` Matthew Wood
2025-07-16 9:23 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).