1ff662b5cSJustin T. Gibbs /******************************************************************************
2ff662b5cSJustin T. Gibbs * Talks to Xen Store to figure out what devices we have.
3ff662b5cSJustin T. Gibbs *
4ff662b5cSJustin T. Gibbs * Copyright (C) 2009, 2010 Spectra Logic Corporation
5ff662b5cSJustin T. Gibbs * Copyright (C) 2008 Doug Rabson
6ff662b5cSJustin T. Gibbs * Copyright (C) 2005 Rusty Russell, IBM Corporation
7ff662b5cSJustin T. Gibbs * Copyright (C) 2005 Mike Wray, Hewlett-Packard
8ff662b5cSJustin T. Gibbs * Copyright (C) 2005 XenSource Ltd
9ff662b5cSJustin T. Gibbs *
10ff662b5cSJustin T. Gibbs * This file may be distributed separately from the Linux kernel, or
11ff662b5cSJustin T. Gibbs * incorporated into other software packages, subject to the following license:
12ff662b5cSJustin T. Gibbs *
13ff662b5cSJustin T. Gibbs * Permission is hereby granted, free of charge, to any person obtaining a copy
14ff662b5cSJustin T. Gibbs * of this source file (the "Software"), to deal in the Software without
15ff662b5cSJustin T. Gibbs * restriction, including without limitation the rights to use, copy, modify,
16ff662b5cSJustin T. Gibbs * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17ff662b5cSJustin T. Gibbs * and to permit persons to whom the Software is furnished to do so, subject to
18ff662b5cSJustin T. Gibbs * the following conditions:
19ff662b5cSJustin T. Gibbs *
20ff662b5cSJustin T. Gibbs * The above copyright notice and this permission notice shall be included in
21ff662b5cSJustin T. Gibbs * all copies or substantial portions of the Software.
22ff662b5cSJustin T. Gibbs *
23ff662b5cSJustin T. Gibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24ff662b5cSJustin T. Gibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25ff662b5cSJustin T. Gibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26ff662b5cSJustin T. Gibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27ff662b5cSJustin T. Gibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28ff662b5cSJustin T. Gibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29ff662b5cSJustin T. Gibbs * IN THE SOFTWARE.
30ff662b5cSJustin T. Gibbs */
31ff662b5cSJustin T. Gibbs
32ff662b5cSJustin T. Gibbs /**
33ff662b5cSJustin T. Gibbs * \file xenbusb_back.c
34ff662b5cSJustin T. Gibbs *
35ff662b5cSJustin T. Gibbs * XenBus management of the NewBus bus containing the backend instances of
36ff662b5cSJustin T. Gibbs * Xen split devices.
37ff662b5cSJustin T. Gibbs */
38fdafd315SWarner Losh
39ff662b5cSJustin T. Gibbs #include <sys/param.h>
40ff662b5cSJustin T. Gibbs #include <sys/bus.h>
41ff662b5cSJustin T. Gibbs #include <sys/kernel.h>
42ff662b5cSJustin T. Gibbs #include <sys/lock.h>
43ff662b5cSJustin T. Gibbs #include <sys/malloc.h>
44ff662b5cSJustin T. Gibbs #include <sys/module.h>
45ff662b5cSJustin T. Gibbs #include <sys/sbuf.h>
46ff662b5cSJustin T. Gibbs #include <sys/sysctl.h>
47ff662b5cSJustin T. Gibbs #include <sys/syslog.h>
48ff662b5cSJustin T. Gibbs #include <sys/systm.h>
49ff662b5cSJustin T. Gibbs #include <sys/sx.h>
50ff662b5cSJustin T. Gibbs #include <sys/taskqueue.h>
51ff662b5cSJustin T. Gibbs
52ff662b5cSJustin T. Gibbs #include <machine/stdarg.h>
53ff662b5cSJustin T. Gibbs
54ff5272caSJulien Grall #include <xen/xen-os.h>
55ff662b5cSJustin T. Gibbs #include <xen/gnttab.h>
56ff662b5cSJustin T. Gibbs #include <xen/xenbus/xenbusvar.h>
57ff662b5cSJustin T. Gibbs #include <xen/xenbus/xenbusb.h>
58ff662b5cSJustin T. Gibbs
59ff662b5cSJustin T. Gibbs /*------------------ Private Device Attachment Functions --------------------*/
60ff662b5cSJustin T. Gibbs /**
61bc9432d0SGordon Bergling * \brief Probe for the existence of the XenBus back bus.
62ff662b5cSJustin T. Gibbs *
63ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this XenBus back bus instance.
64ff662b5cSJustin T. Gibbs *
65ff662b5cSJustin T. Gibbs * \return Always returns 0 indicating success.
66ff662b5cSJustin T. Gibbs */
67ff662b5cSJustin T. Gibbs static int
xenbusb_back_probe(device_t dev)68ff662b5cSJustin T. Gibbs xenbusb_back_probe(device_t dev)
69ff662b5cSJustin T. Gibbs {
70ff662b5cSJustin T. Gibbs device_set_desc(dev, "Xen Backend Devices");
71ff662b5cSJustin T. Gibbs
72ff662b5cSJustin T. Gibbs return (0);
73ff662b5cSJustin T. Gibbs }
74ff662b5cSJustin T. Gibbs
75ff662b5cSJustin T. Gibbs /**
76ff662b5cSJustin T. Gibbs * \brief Attach the XenBus back bus.
77ff662b5cSJustin T. Gibbs *
78ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this XenBus back bus instance.
79ff662b5cSJustin T. Gibbs *
80ff662b5cSJustin T. Gibbs * \return On success, 0. Otherwise an errno value indicating the
81ff662b5cSJustin T. Gibbs * type of failure.
82ff662b5cSJustin T. Gibbs */
83ff662b5cSJustin T. Gibbs static int
xenbusb_back_attach(device_t dev)84ff662b5cSJustin T. Gibbs xenbusb_back_attach(device_t dev)
85ff662b5cSJustin T. Gibbs {
86ff662b5cSJustin T. Gibbs struct xenbusb_softc *xbs;
87ff662b5cSJustin T. Gibbs int error;
88ff662b5cSJustin T. Gibbs
89ff662b5cSJustin T. Gibbs xbs = device_get_softc(dev);
90ff662b5cSJustin T. Gibbs error = xenbusb_attach(dev, "backend", /*id_components*/2);
91ff662b5cSJustin T. Gibbs
92ff662b5cSJustin T. Gibbs /*
93ff662b5cSJustin T. Gibbs * Backend devices operate to serve other domains,
94ff662b5cSJustin T. Gibbs * so there is no need to hold up boot processing
95ff662b5cSJustin T. Gibbs * while connections to foreign domains are made.
96ff662b5cSJustin T. Gibbs */
97ff662b5cSJustin T. Gibbs mtx_lock(&xbs->xbs_lock);
98ff662b5cSJustin T. Gibbs if ((xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) {
99ff662b5cSJustin T. Gibbs xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE;
100ff662b5cSJustin T. Gibbs mtx_unlock(&xbs->xbs_lock);
101ff662b5cSJustin T. Gibbs config_intrhook_disestablish(&xbs->xbs_attach_ch);
102ff662b5cSJustin T. Gibbs } else {
103ff662b5cSJustin T. Gibbs mtx_unlock(&xbs->xbs_lock);
104ff662b5cSJustin T. Gibbs }
105ff662b5cSJustin T. Gibbs
106ff662b5cSJustin T. Gibbs return (error);
107ff662b5cSJustin T. Gibbs }
108ff662b5cSJustin T. Gibbs
109ff662b5cSJustin T. Gibbs /**
110ff662b5cSJustin T. Gibbs * \brief Enumerate all devices of the given type on this bus.
111ff662b5cSJustin T. Gibbs *
112ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this XenBus backend bus instance.
113ff662b5cSJustin T. Gibbs * \param type String indicating the device sub-tree (e.g. "vfb", "vif")
114ff662b5cSJustin T. Gibbs * to enumerate.
115ff662b5cSJustin T. Gibbs *
116ff662b5cSJustin T. Gibbs * \return On success, 0. Otherwise an errno value indicating the
117ff662b5cSJustin T. Gibbs * type of failure.
118ff662b5cSJustin T. Gibbs *
119ff662b5cSJustin T. Gibbs * Devices that are found are entered into the NewBus hierarchy via
120ff662b5cSJustin T. Gibbs * xenbusb_add_device(). xenbusb_add_device() ignores duplicate detects
121ff662b5cSJustin T. Gibbs * and ignores duplicate devices, so it can be called unconditionally
122ff662b5cSJustin T. Gibbs * for any device found in the XenStore.
123ff662b5cSJustin T. Gibbs *
124ff662b5cSJustin T. Gibbs * The backend XenStore hierarchy has the following format:
125ff662b5cSJustin T. Gibbs *
126ff662b5cSJustin T. Gibbs * backend/<device type>/<frontend vm id>/<device id>
127ff662b5cSJustin T. Gibbs *
128ff662b5cSJustin T. Gibbs */
129ff662b5cSJustin T. Gibbs static int
xenbusb_back_enumerate_type(device_t dev,const char * type)130ff662b5cSJustin T. Gibbs xenbusb_back_enumerate_type(device_t dev, const char *type)
131ff662b5cSJustin T. Gibbs {
132ff662b5cSJustin T. Gibbs struct xenbusb_softc *xbs;
133ff662b5cSJustin T. Gibbs const char **vms;
134ff662b5cSJustin T. Gibbs u_int vm_idx;
135ff662b5cSJustin T. Gibbs u_int vm_count;
136ff662b5cSJustin T. Gibbs int error;
137ff662b5cSJustin T. Gibbs
138ff662b5cSJustin T. Gibbs xbs = device_get_softc(dev);
139ff662b5cSJustin T. Gibbs error = xs_directory(XST_NIL, xbs->xbs_node, type, &vm_count, &vms);
140ff662b5cSJustin T. Gibbs if (error)
141ff662b5cSJustin T. Gibbs return (error);
142ff662b5cSJustin T. Gibbs for (vm_idx = 0; vm_idx < vm_count; vm_idx++) {
143ff662b5cSJustin T. Gibbs struct sbuf *vm_path;
144ff662b5cSJustin T. Gibbs const char *vm;
145ff662b5cSJustin T. Gibbs const char **devs;
146ff662b5cSJustin T. Gibbs u_int dev_idx;
147ff662b5cSJustin T. Gibbs u_int dev_count;
148ff662b5cSJustin T. Gibbs
149ff662b5cSJustin T. Gibbs vm = vms[vm_idx];
150ff662b5cSJustin T. Gibbs
151ff662b5cSJustin T. Gibbs vm_path = xs_join(type, vm);
152ff662b5cSJustin T. Gibbs error = xs_directory(XST_NIL, xbs->xbs_node, sbuf_data(vm_path),
153ff662b5cSJustin T. Gibbs &dev_count, &devs);
154ff662b5cSJustin T. Gibbs sbuf_delete(vm_path);
155ff662b5cSJustin T. Gibbs if (error)
156ff662b5cSJustin T. Gibbs break;
157ff662b5cSJustin T. Gibbs
158ff662b5cSJustin T. Gibbs for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
159ff662b5cSJustin T. Gibbs const char *dev_num;
160ff662b5cSJustin T. Gibbs struct sbuf *id;
161ff662b5cSJustin T. Gibbs
162ff662b5cSJustin T. Gibbs dev_num = devs[dev_idx];
163ff662b5cSJustin T. Gibbs id = xs_join(vm, dev_num);
164ff662b5cSJustin T. Gibbs xenbusb_add_device(dev, type, sbuf_data(id));
165ff662b5cSJustin T. Gibbs sbuf_delete(id);
166ff662b5cSJustin T. Gibbs }
167ff662b5cSJustin T. Gibbs free(devs, M_XENSTORE);
168ff662b5cSJustin T. Gibbs }
169ff662b5cSJustin T. Gibbs
170ff662b5cSJustin T. Gibbs free(vms, M_XENSTORE);
171ff662b5cSJustin T. Gibbs
172ff662b5cSJustin T. Gibbs return (0);
173ff662b5cSJustin T. Gibbs }
174ff662b5cSJustin T. Gibbs
175ff662b5cSJustin T. Gibbs /**
176ff662b5cSJustin T. Gibbs * \brief Determine and store the XenStore path for the other end of
177ff662b5cSJustin T. Gibbs * a split device whose local end is represented by ivars.
178ff662b5cSJustin T. Gibbs *
179ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this XenBus backend bus instance.
180ff662b5cSJustin T. Gibbs * \param ivars Instance variables from the XenBus child device for
181ff662b5cSJustin T. Gibbs * which to perform this function.
182ff662b5cSJustin T. Gibbs *
183ff662b5cSJustin T. Gibbs * \return On success, 0. Otherwise an errno value indicating the
184ff662b5cSJustin T. Gibbs * type of failure.
185ff662b5cSJustin T. Gibbs *
186ff662b5cSJustin T. Gibbs * If successful, the xd_otherend_path field of the child's instance
187ff662b5cSJustin T. Gibbs * variables will be updated.
188ff662b5cSJustin T. Gibbs *
189ff662b5cSJustin T. Gibbs */
190ff662b5cSJustin T. Gibbs static int
xenbusb_back_get_otherend_node(device_t dev,struct xenbus_device_ivars * ivars)191ff662b5cSJustin T. Gibbs xenbusb_back_get_otherend_node(device_t dev, struct xenbus_device_ivars *ivars)
192ff662b5cSJustin T. Gibbs {
193ff662b5cSJustin T. Gibbs char *otherend_path;
194ff662b5cSJustin T. Gibbs int error;
195ff662b5cSJustin T. Gibbs
196ff662b5cSJustin T. Gibbs if (ivars->xd_otherend_path != NULL) {
197ff662b5cSJustin T. Gibbs free(ivars->xd_otherend_path, M_XENBUS);
198ff662b5cSJustin T. Gibbs ivars->xd_otherend_path = NULL;
199ff662b5cSJustin T. Gibbs }
200ff662b5cSJustin T. Gibbs
201ff662b5cSJustin T. Gibbs error = xs_gather(XST_NIL, ivars->xd_node,
202ff662b5cSJustin T. Gibbs "frontend-id", "%i", &ivars->xd_otherend_id,
203ff662b5cSJustin T. Gibbs "frontend", NULL, &otherend_path,
204ff662b5cSJustin T. Gibbs NULL);
205ff662b5cSJustin T. Gibbs
206ff662b5cSJustin T. Gibbs if (error == 0) {
207ff662b5cSJustin T. Gibbs ivars->xd_otherend_path = strdup(otherend_path, M_XENBUS);
208283d6f72SJustin T. Gibbs ivars->xd_otherend_path_len = strlen(otherend_path);
209ff662b5cSJustin T. Gibbs free(otherend_path, M_XENSTORE);
210ff662b5cSJustin T. Gibbs }
211ff662b5cSJustin T. Gibbs return (error);
212ff662b5cSJustin T. Gibbs }
213ff662b5cSJustin T. Gibbs
214ff662b5cSJustin T. Gibbs /**
215283d6f72SJustin T. Gibbs * \brief Backend XenBus method implementing responses to peer state changes.
216ff662b5cSJustin T. Gibbs *
217283d6f72SJustin T. Gibbs * \param bus The XenBus bus parent of child.
218283d6f72SJustin T. Gibbs * \param child The XenBus child whose peer stat has changed.
219283d6f72SJustin T. Gibbs * \param state The current state of the peer.
220ff662b5cSJustin T. Gibbs */
221283d6f72SJustin T. Gibbs static void
xenbusb_back_otherend_changed(device_t bus,device_t child,enum xenbus_state peer_state)222283d6f72SJustin T. Gibbs xenbusb_back_otherend_changed(device_t bus, device_t child,
223283d6f72SJustin T. Gibbs enum xenbus_state peer_state)
224ff662b5cSJustin T. Gibbs {
225283d6f72SJustin T. Gibbs /* Perform default processing of state. */
226283d6f72SJustin T. Gibbs xenbusb_otherend_changed(bus, child, peer_state);
227ff662b5cSJustin T. Gibbs
228283d6f72SJustin T. Gibbs /*
229283d6f72SJustin T. Gibbs * "Online" devices are never fully detached in the
230283d6f72SJustin T. Gibbs * newbus sense. Only the front<->back connection is
231283d6f72SJustin T. Gibbs * torn down. If the front returns to the initialising
232283d6f72SJustin T. Gibbs * state after closing a previous connection, signal
233283d6f72SJustin T. Gibbs * our willingness to reconnect and that all necessary
234283d6f72SJustin T. Gibbs * XenStore data for feature negotiation is present.
235283d6f72SJustin T. Gibbs */
236283d6f72SJustin T. Gibbs if (peer_state == XenbusStateInitialising
237283d6f72SJustin T. Gibbs && xenbus_dev_is_online(child) != 0
238283d6f72SJustin T. Gibbs && xenbus_get_state(child) == XenbusStateClosed)
239283d6f72SJustin T. Gibbs xenbus_set_state(child, XenbusStateInitWait);
240283d6f72SJustin T. Gibbs }
241ff662b5cSJustin T. Gibbs
242283d6f72SJustin T. Gibbs /**
243283d6f72SJustin T. Gibbs * \brief Backend XenBus method implementing responses to local
244283d6f72SJustin T. Gibbs * XenStore changes.
245283d6f72SJustin T. Gibbs *
246283d6f72SJustin T. Gibbs * \param bus The XenBus bus parent of child.
247283d6f72SJustin T. Gibbs * \param child The XenBus child whose peer stat has changed.
248283d6f72SJustin T. Gibbs * \param_path The tree relative sub-path to the modified node. The empty
249283d6f72SJustin T. Gibbs * string indicates the root of the tree was destroyed.
250283d6f72SJustin T. Gibbs */
251283d6f72SJustin T. Gibbs static void
xenbusb_back_localend_changed(device_t bus,device_t child,const char * path)252283d6f72SJustin T. Gibbs xenbusb_back_localend_changed(device_t bus, device_t child, const char *path)
253283d6f72SJustin T. Gibbs {
254283d6f72SJustin T. Gibbs
255283d6f72SJustin T. Gibbs xenbusb_localend_changed(bus, child, path);
256283d6f72SJustin T. Gibbs
257283d6f72SJustin T. Gibbs if (strcmp(path, "/state") != 0
258283d6f72SJustin T. Gibbs && strcmp(path, "/online") != 0)
259283d6f72SJustin T. Gibbs return;
260283d6f72SJustin T. Gibbs
261283d6f72SJustin T. Gibbs if (xenbus_get_state(child) != XenbusStateClosed
262283d6f72SJustin T. Gibbs || xenbus_dev_is_online(child) != 0)
263283d6f72SJustin T. Gibbs return;
264ff662b5cSJustin T. Gibbs
265ff662b5cSJustin T. Gibbs /*
266ff662b5cSJustin T. Gibbs * Cleanup the hotplug entry in the XenStore if
267ff662b5cSJustin T. Gibbs * present. The control domain expects any userland
268ff662b5cSJustin T. Gibbs * component associated with this device to destroy
269ff662b5cSJustin T. Gibbs * this node in order to signify it is safe to
270ff662b5cSJustin T. Gibbs * teardown the device. However, not all backends
271ff662b5cSJustin T. Gibbs * rely on userland components, and those that
272ff662b5cSJustin T. Gibbs * do should either use a communication channel
273ff662b5cSJustin T. Gibbs * other than the XenStore, or ensure the hotplug
274ff662b5cSJustin T. Gibbs * data is already cleaned up.
275ff662b5cSJustin T. Gibbs *
276ff662b5cSJustin T. Gibbs * This removal ensures that no matter what path
277ff662b5cSJustin T. Gibbs * is taken to mark a back-end closed, the control
278ff662b5cSJustin T. Gibbs * domain will understand that it is closed.
279ff662b5cSJustin T. Gibbs */
280ff662b5cSJustin T. Gibbs xs_rm(XST_NIL, xenbus_get_node(child), "hotplug-status");
281ff662b5cSJustin T. Gibbs }
282ff662b5cSJustin T. Gibbs
283ff662b5cSJustin T. Gibbs /*-------------------- Private Device Attachment Data -----------------------*/
284ff662b5cSJustin T. Gibbs static device_method_t xenbusb_back_methods[] = {
285ff662b5cSJustin T. Gibbs /* Device interface */
286ff662b5cSJustin T. Gibbs DEVMETHOD(device_identify, xenbusb_identify),
287ff662b5cSJustin T. Gibbs DEVMETHOD(device_probe, xenbusb_back_probe),
288ff662b5cSJustin T. Gibbs DEVMETHOD(device_attach, xenbusb_back_attach),
289ff662b5cSJustin T. Gibbs DEVMETHOD(device_detach, bus_generic_detach),
290ff662b5cSJustin T. Gibbs DEVMETHOD(device_shutdown, bus_generic_shutdown),
291ff662b5cSJustin T. Gibbs DEVMETHOD(device_suspend, bus_generic_suspend),
2922ca7463bSJustin T. Gibbs DEVMETHOD(device_resume, xenbusb_resume),
293ff662b5cSJustin T. Gibbs
294ff662b5cSJustin T. Gibbs /* Bus Interface */
295ff662b5cSJustin T. Gibbs DEVMETHOD(bus_print_child, xenbusb_print_child),
296ff662b5cSJustin T. Gibbs DEVMETHOD(bus_read_ivar, xenbusb_read_ivar),
297283d6f72SJustin T. Gibbs DEVMETHOD(bus_write_ivar, xenbusb_write_ivar),
298ff662b5cSJustin T. Gibbs DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
299ff662b5cSJustin T. Gibbs DEVMETHOD(bus_release_resource, bus_generic_release_resource),
300ff662b5cSJustin T. Gibbs DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
301ff662b5cSJustin T. Gibbs DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
302ff662b5cSJustin T. Gibbs
303ff662b5cSJustin T. Gibbs /* XenBus Bus Interface */
304ff662b5cSJustin T. Gibbs DEVMETHOD(xenbusb_enumerate_type, xenbusb_back_enumerate_type),
305ff662b5cSJustin T. Gibbs DEVMETHOD(xenbusb_get_otherend_node, xenbusb_back_get_otherend_node),
306283d6f72SJustin T. Gibbs DEVMETHOD(xenbusb_otherend_changed, xenbusb_back_otherend_changed),
307283d6f72SJustin T. Gibbs DEVMETHOD(xenbusb_localend_changed, xenbusb_back_localend_changed),
308*3e5e0e2fSElliott Mitchell
309*3e5e0e2fSElliott Mitchell DEVMETHOD_END
310ff662b5cSJustin T. Gibbs };
311ff662b5cSJustin T. Gibbs
312ff662b5cSJustin T. Gibbs DEFINE_CLASS_0(xenbusb_back, xenbusb_back_driver, xenbusb_back_methods,
313ff662b5cSJustin T. Gibbs sizeof(struct xenbusb_softc));
314ff662b5cSJustin T. Gibbs
315f929eb1eSJohn Baldwin DRIVER_MODULE(xenbusb_back, xenstore, xenbusb_back_driver, 0, 0);
316