ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 6435:b4b3f6be5226

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 17:27:49 2005 +0000 (2005-08-25)
parents 0610add7c3fe e75933e27215
children 8799d14bef77 9312a3e8a6f8 112d44270733
line source
1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
3 *
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 *
7 * This file may be distributed separately from the Linux kernel, or
8 * incorporated into other software packages, subject to the following license:
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this source file (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use, copy, modify,
13 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * IN THE SOFTWARE.
27 */
28 #define DEBUG
30 #include <asm-xen/hypervisor.h>
31 #include <asm-xen/xenbus.h>
32 #include <asm-xen/balloon.h>
33 #include <linux/kernel.h>
34 #include <linux/err.h>
35 #include <linux/string.h>
36 #include <linux/ctype.h>
37 #include <linux/fcntl.h>
38 #include <stdarg.h>
39 #include <linux/notifier.h>
40 #include "xenbus_comms.h"
42 #define streq(a, b) (strcmp((a), (b)) == 0)
44 static struct notifier_block *xenstore_chain;
46 /* If something in array of ids matches this device, return it. */
47 static const struct xenbus_device_id *
48 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
49 {
50 for (; !streq(arr->devicetype, ""); arr++) {
51 if (streq(arr->devicetype, dev->devicetype))
52 return arr;
53 }
54 return NULL;
55 }
57 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
58 {
59 struct xenbus_driver *drv = to_xenbus_driver(_drv);
61 if (!drv->ids)
62 return 0;
64 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
65 }
67 struct xen_bus_type
68 {
69 char *root;
70 unsigned int levels;
71 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
72 int (*probe)(const char *type, const char *dir);
73 struct bus_type bus;
74 struct device dev;
75 };
77 /* device/<type>/<id> => <type>-<id> */
78 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
79 {
80 nodename = strchr(nodename, '/');
81 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
82 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
83 return -EINVAL;
84 }
86 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
87 if (!strchr(bus_id, '/')) {
88 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
89 return -EINVAL;
90 }
91 *strchr(bus_id, '/') = '-';
92 return 0;
93 }
95 /* Bus type for frontend drivers. */
96 static int xenbus_probe_frontend(const char *type, const char *name);
97 static struct xen_bus_type xenbus_frontend = {
98 .root = "device",
99 .levels = 2, /* device/type/<id> */
100 .get_bus_id = frontend_bus_id,
101 .probe = xenbus_probe_frontend,
102 .bus = {
103 .name = "xen",
104 .match = xenbus_match,
105 },
106 .dev = {
107 .bus_id = "xen",
108 },
109 };
111 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
112 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
113 {
114 int domid, err;
115 const char *devid, *type, *frontend;
116 unsigned int typelen;
118 type = strchr(nodename, '/');
119 if (!type)
120 return -EINVAL;
121 type++;
122 typelen = strcspn(type, "/");
123 if (!typelen || type[typelen] != '/')
124 return -EINVAL;
126 devid = strrchr(nodename, '/') + 1;
128 err = xenbus_gather(nodename, "frontend-id", "%i", &domid,
129 "frontend", NULL, &frontend,
130 NULL);
131 if (err)
132 return err;
133 if (strlen(frontend) == 0)
134 err = -ERANGE;
136 if (!err && !xenbus_exists(frontend, ""))
137 err = -ENOENT;
139 if (err) {
140 kfree(frontend);
141 return err;
142 }
144 if (snprintf(bus_id, BUS_ID_SIZE,
145 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
146 return -ENOSPC;
147 return 0;
148 }
150 static int xenbus_probe_backend(const char *type, const char *uuid);
151 static struct xen_bus_type xenbus_backend = {
152 .root = "backend",
153 .levels = 3, /* backend/type/<frontend>/<id> */
154 .get_bus_id = backend_bus_id,
155 .probe = xenbus_probe_backend,
156 .bus = {
157 .name = "xen-backend",
158 .match = xenbus_match,
159 },
160 .dev = {
161 .bus_id = "xen-backend",
162 },
163 };
165 static int xenbus_dev_probe(struct device *_dev)
166 {
167 struct xenbus_device *dev = to_xenbus_device(_dev);
168 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
169 const struct xenbus_device_id *id;
171 if (!drv->probe)
172 return -ENODEV;
174 id = match_device(drv->ids, dev);
175 if (!id)
176 return -ENODEV;
178 return drv->probe(dev, id);
179 }
181 static int xenbus_dev_remove(struct device *_dev)
182 {
183 struct xenbus_device *dev = to_xenbus_device(_dev);
184 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
186 if (!drv->remove)
187 return 0;
188 return drv->remove(dev);
189 }
191 static int xenbus_register_driver(struct xenbus_driver *drv,
192 struct xen_bus_type *bus)
193 {
194 int err;
196 drv->driver.name = drv->name;
197 drv->driver.bus = &bus->bus;
198 drv->driver.owner = drv->owner;
199 drv->driver.probe = xenbus_dev_probe;
200 drv->driver.remove = xenbus_dev_remove;
202 down(&xenbus_lock);
203 err = driver_register(&drv->driver);
204 up(&xenbus_lock);
205 return err;
206 }
208 int xenbus_register_device(struct xenbus_driver *drv)
209 {
210 return xenbus_register_driver(drv, &xenbus_frontend);
211 }
213 int xenbus_register_backend(struct xenbus_driver *drv)
214 {
215 return xenbus_register_driver(drv, &xenbus_backend);
216 }
218 void xenbus_unregister_driver(struct xenbus_driver *drv)
219 {
220 down(&xenbus_lock);
221 driver_unregister(&drv->driver);
222 up(&xenbus_lock);
223 }
225 struct xb_find_info
226 {
227 struct xenbus_device *dev;
228 const char *nodename;
229 };
231 static int cmp_dev(struct device *dev, void *data)
232 {
233 struct xenbus_device *xendev = to_xenbus_device(dev);
234 struct xb_find_info *info = data;
236 if (streq(xendev->nodename, info->nodename)) {
237 info->dev = xendev;
238 get_device(dev);
239 return 1;
240 }
241 return 0;
242 }
244 struct xenbus_device *xenbus_device_find(const char *nodename,
245 struct bus_type *bus)
246 {
247 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
249 bus_for_each_dev(bus, NULL, &info, cmp_dev);
250 return info.dev;
251 }
253 static int cleanup_dev(struct device *dev, void *data)
254 {
255 struct xenbus_device *xendev = to_xenbus_device(dev);
256 struct xb_find_info *info = data;
257 int len = strlen(info->nodename);
259 if (!strncmp(xendev->nodename, info->nodename, len)) {
260 info->dev = xendev;
261 get_device(dev);
262 return 1;
263 }
264 return 0;
265 }
267 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
268 {
269 struct xb_find_info info = { .nodename = path };
271 do {
272 info.dev = NULL;
273 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
274 if (info.dev) {
275 device_unregister(&info.dev->dev);
276 put_device(&info.dev->dev);
277 }
278 } while (info.dev);
279 }
281 static void xenbus_release_device(struct device *dev)
282 {
283 if (dev) {
284 struct xenbus_device *xendev = to_xenbus_device(dev);
286 kfree(xendev);
287 }
288 }
290 /* Simplified asprintf. */
291 static char *kasprintf(const char *fmt, ...)
292 {
293 va_list ap;
294 unsigned int len;
295 char *p, dummy[1];
297 va_start(ap, fmt);
298 /* FIXME: vsnprintf has a bug, NULL should work */
299 len = vsnprintf(dummy, 0, fmt, ap);
300 va_end(ap);
302 p = kmalloc(len + 1, GFP_KERNEL);
303 if (!p)
304 return NULL;
305 va_start(ap, fmt);
306 vsprintf(p, fmt, ap);
307 va_end(ap);
308 return p;
309 }
311 static int xenbus_probe_node(struct xen_bus_type *bus,
312 const char *type,
313 const char *nodename)
314 {
315 int err;
316 struct xenbus_device *xendev;
317 unsigned int stringlen;
319 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
320 xendev = kmalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
321 if (!xendev)
322 return -ENOMEM;
323 memset(xendev, 0, sizeof(*xendev));
325 /* Copy the strings into the extra space. */
326 xendev->nodename = (char *)(xendev + 1);
327 strcpy(xendev->nodename, nodename);
328 xendev->devicetype = xendev->nodename + strlen(xendev->nodename) + 1;
329 strcpy(xendev->devicetype, type);
331 xendev->dev.parent = &bus->dev;
332 xendev->dev.bus = &bus->bus;
333 xendev->dev.release = xenbus_release_device;
335 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
336 if (err) {
337 kfree(xendev);
338 return err;
339 }
341 /* Register with generic device framework. */
342 err = device_register(&xendev->dev);
343 if (err) {
344 printk("XENBUS: Registering %s device %s: error %i\n",
345 bus->bus.name, xendev->dev.bus_id, err);
346 kfree(xendev);
347 }
348 return err;
349 }
351 /* device/<typename>/<name> */
352 static int xenbus_probe_frontend(const char *type, const char *name)
353 {
354 char *nodename;
355 int err;
357 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
358 if (!nodename)
359 return -ENOMEM;
361 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
362 kfree(nodename);
363 return err;
364 }
366 /* backend/<typename>/<frontend-uuid>/<name> */
367 static int xenbus_probe_backend_unit(const char *dir,
368 const char *type,
369 const char *name)
370 {
371 char *nodename;
372 int err;
374 nodename = kasprintf("%s/%s", dir, name);
375 if (!nodename)
376 return -ENOMEM;
378 err = xenbus_probe_node(&xenbus_backend, type, nodename);
379 kfree(nodename);
380 return err;
381 }
383 /* backend/<typename>/<frontend-uuid> */
384 static int xenbus_probe_backend(const char *type, const char *uuid)
385 {
386 char *nodename;
387 int err = 0;
388 char **dir;
389 unsigned int i, dir_n = 0;
391 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, uuid);
392 if (!nodename)
393 return -ENOMEM;
395 dir = xenbus_directory(nodename, "", &dir_n);
396 if (IS_ERR(dir)) {
397 kfree(nodename);
398 return PTR_ERR(dir);
399 }
401 for (i = 0; i < dir_n; i++) {
402 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
403 if (err)
404 break;
405 }
406 kfree(dir);
407 kfree(nodename);
408 return err;
409 }
411 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
412 {
413 int err = 0;
414 char **dir;
415 unsigned int dir_n = 0;
416 int i;
418 dir = xenbus_directory(bus->root, type, &dir_n);
419 if (IS_ERR(dir))
420 return PTR_ERR(dir);
422 for (i = 0; i < dir_n; i++) {
423 err = bus->probe(type, dir[i]);
424 if (err)
425 break;
426 }
427 kfree(dir);
428 return err;
429 }
431 static int xenbus_probe_devices(struct xen_bus_type *bus)
432 {
433 int err = 0;
434 char **dir;
435 unsigned int i, dir_n;
437 dir = xenbus_directory(bus->root, "", &dir_n);
438 if (IS_ERR(dir))
439 return PTR_ERR(dir);
441 for (i = 0; i < dir_n; i++) {
442 err = xenbus_probe_device_type(bus, dir[i]);
443 if (err)
444 break;
445 }
446 kfree(dir);
447 return err;
448 }
450 static unsigned int char_count(const char *str, char c)
451 {
452 unsigned int i, ret = 0;
454 for (i = 0; str[i]; i++)
455 if (str[i] == c)
456 ret++;
457 return ret;
458 }
460 static int strsep_len(const char *str, char c, unsigned int len)
461 {
462 unsigned int i;
464 for (i = 0; str[i]; i++)
465 if (str[i] == c) {
466 if (len == 0)
467 return i;
468 len--;
469 }
470 return (len == 0) ? i : -ERANGE;
471 }
473 static void dev_changed(const char *node, struct xen_bus_type *bus)
474 {
475 int exists, rootlen;
476 struct xenbus_device *dev;
477 char type[BUS_ID_SIZE];
478 const char *p, *root;
480 if (char_count(node, '/') < 2)
481 return;
483 exists = xenbus_exists(node, "");
484 if (!exists) {
485 xenbus_cleanup_devices(node, &bus->bus);
486 return;
487 }
489 /* backend/<type>/... or device/<type>/... */
490 p = strchr(node, '/') + 1;
491 snprintf(type, BUS_ID_SIZE, "%.*s", strcspn(p, "/"), p);
492 type[BUS_ID_SIZE-1] = '\0';
494 rootlen = strsep_len(node, '/', bus->levels);
495 if (rootlen < 0)
496 return;
497 root = kasprintf("%.*s", rootlen, node);
498 if (!root)
499 return;
501 dev = xenbus_device_find(root, &bus->bus);
502 if (!dev)
503 xenbus_probe_node(bus, type, root);
504 else
505 put_device(&dev->dev);
507 kfree(root);
508 }
510 static void frontend_changed(struct xenbus_watch *watch, const char *node)
511 {
512 dev_changed(node, &xenbus_frontend);
513 }
515 static void backend_changed(struct xenbus_watch *watch, const char *node)
516 {
517 dev_changed(node, &xenbus_backend);
518 }
520 /* We watch for devices appearing and vanishing. */
521 static struct xenbus_watch fe_watch = {
522 .node = "device",
523 .callback = frontend_changed,
524 };
526 static struct xenbus_watch be_watch = {
527 .node = "backend",
528 .callback = backend_changed,
529 };
531 static int suspend_dev(struct device *dev, void *data)
532 {
533 int err = 0;
534 struct xenbus_driver *drv;
535 struct xenbus_device *xdev;
537 if (dev->driver == NULL)
538 return 0;
539 drv = to_xenbus_driver(dev->driver);
540 xdev = container_of(dev, struct xenbus_device, dev);
541 if (drv->suspend)
542 err = drv->suspend(xdev);
543 if (err)
544 printk("xenbus: suspend %s failed: %i\n", dev->bus_id, err);
545 return 0;
546 }
548 static int resume_dev(struct device *dev, void *data)
549 {
550 int err = 0;
551 struct xenbus_driver *drv;
552 struct xenbus_device *xdev;
554 if (dev->driver == NULL)
555 return 0;
556 drv = to_xenbus_driver(dev->driver);
557 xdev = container_of(dev, struct xenbus_device, dev);
558 if (drv->resume)
559 err = drv->resume(xdev);
560 if (err)
561 printk("xenbus: resume %s failed: %i\n", dev->bus_id, err);
562 return 0;
563 }
565 void xenbus_suspend(void)
566 {
567 /* We keep lock, so no comms can happen as page moves. */
568 down(&xenbus_lock);
569 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
570 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
571 xb_suspend_comms();
572 }
574 void xenbus_resume(void)
575 {
576 xb_init_comms();
577 reregister_xenbus_watches();
578 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
579 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
580 up(&xenbus_lock);
581 }
583 int register_xenstore_notifier(struct notifier_block *nb)
584 {
585 int ret = 0;
587 down(&xenbus_lock);
589 if (xen_start_info.store_evtchn) {
590 ret = nb->notifier_call(nb, 0, NULL);
591 } else {
592 notifier_chain_register(&xenstore_chain, nb);
593 }
595 up(&xenbus_lock);
597 return ret;
598 }
599 EXPORT_SYMBOL(register_xenstore_notifier);
601 void unregister_xenstore_notifier(struct notifier_block *nb)
602 {
603 down(&xenbus_lock);
604 notifier_chain_unregister(&xenstore_chain, nb);
605 up(&xenbus_lock);
606 }
607 EXPORT_SYMBOL(unregister_xenstore_notifier);
609 /* called from a thread in privcmd/privcmd.c */
610 int do_xenbus_probe(void *unused)
611 {
612 int err = 0;
614 /* Initialize xenstore comms unless already done. */
615 printk("store_evtchn = %i\n", xen_start_info.store_evtchn);
616 err = xs_init();
617 if (err) {
618 printk("XENBUS: Error initializing xenstore comms:"
619 " %i\n", err);
620 return err;
621 }
623 down(&xenbus_lock);
624 /* Enumerate devices in xenstore. */
625 xenbus_probe_devices(&xenbus_frontend);
626 xenbus_probe_devices(&xenbus_backend);
627 /* Watch for changes. */
628 register_xenbus_watch(&fe_watch);
629 register_xenbus_watch(&be_watch);
630 /* Notify others that xenstore is up */
631 notifier_call_chain(&xenstore_chain, 0, 0);
632 up(&xenbus_lock);
633 return 0;
634 }
636 static int __init xenbus_probe_init(void)
637 {
638 bus_register(&xenbus_frontend.bus);
639 bus_register(&xenbus_backend.bus);
640 device_register(&xenbus_frontend.dev);
641 device_register(&xenbus_backend.dev);
643 if (!xen_start_info.store_evtchn)
644 return 0;
646 do_xenbus_probe(NULL);
647 return 0;
648 }
650 postcore_initcall(xenbus_probe_init);