1df660251SIan Campbell /****************************************************************************** 2df660251SIan Campbell * Talks to Xen Store to figure out what devices we have (backend half). 3df660251SIan Campbell * 4df660251SIan Campbell * Copyright (C) 2005 Rusty Russell, IBM Corporation 5df660251SIan Campbell * Copyright (C) 2005 Mike Wray, Hewlett-Packard 6df660251SIan Campbell * Copyright (C) 2005, 2006 XenSource Ltd 7df660251SIan Campbell * Copyright (C) 2007 Solarflare Communications, Inc. 8df660251SIan Campbell * 9df660251SIan Campbell * This program is free software; you can redistribute it and/or 10df660251SIan Campbell * modify it under the terms of the GNU General Public License version 2 11df660251SIan Campbell * as published by the Free Software Foundation; or, when distributed 12df660251SIan Campbell * separately from the Linux kernel or incorporated into other 13df660251SIan Campbell * software packages, subject to the following license: 14df660251SIan Campbell * 15df660251SIan Campbell * Permission is hereby granted, free of charge, to any person obtaining a copy 16df660251SIan Campbell * of this source file (the "Software"), to deal in the Software without 17df660251SIan Campbell * restriction, including without limitation the rights to use, copy, modify, 18df660251SIan Campbell * merge, publish, distribute, sublicense, and/or sell copies of the Software, 19df660251SIan Campbell * and to permit persons to whom the Software is furnished to do so, subject to 20df660251SIan Campbell * the following conditions: 21df660251SIan Campbell * 22df660251SIan Campbell * The above copyright notice and this permission notice shall be included in 23df660251SIan Campbell * all copies or substantial portions of the Software. 24df660251SIan Campbell * 25df660251SIan Campbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26df660251SIan Campbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27df660251SIan Campbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28df660251SIan Campbell * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29df660251SIan Campbell * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30df660251SIan Campbell * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 31df660251SIan Campbell * IN THE SOFTWARE. 32df660251SIan Campbell */ 33df660251SIan Campbell 34df660251SIan Campbell #define DPRINTK(fmt, args...) \ 35df660251SIan Campbell pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ 36df660251SIan Campbell __func__, __LINE__, ##args) 37df660251SIan Campbell 38df660251SIan Campbell #include <linux/kernel.h> 39df660251SIan Campbell #include <linux/err.h> 40df660251SIan Campbell #include <linux/string.h> 41df660251SIan Campbell #include <linux/ctype.h> 42df660251SIan Campbell #include <linux/fcntl.h> 43df660251SIan Campbell #include <linux/mm.h> 44df660251SIan Campbell #include <linux/notifier.h> 45df660251SIan Campbell 46df660251SIan Campbell #include <asm/page.h> 47df660251SIan Campbell #include <asm/pgtable.h> 48df660251SIan Campbell #include <asm/xen/hypervisor.h> 49df660251SIan Campbell #include <asm/hypervisor.h> 50df660251SIan Campbell #include <xen/xenbus.h> 51df660251SIan Campbell #include <xen/features.h> 52df660251SIan Campbell 53df660251SIan Campbell #include "xenbus_comms.h" 54df660251SIan Campbell #include "xenbus_probe.h" 55df660251SIan Campbell 56df660251SIan Campbell /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */ 57df660251SIan Campbell static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) 58df660251SIan Campbell { 59df660251SIan Campbell int domid, err; 60df660251SIan Campbell const char *devid, *type, *frontend; 61df660251SIan Campbell unsigned int typelen; 62df660251SIan Campbell 63df660251SIan Campbell type = strchr(nodename, '/'); 64df660251SIan Campbell if (!type) 65df660251SIan Campbell return -EINVAL; 66df660251SIan Campbell type++; 67df660251SIan Campbell typelen = strcspn(type, "/"); 68df660251SIan Campbell if (!typelen || type[typelen] != '/') 69df660251SIan Campbell return -EINVAL; 70df660251SIan Campbell 71df660251SIan Campbell devid = strrchr(nodename, '/') + 1; 72df660251SIan Campbell 73df660251SIan Campbell err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid, 74df660251SIan Campbell "frontend", NULL, &frontend, 75df660251SIan Campbell NULL); 76df660251SIan Campbell if (err) 77df660251SIan Campbell return err; 78df660251SIan Campbell if (strlen(frontend) == 0) 79df660251SIan Campbell err = -ERANGE; 80df660251SIan Campbell if (!err && !xenbus_exists(XBT_NIL, frontend, "")) 81df660251SIan Campbell err = -ENOENT; 82df660251SIan Campbell kfree(frontend); 83df660251SIan Campbell 84df660251SIan Campbell if (err) 85df660251SIan Campbell return err; 86df660251SIan Campbell 87df660251SIan Campbell if (snprintf(bus_id, XEN_BUS_ID_SIZE, 88df660251SIan Campbell "%.*s-%i-%s", typelen, type, domid, devid) >= XEN_BUS_ID_SIZE) 89df660251SIan Campbell return -ENOSPC; 90df660251SIan Campbell return 0; 91df660251SIan Campbell } 92df660251SIan Campbell 93df660251SIan Campbell static int xenbus_uevent_backend(struct device *dev, 94df660251SIan Campbell struct kobj_uevent_env *env) 95df660251SIan Campbell { 96df660251SIan Campbell struct xenbus_device *xdev; 97df660251SIan Campbell struct xenbus_driver *drv; 98df660251SIan Campbell struct xen_bus_type *bus; 99df660251SIan Campbell 100df660251SIan Campbell DPRINTK(""); 101df660251SIan Campbell 102df660251SIan Campbell if (dev == NULL) 103df660251SIan Campbell return -ENODEV; 104df660251SIan Campbell 105df660251SIan Campbell xdev = to_xenbus_device(dev); 106df660251SIan Campbell bus = container_of(xdev->dev.bus, struct xen_bus_type, bus); 107df660251SIan Campbell if (xdev == NULL) 108df660251SIan Campbell return -ENODEV; 109df660251SIan Campbell 110df660251SIan Campbell /* stuff we want to pass to /sbin/hotplug */ 111df660251SIan Campbell if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype)) 112df660251SIan Campbell return -ENOMEM; 113df660251SIan Campbell 114df660251SIan Campbell if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename)) 115df660251SIan Campbell return -ENOMEM; 116df660251SIan Campbell 117df660251SIan Campbell if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root)) 118df660251SIan Campbell return -ENOMEM; 119df660251SIan Campbell 120df660251SIan Campbell if (dev->driver) { 121df660251SIan Campbell drv = to_xenbus_driver(dev->driver); 122df660251SIan Campbell if (drv && drv->uevent) 123df660251SIan Campbell return drv->uevent(xdev, env); 124df660251SIan Campbell } 125df660251SIan Campbell 126df660251SIan Campbell return 0; 127df660251SIan Campbell } 128df660251SIan Campbell 129df660251SIan Campbell /* backend/<typename>/<frontend-uuid>/<name> */ 130df660251SIan Campbell static int xenbus_probe_backend_unit(struct xen_bus_type *bus, 131df660251SIan Campbell const char *dir, 132df660251SIan Campbell const char *type, 133df660251SIan Campbell const char *name) 134df660251SIan Campbell { 135df660251SIan Campbell char *nodename; 136df660251SIan Campbell int err; 137df660251SIan Campbell 138df660251SIan Campbell nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name); 139df660251SIan Campbell if (!nodename) 140df660251SIan Campbell return -ENOMEM; 141df660251SIan Campbell 142df660251SIan Campbell DPRINTK("%s\n", nodename); 143df660251SIan Campbell 144df660251SIan Campbell err = xenbus_probe_node(bus, type, nodename); 145df660251SIan Campbell kfree(nodename); 146df660251SIan Campbell return err; 147df660251SIan Campbell } 148df660251SIan Campbell 149df660251SIan Campbell /* backend/<typename>/<frontend-domid> */ 150df660251SIan Campbell static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type, const char *domid) 151df660251SIan Campbell { 152df660251SIan Campbell char *nodename; 153df660251SIan Campbell int err = 0; 154df660251SIan Campbell char **dir; 155df660251SIan Campbell unsigned int i, dir_n = 0; 156df660251SIan Campbell 157df660251SIan Campbell DPRINTK(""); 158df660251SIan Campbell 159df660251SIan Campbell nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid); 160df660251SIan Campbell if (!nodename) 161df660251SIan Campbell return -ENOMEM; 162df660251SIan Campbell 163df660251SIan Campbell dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n); 164df660251SIan Campbell if (IS_ERR(dir)) { 165df660251SIan Campbell kfree(nodename); 166df660251SIan Campbell return PTR_ERR(dir); 167df660251SIan Campbell } 168df660251SIan Campbell 169df660251SIan Campbell for (i = 0; i < dir_n; i++) { 170df660251SIan Campbell err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]); 171df660251SIan Campbell if (err) 172df660251SIan Campbell break; 173df660251SIan Campbell } 174df660251SIan Campbell kfree(dir); 175df660251SIan Campbell kfree(nodename); 176df660251SIan Campbell return err; 177df660251SIan Campbell } 178df660251SIan Campbell 179df660251SIan Campbell static void frontend_changed(struct xenbus_watch *watch, 180df660251SIan Campbell const char **vec, unsigned int len) 181df660251SIan Campbell { 182df660251SIan Campbell xenbus_otherend_changed(watch, vec, len, 0); 183df660251SIan Campbell } 184df660251SIan Campbell 185df660251SIan Campbell static struct device_attribute xenbus_backend_dev_attrs[] = { 186df660251SIan Campbell __ATTR_NULL 187df660251SIan Campbell }; 188df660251SIan Campbell 189df660251SIan Campbell static struct xen_bus_type xenbus_backend = { 190df660251SIan Campbell .root = "backend", 191df660251SIan Campbell .levels = 3, /* backend/type/<frontend>/<id> */ 192df660251SIan Campbell .get_bus_id = backend_bus_id, 193df660251SIan Campbell .probe = xenbus_probe_backend, 194df660251SIan Campbell .otherend_changed = frontend_changed, 195df660251SIan Campbell .bus = { 196df660251SIan Campbell .name = "xen-backend", 197df660251SIan Campbell .match = xenbus_match, 198df660251SIan Campbell .uevent = xenbus_uevent_backend, 199df660251SIan Campbell .probe = xenbus_dev_probe, 200df660251SIan Campbell .remove = xenbus_dev_remove, 201df660251SIan Campbell .shutdown = xenbus_dev_shutdown, 202df660251SIan Campbell .dev_attrs = xenbus_backend_dev_attrs, 203df660251SIan Campbell }, 204df660251SIan Campbell }; 205df660251SIan Campbell 206df660251SIan Campbell static void backend_changed(struct xenbus_watch *watch, 207df660251SIan Campbell const char **vec, unsigned int len) 208df660251SIan Campbell { 209df660251SIan Campbell DPRINTK(""); 210df660251SIan Campbell 211df660251SIan Campbell xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend); 212df660251SIan Campbell } 213df660251SIan Campbell 214df660251SIan Campbell static struct xenbus_watch be_watch = { 215df660251SIan Campbell .node = "backend", 216df660251SIan Campbell .callback = backend_changed, 217df660251SIan Campbell }; 218df660251SIan Campbell 219df660251SIan Campbell static int read_frontend_details(struct xenbus_device *xendev) 220df660251SIan Campbell { 221df660251SIan Campbell return xenbus_read_otherend_details(xendev, "frontend-id", "frontend"); 222df660251SIan Campbell } 223df660251SIan Campbell 224df660251SIan Campbell //void xenbus_backend_suspend(int (*fn)(struct device *, void *)) 225df660251SIan Campbell //{ 226df660251SIan Campbell // DPRINTK(""); 227df660251SIan Campbell // bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn); 228df660251SIan Campbell //} 229df660251SIan Campbell 230df660251SIan Campbell //void xenbus_backend_resume(int (*fn)(struct device *, void *)) 231df660251SIan Campbell //{ 232df660251SIan Campbell // DPRINTK(""); 233df660251SIan Campbell // bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, fn); 234df660251SIan Campbell //} 235df660251SIan Campbell 236df660251SIan Campbell //int xenbus_for_each_backend(void *arg, int (*fn)(struct device *, void *)) 237df660251SIan Campbell //{ 238df660251SIan Campbell // return bus_for_each_dev(&xenbus_backend.bus, NULL, arg, fn); 239df660251SIan Campbell //} 240df660251SIan Campbell //EXPORT_SYMBOL_GPL(xenbus_for_each_backend); 241df660251SIan Campbell 242df660251SIan Campbell int xenbus_dev_is_online(struct xenbus_device *dev) 243df660251SIan Campbell { 244df660251SIan Campbell int rc, val; 245df660251SIan Campbell 246df660251SIan Campbell rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val); 247df660251SIan Campbell if (rc != 1) 248df660251SIan Campbell val = 0; /* no online node present */ 249df660251SIan Campbell 250df660251SIan Campbell return val; 251df660251SIan Campbell } 252df660251SIan Campbell EXPORT_SYMBOL_GPL(xenbus_dev_is_online); 253df660251SIan Campbell 254df660251SIan Campbell int __xenbus_register_backend(struct xenbus_driver *drv, 255df660251SIan Campbell struct module *owner, const char *mod_name) 256df660251SIan Campbell { 257df660251SIan Campbell drv->read_otherend_details = read_frontend_details; 258df660251SIan Campbell 259df660251SIan Campbell return xenbus_register_driver_common(drv, &xenbus_backend, 260df660251SIan Campbell owner, mod_name); 261df660251SIan Campbell } 262df660251SIan Campbell EXPORT_SYMBOL_GPL(__xenbus_register_backend); 263df660251SIan Campbell 264df660251SIan Campbell static int backend_probe_and_watch(struct notifier_block *notifier, 265df660251SIan Campbell unsigned long event, 266df660251SIan Campbell void *data) 267df660251SIan Campbell { 268df660251SIan Campbell /* Enumerate devices in xenstore and watch for changes. */ 269df660251SIan Campbell xenbus_probe_devices(&xenbus_backend); 270df660251SIan Campbell printk(KERN_CRIT "%s devices probed ok\n", __func__); 271df660251SIan Campbell register_xenbus_watch(&be_watch); 272df660251SIan Campbell printk(KERN_CRIT "%s watch add ok ok\n", __func__); 273df660251SIan Campbell printk(KERN_CRIT "%s all done\n", __func__); 274df660251SIan Campbell return NOTIFY_DONE; 275df660251SIan Campbell } 276df660251SIan Campbell 277df660251SIan Campbell static int __init xenbus_probe_backend_init(void) 278df660251SIan Campbell { 279df660251SIan Campbell static struct notifier_block xenstore_notifier = { 280df660251SIan Campbell .notifier_call = backend_probe_and_watch 281df660251SIan Campbell }; 282df660251SIan Campbell int err; 283df660251SIan Campbell 284df660251SIan Campbell DPRINTK(""); 285df660251SIan Campbell 286df660251SIan Campbell /* Register ourselves with the kernel bus subsystem */ 287df660251SIan Campbell err = bus_register(&xenbus_backend.bus); 288df660251SIan Campbell if (err) { 289df660251SIan Campbell printk(KERN_CRIT "%s didn't register bus!\n", __func__); 290df660251SIan Campbell return err; 291df660251SIan Campbell } 292df660251SIan Campbell printk(KERN_CRIT "%s bus registered ok\n", __func__); 293df660251SIan Campbell 294df660251SIan Campbell register_xenstore_notifier(&xenstore_notifier); 295df660251SIan Campbell 296df660251SIan Campbell return 0; 297df660251SIan Campbell } 298*c3676e84SJeremy Fitzhardinge subsys_initcall(xenbus_probe_backend_init); 299