xenbus_probe.c revision 1.11 1 1.11 bouyer /* $NetBSD: xenbus_probe.c,v 1.11 2006/05/23 21:07:56 bouyer Exp $ */
2 1.1 bouyer /******************************************************************************
3 1.1 bouyer * Talks to Xen Store to figure out what devices we have.
4 1.1 bouyer *
5 1.1 bouyer * Copyright (C) 2005 Rusty Russell, IBM Corporation
6 1.1 bouyer * Copyright (C) 2005 Mike Wray, Hewlett-Packard
7 1.1 bouyer * Copyright (C) 2005 XenSource Ltd
8 1.1 bouyer *
9 1.1 bouyer * This file may be distributed separately from the Linux kernel, or
10 1.1 bouyer * incorporated into other software packages, subject to the following license:
11 1.1 bouyer *
12 1.1 bouyer * Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 bouyer * of this source file (the "Software"), to deal in the Software without
14 1.1 bouyer * restriction, including without limitation the rights to use, copy, modify,
15 1.1 bouyer * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 1.1 bouyer * and to permit persons to whom the Software is furnished to do so, subject to
17 1.1 bouyer * the following conditions:
18 1.1 bouyer *
19 1.1 bouyer * The above copyright notice and this permission notice shall be included in
20 1.1 bouyer * all copies or substantial portions of the Software.
21 1.1 bouyer *
22 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 bouyer * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 bouyer * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 bouyer * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 1.1 bouyer * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 1.1 bouyer * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 1.1 bouyer * IN THE SOFTWARE.
29 1.1 bouyer */
30 1.1 bouyer
31 1.2 bouyer #include <sys/cdefs.h>
32 1.11 bouyer __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.11 2006/05/23 21:07:56 bouyer Exp $");
33 1.2 bouyer
34 1.1 bouyer #if 0
35 1.1 bouyer #define DPRINTK(fmt, args...) \
36 1.2 bouyer printf("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
37 1.1 bouyer #else
38 1.1 bouyer #define DPRINTK(fmt, args...) ((void)0)
39 1.1 bouyer #endif
40 1.1 bouyer
41 1.2 bouyer #include <sys/types.h>
42 1.2 bouyer #include <sys/null.h>
43 1.2 bouyer #include <sys/errno.h>
44 1.2 bouyer #include <sys/malloc.h>
45 1.2 bouyer #include <sys/systm.h>
46 1.2 bouyer #include <sys/param.h>
47 1.2 bouyer #include <sys/kthread.h>
48 1.7 bouyer #include <uvm/uvm.h>
49 1.7 bouyer
50 1.2 bouyer #include <machine/stdarg.h>
51 1.2 bouyer
52 1.2 bouyer #include <machine/hypervisor.h>
53 1.2 bouyer #include <machine/xenbus.h>
54 1.2 bouyer #include <machine/evtchn.h>
55 1.1 bouyer
56 1.1 bouyer #include "xenbus_comms.h"
57 1.1 bouyer
58 1.1 bouyer extern struct semaphore xenwatch_mutex;
59 1.1 bouyer
60 1.1 bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
61 1.1 bouyer
62 1.2 bouyer static int xenbus_match(struct device *, struct cfdata *, void *);
63 1.2 bouyer static void xenbus_attach(struct device *, struct device *, void *);
64 1.2 bouyer static int xenbus_print(void *, const char *);
65 1.2 bouyer
66 1.2 bouyer static void xenbus_kthread_create(void *);
67 1.2 bouyer static void xenbus_probe_init(void *);
68 1.2 bouyer
69 1.5 bouyer static struct xenbus_device *xenbus_lookup_device_path(const char *);
70 1.5 bouyer
71 1.2 bouyer CFATTACH_DECL(xenbus, sizeof(struct device), xenbus_match, xenbus_attach,
72 1.2 bouyer NULL, NULL);
73 1.2 bouyer
74 1.5 bouyer struct device *xenbus_sc;
75 1.5 bouyer
76 1.5 bouyer SLIST_HEAD(, xenbus_device) xenbus_device_list;
77 1.11 bouyer SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
78 1.11 bouyer SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
79 1.2 bouyer
80 1.2 bouyer int
81 1.2 bouyer xenbus_match(struct device *parent, struct cfdata *match, void *aux)
82 1.2 bouyer {
83 1.2 bouyer struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
84 1.2 bouyer
85 1.2 bouyer if (strcmp(xa->xa_device, "xenbus") == 0)
86 1.2 bouyer return 1;
87 1.2 bouyer return 0;
88 1.2 bouyer }
89 1.2 bouyer
90 1.2 bouyer static void
91 1.2 bouyer xenbus_attach(struct device *parent, struct device *self, void *aux)
92 1.2 bouyer {
93 1.2 bouyer aprint_normal(": Xen Virtual Bus Interface\n");
94 1.5 bouyer xenbus_sc = self;
95 1.2 bouyer config_pending_incr();
96 1.2 bouyer kthread_create(xenbus_kthread_create, NULL);
97 1.2 bouyer }
98 1.2 bouyer
99 1.2 bouyer void
100 1.11 bouyer xenbus_backend_register(struct xenbus_backend_driver *xbakd)
101 1.11 bouyer {
102 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
103 1.11 bouyer }
104 1.11 bouyer
105 1.11 bouyer void
106 1.2 bouyer xenbus_kthread_create(void *unused)
107 1.2 bouyer {
108 1.2 bouyer struct proc *p;
109 1.2 bouyer int err;
110 1.2 bouyer err = kthread_create1(xenbus_probe_init, NULL, &p, "xenbus_probe");
111 1.2 bouyer if (err)
112 1.2 bouyer printf("kthread_create1(xenbus_probe): %d\n", err);
113 1.2 bouyer }
114 1.2 bouyer
115 1.2 bouyer static int
116 1.2 bouyer read_otherend_details(struct xenbus_device *xendev,
117 1.2 bouyer const char *id_node, const char *path_node)
118 1.2 bouyer {
119 1.2 bouyer int err;
120 1.2 bouyer char *val, *ep;
121 1.1 bouyer
122 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
123 1.2 bouyer if (err) {
124 1.2 bouyer printf("reading other end details %s from %s\n",
125 1.2 bouyer id_node, xendev->xbusd_path);
126 1.2 bouyer xenbus_dev_fatal(xendev, err,
127 1.2 bouyer "reading other end details %s from %s",
128 1.2 bouyer id_node, xendev->xbusd_path);
129 1.2 bouyer return err;
130 1.2 bouyer }
131 1.2 bouyer xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
132 1.2 bouyer if (val[0] == '\0' || *ep != '\0') {
133 1.2 bouyer printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
134 1.2 bouyer xenbus_dev_fatal(xendev, err,
135 1.2 bouyer "reading other end details %s from %s: %s is not a number",
136 1.2 bouyer id_node, xendev->xbusd_path, val);
137 1.2 bouyer free(val, M_DEVBUF);
138 1.2 bouyer return EFTYPE;
139 1.2 bouyer }
140 1.2 bouyer free(val, M_DEVBUF);
141 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
142 1.1 bouyer if (err) {
143 1.2 bouyer printf("reading other end details %s from %s (%d)\n",
144 1.2 bouyer path_node, xendev->xbusd_path, err);
145 1.1 bouyer xenbus_dev_fatal(xendev, err,
146 1.2 bouyer "reading other end details %s from %s",
147 1.2 bouyer path_node, xendev->xbusd_path);
148 1.1 bouyer return err;
149 1.1 bouyer }
150 1.3 bouyer DPRINTK("read_otherend_details: read %s/%s returned %s\n",
151 1.3 bouyer xendev->xbusd_path, path_node, val);
152 1.2 bouyer xendev->xbusd_otherend = val;
153 1.2 bouyer
154 1.2 bouyer if (strlen(xendev->xbusd_otherend) == 0 ||
155 1.2 bouyer !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
156 1.2 bouyer printf("missing other end from %s\n", xendev->xbusd_path);
157 1.1 bouyer xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
158 1.2 bouyer xendev->xbusd_path);
159 1.2 bouyer free(xendev->xbusd_otherend, M_DEVBUF);
160 1.2 bouyer xendev->xbusd_otherend = NULL;
161 1.2 bouyer return ENOENT;
162 1.1 bouyer }
163 1.1 bouyer
164 1.1 bouyer return 0;
165 1.1 bouyer }
166 1.1 bouyer
167 1.2 bouyer static int
168 1.2 bouyer read_backend_details(struct xenbus_device *xendev)
169 1.1 bouyer {
170 1.1 bouyer return read_otherend_details(xendev, "backend-id", "backend");
171 1.1 bouyer }
172 1.1 bouyer
173 1.1 bouyer
174 1.2 bouyer static int
175 1.2 bouyer read_frontend_details(struct xenbus_device *xendev)
176 1.1 bouyer {
177 1.1 bouyer return read_otherend_details(xendev, "frontend-id", "frontend");
178 1.1 bouyer }
179 1.1 bouyer
180 1.3 bouyer #if unused
181 1.2 bouyer static void
182 1.2 bouyer free_otherend_details(struct xenbus_device *dev)
183 1.1 bouyer {
184 1.2 bouyer free(dev->xbusd_otherend, M_DEVBUF);
185 1.2 bouyer dev->xbusd_otherend = NULL;
186 1.1 bouyer }
187 1.3 bouyer #endif
188 1.1 bouyer
189 1.1 bouyer
190 1.2 bouyer static void
191 1.2 bouyer free_otherend_watch(struct xenbus_device *dev)
192 1.1 bouyer {
193 1.2 bouyer if (dev->xbusd_otherend_watch.node) {
194 1.2 bouyer unregister_xenbus_watch(&dev->xbusd_otherend_watch);
195 1.3 bouyer free(dev->xbusd_otherend_watch.node, M_DEVBUF);
196 1.2 bouyer dev->xbusd_otherend_watch.node = NULL;
197 1.1 bouyer }
198 1.1 bouyer }
199 1.1 bouyer
200 1.2 bouyer static void
201 1.2 bouyer otherend_changed(struct xenbus_watch *watch,
202 1.1 bouyer const char **vec, unsigned int len)
203 1.1 bouyer {
204 1.3 bouyer struct xenbus_device *xdev = watch->xbw_dev;
205 1.1 bouyer XenbusState state;
206 1.1 bouyer
207 1.1 bouyer /* Protect us against watches firing on old details when the otherend
208 1.1 bouyer details change, say immediately after a resume. */
209 1.3 bouyer if (!xdev->xbusd_otherend ||
210 1.3 bouyer strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
211 1.3 bouyer strlen(xdev->xbusd_otherend))) {
212 1.1 bouyer DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
213 1.1 bouyer return;
214 1.1 bouyer }
215 1.1 bouyer
216 1.3 bouyer state = xenbus_read_driver_state(xdev->xbusd_otherend);
217 1.1 bouyer
218 1.1 bouyer DPRINTK("state is %d, %s, %s",
219 1.3 bouyer state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
220 1.5 bouyer if (state == XenbusStateClosed) {
221 1.5 bouyer int error;
222 1.11 bouyer if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
223 1.11 bouyer error = xdev->xbusd_u.b.b_detach(
224 1.11 bouyer xdev->xbusd_u.b.b_cookie);
225 1.11 bouyer if (error) {
226 1.11 bouyer printf("could not detach %s: %d\n",
227 1.11 bouyer xdev->xbusd_path, error);
228 1.11 bouyer return;
229 1.11 bouyer }
230 1.11 bouyer } else {
231 1.11 bouyer error = config_detach(xdev->xbusd_u.f.f_dev,
232 1.11 bouyer DETACH_FORCE);
233 1.11 bouyer if (error) {
234 1.11 bouyer printf("could not detach %s: %d\n",
235 1.11 bouyer xdev->xbusd_u.f.f_dev->dv_xname, error);
236 1.11 bouyer return;
237 1.11 bouyer }
238 1.5 bouyer }
239 1.5 bouyer xenbus_free_device(xdev);
240 1.5 bouyer return;
241 1.5 bouyer }
242 1.3 bouyer if (xdev->xbusd_otherend_changed)
243 1.11 bouyer xdev->xbusd_otherend_changed(
244 1.11 bouyer (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
245 1.11 bouyer xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
246 1.1 bouyer }
247 1.1 bouyer
248 1.2 bouyer static int
249 1.2 bouyer talk_to_otherend(struct xenbus_device *dev)
250 1.1 bouyer {
251 1.1 bouyer free_otherend_watch(dev);
252 1.1 bouyer
253 1.2 bouyer return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
254 1.3 bouyer &dev->xbusd_otherend_watch,
255 1.3 bouyer otherend_changed);
256 1.1 bouyer }
257 1.1 bouyer
258 1.5 bouyer static struct xenbus_device *
259 1.5 bouyer xenbus_lookup_device_path(const char *path)
260 1.5 bouyer {
261 1.5 bouyer struct xenbus_device *xbusd;
262 1.5 bouyer
263 1.5 bouyer SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
264 1.5 bouyer if (strcmp(xbusd->xbusd_path, path) == 0)
265 1.5 bouyer return xbusd;
266 1.5 bouyer }
267 1.5 bouyer return NULL;
268 1.5 bouyer }
269 1.5 bouyer
270 1.2 bouyer static int
271 1.11 bouyer xenbus_probe_device_type(const char *path, const char *type,
272 1.11 bouyer int (*create)(struct xenbus_device *))
273 1.1 bouyer {
274 1.11 bouyer int err, i, msize;
275 1.11 bouyer unsigned long state;
276 1.1 bouyer char **dir;
277 1.1 bouyer unsigned int dir_n = 0;
278 1.2 bouyer struct xenbus_device *xbusd;
279 1.2 bouyer struct xenbusdev_attach_args xa;
280 1.2 bouyer char *ep;
281 1.2 bouyer
282 1.11 bouyer DPRINTK("probe %s type %s", path, type);
283 1.11 bouyer err = xenbus_directory(NULL, path, "", &dir_n, &dir);
284 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
285 1.2 bouyer if (err)
286 1.2 bouyer return err;
287 1.1 bouyer
288 1.1 bouyer for (i = 0; i < dir_n; i++) {
289 1.5 bouyer /*
290 1.5 bouyer * add size of path to size of xenbus_device. xenbus_device
291 1.5 bouyer * already has room for one char in xbusd_path.
292 1.5 bouyer */
293 1.11 bouyer msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
294 1.5 bouyer xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
295 1.2 bouyer if (xbusd == NULL)
296 1.2 bouyer panic("can't malloc xbusd");
297 1.2 bouyer
298 1.2 bouyer snprintf(__UNCONST(xbusd->xbusd_path),
299 1.11 bouyer msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
300 1.5 bouyer if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
301 1.5 bouyer /* device already registered */
302 1.5 bouyer free(xbusd, M_DEVBUF);
303 1.5 bouyer continue;
304 1.5 bouyer }
305 1.11 bouyer err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
306 1.11 bouyer &state);
307 1.11 bouyer if (err) {
308 1.11 bouyer printf("xenbus: can't get state "
309 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
310 1.11 bouyer free(xbusd, M_DEVBUF);
311 1.11 bouyer continue;
312 1.11 bouyer }
313 1.11 bouyer if (state != XenbusStateInitialising) {
314 1.11 bouyer /* device is not new */
315 1.11 bouyer free(xbusd, M_DEVBUF);
316 1.11 bouyer continue;
317 1.11 bouyer }
318 1.5 bouyer
319 1.3 bouyer xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
320 1.2 bouyer DPRINTK("xenbus_probe_device_type probe %s\n",
321 1.2 bouyer xbusd->xbusd_path);
322 1.11 bouyer if (create != NULL) {
323 1.11 bouyer xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
324 1.11 bouyer err = read_frontend_details(xbusd);
325 1.11 bouyer if (err != 0) {
326 1.11 bouyer printf("xenbus: can't get frontend details "
327 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
328 1.11 bouyer break;
329 1.11 bouyer }
330 1.11 bouyer if (create(xbusd)) {
331 1.11 bouyer free(xbusd, M_DEVBUF);
332 1.11 bouyer continue;
333 1.11 bouyer }
334 1.11 bouyer } else {
335 1.11 bouyer xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
336 1.11 bouyer xa.xa_xbusd = xbusd;
337 1.11 bouyer xa.xa_type = type;
338 1.11 bouyer xa.xa_id = strtoul(dir[i], &ep, 0);
339 1.11 bouyer if (dir[i][0] == '\0' || *ep != '\0') {
340 1.11 bouyer printf("xenbus device type %s: id %s is not a"
341 1.11 bouyer " number\n", type, dir[i]);
342 1.11 bouyer err = EFTYPE;
343 1.11 bouyer free(xbusd, M_DEVBUF);
344 1.11 bouyer break;
345 1.11 bouyer }
346 1.11 bouyer err = read_backend_details(xbusd);
347 1.11 bouyer if (err != 0) {
348 1.11 bouyer printf("xenbus: can't get backend details "
349 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
350 1.11 bouyer break;
351 1.11 bouyer }
352 1.11 bouyer xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_sc,
353 1.11 bouyer "xenbus", &xa, xenbus_print);
354 1.11 bouyer if (xbusd->xbusd_u.f.f_dev == NULL) {
355 1.11 bouyer free(xbusd, M_DEVBUF);
356 1.11 bouyer continue;
357 1.11 bouyer }
358 1.3 bouyer }
359 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_device_list,
360 1.11 bouyer xbusd, xbusd_entries);
361 1.11 bouyer talk_to_otherend(xbusd);
362 1.1 bouyer }
363 1.2 bouyer free(dir, M_DEVBUF);
364 1.1 bouyer return err;
365 1.1 bouyer }
366 1.1 bouyer
367 1.2 bouyer static int
368 1.2 bouyer xenbus_print(void *aux, const char *pnp)
369 1.2 bouyer {
370 1.2 bouyer struct xenbusdev_attach_args *xa = aux;
371 1.2 bouyer
372 1.2 bouyer if (pnp) {
373 1.2 bouyer if (strcmp(xa->xa_type, "vbd") == 0)
374 1.2 bouyer aprint_normal("xbd");
375 1.2 bouyer else if (strcmp(xa->xa_type, "vif") == 0)
376 1.2 bouyer aprint_normal("xennet");
377 1.2 bouyer else
378 1.2 bouyer aprint_normal("unknown type %s", xa->xa_type);
379 1.2 bouyer aprint_normal(" at %s", pnp);
380 1.2 bouyer }
381 1.2 bouyer aprint_normal(" id %d", xa->xa_id);
382 1.2 bouyer return(UNCONF);
383 1.2 bouyer }
384 1.2 bouyer
385 1.2 bouyer static int
386 1.11 bouyer xenbus_probe_frontends(void)
387 1.1 bouyer {
388 1.2 bouyer int err;
389 1.1 bouyer char **dir;
390 1.1 bouyer unsigned int i, dir_n;
391 1.11 bouyer char path[30];
392 1.1 bouyer
393 1.11 bouyer DPRINTK("probe device");
394 1.11 bouyer err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
395 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
396 1.2 bouyer if (err)
397 1.2 bouyer return err;
398 1.1 bouyer
399 1.1 bouyer for (i = 0; i < dir_n; i++) {
400 1.11 bouyer snprintf(path, sizeof(path), "device/%s", dir[i]);
401 1.11 bouyer err = xenbus_probe_device_type(path, dir[i], NULL);
402 1.1 bouyer if (err)
403 1.1 bouyer break;
404 1.1 bouyer }
405 1.2 bouyer free(dir, M_DEVBUF);
406 1.1 bouyer return err;
407 1.1 bouyer }
408 1.1 bouyer
409 1.11 bouyer static int
410 1.11 bouyer xenbus_probe_backends(void)
411 1.11 bouyer {
412 1.11 bouyer int err;
413 1.11 bouyer char **dirt, **dirid;
414 1.11 bouyer unsigned int type, id, dirt_n, dirid_n;
415 1.11 bouyer char path[30];
416 1.11 bouyer struct xenbus_backend_driver *xbakd;
417 1.11 bouyer
418 1.11 bouyer DPRINTK("probe backend");
419 1.11 bouyer err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
420 1.11 bouyer DPRINTK("directory err %d dirt_n %d", err, dirt_n);
421 1.11 bouyer if (err)
422 1.11 bouyer return err;
423 1.11 bouyer
424 1.11 bouyer for (type = 0; type < dirt_n; type++) {
425 1.11 bouyer SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
426 1.11 bouyer xbakd_entries) {
427 1.11 bouyer if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
428 1.11 bouyer break;
429 1.11 bouyer }
430 1.11 bouyer if (xbakd == NULL)
431 1.11 bouyer continue;
432 1.11 bouyer err = xenbus_directory(NULL, "backend", dirt[type],
433 1.11 bouyer &dirid_n, &dirid);
434 1.11 bouyer DPRINTK("directory backend/%s err %d dirid_n %d",
435 1.11 bouyer dirt[type], err, dirid_n);
436 1.11 bouyer if (err)
437 1.11 bouyer return err;
438 1.11 bouyer for (id = 0; id < dirid_n; id++) {
439 1.11 bouyer snprintf(path, sizeof(path), "backend/%s/%s",
440 1.11 bouyer dirt[type], dirid[id]);
441 1.11 bouyer err = xenbus_probe_device_type(path, dirt[type],
442 1.11 bouyer xbakd->xbakd_create);
443 1.11 bouyer if (err)
444 1.11 bouyer break;
445 1.11 bouyer }
446 1.11 bouyer free(dirid, M_DEVBUF);
447 1.11 bouyer }
448 1.11 bouyer free(dirt, M_DEVBUF);
449 1.11 bouyer return err;
450 1.11 bouyer }
451 1.11 bouyer
452 1.5 bouyer int
453 1.5 bouyer xenbus_free_device(struct xenbus_device *xbusd)
454 1.5 bouyer {
455 1.5 bouyer KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
456 1.5 bouyer SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
457 1.5 bouyer free_otherend_watch(xbusd);
458 1.5 bouyer free(xbusd->xbusd_otherend, M_DEVBUF);
459 1.5 bouyer xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
460 1.5 bouyer free(xbusd, M_DEVBUF);
461 1.5 bouyer return 0;
462 1.5 bouyer }
463 1.5 bouyer
464 1.2 bouyer static void
465 1.2 bouyer frontend_changed(struct xenbus_watch *watch,
466 1.1 bouyer const char **vec, unsigned int len)
467 1.1 bouyer {
468 1.11 bouyer DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
469 1.11 bouyer xenbus_probe_frontends();
470 1.1 bouyer }
471 1.1 bouyer
472 1.2 bouyer static void
473 1.2 bouyer backend_changed(struct xenbus_watch *watch,
474 1.1 bouyer const char **vec, unsigned int len)
475 1.1 bouyer {
476 1.11 bouyer DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
477 1.11 bouyer xenbus_probe_backends();
478 1.1 bouyer }
479 1.1 bouyer
480 1.2 bouyer
481 1.1 bouyer /* We watch for devices appearing and vanishing. */
482 1.3 bouyer static struct xenbus_watch fe_watch;
483 1.1 bouyer
484 1.3 bouyer static struct xenbus_watch be_watch;
485 1.1 bouyer
486 1.1 bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
487 1.1 bouyer int xenstored_ready = 0;
488 1.1 bouyer
489 1.2 bouyer void
490 1.2 bouyer xenbus_probe(void *unused)
491 1.1 bouyer {
492 1.2 bouyer KASSERT((xenstored_ready > 0));
493 1.1 bouyer
494 1.1 bouyer /* Enumerate devices in xenstore. */
495 1.11 bouyer xenbus_probe_frontends();
496 1.11 bouyer xenbus_probe_backends();
497 1.1 bouyer
498 1.1 bouyer /* Watch for changes. */
499 1.3 bouyer fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
500 1.3 bouyer strcpy(fe_watch.node, "device");
501 1.3 bouyer fe_watch.xbw_callback = frontend_changed;
502 1.1 bouyer register_xenbus_watch(&fe_watch);
503 1.3 bouyer be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
504 1.3 bouyer strcpy(be_watch.node, "backend");
505 1.3 bouyer be_watch.xbw_callback = backend_changed;
506 1.1 bouyer register_xenbus_watch(&be_watch);
507 1.1 bouyer
508 1.1 bouyer /* Notify others that xenstore is up */
509 1.2 bouyer //notifier_call_chain(&xenstore_chain, 0, NULL);
510 1.1 bouyer }
511 1.1 bouyer
512 1.2 bouyer static void
513 1.2 bouyer xenbus_probe_init(void *unused)
514 1.1 bouyer {
515 1.1 bouyer int err = 0, dom0;
516 1.1 bouyer
517 1.1 bouyer DPRINTK("");
518 1.1 bouyer
519 1.5 bouyer SLIST_INIT(&xenbus_device_list);
520 1.5 bouyer
521 1.1 bouyer /*
522 1.1 bouyer ** Domain0 doesn't have a store_evtchn or store_mfn yet.
523 1.1 bouyer */
524 1.2 bouyer dom0 = (xen_start_info.store_evtchn == 0);
525 1.1 bouyer if (dom0) {
526 1.7 bouyer #if defined(DOM0OPS)
527 1.7 bouyer vaddr_t page;
528 1.7 bouyer paddr_t ma;
529 1.1 bouyer evtchn_op_t op = { 0 };
530 1.1 bouyer int ret;
531 1.1 bouyer
532 1.1 bouyer /* Allocate page. */
533 1.7 bouyer page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
534 1.7 bouyer UVM_KMF_ZERO | UVM_KMF_WIRED);
535 1.1 bouyer if (!page)
536 1.7 bouyer panic("can't get xenstore page");
537 1.1 bouyer
538 1.7 bouyer (void)pmap_extract_ma(pmap_kernel(), page, &ma);
539 1.7 bouyer xen_start_info.store_mfn = ma >> PAGE_SHIFT;
540 1.7 bouyer xenstore_interface = (void *)page;
541 1.1 bouyer
542 1.1 bouyer /* Next allocate a local port which xenstored can bind to */
543 1.1 bouyer op.cmd = EVTCHNOP_alloc_unbound;
544 1.1 bouyer op.u.alloc_unbound.dom = DOMID_SELF;
545 1.1 bouyer op.u.alloc_unbound.remote_dom = 0;
546 1.1 bouyer
547 1.1 bouyer ret = HYPERVISOR_event_channel_op(&op);
548 1.7 bouyer if (ret)
549 1.2 bouyer panic("can't register xenstore event");
550 1.7 bouyer xen_start_info.store_evtchn = op.u.alloc_unbound.port;
551 1.1 bouyer
552 1.7 bouyer /* And finally publish the above info in /kern/xen */
553 1.9 bouyer xenbus_kernfs_init();
554 1.7 bouyer #else /* DOM0OPS */
555 1.7 bouyer return ; /* can't get a working xenstore in this case */
556 1.7 bouyer #endif /* DOM0OPS */
557 1.1 bouyer }
558 1.1 bouyer
559 1.10 bouyer /* register event handler */
560 1.10 bouyer xb_init_comms((struct device *)xenbus_sc);
561 1.10 bouyer
562 1.1 bouyer /* Initialize the interface to xenstore. */
563 1.1 bouyer err = xs_init();
564 1.1 bouyer if (err) {
565 1.2 bouyer printf("XENBUS: Error initializing xenstore comms: %i\n", err);
566 1.2 bouyer kthread_exit(err);
567 1.1 bouyer }
568 1.1 bouyer
569 1.1 bouyer if (!dom0) {
570 1.1 bouyer xenstored_ready = 1;
571 1.1 bouyer xenbus_probe(NULL);
572 1.1 bouyer }
573 1.1 bouyer
574 1.2 bouyer DPRINTK("done");
575 1.2 bouyer config_pending_decr();
576 1.11 bouyer #ifdef DOM0OPS
577 1.11 bouyer if (dom0) {
578 1.11 bouyer int s;
579 1.11 bouyer s = spltty();
580 1.11 bouyer while (xenstored_ready == 0) {
581 1.11 bouyer tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
582 1.11 bouyer xenbus_probe(NULL);
583 1.11 bouyer }
584 1.11 bouyer splx(s);
585 1.11 bouyer }
586 1.11 bouyer #endif
587 1.2 bouyer kthread_exit(0);
588 1.1 bouyer }
589 1.1 bouyer
590 1.1 bouyer /*
591 1.1 bouyer * Local variables:
592 1.1 bouyer * c-file-style: "linux"
593 1.1 bouyer * indent-tabs-mode: t
594 1.1 bouyer * c-indent-level: 8
595 1.1 bouyer * c-basic-offset: 8
596 1.1 bouyer * tab-width: 8
597 1.1 bouyer * End:
598 1.1 bouyer */
599