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