xref: /openbmc/qemu/hw/xen/xen-bus.c (revision c4583c8c394ee49ce7d5271f572abd3b000fa9e4)
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 {
561de7096dSVladimir 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",
721de7096dSVladimir Sementsov-Ogievskiy                    errp, "%u", 0);
731de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
74b6af8926SPaul Durrant         goto abort;
75b6af8926SPaul Durrant     }
76b6af8926SPaul Durrant 
77b6af8926SPaul Durrant     xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "state",
781de7096dSVladimir Sementsov-Ogievskiy                    errp, "%u", XenbusStateClosing);
791de7096dSVladimir 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 {
2071de7096dSVladimir 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 
2121de7096dSVladimir Sementsov-Ogievskiy     xs_node_watch(watch_list->xsh, node, key, watch->token, errp);
2131de7096dSVladimir 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 {
2551de7096dSVladimir 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 
3141de7096dSVladimir Sementsov-Ogievskiy     xen_backend_device_create(xenbus, type, name, opts, errp);
315a783f8adSPaul Durrant     qobject_unref(opts);
316a783f8adSPaul Durrant 
3171de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
3181de7096dSVladimir 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) {
433*c4583c8cSPaul Durrant         unsigned int i;
434*c4583c8cSPaul Durrant 
435*c4583c8cSPaul Durrant         for (i = 0; i < xenbus->backend_types; i++) {
436*c4583c8cSPaul Durrant             if (xenbus->backend_watch[i]) {
437*c4583c8cSPaul Durrant                 xen_bus_remove_watch(xenbus, xenbus->backend_watch[i], NULL);
438*c4583c8cSPaul Durrant             }
439*c4583c8cSPaul Durrant         }
440*c4583c8cSPaul Durrant 
441*c4583c8cSPaul Durrant         g_free(xenbus->backend_watch);
442a783f8adSPaul Durrant         xenbus->backend_watch = NULL;
443a783f8adSPaul Durrant     }
444a783f8adSPaul Durrant 
445374752a2SPaul Durrant     if (xenbus->watch_list) {
446374752a2SPaul Durrant         watch_list_destroy(xenbus->watch_list);
447374752a2SPaul Durrant         xenbus->watch_list = NULL;
448094a2239SPaul Durrant     }
449094a2239SPaul Durrant 
450374752a2SPaul Durrant     if (xenbus->xsh) {
451094a2239SPaul Durrant         xs_close(xenbus->xsh);
452108f7bbaSPaul Durrant     }
45382a29e30SPaul Durrant }
45482a29e30SPaul Durrant 
455108f7bbaSPaul Durrant static void xen_bus_realize(BusState *bus, Error **errp)
456108f7bbaSPaul Durrant {
457*c4583c8cSPaul Durrant     char *key = g_strdup_printf("%u", xen_domid);
458094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(bus);
459094a2239SPaul Durrant     unsigned int domid;
460*c4583c8cSPaul Durrant     const char **type;
461*c4583c8cSPaul Durrant     unsigned int i;
462a783f8adSPaul Durrant     Error *local_err = NULL;
463094a2239SPaul Durrant 
464108f7bbaSPaul Durrant     trace_xen_bus_realize();
465094a2239SPaul Durrant 
466094a2239SPaul Durrant     xenbus->xsh = xs_open(0);
467094a2239SPaul Durrant     if (!xenbus->xsh) {
468094a2239SPaul Durrant         error_setg_errno(errp, errno, "failed xs_open");
469094a2239SPaul Durrant         goto fail;
470094a2239SPaul Durrant     }
471094a2239SPaul Durrant 
472094a2239SPaul Durrant     if (xs_node_scanf(xenbus->xsh, XBT_NULL, "", /* domain root node */
473094a2239SPaul Durrant                       "domid", NULL, "%u", &domid) == 1) {
474094a2239SPaul Durrant         xenbus->backend_id = domid;
475094a2239SPaul Durrant     } else {
476094a2239SPaul Durrant         xenbus->backend_id = 0; /* Assume lack of node means dom0 */
477094a2239SPaul Durrant     }
478094a2239SPaul Durrant 
479374752a2SPaul Durrant     xenbus->watch_list = watch_list_create(xenbus->xsh);
480a783f8adSPaul Durrant 
481a783f8adSPaul Durrant     module_call_init(MODULE_INIT_XEN_BACKEND);
482a783f8adSPaul Durrant 
483*c4583c8cSPaul Durrant     type = xen_backend_get_types(&xenbus->backend_types);
484*c4583c8cSPaul Durrant     xenbus->backend_watch = g_new(XenWatch *, xenbus->backend_types);
485*c4583c8cSPaul Durrant 
486*c4583c8cSPaul Durrant     for (i = 0; i < xenbus->backend_types; i++) {
487*c4583c8cSPaul Durrant         char *node = g_strdup_printf("backend/%s", type[i]);
488*c4583c8cSPaul Durrant 
489*c4583c8cSPaul Durrant         xenbus->backend_watch[i] =
490*c4583c8cSPaul Durrant             xen_bus_add_watch(xenbus, node, key, xen_bus_backend_changed,
491*c4583c8cSPaul Durrant                               &local_err);
492a783f8adSPaul Durrant         if (local_err) {
493a783f8adSPaul Durrant             /* This need not be treated as a hard error so don't propagate */
494a783f8adSPaul Durrant             error_reportf_err(local_err,
495*c4583c8cSPaul Durrant                               "failed to set up '%s' enumeration watch: ",
496*c4583c8cSPaul Durrant                               type[i]);
497a783f8adSPaul Durrant         }
498a783f8adSPaul Durrant 
499*c4583c8cSPaul Durrant         g_free(node);
500*c4583c8cSPaul Durrant     }
501*c4583c8cSPaul Durrant 
502*c4583c8cSPaul Durrant     g_free(type);
503*c4583c8cSPaul Durrant     g_free(key);
504094a2239SPaul Durrant     return;
505094a2239SPaul Durrant 
506094a2239SPaul Durrant fail:
507b69c3c21SMarkus Armbruster     xen_bus_unrealize(bus);
508*c4583c8cSPaul Durrant     g_free(key);
509108f7bbaSPaul Durrant }
510108f7bbaSPaul Durrant 
511b6af8926SPaul Durrant static void xen_bus_unplug_request(HotplugHandler *hotplug,
512b6af8926SPaul Durrant                                    DeviceState *dev,
513b6af8926SPaul Durrant                                    Error **errp)
514b6af8926SPaul Durrant {
515b6af8926SPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
516b6af8926SPaul Durrant 
517b6af8926SPaul Durrant     xen_device_unplug(xendev, errp);
518b6af8926SPaul Durrant }
519b6af8926SPaul Durrant 
520108f7bbaSPaul Durrant static void xen_bus_class_init(ObjectClass *class, void *data)
521108f7bbaSPaul Durrant {
522108f7bbaSPaul Durrant     BusClass *bus_class = BUS_CLASS(class);
523b6af8926SPaul Durrant     HotplugHandlerClass *hotplug_class = HOTPLUG_HANDLER_CLASS(class);
524108f7bbaSPaul Durrant 
525094a2239SPaul Durrant     bus_class->print_dev = xen_bus_print_dev;
526094a2239SPaul Durrant     bus_class->get_dev_path = xen_bus_get_dev_path;
527108f7bbaSPaul Durrant     bus_class->realize = xen_bus_realize;
528108f7bbaSPaul Durrant     bus_class->unrealize = xen_bus_unrealize;
529b6af8926SPaul Durrant 
530b6af8926SPaul Durrant     hotplug_class->unplug_request = xen_bus_unplug_request;
531108f7bbaSPaul Durrant }
532108f7bbaSPaul Durrant 
533108f7bbaSPaul Durrant static const TypeInfo xen_bus_type_info = {
534108f7bbaSPaul Durrant     .name = TYPE_XEN_BUS,
535108f7bbaSPaul Durrant     .parent = TYPE_BUS,
536108f7bbaSPaul Durrant     .instance_size = sizeof(XenBus),
537108f7bbaSPaul Durrant     .class_size = sizeof(XenBusClass),
538108f7bbaSPaul Durrant     .class_init = xen_bus_class_init,
539108f7bbaSPaul Durrant     .interfaces = (InterfaceInfo[]) {
540108f7bbaSPaul Durrant         { TYPE_HOTPLUG_HANDLER },
541108f7bbaSPaul Durrant         { }
542108f7bbaSPaul Durrant     },
543108f7bbaSPaul Durrant };
544108f7bbaSPaul Durrant 
545b6af8926SPaul Durrant void xen_device_backend_printf(XenDevice *xendev, const char *key,
546094a2239SPaul Durrant                                const char *fmt, ...)
547094a2239SPaul Durrant {
548094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
549094a2239SPaul Durrant     Error *local_err = NULL;
550094a2239SPaul Durrant     va_list ap;
551094a2239SPaul Durrant 
552094a2239SPaul Durrant     g_assert(xenbus->xsh);
553094a2239SPaul Durrant 
554094a2239SPaul Durrant     va_start(ap, fmt);
555094a2239SPaul Durrant     xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
556094a2239SPaul Durrant                     &local_err, fmt, ap);
557094a2239SPaul Durrant     va_end(ap);
558094a2239SPaul Durrant 
559094a2239SPaul Durrant     if (local_err) {
560094a2239SPaul Durrant         error_report_err(local_err);
561094a2239SPaul Durrant     }
562094a2239SPaul Durrant }
563094a2239SPaul Durrant 
56482a29e30SPaul Durrant static int xen_device_backend_scanf(XenDevice *xendev, const char *key,
56582a29e30SPaul Durrant                                     const char *fmt, ...)
56682a29e30SPaul Durrant {
56782a29e30SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
56882a29e30SPaul Durrant     va_list ap;
56982a29e30SPaul Durrant     int rc;
57082a29e30SPaul Durrant 
57182a29e30SPaul Durrant     g_assert(xenbus->xsh);
57282a29e30SPaul Durrant 
57382a29e30SPaul Durrant     va_start(ap, fmt);
57482a29e30SPaul Durrant     rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
57582a29e30SPaul Durrant                         NULL, fmt, ap);
57682a29e30SPaul Durrant     va_end(ap);
57782a29e30SPaul Durrant 
57882a29e30SPaul Durrant     return rc;
57982a29e30SPaul Durrant }
58082a29e30SPaul Durrant 
58182a29e30SPaul Durrant void xen_device_backend_set_state(XenDevice *xendev,
582094a2239SPaul Durrant                                   enum xenbus_state state)
583094a2239SPaul Durrant {
584094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
585094a2239SPaul Durrant 
586094a2239SPaul Durrant     if (xendev->backend_state == state) {
587094a2239SPaul Durrant         return;
588094a2239SPaul Durrant     }
589094a2239SPaul Durrant 
590094a2239SPaul Durrant     trace_xen_device_backend_state(type, xendev->name,
591094a2239SPaul Durrant                                    xs_strstate(state));
592094a2239SPaul Durrant 
593094a2239SPaul Durrant     xendev->backend_state = state;
594094a2239SPaul Durrant     xen_device_backend_printf(xendev, "state", "%u", state);
595094a2239SPaul Durrant }
596094a2239SPaul Durrant 
59782a29e30SPaul Durrant enum xenbus_state xen_device_backend_get_state(XenDevice *xendev)
59882a29e30SPaul Durrant {
59982a29e30SPaul Durrant     return xendev->backend_state;
60082a29e30SPaul Durrant }
60182a29e30SPaul Durrant 
602b6af8926SPaul Durrant static void xen_device_backend_set_online(XenDevice *xendev, bool online)
603b6af8926SPaul Durrant {
604b6af8926SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
605b6af8926SPaul Durrant 
606b6af8926SPaul Durrant     if (xendev->backend_online == online) {
607b6af8926SPaul Durrant         return;
608b6af8926SPaul Durrant     }
609b6af8926SPaul Durrant 
610b6af8926SPaul Durrant     trace_xen_device_backend_online(type, xendev->name, online);
611b6af8926SPaul Durrant 
612b6af8926SPaul Durrant     xendev->backend_online = online;
613b6af8926SPaul Durrant     xen_device_backend_printf(xendev, "online", "%u", online);
614b6af8926SPaul Durrant }
615b6af8926SPaul Durrant 
616cb323146SAnthony PERARD /*
617cb323146SAnthony PERARD  * Tell from the state whether the frontend is likely alive,
618cb323146SAnthony PERARD  * i.e. it will react to a change of state of the backend.
619cb323146SAnthony PERARD  */
6203809f758SPaul Durrant static bool xen_device_frontend_is_active(XenDevice *xendev)
621cb323146SAnthony PERARD {
6223809f758SPaul Durrant     switch (xendev->frontend_state) {
623cb323146SAnthony PERARD     case XenbusStateInitWait:
624cb323146SAnthony PERARD     case XenbusStateInitialised:
625cb323146SAnthony PERARD     case XenbusStateConnected:
626cb323146SAnthony PERARD     case XenbusStateClosing:
627cb323146SAnthony PERARD         return true;
628cb323146SAnthony PERARD     default:
629cb323146SAnthony PERARD         return false;
630cb323146SAnthony PERARD     }
631cb323146SAnthony PERARD }
632cb323146SAnthony PERARD 
633b6af8926SPaul Durrant static void xen_device_backend_changed(void *opaque)
634b6af8926SPaul Durrant {
635b6af8926SPaul Durrant     XenDevice *xendev = opaque;
636b6af8926SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
637b6af8926SPaul Durrant     enum xenbus_state state;
638b6af8926SPaul Durrant     unsigned int online;
639b6af8926SPaul Durrant 
640b6af8926SPaul Durrant     trace_xen_device_backend_changed(type, xendev->name);
641b6af8926SPaul Durrant 
642b6af8926SPaul Durrant     if (xen_device_backend_scanf(xendev, "state", "%u", &state) != 1) {
643b6af8926SPaul Durrant         state = XenbusStateUnknown;
644b6af8926SPaul Durrant     }
645b6af8926SPaul Durrant 
646b6af8926SPaul Durrant     xen_device_backend_set_state(xendev, state);
647b6af8926SPaul Durrant 
648b6af8926SPaul Durrant     if (xen_device_backend_scanf(xendev, "online", "%u", &online) != 1) {
649b6af8926SPaul Durrant         online = 0;
650b6af8926SPaul Durrant     }
651b6af8926SPaul Durrant 
652b6af8926SPaul Durrant     xen_device_backend_set_online(xendev, !!online);
653b6af8926SPaul Durrant 
654b6af8926SPaul Durrant     /*
655b6af8926SPaul Durrant      * If the toolstack (or unplug request callback) has set the backend
656cb323146SAnthony PERARD      * state to Closing, but there is no active frontend then set the
657cb323146SAnthony PERARD      * backend state to Closed.
658b6af8926SPaul Durrant      */
6593809f758SPaul Durrant     if (state == XenbusStateClosing &&
6603809f758SPaul Durrant         !xen_device_frontend_is_active(xendev)) {
661b6af8926SPaul Durrant         xen_device_backend_set_state(xendev, XenbusStateClosed);
662b6af8926SPaul Durrant     }
663b6af8926SPaul Durrant 
664b6af8926SPaul Durrant     /*
66567bc8e00SPaul Durrant      * If a backend is still 'online' then we should leave it alone but,
6663809f758SPaul Durrant      * if a backend is not 'online', then the device is a candidate
6673809f758SPaul Durrant      * for destruction. Hence add it to the 'inactive' list to be cleaned
6683809f758SPaul Durrant      * by xen_bus_cleanup().
669b6af8926SPaul Durrant      */
6703809f758SPaul Durrant     if (!online &&
6713809f758SPaul Durrant         (state == XenbusStateClosed ||  state == XenbusStateInitialising ||
6723809f758SPaul Durrant          state == XenbusStateInitWait || state == XenbusStateUnknown) &&
6733809f758SPaul Durrant         !xendev->inactive) {
6743809f758SPaul Durrant         XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
675a783f8adSPaul Durrant 
6763809f758SPaul Durrant         xendev->inactive = true;
6773809f758SPaul Durrant         QLIST_INSERT_HEAD(&xenbus->inactive_devices, xendev, list);
678a783f8adSPaul Durrant 
6793809f758SPaul Durrant         /*
6803809f758SPaul Durrant          * Re-write the state to cause a XenBus backend_watch notification,
6813809f758SPaul Durrant          * resulting in a call to xen_bus_cleanup().
6823809f758SPaul Durrant          */
6833809f758SPaul Durrant         xen_device_backend_printf(xendev, "state", "%u", state);
684a783f8adSPaul Durrant     }
685b6af8926SPaul Durrant }
686b6af8926SPaul Durrant 
687d198b711SPaul Durrant static XenWatch *xen_device_add_watch(XenDevice *xendev, const char *node,
688d198b711SPaul Durrant                                       const char *key,
689d198b711SPaul Durrant                                       XenWatchHandler handler,
690d198b711SPaul Durrant                                       Error **errp)
691d198b711SPaul Durrant {
692d198b711SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
693d198b711SPaul Durrant 
694d198b711SPaul Durrant     trace_xen_device_add_watch(type, xendev->name, node, key);
695d198b711SPaul Durrant 
696d198b711SPaul Durrant     return watch_list_add(xendev->watch_list, node, key, handler, xendev,
697d198b711SPaul Durrant                           errp);
698d198b711SPaul Durrant }
699d198b711SPaul Durrant 
700d198b711SPaul Durrant static void xen_device_remove_watch(XenDevice *xendev, XenWatch *watch,
701d198b711SPaul Durrant                                     Error **errp)
702d198b711SPaul Durrant {
703d198b711SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
704d198b711SPaul Durrant 
705d198b711SPaul Durrant     trace_xen_device_remove_watch(type, xendev->name, watch->node,
706d198b711SPaul Durrant                                   watch->key);
707d198b711SPaul Durrant 
708d198b711SPaul Durrant     watch_list_remove(xendev->watch_list, watch, errp);
709d198b711SPaul Durrant }
710d198b711SPaul Durrant 
711d198b711SPaul Durrant 
712094a2239SPaul Durrant static void xen_device_backend_create(XenDevice *xendev, Error **errp)
713094a2239SPaul Durrant {
7141de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
715094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
716094a2239SPaul Durrant     struct xs_permissions perms[2];
717094a2239SPaul Durrant 
718094a2239SPaul Durrant     xendev->backend_path = xen_device_get_backend_path(xendev);
719094a2239SPaul Durrant 
720094a2239SPaul Durrant     perms[0].id = xenbus->backend_id;
721094a2239SPaul Durrant     perms[0].perms = XS_PERM_NONE;
722094a2239SPaul Durrant     perms[1].id = xendev->frontend_id;
723094a2239SPaul Durrant     perms[1].perms = XS_PERM_READ;
724094a2239SPaul Durrant 
725094a2239SPaul Durrant     g_assert(xenbus->xsh);
726094a2239SPaul Durrant 
727094a2239SPaul Durrant     xs_node_create(xenbus->xsh, XBT_NULL, xendev->backend_path, perms,
7281de7096dSVladimir Sementsov-Ogievskiy                    ARRAY_SIZE(perms), errp);
7291de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
7301de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to create backend: ");
731b6af8926SPaul Durrant         return;
732b6af8926SPaul Durrant     }
733b6af8926SPaul Durrant 
734b6af8926SPaul Durrant     xendev->backend_state_watch =
735d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->backend_path,
736b6af8926SPaul Durrant                              "state", xen_device_backend_changed,
7371de7096dSVladimir Sementsov-Ogievskiy                              errp);
7381de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
7391de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch backend state: ");
740b6af8926SPaul Durrant         return;
741b6af8926SPaul Durrant     }
742b6af8926SPaul Durrant 
743b6af8926SPaul Durrant     xendev->backend_online_watch =
744d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->backend_path,
745b6af8926SPaul Durrant                              "online", xen_device_backend_changed,
7461de7096dSVladimir Sementsov-Ogievskiy                              errp);
7471de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
7481de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch backend online: ");
749b6af8926SPaul Durrant         return;
750094a2239SPaul Durrant     }
751094a2239SPaul Durrant }
752094a2239SPaul Durrant 
753094a2239SPaul Durrant static void xen_device_backend_destroy(XenDevice *xendev)
754094a2239SPaul Durrant {
755094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
756094a2239SPaul Durrant     Error *local_err = NULL;
757094a2239SPaul Durrant 
758b6af8926SPaul Durrant     if (xendev->backend_online_watch) {
759d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->backend_online_watch, NULL);
760b6af8926SPaul Durrant         xendev->backend_online_watch = NULL;
761b6af8926SPaul Durrant     }
762b6af8926SPaul Durrant 
763b6af8926SPaul Durrant     if (xendev->backend_state_watch) {
764d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->backend_state_watch, NULL);
765b6af8926SPaul Durrant         xendev->backend_state_watch = NULL;
766b6af8926SPaul Durrant     }
767b6af8926SPaul Durrant 
768094a2239SPaul Durrant     if (!xendev->backend_path) {
769094a2239SPaul Durrant         return;
770094a2239SPaul Durrant     }
771094a2239SPaul Durrant 
772094a2239SPaul Durrant     g_assert(xenbus->xsh);
773094a2239SPaul Durrant 
774094a2239SPaul Durrant     xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->backend_path,
775094a2239SPaul Durrant                     &local_err);
776094a2239SPaul Durrant     g_free(xendev->backend_path);
777094a2239SPaul Durrant     xendev->backend_path = NULL;
778094a2239SPaul Durrant 
779094a2239SPaul Durrant     if (local_err) {
780094a2239SPaul Durrant         error_report_err(local_err);
781094a2239SPaul Durrant     }
782094a2239SPaul Durrant }
783094a2239SPaul Durrant 
784b6af8926SPaul Durrant void xen_device_frontend_printf(XenDevice *xendev, const char *key,
785094a2239SPaul Durrant                                 const char *fmt, ...)
786094a2239SPaul Durrant {
787094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
788094a2239SPaul Durrant     Error *local_err = NULL;
789094a2239SPaul Durrant     va_list ap;
790094a2239SPaul Durrant 
791094a2239SPaul Durrant     g_assert(xenbus->xsh);
792094a2239SPaul Durrant 
793094a2239SPaul Durrant     va_start(ap, fmt);
794094a2239SPaul Durrant     xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
795094a2239SPaul Durrant                     &local_err, fmt, ap);
796094a2239SPaul Durrant     va_end(ap);
797094a2239SPaul Durrant 
798094a2239SPaul Durrant     if (local_err) {
799094a2239SPaul Durrant         error_report_err(local_err);
800094a2239SPaul Durrant     }
801094a2239SPaul Durrant }
802094a2239SPaul Durrant 
803b6af8926SPaul Durrant int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
80482a29e30SPaul Durrant                               const char *fmt, ...)
80582a29e30SPaul Durrant {
80682a29e30SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
80782a29e30SPaul Durrant     va_list ap;
80882a29e30SPaul Durrant     int rc;
80982a29e30SPaul Durrant 
81082a29e30SPaul Durrant     g_assert(xenbus->xsh);
81182a29e30SPaul Durrant 
81282a29e30SPaul Durrant     va_start(ap, fmt);
81382a29e30SPaul Durrant     rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
81482a29e30SPaul Durrant                         NULL, fmt, ap);
81582a29e30SPaul Durrant     va_end(ap);
81682a29e30SPaul Durrant 
81782a29e30SPaul Durrant     return rc;
81882a29e30SPaul Durrant }
81982a29e30SPaul Durrant 
820094a2239SPaul Durrant static void xen_device_frontend_set_state(XenDevice *xendev,
821705be570SAnthony PERARD                                           enum xenbus_state state,
822705be570SAnthony PERARD                                           bool publish)
823094a2239SPaul Durrant {
824094a2239SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
825094a2239SPaul Durrant 
826094a2239SPaul Durrant     if (xendev->frontend_state == state) {
827094a2239SPaul Durrant         return;
828094a2239SPaul Durrant     }
829094a2239SPaul Durrant 
830094a2239SPaul Durrant     trace_xen_device_frontend_state(type, xendev->name,
831094a2239SPaul Durrant                                     xs_strstate(state));
832094a2239SPaul Durrant 
833094a2239SPaul Durrant     xendev->frontend_state = state;
834705be570SAnthony PERARD     if (publish) {
835094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "state", "%u", state);
836094a2239SPaul Durrant     }
837705be570SAnthony PERARD }
838094a2239SPaul Durrant 
83982a29e30SPaul Durrant static void xen_device_frontend_changed(void *opaque)
84082a29e30SPaul Durrant {
84182a29e30SPaul Durrant     XenDevice *xendev = opaque;
84282a29e30SPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
84382a29e30SPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
84482a29e30SPaul Durrant     enum xenbus_state state;
84582a29e30SPaul Durrant 
84682a29e30SPaul Durrant     trace_xen_device_frontend_changed(type, xendev->name);
84782a29e30SPaul Durrant 
84882a29e30SPaul Durrant     if (xen_device_frontend_scanf(xendev, "state", "%u", &state) != 1) {
84982a29e30SPaul Durrant         state = XenbusStateUnknown;
85082a29e30SPaul Durrant     }
85182a29e30SPaul Durrant 
852705be570SAnthony PERARD     xen_device_frontend_set_state(xendev, state, false);
85382a29e30SPaul Durrant 
85467bc8e00SPaul Durrant     if (state == XenbusStateInitialising &&
85567bc8e00SPaul Durrant         xendev->backend_state == XenbusStateClosed &&
85667bc8e00SPaul Durrant         xendev->backend_online) {
85767bc8e00SPaul Durrant         /*
85867bc8e00SPaul Durrant          * The frontend is re-initializing so switch back to
85967bc8e00SPaul Durrant          * InitWait.
86067bc8e00SPaul Durrant          */
86167bc8e00SPaul Durrant         xen_device_backend_set_state(xendev, XenbusStateInitWait);
86267bc8e00SPaul Durrant         return;
86367bc8e00SPaul Durrant     }
86467bc8e00SPaul Durrant 
86582a29e30SPaul Durrant     if (xendev_class->frontend_changed) {
86682a29e30SPaul Durrant         Error *local_err = NULL;
86782a29e30SPaul Durrant 
86882a29e30SPaul Durrant         xendev_class->frontend_changed(xendev, state, &local_err);
86982a29e30SPaul Durrant 
87082a29e30SPaul Durrant         if (local_err) {
87182a29e30SPaul Durrant             error_reportf_err(local_err, "frontend change error: ");
87282a29e30SPaul Durrant         }
87382a29e30SPaul Durrant     }
87482a29e30SPaul Durrant }
87582a29e30SPaul Durrant 
8766bd6b955SMark Syms static bool xen_device_frontend_exists(XenDevice *xendev)
8776bd6b955SMark Syms {
8786bd6b955SMark Syms     enum xenbus_state state;
8796bd6b955SMark Syms 
8806bd6b955SMark Syms     return (xen_device_frontend_scanf(xendev, "state", "%u", &state) == 1);
8816bd6b955SMark Syms }
8826bd6b955SMark Syms 
883094a2239SPaul Durrant static void xen_device_frontend_create(XenDevice *xendev, Error **errp)
884094a2239SPaul Durrant {
8851de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
886094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
887094a2239SPaul Durrant     struct xs_permissions perms[2];
888094a2239SPaul Durrant 
889094a2239SPaul Durrant     xendev->frontend_path = xen_device_get_frontend_path(xendev);
890094a2239SPaul Durrant 
8916bd6b955SMark Syms     /*
8926bd6b955SMark Syms      * The frontend area may have already been created by a legacy
8936bd6b955SMark Syms      * toolstack.
8946bd6b955SMark Syms      */
8956bd6b955SMark Syms     if (!xen_device_frontend_exists(xendev)) {
896094a2239SPaul Durrant         perms[0].id = xendev->frontend_id;
897094a2239SPaul Durrant         perms[0].perms = XS_PERM_NONE;
898094a2239SPaul Durrant         perms[1].id = xenbus->backend_id;
899094a2239SPaul Durrant         perms[1].perms = XS_PERM_READ | XS_PERM_WRITE;
900094a2239SPaul Durrant 
901094a2239SPaul Durrant         g_assert(xenbus->xsh);
902094a2239SPaul Durrant 
903094a2239SPaul Durrant         xs_node_create(xenbus->xsh, XBT_NULL, xendev->frontend_path, perms,
9041de7096dSVladimir Sementsov-Ogievskiy                        ARRAY_SIZE(perms), errp);
9051de7096dSVladimir Sementsov-Ogievskiy         if (*errp) {
9061de7096dSVladimir Sementsov-Ogievskiy             error_prepend(errp, "failed to create frontend: ");
90782a29e30SPaul Durrant             return;
90882a29e30SPaul Durrant         }
9096bd6b955SMark Syms     }
91082a29e30SPaul Durrant 
91182a29e30SPaul Durrant     xendev->frontend_state_watch =
912d198b711SPaul Durrant         xen_device_add_watch(xendev, xendev->frontend_path, "state",
9131de7096dSVladimir Sementsov-Ogievskiy                              xen_device_frontend_changed, errp);
9141de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
9151de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to watch frontend state: ");
916094a2239SPaul Durrant     }
917094a2239SPaul Durrant }
918094a2239SPaul Durrant 
919094a2239SPaul Durrant static void xen_device_frontend_destroy(XenDevice *xendev)
920094a2239SPaul Durrant {
921094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
922094a2239SPaul Durrant     Error *local_err = NULL;
923094a2239SPaul Durrant 
92482a29e30SPaul Durrant     if (xendev->frontend_state_watch) {
925d198b711SPaul Durrant         xen_device_remove_watch(xendev, xendev->frontend_state_watch,
926d198b711SPaul Durrant                                 NULL);
92782a29e30SPaul Durrant         xendev->frontend_state_watch = NULL;
92882a29e30SPaul Durrant     }
92982a29e30SPaul Durrant 
930094a2239SPaul Durrant     if (!xendev->frontend_path) {
931094a2239SPaul Durrant         return;
932094a2239SPaul Durrant     }
933094a2239SPaul Durrant 
934094a2239SPaul Durrant     g_assert(xenbus->xsh);
935094a2239SPaul Durrant 
936094a2239SPaul Durrant     xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->frontend_path,
937094a2239SPaul Durrant                     &local_err);
938094a2239SPaul Durrant     g_free(xendev->frontend_path);
939094a2239SPaul Durrant     xendev->frontend_path = NULL;
940094a2239SPaul Durrant 
941094a2239SPaul Durrant     if (local_err) {
942094a2239SPaul Durrant         error_report_err(local_err);
943094a2239SPaul Durrant     }
944094a2239SPaul Durrant }
945094a2239SPaul Durrant 
9464b34b5b1SPaul Durrant void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
9474b34b5b1SPaul Durrant                                    Error **errp)
9484b34b5b1SPaul Durrant {
9494b34b5b1SPaul Durrant     if (xengnttab_set_max_grants(xendev->xgth, nr_refs)) {
9504b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_set_max_grants failed");
9514b34b5b1SPaul Durrant     }
9524b34b5b1SPaul Durrant }
9534b34b5b1SPaul Durrant 
9544b34b5b1SPaul Durrant void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs,
9554b34b5b1SPaul Durrant                                 unsigned int nr_refs, int prot,
9564b34b5b1SPaul Durrant                                 Error **errp)
9574b34b5b1SPaul Durrant {
9584b34b5b1SPaul Durrant     void *map = xengnttab_map_domain_grant_refs(xendev->xgth, nr_refs,
9594b34b5b1SPaul Durrant                                                 xendev->frontend_id, refs,
9604b34b5b1SPaul Durrant                                                 prot);
9614b34b5b1SPaul Durrant 
9624b34b5b1SPaul Durrant     if (!map) {
9634b34b5b1SPaul Durrant         error_setg_errno(errp, errno,
9644b34b5b1SPaul Durrant                          "xengnttab_map_domain_grant_refs failed");
9654b34b5b1SPaul Durrant     }
9664b34b5b1SPaul Durrant 
9674b34b5b1SPaul Durrant     return map;
9684b34b5b1SPaul Durrant }
9694b34b5b1SPaul Durrant 
9704b34b5b1SPaul Durrant void xen_device_unmap_grant_refs(XenDevice *xendev, void *map,
9714b34b5b1SPaul Durrant                                  unsigned int nr_refs, Error **errp)
9724b34b5b1SPaul Durrant {
9734b34b5b1SPaul Durrant     if (xengnttab_unmap(xendev->xgth, map, nr_refs)) {
9744b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_unmap failed");
9754b34b5b1SPaul Durrant     }
9764b34b5b1SPaul Durrant }
9774b34b5b1SPaul Durrant 
9784b34b5b1SPaul Durrant static void compat_copy_grant_refs(XenDevice *xendev, bool to_domain,
9794b34b5b1SPaul Durrant                                    XenDeviceGrantCopySegment segs[],
9804b34b5b1SPaul Durrant                                    unsigned int nr_segs, Error **errp)
9814b34b5b1SPaul Durrant {
9824b34b5b1SPaul Durrant     uint32_t *refs = g_new(uint32_t, nr_segs);
9834b34b5b1SPaul Durrant     int prot = to_domain ? PROT_WRITE : PROT_READ;
9844b34b5b1SPaul Durrant     void *map;
9854b34b5b1SPaul Durrant     unsigned int i;
9864b34b5b1SPaul Durrant 
9874b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
9884b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
9894b34b5b1SPaul Durrant 
9904b34b5b1SPaul Durrant         refs[i] = to_domain ? seg->dest.foreign.ref :
9914b34b5b1SPaul Durrant             seg->source.foreign.ref;
9924b34b5b1SPaul Durrant     }
9934b34b5b1SPaul Durrant 
9944b34b5b1SPaul Durrant     map = xengnttab_map_domain_grant_refs(xendev->xgth, nr_segs,
9954b34b5b1SPaul Durrant                                           xendev->frontend_id, refs,
9964b34b5b1SPaul Durrant                                           prot);
9974b34b5b1SPaul Durrant     if (!map) {
9984b34b5b1SPaul Durrant         error_setg_errno(errp, errno,
9994b34b5b1SPaul Durrant                          "xengnttab_map_domain_grant_refs failed");
10004b34b5b1SPaul Durrant         goto done;
10014b34b5b1SPaul Durrant     }
10024b34b5b1SPaul Durrant 
10034b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
10044b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
10054b34b5b1SPaul Durrant         void *page = map + (i * XC_PAGE_SIZE);
10064b34b5b1SPaul Durrant 
10074b34b5b1SPaul Durrant         if (to_domain) {
10084b34b5b1SPaul Durrant             memcpy(page + seg->dest.foreign.offset, seg->source.virt,
10094b34b5b1SPaul Durrant                    seg->len);
10104b34b5b1SPaul Durrant         } else {
10114b34b5b1SPaul Durrant             memcpy(seg->dest.virt, page + seg->source.foreign.offset,
10124b34b5b1SPaul Durrant                    seg->len);
10134b34b5b1SPaul Durrant         }
10144b34b5b1SPaul Durrant     }
10154b34b5b1SPaul Durrant 
10164b34b5b1SPaul Durrant     if (xengnttab_unmap(xendev->xgth, map, nr_segs)) {
10174b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_unmap failed");
10184b34b5b1SPaul Durrant     }
10194b34b5b1SPaul Durrant 
10204b34b5b1SPaul Durrant done:
10214b34b5b1SPaul Durrant     g_free(refs);
10224b34b5b1SPaul Durrant }
10234b34b5b1SPaul Durrant 
10244b34b5b1SPaul Durrant void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain,
10254b34b5b1SPaul Durrant                                 XenDeviceGrantCopySegment segs[],
10264b34b5b1SPaul Durrant                                 unsigned int nr_segs, Error **errp)
10274b34b5b1SPaul Durrant {
10284b34b5b1SPaul Durrant     xengnttab_grant_copy_segment_t *xengnttab_segs;
10294b34b5b1SPaul Durrant     unsigned int i;
10304b34b5b1SPaul Durrant 
10314b34b5b1SPaul Durrant     if (!xendev->feature_grant_copy) {
10324b34b5b1SPaul Durrant         compat_copy_grant_refs(xendev, to_domain, segs, nr_segs, errp);
10334b34b5b1SPaul Durrant         return;
10344b34b5b1SPaul Durrant     }
10354b34b5b1SPaul Durrant 
10364b34b5b1SPaul Durrant     xengnttab_segs = g_new0(xengnttab_grant_copy_segment_t, nr_segs);
10374b34b5b1SPaul Durrant 
10384b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
10394b34b5b1SPaul Durrant         XenDeviceGrantCopySegment *seg = &segs[i];
10404b34b5b1SPaul Durrant         xengnttab_grant_copy_segment_t *xengnttab_seg = &xengnttab_segs[i];
10414b34b5b1SPaul Durrant 
10424b34b5b1SPaul Durrant         if (to_domain) {
10434b34b5b1SPaul Durrant             xengnttab_seg->flags = GNTCOPY_dest_gref;
10444b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.domid = xendev->frontend_id;
10454b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.ref = seg->dest.foreign.ref;
10464b34b5b1SPaul Durrant             xengnttab_seg->dest.foreign.offset = seg->dest.foreign.offset;
10474b34b5b1SPaul Durrant             xengnttab_seg->source.virt = seg->source.virt;
10484b34b5b1SPaul Durrant         } else {
10494b34b5b1SPaul Durrant             xengnttab_seg->flags = GNTCOPY_source_gref;
10504b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.domid = xendev->frontend_id;
10514b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.ref = seg->source.foreign.ref;
10524b34b5b1SPaul Durrant             xengnttab_seg->source.foreign.offset =
10534b34b5b1SPaul Durrant                 seg->source.foreign.offset;
10544b34b5b1SPaul Durrant             xengnttab_seg->dest.virt = seg->dest.virt;
10554b34b5b1SPaul Durrant         }
10564b34b5b1SPaul Durrant 
10574b34b5b1SPaul Durrant         xengnttab_seg->len = seg->len;
10584b34b5b1SPaul Durrant     }
10594b34b5b1SPaul Durrant 
10604b34b5b1SPaul Durrant     if (xengnttab_grant_copy(xendev->xgth, nr_segs, xengnttab_segs)) {
10614b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "xengnttab_grant_copy failed");
10624b34b5b1SPaul Durrant         goto done;
10634b34b5b1SPaul Durrant     }
10644b34b5b1SPaul Durrant 
10654b34b5b1SPaul Durrant     for (i = 0; i < nr_segs; i++) {
10664b34b5b1SPaul Durrant         xengnttab_grant_copy_segment_t *xengnttab_seg = &xengnttab_segs[i];
10674b34b5b1SPaul Durrant 
10684b34b5b1SPaul Durrant         if (xengnttab_seg->status != GNTST_okay) {
10694b34b5b1SPaul Durrant             error_setg(errp, "xengnttab_grant_copy seg[%u] failed", i);
10704b34b5b1SPaul Durrant             break;
10714b34b5b1SPaul Durrant         }
10724b34b5b1SPaul Durrant     }
10734b34b5b1SPaul Durrant 
10744b34b5b1SPaul Durrant done:
10754b34b5b1SPaul Durrant     g_free(xengnttab_segs);
10764b34b5b1SPaul Durrant }
10774b34b5b1SPaul Durrant 
1078a3d669c8SPaul Durrant struct XenEventChannel {
1079c0b336eaSPaul Durrant     QLIST_ENTRY(XenEventChannel) list;
108083361a8aSPaul Durrant     AioContext *ctx;
1081c0b336eaSPaul Durrant     xenevtchn_handle *xeh;
1082a3d669c8SPaul Durrant     evtchn_port_t local_port;
1083a3d669c8SPaul Durrant     XenEventHandler handler;
1084a3d669c8SPaul Durrant     void *opaque;
1085a3d669c8SPaul Durrant };
1086a3d669c8SPaul Durrant 
1087345f42b4SPaul Durrant static bool xen_device_poll(void *opaque)
1088345f42b4SPaul Durrant {
1089345f42b4SPaul Durrant     XenEventChannel *channel = opaque;
1090345f42b4SPaul Durrant 
1091345f42b4SPaul Durrant     return channel->handler(channel->opaque);
1092345f42b4SPaul Durrant }
1093345f42b4SPaul Durrant 
1094c0b336eaSPaul Durrant static void xen_device_event(void *opaque)
1095a3d669c8SPaul Durrant {
1096c0b336eaSPaul Durrant     XenEventChannel *channel = opaque;
1097c0b336eaSPaul Durrant     unsigned long port = xenevtchn_pending(channel->xeh);
1098a3d669c8SPaul Durrant 
1099a3d669c8SPaul Durrant     if (port == channel->local_port) {
1100345f42b4SPaul Durrant         xen_device_poll(channel);
1101c0b336eaSPaul Durrant 
1102c0b336eaSPaul Durrant         xenevtchn_unmask(channel->xeh, port);
1103a3d669c8SPaul Durrant     }
1104a3d669c8SPaul Durrant }
1105a3d669c8SPaul Durrant 
110632d0b7beSPaul Durrant void xen_device_set_event_channel_context(XenDevice *xendev,
110732d0b7beSPaul Durrant                                           XenEventChannel *channel,
110883361a8aSPaul Durrant                                           AioContext *ctx,
110932d0b7beSPaul Durrant                                           Error **errp)
111032d0b7beSPaul Durrant {
111132d0b7beSPaul Durrant     if (!channel) {
111232d0b7beSPaul Durrant         error_setg(errp, "bad channel");
111332d0b7beSPaul Durrant         return;
111432d0b7beSPaul Durrant     }
111532d0b7beSPaul Durrant 
111632d0b7beSPaul Durrant     if (channel->ctx)
111732d0b7beSPaul Durrant         aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
111832d0b7beSPaul Durrant                            NULL, NULL, NULL, NULL);
111932d0b7beSPaul Durrant 
112032d0b7beSPaul Durrant     channel->ctx = ctx;
112132d0b7beSPaul Durrant     aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
112232d0b7beSPaul Durrant                        xen_device_event, NULL, xen_device_poll, channel);
112332d0b7beSPaul Durrant }
112432d0b7beSPaul Durrant 
112532d0b7beSPaul Durrant XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev,
1126a3d669c8SPaul Durrant                                                unsigned int port,
1127a3d669c8SPaul Durrant                                                XenEventHandler handler,
1128a3d669c8SPaul Durrant                                                void *opaque, Error **errp)
1129a3d669c8SPaul Durrant {
1130a3d669c8SPaul Durrant     XenEventChannel *channel = g_new0(XenEventChannel, 1);
1131a3d669c8SPaul Durrant     xenevtchn_port_or_error_t local_port;
1132a3d669c8SPaul Durrant 
1133c0b336eaSPaul Durrant     channel->xeh = xenevtchn_open(NULL, 0);
1134c0b336eaSPaul Durrant     if (!channel->xeh) {
1135c0b336eaSPaul Durrant         error_setg_errno(errp, errno, "failed xenevtchn_open");
1136c0b336eaSPaul Durrant         goto fail;
1137c0b336eaSPaul Durrant     }
1138c0b336eaSPaul Durrant 
1139c0b336eaSPaul Durrant     local_port = xenevtchn_bind_interdomain(channel->xeh,
1140a3d669c8SPaul Durrant                                             xendev->frontend_id,
1141a3d669c8SPaul Durrant                                             port);
1142a3d669c8SPaul Durrant     if (local_port < 0) {
1143a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_bind_interdomain failed");
1144c0b336eaSPaul Durrant         goto fail;
1145a3d669c8SPaul Durrant     }
1146a3d669c8SPaul Durrant 
1147a3d669c8SPaul Durrant     channel->local_port = local_port;
1148a3d669c8SPaul Durrant     channel->handler = handler;
1149a3d669c8SPaul Durrant     channel->opaque = opaque;
1150a3d669c8SPaul Durrant 
115132d0b7beSPaul Durrant     /* Only reason for failure is a NULL channel */
115232d0b7beSPaul Durrant     xen_device_set_event_channel_context(xendev, channel,
115332d0b7beSPaul Durrant                                          qemu_get_aio_context(),
115432d0b7beSPaul Durrant                                          &error_abort);
1155c0b336eaSPaul Durrant 
1156c0b336eaSPaul Durrant     QLIST_INSERT_HEAD(&xendev->event_channels, channel, list);
1157a3d669c8SPaul Durrant 
1158a3d669c8SPaul Durrant     return channel;
1159c0b336eaSPaul Durrant 
1160c0b336eaSPaul Durrant fail:
1161c0b336eaSPaul Durrant     if (channel->xeh) {
1162c0b336eaSPaul Durrant         xenevtchn_close(channel->xeh);
1163c0b336eaSPaul Durrant     }
1164c0b336eaSPaul Durrant 
1165c0b336eaSPaul Durrant     g_free(channel);
1166c0b336eaSPaul Durrant 
1167c0b336eaSPaul Durrant     return NULL;
1168a3d669c8SPaul Durrant }
1169a3d669c8SPaul Durrant 
1170a3d669c8SPaul Durrant void xen_device_notify_event_channel(XenDevice *xendev,
1171a3d669c8SPaul Durrant                                      XenEventChannel *channel,
1172a3d669c8SPaul Durrant                                      Error **errp)
1173a3d669c8SPaul Durrant {
1174a3d669c8SPaul Durrant     if (!channel) {
1175a3d669c8SPaul Durrant         error_setg(errp, "bad channel");
1176a3d669c8SPaul Durrant         return;
1177a3d669c8SPaul Durrant     }
1178a3d669c8SPaul Durrant 
1179c0b336eaSPaul Durrant     if (xenevtchn_notify(channel->xeh, channel->local_port) < 0) {
1180a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_notify failed");
1181a3d669c8SPaul Durrant     }
1182a3d669c8SPaul Durrant }
1183a3d669c8SPaul Durrant 
1184a3d669c8SPaul Durrant void xen_device_unbind_event_channel(XenDevice *xendev,
1185a3d669c8SPaul Durrant                                      XenEventChannel *channel,
1186a3d669c8SPaul Durrant                                      Error **errp)
1187a3d669c8SPaul Durrant {
1188a3d669c8SPaul Durrant     if (!channel) {
1189a3d669c8SPaul Durrant         error_setg(errp, "bad channel");
1190a3d669c8SPaul Durrant         return;
1191a3d669c8SPaul Durrant     }
1192a3d669c8SPaul Durrant 
1193c0b336eaSPaul Durrant     QLIST_REMOVE(channel, list);
1194a3d669c8SPaul Durrant 
119583361a8aSPaul Durrant     aio_set_fd_handler(channel->ctx, xenevtchn_fd(channel->xeh), true,
119683361a8aSPaul Durrant                        NULL, NULL, NULL, NULL);
1197c0b336eaSPaul Durrant 
1198c0b336eaSPaul Durrant     if (xenevtchn_unbind(channel->xeh, channel->local_port) < 0) {
1199a3d669c8SPaul Durrant         error_setg_errno(errp, errno, "xenevtchn_unbind failed");
1200a3d669c8SPaul Durrant     }
1201a3d669c8SPaul Durrant 
1202c0b336eaSPaul Durrant     xenevtchn_close(channel->xeh);
1203a3d669c8SPaul Durrant     g_free(channel);
1204a3d669c8SPaul Durrant }
1205a3d669c8SPaul Durrant 
1206b69c3c21SMarkus Armbruster static void xen_device_unrealize(DeviceState *dev)
1207108f7bbaSPaul Durrant {
1208108f7bbaSPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
1209108f7bbaSPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
1210108f7bbaSPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
1211c0b336eaSPaul Durrant     XenEventChannel *channel, *next;
1212108f7bbaSPaul Durrant 
1213094a2239SPaul Durrant     if (!xendev->name) {
1214094a2239SPaul Durrant         return;
1215094a2239SPaul Durrant     }
1216094a2239SPaul Durrant 
1217094a2239SPaul Durrant     trace_xen_device_unrealize(type, xendev->name);
1218094a2239SPaul Durrant 
1219094a2239SPaul Durrant     if (xendev->exit.notify) {
1220094a2239SPaul Durrant         qemu_remove_exit_notifier(&xendev->exit);
1221094a2239SPaul Durrant         xendev->exit.notify = NULL;
1222094a2239SPaul Durrant     }
1223108f7bbaSPaul Durrant 
1224108f7bbaSPaul Durrant     if (xendev_class->unrealize) {
1225b69c3c21SMarkus Armbruster         xendev_class->unrealize(xendev);
1226108f7bbaSPaul Durrant     }
1227094a2239SPaul Durrant 
1228c0b336eaSPaul Durrant     /* Make sure all event channels are cleaned up */
1229c0b336eaSPaul Durrant     QLIST_FOREACH_SAFE(channel, &xendev->event_channels, list, next) {
1230c0b336eaSPaul Durrant         xen_device_unbind_event_channel(xendev, channel, NULL);
1231c0b336eaSPaul Durrant     }
1232c0b336eaSPaul Durrant 
1233094a2239SPaul Durrant     xen_device_frontend_destroy(xendev);
1234094a2239SPaul Durrant     xen_device_backend_destroy(xendev);
1235094a2239SPaul Durrant 
12364b34b5b1SPaul Durrant     if (xendev->xgth) {
12374b34b5b1SPaul Durrant         xengnttab_close(xendev->xgth);
12384b34b5b1SPaul Durrant         xendev->xgth = NULL;
12394b34b5b1SPaul Durrant     }
12404b34b5b1SPaul Durrant 
1241d198b711SPaul Durrant     if (xendev->watch_list) {
1242d198b711SPaul Durrant         watch_list_destroy(xendev->watch_list);
1243d198b711SPaul Durrant         xendev->watch_list = NULL;
1244d198b711SPaul Durrant     }
1245d198b711SPaul Durrant 
1246d198b711SPaul Durrant     if (xendev->xsh) {
1247d198b711SPaul Durrant         xs_close(xendev->xsh);
1248d198b711SPaul Durrant         xendev->xsh = NULL;
1249d198b711SPaul Durrant     }
1250d198b711SPaul Durrant 
1251094a2239SPaul Durrant     g_free(xendev->name);
1252094a2239SPaul Durrant     xendev->name = NULL;
1253094a2239SPaul Durrant }
1254094a2239SPaul Durrant 
1255094a2239SPaul Durrant static void xen_device_exit(Notifier *n, void *data)
1256094a2239SPaul Durrant {
1257094a2239SPaul Durrant     XenDevice *xendev = container_of(n, XenDevice, exit);
1258094a2239SPaul Durrant 
1259b69c3c21SMarkus Armbruster     xen_device_unrealize(DEVICE(xendev));
1260108f7bbaSPaul Durrant }
1261108f7bbaSPaul Durrant 
1262108f7bbaSPaul Durrant static void xen_device_realize(DeviceState *dev, Error **errp)
1263108f7bbaSPaul Durrant {
12641de7096dSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
1265108f7bbaSPaul Durrant     XenDevice *xendev = XEN_DEVICE(dev);
1266108f7bbaSPaul Durrant     XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
1267094a2239SPaul Durrant     XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
1268108f7bbaSPaul Durrant     const char *type = object_get_typename(OBJECT(xendev));
1269108f7bbaSPaul Durrant 
1270094a2239SPaul Durrant     if (xendev->frontend_id == DOMID_INVALID) {
1271094a2239SPaul Durrant         xendev->frontend_id = xen_domid;
1272094a2239SPaul Durrant     }
1273094a2239SPaul Durrant 
1274094a2239SPaul Durrant     if (xendev->frontend_id >= DOMID_FIRST_RESERVED) {
1275094a2239SPaul Durrant         error_setg(errp, "invalid frontend-id");
1276094a2239SPaul Durrant         goto unrealize;
1277094a2239SPaul Durrant     }
1278094a2239SPaul Durrant 
1279094a2239SPaul Durrant     if (!xendev_class->get_name) {
1280094a2239SPaul Durrant         error_setg(errp, "get_name method not implemented");
1281094a2239SPaul Durrant         goto unrealize;
1282094a2239SPaul Durrant     }
1283094a2239SPaul Durrant 
12841de7096dSVladimir Sementsov-Ogievskiy     xendev->name = xendev_class->get_name(xendev, errp);
12851de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
12861de7096dSVladimir Sementsov-Ogievskiy         error_prepend(errp, "failed to get device name: ");
1287094a2239SPaul Durrant         goto unrealize;
1288094a2239SPaul Durrant     }
1289094a2239SPaul Durrant 
1290094a2239SPaul Durrant     trace_xen_device_realize(type, xendev->name);
1291094a2239SPaul Durrant 
1292d198b711SPaul Durrant     xendev->xsh = xs_open(0);
1293d198b711SPaul Durrant     if (!xendev->xsh) {
1294d198b711SPaul Durrant         error_setg_errno(errp, errno, "failed xs_open");
1295d198b711SPaul Durrant         goto unrealize;
1296d198b711SPaul Durrant     }
1297d198b711SPaul Durrant 
1298d198b711SPaul Durrant     xendev->watch_list = watch_list_create(xendev->xsh);
1299d198b711SPaul Durrant 
13004b34b5b1SPaul Durrant     xendev->xgth = xengnttab_open(NULL, 0);
13014b34b5b1SPaul Durrant     if (!xendev->xgth) {
13024b34b5b1SPaul Durrant         error_setg_errno(errp, errno, "failed xengnttab_open");
13034b34b5b1SPaul Durrant         goto unrealize;
13044b34b5b1SPaul Durrant     }
13054b34b5b1SPaul Durrant 
13064b34b5b1SPaul Durrant     xendev->feature_grant_copy =
13074b34b5b1SPaul Durrant         (xengnttab_grant_copy(xendev->xgth, 0, NULL) == 0);
13084b34b5b1SPaul Durrant 
13091de7096dSVladimir Sementsov-Ogievskiy     xen_device_backend_create(xendev, errp);
13101de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
1311094a2239SPaul Durrant         goto unrealize;
1312094a2239SPaul Durrant     }
1313094a2239SPaul Durrant 
13141de7096dSVladimir Sementsov-Ogievskiy     xen_device_frontend_create(xendev, errp);
13151de7096dSVladimir Sementsov-Ogievskiy     if (*errp) {
1316094a2239SPaul Durrant         goto unrealize;
1317094a2239SPaul Durrant     }
1318108f7bbaSPaul Durrant 
1319108f7bbaSPaul Durrant     if (xendev_class->realize) {
13201de7096dSVladimir Sementsov-Ogievskiy         xendev_class->realize(xendev, errp);
13211de7096dSVladimir Sementsov-Ogievskiy         if (*errp) {
1322108f7bbaSPaul Durrant             goto unrealize;
1323108f7bbaSPaul Durrant         }
1324108f7bbaSPaul Durrant     }
1325108f7bbaSPaul Durrant 
1326094a2239SPaul Durrant     xen_device_backend_printf(xendev, "frontend", "%s",
1327094a2239SPaul Durrant                               xendev->frontend_path);
1328094a2239SPaul Durrant     xen_device_backend_printf(xendev, "frontend-id", "%u",
1329094a2239SPaul Durrant                               xendev->frontend_id);
1330094a2239SPaul Durrant     xen_device_backend_printf(xendev, "hotplug-status", "connected");
1331094a2239SPaul Durrant 
1332b6af8926SPaul Durrant     xen_device_backend_set_online(xendev, true);
1333094a2239SPaul Durrant     xen_device_backend_set_state(xendev, XenbusStateInitWait);
1334094a2239SPaul Durrant 
13356bd6b955SMark Syms     if (!xen_device_frontend_exists(xendev)) {
1336094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "backend", "%s",
1337094a2239SPaul Durrant                                    xendev->backend_path);
1338094a2239SPaul Durrant         xen_device_frontend_printf(xendev, "backend-id", "%u",
1339094a2239SPaul Durrant                                    xenbus->backend_id);
1340094a2239SPaul Durrant 
1341705be570SAnthony PERARD         xen_device_frontend_set_state(xendev, XenbusStateInitialising, true);
13426bd6b955SMark Syms     }
1343094a2239SPaul Durrant 
1344094a2239SPaul Durrant     xendev->exit.notify = xen_device_exit;
1345094a2239SPaul Durrant     qemu_add_exit_notifier(&xendev->exit);
1346108f7bbaSPaul Durrant     return;
1347108f7bbaSPaul Durrant 
1348108f7bbaSPaul Durrant unrealize:
1349b69c3c21SMarkus Armbruster     xen_device_unrealize(dev);
1350108f7bbaSPaul Durrant }
1351108f7bbaSPaul Durrant 
1352094a2239SPaul Durrant static Property xen_device_props[] = {
1353094a2239SPaul Durrant     DEFINE_PROP_UINT16("frontend-id", XenDevice, frontend_id,
1354094a2239SPaul Durrant                        DOMID_INVALID),
1355094a2239SPaul Durrant     DEFINE_PROP_END_OF_LIST()
1356094a2239SPaul Durrant };
1357094a2239SPaul Durrant 
1358108f7bbaSPaul Durrant static void xen_device_class_init(ObjectClass *class, void *data)
1359108f7bbaSPaul Durrant {
1360108f7bbaSPaul Durrant     DeviceClass *dev_class = DEVICE_CLASS(class);
1361108f7bbaSPaul Durrant 
1362108f7bbaSPaul Durrant     dev_class->realize = xen_device_realize;
1363108f7bbaSPaul Durrant     dev_class->unrealize = xen_device_unrealize;
13644f67d30bSMarc-André Lureau     device_class_set_props(dev_class, xen_device_props);
1365108f7bbaSPaul Durrant     dev_class->bus_type = TYPE_XEN_BUS;
1366108f7bbaSPaul Durrant }
1367108f7bbaSPaul Durrant 
1368108f7bbaSPaul Durrant static const TypeInfo xen_device_type_info = {
1369108f7bbaSPaul Durrant     .name = TYPE_XEN_DEVICE,
1370108f7bbaSPaul Durrant     .parent = TYPE_DEVICE,
1371108f7bbaSPaul Durrant     .instance_size = sizeof(XenDevice),
1372108f7bbaSPaul Durrant     .abstract = true,
1373108f7bbaSPaul Durrant     .class_size = sizeof(XenDeviceClass),
1374108f7bbaSPaul Durrant     .class_init = xen_device_class_init,
1375108f7bbaSPaul Durrant };
1376108f7bbaSPaul Durrant 
1377108f7bbaSPaul Durrant typedef struct XenBridge {
1378108f7bbaSPaul Durrant     SysBusDevice busdev;
1379108f7bbaSPaul Durrant } XenBridge;
1380108f7bbaSPaul Durrant 
1381108f7bbaSPaul Durrant #define TYPE_XEN_BRIDGE "xen-bridge"
1382108f7bbaSPaul Durrant 
1383108f7bbaSPaul Durrant static const TypeInfo xen_bridge_type_info = {
1384108f7bbaSPaul Durrant     .name = TYPE_XEN_BRIDGE,
1385108f7bbaSPaul Durrant     .parent = TYPE_SYS_BUS_DEVICE,
1386108f7bbaSPaul Durrant     .instance_size = sizeof(XenBridge),
1387108f7bbaSPaul Durrant };
1388108f7bbaSPaul Durrant 
1389108f7bbaSPaul Durrant static void xen_register_types(void)
1390108f7bbaSPaul Durrant {
1391108f7bbaSPaul Durrant     type_register_static(&xen_bridge_type_info);
1392108f7bbaSPaul Durrant     type_register_static(&xen_bus_type_info);
1393108f7bbaSPaul Durrant     type_register_static(&xen_device_type_info);
1394108f7bbaSPaul Durrant }
1395108f7bbaSPaul Durrant 
1396108f7bbaSPaul Durrant type_init(xen_register_types)
1397108f7bbaSPaul Durrant 
1398108f7bbaSPaul Durrant void xen_bus_init(void)
1399108f7bbaSPaul Durrant {
14003e80f690SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_XEN_BRIDGE);
1401108f7bbaSPaul Durrant     BusState *bus = qbus_create(TYPE_XEN_BUS, dev, NULL);
1402108f7bbaSPaul Durrant 
14033c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1404cd7c8660SMarkus Armbruster     qbus_set_bus_hotplug_handler(bus);
1405108f7bbaSPaul Durrant }
1406