xref: /freebsd/sys/xen/xenbus/xenbus.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
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 __FBSDID("$FreeBSD$");
44 
45 #include <sys/cdefs.h>
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/types.h>
49 #include <sys/malloc.h>
50 #include <sys/libkern.h>
51 #include <sys/sbuf.h>
52 
53 #include <xen/xen-os.h>
54 #include <xen/hypervisor.h>
55 #include <xen/evtchn.h>
56 #include <xen/gnttab.h>
57 #include <xen/xenbus/xenbusvar.h>
58 
59 #include <machine/stdarg.h>
60 
61 MALLOC_DEFINE(M_XENBUS, "xenbus", "XenBus Support");
62 
63 /*------------------------- Private Functions --------------------------------*/
64 /**
65  * \brief Construct the error path corresponding to the given XenBus
66  *        device.
67  *
68  * \param dev  The XenBus device for which we are constructing an error path.
69  *
70  * \return  On success, the contructed error path.  Otherwise NULL.
71  *
72  * It is the caller's responsibility to free any returned error path
73  * node using the M_XENBUS malloc type.
74  */
75 static char *
76 error_path(device_t dev)
77 {
78 	char *path_buffer = malloc(strlen("error/")
79 	    + strlen(xenbus_get_node(dev)) + 1,M_XENBUS, M_WAITOK);
80 
81 	strcpy(path_buffer, "error/");
82 	strcpy(path_buffer + strlen("error/"), xenbus_get_node(dev));
83 
84 	return (path_buffer);
85 }
86 
87 /*--------------------------- Public Functions -------------------------------*/
88 /*-------- API comments for these methods can be found in xenbusvar.h --------*/
89 const char *
90 xenbus_strstate(XenbusState state)
91 {
92 	static const char *const name[] = {
93 		[ XenbusStateUnknown      ] = "Unknown",
94 		[ XenbusStateInitialising ] = "Initialising",
95 		[ XenbusStateInitWait     ] = "InitWait",
96 		[ XenbusStateInitialised  ] = "Initialised",
97 		[ XenbusStateConnected    ] = "Connected",
98 		[ XenbusStateClosing      ] = "Closing",
99 		[ XenbusStateClosed	  ] = "Closed",
100 	};
101 
102 	return ((state < (XenbusStateClosed + 1)) ? name[state] : "INVALID");
103 }
104 
105 void
106 xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
107 {
108 	int ret;
109 	unsigned int len;
110 	char *printf_buffer = NULL, *path_buffer = NULL;
111 
112 #define PRINTF_BUFFER_SIZE 4096
113 	printf_buffer = malloc(PRINTF_BUFFER_SIZE,M_XENBUS, M_WAITOK);
114 
115 	len = sprintf(printf_buffer, "%i ", err);
116 	ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
117 
118 	KASSERT(len + ret <= PRINTF_BUFFER_SIZE-1, ("xenbus error message too big"));
119 	device_printf(dev, "Error %s\n", printf_buffer);
120 	path_buffer = error_path(dev);
121 
122 	if (path_buffer == NULL) {
123 		printf("xenbus: failed to write error node for %s (%s)\n",
124 		       xenbus_get_node(dev), printf_buffer);
125 		goto fail;
126 	}
127 
128 	if (xs_write(XST_NIL, path_buffer, "error", printf_buffer) != 0) {
129 		printf("xenbus: failed to write error node for %s (%s)\n",
130 		       xenbus_get_node(dev), printf_buffer);
131 		goto fail;
132 	}
133 
134  fail:
135 	if (printf_buffer)
136 		free(printf_buffer,M_XENBUS);
137 	if (path_buffer)
138 		free(path_buffer,M_XENBUS);
139 }
140 
141 void
142 xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
143 {
144 	va_list ap;
145 
146 	va_start(ap, fmt);
147 	xenbus_dev_verror(dev, err, fmt, ap);
148 	va_end(ap);
149 }
150 
151 void
152 xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list ap)
153 {
154 	xenbus_dev_verror(dev, err, fmt, ap);
155 	device_printf(dev, "Fatal error. Transitioning to Closing State\n");
156 	xenbus_set_state(dev, XenbusStateClosing);
157 }
158 
159 void
160 xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
161 {
162 	va_list ap;
163 
164 	va_start(ap, fmt);
165 	xenbus_dev_vfatal(dev, err, fmt, ap);
166 	va_end(ap);
167 }
168 
169 int
170 xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp)
171 {
172 	int error;
173 
174 	error = gnttab_grant_foreign_access(
175 		xenbus_get_otherend_id(dev), ring_mfn, 0, refp);
176 	if (error) {
177 		xenbus_dev_fatal(dev, error, "granting access to ring page");
178 		return (error);
179 	}
180 
181 	return (0);
182 }
183 
184 XenbusState
185 xenbus_read_driver_state(const char *path)
186 {
187         XenbusState result;
188         int error;
189 
190         error = xs_gather(XST_NIL, path, "state", "%d", &result, NULL);
191         if (error)
192                 result = XenbusStateClosed;
193 
194         return (result);
195 }
196 
197 int
198 xenbus_dev_is_online(device_t dev)
199 {
200 	const char *path;
201 	int error;
202 	int value;
203 
204 	path = xenbus_get_node(dev);
205 	error = xs_gather(XST_NIL, path, "online", "%d", &value, NULL);
206 	if (error != 0) {
207 		/* Default to not online. */
208 		value = 0;
209 	}
210 
211 	return (value);
212 }
213 
214 void
215 xenbus_localend_changed(device_t dev, const char *path)
216 {
217 }
218