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>
29 #include "hw/sysbus.h"
30 #include "sysemu/char.h"
32 #include "hw/xen/xen_backend.h"
33 #include "hw/xen/xen_pvdev.h"
35 #include <xen/grant_table.h>
37 #define TYPE_XENSYSDEV "xensysdev"
39 DeviceState
*xen_sysdev
;
41 /* ------------------------------------------------------------- */
44 xc_interface
*xen_xc
= NULL
;
45 xenforeignmemory_handle
*xen_fmem
= NULL
;
46 struct xs_handle
*xenstore
= NULL
;
47 const char *xen_protocol
;
52 QTAILQ_ENTRY(xs_dirs
) list
;
54 static QTAILQ_HEAD(xs_dirs_head
, xs_dirs
) xs_cleanup
=
55 QTAILQ_HEAD_INITIALIZER(xs_cleanup
);
57 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
=
58 QTAILQ_HEAD_INITIALIZER(xendevs
);
61 static void xenstore_cleanup_dir(char *dir
)
65 d
= g_malloc(sizeof(*d
));
67 QTAILQ_INSERT_TAIL(&xs_cleanup
, d
, list
);
70 void xen_config_cleanup(void)
74 QTAILQ_FOREACH(d
, &xs_cleanup
, list
) {
75 xs_rm(xenstore
, 0, d
->xs_dir
);
79 int xenstore_mkdir(char *path
, int p
)
81 struct xs_permissions perms
[2] = {
83 .id
= 0, /* set owner: dom0 */
90 if (!xs_mkdir(xenstore
, 0, path
)) {
91 xen_be_printf(NULL
, 0, "xs_mkdir %s: failed\n", path
);
94 xenstore_cleanup_dir(g_strdup(path
));
96 if (!xs_set_permissions(xenstore
, 0, path
, perms
, 2)) {
97 xen_be_printf(NULL
, 0, "xs_set_permissions %s: failed\n", path
);
103 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
105 return xenstore_write_str(xendev
->be
, node
, val
);
108 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
110 return xenstore_write_int(xendev
->be
, node
, ival
);
113 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
115 return xenstore_write_int64(xendev
->be
, node
, ival
);
118 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
120 return xenstore_read_str(xendev
->be
, node
);
123 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
125 return xenstore_read_int(xendev
->be
, node
, ival
);
128 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
130 return xenstore_read_str(xendev
->fe
, node
);
133 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
135 return xenstore_read_int(xendev
->fe
, node
, ival
);
138 int xenstore_read_fe_uint64(struct XenDevice
*xendev
, const char *node
,
141 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
144 /* ------------------------------------------------------------- */
146 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
150 rc
= xenstore_write_be_int(xendev
, "state", state
);
154 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
155 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
156 xendev
->be_state
= state
;
160 /* ------------------------------------------------------------- */
162 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
164 struct XenDevice
*xendev
;
166 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
167 if (xendev
->dom
!= dom
) {
170 if (xendev
->dev
!= dev
) {
173 if (strcmp(xendev
->type
, type
) != 0) {
182 * get xen backend device, allocate a new one if it doesn't exist.
184 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
185 struct XenDevOps
*ops
)
187 struct XenDevice
*xendev
;
189 xendev
= xen_be_find_xendev(type
, dom
, dev
);
194 /* init new xendev */
195 xendev
= g_malloc0(ops
->size
);
201 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
202 xendev
->type
, xendev
->dom
, xendev
->dev
);
203 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
204 xendev
->type
, xendev
->dev
);
206 xendev
->debug
= debug
;
207 xendev
->local_port
= -1;
209 xendev
->evtchndev
= xenevtchn_open(NULL
, 0);
210 if (xendev
->evtchndev
== NULL
) {
211 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
215 fcntl(xenevtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
217 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
218 xendev
->gnttabdev
= xengnttab_open(NULL
, 0);
219 if (xendev
->gnttabdev
== NULL
) {
220 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
221 xenevtchn_close(xendev
->evtchndev
);
226 xendev
->gnttabdev
= NULL
;
229 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
231 if (xendev
->ops
->alloc
) {
232 xendev
->ops
->alloc(xendev
);
239 * release xen backend device.
241 static void xen_be_del_xendev(struct XenDevice
*xendev
)
243 if (xendev
->ops
->free
) {
244 xendev
->ops
->free(xendev
);
248 char token
[XEN_BUFSIZE
];
249 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
250 xs_unwatch(xenstore
, xendev
->fe
, token
);
254 if (xendev
->evtchndev
!= NULL
) {
255 xenevtchn_close(xendev
->evtchndev
);
257 if (xendev
->gnttabdev
!= NULL
) {
258 xengnttab_close(xendev
->gnttabdev
);
261 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
266 * Sync internal data structures on xenstore updates.
267 * Node specifies the changed field. node = NULL means
268 * update all fields (used for initialization).
270 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
272 if (node
== NULL
|| strcmp(node
, "online") == 0) {
273 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
279 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
280 if (xendev
->ops
->backend_changed
) {
281 xendev
->ops
->backend_changed(xendev
, node
);
286 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
290 if (node
== NULL
|| strcmp(node
, "state") == 0) {
291 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
292 fe_state
= XenbusStateUnknown
;
294 if (xendev
->fe_state
!= fe_state
) {
295 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
296 xenbus_strstate(xendev
->fe_state
),
297 xenbus_strstate(fe_state
));
299 xendev
->fe_state
= fe_state
;
301 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
302 g_free(xendev
->protocol
);
303 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
304 if (xendev
->protocol
) {
305 xen_be_printf(xendev
, 1, "frontend protocol: %s\n",
311 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
312 if (xendev
->ops
->frontend_changed
) {
313 xendev
->ops
->frontend_changed(xendev
, node
);
318 /* ------------------------------------------------------------- */
319 /* Check for possible state transitions and perform them. */
322 * Initial xendev setup. Read frontend path, register watch for it.
323 * Should succeed once xend finished setting up the backend device.
325 * Also sets initial state (-> Initializing) when done. Which
326 * only affects the xendev->be_state variable as xenbus should
327 * already be put into that state by xend.
329 static int xen_be_try_setup(struct XenDevice
*xendev
)
331 char token
[XEN_BUFSIZE
];
334 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
335 xen_be_printf(xendev
, 0, "reading backend state failed\n");
339 if (be_state
!= XenbusStateInitialising
) {
340 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
341 xenbus_strstate(be_state
));
345 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
346 if (xendev
->fe
== NULL
) {
347 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
351 /* setup frontend watch */
352 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
353 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
354 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
358 xen_be_set_state(xendev
, XenbusStateInitialising
);
360 xen_be_backend_changed(xendev
, NULL
);
361 xen_be_frontend_changed(xendev
, NULL
);
366 * Try initialize xendev. Prepare everything the backend can do
367 * without synchronizing with the frontend. Fakes hotplug-status. No
368 * hotplug involved here because this is about userspace drivers, thus
369 * there are kernel backend devices which could invoke hotplug.
371 * Goes to InitWait on success.
373 static int xen_be_try_init(struct XenDevice
*xendev
)
377 if (!xendev
->online
) {
378 xen_be_printf(xendev
, 1, "not online\n");
382 if (xendev
->ops
->init
) {
383 rc
= xendev
->ops
->init(xendev
);
386 xen_be_printf(xendev
, 1, "init() failed\n");
390 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
391 xen_be_set_state(xendev
, XenbusStateInitWait
);
396 * Try to initialise xendev. Depends on the frontend being ready
397 * for it (shared ring and evtchn info in xenstore, state being
398 * Initialised or Connected).
400 * Goes to Connected on success.
402 static int xen_be_try_initialise(struct XenDevice
*xendev
)
406 if (xendev
->fe_state
!= XenbusStateInitialised
&&
407 xendev
->fe_state
!= XenbusStateConnected
) {
408 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
409 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
411 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
416 if (xendev
->ops
->initialise
) {
417 rc
= xendev
->ops
->initialise(xendev
);
420 xen_be_printf(xendev
, 0, "initialise() failed\n");
424 xen_be_set_state(xendev
, XenbusStateConnected
);
429 * Try to let xendev know that it is connected. Depends on the
430 * frontend being Connected. Note that this may be called more
431 * than once since the backend state is not modified.
433 static void xen_be_try_connected(struct XenDevice
*xendev
)
435 if (!xendev
->ops
->connected
) {
439 if (xendev
->fe_state
!= XenbusStateConnected
) {
440 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
441 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
443 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
448 xendev
->ops
->connected(xendev
);
452 * Teardown connection.
454 * Goes to Closed when done.
456 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
458 if (xendev
->be_state
!= XenbusStateClosing
&&
459 xendev
->be_state
!= XenbusStateClosed
&&
460 xendev
->ops
->disconnect
) {
461 xendev
->ops
->disconnect(xendev
);
463 if (xendev
->be_state
!= state
) {
464 xen_be_set_state(xendev
, state
);
469 * Try to reset xendev, for reconnection by another frontend instance.
471 static int xen_be_try_reset(struct XenDevice
*xendev
)
473 if (xendev
->fe_state
!= XenbusStateInitialising
) {
477 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
478 xen_be_set_state(xendev
, XenbusStateInitialising
);
483 * state change dispatcher function
485 void xen_be_check_state(struct XenDevice
*xendev
)
489 /* frontend may request shutdown from almost anywhere */
490 if (xendev
->fe_state
== XenbusStateClosing
||
491 xendev
->fe_state
== XenbusStateClosed
) {
492 xen_be_disconnect(xendev
, xendev
->fe_state
);
496 /* check for possible backend state transitions */
498 switch (xendev
->be_state
) {
499 case XenbusStateUnknown
:
500 rc
= xen_be_try_setup(xendev
);
502 case XenbusStateInitialising
:
503 rc
= xen_be_try_init(xendev
);
505 case XenbusStateInitWait
:
506 rc
= xen_be_try_initialise(xendev
);
508 case XenbusStateConnected
:
509 /* xendev->be_state doesn't change */
510 xen_be_try_connected(xendev
);
513 case XenbusStateClosed
:
514 rc
= xen_be_try_reset(xendev
);
525 /* ------------------------------------------------------------- */
527 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
529 struct XenDevice
*xendev
;
530 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
532 unsigned int cdev
, j
;
535 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
536 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
537 if (!xs_watch(xenstore
, path
, token
)) {
538 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n",
543 /* look for backends */
544 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
548 for (j
= 0; j
< cdev
; j
++) {
549 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
550 if (xendev
== NULL
) {
553 xen_be_check_state(xendev
);
559 static void xenstore_update_be(char *watch
, char *type
, int dom
,
560 struct XenDevOps
*ops
)
562 struct XenDevice
*xendev
;
563 char path
[XEN_BUFSIZE
], *bepath
;
564 unsigned int len
, dev
;
566 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
567 if (strncmp(path
, watch
, len
) != 0) {
570 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
572 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
580 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
581 if (xendev
!= NULL
) {
582 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
583 if (bepath
== NULL
) {
584 xen_be_del_xendev(xendev
);
587 xen_be_backend_changed(xendev
, path
);
588 xen_be_check_state(xendev
);
593 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
598 len
= strlen(xendev
->fe
);
599 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
602 if (watch
[len
] != '/') {
605 node
= watch
+ len
+ 1;
607 xen_be_frontend_changed(xendev
, node
);
608 xen_be_check_state(xendev
);
611 static void xenstore_update(void *unused
)
614 intptr_t type
, ops
, ptr
;
615 unsigned int dom
, count
;
617 vec
= xs_read_watch(xenstore
, &count
);
622 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
623 &type
, &dom
, &ops
) == 3) {
624 xenstore_update_be(vec
[XS_WATCH_PATH
], (void *)type
, dom
, (void*)ops
);
626 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
627 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void *)ptr
);
634 static void xen_be_evtchn_event(void *opaque
)
636 struct XenDevice
*xendev
= opaque
;
639 port
= xenevtchn_pending(xendev
->evtchndev
);
640 if (port
!= xendev
->local_port
) {
641 xen_be_printf(xendev
, 0,
642 "xenevtchn_pending returned %d (expected %d)\n",
643 port
, xendev
->local_port
);
646 xenevtchn_unmask(xendev
->evtchndev
, port
);
648 if (xendev
->ops
->event
) {
649 xendev
->ops
->event(xendev
);
653 /* -------------------------------------------------------------------- */
655 int xen_be_init(void)
657 xenstore
= xs_daemon_open();
659 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
663 qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
);
665 if (xen_xc
== NULL
|| xen_fmem
== NULL
) {
666 /* Check if xen_init() have been called */
670 xen_sysdev
= qdev_create(NULL
, TYPE_XENSYSDEV
);
671 qdev_init_nofail(xen_sysdev
);
676 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
677 xs_daemon_close(xenstore
);
683 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
688 if (ops
->backend_register
) {
689 rc
= ops
->backend_register();
695 snprintf(path
, sizeof(path
), "device-model/%u/backends/%s", xen_domid
,
697 xenstore_mkdir(path
, XS_PERM_NONE
);
699 return xenstore_scan(type
, xen_domid
, ops
);
702 void xen_be_register_common(void)
704 xen_be_register("console", &xen_console_ops
);
705 xen_be_register("vkbd", &xen_kbdmouse_ops
);
706 xen_be_register("qdisk", &xen_blkdev_ops
);
707 #ifdef CONFIG_USB_LIBUSB
708 xen_be_register("qusb", &xen_usb_ops
);
712 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
714 if (xendev
->local_port
!= -1) {
717 xendev
->local_port
= xenevtchn_bind_interdomain
718 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
719 if (xendev
->local_port
== -1) {
720 xen_be_printf(xendev
, 0, "xenevtchn_bind_interdomain failed\n");
723 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
724 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
),
725 xen_be_evtchn_event
, NULL
, xendev
);
729 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
731 if (xendev
->local_port
== -1) {
734 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
735 xenevtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
736 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
737 xendev
->local_port
= -1;
740 int xen_be_send_notify(struct XenDevice
*xendev
)
742 return xenevtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
746 static int xen_sysdev_init(SysBusDevice
*dev
)
751 static Property xen_sysdev_properties
[] = {
752 {/* end of property list */},
755 static void xen_sysdev_class_init(ObjectClass
*klass
, void *data
)
757 DeviceClass
*dc
= DEVICE_CLASS(klass
);
758 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
760 k
->init
= xen_sysdev_init
;
761 dc
->props
= xen_sysdev_properties
;
764 static const TypeInfo xensysdev_info
= {
765 .name
= TYPE_XENSYSDEV
,
766 .parent
= TYPE_SYS_BUS_DEVICE
,
767 .instance_size
= sizeof(SysBusDevice
),
768 .class_init
= xen_sysdev_class_init
,
771 static void xenbe_register_types(void)
773 type_register_static(&xensysdev_info
);
776 type_init(xenbe_register_types
);