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/main-loop.h"
10 #include "qemu/module.h"
11 #include "qemu/uuid.h"
12 #include "hw/qdev-properties.h"
13 #include "hw/sysbus.h"
14 #include "hw/xen/xen.h"
15 #include "hw/xen/xen-backend.h"
16 #include "hw/xen/xen-bus.h"
17 #include "hw/xen/xen-bus-helper.h"
18 #include "monitor/monitor.h"
19 #include "qapi/error.h"
20 #include "qapi/qmp/qdict.h"
21 #include "sysemu/sysemu.h"
24 static char *xen_device_get_backend_path(XenDevice
*xendev
)
26 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
27 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
28 const char *type
= object_get_typename(OBJECT(xendev
));
29 const char *backend
= xendev_class
->backend
;
35 return g_strdup_printf("/local/domain/%u/backend/%s/%u/%s",
36 xenbus
->backend_id
, backend
, xendev
->frontend_id
,
40 static char *xen_device_get_frontend_path(XenDevice
*xendev
)
42 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
43 const char *type
= object_get_typename(OBJECT(xendev
));
44 const char *device
= xendev_class
->device
;
50 return g_strdup_printf("/local/domain/%u/device/%s/%s",
51 xendev
->frontend_id
, device
, xendev
->name
);
54 static void xen_device_unplug(XenDevice
*xendev
, Error
**errp
)
57 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
58 const char *type
= object_get_typename(OBJECT(xendev
));
61 trace_xen_device_unplug(type
, xendev
->name
);
63 /* Mimic the way the Xen toolstack does an unplug */
65 tid
= qemu_xen_xs_transaction_start(xenbus
->xsh
);
66 if (tid
== XBT_NULL
) {
67 error_setg_errno(errp
, errno
, "failed xs_transaction_start");
71 xs_node_printf(xenbus
->xsh
, tid
, xendev
->backend_path
, "online",
77 xs_node_printf(xenbus
->xsh
, tid
, xendev
->backend_path
, "state",
78 errp
, "%u", XenbusStateClosing
);
83 if (!qemu_xen_xs_transaction_end(xenbus
->xsh
, tid
, false)) {
84 if (errno
== EAGAIN
) {
88 error_setg_errno(errp
, errno
, "failed xs_transaction_end");
95 * We only abort if there is already a failure so ignore any error
96 * from ending the transaction.
98 qemu_xen_xs_transaction_end(xenbus
->xsh
, tid
, true);
101 static void xen_bus_print_dev(Monitor
*mon
, DeviceState
*dev
, int indent
)
103 XenDevice
*xendev
= XEN_DEVICE(dev
);
105 monitor_printf(mon
, "%*sname = '%s' frontend_id = %u\n",
106 indent
, "", xendev
->name
, xendev
->frontend_id
);
109 static char *xen_bus_get_dev_path(DeviceState
*dev
)
111 return xen_device_get_backend_path(XEN_DEVICE(dev
));
114 static void xen_bus_backend_create(XenBus
*xenbus
, const char *type
,
115 const char *name
, char *path
,
119 xs_transaction_t tid
;
124 trace_xen_bus_backend_create(type
, path
);
127 tid
= qemu_xen_xs_transaction_start(xenbus
->xsh
);
128 if (tid
== XBT_NULL
) {
129 error_setg(errp
, "failed xs_transaction_start");
133 key
= qemu_xen_xs_directory(xenbus
->xsh
, tid
, path
, &n
);
135 if (!qemu_xen_xs_transaction_end(xenbus
->xsh
, tid
, true)) {
136 error_setg_errno(errp
, errno
, "failed xs_transaction_end");
142 for (i
= 0; i
< n
; i
++) {
146 * Assume anything found in the xenstore backend area, other than
147 * the keys created for a generic XenDevice, are parameters
148 * to be used to configure the backend.
150 if (!strcmp(key
[i
], "state") ||
151 !strcmp(key
[i
], "online") ||
152 !strcmp(key
[i
], "frontend") ||
153 !strcmp(key
[i
], "frontend-id") ||
154 !strcmp(key
[i
], "hotplug-status"))
157 if (xs_node_scanf(xenbus
->xsh
, tid
, path
, key
[i
], NULL
, "%ms",
159 qdict_put_str(opts
, key
[i
], val
);
166 if (!qemu_xen_xs_transaction_end(xenbus
->xsh
, tid
, false)) {
169 if (errno
== EAGAIN
) {
173 error_setg_errno(errp
, errno
, "failed xs_transaction_end");
177 xen_backend_device_create(xenbus
, type
, name
, opts
, errp
);
181 error_prepend(errp
, "failed to create '%s' device '%s': ", type
, name
);
185 static void xen_bus_type_enumerate(XenBus
*xenbus
, const char *type
)
187 char *domain_path
= g_strdup_printf("backend/%s/%u", type
, xen_domid
);
191 trace_xen_bus_type_enumerate(type
);
193 backend
= qemu_xen_xs_directory(xenbus
->xsh
, XBT_NULL
, domain_path
, &n
);
198 for (i
= 0; i
< n
; i
++) {
199 char *backend_path
= g_strdup_printf("%s/%s", domain_path
,
201 enum xenbus_state state
;
204 if (xs_node_scanf(xenbus
->xsh
, XBT_NULL
, backend_path
, "state",
205 NULL
, "%u", &state
) != 1)
206 state
= XenbusStateUnknown
;
208 if (xs_node_scanf(xenbus
->xsh
, XBT_NULL
, backend_path
, "online",
209 NULL
, "%u", &online
) != 1)
212 if (online
&& state
== XenbusStateInitialising
&&
213 !xen_backend_exists(type
, backend
[i
])) {
214 Error
*local_err
= NULL
;
216 xen_bus_backend_create(xenbus
, type
, backend
[i
], backend_path
,
219 error_report_err(local_err
);
223 g_free(backend_path
);
232 static void xen_bus_enumerate(XenBus
*xenbus
)
237 trace_xen_bus_enumerate();
239 type
= qemu_xen_xs_directory(xenbus
->xsh
, XBT_NULL
, "backend", &n
);
244 for (i
= 0; i
< n
; i
++) {
245 xen_bus_type_enumerate(xenbus
, type
[i
]);
251 static void xen_bus_device_cleanup(XenDevice
*xendev
)
253 const char *type
= object_get_typename(OBJECT(xendev
));
254 Error
*local_err
= NULL
;
256 trace_xen_bus_device_cleanup(type
, xendev
->name
);
258 g_assert(!xendev
->backend_online
);
260 if (!xen_backend_try_device_destroy(xendev
, &local_err
)) {
261 object_unparent(OBJECT(xendev
));
265 error_report_err(local_err
);
269 static void xen_bus_cleanup(XenBus
*xenbus
)
271 XenDevice
*xendev
, *next
;
273 trace_xen_bus_cleanup();
275 QLIST_FOREACH_SAFE(xendev
, &xenbus
->inactive_devices
, list
, next
) {
276 g_assert(xendev
->inactive
);
277 QLIST_REMOVE(xendev
, list
);
278 xen_bus_device_cleanup(xendev
);
282 static void xen_bus_backend_changed(void *opaque
, const char *path
)
284 XenBus
*xenbus
= opaque
;
286 xen_bus_enumerate(xenbus
);
287 xen_bus_cleanup(xenbus
);
290 static void xen_bus_unrealize(BusState
*bus
)
292 XenBus
*xenbus
= XEN_BUS(bus
);
294 trace_xen_bus_unrealize();
296 if (xenbus
->backend_watch
) {
299 for (i
= 0; i
< xenbus
->backend_types
; i
++) {
300 if (xenbus
->backend_watch
[i
]) {
301 xs_node_unwatch(xenbus
->xsh
, xenbus
->backend_watch
[i
]);
305 g_free(xenbus
->backend_watch
);
306 xenbus
->backend_watch
= NULL
;
310 qemu_xen_xs_close(xenbus
->xsh
);
314 static void xen_bus_realize(BusState
*bus
, Error
**errp
)
316 char *key
= g_strdup_printf("%u", xen_domid
);
317 XenBus
*xenbus
= XEN_BUS(bus
);
321 Error
*local_err
= NULL
;
323 trace_xen_bus_realize();
325 xenbus
->xsh
= qemu_xen_xs_open();
327 error_setg_errno(errp
, errno
, "failed xs_open");
331 if (xs_node_scanf(xenbus
->xsh
, XBT_NULL
, "", /* domain root node */
332 "domid", NULL
, "%u", &domid
) == 1) {
333 xenbus
->backend_id
= domid
;
335 xenbus
->backend_id
= 0; /* Assume lack of node means dom0 */
338 module_call_init(MODULE_INIT_XEN_BACKEND
);
340 type
= xen_backend_get_types(&xenbus
->backend_types
);
341 xenbus
->backend_watch
= g_new(struct qemu_xs_watch
*,
342 xenbus
->backend_types
);
344 for (i
= 0; i
< xenbus
->backend_types
; i
++) {
345 char *node
= g_strdup_printf("backend/%s", type
[i
]);
347 xenbus
->backend_watch
[i
] =
348 xs_node_watch(xenbus
->xsh
, node
, key
, xen_bus_backend_changed
,
351 /* This need not be treated as a hard error so don't propagate */
352 error_reportf_err(local_err
,
353 "failed to set up '%s' enumeration watch: ",
365 xen_bus_unrealize(bus
);
369 static void xen_bus_unplug_request(HotplugHandler
*hotplug
,
373 XenDevice
*xendev
= XEN_DEVICE(dev
);
375 xen_device_unplug(xendev
, errp
);
378 static void xen_bus_class_init(ObjectClass
*class, void *data
)
380 BusClass
*bus_class
= BUS_CLASS(class);
381 HotplugHandlerClass
*hotplug_class
= HOTPLUG_HANDLER_CLASS(class);
383 bus_class
->print_dev
= xen_bus_print_dev
;
384 bus_class
->get_dev_path
= xen_bus_get_dev_path
;
385 bus_class
->realize
= xen_bus_realize
;
386 bus_class
->unrealize
= xen_bus_unrealize
;
388 hotplug_class
->unplug_request
= xen_bus_unplug_request
;
391 static const TypeInfo xen_bus_type_info
= {
392 .name
= TYPE_XEN_BUS
,
394 .instance_size
= sizeof(XenBus
),
395 .class_size
= sizeof(XenBusClass
),
396 .class_init
= xen_bus_class_init
,
397 .interfaces
= (InterfaceInfo
[]) {
398 { TYPE_HOTPLUG_HANDLER
},
403 void xen_device_backend_printf(XenDevice
*xendev
, const char *key
,
404 const char *fmt
, ...)
406 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
407 Error
*local_err
= NULL
;
410 g_assert(xenbus
->xsh
);
413 xs_node_vprintf(xenbus
->xsh
, XBT_NULL
, xendev
->backend_path
, key
,
414 &local_err
, fmt
, ap
);
418 error_report_err(local_err
);
423 static int xen_device_backend_scanf(XenDevice
*xendev
, const char *key
,
424 const char *fmt
, ...)
426 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
430 g_assert(xenbus
->xsh
);
433 rc
= xs_node_vscanf(xenbus
->xsh
, XBT_NULL
, xendev
->backend_path
, key
,
440 void xen_device_backend_set_state(XenDevice
*xendev
,
441 enum xenbus_state state
)
443 const char *type
= object_get_typename(OBJECT(xendev
));
445 if (xendev
->backend_state
== state
) {
449 trace_xen_device_backend_state(type
, xendev
->name
,
452 xendev
->backend_state
= state
;
453 xen_device_backend_printf(xendev
, "state", "%u", state
);
456 enum xenbus_state
xen_device_backend_get_state(XenDevice
*xendev
)
458 return xendev
->backend_state
;
461 static void xen_device_backend_set_online(XenDevice
*xendev
, bool online
)
463 const char *type
= object_get_typename(OBJECT(xendev
));
465 if (xendev
->backend_online
== online
) {
469 trace_xen_device_backend_online(type
, xendev
->name
, online
);
471 xendev
->backend_online
= online
;
472 xen_device_backend_printf(xendev
, "online", "%u", online
);
476 * Tell from the state whether the frontend is likely alive,
477 * i.e. it will react to a change of state of the backend.
479 static bool xen_device_frontend_is_active(XenDevice
*xendev
)
481 switch (xendev
->frontend_state
) {
482 case XenbusStateInitWait
:
483 case XenbusStateInitialised
:
484 case XenbusStateConnected
:
485 case XenbusStateClosing
:
492 static void xen_device_backend_changed(void *opaque
, const char *path
)
494 XenDevice
*xendev
= opaque
;
495 const char *type
= object_get_typename(OBJECT(xendev
));
496 enum xenbus_state state
;
499 trace_xen_device_backend_changed(type
, xendev
->name
);
501 if (xen_device_backend_scanf(xendev
, "state", "%u", &state
) != 1) {
502 state
= XenbusStateUnknown
;
505 xen_device_backend_set_state(xendev
, state
);
507 if (xen_device_backend_scanf(xendev
, "online", "%u", &online
) != 1) {
511 xen_device_backend_set_online(xendev
, !!online
);
514 * If the toolstack (or unplug request callback) has set the backend
515 * state to Closing, but there is no active frontend then set the
516 * backend state to Closed.
518 if (state
== XenbusStateClosing
&&
519 !xen_device_frontend_is_active(xendev
)) {
520 xen_device_backend_set_state(xendev
, XenbusStateClosed
);
524 * If a backend is still 'online' then we should leave it alone but,
525 * if a backend is not 'online', then the device is a candidate
526 * for destruction. Hence add it to the 'inactive' list to be cleaned
527 * by xen_bus_cleanup().
530 (state
== XenbusStateClosed
|| state
== XenbusStateInitialising
||
531 state
== XenbusStateInitWait
|| state
== XenbusStateUnknown
) &&
533 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
535 xendev
->inactive
= true;
536 QLIST_INSERT_HEAD(&xenbus
->inactive_devices
, xendev
, list
);
539 * Re-write the state to cause a XenBus backend_watch notification,
540 * resulting in a call to xen_bus_cleanup().
542 xen_device_backend_printf(xendev
, "state", "%u", state
);
546 static void xen_device_backend_create(XenDevice
*xendev
, Error
**errp
)
549 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
551 xendev
->backend_path
= xen_device_get_backend_path(xendev
);
553 g_assert(xenbus
->xsh
);
555 xs_node_create(xenbus
->xsh
, XBT_NULL
, xendev
->backend_path
,
556 xenbus
->backend_id
, xendev
->frontend_id
, XS_PERM_READ
, errp
);
558 error_prepend(errp
, "failed to create backend: ");
562 xendev
->backend_state_watch
=
563 xs_node_watch(xendev
->xsh
, xendev
->backend_path
,
564 "state", xen_device_backend_changed
, xendev
,
567 error_prepend(errp
, "failed to watch backend state: ");
571 xendev
->backend_online_watch
=
572 xs_node_watch(xendev
->xsh
, xendev
->backend_path
,
573 "online", xen_device_backend_changed
, xendev
,
576 error_prepend(errp
, "failed to watch backend online: ");
581 static void xen_device_backend_destroy(XenDevice
*xendev
)
583 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
584 Error
*local_err
= NULL
;
586 if (xendev
->backend_online_watch
) {
587 xs_node_unwatch(xendev
->xsh
, xendev
->backend_online_watch
);
588 xendev
->backend_online_watch
= NULL
;
591 if (xendev
->backend_state_watch
) {
592 xs_node_unwatch(xendev
->xsh
, xendev
->backend_state_watch
);
593 xendev
->backend_state_watch
= NULL
;
596 if (!xendev
->backend_path
) {
600 g_assert(xenbus
->xsh
);
602 xs_node_destroy(xenbus
->xsh
, XBT_NULL
, xendev
->backend_path
,
604 g_free(xendev
->backend_path
);
605 xendev
->backend_path
= NULL
;
608 error_report_err(local_err
);
612 void xen_device_frontend_printf(XenDevice
*xendev
, const char *key
,
613 const char *fmt
, ...)
615 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
616 Error
*local_err
= NULL
;
619 g_assert(xenbus
->xsh
);
622 xs_node_vprintf(xenbus
->xsh
, XBT_NULL
, xendev
->frontend_path
, key
,
623 &local_err
, fmt
, ap
);
627 error_report_err(local_err
);
631 int xen_device_frontend_scanf(XenDevice
*xendev
, const char *key
,
632 const char *fmt
, ...)
634 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
638 g_assert(xenbus
->xsh
);
641 rc
= xs_node_vscanf(xenbus
->xsh
, XBT_NULL
, xendev
->frontend_path
, key
,
648 static void xen_device_frontend_set_state(XenDevice
*xendev
,
649 enum xenbus_state state
,
652 const char *type
= object_get_typename(OBJECT(xendev
));
654 if (xendev
->frontend_state
== state
) {
658 trace_xen_device_frontend_state(type
, xendev
->name
,
661 xendev
->frontend_state
= state
;
663 xen_device_frontend_printf(xendev
, "state", "%u", state
);
667 static void xen_device_frontend_changed(void *opaque
, const char *path
)
669 XenDevice
*xendev
= opaque
;
670 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
671 const char *type
= object_get_typename(OBJECT(xendev
));
672 enum xenbus_state state
;
674 trace_xen_device_frontend_changed(type
, xendev
->name
);
676 if (xen_device_frontend_scanf(xendev
, "state", "%u", &state
) != 1) {
677 state
= XenbusStateUnknown
;
680 xen_device_frontend_set_state(xendev
, state
, false);
682 if (state
== XenbusStateInitialising
&&
683 xendev
->backend_state
== XenbusStateClosed
&&
684 xendev
->backend_online
) {
686 * The frontend is re-initializing so switch back to
689 xen_device_backend_set_state(xendev
, XenbusStateInitWait
);
693 if (xendev_class
->frontend_changed
) {
694 Error
*local_err
= NULL
;
696 xendev_class
->frontend_changed(xendev
, state
, &local_err
);
699 error_reportf_err(local_err
, "frontend change error: ");
704 static bool xen_device_frontend_exists(XenDevice
*xendev
)
706 enum xenbus_state state
;
708 return (xen_device_frontend_scanf(xendev
, "state", "%u", &state
) == 1);
711 static void xen_device_frontend_create(XenDevice
*xendev
, Error
**errp
)
714 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
715 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
717 if (xendev_class
->get_frontend_path
) {
718 xendev
->frontend_path
= xendev_class
->get_frontend_path(xendev
, errp
);
719 if (!xendev
->frontend_path
) {
720 error_prepend(errp
, "failed to create frontend: ");
724 xendev
->frontend_path
= xen_device_get_frontend_path(xendev
);
728 * The frontend area may have already been created by a legacy
731 if (!xen_device_frontend_exists(xendev
)) {
732 g_assert(xenbus
->xsh
);
734 xs_node_create(xenbus
->xsh
, XBT_NULL
, xendev
->frontend_path
,
735 xendev
->frontend_id
, xenbus
->backend_id
,
736 XS_PERM_READ
| XS_PERM_WRITE
, errp
);
738 error_prepend(errp
, "failed to create frontend: ");
743 xendev
->frontend_state_watch
=
744 xs_node_watch(xendev
->xsh
, xendev
->frontend_path
, "state",
745 xen_device_frontend_changed
, xendev
, errp
);
747 error_prepend(errp
, "failed to watch frontend state: ");
751 static void xen_device_frontend_destroy(XenDevice
*xendev
)
753 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
754 Error
*local_err
= NULL
;
756 if (xendev
->frontend_state_watch
) {
757 xs_node_unwatch(xendev
->xsh
, xendev
->frontend_state_watch
);
758 xendev
->frontend_state_watch
= NULL
;
761 if (!xendev
->frontend_path
) {
765 g_assert(xenbus
->xsh
);
767 xs_node_destroy(xenbus
->xsh
, XBT_NULL
, xendev
->frontend_path
,
769 g_free(xendev
->frontend_path
);
770 xendev
->frontend_path
= NULL
;
773 error_report_err(local_err
);
777 void xen_device_set_max_grant_refs(XenDevice
*xendev
, unsigned int nr_refs
,
780 if (qemu_xen_gnttab_set_max_grants(xendev
->xgth
, nr_refs
)) {
781 error_setg_errno(errp
, errno
, "xengnttab_set_max_grants failed");
785 void *xen_device_map_grant_refs(XenDevice
*xendev
, uint32_t *refs
,
786 unsigned int nr_refs
, int prot
,
789 void *map
= qemu_xen_gnttab_map_refs(xendev
->xgth
, nr_refs
,
790 xendev
->frontend_id
, refs
, prot
);
793 error_setg_errno(errp
, errno
,
794 "xengnttab_map_domain_grant_refs failed");
800 void xen_device_unmap_grant_refs(XenDevice
*xendev
, void *map
, uint32_t *refs
,
801 unsigned int nr_refs
, Error
**errp
)
803 if (qemu_xen_gnttab_unmap(xendev
->xgth
, map
, refs
, nr_refs
)) {
804 error_setg_errno(errp
, errno
, "xengnttab_unmap failed");
808 void xen_device_copy_grant_refs(XenDevice
*xendev
, bool to_domain
,
809 XenDeviceGrantCopySegment segs
[],
810 unsigned int nr_segs
, Error
**errp
)
812 qemu_xen_gnttab_grant_copy(xendev
->xgth
, to_domain
, xendev
->frontend_id
,
813 (XenGrantCopySegment
*)segs
, nr_segs
, errp
);
816 struct XenEventChannel
{
817 QLIST_ENTRY(XenEventChannel
) list
;
819 xenevtchn_handle
*xeh
;
820 evtchn_port_t local_port
;
821 XenEventHandler handler
;
825 static bool xen_device_poll(void *opaque
)
827 XenEventChannel
*channel
= opaque
;
829 return channel
->handler(channel
->opaque
);
832 static void xen_device_event(void *opaque
)
834 XenEventChannel
*channel
= opaque
;
835 unsigned long port
= qemu_xen_evtchn_pending(channel
->xeh
);
837 if (port
== channel
->local_port
) {
838 xen_device_poll(channel
);
840 qemu_xen_evtchn_unmask(channel
->xeh
, port
);
844 void xen_device_set_event_channel_context(XenDevice
*xendev
,
845 XenEventChannel
*channel
,
850 error_setg(errp
, "bad channel");
855 aio_set_fd_handler(channel
->ctx
, qemu_xen_evtchn_fd(channel
->xeh
),
856 NULL
, NULL
, NULL
, NULL
, NULL
);
860 aio_set_fd_handler(channel
->ctx
, qemu_xen_evtchn_fd(channel
->xeh
),
861 xen_device_event
, NULL
, xen_device_poll
, NULL
,
866 XenEventChannel
*xen_device_bind_event_channel(XenDevice
*xendev
,
868 XenEventHandler handler
,
869 void *opaque
, Error
**errp
)
871 XenEventChannel
*channel
= g_new0(XenEventChannel
, 1);
872 xenevtchn_port_or_error_t local_port
;
874 channel
->xeh
= qemu_xen_evtchn_open();
876 error_setg_errno(errp
, errno
, "failed xenevtchn_open");
880 local_port
= qemu_xen_evtchn_bind_interdomain(channel
->xeh
,
883 if (local_port
< 0) {
884 error_setg_errno(errp
, errno
, "xenevtchn_bind_interdomain failed");
888 channel
->local_port
= local_port
;
889 channel
->handler
= handler
;
890 channel
->opaque
= opaque
;
892 /* Only reason for failure is a NULL channel */
893 xen_device_set_event_channel_context(xendev
, channel
,
894 qemu_get_aio_context(),
897 QLIST_INSERT_HEAD(&xendev
->event_channels
, channel
, list
);
903 qemu_xen_evtchn_close(channel
->xeh
);
911 void xen_device_notify_event_channel(XenDevice
*xendev
,
912 XenEventChannel
*channel
,
916 error_setg(errp
, "bad channel");
920 if (qemu_xen_evtchn_notify(channel
->xeh
, channel
->local_port
) < 0) {
921 error_setg_errno(errp
, errno
, "xenevtchn_notify failed");
925 unsigned int xen_event_channel_get_local_port(XenEventChannel
*channel
)
927 return channel
->local_port
;
930 void xen_device_unbind_event_channel(XenDevice
*xendev
,
931 XenEventChannel
*channel
,
935 error_setg(errp
, "bad channel");
939 QLIST_REMOVE(channel
, list
);
942 aio_set_fd_handler(channel
->ctx
, qemu_xen_evtchn_fd(channel
->xeh
),
943 NULL
, NULL
, NULL
, NULL
, NULL
);
946 if (qemu_xen_evtchn_unbind(channel
->xeh
, channel
->local_port
) < 0) {
947 error_setg_errno(errp
, errno
, "xenevtchn_unbind failed");
950 qemu_xen_evtchn_close(channel
->xeh
);
954 static void xen_device_unrealize(DeviceState
*dev
)
956 XenDevice
*xendev
= XEN_DEVICE(dev
);
957 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
958 const char *type
= object_get_typename(OBJECT(xendev
));
959 XenEventChannel
*channel
, *next
;
965 trace_xen_device_unrealize(type
, xendev
->name
);
967 if (xendev
->exit
.notify
) {
968 qemu_remove_exit_notifier(&xendev
->exit
);
969 xendev
->exit
.notify
= NULL
;
972 if (xendev_class
->unrealize
) {
973 xendev_class
->unrealize(xendev
);
976 /* Make sure all event channels are cleaned up */
977 QLIST_FOREACH_SAFE(channel
, &xendev
->event_channels
, list
, next
) {
978 xen_device_unbind_event_channel(xendev
, channel
, NULL
);
981 xen_device_frontend_destroy(xendev
);
982 xen_device_backend_destroy(xendev
);
985 qemu_xen_gnttab_close(xendev
->xgth
);
990 qemu_xen_xs_close(xendev
->xsh
);
994 g_free(xendev
->name
);
998 static void xen_device_exit(Notifier
*n
, void *data
)
1000 XenDevice
*xendev
= container_of(n
, XenDevice
, exit
);
1002 xen_device_unrealize(DEVICE(xendev
));
1005 static void xen_device_realize(DeviceState
*dev
, Error
**errp
)
1008 XenDevice
*xendev
= XEN_DEVICE(dev
);
1009 XenDeviceClass
*xendev_class
= XEN_DEVICE_GET_CLASS(xendev
);
1010 XenBus
*xenbus
= XEN_BUS(qdev_get_parent_bus(DEVICE(xendev
)));
1011 const char *type
= object_get_typename(OBJECT(xendev
));
1013 if (xendev
->frontend_id
== DOMID_INVALID
) {
1014 xendev
->frontend_id
= xen_domid
;
1017 if (xendev
->frontend_id
>= DOMID_FIRST_RESERVED
) {
1018 error_setg(errp
, "invalid frontend-id");
1022 if (!xendev_class
->get_name
) {
1023 error_setg(errp
, "get_name method not implemented");
1027 xendev
->name
= xendev_class
->get_name(xendev
, errp
);
1029 error_prepend(errp
, "failed to get device name: ");
1033 trace_xen_device_realize(type
, xendev
->name
);
1035 xendev
->xsh
= qemu_xen_xs_open();
1037 error_setg_errno(errp
, errno
, "failed xs_open");
1041 xendev
->xgth
= qemu_xen_gnttab_open();
1042 if (!xendev
->xgth
) {
1043 error_setg_errno(errp
, errno
, "failed xengnttab_open");
1047 xen_device_backend_create(xendev
, errp
);
1052 xen_device_frontend_create(xendev
, errp
);
1057 xen_device_backend_printf(xendev
, "frontend", "%s",
1058 xendev
->frontend_path
);
1059 xen_device_backend_printf(xendev
, "frontend-id", "%u",
1060 xendev
->frontend_id
);
1061 xen_device_backend_printf(xendev
, "hotplug-status", "connected");
1063 xen_device_backend_set_online(xendev
, true);
1064 xen_device_backend_set_state(xendev
, XenbusStateInitWait
);
1066 if (!xen_device_frontend_exists(xendev
)) {
1067 xen_device_frontend_printf(xendev
, "backend", "%s",
1068 xendev
->backend_path
);
1069 xen_device_frontend_printf(xendev
, "backend-id", "%u",
1070 xenbus
->backend_id
);
1072 xen_device_frontend_set_state(xendev
, XenbusStateInitialising
, true);
1075 if (xendev_class
->realize
) {
1076 xendev_class
->realize(xendev
, errp
);
1082 xendev
->exit
.notify
= xen_device_exit
;
1083 qemu_add_exit_notifier(&xendev
->exit
);
1087 xen_device_unrealize(dev
);
1090 static Property xen_device_props
[] = {
1091 DEFINE_PROP_UINT16("frontend-id", XenDevice
, frontend_id
,
1093 DEFINE_PROP_END_OF_LIST()
1096 static void xen_device_class_init(ObjectClass
*class, void *data
)
1098 DeviceClass
*dev_class
= DEVICE_CLASS(class);
1100 dev_class
->realize
= xen_device_realize
;
1101 dev_class
->unrealize
= xen_device_unrealize
;
1102 device_class_set_props(dev_class
, xen_device_props
);
1103 dev_class
->bus_type
= TYPE_XEN_BUS
;
1106 static const TypeInfo xen_device_type_info
= {
1107 .name
= TYPE_XEN_DEVICE
,
1108 .parent
= TYPE_DEVICE
,
1109 .instance_size
= sizeof(XenDevice
),
1111 .class_size
= sizeof(XenDeviceClass
),
1112 .class_init
= xen_device_class_init
,
1115 typedef struct XenBridge
{
1116 SysBusDevice busdev
;
1119 #define TYPE_XEN_BRIDGE "xen-bridge"
1121 static const TypeInfo xen_bridge_type_info
= {
1122 .name
= TYPE_XEN_BRIDGE
,
1123 .parent
= TYPE_SYS_BUS_DEVICE
,
1124 .instance_size
= sizeof(XenBridge
),
1127 static void xen_register_types(void)
1129 type_register_static(&xen_bridge_type_info
);
1130 type_register_static(&xen_bus_type_info
);
1131 type_register_static(&xen_device_type_info
);
1134 type_init(xen_register_types
)
1136 BusState
*xen_bus_init(void)
1138 DeviceState
*dev
= qdev_new(TYPE_XEN_BRIDGE
);
1139 BusState
*bus
= qbus_new(TYPE_XEN_BUS
, dev
, NULL
);
1141 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
1142 qbus_set_bus_hotplug_handler(bus
);