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.
6 * Copyright (C) 2005 XenSource Ltd
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:
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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
33 #include <linux/types.h>
34 #include <linux/vmalloc.h>
35 #include <asm/xen/hypervisor.h>
36 #include <xen/interface/xen.h>
37 #include <xen/interface/event_channel.h>
38 #include <xen/events.h>
39 #include <xen/grant_table.h>
40 #include <xen/xenbus.h>
42 const char *xenbus_strstate(enum xenbus_state state)
44 static const char *const name[] = {
45 [ XenbusStateUnknown ] = "Unknown",
46 [ XenbusStateInitialising ] = "Initialising",
47 [ XenbusStateInitWait ] = "InitWait",
48 [ XenbusStateInitialised ] = "Initialised",
49 [ XenbusStateConnected ] = "Connected",
50 [ XenbusStateClosing ] = "Closing",
51 [ XenbusStateClosed ] = "Closed",
53 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
55 EXPORT_SYMBOL_GPL(xenbus_strstate);
58 * xenbus_watch_path - register a watch
60 * @path: path to watch
61 * @watch: watch to register
62 * @callback: callback to register
64 * Register a @watch on the given path, using the given xenbus_watch structure
65 * for storage, and the given @callback function as the callback. Return 0 on
66 * success, or -errno on error. On success, the given @path will be saved as
67 * @watch->node, and remains the caller's to free. On error, @watch->node will
68 * be NULL, the device will switch to %XenbusStateClosing, and the error will
69 * be saved in the store.
71 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
72 struct xenbus_watch *watch,
73 void (*callback)(struct xenbus_watch *,
74 const char **, unsigned int))
79 watch->callback = callback;
81 err = register_xenbus_watch(watch);
85 watch->callback = NULL;
86 xenbus_dev_fatal(dev, err, "adding watch on %s", path);
91 EXPORT_SYMBOL_GPL(xenbus_watch_path);
95 * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
97 * @watch: watch to register
98 * @callback: callback to register
99 * @pathfmt: format of path to watch
101 * Register a watch on the given @path, using the given xenbus_watch
102 * structure for storage, and the given @callback function as the callback.
103 * Return 0 on success, or -errno on error. On success, the watched path
104 * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
105 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
106 * free, the device will switch to %XenbusStateClosing, and the error will be
107 * saved in the store.
109 int xenbus_watch_pathfmt(struct xenbus_device *dev,
110 struct xenbus_watch *watch,
111 void (*callback)(struct xenbus_watch *,
112 const char **, unsigned int),
113 const char *pathfmt, ...)
119 va_start(ap, pathfmt);
120 path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
124 xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
127 err = xenbus_watch_path(dev, path, watch, callback);
133 EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
137 * xenbus_switch_state
138 * @dev: xenbus device
141 * Advertise in the store a change of the given driver to the given new_state.
142 * Return 0 on success, or -errno on error. On error, the device will switch
143 * to XenbusStateClosing, and the error will be saved in the store.
145 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
147 /* We check whether the state is currently set to the given value, and
148 if not, then the state is set. We don't want to unconditionally
149 write the given state, because we don't want to fire watches
150 unnecessarily. Furthermore, if the node has gone, we don't write
151 to it, as the device will be tearing down, and we don't want to
152 resurrect that directory.
154 Note that, because of this cached value of our state, this function
155 will not work inside a Xenstore transaction (something it was
156 trying to in the past) because dev->state would not get reset if
157 the transaction was aborted.
164 if (state == dev->state)
167 err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
172 err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
174 if (state != XenbusStateClosing) /* Avoid looping */
175 xenbus_dev_fatal(dev, err, "writing new state");
183 EXPORT_SYMBOL_GPL(xenbus_switch_state);
185 int xenbus_frontend_closed(struct xenbus_device *dev)
187 xenbus_switch_state(dev, XenbusStateClosed);
188 complete(&dev->down);
191 EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
194 * Return the path to the error node for the given device, or NULL on failure.
195 * If the value returned is non-NULL, then it is the caller's to kfree.
197 static char *error_path(struct xenbus_device *dev)
199 return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
203 static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
204 const char *fmt, va_list ap)
208 char *printf_buffer = NULL;
209 char *path_buffer = NULL;
211 #define PRINTF_BUFFER_SIZE 4096
212 printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
213 if (printf_buffer == NULL)
216 len = sprintf(printf_buffer, "%i ", -err);
217 ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
219 BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
221 dev_err(&dev->dev, "%s\n", printf_buffer);
223 path_buffer = error_path(dev);
225 if (path_buffer == NULL) {
226 dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
227 dev->nodename, printf_buffer);
231 if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
232 dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
233 dev->nodename, printf_buffer);
238 kfree(printf_buffer);
245 * @dev: xenbus device
246 * @err: error to report
247 * @fmt: error message format
249 * Report the given negative errno into the store, along with the given
252 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
257 xenbus_va_dev_error(dev, err, fmt, ap);
260 EXPORT_SYMBOL_GPL(xenbus_dev_error);
264 * @dev: xenbus device
265 * @err: error to report
266 * @fmt: error message format
268 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
269 * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
270 * closedown of this driver and its peer.
273 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
278 xenbus_va_dev_error(dev, err, fmt, ap);
281 xenbus_switch_state(dev, XenbusStateClosing);
283 EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
287 * @dev: xenbus device
288 * @ring_mfn: mfn of ring to grant
290 * Grant access to the given @ring_mfn to the peer of the given device. Return
291 * 0 on success, or -errno on error. On error, the device will switch to
292 * XenbusStateClosing, and the error will be saved in the store.
294 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
296 int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
298 xenbus_dev_fatal(dev, err, "granting access to ring page");
301 EXPORT_SYMBOL_GPL(xenbus_grant_ring);
305 * Allocate an event channel for the given xenbus_device, assigning the newly
306 * created local port to *port. Return 0 on success, or -errno on error. On
307 * error, the device will switch to XenbusStateClosing, and the error will be
308 * saved in the store.
310 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
312 struct evtchn_alloc_unbound alloc_unbound;
315 alloc_unbound.dom = DOMID_SELF;
316 alloc_unbound.remote_dom = dev->otherend_id;
318 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
321 xenbus_dev_fatal(dev, err, "allocating event channel");
323 *port = alloc_unbound.port;
327 EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
331 * Bind to an existing interdomain event channel in another domain. Returns 0
332 * on success and stores the local port in *port. On error, returns -errno,
333 * switches the device to XenbusStateClosing, and saves the error in XenStore.
335 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
337 struct evtchn_bind_interdomain bind_interdomain;
340 bind_interdomain.remote_dom = dev->otherend_id;
341 bind_interdomain.remote_port = remote_port;
343 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
346 xenbus_dev_fatal(dev, err,
347 "binding to event channel %d from domain %d",
348 remote_port, dev->otherend_id);
350 *port = bind_interdomain.local_port;
354 EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
358 * Free an existing event channel. Returns 0 on success or -errno on error.
360 int xenbus_free_evtchn(struct xenbus_device *dev, int port)
362 struct evtchn_close close;
367 err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
369 xenbus_dev_error(dev, err, "freeing event channel %d", port);
373 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
377 * xenbus_map_ring_valloc
378 * @dev: xenbus device
379 * @gnt_ref: grant reference
380 * @vaddr: pointer to address to be filled out by mapping
382 * Based on Rusty Russell's skeleton driver's map_page.
383 * Map a page of memory into this domain from another domain's grant table.
384 * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
385 * page to that address, and sets *vaddr to that address.
386 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
387 * or -ENOMEM on error. If an error is returned, device will switch to
388 * XenbusStateClosing and the error message will be saved in XenStore.
390 int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
392 struct gnttab_map_grant_ref op = {
393 .flags = GNTMAP_host_map,
395 .dom = dev->otherend_id,
397 struct vm_struct *area;
401 area = xen_alloc_vm_area(PAGE_SIZE);
405 op.host_addr = (unsigned long)area->addr;
407 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
410 if (op.status != GNTST_okay) {
411 xen_free_vm_area(area);
412 xenbus_dev_fatal(dev, op.status,
413 "mapping in shared page %d from domain %d",
414 gnt_ref, dev->otherend_id);
418 /* Stuff the handle in an unused field */
419 area->phys_addr = (unsigned long)op.handle;
424 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
429 * @dev: xenbus device
430 * @gnt_ref: grant reference
431 * @handle: pointer to grant handle to be filled
432 * @vaddr: address to be mapped to
434 * Map a page of memory into this domain from another domain's grant table.
435 * xenbus_map_ring does not allocate the virtual address space (you must do
436 * this yourself!). It only maps in the page to the specified address.
437 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
438 * or -ENOMEM on error. If an error is returned, device will switch to
439 * XenbusStateClosing and the error message will be saved in XenStore.
441 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
442 grant_handle_t *handle, void *vaddr)
444 struct gnttab_map_grant_ref op = {
445 .host_addr = (unsigned long)vaddr,
446 .flags = GNTMAP_host_map,
448 .dom = dev->otherend_id,
451 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
454 if (op.status != GNTST_okay) {
455 xenbus_dev_fatal(dev, op.status,
456 "mapping in shared page %d from domain %d",
457 gnt_ref, dev->otherend_id);
463 EXPORT_SYMBOL_GPL(xenbus_map_ring);
467 * xenbus_unmap_ring_vfree
468 * @dev: xenbus device
469 * @vaddr: addr to unmap
471 * Based on Rusty Russell's skeleton driver's unmap_page.
472 * Unmap a page of memory in this domain that was imported from another domain.
473 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
474 * xenbus_map_ring_valloc (it will free the virtual address space).
475 * Returns 0 on success and returns GNTST_* on error
476 * (see xen/include/interface/grant_table.h).
478 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
480 struct vm_struct *area;
481 struct gnttab_unmap_grant_ref op = {
482 .host_addr = (unsigned long)vaddr,
485 /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
486 * method so that we don't have to muck with vmalloc internals here.
487 * We could force the user to hang on to their struct vm_struct from
488 * xenbus_map_ring_valloc, but these 6 lines considerably simplify
491 read_lock(&vmlist_lock);
492 for (area = vmlist; area != NULL; area = area->next) {
493 if (area->addr == vaddr)
496 read_unlock(&vmlist_lock);
499 xenbus_dev_error(dev, -ENOENT,
500 "can't find mapped virtual address %p", vaddr);
501 return GNTST_bad_virt_addr;
504 op.handle = (grant_handle_t)area->phys_addr;
506 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
509 if (op.status == GNTST_okay)
510 xen_free_vm_area(area);
512 xenbus_dev_error(dev, op.status,
513 "unmapping page at handle %d error %d",
514 (int16_t)area->phys_addr, op.status);
518 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
523 * @dev: xenbus device
524 * @handle: grant handle
525 * @vaddr: addr to unmap
527 * Unmap a page of memory in this domain that was imported from another domain.
528 * Returns 0 on success and returns GNTST_* on error
529 * (see xen/include/interface/grant_table.h).
531 int xenbus_unmap_ring(struct xenbus_device *dev,
532 grant_handle_t handle, void *vaddr)
534 struct gnttab_unmap_grant_ref op = {
535 .host_addr = (unsigned long)vaddr,
539 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
542 if (op.status != GNTST_okay)
543 xenbus_dev_error(dev, op.status,
544 "unmapping page at handle %d error %d",
549 EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
553 * xenbus_read_driver_state
554 * @path: path for driver
556 * Return the state of the driver rooted at the given store path, or
557 * XenbusStateUnknown if no state can be read.
559 enum xenbus_state xenbus_read_driver_state(const char *path)
561 enum xenbus_state result;
562 int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
564 result = XenbusStateUnknown;
568 EXPORT_SYMBOL_GPL(xenbus_read_driver_state);