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 "hw/boards.h"
31 #include "sysemu/char.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
;
43 /* ------------------------------------------------------------- */
46 xc_interface
*xen_xc
= NULL
;
47 xenforeignmemory_handle
*xen_fmem
= NULL
;
48 struct xs_handle
*xenstore
= NULL
;
49 const char *xen_protocol
;
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
,
92 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
95 /* ------------------------------------------------------------- */
97 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
101 rc
= xenstore_write_be_int(xendev
, "state", state
);
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
;
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
);
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
));
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
);
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
);
163 xendev
->gnttabdev
= NULL
;
166 xen_pv_insert_xendev(xendev
);
168 if (xendev
->ops
->alloc
) {
169 xendev
->ops
->alloc(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) {
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
)
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",
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
];
245 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
246 xen_pv_printf(xendev
, 0, "reading backend state failed\n");
250 if (be_state
!= XenbusStateInitialising
) {
251 xen_pv_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
252 xenbus_strstate(be_state
));
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");
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",
269 xen_be_set_state(xendev
, XenbusStateInitialising
);
271 xen_be_backend_changed(xendev
, NULL
);
272 xen_be_frontend_changed(xendev
, NULL
);
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
)
288 if (!xendev
->online
) {
289 xen_pv_printf(xendev
, 1, "not online\n");
293 if (xendev
->ops
->init
) {
294 rc
= xendev
->ops
->init(xendev
);
297 xen_pv_printf(xendev
, 1, "init() failed\n");
301 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
302 xen_be_set_state(xendev
, XenbusStateInitWait
);
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
)
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");
322 xen_pv_printf(xendev
, 2, "frontend not ready (yet)\n");
327 if (xendev
->ops
->initialise
) {
328 rc
= xendev
->ops
->initialise(xendev
);
331 xen_pv_printf(xendev
, 0, "initialise() failed\n");
335 xen_be_set_state(xendev
, XenbusStateConnected
);
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
) {
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");
354 xen_pv_printf(xendev
, 2, "frontend not ready (yet)\n");
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
) {
388 xen_pv_printf(xendev
, 1, "device reset (for re-connect)\n");
389 xen_be_set_state(xendev
, XenbusStateInitialising
);
394 * state change dispatcher function
396 void xen_be_check_state(struct XenDevice
*xendev
)
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
);
407 /* check for possible backend state transitions */
409 switch (xendev
->be_state
) {
410 case XenbusStateUnknown
:
411 rc
= xen_be_try_setup(xendev
);
413 case XenbusStateInitialising
:
414 rc
= xen_be_try_init(xendev
);
416 case XenbusStateInitWait
:
417 rc
= xen_be_try_initialise(xendev
);
419 case XenbusStateConnected
:
420 /* xendev->be_state doesn't change */
421 xen_be_try_connected(xendev
);
424 case XenbusStateClosed
:
425 rc
= xen_be_try_reset(xendev
);
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
];
443 unsigned int cdev
, j
;
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",
454 /* look for backends */
455 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
459 for (j
= 0; j
< cdev
; j
++) {
460 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
461 if (xendev
== NULL
) {
464 xen_be_check_state(xendev
);
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) {
481 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
483 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
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
);
498 xen_be_backend_changed(xendev
, path
);
499 xen_be_check_state(xendev
);
504 void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
509 len
= strlen(xendev
->fe
);
510 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
513 if (watch
[len
] != '/') {
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();
527 xen_pv_printf(NULL
, 0, "can't connect to xenstored\n");
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 */
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
);
546 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
547 xs_daemon_close(xenstore
);
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
)
567 if (ops
->backend_register
) {
568 rc
= ops
->backend_register();
574 snprintf(path
, sizeof(path
), "device-model/%u/backends/%s", xen_domid
,
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
);
593 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
595 if (xendev
->local_port
!= -1) {
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");
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
);
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
,
640 .class_init
= xen_sysbus_class_init
,
641 .interfaces
= (InterfaceInfo
[]) {
642 { TYPE_HOTPLUG_HANDLER
},
647 static int xen_sysdev_init(SysBusDevice
*dev
)
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
)