Deleted Added
full compact
xenbus.c (181912) xenbus.c (185605)
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

--- 22 unchanged lines hidden (view full) ---

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>
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

--- 22 unchanged lines hidden (view full) ---

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: head/sys/xen/xenbus/xenbus_client.c 181912 2008-08-20 09:20:12Z kmacy $");
39__FBSDID("$FreeBSD: head/sys/xen/xenbus/xenbus_client.c 185605 2008-12-04 07:59:05Z kmacy $");
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 <machine/xen/evtchn.h>
48#include <xen/gnttab.h>
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 <machine/xen/evtchn.h>
48#include <xen/gnttab.h>
49#include <machine/xen/xenbus.h>
49#include <xen/xenbus/xenbusvar.h>
50#include <machine/stdarg.h>
51
52
53#define EXPORT_SYMBOL(x)
54#define kmalloc(size, unused) malloc(size, M_DEVBUF, M_WAITOK)
55#define kfree(ptr) free(ptr, M_DEVBUF)
56#define BUG_ON PANIC_IF
57

--- 8 unchanged lines hidden (view full) ---

66 [ XenbusStateConnected ] = "Connected",
67 [ XenbusStateClosing ] = "Closing",
68 [ XenbusStateClosed ] = "Closed",
69 };
70 return (state < (XenbusStateClosed + 1)) ? name[state] : "INVALID";
71}
72
73int
50#include <machine/stdarg.h>
51
52
53#define EXPORT_SYMBOL(x)
54#define kmalloc(size, unused) malloc(size, M_DEVBUF, M_WAITOK)
55#define kfree(ptr) free(ptr, M_DEVBUF)
56#define BUG_ON PANIC_IF
57

--- 8 unchanged lines hidden (view full) ---

