bf143f
From 8020177f1c40da2a9ca09fa20dc90eda65739671 Mon Sep 17 00:00:00 2001
bf143f
From: Matthew Rosato <mjrosato@linux.ibm.com>
bf143f
Date: Fri, 2 Sep 2022 13:27:31 -0400
bf143f
Subject: [PATCH 06/42] s390x/pci: add routine to get host function handle from
bf143f
 CLP info
bf143f
MIME-Version: 1.0
bf143f
Content-Type: text/plain; charset=UTF-8
bf143f
Content-Transfer-Encoding: 8bit
bf143f
bf143f
RH-Author: Cédric Le Goater <clg@redhat.com>
bf143f
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
bf143f
RH-Bugzilla: 1664378 2043909
bf143f
RH-Acked-by: Thomas Huth <thuth@redhat.com>
bf143f
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
bf143f
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
bf143f
RH-Commit: [6/41] 8ab652cf4095e61f5f55726d41111de227d452e7
bf143f
bf143f
In order to interface with the underlying host zPCI device, we need
bf143f
to know its function handle. Add a routine to grab this from the
bf143f
vfio CLP capabilities chain.
bf143f
bf143f
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
bf143f
Reviewed-by: Pierre Morel <pmorel@linux.ibm.com>
bf143f
Message-Id: <20220902172737.170349-3-mjrosato@linux.ibm.com>
bf143f
[thuth: Replace free(info) with g_free(info)]
bf143f
Signed-off-by: Thomas Huth <thuth@redhat.com>
bf143f
(cherry picked from commit 21fa15298d88db2050a713cdf79c10cb0e09146f)
bf143f
Signed-off-by: Cédric Le Goater <clg@redhat.com>
bf143f
---
bf143f
 hw/s390x/s390-pci-vfio.c         | 83 ++++++++++++++++++++++++++------
bf143f
 include/hw/s390x/s390-pci-vfio.h |  5 ++
bf143f
 2 files changed, 72 insertions(+), 16 deletions(-)
bf143f
bf143f
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
bf143f
index 6f80a47e29..08bcc55e85 100644
bf143f
--- a/hw/s390x/s390-pci-vfio.c
bf143f
+++ b/hw/s390x/s390-pci-vfio.c
bf143f
@@ -124,6 +124,27 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
bf143f
     pbdev->zpci_fn.pft = 0;
bf143f
 }
bf143f
 
