1 /******************************************************************************
2 * Client-facing interface for the Xenbus driver. In other words, the
3 * interface between the Xenbus and the device-specific code, be it the
4 * frontend or the backend of that driver.
6 * Copyright (C) 2005 XenSource Ltd
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <xen/evtchn.h>
34 #include <xen/gnttab.h>
35 #include <xen/xenbus.h>
36 #include <xen/driver_util.h>
38 #define DPRINTK(fmt, args...) \
39 pr_debug("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
41 char *xenbus_strstate(enum xenbus_state state)
43 static char *name[] = {
44 [ XenbusStateUnknown ] = "Unknown",
45 [ XenbusStateInitialising ] = "Initialising",
46 [ XenbusStateInitWait ] = "InitWait",
47 [ XenbusStateInitialised ] = "Initialised",
48 [ XenbusStateConnected ] = "Connected",
49 [ XenbusStateClosing ] = "Closing",
50 [ XenbusStateClosed ] = "Closed",
52 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
54 EXPORT_SYMBOL_GPL(xenbus_strstate);
56 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
57 struct xenbus_watch *watch,
58 void (*callback)(struct xenbus_watch *,
59 const char **, unsigned int))
64 watch->callback = callback;
66 err = register_xenbus_watch(watch);
70 watch->callback = NULL;
71 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
76 EXPORT_SYMBOL_GPL(xenbus_watch_path);
79 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
80 const char *path2, struct xenbus_watch *watch,
81 void (*callback)(struct xenbus_watch *,
82 const char **, unsigned int))
85 char *state = kasprintf(GFP_KERNEL, "%s/%s", path, path2);
87 xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
90 err = xenbus_watch_path(dev, state, watch, callback);
96 EXPORT_SYMBOL_GPL(xenbus_watch_path2);
99 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
101 /* We check whether the state is currently set to the given value, and
102 if not, then the state is set. We don't want to unconditionally
103 write the given state, because we don't want to fire watches
104 unnecessarily. Furthermore, if the node has gone, we don't write
105 to it, as the device will be tearing down, and we don't want to
106 resurrect that directory.
108 Note that, because of this cached value of our state, this function
109 will not work inside a Xenstore transaction (something it was
110 trying to in the past) because dev->state would not get reset if
111 the transaction was aborted.
118 if (state == dev->state)
121 err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
126 err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
128 if (state != XenbusStateClosing) /* Avoid looping */
129 xenbus_dev_fatal(dev, err, "writing new state");
137 EXPORT_SYMBOL_GPL(xenbus_switch_state);
139 int xenbus_frontend_closed(struct xenbus_device *dev)
141 xenbus_switch_state(dev, XenbusStateClosed);
142 complete(&dev->down);
145 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
148 * Return the path to the error node for the given device, or NULL on failure.
149 * If the value returned is non-NULL, then it is the caller's to kfree.
151 static char *error_path(struct xenbus_device *dev)
153 return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
157 void _dev_error(struct xenbus_device *dev, int err, const char *fmt,
162 char *printf_buffer = NULL, *path_buffer = NULL;
164 #define PRINTF_BUFFER_SIZE 4096
165 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
166 if (printf_buffer == NULL)
169 len = sprintf(printf_buffer, "%i ", -err);
170 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
172 BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
174 dev_err(&dev->dev, "%s\n", printf_buffer);
176 path_buffer = error_path(dev);
178 if (path_buffer == NULL) {
179 printk("xenbus: failed to write error node for %s (%s)\n",
180 dev->nodename, printf_buffer);
184 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
185 printk("xenbus: failed to write error node for %s (%s)\n",
186 dev->nodename, printf_buffer);
192 kfree(printf_buffer);
198 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
204 _dev_error(dev, err, fmt, ap);
207 EXPORT_SYMBOL_GPL(xenbus_dev_error);
210 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
216 _dev_error(dev, err, fmt, ap);
219 xenbus_switch_state(dev, XenbusStateClosing);
221 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
224 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
226 int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
228 xenbus_dev_fatal(dev, err, "granting access to ring page");
231 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
234 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
236 struct evtchn_alloc_unbound alloc_unbound;
239 alloc_unbound.dom = DOMID_SELF;
240 alloc_unbound.remote_dom = dev->otherend_id;
242 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
245 xenbus_dev_fatal(dev, err, "allocating event channel");
247 *port = alloc_unbound.port;
251 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
254 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
256 struct evtchn_bind_interdomain bind_interdomain;
259 bind_interdomain.remote_dom = dev->otherend_id;
260 bind_interdomain.remote_port = remote_port,
262 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
265 xenbus_dev_fatal(dev, err,
266 "binding to event channel %d from domain %d",
267 remote_port, dev->otherend_id);
269 *port = bind_interdomain.local_port;
273 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
276 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
278 struct evtchn_close close;
283 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
285 xenbus_dev_error(dev, err, "freeing event channel %d", port);
291 enum xenbus_state xenbus_read_driver_state(const char *path)
293 enum xenbus_state result;
294 int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
296 result = XenbusStateUnknown;
300 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);