1 /* 2 * Copyright (c) 2018 Citrix Systems Inc. 3 * 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. 6 */ 7 8 #ifndef HW_XEN_BUS_H 9 #define HW_XEN_BUS_H 10 11 #include "hw/xen/xen_backend_ops.h" 12 #include "hw/sysbus.h" 13 #include "qemu/notify.h" 14 #include "qom/object.h" 15 16 typedef struct XenEventChannel XenEventChannel; 17 18 struct XenDevice { 19 DeviceState qdev; 20 domid_t frontend_id; 21 char *name; 22 struct qemu_xs_handle *xsh; 23 char *backend_path, *frontend_path; 24 enum xenbus_state backend_state, frontend_state; 25 Notifier exit; 26 struct qemu_xs_watch *backend_state_watch, *frontend_state_watch; 27 bool backend_online; 28 struct qemu_xs_watch *backend_online_watch; 29 xengnttab_handle *xgth; 30 bool inactive; 31 QLIST_HEAD(, XenEventChannel) event_channels; 32 QLIST_ENTRY(XenDevice) list; 33 }; 34 typedef struct XenDevice XenDevice; 35 36 typedef char *(*XenDeviceGetFrontendPath)(XenDevice *xendev, Error **errp); 37 typedef char *(*XenDeviceGetName)(XenDevice *xendev, Error **errp); 38 typedef void (*XenDeviceRealize)(XenDevice *xendev, Error **errp); 39 typedef void (*XenDeviceFrontendChanged)(XenDevice *xendev, 40 enum xenbus_state frontend_state, 41 Error **errp); 42 typedef void (*XenDeviceUnrealize)(XenDevice *xendev); 43 44 struct XenDeviceClass { 45 /*< private >*/ 46 DeviceClass parent_class; 47 /*< public >*/ 48 const char *backend; 49 const char *device; 50 XenDeviceGetFrontendPath get_frontend_path; 51 XenDeviceGetName get_name; 52 XenDeviceRealize realize; 53 XenDeviceFrontendChanged frontend_changed; 54 XenDeviceUnrealize unrealize; 55 }; 56 57 #define TYPE_XEN_DEVICE "xen-device" 58 OBJECT_DECLARE_TYPE(XenDevice, XenDeviceClass, XEN_DEVICE) 59 60 struct XenBus { 61 BusState qbus; 62 domid_t backend_id; 63 struct qemu_xs_handle *xsh; 64 unsigned int backend_types; 65 struct qemu_xs_watch **backend_watch; 66 QLIST_HEAD(, XenDevice) inactive_devices; 67 }; 68 69 struct XenBusClass { 70 /*< private >*/ 71 BusClass parent_class; 72 }; 73 74 #define TYPE_XEN_BUS "xen-bus" 75 OBJECT_DECLARE_TYPE(XenBus, XenBusClass, 76 XEN_BUS) 77 78 BusState *xen_bus_init(void); 79 80 void xen_device_backend_set_state(XenDevice *xendev, 81 enum xenbus_state state); 82 enum xenbus_state xen_device_backend_get_state(XenDevice *xendev); 83 84 void xen_device_backend_printf(XenDevice *xendev, const char *key, 85 const char *fmt, ...) 86 G_GNUC_PRINTF(3, 4); 87 void xen_device_frontend_printf(XenDevice *xendev, const char *key, 88 const char *fmt, ...) 89 G_GNUC_PRINTF(3, 4); 90 91 int xen_device_frontend_scanf(XenDevice *xendev, const char *key, 92 const char *fmt, ...) 93 G_GNUC_SCANF(3, 4); 94 95 void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs, 96 Error **errp); 97 void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs, 98 unsigned int nr_refs, int prot, 99 Error **errp); 100 void xen_device_unmap_grant_refs(XenDevice *xendev, void *map, uint32_t *refs, 101 unsigned int nr_refs, Error **errp); 102 103 typedef struct XenDeviceGrantCopySegment { 104 union { 105 void *virt; 106 struct { 107 uint32_t ref; 108 off_t offset; 109 } foreign; 110 } source, dest; 111 size_t len; 112 } XenDeviceGrantCopySegment; 113 114 void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain, 115 XenDeviceGrantCopySegment segs[], 116 unsigned int nr_segs, Error **errp); 117 118 typedef bool (*XenEventHandler)(void *opaque); 119 120 XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev, 121 unsigned int port, 122 XenEventHandler handler, 123 void *opaque, Error **errp); 124 void xen_device_set_event_channel_context(XenDevice *xendev, 125 XenEventChannel *channel, 126 AioContext *ctx, 127 Error **errp); 128 void xen_device_notify_event_channel(XenDevice *xendev, 129 XenEventChannel *channel, 130 Error **errp); 131 void xen_device_unbind_event_channel(XenDevice *xendev, 132 XenEventChannel *channel, 133 Error **errp); 134 unsigned int xen_event_channel_get_local_port(XenEventChannel *channel); 135 136 #endif /* HW_XEN_BUS_H */ 137