Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / xenbus / xenbus_client.c
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.
5  *
6  * Copyright (C) 2005 XenSource Ltd
7  * 
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:
13  * 
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:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
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
30  * IN THE SOFTWARE.
31  */
32
33 #include <xen/evtchn.h>
34 #include <xen/gnttab.h>
35 #include <xen/xenbus.h>
36 #include <xen/driver_util.h>
37
38 #define DPRINTK(fmt, args...) \
39     pr_debug("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
40
41 char *xenbus_strstate(enum xenbus_state state)
42 {
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",
51         };
52         return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
53 }
54
55 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
56                       struct xenbus_watch *watch,
57                       void (*callback)(struct xenbus_watch *,
58                                        const char **, unsigned int))
59 {
60         int err;
61
62         watch->node = path;
63         watch->callback = callback;
64
65         err = register_xenbus_watch(watch);
66
67         if (err) {
68                 watch->node = NULL;
69                 watch->callback = NULL;
70                 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
71         }
72
73         return err;
74 }
75 EXPORT_SYMBOL_GPL(xenbus_watch_path);
76
77
78 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
79                        const char *path2, struct xenbus_watch *watch,
80                        void (*callback)(struct xenbus_watch *,
81                                         const char **, unsigned int))
82 {
83         int err;
84         char *state = kasprintf(GFP_KERNEL, "%s/%s", path, path2);
85         if (!state) {
86                 xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
87                 return -ENOMEM;
88         }
89         err = xenbus_watch_path(dev, state, watch, callback);
90
91         if (err)
92                 kfree(state);
93         return err;
94 }
95 EXPORT_SYMBOL_GPL(xenbus_watch_path2);
96
97
98 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
99 {
100         /* We check whether the state is currently set to the given value, and
101            if not, then the state is set.  We don't want to unconditionally
102            write the given state, because we don't want to fire watches
103            unnecessarily.  Furthermore, if the node has gone, we don't write
104            to it, as the device will be tearing down, and we don't want to
105            resurrect that directory.
106
107            Note that, because of this cached value of our state, this function
108            will not work inside a Xenstore transaction (something it was
109            trying to in the past) because dev->state would not get reset if
110            the transaction was aborted.
111
112          */
113
114         int current_state;
115         int err;
116
117         if (state == dev->state)
118                 return 0;
119
120         err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
121                            &current_state);
122         if (err != 1)
123                 return 0;
124
125         err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
126         if (err) {
127                 if (state != XenbusStateClosing) /* Avoid looping */
128                         xenbus_dev_fatal(dev, err, "writing new state");
129                 return err;
130         }
131
132         dev->state = state;
133
134         return 0;
135 }
136 EXPORT_SYMBOL_GPL(xenbus_switch_state);
137
138 int xenbus_frontend_closed(struct xenbus_device *dev)
139 {
140         xenbus_switch_state(dev, XenbusStateClosed);
141         complete(&dev->down);
142         return 0;
143 }
144 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
145
146 /**
147  * Return the path to the error node for the given device, or NULL on failure.
148  * If the value returned is non-NULL, then it is the caller's to kfree.
149  */
150 static char *error_path(struct xenbus_device *dev)
151 {
152         return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
153 }
154
155
156 void _dev_error(struct xenbus_device *dev, int err, const char *fmt,
157                 va_list ap)
158 {
159         int ret;
160         unsigned int len;
161         char *printf_buffer = NULL, *path_buffer = NULL;
162
163 #define PRINTF_BUFFER_SIZE 4096
164         printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
165         if (printf_buffer == NULL)
166                 goto fail;
167
168         len = sprintf(printf_buffer, "%i ", -err);
169         ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
170
171         BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
172
173         dev_err(&dev->dev, "%s\n", printf_buffer);
174
175         path_buffer = error_path(dev);
176
177         if (path_buffer == NULL) {
178                 printk("xenbus: failed to write error node for %s (%s)\n",
179                        dev->nodename, printf_buffer);
180                 goto fail;
181         }
182
183         if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
184                 printk("xenbus: failed to write error node for %s (%s)\n",
185                        dev->nodename, printf_buffer);
186                 goto fail;
187         }
188
189 fail:
190         if (printf_buffer)
191                 kfree(printf_buffer);
192         if (path_buffer)
193                 kfree(path_buffer);
194 }
195
196
197 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
198                       ...)
199 {
200         va_list ap;
201
202         va_start(ap, fmt);
203         _dev_error(dev, err, fmt, ap);
204         va_end(ap);
205 }
206 EXPORT_SYMBOL_GPL(xenbus_dev_error);
207
208
209 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
210                       ...)
211 {
212         va_list ap;
213
214         va_start(ap, fmt);
215         _dev_error(dev, err, fmt, ap);
216         va_end(ap);
217
218         xenbus_switch_state(dev, XenbusStateClosing);
219 }
220 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
221
222
223 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
224 {
225         int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
226         if (err < 0)
227                 xenbus_dev_fatal(dev, err, "granting access to ring page");
228         return err;
229 }
230 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
231
232
233 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
234 {
235         struct evtchn_alloc_unbound alloc_unbound;
236         int err;
237
238         alloc_unbound.dom        = DOMID_SELF;
239         alloc_unbound.remote_dom = dev->otherend_id;
240
241         err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
242                                           &alloc_unbound);
243         if (err)
244                 xenbus_dev_fatal(dev, err, "allocating event channel");
245         else
246                 *port = alloc_unbound.port;
247
248         return err;
249 }
250 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
251
252
253 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
254 {
255         struct evtchn_bind_interdomain bind_interdomain;
256         int err;
257
258         bind_interdomain.remote_dom  = dev->otherend_id;
259         bind_interdomain.remote_port = remote_port,
260
261         err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
262                                           &bind_interdomain);
263         if (err)
264                 xenbus_dev_fatal(dev, err,
265                                  "binding to event channel %d from domain %d",
266                                  remote_port, dev->otherend_id);
267         else
268                 *port = bind_interdomain.local_port;
269
270         return err;
271 }
272 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
273
274
275 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
276 {
277         struct evtchn_close close;
278         int err;
279
280         close.port = port;
281
282         err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
283         if (err)
284                 xenbus_dev_error(dev, err, "freeing event channel %d", port);
285
286         return err;
287 }
288
289
290 enum xenbus_state xenbus_read_driver_state(const char *path)
291 {
292         enum xenbus_state result;
293         int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
294         if (err)
295                 result = XenbusStateUnknown;
296
297         return result;
298 }
299 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);