aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIan Campbell <ian.campbell@citrix.com>2009-02-09 15:05:51 -0500
committerKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>2011-01-05 16:29:17 -0500
commit2de06cc1f18d638cc7ab1169f61a8599045c2d4f (patch)
treed1799d08078c4542b730e18397990ad92ec1d917
parentcf7d7e5a1980d1116ee152d25dac382b112b9c17 (diff)
xen: separate out frontend xenbus
Impact: refactor Make a distinct frontend xenbus, in preparation for adding a backend xenbus. Signed-off-by: Ian Campbell <ian.campbell@citrix.com> Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com> [corresponds to 2fd433a4188f in git://git.kernel.org/pub/scm/linux/kernel/git/jeremy/xen.git with adjustments to reflect changes in the code which is moved] Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
-rw-r--r--drivers/block/Kconfig1
-rw-r--r--drivers/xen/Kconfig3
-rw-r--r--drivers/xen/xenbus/Makefile2
-rw-r--r--drivers/xen/xenbus/xenbus_probe.c361
-rw-r--r--drivers/xen/xenbus/xenbus_probe.h30
-rw-r--r--drivers/xen/xenbus/xenbus_probe_frontend.c275
6 files changed, 367 insertions, 305 deletions
diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig
index 4b9359a6f6ca..83c32cb72582 100644
--- a/drivers/block/Kconfig
+++ b/drivers/block/Kconfig
@@ -464,6 +464,7 @@ config XEN_BLKDEV_FRONTEND
464 tristate "Xen virtual block device support" 464 tristate "Xen virtual block device support"
465 depends on XEN 465 depends on XEN
466 default y 466 default y
467 select XEN_XENBUS_FRONTEND
467 help 468 help
468 This driver implements the front-end of the Xen virtual 469 This driver implements the front-end of the Xen virtual
469 block device driver. It communicates with a back-end driver 470 block device driver. It communicates with a back-end driver
diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig
index 6e6180ccd726..10b6bef2e217 100644
--- a/drivers/xen/Kconfig
+++ b/drivers/xen/Kconfig
@@ -62,6 +62,9 @@ config XEN_SYS_HYPERVISOR
62 virtual environment, /sys/hypervisor will still be present, 62 virtual environment, /sys/hypervisor will still be present,
63 but will have no xen contents. 63 but will have no xen contents.
64 64
65config XEN_XENBUS_FRONTEND
66 tristate
67
65config XEN_PLATFORM_PCI 68config XEN_PLATFORM_PCI
66 tristate "xen platform pci device driver" 69 tristate "xen platform pci device driver"
67 depends on XEN_PVHVM 70 depends on XEN_PVHVM
diff --git a/drivers/xen/xenbus/Makefile b/drivers/xen/xenbus/Makefile
index 5571f5b84223..33e8cf8149b3 100644
--- a/drivers/xen/xenbus/Makefile
+++ b/drivers/xen/xenbus/Makefile
@@ -5,3 +5,5 @@ xenbus-objs += xenbus_client.o
5xenbus-objs += xenbus_comms.o 5xenbus-objs += xenbus_comms.o
6xenbus-objs += xenbus_xs.o 6xenbus-objs += xenbus_xs.o
7xenbus-objs += xenbus_probe.o 7xenbus-objs += xenbus_probe.o
8
9obj-$(CONFIG_XEN_XENBUS_FRONTEND) += xenbus_probe_frontend.o
diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c
index deb9c4ba3a93..d46205868b88 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,8 +92,10 @@ 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}
95EXPORT_SYMBOL_GPL(xenbus_match);
96
105 97
106static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env) 98int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
107{ 99{
108 struct xenbus_device *dev = to_xenbus_device(_dev); 100 struct xenbus_device *dev = to_xenbus_device(_dev);
109 101
@@ -112,25 +104,7 @@ static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
112 104
113 return 0; 105 return 0;
114} 106}
115 107EXPORT_SYMBOL_GPL(xenbus_uevent);
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
134 108
135static void free_otherend_details(struct xenbus_device *dev) 109static void free_otherend_details(struct xenbus_device *dev)
136{ 110{
@@ -149,7 +123,28 @@ static void free_otherend_watch(struct xenbus_device *dev)
149} 123}
150 124
151 125
152int read_otherend_details(struct xenbus_device *xendev, 126static int talk_to_otherend(struct xenbus_device *dev)
127{
128 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
129
130 free_otherend_watch(dev);
131 free_otherend_details(dev);
132
133 return drv->read_otherend_details(dev);
134}
135
136
137
138static int watch_otherend(struct xenbus_device *dev)
139{
140 struct xen_bus_type *bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
141
142 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, bus->otherend_changed,
143 "%s/%s", dev->otherend, "state");
144}
145
146
147int xenbus_read_otherend_details(struct xenbus_device *xendev,
153 char *id_node, char *path_node) 148 char *id_node, char *path_node)
154{ 149{
155 int err = xenbus_gather(XBT_NIL, xendev->nodename, 150 int err = xenbus_gather(XBT_NIL, xendev->nodename,
@@ -174,39 +169,11 @@ int read_otherend_details(struct xenbus_device *xendev,
174 169
175 return 0; 170 return 0;
176} 171}
172EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
177 173
178 174void xenbus_otherend_changed(struct xenbus_watch *watch,
179static int read_backend_details(struct xenbus_device *xendev) 175 const char **vec, unsigned int len,
180{ 176 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{ 177{
211 struct xenbus_device *dev = 178 struct xenbus_device *dev =
212 container_of(watch, struct xenbus_device, otherend_watch); 179 container_of(watch, struct xenbus_device, otherend_watch);
@@ -234,11 +201,7 @@ static void otherend_changed(struct xenbus_watch *watch,
234 * work that can fail e.g., when the rootfs is gone. 201 * work that can fail e.g., when the rootfs is gone.
235 */ 202 */
236 if (system_state > SYSTEM_RUNNING) { 203 if (system_state > SYSTEM_RUNNING) {
237 struct xen_bus_type *bus = bus; 204 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); 205 xenbus_frontend_closed(dev);
243 return; 206 return;
244 } 207 }
@@ -246,25 +209,7 @@ static void otherend_changed(struct xenbus_watch *watch,
246 if (drv->otherend_changed) 209 if (drv->otherend_changed)
247 drv->otherend_changed(dev, state); 210 drv->otherend_changed(dev, state);
248} 211}
249 212EXPORT_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 213
269int xenbus_dev_probe(struct device *_dev) 214int xenbus_dev_probe(struct device *_dev)
270{ 215{
@@ -310,6 +255,7 @@ fail:
310 xenbus_switch_state(dev, XenbusStateClosed); 255 xenbus_switch_state(dev, XenbusStateClosed);
311 return -ENODEV; 256 return -ENODEV;
312} 257}
258EXPORT_SYMBOL_GPL(xenbus_dev_probe);
313 259
314int xenbus_dev_remove(struct device *_dev) 260int xenbus_dev_remove(struct device *_dev)
315{ 261{
@@ -327,8 +273,9 @@ int xenbus_dev_remove(struct device *_dev)
327 xenbus_switch_state(dev, XenbusStateClosed); 273 xenbus_switch_state(dev, XenbusStateClosed);
328 return 0; 274 return 0;
329} 275}
276EXPORT_SYMBOL_GPL(xenbus_dev_remove);
330 277
331static void xenbus_dev_shutdown(struct device *_dev) 278void xenbus_dev_shutdown(struct device *_dev)
332{ 279{
333 struct xenbus_device *dev = to_xenbus_device(_dev); 280 struct xenbus_device *dev = to_xenbus_device(_dev);
334 unsigned long timeout = 5*HZ; 281 unsigned long timeout = 5*HZ;
@@ -349,6 +296,7 @@ static void xenbus_dev_shutdown(struct device *_dev)
349 out: 296 out:
350 put_device(&dev->dev); 297 put_device(&dev->dev);
351} 298}
299EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
352 300
353int xenbus_register_driver_common(struct xenbus_driver *drv, 301int xenbus_register_driver_common(struct xenbus_driver *drv,
354 struct xen_bus_type *bus, 302 struct xen_bus_type *bus,
@@ -362,25 +310,7 @@ int xenbus_register_driver_common(struct xenbus_driver *drv,
362 310
363 return driver_register(&drv->driver); 311 return driver_register(&drv->driver);
364} 312}
365 313EXPORT_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 314
385void xenbus_unregister_driver(struct xenbus_driver *drv) 315void xenbus_unregister_driver(struct xenbus_driver *drv)
386{ 316{
@@ -551,24 +481,7 @@ fail:
551 kfree(xendev); 481 kfree(xendev);
552 return err; 482 return err;
553} 483}
554 484EXPORT_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 485
573static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) 486static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
574{ 487{
@@ -577,15 +490,23 @@ static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
577 unsigned int dir_n = 0; 490 unsigned int dir_n = 0;
578 int i; 491 int i;
579 492
493 printk(KERN_CRIT "%s type %s\n", __func__, type);
494
580 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); 495 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
581 if (IS_ERR(dir)) 496 if (IS_ERR(dir)) {
497 printk(KERN_CRIT "%s failed xenbus_directory\n", __func__);
582 return PTR_ERR(dir); 498 return PTR_ERR(dir);
499 }
583 500
584 for (i = 0; i < dir_n; i++) { 501 for (i = 0; i < dir_n; i++) {
585 err = bus->probe(type, dir[i]); 502 printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]);
586 if (err) 503 err = bus->probe(bus, type, dir[i]);
504 if (err) {
505 printk(KERN_CRIT "%s failed\n", __func__);
587 break; 506 break;
507 }
588 } 508 }
509 printk("%s done\n", __func__);
589 kfree(dir); 510 kfree(dir);
590 return err; 511 return err;
591} 512}
@@ -596,18 +517,27 @@ int xenbus_probe_devices(struct xen_bus_type *bus)
596 char **dir; 517 char **dir;
597 unsigned int i, dir_n; 518 unsigned int i, dir_n;
598 519
520 printk(KERN_CRIT "%s %s\n", __func__, bus->root);
521
599 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); 522 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
600 if (IS_ERR(dir)) 523 if (IS_ERR(dir)) {
524 printk(KERN_CRIT "%s failed xenbus_directory\n", __func__);
601 return PTR_ERR(dir); 525 return PTR_ERR(dir);
526 }
602 527
603 for (i = 0; i < dir_n; i++) { 528 for (i = 0; i < dir_n; i++) {
529 printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]);
604 err = xenbus_probe_device_type(bus, dir[i]); 530 err = xenbus_probe_device_type(bus, dir[i]);
605 if (err) 531 if (err) {
532 printk(KERN_CRIT "%s failed\n", __func__);
606 break; 533 break;
534 }
607 } 535 }
536 printk("%s done\n", __func__);
608 kfree(dir); 537 kfree(dir);
609 return err; 538 return err;
610} 539}
540EXPORT_SYMBOL_GPL(xenbus_probe_devices);
611 541
612static unsigned int char_count(const char *str, char c) 542static unsigned int char_count(const char *str, char c)
613{ 543{
@@ -670,32 +600,17 @@ void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
670} 600}
671EXPORT_SYMBOL_GPL(xenbus_dev_changed); 601EXPORT_SYMBOL_GPL(xenbus_dev_changed);
672 602
673static void frontend_changed(struct xenbus_watch *watch, 603int 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{ 604{
689 int err = 0; 605 int err = 0;
690 struct xenbus_driver *drv; 606 struct xenbus_driver *drv;
691 struct xenbus_device *xdev; 607 struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev);
692 608
693 DPRINTK(""); 609 DPRINTK("%s", xdev->nodename);
694 610
695 if (dev->driver == NULL) 611 if (dev->driver == NULL)
696 return 0; 612 return 0;
697 drv = to_xenbus_driver(dev->driver); 613 drv = to_xenbus_driver(dev->driver);
698 xdev = container_of(dev, struct xenbus_device, dev);
699 if (drv->suspend) 614 if (drv->suspend)
700 err = drv->suspend(xdev, state); 615 err = drv->suspend(xdev, state);
701 if (err) 616 if (err)
@@ -703,21 +618,19 @@ static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
703 "xenbus: suspend %s failed: %i\n", dev_name(dev), err); 618 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
704 return 0; 619 return 0;
705} 620}
621EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
706 622
707static int xenbus_dev_resume(struct device *dev) 623int xenbus_dev_resume(struct device *dev)
708{ 624{
709 int err; 625 int err;
710 struct xenbus_driver *drv; 626 struct xenbus_driver *drv;
711 struct xenbus_device *xdev; 627 struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev);
712 628
713 DPRINTK(""); 629 DPRINTK("%s", xdev->nodename);
714 630
715 if (dev->driver == NULL) 631 if (dev->driver == NULL)
716 return 0; 632 return 0;
717
718 drv = to_xenbus_driver(dev->driver); 633 drv = to_xenbus_driver(dev->driver);
719 xdev = container_of(dev, struct xenbus_device, dev);
720
721 err = talk_to_otherend(xdev); 634 err = talk_to_otherend(xdev);
722 if (err) { 635 if (err) {
723 printk(KERN_WARNING 636 printk(KERN_WARNING
@@ -748,6 +661,7 @@ static int xenbus_dev_resume(struct device *dev)
748 661
749 return 0; 662 return 0;
750} 663}
664EXPORT_SYMBOL_GPL(xenbus_dev_resume);
751 665
752/* A flag to determine if xenstored is 'ready' (i.e. has started) */ 666/* A flag to determine if xenstored is 'ready' (i.e. has started) */
753int xenstored_ready = 0; 667int xenstored_ready = 0;
@@ -776,11 +690,6 @@ void xenbus_probe(struct work_struct *unused)
776{ 690{
777 xenstored_ready = 1; 691 xenstored_ready = 1;
778 692
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 */ 693 /* Notify others that xenstore is up */
785 blocking_notifier_call_chain(&xenstore_chain, 0, NULL); 694 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
786} 695}
@@ -811,15 +720,6 @@ static int __init xenbus_init(void)
811 if (!xen_domain()) 720 if (!xen_domain())
812 goto out_error; 721 goto out_error;
813 722
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
823 /* 723 /*
824 * Domain0 doesn't have a store_evtchn or store_mfn yet. 724 * Domain0 doesn't have a store_evtchn or store_mfn yet.
825 */ 725 */
@@ -874,7 +774,7 @@ static int __init xenbus_init(void)
874 if (err) { 774 if (err) {
875 printk(KERN_WARNING 775 printk(KERN_WARNING
876 "XENBUS: Error initializing xenstore comms: %i\n", err); 776 "XENBUS: Error initializing xenstore comms: %i\n", err);
877 goto out_unreg_back; 777 goto out_error;
878 } 778 }
879 779
880#ifdef CONFIG_XEN_COMPAT_XENFS 780#ifdef CONFIG_XEN_COMPAT_XENFS
@@ -885,135 +785,16 @@ static int __init xenbus_init(void)
885 proc_mkdir("xen", NULL); 785 proc_mkdir("xen", NULL);
886#endif 786#endif
887 787
788 printk(KERN_CRIT "%s ok\n", __func__);
888 return 0; 789 return 0;
889 790
890 out_unreg_back:
891 xenbus_backend_bus_unregister();
892
893 out_unreg_front:
894 bus_unregister(&xenbus_frontend.bus);
895
896 out_error: 791 out_error:
897 if (page != 0) 792 if (page != 0)
898 free_page(page); 793 free_page(page);
794
899 return err; 795 return err;
900} 796}
901 797
902postcore_initcall(xenbus_init); 798postcore_initcall(xenbus_init);
903 799
904MODULE_LICENSE("GPL"); 800MODULE_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
diff --git a/drivers/xen/xenbus/xenbus_probe.h b/drivers/xen/xenbus/xenbus_probe.h
index 6c5e3185a6a2..be390cef329f 100644
--- a/drivers/xen/xenbus/xenbus_probe.h
+++ b/drivers/xen/xenbus/xenbus_probe.h
@@ -36,26 +36,13 @@
36 36
37#define XEN_BUS_ID_SIZE 20 37#define XEN_BUS_ID_SIZE 20
38 38
39#ifdef CONFIG_XEN_BACKEND
40extern void xenbus_backend_suspend(int (*fn)(struct device *, void *));
41extern void xenbus_backend_resume(int (*fn)(struct device *, void *));
42extern void xenbus_backend_probe_and_watch(void);
43extern int xenbus_backend_bus_register(void);
44extern void xenbus_backend_bus_unregister(void);
45#else
46static inline void xenbus_backend_suspend(int (*fn)(struct device *, void *)) {}
47static inline void xenbus_backend_resume(int (*fn)(struct device *, void *)) {}
48static inline void xenbus_backend_probe_and_watch(void) {}
49static inline int xenbus_backend_bus_register(void) { return 0; }
50static inline void xenbus_backend_bus_unregister(void) {}
51#endif
52
53struct xen_bus_type 39struct xen_bus_type
54{ 40{
55 char *root; 41 char *root;
56 unsigned int levels; 42 unsigned int levels;
57 int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char *nodename); 43 int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char *nodename);
58 int (*probe)(const char *type, const char *dir); 44 int (*probe)(struct xen_bus_type *bus, const char *type, const char *dir);
45 void (*otherend_changed)(struct xenbus_watch *watch, const char **vec, unsigned int len);
59 struct bus_type bus; 46 struct bus_type bus;
60}; 47};
61 48
@@ -73,4 +60,17 @@ extern int xenbus_probe_devices(struct xen_bus_type *bus);
73 60
74extern void xenbus_dev_changed(const char *node, struct xen_bus_type *bus); 61extern void xenbus_dev_changed(const char *node, struct xen_bus_type *bus);
75 62
63extern int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env);
64extern void xenbus_dev_shutdown(struct device *_dev);
65
66extern int xenbus_dev_suspend(struct device *dev, pm_message_t state);
67extern int xenbus_dev_resume(struct device *dev);
68
69extern void xenbus_otherend_changed(struct xenbus_watch *watch,
70 const char **vec, unsigned int len,
71 int ignore_on_shutdown);
72
73extern int xenbus_read_otherend_details(struct xenbus_device *xendev,
74 char *id_node, char *path_node);
75
76#endif 76#endif
diff --git a/drivers/xen/xenbus/xenbus_probe_frontend.c b/drivers/xen/xenbus/xenbus_probe_frontend.c
new file mode 100644
index 000000000000..04c4f0123521
--- /dev/null
+++ b/drivers/xen/xenbus/xenbus_probe_frontend.c
@@ -0,0 +1,275 @@
1#define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
4
5#include <linux/kernel.h>
6#include <linux/err.h>
7#include <linux/string.h>
8#include <linux/ctype.h>
9#include <linux/fcntl.h>
10#include <linux/mm.h>
11#include <linux/proc_fs.h>
12#include <linux/notifier.h>
13#include <linux/kthread.h>
14#include <linux/mutex.h>
15#include <linux/io.h>
16
17#include <asm/page.h>
18#include <asm/pgtable.h>
19#include <asm/xen/hypervisor.h>
20#include <xen/xenbus.h>
21#include <xen/events.h>
22#include <xen/page.h>
23
24#include <xen/platform_pci.h>
25
26#include "xenbus_comms.h"
27#include "xenbus_probe.h"
28
29
30/* device/<type>/<id> => <type>-<id> */
31static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
32{
33 nodename = strchr(nodename, '/');
34 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
35 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
36 return -EINVAL;
37 }
38
39 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
40 if (!strchr(bus_id, '/')) {
41 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
42 return -EINVAL;
43 }
44 *strchr(bus_id, '/') = '-';
45 return 0;
46}
47
48/* device/<typename>/<name> */
49static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, const char *name)
50{
51 char *nodename;
52 int err;
53
54 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
55 if (!nodename)
56 return -ENOMEM;
57
58 DPRINTK("%s", nodename);
59
60 err = xenbus_probe_node(bus, type, nodename);
61 kfree(nodename);
62 return err;
63}
64
65static void backend_changed(struct xenbus_watch *watch,
66 const char **vec, unsigned int len)
67{
68 xenbus_otherend_changed(watch, vec, len, 1);
69}
70
71static struct device_attribute xenbus_frontend_dev_attrs[] = {
72 __ATTR_NULL
73};
74
75static struct xen_bus_type xenbus_frontend = {
76 .root = "device",
77 .levels = 2, /* device/type/<id> */
78 .get_bus_id = frontend_bus_id,
79 .probe = xenbus_probe_frontend,
80 .otherend_changed = backend_changed,
81 .bus = {
82 .name = "xen",
83 .match = xenbus_match,
84 .uevent = xenbus_uevent,
85 .probe = xenbus_dev_probe,
86 .remove = xenbus_dev_remove,
87 .shutdown = xenbus_dev_shutdown,
88 .dev_attrs= xenbus_frontend_dev_attrs,
89
90 .suspend = xenbus_dev_suspend,
91 .resume = xenbus_dev_resume,
92 },
93};
94
95static void frontend_changed(struct xenbus_watch *watch,
96 const char **vec, unsigned int len)
97{
98 DPRINTK("");
99
100 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
101}
102
103
104/* We watch for devices appearing and vanishing. */
105static struct xenbus_watch fe_watch = {
106 .node = "device",
107 .callback = frontend_changed,
108};
109
110static int read_backend_details(struct xenbus_device *xendev)
111{
112 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
113}
114
115static int is_device_connecting(struct device *dev, void *data)
116{
117 struct xenbus_device *xendev = to_xenbus_device(dev);
118 struct device_driver *drv = data;
119 struct xenbus_driver *xendrv;
120
121 /*
122 * A device with no driver will never connect. We care only about
123 * devices which should currently be in the process of connecting.
124 */
125 if (!dev->driver)
126 return 0;
127
128 /* Is this search limited to a particular driver? */
129 if (drv && (dev->driver != drv))
130 return 0;
131
132 xendrv = to_xenbus_driver(dev->driver);
133 return (xendev->state < XenbusStateConnected ||
134 (xendev->state == XenbusStateConnected &&
135 xendrv->is_ready && !xendrv->is_ready(xendev)));
136}
137
138static int exists_connecting_device(struct device_driver *drv)
139{
140 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
141 is_device_connecting);
142}
143
144static int print_device_status(struct device *dev, void *data)
145{
146 struct xenbus_device *xendev = to_xenbus_device(dev);
147 struct device_driver *drv = data;
148
149 /* Is this operation limited to a particular driver? */
150 if (drv && (dev->driver != drv))
151 return 0;
152
153 if (!dev->driver) {
154 /* Information only: is this too noisy? */
155 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
156 xendev->nodename);
157 } else if (xendev->state < XenbusStateConnected) {
158 enum xenbus_state rstate = XenbusStateUnknown;
159 if (xendev->otherend)
160 rstate = xenbus_read_driver_state(xendev->otherend);
161 printk(KERN_WARNING "XENBUS: Timeout connecting "
162 "to device: %s (local state %d, remote state %d)\n",
163 xendev->nodename, xendev->state, rstate);
164 }
165
166 return 0;
167}
168
169/* We only wait for device setup after most initcalls have run. */
170static int ready_to_wait_for_devices;
171
172/*
173 * On a 5-minute timeout, wait for all devices currently configured. We need
174 * to do this to guarantee that the filesystems and / or network devices
175 * needed for boot are available, before we can allow the boot to proceed.
176 *
177 * This needs to be on a late_initcall, to happen after the frontend device
178 * drivers have been initialised, but before the root fs is mounted.
179 *
180 * A possible improvement here would be to have the tools add a per-device
181 * flag to the store entry, indicating whether it is needed at boot time.
182 * This would allow people who knew what they were doing to accelerate their
183 * boot slightly, but of course needs tools or manual intervention to set up
184 * those flags correctly.
185 */
186static void wait_for_devices(struct xenbus_driver *xendrv)
187{
188 unsigned long start = jiffies;
189 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
190 unsigned int seconds_waited = 0;
191
192 if (!ready_to_wait_for_devices || !xen_domain())
193 return;
194
195 while (exists_connecting_device(drv)) {
196 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
197 if (!seconds_waited)
198 printk(KERN_WARNING "XENBUS: Waiting for "
199 "devices to initialise: ");
200 seconds_waited += 5;
201 printk("%us...", 300 - seconds_waited);
202 if (seconds_waited == 300)
203 break;
204 }
205
206 schedule_timeout_interruptible(HZ/10);
207 }
208
209 if (seconds_waited)
210 printk("\n");
211
212 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
213 print_device_status);
214}
215
216int __xenbus_register_frontend(struct xenbus_driver *drv,
217 struct module *owner, const char *mod_name)
218{
219 int ret;
220
221 drv->read_otherend_details = read_backend_details;
222
223 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
224 owner, mod_name);
225 if (ret)
226 return ret;
227
228 /* If this driver is loaded as a module wait for devices to attach. */
229 wait_for_devices(drv);
230
231 return 0;
232}
233EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
234
235static int __init xenbus_probe_frontend_init(void)
236{
237 int err;
238
239 DPRINTK("");
240
241 /* Register ourselves with the kernel bus subsystem */
242 err = bus_register(&xenbus_frontend.bus);
243 if (err) {
244 printk(KERN_CRIT "%s didn't register bus!\n", __func__);
245 return err;
246 }
247 printk(KERN_CRIT "%s bus registered ok\n", __func__);
248
249 if (!xen_initial_domain()) {
250 /* Enumerate devices in xenstore and watch for changes. */
251 xenbus_probe_devices(&xenbus_frontend);
252 printk(KERN_CRIT "%s devices probed ok\n", __func__);
253 register_xenbus_watch(&fe_watch);
254 printk(KERN_CRIT "%s watch add ok ok\n", __func__);
255 printk(KERN_CRIT "%s all done\n", __func__);
256 }
257
258 return 0;
259}
260
261module_init(xenbus_probe_frontend_init);
262
263#ifndef MODULE
264static int __init boot_wait_for_devices(void)
265{
266 if (xen_hvm_domain() && !xen_platform_pci_unplug)
267 return -ENODEV;
268
269 ready_to_wait_for_devices = 1;
270 wait_for_devices(NULL);
271 return 0;
272}
273
274late_initcall(boot_wait_for_devices);
275#endif