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