xenbusb_back.c revision 222975
1214077Sgibbs/****************************************************************************** 2214077Sgibbs * Talks to Xen Store to figure out what devices we have. 3214077Sgibbs * 4214077Sgibbs * Copyright (C) 2009, 2010 Spectra Logic Corporation 5214077Sgibbs * Copyright (C) 2008 Doug Rabson 6214077Sgibbs * Copyright (C) 2005 Rusty Russell, IBM Corporation 7214077Sgibbs * Copyright (C) 2005 Mike Wray, Hewlett-Packard 8214077Sgibbs * Copyright (C) 2005 XenSource Ltd 9214077Sgibbs * 10214077Sgibbs * This file may be distributed separately from the Linux kernel, or 11214077Sgibbs * incorporated into other software packages, subject to the following license: 12214077Sgibbs * 13214077Sgibbs * Permission is hereby granted, free of charge, to any person obtaining a copy 14214077Sgibbs * of this source file (the "Software"), to deal in the Software without 15214077Sgibbs * restriction, including without limitation the rights to use, copy, modify, 16214077Sgibbs * merge, publish, distribute, sublicense, and/or sell copies of the Software, 17214077Sgibbs * and to permit persons to whom the Software is furnished to do so, subject to 18214077Sgibbs * the following conditions: 19214077Sgibbs * 20214077Sgibbs * The above copyright notice and this permission notice shall be included in 21214077Sgibbs * all copies or substantial portions of the Software. 22214077Sgibbs * 23214077Sgibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24214077Sgibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25214077Sgibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26214077Sgibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27214077Sgibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28214077Sgibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 29214077Sgibbs * IN THE SOFTWARE. 30214077Sgibbs */ 31214077Sgibbs 32214077Sgibbs/** 33214077Sgibbs * \file xenbusb_back.c 34214077Sgibbs * 35214077Sgibbs * XenBus management of the NewBus bus containing the backend instances of 36214077Sgibbs * Xen split devices. 37214077Sgibbs */ 38214077Sgibbs#include <sys/cdefs.h> 39214077Sgibbs__FBSDID("$FreeBSD: head/sys/xen/xenbus/xenbusb_back.c 222975 2011-06-11 04:59:01Z gibbs $"); 40214077Sgibbs 41214077Sgibbs#include <sys/param.h> 42214077Sgibbs#include <sys/bus.h> 43214077Sgibbs#include <sys/kernel.h> 44214077Sgibbs#include <sys/lock.h> 45214077Sgibbs#include <sys/malloc.h> 46214077Sgibbs#include <sys/module.h> 47214077Sgibbs#include <sys/sbuf.h> 48214077Sgibbs#include <sys/sysctl.h> 49214077Sgibbs#include <sys/syslog.h> 50214077Sgibbs#include <sys/systm.h> 51214077Sgibbs#include <sys/sx.h> 52214077Sgibbs#include <sys/taskqueue.h> 53214077Sgibbs 54214077Sgibbs#include <machine/xen/xen-os.h> 55214077Sgibbs#include <machine/stdarg.h> 56214077Sgibbs 57214077Sgibbs#include <xen/gnttab.h> 58214077Sgibbs#include <xen/xenbus/xenbusvar.h> 59214077Sgibbs#include <xen/xenbus/xenbusb.h> 60214077Sgibbs 61214077Sgibbs 62214077Sgibbs/*------------------ Private Device Attachment Functions --------------------*/ 63214077Sgibbs/** 64214077Sgibbs * \brief Probe for the existance of the XenBus back bus. 65214077Sgibbs * 66214077Sgibbs * \param dev NewBus device_t for this XenBus back bus instance. 67214077Sgibbs * 68214077Sgibbs * \return Always returns 0 indicating success. 69214077Sgibbs */ 70214077Sgibbsstatic int 71214077Sgibbsxenbusb_back_probe(device_t dev) 72214077Sgibbs{ 73214077Sgibbs device_set_desc(dev, "Xen Backend Devices"); 74214077Sgibbs 75214077Sgibbs return (0); 76214077Sgibbs} 77214077Sgibbs 78214077Sgibbs/** 79214077Sgibbs * \brief Attach the XenBus back bus. 80214077Sgibbs * 81214077Sgibbs * \param dev NewBus device_t for this XenBus back bus instance. 82214077Sgibbs * 83214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 84214077Sgibbs * type of failure. 85214077Sgibbs */ 86214077Sgibbsstatic int 87214077Sgibbsxenbusb_back_attach(device_t dev) 88214077Sgibbs{ 89214077Sgibbs struct xenbusb_softc *xbs; 90214077Sgibbs int error; 91214077Sgibbs 92214077Sgibbs xbs = device_get_softc(dev); 93214077Sgibbs error = xenbusb_attach(dev, "backend", /*id_components*/2); 94214077Sgibbs 95214077Sgibbs /* 96214077Sgibbs * Backend devices operate to serve other domains, 97214077Sgibbs * so there is no need to hold up boot processing 98214077Sgibbs * while connections to foreign domains are made. 99214077Sgibbs */ 100214077Sgibbs mtx_lock(&xbs->xbs_lock); 101214077Sgibbs if ((xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) { 102214077Sgibbs xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE; 103214077Sgibbs mtx_unlock(&xbs->xbs_lock); 104214077Sgibbs config_intrhook_disestablish(&xbs->xbs_attach_ch); 105214077Sgibbs } else { 106214077Sgibbs mtx_unlock(&xbs->xbs_lock); 107214077Sgibbs } 108214077Sgibbs 109214077Sgibbs return (error); 110214077Sgibbs} 111214077Sgibbs 112214077Sgibbs/** 113214077Sgibbs * \brief Enumerate all devices of the given type on this bus. 114214077Sgibbs * 115214077Sgibbs * \param dev NewBus device_t for this XenBus backend bus instance. 116214077Sgibbs * \param type String indicating the device sub-tree (e.g. "vfb", "vif") 117214077Sgibbs * to enumerate. 118214077Sgibbs * 119214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 120214077Sgibbs * type of failure. 121214077Sgibbs * 122214077Sgibbs * Devices that are found are entered into the NewBus hierarchy via 123214077Sgibbs * xenbusb_add_device(). xenbusb_add_device() ignores duplicate detects 124214077Sgibbs * and ignores duplicate devices, so it can be called unconditionally 125214077Sgibbs * for any device found in the XenStore. 126214077Sgibbs * 127214077Sgibbs * The backend XenStore hierarchy has the following format: 128214077Sgibbs * 129214077Sgibbs * backend/<device type>/<frontend vm id>/<device id> 130214077Sgibbs * 131214077Sgibbs */ 132214077Sgibbsstatic int 133214077Sgibbsxenbusb_back_enumerate_type(device_t dev, const char *type) 134214077Sgibbs{ 135214077Sgibbs struct xenbusb_softc *xbs; 136214077Sgibbs const char **vms; 137214077Sgibbs u_int vm_idx; 138214077Sgibbs u_int vm_count; 139214077Sgibbs int error; 140214077Sgibbs 141214077Sgibbs xbs = device_get_softc(dev); 142214077Sgibbs error = xs_directory(XST_NIL, xbs->xbs_node, type, &vm_count, &vms); 143214077Sgibbs if (error) 144214077Sgibbs return (error); 145214077Sgibbs for (vm_idx = 0; vm_idx < vm_count; vm_idx++) { 146214077Sgibbs struct sbuf *vm_path; 147214077Sgibbs const char *vm; 148214077Sgibbs const char **devs; 149214077Sgibbs u_int dev_idx; 150214077Sgibbs u_int dev_count; 151214077Sgibbs 152214077Sgibbs vm = vms[vm_idx]; 153214077Sgibbs 154214077Sgibbs vm_path = xs_join(type, vm); 155214077Sgibbs error = xs_directory(XST_NIL, xbs->xbs_node, sbuf_data(vm_path), 156214077Sgibbs &dev_count, &devs); 157214077Sgibbs sbuf_delete(vm_path); 158214077Sgibbs if (error) 159214077Sgibbs break; 160214077Sgibbs 161214077Sgibbs for (dev_idx = 0; dev_idx < dev_count; dev_idx++) { 162214077Sgibbs const char *dev_num; 163214077Sgibbs struct sbuf *id; 164214077Sgibbs 165214077Sgibbs dev_num = devs[dev_idx]; 166214077Sgibbs id = xs_join(vm, dev_num); 167214077Sgibbs xenbusb_add_device(dev, type, sbuf_data(id)); 168214077Sgibbs sbuf_delete(id); 169214077Sgibbs } 170214077Sgibbs free(devs, M_XENSTORE); 171214077Sgibbs } 172214077Sgibbs 173214077Sgibbs free(vms, M_XENSTORE); 174214077Sgibbs 175214077Sgibbs return (0); 176214077Sgibbs} 177214077Sgibbs 178214077Sgibbs/** 179214077Sgibbs * \brief Determine and store the XenStore path for the other end of 180214077Sgibbs * a split device whose local end is represented by ivars. 181214077Sgibbs * 182214077Sgibbs * \param dev NewBus device_t for this XenBus backend bus instance. 183214077Sgibbs * \param ivars Instance variables from the XenBus child device for 184214077Sgibbs * which to perform this function. 185214077Sgibbs * 186214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 187214077Sgibbs * type of failure. 188214077Sgibbs * 189214077Sgibbs * If successful, the xd_otherend_path field of the child's instance 190214077Sgibbs * variables will be updated. 191214077Sgibbs * 192214077Sgibbs */ 193214077Sgibbsstatic int 194214077Sgibbsxenbusb_back_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars) 195214077Sgibbs{ 196214077Sgibbs char *otherend_path; 197214077Sgibbs int error; 198214077Sgibbs 199214077Sgibbs if (ivars->xd_otherend_path != NULL) { 200214077Sgibbs free(ivars->xd_otherend_path, M_XENBUS); 201214077Sgibbs ivars->xd_otherend_path = NULL; 202214077Sgibbs } 203214077Sgibbs 204214077Sgibbs error = xs_gather(XST_NIL, ivars->xd_node, 205214077Sgibbs "frontend-id", "%i", &ivars->xd_otherend_id, 206214077Sgibbs "frontend", NULL, &otherend_path, 207214077Sgibbs NULL); 208214077Sgibbs 209214077Sgibbs if (error == 0) { 210214077Sgibbs ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS); 211222975Sgibbs ivars->xd_otherend_path_len = strlen(otherend_path); 212214077Sgibbs free(otherend_path, M_XENSTORE); 213214077Sgibbs } 214214077Sgibbs return (error); 215214077Sgibbs} 216214077Sgibbs 217214077Sgibbs/** 218222975Sgibbs * \brief Backend XenBus method implementing responses to peer state changes. 219222975Sgibbs * 220222975Sgibbs * \param bus The XenBus bus parent of child. 221222975Sgibbs * \param child The XenBus child whose peer stat has changed. 222222975Sgibbs * \param state The current state of the peer. 223214077Sgibbs */ 224222975Sgibbsstatic void 225222975Sgibbsxenbusb_back_otherend_changed(device_t bus, device_t child, 226222975Sgibbs enum xenbus_state peer_state) 227214077Sgibbs{ 228222975Sgibbs /* Perform default processing of state. */ 229222975Sgibbs xenbusb_otherend_changed(bus, child, peer_state); 230214077Sgibbs 231222975Sgibbs /* 232222975Sgibbs * "Online" devices are never fully detached in the 233222975Sgibbs * newbus sense. Only the front<->back connection is 234222975Sgibbs * torn down. If the front returns to the initialising 235222975Sgibbs * state after closing a previous connection, signal 236222975Sgibbs * our willingness to reconnect and that all necessary 237222975Sgibbs * XenStore data for feature negotiation is present. 238222975Sgibbs */ 239222975Sgibbs if (peer_state == XenbusStateInitialising 240222975Sgibbs && xenbus_dev_is_online(child) != 0 241222975Sgibbs && xenbus_get_state(child) == XenbusStateClosed) 242222975Sgibbs xenbus_set_state(child, XenbusStateInitWait); 243222975Sgibbs} 244214077Sgibbs 245222975Sgibbs/** 246222975Sgibbs * \brief Backend XenBus method implementing responses to local 247222975Sgibbs * XenStore changes. 248222975Sgibbs * 249222975Sgibbs * \param bus The XenBus bus parent of child. 250222975Sgibbs * \param child The XenBus child whose peer stat has changed. 251222975Sgibbs * \param_path The tree relative sub-path to the modified node. The empty 252222975Sgibbs * string indicates the root of the tree was destroyed. 253222975Sgibbs */ 254222975Sgibbsstatic void 255222975Sgibbsxenbusb_back_localend_changed(device_t bus, device_t child, const char *path) 256222975Sgibbs{ 257214077Sgibbs 258222975Sgibbs xenbusb_localend_changed(bus, child, path); 259214077Sgibbs 260222975Sgibbs if (strcmp(path, "/state") != 0 261222975Sgibbs && strcmp(path, "/online") != 0) 262222975Sgibbs return; 263222975Sgibbs 264222975Sgibbs if (xenbus_get_state(child) != XenbusStateClosed 265222975Sgibbs || xenbus_dev_is_online(child) != 0) 266222975Sgibbs return; 267222975Sgibbs 268222975Sgibbs /* 269222975Sgibbs * Cleanup the hotplug entry in the XenStore if 270222975Sgibbs * present. The control domain expects any userland 271222975Sgibbs * component associated with this device to destroy 272222975Sgibbs * this node in order to signify it is safe to 273222975Sgibbs * teardown the device. However, not all backends 274222975Sgibbs * rely on userland components, and those that 275222975Sgibbs * do should either use a communication channel 276222975Sgibbs * other than the XenStore, or ensure the hotplug 277222975Sgibbs * data is already cleaned up. 278222975Sgibbs * 279222975Sgibbs * This removal ensures that no matter what path 280222975Sgibbs * is taken to mark a back-end closed, the control 281222975Sgibbs * domain will understand that it is closed. 282222975Sgibbs */ 283222975Sgibbs xs_rm(XST_NIL, xenbus_get_node(child), "hotplug-status"); 284214077Sgibbs} 285214077Sgibbs 286214077Sgibbs/*-------------------- Private Device Attachment Data -----------------------*/ 287214077Sgibbsstatic device_method_t xenbusb_back_methods[] = { 288214077Sgibbs /* Device interface */ 289214077Sgibbs DEVMETHOD(device_identify, xenbusb_identify), 290214077Sgibbs DEVMETHOD(device_probe, xenbusb_back_probe), 291214077Sgibbs DEVMETHOD(device_attach, xenbusb_back_attach), 292214077Sgibbs DEVMETHOD(device_detach, bus_generic_detach), 293214077Sgibbs DEVMETHOD(device_shutdown, bus_generic_shutdown), 294214077Sgibbs DEVMETHOD(device_suspend, bus_generic_suspend), 295214077Sgibbs DEVMETHOD(device_resume, bus_generic_resume), 296214077Sgibbs 297214077Sgibbs /* Bus Interface */ 298214077Sgibbs DEVMETHOD(bus_print_child, xenbusb_print_child), 299214077Sgibbs DEVMETHOD(bus_read_ivar, xenbusb_read_ivar), 300222975Sgibbs DEVMETHOD(bus_write_ivar, xenbusb_write_ivar), 301214077Sgibbs DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 302214077Sgibbs DEVMETHOD(bus_release_resource, bus_generic_release_resource), 303214077Sgibbs DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 304214077Sgibbs DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 305214077Sgibbs 306214077Sgibbs /* XenBus Bus Interface */ 307214077Sgibbs DEVMETHOD(xenbusb_enumerate_type, xenbusb_back_enumerate_type), 308214077Sgibbs DEVMETHOD(xenbusb_get_otherend_node, xenbusb_back_get_otherend_node), 309222975Sgibbs DEVMETHOD(xenbusb_otherend_changed, xenbusb_back_otherend_changed), 310222975Sgibbs DEVMETHOD(xenbusb_localend_changed, xenbusb_back_localend_changed), 311214077Sgibbs { 0, 0 } 312214077Sgibbs}; 313214077Sgibbs 314214077SgibbsDEFINE_CLASS_0(xenbusb_back, xenbusb_back_driver, xenbusb_back_methods, 315214077Sgibbs sizeof(struct xenbusb_softc)); 316214077Sgibbsdevclass_t xenbusb_back_devclass; 317214077Sgibbs 318214077SgibbsDRIVER_MODULE(xenbusb_back, xenstore, xenbusb_back_driver, 319214077Sgibbs xenbusb_back_devclass, 0, 0); 320