1 /* $NetBSD: xenbus_client.c,v 1.9 2008/09/05 13:37:24 tron Exp $ */
2 /******************************************************************************
3 * Client-facing interface for the Xenbus driver. In other words, the
4 * interface between the Xenbus and the device-specific code, be it the
5 * frontend or the backend of that driver.
7 * Copyright (C) 2005 XenSource Ltd
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.9 2008/09/05 13:37:24 tron Exp $");
35 #define DPRINTK(fmt, args...) \
36 printk("xenbus_client (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
38 #define DPRINTK(fmt, args...) ((void)0)
41 #include <sys/types.h>
43 #include <sys/errno.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
47 #include <machine/stdarg.h>
50 #include <xen/hypervisor.h>
51 #include <xen/evtchn.h>
52 #include <xen/xenbus.h>
53 #include <xen/granttables.h>
57 xenbus_watch_path(struct xenbus_device
*dev
, char *path
,
58 struct xenbus_watch
*watch
,
59 void (*callback
)(struct xenbus_watch
*,
60 const char **, unsigned int))
65 watch
->xbw_callback
= callback
;
67 err
= register_xenbus_watch(watch
);
71 watch
->xbw_callback
= NULL
;
72 xenbus_dev_fatal(dev
, err
, "adding watch on %s", path
);
80 xenbus_watch_path2(struct xenbus_device
*dev
, const char *path
,
81 const char *path2
, struct xenbus_watch
*watch
,
82 void (*callback
)(struct xenbus_watch
*,
83 const char **, unsigned int))
88 DPRINTK("xenbus_watch_path2 path %s path2 %s\n", path
, path2
);
90 malloc(strlen(path
) + 1 + strlen(path2
) + 1, M_DEVBUF
,
93 xenbus_dev_fatal(dev
, ENOMEM
, "allocating path for watch");
100 err
= xenbus_watch_path(dev
, state
, watch
, callback
);
103 free(state
, M_DEVBUF
);
110 xenbus_switch_state(struct xenbus_device
*dev
,
111 struct xenbus_transaction
*xbt
,
114 /* We check whether the state is currently set to the given value, and
115 if not, then the state is set. We don't want to unconditionally
116 write the given state, because we don't want to fire watches
117 unnecessarily. Furthermore, if the node has gone, we don't write
118 to it, as the device will be tearing down, and we don't want to
119 resurrect that directory.
122 u_long current_state
;
124 int err
= xenbus_read_ul(xbt
, dev
->xbusd_path
, "state",
129 if ((XenbusState
)current_state
== state
)
132 err
= xenbus_printf(xbt
, dev
->xbusd_path
, "state", "%d", state
);
134 xenbus_dev_fatal(dev
, err
, "writing new state");
141 * Return the path to the error node for the given device, or NULL on failure.
142 * If the value returned is non-NULL, then it is the caller's to kfree.
145 error_path(struct xenbus_device
*dev
)
147 char *path_buffer
= malloc(strlen("error/") + strlen(dev
->xbusd_path
) +
148 1, M_DEVBUF
, M_NOWAIT
);
149 if (path_buffer
== NULL
) {
153 strcpy(path_buffer
, "error/");
154 strcpy(path_buffer
+ strlen("error/"), dev
->xbusd_path
);
161 _dev_error(struct xenbus_device
*dev
, int err
, const char *fmt
,
166 char *printf_buffer
= NULL
, *path_buffer
= NULL
;
168 #define PRINTF_BUFFER_SIZE 4096
169 printf_buffer
= malloc(PRINTF_BUFFER_SIZE
, M_DEVBUF
, M_NOWAIT
);
170 if (printf_buffer
== NULL
)
173 len
= snprintf(printf_buffer
, PRINTF_BUFFER_SIZE
, "%i ", -err
);
174 ret
= vsnprintf(printf_buffer
+len
, PRINTF_BUFFER_SIZE
-len
, fmt
, ap
);
176 KASSERT(len
+ ret
< PRINTF_BUFFER_SIZE
);
177 dev
->xbusd_has_error
= 1;
179 path_buffer
= error_path(dev
);
181 if (path_buffer
== NULL
) {
182 printk("xenbus: failed to write error node for %s (%s)\n",
183 dev
->xbusd_path
, printf_buffer
);
187 if (xenbus_write(NULL
, path_buffer
, "error", printf_buffer
) != 0) {
188 printk("xenbus: failed to write error node for %s (%s)\n",
189 dev
->xbusd_path
, printf_buffer
);
195 free(printf_buffer
, M_DEVBUF
);
197 free(path_buffer
, M_DEVBUF
);
202 xenbus_dev_error(struct xenbus_device
*dev
, int err
, const char *fmt
,
208 _dev_error(dev
, err
, fmt
, ap
);
214 xenbus_dev_fatal(struct xenbus_device
*dev
, int err
, const char *fmt
,
220 _dev_error(dev
, err
, fmt
, ap
);
223 xenbus_switch_state(dev
, NULL
, XenbusStateClosing
);
228 xenbus_grant_ring(struct xenbus_device
*dev
, paddr_t ring_pa
,
231 int err
= xengnt_grant_access(dev
->xbusd_otherend_id
, ring_pa
,
234 xenbus_dev_fatal(dev
, err
, "granting access to ring page");
240 xenbus_alloc_evtchn(struct xenbus_device
*dev
, int *port
)
243 .cmd
= EVTCHNOP_alloc_unbound
,
246 .remote_dom
= dev
->xbusd_otherend_id
,
251 int err
= HYPERVISOR_event_channel_op(&op
);
253 xenbus_dev_fatal(dev
, err
, "allocating event channel");
255 *port
= op
.u
.alloc_unbound
.port
;
261 xenbus_read_driver_state(const char *path
)
265 int err
= xenbus_read_ul(NULL
, path
, "state", &result
, 10);
267 result
= XenbusStateClosed
;
275 * c-file-style: "linux"
276 * indent-tabs-mode: t