blob: 96bd1ef61ebee536480af99b906bc3874b42f269 [file] [log] [blame]
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -07001/******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
3 *
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
32
33#define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __func__, __LINE__, ##args)
36
37#include <linux/kernel.h>
38#include <linux/err.h>
39#include <linux/string.h>
40#include <linux/ctype.h>
41#include <linux/fcntl.h>
42#include <linux/mm.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080043#include <linux/proc_fs.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070044#include <linux/notifier.h>
45#include <linux/kthread.h>
46#include <linux/mutex.h>
47#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070049
50#include <asm/page.h>
51#include <asm/pgtable.h>
52#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070053
54#include <xen/xen.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070055#include <xen/xenbus.h>
56#include <xen/events.h>
57#include <xen/page.h>
58
Sheng Yangbee6ab52010-05-14 12:39:33 +010059#include <xen/hvm.h>
60
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070061#include "xenbus_comms.h"
62#include "xenbus_probe.h"
63
Alex Zeffertt1107ba82009-01-07 18:07:11 -080064
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070065int xen_store_evtchn;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070066EXPORT_SYMBOL_GPL(xen_store_evtchn);
Alex Zeffertt1107ba82009-01-07 18:07:11 -080067
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070068struct xenstore_domain_interface *xen_store_interface;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070069EXPORT_SYMBOL_GPL(xen_store_interface);
70
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070071static unsigned long xen_store_mfn;
72
73static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
74
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070075/* If something in array of ids matches this device, return it. */
76static const struct xenbus_device_id *
77match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
78{
79 for (; *arr->devicetype != '\0'; arr++) {
80 if (!strcmp(arr->devicetype, dev->devicetype))
81 return arr;
82 }
83 return NULL;
84}
85
86int xenbus_match(struct device *_dev, struct device_driver *_drv)
87{
88 struct xenbus_driver *drv = to_xenbus_driver(_drv);
89
90 if (!drv->ids)
91 return 0;
92
93 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
94}
Ian Campbell2de06cc2009-02-09 12:05:51 -080095EXPORT_SYMBOL_GPL(xenbus_match);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070096
Ian Campbell2de06cc2009-02-09 12:05:51 -080097
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070098static void free_otherend_details(struct xenbus_device *dev)
99{
100 kfree(dev->otherend);
101 dev->otherend = NULL;
102}
103
104
105static void free_otherend_watch(struct xenbus_device *dev)
106{
107 if (dev->otherend_watch.node) {
108 unregister_xenbus_watch(&dev->otherend_watch);
109 kfree(dev->otherend_watch.node);
110 dev->otherend_watch.node = NULL;
111 }
112}
113
114
Ian Campbell2de06cc2009-02-09 12:05:51 -0800115static int talk_to_otherend(struct xenbus_device *dev)
116{
117 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
118
119 free_otherend_watch(dev);
120 free_otherend_details(dev);
121
122 return drv->read_otherend_details(dev);
123}
124
125
126
127static int watch_otherend(struct xenbus_device *dev)
128{
129 struct xen_bus_type *bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
130
131 return xenbus_watch_pathfmt(dev, &dev->otherend_watch, bus->otherend_changed,
132 "%s/%s", dev->otherend, "state");
133}
134
135
136int xenbus_read_otherend_details(struct xenbus_device *xendev,
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700137 char *id_node, char *path_node)
138{
139 int err = xenbus_gather(XBT_NIL, xendev->nodename,
140 id_node, "%i", &xendev->otherend_id,
141 path_node, NULL, &xendev->otherend,
142 NULL);
143 if (err) {
144 xenbus_dev_fatal(xendev, err,
145 "reading other end details from %s",
146 xendev->nodename);
147 return err;
148 }
149 if (strlen(xendev->otherend) == 0 ||
150 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
151 xenbus_dev_fatal(xendev, -ENOENT,
152 "unable to read other end from %s. "
153 "missing or inaccessible.",
154 xendev->nodename);
155 free_otherend_details(xendev);
156 return -ENOENT;
157 }
158
159 return 0;
160}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800161EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700162
Ian Campbell2de06cc2009-02-09 12:05:51 -0800163void xenbus_otherend_changed(struct xenbus_watch *watch,
164 const char **vec, unsigned int len,
165 int ignore_on_shutdown)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700166{
167 struct xenbus_device *dev =
168 container_of(watch, struct xenbus_device, otherend_watch);
169 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
170 enum xenbus_state state;
171
172 /* Protect us against watches firing on old details when the otherend
173 details change, say immediately after a resume. */
174 if (!dev->otherend ||
175 strncmp(dev->otherend, vec[XS_WATCH_PATH],
176 strlen(dev->otherend))) {
Joe Perches898eb712007-10-18 03:06:30 -0700177 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
178 vec[XS_WATCH_PATH]);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700179 return;
180 }
181
182 state = xenbus_read_driver_state(dev->otherend);
183
Joe Perches898eb712007-10-18 03:06:30 -0700184 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700185 state, xenbus_strstate(state), dev->otherend_watch.node,
186 vec[XS_WATCH_PATH]);
187
188 /*
189 * Ignore xenbus transitions during shutdown. This prevents us doing
190 * work that can fail e.g., when the rootfs is gone.
191 */
192 if (system_state > SYSTEM_RUNNING) {
Ian Campbell2de06cc2009-02-09 12:05:51 -0800193 if (ignore_on_shutdown && (state == XenbusStateClosing))
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700194 xenbus_frontend_closed(dev);
195 return;
196 }
197
198 if (drv->otherend_changed)
199 drv->otherend_changed(dev, state);
200}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800201EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700202
203int xenbus_dev_probe(struct device *_dev)
204{
205 struct xenbus_device *dev = to_xenbus_device(_dev);
206 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
207 const struct xenbus_device_id *id;
208 int err;
209
210 DPRINTK("%s", dev->nodename);
211
212 if (!drv->probe) {
213 err = -ENODEV;
214 goto fail;
215 }
216
217 id = match_device(drv->ids, dev);
218 if (!id) {
219 err = -ENODEV;
220 goto fail;
221 }
222
223 err = talk_to_otherend(dev);
224 if (err) {
225 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
226 dev->nodename);
227 return err;
228 }
229
230 err = drv->probe(dev, id);
231 if (err)
232 goto fail;
233
234 err = watch_otherend(dev);
235 if (err) {
236 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
237 dev->nodename);
238 return err;
239 }
240
241 return 0;
242fail:
243 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
244 xenbus_switch_state(dev, XenbusStateClosed);
Jeremy Fitzhardinge7432e4b2009-10-29 14:19:42 -0700245 return err;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700246}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800247EXPORT_SYMBOL_GPL(xenbus_dev_probe);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700248
249int xenbus_dev_remove(struct device *_dev)
250{
251 struct xenbus_device *dev = to_xenbus_device(_dev);
252 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
253
254 DPRINTK("%s", dev->nodename);
255
256 free_otherend_watch(dev);
257 free_otherend_details(dev);
258
259 if (drv->remove)
260 drv->remove(dev);
261
262 xenbus_switch_state(dev, XenbusStateClosed);
263 return 0;
264}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800265EXPORT_SYMBOL_GPL(xenbus_dev_remove);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700266
Ian Campbell2de06cc2009-02-09 12:05:51 -0800267void xenbus_dev_shutdown(struct device *_dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700268{
269 struct xenbus_device *dev = to_xenbus_device(_dev);
270 unsigned long timeout = 5*HZ;
271
272 DPRINTK("%s", dev->nodename);
273
274 get_device(&dev->dev);
275 if (dev->state != XenbusStateConnected) {
276 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
277 dev->nodename, xenbus_strstate(dev->state));
278 goto out;
279 }
280 xenbus_switch_state(dev, XenbusStateClosing);
281 timeout = wait_for_completion_timeout(&dev->down, timeout);
282 if (!timeout)
283 printk(KERN_INFO "%s: %s timeout closing device\n",
284 __func__, dev->nodename);
285 out:
286 put_device(&dev->dev);
287}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800288EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700289
290int xenbus_register_driver_common(struct xenbus_driver *drv,
291 struct xen_bus_type *bus,
292 struct module *owner,
293 const char *mod_name)
294{
295 drv->driver.name = drv->name;
296 drv->driver.bus = &bus->bus;
297 drv->driver.owner = owner;
298 drv->driver.mod_name = mod_name;
299
300 return driver_register(&drv->driver);
301}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800302EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700303
304void xenbus_unregister_driver(struct xenbus_driver *drv)
305{
306 driver_unregister(&drv->driver);
307}
308EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
309
310struct xb_find_info
311{
312 struct xenbus_device *dev;
313 const char *nodename;
314};
315
316static int cmp_dev(struct device *dev, void *data)
317{
318 struct xenbus_device *xendev = to_xenbus_device(dev);
319 struct xb_find_info *info = data;
320
321 if (!strcmp(xendev->nodename, info->nodename)) {
322 info->dev = xendev;
323 get_device(dev);
324 return 1;
325 }
326 return 0;
327}
328
329struct xenbus_device *xenbus_device_find(const char *nodename,
330 struct bus_type *bus)
331{
332 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
333
334 bus_for_each_dev(bus, NULL, &info, cmp_dev);
335 return info.dev;
336}
337
338static int cleanup_dev(struct device *dev, void *data)
339{
340 struct xenbus_device *xendev = to_xenbus_device(dev);
341 struct xb_find_info *info = data;
342 int len = strlen(info->nodename);
343
344 DPRINTK("%s", info->nodename);
345
346 /* Match the info->nodename path, or any subdirectory of that path. */
347 if (strncmp(xendev->nodename, info->nodename, len))
348 return 0;
349
350 /* If the node name is longer, ensure it really is a subdirectory. */
351 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
352 return 0;
353
354 info->dev = xendev;
355 get_device(dev);
356 return 1;
357}
358
359static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
360{
361 struct xb_find_info info = { .nodename = path };
362
363 do {
364 info.dev = NULL;
365 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
366 if (info.dev) {
367 device_unregister(&info.dev->dev);
368 put_device(&info.dev->dev);
369 }
370 } while (info.dev);
371}
372
373static void xenbus_dev_release(struct device *dev)
374{
375 if (dev)
376 kfree(to_xenbus_device(dev));
377}
378
379static ssize_t xendev_show_nodename(struct device *dev,
380 struct device_attribute *attr, char *buf)
381{
382 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
383}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800384static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700385
386static ssize_t xendev_show_devtype(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
390}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800391static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700392
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700393static ssize_t xendev_show_modalias(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
396 return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
397}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800398static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700399
400int xenbus_probe_node(struct xen_bus_type *bus,
401 const char *type,
402 const char *nodename)
403{
Kay Sievers2a678cc2009-01-06 10:44:34 -0800404 char devname[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700405 int err;
406 struct xenbus_device *xendev;
407 size_t stringlen;
408 char *tmpstring;
409
410 enum xenbus_state state = xenbus_read_driver_state(nodename);
411
412 if (state != XenbusStateInitialising) {
413 /* Device is not new, so ignore it. This can happen if a
414 device is going away after switching to Closed. */
415 return 0;
416 }
417
418 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
419 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
420 if (!xendev)
421 return -ENOMEM;
422
423 xendev->state = XenbusStateInitialising;
424
425 /* Copy the strings into the extra space. */
426
427 tmpstring = (char *)(xendev + 1);
428 strcpy(tmpstring, nodename);
429 xendev->nodename = tmpstring;
430
431 tmpstring += strlen(tmpstring) + 1;
432 strcpy(tmpstring, type);
433 xendev->devicetype = tmpstring;
434 init_completion(&xendev->down);
435
436 xendev->dev.bus = &bus->bus;
437 xendev->dev.release = xenbus_dev_release;
438
Kay Sievers2a678cc2009-01-06 10:44:34 -0800439 err = bus->get_bus_id(devname, xendev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700440 if (err)
441 goto fail;
442
Kay Sievers2a678cc2009-01-06 10:44:34 -0800443 dev_set_name(&xendev->dev, devname);
444
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700445 /* Register with generic device framework. */
446 err = device_register(&xendev->dev);
447 if (err)
448 goto fail;
449
450 err = device_create_file(&xendev->dev, &dev_attr_nodename);
451 if (err)
452 goto fail_unregister;
453
454 err = device_create_file(&xendev->dev, &dev_attr_devtype);
455 if (err)
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700456 goto fail_remove_nodename;
457
458 err = device_create_file(&xendev->dev, &dev_attr_modalias);
459 if (err)
460 goto fail_remove_devtype;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700461
462 return 0;
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700463fail_remove_devtype:
464 device_remove_file(&xendev->dev, &dev_attr_devtype);
465fail_remove_nodename:
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700466 device_remove_file(&xendev->dev, &dev_attr_nodename);
467fail_unregister:
468 device_unregister(&xendev->dev);
469fail:
470 kfree(xendev);
471 return err;
472}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800473EXPORT_SYMBOL_GPL(xenbus_probe_node);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700474
475static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
476{
477 int err = 0;
478 char **dir;
479 unsigned int dir_n = 0;
480 int i;
481
Ian Campbell2de06cc2009-02-09 12:05:51 -0800482 printk(KERN_CRIT "%s type %s\n", __func__, type);
483
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700484 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800485 if (IS_ERR(dir)) {
486 printk(KERN_CRIT "%s failed xenbus_directory\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700487 return PTR_ERR(dir);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800488 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700489
490 for (i = 0; i < dir_n; i++) {
Ian Campbell2de06cc2009-02-09 12:05:51 -0800491 printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]);
492 err = bus->probe(bus, type, dir[i]);
493 if (err) {
494 printk(KERN_CRIT "%s failed\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700495 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800496 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700497 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800498 printk("%s done\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700499 kfree(dir);
500 return err;
501}
502
503int xenbus_probe_devices(struct xen_bus_type *bus)
504{
505 int err = 0;
506 char **dir;
507 unsigned int i, dir_n;
508
Ian Campbell2de06cc2009-02-09 12:05:51 -0800509 printk(KERN_CRIT "%s %s\n", __func__, bus->root);
510
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700511 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800512 if (IS_ERR(dir)) {
513 printk(KERN_CRIT "%s failed xenbus_directory\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700514 return PTR_ERR(dir);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800515 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700516
517 for (i = 0; i < dir_n; i++) {
Ian Campbell2de06cc2009-02-09 12:05:51 -0800518 printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700519 err = xenbus_probe_device_type(bus, dir[i]);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800520 if (err) {
521 printk(KERN_CRIT "%s failed\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700522 break;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800523 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700524 }
Ian Campbell2de06cc2009-02-09 12:05:51 -0800525 printk("%s done\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700526 kfree(dir);
527 return err;
528}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800529EXPORT_SYMBOL_GPL(xenbus_probe_devices);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700530
531static unsigned int char_count(const char *str, char c)
532{
533 unsigned int i, ret = 0;
534
535 for (i = 0; str[i]; i++)
536 if (str[i] == c)
537 ret++;
538 return ret;
539}
540
541static int strsep_len(const char *str, char c, unsigned int len)
542{
543 unsigned int i;
544
545 for (i = 0; str[i]; i++)
546 if (str[i] == c) {
547 if (len == 0)
548 return i;
549 len--;
550 }
551 return (len == 0) ? i : -ERANGE;
552}
553
554void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
555{
556 int exists, rootlen;
557 struct xenbus_device *dev;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800558 char type[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700559 const char *p, *root;
560
561 if (char_count(node, '/') < 2)
562 return;
563
564 exists = xenbus_exists(XBT_NIL, node, "");
565 if (!exists) {
566 xenbus_cleanup_devices(node, &bus->bus);
567 return;
568 }
569
570 /* backend/<type>/... or device/<type>/... */
571 p = strchr(node, '/') + 1;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800572 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
573 type[XEN_BUS_ID_SIZE-1] = '\0';
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700574
575 rootlen = strsep_len(node, '/', bus->levels);
576 if (rootlen < 0)
577 return;
578 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
579 if (!root)
580 return;
581
582 dev = xenbus_device_find(root, &bus->bus);
583 if (!dev)
584 xenbus_probe_node(bus, type, root);
585 else
586 put_device(&dev->dev);
587
588 kfree(root);
589}
Jeremy Fitzhardingec6a960c2009-02-09 12:05:53 -0800590EXPORT_SYMBOL_GPL(xenbus_dev_changed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700591
Ian Campbell2de06cc2009-02-09 12:05:51 -0800592int xenbus_dev_suspend(struct device *dev, pm_message_t state)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700593{
594 int err = 0;
595 struct xenbus_driver *drv;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800596 struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700597
Ian Campbell2de06cc2009-02-09 12:05:51 -0800598 DPRINTK("%s", xdev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700599
600 if (dev->driver == NULL)
601 return 0;
602 drv = to_xenbus_driver(dev->driver);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700603 if (drv->suspend)
Ian Campbellde5b31b2009-02-09 12:05:50 -0800604 err = drv->suspend(xdev, state);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700605 if (err)
606 printk(KERN_WARNING
Kay Sievers2a678cc2009-01-06 10:44:34 -0800607 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700608 return 0;
609}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800610EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700611
Ian Campbell2de06cc2009-02-09 12:05:51 -0800612int xenbus_dev_resume(struct device *dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700613{
614 int err;
615 struct xenbus_driver *drv;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800616 struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700617
Ian Campbell2de06cc2009-02-09 12:05:51 -0800618 DPRINTK("%s", xdev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700619
620 if (dev->driver == NULL)
621 return 0;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700622 drv = to_xenbus_driver(dev->driver);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700623 err = talk_to_otherend(xdev);
624 if (err) {
625 printk(KERN_WARNING
626 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
Kay Sievers2a678cc2009-01-06 10:44:34 -0800627 dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700628 return err;
629 }
630
631 xdev->state = XenbusStateInitialising;
632
633 if (drv->resume) {
634 err = drv->resume(xdev);
635 if (err) {
636 printk(KERN_WARNING
637 "xenbus: resume %s failed: %i\n",
Kay Sievers2a678cc2009-01-06 10:44:34 -0800638 dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700639 return err;
640 }
641 }
642
643 err = watch_otherend(xdev);
644 if (err) {
645 printk(KERN_WARNING
646 "xenbus_probe: resume (watch_otherend) %s failed: "
Kay Sievers2a678cc2009-01-06 10:44:34 -0800647 "%d.\n", dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700648 return err;
649 }
650
651 return 0;
652}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800653EXPORT_SYMBOL_GPL(xenbus_dev_resume);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700654
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700655/* A flag to determine if xenstored is 'ready' (i.e. has started) */
656int xenstored_ready = 0;
657
658
659int register_xenstore_notifier(struct notifier_block *nb)
660{
661 int ret = 0;
662
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100663 if (xenstored_ready > 0)
664 ret = nb->notifier_call(nb, 0, NULL);
665 else
666 blocking_notifier_chain_register(&xenstore_chain, nb);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700667
668 return ret;
669}
670EXPORT_SYMBOL_GPL(register_xenstore_notifier);
671
672void unregister_xenstore_notifier(struct notifier_block *nb)
673{
674 blocking_notifier_chain_unregister(&xenstore_chain, nb);
675}
676EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
677
678void xenbus_probe(struct work_struct *unused)
679{
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100680 xenstored_ready = 1;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700681
Ian Campbelldf660252009-02-09 12:05:51 -0800682 printk(KERN_CRIT "xenbus_probe wake_waiting\n");
683
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700684 /* Notify others that xenstore is up */
685 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
686}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100687EXPORT_SYMBOL_GPL(xenbus_probe);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700688
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100689static int __init xenbus_probe_initcall(void)
690{
691 if (!xen_domain())
692 return -ENODEV;
693
694 if (xen_initial_domain() || xen_hvm_domain())
695 return 0;
696
697 xenbus_probe(NULL);
698 return 0;
699}
700
701device_initcall(xenbus_probe_initcall);
702
703static int __init xenbus_init(void)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700704{
705 int err = 0;
Juan Quintelab37a56d2010-09-02 14:53:56 +0100706 unsigned long page = 0;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700707
708 DPRINTK("");
709
710 err = -ENODEV;
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700711 if (!xen_domain())
Jeremy Fitzhardinge7432e4b2009-10-29 14:19:42 -0700712 return err;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700713
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700714 /*
715 * Domain0 doesn't have a store_evtchn or store_mfn yet.
716 */
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700717 if (xen_initial_domain()) {
Juan Quintelab37a56d2010-09-02 14:53:56 +0100718 struct evtchn_alloc_unbound alloc_unbound;
719
720 /* Allocate Xenstore page */
721 page = get_zeroed_page(GFP_KERNEL);
722 if (!page)
723 goto out_error;
724
725 xen_store_mfn = xen_start_info->store_mfn =
726 pfn_to_mfn(virt_to_phys((void *)page) >>
727 PAGE_SHIFT);
728
729 /* Next allocate a local port which xenstored can bind to */
730 alloc_unbound.dom = DOMID_SELF;
731 alloc_unbound.remote_dom = 0;
732
733 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
734 &alloc_unbound);
735 if (err == -ENOSYS)
736 goto out_error;
737
738 BUG_ON(err);
739 xen_store_evtchn = xen_start_info->store_evtchn =
740 alloc_unbound.port;
741
742 xen_store_interface = mfn_to_virt(xen_store_mfn);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700743 } else {
Sheng Yangbee6ab52010-05-14 12:39:33 +0100744 if (xen_hvm_domain()) {
745 uint64_t v = 0;
746 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
747 if (err)
748 goto out_error;
749 xen_store_evtchn = (int)v;
750 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
751 if (err)
752 goto out_error;
753 xen_store_mfn = (unsigned long)v;
754 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
755 } else {
756 xen_store_evtchn = xen_start_info->store_evtchn;
757 xen_store_mfn = xen_start_info->store_mfn;
758 xen_store_interface = mfn_to_virt(xen_store_mfn);
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100759 xenstored_ready = 1;
Sheng Yangbee6ab52010-05-14 12:39:33 +0100760 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700761 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700762
763 /* Initialize the interface to xenstore. */
764 err = xs_init();
765 if (err) {
766 printk(KERN_WARNING
767 "XENBUS: Error initializing xenstore comms: %i\n", err);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800768 goto out_error;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700769 }
770
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800771#ifdef CONFIG_XEN_COMPAT_XENFS
772 /*
773 * Create xenfs mountpoint in /proc for compatibility with
774 * utilities that expect to find "xenbus" under "/proc/xen".
775 */
776 proc_mkdir("xen", NULL);
777#endif
778
Ian Campbell2de06cc2009-02-09 12:05:51 -0800779 printk(KERN_CRIT "%s ok\n", __func__);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700780 return 0;
781
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700782 out_error:
Juan Quintelab37a56d2010-09-02 14:53:56 +0100783 if (page != 0)
784 free_page(page);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800785
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700786 return err;
787}
788
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100789postcore_initcall(xenbus_init);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700790
791MODULE_LICENSE("GPL");