diff mbox series

[RFC,2/3] Implement a function to get cxl1.1 device uid

Message ID 20231220050738.178481-3-kobayashi.da-06@fujitsu.com (mailing list archive)
State Handled Elsewhere
Headers show
Series lspci: Display cxl1.1 device link status | expand

Commit Message

Daisuke Kobayashi (Fujitsu) Dec. 20, 2023, 5:07 a.m. UTC
This patch adds a function to obtain the uid of the host bridge containing 
the device.

In this function, the host bridge is found by exploring the tree structure
of pci, and the uid is obtained. The uid obtained here is used to identify
the CHBS structure of the device.

Signed-off-by: KobayashiDaisuke <kobayashi.da-06@fujitsu.com>
---
 ls-caps.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
diff mbox series

Patch

diff --git a/ls-caps.c b/ls-caps.c
index be81bb9..077e8ea 100644
--- a/ls-caps.c
+++ b/ls-caps.c
@@ -1386,6 +1386,54 @@  static void cap_express_slot2(struct device *d UNUSED, int where UNUSED)
 
 #define OBJNAMELEN 1024
 static int get_device_uid(struct device *d){
+  char link_path[OBJNAMELEN];
+  char path[OBJNAMELEN];
+  ssize_t bytes_read;
+  int n = snprintf(path, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d",
+      pci_get_param(d->dev->access, "sysfs.path"), d->dev->domain, d->dev->bus, d->dev->dev, d->dev->func);
+  if (n < 0 || n >= OBJNAMELEN){
+    d->dev->access->error("sysfs file name error");
+    return -1;
+  }
+
+  /* get absolute path pointed by the sym link */
+  bytes_read = readlink(path, link_path, sizeof(link_path));
+  if (bytes_read == -1)
+    return -1;
+  
+  link_path[bytes_read] = '\0'; 
+  
+  char *path_copy = strdup(link_path);
+  char *token = strtok(path_copy, "/");
+  int device_uid;
+  while (token)
+  {
+    if (strncmp(token, "pci", 3) == 0)
+    {
+      char buffer[OBJNAMELEN];
+      sprintf(buffer, "/sys/devices/%s/firmware_node/uid", token);
+      FILE *file = fopen(buffer, "r");
+      if (file == NULL)
+      {
+        free(path_copy);
+        return -1;
+      }
+
+      char line[OBJNAMELEN];
+      while (fgets(line, sizeof(line), file) != NULL)
+      {
+        if (sscanf(line, "%d", &device_uid) == 1)
+        {
+          fclose(file);
+          free(path_copy);
+          return device_uid;
+        }
+      }
+      fclose(file);
+    }
+    token = strtok(NULL, "/");
+  }
+  free(path_copy);
   return -1;
 }