xenbus_probe.c revision 1.3 1 1.3 bouyer /* $NetBSD: xenbus_probe.c,v 1.3 2006/03/15 22:20:06 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.3 bouyer __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.3 2006/03/15 22:20:06 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.2 bouyer
49 1.2 bouyer #include <machine/stdarg.h>
50 1.2 bouyer
51 1.2 bouyer #include <machine/hypervisor.h>
52 1.2 bouyer #include <machine/xenbus.h>
53 1.2 bouyer #include <machine/evtchn.h>
54 1.1 bouyer
55 1.1 bouyer #include "xenbus_comms.h"
56 1.1 bouyer
57 1.1 bouyer extern struct semaphore xenwatch_mutex;
58 1.1 bouyer
59 1.1 bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
60 1.1 bouyer
61 1.2 bouyer static int xenbus_match(struct device *, struct cfdata *, void *);
62 1.2 bouyer static void xenbus_attach(struct device *, struct device *, void *);
63 1.2 bouyer static int xenbus_print(void *, const char *);
64 1.2 bouyer
65 1.2 bouyer static void xenbus_kthread_create(void *);
66 1.2 bouyer static void xenbus_probe_init(void *);
67 1.2 bouyer
68 1.2 bouyer CFATTACH_DECL(xenbus, sizeof(struct device), xenbus_match, xenbus_attach,
69 1.2 bouyer NULL, NULL);
70 1.2 bouyer
71 1.2 bouyer struct device *xenbus_device;
72 1.2 bouyer
73 1.2 bouyer int
74 1.2 bouyer xenbus_match(struct device *parent, struct cfdata *match, void *aux)
75 1.2 bouyer {
76 1.2 bouyer struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
77 1.2 bouyer
78 1.2 bouyer if (strcmp(xa->xa_device, "xenbus") == 0)
79 1.2 bouyer return 1;
80 1.2 bouyer return 0;
81 1.2 bouyer }
82 1.2 bouyer
83 1.2 bouyer static void
84 1.2 bouyer xenbus_attach(struct device *parent, struct device *self, void *aux)
85 1.2 bouyer {
86 1.2 bouyer aprint_normal(": Xen Virtual Bus Interface\n");
87 1.2 bouyer xenbus_device = self;
88 1.2 bouyer xb_init_comms(self);
89 1.2 bouyer config_pending_incr();
90 1.2 bouyer kthread_create(xenbus_kthread_create, NULL);
91 1.2 bouyer }
92 1.2 bouyer
93 1.2 bouyer void
94 1.2 bouyer xenbus_kthread_create(void *unused)
95 1.2 bouyer {
96 1.2 bouyer struct proc *p;
97 1.2 bouyer int err;
98 1.2 bouyer err = kthread_create1(xenbus_probe_init, NULL, &p, "xenbus_probe");
99 1.2 bouyer if (err)
100 1.2 bouyer printf("kthread_create1(xenbus_probe): %d\n", err);
101 1.2 bouyer }
102 1.2 bouyer
103 1.2 bouyer
104 1.2 bouyer #if 0
105 1.1 bouyer static char *kasprintf(const char *fmt, ...);
106 1.1 bouyer
107 1.1 bouyer static struct notifier_block *xenstore_chain;
108 1.1 bouyer
109 1.1 bouyer /* If something in array of ids matches this device, return it. */
110 1.1 bouyer static const struct xenbus_device_id *
111 1.1 bouyer match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
112 1.1 bouyer {
113 1.1 bouyer for (; !streq(arr->devicetype, ""); arr++) {
114 1.1 bouyer if (streq(arr->devicetype, dev->devicetype))
115 1.1 bouyer return arr;
116 1.1 bouyer }
117 1.1 bouyer return NULL;
118 1.1 bouyer }
119 1.1 bouyer
120 1.2 bouyer static int xenbus_match_device(struct device *_dev, struct device_driver *_drv)
121 1.1 bouyer {
122 1.1 bouyer struct xenbus_driver *drv = to_xenbus_driver(_drv);
123 1.1 bouyer
124 1.1 bouyer if (!drv->ids)
125 1.1 bouyer return 0;
126 1.1 bouyer
127 1.1 bouyer return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
128 1.1 bouyer }
129 1.2 bouyer #endif
130 1.1 bouyer
131 1.1 bouyer struct xen_bus_type
132 1.1 bouyer {
133 1.2 bouyer const char *root;
134 1.1 bouyer unsigned int levels;
135 1.1 bouyer };
136 1.1 bouyer
137 1.2 bouyer #if 0
138 1.1 bouyer /* device/<type>/<id> => <type>-<id> */
139 1.2 bouyer static int
140 1.2 bouyer frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
141 1.1 bouyer {
142 1.1 bouyer nodename = strchr(nodename, '/');
143 1.1 bouyer if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
144 1.1 bouyer printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
145 1.2 bouyer return EINVAL;
146 1.1 bouyer }
147 1.1 bouyer
148 1.1 bouyer strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
149 1.1 bouyer if (!strchr(bus_id, '/')) {
150 1.1 bouyer printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
151 1.2 bouyer return EINVAL;
152 1.1 bouyer }
153 1.1 bouyer *strchr(bus_id, '/') = '-';
154 1.1 bouyer return 0;
155 1.1 bouyer }
156 1.2 bouyer #endif
157 1.1 bouyer
158 1.2 bouyer static int
159 1.2 bouyer read_otherend_details(struct xenbus_device *xendev,
160 1.2 bouyer const char *id_node, const char *path_node)
161 1.2 bouyer {
162 1.2 bouyer int err;
163 1.2 bouyer char *val, *ep;
164 1.1 bouyer
165 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
166 1.2 bouyer if (err) {
167 1.2 bouyer printf("reading other end details %s from %s\n",
168 1.2 bouyer id_node, xendev->xbusd_path);
169 1.2 bouyer xenbus_dev_fatal(xendev, err,
170 1.2 bouyer "reading other end details %s from %s",
171 1.2 bouyer id_node, xendev->xbusd_path);
172 1.2 bouyer return err;
173 1.2 bouyer }
174 1.2 bouyer xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
175 1.2 bouyer if (val[0] == '\0' || *ep != '\0') {
176 1.2 bouyer printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
177 1.2 bouyer xenbus_dev_fatal(xendev, err,
178 1.2 bouyer "reading other end details %s from %s: %s is not a number",
179 1.2 bouyer id_node, xendev->xbusd_path, val);
180 1.2 bouyer free(val, M_DEVBUF);
181 1.2 bouyer return EFTYPE;
182 1.2 bouyer }
183 1.2 bouyer free(val, M_DEVBUF);
184 1.2 bouyer err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
185 1.1 bouyer if (err) {
186 1.2 bouyer printf("reading other end details %s from %s (%d)\n",
187 1.2 bouyer path_node, xendev->xbusd_path, err);
188 1.1 bouyer xenbus_dev_fatal(xendev, err,
189 1.2 bouyer "reading other end details %s from %s",
190 1.2 bouyer path_node, xendev->xbusd_path);
191 1.1 bouyer return err;
192 1.1 bouyer }
193 1.3 bouyer DPRINTK("read_otherend_details: read %s/%s returned %s\n",
194 1.3 bouyer xendev->xbusd_path, path_node, val);
195 1.2 bouyer xendev->xbusd_otherend = val;
196 1.2 bouyer
197 1.2 bouyer if (strlen(xendev->xbusd_otherend) == 0 ||
198 1.2 bouyer !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
199 1.2 bouyer printf("missing other end from %s\n", xendev->xbusd_path);
200 1.1 bouyer xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
201 1.2 bouyer xendev->xbusd_path);
202 1.2 bouyer free(xendev->xbusd_otherend, M_DEVBUF);
203 1.2 bouyer xendev->xbusd_otherend = NULL;
204 1.2 bouyer return ENOENT;
205 1.1 bouyer }
206 1.1 bouyer
207 1.1 bouyer return 0;
208 1.1 bouyer }
209 1.1 bouyer
210 1.2 bouyer static int
211 1.2 bouyer read_backend_details(struct xenbus_device *xendev)
212 1.1 bouyer {
213 1.1 bouyer return read_otherend_details(xendev, "backend-id", "backend");
214 1.1 bouyer }
215 1.1 bouyer
216 1.1 bouyer
217 1.2 bouyer #ifdef DOM0OPS
218 1.2 bouyer static int
219 1.2 bouyer read_frontend_details(struct xenbus_device *xendev)
220 1.1 bouyer {
221 1.1 bouyer return read_otherend_details(xendev, "frontend-id", "frontend");
222 1.1 bouyer }
223 1.2 bouyer #endif
224 1.1 bouyer
225 1.3 bouyer #if unused
226 1.2 bouyer static void
227 1.2 bouyer free_otherend_details(struct xenbus_device *dev)
228 1.1 bouyer {
229 1.2 bouyer free(dev->xbusd_otherend, M_DEVBUF);
230 1.2 bouyer dev->xbusd_otherend = NULL;
231 1.1 bouyer }
232 1.3 bouyer #endif
233 1.1 bouyer
234 1.1 bouyer
235 1.2 bouyer static void
236 1.2 bouyer free_otherend_watch(struct xenbus_device *dev)
237 1.1 bouyer {
238 1.2 bouyer if (dev->xbusd_otherend_watch.node) {
239 1.2 bouyer unregister_xenbus_watch(&dev->xbusd_otherend_watch);
240 1.3 bouyer free(dev->xbusd_otherend_watch.node, M_DEVBUF);
241 1.2 bouyer dev->xbusd_otherend_watch.node = NULL;
242 1.1 bouyer }
243 1.1 bouyer }
244 1.1 bouyer
245 1.3 bouyer #if 0
246 1.1 bouyer
247 1.1 bouyer /* Bus type for frontend drivers. */
248 1.1 bouyer static int xenbus_probe_frontend(const char *type, const char *name);
249 1.2 bouyer #endif
250 1.1 bouyer static struct xen_bus_type xenbus_frontend = {
251 1.1 bouyer .root = "device",
252 1.1 bouyer .levels = 2, /* device/type/<id> */
253 1.1 bouyer };
254 1.1 bouyer
255 1.2 bouyer #if 0
256 1.1 bouyer /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
257 1.2 bouyer static int
258 1.2 bouyer backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
259 1.1 bouyer {
260 1.1 bouyer int domid, err;
261 1.1 bouyer const char *devid, *type, *frontend;
262 1.1 bouyer unsigned int typelen;
263 1.1 bouyer
264 1.1 bouyer type = strchr(nodename, '/');
265 1.1 bouyer if (!type)
266 1.2 bouyer return EINVAL;
267 1.1 bouyer type++;
268 1.1 bouyer typelen = strcspn(type, "/");
269 1.1 bouyer if (!typelen || type[typelen] != '/')
270 1.2 bouyer return EINVAL;
271 1.1 bouyer
272 1.1 bouyer devid = strrchr(nodename, '/') + 1;
273 1.1 bouyer
274 1.1 bouyer err = xenbus_gather(NULL, nodename, "frontend-id", "%i", &domid,
275 1.1 bouyer "frontend", NULL, &frontend,
276 1.1 bouyer NULL);
277 1.1 bouyer if (err)
278 1.1 bouyer return err;
279 1.1 bouyer if (strlen(frontend) == 0)
280 1.2 bouyer err = ERANGE;
281 1.1 bouyer
282 1.1 bouyer if (!err && !xenbus_exists(NULL, frontend, ""))
283 1.2 bouyer err = ENOENT;
284 1.1 bouyer
285 1.1 bouyer if (err) {
286 1.2 bouyer free(frontend, M_DEVBUF);
287 1.1 bouyer return err;
288 1.1 bouyer }
289 1.1 bouyer
290 1.1 bouyer if (snprintf(bus_id, BUS_ID_SIZE,
291 1.1 bouyer "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
292 1.2 bouyer return ENOSPC;
293 1.1 bouyer return 0;
294 1.1 bouyer }
295 1.1 bouyer
296 1.2 bouyer static int
297 1.2 bouyer xenbus_hotplug_backend(struct device *dev, char **envp,
298 1.1 bouyer int num_envp, char *buffer, int buffer_size)
299 1.1 bouyer {
300 1.1 bouyer struct xenbus_device *xdev;
301 1.1 bouyer struct xenbus_driver *drv = NULL;
302 1.1 bouyer int i = 0;
303 1.1 bouyer int length = 0;
304 1.1 bouyer char *basepath_end;
305 1.1 bouyer char *frontend_id;
306 1.1 bouyer
307 1.1 bouyer DPRINTK("");
308 1.1 bouyer
309 1.1 bouyer if (dev == NULL)
310 1.2 bouyer return ENODEV;
311 1.1 bouyer
312 1.1 bouyer xdev = to_xenbus_device(dev);
313 1.1 bouyer if (xdev == NULL)
314 1.2 bouyer return ENODEV;
315 1.1 bouyer
316 1.1 bouyer if (dev->driver)
317 1.1 bouyer drv = to_xenbus_driver(dev->driver);
318 1.1 bouyer
319 1.1 bouyer /* stuff we want to pass to /sbin/hotplug */
320 1.1 bouyer add_hotplug_env_var(envp, num_envp, &i,
321 1.1 bouyer buffer, buffer_size, &length,
322 1.1 bouyer "XENBUS_TYPE=%s", xdev->devicetype);
323 1.1 bouyer
324 1.1 bouyer add_hotplug_env_var(envp, num_envp, &i,
325 1.1 bouyer buffer, buffer_size, &length,
326 1.2 bouyer "XENBUS_PATH=%s", xdev->xbusd_path);
327 1.1 bouyer
328 1.1 bouyer add_hotplug_env_var(envp, num_envp, &i,
329 1.1 bouyer buffer, buffer_size, &length,
330 1.2 bouyer "XENBUS_BASE_PATH=%s", xdev->xbusd_path);
331 1.1 bouyer
332 1.1 bouyer basepath_end = strrchr(envp[i - 1], '/');
333 1.1 bouyer length -= strlen(basepath_end);
334 1.1 bouyer *basepath_end = '\0';
335 1.1 bouyer basepath_end = strrchr(envp[i - 1], '/');
336 1.1 bouyer length -= strlen(basepath_end);
337 1.1 bouyer *basepath_end = '\0';
338 1.1 bouyer
339 1.1 bouyer basepath_end++;
340 1.1 bouyer frontend_id = kmalloc(strlen(basepath_end) + 1, GFP_KERNEL);
341 1.1 bouyer strcpy(frontend_id, basepath_end);
342 1.1 bouyer add_hotplug_env_var(envp, num_envp, &i,
343 1.1 bouyer buffer, buffer_size, &length,
344 1.1 bouyer "XENBUS_FRONTEND_ID=%s", frontend_id);
345 1.2 bouyer free(frontend_id, M_DEVBUF);
346 1.1 bouyer
347 1.1 bouyer /* terminate, set to next free slot, shrink available space */
348 1.1 bouyer envp[i] = NULL;
349 1.1 bouyer envp = &envp[i];
350 1.1 bouyer num_envp -= i;
351 1.1 bouyer buffer = &buffer[length];
352 1.1 bouyer buffer_size -= length;
353 1.1 bouyer
354 1.1 bouyer if (drv && drv->hotplug)
355 1.1 bouyer return drv->hotplug(xdev, envp, num_envp, buffer,
356 1.1 bouyer buffer_size);
357 1.1 bouyer
358 1.1 bouyer return 0;
359 1.1 bouyer }
360 1.1 bouyer
361 1.1 bouyer static int xenbus_probe_backend(const char *type, const char *domid);
362 1.2 bouyer #endif
363 1.2 bouyer
364 1.1 bouyer static struct xen_bus_type xenbus_backend = {
365 1.1 bouyer .root = "backend",
366 1.1 bouyer .levels = 3, /* backend/type/<frontend>/<id> */
367 1.1 bouyer };
368 1.1 bouyer
369 1.2 bouyer static void
370 1.2 bouyer otherend_changed(struct xenbus_watch *watch,
371 1.1 bouyer const char **vec, unsigned int len)
372 1.1 bouyer {
373 1.3 bouyer struct xenbus_device *xdev = watch->xbw_dev;
374 1.1 bouyer XenbusState state;
375 1.1 bouyer
376 1.1 bouyer /* Protect us against watches firing on old details when the otherend
377 1.1 bouyer details change, say immediately after a resume. */
378 1.3 bouyer if (!xdev->xbusd_otherend ||
379 1.3 bouyer strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
380 1.3 bouyer strlen(xdev->xbusd_otherend))) {
381 1.1 bouyer DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
382 1.1 bouyer return;
383 1.1 bouyer }
384 1.1 bouyer
385 1.3 bouyer state = xenbus_read_driver_state(xdev->xbusd_otherend);
386 1.1 bouyer
387 1.1 bouyer DPRINTK("state is %d, %s, %s",
388 1.3 bouyer state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
389 1.3 bouyer if (xdev->xbusd_otherend_changed)
390 1.3 bouyer xdev->xbusd_otherend_changed(xdev->xbusd_data, state);
391 1.1 bouyer }
392 1.1 bouyer
393 1.2 bouyer static int
394 1.2 bouyer talk_to_otherend(struct xenbus_device *dev)
395 1.1 bouyer {
396 1.1 bouyer free_otherend_watch(dev);
397 1.1 bouyer
398 1.2 bouyer return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
399 1.3 bouyer &dev->xbusd_otherend_watch,
400 1.3 bouyer otherend_changed);
401 1.1 bouyer }
402 1.1 bouyer
403 1.3 bouyer #if 0
404 1.1 bouyer
405 1.2 bouyer static int
406 1.2 bouyer xenbus_dev_probe(struct device *_dev)
407 1.1 bouyer {
408 1.1 bouyer struct xenbus_device *dev = to_xenbus_device(_dev);
409 1.1 bouyer struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
410 1.1 bouyer const struct xenbus_device_id *id;
411 1.1 bouyer int err;
412 1.1 bouyer
413 1.1 bouyer DPRINTK("");
414 1.1 bouyer
415 1.1 bouyer err = talk_to_otherend(dev);
416 1.1 bouyer if (err) {
417 1.1 bouyer printk(KERN_WARNING
418 1.1 bouyer "xenbus_probe: talk_to_otherend on %s failed.\n",
419 1.2 bouyer dev->xbusd_path);
420 1.1 bouyer return err;
421 1.1 bouyer }
422 1.1 bouyer
423 1.1 bouyer if (!drv->probe) {
424 1.2 bouyer err = ENODEV;
425 1.1 bouyer goto fail;
426 1.1 bouyer }
427 1.1 bouyer
428 1.1 bouyer id = match_device(drv->ids, dev);
429 1.1 bouyer if (!id) {
430 1.2 bouyer err = ENODEV;
431 1.1 bouyer goto fail;
432 1.1 bouyer }
433 1.1 bouyer
434 1.1 bouyer err = drv->probe(dev, id);
435 1.1 bouyer if (err)
436 1.1 bouyer goto fail;
437 1.1 bouyer
438 1.1 bouyer return 0;
439 1.1 bouyer fail:
440 1.2 bouyer xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->xbusd_path);
441 1.1 bouyer xenbus_switch_state(dev, NULL, XenbusStateClosed);
442 1.2 bouyer return ENODEV;
443 1.1 bouyer
444 1.1 bouyer }
445 1.1 bouyer
446 1.2 bouyer static int
447 1.2 bouyer xenbus_dev_remove(struct device *_dev)
448 1.1 bouyer {
449 1.1 bouyer struct xenbus_device *dev = to_xenbus_device(_dev);
450 1.1 bouyer struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
451 1.1 bouyer
452 1.1 bouyer DPRINTK("");
453 1.1 bouyer
454 1.1 bouyer free_otherend_watch(dev);
455 1.1 bouyer free_otherend_details(dev);
456 1.1 bouyer
457 1.1 bouyer if (drv->remove)
458 1.1 bouyer drv->remove(dev);
459 1.1 bouyer
460 1.1 bouyer xenbus_switch_state(dev, NULL, XenbusStateClosed);
461 1.1 bouyer return 0;
462 1.1 bouyer }
463 1.1 bouyer
464 1.2 bouyer static int
465 1.2 bouyer xenbus_register_driver_common(struct xenbus_driver *drv,
466 1.1 bouyer struct xen_bus_type *bus)
467 1.1 bouyer {
468 1.1 bouyer int ret;
469 1.1 bouyer
470 1.1 bouyer drv->driver.name = drv->name;
471 1.1 bouyer drv->driver.bus = &bus->bus;
472 1.1 bouyer drv->driver.owner = drv->owner;
473 1.1 bouyer drv->driver.probe = xenbus_dev_probe;
474 1.1 bouyer drv->driver.remove = xenbus_dev_remove;
475 1.1 bouyer
476 1.1 bouyer down(&xenwatch_mutex);
477 1.1 bouyer ret = driver_register(&drv->driver);
478 1.1 bouyer up(&xenwatch_mutex);
479 1.1 bouyer return ret;
480 1.1 bouyer }
481 1.1 bouyer
482 1.2 bouyer int
483 1.2 bouyer xenbus_register_frontend(struct xenbus_driver *drv)
484 1.1 bouyer {
485 1.1 bouyer drv->read_otherend_details = read_backend_details;
486 1.1 bouyer
487 1.1 bouyer return xenbus_register_driver_common(drv, &xenbus_frontend);
488 1.1 bouyer }
489 1.1 bouyer
490 1.2 bouyer int
491 1.2 bouyer xenbus_register_backend(struct xenbus_driver *drv)
492 1.1 bouyer {
493 1.1 bouyer drv->read_otherend_details = read_frontend_details;
494 1.1 bouyer
495 1.1 bouyer return xenbus_register_driver_common(drv, &xenbus_backend);
496 1.1 bouyer }
497 1.1 bouyer
498 1.2 bouyer void
499 1.2 bouyer xenbus_unregister_driver(struct xenbus_driver *drv)
500 1.1 bouyer {
501 1.1 bouyer driver_unregister(&drv->driver);
502 1.1 bouyer }
503 1.1 bouyer
504 1.1 bouyer struct xb_find_info
505 1.1 bouyer {
506 1.1 bouyer struct xenbus_device *dev;
507 1.1 bouyer const char *nodename;
508 1.1 bouyer };
509 1.1 bouyer
510 1.2 bouyer static int
511 1.2 bouyer cmp_dev(struct device *dev, void *data)
512 1.1 bouyer {
513 1.1 bouyer struct xenbus_device *xendev = to_xenbus_device(dev);
514 1.1 bouyer struct xb_find_info *info = data;
515 1.1 bouyer
516 1.2 bouyer if (streq(xendev->xbusd_path, info->nodename)) {
517 1.1 bouyer info->dev = xendev;
518 1.1 bouyer get_device(dev);
519 1.1 bouyer return 1;
520 1.1 bouyer }
521 1.1 bouyer return 0;
522 1.1 bouyer }
523 1.1 bouyer
524 1.2 bouyer struct xenbus_device *
525 1.2 bouyer xenbus_device_find(const char *nodename, struct bus_type *bus)
526 1.1 bouyer {
527 1.1 bouyer struct xb_find_info info = { .dev = NULL, .nodename = nodename };
528 1.1 bouyer
529 1.1 bouyer bus_for_each_dev(bus, NULL, &info, cmp_dev);
530 1.1 bouyer return info.dev;
531 1.1 bouyer }
532 1.1 bouyer
533 1.2 bouyer static int
534 1.2 bouyer cleanup_dev(struct device *dev, void *data)
535 1.1 bouyer {
536 1.1 bouyer struct xenbus_device *xendev = to_xenbus_device(dev);
537 1.1 bouyer struct xb_find_info *info = data;
538 1.1 bouyer int len = strlen(info->nodename);
539 1.1 bouyer
540 1.1 bouyer DPRINTK("%s", info->nodename);
541 1.1 bouyer
542 1.2 bouyer if (!strncmp(xendev->xbusd_path, info->nodename, len)) {
543 1.1 bouyer info->dev = xendev;
544 1.1 bouyer get_device(dev);
545 1.1 bouyer return 1;
546 1.1 bouyer }
547 1.1 bouyer return 0;
548 1.1 bouyer }
549 1.1 bouyer
550 1.2 bouyer static void
551 1.2 bouyer xenbus_cleanup_devices(const char *path, struct bus_type *bus)
552 1.1 bouyer {
553 1.1 bouyer struct xb_find_info info = { .nodename = path };
554 1.1 bouyer
555 1.1 bouyer do {
556 1.1 bouyer info.dev = NULL;
557 1.1 bouyer bus_for_each_dev(bus, NULL, &info, cleanup_dev);
558 1.1 bouyer if (info.dev) {
559 1.1 bouyer device_unregister(&info.dev->dev);
560 1.1 bouyer put_device(&info.dev->dev);
561 1.1 bouyer }
562 1.1 bouyer } while (info.dev);
563 1.1 bouyer }
564 1.1 bouyer
565 1.2 bouyer static void
566 1.2 bouyer xenbus_dev_free(struct xenbus_device *xendev)
567 1.1 bouyer {
568 1.2 bouyer free(xendev, M_DEVBUF);
569 1.1 bouyer }
570 1.1 bouyer
571 1.2 bouyer static void
572 1.2 bouyer xenbus_dev_release(struct device *dev)
573 1.1 bouyer {
574 1.1 bouyer if (dev) {
575 1.1 bouyer xenbus_dev_free(to_xenbus_device(dev));
576 1.1 bouyer }
577 1.1 bouyer }
578 1.1 bouyer
579 1.1 bouyer /* Simplified asprintf. */
580 1.1 bouyer static char *kasprintf(const char *fmt, ...)
581 1.1 bouyer {
582 1.1 bouyer va_list ap;
583 1.1 bouyer unsigned int len;
584 1.1 bouyer char *p, dummy[1];
585 1.1 bouyer
586 1.1 bouyer va_start(ap, fmt);
587 1.1 bouyer /* FIXME: vsnprintf has a bug, NULL should work */
588 1.1 bouyer len = vsnprintf(dummy, 0, fmt, ap);
589 1.1 bouyer va_end(ap);
590 1.1 bouyer
591 1.2 bouyer p = malloc(len + 1, M_DEVBUF, M_NOWAIT);
592 1.1 bouyer if (!p)
593 1.1 bouyer return NULL;
594 1.1 bouyer va_start(ap, fmt);
595 1.1 bouyer vsprintf(p, fmt, ap);
596 1.1 bouyer va_end(ap);
597 1.1 bouyer return p;
598 1.1 bouyer }
599 1.2 bouyer #endif
600 1.1 bouyer
601 1.2 bouyer #if 0
602 1.2 bouyer static ssize_t
603 1.2 bouyer xendev_show_nodename(struct device *dev, char *buf)
604 1.1 bouyer {
605 1.1 bouyer return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
606 1.1 bouyer }
607 1.2 bouyer // XXX implement
608 1.1 bouyer DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
609 1.1 bouyer
610 1.2 bouyer static ssize_t
611 1.2 bouyer xendev_show_devtype(struct device *dev, char *buf)
612 1.1 bouyer {
613 1.1 bouyer return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
614 1.1 bouyer }
615 1.1 bouyer DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
616 1.1 bouyer
617 1.1 bouyer
618 1.2 bouyer static int
619 1.2 bouyer xenbus_probe_node(struct xen_bus_type *bus,
620 1.1 bouyer const char *type,
621 1.1 bouyer const char *nodename)
622 1.1 bouyer {
623 1.1 bouyer #define CHECK_FAIL \
624 1.1 bouyer do { \
625 1.1 bouyer if (err) \
626 1.1 bouyer goto fail; \
627 1.1 bouyer } \
628 1.1 bouyer while (0) \
629 1.1 bouyer
630 1.1 bouyer
631 1.1 bouyer struct xenbus_device *xendev;
632 1.1 bouyer size_t stringlen;
633 1.1 bouyer char *tmpstring;
634 1.1 bouyer
635 1.1 bouyer XenbusState state = xenbus_read_driver_state(nodename);
636 1.1 bouyer
637 1.2 bouyer printf("xenbus_probe_node %s %s %s %d\n", bus->root, type, nodename, state);
638 1.1 bouyer if (state != XenbusStateInitialising) {
639 1.1 bouyer /* Device is not new, so ignore it. This can happen if a
640 1.1 bouyer device is going away after switching to Closed. */
641 1.1 bouyer return 0;
642 1.1 bouyer }
643 1.1 bouyer
644 1.2 bouyer
645 1.1 bouyer stringlen = strlen(nodename) + 1 + strlen(type) + 1;
646 1.2 bouyer xendev = malloc(sizeof(*xendev) + stringlen, M_DEVBUF, M_NOWAIT);
647 1.1 bouyer if (!xendev)
648 1.2 bouyer return ENOMEM;
649 1.1 bouyer memset(xendev, 0, sizeof(*xendev));
650 1.1 bouyer
651 1.1 bouyer /* Copy the strings into the extra space. */
652 1.1 bouyer
653 1.1 bouyer tmpstring = (char *)(xendev + 1);
654 1.1 bouyer strcpy(tmpstring, nodename);
655 1.1 bouyer xendev->nodename = tmpstring;
656 1.1 bouyer
657 1.1 bouyer tmpstring += strlen(tmpstring) + 1;
658 1.1 bouyer strcpy(tmpstring, type);
659 1.1 bouyer xendev->devicetype = tmpstring;
660 1.1 bouyer
661 1.2 bouyer #if 0
662 1.1 bouyer xendev->dev.parent = &bus->dev;
663 1.1 bouyer xendev->dev.bus = &bus->bus;
664 1.1 bouyer xendev->dev.release = xenbus_dev_release;
665 1.1 bouyer
666 1.1 bouyer err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
667 1.1 bouyer CHECK_FAIL;
668 1.1 bouyer
669 1.1 bouyer /* Register with generic device framework. */
670 1.1 bouyer err = device_register(&xendev->dev);
671 1.1 bouyer CHECK_FAIL;
672 1.1 bouyer
673 1.1 bouyer device_create_file(&xendev->dev, &dev_attr_nodename);
674 1.1 bouyer device_create_file(&xendev->dev, &dev_attr_devtype);
675 1.1 bouyer
676 1.2 bouyer #endif
677 1.1 bouyer return 0;
678 1.1 bouyer
679 1.1 bouyer #undef CHECK_FAIL
680 1.2 bouyer #if 0
681 1.1 bouyer fail:
682 1.1 bouyer xenbus_dev_free(xendev);
683 1.1 bouyer return err;
684 1.2 bouyer #endif
685 1.1 bouyer }
686 1.2 bouyer #endif
687 1.1 bouyer
688 1.2 bouyer #if 0
689 1.1 bouyer /* device/<typename>/<name> */
690 1.2 bouyer static int
691 1.2 bouyer xenbus_probe_frontend(const char *type, const char *name)
692 1.1 bouyer {
693 1.1 bouyer char *nodename;
694 1.1 bouyer int err;
695 1.1 bouyer
696 1.1 bouyer nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
697 1.1 bouyer if (!nodename)
698 1.2 bouyer return ENOMEM;
699 1.1 bouyer
700 1.1 bouyer DPRINTK("%s", nodename);
701 1.1 bouyer
702 1.1 bouyer err = xenbus_probe_node(&xenbus_frontend, type, nodename);
703 1.2 bouyer free(nodename, M_DEVBUF);
704 1.1 bouyer return err;
705 1.1 bouyer }
706 1.1 bouyer
707 1.1 bouyer /* backend/<typename>/<frontend-uuid>/<name> */
708 1.2 bouyer static int
709 1.2 bouyer xenbus_probe_backend_unit(const char *dir,
710 1.1 bouyer const char *type,
711 1.1 bouyer const char *name)
712 1.1 bouyer {
713 1.1 bouyer char *nodename;
714 1.1 bouyer int err;
715 1.1 bouyer
716 1.1 bouyer nodename = kasprintf("%s/%s", dir, name);
717 1.1 bouyer if (!nodename)
718 1.2 bouyer return ENOMEM;
719 1.1 bouyer
720 1.2 bouyer DPRINTK("%s", nodename);
721 1.1 bouyer
722 1.1 bouyer err = xenbus_probe_node(&xenbus_backend, type, nodename);
723 1.2 bouyer free(nodename, M_DEVBUF);
724 1.1 bouyer return err;
725 1.1 bouyer }
726 1.1 bouyer
727 1.1 bouyer /* backend/<typename>/<frontend-domid> */
728 1.2 bouyer static int
729 1.2 bouyer xenbus_probe_backend(const char *type, const char *domid)
730 1.1 bouyer {
731 1.1 bouyer char *nodename;
732 1.2 bouyer int err;
733 1.1 bouyer char **dir;
734 1.1 bouyer unsigned int i, dir_n = 0;
735 1.1 bouyer
736 1.1 bouyer DPRINTK("");
737 1.1 bouyer
738 1.1 bouyer nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
739 1.1 bouyer if (!nodename)
740 1.2 bouyer return ENOMEM;
741 1.1 bouyer
742 1.2 bouyer err = xenbus_directory(NULL, nodename, "", &dir_n, &dir);
743 1.2 bouyer DPRINTK("xenbus_probe_backend err %d dir_n %d", err, dir_n);
744 1.2 bouyer if (err) {
745 1.2 bouyer free(nodename, M_DEVBUF);
746 1.2 bouyer return err;
747 1.1 bouyer }
748 1.1 bouyer
749 1.1 bouyer for (i = 0; i < dir_n; i++) {
750 1.1 bouyer err = xenbus_probe_backend_unit(nodename, type, dir[i]);
751 1.1 bouyer if (err)
752 1.1 bouyer break;
753 1.1 bouyer }
754 1.2 bouyer free(dir, M_DEVBUF);
755 1.2 bouyer free(nodename, M_DEVBUF);
756 1.1 bouyer return err;
757 1.1 bouyer }
758 1.2 bouyer #endif
759 1.1 bouyer
760 1.2 bouyer static int
761 1.2 bouyer xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
762 1.1 bouyer {
763 1.2 bouyer int err;
764 1.1 bouyer char **dir;
765 1.1 bouyer unsigned int dir_n = 0;
766 1.1 bouyer int i;
767 1.2 bouyer struct xenbus_device *xbusd;
768 1.2 bouyer struct xenbusdev_attach_args xa;
769 1.2 bouyer char *ep;
770 1.2 bouyer
771 1.2 bouyer DPRINTK("probe %s type %s", bus->root, type);
772 1.2 bouyer err = xenbus_directory(NULL, bus->root, type, &dir_n, &dir);
773 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
774 1.2 bouyer if (err)
775 1.2 bouyer return err;
776 1.1 bouyer
777 1.1 bouyer for (i = 0; i < dir_n; i++) {
778 1.2 bouyer xbusd = malloc(sizeof(*xbusd), M_DEVBUF, M_WAITOK);
779 1.2 bouyer if (xbusd == NULL)
780 1.2 bouyer panic("can't malloc xbusd");
781 1.2 bouyer
782 1.2 bouyer snprintf(__UNCONST(xbusd->xbusd_path),
783 1.2 bouyer sizeof(xbusd->xbusd_path), "%s/%s/%s",
784 1.2 bouyer bus->root, type, dir[i]);
785 1.3 bouyer xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
786 1.2 bouyer DPRINTK("xenbus_probe_device_type probe %s\n",
787 1.2 bouyer xbusd->xbusd_path);
788 1.2 bouyer xa.xa_xbusd = xbusd;
789 1.2 bouyer xa.xa_type = type;
790 1.2 bouyer xa.xa_id = strtoul(dir[i], &ep, 0);
791 1.2 bouyer if (dir[i][0] == '\0' || *ep != '\0') {
792 1.2 bouyer printf("xenbus device type %s: id %s is not a number\n",
793 1.2 bouyer type, dir[i]);
794 1.2 bouyer err = EFTYPE;
795 1.2 bouyer free(xbusd, M_DEVBUF);
796 1.2 bouyer break;
797 1.2 bouyer }
798 1.2 bouyer err = read_backend_details(xbusd);
799 1.2 bouyer if (err != 0) {
800 1.2 bouyer printf("xenbus: can't get backend details "
801 1.2 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
802 1.1 bouyer break;
803 1.2 bouyer }
804 1.2 bouyer if (config_found_ia(xenbus_device, "xenbus", &xa,
805 1.2 bouyer xenbus_print) == NULL)
806 1.2 bouyer free(xbusd, M_DEVBUF);
807 1.3 bouyer else {
808 1.3 bouyer talk_to_otherend(xbusd);
809 1.3 bouyer }
810 1.1 bouyer }
811 1.2 bouyer free(dir, M_DEVBUF);
812 1.1 bouyer return err;
813 1.1 bouyer }
814 1.1 bouyer
815 1.2 bouyer static int
816 1.2 bouyer xenbus_print(void *aux, const char *pnp)
817 1.2 bouyer {
818 1.2 bouyer struct xenbusdev_attach_args *xa = aux;
819 1.2 bouyer
820 1.2 bouyer if (pnp) {
821 1.2 bouyer if (strcmp(xa->xa_type, "vbd") == 0)
822 1.2 bouyer aprint_normal("xbd");
823 1.2 bouyer else if (strcmp(xa->xa_type, "vif") == 0)
824 1.2 bouyer aprint_normal("xennet");
825 1.2 bouyer else
826 1.2 bouyer aprint_normal("unknown type %s", xa->xa_type);
827 1.2 bouyer aprint_normal(" at %s", pnp);
828 1.2 bouyer }
829 1.2 bouyer aprint_normal(" id %d", xa->xa_id);
830 1.2 bouyer return(UNCONF);
831 1.2 bouyer }
832 1.2 bouyer
833 1.2 bouyer static int
834 1.2 bouyer xenbus_probe_devices(struct xen_bus_type *bus)
835 1.1 bouyer {
836 1.2 bouyer int err;
837 1.1 bouyer char **dir;
838 1.1 bouyer unsigned int i, dir_n;
839 1.1 bouyer
840 1.2 bouyer DPRINTK("probe %s", bus->root);
841 1.2 bouyer err = xenbus_directory(NULL, bus->root, "", &dir_n, &dir);
842 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
843 1.2 bouyer if (err)
844 1.2 bouyer return err;
845 1.1 bouyer
846 1.1 bouyer for (i = 0; i < dir_n; i++) {
847 1.1 bouyer err = xenbus_probe_device_type(bus, dir[i]);
848 1.1 bouyer if (err)
849 1.1 bouyer break;
850 1.1 bouyer }
851 1.2 bouyer free(dir, M_DEVBUF);
852 1.1 bouyer return err;
853 1.1 bouyer }
854 1.1 bouyer
855 1.2 bouyer #if 0
856 1.2 bouyer static unsigned int
857 1.2 bouyer char_count(const char *str, char c)
858 1.1 bouyer {
859 1.1 bouyer unsigned int i, ret = 0;
860 1.1 bouyer
861 1.1 bouyer for (i = 0; str[i]; i++)
862 1.1 bouyer if (str[i] == c)
863 1.1 bouyer ret++;
864 1.1 bouyer return ret;
865 1.1 bouyer }
866 1.1 bouyer
867 1.2 bouyer static int
868 1.2 bouyer strsep_len(const char *str, char c, unsigned int len)
869 1.1 bouyer {
870 1.1 bouyer unsigned int i;
871 1.1 bouyer
872 1.1 bouyer for (i = 0; str[i]; i++)
873 1.1 bouyer if (str[i] == c) {
874 1.1 bouyer if (len == 0)
875 1.1 bouyer return i;
876 1.1 bouyer len--;
877 1.1 bouyer }
878 1.1 bouyer return (len == 0) ? i : -ERANGE;
879 1.1 bouyer }
880 1.1 bouyer
881 1.2 bouyer static void
882 1.2 bouyer dev_changed(const char *node, struct xen_bus_type *bus)
883 1.1 bouyer {
884 1.1 bouyer int exists, rootlen;
885 1.1 bouyer struct xenbus_device *dev;
886 1.1 bouyer char type[BUS_ID_SIZE];
887 1.1 bouyer const char *p, *root;
888 1.1 bouyer
889 1.1 bouyer if (char_count(node, '/') < 2)
890 1.1 bouyer return;
891 1.1 bouyer
892 1.1 bouyer exists = xenbus_exists(NULL, node, "");
893 1.1 bouyer if (!exists) {
894 1.1 bouyer xenbus_cleanup_devices(node, &bus->bus);
895 1.1 bouyer return;
896 1.1 bouyer }
897 1.1 bouyer
898 1.1 bouyer /* backend/<type>/... or device/<type>/... */
899 1.1 bouyer p = strchr(node, '/') + 1;
900 1.1 bouyer snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
901 1.1 bouyer type[BUS_ID_SIZE-1] = '\0';
902 1.1 bouyer
903 1.1 bouyer rootlen = strsep_len(node, '/', bus->levels);
904 1.1 bouyer if (rootlen < 0)
905 1.1 bouyer return;
906 1.1 bouyer root = kasprintf("%.*s", rootlen, node);
907 1.1 bouyer if (!root)
908 1.1 bouyer return;
909 1.1 bouyer
910 1.1 bouyer dev = xenbus_device_find(root, &bus->bus);
911 1.1 bouyer if (!dev)
912 1.1 bouyer xenbus_probe_node(bus, type, root);
913 1.1 bouyer else
914 1.1 bouyer put_device(&dev->dev);
915 1.1 bouyer
916 1.2 bouyer free(root, M_DEVBUF);
917 1.1 bouyer }
918 1.2 bouyer #endif
919 1.1 bouyer
920 1.2 bouyer static void
921 1.2 bouyer frontend_changed(struct xenbus_watch *watch,
922 1.1 bouyer const char **vec, unsigned int len)
923 1.1 bouyer {
924 1.1 bouyer DPRINTK("");
925 1.2 bouyer printf("frontend_changed %s\n", vec[XS_WATCH_PATH]);
926 1.1 bouyer
927 1.2 bouyer //dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
928 1.1 bouyer }
929 1.1 bouyer
930 1.2 bouyer static void
931 1.2 bouyer backend_changed(struct xenbus_watch *watch,
932 1.1 bouyer const char **vec, unsigned int len)
933 1.1 bouyer {
934 1.1 bouyer DPRINTK("");
935 1.1 bouyer
936 1.2 bouyer printf("backend_changed %s\n", vec[XS_WATCH_PATH]);
937 1.2 bouyer //dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
938 1.1 bouyer }
939 1.1 bouyer
940 1.2 bouyer
941 1.1 bouyer /* We watch for devices appearing and vanishing. */
942 1.3 bouyer static struct xenbus_watch fe_watch;
943 1.1 bouyer
944 1.3 bouyer static struct xenbus_watch be_watch;
945 1.1 bouyer
946 1.2 bouyer #if 0
947 1.1 bouyer static int suspend_dev(struct device *dev, void *data)
948 1.1 bouyer {
949 1.1 bouyer int err = 0;
950 1.1 bouyer struct xenbus_driver *drv;
951 1.1 bouyer struct xenbus_device *xdev;
952 1.1 bouyer
953 1.1 bouyer DPRINTK("");
954 1.1 bouyer
955 1.1 bouyer if (dev->driver == NULL)
956 1.1 bouyer return 0;
957 1.1 bouyer drv = to_xenbus_driver(dev->driver);
958 1.1 bouyer xdev = container_of(dev, struct xenbus_device, dev);
959 1.1 bouyer if (drv->suspend)
960 1.1 bouyer err = drv->suspend(xdev);
961 1.1 bouyer if (err)
962 1.1 bouyer printk(KERN_WARNING
963 1.1 bouyer "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
964 1.1 bouyer return 0;
965 1.1 bouyer }
966 1.1 bouyer
967 1.2 bouyer static int
968 1.2 bouyer resume_dev(struct device *dev, void *data)
969 1.1 bouyer {
970 1.1 bouyer int err;
971 1.1 bouyer struct xenbus_driver *drv;
972 1.1 bouyer struct xenbus_device *xdev;
973 1.1 bouyer
974 1.1 bouyer DPRINTK("");
975 1.1 bouyer
976 1.1 bouyer if (dev->driver == NULL)
977 1.1 bouyer return 0;
978 1.1 bouyer drv = to_xenbus_driver(dev->driver);
979 1.1 bouyer xdev = container_of(dev, struct xenbus_device, dev);
980 1.1 bouyer
981 1.1 bouyer err = talk_to_otherend(xdev);
982 1.1 bouyer if (err) {
983 1.1 bouyer printk(KERN_WARNING
984 1.1 bouyer "xenbus: resume (talk_to_otherend) %s failed: %i\n",
985 1.1 bouyer dev->bus_id, err);
986 1.1 bouyer return err;
987 1.1 bouyer }
988 1.1 bouyer
989 1.1 bouyer if (drv->resume)
990 1.1 bouyer err = drv->resume(xdev);
991 1.1 bouyer if (err)
992 1.1 bouyer printk(KERN_WARNING
993 1.1 bouyer "xenbus: resume %s failed: %i\n", dev->bus_id, err);
994 1.1 bouyer return err;
995 1.1 bouyer }
996 1.1 bouyer
997 1.2 bouyer void
998 1.2 bouyer xenbus_suspend(void)
999 1.1 bouyer {
1000 1.1 bouyer DPRINTK("");
1001 1.1 bouyer
1002 1.1 bouyer bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
1003 1.1 bouyer bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
1004 1.1 bouyer xs_suspend();
1005 1.1 bouyer }
1006 1.1 bouyer
1007 1.2 bouyer void xenbus_resume(struct device *dev)
1008 1.1 bouyer {
1009 1.2 bouyer xb_init_comms(dev);
1010 1.1 bouyer xs_resume();
1011 1.1 bouyer bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
1012 1.1 bouyer bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
1013 1.1 bouyer }
1014 1.2 bouyer #endif
1015 1.1 bouyer
1016 1.1 bouyer
1017 1.1 bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
1018 1.1 bouyer int xenstored_ready = 0;
1019 1.1 bouyer
1020 1.1 bouyer
1021 1.2 bouyer #if 0
1022 1.2 bouyer int
1023 1.2 bouyer register_xenstore_notifier(struct notifier_block *nb)
1024 1.1 bouyer {
1025 1.1 bouyer int ret = 0;
1026 1.1 bouyer
1027 1.1 bouyer if (xenstored_ready > 0)
1028 1.1 bouyer ret = nb->notifier_call(nb, 0, NULL);
1029 1.1 bouyer else
1030 1.1 bouyer notifier_chain_register(&xenstore_chain, nb);
1031 1.1 bouyer
1032 1.1 bouyer return ret;
1033 1.1 bouyer }
1034 1.1 bouyer
1035 1.1 bouyer void unregister_xenstore_notifier(struct notifier_block *nb)
1036 1.1 bouyer {
1037 1.1 bouyer notifier_chain_unregister(&xenstore_chain, nb);
1038 1.1 bouyer }
1039 1.2 bouyer #endif
1040 1.1 bouyer
1041 1.1 bouyer
1042 1.1 bouyer
1043 1.2 bouyer void
1044 1.2 bouyer xenbus_probe(void *unused)
1045 1.1 bouyer {
1046 1.2 bouyer KASSERT((xenstored_ready > 0));
1047 1.1 bouyer
1048 1.1 bouyer /* Enumerate devices in xenstore. */
1049 1.1 bouyer xenbus_probe_devices(&xenbus_frontend);
1050 1.1 bouyer xenbus_probe_devices(&xenbus_backend);
1051 1.1 bouyer
1052 1.1 bouyer /* Watch for changes. */
1053 1.3 bouyer fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
1054 1.3 bouyer strcpy(fe_watch.node, "device");
1055 1.3 bouyer fe_watch.xbw_callback = frontend_changed;
1056 1.1 bouyer register_xenbus_watch(&fe_watch);
1057 1.3 bouyer be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
1058 1.3 bouyer strcpy(be_watch.node, "backend");
1059 1.3 bouyer be_watch.xbw_callback = backend_changed;
1060 1.1 bouyer register_xenbus_watch(&be_watch);
1061 1.1 bouyer
1062 1.1 bouyer /* Notify others that xenstore is up */
1063 1.2 bouyer //notifier_call_chain(&xenstore_chain, 0, NULL);
1064 1.1 bouyer }
1065 1.1 bouyer
1066 1.2 bouyer #if 0
1067 1.1 bouyer static struct proc_dir_entry *xsd_mfn_intf;
1068 1.1 bouyer static struct proc_dir_entry *xsd_port_intf;
1069 1.1 bouyer
1070 1.1 bouyer
1071 1.2 bouyer static int
1072 1.2 bouyer xsd_mfn_read(char *page, char **start, off_t off,
1073 1.1 bouyer int count, int *eof, void *data)
1074 1.1 bouyer {
1075 1.1 bouyer int len;
1076 1.2 bouyer len = sprintf(page, "%ld", xen_start_info.store_mfn);
1077 1.1 bouyer *eof = 1;
1078 1.1 bouyer return len;
1079 1.1 bouyer }
1080 1.1 bouyer
1081 1.2 bouyer static int
1082 1.2 bouyer xsd_port_read(char *page, char **start, off_t off,
1083 1.1 bouyer int count, int *eof, void *data)
1084 1.1 bouyer {
1085 1.1 bouyer int len;
1086 1.1 bouyer
1087 1.2 bouyer len = sprintf(page, "%d", xen_start_info.store_evtchn);
1088 1.1 bouyer *eof = 1;
1089 1.1 bouyer return len;
1090 1.1 bouyer }
1091 1.2 bouyer #endif
1092 1.1 bouyer
1093 1.1 bouyer
1094 1.2 bouyer static void
1095 1.2 bouyer xenbus_probe_init(void *unused)
1096 1.1 bouyer {
1097 1.1 bouyer int err = 0, dom0;
1098 1.1 bouyer
1099 1.1 bouyer DPRINTK("");
1100 1.1 bouyer
1101 1.2 bouyer #if 0
1102 1.1 bouyer if (xen_init() < 0) {
1103 1.1 bouyer DPRINTK("failed");
1104 1.1 bouyer return -ENODEV;
1105 1.1 bouyer }
1106 1.1 bouyer
1107 1.2 bouyer
1108 1.2 bouyer
1109 1.1 bouyer /* Register ourselves with the kernel bus & device subsystems */
1110 1.1 bouyer bus_register(&xenbus_frontend.bus);
1111 1.1 bouyer bus_register(&xenbus_backend.bus);
1112 1.1 bouyer device_register(&xenbus_frontend.dev);
1113 1.1 bouyer device_register(&xenbus_backend.dev);
1114 1.2 bouyer #endif
1115 1.1 bouyer
1116 1.1 bouyer /*
1117 1.1 bouyer ** Domain0 doesn't have a store_evtchn or store_mfn yet.
1118 1.1 bouyer */
1119 1.2 bouyer dom0 = (xen_start_info.store_evtchn == 0);
1120 1.2 bouyer #if 0
1121 1.1 bouyer if (dom0) {
1122 1.1 bouyer unsigned long page;
1123 1.1 bouyer evtchn_op_t op = { 0 };
1124 1.1 bouyer int ret;
1125 1.1 bouyer
1126 1.1 bouyer
1127 1.2 bouyer // XXX implement
1128 1.1 bouyer /* Allocate page. */
1129 1.1 bouyer page = get_zeroed_page(GFP_KERNEL);
1130 1.1 bouyer if (!page)
1131 1.1 bouyer return -ENOMEM;
1132 1.1 bouyer
1133 1.1 bouyer /* We don't refcnt properly, so set reserved on page.
1134 1.1 bouyer * (this allocation is permanent) */
1135 1.1 bouyer SetPageReserved(virt_to_page(page));
1136 1.1 bouyer
1137 1.1 bouyer xen_start_info->store_mfn =
1138 1.1 bouyer pfn_to_mfn(virt_to_phys((void *)page) >>
1139 1.1 bouyer PAGE_SHIFT);
1140 1.1 bouyer
1141 1.1 bouyer /* Next allocate a local port which xenstored can bind to */
1142 1.1 bouyer op.cmd = EVTCHNOP_alloc_unbound;
1143 1.1 bouyer op.u.alloc_unbound.dom = DOMID_SELF;
1144 1.1 bouyer op.u.alloc_unbound.remote_dom = 0;
1145 1.1 bouyer
1146 1.1 bouyer ret = HYPERVISOR_event_channel_op(&op);
1147 1.2 bouyer if (ret == 0)
1148 1.2 bouyer panic("can't register xenstore event");
1149 1.1 bouyer xen_start_info->store_evtchn = op.u.alloc_unbound.port;
1150 1.1 bouyer
1151 1.2 bouyer // XXX implement
1152 1.1 bouyer /* And finally publish the above info in /proc/xen */
1153 1.1 bouyer if((xsd_mfn_intf = create_xen_proc_entry("xsd_mfn", 0400)))
1154 1.1 bouyer xsd_mfn_intf->read_proc = xsd_mfn_read;
1155 1.1 bouyer if((xsd_port_intf = create_xen_proc_entry("xsd_port", 0400)))
1156 1.1 bouyer xsd_port_intf->read_proc = xsd_port_read;
1157 1.1 bouyer }
1158 1.2 bouyer #endif
1159 1.1 bouyer
1160 1.1 bouyer /* Initialize the interface to xenstore. */
1161 1.1 bouyer err = xs_init();
1162 1.1 bouyer if (err) {
1163 1.2 bouyer printf("XENBUS: Error initializing xenstore comms: %i\n", err);
1164 1.2 bouyer kthread_exit(err);
1165 1.1 bouyer }
1166 1.1 bouyer
1167 1.1 bouyer if (!dom0) {
1168 1.1 bouyer xenstored_ready = 1;
1169 1.1 bouyer xenbus_probe(NULL);
1170 1.1 bouyer }
1171 1.1 bouyer
1172 1.2 bouyer DPRINTK("done");
1173 1.2 bouyer config_pending_decr();
1174 1.2 bouyer kthread_exit(0);
1175 1.1 bouyer }
1176 1.1 bouyer
1177 1.1 bouyer /*
1178 1.1 bouyer * Local variables:
1179 1.1 bouyer * c-file-style: "linux"
1180 1.1 bouyer * indent-tabs-mode: t
1181 1.1 bouyer * c-indent-level: 8
1182 1.1 bouyer * c-basic-offset: 8
1183 1.1 bouyer * tab-width: 8
1184 1.1 bouyer * End:
1185 1.1 bouyer */
1186