Merge remote-tracking branch 'remotes/dgilbert/tags/pull-hmp-20170331' into staging
[qemu/ar7.git] / hw / xen / xen_backend.c
blob6c21c37d682ba240a986735b335b756d76bf76be
1 /*
2 * xen backend driver infrastructure
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
22 * TODO: add some xenbus / xenstore concepts overview here.
25 #include "qemu/osdep.h"
26 #include <sys/signal.h>
28 #include "hw/hw.h"
29 #include "hw/sysbus.h"
30 #include "hw/boards.h"
31 #include "sysemu/char.h"
32 #include "qemu/log.h"
33 #include "qapi/error.h"
34 #include "hw/xen/xen_backend.h"
35 #include "hw/xen/xen_pvdev.h"
36 #include "monitor/qdev.h"
38 #include <xen/grant_table.h>
40 DeviceState *xen_sysdev;
41 BusState *xen_sysbus;
43 /* ------------------------------------------------------------- */
45 /* public */
46 xc_interface *xen_xc = NULL;
47 xenforeignmemory_handle *xen_fmem = NULL;
48 struct xs_handle *xenstore = NULL;
49 const char *xen_protocol;
51 /* private */
52 static int debug;
54 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
56 return xenstore_write_str(xendev->be, node, val);
59 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
61 return xenstore_write_int(xendev->be, node, ival);
64 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
66 return xenstore_write_int64(xendev->be, node, ival);
69 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
71 return xenstore_read_str(xendev->be, node);
74 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
76 return xenstore_read_int(xendev->be, node, ival);
79 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
81 return xenstore_read_str(xendev->fe, node);
84 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
86 return xenstore_read_int(xendev->fe, node, ival);
89 int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node,
90 uint64_t *uval)
92 return xenstore_read_uint64(xendev->fe, node, uval);
95 /* ------------------------------------------------------------- */
97 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
99 int rc;
101 rc = xenstore_write_be_int(xendev, "state", state);
102 if (rc < 0) {
103 return rc;
105 xen_pv_printf(xendev, 1, "backend state: %s -> %s\n",
106 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
107 xendev->be_state = state;
108 return 0;
112 * get xen backend device, allocate a new one if it doesn't exist.
114 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
115 struct XenDevOps *ops)
117 struct XenDevice *xendev;
119 xendev = xen_pv_find_xendev(type, dom, dev);
120 if (xendev) {
121 return xendev;
124 /* init new xendev */
125 xendev = g_malloc0(ops->size);
126 object_initialize(&xendev->qdev, ops->size, TYPE_XENBACKEND);
127 OBJECT(xendev)->free = g_free;
128 qdev_set_parent_bus(DEVICE(xendev), xen_sysbus);
129 qdev_set_id(DEVICE(xendev), g_strdup_printf("xen-%s-%d", type, dev));
130 qdev_init_nofail(DEVICE(xendev));
131 object_unref(OBJECT(xendev));
133 xendev->type = type;
134 xendev->dom = dom;
135 xendev->dev = dev;
136 xendev->ops = ops;
138 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
139 xendev->type, xendev->dom, xendev->dev);
140 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
141 xendev->type, xendev->dev);
143 xendev->debug = debug;
144 xendev->local_port = -1;
146 xendev->evtchndev = xenevtchn_open(NULL, 0);
147 if (xendev->evtchndev == NULL) {
148 xen_pv_printf(NULL, 0, "can't open evtchn device\n");
149 qdev_unplug(DEVICE(xendev), NULL);
150 return NULL;
152 fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
154 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
155 xendev->gnttabdev = xengnttab_open(NULL, 0);
156 if (xendev->gnttabdev == NULL) {
157 xen_pv_printf(NULL, 0, "can't open gnttab device\n");
158 xenevtchn_close(xendev->evtchndev);
159 qdev_unplug(DEVICE(xendev), NULL);
160 return NULL;
162 } else {
163 xendev->gnttabdev = NULL;
166 xen_pv_insert_xendev(xendev);
168 if (xendev->ops->alloc) {
169 xendev->ops->alloc(xendev);
172 return xendev;
177 * Sync internal data structures on xenstore updates.
178 * Node specifies the changed field. node = NULL means
179 * update all fields (used for initialization).
181 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
183 if (node == NULL || strcmp(node, "online") == 0) {
184 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
185 xendev->online = 0;
189 if (node) {
190 xen_pv_printf(xendev, 2, "backend update: %s\n", node);
191 if (xendev->ops->backend_changed) {
192 xendev->ops->backend_changed(xendev, node);
197 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
199 int fe_state;
201 if (node == NULL || strcmp(node, "state") == 0) {
202 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
203 fe_state = XenbusStateUnknown;
205 if (xendev->fe_state != fe_state) {
206 xen_pv_printf(xendev, 1, "frontend state: %s -> %s\n",
207 xenbus_strstate(xendev->fe_state),
208 xenbus_strstate(fe_state));
210 xendev->fe_state = fe_state;
212 if (node == NULL || strcmp(node, "protocol") == 0) {
213 g_free(xendev->protocol);
214 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
215 if (xendev->protocol) {
216 xen_pv_printf(xendev, 1, "frontend protocol: %s\n",
217 xendev->protocol);
221 if (node) {
222 xen_pv_printf(xendev, 2, "frontend update: %s\n", node);
223 if (xendev->ops->frontend_changed) {
224 xendev->ops->frontend_changed(xendev, node);
229 /* ------------------------------------------------------------- */
230 /* Check for possible state transitions and perform them. */
233 * Initial xendev setup. Read frontend path, register watch for it.
234 * Should succeed once xend finished setting up the backend device.
236 * Also sets initial state (-> Initializing) when done. Which
237 * only affects the xendev->be_state variable as xenbus should
238 * already be put into that state by xend.
240 static int xen_be_try_setup(struct XenDevice *xendev)
242 char token[XEN_BUFSIZE];
243 int be_state;
245 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
246 xen_pv_printf(xendev, 0, "reading backend state failed\n");
247 return -1;
250 if (be_state != XenbusStateInitialising) {
251 xen_pv_printf(xendev, 0, "initial backend state is wrong (%s)\n",
252 xenbus_strstate(be_state));
253 return -1;
256 xendev->fe = xenstore_read_be_str(xendev, "frontend");
257 if (xendev->fe == NULL) {
258 xen_pv_printf(xendev, 0, "reading frontend path failed\n");
259 return -1;
262 /* setup frontend watch */
263 snprintf(token, sizeof(token), "fe:%p", xendev);
264 if (!xs_watch(xenstore, xendev->fe, token)) {
265 xen_pv_printf(xendev, 0, "watching frontend path (%s) failed\n",
266 xendev->fe);
267 return -1;
269 xen_be_set_state(xendev, XenbusStateInitialising);
271 xen_be_backend_changed(xendev, NULL);
272 xen_be_frontend_changed(xendev, NULL);
273 return 0;
277 * Try initialize xendev. Prepare everything the backend can do
278 * without synchronizing with the frontend. Fakes hotplug-status. No
279 * hotplug involved here because this is about userspace drivers, thus
280 * there are kernel backend devices which could invoke hotplug.
282 * Goes to InitWait on success.
284 static int xen_be_try_init(struct XenDevice *xendev)
286 int rc = 0;
288 if (!xendev->online) {
289 xen_pv_printf(xendev, 1, "not online\n");
290 return -1;
293 if (xendev->ops->init) {
294 rc = xendev->ops->init(xendev);
296 if (rc != 0) {
297 xen_pv_printf(xendev, 1, "init() failed\n");
298 return rc;
301 xenstore_write_be_str(xendev, "hotplug-status", "connected");
302 xen_be_set_state(xendev, XenbusStateInitWait);
303 return 0;
307 * Try to initialise xendev. Depends on the frontend being ready
308 * for it (shared ring and evtchn info in xenstore, state being
309 * Initialised or Connected).
311 * Goes to Connected on success.
313 static int xen_be_try_initialise(struct XenDevice *xendev)
315 int rc = 0;
317 if (xendev->fe_state != XenbusStateInitialised &&
318 xendev->fe_state != XenbusStateConnected) {
319 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
320 xen_pv_printf(xendev, 2, "frontend not ready, ignoring\n");
321 } else {
322 xen_pv_printf(xendev, 2, "frontend not ready (yet)\n");
323 return -1;
327 if (xendev->ops->initialise) {
328 rc = xendev->ops->initialise(xendev);
330 if (rc != 0) {
331 xen_pv_printf(xendev, 0, "initialise() failed\n");
332 return rc;
335 xen_be_set_state(xendev, XenbusStateConnected);
336 return 0;
340 * Try to let xendev know that it is connected. Depends on the
341 * frontend being Connected. Note that this may be called more
342 * than once since the backend state is not modified.
344 static void xen_be_try_connected(struct XenDevice *xendev)
346 if (!xendev->ops->connected) {
347 return;
350 if (xendev->fe_state != XenbusStateConnected) {
351 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
352 xen_pv_printf(xendev, 2, "frontend not ready, ignoring\n");
353 } else {
354 xen_pv_printf(xendev, 2, "frontend not ready (yet)\n");
355 return;
359 xendev->ops->connected(xendev);
363 * Teardown connection.
365 * Goes to Closed when done.
367 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
369 if (xendev->be_state != XenbusStateClosing &&
370 xendev->be_state != XenbusStateClosed &&
371 xendev->ops->disconnect) {
372 xendev->ops->disconnect(xendev);
374 if (xendev->be_state != state) {
375 xen_be_set_state(xendev, state);
380 * Try to reset xendev, for reconnection by another frontend instance.
382 static int xen_be_try_reset(struct XenDevice *xendev)
384 if (xendev->fe_state != XenbusStateInitialising) {
385 return -1;
388 xen_pv_printf(xendev, 1, "device reset (for re-connect)\n");
389 xen_be_set_state(xendev, XenbusStateInitialising);
390 return 0;
394 * state change dispatcher function
396 void xen_be_check_state(struct XenDevice *xendev)
398 int rc = 0;
400 /* frontend may request shutdown from almost anywhere */
401 if (xendev->fe_state == XenbusStateClosing ||
402 xendev->fe_state == XenbusStateClosed) {
403 xen_be_disconnect(xendev, xendev->fe_state);
404 return;
407 /* check for possible backend state transitions */
408 for (;;) {
409 switch (xendev->be_state) {
410 case XenbusStateUnknown:
411 rc = xen_be_try_setup(xendev);
412 break;
413 case XenbusStateInitialising:
414 rc = xen_be_try_init(xendev);
415 break;
416 case XenbusStateInitWait:
417 rc = xen_be_try_initialise(xendev);
418 break;
419 case XenbusStateConnected:
420 /* xendev->be_state doesn't change */
421 xen_be_try_connected(xendev);
422 rc = -1;
423 break;
424 case XenbusStateClosed:
425 rc = xen_be_try_reset(xendev);
426 break;
427 default:
428 rc = -1;
430 if (rc != 0) {
431 break;
436 /* ------------------------------------------------------------- */
438 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
440 struct XenDevice *xendev;
441 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
442 char **dev = NULL;
443 unsigned int cdev, j;
445 /* setup watch */
446 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
447 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
448 if (!xs_watch(xenstore, path, token)) {
449 xen_pv_printf(NULL, 0, "xen be: watching backend path (%s) failed\n",
450 path);
451 return -1;
454 /* look for backends */
455 dev = xs_directory(xenstore, 0, path, &cdev);
456 if (!dev) {
457 return 0;
459 for (j = 0; j < cdev; j++) {
460 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
461 if (xendev == NULL) {
462 continue;
464 xen_be_check_state(xendev);
466 free(dev);
467 return 0;
470 void xenstore_update_be(char *watch, char *type, int dom,
471 struct XenDevOps *ops)
473 struct XenDevice *xendev;
474 char path[XEN_BUFSIZE], *bepath;
475 unsigned int len, dev;
477 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
478 if (strncmp(path, watch, len) != 0) {
479 return;
481 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
482 strcpy(path, "");
483 if (sscanf(watch+len, "/%u", &dev) != 1) {
484 dev = -1;
487 if (dev == -1) {
488 return;
491 xendev = xen_be_get_xendev(type, dom, dev, ops);
492 if (xendev != NULL) {
493 bepath = xs_read(xenstore, 0, xendev->be, &len);
494 if (bepath == NULL) {
495 xen_pv_del_xendev(xendev);
496 } else {
497 free(bepath);
498 xen_be_backend_changed(xendev, path);
499 xen_be_check_state(xendev);
504 void xenstore_update_fe(char *watch, struct XenDevice *xendev)
506 char *node;
507 unsigned int len;
509 len = strlen(xendev->fe);
510 if (strncmp(xendev->fe, watch, len) != 0) {
511 return;
513 if (watch[len] != '/') {
514 return;
516 node = watch + len + 1;
518 xen_be_frontend_changed(xendev, node);
519 xen_be_check_state(xendev);
521 /* -------------------------------------------------------------------- */
523 int xen_be_init(void)
525 xenstore = xs_daemon_open();
526 if (!xenstore) {
527 xen_pv_printf(NULL, 0, "can't connect to xenstored\n");
528 return -1;
531 qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
533 if (xen_xc == NULL || xen_fmem == NULL) {
534 /* Check if xen_init() have been called */
535 goto err;
538 xen_sysdev = qdev_create(NULL, TYPE_XENSYSDEV);
539 qdev_init_nofail(xen_sysdev);
540 xen_sysbus = qbus_create(TYPE_XENSYSBUS, DEVICE(xen_sysdev), "xen-sysbus");
541 qbus_set_bus_hotplug_handler(xen_sysbus, &error_abort);
543 return 0;
545 err:
546 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
547 xs_daemon_close(xenstore);
548 xenstore = NULL;
550 return -1;
553 static void xen_set_dynamic_sysbus(void)
555 Object *machine = qdev_get_machine();
556 ObjectClass *oc = object_get_class(machine);
557 MachineClass *mc = MACHINE_CLASS(oc);
559 mc->has_dynamic_sysbus = true;
562 int xen_be_register(const char *type, struct XenDevOps *ops)
564 char path[50];
565 int rc;
567 if (ops->backend_register) {
568 rc = ops->backend_register();
569 if (rc) {
570 return rc;
574 snprintf(path, sizeof(path), "device-model/%u/backends/%s", xen_domid,
575 type);
576 xenstore_mkdir(path, XS_PERM_NONE);
578 return xenstore_scan(type, xen_domid, ops);
581 void xen_be_register_common(void)
583 xen_set_dynamic_sysbus();
585 xen_be_register("console", &xen_console_ops);
586 xen_be_register("vkbd", &xen_kbdmouse_ops);
587 xen_be_register("qdisk", &xen_blkdev_ops);
588 #ifdef CONFIG_USB_LIBUSB
589 xen_be_register("qusb", &xen_usb_ops);
590 #endif
593 int xen_be_bind_evtchn(struct XenDevice *xendev)
595 if (xendev->local_port != -1) {
596 return 0;
598 xendev->local_port = xenevtchn_bind_interdomain
599 (xendev->evtchndev, xendev->dom, xendev->remote_port);
600 if (xendev->local_port == -1) {
601 xen_pv_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
602 return -1;
604 xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
605 qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
606 xen_pv_evtchn_event, NULL, xendev);
607 return 0;
611 static Property xendev_properties[] = {
612 DEFINE_PROP_END_OF_LIST(),
615 static void xendev_class_init(ObjectClass *klass, void *data)
617 DeviceClass *dc = DEVICE_CLASS(klass);
619 dc->props = xendev_properties;
620 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
623 static const TypeInfo xendev_type_info = {
624 .name = TYPE_XENBACKEND,
625 .parent = TYPE_XENSYSDEV,
626 .class_init = xendev_class_init,
627 .instance_size = sizeof(struct XenDevice),
630 static void xen_sysbus_class_init(ObjectClass *klass, void *data)
632 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
634 hc->unplug = qdev_simple_device_unplug_cb;
637 static const TypeInfo xensysbus_info = {
638 .name = TYPE_XENSYSBUS,
639 .parent = TYPE_BUS,
640 .class_init = xen_sysbus_class_init,
641 .interfaces = (InterfaceInfo[]) {
642 { TYPE_HOTPLUG_HANDLER },
647 static int xen_sysdev_init(SysBusDevice *dev)
649 return 0;
652 static Property xen_sysdev_properties[] = {
653 {/* end of property list */},
656 static void xen_sysdev_class_init(ObjectClass *klass, void *data)
658 DeviceClass *dc = DEVICE_CLASS(klass);
659 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
661 k->init = xen_sysdev_init;
662 dc->props = xen_sysdev_properties;
663 dc->bus_type = TYPE_XENSYSBUS;
666 static const TypeInfo xensysdev_info = {
667 .name = TYPE_XENSYSDEV,
668 .parent = TYPE_SYS_BUS_DEVICE,
669 .instance_size = sizeof(SysBusDevice),
670 .class_init = xen_sysdev_class_init,
673 static void xenbe_register_types(void)
675 type_register_static(&xensysbus_info);
676 type_register_static(&xensysdev_info);
677 type_register_static(&xendev_type_info);
680 type_init(xenbe_register_types)