vfio/pds: register with the pds_core PF
authorBrett Creeley <brett.creeley@amd.com>
Mon, 7 Aug 2023 20:57:51 +0000 (13:57 -0700)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 16 Aug 2023 16:53:20 +0000 (10:53 -0600)
The pds_core driver will supply adminq services, so find the PF
and register with the DSC services.

Use the following commands to enable a VF:
echo 1 > /sys/bus/pci/drivers/pds_core/$PF_BDF/sriov_numvfs

Signed-off-by: Brett Creeley <brett.creeley@amd.com>
Signed-off-by: Shannon Nelson <shannon.nelson@amd.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/20230807205755.29579-5-brett.creeley@amd.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/pci/pds/Makefile
drivers/vfio/pci/pds/cmds.c [new file with mode: 0644]
drivers/vfio/pci/pds/cmds.h [new file with mode: 0644]
drivers/vfio/pci/pds/pci_drv.c
drivers/vfio/pci/pds/pci_drv.h [new file with mode: 0644]
drivers/vfio/pci/pds/vfio_dev.c
drivers/vfio/pci/pds/vfio_dev.h
include/linux/pds/pds_common.h

index e5e53a6..91587c7 100644 (file)
@@ -4,5 +4,6 @@
 obj-$(CONFIG_PDS_VFIO_PCI) += pds-vfio-pci.o
 
 pds-vfio-pci-y := \
+       cmds.o          \
        pci_drv.o       \
        vfio_dev.o
diff --git a/drivers/vfio/pci/pds/cmds.c b/drivers/vfio/pci/pds/cmds.c
new file mode 100644 (file)
index 0000000..7f1618a
--- /dev/null
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
+
+#include <linux/io.h>
+#include <linux/types.h>
+
+#include <linux/pds/pds_common.h>
+#include <linux/pds/pds_core_if.h>
+#include <linux/pds/pds_adminq.h>
+
+#include "vfio_dev.h"
+#include "cmds.h"
+
+int pds_vfio_register_client_cmd(struct pds_vfio_pci_device *pds_vfio)
+{
+       struct pci_dev *pdev = pds_vfio_to_pci_dev(pds_vfio);
+       char devname[PDS_DEVNAME_LEN];
+       struct pdsc *pdsc;
+       int ci;
+
+       snprintf(devname, sizeof(devname), "%s.%d-%u", PDS_VFIO_LM_DEV_NAME,
+                pci_domain_nr(pdev->bus),
+                PCI_DEVID(pdev->bus->number, pdev->devfn));
+
+       pdsc = pdsc_get_pf_struct(pdev);
+       if (IS_ERR(pdsc))
+               return PTR_ERR(pdsc);
+
+       ci = pds_client_register(pdsc, devname);
+       if (ci < 0)
+               return ci;
+
+       pds_vfio->client_id = ci;
+
+       return 0;
+}
+
+void pds_vfio_unregister_client_cmd(struct pds_vfio_pci_device *pds_vfio)
+{
+       struct pci_dev *pdev = pds_vfio_to_pci_dev(pds_vfio);
+       struct pdsc *pdsc;
+       int err;
+
+       pdsc = pdsc_get_pf_struct(pdev);
+       if (IS_ERR(pdsc))
+               return;
+
+       err = pds_client_unregister(pdsc, pds_vfio->client_id);
+       if (err)
+               dev_err(&pdev->dev, "unregister from DSC failed: %pe\n",
+                       ERR_PTR(err));
+
+       pds_vfio->client_id = 0;
+}
diff --git a/drivers/vfio/pci/pds/cmds.h b/drivers/vfio/pci/pds/cmds.h
new file mode 100644 (file)
index 0000000..4c592af
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
+
+#ifndef _CMDS_H_
+#define _CMDS_H_
+
+int pds_vfio_register_client_cmd(struct pds_vfio_pci_device *pds_vfio);
+void pds_vfio_unregister_client_cmd(struct pds_vfio_pci_device *pds_vfio);
+
+#endif /* _CMDS_H_ */
index 4670ddd..d9d2725 100644 (file)
@@ -8,9 +8,13 @@
 #include <linux/types.h>
 #include <linux/vfio.h>
 
+#include <linux/pds/pds_common.h>
 #include <linux/pds/pds_core_if.h>
+#include <linux/pds/pds_adminq.h>
 
 #include "vfio_dev.h"
