blob: 5967aa9372550eb6c5e67b4b7d6adc3bf8b370aa [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
Joe Perches283c0972013-06-28 03:21:41 -070033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Bjorn Helgaas64b3eaf2020-05-27 12:43:26 -050034#define dev_fmt pr_fmt
Joe Perches283c0972013-06-28 03:21:41 -070035
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070036#define DPRINTK(fmt, args...) \
37 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
38 __func__, __LINE__, ##args)
39
40#include <linux/kernel.h>
41#include <linux/err.h>
42#include <linux/string.h>
43#include <linux/ctype.h>
44#include <linux/fcntl.h>
45#include <linux/mm.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080046#include <linux/proc_fs.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070047#include <linux/notifier.h>
48#include <linux/kthread.h>
49#include <linux/mutex.h>
50#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Paul Gortmaker72ee5112011-07-03 16:20:57 -040052#include <linux/module.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070053
54#include <asm/page.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070055#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070056
57#include <xen/xen.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070058#include <xen/xenbus.h>
59#include <xen/events.h>
Boris Ostrovsky16f1cf32015-04-29 17:10:13 -040060#include <xen/xen-ops.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070061#include <xen/page.h>
62
Sheng Yangbee6ab532010-05-14 12:39:33 +010063#include <xen/hvm.h>
64
Juergen Gross332f7912017-02-09 14:39:56 +010065#include "xenbus.h"
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070066
Alex Zeffertt1107ba82009-01-07 18:07:11 -080067
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070068int xen_store_evtchn;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070069EXPORT_SYMBOL_GPL(xen_store_evtchn);
Alex Zeffertt1107ba82009-01-07 18:07:11 -080070
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070071struct xenstore_domain_interface *xen_store_interface;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070072EXPORT_SYMBOL_GPL(xen_store_interface);
73
Aurelien Chartier33c11742013-05-28 18:09:55 +010074enum xenstore_init xen_store_domain_type;
75EXPORT_SYMBOL_GPL(xen_store_domain_type);
76
Julien Grall5f510422015-08-07 17:34:42 +010077static unsigned long xen_store_gfn;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070078
79static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
80
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070081/* 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}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800101EXPORT_SYMBOL_GPL(xenbus_match);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700102
Ian Campbell2de06cc2009-02-09 12:05:51 -0800103
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700104static 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
Ian Campbell2de06cc2009-02-09 12:05:51 -0800121static 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{
Ian Campbell6bac7f92010-12-10 14:39:15 +0000135 struct xen_bus_type *bus =
136 container_of(dev->dev.bus, struct xen_bus_type, bus);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800137
Ian Campbell6bac7f92010-12-10 14:39:15 +0000138 return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
SeongJae Parkbe987202020-12-14 10:05:47 +0100139 bus->otherend_will_handle,
140 bus->otherend_changed,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800141 "%s/%s", dev->otherend, "state");
142}
143
144
145int xenbus_read_otherend_details(struct xenbus_device *xendev,
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700146 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}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800170EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700171
Ian Campbell2de06cc2009-02-09 12:05:51 -0800172void xenbus_otherend_changed(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100173 const char *path, const char *token,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800174 int ignore_on_shutdown)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700175{
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 ||
Juergen Gross5584ea22017-02-09 14:39:57 +0100184 strncmp(dev->otherend, path, strlen(dev->otherend))) {
185 dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700186 return;
187 }
188
189 state = xenbus_read_driver_state(dev->otherend);
190
Joe Perches898eb712007-10-18 03:06:30 -0700191 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
Juergen Gross5584ea22017-02-09 14:39:57 +0100192 state, xenbus_strstate(state), dev->otherend_watch.node, path);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700193
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) {
Ian Campbell2de06cc2009-02-09 12:05:51 -0800199 if (ignore_on_shutdown && (state == XenbusStateClosing))
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700200 xenbus_frontend_closed(dev);
201 return;
202 }
203
204 if (drv->otherend_changed)
205 drv->otherend_changed(dev, state);
206}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800207EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700208
Juergen Gross06f45fe2021-02-19 16:40:28 +0100209#define XENBUS_SHOW_STAT(name) \
YueHaibing20600612021-05-26 22:10:19 +0800210static ssize_t name##_show(struct device *_dev, \
Juergen Gross06f45fe2021-02-19 16:40:28 +0100211 struct device_attribute *attr, \
212 char *buf) \
213{ \
214 struct xenbus_device *dev = to_xenbus_device(_dev); \
215 \
216 return sprintf(buf, "%d\n", atomic_read(&dev->name)); \
217} \
YueHaibing20600612021-05-26 22:10:19 +0800218static DEVICE_ATTR_RO(name)
Juergen Gross06f45fe2021-02-19 16:40:28 +0100219
220XENBUS_SHOW_STAT(event_channels);
221XENBUS_SHOW_STAT(events);
222XENBUS_SHOW_STAT(spurious_events);
223XENBUS_SHOW_STAT(jiffies_eoi_delayed);
224
YueHaibing20600612021-05-26 22:10:19 +0800225static ssize_t spurious_threshold_show(struct device *_dev,
Juergen Gross06f45fe2021-02-19 16:40:28 +0100226 struct device_attribute *attr,
227 char *buf)
228{
229 struct xenbus_device *dev = to_xenbus_device(_dev);
230
231 return sprintf(buf, "%d\n", dev->spurious_threshold);
232}
233
YueHaibing20600612021-05-26 22:10:19 +0800234static ssize_t spurious_threshold_store(struct device *_dev,
235 struct device_attribute *attr,
236 const char *buf, size_t count)
Juergen Gross06f45fe2021-02-19 16:40:28 +0100237{
238 struct xenbus_device *dev = to_xenbus_device(_dev);
239 unsigned int val;
240 ssize_t ret;
241
242 ret = kstrtouint(buf, 0, &val);
243 if (ret)
244 return ret;
245
246 dev->spurious_threshold = val;
247
248 return count;
249}
250
YueHaibing20600612021-05-26 22:10:19 +0800251static DEVICE_ATTR_RW(spurious_threshold);
Juergen Gross06f45fe2021-02-19 16:40:28 +0100252
253static struct attribute *xenbus_attrs[] = {
254 &dev_attr_event_channels.attr,
255 &dev_attr_events.attr,
256 &dev_attr_spurious_events.attr,
257 &dev_attr_jiffies_eoi_delayed.attr,
258 &dev_attr_spurious_threshold.attr,
259 NULL
260};
261
262static const struct attribute_group xenbus_group = {
263 .name = "xenbus",
264 .attrs = xenbus_attrs,
265};
266
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700267int xenbus_dev_probe(struct device *_dev)
268{
269 struct xenbus_device *dev = to_xenbus_device(_dev);
270 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
271 const struct xenbus_device_id *id;
272 int err;
273
274 DPRINTK("%s", dev->nodename);
275
276 if (!drv->probe) {
277 err = -ENODEV;
278 goto fail;
279 }
280
281 id = match_device(drv->ids, dev);
282 if (!id) {
283 err = -ENODEV;
284 goto fail;
285 }
286
287 err = talk_to_otherend(dev);
288 if (err) {
289 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
290 dev->nodename);
291 return err;
292 }
293
Paul Durrant196748a2019-12-02 11:41:16 +0000294 if (!try_module_get(drv->driver.owner)) {
295 dev_warn(&dev->dev, "failed to acquire module reference on '%s'\n",
296 drv->driver.name);
297 err = -ESRCH;
298 goto fail;
299 }
300
Juergen Gross2f69a112020-03-05 11:03:23 +0100301 down(&dev->reclaim_sem);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700302 err = drv->probe(dev, id);
Juergen Gross2f69a112020-03-05 11:03:23 +0100303 up(&dev->reclaim_sem);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700304 if (err)
Paul Durrant196748a2019-12-02 11:41:16 +0000305 goto fail_put;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700306
307 err = watch_otherend(dev);
308 if (err) {
309 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
310 dev->nodename);
311 return err;
312 }
313
Juergen Gross06f45fe2021-02-19 16:40:28 +0100314 dev->spurious_threshold = 1;
315 if (sysfs_create_group(&dev->dev.kobj, &xenbus_group))
316 dev_warn(&dev->dev, "sysfs_create_group on %s failed.\n",
317 dev->nodename);
318
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700319 return 0;
Paul Durrant196748a2019-12-02 11:41:16 +0000320fail_put:
321 module_put(drv->driver.owner);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700322fail:
323 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
Jeremy Fitzhardinge7432e4b2009-10-29 14:19:42 -0700324 return err;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700325}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800326EXPORT_SYMBOL_GPL(xenbus_dev_probe);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700327
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200328void xenbus_dev_remove(struct device *_dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700329{
330 struct xenbus_device *dev = to_xenbus_device(_dev);
331 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
332
333 DPRINTK("%s", dev->nodename);
334
Juergen Gross06f45fe2021-02-19 16:40:28 +0100335 sysfs_remove_group(&dev->dev.kobj, &xenbus_group);
336
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700337 free_otherend_watch(dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700338
SeongJae Park060eabe2020-01-27 09:18:09 +0100339 if (drv->remove) {
Juergen Gross2f69a112020-03-05 11:03:23 +0100340 down(&dev->reclaim_sem);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700341 drv->remove(dev);
Juergen Gross2f69a112020-03-05 11:03:23 +0100342 up(&dev->reclaim_sem);
SeongJae Park060eabe2020-01-27 09:18:09 +0100343 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700344
Paul Durrant196748a2019-12-02 11:41:16 +0000345 module_put(drv->driver.owner);
346
Jan Beulichbd0d5aa2012-03-05 17:11:31 +0000347 free_otherend_details(dev);
348
Paul Durrant672b7762019-12-11 15:29:54 +0000349 /*
350 * If the toolstack has forced the device state to closing then set
351 * the state to closed now to allow it to be cleaned up.
352 * Similarly, if the driver does not support re-bind, set the
353 * closed.
354 */
355 if (!drv->allow_rebind ||
356 xenbus_read_driver_state(dev->nodename) == XenbusStateClosing)
357 xenbus_switch_state(dev, XenbusStateClosed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700358}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800359EXPORT_SYMBOL_GPL(xenbus_dev_remove);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700360
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700361int xenbus_register_driver_common(struct xenbus_driver *drv,
David Vrabel95afae42014-09-08 17:30:41 +0100362 struct xen_bus_type *bus,
363 struct module *owner, const char *mod_name)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700364{
David Vrabel95afae42014-09-08 17:30:41 +0100365 drv->driver.name = drv->name ? drv->name : drv->ids[0].devicetype;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700366 drv->driver.bus = &bus->bus;
David Vrabel95afae42014-09-08 17:30:41 +0100367 drv->driver.owner = owner;
368 drv->driver.mod_name = mod_name;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700369
370 return driver_register(&drv->driver);
371}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800372EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700373
374void xenbus_unregister_driver(struct xenbus_driver *drv)
375{
376 driver_unregister(&drv->driver);
377}
378EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
379
Ruslan Pisarev69132002011-07-26 14:17:23 +0300380struct xb_find_info {
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700381 struct xenbus_device *dev;
382 const char *nodename;
383};
384
385static int cmp_dev(struct device *dev, void *data)
386{
387 struct xenbus_device *xendev = to_xenbus_device(dev);
388 struct xb_find_info *info = data;
389
390 if (!strcmp(xendev->nodename, info->nodename)) {
391 info->dev = xendev;
392 get_device(dev);
393 return 1;
394 }
395 return 0;
396}
397
Konrad Rzeszutek Wilkb8b0f552012-08-21 14:49:34 -0400398static struct xenbus_device *xenbus_device_find(const char *nodename,
399 struct bus_type *bus)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700400{
401 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
402
403 bus_for_each_dev(bus, NULL, &info, cmp_dev);
404 return info.dev;
405}
406
407static int cleanup_dev(struct device *dev, void *data)
408{
409 struct xenbus_device *xendev = to_xenbus_device(dev);
410 struct xb_find_info *info = data;
411 int len = strlen(info->nodename);
412
413 DPRINTK("%s", info->nodename);
414
415 /* Match the info->nodename path, or any subdirectory of that path. */
416 if (strncmp(xendev->nodename, info->nodename, len))
417 return 0;
418
419 /* If the node name is longer, ensure it really is a subdirectory. */
420 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
421 return 0;
422
423 info->dev = xendev;
424 get_device(dev);
425 return 1;
426}
427
428static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
429{
430 struct xb_find_info info = { .nodename = path };
431
432 do {
433 info.dev = NULL;
434 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
435 if (info.dev) {
436 device_unregister(&info.dev->dev);
437 put_device(&info.dev->dev);
438 }
439 } while (info.dev);
440}
441
442static void xenbus_dev_release(struct device *dev)
443{
444 if (dev)
445 kfree(to_xenbus_device(dev));
446}
447
Bastian Blankcc85e932011-06-29 14:39:26 +0200448static ssize_t nodename_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700450{
451 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
452}
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700453static DEVICE_ATTR_RO(nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700454
Bastian Blankcc85e932011-06-29 14:39:26 +0200455static ssize_t devtype_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700457{
458 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
459}
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700460static DEVICE_ATTR_RO(devtype);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700461
Bastian Blankcc85e932011-06-29 14:39:26 +0200462static ssize_t modalias_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700464{
Bastian Blank149bb2f2011-06-29 14:40:08 +0200465 return sprintf(buf, "%s:%s\n", dev->bus->name,
466 to_xenbus_device(dev)->devicetype);
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700467}
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700468static DEVICE_ATTR_RO(modalias);
Bastian Blankcc85e932011-06-29 14:39:26 +0200469
Joe Jin076e2ce2018-08-28 07:56:08 -0700470static ssize_t state_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
473 return sprintf(buf, "%s\n",
474 xenbus_strstate(to_xenbus_device(dev)->state));
475}
476static DEVICE_ATTR_RO(state);
477
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700478static struct attribute *xenbus_dev_attrs[] = {
479 &dev_attr_nodename.attr,
480 &dev_attr_devtype.attr,
481 &dev_attr_modalias.attr,
Joe Jin076e2ce2018-08-28 07:56:08 -0700482 &dev_attr_state.attr,
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700483 NULL,
Bastian Blankcc85e932011-06-29 14:39:26 +0200484};
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700485
486static const struct attribute_group xenbus_dev_group = {
487 .attrs = xenbus_dev_attrs,
488};
489
490const struct attribute_group *xenbus_dev_groups[] = {
491 &xenbus_dev_group,
492 NULL,
493};
494EXPORT_SYMBOL_GPL(xenbus_dev_groups);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700495
496int xenbus_probe_node(struct xen_bus_type *bus,
497 const char *type,
498 const char *nodename)
499{
Kay Sievers2a678cc2009-01-06 10:44:34 -0800500 char devname[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700501 int err;
502 struct xenbus_device *xendev;
503 size_t stringlen;
504 char *tmpstring;
505
506 enum xenbus_state state = xenbus_read_driver_state(nodename);
507
508 if (state != XenbusStateInitialising) {
509 /* Device is not new, so ignore it. This can happen if a
510 device is going away after switching to Closed. */
511 return 0;
512 }
513
514 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
515 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
516 if (!xendev)
517 return -ENOMEM;
518
519 xendev->state = XenbusStateInitialising;
520
521 /* Copy the strings into the extra space. */
522
523 tmpstring = (char *)(xendev + 1);
524 strcpy(tmpstring, nodename);
525 xendev->nodename = tmpstring;
526
527 tmpstring += strlen(tmpstring) + 1;
528 strcpy(tmpstring, type);
529 xendev->devicetype = tmpstring;
530 init_completion(&xendev->down);
531
532 xendev->dev.bus = &bus->bus;
533 xendev->dev.release = xenbus_dev_release;
534
Kay Sievers2a678cc2009-01-06 10:44:34 -0800535 err = bus->get_bus_id(devname, xendev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700536 if (err)
537 goto fail;
538
Kees Cook02aa2a32013-07-03 15:04:56 -0700539 dev_set_name(&xendev->dev, "%s", devname);
Juergen Gross2f69a112020-03-05 11:03:23 +0100540 sema_init(&xendev->reclaim_sem, 1);
Kay Sievers2a678cc2009-01-06 10:44:34 -0800541
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700542 /* Register with generic device framework. */
543 err = device_register(&xendev->dev);
Arvind Yadav351b2bc2018-03-06 15:40:37 +0530544 if (err) {
545 put_device(&xendev->dev);
546 xendev = NULL;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700547 goto fail;
Arvind Yadav351b2bc2018-03-06 15:40:37 +0530548 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700549
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700550 return 0;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700551fail:
552 kfree(xendev);
553 return err;
554}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800555EXPORT_SYMBOL_GPL(xenbus_probe_node);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700556
557static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
558{
559 int err = 0;
560 char **dir;
561 unsigned int dir_n = 0;
562 int i;
563
564 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700565 if (IS_ERR(dir))
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700566 return PTR_ERR(dir);
567
568 for (i = 0; i < dir_n; i++) {
Ian Campbell2de06cc2009-02-09 12:05:51 -0800569 err = bus->probe(bus, type, dir[i]);
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700570 if (err)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700571 break;
572 }
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700573
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700574 kfree(dir);
575 return err;
576}
577
578int xenbus_probe_devices(struct xen_bus_type *bus)
579{
580 int err = 0;
581 char **dir;
582 unsigned int i, dir_n;
583
584 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700585 if (IS_ERR(dir))
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700586 return PTR_ERR(dir);
587
588 for (i = 0; i < dir_n; i++) {
589 err = xenbus_probe_device_type(bus, dir[i]);
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700590 if (err)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700591 break;
592 }
Jeremy Fitzhardingea39bda22010-03-29 14:38:12 -0700593
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700594 kfree(dir);
595 return err;
596}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800597EXPORT_SYMBOL_GPL(xenbus_probe_devices);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700598
599static unsigned int char_count(const char *str, char c)
600{
601 unsigned int i, ret = 0;
602
603 for (i = 0; str[i]; i++)
604 if (str[i] == c)
605 ret++;
606 return ret;
607}
608
609static int strsep_len(const char *str, char c, unsigned int len)
610{
611 unsigned int i;
612
613 for (i = 0; str[i]; i++)
614 if (str[i] == c) {
615 if (len == 0)
616 return i;
617 len--;
618 }
619 return (len == 0) ? i : -ERANGE;
620}
621
622void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
623{
624 int exists, rootlen;
625 struct xenbus_device *dev;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800626 char type[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700627 const char *p, *root;
628
629 if (char_count(node, '/') < 2)
630 return;
631
632 exists = xenbus_exists(XBT_NIL, node, "");
633 if (!exists) {
634 xenbus_cleanup_devices(node, &bus->bus);
635 return;
636 }
637
638 /* backend/<type>/... or device/<type>/... */
639 p = strchr(node, '/') + 1;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800640 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
641 type[XEN_BUS_ID_SIZE-1] = '\0';
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700642
643 rootlen = strsep_len(node, '/', bus->levels);
644 if (rootlen < 0)
645 return;
646 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
647 if (!root)
648 return;
649
650 dev = xenbus_device_find(root, &bus->bus);
651 if (!dev)
652 xenbus_probe_node(bus, type, root);
653 else
654 put_device(&dev->dev);
655
656 kfree(root);
657}
Jeremy Fitzhardingec6a960c2009-02-09 12:05:53 -0800658EXPORT_SYMBOL_GPL(xenbus_dev_changed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700659
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800660int xenbus_dev_suspend(struct device *dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700661{
662 int err = 0;
663 struct xenbus_driver *drv;
Ian Campbell6bac7f92010-12-10 14:39:15 +0000664 struct xenbus_device *xdev
665 = container_of(dev, struct xenbus_device, dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700666
Ian Campbell2de06cc2009-02-09 12:05:51 -0800667 DPRINTK("%s", xdev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700668
669 if (dev->driver == NULL)
670 return 0;
671 drv = to_xenbus_driver(dev->driver);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700672 if (drv->suspend)
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800673 err = drv->suspend(xdev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700674 if (err)
Bjorn Helgaas64b3eaf2020-05-27 12:43:26 -0500675 dev_warn(dev, "suspend failed: %i\n", err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700676 return 0;
677}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800678EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700679
Ian Campbell2de06cc2009-02-09 12:05:51 -0800680int xenbus_dev_resume(struct device *dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700681{
682 int err;
683 struct xenbus_driver *drv;
Ian Campbell6bac7f92010-12-10 14:39:15 +0000684 struct xenbus_device *xdev
685 = container_of(dev, struct xenbus_device, dev);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700686
Ian Campbell2de06cc2009-02-09 12:05:51 -0800687 DPRINTK("%s", xdev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700688
689 if (dev->driver == NULL)
690 return 0;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700691 drv = to_xenbus_driver(dev->driver);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700692 err = talk_to_otherend(xdev);
693 if (err) {
Bjorn Helgaas64b3eaf2020-05-27 12:43:26 -0500694 dev_warn(dev, "resume (talk_to_otherend) failed: %i\n", err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700695 return err;
696 }
697
698 xdev->state = XenbusStateInitialising;
699
700 if (drv->resume) {
701 err = drv->resume(xdev);
702 if (err) {
Bjorn Helgaas64b3eaf2020-05-27 12:43:26 -0500703 dev_warn(dev, "resume failed: %i\n", err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700704 return err;
705 }
706 }
707
708 err = watch_otherend(xdev);
709 if (err) {
Bjorn Helgaas64b3eaf2020-05-27 12:43:26 -0500710 dev_warn(dev, "resume (watch_otherend) failed: %d\n", err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700711 return err;
712 }
713
714 return 0;
715}
Ian Campbell2de06cc2009-02-09 12:05:51 -0800716EXPORT_SYMBOL_GPL(xenbus_dev_resume);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700717
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800718int xenbus_dev_cancel(struct device *dev)
719{
720 /* Do nothing */
721 DPRINTK("cancel");
722 return 0;
723}
724EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
725
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700726/* A flag to determine if xenstored is 'ready' (i.e. has started) */
Ruslan Pisarev69132002011-07-26 14:17:23 +0300727int xenstored_ready;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700728
729
730int register_xenstore_notifier(struct notifier_block *nb)
731{
732 int ret = 0;
733
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100734 if (xenstored_ready > 0)
735 ret = nb->notifier_call(nb, 0, NULL);
736 else
737 blocking_notifier_chain_register(&xenstore_chain, nb);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700738
739 return ret;
740}
741EXPORT_SYMBOL_GPL(register_xenstore_notifier);
742
743void unregister_xenstore_notifier(struct notifier_block *nb)
744{
745 blocking_notifier_chain_unregister(&xenstore_chain, nb);
746}
747EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
748
Julien Grallc4295ab2021-02-10 17:06:54 +0000749static void xenbus_probe(void)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700750{
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100751 xenstored_ready = 1;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700752
David Woodhouse3499ba82021-01-13 13:26:02 +0000753 /*
754 * In the HVM case, xenbus_init() deferred its call to
755 * xs_init() in case callbacks were not operational yet.
756 * So do it now.
757 */
758 if (xen_store_domain_type == XS_HVM)
759 xs_init();
760
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700761 /* Notify others that xenstore is up */
762 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
763}
David Woodhouse3499ba82021-01-13 13:26:02 +0000764
765/*
766 * Returns true when XenStore init must be deferred in order to
767 * allow the PCI platform device to be initialised, before we
768 * can actually have event channel interrupts working.
769 */
770static bool xs_hvm_defer_init_for_callback(void)
771{
772#ifdef CONFIG_XEN_PVHVM
773 return xen_store_domain_type == XS_HVM &&
774 !xen_have_vector_callback;
775#else
776 return false;
777#endif
778}
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700779
David Woodhouse5f464002021-01-26 17:01:49 +0000780static int xenbus_probe_thread(void *unused)
781{
782 DEFINE_WAIT(w);
783
784 /*
785 * We actually just want to wait for *any* trigger of xb_waitq,
786 * and run xenbus_probe() the moment it occurs.
787 */
788 prepare_to_wait(&xb_waitq, &w, TASK_INTERRUPTIBLE);
789 schedule();
790 finish_wait(&xb_waitq, &w);
791
792 DPRINTK("probing");
793 xenbus_probe();
794 return 0;
795}
796
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100797static int __init xenbus_probe_initcall(void)
798{
David Woodhouse3499ba82021-01-13 13:26:02 +0000799 /*
800 * Probe XenBus here in the XS_PV case, and also XS_HVM unless we
801 * need to wait for the platform PCI device to come up.
802 */
803 if (xen_store_domain_type == XS_PV ||
804 (xen_store_domain_type == XS_HVM &&
805 !xs_hvm_defer_init_for_callback()))
806 xenbus_probe();
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100807
David Woodhouse5f464002021-01-26 17:01:49 +0000808 /*
809 * For XS_LOCAL, spawn a thread which will wait for xenstored
810 * or a xenstore-stubdom to be started, then probe. It will be
811 * triggered when communication starts happening, by waiting
812 * on xb_waitq.
813 */
814 if (xen_store_domain_type == XS_LOCAL) {
815 struct task_struct *probe_task;
816
817 probe_task = kthread_run(xenbus_probe_thread, NULL,
818 "xenbus_probe");
819 if (IS_ERR(probe_task))
820 return PTR_ERR(probe_task);
821 }
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100822 return 0;
823}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100824device_initcall(xenbus_probe_initcall);
825
David Woodhouse3499ba82021-01-13 13:26:02 +0000826int xen_set_callback_via(uint64_t via)
827{
828 struct xen_hvm_param a;
829 int ret;
830
831 a.domid = DOMID_SELF;
832 a.index = HVM_PARAM_CALLBACK_IRQ;
833 a.value = via;
834
835 ret = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
836 if (ret)
837 return ret;
838
839 /*
840 * If xenbus_probe_initcall() deferred the xenbus_probe()
841 * due to the callback not functioning yet, we can do it now.
842 */
843 if (!xenstored_ready && xs_hvm_defer_init_for_callback())
844 xenbus_probe();
845
846 return ret;
847}
848EXPORT_SYMBOL_GPL(xen_set_callback_via);
849
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400850/* Set up event channel for xenstored which is run as a local process
851 * (this is normally used only in dom0)
852 */
853static int __init xenstored_local_init(void)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700854{
Pan Bian2466d4b2016-12-05 16:22:22 +0800855 int err = -ENOMEM;
Juan Quintelab37a56d2010-09-02 14:53:56 +0100856 unsigned long page = 0;
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400857 struct evtchn_alloc_unbound alloc_unbound;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700858
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400859 /* Allocate Xenstore page */
860 page = get_zeroed_page(GFP_KERNEL);
861 if (!page)
862 goto out_err;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700863
Roger Pau Monne515e6542018-05-09 11:21:28 +0100864 xen_store_gfn = virt_to_gfn((void *)page);
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400865
866 /* Next allocate a local port which xenstored can bind to */
867 alloc_unbound.dom = DOMID_SELF;
868 alloc_unbound.remote_dom = DOMID_SELF;
869
870 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
871 &alloc_unbound);
872 if (err == -ENOSYS)
873 goto out_err;
874
875 BUG_ON(err);
Roger Pau Monne515e6542018-05-09 11:21:28 +0100876 xen_store_evtchn = alloc_unbound.port;
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400877
878 return 0;
879
880 out_err:
881 if (page != 0)
882 free_page(page);
883 return err;
884}
885
Boris Ostrovsky16f1cf32015-04-29 17:10:13 -0400886static int xenbus_resume_cb(struct notifier_block *nb,
887 unsigned long action, void *data)
888{
889 int err = 0;
890
891 if (xen_hvm_domain()) {
Jan Beulich76ea3cb2015-05-28 09:26:37 +0100892 uint64_t v = 0;
Boris Ostrovsky16f1cf32015-04-29 17:10:13 -0400893
894 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
895 if (!err && v)
896 xen_store_evtchn = v;
897 else
898 pr_warn("Cannot update xenstore event channel: %d\n",
899 err);
900 } else
901 xen_store_evtchn = xen_start_info->store_evtchn;
902
903 return err;
904}
905
906static struct notifier_block xenbus_resume_nb = {
907 .notifier_call = xenbus_resume_cb,
908};
909
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400910static int __init xenbus_init(void)
911{
Stefano Stabellini08f6c2b2021-11-15 14:27:19 -0800912 int err;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100913 uint64_t v = 0;
Aurelien Chartier33c11742013-05-28 18:09:55 +0100914 xen_store_domain_type = XS_UNKNOWN;
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400915
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700916 if (!xen_domain())
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400917 return -ENODEV;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700918
Daniel De Graaf2c5d37d2011-12-19 14:55:14 -0500919 xenbus_ring_ops_init();
920
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100921 if (xen_pv_domain())
Aurelien Chartier33c11742013-05-28 18:09:55 +0100922 xen_store_domain_type = XS_PV;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100923 if (xen_hvm_domain())
Aurelien Chartier33c11742013-05-28 18:09:55 +0100924 xen_store_domain_type = XS_HVM;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100925 if (xen_hvm_domain() && xen_initial_domain())
Aurelien Chartier33c11742013-05-28 18:09:55 +0100926 xen_store_domain_type = XS_LOCAL;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100927 if (xen_pv_domain() && !xen_start_info->store_evtchn)
Aurelien Chartier33c11742013-05-28 18:09:55 +0100928 xen_store_domain_type = XS_LOCAL;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100929 if (xen_pv_domain() && xen_start_info->store_evtchn)
930 xenstored_ready = 1;
931
Aurelien Chartier33c11742013-05-28 18:09:55 +0100932 switch (xen_store_domain_type) {
933 case XS_LOCAL:
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100934 err = xenstored_local_init();
935 if (err)
936 goto out_error;
Julien Grall5f510422015-08-07 17:34:42 +0100937 xen_store_interface = gfn_to_virt(xen_store_gfn);
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100938 break;
Aurelien Chartier33c11742013-05-28 18:09:55 +0100939 case XS_PV:
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100940 xen_store_evtchn = xen_start_info->store_evtchn;
Julien Grall5f510422015-08-07 17:34:42 +0100941 xen_store_gfn = xen_start_info->store_mfn;
942 xen_store_interface = gfn_to_virt(xen_store_gfn);
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100943 break;
Aurelien Chartier33c11742013-05-28 18:09:55 +0100944 case XS_HVM:
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400945 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
946 if (err)
Juan Quintelab37a56d2010-09-02 14:53:56 +0100947 goto out_error;
Daniel De Graafe4184aa2011-10-13 16:07:08 -0400948 xen_store_evtchn = (int)v;
949 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
950 if (err)
Juan Quintelab37a56d2010-09-02 14:53:56 +0100951 goto out_error;
Julien Grall5f510422015-08-07 17:34:42 +0100952 xen_store_gfn = (unsigned long)v;
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100953 xen_store_interface =
Julien Grall7d567922015-05-05 16:38:27 +0100954 xen_remap(xen_store_gfn << XEN_PAGE_SHIFT,
955 XEN_PAGE_SIZE);
Stefano Stabelliniecc635f2012-09-14 12:13:12 +0100956 break;
957 default:
958 pr_warn("Xenstore state unknown\n");
959 break;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700960 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700961
David Woodhouse3499ba82021-01-13 13:26:02 +0000962 /*
963 * HVM domains may not have a functional callback yet. In that
964 * case let xs_init() be called from xenbus_probe(), which will
965 * get invoked at an appropriate time.
966 */
967 if (xen_store_domain_type != XS_HVM) {
968 err = xs_init();
969 if (err) {
970 pr_warn("Error initializing xenstore comms: %i\n", err);
971 goto out_error;
972 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700973 }
974
Boris Ostrovsky16f1cf32015-04-29 17:10:13 -0400975 if ((xen_store_domain_type != XS_LOCAL) &&
976 (xen_store_domain_type != XS_UNKNOWN))
977 xen_resume_notifier_register(&xenbus_resume_nb);
978
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800979#ifdef CONFIG_XEN_COMPAT_XENFS
980 /*
981 * Create xenfs mountpoint in /proc for compatibility with
982 * utilities that expect to find "xenbus" under "/proc/xen".
983 */
Seth Forsheef97df702016-11-14 11:12:56 +0000984 proc_create_mount_point("xen");
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800985#endif
Stefano Stabellini08f6c2b2021-11-15 14:27:19 -0800986 return 0;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800987
Ruslan Pisarev69132002011-07-26 14:17:23 +0300988out_error:
Stefano Stabellini08f6c2b2021-11-15 14:27:19 -0800989 xen_store_domain_type = XS_UNKNOWN;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700990 return err;
991}
992
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100993postcore_initcall(xenbus_init);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700994
995MODULE_LICENSE("GPL");