xenbus_client.c revision 1.2 1 /* $NetBSD: xenbus_client.c,v 1.2 2006/03/06 20:21:35 bouyer 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.
6 *
7 * Copyright (C) 2005 XenSource Ltd
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
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
28 * IN THE SOFTWARE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.2 2006/03/06 20:21:35 bouyer Exp $");
33
34 #if 0
35 #define DPRINTK(fmt, args...) \
36 printk("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
37 #else
38 #define DPRINTK(fmt, args...) ((void)0)
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/null.h>
43 #include <sys/errno.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46
47 #include <machine/stdarg.h>
48
49 #include <machine/evtchn.h>
50 #include <machine/xenbus.h>
51 #include <machine/granttables.h>
52
53
54 int
55 xenbus_watch_path(struct xenbus_device *dev, char *path,
56 struct xenbus_watch *watch,
57 void (*callback)(struct xenbus_watch *,
58 const char **, unsigned int))
59 {
60 int err;
61
62 watch->node = path;
63 watch->callback = callback;
64
65 err = register_xenbus_watch(watch);
66
67 if (err) {
68 watch->node = NULL;
69 watch->callback = NULL;
70 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
71 }
72
73 return err;
74 }
75
76 int
77 xenbus_watch_path2(struct xenbus_device *dev, const char *path,
78 const char *path2, struct xenbus_watch *watch,
79 void (*callback)(struct xenbus_watch *,
80 const char **, unsigned int))
81 {
82 int err;
83 char *state =
84 malloc(strlen(path) + 1 + strlen(path2) + 1, M_DEVBUF,
85 M_NOWAIT);
86 if (!state) {
87 xenbus_dev_fatal(dev, ENOMEM, "allocating path for watch");
88 return ENOMEM;
89 }
90 strcpy(state, path);
91 strcat(state, "/");
92 strcat(state, path2);
93
94 err = xenbus_watch_path(dev, state, watch, callback);
95
96 if (err) {
97 free(state, M_DEVBUF);
98 }
99 return err;
100 }
101
102
103 int
104 xenbus_switch_state(struct xenbus_device *dev,
105 struct xenbus_transaction *xbt,
106 XenbusState state)
107 {
108 /* We check whether the state is currently set to the given value, and
109 if not, then the state is set. We don't want to unconditionally
110 write the given state, because we don't want to fire watches
111 unnecessarily. Furthermore, if the node has gone, we don't write
112 to it, as the device will be tearing down, and we don't want to
113 resurrect that directory.
114 */
115
116 int current_state;
117
118 int err = xenbus_scanf(xbt, dev->xbusd_path, "state", "%d",
119 ¤t_state);
120 if ((err == 1 && (XenbusState)current_state == state) ||
121 err == 0)
122 return 0;
123
124 err = xenbus_printf(xbt, dev->xbusd_path, "state", "%d", state);
125 if (err) {
126 xenbus_dev_fatal(dev, err, "writing new state");
127 return err;
128 }
129 return 0;
130 }
131
132
133 /**
134 * Return the path to the error node for the given device, or NULL on failure.
135 * If the value returned is non-NULL, then it is the caller's to kfree.
136 */
137 static char *
138 error_path(struct xenbus_device *dev)
139 {
140 char *path_buffer = malloc(strlen("error/") + strlen(dev->xbusd_path) +
141 1, M_DEVBUF, M_NOWAIT);
142 if (path_buffer == NULL) {
143 return NULL;
144 }
145
146 strcpy(path_buffer, "error/");
147 strcpy(path_buffer + strlen("error/"), dev->xbusd_path);
148
149 return path_buffer;
150 }
151
152
153 static void
154 _dev_error(struct xenbus_device *dev, int err, const char *fmt,
155 va_list ap)
156 {
157 int ret;
158 unsigned int len;
159 char *printf_buffer = NULL, *path_buffer = NULL;
160
161 #define PRINTF_BUFFER_SIZE 4096
162 printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
163 if (printf_buffer == NULL)
164 goto fail;
165
166 len = sprintf(printf_buffer, "%i ", -err);
167 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
168
169 KASSERT(len + ret < PRINTF_BUFFER_SIZE);
170 dev->xbusd_has_error = 1;
171
172 path_buffer = error_path(dev);
173
174 if (path_buffer == NULL) {
175 printk("xenbus: failed to write error node for %s (%s)\n",
176 dev->xbusd_path, printf_buffer);
177 goto fail;
178 }
179
180 if (xenbus_write(NULL, path_buffer, "error", printf_buffer) != 0) {
181 printk("xenbus: failed to write error node for %s (%s)\n",
182 dev->xbusd_path, printf_buffer);
183 goto fail;
184 }
185
186 fail:
187 if (printf_buffer)
188 free(printf_buffer, M_DEVBUF);
189 if (path_buffer)
190 free(path_buffer, M_DEVBUF);
191 }
192
193
194 void
195 xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
196 ...)
197 {
198 va_list ap;
199
200 va_start(ap, fmt);
201 _dev_error(dev, err, fmt, ap);
202 va_end(ap);
203 }
204
205
206 void
207 xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
208 ...)
209 {
210 va_list ap;
211
212 va_start(ap, fmt);
213 _dev_error(dev, err, fmt, ap);
214 va_end(ap);
215
216 xenbus_switch_state(dev, NULL, XenbusStateClosing);
217 }
218
219
220 int
221 xenbus_grant_ring(struct xenbus_device *dev, paddr_t ring_pa,
222 grant_ref_t *entryp)
223 {
224 int err = xengnt_grant_access(dev->xbusd_otherend_id, ring_pa,
225 0, entryp);
226 if (err != 0)
227 xenbus_dev_fatal(dev, err, "granting access to ring page");
228 return err;
229 }
230
231
232 int
233 xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
234 {
235 evtchn_op_t op = {
236 .cmd = EVTCHNOP_alloc_unbound,
237 .u.alloc_unbound.dom = DOMID_SELF,
238 .u.alloc_unbound.remote_dom = dev->xbusd_otherend_id };
239
240 int err = HYPERVISOR_event_channel_op(&op);
241 if (err)
242 xenbus_dev_fatal(dev, err, "allocating event channel");
243 else
244 *port = op.u.alloc_unbound.port;
245 return err;
246 }
247
248
249 XenbusState
250 xenbus_read_driver_state(const char *path)
251 {
252 XenbusState result;
253
254 int err = xenbus_gather(NULL, path, "state", "%d", &result, NULL);
255 if (err)
256 result = XenbusStateClosed;
257
258 return result;
259 }
260
261
262 /*
263 * Local variables:
264 * c-file-style: "linux"
265 * indent-tabs-mode: t
266 * c-indent-level: 8
267 * c-basic-offset: 8
268 * tab-width: 8
269 * End:
270 */
271