aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/xen/xenbus/xenbus_probe.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/xen/xenbus/xenbus_probe.c')
-rw-r--r--drivers/xen/xenbus/xenbus_probe.c351
1 files changed, 55 insertions, 296 deletions
diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c
index deb9c4ba3a93..baa65e7fbbc7 100644
--- a/drivers/xen/xenbus/xenbus_probe.c
+++ b/drivers/xen/xenbus/xenbus_probe.c
@@ -56,7 +56,6 @@
56#include <xen/events.h> 56#include <xen/events.h>
57#include <xen/page.h> 57#include <xen/page.h>
58 58
59#include <xen/platform_pci.h>
60#include <xen/hvm.h> 59#include <xen/hvm.h>
61 60
62#include "xenbus_comms.h" 61#include "xenbus_comms.h"
@@ -73,15 +72,6 @@ static unsigned long xen_store_mfn;
73 72
74static BLOCKING_NOTIFIER_HEAD(xenstore_chain); 73static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
75 74
76static void wait_for_devices(struct xenbus_driver *xendrv);
77
78static int xenbus_probe_frontend(const char *type, const char *name);
79
80static void xenbus_dev_shutdown(struct device *_dev);
81
82static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
83static int xenbus_dev_resume(struct device *dev);
84
85/* If something in array of ids matches this device, return it. */ 75/* If something in array of ids matches this device, return it. */
86static const struct xenbus_device_id * 76static const struct xenbus_device_id *
87match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) 77match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
@@ -102,34 +92,7 @@ int xenbus_match(struct device *_dev, struct device_driver *_drv)
102 92
103 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; 93 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
104} 94}
105 95EXPORT_SYMBOL_GPL(xenbus_match);
106static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
107{
108 struct xenbus_device *dev = to_xenbus_device(_dev);
109
110 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
111 return -ENOMEM;
112
113 return 0;
114}
115
116/* device/<type>/<id> => <type>-<id> */
117static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
118{
119 nodename = strchr(nodename, '/');
120 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
121 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
122 return -EINVAL;
123 }
124
125 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
126 if (!strchr(bus_id, '/')) {
127 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
128 return -EINVAL;
129 }
130 *strchr(bus_id, '/') = '-';
131 return 0;
132}
133 96
134 97
135static void free_otherend_details(struct xenbus_device *dev) 98static void free_otherend_details(struct xenbus_device *dev)
@@ -149,7 +112,30 @@ static void free_otherend_watch(struct xenbus_device *dev)
149} 112}
150 113
151 114
152int read_otherend_details(struct xenbus_device *xendev, 115static int talk_to_otherend(struct xenbus_device *dev)
116{
117 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
118
119 free_otherend_watch(dev);
120 free_otherend_details(dev);
121
122 return drv->read_otherend_details(dev);
123}
124
125
126
127static int watch_otherend(struct xenbus_device *dev)
128{
129 struct xen_bus_type *bus =
130 container_of(dev->dev.bus, struct xen_bus_type, bus);
131
132 return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
133 bus->otherend_changed,
134 "%s/%s", dev->otherend, "state");
135}
136
137
138int xenbus_read_otherend_details(struct xenbus_device *xendev,
153 char *id_node, char *path_node) 139 char *id_node, char *path_node)
154{ 140{
155 int err = xenbus_gather(XBT_NIL, xendev->nodename, 141 int err = xenbus_gather(XBT_NIL, xendev->nodename,
@@ -174,39 +160,11 @@ int read_otherend_details(struct xenbus_device *xendev,
174 160
175 return 0; 161 return 0;
176} 162}
163EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
177 164
178 165void xenbus_otherend_changed(struct xenbus_watch *watch,
179static int read_backend_details(struct xenbus_device *xendev) 166 const char **vec, unsigned int len,
180{ 167 int ignore_on_shutdown)
181 return read_otherend_details(xendev, "backend-id", "backend");
182}
183
184static struct device_attribute xenbus_dev_attrs[] = {
185 __ATTR_NULL
186};
187
188/* Bus type for frontend drivers. */
189static struct xen_bus_type xenbus_frontend = {
190 .root = "device",
191 .levels = 2, /* device/type/<id> */
192 .get_bus_id = frontend_bus_id,
193 .probe = xenbus_probe_frontend,
194 .bus = {
195 .name = "xen",
196 .match = xenbus_match,
197 .uevent = xenbus_uevent,
198 .probe = xenbus_dev_probe,
199 .remove = xenbus_dev_remove,
200 .shutdown = xenbus_dev_shutdown,
201 .dev_attrs = xenbus_dev_attrs,
202
203 .suspend = xenbus_dev_suspend,
204 .resume = xenbus_dev_resume,
205 },
206};
207
208static void otherend_changed(struct xenbus_watch *watch,
209 const char **vec, unsigned int len)
210{ 168{
211 struct xenbus_device *dev = 169 struct xenbus_device *dev =
212 container_of(watch, struct xenbus_device, otherend_watch); 170 container_of(watch, struct xenbus_device, otherend_watch);
@@ -234,11 +192,7 @@ static void otherend_changed(struct xenbus_watch *watch,
234 * work that can fail e.g., when the rootfs is gone. 192 * work that can fail e.g., when the rootfs is gone.
235 */ 193 */
236 if (system_state > SYSTEM_RUNNING) { 194 if (system_state > SYSTEM_RUNNING) {
237 struct xen_bus_type *bus = bus; 195 if (ignore_on_shutdown && (state == XenbusStateClosing))
238 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
239 /* If we're frontend, drive the state machine to Closed. */
240 /* This should cause the backend to release our resources. */
241 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
242 xenbus_frontend_closed(dev); 196 xenbus_frontend_closed(dev);
243 return; 197 return;
244 } 198 }
@@ -246,25 +200,7 @@ static void otherend_changed(struct xenbus_watch *watch,
246 if (drv->otherend_changed) 200 if (drv->otherend_changed)
247 drv->otherend_changed(dev, state); 201 drv->otherend_changed(dev, state);
248} 202}
249 203EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
250
251static int talk_to_otherend(struct xenbus_device *dev)
252{
253 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
254
255 free_otherend_watch(dev);
256 free_otherend_details(dev);
257
258 return drv->read_otherend_details(dev);
259}
260
261
262static int watch_otherend(struct xenbus_device *dev)
263{
264 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
265 "%s/%s", dev->otherend, "state");
266}
267
268 204
269int xenbus_dev_probe(struct device *_dev) 205int xenbus_dev_probe(struct device *_dev)
270{ 206{
@@ -308,8 +244,9 @@ int xenbus_dev_probe(struct device *_dev)
308fail: 244fail:
309 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); 245 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
310 xenbus_switch_state(dev, XenbusStateClosed); 246 xenbus_switch_state(dev, XenbusStateClosed);
311 return -ENODEV; 247 return err;
312} 248}
249EXPORT_SYMBOL_GPL(xenbus_dev_probe);
313 250
314int xenbus_dev_remove(struct device *_dev) 251int xenbus_dev_remove(struct device *_dev)
315{ 252{
@@ -327,8 +264,9 @@ int xenbus_dev_remove(struct device *_dev)
327 xenbus_switch_state(dev, XenbusStateClosed); 264 xenbus_switch_state(dev, XenbusStateClosed);
328 return 0; 265 return 0;
329} 266}
267EXPORT_SYMBOL_GPL(xenbus_dev_remove);
330 268
331static void xenbus_dev_shutdown(struct device *_dev) 269void xenbus_dev_shutdown(struct device *_dev)
332{ 270{
333 struct xenbus_device *dev = to_xenbus_device(_dev); 271 struct xenbus_device *dev = to_xenbus_device(_dev);
334 unsigned long timeout = 5*HZ; 272 unsigned long timeout = 5*HZ;
@@ -349,6 +287,7 @@ static void xenbus_dev_shutdown(struct device *_dev)
349 out: 287 out:
350 put_device(&dev->dev); 288 put_device(&dev->dev);
351} 289}
290EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
352 291
353int xenbus_register_driver_common(struct xenbus_driver *drv, 292int xenbus_register_driver_common(struct xenbus_driver *drv,
354 struct xen_bus_type *bus, 293 struct xen_bus_type *bus,
@@ -362,25 +301,7 @@ int xenbus_register_driver_common(struct xenbus_driver *drv,
362 301
363 return driver_register(&drv->driver); 302 return driver_register(&drv->driver);
364} 303}
365 304EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
366int __xenbus_register_frontend(struct xenbus_driver *drv,
367 struct module *owner, const char *mod_name)
368{
369 int ret;
370
371 drv->read_otherend_details = read_backend_details;
372
373 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
374 owner, mod_name);
375 if (ret)
376 return ret;
377
378 /* If this driver is loaded as a module wait for devices to attach. */
379 wait_for_devices(drv);
380
381 return 0;
382}
383EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
384 305
385void xenbus_unregister_driver(struct xenbus_driver *drv) 306void xenbus_unregister_driver(struct xenbus_driver *drv)
386{ 307{
@@ -551,24 +472,7 @@ fail:
551 kfree(xendev); 472 kfree(xendev);
552 return err; 473 return err;
553} 474}
554 475EXPORT_SYMBOL_GPL(xenbus_probe_node);
555/* device/<typename>/<name> */
556static int xenbus_probe_frontend(const char *type, const char *name)
557{
558 char *nodename;
559 int err;
560
561 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
562 xenbus_frontend.root, type, name);
563 if (!nodename)
564 return -ENOMEM;
565
566 DPRINTK("%s", nodename);
567
568 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
569 kfree(nodename);
570 return err;
571}
572 476
573static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) 477static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
574{ 478{
@@ -582,10 +486,11 @@ static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
582 return PTR_ERR(dir); 486 return PTR_ERR(dir);
583 487
584 for (i = 0; i < dir_n; i++) { 488 for (i = 0; i < dir_n; i++) {
585 err = bus->probe(type, dir[i]); 489 err = bus->probe(bus, type, dir[i]);
586 if (err) 490 if (err)
587 break; 491 break;
588 } 492 }
493
589 kfree(dir); 494 kfree(dir);
590 return err; 495 return err;
591} 496}
@@ -605,9 +510,11 @@ int xenbus_probe_devices(struct xen_bus_type *bus)
605 if (err) 510 if (err)
606 break; 511 break;
607 } 512 }
513
608 kfree(dir); 514 kfree(dir);
609 return err; 515 return err;
610} 516}
517EXPORT_SYMBOL_GPL(xenbus_probe_devices);
611 518
612static unsigned int char_count(const char *str, char c) 519static unsigned int char_count(const char *str, char c)
613{ 520{
@@ -670,32 +577,18 @@ void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
670} 577}
671EXPORT_SYMBOL_GPL(xenbus_dev_changed); 578EXPORT_SYMBOL_GPL(xenbus_dev_changed);
672 579
673static void frontend_changed(struct xenbus_watch *watch, 580int xenbus_dev_suspend(struct device *dev, pm_message_t state)
674 const char **vec, unsigned int len)
675{
676 DPRINTK("");
677
678 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
679}
680
681/* We watch for devices appearing and vanishing. */
682static struct xenbus_watch fe_watch = {
683 .node = "device",
684 .callback = frontend_changed,
685};
686
687static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
688{ 581{
689 int err = 0; 582 int err = 0;
690 struct xenbus_driver *drv; 583 struct xenbus_driver *drv;
691 struct xenbus_device *xdev; 584 struct xenbus_device *xdev
585 = container_of(dev, struct xenbus_device, dev);
692 586
693 DPRINTK(""); 587 DPRINTK("%s", xdev->nodename);
694 588
695 if (dev->driver == NULL) 589 if (dev->driver == NULL)
696 return 0; 590 return 0;
697 drv = to_xenbus_driver(dev->driver); 591 drv = to_xenbus_driver(dev->driver);
698 xdev = container_of(dev, struct xenbus_device, dev);
699 if (drv->suspend) 592 if (drv->suspend)
700 err = drv->suspend(xdev, state); 593 err = drv->suspend(xdev, state);
701 if (err) 594 if (err)
@@ -703,21 +596,20 @@ static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
703 "xenbus: suspend %s failed: %i\n", dev_name(dev), err); 596 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
704 return 0; 597 return 0;
705} 598}
599EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
706 600
707static int xenbus_dev_resume(struct device *dev) 601int xenbus_dev_resume(struct device *dev)
708{ 602{
709 int err; 603 int err;
710 struct xenbus_driver *drv; 604 struct xenbus_driver *drv;
711 struct xenbus_device *xdev; 605 struct xenbus_device *xdev
606 = container_of(dev, struct xenbus_device, dev);
712 607
713 DPRINTK(""); 608 DPRINTK("%s", xdev->nodename);
714 609
715 if (dev->driver == NULL) 610 if (dev->driver == NULL)
716 return 0; 611 return 0;
717
718 drv = to_xenbus_driver(dev->driver); 612 drv = to_xenbus_driver(dev->driver);
719 xdev = container_of(dev, struct xenbus_device, dev);
720
721 err = talk_to_otherend(xdev); 613 err = talk_to_otherend(xdev);
722 if (err) { 614 if (err) {
723 printk(KERN_WARNING 615 printk(KERN_WARNING
@@ -748,6 +640,7 @@ static int xenbus_dev_resume(struct device *dev)
748 640
749 return 0; 641 return 0;
750} 642}
643EXPORT_SYMBOL_GPL(xenbus_dev_resume);
751 644
752/* A flag to determine if xenstored is 'ready' (i.e. has started) */ 645/* A flag to determine if xenstored is 'ready' (i.e. has started) */
753int xenstored_ready = 0; 646int xenstored_ready = 0;
@@ -776,11 +669,6 @@ void xenbus_probe(struct work_struct *unused)
776{ 669{
777 xenstored_ready = 1; 670 xenstored_ready = 1;
778 671
779 /* Enumerate devices in xenstore and watch for changes. */
780 xenbus_probe_devices(&xenbus_frontend);
781 register_xenbus_watch(&fe_watch);
782 xenbus_backend_probe_and_watch();
783
784 /* Notify others that xenstore is up */ 672 /* Notify others that xenstore is up */
785 blocking_notifier_call_chain(&xenstore_chain, 0, NULL); 673 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
786} 674}
@@ -809,16 +697,7 @@ static int __init xenbus_init(void)
809 697
810 err = -ENODEV; 698 err = -ENODEV;
811 if (!xen_domain()) 699 if (!xen_domain())
812 goto out_error; 700 return err;
813
814 /* Register ourselves with the kernel bus subsystem */
815 err = bus_register(&xenbus_frontend.bus);
816 if (err)
817 goto out_error;
818
819 err = xenbus_backend_bus_register();
820 if (err)
821 goto out_unreg_front;
822 701
823 /* 702 /*
824 * Domain0 doesn't have a store_evtchn or store_mfn yet. 703 * Domain0 doesn't have a store_evtchn or store_mfn yet.
@@ -874,7 +753,7 @@ static int __init xenbus_init(void)
874 if (err) { 753 if (err) {
875 printk(KERN_WARNING 754 printk(KERN_WARNING
876 "XENBUS: Error initializing xenstore comms: %i\n", err); 755 "XENBUS: Error initializing xenstore comms: %i\n", err);
877 goto out_unreg_back; 756 goto out_error;
878 } 757 }
879 758
880#ifdef CONFIG_XEN_COMPAT_XENFS 759#ifdef CONFIG_XEN_COMPAT_XENFS
@@ -887,133 +766,13 @@ static int __init xenbus_init(void)
887 766
888 return 0; 767 return 0;
889 768
890 out_unreg_back:
891 xenbus_backend_bus_unregister();
892
893 out_unreg_front:
894 bus_unregister(&xenbus_frontend.bus);
895
896 out_error: 769 out_error:
897 if (page != 0) 770 if (page != 0)
898 free_page(page); 771 free_page(page);
772
899 return err; 773 return err;
900} 774}
901 775
902postcore_initcall(xenbus_init); 776postcore_initcall(xenbus_init);
903 777
904MODULE_LICENSE("GPL"); 778MODULE_LICENSE("GPL");
905
906static int is_device_connecting(struct device *dev, void *data)
907{
908 struct xenbus_device *xendev = to_xenbus_device(dev);
909 struct device_driver *drv = data;
910 struct xenbus_driver *xendrv;
911
912 /*
913 * A device with no driver will never connect. We care only about
914 * devices which should currently be in the process of connecting.
915 */
916 if (!dev->driver)
917 return 0;
918
919 /* Is this search limited to a particular driver? */
920 if (drv && (dev->driver != drv))
921 return 0;
922
923 xendrv = to_xenbus_driver(dev->driver);
924 return (xendev->state < XenbusStateConnected ||
925 (xendev->state == XenbusStateConnected &&
926 xendrv->is_ready && !xendrv->is_ready(xendev)));
927}
928
929static int exists_connecting_device(struct device_driver *drv)
930{
931 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
932 is_device_connecting);
933}
934
935static int print_device_status(struct device *dev, void *data)
936{
937 struct xenbus_device *xendev = to_xenbus_device(dev);
938 struct device_driver *drv = data;
939
940 /* Is this operation limited to a particular driver? */
941 if (drv && (dev->driver != drv))
942 return 0;
943
944 if (!dev->driver) {
945 /* Information only: is this too noisy? */
946 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
947 xendev->nodename);
948 } else if (xendev->state < XenbusStateConnected) {
949 enum xenbus_state rstate = XenbusStateUnknown;
950 if (xendev->otherend)
951 rstate = xenbus_read_driver_state(xendev->otherend);
952 printk(KERN_WARNING "XENBUS: Timeout connecting "
953 "to device: %s (local state %d, remote state %d)\n",
954 xendev->nodename, xendev->state, rstate);
955 }
956
957 return 0;
958}
959
960/* We only wait for device setup after most initcalls have run. */
961static int ready_to_wait_for_devices;
962
963/*
964 * On a 5-minute timeout, wait for all devices currently configured. We need
965 * to do this to guarantee that the filesystems and / or network devices
966 * needed for boot are available, before we can allow the boot to proceed.
967 *
968 * This needs to be on a late_initcall, to happen after the frontend device
969 * drivers have been initialised, but before the root fs is mounted.
970 *
971 * A possible improvement here would be to have the tools add a per-device
972 * flag to the store entry, indicating whether it is needed at boot time.
973 * This would allow people who knew what they were doing to accelerate their
974 * boot slightly, but of course needs tools or manual intervention to set up
975 * those flags correctly.
976 */
977static void wait_for_devices(struct xenbus_driver *xendrv)
978{
979 unsigned long start = jiffies;
980 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
981 unsigned int seconds_waited = 0;
982
983 if (!ready_to_wait_for_devices || !xen_domain())
984 return;
985
986 while (exists_connecting_device(drv)) {
987 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
988 if (!seconds_waited)
989 printk(KERN_WARNING "XENBUS: Waiting for "
990 "devices to initialise: ");
991 seconds_waited += 5;
992 printk("%us...", 300 - seconds_waited);
993 if (seconds_waited == 300)
994 break;
995 }
996
997 schedule_timeout_interruptible(HZ/10);
998 }
999
1000 if (seconds_waited)
1001 printk("\n");
1002
1003 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1004 print_device_status);
1005}
1006
1007#ifndef MODULE
1008static int __init boot_wait_for_devices(void)
1009{
1010 if (xen_hvm_domain() && !xen_platform_pci_unplug)
1011 return -ENODEV;
1012
1013 ready_to_wait_for_devices = 1;
1014 wait_for_devices(NULL);
1015 return 0;
1016}
1017
1018late_initcall(boot_wait_for_devices);
1019#endif