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_front.c
34214077Sgibbs *
35214077Sgibbs * XenBus management of the NewBus bus containing the frontend instances of
36214077Sgibbs * Xen split devices.
37214077Sgibbs */
38214077Sgibbs#include <sys/cdefs.h>
39214077Sgibbs__FBSDID("$FreeBSD$");
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/stdarg.h>
55214077Sgibbs
56255040Sgibbs#include <xen/xen-os.h>
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 front bus.
65214077Sgibbs *
66214077Sgibbs * \param dev  NewBus device_t for this XenBus front bus instance.
67214077Sgibbs *
68214077Sgibbs * \return  Always returns 0 indicating success.
69214077Sgibbs */
70214077Sgibbsstatic int
71214077Sgibbsxenbusb_front_probe(device_t dev)
72214077Sgibbs{
73214077Sgibbs	device_set_desc(dev, "Xen Frontend Devices");
74214077Sgibbs
75214077Sgibbs	return (0);
76214077Sgibbs}
77214077Sgibbs
78214077Sgibbs/**
79214077Sgibbs * \brief Attach the XenBus front bus.
80214077Sgibbs *
81214077Sgibbs * \param dev  NewBus device_t for this XenBus front bus instance.
82214077Sgibbs *
83214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
84214077Sgibbs *          type of failure.
85214077Sgibbs */
86214077Sgibbsstatic int
87214077Sgibbsxenbusb_front_attach(device_t dev)
88214077Sgibbs{
89214077Sgibbs	return (xenbusb_attach(dev, "device", /*id_components*/1));
90214077Sgibbs}
91214077Sgibbs
92214077Sgibbs/**
93214077Sgibbs * \brief Enumerate all devices of the given type on this bus.
94214077Sgibbs *
95214077Sgibbs * \param dev   NewBus device_t for this XenBus front bus instance.
96214077Sgibbs * \param type  String indicating the device sub-tree (e.g. "vfb", "vif")
97214077Sgibbs *              to enumerate.
98214077Sgibbs *
99214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
100214077Sgibbs *          type of failure.
101214077Sgibbs *
102214077Sgibbs * Devices that are found are entered into the NewBus hierarchy via
103214077Sgibbs * xenbusb_add_device().  xenbusb_add_device() ignores duplicate detects
104214077Sgibbs * and ignores duplicate devices, so it can be called unconditionally
105214077Sgibbs * for any device found in the XenStore.
106214077Sgibbs */
107214077Sgibbsstatic int
108214077Sgibbsxenbusb_front_enumerate_type(device_t dev, const char *type)
109214077Sgibbs{
110214077Sgibbs	struct xenbusb_softc *xbs;
111214077Sgibbs	const char **dir;
112214077Sgibbs	unsigned int i, count;
113214077Sgibbs	int error;
114214077Sgibbs
115214077Sgibbs	xbs = device_get_softc(dev);
116214077Sgibbs	error = xs_directory(XST_NIL, xbs->xbs_node, type, &count, &dir);
117214077Sgibbs	if (error)
118214077Sgibbs		return (error);
119214077Sgibbs	for (i = 0; i < count; i++)
120214077Sgibbs		xenbusb_add_device(dev, type, dir[i]);
121214077Sgibbs
122214077Sgibbs	free(dir, M_XENSTORE);
123214077Sgibbs
124214077Sgibbs	return (0);
125214077Sgibbs}
126214077Sgibbs
127214077Sgibbs/**
128214077Sgibbs * \brief Determine and store the XenStore path for the other end of
129214077Sgibbs *        a split device whose local end is represented by ivars.
130214077Sgibbs *
131214077Sgibbs * If successful, the xd_otherend_path field of the child's instance
132214077Sgibbs * variables will be updated.
133214077Sgibbs *
134214077Sgibbs * \param dev    NewBus device_t for this XenBus front bus instance.
135214077Sgibbs * \param ivars  Instance variables from the XenBus child device for
136214077Sgibbs *               which to perform this function.
137214077Sgibbs *
138214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
139214077Sgibbs *          type of failure.
140214077Sgibbs */
141214077Sgibbsstatic int
142214077Sgibbsxenbusb_front_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
143214077Sgibbs{
144214077Sgibbs	char *otherend_path;
145214077Sgibbs	int error;
146214077Sgibbs
147214077Sgibbs	if (ivars->xd_otherend_path != NULL) {
148214077Sgibbs		free(ivars->xd_otherend_path, M_XENBUS);
149214077Sgibbs		ivars->xd_otherend_path = NULL;
150214077Sgibbs	}
151214077Sgibbs
152214077Sgibbs	error = xs_gather(XST_NIL, ivars->xd_node,
153214077Sgibbs	    "backend-id", "%i", &ivars->xd_otherend_id,
154214077Sgibbs	    "backend", NULL, &otherend_path,
155214077Sgibbs	    NULL);
156214077Sgibbs
157214077Sgibbs	if (error == 0) {
158214077Sgibbs		ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
159222975Sgibbs		ivars->xd_otherend_path_len = strlen(otherend_path);
160214077Sgibbs		free(otherend_path, M_XENSTORE);
161214077Sgibbs	}
162214077Sgibbs	return (error);
163214077Sgibbs}
164214077Sgibbs
165214077Sgibbs/*-------------------- Private Device Attachment Data  -----------------------*/
166214077Sgibbsstatic device_method_t xenbusb_front_methods[] = {
167214077Sgibbs	/* Device interface */
168214077Sgibbs	DEVMETHOD(device_identify,	xenbusb_identify),
169214077Sgibbs	DEVMETHOD(device_probe,         xenbusb_front_probe),
170214077Sgibbs	DEVMETHOD(device_attach,        xenbusb_front_attach),
171214077Sgibbs	DEVMETHOD(device_detach,        bus_generic_detach),
172214077Sgibbs	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
173214077Sgibbs	DEVMETHOD(device_suspend,       bus_generic_suspend),
174225704Sgibbs	DEVMETHOD(device_resume,        xenbusb_resume),
175214077Sgibbs
176214077Sgibbs	/* Bus Interface */
177214077Sgibbs	DEVMETHOD(bus_print_child,      xenbusb_print_child),
178214077Sgibbs	DEVMETHOD(bus_read_ivar,        xenbusb_read_ivar),
179214077Sgibbs	DEVMETHOD(bus_write_ivar,       xenbusb_write_ivar),
180214077Sgibbs	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
181214077Sgibbs	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
182214077Sgibbs	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
183214077Sgibbs	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
184214077Sgibbs
185214077Sgibbs	/* XenBus Bus Interface */
186214077Sgibbs	DEVMETHOD(xenbusb_enumerate_type, xenbusb_front_enumerate_type),
187214077Sgibbs	DEVMETHOD(xenbusb_get_otherend_node, xenbusb_front_get_otherend_node),
188214077Sgibbs	{ 0, 0 }
189214077Sgibbs};
190214077Sgibbs
191214077SgibbsDEFINE_CLASS_0(xenbusb_front, xenbusb_front_driver, xenbusb_front_methods,
192214077Sgibbs	       sizeof(struct xenbusb_softc));
193214077Sgibbsdevclass_t xenbusb_front_devclass;
194214077Sgibbs
195214077SgibbsDRIVER_MODULE(xenbusb_front, xenstore, xenbusb_front_driver,
196214077Sgibbs	      xenbusb_front_devclass, 0, 0);
197