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