Add bios-256k.bin to BLOBS on Makefile
[qemu/ar7.git] / hw / xen / xen_backend.c
blob197795ffe15a6552bae106cc46f5bdf8eb592461
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 XenGnttab xen_xcg = XC_HANDLER_INITIAL_VALUE;
49 struct xs_handle *xenstore = NULL;
50 const char *xen_protocol;
52 /* private */
53 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
54 static int debug = 0;
56 /* ------------------------------------------------------------- */
58 int xenstore_write_str(const char *base, const char *node, const char *val)
60 char abspath[XEN_BUFSIZE];
62 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
63 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
64 return -1;
66 return 0;
69 char *xenstore_read_str(const char *base, const char *node)
71 char abspath[XEN_BUFSIZE];
72 unsigned int len;
73 char *str, *ret = NULL;
75 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
76 str = xs_read(xenstore, 0, abspath, &len);
77 if (str != NULL) {
78 /* move to qemu-allocated memory to make sure
79 * callers can savely g_free() stuff. */
80 ret = g_strdup(str);
81 free(str);
83 return ret;
86 int xenstore_write_int(const char *base, const char *node, int ival)
88 char val[12];
90 snprintf(val, sizeof(val), "%d", ival);
91 return xenstore_write_str(base, node, val);
94 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
96 char val[21];
98 snprintf(val, sizeof(val), "%"PRId64, ival);
99 return xenstore_write_str(base, node, val);
102 int xenstore_read_int(const char *base, const char *node, int *ival)
104 char *val;
105 int rc = -1;
107 val = xenstore_read_str(base, node);
108 if (val && 1 == sscanf(val, "%d", ival)) {
109 rc = 0;
111 g_free(val);
112 return rc;
115 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
117 return xenstore_write_str(xendev->be, node, val);
120 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
122 return xenstore_write_int(xendev->be, node, ival);
125 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
127 return xenstore_write_int64(xendev->be, node, ival);
130 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
132 return xenstore_read_str(xendev->be, node);
135 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
137 return xenstore_read_int(xendev->be, node, ival);
140 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
142 return xenstore_read_str(xendev->fe, node);
145 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
147 return xenstore_read_int(xendev->fe, node, ival);
150 /* ------------------------------------------------------------- */
152 const char *xenbus_strstate(enum xenbus_state state)
154 static const char *const name[] = {
155 [ XenbusStateUnknown ] = "Unknown",
156 [ XenbusStateInitialising ] = "Initialising",
157 [ XenbusStateInitWait ] = "InitWait",
158 [ XenbusStateInitialised ] = "Initialised",
159 [ XenbusStateConnected ] = "Connected",
160 [ XenbusStateClosing ] = "Closing",
161 [ XenbusStateClosed ] = "Closed",
163 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
166 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
168 int rc;
170 rc = xenstore_write_be_int(xendev, "state", state);
171 if (rc < 0) {
172 return rc;
174 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
175 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
176 xendev->be_state = state;
177 return 0;
180 /* ------------------------------------------------------------- */
182 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
184 struct XenDevice *xendev;
186 QTAILQ_FOREACH(xendev, &xendevs, next) {
187 if (xendev->dom != dom) {
188 continue;
190 if (xendev->dev != dev) {
191 continue;
193 if (strcmp(xendev->type, type) != 0) {
194 continue;
196 return xendev;
198 return NULL;
202 * get xen backend device, allocate a new one if it doesn't exist.
204 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
205 struct XenDevOps *ops)
207 struct XenDevice *xendev;
209 xendev = xen_be_find_xendev(type, dom, dev);
210 if (xendev) {
211 return xendev;
214 /* init new xendev */
215 xendev = g_malloc0(ops->size);
216 xendev->type = type;
217 xendev->dom = dom;
218 xendev->dev = dev;
219 xendev->ops = ops;
221 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
222 xendev->type, xendev->dom, xendev->dev);
223 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
224 xendev->type, xendev->dev);
226 xendev->debug = debug;
227 xendev->local_port = -1;
229 xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
230 if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
231 xen_be_printf(NULL, 0, "can't open evtchn device\n");
232 g_free(xendev);
233 return NULL;
235 fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
237 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
238 xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
239 if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
240 xen_be_printf(NULL, 0, "can't open gnttab device\n");
241 xc_evtchn_close(xendev->evtchndev);
242 g_free(xendev);
243 return NULL;
245 } else {
246 xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
249 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
251 if (xendev->ops->alloc) {
252 xendev->ops->alloc(xendev);
255 return xendev;
259 * release xen backend device.
261 static struct XenDevice *xen_be_del_xendev(int dom, int dev)
263 struct XenDevice *xendev, *xnext;
266 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
267 * we save the next pointer in xnext because we might free xendev.
269 xnext = xendevs.tqh_first;
270 while (xnext) {
271 xendev = xnext;
272 xnext = xendev->next.tqe_next;
274 if (xendev->dom != dom) {
275 continue;
277 if (xendev->dev != dev && dev != -1) {
278 continue;
281 if (xendev->ops->free) {
282 xendev->ops->free(xendev);
285 if (xendev->fe) {
286 char token[XEN_BUFSIZE];
287 snprintf(token, sizeof(token), "fe:%p", xendev);
288 xs_unwatch(xenstore, xendev->fe, token);
289 g_free(xendev->fe);
292 if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
293 xc_evtchn_close(xendev->evtchndev);
295 if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
296 xc_gnttab_close(xendev->gnttabdev);
299 QTAILQ_REMOVE(&xendevs, xendev, next);
300 g_free(xendev);
302 return NULL;
306 * Sync internal data structures on xenstore updates.
307 * Node specifies the changed field. node = NULL means
308 * update all fields (used for initialization).
310 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
312 if (node == NULL || strcmp(node, "online") == 0) {
313 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
314 xendev->online = 0;
318 if (node) {
319 xen_be_printf(xendev, 2, "backend update: %s\n", node);
320 if (xendev->ops->backend_changed) {
321 xendev->ops->backend_changed(xendev, node);
326 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
328 int fe_state;
330 if (node == NULL || strcmp(node, "state") == 0) {
331 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
332 fe_state = XenbusStateUnknown;
334 if (xendev->fe_state != fe_state) {
335 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
336 xenbus_strstate(xendev->fe_state),
337 xenbus_strstate(fe_state));
339 xendev->fe_state = fe_state;
341 if (node == NULL || strcmp(node, "protocol") == 0) {
342 g_free(xendev->protocol);
343 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
344 if (xendev->protocol) {
345 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
349 if (node) {
350 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
351 if (xendev->ops->frontend_changed) {
352 xendev->ops->frontend_changed(xendev, node);
357 /* ------------------------------------------------------------- */
358 /* Check for possible state transitions and perform them. */
361 * Initial xendev setup. Read frontend path, register watch for it.
362 * Should succeed once xend finished setting up the backend device.
364 * Also sets initial state (-> Initializing) when done. Which
365 * only affects the xendev->be_state variable as xenbus should
366 * already be put into that state by xend.
368 static int xen_be_try_setup(struct XenDevice *xendev)
370 char token[XEN_BUFSIZE];
371 int be_state;
373 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
374 xen_be_printf(xendev, 0, "reading backend state failed\n");
375 return -1;
378 if (be_state != XenbusStateInitialising) {
379 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
380 xenbus_strstate(be_state));
381 return -1;
384 xendev->fe = xenstore_read_be_str(xendev, "frontend");
385 if (xendev->fe == NULL) {
386 xen_be_printf(xendev, 0, "reading frontend path failed\n");
387 return -1;
390 /* setup frontend watch */
391 snprintf(token, sizeof(token), "fe:%p", xendev);
392 if (!xs_watch(xenstore, xendev->fe, token)) {
393 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
394 xendev->fe);
395 return -1;
397 xen_be_set_state(xendev, XenbusStateInitialising);
399 xen_be_backend_changed(xendev, NULL);
400 xen_be_frontend_changed(xendev, NULL);
401 return 0;
405 * Try initialize xendev. Prepare everything the backend can do
406 * without synchronizing with the frontend. Fakes hotplug-status. No
407 * hotplug involved here because this is about userspace drivers, thus
408 * there are kernel backend devices which could invoke hotplug.
410 * Goes to InitWait on success.
412 static int xen_be_try_init(struct XenDevice *xendev)
414 int rc = 0;
416 if (!xendev->online) {
417 xen_be_printf(xendev, 1, "not online\n");
418 return -1;
421 if (xendev->ops->init) {
422 rc = xendev->ops->init(xendev);
424 if (rc != 0) {
425 xen_be_printf(xendev, 1, "init() failed\n");
426 return rc;
429 xenstore_write_be_str(xendev, "hotplug-status", "connected");
430 xen_be_set_state(xendev, XenbusStateInitWait);
431 return 0;
435 * Try to initialise xendev. Depends on the frontend being ready
436 * for it (shared ring and evtchn info in xenstore, state being
437 * Initialised or Connected).
439 * Goes to Connected on success.
441 static int xen_be_try_initialise(struct XenDevice *xendev)
443 int rc = 0;
445 if (xendev->fe_state != XenbusStateInitialised &&
446 xendev->fe_state != XenbusStateConnected) {
447 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
448 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
449 } else {
450 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
451 return -1;
455 if (xendev->ops->initialise) {
456 rc = xendev->ops->initialise(xendev);
458 if (rc != 0) {
459 xen_be_printf(xendev, 0, "initialise() failed\n");
460 return rc;
463 xen_be_set_state(xendev, XenbusStateConnected);
464 return 0;
468 * Try to let xendev know that it is connected. Depends on the
469 * frontend being Connected. Note that this may be called more
470 * than once since the backend state is not modified.
472 static void xen_be_try_connected(struct XenDevice *xendev)
474 if (!xendev->ops->connected) {
475 return;
478 if (xendev->fe_state != XenbusStateConnected) {
479 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
480 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
481 } else {
482 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
483 return;
487 xendev->ops->connected(xendev);
491 * Teardown connection.
493 * Goes to Closed when done.
495 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
497 if (xendev->be_state != XenbusStateClosing &&
498 xendev->be_state != XenbusStateClosed &&
499 xendev->ops->disconnect) {
500 xendev->ops->disconnect(xendev);
502 if (xendev->be_state != state) {
503 xen_be_set_state(xendev, state);
508 * Try to reset xendev, for reconnection by another frontend instance.
510 static int xen_be_try_reset(struct XenDevice *xendev)
512 if (xendev->fe_state != XenbusStateInitialising) {
513 return -1;
516 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
517 xen_be_set_state(xendev, XenbusStateInitialising);
518 return 0;
522 * state change dispatcher function
524 void xen_be_check_state(struct XenDevice *xendev)
526 int rc = 0;
528 /* frontend may request shutdown from almost anywhere */
529 if (xendev->fe_state == XenbusStateClosing ||
530 xendev->fe_state == XenbusStateClosed) {
531 xen_be_disconnect(xendev, xendev->fe_state);
532 return;
535 /* check for possible backend state transitions */
536 for (;;) {
537 switch (xendev->be_state) {
538 case XenbusStateUnknown:
539 rc = xen_be_try_setup(xendev);
540 break;
541 case XenbusStateInitialising:
542 rc = xen_be_try_init(xendev);
543 break;
544 case XenbusStateInitWait:
545 rc = xen_be_try_initialise(xendev);
546 break;
547 case XenbusStateConnected:
548 /* xendev->be_state doesn't change */
549 xen_be_try_connected(xendev);
550 rc = -1;
551 break;
552 case XenbusStateClosed:
553 rc = xen_be_try_reset(xendev);
554 break;
555 default:
556 rc = -1;
558 if (rc != 0) {
559 break;
564 /* ------------------------------------------------------------- */
566 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
568 struct XenDevice *xendev;
569 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
570 char **dev = NULL;
571 unsigned int cdev, j;
573 /* setup watch */
574 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
575 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
576 if (!xs_watch(xenstore, path, token)) {
577 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
578 return -1;
581 /* look for backends */
582 dev = xs_directory(xenstore, 0, path, &cdev);
583 if (!dev) {
584 return 0;
586 for (j = 0; j < cdev; j++) {
587 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
588 if (xendev == NULL) {
589 continue;
591 xen_be_check_state(xendev);
593 free(dev);
594 return 0;
597 static void xenstore_update_be(char *watch, char *type, int dom,
598 struct XenDevOps *ops)
600 struct XenDevice *xendev;
601 char path[XEN_BUFSIZE], *bepath;
602 unsigned int len, dev;
604 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
605 if (strncmp(path, watch, len) != 0) {
606 return;
608 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
609 strcpy(path, "");
610 if (sscanf(watch+len, "/%u", &dev) != 1) {
611 dev = -1;
614 if (dev == -1) {
615 return;
618 xendev = xen_be_get_xendev(type, dom, dev, ops);
619 if (xendev != NULL) {
620 bepath = xs_read(xenstore, 0, xendev->be, &len);
621 if (bepath == NULL) {
622 xen_be_del_xendev(dom, dev);
623 } else {
624 free(bepath);
625 xen_be_backend_changed(xendev, path);
626 xen_be_check_state(xendev);
631 static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
633 char *node;
634 unsigned int len;
636 len = strlen(xendev->fe);
637 if (strncmp(xendev->fe, watch, len) != 0) {
638 return;
640 if (watch[len] != '/') {
641 return;
643 node = watch + len + 1;
645 xen_be_frontend_changed(xendev, node);
646 xen_be_check_state(xendev);
649 static void xenstore_update(void *unused)
651 char **vec = NULL;
652 intptr_t type, ops, ptr;
653 unsigned int dom, count;
655 vec = xs_read_watch(xenstore, &count);
656 if (vec == NULL) {
657 goto cleanup;
660 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
661 &type, &dom, &ops) == 3) {
662 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
664 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
665 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
668 cleanup:
669 free(vec);
672 static void xen_be_evtchn_event(void *opaque)
674 struct XenDevice *xendev = opaque;
675 evtchn_port_t port;
677 port = xc_evtchn_pending(xendev->evtchndev);
678 if (port != xendev->local_port) {
679 xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
680 port, xendev->local_port);
681 return;
683 xc_evtchn_unmask(xendev->evtchndev, port);
685 if (xendev->ops->event) {
686 xendev->ops->event(xendev);
690 /* -------------------------------------------------------------------- */
692 int xen_be_init(void)
694 xenstore = xs_daemon_open();
695 if (!xenstore) {
696 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
697 return -1;
700 if (qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL) < 0) {
701 goto err;
704 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
705 /* Check if xen_init() have been called */
706 goto err;
708 return 0;
710 err:
711 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
712 xs_daemon_close(xenstore);
713 xenstore = NULL;
715 return -1;
718 int xen_be_register(const char *type, struct XenDevOps *ops)
720 return xenstore_scan(type, xen_domid, ops);
723 int xen_be_bind_evtchn(struct XenDevice *xendev)
725 if (xendev->local_port != -1) {
726 return 0;
728 xendev->local_port = xc_evtchn_bind_interdomain
729 (xendev->evtchndev, xendev->dom, xendev->remote_port);
730 if (xendev->local_port == -1) {
731 xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
732 return -1;
734 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
735 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
736 xen_be_evtchn_event, NULL, xendev);
737 return 0;
740 void xen_be_unbind_evtchn(struct XenDevice *xendev)
742 if (xendev->local_port == -1) {
743 return;
745 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
746 xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
747 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
748 xendev->local_port = -1;
751 int xen_be_send_notify(struct XenDevice *xendev)
753 return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
757 * msg_level:
758 * 0 == errors (stderr + logfile).
759 * 1 == informative debug messages (logfile only).
760 * 2 == noisy debug messages (logfile only).
761 * 3 == will flood your log (logfile only).
763 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
765 va_list args;
767 if (xendev) {
768 if (msg_level > xendev->debug) {
769 return;
771 qemu_log("xen be: %s: ", xendev->name);
772 if (msg_level == 0) {
773 fprintf(stderr, "xen be: %s: ", xendev->name);
775 } else {
776 if (msg_level > debug) {
777 return;
779 qemu_log("xen be core: ");
780 if (msg_level == 0) {
781 fprintf(stderr, "xen be core: ");
784 va_start(args, fmt);
785 qemu_log_vprintf(fmt, args);
786 va_end(args);
787 if (msg_level == 0) {
788 va_start(args, fmt);
789 vfprintf(stderr, fmt, args);
790 va_end(args);
792 qemu_log_flush();