do not call g_thread_init() for glib >= 2.31
[qemu/ar7.git] / hw / xen / xen_backend.c
blob3cd45b407cccc0a23d642958deaeafb5366f2af6
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 <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/signal.h>
37 #include "hw/hw.h"
38 #include "sysemu/char.h"
39 #include "qemu/log.h"
40 #include "hw/xen/xen_backend.h"
42 #include <xen/grant_table.h>
44 /* ------------------------------------------------------------- */
46 /* public */
47 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
48 struct xs_handle *xenstore = NULL;
49 const char *xen_protocol;
51 /* private */
52 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
53 static int debug = 0;
55 /* ------------------------------------------------------------- */
57 int xenstore_write_str(const char *base, const char *node, const char *val)
59 char abspath[XEN_BUFSIZE];
61 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
62 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
63 return -1;
65 return 0;
68 char *xenstore_read_str(const char *base, const char *node)
70 char abspath[XEN_BUFSIZE];
71 unsigned int len;
72 char *str, *ret = NULL;
74 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
75 str = xs_read(xenstore, 0, abspath, &len);
76 if (str != NULL) {
77 /* move to qemu-allocated memory to make sure
78 * callers can savely g_free() stuff. */
79 ret = g_strdup(str);
80 free(str);
82 return ret;
85 int xenstore_write_int(const char *base, const char *node, int ival)
87 char val[12];
89 snprintf(val, sizeof(val), "%d", ival);
90 return xenstore_write_str(base, node, val);
93 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
95 char val[21];
97 snprintf(val, sizeof(val), "%"PRId64, ival);
98 return xenstore_write_str(base, node, val);
101 int xenstore_read_int(const char *base, const char *node, int *ival)
103 char *val;
104 int rc = -1;
106 val = xenstore_read_str(base, node);
107 if (val && 1 == sscanf(val, "%d", ival)) {
108 rc = 0;
110 g_free(val);
111 return rc;
114 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
116 return xenstore_write_str(xendev->be, node, val);
119 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
121 return xenstore_write_int(xendev->be, node, ival);
124 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
126 return xenstore_write_int64(xendev->be, node, ival);
129 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
131 return xenstore_read_str(xendev->be, node);
134 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
136 return xenstore_read_int(xendev->be, node, ival);
139 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
141 return xenstore_read_str(xendev->fe, node);
144 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
146 return xenstore_read_int(xendev->fe, node, ival);
149 /* ------------------------------------------------------------- */
151 const char *xenbus_strstate(enum xenbus_state state)
153 static const char *const name[] = {
154 [ XenbusStateUnknown ] = "Unknown",
155 [ XenbusStateInitialising ] = "Initialising",
156 [ XenbusStateInitWait ] = "InitWait",
157 [ XenbusStateInitialised ] = "Initialised",
158 [ XenbusStateConnected ] = "Connected",
159 [ XenbusStateClosing ] = "Closing",
160 [ XenbusStateClosed ] = "Closed",
162 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
165 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
167 int rc;
169 rc = xenstore_write_be_int(xendev, "state", state);
170 if (rc < 0) {
171 return rc;
173 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
174 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
175 xendev->be_state = state;
176 return 0;
179 /* ------------------------------------------------------------- */
181 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
183 struct XenDevice *xendev;
185 QTAILQ_FOREACH(xendev, &xendevs, next) {
186 if (xendev->dom != dom) {
187 continue;
189 if (xendev->dev != dev) {
190 continue;
192 if (strcmp(xendev->type, type) != 0) {
193 continue;
195 return xendev;
197 return NULL;
201 * get xen backend device, allocate a new one if it doesn't exist.
203 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
204 struct XenDevOps *ops)
206 struct XenDevice *xendev;
208 xendev = xen_be_find_xendev(type, dom, dev);
209 if (xendev) {
210 return xendev;
213 /* init new xendev */
214 xendev = g_malloc0(ops->size);
215 xendev->type = type;
216 xendev->dom = dom;
217 xendev->dev = dev;
218 xendev->ops = ops;
220 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
221 xendev->type, xendev->dom, xendev->dev);
222 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
223 xendev->type, xendev->dev);
225 xendev->debug = debug;
226 xendev->local_port = -1;
228 xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
229 if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
230 xen_be_printf(NULL, 0, "can't open evtchn device\n");
231 g_free(xendev);
232 return NULL;
234 fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
236 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
237 xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
238 if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
239 xen_be_printf(NULL, 0, "can't open gnttab device\n");
240 xc_evtchn_close(xendev->evtchndev);
241 g_free(xendev);
242 return NULL;
244 } else {
245 xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
248 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
250 if (xendev->ops->alloc) {
251 xendev->ops->alloc(xendev);
254 return xendev;
258 * release xen backend device.
260 static struct XenDevice *xen_be_del_xendev(int dom, int dev)
262 struct XenDevice *xendev, *xnext;
265 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
266 * we save the next pointer in xnext because we might free xendev.
268 xnext = xendevs.tqh_first;
269 while (xnext) {
270 xendev = xnext;
271 xnext = xendev->next.tqe_next;
273 if (xendev->dom != dom) {
274 continue;
276 if (xendev->dev != dev && dev != -1) {
277 continue;
280 if (xendev->ops->free) {
281 xendev->ops->free(xendev);
284 if (xendev->fe) {
285 char token[XEN_BUFSIZE];
286 snprintf(token, sizeof(token), "fe:%p", xendev);
287 xs_unwatch(xenstore, xendev->fe, token);
288 g_free(xendev->fe);
291 if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
292 xc_evtchn_close(xendev->evtchndev);
294 if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
295 xc_gnttab_close(xendev->gnttabdev);
298 QTAILQ_REMOVE(&xendevs, xendev, next);
299 g_free(xendev);
301 return NULL;
305 * Sync internal data structures on xenstore updates.
306 * Node specifies the changed field. node = NULL means
307 * update all fields (used for initialization).
309 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
311 if (node == NULL || strcmp(node, "online") == 0) {
312 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
313 xendev->online = 0;
317 if (node) {
318 xen_be_printf(xendev, 2, "backend update: %s\n", node);
319 if (xendev->ops->backend_changed) {
320 xendev->ops->backend_changed(xendev, node);
325 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
327 int fe_state;
329 if (node == NULL || strcmp(node, "state") == 0) {
330 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
331 fe_state = XenbusStateUnknown;
333 if (xendev->fe_state != fe_state) {
334 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
335 xenbus_strstate(xendev->fe_state),
336 xenbus_strstate(fe_state));
338 xendev->fe_state = fe_state;
340 if (node == NULL || strcmp(node, "protocol") == 0) {
341 g_free(xendev->protocol);
342 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
343 if (xendev->protocol) {
344 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
348 if (node) {
349 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
350 if (xendev->ops->frontend_changed) {
351 xendev->ops->frontend_changed(xendev, node);
356 /* ------------------------------------------------------------- */
357 /* Check for possible state transitions and perform them. */
360 * Initial xendev setup. Read frontend path, register watch for it.
361 * Should succeed once xend finished setting up the backend device.
363 * Also sets initial state (-> Initializing) when done. Which
364 * only affects the xendev->be_state variable as xenbus should
365 * already be put into that state by xend.
367 static int xen_be_try_setup(struct XenDevice *xendev)
369 char token[XEN_BUFSIZE];
370 int be_state;
372 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
373 xen_be_printf(xendev, 0, "reading backend state failed\n");
374 return -1;
377 if (be_state != XenbusStateInitialising) {
378 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
379 xenbus_strstate(be_state));
380 return -1;
383 xendev->fe = xenstore_read_be_str(xendev, "frontend");
384 if (xendev->fe == NULL) {
385 xen_be_printf(xendev, 0, "reading frontend path failed\n");
386 return -1;
389 /* setup frontend watch */
390 snprintf(token, sizeof(token), "fe:%p", xendev);
391 if (!xs_watch(xenstore, xendev->fe, token)) {
392 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
393 xendev->fe);
394 return -1;
396 xen_be_set_state(xendev, XenbusStateInitialising);
398 xen_be_backend_changed(xendev, NULL);
399 xen_be_frontend_changed(xendev, NULL);
400 return 0;
404 * Try initialize xendev. Prepare everything the backend can do
405 * without synchronizing with the frontend. Fakes hotplug-status. No
406 * hotplug involved here because this is about userspace drivers, thus
407 * there are kernel backend devices which could invoke hotplug.
409 * Goes to InitWait on success.
411 static int xen_be_try_init(struct XenDevice *xendev)
413 int rc = 0;
415 if (!xendev->online) {
416 xen_be_printf(xendev, 1, "not online\n");
417 return -1;
420 if (xendev->ops->init) {
421 rc = xendev->ops->init(xendev);
423 if (rc != 0) {
424 xen_be_printf(xendev, 1, "init() failed\n");
425 return rc;
428 xenstore_write_be_str(xendev, "hotplug-status", "connected");
429 xen_be_set_state(xendev, XenbusStateInitWait);
430 return 0;
434 * Try to initialise xendev. Depends on the frontend being ready
435 * for it (shared ring and evtchn info in xenstore, state being
436 * Initialised or Connected).
438 * Goes to Connected on success.
440 static int xen_be_try_initialise(struct XenDevice *xendev)
442 int rc = 0;
444 if (xendev->fe_state != XenbusStateInitialised &&
445 xendev->fe_state != XenbusStateConnected) {
446 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
447 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
448 } else {
449 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
450 return -1;
454 if (xendev->ops->initialise) {
455 rc = xendev->ops->initialise(xendev);
457 if (rc != 0) {
458 xen_be_printf(xendev, 0, "initialise() failed\n");
459 return rc;
462 xen_be_set_state(xendev, XenbusStateConnected);
463 return 0;
467 * Try to let xendev know that it is connected. Depends on the
468 * frontend being Connected. Note that this may be called more
469 * than once since the backend state is not modified.
471 static void xen_be_try_connected(struct XenDevice *xendev)
473 if (!xendev->ops->connected) {
474 return;
477 if (xendev->fe_state != XenbusStateConnected) {
478 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
479 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
480 } else {
481 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
482 return;
486 xendev->ops->connected(xendev);
490 * Teardown connection.
492 * Goes to Closed when done.
494 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
496 if (xendev->be_state != XenbusStateClosing &&
497 xendev->be_state != XenbusStateClosed &&
498 xendev->ops->disconnect) {
499 xendev->ops->disconnect(xendev);
501 if (xendev->be_state != state) {
502 xen_be_set_state(xendev, state);
507 * Try to reset xendev, for reconnection by another frontend instance.
509 static int xen_be_try_reset(struct XenDevice *xendev)
511 if (xendev->fe_state != XenbusStateInitialising) {
512 return -1;
515 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
516 xen_be_set_state(xendev, XenbusStateInitialising);
517 return 0;
521 * state change dispatcher function
523 void xen_be_check_state(struct XenDevice *xendev)
525 int rc = 0;
527 /* frontend may request shutdown from almost anywhere */
528 if (xendev->fe_state == XenbusStateClosing ||
529 xendev->fe_state == XenbusStateClosed) {
530 xen_be_disconnect(xendev, xendev->fe_state);
531 return;
534 /* check for possible backend state transitions */
535 for (;;) {
536 switch (xendev->be_state) {
537 case XenbusStateUnknown:
538 rc = xen_be_try_setup(xendev);
539 break;
540 case XenbusStateInitialising:
541 rc = xen_be_try_init(xendev);
542 break;
543 case XenbusStateInitWait:
544 rc = xen_be_try_initialise(xendev);
545 break;
546 case XenbusStateConnected:
547 /* xendev->be_state doesn't change */
548 xen_be_try_connected(xendev);
549 rc = -1;
550 break;
551 case XenbusStateClosed:
552 rc = xen_be_try_reset(xendev);
553 break;
554 default:
555 rc = -1;
557 if (rc != 0) {
558 break;
563 /* ------------------------------------------------------------- */
565 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
567 struct XenDevice *xendev;
568 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
569 char **dev = NULL;
570 unsigned int cdev, j;
572 /* setup watch */
573 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
574 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
575 if (!xs_watch(xenstore, path, token)) {
576 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
577 return -1;
580 /* look for backends */
581 dev = xs_directory(xenstore, 0, path, &cdev);
582 if (!dev) {
583 return 0;
585 for (j = 0; j < cdev; j++) {
586 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
587 if (xendev == NULL) {
588 continue;
590 xen_be_check_state(xendev);
592 free(dev);
593 return 0;
596 static void xenstore_update_be(char *watch, char *type, int dom,
597 struct XenDevOps *ops)
599 struct XenDevice *xendev;
600 char path[XEN_BUFSIZE], *bepath;
601 unsigned int len, dev;
603 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
604 if (strncmp(path, watch, len) != 0) {
605 return;
607 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
608 strcpy(path, "");
609 if (sscanf(watch+len, "/%u", &dev) != 1) {
610 dev = -1;
613 if (dev == -1) {
614 return;
617 xendev = xen_be_get_xendev(type, dom, dev, ops);
618 if (xendev != NULL) {
619 bepath = xs_read(xenstore, 0, xendev->be, &len);
620 if (bepath == NULL) {
621 xen_be_del_xendev(dom, dev);
622 } else {
623 free(bepath);
624 xen_be_backend_changed(xendev, path);
625 xen_be_check_state(xendev);
630 static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
632 char *node;
633 unsigned int len;
635 len = strlen(xendev->fe);
636 if (strncmp(xendev->fe, watch, len) != 0) {
637 return;
639 if (watch[len] != '/') {
640 return;
642 node = watch + len + 1;
644 xen_be_frontend_changed(xendev, node);
645 xen_be_check_state(xendev);
648 static void xenstore_update(void *unused)
650 char **vec = NULL;
651 intptr_t type, ops, ptr;
652 unsigned int dom, count;
654 vec = xs_read_watch(xenstore, &count);
655 if (vec == NULL) {
656 goto cleanup;
659 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
660 &type, &dom, &ops) == 3) {
661 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
663 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
664 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
667 cleanup:
668 free(vec);
671 static void xen_be_evtchn_event(void *opaque)
673 struct XenDevice *xendev = opaque;
674 evtchn_port_t port;
676 port = xc_evtchn_pending(xendev->evtchndev);
677 if (port != xendev->local_port) {
678 xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
679 port, xendev->local_port);
680 return;
682 xc_evtchn_unmask(xendev->evtchndev, port);
684 if (xendev->ops->event) {
685 xendev->ops->event(xendev);
689 /* -------------------------------------------------------------------- */
691 int xen_be_init(void)
693 xenstore = xs_daemon_open();
694 if (!xenstore) {
695 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
696 return -1;
699 if (qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL) < 0) {
700 goto err;
703 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
704 /* Check if xen_init() have been called */
705 goto err;
707 return 0;
709 err:
710 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
711 xs_daemon_close(xenstore);
712 xenstore = NULL;
714 return -1;
717 int xen_be_register(const char *type, struct XenDevOps *ops)
719 return xenstore_scan(type, xen_domid, ops);
722 int xen_be_bind_evtchn(struct XenDevice *xendev)
724 if (xendev->local_port != -1) {
725 return 0;
727 xendev->local_port = xc_evtchn_bind_interdomain
728 (xendev->evtchndev, xendev->dom, xendev->remote_port);
729 if (xendev->local_port == -1) {
730 xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
731 return -1;
733 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
734 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
735 xen_be_evtchn_event, NULL, xendev);
736 return 0;
739 void xen_be_unbind_evtchn(struct XenDevice *xendev)
741 if (xendev->local_port == -1) {
742 return;
744 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
745 xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
746 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
747 xendev->local_port = -1;
750 int xen_be_send_notify(struct XenDevice *xendev)
752 return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
756 * msg_level:
757 * 0 == errors (stderr + logfile).
758 * 1 == informative debug messages (logfile only).
759 * 2 == noisy debug messages (logfile only).
760 * 3 == will flood your log (logfile only).
762 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
764 va_list args;
766 if (xendev) {
767 if (msg_level > xendev->debug) {
768 return;
770 qemu_log("xen be: %s: ", xendev->name);
771 if (msg_level == 0) {
772 fprintf(stderr, "xen be: %s: ", xendev->name);
774 } else {
775 if (msg_level > debug) {
776 return;
778 qemu_log("xen be core: ");
779 if (msg_level == 0) {
780 fprintf(stderr, "xen be core: ");
783 va_start(args, fmt);
784 qemu_log_vprintf(fmt, args);
785 va_end(args);
786 if (msg_level == 0) {
787 va_start(args, fmt);
788 vfprintf(stderr, fmt, args);
789 va_end(args);
791 qemu_log_flush();