Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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