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