Skip to contentSkip to conversation

PCI: introduce PCIe Device Serial NUmber Capability support

PATCH In review Yijing Wang 3 participants 5 messages Jul 9, 2013 – Jul 11, 2013 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 PCI: introduce PCIe Device Serial NUmber Capability support

Introduce PCIe Ext Capability Device Serial Number support, so we can use the unique device serial number to identify the physical device. During system suspend, if the PCIe device was removed and inserted a new same device, after system resume there is no good way to identify it, maybe Device Serial Number is a good choice if device support.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Paul Bolle <[email protected]>
Cc Gu Zheng <[email protected]>
---
 drivers/pci/pci.c   |   33 +++++++++++++++++++++++++++++++++
 drivers/pci/pci.h   |    1 +
 drivers/pci/probe.c |    3 +++
 include/linux/pci.h |    4 ++++
 4 files changed, 41 insertions(+), 0 deletions(-)
drivers/pci/pci.c +33 -0
index e37fea6..d08df2b 100644
@@ -2048,6 +2048,39 @@ void pci_free_cap_save_buffers(struct pci_dev *dev)
 }
 
 /**
+ * pci_get_dsn - get device serial number
+ * @dev: the PCI device
+ * @sn: saved device serial number
+ */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn)
+{
+	int pos;
+	u32 lo, hi;
+
+	if (!pci_is_pcie(dev))
+		goto out;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
+	if (!pos)
+		goto out;
+
+	pci_read_config_dword(dev, pos + 4, &lo);
+	pci_read_config_dword(dev, pos + 8, &hi);
+	*sn = ((u64)hi << 32) | lo;
+	return;
+
+out:
+	*sn = 0;
+	return;
+}
+EXPORT_SYMBOL(pci_get_dsn);
+
+void pci_dsn_init(struct pci_dev *dev)
+{
+	return pci_get_dsn(dev, &dev->sn);
+}
+
+/**
  * pci_configure_ari - enable or disable ARI forwarding
  * @dev: the PCI device
  *
drivers/pci/pci.h +1 -0
index 68678ed..f626006 100644
@@ -202,6 +202,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 		    struct resource *res, unsigned int reg);
 int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type);
 void pci_configure_ari(struct pci_dev *dev);
+void pci_dsn_init(struct pci_dev *dev);
 
 /**
  * pci_ari_enabled - query ARI forwarding status
drivers/pci/probe.c +3 -0
index 46ada5c..d4c6e7e 100644
@@ -1322,6 +1322,9 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	/* Power Management */
 	pci_pm_init(dev);
 
+	/* Device Serial Number */
+	pci_dsn_init(dev);
+
 	/* Vital Product Data */
 	pci_vpd_pci22_init(dev);
 
include/linux/pci.h +4 -0
index 0fd1f15..59cd205 100644
@@ -342,6 +342,7 @@ struct pci_dev {
 	struct list_head msi_list;
 	struct kset *msi_kset;
 #endif
+	u64 sn;		/* device serieal number, 0 if not support */
 	struct pci_vpd *vpd;
 #ifdef CONFIG_PCI_ATS
 	union {
@@ -995,6 +996,9 @@ ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
 int pci_vpd_truncate(struct pci_dev *dev, size_t size);
 
+/* Device Serial Number */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn);
+
 /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
 resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);
Signature · 1 line
1.7.1
Reviewed-by on v1 1/2
#1 lore raw
Quoted 6 lines · Yijing Wang

Introduce PCIe Ext Capability Device Serial Number support, so we can use the unique device serial number to identify the physical device. During system suspend, if the PCIe device was removed and inserted a new same device, after system resume there is no good way to identify it, maybe Device Serial Number is a good choice if device support.

Nice idea!

Regards,
Gu

Quoted 2 lines
Quoted 104 lines
Cc "Rafael J. Wysocki" <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Paul Bolle <[email protected]>
Cc Gu Zheng <[email protected]>
---
 drivers/pci/pci.c   |   33 +++++++++++++++++++++++++++++++++
 drivers/pci/pci.h   |    1 +
 drivers/pci/probe.c |    3 +++
 include/linux/pci.h |    4 ++++
 4 files changed, 41 insertions(+), 0 deletions(-)
drivers/pci/pci.c +33 -0
index e37fea6..d08df2b 100644
@@ -2048,6 +2048,39 @@ void pci_free_cap_save_buffers(struct pci_dev *dev)
 }
 
 /**
+ * pci_get_dsn - get device serial number
+ * @dev: the PCI device
+ * @sn: saved device serial number
+ */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn)
+{
+	int pos;
+	u32 lo, hi;
+
+	if (!pci_is_pcie(dev))
+		goto out;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
+	if (!pos)
+		goto out;
+
+	pci_read_config_dword(dev, pos + 4, &lo);
+	pci_read_config_dword(dev, pos + 8, &hi);
+	*sn = ((u64)hi << 32) | lo;
+	return;
+
+out:
+	*sn = 0;
+	return;
+}
+EXPORT_SYMBOL(pci_get_dsn);
+
+void pci_dsn_init(struct pci_dev *dev)
+{
+	return pci_get_dsn(dev, &dev->sn);
+}
+
+/**
  * pci_configure_ari - enable or disable ARI forwarding
  * @dev: the PCI device
  *
drivers/pci/pci.h +1 -0
index 68678ed..f626006 100644
@@ -202,6 +202,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 		    struct resource *res, unsigned int reg);
 int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type);
 void pci_configure_ari(struct pci_dev *dev);
+void pci_dsn_init(struct pci_dev *dev);
 
 /**
  * pci_ari_enabled - query ARI forwarding status
drivers/pci/probe.c +3 -0
index 46ada5c..d4c6e7e 100644
@@ -1322,6 +1322,9 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	/* Power Management */
 	pci_pm_init(dev);
 
