blob: fd686b962727a8c54dcd45d4dc21408d2df781cd [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35#define DPRINTK(fmt, args...) \
36 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
37 __func__, __LINE__, ##args)
38
39#include <linux/kernel.h>
40#include <linux/err.h>
41#include <linux/string.h>
42#include <linux/ctype.h>
43#include <linux/fcntl.h>
44#include <linux/mm.h>
45#include <linux/proc_fs.h>
46#include <linux/notifier.h>
47#include <linux/kthread.h>
48#include <linux/mutex.h>
49#include <linux/io.h>
50#include <linux/slab.h>
51#include <linux/module.h>
52
53#include <asm/page.h>
54#include <asm/pgtable.h>
55#include <asm/xen/hypervisor.h>
56
57#include <xen/xen.h>
58#include <xen/xenbus.h>
59#include <xen/events.h>
60#include <xen/xen-ops.h>
61#include <xen/page.h>
62
63#include <xen/hvm.h>
64
65#include "xenbus.h"
66
67
68int xen_store_evtchn;
69EXPORT_SYMBOL_GPL(xen_store_evtchn);
70
71struct xenstore_domain_interface *xen_store_interface;
72EXPORT_SYMBOL_GPL(xen_store_interface);
73
74enum xenstore_init xen_store_domain_type;
75EXPORT_SYMBOL_GPL(xen_store_domain_type);
76
77static unsigned long xen_store_gfn;
78
79static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
80
81/* If something in array of ids matches this device, return it. */
82static const struct xenbus_device_id *
83match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
84{
85 for (; *arr->devicetype != '\0'; arr++) {
86 if (!strcmp(arr->devicetype, dev->devicetype))
87 return arr;
88 }
89 return NULL;
90}
91
92int xenbus_match(struct device *_dev, struct device_driver *_drv)
93{
94 struct xenbus_driver *drv = to_xenbus_driver(_drv);
95
96 if (!drv->ids)
97 return 0;
98
99 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
100}
101EXPORT_SYMBOL_GPL(xenbus_match);
102
103
104static void free_otherend_details(struct xenbus_device *dev)
105{
106 kfree(dev->otherend);
107 dev->otherend = NULL;
108}
109
110
111static void free_otherend_watch(struct xenbus_device *dev)
112{
113 if (dev->otherend_watch.node) {
114 unregister_xenbus_watch(&dev->otherend_watch);
115 kfree(dev->otherend_watch.node);
116 dev->otherend_watch.node = NULL;
117 }
118}
119
120
121static int talk_to_otherend(struct xenbus_device *dev)
122{
123 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
124
125 free_otherend_watch(dev);
126 free_otherend_details(dev);
127
128 return drv->read_otherend_details(dev);
129}
130
131
132
133static int watch_otherend(struct xenbus_device *dev)
134{
135 struct xen_bus_type *bus =
136 container_of(dev->dev.bus, struct xen_bus_type, bus);
137
138 return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
139 bus->otherend_will_handle,
140 bus->otherend_changed,
141 "%s/%s", dev->otherend, "state");
142}
143
144
145int xenbus_read_otherend_details(struct xenbus_device *xendev,
146 char *id_node, char *path_node)
147{
148 int err = xenbus_gather(XBT_NIL, xendev->nodename,
149 id_node, "%i", &xendev->otherend_id,
150 path_node, NULL, &xendev->otherend,
151 NULL);
152 if (err) {
153 xenbus_dev_fatal(xendev, err,
154 "reading other end details from %s",
155 xendev->nodename);
156 return err;
157 }
158 if (strlen(xendev->otherend) == 0 ||
159 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
160 xenbus_dev_fatal(xendev, -ENOENT,
161 "unable to read other end from %s. "
162 "missing or inaccessible.",
163 xendev->nodename);
164 free_otherend_details(xendev);
165 return -ENOENT;
166 }
167
168 return 0;
169}
170EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
171
172void xenbus_otherend_changed(struct xenbus_watch *watch,
173 const char *path, const char *token,
174 int ignore_on_shutdown)
175{
176 struct xenbus_device *dev =
177 container_of(watch, struct xenbus_device, otherend_watch);
178 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
179 enum xenbus_state state;
180
181 /* Protect us against watches firing on old details when the otherend
182 details change, say immediately after a resume. */
183 if (!dev->otherend ||
184 strncmp(dev->otherend, path, strlen(dev->otherend))) {
185 dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
186 return;
187 }
188
189 state = xenbus_read_driver_state(dev->otherend);
190
191 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
192 state, xenbus_strstate(state), dev->otherend_watch.node, path);
193
194 /*
195 * Ignore xenbus transitions during shutdown. This prevents us doing
196 * work that can fail e.g., when the rootfs is gone.
197 */
198 if (system_state > SYSTEM_RUNNING) {
199 if (ignore_on_shutdown && (state == XenbusStateClosing))
200 xenbus_frontend_closed(dev);
201 return;
202 }
203
204 if (drv->otherend_changed)
205 drv->otherend_changed(dev, state);
206}
207EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
208
209int xenbus_dev_probe(struct device *_dev)
210{
211 struct xenbus_device *dev = to_xenbus_device(_dev);
212 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
213 const struct xenbus_device_id *id;
214 int err;
215
216 DPRINTK("%s", dev->nodename);
217
218 if (!drv->probe) {
219 err = -ENODEV;
220 goto fail;
221 }
222
223 id = match_device(drv->ids, dev);
224 if (!id) {
225 err = -ENODEV;
226 goto fail;
227 }
228
229 err = talk_to_otherend(dev);
230 if (err) {
231 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
232 dev->nodename);
233 return err;
234 }
235
236 if (!try_module_get(drv->driver.owner)) {
237 dev_warn(&dev->dev, "failed to acquire module reference on '%s'\n",
238 drv->driver.name);
239 err = -ESRCH;
240 goto fail;
241 }
242
243 down(&dev->reclaim_sem);
244 err = drv->probe(dev, id);
245 up(&dev->reclaim_sem);
246 if (err)
247 goto fail_put;
248
249 err = watch_otherend(dev);
250 if (err) {
251 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
252 dev->nodename);
253 goto fail_remove;
254 }
255
256 return 0;
257fail_remove:
258 if (drv->remove) {
259 down(&dev->reclaim_sem);
260 drv->remove(dev);
261 up(&dev->reclaim_sem);
262 }
263fail_put:
264 module_put(drv->driver.owner);
265fail:
266 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
267 xenbus_switch_state(dev, XenbusStateClosed);
268 return err;
269}
270EXPORT_SYMBOL_GPL(xenbus_dev_probe);
271
272int xenbus_dev_remove(struct device *_dev)
273{
274 struct xenbus_device *dev = to_xenbus_device(_dev);
275 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
276
277 DPRINTK("%s", dev->nodename);
278
279 free_otherend_watch(dev);
280
281 if (drv->remove) {
282 down(&dev->reclaim_sem);
283 drv->remove(dev);
284 up(&dev->reclaim_sem);
285 }
286
287 module_put(drv->driver.owner);
288
289 free_otherend_details(dev);
290
291 xenbus_switch_state(dev, XenbusStateClosed);
292 return 0;
293}
294EXPORT_SYMBOL_GPL(xenbus_dev_remove);
295
296void xenbus_dev_shutdown(struct device *_dev)
297{
298 struct xenbus_device *dev = to_xenbus_device(_dev);
299 unsigned long timeout = 5*HZ;
300
301 DPRINTK("%s", dev->nodename);
302
303 get_device(&dev->dev);
304 if (dev->state != XenbusStateConnected) {
305 pr_info("%s: %s: %s != Connected, skipping\n",
306 __func__, dev->nodename, xenbus_strstate(dev->state));
307 goto out;
308 }
309 xenbus_switch_state(dev, XenbusStateClosing);
310 timeout = wait_for_completion_timeout(&dev->down, timeout);
311 if (!timeout)
312 pr_info("%s: %s timeout closing device\n",
313 __func__, dev->nodename);
314 out:
315 put_device(&dev->dev);
316}
317EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
318
319int xenbus_register_driver_common(struct xenbus_driver *drv,
320 struct xen_bus_type *bus,
321 struct module *owner, const char *mod_name)
322{
323 drv->driver.name = drv->name ? drv->name : drv->ids[0].devicetype;
324 drv->driver.bus = &bus->bus;
325 drv->driver.owner = owner;
326 drv->driver.mod_name = mod_name;
327
328 return driver_register(&drv->driver);
329}
330EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
331
332void xenbus_unregister_driver(struct xenbus_driver *drv)
333{
334 driver_unregister(&drv->driver);
335}
336EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
337
338struct xb_find_info {
339 struct xenbus_device *dev;
340 const char *nodename;
341};
342
343static int cmp_dev(struct device *dev, void *data)
344{
345 struct xenbus_device *xendev = to_xenbus_device(dev);
346 struct xb_find_info *info = data;
347
348 if (!strcmp(xendev->nodename, info->nodename)) {
349 info->dev = xendev;
350 get_device(dev);
351 return 1;
352 }
353 return 0;
354}
355
356static struct xenbus_device *xenbus_device_find(const char *nodename,
357 struct bus_type *bus)
358{
359 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
360
361 bus_for_each_dev(bus, NULL, &info, cmp_dev);
362 return info.dev;
363}
364
365static int cleanup_dev(struct device *dev, void *data)
366{
367 struct xenbus_device *xendev = to_xenbus_device(dev);
368 struct xb_find_info *info = data;
369 int len = strlen(info->nodename);
370
371 DPRINTK("%s", info->nodename);
372
373 /* Match the info->nodename path, or any subdirectory of that path. */
374 if (strncmp(xendev->nodename, info->nodename, len))
375 return 0;
376
377 /* If the node name is longer, ensure it really is a subdirectory. */
378 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
379 return 0;
380
381 info->dev = xendev;
382 get_device(dev);
383 return 1;
384}
385
386static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
387{
388 struct xb_find_info info = { .nodename = path };
389
390 do {
391 info.dev = NULL;
392 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
393 if (info.dev) {
394 device_unregister(&info.dev->dev);
395 put_device(&info.dev->dev);
396 }
397 } while (info.dev);
398}
399
400static void xenbus_dev_release(struct device *dev)
401{
402 if (dev)
403 kfree(to_xenbus_device(dev));
404}
405
406static ssize_t nodename_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
410}
411static DEVICE_ATTR_RO(nodename);
412
413static ssize_t devtype_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
417}
418static DEVICE_ATTR_RO(devtype);
419
420static ssize_t modalias_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 return sprintf(buf, "%s:%s\n", dev->bus->name,
424 to_xenbus_device(dev)->devicetype);
425}
426static DEVICE_ATTR_RO(modalias);
427
428static ssize_t state_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
431 return sprintf(buf, "%s\n",
432 xenbus_strstate(to_xenbus_device(dev)->state));
433}
434static DEVICE_ATTR_RO(state);
435
436static struct attribute *xenbus_dev_attrs[] = {
437 &dev_attr_nodename.attr,
438 &dev_attr_devtype.attr,
439 &dev_attr_modalias.attr,
440 &dev_attr_state.attr,
441 NULL,
442};
443
444static const struct attribute_group xenbus_dev_group = {
445 .attrs = xenbus_dev_attrs,
446};
447
448const struct attribute_group *xenbus_dev_groups[] = {
449 &xenbus_dev_group,
450 NULL,
451};
452EXPORT_SYMBOL_GPL(xenbus_dev_groups);
453
454int xenbus_probe_node(struct xen_bus_type *bus,
455 const char *type,
456 const char *nodename)
457{
458 char devname[XEN_BUS_ID_SIZE];
459 int err;
460 struct xenbus_device *xendev;
461 size_t stringlen;
462 char *tmpstring;
463
464 enum xenbus_state state = xenbus_read_driver_state(nodename);
465
466 if (state != XenbusStateInitialising) {
467 /* Device is not new, so ignore it. This can happen if a
468 device is going away after switching to Closed. */
469 return 0;
470 }
471
472 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
473 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
474 if (!xendev)
475 return -ENOMEM;
476
477 xendev->state = XenbusStateInitialising;
478
479 /* Copy the strings into the extra space. */
480
481 tmpstring = (char *)(xendev + 1);
482 strcpy(tmpstring, nodename);
483 xendev->nodename = tmpstring;
484
485 tmpstring += strlen(tmpstring) + 1;
486 strcpy(tmpstring, type);
487 xendev->devicetype = tmpstring;
488 init_completion(&xendev->down);
489
490 xendev->dev.bus = &bus->bus;
491 xendev->dev.release = xenbus_dev_release;
492
493 err = bus->get_bus_id(devname, xendev->nodename);
494 if (err)
495 goto fail;
496
497 dev_set_name(&xendev->dev, "%s", devname);
498 sema_init(&xendev->reclaim_sem, 1);
499
500 /* Register with generic device framework. */
501 err = device_register(&xendev->dev);
502 if (err) {
503 put_device(&xendev->dev);
504 xendev = NULL;
505 goto fail;
506 }
507
508 return 0;
509fail:
510 kfree(xendev);
511 return err;
512}
513EXPORT_SYMBOL_GPL(xenbus_probe_node);
514
515static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
516{
517 int err = 0;
518 char **dir;
519 unsigned int dir_n = 0;
520 int i;
521
522 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
523 if (IS_ERR(dir))
524 return PTR_ERR(dir);
525
526 for (i = 0; i < dir_n; i++) {
527 err = bus->probe(bus, type, dir[i]);
528 if (err)
529 break;
530 }
531
532 kfree(dir);
533 return err;
534}
535
536int xenbus_probe_devices(struct xen_bus_type *bus)
537{
538 int err = 0;
539 char **dir;
540 unsigned int i, dir_n;
541
542 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
543 if (IS_ERR(dir))
544 return PTR_ERR(dir);
545
546 for (i = 0; i < dir_n; i++) {
547 err = xenbus_probe_device_type(bus, dir[i]);
548 if (err)
549 break;
550 }
551
552 kfree(dir);
553 return err;
554}
555EXPORT_SYMBOL_GPL(xenbus_probe_devices);
556
557static unsigned int char_count(const char *str, char c)
558{
559 unsigned int i, ret = 0;
560
561 for (i = 0; str[i]; i++)
562 if (str[i] == c)
563 ret++;
564 return ret;
565}
566
567static int strsep_len(const char *str, char c, unsigned int len)
568{
569 unsigned int i;
570
571 for (i = 0; str[i]; i++)
572 if (str[i] == c) {
573 if (len == 0)
574 return i;
575 len--;
576 }
577 return (len == 0) ? i : -ERANGE;
578}
579
580void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
581{
582 int exists, rootlen;
583 struct xenbus_device *dev;
584 char type[XEN_BUS_ID_SIZE];
585 const char *p, *root;
586
587 if (char_count(node, '/') < 2)
588 return;
589
590 exists = xenbus_exists(XBT_NIL, node, "");
591 if (!exists) {
592 xenbus_cleanup_devices(node, &bus->bus);
593 return;
594 }
595
596 /* backend/<type>/... or device/<type>/... */
597 p = strchr(node, '/') + 1;
598 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
599 type[XEN_BUS_ID_SIZE-1] = '\0';
600
601 rootlen = strsep_len(node, '/', bus->levels);
602 if (rootlen < 0)
603 return;
604 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
605 if (!root)
606 return;
607
608 dev = xenbus_device_find(root, &bus->bus);
609 if (!dev)
610 xenbus_probe_node(bus, type, root);
611 else
612 put_device(&dev->dev);
613
614 kfree(root);
615}
616EXPORT_SYMBOL_GPL(xenbus_dev_changed);
617
618int xenbus_dev_suspend(struct device *dev)
619{
620 int err = 0;
621 struct xenbus_driver *drv;
622 struct xenbus_device *xdev
623 = container_of(dev, struct xenbus_device, dev);
624
625 DPRINTK("%s", xdev->nodename);
626
627 if (dev->driver == NULL)
628 return 0;
629 drv = to_xenbus_driver(dev->driver);
630 if (drv->suspend)
631 err = drv->suspend(xdev);
632 if (err)
633 pr_warn("suspend %s failed: %i\n", dev_name(dev), err);
634 return 0;
635}
636EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
637
638int xenbus_dev_resume(struct device *dev)
639{
640 int err;
641 struct xenbus_driver *drv;
642 struct xenbus_device *xdev
643 = container_of(dev, struct xenbus_device, dev);
644
645 DPRINTK("%s", xdev->nodename);
646
647 if (dev->driver == NULL)
648 return 0;
649 drv = to_xenbus_driver(dev->driver);
650 err = talk_to_otherend(xdev);
651 if (err) {
652 pr_warn("resume (talk_to_otherend) %s failed: %i\n",
653 dev_name(dev), err);
654 return err;
655 }
656
657 xdev->state = XenbusStateInitialising;
658
659 if (drv->resume) {
660 err = drv->resume(xdev);
661 if (err) {
662 pr_warn("resume %s failed: %i\n", dev_name(dev), err);
663 return err;
664 }
665 }
666
667 err = watch_otherend(xdev);
668 if (err) {
669 pr_warn("resume (watch_otherend) %s failed: %d.\n",
670 dev_name(dev), err);
671 return err;
672 }
673
674 return 0;
675}
676EXPORT_SYMBOL_GPL(xenbus_dev_resume);
677
678int xenbus_dev_cancel(struct device *dev)
679{
680 /* Do nothing */
681 DPRINTK("cancel");
682 return 0;
683}
684EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
685
686/* A flag to determine if xenstored is 'ready' (i.e. has started) */
687int xenstored_ready;
688
689
690int register_xenstore_notifier(struct notifier_block *nb)
691{
692 int ret = 0;
693
694 if (xenstored_ready > 0)
695 ret = nb->notifier_call(nb, 0, NULL);
696 else
697 blocking_notifier_chain_register(&xenstore_chain, nb);
698
699 return ret;
700}
701EXPORT_SYMBOL_GPL(register_xenstore_notifier);
702
703void unregister_xenstore_notifier(struct notifier_block *nb)
704{
705 blocking_notifier_chain_unregister(&xenstore_chain, nb);
706}
707EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
708
709static void xenbus_probe(void)
710{
711 xenstored_ready = 1;
712
713 /*
714 * In the HVM case, xenbus_init() deferred its call to
715 * xs_init() in case callbacks were not operational yet.
716 * So do it now.
717 */
718 if (xen_store_domain_type == XS_HVM)
719 xs_init();
720
721 /* Notify others that xenstore is up */
722 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
723}
724
725/*
726 * Returns true when XenStore init must be deferred in order to
727 * allow the PCI platform device to be initialised, before we
728 * can actually have event channel interrupts working.
729 */
730static bool xs_hvm_defer_init_for_callback(void)
731{
732#ifdef CONFIG_XEN_PVHVM
733 return xen_store_domain_type == XS_HVM &&
734 !xen_have_vector_callback;
735#else
736 return false;
737#endif
738}
739
740static int xenbus_probe_thread(void *unused)
741{
742 DEFINE_WAIT(w);
743
744 /*
745 * We actually just want to wait for *any* trigger of xb_waitq,
746 * and run xenbus_probe() the moment it occurs.
747 */
748 prepare_to_wait(&xb_waitq, &w, TASK_INTERRUPTIBLE);
749 schedule();
750 finish_wait(&xb_waitq, &w);
751
752 DPRINTK("probing");
753 xenbus_probe();
754 return 0;
755}
756
757static int __init xenbus_probe_initcall(void)
758{
759 /*
760 * Probe XenBus here in the XS_PV case, and also XS_HVM unless we
761 * need to wait for the platform PCI device to come up.
762 */
763 if (xen_store_domain_type == XS_PV ||
764 (xen_store_domain_type == XS_HVM &&
765 !xs_hvm_defer_init_for_callback()))
766 xenbus_probe();
767
768 /*
769 * For XS_LOCAL, spawn a thread which will wait for xenstored
770 * or a xenstore-stubdom to be started, then probe. It will be
771 * triggered when communication starts happening, by waiting
772 * on xb_waitq.
773 */
774 if (xen_store_domain_type == XS_LOCAL) {
775 struct task_struct *probe_task;
776
777 probe_task = kthread_run(xenbus_probe_thread, NULL,
778 "xenbus_probe");
779 if (IS_ERR(probe_task))
780 return PTR_ERR(probe_task);
781 }
782 return 0;
783}
784device_initcall(xenbus_probe_initcall);
785
786int xen_set_callback_via(uint64_t via)
787{
788 struct xen_hvm_param a;
789 int ret;
790
791 a.domid = DOMID_SELF;
792 a.index = HVM_PARAM_CALLBACK_IRQ;
793 a.value = via;
794
795 ret = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
796 if (ret)
797 return ret;
798
799 /*
800 * If xenbus_probe_initcall() deferred the xenbus_probe()
801 * due to the callback not functioning yet, we can do it now.
802 */
803 if (!xenstored_ready && xs_hvm_defer_init_for_callback())
804 xenbus_probe();
805
806 return ret;
807}
808EXPORT_SYMBOL_GPL(xen_set_callback_via);
809
810/* Set up event channel for xenstored which is run as a local process
811 * (this is normally used only in dom0)
812 */
813static int __init xenstored_local_init(void)
814{
815 int err = -ENOMEM;
816 unsigned long page = 0;
817 struct evtchn_alloc_unbound alloc_unbound;
818
819 /* Allocate Xenstore page */
820 page = get_zeroed_page(GFP_KERNEL);
821 if (!page)
822 goto out_err;
823
824 xen_store_gfn = virt_to_gfn((void *)page);
825
826 /* Next allocate a local port which xenstored can bind to */
827 alloc_unbound.dom = DOMID_SELF;
828 alloc_unbound.remote_dom = DOMID_SELF;
829
830 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
831 &alloc_unbound);
832 if (err == -ENOSYS)
833 goto out_err;
834
835 BUG_ON(err);
836 xen_store_evtchn = alloc_unbound.port;
837
838 return 0;
839
840 out_err:
841 if (page != 0)
842 free_page(page);
843 return err;
844}
845
846static int xenbus_resume_cb(struct notifier_block *nb,
847 unsigned long action, void *data)
848{
849 int err = 0;
850
851 if (xen_hvm_domain()) {
852 uint64_t v = 0;
853
854 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
855 if (!err && v)
856 xen_store_evtchn = v;
857 else
858 pr_warn("Cannot update xenstore event channel: %d\n",
859 err);
860 } else
861 xen_store_evtchn = xen_start_info->store_evtchn;
862
863 return err;
864}
865
866static struct notifier_block xenbus_resume_nb = {
867 .notifier_call = xenbus_resume_cb,
868};
869
870static int __init xenbus_init(void)
871{
872 int err;
873 uint64_t v = 0;
874 xen_store_domain_type = XS_UNKNOWN;
875
876 if (!xen_domain())
877 return -ENODEV;
878
879 xenbus_ring_ops_init();
880
881 if (xen_pv_domain())
882 xen_store_domain_type = XS_PV;
883 if (xen_hvm_domain())
884 xen_store_domain_type = XS_HVM;
885 if (xen_hvm_domain() && xen_initial_domain())
886 xen_store_domain_type = XS_LOCAL;
887 if (xen_pv_domain() && !xen_start_info->store_evtchn)
888 xen_store_domain_type = XS_LOCAL;
889 if (xen_pv_domain() && xen_start_info->store_evtchn)
890 xenstored_ready = 1;
891
892 switch (xen_store_domain_type) {
893 case XS_LOCAL:
894 err = xenstored_local_init();
895 if (err)
896 goto out_error;
897 xen_store_interface = gfn_to_virt(xen_store_gfn);
898 break;
899 case XS_PV:
900 xen_store_evtchn = xen_start_info->store_evtchn;
901 xen_store_gfn = xen_start_info->store_mfn;
902 xen_store_interface = gfn_to_virt(xen_store_gfn);
903 break;
904 case XS_HVM:
905 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
906 if (err)
907 goto out_error;
908 xen_store_evtchn = (int)v;
909 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
910 if (err)
911 goto out_error;
912 /*
913 * Uninitialized hvm_params are zero and return no error.
914 * Although it is theoretically possible to have
915 * HVM_PARAM_STORE_PFN set to zero on purpose, in reality it is
916 * not zero when valid. If zero, it means that Xenstore hasn't
917 * been properly initialized. Instead of attempting to map a
918 * wrong guest physical address return error.
919 *
920 * Also recognize all bits set as an invalid value.
921 */
922 if (!v || !~v) {
923 err = -ENOENT;
924 goto out_error;
925 }
926 /* Avoid truncation on 32-bit. */
927#if BITS_PER_LONG == 32
928 if (v > ULONG_MAX) {
929 pr_err("%s: cannot handle HVM_PARAM_STORE_PFN=%llx > ULONG_MAX\n",
930 __func__, v);
931 err = -EINVAL;
932 goto out_error;
933 }
934#endif
935 xen_store_gfn = (unsigned long)v;
936 xen_store_interface =
937 xen_remap(xen_store_gfn << XEN_PAGE_SHIFT,
938 XEN_PAGE_SIZE);
939 break;
940 default:
941 pr_warn("Xenstore state unknown\n");
942 break;
943 }
944
945 /*
946 * HVM domains may not have a functional callback yet. In that
947 * case let xs_init() be called from xenbus_probe(), which will
948 * get invoked at an appropriate time.
949 */
950 if (xen_store_domain_type != XS_HVM) {
951 err = xs_init();
952 if (err) {
953 pr_warn("Error initializing xenstore comms: %i\n", err);
954 goto out_error;
955 }
956 }
957
958 if ((xen_store_domain_type != XS_LOCAL) &&
959 (xen_store_domain_type != XS_UNKNOWN))
960 xen_resume_notifier_register(&xenbus_resume_nb);
961
962#ifdef CONFIG_XEN_COMPAT_XENFS
963 /*
964 * Create xenfs mountpoint in /proc for compatibility with
965 * utilities that expect to find "xenbus" under "/proc/xen".
966 */
967 proc_create_mount_point("xen");
968#endif
969 return 0;
970
971out_error:
972 xen_store_domain_type = XS_UNKNOWN;
973 return err;
974}
975
976postcore_initcall(xenbus_init);
977
978MODULE_LICENSE("GPL");