+#include "pci_drv.h"
+#include "cmds.h"
 
 #define PDS_VFIO_DRV_DESCRIPTION       "AMD/Pensando VFIO Device Driver"
 #define PCI_VENDOR_ID_PENSANDO         0x1dd8
@@ -32,8 +36,17 @@ static int pds_vfio_pci_probe(struct pci_dev *pdev,
        if (err)
                goto out_put_vdev;
 
+       err = pds_vfio_register_client_cmd(pds_vfio);
+       if (err) {
+               dev_err(&pdev->dev, "failed to register as client: %pe\n",
+                       ERR_PTR(err));
+               goto out_unregister_coredev;
+       }
+
        return 0;
 
+out_unregister_coredev:
+       vfio_pci_core_unregister_device(&pds_vfio->vfio_coredev);
 out_put_vdev:
        vfio_put_device(&pds_vfio->vfio_coredev.vdev);
        return err;
@@ -43,6 +56,7 @@ static void pds_vfio_pci_remove(struct pci_dev *pdev)
 {
        struct pds_vfio_pci_device *pds_vfio = pds_vfio_pci_drvdata(pdev);
 
+       pds_vfio_unregister_client_cmd(pds_vfio);
        vfio_pci_core_unregister_device(&pds_vfio->vfio_coredev);
        vfio_put_device(&pds_vfio->vfio_coredev.vdev);
 }
diff --git a/drivers/vfio/pci/pds/pci_drv.h b/drivers/vfio/pci/pds/pci_drv.h
new file mode 100644 (file)
index 0000000..e79bed1
--- /dev/null
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
+
+#ifndef _PCI_DRV_H
+#define _PCI_DRV_H
+
+#include <linux/pci.h>
+
+#endif /* _PCI_DRV_H */
index 6d7ff1e..ce42f0b 100644 (file)
@@ -6,6 +6,11 @@
 
 #include "vfio_dev.h"
 
+struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio)
+{
+       return pds_vfio->vfio_coredev.pdev;
+}
+
 struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev)
 {
        struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
@@ -20,7 +25,7 @@ static int pds_vfio_init_device(struct vfio_device *vdev)
                container_of(vdev, struct pds_vfio_pci_device,
                             vfio_coredev.vdev);
        struct pci_dev *pdev = to_pci_dev(vdev->dev);
-       int err, vf_id;
+       int err, vf_id, pci_id;
 
        vf_id = pci_iov_vf_id(pdev);
        if (vf_id < 0)
@@ -32,6 +37,12 @@ static int pds_vfio_init_device(struct vfio_device *vdev)
 
        pds_vfio->vf_id = vf_id;
 
+       pci_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
+       dev_dbg(&pdev->dev,
+               "%s: PF %#04x VF %#04x vf_id %d domain %d pds_vfio %p\n",
+               __func__, pci_dev_id(pdev->physfn), pci_id, vf_id,
+               pci_domain_nr(pdev->bus), pds_vfio);
+
        return 0;
 }
 
index a4d4b65..ba4ee8c 100644 (file)
@@ -11,9 +11,12 @@ struct pds_vfio_pci_device {
        struct vfio_pci_core_device vfio_coredev;
 
        int vf_id;
+       u16 client_id;
 };
 
 const struct vfio_device_ops *pds_vfio_ops_info(void);
 struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev);
 
+struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio);
+
 #endif /* _VFIO_DEV_H_ */
index 04427dc..30581e2 100644 (file)
@@ -34,12 +34,13 @@ enum pds_core_vif_types {
 
 #define PDS_DEV_TYPE_CORE_STR  "Core"
 #define PDS_DEV_TYPE_VDPA_STR  "vDPA"
-#define PDS_DEV_TYPE_VFIO_STR  "VFio"
+#define PDS_DEV_TYPE_VFIO_STR  "vfio"
 #define PDS_DEV_TYPE_ETH_STR   "Eth"
 #define PDS_DEV_TYPE_RDMA_STR  "RDMA"
 #define PDS_DEV_TYPE_LM_STR    "LM"
 
 #define PDS_VDPA_DEV_NAME      PDS_CORE_DRV_NAME "." PDS_DEV_TYPE_VDPA_STR
+#define PDS_VFIO_LM_DEV_NAME   PDS_CORE_DRV_NAME "." PDS_DEV_TYPE_LM_STR "." PDS_DEV_TYPE_VFIO_STR
 
 struct pdsc;