+	/* Device Serial Number */
+	pci_dsn_init(dev);
+
 	/* Vital Product Data */
 	pci_vpd_pci22_init(dev);
 
include/linux/pci.h +4 -0
index 0fd1f15..59cd205 100644
@@ -342,6 +342,7 @@ struct pci_dev {
 	struct list_head msi_list;
 	struct kset *msi_kset;
 #endif
+	u64 sn;		/* device serieal number, 0 if not support */
 	struct pci_vpd *vpd;
 #ifdef CONFIG_PCI_ATS
 	union {
@@ -995,6 +996,9 @@ ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
 int pci_vpd_truncate(struct pci_dev *dev, size_t size);
 
+/* Device Serial Number */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn);
+
 /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
 resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);
#2 lore raw
Quoted 11 lines · Gu Zheng
Quoted 6 lines · Yijing Wang

Introduce PCIe Ext Capability Device Serial Number support, so we can use the unique device serial number to identify the physical device. During system suspend, if the PCIe device was removed and inserted a new same device, after system resume there is no good way to identify it, maybe Device Serial Number is a good choice if device support.

Nice idea!

Quoted 8 lines

Regards,
Gu

Quoted 2 lines

Thanks very much!

Yijing.

Quoted 110 lines
Quoted 96 lines
Cc "Rafael J. Wysocki" <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Paul Bolle <[email protected]>
Cc Gu Zheng <[email protected]>
---
 drivers/pci/pci.c   |   33 +++++++++++++++++++++++++++++++++
 drivers/pci/pci.h   |    1 +
 drivers/pci/probe.c |    3 +++
 include/linux/pci.h |    4 ++++
 4 files changed, 41 insertions(+), 0 deletions(-)
drivers/pci/pci.c +33 -0
index e37fea6..d08df2b 100644
@@ -2048,6 +2048,39 @@ void pci_free_cap_save_buffers(struct pci_dev *dev)
 }
 
 /**
+ * pci_get_dsn - get device serial number
+ * @dev: the PCI device
+ * @sn: saved device serial number
+ */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn)
+{
+	int pos;
+	u32 lo, hi;
+
+	if (!pci_is_pcie(dev))
+		goto out;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
+	if (!pos)
+		goto out;
+
+	pci_read_config_dword(dev, pos + 4, &lo);
+	pci_read_config_dword(dev, pos + 8, &hi);
+	*sn = ((u64)hi << 32) | lo;
+	return;
+
+out:
+	*sn = 0;
+	return;
+}
+EXPORT_SYMBOL(pci_get_dsn);
+
+void pci_dsn_init(struct pci_dev *dev)
+{
+	return pci_get_dsn(dev, &dev->sn);
+}
+
+/**
  * pci_configure_ari - enable or disable ARI forwarding
  * @dev: the PCI device
  *
drivers/pci/pci.h +1 -0
index 68678ed..f626006 100644
@@ -202,6 +202,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 		    struct resource *res, unsigned int reg);
 int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type);
 void pci_configure_ari(struct pci_dev *dev);
+void pci_dsn_init(struct pci_dev *dev);
 
 /**
  * pci_ari_enabled - query ARI forwarding status
drivers/pci/probe.c +3 -0
index 46ada5c..d4c6e7e 100644
@@ -1322,6 +1322,9 @@ static void pci_init_capabilities(struct pci_dev *dev)
 	/* Power Management */
 	pci_pm_init(dev);
 
+	/* Device Serial Number */
+	pci_dsn_init(dev);
+
 	/* Vital Product Data */
 	pci_vpd_pci22_init(dev);
 
include/linux/pci.h +1 -0
index 0fd1f15..59cd205 100644
@@ -342,6 +342,7 @@ struct pci_dev {
 	struct list_head msi_list;
 	struct kset *msi_kset;
 #endif
+	u64 sn;		/* device serieal number, 0 if not support */
 	struct pci_vpd *vpd;
 #ifdef CONFIG_PCI_ATS
 	union {

include/linux/pci.h ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf); · line 1004

Quoted 9 lines
include/linux/pci.h +3 -0
@@ -995,6 +996,9 @@ ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
 int pci_vpd_truncate(struct pci_dev *dev, size_t size);
 
+/* Device Serial Number */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn);
+
 /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
 resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);

.