bf143f
+static bool get_host_fh(S390PCIBusDevice *pbdev, struct vfio_device_info *info,
bf143f
+                        uint32_t *fh)
bf143f
+{
bf143f
+    struct vfio_info_cap_header *hdr;
bf143f
+    struct vfio_device_info_cap_zpci_base *cap;
bf143f
+    VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
bf143f
+
bf143f
+    hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
bf143f
+
bf143f
+    /* Can only get the host fh with version 2 or greater */
bf143f
+    if (hdr == NULL || hdr->version < 2) {
bf143f
+        trace_s390_pci_clp_cap(vpci->vbasedev.name,
bf143f
+                               VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
bf143f
+        return false;
bf143f
+    }
bf143f
+    cap = (void *) hdr;
bf143f
+
bf143f
+    *fh = cap->fh;
bf143f
+    return true;
bf143f
+}
bf143f
+
bf143f
 static void s390_pci_read_group(S390PCIBusDevice *pbdev,
bf143f
                                 struct vfio_device_info *info)
bf143f
 {
bf143f
@@ -217,25 +238,13 @@ static void s390_pci_read_pfip(S390PCIBusDevice *pbdev,
bf143f
     memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS);
bf143f
 }
bf143f
 
bf143f
-/*
bf143f
- * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for
bf143f
- * capabilities that contain information about CLP features provided by the
bf143f
- * underlying host.
bf143f
- * On entry, defaults have already been placed into the guest CLP response
bf143f
- * buffers.  On exit, defaults will have been overwritten for any CLP features
bf143f
- * found in the capability chain; defaults will remain for any CLP features not
bf143f
- * found in the chain.
bf143f
- */
bf143f
-void s390_pci_get_clp_info(S390PCIBusDevice *pbdev)
bf143f
+static struct vfio_device_info *get_device_info(S390PCIBusDevice *pbdev,
bf143f
+                                                uint32_t argsz)
bf143f
 {
bf143f
-    g_autofree struct vfio_device_info *info = NULL;
bf143f
+    struct vfio_device_info *info = g_malloc0(argsz);
bf143f
     VFIOPCIDevice *vfio_pci;
bf143f
-    uint32_t argsz;
bf143f
     int fd;
bf143f
 
bf143f
-    argsz = sizeof(*info);
bf143f
-    info = g_malloc0(argsz);
bf143f
-
bf143f
     vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
bf143f
     fd = vfio_pci->vbasedev.fd;
bf143f
 
bf143f
@@ -250,7 +259,8 @@ retry:
bf143f
 
bf143f
     if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) {
bf143f
         trace_s390_pci_clp_dev_info(vfio_pci->vbasedev.name);
bf143f
-        return;
bf143f
+        g_free(info);
bf143f
+        return NULL;
bf143f
     }
bf143f
 
bf143f
     if (info->argsz > argsz) {
bf143f
@@ -259,6 +269,47 @@ retry:
bf143f
         goto retry;
bf143f
     }
bf143f
 
bf143f
+    return info;
bf143f
+}
bf143f
+
bf143f
+/*
bf143f
+ * Get the host function handle from the vfio CLP capabilities chain.  Returns
bf143f
+ * true if a fh value was placed into the provided buffer.  Returns false
bf143f
+ * if a fh could not be obtained (ioctl failed or capabilitiy version does
bf143f
+ * not include the fh)
bf143f
+ */
bf143f
+bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)
bf143f
+{
bf143f
+    g_autofree struct vfio_device_info *info = NULL;
bf143f
+
bf143f
+    assert(fh);
bf143f
+
bf143f
+    info = get_device_info(pbdev, sizeof(*info));
bf143f
+    if (!info) {
bf143f
+        return false;
bf143f
+    }
bf143f
+
bf143f
+    return get_host_fh(pbdev, info, fh);
bf143f
+}
bf143f
+
bf143f
+/*
bf143f
+ * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for
bf143f
+ * capabilities that contain information about CLP features provided by the
bf143f
+ * underlying host.
bf143f
+ * On entry, defaults have already been placed into the guest CLP response
bf143f
+ * buffers.  On exit, defaults will have been overwritten for any CLP features
bf143f
+ * found in the capability chain; defaults will remain for any CLP features not
bf143f
+ * found in the chain.
bf143f
+ */
bf143f
+void s390_pci_get_clp_info(S390PCIBusDevice *pbdev)
bf143f
+{
bf143f
+    g_autofree struct vfio_device_info *info = NULL;
bf143f
+
bf143f
+    info = get_device_info(pbdev, sizeof(*info));
bf143f
+    if (!info) {
bf143f
+        return;
bf143f
+    }
bf143f
+
bf143f
     /*
bf143f
      * Find the CLP features provided and fill in the guest CLP responses.
bf143f
      * Always call s390_pci_read_base first as information from this could
bf143f
diff --git a/include/hw/s390x/s390-pci-vfio.h b/include/hw/s390x/s390-pci-vfio.h
bf143f
index ff708aef50..ae1b126ff7 100644
bf143f
--- a/include/hw/s390x/s390-pci-vfio.h
bf143f
+++ b/include/hw/s390x/s390-pci-vfio.h
bf143f
@@ -20,6 +20,7 @@ bool s390_pci_update_dma_avail(int fd, unsigned int *avail);
bf143f
 S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
bf143f
                                           S390PCIBusDevice *pbdev);
bf143f
 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt);
bf143f
+bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh);
bf143f
 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev);
bf143f
 #else
bf143f
 static inline bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
bf143f
@@ -33,6 +34,10 @@ static inline S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
bf143f
 }
bf143f
 static inline void s390_pci_end_dma_count(S390pciState *s,
bf143f
                                           S390PCIDMACount *cnt) { }
bf143f
+static inline bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)
bf143f
+{
bf143f
+    return false;
bf143f
+}
bf143f
 static inline void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) { }
bf143f
 #endif
bf143f
 
bf143f
-- 
bf143f
2.37.3
bf143f