diff options
Diffstat (limited to 'drivers/xen/xenbus')
-rw-r--r-- | drivers/xen/xenbus/Makefile | 5 | ||||
-rw-r--r-- | drivers/xen/xenbus/xenbus_client.c | 2 | ||||
-rw-r--r-- | drivers/xen/xenbus/xenbus_probe.c | 394 | ||||
-rw-r--r-- | drivers/xen/xenbus/xenbus_probe.h | 32 | ||||
-rw-r--r-- | drivers/xen/xenbus/xenbus_probe_backend.c | 276 | ||||
-rw-r--r-- | drivers/xen/xenbus/xenbus_probe_frontend.c | 301 |
6 files changed, 696 insertions, 314 deletions
diff --git a/drivers/xen/xenbus/Makefile b/drivers/xen/xenbus/Makefile index 5571f5b84223..8dca685358b4 100644 --- a/drivers/xen/xenbus/Makefile +++ b/drivers/xen/xenbus/Makefile | |||
@@ -5,3 +5,8 @@ xenbus-objs += xenbus_client.o | |||
5 | xenbus-objs += xenbus_comms.o | 5 | xenbus-objs += xenbus_comms.o |
6 | xenbus-objs += xenbus_xs.o | 6 | xenbus-objs += xenbus_xs.o |
7 | xenbus-objs += xenbus_probe.o | 7 | xenbus-objs += xenbus_probe.o |
8 | |||
9 | xenbus-be-objs-$(CONFIG_XEN_BACKEND) += xenbus_probe_backend.o | ||
10 | xenbus-objs += $(xenbus-be-objs-y) | ||
11 | |||
12 | obj-$(CONFIG_XEN_XENBUS_FRONTEND) += xenbus_probe_frontend.o | ||
diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus_client.c index 7e49527189b6..cdacf923e073 100644 --- a/drivers/xen/xenbus/xenbus_client.c +++ b/drivers/xen/xenbus/xenbus_client.c | |||
@@ -50,6 +50,8 @@ const char *xenbus_strstate(enum xenbus_state state) | |||
50 | [ XenbusStateConnected ] = "Connected", | 50 | [ XenbusStateConnected ] = "Connected", |
51 | [ XenbusStateClosing ] = "Closing", | 51 | [ XenbusStateClosing ] = "Closing", |
52 | [ XenbusStateClosed ] = "Closed", | 52 | [ XenbusStateClosed ] = "Closed", |
53 | [XenbusStateReconfiguring] = "Reconfiguring", | ||
54 | [XenbusStateReconfigured] = "Reconfigured", | ||
53 | }; | 55 | }; |
54 | return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID"; | 56 | return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID"; |
55 | } | 57 | } |
diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c index d409495876f1..739769551e33 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" |
@@ -64,22 +63,15 @@ | |||
64 | 63 | ||
65 | 64 | ||
66 | int xen_store_evtchn; | 65 | int xen_store_evtchn; |
67 | EXPORT_SYMBOL(xen_store_evtchn); | 66 | EXPORT_SYMBOL_GPL(xen_store_evtchn); |
68 | 67 | ||
69 | struct xenstore_domain_interface *xen_store_interface; | 68 | struct xenstore_domain_interface *xen_store_interface; |
69 | EXPORT_SYMBOL_GPL(xen_store_interface); | ||
70 | |||
70 | static unsigned long xen_store_mfn; | 71 | static unsigned long xen_store_mfn; |
71 | 72 | ||
72 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); | 73 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); |
73 | 74 | ||
74 | static void wait_for_devices(struct xenbus_driver *xendrv); | ||
75 | |||
76 | static int xenbus_probe_frontend(const char *type, const char *name); | ||
77 | |||
78 | static void xenbus_dev_shutdown(struct device *_dev); | ||
79 | |||
80 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state); | ||
81 | static int xenbus_dev_resume(struct device *dev); | ||
82 | |||
83 | /* If something in array of ids matches this device, return it. */ | 75 | /* If something in array of ids matches this device, return it. */ |
84 | static const struct xenbus_device_id * | 76 | static const struct xenbus_device_id * |
85 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) | 77 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) |
@@ -100,34 +92,7 @@ int xenbus_match(struct device *_dev, struct device_driver *_drv) | |||
100 | 92 | ||
101 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; | 93 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; |
102 | } | 94 | } |
103 | 95 | EXPORT_SYMBOL_GPL(xenbus_match); | |
104 | static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env) | ||
105 | { | ||
106 | struct xenbus_device *dev = to_xenbus_device(_dev); | ||
107 | |||
108 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) | ||
109 | return -ENOMEM; | ||
110 | |||
111 | return 0; | ||
112 | } | ||
113 | |||
114 | /* device/<type>/<id> => <type>-<id> */ | ||
115 | static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) | ||
116 | { | ||
117 | nodename = strchr(nodename, '/'); | ||
118 | if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { | ||
119 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); | ||
120 | return -EINVAL; | ||
121 | } | ||
122 | |||
123 | strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); | ||
124 | if (!strchr(bus_id, '/')) { | ||
125 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); | ||
126 | return -EINVAL; | ||
127 | } | ||
128 | *strchr(bus_id, '/') = '-'; | ||
129 | return 0; | ||
130 | } | ||
131 | 96 | ||
132 | 97 | ||
133 | static void free_otherend_details(struct xenbus_device *dev) | 98 | static void free_otherend_details(struct xenbus_device *dev) |
@@ -147,7 +112,30 @@ static void free_otherend_watch(struct xenbus_device *dev) | |||
147 | } | 112 | } |
148 | 113 | ||
149 | 114 | ||
150 | int read_otherend_details(struct xenbus_device *xendev, | 115 | static 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 | |||
127 | static 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 | |||
138 | int xenbus_read_otherend_details(struct xenbus_device *xendev, | ||
151 | char *id_node, char *path_node) | 139 | char *id_node, char *path_node) |
152 | { | 140 | { |
153 | int err = xenbus_gather(XBT_NIL, xendev->nodename, | 141 | int err = xenbus_gather(XBT_NIL, xendev->nodename, |
@@ -172,39 +160,11 @@ int read_otherend_details(struct xenbus_device *xendev, | |||
172 | 160 | ||
173 | return 0; | 161 | return 0; |
174 | } | 162 | } |
163 | EXPORT_SYMBOL_GPL(xenbus_read_otherend_details); | ||
175 | 164 | ||
176 | 165 | void xenbus_otherend_changed(struct xenbus_watch *watch, | |
177 | static int read_backend_details(struct xenbus_device *xendev) | 166 | const char **vec, unsigned int len, |
178 | { | 167 | int ignore_on_shutdown) |
179 | return read_otherend_details(xendev, "backend-id", "backend"); | ||
180 | } | ||
181 | |||
182 | static struct device_attribute xenbus_dev_attrs[] = { | ||
183 | __ATTR_NULL | ||
184 | }; | ||
185 | |||
186 | /* Bus type for frontend drivers. */ | ||
187 | static struct xen_bus_type xenbus_frontend = { | ||
188 | .root = "device", | ||
189 | .levels = 2, /* device/type/<id> */ | ||
190 | .get_bus_id = frontend_bus_id, | ||
191 | .probe = xenbus_probe_frontend, | ||
192 | .bus = { | ||
193 | .name = "xen", | ||
194 | .match = xenbus_match, | ||
195 | .uevent = xenbus_uevent, | ||
196 | .probe = xenbus_dev_probe, | ||
197 | .remove = xenbus_dev_remove, | ||
198 | .shutdown = xenbus_dev_shutdown, | ||
199 | .dev_attrs = xenbus_dev_attrs, | ||
200 | |||
201 | .suspend = xenbus_dev_suspend, | ||
202 | .resume = xenbus_dev_resume, | ||
203 | }, | ||
204 | }; | ||
205 | |||
206 | static void otherend_changed(struct xenbus_watch *watch, | ||
207 | const char **vec, unsigned int len) | ||
208 | { | 168 | { |
209 | struct xenbus_device *dev = | 169 | struct xenbus_device *dev = |
210 | container_of(watch, struct xenbus_device, otherend_watch); | 170 | container_of(watch, struct xenbus_device, otherend_watch); |
@@ -232,11 +192,7 @@ static void otherend_changed(struct xenbus_watch *watch, | |||
232 | * work that can fail e.g., when the rootfs is gone. | 192 | * work that can fail e.g., when the rootfs is gone. |
233 | */ | 193 | */ |
234 | if (system_state > SYSTEM_RUNNING) { | 194 | if (system_state > SYSTEM_RUNNING) { |
235 | struct xen_bus_type *bus = bus; | 195 | if (ignore_on_shutdown && (state == XenbusStateClosing)) |
236 | bus = container_of(dev->dev.bus, struct xen_bus_type, bus); | ||
237 | /* If we're frontend, drive the state machine to Closed. */ | ||
238 | /* This should cause the backend to release our resources. */ | ||
239 | if ((bus == &xenbus_frontend) && (state == XenbusStateClosing)) | ||
240 | xenbus_frontend_closed(dev); | 196 | xenbus_frontend_closed(dev); |
241 | return; | 197 | return; |
242 | } | 198 | } |
@@ -244,25 +200,7 @@ static void otherend_changed(struct xenbus_watch *watch, | |||
244 | if (drv->otherend_changed) | 200 | if (drv->otherend_changed) |
245 | drv->otherend_changed(dev, state); | 201 | drv->otherend_changed(dev, state); |
246 | } | 202 | } |
247 | 203 | EXPORT_SYMBOL_GPL(xenbus_otherend_changed); | |
248 | |||
249 | static int talk_to_otherend(struct xenbus_device *dev) | ||
250 | { | ||
251 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); | ||
252 | |||
253 | free_otherend_watch(dev); | ||
254 | free_otherend_details(dev); | ||
255 | |||
256 | return drv->read_otherend_details(dev); | ||
257 | } | ||
258 | |||
259 | |||
260 | static int watch_otherend(struct xenbus_device *dev) | ||
261 | { | ||
262 | return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed, | ||
263 | "%s/%s", dev->otherend, "state"); | ||
264 | } | ||
265 | |||
266 | 204 | ||
267 | int xenbus_dev_probe(struct device *_dev) | 205 | int xenbus_dev_probe(struct device *_dev) |
268 | { | 206 | { |
@@ -306,8 +244,9 @@ int xenbus_dev_probe(struct device *_dev) | |||
306 | fail: | 244 | fail: |
307 | 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); |
308 | xenbus_switch_state(dev, XenbusStateClosed); | 246 | xenbus_switch_state(dev, XenbusStateClosed); |
309 | return -ENODEV; | 247 | return err; |
310 | } | 248 | } |
249 | EXPORT_SYMBOL_GPL(xenbus_dev_probe); | ||
311 | 250 | ||
312 | int xenbus_dev_remove(struct device *_dev) | 251 | int xenbus_dev_remove(struct device *_dev) |
313 | { | 252 | { |
@@ -325,8 +264,9 @@ int xenbus_dev_remove(struct device *_dev) | |||
325 | xenbus_switch_state(dev, XenbusStateClosed); | 264 | xenbus_switch_state(dev, XenbusStateClosed); |
326 | return 0; | 265 | return 0; |
327 | } | 266 | } |
267 | EXPORT_SYMBOL_GPL(xenbus_dev_remove); | ||
328 | 268 | ||
329 | static void xenbus_dev_shutdown(struct device *_dev) | 269 | void xenbus_dev_shutdown(struct device *_dev) |
330 | { | 270 | { |
331 | struct xenbus_device *dev = to_xenbus_device(_dev); | 271 | struct xenbus_device *dev = to_xenbus_device(_dev); |
332 | unsigned long timeout = 5*HZ; | 272 | unsigned long timeout = 5*HZ; |
@@ -347,6 +287,7 @@ static void xenbus_dev_shutdown(struct device *_dev) | |||
347 | out: | 287 | out: |
348 | put_device(&dev->dev); | 288 | put_device(&dev->dev); |
349 | } | 289 | } |
290 | EXPORT_SYMBOL_GPL(xenbus_dev_shutdown); | ||
350 | 291 | ||
351 | int xenbus_register_driver_common(struct xenbus_driver *drv, | 292 | int xenbus_register_driver_common(struct xenbus_driver *drv, |
352 | struct xen_bus_type *bus, | 293 | struct xen_bus_type *bus, |
@@ -360,25 +301,7 @@ int xenbus_register_driver_common(struct xenbus_driver *drv, | |||
360 | 301 | ||
361 | return driver_register(&drv->driver); | 302 | return driver_register(&drv->driver); |
362 | } | 303 | } |
363 | 304 | EXPORT_SYMBOL_GPL(xenbus_register_driver_common); | |
364 | int __xenbus_register_frontend(struct xenbus_driver *drv, | ||
365 | struct module *owner, const char *mod_name) | ||
366 | { | ||
367 | int ret; | ||
368 | |||
369 | drv->read_otherend_details = read_backend_details; | ||
370 | |||
371 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, | ||
372 | owner, mod_name); | ||
373 | if (ret) | ||
374 | return ret; | ||
375 | |||
376 | /* If this driver is loaded as a module wait for devices to attach. */ | ||
377 | wait_for_devices(drv); | ||
378 | |||
379 | return 0; | ||
380 | } | ||
381 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); | ||
382 | 305 | ||
383 | void xenbus_unregister_driver(struct xenbus_driver *drv) | 306 | void xenbus_unregister_driver(struct xenbus_driver *drv) |
384 | { | 307 | { |
@@ -549,24 +472,7 @@ fail: | |||
549 | kfree(xendev); | 472 | kfree(xendev); |
550 | return err; | 473 | return err; |
551 | } | 474 | } |
552 | 475 | EXPORT_SYMBOL_GPL(xenbus_probe_node); | |
553 | /* device/<typename>/<name> */ | ||
554 | static int xenbus_probe_frontend(const char *type, const char *name) | ||
555 | { | ||
556 | char *nodename; | ||
557 | int err; | ||
558 | |||
559 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", | ||
560 | xenbus_frontend.root, type, name); | ||
561 | if (!nodename) | ||
562 | return -ENOMEM; | ||
563 | |||
564 | DPRINTK("%s", nodename); | ||
565 | |||
566 | err = xenbus_probe_node(&xenbus_frontend, type, nodename); | ||
567 | kfree(nodename); | ||
568 | return err; | ||
569 | } | ||
570 | 476 | ||
571 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) | 477 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) |
572 | { | 478 | { |
@@ -580,10 +486,11 @@ static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) | |||
580 | return PTR_ERR(dir); | 486 | return PTR_ERR(dir); |
581 | 487 | ||
582 | for (i = 0; i < dir_n; i++) { | 488 | for (i = 0; i < dir_n; i++) { |
583 | err = bus->probe(type, dir[i]); | 489 | err = bus->probe(bus, type, dir[i]); |
584 | if (err) | 490 | if (err) |
585 | break; | 491 | break; |
586 | } | 492 | } |
493 | |||
587 | kfree(dir); | 494 | kfree(dir); |
588 | return err; | 495 | return err; |
589 | } | 496 | } |
@@ -603,9 +510,11 @@ int xenbus_probe_devices(struct xen_bus_type *bus) | |||
603 | if (err) | 510 | if (err) |
604 | break; | 511 | break; |
605 | } | 512 | } |
513 | |||
606 | kfree(dir); | 514 | kfree(dir); |
607 | return err; | 515 | return err; |
608 | } | 516 | } |
517 | EXPORT_SYMBOL_GPL(xenbus_probe_devices); | ||
609 | 518 | ||
610 | static unsigned int char_count(const char *str, char c) | 519 | static unsigned int char_count(const char *str, char c) |
611 | { | 520 | { |
@@ -668,54 +577,39 @@ void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) | |||
668 | } | 577 | } |
669 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); | 578 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); |
670 | 579 | ||
671 | static void frontend_changed(struct xenbus_watch *watch, | 580 | int xenbus_dev_suspend(struct device *dev) |
672 | const char **vec, unsigned int len) | ||
673 | { | ||
674 | DPRINTK(""); | ||
675 | |||
676 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | ||
677 | } | ||
678 | |||
679 | /* We watch for devices appearing and vanishing. */ | ||
680 | static struct xenbus_watch fe_watch = { | ||
681 | .node = "device", | ||
682 | .callback = frontend_changed, | ||
683 | }; | ||
684 | |||
685 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state) | ||
686 | { | 581 | { |
687 | int err = 0; | 582 | int err = 0; |
688 | struct xenbus_driver *drv; | 583 | struct xenbus_driver *drv; |
689 | struct xenbus_device *xdev; | 584 | struct xenbus_device *xdev |
585 | = container_of(dev, struct xenbus_device, dev); | ||
690 | 586 | ||
691 | DPRINTK(""); | 587 | DPRINTK("%s", xdev->nodename); |
692 | 588 | ||
693 | if (dev->driver == NULL) | 589 | if (dev->driver == NULL) |
694 | return 0; | 590 | return 0; |
695 | drv = to_xenbus_driver(dev->driver); | 591 | drv = to_xenbus_driver(dev->driver); |
696 | xdev = container_of(dev, struct xenbus_device, dev); | ||
697 | if (drv->suspend) | 592 | if (drv->suspend) |
698 | err = drv->suspend(xdev, state); | 593 | err = drv->suspend(xdev); |
699 | if (err) | 594 | if (err) |
700 | printk(KERN_WARNING | 595 | printk(KERN_WARNING |
701 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); | 596 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
702 | return 0; | 597 | return 0; |
703 | } | 598 | } |
599 | EXPORT_SYMBOL_GPL(xenbus_dev_suspend); | ||
704 | 600 | ||
705 | static int xenbus_dev_resume(struct device *dev) | 601 | int xenbus_dev_resume(struct device *dev) |
706 | { | 602 | { |
707 | int err; | 603 | int err; |
708 | struct xenbus_driver *drv; | 604 | struct xenbus_driver *drv; |
709 | struct xenbus_device *xdev; | 605 | struct xenbus_device *xdev |
606 | = container_of(dev, struct xenbus_device, dev); | ||
710 | 607 | ||
711 | DPRINTK(""); | 608 | DPRINTK("%s", xdev->nodename); |
712 | 609 | ||
713 | if (dev->driver == NULL) | 610 | if (dev->driver == NULL) |
714 | return 0; | 611 | return 0; |
715 | |||
716 | drv = to_xenbus_driver(dev->driver); | 612 | drv = to_xenbus_driver(dev->driver); |
717 | xdev = container_of(dev, struct xenbus_device, dev); | ||
718 | |||
719 | err = talk_to_otherend(xdev); | 613 | err = talk_to_otherend(xdev); |
720 | if (err) { | 614 | if (err) { |
721 | printk(KERN_WARNING | 615 | printk(KERN_WARNING |
@@ -746,6 +640,15 @@ static int xenbus_dev_resume(struct device *dev) | |||
746 | 640 | ||
747 | return 0; | 641 | return 0; |
748 | } | 642 | } |
643 | EXPORT_SYMBOL_GPL(xenbus_dev_resume); | ||
644 | |||
645 | int xenbus_dev_cancel(struct device *dev) | ||
646 | { | ||
647 | /* Do nothing */ | ||
648 | DPRINTK("cancel"); | ||
649 | return 0; | ||
650 | } | ||
651 | EXPORT_SYMBOL_GPL(xenbus_dev_cancel); | ||
749 | 652 | ||
750 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ | 653 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
751 | int xenstored_ready = 0; | 654 | int xenstored_ready = 0; |
@@ -774,11 +677,6 @@ void xenbus_probe(struct work_struct *unused) | |||
774 | { | 677 | { |
775 | xenstored_ready = 1; | 678 | xenstored_ready = 1; |
776 | 679 | ||
777 | /* Enumerate devices in xenstore and watch for changes. */ | ||
778 | xenbus_probe_devices(&xenbus_frontend); | ||
779 | register_xenbus_watch(&fe_watch); | ||
780 | xenbus_backend_probe_and_watch(); | ||
781 | |||
782 | /* Notify others that xenstore is up */ | 680 | /* Notify others that xenstore is up */ |
783 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); | 681 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); |
784 | } | 682 | } |
@@ -801,27 +699,43 @@ device_initcall(xenbus_probe_initcall); | |||
801 | static int __init xenbus_init(void) | 699 | static int __init xenbus_init(void) |
802 | { | 700 | { |
803 | int err = 0; | 701 | int err = 0; |
702 | unsigned long page = 0; | ||
804 | 703 | ||
805 | DPRINTK(""); | 704 | DPRINTK(""); |
806 | 705 | ||
807 | err = -ENODEV; | 706 | err = -ENODEV; |
808 | if (!xen_domain()) | 707 | if (!xen_domain()) |
809 | goto out_error; | 708 | return err; |
810 | |||
811 | /* Register ourselves with the kernel bus subsystem */ | ||
812 | err = bus_register(&xenbus_frontend.bus); | ||
813 | if (err) | ||
814 | goto out_error; | ||
815 | |||
816 | err = xenbus_backend_bus_register(); | ||
817 | if (err) | ||
818 | goto out_unreg_front; | ||
819 | 709 | ||
820 | /* | 710 | /* |
821 | * Domain0 doesn't have a store_evtchn or store_mfn yet. | 711 | * Domain0 doesn't have a store_evtchn or store_mfn yet. |
822 | */ | 712 | */ |
823 | if (xen_initial_domain()) { | 713 | if (xen_initial_domain()) { |
824 | /* dom0 not yet supported */ | 714 | struct evtchn_alloc_unbound alloc_unbound; |
715 | |||
716 | /* Allocate Xenstore page */ | ||
717 | page = get_zeroed_page(GFP_KERNEL); | ||
718 | if (!page) | ||
719 | goto out_error; | ||
720 | |||
721 | xen_store_mfn = xen_start_info->store_mfn = | ||
722 | pfn_to_mfn(virt_to_phys((void *)page) >> | ||
723 | PAGE_SHIFT); | ||
724 | |||
725 | /* Next allocate a local port which xenstored can bind to */ | ||
726 | alloc_unbound.dom = DOMID_SELF; | ||
727 | alloc_unbound.remote_dom = 0; | ||
728 | |||
729 | err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, | ||
730 | &alloc_unbound); | ||
731 | if (err == -ENOSYS) | ||
732 | goto out_error; | ||
733 | |||
734 | BUG_ON(err); | ||
735 | xen_store_evtchn = xen_start_info->store_evtchn = | ||
736 | alloc_unbound.port; | ||
737 | |||
738 | xen_store_interface = mfn_to_virt(xen_store_mfn); | ||
825 | } else { | 739 | } else { |
826 | if (xen_hvm_domain()) { | 740 | if (xen_hvm_domain()) { |
827 | uint64_t v = 0; | 741 | uint64_t v = 0; |
@@ -847,7 +761,7 @@ static int __init xenbus_init(void) | |||
847 | if (err) { | 761 | if (err) { |
848 | printk(KERN_WARNING | 762 | printk(KERN_WARNING |
849 | "XENBUS: Error initializing xenstore comms: %i\n", err); | 763 | "XENBUS: Error initializing xenstore comms: %i\n", err); |
850 | goto out_unreg_back; | 764 | goto out_error; |
851 | } | 765 | } |
852 | 766 | ||
853 | #ifdef CONFIG_XEN_COMPAT_XENFS | 767 | #ifdef CONFIG_XEN_COMPAT_XENFS |
@@ -860,131 +774,13 @@ static int __init xenbus_init(void) | |||
860 | 774 | ||
861 | return 0; | 775 | return 0; |
862 | 776 | ||
863 | out_unreg_back: | ||
864 | xenbus_backend_bus_unregister(); | ||
865 | |||
866 | out_unreg_front: | ||
867 | bus_unregister(&xenbus_frontend.bus); | ||
868 | |||
869 | out_error: | 777 | out_error: |
778 | if (page != 0) | ||
779 | free_page(page); | ||
780 | |||
870 | return err; | 781 | return err; |
871 | } | 782 | } |
872 | 783 | ||
873 | postcore_initcall(xenbus_init); | 784 | postcore_initcall(xenbus_init); |
874 | 785 | ||
875 | MODULE_LICENSE("GPL"); | 786 | MODULE_LICENSE("GPL"); |
876 | |||
877 | static int is_device_connecting(struct device *dev, void *data) | ||
878 | { | ||
879 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
880 | struct device_driver *drv = data; | ||
881 | struct xenbus_driver *xendrv; | ||
882 | |||
883 | /* | ||
884 | * A device with no driver will never connect. We care only about | ||
885 | * devices which should currently be in the process of connecting. | ||
886 | */ | ||
887 | if (!dev->driver) | ||
888 | return 0; | ||
889 | |||
890 | /* Is this search limited to a particular driver? */ | ||
891 | if (drv && (dev->driver != drv)) | ||
892 | return 0; | ||
893 | |||
894 | xendrv = to_xenbus_driver(dev->driver); | ||
895 | return (xendev->state < XenbusStateConnected || | ||
896 | (xendev->state == XenbusStateConnected && | ||
897 | xendrv->is_ready && !xendrv->is_ready(xendev))); | ||
898 | } | ||
899 | |||
900 | static int exists_connecting_device(struct device_driver *drv) | ||
901 | { | ||
902 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
903 | is_device_connecting); | ||
904 | } | ||
905 | |||
906 | static int print_device_status(struct device *dev, void *data) | ||
907 | { | ||
908 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
909 | struct device_driver *drv = data; | ||
910 | |||
911 | /* Is this operation limited to a particular driver? */ | ||
912 | if (drv && (dev->driver != drv)) | ||
913 | return 0; | ||
914 | |||
915 | if (!dev->driver) { | ||
916 | /* Information only: is this too noisy? */ | ||
917 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | ||
918 | xendev->nodename); | ||
919 | } else if (xendev->state < XenbusStateConnected) { | ||
920 | enum xenbus_state rstate = XenbusStateUnknown; | ||
921 | if (xendev->otherend) | ||
922 | rstate = xenbus_read_driver_state(xendev->otherend); | ||
923 | printk(KERN_WARNING "XENBUS: Timeout connecting " | ||
924 | "to device: %s (local state %d, remote state %d)\n", | ||
925 | xendev->nodename, xendev->state, rstate); | ||
926 | } | ||
927 | |||
928 | return 0; | ||
929 | } | ||
930 | |||
931 | /* We only wait for device setup after most initcalls have run. */ | ||
932 | static int ready_to_wait_for_devices; | ||
933 | |||
934 | /* | ||
935 | * On a 5-minute timeout, wait for all devices currently configured. We need | ||
936 | * to do this to guarantee that the filesystems and / or network devices | ||
937 | * needed for boot are available, before we can allow the boot to proceed. | ||
938 | * | ||
939 | * This needs to be on a late_initcall, to happen after the frontend device | ||
940 | * drivers have been initialised, but before the root fs is mounted. | ||
941 | * | ||
942 | * A possible improvement here would be to have the tools add a per-device | ||
943 | * flag to the store entry, indicating whether it is needed at boot time. | ||
944 | * This would allow people who knew what they were doing to accelerate their | ||
945 | * boot slightly, but of course needs tools or manual intervention to set up | ||
946 | * those flags correctly. | ||
947 | */ | ||
948 | static void wait_for_devices(struct xenbus_driver *xendrv) | ||
949 | { | ||
950 | unsigned long start = jiffies; | ||
951 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; | ||
952 | unsigned int seconds_waited = 0; | ||
953 | |||
954 | if (!ready_to_wait_for_devices || !xen_domain()) | ||
955 | return; | ||
956 | |||
957 | while (exists_connecting_device(drv)) { | ||
958 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { | ||
959 | if (!seconds_waited) | ||
960 | printk(KERN_WARNING "XENBUS: Waiting for " | ||
961 | "devices to initialise: "); | ||
962 | seconds_waited += 5; | ||
963 | printk("%us...", 300 - seconds_waited); | ||
964 | if (seconds_waited == 300) | ||
965 | break; | ||
966 | } | ||
967 | |||
968 | schedule_timeout_interruptible(HZ/10); | ||
969 | } | ||
970 | |||
971 | if (seconds_waited) | ||
972 | printk("\n"); | ||
973 | |||
974 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
975 | print_device_status); | ||
976 | } | ||
977 | |||
978 | #ifndef MODULE | ||
979 | static int __init boot_wait_for_devices(void) | ||
980 | { | ||
981 | if (xen_hvm_domain() && !xen_platform_pci_unplug) | ||
982 | return -ENODEV; | ||
983 | |||
984 | ready_to_wait_for_devices = 1; | ||
985 | wait_for_devices(NULL); | ||
986 | return 0; | ||
987 | } | ||
988 | |||
989 | late_initcall(boot_wait_for_devices); | ||
990 | #endif | ||
diff --git a/drivers/xen/xenbus/xenbus_probe.h b/drivers/xen/xenbus/xenbus_probe.h index 6c5e3185a6a2..888b9900ca08 100644 --- a/drivers/xen/xenbus/xenbus_probe.h +++ b/drivers/xen/xenbus/xenbus_probe.h | |||
@@ -36,26 +36,15 @@ | |||
36 | 36 | ||
37 | #define XEN_BUS_ID_SIZE 20 | 37 | #define XEN_BUS_ID_SIZE 20 |
38 | 38 | ||
39 | #ifdef CONFIG_XEN_BACKEND | ||
40 | extern void xenbus_backend_suspend(int (*fn)(struct device *, void *)); | ||
41 | extern void xenbus_backend_resume(int (*fn)(struct device *, void *)); | ||
42 | extern void xenbus_backend_probe_and_watch(void); | ||
43 | extern int xenbus_backend_bus_register(void); | ||
44 | extern void xenbus_backend_bus_unregister(void); | ||
45 | #else | ||
46 | static inline void xenbus_backend_suspend(int (*fn)(struct device *, void *)) {} | ||
47 | static inline void xenbus_backend_resume(int (*fn)(struct device *, void *)) {} | ||
48 | static inline void xenbus_backend_probe_and_watch(void) {} | ||
49 | static inline int xenbus_backend_bus_register(void) { return 0; } | ||
50 | static inline void xenbus_backend_bus_unregister(void) {} | ||
51 | #endif | ||
52 | |||
53 | struct xen_bus_type | 39 | struct 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, |
45 | const char *dir); | ||
46 | void (*otherend_changed)(struct xenbus_watch *watch, const char **vec, | ||
47 | unsigned int len); | ||
59 | struct bus_type bus; | 48 | struct bus_type bus; |
60 | }; | 49 | }; |
61 | 50 | ||
@@ -73,4 +62,17 @@ extern int xenbus_probe_devices(struct xen_bus_type *bus); | |||
73 | 62 | ||
74 | extern void xenbus_dev_changed(const char *node, struct xen_bus_type *bus); | 63 | extern void xenbus_dev_changed(const char *node, struct xen_bus_type *bus); |
75 | 64 | ||
65 | extern void xenbus_dev_shutdown(struct device *_dev); | ||
66 | |||
67 | extern int xenbus_dev_suspend(struct device *dev); | ||
68 | extern int xenbus_dev_resume(struct device *dev); | ||
69 | extern int xenbus_dev_cancel(struct device *dev); | ||
70 | |||
71 | extern void xenbus_otherend_changed(struct xenbus_watch *watch, | ||
72 | const char **vec, unsigned int len, | ||
73 | int ignore_on_shutdown); | ||
74 | |||
75 | extern int xenbus_read_otherend_details(struct xenbus_device *xendev, | ||
76 | char *id_node, char *path_node); | ||
77 | |||
76 | #endif | 78 | #endif |
diff --git a/drivers/xen/xenbus/xenbus_probe_backend.c b/drivers/xen/xenbus/xenbus_probe_backend.c new file mode 100644 index 000000000000..6cf467bf63ec --- /dev/null +++ b/drivers/xen/xenbus/xenbus_probe_backend.c | |||
@@ -0,0 +1,276 @@ | |||
1 | /****************************************************************************** | ||
2 | * Talks to Xen Store to figure out what devices we have (backend half). | ||
3 | * | ||
4 | * Copyright (C) 2005 Rusty Russell, IBM Corporation | ||
5 | * Copyright (C) 2005 Mike Wray, Hewlett-Packard | ||
6 | * Copyright (C) 2005, 2006 XenSource Ltd | ||
7 | * Copyright (C) 2007 Solarflare Communications, Inc. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU General Public License version 2 | ||
11 | * as published by the Free Software Foundation; or, when distributed | ||
12 | * separately from the Linux kernel or incorporated into other | ||
13 | * software packages, subject to the following license: | ||
14 | * | ||
15 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
16 | * of this source file (the "Software"), to deal in the Software without | ||
17 | * restriction, including without limitation the rights to use, copy, modify, | ||
18 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | ||
19 | * and to permit persons to whom the Software is furnished to do so, subject to | ||
20 | * the following conditions: | ||
21 | * | ||
22 | * The above copyright notice and this permission notice shall be included in | ||
23 | * all copies or substantial portions of the Software. | ||
24 | * | ||
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
30 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
31 | * IN THE SOFTWARE. | ||
32 | */ | ||
33 | |||
34 | #define DPRINTK(fmt, args...) \ | ||
35 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ | ||
36 | __func__, __LINE__, ##args) | ||
37 | |||
38 | #include <linux/kernel.h> | ||
39 | #include <linux/err.h> | ||
40 | #include <linux/string.h> | ||
41 | #include <linux/ctype.h> | ||
42 | #include <linux/fcntl.h> | ||
43 | #include <linux/mm.h> | ||
44 | #include <linux/notifier.h> | ||
45 | |||
46 | #include <asm/page.h> | ||
47 | #include <asm/pgtable.h> | ||
48 | #include <asm/xen/hypervisor.h> | ||
49 | #include <asm/hypervisor.h> | ||
50 | #include <xen/xenbus.h> | ||
51 | #include <xen/features.h> | ||
52 | |||
53 | #include "xenbus_comms.h" | ||
54 | #include "xenbus_probe.h" | ||
55 | |||
56 | /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */ | ||
57 | static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) | ||
58 | { | ||
59 | int domid, err; | ||
60 | const char *devid, *type, *frontend; | ||
61 | unsigned int typelen; | ||
62 | |||
63 | type = strchr(nodename, '/'); | ||
64 | if (!type) | ||
65 | return -EINVAL; | ||
66 | type++; | ||
67 | typelen = strcspn(type, "/"); | ||
68 | if (!typelen || type[typelen] != '/') | ||
69 | return -EINVAL; | ||
70 | |||
71 | devid = strrchr(nodename, '/') + 1; | ||
72 | |||
73 | err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid, | ||
74 | "frontend", NULL, &frontend, | ||
75 | NULL); | ||
76 | if (err) | ||
77 | return err; | ||
78 | if (strlen(frontend) == 0) | ||
79 | err = -ERANGE; | ||
80 | if (!err && !xenbus_exists(XBT_NIL, frontend, "")) | ||
81 | err = -ENOENT; | ||
82 | kfree(frontend); | ||
83 | |||
84 | if (err) | ||
85 | return err; | ||
86 | |||
87 | if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s", | ||
88 | typelen, type, domid, devid) >= XEN_BUS_ID_SIZE) | ||
89 | return -ENOSPC; | ||
90 | return 0; | ||
91 | } | ||
92 | |||
93 | static int xenbus_uevent_backend(struct device *dev, | ||
94 | struct kobj_uevent_env *env) | ||
95 | { | ||
96 | struct xenbus_device *xdev; | ||
97 | struct xenbus_driver *drv; | ||
98 | struct xen_bus_type *bus; | ||
99 | |||
100 | DPRINTK(""); | ||
101 | |||
102 | if (dev == NULL) | ||
103 | return -ENODEV; | ||
104 | |||
105 | xdev = to_xenbus_device(dev); | ||
106 | bus = container_of(xdev->dev.bus, struct xen_bus_type, bus); | ||
107 | if (xdev == NULL) | ||
108 | return -ENODEV; | ||
109 | |||
110 | /* stuff we want to pass to /sbin/hotplug */ | ||
111 | if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype)) | ||
112 | return -ENOMEM; | ||
113 | |||
114 | if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename)) | ||
115 | return -ENOMEM; | ||
116 | |||
117 | if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root)) | ||
118 | return -ENOMEM; | ||
119 | |||
120 | if (dev->driver) { | ||
121 | drv = to_xenbus_driver(dev->driver); | ||
122 | if (drv && drv->uevent) | ||
123 | return drv->uevent(xdev, env); | ||
124 | } | ||
125 | |||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | /* backend/<typename>/<frontend-uuid>/<name> */ | ||
130 | static int xenbus_probe_backend_unit(struct xen_bus_type *bus, | ||
131 | const char *dir, | ||
132 | const char *type, | ||
133 | const char *name) | ||
134 | { | ||
135 | char *nodename; | ||
136 | int err; | ||
137 | |||
138 | nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name); | ||
139 | if (!nodename) | ||
140 | return -ENOMEM; | ||
141 | |||
142 | DPRINTK("%s\n", nodename); | ||
143 | |||
144 | err = xenbus_probe_node(bus, type, nodename); | ||
145 | kfree(nodename); | ||
146 | return err; | ||
147 | } | ||
148 | |||
149 | /* backend/<typename>/<frontend-domid> */ | ||
150 | static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type, | ||
151 | const char *domid) | ||
152 | { | ||
153 | char *nodename; | ||
154 | int err = 0; | ||
155 | char **dir; | ||
156 | unsigned int i, dir_n = 0; | ||
157 | |||
158 | DPRINTK(""); | ||
159 | |||
160 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid); | ||
161 | if (!nodename) | ||
162 | return -ENOMEM; | ||
163 | |||
164 | dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n); | ||
165 | if (IS_ERR(dir)) { | ||
166 | kfree(nodename); | ||
167 | return PTR_ERR(dir); | ||
168 | } | ||
169 | |||
170 | for (i = 0; i < dir_n; i++) { | ||
171 | err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]); | ||
172 | if (err) | ||
173 | break; | ||
174 | } | ||
175 | kfree(dir); | ||
176 | kfree(nodename); | ||
177 | return err; | ||
178 | } | ||
179 | |||
180 | static void frontend_changed(struct xenbus_watch *watch, | ||
181 | const char **vec, unsigned int len) | ||
182 | { | ||
183 | xenbus_otherend_changed(watch, vec, len, 0); | ||
184 | } | ||
185 | |||
186 | static struct device_attribute xenbus_backend_dev_attrs[] = { | ||
187 | __ATTR_NULL | ||
188 | }; | ||
189 | |||
190 | static struct xen_bus_type xenbus_backend = { | ||
191 | .root = "backend", | ||
192 | .levels = 3, /* backend/type/<frontend>/<id> */ | ||
193 | .get_bus_id = backend_bus_id, | ||
194 | .probe = xenbus_probe_backend, | ||
195 | .otherend_changed = frontend_changed, | ||
196 | .bus = { | ||
197 | .name = "xen-backend", | ||
198 | .match = xenbus_match, | ||
199 | .uevent = xenbus_uevent_backend, | ||
200 | .probe = xenbus_dev_probe, | ||
201 | .remove = xenbus_dev_remove, | ||
202 | .shutdown = xenbus_dev_shutdown, | ||
203 | .dev_attrs = xenbus_backend_dev_attrs, | ||
204 | }, | ||
205 | }; | ||
206 | |||
207 | static void backend_changed(struct xenbus_watch *watch, | ||
208 | const char **vec, unsigned int len) | ||
209 | { | ||
210 | DPRINTK(""); | ||
211 | |||
212 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend); | ||
213 | } | ||
214 | |||
215 | static struct xenbus_watch be_watch = { | ||
216 | .node = "backend", | ||
217 | .callback = backend_changed, | ||
218 | }; | ||
219 | |||
220 | static int read_frontend_details(struct xenbus_device *xendev) | ||
221 | { | ||
222 | return xenbus_read_otherend_details(xendev, "frontend-id", "frontend"); | ||
223 | } | ||
224 | |||
225 | int xenbus_dev_is_online(struct xenbus_device *dev) | ||
226 | { | ||
227 | int rc, val; | ||
228 | |||
229 | rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val); | ||
230 | if (rc != 1) | ||
231 | val = 0; /* no online node present */ | ||
232 | |||
233 | return val; | ||
234 | } | ||
235 | EXPORT_SYMBOL_GPL(xenbus_dev_is_online); | ||
236 | |||
237 | int __xenbus_register_backend(struct xenbus_driver *drv, | ||
238 | struct module *owner, const char *mod_name) | ||
239 | { | ||
240 | drv->read_otherend_details = read_frontend_details; | ||
241 | |||
242 | return xenbus_register_driver_common(drv, &xenbus_backend, | ||
243 | owner, mod_name); | ||
244 | } | ||
245 | EXPORT_SYMBOL_GPL(__xenbus_register_backend); | ||
246 | |||
247 | static int backend_probe_and_watch(struct notifier_block *notifier, | ||
248 | unsigned long event, | ||
249 | void *data) | ||
250 | { | ||
251 | /* Enumerate devices in xenstore and watch for changes. */ | ||
252 | xenbus_probe_devices(&xenbus_backend); | ||
253 | register_xenbus_watch(&be_watch); | ||
254 | |||
255 | return NOTIFY_DONE; | ||
256 | } | ||
257 | |||
258 | static int __init xenbus_probe_backend_init(void) | ||
259 | { | ||
260 | static struct notifier_block xenstore_notifier = { | ||
261 | .notifier_call = backend_probe_and_watch | ||
262 | }; | ||
263 | int err; | ||
264 | |||
265 | DPRINTK(""); | ||
266 | |||
267 | /* Register ourselves with the kernel bus subsystem */ | ||
268 | err = bus_register(&xenbus_backend.bus); | ||
269 | if (err) | ||
270 | return err; | ||
271 | |||
272 | register_xenstore_notifier(&xenstore_notifier); | ||
273 | |||
274 | return 0; | ||
275 | } | ||
276 | subsys_initcall(xenbus_probe_backend_init); | ||
diff --git a/drivers/xen/xenbus/xenbus_probe_frontend.c b/drivers/xen/xenbus/xenbus_probe_frontend.c new file mode 100644 index 000000000000..b6a2690c9d49 --- /dev/null +++ b/drivers/xen/xenbus/xenbus_probe_frontend.c | |||
@@ -0,0 +1,301 @@ | |||
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> */ | ||
31 | static 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> */ | ||
49 | static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, | ||
50 | const char *name) | ||
51 | { | ||
52 | char *nodename; | ||
53 | int err; | ||
54 | |||
55 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name); | ||
56 | if (!nodename) | ||
57 | return -ENOMEM; | ||
58 | |||
59 | DPRINTK("%s", nodename); | ||
60 | |||
61 | err = xenbus_probe_node(bus, type, nodename); | ||
62 | kfree(nodename); | ||
63 | return err; | ||
64 | } | ||
65 | |||
66 | static int xenbus_uevent_frontend(struct device *_dev, | ||
67 | struct kobj_uevent_env *env) | ||
68 | { | ||
69 | struct xenbus_device *dev = to_xenbus_device(_dev); | ||
70 | |||
71 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) | ||
72 | return -ENOMEM; | ||
73 | |||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | |||
78 | static void backend_changed(struct xenbus_watch *watch, | ||
79 | const char **vec, unsigned int len) | ||
80 | { | ||
81 | xenbus_otherend_changed(watch, vec, len, 1); | ||
82 | } | ||
83 | |||
84 | static struct device_attribute xenbus_frontend_dev_attrs[] = { | ||
85 | __ATTR_NULL | ||
86 | }; | ||
87 | |||
88 | static const struct dev_pm_ops xenbus_pm_ops = { | ||
89 | .suspend = xenbus_dev_suspend, | ||
90 | .resume = xenbus_dev_resume, | ||
91 | .freeze = xenbus_dev_suspend, | ||
92 | .thaw = xenbus_dev_cancel, | ||
93 | .restore = xenbus_dev_resume, | ||
94 | }; | ||
95 | |||
96 | static struct xen_bus_type xenbus_frontend = { | ||
97 | .root = "device", | ||
98 | .levels = 2, /* device/type/<id> */ | ||
99 | .get_bus_id = frontend_bus_id, | ||
100 | .probe = xenbus_probe_frontend, | ||
101 | .otherend_changed = backend_changed, | ||
102 | .bus = { | ||
103 | .name = "xen", | ||
104 | .match = xenbus_match, | ||
105 | .uevent = xenbus_uevent_frontend, | ||
106 | .probe = xenbus_dev_probe, | ||
107 | .remove = xenbus_dev_remove, | ||
108 | .shutdown = xenbus_dev_shutdown, | ||
109 | .dev_attrs = xenbus_frontend_dev_attrs, | ||
110 | |||
111 | .pm = &xenbus_pm_ops, | ||
112 | }, | ||
113 | }; | ||
114 | |||
115 | static void frontend_changed(struct xenbus_watch *watch, | ||
116 | const char **vec, unsigned int len) | ||
117 | { | ||
118 | DPRINTK(""); | ||
119 | |||
120 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | ||
121 | } | ||
122 | |||
123 | |||
124 | /* We watch for devices appearing and vanishing. */ | ||
125 | static struct xenbus_watch fe_watch = { | ||
126 | .node = "device", | ||
127 | .callback = frontend_changed, | ||
128 | }; | ||
129 | |||
130 | static int read_backend_details(struct xenbus_device *xendev) | ||
131 | { | ||
132 | return xenbus_read_otherend_details(xendev, "backend-id", "backend"); | ||
133 | } | ||
134 | |||
135 | static int is_device_connecting(struct device *dev, void *data) | ||
136 | { | ||
137 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
138 | struct device_driver *drv = data; | ||
139 | struct xenbus_driver *xendrv; | ||
140 | |||
141 | /* | ||
142 | * A device with no driver will never connect. We care only about | ||
143 | * devices which should currently be in the process of connecting. | ||
144 | */ | ||
145 | if (!dev->driver) | ||
146 | return 0; | ||
147 | |||
148 | /* Is this search limited to a particular driver? */ | ||
149 | if (drv && (dev->driver != drv)) | ||
150 | return 0; | ||
151 | |||
152 | xendrv = to_xenbus_driver(dev->driver); | ||
153 | return (xendev->state < XenbusStateConnected || | ||
154 | (xendev->state == XenbusStateConnected && | ||
155 | xendrv->is_ready && !xendrv->is_ready(xendev))); | ||
156 | } | ||
157 | |||
158 | static int exists_connecting_device(struct device_driver *drv) | ||
159 | { | ||
160 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
161 | is_device_connecting); | ||
162 | } | ||
163 | |||
164 | static int print_device_status(struct device *dev, void *data) | ||
165 | { | ||
166 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
167 | struct device_driver *drv = data; | ||
168 | |||
169 | /* Is this operation limited to a particular driver? */ | ||
170 | if (drv && (dev->driver != drv)) | ||
171 | return 0; | ||
172 | |||
173 | if (!dev->driver) { | ||
174 | /* Information only: is this too noisy? */ | ||
175 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | ||
176 | xendev->nodename); | ||
177 | } else if (xendev->state < XenbusStateConnected) { | ||
178 | enum xenbus_state rstate = XenbusStateUnknown; | ||
179 | if (xendev->otherend) | ||
180 | rstate = xenbus_read_driver_state(xendev->otherend); | ||
181 | printk(KERN_WARNING "XENBUS: Timeout connecting " | ||
182 | "to device: %s (local state %d, remote state %d)\n", | ||
183 | xendev->nodename, xendev->state, rstate); | ||
184 | } | ||
185 | |||
186 | return 0; | ||
187 | } | ||
188 | |||
189 | /* We only wait for device setup after most initcalls have run. */ | ||
190 | static int ready_to_wait_for_devices; | ||
191 | |||
192 | /* | ||
193 | * On a 5-minute timeout, wait for all devices currently configured. We need | ||
194 | * to do this to guarantee that the filesystems and / or network devices | ||
195 | * needed for boot are available, before we can allow the boot to proceed. | ||
196 | * | ||
197 | * This needs to be on a late_initcall, to happen after the frontend device | ||
198 | * drivers have been initialised, but before the root fs is mounted. | ||
199 | * | ||
200 | * A possible improvement here would be to have the tools add a per-device | ||
201 | * flag to the store entry, indicating whether it is needed at boot time. | ||
202 | * This would allow people who knew what they were doing to accelerate their | ||
203 | * boot slightly, but of course needs tools or manual intervention to set up | ||
204 | * those flags correctly. | ||
205 | */ | ||
206 | static void wait_for_devices(struct xenbus_driver *xendrv) | ||
207 | { | ||
208 | unsigned long start = jiffies; | ||
209 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; | ||
210 | unsigned int seconds_waited = 0; | ||
211 | |||
212 | if (!ready_to_wait_for_devices || !xen_domain()) | ||
213 | return; | ||
214 | |||
215 | while (exists_connecting_device(drv)) { | ||
216 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { | ||
217 | if (!seconds_waited) | ||
218 | printk(KERN_WARNING "XENBUS: Waiting for " | ||
219 | "devices to initialise: "); | ||
220 | seconds_waited += 5; | ||
221 | printk("%us...", 300 - seconds_waited); | ||
222 | if (seconds_waited == 300) | ||
223 | break; | ||
224 | } | ||
225 | |||
226 | schedule_timeout_interruptible(HZ/10); | ||
227 | } | ||
228 | |||
229 | if (seconds_waited) | ||
230 | printk("\n"); | ||
231 | |||
232 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
233 | print_device_status); | ||
234 | } | ||
235 | |||
236 | int __xenbus_register_frontend(struct xenbus_driver *drv, | ||
237 | struct module *owner, const char *mod_name) | ||
238 | { | ||
239 | int ret; | ||
240 | |||
241 | drv->read_otherend_details = read_backend_details; | ||
242 | |||
243 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, | ||
244 | owner, mod_name); | ||
245 | if (ret) | ||
246 | return ret; | ||
247 | |||
248 | /* If this driver is loaded as a module wait for devices to attach. */ | ||
249 | wait_for_devices(drv); | ||
250 | |||
251 | return 0; | ||
252 | } | ||
253 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); | ||
254 | |||
255 | static int frontend_probe_and_watch(struct notifier_block *notifier, | ||
256 | unsigned long event, | ||
257 | void *data) | ||
258 | { | ||
259 | /* Enumerate devices in xenstore and watch for changes. */ | ||
260 | xenbus_probe_devices(&xenbus_frontend); | ||
261 | register_xenbus_watch(&fe_watch); | ||
262 | |||
263 | return NOTIFY_DONE; | ||
264 | } | ||
265 | |||
266 | |||
267 | static int __init xenbus_probe_frontend_init(void) | ||
268 | { | ||
269 | static struct notifier_block xenstore_notifier = { | ||
270 | .notifier_call = frontend_probe_and_watch | ||
271 | }; | ||
272 | int err; | ||
273 | |||
274 | DPRINTK(""); | ||
275 | |||
276 | /* Register ourselves with the kernel bus subsystem */ | ||
277 | err = bus_register(&xenbus_frontend.bus); | ||
278 | if (err) | ||
279 | return err; | ||
280 | |||
281 | register_xenstore_notifier(&xenstore_notifier); | ||
282 | |||
283 | return 0; | ||
284 | } | ||
285 | subsys_initcall(xenbus_probe_frontend_init); | ||
286 | |||
287 | #ifndef MODULE | ||
288 | static int __init boot_wait_for_devices(void) | ||
289 | { | ||
290 | if (xen_hvm_domain() && !xen_platform_pci_unplug) | ||
291 | return -ENODEV; | ||
292 | |||
293 | ready_to_wait_for_devices = 1; | ||
294 | wait_for_devices(NULL); | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | late_initcall(boot_wait_for_devices); | ||
299 | #endif | ||
300 | |||
301 | MODULE_LICENSE("GPL"); | ||