]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/xen/xenbus/xenbus_client.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / 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 file may be distributed separately from the Linux kernel, or
9  * incorporated into other software packages, subject to the following license:
10  * 
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this source file (the "Software"), to deal in the Software without
13  * restriction, including without limitation the rights to use, copy, modify,
14  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15  * and to permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  * 
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  * 
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27  * IN THE SOFTWARE.
28  */
29
30
31 #if 0
32 #define DPRINTK(fmt, args...) \
33     printk("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
34 #else
35 #define DPRINTK(fmt, args...) ((void)0)
36 #endif
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
43 #include <sys/malloc.h>
44 #include <sys/libkern.h>
45
46 #include <machine/xen/xen-os.h>
47 #include <xen/hypervisor.h>
48 #include <xen/evtchn.h>
49 #include <xen/gnttab.h>
50 #include <xen/xenbus/xenbusvar.h>
51 #include <machine/stdarg.h>
52
53 const char *
54 xenbus_strstate(XenbusState state)
55 {
56         static const char *const name[] = {
57                 [ XenbusStateUnknown      ] = "Unknown",
58                 [ XenbusStateInitialising ] = "Initialising",
59                 [ XenbusStateInitWait     ] = "InitWait",
60                 [ XenbusStateInitialised  ] = "Initialised",
61                 [ XenbusStateConnected    ] = "Connected",
62                 [ XenbusStateClosing      ] = "Closing",
63                 [ XenbusStateClosed       ] = "Closed",
64         };
65
66         return ((state < (XenbusStateClosed + 1)) ? name[state] : "INVALID");
67 }
68
69 int 
70 xenbus_watch_path(device_t dev, char *path, struct xenbus_watch *watch, 
71     void (*callback)(struct xenbus_watch *, const char **, unsigned int))
72 {
73         int error;
74
75         watch->node = path;
76         watch->callback = callback;
77
78         error = register_xenbus_watch(watch);
79
80         if (error) {
81                 watch->node = NULL;
82                 watch->callback = NULL;
83                 xenbus_dev_fatal(dev, error, "adding watch on %s", path);
84         }
85
86         return (error);
87 }
88
89 int
90 xenbus_watch_path2(device_t dev, const char *path,
91     const char *path2, struct xenbus_watch *watch, 
92     void (*callback)(struct xenbus_watch *, const char **, unsigned int))
93 {
94         int error;
95         char *state = malloc(strlen(path) + 1 + strlen(path2) + 1,
96             M_DEVBUF, M_WAITOK);
97
98         strcpy(state, path);
99         strcat(state, "/");
100         strcat(state, path2);
101
102         error = xenbus_watch_path(dev, state, watch, callback);
103         if (error) {
104                 free(state, M_DEVBUF);
105         }
106
107         return (error);
108 }
109
110 /**
111  * Return the path to the error node for the given device, or NULL on failure.
112  * If the value returned is non-NULL, then it is the caller's to kfree.
113  */
114 static char *
115 error_path(device_t dev)
116 {
117         char *path_buffer = malloc(strlen("error/")
118             + strlen(xenbus_get_node(dev)) + 1, M_DEVBUF, M_WAITOK);
119
120         strcpy(path_buffer, "error/");
121         strcpy(path_buffer + strlen("error/"), xenbus_get_node(dev));
122
123         return (path_buffer);
124 }
125
126
127 static void
128 _dev_error(device_t dev, int err, const char *fmt, va_list ap)
129 {
130         int ret;
131         unsigned int len;
132         char *printf_buffer = NULL, *path_buffer = NULL;
133
134 #define PRINTF_BUFFER_SIZE 4096
135         printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_WAITOK);
136
137         len = sprintf(printf_buffer, "%i ", err);
138         ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
139
140         KASSERT(len + ret <= PRINTF_BUFFER_SIZE-1, ("xenbus error message too big"));
141 #if 0   
142         dev_err(&dev->dev, "%s\n", printf_buffer);
143 #endif          
144         path_buffer = error_path(dev);
145
146         if (path_buffer == NULL) {
147                 printf("xenbus: failed to write error node for %s (%s)\n",
148                        xenbus_get_node(dev), printf_buffer);
149                 goto fail;
150         }
151
152         if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
153                 printf("xenbus: failed to write error node for %s (%s)\n",
154                        xenbus_get_node(dev), printf_buffer);
155                 goto fail;
156         }
157
158  fail:
159         if (printf_buffer)
160                 free(printf_buffer, M_DEVBUF);
161         if (path_buffer)
162                 free(path_buffer, M_DEVBUF);
163 }
164
165 void
166 xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
167 {
168         va_list ap;
169
170         va_start(ap, fmt);
171         _dev_error(dev, err, fmt, ap);
172         va_end(ap);
173 }
174
175 void
176 xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
177 {
178         va_list ap;
179
180         va_start(ap, fmt);
181         _dev_error(dev, err, fmt, ap);
182         va_end(ap);
183         
184         xenbus_set_state(dev, XenbusStateClosing);
185 }
186
187 int
188 xenbus_grant_ring(device_t dev, unsigned long ring_mfn, int *refp)
189 {
190         int error;
191         grant_ref_t ref;
192
193         error = gnttab_grant_foreign_access(
194                 xenbus_get_otherend_id(dev), ring_mfn, 0, &ref);
195         if (error) {
196                 xenbus_dev_fatal(dev, error, "granting access to ring page");
197                 return (error);
198         }
199
200         *refp = ref;
201         return (0);
202 }
203
204 int
205 xenbus_alloc_evtchn(device_t dev, int *port)
206 {
207         struct evtchn_alloc_unbound alloc_unbound;
208         int err;
209
210         alloc_unbound.dom        = DOMID_SELF;
211         alloc_unbound.remote_dom = xenbus_get_otherend_id(dev);
212
213         err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
214                                           &alloc_unbound);
215
216         if (err) {
217                 xenbus_dev_fatal(dev, -err, "allocating event channel");
218                 return (-err);
219         }
220         *port = alloc_unbound.port;
221         return (0);
222 }
223
224 int
225 xenbus_free_evtchn(device_t dev, int port)
226 {
227         struct evtchn_close close;
228         int err;
229
230         close.port = port;
231
232         err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
233         if (err) {
234                 xenbus_dev_error(dev, -err, "freeing event channel %d", port);
235                 return (-err);
236         }
237         return (0);
238 }
239
240 XenbusState
241 xenbus_read_driver_state(const char *path)
242 {
243         XenbusState result;
244         int error;
245
246         error = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
247         if (error)
248                 result = XenbusStateClosed;
249
250         return (result);
251 }