[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v10 07/14] vfio-user: find and init PCI device
From: |
Jagannathan Raman |
Subject: |
[PATCH v10 07/14] vfio-user: find and init PCI device |
Date: |
Tue, 24 May 2022 11:30:26 -0400 |
Find the PCI device with specified id. Initialize the device context
with the QEMU PCI device
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---
hw/remote/vfio-user-obj.c | 67 +++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index 68aac0c2b9..fdee274933 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -43,6 +43,8 @@
#include "qemu/notify.h"
#include "sysemu/sysemu.h"
#include "libvfio-user.h"
+#include "hw/qdev-core.h"
+#include "hw/pci/pci.h"
#define TYPE_VFU_OBJECT "x-vfio-user-server"
OBJECT_DECLARE_TYPE(VfuObject, VfuObjectClass, VFU_OBJECT)
@@ -80,6 +82,10 @@ struct VfuObject {
Notifier machine_done;
vfu_ctx_t *vfu_ctx;
+
+ PCIDevice *pci_dev;
+
+ Error *unplug_blocker;
};
static void vfu_object_init_ctx(VfuObject *o, Error **errp);
@@ -195,6 +201,9 @@ static void vfu_object_machine_done(Notifier *notifier,
void *data)
static void vfu_object_init_ctx(VfuObject *o, Error **errp)
{
ERRP_GUARD();
+ DeviceState *dev = NULL;
+ vfu_pci_type_t pci_type = VFU_PCI_TYPE_CONVENTIONAL;
+ int ret;
if (o->vfu_ctx || !o->socket || !o->device ||
!phase_check(PHASE_MACHINE_READY)) {
@@ -213,6 +222,53 @@ static void vfu_object_init_ctx(VfuObject *o, Error **errp)
error_setg(errp, "vfu: Failed to create context - %s",
strerror(errno));
return;
}
+
+ dev = qdev_find_recursive(sysbus_get_default(), o->device);
+ if (dev == NULL) {
+ error_setg(errp, "vfu: Device %s not found", o->device);
+ goto fail;
+ }
+
+ if (!object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
+ error_setg(errp, "vfu: %s not a PCI device", o->device);
+ goto fail;
+ }
+
+ o->pci_dev = PCI_DEVICE(dev);
+
+ object_ref(OBJECT(o->pci_dev));
+
+ if (pci_is_express(o->pci_dev)) {
+ pci_type = VFU_PCI_TYPE_EXPRESS;
+ }
+
+ ret = vfu_pci_init(o->vfu_ctx, pci_type, PCI_HEADER_TYPE_NORMAL, 0);
+ if (ret < 0) {
+ error_setg(errp,
+ "vfu: Failed to attach PCI device %s to context - %s",
+ o->device, strerror(errno));
+ goto fail;
+ }
+
+ error_setg(&o->unplug_blocker,
+ "vfu: %s for %s must be deleted before unplugging",
+ TYPE_VFU_OBJECT, o->device);
+ qdev_add_unplug_blocker(DEVICE(o->pci_dev), o->unplug_blocker);
+
+ return;
+
+fail:
+ vfu_destroy_ctx(o->vfu_ctx);
+ if (o->unplug_blocker && o->pci_dev) {
+ qdev_del_unplug_blocker(DEVICE(o->pci_dev), o->unplug_blocker);
+ error_free(o->unplug_blocker);
+ o->unplug_blocker = NULL;
+ }
+ if (o->pci_dev) {
+ object_unref(OBJECT(o->pci_dev));
+ o->pci_dev = NULL;
+ }
+ o->vfu_ctx = NULL;
}
static void vfu_object_init(Object *obj)
@@ -255,6 +311,17 @@ static void vfu_object_finalize(Object *obj)
o->device = NULL;
+ if (o->unplug_blocker && o->pci_dev) {
+ qdev_del_unplug_blocker(DEVICE(o->pci_dev), o->unplug_blocker);
+ error_free(o->unplug_blocker);
+ o->unplug_blocker = NULL;
+ }
+
+ if (o->pci_dev) {
+ object_unref(OBJECT(o->pci_dev));
+ o->pci_dev = NULL;
+ }
+
if (!k->nr_devs && vfu_object_auto_shutdown()) {
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
}
--
2.20.1
- [PATCH v10 00/14] vfio-user server in QEMU, Jagannathan Raman, 2022/05/24
- [PATCH v10 02/14] remote/machine: add HotplugHandler for remote machine, Jagannathan Raman, 2022/05/24
- [PATCH v10 01/14] qdev: unplug blocker for devices, Jagannathan Raman, 2022/05/24
- [PATCH v10 06/14] vfio-user: instantiate vfio-user context, Jagannathan Raman, 2022/05/24
- [PATCH v10 03/14] remote/machine: add vfio-user property, Jagannathan Raman, 2022/05/24
- [PATCH v10 04/14] vfio-user: build library, Jagannathan Raman, 2022/05/24
- [PATCH v10 08/14] vfio-user: run vfio-user context, Jagannathan Raman, 2022/05/24
- [PATCH v10 07/14] vfio-user: find and init PCI device,
Jagannathan Raman <=
- [PATCH v10 09/14] vfio-user: handle PCI config space accesses, Jagannathan Raman, 2022/05/24
- [PATCH v10 05/14] vfio-user: define vfio-user-server object, Jagannathan Raman, 2022/05/24
- [PATCH v10 14/14] vfio-user: handle reset of remote device, Jagannathan Raman, 2022/05/24
- [PATCH v10 12/14] vfio-user: handle PCI BAR accesses, Jagannathan Raman, 2022/05/24
- [PATCH v10 11/14] vfio-user: handle DMA mappings, Jagannathan Raman, 2022/05/24
- [PATCH v10 10/14] vfio-user: IOMMU support for remote device, Jagannathan Raman, 2022/05/24
- [PATCH v10 13/14] vfio-user: handle device interrupts, Jagannathan Raman, 2022/05/24