2 * Copyright (c) 2018 Citrix Systems Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
8 #include "qemu/osdep.h"
9 #include "qemu/error-report.h"
10 #include "qapi/error.h"
11 #include "hw/xen/xen-backend.h"
12 #include "hw/xen/xen-bus.h"
14 typedef struct XenBackendImpl
{
16 XenBackendDeviceCreate create
;
17 XenBackendDeviceDestroy destroy
;
20 struct XenBackendInstance
{
21 QLIST_ENTRY(XenBackendInstance
) entry
;
22 const XenBackendImpl
*impl
;
28 static GHashTable
*xen_backend_table_get(void)
30 static GHashTable
*table
;
33 table
= g_hash_table_new(g_str_hash
, g_str_equal
);
39 static void xen_backend_table_add(XenBackendImpl
*impl
)
41 g_hash_table_insert(xen_backend_table_get(), (void *)impl
->type
, impl
);
44 static const XenBackendImpl
*xen_backend_table_lookup(const char *type
)
46 return g_hash_table_lookup(xen_backend_table_get(), type
);
49 void xen_backend_register(const XenBackendInfo
*info
)
51 XenBackendImpl
*impl
= g_new0(XenBackendImpl
, 1);
55 if (xen_backend_table_lookup(info
->type
)) {
56 error_report("attempt to register duplicate Xen backend type '%s'",
62 error_report("backend type '%s' has no creator", info
->type
);
66 impl
->type
= info
->type
;
67 impl
->create
= info
->create
;
68 impl
->destroy
= info
->destroy
;
70 xen_backend_table_add(impl
);
73 static QLIST_HEAD(, XenBackendInstance
) backend_list
;
75 static void xen_backend_list_add(XenBackendInstance
*backend
)
77 QLIST_INSERT_HEAD(&backend_list
, backend
, entry
);
80 static XenBackendInstance
*xen_backend_list_find(XenDevice
*xendev
)
82 XenBackendInstance
*backend
;
84 QLIST_FOREACH(backend
, &backend_list
, entry
) {
85 if (backend
->xendev
== xendev
) {
93 static void xen_backend_list_remove(XenBackendInstance
*backend
)
95 QLIST_REMOVE(backend
, entry
);
98 void xen_backend_device_create(XenBus
*xenbus
, const char *type
,
99 const char *name
, QDict
*opts
, Error
**errp
)
101 const XenBackendImpl
*impl
= xen_backend_table_lookup(type
);
102 XenBackendInstance
*backend
;
103 Error
*local_error
= NULL
;
109 backend
= g_new0(XenBackendInstance
, 1);
110 backend
->xenbus
= xenbus
;
111 backend
->name
= g_strdup(name
);
113 impl
->create(backend
, opts
, &local_error
);
115 error_propagate(errp
, local_error
);
116 g_free(backend
->name
);
121 backend
->impl
= impl
;
122 xen_backend_list_add(backend
);
125 XenBus
*xen_backend_get_bus(XenBackendInstance
*backend
)
127 return backend
->xenbus
;
130 const char *xen_backend_get_name(XenBackendInstance
*backend
)
132 return backend
->name
;
135 void xen_backend_set_device(XenBackendInstance
*backend
,
138 g_assert(!backend
->xendev
);
139 backend
->xendev
= xendev
;
142 XenDevice
*xen_backend_get_device(XenBackendInstance
*backend
)
144 return backend
->xendev
;
148 bool xen_backend_try_device_destroy(XenDevice
*xendev
, Error
**errp
)
150 XenBackendInstance
*backend
= xen_backend_list_find(xendev
);
151 const XenBackendImpl
*impl
;
157 impl
= backend
->impl
;
158 impl
->destroy(backend
, errp
);
160 xen_backend_list_remove(backend
);
161 g_free(backend
->name
);