xen: add backend driver support
[pandora-kernel.git] / drivers / xen / xenbus / xenbus_probe_frontend.c
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, 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
65 static int xenbus_uevent_frontend(struct device *_dev, struct kobj_uevent_env *env)
66 {
67         struct xenbus_device *dev = to_xenbus_device(_dev);
68
69         if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
70                 return -ENOMEM;
71
72         return 0;
73 }
74
75
76 static void backend_changed(struct xenbus_watch *watch,
77                             const char **vec, unsigned int len)
78 {
79         xenbus_otherend_changed(watch, vec, len, 1);
80 }
81
82 static struct device_attribute xenbus_frontend_dev_attrs[] = {
83         __ATTR_NULL
84 };
85
86 static struct xen_bus_type xenbus_frontend = {
87         .root = "device",
88         .levels = 2,            /* device/type/<id> */
89         .get_bus_id = frontend_bus_id,
90         .probe = xenbus_probe_frontend,
91         .otherend_changed = backend_changed,
92         .bus = {
93                 .name     = "xen",
94                 .match    = xenbus_match,
95                 .uevent   = xenbus_uevent_frontend,
96                 .probe    = xenbus_dev_probe,
97                 .remove   = xenbus_dev_remove,
98                 .shutdown = xenbus_dev_shutdown,
99                 .dev_attrs= xenbus_frontend_dev_attrs,
100
101                 .suspend  = xenbus_dev_suspend,
102                 .resume   = xenbus_dev_resume,
103         },
104 };
105
106 static void frontend_changed(struct xenbus_watch *watch,
107                              const char **vec, unsigned int len)
108 {
109         DPRINTK("");
110
111         xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
112 }
113
114
115 /* We watch for devices appearing and vanishing. */
116 static struct xenbus_watch fe_watch = {
117         .node = "device",
118         .callback = frontend_changed,
119 };
120
121 static int read_backend_details(struct xenbus_device *xendev)
122 {
123         return xenbus_read_otherend_details(xendev, "backend-id", "backend");
124 }
125
126 static int is_device_connecting(struct device *dev, void *data)
127 {
128         struct xenbus_device *xendev = to_xenbus_device(dev);
129         struct device_driver *drv = data;
130         struct xenbus_driver *xendrv;
131
132         /*
133          * A device with no driver will never connect. We care only about
134          * devices which should currently be in the process of connecting.
135          */
136         if (!dev->driver)
137                 return 0;
138
139         /* Is this search limited to a particular driver? */
140         if (drv && (dev->driver != drv))
141                 return 0;
142
143         xendrv = to_xenbus_driver(dev->driver);
144         return (xendev->state < XenbusStateConnected ||
145                 (xendev->state == XenbusStateConnected &&
146                  xendrv->is_ready && !xendrv->is_ready(xendev)));
147 }
148
149 static int exists_connecting_device(struct device_driver *drv)
150 {
151         return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
152                                 is_device_connecting);
153 }
154
155 static int print_device_status(struct device *dev, void *data)
156 {
157         struct xenbus_device *xendev = to_xenbus_device(dev);
158         struct device_driver *drv = data;
159
160         /* Is this operation limited to a particular driver? */
161         if (drv && (dev->driver != drv))
162                 return 0;
163
164         if (!dev->driver) {
165                 /* Information only: is this too noisy? */
166                 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
167                        xendev->nodename);
168         } else if (xendev->state < XenbusStateConnected) {
169                 enum xenbus_state rstate = XenbusStateUnknown;
170                 if (xendev->otherend)
171                         rstate = xenbus_read_driver_state(xendev->otherend);
172                 printk(KERN_WARNING "XENBUS: Timeout connecting "
173                        "to device: %s (local state %d, remote state %d)\n",
174                        xendev->nodename, xendev->state, rstate);
175         }
176
177         return 0;
178 }
179
180 /* We only wait for device setup after most initcalls have run. */
181 static int ready_to_wait_for_devices;
182
183 /*
184  * On a 5-minute timeout, wait for all devices currently configured.  We need
185  * to do this to guarantee that the filesystems and / or network devices
186  * needed for boot are available, before we can allow the boot to proceed.
187  *
188  * This needs to be on a late_initcall, to happen after the frontend device
189  * drivers have been initialised, but before the root fs is mounted.
190  *
191  * A possible improvement here would be to have the tools add a per-device
192  * flag to the store entry, indicating whether it is needed at boot time.
193  * This would allow people who knew what they were doing to accelerate their
194  * boot slightly, but of course needs tools or manual intervention to set up
195  * those flags correctly.
196  */
197 static void wait_for_devices(struct xenbus_driver *xendrv)
198 {
199         unsigned long start = jiffies;
200         struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
201         unsigned int seconds_waited = 0;
202
203         if (!ready_to_wait_for_devices || !xen_domain())
204                 return;
205
206         while (exists_connecting_device(drv)) {
207                 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
208                         if (!seconds_waited)
209                                 printk(KERN_WARNING "XENBUS: Waiting for "
210                                        "devices to initialise: ");
211                         seconds_waited += 5;
212                         printk("%us...", 300 - seconds_waited);
213                         if (seconds_waited == 300)
214                                 break;
215                 }
216
217                 schedule_timeout_interruptible(HZ/10);
218         }
219
220         if (seconds_waited)
221                 printk("\n");
222
223         bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
224                          print_device_status);
225 }
226
227 int __xenbus_register_frontend(struct xenbus_driver *drv,
228                                struct module *owner, const char *mod_name)
229 {
230         int ret;
231
232         drv->read_otherend_details = read_backend_details;
233
234         ret = xenbus_register_driver_common(drv, &xenbus_frontend,
235                                             owner, mod_name);
236         if (ret)
237                 return ret;
238
239         /* If this driver is loaded as a module wait for devices to attach. */
240         wait_for_devices(drv);
241
242         return 0;
243 }
244 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
245
246 static int frontend_probe_and_watch(struct notifier_block *notifier,
247                                    unsigned long event,
248                                    void *data)
249 {
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         return NOTIFY_DONE;
257 }
258
259
260 static int __init xenbus_probe_frontend_init(void)
261 {
262         static struct notifier_block xenstore_notifier = {
263                 .notifier_call = frontend_probe_and_watch
264         };
265         int err;
266
267         DPRINTK("");
268
269         /* Register ourselves with the kernel bus subsystem */
270         err = bus_register(&xenbus_frontend.bus);
271         if (err) {
272                 printk(KERN_CRIT "%s didn't register bus!\n", __func__);
273                 return err;
274         }
275         printk(KERN_CRIT "%s bus registered ok\n", __func__);
276
277         register_xenstore_notifier(&xenstore_notifier);
278
279         return 0;
280 }
281
282 module_init(xenbus_probe_frontend_init);
283
284 #ifndef MODULE
285 static int __init boot_wait_for_devices(void)
286 {
287         if (xen_hvm_domain() && !xen_platform_pci_unplug)
288                 return -ENODEV;
289
290         ready_to_wait_for_devices = 1;
291         wait_for_devices(NULL);
292         return 0;
293 }
294
295 late_initcall(boot_wait_for_devices);
296 #endif