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"
32 #include "qapi/error.h"
33 #include "hw/xen/xen_backend.h"
34 #include "hw/xen/xen_pvdev.h"
35 #include "monitor/qdev.h"
37 #include <xen/grant_table.h>
39 DeviceState
*xen_sysdev
;
42 /* ------------------------------------------------------------- */
45 struct xs_handle
*xenstore
= NULL
;
46 const char *xen_protocol
;
47 bool xen_feature_grant_copy
;
52 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
54 return xenstore_write_str(xendev
->be
, node
, val
);
57 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
59 return xenstore_write_int(xendev
->be
, node
, ival
);
62 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
64 return xenstore_write_int64(xendev
->be
, node
, ival
);
67 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
69 return xenstore_read_str(xendev
->be
, node
);
72 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
74 return xenstore_read_int(xendev
->be
, node
, ival
);
77 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
79 return xenstore_read_str(xendev
->fe
, node
);
82 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
84 return xenstore_read_int(xendev
->fe
, node
, ival
);
87 int xenstore_read_fe_uint64(struct XenDevice
*xendev
, const char *node
,
90 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
93 /* ------------------------------------------------------------- */
95 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
99 rc
= xenstore_write_be_int(xendev
, "state", state
);
103 xen_pv_printf(xendev
, 1, "backend state: %s -> %s\n",
104 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
105 xendev
->be_state
= state
;
110 * get xen backend device, allocate a new one if it doesn't exist.
112 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
113 struct XenDevOps
*ops
)
115 struct XenDevice
*xendev
;
117 xendev
= xen_pv_find_xendev(type
, dom
, dev
);
122 /* init new xendev */
123 xendev
= g_malloc0(ops
->size
);
124 object_initialize(&xendev
->qdev
, ops
->size
, TYPE_XENBACKEND
);
125 OBJECT(xendev
)->free
= g_free
;
126 qdev_set_parent_bus(DEVICE(xendev
), xen_sysbus
);
127 qdev_set_id(DEVICE(xendev
), g_strdup_printf("xen-%s-%d", type
, dev
));
128 qdev_init_nofail(DEVICE(xendev
));
129 object_unref(OBJECT(xendev
));
136 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
137 xendev
->type
, xendev
->dom
, xendev
->dev
);
138 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
139 xendev
->type
, xendev
->dev
);
141 xendev
->debug
= debug
;
142 xendev
->local_port
= -1;
144 xendev
->evtchndev
= xenevtchn_open(NULL
, 0);
145 if (xendev
->evtchndev
== NULL
) {
146 xen_pv_printf(NULL
, 0, "can't open evtchn device\n");
147 qdev_unplug(DEVICE(xendev
), NULL
);
150 qemu_set_cloexec(xenevtchn_fd(xendev
->evtchndev
));
152 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
153 xendev
->gnttabdev
= xengnttab_open(NULL
, 0);
154 if (xendev
->gnttabdev
== NULL
) {
155 xen_pv_printf(NULL
, 0, "can't open gnttab device\n");
156 xenevtchn_close(xendev
->evtchndev
);
157 qdev_unplug(DEVICE(xendev
), NULL
);
161 xendev
->gnttabdev
= NULL
;
164 xen_pv_insert_xendev(xendev
);
166 if (xendev
->ops
->alloc
) {
167 xendev
->ops
->alloc(xendev
);
175 * Sync internal data structures on xenstore updates.
176 * Node specifies the changed field. node = NULL means
177 * update all fields (used for initialization).
179 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
181 if (node
== NULL
|| strcmp(node
, "online") == 0) {
182 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
188 xen_pv_printf(xendev
, 2, "backend update: %s\n", node
);
189 if (xendev
->ops
->backend_changed
) {
190 xendev
->ops
->backend_changed(xendev
, node
);
195 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
199 if (node
== NULL
|| strcmp(node
, "state") == 0) {
200 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
201 fe_state
= XenbusStateUnknown
;
203 if (xendev
->fe_state
!= fe_state
) {
204 xen_pv_printf(xendev
, 1, "frontend state: %s -> %s\n",
205 xenbus_strstate(xendev
->fe_state
),
206 xenbus_strstate(fe_state
));
208 xendev
->fe_state
= fe_state
;
210 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
211 g_free(xendev
->protocol
);
212 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
213 if (xendev
->protocol
) {
214 xen_pv_printf(xendev
, 1, "frontend protocol: %s\n",
220 xen_pv_printf(xendev
, 2, "frontend update: %s\n", node
);
221 if (xendev
->ops
->frontend_changed
) {
222 xendev
->ops
->frontend_changed(xendev
, node
);
227 /* ------------------------------------------------------------- */
228 /* Check for possible state transitions and perform them. */
231 * Initial xendev setup. Read frontend path, register watch for it.
232 * Should succeed once xend finished setting up the backend device.
234 * Also sets initial state (-> Initializing) when done. Which
235 * only affects the xendev->be_state variable as xenbus should
236 * already be put into that state by xend.
238 static int xen_be_try_setup(struct XenDevice
*xendev
)
240 char token
[XEN_BUFSIZE
];
243 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
244 xen_pv_printf(xendev
, 0, "reading backend state failed\n");
248 if (be_state
!= XenbusStateInitialising
) {
249 xen_pv_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
250 xenbus_strstate(be_state
));
254 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
255 if (xendev
->fe
== NULL
) {
256 xen_pv_printf(xendev
, 0, "reading frontend path failed\n");
260 /* setup frontend watch */
261 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
262 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
263 xen_pv_printf(xendev
, 0, "watching frontend path (%s) failed\n",
267 xen_be_set_state(xendev
, XenbusStateInitialising
);
269 xen_be_backend_changed(xendev
, NULL
);
270 xen_be_frontend_changed(xendev
, NULL
);
275 * Try initialize xendev. Prepare everything the backend can do
276 * without synchronizing with the frontend. Fakes hotplug-status. No
277 * hotplug involved here because this is about userspace drivers, thus
278 * there are kernel backend devices which could invoke hotplug.
280 * Goes to InitWait on success.
282 static int xen_be_try_init(struct XenDevice
*xendev
)
286 if (!xendev
->online
) {
287 xen_pv_printf(xendev
, 1, "not online\n");
291 if (xendev
->ops
->init
) {
292 rc
= xendev
->ops
->init(xendev
);
295 xen_pv_printf(xendev
, 1, "init() failed\n");
299 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
300 xen_be_set_state(xendev
, XenbusStateInitWait
);
305 * Try to initialise xendev. Depends on the frontend being ready
306 * for it (shared ring and evtchn info in xenstore, state being
307 * Initialised or Connected).
309 * Goes to Connected on success.
311 static int xen_be_try_initialise(struct XenDevice
*xendev
)
315 if (xendev
->fe_state
!= XenbusStateInitialised
&&
316 xendev
->fe_state
!= XenbusStateConnected
) {
317 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
318 xen_pv_printf(xendev
, 2, "frontend not ready, ignoring\n");
320 xen_pv_printf(xendev
, 2, "frontend not ready (yet)\n");
325 if (xendev
->ops
->initialise
) {
326 rc
= xendev
->ops
->initialise(xendev
);
329 xen_pv_printf(xendev
, 0, "initialise() failed\n");
333 xen_be_set_state(xendev
, XenbusStateConnected
);
338 * Try to let xendev know that it is connected. Depends on the
339 * frontend being Connected. Note that this may be called more
340 * than once since the backend state is not modified.
342 static void xen_be_try_connected(struct XenDevice
*xendev
)
344 if (!xendev
->ops
->connected
) {
348 if (xendev
->fe_state
!= XenbusStateConnected
) {
349 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
350 xen_pv_printf(xendev
, 2, "frontend not ready, ignoring\n");
352 xen_pv_printf(xendev
, 2, "frontend not ready (yet)\n");
357 xendev
->ops
->connected(xendev
);
361 * Teardown connection.
363 * Goes to Closed when done.
365 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
367 if (xendev
->be_state
!= XenbusStateClosing
&&
368 xendev
->be_state
!= XenbusStateClosed
&&
369 xendev
->ops
->disconnect
) {
370 xendev
->ops
->disconnect(xendev
);
372 if (xendev
->be_state
!= state
) {
373 xen_be_set_state(xendev
, state
);
378 * Try to reset xendev, for reconnection by another frontend instance.
380 static int xen_be_try_reset(struct XenDevice
*xendev
)
382 if (xendev
->fe_state
!= XenbusStateInitialising
) {
386 xen_pv_printf(xendev
, 1, "device reset (for re-connect)\n");
387 xen_be_set_state(xendev
, XenbusStateInitialising
);
392 * state change dispatcher function
394 void xen_be_check_state(struct XenDevice
*xendev
)
398 /* frontend may request shutdown from almost anywhere */
399 if (xendev
->fe_state
== XenbusStateClosing
||
400 xendev
->fe_state
== XenbusStateClosed
) {
401 xen_be_disconnect(xendev
, xendev
->fe_state
);
405 /* check for possible backend state transitions */
407 switch (xendev
->be_state
) {
408 case XenbusStateUnknown
:
409 rc
= xen_be_try_setup(xendev
);
411 case XenbusStateInitialising
:
412 rc
= xen_be_try_init(xendev
);
414 case XenbusStateInitWait
:
415 rc
= xen_be_try_initialise(xendev
);
417 case XenbusStateConnected
:
418 /* xendev->be_state doesn't change */
419 xen_be_try_connected(xendev
);
422 case XenbusStateClosed
:
423 rc
= xen_be_try_reset(xendev
);
434 /* ------------------------------------------------------------- */
436 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
438 struct XenDevice
*xendev
;
439 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
441 unsigned int cdev
, j
;
444 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
445 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
446 if (!xs_watch(xenstore
, path
, token
)) {
447 xen_pv_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n",
452 /* look for backends */
453 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
457 for (j
= 0; j
< cdev
; j
++) {
458 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
459 if (xendev
== NULL
) {
462 xen_be_check_state(xendev
);
468 void xenstore_update_be(char *watch
, char *type
, int dom
,
469 struct XenDevOps
*ops
)
471 struct XenDevice
*xendev
;
472 char path
[XEN_BUFSIZE
], *bepath
;
473 unsigned int len
, dev
;
475 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
476 if (strncmp(path
, watch
, len
) != 0) {
479 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
481 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
489 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
490 if (xendev
!= NULL
) {
491 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
492 if (bepath
== NULL
) {
493 xen_pv_del_xendev(xendev
);
496 xen_be_backend_changed(xendev
, path
);
497 xen_be_check_state(xendev
);
502 void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
507 len
= strlen(xendev
->fe
);
508 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
511 if (watch
[len
] != '/') {
514 node
= watch
+ len
+ 1;
516 xen_be_frontend_changed(xendev
, node
);
517 xen_be_check_state(xendev
);
519 /* -------------------------------------------------------------------- */
521 int xen_be_init(void)
523 xengnttab_handle
*gnttabdev
;
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 gnttabdev
= xengnttab_open(NULL
, 0);
539 if (gnttabdev
!= NULL
) {
540 if (xengnttab_grant_copy(gnttabdev
, 0, NULL
) == 0) {
541 xen_feature_grant_copy
= true;
543 xengnttab_close(gnttabdev
);
546 xen_sysdev
= qdev_create(NULL
, TYPE_XENSYSDEV
);
547 qdev_init_nofail(xen_sysdev
);
548 xen_sysbus
= qbus_create(TYPE_XENSYSBUS
, DEVICE(xen_sysdev
), "xen-sysbus");
549 qbus_set_bus_hotplug_handler(xen_sysbus
, &error_abort
);
554 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
555 xs_daemon_close(xenstore
);
561 static void xen_set_dynamic_sysbus(void)
563 Object
*machine
= qdev_get_machine();
564 ObjectClass
*oc
= object_get_class(machine
);
565 MachineClass
*mc
= MACHINE_CLASS(oc
);
567 mc
->has_dynamic_sysbus
= true;
570 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
575 if (ops
->backend_register
) {
576 rc
= ops
->backend_register();
582 snprintf(path
, sizeof(path
), "device-model/%u/backends/%s", xen_domid
,
584 xenstore_mkdir(path
, XS_PERM_NONE
);
586 return xenstore_scan(type
, xen_domid
, ops
);
589 void xen_be_register_common(void)
591 xen_set_dynamic_sysbus();
593 xen_be_register("console", &xen_console_ops
);
594 xen_be_register("vkbd", &xen_kbdmouse_ops
);
595 xen_be_register("qdisk", &xen_blkdev_ops
);
597 xen_be_register("9pfs", &xen_9pfs_ops
);
599 #ifdef CONFIG_USB_LIBUSB
600 xen_be_register("qusb", &xen_usb_ops
);
604 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
606 if (xendev
->local_port
!= -1) {
609 xendev
->local_port
= xenevtchn_bind_interdomain
610 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
611 if (xendev
->local_port
== -1) {
612 xen_pv_printf(xendev
, 0, "xenevtchn_bind_interdomain failed\n");
615 xen_pv_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
616 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
),
617 xen_pv_evtchn_event
, NULL
, xendev
);
622 static Property xendev_properties
[] = {
623 DEFINE_PROP_END_OF_LIST(),
626 static void xendev_class_init(ObjectClass
*klass
, void *data
)
628 DeviceClass
*dc
= DEVICE_CLASS(klass
);
630 dc
->props
= xendev_properties
;
631 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
632 /* xen-backend devices can be plugged/unplugged dynamically */
633 dc
->user_creatable
= true;
636 static const TypeInfo xendev_type_info
= {
637 .name
= TYPE_XENBACKEND
,
638 .parent
= TYPE_XENSYSDEV
,
639 .class_init
= xendev_class_init
,
640 .instance_size
= sizeof(struct XenDevice
),
643 static void xen_sysbus_class_init(ObjectClass
*klass
, void *data
)
645 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
647 hc
->unplug
= qdev_simple_device_unplug_cb
;
650 static const TypeInfo xensysbus_info
= {
651 .name
= TYPE_XENSYSBUS
,
653 .class_init
= xen_sysbus_class_init
,
654 .interfaces
= (InterfaceInfo
[]) {
655 { TYPE_HOTPLUG_HANDLER
},
660 static int xen_sysdev_init(SysBusDevice
*dev
)
665 static Property xen_sysdev_properties
[] = {
666 {/* end of property list */},
669 static void xen_sysdev_class_init(ObjectClass
*klass
, void *data
)
671 DeviceClass
*dc
= DEVICE_CLASS(klass
);
672 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
674 k
->init
= xen_sysdev_init
;
675 dc
->props
= xen_sysdev_properties
;
676 dc
->bus_type
= TYPE_XENSYSBUS
;
679 static const TypeInfo xensysdev_info
= {
680 .name
= TYPE_XENSYSDEV
,
681 .parent
= TYPE_SYS_BUS_DEVICE
,
682 .instance_size
= sizeof(SysBusDevice
),
683 .class_init
= xen_sysdev_class_init
,
686 static void xenbe_register_types(void)
688 type_register_static(&xensysbus_info
);
689 type_register_static(&xensysdev_info
);
690 type_register_static(&xendev_type_info
);
693 type_init(xenbe_register_types
)