1 /******************************************************************************
2 * Client-facing interface for the Xenbus driver. In other words, the
3 * interface between the Xenbus and the device-specific code, be it the
4 * frontend or the backend of that driver.
6 * Copyright (C) 2005 XenSource Ltd
8 * This file may be distributed separately from the Linux kernel, or
9 * incorporated into other software packages, subject to the following license:
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this source file (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use, copy, modify,
14 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #define DPRINTK(fmt, args...) \
33 printk("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
35 #define DPRINTK(fmt, args...) ((void)0)
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
43 #include <sys/malloc.h>
44 #include <sys/libkern.h>
46 #include <machine/xen/xen-os.h>
47 #include <xen/hypervisor.h>
48 #include <xen/evtchn.h>
49 #include <xen/gnttab.h>
50 #include <xen/xenbus/xenbusvar.h>
51 #include <machine/stdarg.h>
54 xenbus_strstate(XenbusState state)
56 static const char *const name[] = {
57 [ XenbusStateUnknown ] = "Unknown",
58 [ XenbusStateInitialising ] = "Initialising",
59 [ XenbusStateInitWait ] = "InitWait",
60 [ XenbusStateInitialised ] = "Initialised",
61 [ XenbusStateConnected ] = "Connected",
62 [ XenbusStateClosing ] = "Closing",
63 [ XenbusStateClosed ] = "Closed",
66 return ((state < (XenbusStateClosed + 1)) ? name[state] : "INVALID");
70 xenbus_watch_path(device_t dev, char *path, struct xenbus_watch *watch,
71 void (*callback)(struct xenbus_watch *, const char **, unsigned int))
76 watch->callback = callback;
78 error = register_xenbus_watch(watch);
82 watch->callback = NULL;
83 xenbus_dev_fatal(dev, error, "adding watch on %s", path);
90 xenbus_watch_path2(device_t dev, const char *path,
91 const char *path2, struct xenbus_watch *watch,
92 void (*callback)(struct xenbus_watch *, const char **, unsigned int))
95 char *state = malloc(strlen(path) + 1 + strlen(path2) + 1,
100 strcat(state, path2);
102 error = xenbus_watch_path(dev, state, watch, callback);
104 free(state, M_DEVBUF);
111 * Return the path to the error node for the given device, or NULL on failure.
112 * If the value returned is non-NULL, then it is the caller's to kfree.
115 error_path(device_t dev)
117 char *path_buffer = malloc(strlen("error/")
118 + strlen(xenbus_get_node(dev)) + 1, M_DEVBUF, M_WAITOK);
120 strcpy(path_buffer, "error/");
121 strcpy(path_buffer + strlen("error/"), xenbus_get_node(dev));
123 return (path_buffer);
128 _dev_error(device_t dev, int err, const char *fmt, va_list ap)
132 char *printf_buffer = NULL, *path_buffer = NULL;
134 #define PRINTF_BUFFER_SIZE 4096
135 printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_WAITOK);
137 len = sprintf(printf_buffer, "%i ", err);
138 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
140 KASSERT(len + ret <= PRINTF_BUFFER_SIZE-1, ("xenbus error message too big"));
142 dev_err(&dev->dev, "%s\n", printf_buffer);
144 path_buffer = error_path(dev);
146 if (path_buffer == NULL) {
147 printf("xenbus: failed to write error node for %s (%s)\n",
148 xenbus_get_node(dev), printf_buffer);
152 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
153 printf("xenbus: failed to write error node for %s (%s)\n",
154 xenbus_get_node(dev), printf_buffer);
160 free(printf_buffer, M_DEVBUF);
162 free(path_buffer, M_DEVBUF);
166 xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
171 _dev_error(dev, err, fmt, ap);
176 xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
181 _dev_error(dev, err, fmt, ap);
184 xenbus_set_state(dev, XenbusStateClosing);
188 xenbus_grant_ring(device_t dev, unsigned long ring_mfn, int *refp)
193 error = gnttab_grant_foreign_access(
194 xenbus_get_otherend_id(dev), ring_mfn, 0, &ref);
196 xenbus_dev_fatal(dev, error, "granting access to ring page");
205 xenbus_alloc_evtchn(device_t dev, int *port)
207 struct evtchn_alloc_unbound alloc_unbound;
210 alloc_unbound.dom = DOMID_SELF;
211 alloc_unbound.remote_dom = xenbus_get_otherend_id(dev);
213 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
217 xenbus_dev_fatal(dev, -err, "allocating event channel");
220 *port = alloc_unbound.port;
225 xenbus_free_evtchn(device_t dev, int port)
227 struct evtchn_close close;
232 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
234 xenbus_dev_error(dev, -err, "freeing event channel %d", port);
241 xenbus_read_driver_state(const char *path)
246 error = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
248 result = XenbusStateClosed;