Signature · 2 lines
Thanks!
Yijing
maintainer
#3 lore raw
In reply to Yijing Wang
Quoted 25 lines · Yijing Wang

Introduce PCIe Ext Capability Device Serial Number support, so we can use the unique device serial number to identify the physical device. During system suspend, if the PCIe device was removed and inserted a new same device, after system resume there is no good way to identify it, maybe Device Serial Number is a good choice if device support.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Paul Bolle <[email protected]>
Cc Gu Zheng <[email protected]>
---
 drivers/pci/pci.c   |   33 +++++++++++++++++++++++++++++++++
 drivers/pci/pci.h   |    1 +
 drivers/pci/probe.c |    3 +++
 include/linux/pci.h |    4 ++++
 4 files changed, 41 insertions(+), 0 deletions(-)
drivers/pci/pci.c +0 -0
index e37fea6..d08df2b 100644

drivers/pci/pci.c void pci_free_cap_save_buffers(struct pci_dev *dev) · line 2055

Quoted 8 lines
drivers/pci/pci.c +5 -0
@@ -2048,6 +2048,39 @@ void pci_free_cap_save_buffers(struct pci_dev *dev)
 }
 
 /**
+ * pci_get_dsn - get device serial number
+ * @dev: the PCI device
+ * @sn: saved device serial number
+ */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn)

How about:

u64 pci_device_serial_number(struct pci_dev *dev)

"get" suggests reference counting, which isn't happening here. And why pass a pointer to a return value when we can just as easily return it directly?

Quoted 48 lines
drivers/pci/pci.c +28 -0
+{
+       int pos;
+       u32 lo, hi;
+
+       if (!pci_is_pcie(dev))
+               goto out;
+
+       pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
+       if (!pos)
+               goto out;
+
+       pci_read_config_dword(dev, pos + 4, &lo);
+       pci_read_config_dword(dev, pos + 8, &hi);
+       *sn = ((u64)hi << 32) | lo;
+       return;
+
+out:
+       *sn = 0;
+       return;
+}
+EXPORT_SYMBOL(pci_get_dsn);
+
+void pci_dsn_init(struct pci_dev *dev)
+{
+       return pci_get_dsn(dev, &dev->sn);
+}
+
+/**
  * pci_configure_ari - enable or disable ARI forwarding
  * @dev: the PCI device
  *
drivers/pci/pci.h +1 -0
index 68678ed..f626006 100644
@@ -202,6 +202,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
                    struct resource *res, unsigned int reg);
 int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type);
 void pci_configure_ari(struct pci_dev *dev);
+void pci_dsn_init(struct pci_dev *dev);
 
 /**
  * pci_ari_enabled - query ARI forwarding status
drivers/pci/probe.c +0 -0
index 46ada5c..d4c6e7e 100644

drivers/pci/probe.c static void pci_init_capabilities(struct pci_dev *dev) · line 1326

Quoted 5 lines
drivers/pci/probe.c +2 -0
@@ -1322,6 +1322,9 @@ static void pci_init_capabilities(struct pci_dev *dev)
        /* Power Management */
        pci_pm_init(dev);
 
+       /* Device Serial Number */
+       pci_dsn_init(dev);
    dev->sn = pci_serial_number(dev);

Obvious, no comment or wrapper function needed.

Quoted 30 lines
drivers/pci/probe.c +1 -0
+
        /* Vital Product Data */
        pci_vpd_pci22_init(dev);
 
include/linux/pci.h +4 -1
index 0fd1f15..59cd205 100644
@@ -342,6 +342,7 @@ struct pci_dev {
        struct list_head msi_list;
        struct kset *msi_kset;
 #endif
+       u64 sn;         /* device serieal number, 0 if not support */
        struct pci_vpd *vpd;
 #ifdef CONFIG_PCI_ATS
        union {
@@ -995,6 +996,9 @@ ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
 int pci_vpd_truncate(struct pci_dev *dev, size_t size);
 
+/* Device Serial Number */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn);
+
 /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
 resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);
--

1.7.1

#4 lore raw
Quoted 10 lines
Quoted 2 lines

+ */
+void pci_get_dsn(struct pci_dev *dev, u64 *sn)

How about:

u64 pci_device_serial_number(struct pci_dev *dev)

"get" suggests reference counting, which isn't happening here. And why pass a pointer to a return value when we can just as easily return it directly?

Yes, u64 pci_device_serial_number(struct pci_dev *dev) is better, thanks!

Quoted 1 line

id pci_init_capabilities(struct pci_dev *dev)

Quoted 9 lines

drivers/pci/probe.c static void pci_init_capabilities(struct pci_dev *dev) · line 1326

Quoted 5 lines
        /* Power Management */
        pci_pm_init(dev);
drivers/pci/probe.c +2 -0
+       /* Device Serial Number */
+       pci_dsn_init(dev);
    dev->sn = pci_serial_number(dev);

Obvious, no comment or wrapper function needed.

OK, will remove the unnecessary comment.

Quoted 4 lines
Quoted 4 lines

+

        /* Vital Product Data */
        pci_vpd_pci22_init(dev);