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