From: Alastair D'Silva <alast...@d-silva.org>

This patch exposes the OpenCAPI device serial number to
userspace.

It also includes placeholders for the LPC & special purpose
memory information (which will be populated in a subsequent patch)
to avoid creating excessive versions of the IOCTL.

Signed-off-by: Alastair D'Silva <alast...@d-silva.org>
---
 drivers/misc/ocxl/config.c | 46 ++++++++++++++++++++++++++++++++++++++
 drivers/misc/ocxl/file.c   |  3 ++-
 include/misc/ocxl.h        |  1 +
 include/uapi/misc/ocxl.h   |  9 +++++++-
 4 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/ocxl/config.c b/drivers/misc/ocxl/config.c
index fb0c3b6f8312..a9203c309365 100644
--- a/drivers/misc/ocxl/config.c
+++ b/drivers/misc/ocxl/config.c
@@ -71,6 +71,51 @@ static int find_dvsec_afu_ctrl(struct pci_dev *dev, u8 
afu_idx)
        return 0;
 }
 
+/**
+ * Find a related PCI device (function 0)
+ * @device: PCI device to match
+ *
+ * Returns a pointer to the related device, or null if not found
+ */
+static struct pci_dev *get_function_0(struct pci_dev *dev)
+{
+       unsigned int devfn = PCI_DEVFN(PCI_SLOT(dev->devfn), 0); // Look for 
function 0
+
+       return pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
+                                       dev->bus->number, devfn);
+}
+
+static void read_serial(struct pci_dev *dev, struct ocxl_fn_config *fn)
+{
+       u32 low, high;
+       int pos;
+
+       pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
+       if (pos) {
+               pci_read_config_dword(dev, pos + 0x04, &low);
+               pci_read_config_dword(dev, pos + 0x08, &high);
+
+               fn->serial = low | ((u64)high) << 32;
+
+               return;
+       }
+
+       if (PCI_FUNC(dev->devfn) != 0) {
+               struct pci_dev *related = get_function_0(dev);
+
+               if (!related) {
+                       fn->serial = 0;
+                       return;
+               }
+
+               read_serial(related, fn);
+               pci_dev_put(related);
+               return;
+       }
+
+       fn->serial = 0;
+}
+
 static void read_pasid(struct pci_dev *dev, struct ocxl_fn_config *fn)
 {
        u16 val;
@@ -208,6 +253,7 @@ int ocxl_config_read_function(struct pci_dev *dev, struct 
ocxl_fn_config *fn)
        int rc;
 
        read_pasid(dev, fn);
+       read_serial(dev, fn);
 
        rc = read_dvsec_tl(dev, fn);
        if (rc) {
diff --git a/drivers/misc/ocxl/file.c b/drivers/misc/ocxl/file.c
index 2870c25da166..08f6f594a11d 100644
--- a/drivers/misc/ocxl/file.c
+++ b/drivers/misc/ocxl/file.c
@@ -98,13 +98,14 @@ static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
 
        memset(&arg, 0, sizeof(arg));
 
-       arg.version = 0;
+       arg.version = 1;
 
        arg.afu_version_major = ctx->afu->config.version_major;
        arg.afu_version_minor = ctx->afu->config.version_minor;
        arg.pasid = ctx->pasid;
        arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
        arg.global_mmio_size = ctx->afu->config.global_mmio_size;
+       arg.serial = ctx->afu->fn->config.serial;
 
        if (copy_to_user(uarg, &arg, sizeof(arg)))
                return -EFAULT;
diff --git a/include/misc/ocxl.h b/include/misc/ocxl.h
index a1897737908d..da75db149e6c 100644
--- a/include/misc/ocxl.h
+++ b/include/misc/ocxl.h
@@ -46,6 +46,7 @@ struct ocxl_fn_config {
        int dvsec_afu_info_pos; /* offset of the AFU information DVSEC */
        s8 max_pasid_log;
        s8 max_afu_index;
+       u64 serial;
 };
 
 enum ocxl_endian {
diff --git a/include/uapi/misc/ocxl.h b/include/uapi/misc/ocxl.h
index 6d29a60a896a..d4c6bf10580c 100644
--- a/include/uapi/misc/ocxl.h
+++ b/include/uapi/misc/ocxl.h
@@ -45,7 +45,14 @@ struct ocxl_ioctl_metadata {
 
        /* End version 0 fields */
 
-       __u64 reserved[13]; /* Total of 16*u64 */
+       // Version 1 fields
+       __u64 lpc_mem_size;
+       __u64 special_purpose_mem_size;
+       __u64 serial;           // Device serial number
+
+       // End version 1 fields
+
+       __u64 reserved[10]; // Total of 16*u64
 };
 
 struct ocxl_ioctl_p9_wait {
-- 
2.21.0

Reply via email to