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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx>
---
 ls-caps.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

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;
 }
 
-- 
2.43.0





[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux