xref: /openbmc/qemu/hw/xen/xen-bus.c (revision 1de7096d8378a57e2d75d9cacc9a119e7e41640d)
1108f7bbaSPaul Durrant /*
2108f7bbaSPaul Durrant  * Copyright (c) 2018  Citrix Systems Inc.
3108f7bbaSPaul Durrant  *
4108f7bbaSPaul Durrant  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5108f7bbaSPaul Durrant  * See the COPYING file in the top-level directory.
6108f7bbaSPaul Durrant  */
7108f7bbaSPaul Durrant 
8108f7bbaSPaul Durrant #include "qemu/osdep.h"
982a29e30SPaul Durrant #include "qemu/main-loop.h"
100b8fa32fSMarkus Armbruster #include "qemu/module.h"
1182a29e30SPaul Durrant #include "qemu/uuid.h"
12a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
13108f7bbaSPaul Durrant #include "hw/sysbus.h"
14094a2239SPaul Durrant #include "hw/xen/xen.h"
15a783f8adSPaul Durrant #include "hw/xen/xen-backend.h"
16108f7bbaSPaul Durrant #include "hw/xen/xen-bus.h"
17094a2239SPaul Durrant #include "hw/xen/xen-bus-helper.h"
18094a2239SPaul Durrant #include "monitor/monitor.h"
19108f7bbaSPaul Durrant #include "qapi/error.h"
20a783f8adSPaul Durrant #include "qapi/qmp/qdict.h"
21094a2239SPaul Durrant #include "sysemu/sysemu.h"
22108f7bbaSPaul Durrant #include "trace.h"
23108f7bbaSPaul Durrant 
24094a2239SPaul Durrant static char *xen_device_get_backend_path(XenDevice *xendev)
25094a2239SPaul Durrant {
26094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
27094a2239SPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
28094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
29094a2239SPaul Durrant     const char *backend = xendev_class->backend;
30094a2239SPaul Durrant 
31094a2239SPaul Durrant     if (!backend) {
32094a2239SPaul Durrant         backend = type;
33094a2239SPaul Durrant     }
34094a2239SPaul Durrant 
35094a2239SPaul Durrant     return g_strdup_printf("/local/domain/%u/backend/%s/%u/%s",
36094a2239SPaul Durrant                            xenbus->backend_id, backend, xendev->frontend_id,
37094a2239SPaul Durrant                            xendev->name);
38094a2239SPaul Durrant }
39094a2239SPaul Durrant 
40094a2239SPaul Durrant static char *xen_device_get_frontend_path(XenDevice *xendev)
41094a2239SPaul Durrant {
42094a2239SPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
43094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
44094a2239SPaul Durrant     const char *device = xendev_class->device;
45094a2239SPaul Durrant 
46094a2239SPaul Durrant     if (!device) {
47094a2239SPaul Durrant         device = type;
48094a2239SPaul Durrant     }
49094a2239SPaul Durrant 
50094a2239SPaul Durrant     return g_strdup_printf("/local/domain/%u/device/%s/%s",
51094a2239SPaul Durrant                            xendev->frontend_id, device, xendev->name);
52094a2239SPaul Durrant }
53094a2239SPaul Durrant 
54b6af8926SPaul Durrant static void xen_device_unplug(XenDevice *xendev, Error **errp)
55b6af8926SPaul Durrant {
56*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
57b6af8926SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
58b6af8926SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
59b6af8926SPaul Durrant     xs_transaction_t tid;
60b6af8926SPaul Durrant 
61b6af8926SPaul Durrant     trace_xen_device_unplug(type, xendev->name);
62b6af8926SPaul Durrant 
63b6af8926SPaul Durrant     /* Mimic the way the Xen toolstack does an unplug */
64b6af8926SPaul Durrant again:
65b6af8926SPaul Durrant     tid = xs_transaction_start(xenbus->xsh);
66b6af8926SPaul Durrant     if (tid == XBT_NULL) {
67b6af8926SPaul Durrant         error_setg_errno(errp, errno, "failed xs_transaction_start");
68b6af8926SPaul Durrant         return;
69b6af8926SPaul Durrant     }
70b6af8926SPaul Durrant 
71b6af8926SPaul Durrant     xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "online",
72*1de7096dSVladimir Sementsov-Ogievskiy                    errp, "%u", 0);
73*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
74b6af8926SPaul Durrant         goto abort;
75b6af8926SPaul Durrant     }
76b6af8926SPaul Durrant 
77b6af8926SPaul Durrant     xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "state",
78*1de7096dSVladimir Sementsov-Ogievskiy                    errp, "%u", XenbusStateClosing);
79*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
80b6af8926SPaul Durrant         goto abort;
81b6af8926SPaul Durrant     }
82b6af8926SPaul Durrant 
83b6af8926SPaul Durrant     if (!xs_transaction_end(xenbus->xsh, tid, false)) {
84b6af8926SPaul Durrant         if (errno == EAGAIN) {
85b6af8926SPaul Durrant             goto again;
86b6af8926SPaul Durrant         }
87b6af8926SPaul Durrant 
88b6af8926SPaul Durrant         error_setg_errno(errp, errno, "failed xs_transaction_end");
89b6af8926SPaul Durrant     }
90b6af8926SPaul Durrant 
91b6af8926SPaul Durrant     return;
92b6af8926SPaul Durrant 
93b6af8926SPaul Durrant abort:
94b6af8926SPaul Durrant     /*
95b6af8926SPaul Durrant      * We only abort if there is already a failure so ignore any error
96b6af8926SPaul Durrant      * from ending the transaction.
97b6af8926SPaul Durrant      */
98b6af8926SPaul Durrant     xs_transaction_end(xenbus->xsh, tid, true);
99b6af8926SPaul Durrant }
100b6af8926SPaul Durrant 
101094a2239SPaul Durrant static void xen_bus_print_dev(Monitor *mon, DeviceState *dev, int indent)
102094a2239SPaul Durrant {
103094a2239SPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
104094a2239SPaul Durrant 
105094a2239SPaul Durrant     monitor_printf(mon, "%*sname = '%s' frontend_id = %u\n",
106094a2239SPaul Durrant                    indent, "", xendev->name, xendev->frontend_id);
107094a2239SPaul Durrant }
108094a2239SPaul Durrant 
109094a2239SPaul Durrant static char *xen_bus_get_dev_path(DeviceState *dev)
110094a2239SPaul Durrant {
111094a2239SPaul Durrant     return xen_device_get_backend_path(XEN_DEVICE(dev));
112094a2239SPaul Durrant }
113094a2239SPaul Durrant 
11482a29e30SPaul Durrant struct XenWatch {
11582a29e30SPaul Durrant     char *node, *key;
11682a29e30SPaul Durrant     char *token;
11782a29e30SPaul Durrant     XenWatchHandler handler;
11882a29e30SPaul Durrant     void *opaque;
11982a29e30SPaul Durrant     Notifier notifier;
12082a29e30SPaul Durrant };
12182a29e30SPaul Durrant 
12282a29e30SPaul Durrant static void watch_notify(Notifier *n, void *data)
12382a29e30SPaul Durrant {
12482a29e30SPaul Durrant     XenWatch *watch = container_of(n, XenWatch, notifier);
12582a29e30SPaul Durrant     const char *token = data;
12682a29e30SPaul Durrant 
12782a29e30SPaul Durrant     if (!strcmp(watch->token, token)) {
12882a29e30SPaul Durrant         watch->handler(watch->opaque);
12982a29e30SPaul Durrant     }
13082a29e30SPaul Durrant }
13182a29e30SPaul Durrant 
13282a29e30SPaul Durrant static XenWatch *new_watch(const char *node, const char *key,
13382a29e30SPaul Durrant                            XenWatchHandler handler, void *opaque)
13482a29e30SPaul Durrant {
13582a29e30SPaul Durrant     XenWatch *watch = g_new0(XenWatch, 1);
13682a29e30SPaul Durrant     QemuUUID uuid;
13782a29e30SPaul Durrant 
13882a29e30SPaul Durrant     qemu_uuid_generate(&uuid);
13982a29e30SPaul Durrant 
14082a29e30SPaul Durrant     watch->token = qemu_uuid_unparse_strdup(&uuid);
14182a29e30SPaul Durrant     watch->node = g_strdup(node);
14282a29e30SPaul Durrant     watch->key = g_strdup(key);
14382a29e30SPaul Durrant     watch->handler = handler;
14482a29e30SPaul Durrant     watch->opaque = opaque;
14582a29e30SPaul Durrant     watch->notifier.notify = watch_notify;
14682a29e30SPaul Durrant 
14782a29e30SPaul Durrant     return watch;
14882a29e30SPaul Durrant }
14982a29e30SPaul Durrant 
15082a29e30SPaul Durrant static void free_watch(XenWatch *watch)
15182a29e30SPaul Durrant {
15282a29e30SPaul Durrant     g_free(watch->token);
15382a29e30SPaul Durrant     g_free(watch->key);
15482a29e30SPaul Durrant     g_free(watch->node);
15582a29e30SPaul Durrant 
15682a29e30SPaul Durrant     g_free(watch);
15782a29e30SPaul Durrant }
15882a29e30SPaul Durrant 
159374752a2SPaul Durrant struct XenWatchList {
160374752a2SPaul Durrant     struct xs_handle *xsh;
161374752a2SPaul Durrant     NotifierList notifiers;
162374752a2SPaul Durrant };
163374752a2SPaul Durrant 
164374752a2SPaul Durrant static void watch_list_event(void *opaque)
165374752a2SPaul Durrant {
166374752a2SPaul Durrant     XenWatchList *watch_list = opaque;
167374752a2SPaul Durrant     char **v;
168374752a2SPaul Durrant     const char *token;
169374752a2SPaul Durrant 
170374752a2SPaul Durrant     v = xs_check_watch(watch_list->xsh);
171374752a2SPaul Durrant     if (!v) {
172374752a2SPaul Durrant         return;
173374752a2SPaul Durrant     }
174374752a2SPaul Durrant 
175374752a2SPaul Durrant     token = v[XS_WATCH_TOKEN];
176374752a2SPaul Durrant 
177374752a2SPaul Durrant     notifier_list_notify(&watch_list->notifiers, (void *)token);
178374752a2SPaul Durrant 
179374752a2SPaul Durrant     free(v);
180374752a2SPaul Durrant }
181374752a2SPaul Durrant 
182374752a2SPaul Durrant static XenWatchList *watch_list_create(struct xs_handle *xsh)
183374752a2SPaul Durrant {
184374752a2SPaul Durrant     XenWatchList *watch_list = g_new0(XenWatchList, 1);
185374752a2SPaul Durrant 
186374752a2SPaul Durrant     g_assert(xsh);
187374752a2SPaul Durrant 
188374752a2SPaul Durrant     watch_list->xsh = xsh;
189374752a2SPaul Durrant     notifier_list_init(&watch_list->notifiers);
190374752a2SPaul Durrant     qemu_set_fd_handler(xs_fileno(watch_list->xsh), watch_list_event, NULL,
191374752a2SPaul Durrant                         watch_list);
192374752a2SPaul Durrant 
193374752a2SPaul Durrant     return watch_list;
194374752a2SPaul Durrant }
195374752a2SPaul Durrant 
196374752a2SPaul Durrant static void watch_list_destroy(XenWatchList *watch_list)
197374752a2SPaul Durrant {
198374752a2SPaul Durrant     g_assert(notifier_list_empty(&watch_list->notifiers));
199374752a2SPaul Durrant     qemu_set_fd_handler(xs_fileno(watch_list->xsh), NULL, NULL, NULL);
200374752a2SPaul Durrant     g_free(watch_list);
201374752a2SPaul Durrant }
202374752a2SPaul Durrant 
203374752a2SPaul Durrant static XenWatch *watch_list_add(XenWatchList *watch_list, const char *node,
20482a29e30SPaul Durrant                                 const char *key, XenWatchHandler handler,
20582a29e30SPaul Durrant                                 void *opaque, Error **errp)
20682a29e30SPaul Durrant {
207*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
20882a29e30SPaul Durrant     XenWatch *watch = new_watch(node, key, handler, opaque);
20982a29e30SPaul Durrant 
210374752a2SPaul Durrant     notifier_list_add(&watch_list->notifiers, &watch->notifier);
21182a29e30SPaul Durrant 
212*1de7096dSVladimir Sementsov-Ogievskiy     xs_node_watch(watch_list->xsh, node, key, watch->token, errp);
213*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
21482a29e30SPaul Durrant         notifier_remove(&watch->notifier);
21582a29e30SPaul Durrant         free_watch(watch);
21682a29e30SPaul Durrant 
21782a29e30SPaul Durrant         return NULL;
21882a29e30SPaul Durrant     }
21982a29e30SPaul Durrant 
22082a29e30SPaul Durrant     return watch;
22182a29e30SPaul Durrant }
22282a29e30SPaul Durrant 
223374752a2SPaul Durrant static void watch_list_remove(XenWatchList *watch_list, XenWatch *watch,
22482a29e30SPaul Durrant                               Error **errp)
22582a29e30SPaul Durrant {
226374752a2SPaul Durrant     xs_node_unwatch(watch_list->xsh, watch->node, watch->key, watch->token,
22782a29e30SPaul Durrant                     errp);
22882a29e30SPaul Durrant 
22982a29e30SPaul Durrant     notifier_remove(&watch->notifier);
23082a29e30SPaul Durrant     free_watch(watch);
23182a29e30SPaul Durrant }
23282a29e30SPaul Durrant 
233374752a2SPaul Durrant static XenWatch *xen_bus_add_watch(XenBus *xenbus, const char *node,
234374752a2SPaul Durrant                                    const char *key, XenWatchHandler handler,
235d198b711SPaul Durrant                                    Error **errp)
236374752a2SPaul Durrant {
237374752a2SPaul Durrant     trace_xen_bus_add_watch(node, key);
238374752a2SPaul Durrant 
239d198b711SPaul Durrant     return watch_list_add(xenbus->watch_list, node, key, handler, xenbus,
240374752a2SPaul Durrant                           errp);
241374752a2SPaul Durrant }
242374752a2SPaul Durrant 
243374752a2SPaul Durrant static void xen_bus_remove_watch(XenBus *xenbus, XenWatch *watch,
244374752a2SPaul Durrant                                  Error **errp)
245374752a2SPaul Durrant {
246374752a2SPaul Durrant     trace_xen_bus_remove_watch(watch->node, watch->key);
247374752a2SPaul Durrant 
248374752a2SPaul Durrant     watch_list_remove(xenbus->watch_list, watch, errp);
249374752a2SPaul Durrant }
250374752a2SPaul Durrant 
251a783f8adSPaul Durrant static void xen_bus_backend_create(XenBus *xenbus, const char *type,
252a783f8adSPaul Durrant                                    const char *name, char *path,
253a783f8adSPaul Durrant                                    Error **errp)
254a783f8adSPaul Durrant {
255*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
256a783f8adSPaul Durrant     xs_transaction_t tid;
257a783f8adSPaul Durrant     char **key;
258a783f8adSPaul Durrant     QDict *opts;
259a783f8adSPaul Durrant     unsigned int i, n;
260a783f8adSPaul Durrant 
261a783f8adSPaul Durrant     trace_xen_bus_backend_create(type, path);
262a783f8adSPaul Durrant 
263a783f8adSPaul Durrant again:
264a783f8adSPaul Durrant     tid = xs_transaction_start(xenbus->xsh);
265a783f8adSPaul Durrant     if (tid == XBT_NULL) {
266a783f8adSPaul Durrant         error_setg(errp, "failed xs_transaction_start");
267a783f8adSPaul Durrant         return;
268a783f8adSPaul Durrant     }
269a783f8adSPaul Durrant 
270a783f8adSPaul Durrant     key = xs_directory(xenbus->xsh, tid, path, &n);
271a783f8adSPaul Durrant     if (!key) {
272a783f8adSPaul Durrant         if (!xs_transaction_end(xenbus->xsh, tid, true)) {
273a783f8adSPaul Durrant             error_setg_errno(errp, errno, "failed xs_transaction_end");
274a783f8adSPaul Durrant         }
275a783f8adSPaul Durrant         return;
276a783f8adSPaul Durrant     }
277a783f8adSPaul Durrant 
278a783f8adSPaul Durrant     opts = qdict_new();
279a783f8adSPaul Durrant     for (i = 0; i < n; i++) {
280a783f8adSPaul Durrant         char *val;
281a783f8adSPaul Durrant 
282a783f8adSPaul Durrant         /*
283a783f8adSPaul Durrant          * Assume anything found in the xenstore backend area, other than
284a783f8adSPaul Durrant          * the keys created for a generic XenDevice, are parameters
285a783f8adSPaul Durrant          * to be used to configure the backend.
286a783f8adSPaul Durrant          */
287a783f8adSPaul Durrant         if (!strcmp(key[i], "state") ||
288a783f8adSPaul Durrant             !strcmp(key[i], "online") ||
289a783f8adSPaul Durrant             !strcmp(key[i], "frontend") ||
290a783f8adSPaul Durrant             !strcmp(key[i], "frontend-id") ||
291a783f8adSPaul Durrant             !strcmp(key[i], "hotplug-status"))
292a783f8adSPaul Durrant             continue;
293a783f8adSPaul Durrant 
294a783f8adSPaul Durrant         if (xs_node_scanf(xenbus->xsh, tid, path, key[i], NULL, "%ms",
295a783f8adSPaul Durrant                           &val) == 1) {
296a783f8adSPaul Durrant             qdict_put_str(opts, key[i], val);
297a783f8adSPaul Durrant             free(val);
298a783f8adSPaul Durrant         }
299a783f8adSPaul Durrant     }
300a783f8adSPaul Durrant 
301a783f8adSPaul Durrant     free(key);
302a783f8adSPaul Durrant 
303a783f8adSPaul Durrant     if (!xs_transaction_end(xenbus->xsh, tid, false)) {
304a783f8adSPaul Durrant         qobject_unref(opts);
305a783f8adSPaul Durrant 
306a783f8adSPaul Durrant         if (errno == EAGAIN) {
307a783f8adSPaul Durrant             goto again;
308a783f8adSPaul Durrant         }
309a783f8adSPaul Durrant 
310a783f8adSPaul Durrant         error_setg_errno(errp, errno, "failed xs_transaction_end");
311a783f8adSPaul Durrant         return;
312a783f8adSPaul Durrant     }
313a783f8adSPaul Durrant 
314*1de7096dSVladimir Sementsov-Ogievskiy     xen_backend_device_create(xenbus, type, name, opts, errp);
315a783f8adSPaul Durrant     qobject_unref(opts);
316a783f8adSPaul Durrant 
317*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
318*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to create '%s' device '%s': ", type, name);
319a783f8adSPaul Durrant     }
320a783f8adSPaul Durrant }
321a783f8adSPaul Durrant 
322a783f8adSPaul Durrant static void xen_bus_type_enumerate(XenBus *xenbus, const char *type)
323a783f8adSPaul Durrant {
324a783f8adSPaul Durrant     char *domain_path = g_strdup_printf("backend/%s/%u", type, xen_domid);
325a783f8adSPaul Durrant     char **backend;
326a783f8adSPaul Durrant     unsigned int i, n;
327a783f8adSPaul Durrant 
328a783f8adSPaul Durrant     trace_xen_bus_type_enumerate(type);
329a783f8adSPaul Durrant 
330a783f8adSPaul Durrant     backend = xs_directory(xenbus->xsh, XBT_NULL, domain_path, &n);
331a783f8adSPaul Durrant     if (!backend) {
332a783f8adSPaul Durrant         goto out;
333a783f8adSPaul Durrant     }
334a783f8adSPaul Durrant 
335a783f8adSPaul Durrant     for (i = 0; i < n; i++) {
336a783f8adSPaul Durrant         char *backend_path = g_strdup_printf("%s/%s", domain_path,
337a783f8adSPaul Durrant                                              backend[i]);
3383809f758SPaul Durrant         enum xenbus_state state;
3393809f758SPaul Durrant         unsigned int online;
340a783f8adSPaul Durrant 
341a783f8adSPaul Durrant         if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "state",
3423809f758SPaul Durrant                           NULL, "%u", &state) != 1)
3433809f758SPaul Durrant             state = XenbusStateUnknown;
344a783f8adSPaul Durrant 
3453809f758SPaul Durrant         if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "online",
3463809f758SPaul Durrant                           NULL, "%u", &online) != 1)
3473809f758SPaul Durrant             online = 0;
3483809f758SPaul Durrant 
3493809f758SPaul Durrant         if (online && state == XenbusStateInitialising) {
350a783f8adSPaul Durrant             Error *local_err = NULL;
351a783f8adSPaul Durrant 
352a783f8adSPaul Durrant             xen_bus_backend_create(xenbus, type, backend[i], backend_path,
353a783f8adSPaul Durrant                                    &local_err);
354a783f8adSPaul Durrant             if (local_err) {
355a783f8adSPaul Durrant                 error_report_err(local_err);
356a783f8adSPaul Durrant             }
357a783f8adSPaul Durrant         }
358a783f8adSPaul Durrant 
359a783f8adSPaul Durrant         g_free(backend_path);
360a783f8adSPaul Durrant     }
361a783f8adSPaul Durrant 
362a783f8adSPaul Durrant     free(backend);
363a783f8adSPaul Durrant 
364a783f8adSPaul Durrant out:
365a783f8adSPaul Durrant     g_free(domain_path);
366a783f8adSPaul Durrant }
367a783f8adSPaul Durrant 
3683809f758SPaul Durrant static void xen_bus_enumerate(XenBus *xenbus)
369a783f8adSPaul Durrant {
370a783f8adSPaul Durrant     char **type;
371a783f8adSPaul Durrant     unsigned int i, n;
372a783f8adSPaul Durrant 
373a783f8adSPaul Durrant     trace_xen_bus_enumerate();
374a783f8adSPaul Durrant 
375a783f8adSPaul Durrant     type = xs_directory(xenbus->xsh, XBT_NULL, "backend", &n);
376a783f8adSPaul Durrant     if (!type) {
377a783f8adSPaul Durrant         return;
378a783f8adSPaul Durrant     }
379a783f8adSPaul Durrant 
380a783f8adSPaul Durrant     for (i = 0; i < n; i++) {
381a783f8adSPaul Durrant         xen_bus_type_enumerate(xenbus, type[i]);
382a783f8adSPaul Durrant     }
383a783f8adSPaul Durrant 
384a783f8adSPaul Durrant     free(type);
385a783f8adSPaul Durrant }
386a783f8adSPaul Durrant 
3873809f758SPaul Durrant static void xen_bus_device_cleanup(XenDevice *xendev)
3883809f758SPaul Durrant {
3893809f758SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
3903809f758SPaul Durrant     Error *local_err = NULL;
3913809f758SPaul Durrant 
3923809f758SPaul Durrant     trace_xen_bus_device_cleanup(type, xendev->name);
3933809f758SPaul Durrant 
3943809f758SPaul Durrant     g_assert(!xendev->backend_online);
3953809f758SPaul Durrant 
3963809f758SPaul Durrant     if (!xen_backend_try_device_destroy(xendev, &local_err)) {
3973809f758SPaul Durrant         object_unparent(OBJECT(xendev));
3983809f758SPaul Durrant     }
3993809f758SPaul Durrant 
4003809f758SPaul Durrant     if (local_err) {
4013809f758SPaul Durrant         error_report_err(local_err);
4023809f758SPaul Durrant     }
4033809f758SPaul Durrant }
4043809f758SPaul Durrant 
4053809f758SPaul Durrant static void xen_bus_cleanup(XenBus *xenbus)
4063809f758SPaul Durrant {
4073809f758SPaul Durrant     XenDevice *xendev, *next;
4083809f758SPaul Durrant 
4093809f758SPaul Durrant     trace_xen_bus_cleanup();
4103809f758SPaul Durrant 
4113809f758SPaul Durrant     QLIST_FOREACH_SAFE(xendev, &xenbus->inactive_devices, list, next) {
4123809f758SPaul Durrant         g_assert(xendev->inactive);
4133809f758SPaul Durrant         QLIST_REMOVE(xendev, list);
4143809f758SPaul Durrant         xen_bus_device_cleanup(xendev);
4153809f758SPaul Durrant     }
4163809f758SPaul Durrant }
4173809f758SPaul Durrant 
4183809f758SPaul Durrant static void xen_bus_backend_changed(void *opaque)
4193809f758SPaul Durrant {
4203809f758SPaul Durrant     XenBus *xenbus = opaque;
4213809f758SPaul Durrant 
4223809f758SPaul Durrant     xen_bus_enumerate(xenbus);
4233809f758SPaul Durrant     xen_bus_cleanup(xenbus);
4243809f758SPaul Durrant }
4253809f758SPaul Durrant 
426b69c3c21SMarkus Armbruster static void xen_bus_unrealize(BusState *bus)
427108f7bbaSPaul Durrant {
428094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(bus);
429094a2239SPaul Durrant 
430108f7bbaSPaul Durrant     trace_xen_bus_unrealize();
431094a2239SPaul Durrant 
432a783f8adSPaul Durrant     if (xenbus->backend_watch) {
433a783f8adSPaul Durrant         xen_bus_remove_watch(xenbus, xenbus->backend_watch, NULL);
434a783f8adSPaul Durrant         xenbus->backend_watch = NULL;
435a783f8adSPaul Durrant     }
436a783f8adSPaul Durrant 
437374752a2SPaul Durrant     if (xenbus->watch_list) {
438374752a2SPaul Durrant         watch_list_destroy(xenbus->watch_list);
439374752a2SPaul Durrant         xenbus->watch_list = NULL;
440094a2239SPaul Durrant     }
441094a2239SPaul Durrant 
442374752a2SPaul Durrant     if (xenbus->xsh) {
443094a2239SPaul Durrant         xs_close(xenbus->xsh);
444108f7bbaSPaul Durrant     }
44582a29e30SPaul Durrant }
44682a29e30SPaul Durrant 
447108f7bbaSPaul Durrant static void xen_bus_realize(BusState *bus, Error **errp)
448108f7bbaSPaul Durrant {
449094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(bus);
450094a2239SPaul Durrant     unsigned int domid;
451a783f8adSPaul Durrant     Error *local_err = NULL;
452094a2239SPaul Durrant 
453108f7bbaSPaul Durrant     trace_xen_bus_realize();
454094a2239SPaul Durrant 
455094a2239SPaul Durrant     xenbus->xsh = xs_open(0);
456094a2239SPaul Durrant     if (!xenbus->xsh) {
457094a2239SPaul Durrant         error_setg_errno(errp, errno, "failed xs_open");
458094a2239SPaul Durrant         goto fail;
459094a2239SPaul Durrant     }
460094a2239SPaul Durrant 
461094a2239SPaul Durrant     if (xs_node_scanf(xenbus->xsh, XBT_NULL, "", /* domain root node */
462094a2239SPaul Durrant                       "domid", NULL, "%u", &domid) == 1) {
463094a2239SPaul Durrant         xenbus->backend_id = domid;
464094a2239SPaul Durrant     } else {
465094a2239SPaul Durrant         xenbus->backend_id = 0; /* Assume lack of node means dom0 */
466094a2239SPaul Durrant     }
467094a2239SPaul Durrant 
468374752a2SPaul Durrant     xenbus->watch_list = watch_list_create(xenbus->xsh);
469a783f8adSPaul Durrant 
470a783f8adSPaul Durrant     module_call_init(MODULE_INIT_XEN_BACKEND);
471a783f8adSPaul Durrant 
472a783f8adSPaul Durrant     xenbus->backend_watch =
473a783f8adSPaul Durrant         xen_bus_add_watch(xenbus, "", /* domain root node */
4743809f758SPaul Durrant                           "backend", xen_bus_backend_changed, &local_err);
475a783f8adSPaul Durrant     if (local_err) {
476a783f8adSPaul Durrant         /* This need not be treated as a hard error so don't propagate */
477a783f8adSPaul Durrant         error_reportf_err(local_err,
478a783f8adSPaul Durrant                           "failed to set up enumeration watch: ");
479a783f8adSPaul Durrant     }
480a783f8adSPaul Durrant 
481094a2239SPaul Durrant     return;
482094a2239SPaul Durrant 
483094a2239SPaul Durrant fail:
484b69c3c21SMarkus Armbruster     xen_bus_unrealize(bus);
485108f7bbaSPaul Durrant }
486108f7bbaSPaul Durrant 
487b6af8926SPaul Durrant static void xen_bus_unplug_request(HotplugHandler *hotplug,
488b6af8926SPaul Durrant                                    DeviceState *dev,
489b6af8926SPaul Durrant                                    Error **errp)
490b6af8926SPaul Durrant {
491b6af8926SPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
492b6af8926SPaul Durrant 
493b6af8926SPaul Durrant     xen_device_unplug(xendev, errp);
494b6af8926SPaul Durrant }
495b6af8926SPaul Durrant 
496108f7bbaSPaul Durrant static void xen_bus_class_init(ObjectClass *class, void *data)
497108f7bbaSPaul Durrant {
498108f7bbaSPaul Durrant     BusClass *bus_class = BUS_CLASS(class);
499b6af8926SPaul Durrant     HotplugHandlerClass *hotplug_class = HOTPLUG_HANDLER_CLASS(class);
500108f7bbaSPaul Durrant 
501094a2239SPaul Durrant     bus_class->print_dev = xen_bus_print_dev;
502094a2239SPaul Durrant     bus_class->get_dev_path = xen_bus_get_dev_path;
503108f7bbaSPaul Durrant     bus_class->realize = xen_bus_realize;
504108f7bbaSPaul Durrant     bus_class->unrealize = xen_bus_unrealize;
505b6af8926SPaul Durrant 
506b6af8926SPaul Durrant     hotplug_class->unplug_request = xen_bus_unplug_request;
507108f7bbaSPaul Durrant }
508108f7bbaSPaul Durrant 
509108f7bbaSPaul Durrant static const TypeInfo xen_bus_type_info = {
510108f7bbaSPaul Durrant     .name = TYPE_XEN_BUS,
511108f7bbaSPaul Durrant     .parent = TYPE_BUS,
512108f7bbaSPaul Durrant     .instance_size = sizeof(XenBus),
513108f7bbaSPaul Durrant     .class_size = sizeof(XenBusClass),
514108f7bbaSPaul Durrant     .class_init = xen_bus_class_init,
515108f7bbaSPaul Durrant     .interfaces = (InterfaceInfo[]) {
516108f7bbaSPaul Durrant         { TYPE_HOTPLUG_HANDLER },
517108f7bbaSPaul Durrant         { }
518108f7bbaSPaul Durrant     },
519108f7bbaSPaul Durrant };
520108f7bbaSPaul Durrant 
521b6af8926SPaul Durrant void xen_device_backend_printf(XenDevice *xendev, const char *key,
522094a2239SPaul Durrant                                const char *fmt, ...)
523094a2239SPaul Durrant {
524094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
525094a2239SPaul Durrant     Error *local_err = NULL;
526094a2239SPaul Durrant     va_list ap;
527094a2239SPaul Durrant 
528094a2239SPaul Durrant     g_assert(xenbus->xsh);
529094a2239SPaul Durrant 
530094a2239SPaul Durrant     va_start(ap, fmt);
531094a2239SPaul Durrant     xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
532094a2239SPaul Durrant                     &local_err, fmt, ap);
533094a2239SPaul Durrant     va_end(ap);
534094a2239SPaul Durrant 
535094a2239SPaul Durrant     if (local_err) {
536094a2239SPaul Durrant         error_report_err(local_err);
537094a2239SPaul Durrant     }
538094a2239SPaul Durrant }
539094a2239SPaul Durrant 
54082a29e30SPaul Durrant static int xen_device_backend_scanf(XenDevice *xendev, const char *key,
54182a29e30SPaul Durrant                                     const char *fmt, ...)
54282a29e30SPaul Durrant {
54382a29e30SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
54482a29e30SPaul Durrant     va_list ap;
54582a29e30SPaul Durrant     int rc;
54682a29e30SPaul Durrant 
54782a29e30SPaul Durrant     g_assert(xenbus->xsh);
54882a29e30SPaul Durrant 
54982a29e30SPaul Durrant     va_start(ap, fmt);
55082a29e30SPaul Durrant     rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
55182a29e30SPaul Durrant                         NULL, fmt, ap);
55282a29e30SPaul Durrant     va_end(ap);
55382a29e30SPaul Durrant 
55482a29e30SPaul Durrant     return rc;
55582a29e30SPaul Durrant }
55682a29e30SPaul Durrant 
55782a29e30SPaul Durrant void xen_device_backend_set_state(XenDevice *xendev,
558094a2239SPaul Durrant                                   enum xenbus_state state)
559094a2239SPaul Durrant {
560094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
561094a2239SPaul Durrant 
562094a2239SPaul Durrant     if (xendev->backend_state == state) {
563094a2239SPaul Durrant         return;
564094a2239SPaul Durrant     }
565094a2239SPaul Durrant 
566094a2239SPaul Durrant     trace_xen_device_backend_state(type, xendev->name,
567094a2239SPaul Durrant                                    xs_strstate(state));
568094a2239SPaul Durrant 
569094a2239SPaul Durrant     xendev->backend_state = state;
570094a2239SPaul Durrant     xen_device_backend_printf(xendev, "state", "%u", state);
571094a2239SPaul Durrant }
572094a2239SPaul Durrant 
57382a29e30SPaul Durrant enum xenbus_state xen_device_backend_get_state(XenDevice *xendev)
57482a29e30SPaul Durrant {
57582a29e30SPaul Durrant     return xendev->backend_state;
57682a29e30SPaul Durrant }
57782a29e30SPaul Durrant 
578b6af8926SPaul Durrant static void xen_device_backend_set_online(XenDevice *xendev, bool online)
579b6af8926SPaul Durrant {
580b6af8926SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
581b6af8926SPaul Durrant 
582b6af8926SPaul Durrant     if (xendev->backend_online == online) {
583b6af8926SPaul Durrant         return;
584b6af8926SPaul Durrant     }
585b6af8926SPaul Durrant 
586b6af8926SPaul Durrant     trace_xen_device_backend_online(type, xendev->name, online);
587b6af8926SPaul Durrant 
588b6af8926SPaul Durrant     xendev->backend_online = online;
589b6af8926SPaul Durrant     xen_device_backend_printf(xendev, "online", "%u", online);
590b6af8926SPaul Durrant }
591b6af8926SPaul Durrant 
592cb323146SAnthony PERARD /*
593cb323146SAnthony PERARD  * Tell from the state whether the frontend is likely alive,
594cb323146SAnthony PERARD  * i.e. it will react to a change of state of the backend.
595cb323146SAnthony PERARD  */
5963809f758SPaul Durrant static bool xen_device_frontend_is_active(XenDevice *xendev)
597cb323146SAnthony PERARD {
5983809f758SPaul Durrant     switch (xendev->frontend_state) {
599cb323146SAnthony PERARD     case XenbusStateInitWait:
600cb323146SAnthony PERARD     case XenbusStateInitialised:
601cb323146SAnthony PERARD     case XenbusStateConnected:
602cb323146SAnthony PERARD     case XenbusStateClosing:
603cb323146SAnthony PERARD         return true;
604cb323146SAnthony PERARD     default:
605cb323146SAnthony PERARD         return false;
606cb323146SAnthony PERARD     }
607cb323146SAnthony PERARD }
608cb323146SAnthony PERARD 
609b6af8926SPaul Durrant static void xen_device_backend_changed(void *opaque)
610b6af8926SPaul Durrant {
611b6af8926SPaul Durrant     XenDevice *xendev = opaque;
612b6af8926SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
613b6af8926SPaul Durrant     enum xenbus_state state;
614b6af8926SPaul Durrant     unsigned int online;
615b6af8926SPaul Durrant 
616b6af8926SPaul Durrant     trace_xen_device_backend_changed(type, xendev->name);
617b6af8926SPaul Durrant 
618b6af8926SPaul Durrant     if (xen_device_backend_scanf(xendev, "state", "%u", &state) != 1) {
619b6af8926SPaul Durrant         state = XenbusStateUnknown;
620b6af8926SPaul Durrant     }
621b6af8926SPaul Durrant 
622b6af8926SPaul Durrant     xen_device_backend_set_state(xendev, state);
623b6af8926SPaul Durrant 
624b6af8926SPaul Durrant     if (xen_device_backend_scanf(xendev, "online", "%u", &online) != 1) {
625b6af8926SPaul Durrant         online = 0;
626b6af8926SPaul Durrant     }
627b6af8926SPaul Durrant 
628b6af8926SPaul Durrant     xen_device_backend_set_online(xendev, !!online);
629b6af8926SPaul Durrant 
630b6af8926SPaul Durrant     /*
631b6af8926SPaul Durrant      * If the toolstack (or unplug request callback) has set the backend
632cb323146SAnthony PERARD      * state to Closing, but there is no active frontend then set the
633cb323146SAnthony PERARD      * backend state to Closed.
634b6af8926SPaul Durrant      */
6353809f758SPaul Durrant     if (state == XenbusStateClosing &&
6363809f758SPaul Durrant         !xen_device_frontend_is_active(xendev)) {
637b6af8926SPaul Durrant         xen_device_backend_set_state(xendev, XenbusStateClosed);
638b6af8926SPaul Durrant     }
639b6af8926SPaul Durrant 
640b6af8926SPaul Durrant     /*
64167bc8e00SPaul Durrant      * If a backend is still 'online' then we should leave it alone but,
6423809f758SPaul Durrant      * if a backend is not 'online', then the device is a candidate
6433809f758SPaul Durrant      * for destruction. Hence add it to the 'inactive' list to be cleaned
6443809f758SPaul Durrant      * by xen_bus_cleanup().
645b6af8926SPaul Durrant      */
6463809f758SPaul Durrant     if (!online &&
6473809f758SPaul Durrant         (state == XenbusStateClosed ||  state == XenbusStateInitialising ||
6483809f758SPaul Durrant          state == XenbusStateInitWait || state == XenbusStateUnknown) &&
6493809f758SPaul Durrant         !xendev->inactive) {
6503809f758SPaul Durrant         XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
651a783f8adSPaul Durrant 
6523809f758SPaul Durrant         xendev->inactive = true;
6533809f758SPaul Durrant         QLIST_INSERT_HEAD(&xenbus->inactive_devices, xendev, list);
654a783f8adSPaul Durrant 
6553809f758SPaul Durrant         /*
6563809f758SPaul Durrant          * Re-write the state to cause a XenBus backend_watch notification,
6573809f758SPaul Durrant          * resulting in a call to xen_bus_cleanup().
6583809f758SPaul Durrant          */
6593809f758SPaul Durrant         xen_device_backend_printf(xendev, "state", "%u", state);
660a783f8adSPaul Durrant     }
661b6af8926SPaul Durrant }
662b6af8926SPaul Durrant 
663d198b711SPaul Durrant static XenWatch *xen_device_add_watch(XenDevice *xendev, const char *node,
664d198b711SPaul Durrant                                       const char *key,
665d198b711SPaul Durrant                                       XenWatchHandler handler,
666d198b711SPaul Durrant                                       Error **errp)
667d198b711SPaul Durrant {
668d198b711SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
669d198b711SPaul Durrant 
670d198b711SPaul Durrant     trace_xen_device_add_watch(type, xendev->name, node, key);
671d198b711SPaul Durrant 
672d198b711SPaul Durrant     return watch_list_add(xendev->watch_list, node, key, handler, xendev,
673d198b711SPaul Durrant                           errp);
674d198b711SPaul Durrant }
675d198b711SPaul Durrant 
676d198b711SPaul Durrant static void xen_device_remove_watch(XenDevice *xendev, XenWatch *watch,
677d198b711SPaul Durrant                                     Error **errp)
678d198b711SPaul Durrant {
679d198b711SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
680d198b711SPaul Durrant 
681d198b711SPaul Durrant     trace_xen_device_remove_watch(type, xendev->name, watch->node,
682d198b711SPaul Durrant                                   watch->key);
683d198b711SPaul Durrant 
684d198b711SPaul Durrant     watch_list_remove(xendev->watch_list, watch, errp);
685d198b711SPaul Durrant }
686d198b711SPaul Durrant 
687d198b711SPaul Durrant 
688094a2239SPaul Durrant static void xen_device_backend_create(XenDevice *xendev, Error **errp)
689094a2239SPaul Durrant {
690*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
691094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
692094a2239SPaul Durrant     struct xs_permissions perms[2];
693094a2239SPaul Durrant 
694094a2239SPaul Durrant     xendev->backend_path = xen_device_get_backend_path(xendev);
695094a2239SPaul Durrant 
696094a2239SPaul Durrant     perms[0].id = xenbus->backend_id;
697094a2239SPaul Durrant     perms[0].perms = XS_PERM_NONE;
698094a2239SPaul Durrant     perms[1].id = xendev->frontend_id;
699094a2239SPaul Durrant     perms[1].perms = XS_PERM_READ;
700094a2239SPaul Durrant 
701094a2239SPaul Durrant     g_assert(xenbus->xsh);
702094a2239SPaul Durrant 
703094a2239SPaul Durrant     xs_node_create(xenbus->xsh, XBT_NULL, xendev->backend_path, perms,
704*1de7096dSVladimir Sementsov-Ogievskiy                    ARRAY_SIZE(perms), errp);
705*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
706*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to create backend: ");
707b6af8926SPaul Durrant         return;
708b6af8926SPaul Durrant     }
709b6af8926SPaul Durrant 
710b6af8926SPaul Durrant     xendev->backend_state_watch =
711d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->backend_path,
712b6af8926SPaul Durrant                              "state", xen_device_backend_changed,
713*1de7096dSVladimir Sementsov-Ogievskiy                              errp);
714*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
715*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch backend state: ");
716b6af8926SPaul Durrant         return;
717b6af8926SPaul Durrant     }
718b6af8926SPaul Durrant 
719b6af8926SPaul Durrant     xendev->backend_online_watch =
720d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->backend_path,
721b6af8926SPaul Durrant                              "online", xen_device_backend_changed,
722*1de7096dSVladimir Sementsov-Ogievskiy                              errp);
723*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
724*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch backend online: ");
725b6af8926SPaul Durrant         return;
726094a2239SPaul Durrant     }
727094a2239SPaul Durrant }
728094a2239SPaul Durrant 
729094a2239SPaul Durrant static void xen_device_backend_destroy(XenDevice *xendev)
730094a2239SPaul Durrant {
731094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
732094a2239SPaul Durrant     Error *local_err = NULL;
733094a2239SPaul Durrant 
734b6af8926SPaul Durrant     if (xendev->backend_online_watch) {
735d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->backend_online_watch, NULL);
736b6af8926SPaul Durrant         xendev->backend_online_watch = NULL;
737b6af8926SPaul Durrant     }
738b6af8926SPaul Durrant 
739b6af8926SPaul Durrant     if (xendev->backend_state_watch) {
740d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->backend_state_watch, NULL);
741b6af8926SPaul Durrant         xendev->backend_state_watch = NULL;
742b6af8926SPaul Durrant     }
743b6af8926SPaul Durrant 
744094a2239SPaul Durrant     if (!xendev->backend_path) {
745094a2239SPaul Durrant         return;
746094a2239SPaul Durrant     }
747094a2239SPaul Durrant 
748094a2239SPaul Durrant     g_assert(xenbus->xsh);
749094a2239SPaul Durrant 
750094a2239SPaul Durrant     xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->backend_path,
751094a2239SPaul Durrant                     &local_err);
752094a2239SPaul Durrant     g_free(xendev->backend_path);
753094a2239SPaul Durrant     xendev->backend_path = NULL;
754094a2239SPaul Durrant 
755094a2239SPaul Durrant     if (local_err) {
756094a2239SPaul Durrant         error_report_err(local_err);
757094a2239SPaul Durrant     }
758094a2239SPaul Durrant }
759094a2239SPaul Durrant 
760b6af8926SPaul Durrant void xen_device_frontend_printf(XenDevice *xendev, const char *key,
761094a2239SPaul Durrant                                 const char *fmt, ...)
762094a2239SPaul Durrant {
763094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
764094a2239SPaul Durrant     Error *local_err = NULL;
765094a2239SPaul Durrant     va_list ap;
766094a2239SPaul Durrant 
767094a2239SPaul Durrant     g_assert(xenbus->xsh);
768094a2239SPaul Durrant 
769094a2239SPaul Durrant     va_start(ap, fmt);
770094a2239SPaul Durrant     xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
771094a2239SPaul Durrant                     &local_err, fmt, ap);
772094a2239SPaul Durrant     va_end(ap);
773094a2239SPaul Durrant 
774094a2239SPaul Durrant     if (local_err) {
775094a2239SPaul Durrant         error_report_err(local_err);
776094a2239SPaul Durrant     }
777094a2239SPaul Durrant }
778094a2239SPaul Durrant 
779b6af8926SPaul Durrant int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
78082a29e30SPaul Durrant                               const char *fmt, ...)
78182a29e30SPaul Durrant {
78282a29e30SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
78382a29e30SPaul Durrant     va_list ap;
78482a29e30SPaul Durrant     int rc;
78582a29e30SPaul Durrant 
78682a29e30SPaul Durrant     g_assert(xenbus->xsh);
78782a29e30SPaul Durrant 
78882a29e30SPaul Durrant     va_start(ap, fmt);
78982a29e30SPaul Durrant     rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
79082a29e30SPaul Durrant                         NULL, fmt, ap);
79182a29e30SPaul Durrant     va_end(ap);
79282a29e30SPaul Durrant 
79382a29e30SPaul Durrant     return rc;
79482a29e30SPaul Durrant }
79582a29e30SPaul Durrant 
796094a2239SPaul Durrant static void xen_device_frontend_set_state(XenDevice *xendev,
797705be570SAnthony PERARD                                           enum xenbus_state state,
798705be570SAnthony PERARD                                           bool publish)
799094a2239SPaul Durrant {
800094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
801094a2239SPaul Durrant 
802094a2239SPaul Durrant     if (xendev->frontend_state == state) {
803094a2239SPaul Durrant         return;
804094a2239SPaul Durrant     }
805094a2239SPaul Durrant 
806094a2239SPaul Durrant     trace_xen_device_frontend_state(type, xendev->name,
807094a2239SPaul Durrant                                     xs_strstate(state));
808094a2239SPaul Durrant 
809094a2239SPaul Durrant     xendev->frontend_state = state;
810705be570SAnthony PERARD     if (publish) {
811094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "state", "%u", state);
812094a2239SPaul Durrant     }
813705be570SAnthony PERARD }
814094a2239SPaul Durrant 
81582a29e30SPaul Durrant static void xen_device_frontend_changed(void *opaque)
81682a29e30SPaul Durrant {
81782a29e30SPaul Durrant     XenDevice *xendev = opaque;
81882a29e30SPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
81982a29e30SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
82082a29e30SPaul Durrant     enum xenbus_state state;
82182a29e30SPaul Durrant 
82282a29e30SPaul Durrant     trace_xen_device_frontend_changed(type, xendev->name);
82382a29e30SPaul Durrant 
82482a29e30SPaul Durrant     if (xen_device_frontend_scanf(xendev, "state", "%u", &state) != 1) {
82582a29e30SPaul Durrant         state = XenbusStateUnknown;
82682a29e30SPaul Durrant     }
82782a29e30SPaul Durrant 
828705be570SAnthony PERARD     xen_device_frontend_set_state(xendev, state, false);
82982a29e30SPaul Durrant 
83067bc8e00SPaul Durrant     if (state == XenbusStateInitialising &&
83167bc8e00SPaul Durrant         xendev->backend_state == XenbusStateClosed &&
83267bc8e00SPaul Durrant         xendev->backend_online) {
83367bc8e00SPaul Durrant         /*
83467bc8e00SPaul Durrant          * The frontend is re-initializing so switch back to
83567bc8e00SPaul Durrant          * InitWait.
83667bc8e00SPaul Durrant          */
83767bc8e00SPaul Durrant         xen_device_backend_set_state(xendev, XenbusStateInitWait);
83867bc8e00SPaul Durrant         return;
83967bc8e00SPaul Durrant     }
84067bc8e00SPaul Durrant 
84182a29e30SPaul Durrant     if (xendev_class->frontend_changed) {
84282a29e30SPaul Durrant         Error *local_err = NULL;
84382a29e30SPaul Durrant 
84482a29e30SPaul Durrant         xendev_class->frontend_changed(xendev, state, &local_err);
84582a29e30SPaul Durrant 
84682a29e30SPaul Durrant         if (local_err) {
84782a29e30SPaul Durrant             error_reportf_err(local_err, "frontend change error: ");
84882a29e30SPaul Durrant         }
84982a29e30SPaul Durrant     }
85082a29e30SPaul Durrant }
85182a29e30SPaul Durrant 
8526bd6b955SMark Syms static bool xen_device_frontend_exists(XenDevice *xendev)
8536bd6b955SMark Syms {
8546bd6b955SMark Syms     enum xenbus_state state;
8556bd6b955SMark Syms 
8566bd6b955SMark Syms     return (xen_device_frontend_scanf(xendev, "state", "%u", &state) == 1);
8576bd6b955SMark Syms }
8586bd6b955SMark Syms 
859094a2239SPaul Durrant static void xen_device_frontend_create(XenDevice *xendev, Error **errp)
860094a2239SPaul Durrant {
861*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
862094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
863094a2239SPaul Durrant     struct xs_permissions perms[2];
864094a2239SPaul Durrant 
865094a2239SPaul Durrant     xendev->frontend_path = xen_device_get_frontend_path(xendev);
866094a2239SPaul Durrant 
8676bd6b955SMark Syms     /*
8686bd6b955SMark Syms      * The frontend area may have already been created by a legacy
8696bd6b955SMark Syms      * toolstack.
8706bd6b955SMark Syms      */
8716bd6b955SMark Syms     if (!xen_device_frontend_exists(xendev)) {
872094a2239SPaul Durrant         perms[0].id = xendev->frontend_id;
873094a2239SPaul Durrant         perms[0].perms = XS_PERM_NONE;
874094a2239SPaul Durrant         perms[1].id = xenbus->backend_id;
875094a2239SPaul Durrant         perms[1].perms = XS_PERM_READ | XS_PERM_WRITE;
876094a2239SPaul Durrant 
877094a2239SPaul Durrant         g_assert(xenbus->xsh);
878094a2239SPaul Durrant 
879094a2239SPaul Durrant         xs_node_create(xenbus->xsh, XBT_NULL, xendev->frontend_path, perms,
880*1de7096dSVladimir Sementsov-Ogievskiy                        ARRAY_SIZE(perms), errp);
881*1de7096dSVladimir Sementsov-Ogievskiy         if (*errp) {
882*1de7096dSVladimir Sementsov-Ogievskiy             error_prepend(errp, "failed to create frontend: ");
88382a29e30SPaul Durrant             return;
88482a29e30SPaul Durrant         }
8856bd6b955SMark Syms     }
88682a29e30SPaul Durrant 
88782a29e30SPaul Durrant     xendev->frontend_state_watch =
888d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->frontend_path, "state",
889*1de7096dSVladimir Sementsov-Ogievskiy                              xen_device_frontend_changed, errp);
890*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
891*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch frontend state: ");
892094a2239SPaul Durrant     }
893094a2239SPaul Durrant }
894094a2239SPaul Durrant 
895094a2239SPaul Durrant static void xen_device_frontend_destroy(XenDevice *xendev)
896094a2239SPaul Durrant {
897094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
898094a2239SPaul Durrant     Error *local_err = NULL;
899094a2239SPaul Durrant 
90082a29e30SPaul Durrant     if (xendev->frontend_state_watch) {
901d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->frontend_state_watch,
902d198b711SPaul Durrant                                 NULL);
90382a29e30SPaul Durrant         xendev->frontend_state_watch = NULL;
90482a29e30SPaul Durrant     }
90582a29e30SPaul Durrant 
906094a2239SPaul Durrant     if (!xendev->frontend_path) {
907094a2239SPaul Durrant         return;
908094a2239SPaul Durrant     }
909094a2239SPaul Durrant 
910094a2239SPaul Durrant     g_assert(xenbus->xsh);
911094a2239SPaul Durrant 
912094a2239SPaul Durrant     xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->frontend_path,
913094a2239SPaul Durrant                     &local_err);
914094a2239SPaul Durrant     g_free(xendev->frontend_path);
915094a2239SPaul Durrant     xendev->frontend_path = NULL;
916094a2239SPaul Durrant 
917094a2239SPaul Durrant     if (local_err) {
918094a2239SPaul Durrant         error_report_err(local_err);
919094a2239SPaul Durrant     }
920094a2239SPaul Durrant }
921094a2239SPaul Durrant 
9224b34b5b1SPaul Durrant void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
9234b34b5b1SPaul Durrant                                    Error **errp)
9244b34b5b1SPaul Durrant {
9254b34b5b1SPaul Durrant     if (xengnttab_set_max_grants(xendev->xgth, nr_refs)) {
9264b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_set_max_grants failed");
9274b34b5b1SPaul Durrant     }
9284b34b5b1SPaul Durrant }
9294b34b5b1SPaul Durrant 
9304b34b5b1SPaul Durrant void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs,
9314b34b5b1SPaul Durrant                                 unsigned int nr_refs, int prot,
9324b34b5b1SPaul Durrant                                 Error **errp)
9334b34b5b1SPaul Durrant {
9344b34b5b1SPaul Durrant     void *map = xengnttab_map_domain_grant_refs(xendev->xgth, nr_refs,
9354b34b5b1SPaul Durrant                                                 xendev->frontend_id, refs,
9364b34b5b1SPaul Durrant                                                 prot);
9374b34b5b1SPaul Durrant 
9384b34b5b1SPaul Durrant     if (!map) {
9394b34b5b1SPaul Durrant         error_setg_errno(errp, errno,
9404b34b5b1SPaul Durrant                          "xengnttab_map_domain_grant_refs failed");
9414b34b5b1SPaul Durrant     }
9424b34b5b1SPaul Durrant 
9434b34b5b1SPaul Durrant     return map;
9444b34b5b1SPaul Durrant }
9454b34b5b1SPaul Durrant 
9464b34b5b1SPaul Durrant void xen_device_unmap_grant_refs(XenDevice *xendev, void *map,
9474b34b5b1SPaul Durrant                                  unsigned int nr_refs, Error **errp)
9484b34b5b1SPaul Durrant {
9494b34b5b1SPaul Durrant     if (xengnttab_unmap(xendev->xgth, map, nr_refs)) {
9504b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_unmap failed");
9514b34b5b1SPaul Durrant     }
9524b34b5b1SPaul Durrant }
9534b34b5b1SPaul Durrant 
9544b34b5b1SPaul Durrant static void compat_copy_grant_refs(XenDevice *xendev, bool to_domain,
9554b34b5b1SPaul Durrant                                    XenDeviceGrantCopySegment segs[],
9564b34b5b1SPaul Durrant                                    unsigned int nr_segs, Error **errp)
9574b34b5b1SPaul Durrant {
9584b34b5b1SPaul Durrant     uint32_t *refs = g_new(uint32_t, nr_segs);
9594b34b5b1SPaul Durrant     int prot = to_domain ? PROT_WRITE : PROT_READ;
9604b34b5b1SPaul Durrant     void *map;
9614b34b5b1SPaul Durrant     unsigned int i;
9624b34b5b1SPaul Durrant 
9634b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
9644b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
9654b34b5b1SPaul Durrant 
9664b34b5b1SPaul Durrant         refs[i] = to_domain ? seg->dest.foreign.ref :
9674b34b5b1SPaul Durrant             seg->source.foreign.ref;
9684b34b5b1SPaul Durrant     }
9694b34b5b1SPaul Durrant 
9704b34b5b1SPaul Durrant     map = xengnttab_map_domain_grant_refs(xendev->xgth, nr_segs,
9714b34b5b1SPaul Durrant                                           xendev->frontend_id, refs,
9724b34b5b1SPaul Durrant                                           prot);
9734b34b5b1SPaul Durrant     if (!map) {
9744b34b5b1SPaul Durrant         error_setg_errno(errp, errno,
9754b34b5b1SPaul Durrant                          "xengnttab_map_domain_grant_refs failed");
9764b34b5b1SPaul Durrant         goto done;
9774b34b5b1SPaul Durrant     }
9784b34b5b1SPaul Durrant 
9794b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
9804b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
9814b34b5b1SPaul Durrant         void *page = map + (i * XC_PAGE_SIZE);
9824b34b5b1SPaul Durrant 
9834b34b5b1SPaul Durrant         if (to_domain) {
9844b34b5b1SPaul Durrant             memcpy(page + seg->dest.foreign.offset, seg->source.virt,
9854b34b5b1SPaul Durrant                    seg->len);
9864b34b5b1SPaul Durrant         } else {
9874b34b5b1SPaul Durrant             memcpy(seg->dest.virt, page + seg->source.foreign.offset,
9884b34b5b1SPaul Durrant                    seg->len);
9894b34b5b1SPaul Durrant         }
9904b34b5b1SPaul Durrant     }
9914b34b5b1SPaul Durrant 
9924b34b5b1SPaul Durrant     if (xengnttab_unmap(xendev->xgth, map, nr_segs)) {
9934b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_unmap failed");
9944b34b5b1SPaul Durrant     }
9954b34b5b1SPaul Durrant 
9964b34b5b1SPaul Durrant done:
9974b34b5b1SPaul Durrant     g_free(refs);
9984b34b5b1SPaul Durrant }
9994b34b5b1SPaul Durrant 
10004b34b5b1SPaul Durrant void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain,
10014b34b5b1SPaul Durrant                                 XenDeviceGrantCopySegment segs[],
10024b34b5b1SPaul Durrant                                 unsigned int nr_segs, Error **errp)
10034b34b5b1SPaul Durrant {
10044b34b5b1SPaul Durrant     xengnttab_grant_copy_segment_t *xengnttab_segs;
10054b34b5b1SPaul Durrant     unsigned int i;
10064b34b5b1SPaul Durrant 
10074b34b5b1SPaul Durrant     if (!xendev->feature_grant_copy) {
10084b34b5b1SPaul Durrant         compat_copy_grant_refs(xendev, to_domain, segs, nr_segs, errp);
10094b34b5b1SPaul Durrant         return;
10104b34b5b1SPaul Durrant     }
10114b34b5b1SPaul Durrant 
10124b34b5b1SPaul Durrant     xengnttab_segs = g_new0(xengnttab_grant_copy_segment_t, nr_segs);
10134b34b5b1SPaul Durrant 
10144b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
10154b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
10164b34b5b1SPaul Durrant         xengnttab_grant_copy_segment_t *xengnttab_seg = &xengnttab_segs[i];
10174b34b5b1SPaul Durrant 
10184b34b5b1SPaul Durrant         if (to_domain) {
10194b34b5b1SPaul Durrant             xengnttab_seg->flags = GNTCOPY_dest_gref;
10204b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.domid = xendev->frontend_id;
10214b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.ref = seg->dest.foreign.ref;
10224b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.offset = seg->dest.foreign.offset;
10234b34b5b1SPaul Durrant             xengnttab_seg->source.virt = seg->source.virt;
10244b34b5b1SPaul Durrant         } else {
10254b34b5b1SPaul Durrant             xengnttab_seg->flags = GNTCOPY_source_gref;
10264b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.domid = xendev->frontend_id;
10274b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.ref = seg->source.foreign.ref;
10284b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.offset =
10294b34b5b1SPaul Durrant                 seg->source.foreign.offset;
10304b34b5b1SPaul Durrant             xengnttab_seg->dest.virt = seg->dest.virt;
10314b34b5b1SPaul Durrant         }
10324b34b5b1SPaul Durrant 
10334b34b5b1SPaul Durrant         xengnttab_seg->len = seg->len;
10344b34b5b1SPaul Durrant     }
10354b34b5b1SPaul Durrant 
10364b34b5b1SPaul Durrant     if (xengnttab_grant_copy(xendev->xgth, nr_segs, xengnttab_segs)) {
10374b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_grant_copy failed");
10384b34b5b1SPaul Durrant         goto done;
10394b34b5b1SPaul Durrant     }
10404b34b5b1SPaul Durrant 
10414b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
10424b34b5b1SPaul Durrant         xengnttab_grant_copy_segment_t *xengnttab_seg = &xengnttab_segs[i];
10434b34b5b1SPaul Durrant 
10444b34b5b1SPaul Durrant         if (xengnttab_seg->status != GNTST_okay) {
10454b34b5b1SPaul Durrant             error_setg(errp, "xengnttab_grant_copy seg[%u] failed", i);
10464b34b5b1SPaul Durrant             break;
10474b34b5b1SPaul Durrant         }
10484b34b5b1SPaul Durrant     }
10494b34b5b1SPaul Durrant 
10504b34b5b1SPaul Durrant done:
10514b34b5b1SPaul Durrant     g_free(xengnttab_segs);
10524b34b5b1SPaul Durrant }
10534b34b5b1SPaul Durrant 
1054a3d669c8SPaul Durrant struct XenEventChannel {
1055c0b336eaSPaul Durrant     QLIST_ENTRY(XenEventChannel) list;
105683361a8aSPaul Durrant     AioContext *ctx;
1057c0b336eaSPaul Durrant     xenevtchn_handle *xeh;
1058a3d669c8SPaul Durrant     evtchn_port_t local_port;
1059a3d669c8SPaul Durrant     XenEventHandler handler;
1060a3d669c8SPaul Durrant     void *opaque;
1061a3d669c8SPaul Durrant };
1062a3d669c8SPaul Durrant 
1063345f42b4SPaul Durrant static bool xen_device_poll(void *opaque)
1064345f42b4SPaul Durrant {
1065345f42b4SPaul Durrant     XenEventChannel *channel = opaque;
1066345f42b4SPaul Durrant 
1067345f42b4SPaul Durrant     return channel->handler(channel->opaque);
1068345f42b4SPaul Durrant }
1069345f42b4SPaul Durrant 
1070c0b336eaSPaul Durrant static void xen_device_event(void *opaque)
1071a3d669c8SPaul Durrant {
1072c0b336eaSPaul Durrant     XenEventChannel *channel = opaque;
1073c0b336eaSPaul Durrant     unsigned long port = xenevtchn_pending(channel->xeh);
1074a3d669c8SPaul Durrant 
1075a3d669c8SPaul Durrant     if (port == channel->local_port) {
1076345f42b4SPaul Durrant         xen_device_poll(channel);
1077c0b336eaSPaul Durrant 
1078c0b336eaSPaul Durrant         xenevtchn_unmask(channel->xeh, port);
1079a3d669c8SPaul Durrant     }
1080a3d669c8SPaul Durrant }
1081a3d669c8SPaul Durrant 
108232d0b7beSPaul Durrant void xen_device_set_event_channel_context(XenDevice *xendev,
108332d0b7beSPaul Durrant                                           XenEventChannel *channel,
108483361a8aSPaul Durrant                                           AioContext *ctx,
108532d0b7beSPaul Durrant                                           Error **errp)
108632d0b7beSPaul Durrant {
108732d0b7beSPaul Durrant     if (!channel) {
108832d0b7beSPaul Durrant         error_setg(errp, "bad channel");
108932d0b7beSPaul Durrant         return;
109032d0b7beSPaul Durrant     }
109132d0b7beSPaul Durrant 
109232d0b7beSPaul Durrant     if (channel->ctx)
109332d0b7beSPaul Durrant         aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
109432d0b7beSPaul Durrant                            NULL, NULL, NULL, NULL);
109532d0b7beSPaul Durrant 
109632d0b7beSPaul Durrant     channel->ctx = ctx;
109732d0b7beSPaul Durrant     aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
109832d0b7beSPaul Durrant                        xen_device_event, NULL, xen_device_poll, channel);
109932d0b7beSPaul Durrant }
110032d0b7beSPaul Durrant 
110132d0b7beSPaul Durrant XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev,
1102a3d669c8SPaul Durrant                                                unsigned int port,
1103a3d669c8SPaul Durrant                                                XenEventHandler handler,
1104a3d669c8SPaul Durrant                                                void *opaque, Error **errp)
1105a3d669c8SPaul Durrant {
1106a3d669c8SPaul Durrant     XenEventChannel *channel = g_new0(XenEventChannel, 1);
1107a3d669c8SPaul Durrant     xenevtchn_port_or_error_t local_port;
1108a3d669c8SPaul Durrant 
1109c0b336eaSPaul Durrant     channel->xeh = xenevtchn_open(NULL, 0);
1110c0b336eaSPaul Durrant     if (!channel->xeh) {
1111c0b336eaSPaul Durrant         error_setg_errno(errp, errno, "failed xenevtchn_open");
1112c0b336eaSPaul Durrant         goto fail;
1113c0b336eaSPaul Durrant     }
1114c0b336eaSPaul Durrant 
1115c0b336eaSPaul Durrant     local_port = xenevtchn_bind_interdomain(channel->xeh,
1116a3d669c8SPaul Durrant                                             xendev->frontend_id,
1117a3d669c8SPaul Durrant                                             port);
1118a3d669c8SPaul Durrant     if (local_port < 0) {
1119a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_bind_interdomain failed");
1120c0b336eaSPaul Durrant         goto fail;
1121a3d669c8SPaul Durrant     }
1122a3d669c8SPaul Durrant 
1123a3d669c8SPaul Durrant     channel->local_port = local_port;
1124a3d669c8SPaul Durrant     channel->handler = handler;
1125a3d669c8SPaul Durrant     channel->opaque = opaque;
1126a3d669c8SPaul Durrant 
112732d0b7beSPaul Durrant     /* Only reason for failure is a NULL channel */
112832d0b7beSPaul Durrant     xen_device_set_event_channel_context(xendev, channel,
112932d0b7beSPaul Durrant                                          qemu_get_aio_context(),
113032d0b7beSPaul Durrant                                          &error_abort);
1131c0b336eaSPaul Durrant 
1132c0b336eaSPaul Durrant     QLIST_INSERT_HEAD(&xendev->event_channels, channel, list);
1133a3d669c8SPaul Durrant 
1134a3d669c8SPaul Durrant     return channel;
1135c0b336eaSPaul Durrant 
1136c0b336eaSPaul Durrant fail:
1137c0b336eaSPaul Durrant     if (channel->xeh) {
1138c0b336eaSPaul Durrant         xenevtchn_close(channel->xeh);
1139c0b336eaSPaul Durrant     }
1140c0b336eaSPaul Durrant 
1141c0b336eaSPaul Durrant     g_free(channel);
1142c0b336eaSPaul Durrant 
1143c0b336eaSPaul Durrant     return NULL;
1144a3d669c8SPaul Durrant }
1145a3d669c8SPaul Durrant 
1146a3d669c8SPaul Durrant void xen_device_notify_event_channel(XenDevice *xendev,
1147a3d669c8SPaul Durrant                                      XenEventChannel *channel,
1148a3d669c8SPaul Durrant                                      Error **errp)
1149a3d669c8SPaul Durrant {
1150a3d669c8SPaul Durrant     if (!channel) {
1151a3d669c8SPaul Durrant         error_setg(errp, "bad channel");
1152a3d669c8SPaul Durrant         return;
1153a3d669c8SPaul Durrant     }
1154a3d669c8SPaul Durrant 
1155c0b336eaSPaul Durrant     if (xenevtchn_notify(channel->xeh, channel->local_port) < 0) {
1156a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_notify failed");
1157a3d669c8SPaul Durrant     }
1158a3d669c8SPaul Durrant }
1159a3d669c8SPaul Durrant 
1160a3d669c8SPaul Durrant void xen_device_unbind_event_channel(XenDevice *xendev,
1161a3d669c8SPaul Durrant                                      XenEventChannel *channel,
1162a3d669c8SPaul Durrant                                      Error **errp)
1163a3d669c8SPaul Durrant {
1164a3d669c8SPaul Durrant     if (!channel) {
1165a3d669c8SPaul Durrant         error_setg(errp, "bad channel");
1166a3d669c8SPaul Durrant         return;
1167a3d669c8SPaul Durrant     }
1168a3d669c8SPaul Durrant 
1169c0b336eaSPaul Durrant     QLIST_REMOVE(channel, list);
1170a3d669c8SPaul Durrant 
117183361a8aSPaul Durrant     aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
117283361a8aSPaul Durrant                        NULL, NULL, NULL, NULL);
1173c0b336eaSPaul Durrant 
1174c0b336eaSPaul Durrant     if (xenevtchn_unbind(channel->xeh, channel->local_port) < 0) {
1175a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_unbind failed");
1176a3d669c8SPaul Durrant     }
1177a3d669c8SPaul Durrant 
1178c0b336eaSPaul Durrant     xenevtchn_close(channel->xeh);
1179a3d669c8SPaul Durrant     g_free(channel);
1180a3d669c8SPaul Durrant }
1181a3d669c8SPaul Durrant 
1182b69c3c21SMarkus Armbruster static void xen_device_unrealize(DeviceState *dev)
1183108f7bbaSPaul Durrant {
1184108f7bbaSPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
1185108f7bbaSPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
1186108f7bbaSPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
1187c0b336eaSPaul Durrant     XenEventChannel *channel, *next;
1188108f7bbaSPaul Durrant 
1189094a2239SPaul Durrant     if (!xendev->name) {
1190094a2239SPaul Durrant         return;
1191094a2239SPaul Durrant     }
1192094a2239SPaul Durrant 
1193094a2239SPaul Durrant     trace_xen_device_unrealize(type, xendev->name);
1194094a2239SPaul Durrant 
1195094a2239SPaul Durrant     if (xendev->exit.notify) {
1196094a2239SPaul Durrant         qemu_remove_exit_notifier(&xendev->exit);
1197094a2239SPaul Durrant         xendev->exit.notify = NULL;
1198094a2239SPaul Durrant     }
1199108f7bbaSPaul Durrant 
1200108f7bbaSPaul Durrant     if (xendev_class->unrealize) {
1201b69c3c21SMarkus Armbruster         xendev_class->unrealize(xendev);
1202108f7bbaSPaul Durrant     }
1203094a2239SPaul Durrant 
1204c0b336eaSPaul Durrant     /* Make sure all event channels are cleaned up */
1205c0b336eaSPaul Durrant     QLIST_FOREACH_SAFE(channel, &xendev->event_channels, list, next) {
1206c0b336eaSPaul Durrant         xen_device_unbind_event_channel(xendev, channel, NULL);
1207c0b336eaSPaul Durrant     }
1208c0b336eaSPaul Durrant 
1209094a2239SPaul Durrant     xen_device_frontend_destroy(xendev);
1210094a2239SPaul Durrant     xen_device_backend_destroy(xendev);
1211094a2239SPaul Durrant 
12124b34b5b1SPaul Durrant     if (xendev->xgth) {
12134b34b5b1SPaul Durrant         xengnttab_close(xendev->xgth);
12144b34b5b1SPaul Durrant         xendev->xgth = NULL;
12154b34b5b1SPaul Durrant     }
12164b34b5b1SPaul Durrant 
1217d198b711SPaul Durrant     if (xendev->watch_list) {
1218d198b711SPaul Durrant         watch_list_destroy(xendev->watch_list);
1219d198b711SPaul Durrant         xendev->watch_list = NULL;
1220d198b711SPaul Durrant     }
1221d198b711SPaul Durrant 
1222d198b711SPaul Durrant     if (xendev->xsh) {
1223d198b711SPaul Durrant         xs_close(xendev->xsh);
1224d198b711SPaul Durrant         xendev->xsh = NULL;
1225d198b711SPaul Durrant     }
1226d198b711SPaul Durrant 
1227094a2239SPaul Durrant     g_free(xendev->name);
1228094a2239SPaul Durrant     xendev->name = NULL;
1229094a2239SPaul Durrant }
1230094a2239SPaul Durrant 
1231094a2239SPaul Durrant static void xen_device_exit(Notifier *n, void *data)
1232094a2239SPaul Durrant {
1233094a2239SPaul Durrant     XenDevice *xendev = container_of(n, XenDevice, exit);
1234094a2239SPaul Durrant 
1235b69c3c21SMarkus Armbruster     xen_device_unrealize(DEVICE(xendev));
1236108f7bbaSPaul Durrant }
1237108f7bbaSPaul Durrant 
1238108f7bbaSPaul Durrant static void xen_device_realize(DeviceState *dev, Error **errp)
1239108f7bbaSPaul Durrant {
1240*1de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
1241108f7bbaSPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
1242108f7bbaSPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
1243094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
1244108f7bbaSPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
1245108f7bbaSPaul Durrant 
1246094a2239SPaul Durrant     if (xendev->frontend_id == DOMID_INVALID) {
1247094a2239SPaul Durrant         xendev->frontend_id = xen_domid;
1248094a2239SPaul Durrant     }
1249094a2239SPaul Durrant 
1250094a2239SPaul Durrant     if (xendev->frontend_id >= DOMID_FIRST_RESERVED) {
1251094a2239SPaul Durrant         error_setg(errp, "invalid frontend-id");
1252094a2239SPaul Durrant         goto unrealize;
1253094a2239SPaul Durrant     }
1254094a2239SPaul Durrant 
1255094a2239SPaul Durrant     if (!xendev_class->get_name) {
1256094a2239SPaul Durrant         error_setg(errp, "get_name method not implemented");
1257094a2239SPaul Durrant         goto unrealize;
1258094a2239SPaul Durrant     }
1259094a2239SPaul Durrant 
1260*1de7096dSVladimir Sementsov-Ogievskiy     xendev->name = xendev_class->get_name(xendev, errp);
1261*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
1262*1de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to get device name: ");
1263094a2239SPaul Durrant         goto unrealize;
1264094a2239SPaul Durrant     }
1265094a2239SPaul Durrant 
1266094a2239SPaul Durrant     trace_xen_device_realize(type, xendev->name);
1267094a2239SPaul Durrant 
1268d198b711SPaul Durrant     xendev->xsh = xs_open(0);
1269d198b711SPaul Durrant     if (!xendev->xsh) {
1270d198b711SPaul Durrant         error_setg_errno(errp, errno, "failed xs_open");
1271d198b711SPaul Durrant         goto unrealize;
1272d198b711SPaul Durrant     }
1273d198b711SPaul Durrant 
1274d198b711SPaul Durrant     xendev->watch_list = watch_list_create(xendev->xsh);
1275d198b711SPaul Durrant 
12764b34b5b1SPaul Durrant     xendev->xgth = xengnttab_open(NULL, 0);
12774b34b5b1SPaul Durrant     if (!xendev->xgth) {
12784b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "failed xengnttab_open");
12794b34b5b1SPaul Durrant         goto unrealize;
12804b34b5b1SPaul Durrant     }
12814b34b5b1SPaul Durrant 
12824b34b5b1SPaul Durrant     xendev->feature_grant_copy =
12834b34b5b1SPaul Durrant         (xengnttab_grant_copy(xendev->xgth, 0, NULL) == 0);
12844b34b5b1SPaul Durrant 
1285*1de7096dSVladimir Sementsov-Ogievskiy     xen_device_backend_create(xendev, errp);
1286*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
1287094a2239SPaul Durrant         goto unrealize;
1288094a2239SPaul Durrant     }
1289094a2239SPaul Durrant 
1290*1de7096dSVladimir Sementsov-Ogievskiy     xen_device_frontend_create(xendev, errp);
1291*1de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
1292094a2239SPaul Durrant         goto unrealize;
1293094a2239SPaul Durrant     }
1294108f7bbaSPaul Durrant 
1295108f7bbaSPaul Durrant     if (xendev_class->realize) {
1296*1de7096dSVladimir Sementsov-Ogievskiy         xendev_class->realize(xendev, errp);
1297*1de7096dSVladimir Sementsov-Ogievskiy         if (*errp) {
1298108f7bbaSPaul Durrant             goto unrealize;
1299108f7bbaSPaul Durrant         }
1300108f7bbaSPaul Durrant     }
1301108f7bbaSPaul Durrant 
1302094a2239SPaul Durrant     xen_device_backend_printf(xendev, "frontend", "%s",
1303094a2239SPaul Durrant                               xendev->frontend_path);
1304094a2239SPaul Durrant     xen_device_backend_printf(xendev, "frontend-id", "%u",
1305094a2239SPaul Durrant                               xendev->frontend_id);
1306094a2239SPaul Durrant     xen_device_backend_printf(xendev, "hotplug-status", "connected");
1307094a2239SPaul Durrant 
1308b6af8926SPaul Durrant     xen_device_backend_set_online(xendev, true);
1309094a2239SPaul Durrant     xen_device_backend_set_state(xendev, XenbusStateInitWait);
1310094a2239SPaul Durrant 
13116bd6b955SMark Syms     if (!xen_device_frontend_exists(xendev)) {
1312094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "backend", "%s",
1313094a2239SPaul Durrant                                    xendev->backend_path);
1314094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "backend-id", "%u",
1315094a2239SPaul Durrant                                    xenbus->backend_id);
1316094a2239SPaul Durrant 
1317705be570SAnthony PERARD         xen_device_frontend_set_state(xendev, XenbusStateInitialising, true);
13186bd6b955SMark Syms     }
1319094a2239SPaul Durrant 
1320094a2239SPaul Durrant     xendev->exit.notify = xen_device_exit;
1321094a2239SPaul Durrant     qemu_add_exit_notifier(&xendev->exit);
1322108f7bbaSPaul Durrant     return;
1323108f7bbaSPaul Durrant 
1324108f7bbaSPaul Durrant unrealize:
1325b69c3c21SMarkus Armbruster     xen_device_unrealize(dev);
1326108f7bbaSPaul Durrant }
1327108f7bbaSPaul Durrant 
1328094a2239SPaul Durrant static Property xen_device_props[] = {
1329094a2239SPaul Durrant     DEFINE_PROP_UINT16("frontend-id", XenDevice, frontend_id,
1330094a2239SPaul Durrant                        DOMID_INVALID),
1331094a2239SPaul Durrant     DEFINE_PROP_END_OF_LIST()
1332094a2239SPaul Durrant };
1333094a2239SPaul Durrant 
1334108f7bbaSPaul Durrant static void xen_device_class_init(ObjectClass *class, void *data)
1335108f7bbaSPaul Durrant {
1336108f7bbaSPaul Durrant     DeviceClass *dev_class = DEVICE_CLASS(class);
1337108f7bbaSPaul Durrant 
1338108f7bbaSPaul Durrant     dev_class->realize = xen_device_realize;
1339108f7bbaSPaul Durrant     dev_class->unrealize = xen_device_unrealize;
13404f67d30bSMarc-André Lureau     device_class_set_props(dev_class, xen_device_props);
1341108f7bbaSPaul Durrant     dev_class->bus_type = TYPE_XEN_BUS;
1342108f7bbaSPaul Durrant }
1343108f7bbaSPaul Durrant 
1344108f7bbaSPaul Durrant static const TypeInfo xen_device_type_info = {
1345108f7bbaSPaul Durrant     .name = TYPE_XEN_DEVICE,
1346108f7bbaSPaul Durrant     .parent = TYPE_DEVICE,
1347108f7bbaSPaul Durrant     .instance_size = sizeof(XenDevice),
1348108f7bbaSPaul Durrant     .abstract = true,
1349108f7bbaSPaul Durrant     .class_size = sizeof(XenDeviceClass),
1350108f7bbaSPaul Durrant     .class_init = xen_device_class_init,
1351108f7bbaSPaul Durrant };
1352108f7bbaSPaul Durrant 
1353108f7bbaSPaul Durrant typedef struct XenBridge {
1354108f7bbaSPaul Durrant     SysBusDevice busdev;
1355108f7bbaSPaul Durrant } XenBridge;
1356108f7bbaSPaul Durrant 
1357108f7bbaSPaul Durrant #define TYPE_XEN_BRIDGE "xen-bridge"
1358108f7bbaSPaul Durrant 
1359108f7bbaSPaul Durrant static const TypeInfo xen_bridge_type_info = {
1360108f7bbaSPaul Durrant     .name = TYPE_XEN_BRIDGE,
1361108f7bbaSPaul Durrant     .parent = TYPE_SYS_BUS_DEVICE,
1362108f7bbaSPaul Durrant     .instance_size = sizeof(XenBridge),
1363108f7bbaSPaul Durrant };
1364108f7bbaSPaul Durrant 
1365108f7bbaSPaul Durrant static void xen_register_types(void)
1366108f7bbaSPaul Durrant {
1367108f7bbaSPaul Durrant     type_register_static(&xen_bridge_type_info);
1368108f7bbaSPaul Durrant     type_register_static(&xen_bus_type_info);
1369108f7bbaSPaul Durrant     type_register_static(&xen_device_type_info);
1370108f7bbaSPaul Durrant }
1371108f7bbaSPaul Durrant 
1372108f7bbaSPaul Durrant type_init(xen_register_types)
1373108f7bbaSPaul Durrant 
1374108f7bbaSPaul Durrant void xen_bus_init(void)
1375108f7bbaSPaul Durrant {
13763e80f690SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_XEN_BRIDGE);
1377108f7bbaSPaul Durrant     BusState *bus = qbus_create(TYPE_XEN_BUS, dev, NULL);
1378108f7bbaSPaul Durrant 
13793c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1380cd7c8660SMarkus Armbruster     qbus_set_bus_hotplug_handler(bus);
1381108f7bbaSPaul Durrant }
1382