1 /******************************************************************************
2 * Copyright (C) 2005 XenSource Ltd
3 *
4 * This file may be distributed separately from the Linux kernel, or
5 * incorporated into other software packages, subject to the following license:
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this source file (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy, modify,
10 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 /**
27 * \file xenbus.c
28 *
29 * \brief Client-facing interface for the Xenbus driver.
30 *
31 * In other words, the interface between the Xenbus and the device-specific
32 * code, be it the frontend or the backend of that driver.
33 */
34
35 #if 0
36 #define DPRINTK(fmt, args...) \
37 printk("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
38 #else
39 #define DPRINTK(fmt, args...) ((void)0)
40 #endif
41
42 #include <sys/cdefs.h>
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/types.h>
47 #include <sys/malloc.h>
48 #include <sys/libkern.h>
49 #include <sys/sbuf.h>
50 #include <sys/stdarg.h>
51
52 #include <xen/xen-os.h>
53 #include <xen/hypervisor.h>
54 #include <xen/evtchn.h>
55 #include <xen/gnttab.h>
56 #include <xen/xenbus/xenbusvar.h>
57
58 MALLOC_DEFINE(M_XENBUS, "xenbus", "XenBus Support");
59
60 /*------------------------- Private Functions --------------------------------*/
61 /**
62 * \brief Construct the error path corresponding to the given XenBus
63 * device.
64 *
65 * \param dev The XenBus device for which we are constructing an error path.
66 *
67 * \return On success, the contructed error path. Otherwise NULL.
68 *
69 * It is the caller's responsibility to free any returned error path
70 * node using the M_XENBUS malloc type.
71 */
72 static char *
error_path(device_t dev)73 error_path(device_t dev)
74 {
75 char *path_buffer = malloc(strlen("error/")
76 + strlen(xenbus_get_node(dev)) + 1,M_XENBUS, M_WAITOK);
77
78 strcpy(path_buffer, "error/");
79 strcpy(path_buffer + strlen("error/"), xenbus_get_node(dev));
80
81 return (path_buffer);
82 }
83
84 /*--------------------------- Public Functions -------------------------------*/
85 /*-------- API comments for these methods can be found in xenbusvar.h --------*/
86 const char *
xenbus_strstate(XenbusState state)87 xenbus_strstate(XenbusState state)
88 {
89 static const char *const name[] = {
90 [ XenbusStateUnknown ] = "Unknown",
91 [ XenbusStateInitialising ] = "Initialising",
92 [ XenbusStateInitWait ] = "InitWait",
93 [ XenbusStateInitialised ] = "Initialised",
94 [ XenbusStateConnected ] = "Connected",
95 [ XenbusStateClosing ] = "Closing",
96 [ XenbusStateClosed ] = "Closed",
97 };
98
99 return ((state < (XenbusStateClosed + 1)) ? name[state] : "INVALID");
100 }
101
102 void
xenbus_dev_verror(device_t dev,int err,const char * fmt,va_list ap)103 xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
104 {
105 int ret __diagused;
106 unsigned int len;
107 char *printf_buffer = NULL, *path_buffer = NULL;
108
109 #define PRINTF_BUFFER_SIZE 4096
110 printf_buffer = malloc(PRINTF_BUFFER_SIZE,M_XENBUS, M_WAITOK);
111
112 len = sprintf(printf_buffer, "%i ", err);
113 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
114
115 KASSERT(len + ret <= PRINTF_BUFFER_SIZE-1, ("xenbus error message too big"));
116 device_printf(dev, "Error %s\n", printf_buffer);
117 path_buffer = error_path(dev);
118
119 if (path_buffer == NULL) {
120 printf("xenbus: failed to write error node for %s (%s)\n",
121 xenbus_get_node(dev), printf_buffer);
122 goto fail;
123 }
124
125 if (xs_write(XST_NIL, path_buffer, "error", printf_buffer) != 0) {
126 printf("xenbus: failed to write error node for %s (%s)\n",
127 xenbus_get_node(dev), printf_buffer);
128 goto fail;
129 }
130
131 fail:
132 if (printf_buffer)
133 free(printf_buffer,M_XENBUS);
134 if (path_buffer)
135 free(path_buffer,M_XENBUS);
136 }
137
138 void
xenbus_dev_error(device_t dev,int err,const char * fmt,...)139 xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
140 {
141 va_list ap;
142
143 va_start(ap, fmt);
144 xenbus_dev_verror(dev, err, fmt, ap);
145 va_end(ap);
146 }
147
148 void
xenbus_dev_vfatal(device_t dev,int err,const char * fmt,va_list ap)149 xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list ap)
150 {
151 xenbus_dev_verror(dev, err, fmt, ap);
152 device_printf(dev, "Fatal error. Transitioning to Closing State\n");
153 xenbus_set_state(dev, XenbusStateClosing);
154 }
155
156 void
xenbus_dev_fatal(device_t dev,int err,const char * fmt,...)157 xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
158 {
159 va_list ap;
160
161 va_start(ap, fmt);
162 xenbus_dev_vfatal(dev, err, fmt, ap);
163 va_end(ap);
164 }
165
166 int
xenbus_grant_ring(device_t dev,unsigned long ring_mfn,grant_ref_t * refp)167 xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp)
168 {
169 int error;
170
171 error = gnttab_grant_foreign_access(
172 xenbus_get_otherend_id(dev), ring_mfn, 0, refp);
173 if (error) {
174 xenbus_dev_fatal(dev, error, "granting access to ring page");
175 return (error);
176 }
177
178 return (0);
179 }
180
181 XenbusState
xenbus_read_driver_state(const char * path)182 xenbus_read_driver_state(const char *path)
183 {
184 XenbusState result;
185 int error;
186
187 error = xs_gather(XST_NIL, path, "state", "%d", &result, NULL);
188 if (error)
189 result = XenbusStateClosed;
190
191 return (result);
192 }
193
194 int
xenbus_dev_is_online(device_t dev)195 xenbus_dev_is_online(device_t dev)
196 {
197 const char *path;
198 int error;
199 int value;
200
201 path = xenbus_get_node(dev);
202 error = xs_gather(XST_NIL, path, "online", "%d", &value, NULL);
203 if (error != 0) {
204 /* Default to not online. */
205 value = 0;
206 }
207
208 return (value);
209 }
210
211 void
xenbus_localend_changed(device_t dev,const char * path)212 xenbus_localend_changed(device_t dev, const char *path)
213 {
214 }
215