66 [ XenbusStateConnected ] = "Connected",
67 [ XenbusStateClosing ] = "Closing",
68 [ XenbusStateClosed ] = "Closed",
69 };
70 return (state < (XenbusStateClosed + 1)) ? name[state] : "INVALID";
71}
72
73int
74xenbus_watch_path(struct xenbus_device *dev, char *path,
74xenbus_watch_path(device_t dev, char *path,
75 struct xenbus_watch *watch,
76 void (*callback)(struct xenbus_watch *,
77 const char **, unsigned int))
78{
79 int err;
80
81 watch->node = path;
82 watch->callback = callback;

--- 6 unchanged lines hidden (view full) ---

89 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
90 }
91
92 return err;
93}
94EXPORT_SYMBOL(xenbus_watch_path);
95
96
75 struct xenbus_watch *watch,
76 void (*callback)(struct xenbus_watch *,
77 const char **, unsigned int))
78{
79 int err;
80
81 watch->node = path;
82 watch->callback = callback;

--- 6 unchanged lines hidden (view full) ---

89 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
90 }
91
92 return err;
93}
94EXPORT_SYMBOL(xenbus_watch_path);
95
96
97int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
97int xenbus_watch_path2(device_t dev, const char *path,
98 const char *path2, struct xenbus_watch *watch,
99 void (*callback)(struct xenbus_watch *,
100 const char **, unsigned int))
101{
102 int err;
103 char *state =
104 kmalloc(strlen(path) + 1 + strlen(path2) + 1, GFP_KERNEL);
105 if (!state) {

--- 8 unchanged lines hidden (view full) ---

114
115 if (err) {
116 kfree(state);
117 }
118 return err;
119}
120EXPORT_SYMBOL(xenbus_watch_path2);
121
98 const char *path2, struct xenbus_watch *watch,
99 void (*callback)(struct xenbus_watch *,
100 const char **, unsigned int))
101{
102 int err;
103 char *state =
104 kmalloc(strlen(path) + 1 + strlen(path2) + 1, GFP_KERNEL);
105 if (!state) {

--- 8 unchanged lines hidden (view full) ---

114
115 if (err) {
116 kfree(state);
117 }
118 return err;
119}
120EXPORT_SYMBOL(xenbus_watch_path2);
121
122
123int xenbus_switch_state(struct xenbus_device *dev,
124 XenbusState state)
125{
126 /* We check whether the state is currently set to the given value, and
127 if not, then the state is set. We don't want to unconditionally
128 write the given state, because we don't want to fire watches
129 unnecessarily. Furthermore, if the node has gone, we don't write
130 to it, as the device will be tearing down, and we don't want to
131 resurrect that directory.
132 */
133
134 int current_state;
135 int err;
136
137 if (state == dev->state)
138 return (0);
139
140 err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
141 &current_state);
142 if (err != 1)
143 return 0;
144
145 err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
146 if (err) {
147 if (state != XenbusStateClosing) /* Avoid looping */
148 xenbus_dev_fatal(dev, err, "writing new state");
149 return err;
150 }
151
152 dev->state = state;
153 return 0;
154
155}
156
157int xenbus_frontend_closed(struct xenbus_device *dev)
158{
159 xenbus_switch_state(dev, XenbusStateClosed);
160#if 0
161 complete(&dev->down);
162#endif
163 return 0;
164}
165
166/**
167 * Return the path to the error node for the given device, or NULL on failure.
168 * If the value returned is non-NULL, then it is the caller's to kfree.
169 */
122/**
123 * Return the path to the error node for the given device, or NULL on failure.
124 * If the value returned is non-NULL, then it is the caller's to kfree.
125 */
170static char *error_path(struct xenbus_device *dev)
126static char *error_path(device_t dev)
171{
127{
172 char *path_buffer = kmalloc(strlen("error/") + strlen(dev->nodename) +
128 char *path_buffer = kmalloc(strlen("error/")
129 + strlen(xenbus_get_node(dev)) +
173 1, GFP_KERNEL);
174 if (path_buffer == NULL) {
175 return NULL;
176 }
177
178 strcpy(path_buffer, "error/");
130 1, GFP_KERNEL);
131 if (path_buffer == NULL) {
132 return NULL;
133 }
134
135 strcpy(path_buffer, "error/");
179 strcpy(path_buffer + strlen("error/"), dev->nodename);
136 strcpy(path_buffer + strlen("error/"), xenbus_get_node(dev));
180
181 return path_buffer;
182}
183
184
137
138 return path_buffer;
139}
140
141
185static void _dev_error(struct xenbus_device *dev, int err, const char *fmt,
142static void _dev_error(device_t dev, int err, const char *fmt,
186 va_list ap)
187{
188 int ret;
189 unsigned int len;
190 char *printf_buffer = NULL, *path_buffer = NULL;
191
192#define PRINTF_BUFFER_SIZE 4096
193 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);

--- 6 unchanged lines hidden (view full) ---

200 BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
201#if 0
202 dev_err(&dev->dev, "%s\n", printf_buffer);
203#endif
204 path_buffer = error_path(dev);
205
206 if (path_buffer == NULL) {
207 printk("xenbus: failed to write error node for %s (%s)\n",
143 va_list ap)
144{
145 int ret;
146 unsigned int len;
147 char *printf_buffer = NULL, *path_buffer = NULL;
148
149#define PRINTF_BUFFER_SIZE 4096
150 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);

--- 6 unchanged lines hidden (view full) ---

157 BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
158#if 0
159 dev_err(&dev->dev, "%s\n", printf_buffer);
160#endif
161 path_buffer = error_path(dev);
162
163 if (path_buffer == NULL) {
164 printk("xenbus: failed to write error node for %s (%s)\n",
208 dev->nodename, printf_buffer);
165 xenbus_get_node(dev), printf_buffer);
209 goto fail;
210 }
211
212 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
213 printk("xenbus: failed to write error node for %s (%s)\n",
166 goto fail;
167 }
168
169 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
170 printk("xenbus: failed to write error node for %s (%s)\n",
214 dev->nodename, printf_buffer);
171 xenbus_get_node(dev), printf_buffer);
215 goto fail;
216 }
217
218 fail:
219 if (printf_buffer)
220 kfree(printf_buffer);
221 if (path_buffer)
222 kfree(path_buffer);
223}
224
225
172 goto fail;
173 }
174
175 fail:
176 if (printf_buffer)
177 kfree(printf_buffer);
178 if (path_buffer)
179 kfree(path_buffer);
180}
181
182
226void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
183void xenbus_dev_error(device_t dev, int err, const char *fmt,
227 ...)
228{
229 va_list ap;
230
231 va_start(ap, fmt);
232 _dev_error(dev, err, fmt, ap);
233 va_end(ap);
234}
235EXPORT_SYMBOL(xenbus_dev_error);
236
237
184 ...)
185{
186 va_list ap;
187
188 va_start(ap, fmt);
189 _dev_error(dev, err, fmt, ap);
190 va_end(ap);
191}
192EXPORT_SYMBOL(xenbus_dev_error);
193
194
238void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
195void xenbus_dev_fatal(device_t dev, int err, const char *fmt,
239 ...)
240{
241 va_list ap;
242
243 va_start(ap, fmt);
244 _dev_error(dev, err, fmt, ap);
245 va_end(ap);
246
196 ...)
197{
198 va_list ap;
199
200 va_start(ap, fmt);
201 _dev_error(dev, err, fmt, ap);
202 va_end(ap);
203
247 xenbus_switch_state(dev, XenbusStateClosing);
204 xenbus_set_state(dev, XenbusStateClosing);
248}
249EXPORT_SYMBOL(xenbus_dev_fatal);
250
251
205}
206EXPORT_SYMBOL(xenbus_dev_fatal);
207
208
252int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
209int xenbus_grant_ring(device_t dev, unsigned long ring_mfn)
253{
210{
254 int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
211 int err = gnttab_grant_foreign_access(
212 xenbus_get_otherend_id(dev), ring_mfn, 0);
255 if (err < 0)
256 xenbus_dev_fatal(dev, err, "granting access to ring page");
257 return err;
258}
259EXPORT_SYMBOL(xenbus_grant_ring);
260
261
213 if (err < 0)
214 xenbus_dev_fatal(dev, err, "granting access to ring page");
215 return err;
216}
217EXPORT_SYMBOL(xenbus_grant_ring);
218
219
262int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
220int xenbus_alloc_evtchn(device_t dev, int *port)
263{
264 struct evtchn_alloc_unbound alloc_unbound;
265 int err;
266
267 alloc_unbound.dom = DOMID_SELF;
221{
222 struct evtchn_alloc_unbound alloc_unbound;
223 int err;
224
225 alloc_unbound.dom = DOMID_SELF;
268 alloc_unbound.remote_dom = dev->otherend_id;
226 alloc_unbound.remote_dom = xenbus_get_otherend_id(dev);
269
270 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
271 &alloc_unbound);
272
273 if (err)
274 xenbus_dev_fatal(dev, err, "allocating event channel");
275 else
276 *port = alloc_unbound.port;
277 return err;
278}
279EXPORT_SYMBOL(xenbus_alloc_evtchn);
280
281
227
228 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
229 &alloc_unbound);
230
231 if (err)
232 xenbus_dev_fatal(dev, err, "allocating event channel");
233 else
234 *port = alloc_unbound.port;
235 return err;
236}
237EXPORT_SYMBOL(xenbus_alloc_evtchn);
238
239
282int xenbus_free_evtchn(struct xenbus_device *dev, int port)
240int xenbus_free_evtchn(device_t dev, int port)
283{
284 struct evtchn_close close;
285 int err;
286
287 close.port = port;
288
289 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
290 if (err)

--- 28 unchanged lines hidden ---
241{
242 struct evtchn_close close;
243 int err;
244
245 close.port = port;
246
247 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
248 if (err)

--- 28 unchanged lines hidden ---