xref: /freebsd/sys/xen/xenbus/xenbusb.h (revision ff662b5c9814043f1fb93ab7b16eaa0d77515959)
1*ff662b5cSJustin T. Gibbs /*-
2*ff662b5cSJustin T. Gibbs  * Core definitions and data structures shareable across OS platforms.
3*ff662b5cSJustin T. Gibbs  *
4*ff662b5cSJustin T. Gibbs  * Copyright (c) 2010 Spectra Logic Corporation
5*ff662b5cSJustin T. Gibbs  * Copyright (C) 2008 Doug Rabson
6*ff662b5cSJustin T. Gibbs  * All rights reserved.
7*ff662b5cSJustin T. Gibbs  *
8*ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
9*ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
10*ff662b5cSJustin T. Gibbs  * are met:
11*ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
12*ff662b5cSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
13*ff662b5cSJustin T. Gibbs  *    without modification.
14*ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15*ff662b5cSJustin T. Gibbs  *    substantially similar to the "NO WARRANTY" disclaimer below
16*ff662b5cSJustin T. Gibbs  *    ("Disclaimer") and any redistribution must be conditioned upon
17*ff662b5cSJustin T. Gibbs  *    including a substantially similar Disclaimer requirement for further
18*ff662b5cSJustin T. Gibbs  *    binary redistribution.
19*ff662b5cSJustin T. Gibbs  *
20*ff662b5cSJustin T. Gibbs  * NO WARRANTY
21*ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*ff662b5cSJustin T. Gibbs  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*ff662b5cSJustin T. Gibbs  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
24*ff662b5cSJustin T. Gibbs  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25*ff662b5cSJustin T. Gibbs  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*ff662b5cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*ff662b5cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*ff662b5cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29*ff662b5cSJustin T. Gibbs  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30*ff662b5cSJustin T. Gibbs  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31*ff662b5cSJustin T. Gibbs  * POSSIBILITY OF SUCH DAMAGES.
32*ff662b5cSJustin T. Gibbs  *
33*ff662b5cSJustin T. Gibbs  * $FreeBSD$
34*ff662b5cSJustin T. Gibbs  */
35*ff662b5cSJustin T. Gibbs #ifndef _XEN_XENBUS_XENBUSB_H
36*ff662b5cSJustin T. Gibbs #define _XEN_XENBUS_XENBUSB_H
37*ff662b5cSJustin T. Gibbs 
38*ff662b5cSJustin T. Gibbs /**
39*ff662b5cSJustin T. Gibbs  * \file xenbusb.h
40*ff662b5cSJustin T. Gibbs  *
41*ff662b5cSJustin T. Gibbs  * Datastructures and function declarations for use in implementing
42*ff662b5cSJustin T. Gibbs  * bus attachements (e.g. frontend and backend device busses) for XenBus.
43*ff662b5cSJustin T. Gibbs  */
44*ff662b5cSJustin T. Gibbs #include "xenbusb_if.h"
45*ff662b5cSJustin T. Gibbs 
46*ff662b5cSJustin T. Gibbs /**
47*ff662b5cSJustin T. Gibbs  * Enumeration of state flag values for the xbs_flags field of
48*ff662b5cSJustin T. Gibbs  * the xenbusb_softc structure.
49*ff662b5cSJustin T. Gibbs  */
50*ff662b5cSJustin T. Gibbs typedef enum {
51*ff662b5cSJustin T. Gibbs 	/** */
52*ff662b5cSJustin T. Gibbs 	XBS_ATTACH_CH_ACTIVE = 0x01
53*ff662b5cSJustin T. Gibbs } xenbusb_softc_flag;
54*ff662b5cSJustin T. Gibbs 
55*ff662b5cSJustin T. Gibbs /**
56*ff662b5cSJustin T. Gibbs  * \brief Container for all state needed to manage a Xenbus Bus
57*ff662b5cSJustin T. Gibbs  *	  attachment.
58*ff662b5cSJustin T. Gibbs  */
59*ff662b5cSJustin T. Gibbs struct xenbusb_softc {
60*ff662b5cSJustin T. Gibbs 	/**
61*ff662b5cSJustin T. Gibbs 	 * XenStore watch used to monitor the subtree of the
62*ff662b5cSJustin T. Gibbs 	 * XenStore where devices for this bus attachment arrive
63*ff662b5cSJustin T. Gibbs 	 * and depart.
64*ff662b5cSJustin T. Gibbs 	 *
65*ff662b5cSJustin T. Gibbs 	 * \note This field must be the first in the softc structure
66*ff662b5cSJustin T. Gibbs 	 *       so that a simple cast can be used to retrieve the
67*ff662b5cSJustin T. Gibbs 	 *	 softc from within a XenStore watch event callback.
68*ff662b5cSJustin T. Gibbs 	 */
69*ff662b5cSJustin T. Gibbs 	struct xs_watch	        xbs_device_watch;
70*ff662b5cSJustin T. Gibbs 
71*ff662b5cSJustin T. Gibbs 	/** Mutex used to protect fields of the xenbusb_softc. */
72*ff662b5cSJustin T. Gibbs 	struct mtx		xbs_lock;
73*ff662b5cSJustin T. Gibbs 
74*ff662b5cSJustin T. Gibbs 	/** State flags. */
75*ff662b5cSJustin T. Gibbs 	xenbusb_softc_flag	xbs_flags;
76*ff662b5cSJustin T. Gibbs 
77*ff662b5cSJustin T. Gibbs 	/**
78*ff662b5cSJustin T. Gibbs 	 * A dedicated task for processing child arrival and
79*ff662b5cSJustin T. Gibbs 	 * departure events.
80*ff662b5cSJustin T. Gibbs 	 */
81*ff662b5cSJustin T. Gibbs 	struct task		xbs_probe_children;
82*ff662b5cSJustin T. Gibbs 
83*ff662b5cSJustin T. Gibbs 	/**
84*ff662b5cSJustin T. Gibbs 	 * Config Hook used to block boot processing until
85*ff662b5cSJustin T. Gibbs 	 * XenBus devices complete their connection processing
86*ff662b5cSJustin T. Gibbs 	 * with other VMs.
87*ff662b5cSJustin T. Gibbs 	 */
88*ff662b5cSJustin T. Gibbs 	struct intr_config_hook xbs_attach_ch;
89*ff662b5cSJustin T. Gibbs 
90*ff662b5cSJustin T. Gibbs 	/**
91*ff662b5cSJustin T. Gibbs 	 * The number of children for this bus that are still
92*ff662b5cSJustin T. Gibbs 	 * in the connecting (to other VMs) state.  This variable
93*ff662b5cSJustin T. Gibbs 	 * is used to determine when to release xbs_attach_ch.
94*ff662b5cSJustin T. Gibbs 	 */
95*ff662b5cSJustin T. Gibbs 	u_int			xbs_connecting_children;
96*ff662b5cSJustin T. Gibbs 
97*ff662b5cSJustin T. Gibbs 	/** The NewBus device_t for this bus attachment. */
98*ff662b5cSJustin T. Gibbs 	device_t		xbs_dev;
99*ff662b5cSJustin T. Gibbs 
100*ff662b5cSJustin T. Gibbs 	/**
101*ff662b5cSJustin T. Gibbs 	 * The VM relative path to the XenStore subtree this
102*ff662b5cSJustin T. Gibbs 	 * bus attachment manages.
103*ff662b5cSJustin T. Gibbs 	 */
104*ff662b5cSJustin T. Gibbs 	const char	       *xbs_node;
105*ff662b5cSJustin T. Gibbs 
106*ff662b5cSJustin T. Gibbs 	/**
107*ff662b5cSJustin T. Gibbs 	 * The number of path components (strings separated by the '/'
108*ff662b5cSJustin T. Gibbs 	 * character) that make up the device ID on this bus.
109*ff662b5cSJustin T. Gibbs 	 */
110*ff662b5cSJustin T. Gibbs 	u_int			xbs_id_components;
111*ff662b5cSJustin T. Gibbs };
112*ff662b5cSJustin T. Gibbs 
113*ff662b5cSJustin T. Gibbs /**
114*ff662b5cSJustin T. Gibbs  * Enumeration of state flag values for the xbs_flags field of
115*ff662b5cSJustin T. Gibbs  * the xenbusb_softc structure.
116*ff662b5cSJustin T. Gibbs  */
117*ff662b5cSJustin T. Gibbs typedef enum {
118*ff662b5cSJustin T. Gibbs 
119*ff662b5cSJustin T. Gibbs 	/**
120*ff662b5cSJustin T. Gibbs 	 * This device is contributing to the xbs_connecting_children
121*ff662b5cSJustin T. Gibbs 	 * count of its parent bus.
122*ff662b5cSJustin T. Gibbs 	 */
123*ff662b5cSJustin T. Gibbs 	XDF_CONNECTING = 0x01
124*ff662b5cSJustin T. Gibbs } xenbus_dev_flag;
125*ff662b5cSJustin T. Gibbs 
126*ff662b5cSJustin T. Gibbs /** Instance variables for devices on a XenBus bus. */
127*ff662b5cSJustin T. Gibbs struct xenbus_device_ivars {
128*ff662b5cSJustin T. Gibbs 	/**
129*ff662b5cSJustin T. Gibbs 	 * XenStore watch used to monitor the subtree of the
130*ff662b5cSJustin T. Gibbs 	 * XenStore where information about the otherend of
131*ff662b5cSJustin T. Gibbs 	 * the split Xen device this device instance represents.
132*ff662b5cSJustin T. Gibbs 	 *
133*ff662b5cSJustin T. Gibbs 	 * \note This field must be the first in the instance
134*ff662b5cSJustin T. Gibbs 	 *	 variable structure so that a simple cast can be
135*ff662b5cSJustin T. Gibbs 	 *	 used to retrieve ivar data from within a XenStore
136*ff662b5cSJustin T. Gibbs 	 *	 watch event callback.
137*ff662b5cSJustin T. Gibbs 	 */
138*ff662b5cSJustin T. Gibbs 	struct xs_watch		xd_otherend_watch;
139*ff662b5cSJustin T. Gibbs 
140*ff662b5cSJustin T. Gibbs 	/** Sleepable lock used to protect instance data. */
141*ff662b5cSJustin T. Gibbs 	struct sx		xd_lock;
142*ff662b5cSJustin T. Gibbs 
143*ff662b5cSJustin T. Gibbs 	/** State flags. */
144*ff662b5cSJustin T. Gibbs 	xenbus_dev_flag		xd_flags;
145*ff662b5cSJustin T. Gibbs 
146*ff662b5cSJustin T. Gibbs 	/** The NewBus device_t for this XenBus device instance. */
147*ff662b5cSJustin T. Gibbs 	device_t		xd_dev;
148*ff662b5cSJustin T. Gibbs 
149*ff662b5cSJustin T. Gibbs 	/**
150*ff662b5cSJustin T. Gibbs 	 * The VM relative path to the XenStore subtree representing
151*ff662b5cSJustin T. Gibbs 	 * this VMs half of this device.
152*ff662b5cSJustin T. Gibbs 	 */
153*ff662b5cSJustin T. Gibbs 	char		       *xd_node;
154*ff662b5cSJustin T. Gibbs 
155*ff662b5cSJustin T. Gibbs 	/** XenBus device type ("vbd", "vif", etc.). */
156*ff662b5cSJustin T. Gibbs 	char		       *xd_type;
157*ff662b5cSJustin T. Gibbs 
158*ff662b5cSJustin T. Gibbs 	/**
159*ff662b5cSJustin T. Gibbs 	 * Cached version of <xd_node>/state node in the XenStore.
160*ff662b5cSJustin T. Gibbs 	 */
161*ff662b5cSJustin T. Gibbs 	enum xenbus_state	xd_state;
162*ff662b5cSJustin T. Gibbs 
163*ff662b5cSJustin T. Gibbs 	/** The VM identifier of the other end of this split device. */
164*ff662b5cSJustin T. Gibbs 	int			xd_otherend_id;
165*ff662b5cSJustin T. Gibbs 
166*ff662b5cSJustin T. Gibbs 	/**
167*ff662b5cSJustin T. Gibbs 	 * The path to the subtree of the XenStore where information
168*ff662b5cSJustin T. Gibbs 	 * about the otherend of this split device instance.
169*ff662b5cSJustin T. Gibbs 	 */
170*ff662b5cSJustin T. Gibbs 	char		       *xd_otherend_path;
171*ff662b5cSJustin T. Gibbs };
172*ff662b5cSJustin T. Gibbs 
173*ff662b5cSJustin T. Gibbs /**
174*ff662b5cSJustin T. Gibbs  * \brief Identify instances of this device type in the system.
175*ff662b5cSJustin T. Gibbs  *
176*ff662b5cSJustin T. Gibbs  * \param driver  The driver performing this identify action.
177*ff662b5cSJustin T. Gibbs  * \param parent  The NewBus parent device for any devices this method adds.
178*ff662b5cSJustin T. Gibbs  */
179*ff662b5cSJustin T. Gibbs void xenbusb_identify(driver_t *driver __unused, device_t parent);
180*ff662b5cSJustin T. Gibbs 
181*ff662b5cSJustin T. Gibbs /**
182*ff662b5cSJustin T. Gibbs  * \brief Perform common XenBus bus attach processing.
183*ff662b5cSJustin T. Gibbs  *
184*ff662b5cSJustin T. Gibbs  * \param dev            The NewBus device representing this XenBus bus.
185*ff662b5cSJustin T. Gibbs  * \param bus_node       The XenStore path to the XenStore subtree for
186*ff662b5cSJustin T. Gibbs  *                       this XenBus bus.
187*ff662b5cSJustin T. Gibbs  * \param id_components  The number of '/' separated path components that
188*ff662b5cSJustin T. Gibbs  *                       make up a unique device ID on this XenBus bus.
189*ff662b5cSJustin T. Gibbs  *
190*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
191*ff662b5cSJustin T. Gibbs  *          type of failure.
192*ff662b5cSJustin T. Gibbs  *
193*ff662b5cSJustin T. Gibbs  * Intiailizes the softc for this bus, installs an interrupt driven
194*ff662b5cSJustin T. Gibbs  * configuration hook to block boot processing until XenBus devices fully
195*ff662b5cSJustin T. Gibbs  * configure, performs an initial probe/attach of the bus, and registers
196*ff662b5cSJustin T. Gibbs  * a XenStore watch so we are notified when the bus topology changes.
197*ff662b5cSJustin T. Gibbs  */
198*ff662b5cSJustin T. Gibbs int xenbusb_attach(device_t dev, char *bus_node, u_int id_components);
199*ff662b5cSJustin T. Gibbs 
200*ff662b5cSJustin T. Gibbs /**
201*ff662b5cSJustin T. Gibbs  * \brief Perform common XenBus bus resume handling.
202*ff662b5cSJustin T. Gibbs  *
203*ff662b5cSJustin T. Gibbs  * \param dev  The NewBus device representing this XenBus bus.
204*ff662b5cSJustin T. Gibbs  *
205*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
206*ff662b5cSJustin T. Gibbs  *          type of failure.
207*ff662b5cSJustin T. Gibbs  */
208*ff662b5cSJustin T. Gibbs int xenbusb_resume(device_t dev);
209*ff662b5cSJustin T. Gibbs 
210*ff662b5cSJustin T. Gibbs /**
211*ff662b5cSJustin T. Gibbs  * \brief Pretty-prints information about a child of a XenBus bus.
212*ff662b5cSJustin T. Gibbs  *
213*ff662b5cSJustin T. Gibbs  * \param dev    The NewBus device representing this XenBus bus.
214*ff662b5cSJustin T. Gibbs  * \param child	 The NewBus device representing a child of dev%'s XenBus bus.
215*ff662b5cSJustin T. Gibbs  *
216*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
217*ff662b5cSJustin T. Gibbs  *          type of failure.
218*ff662b5cSJustin T. Gibbs  */
219*ff662b5cSJustin T. Gibbs int xenbusb_print_child(device_t dev, device_t child);
220*ff662b5cSJustin T. Gibbs 
221*ff662b5cSJustin T. Gibbs /**
222*ff662b5cSJustin T. Gibbs  * \brief Common XenBus child instance variable read access method.
223*ff662b5cSJustin T. Gibbs  *
224*ff662b5cSJustin T. Gibbs  * \param dev     The NewBus device representing this XenBus bus.
225*ff662b5cSJustin T. Gibbs  * \param child	  The NewBus device representing a child of dev%'s XenBus bus.
226*ff662b5cSJustin T. Gibbs  * \param index	  The index of the instance variable to access.
227*ff662b5cSJustin T. Gibbs  * \param result  The value of the instance variable accessed.
228*ff662b5cSJustin T. Gibbs  *
229*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
230*ff662b5cSJustin T. Gibbs  *          type of failure.
231*ff662b5cSJustin T. Gibbs  */
232*ff662b5cSJustin T. Gibbs int xenbusb_read_ivar(device_t dev, device_t child, int index,
233*ff662b5cSJustin T. Gibbs 		      uintptr_t *result);
234*ff662b5cSJustin T. Gibbs 
235*ff662b5cSJustin T. Gibbs /**
236*ff662b5cSJustin T. Gibbs  * \brief Common XenBus child instance variable write access method.
237*ff662b5cSJustin T. Gibbs  *
238*ff662b5cSJustin T. Gibbs  * \param dev    The NewBus device representing this XenBus bus.
239*ff662b5cSJustin T. Gibbs  * \param child	 The NewBus device representing a child of dev%'s XenBus bus.
240*ff662b5cSJustin T. Gibbs  * \param index	 The index of the instance variable to access.
241*ff662b5cSJustin T. Gibbs  * \param value  The new value to set in the instance variable accessed.
242*ff662b5cSJustin T. Gibbs  *
243*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
244*ff662b5cSJustin T. Gibbs  *          type of failure.
245*ff662b5cSJustin T. Gibbs  */
246*ff662b5cSJustin T. Gibbs int xenbusb_write_ivar(device_t dev, device_t child, int index,
247*ff662b5cSJustin T. Gibbs 		       uintptr_t value);
248*ff662b5cSJustin T. Gibbs 
249*ff662b5cSJustin T. Gibbs /**
250*ff662b5cSJustin T. Gibbs  * \brief Attempt to add a XenBus device instance to this XenBus bus.
251*ff662b5cSJustin T. Gibbs  *
252*ff662b5cSJustin T. Gibbs  * \param dev   The NewBus device representing this XenBus bus.
253*ff662b5cSJustin T. Gibbs  * \param type  The device type being added (e.g. "vbd", "vif").
254*ff662b5cSJustin T. Gibbs  * \param id	The device ID for this device.
255*ff662b5cSJustin T. Gibbs  *
256*ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
257*ff662b5cSJustin T. Gibbs  *          type of failure.  Failure indicates that either the
258*ff662b5cSJustin T. Gibbs  *          path to this device no longer exists or insufficient
259*ff662b5cSJustin T. Gibbs  *          information exists in the XenStore to create a new
260*ff662b5cSJustin T. Gibbs  *          device.
261*ff662b5cSJustin T. Gibbs  *
262*ff662b5cSJustin T. Gibbs  * If successful, this routine will add a device_t with instance
263*ff662b5cSJustin T. Gibbs  * variable storage to the NewBus device topology.  Probe/Attach
264*ff662b5cSJustin T. Gibbs  * processing is not performed by this routine, but must be scheduled
265*ff662b5cSJustin T. Gibbs  * via the xbs_probe_children task.  This separation of responsibilities
266*ff662b5cSJustin T. Gibbs  * is required to avoid hanging up the XenStore event delivery thread
267*ff662b5cSJustin T. Gibbs  * with our probe/attach work in the event a device is added via
268*ff662b5cSJustin T. Gibbs  * a callback from the XenStore.
269*ff662b5cSJustin T. Gibbs  */
270*ff662b5cSJustin T. Gibbs int xenbusb_add_device(device_t dev, const char *type, const char *id);
271*ff662b5cSJustin T. Gibbs 
272*ff662b5cSJustin T. Gibbs #endif /* _XEN_XENBUS_XENBUSB_H */
273