/****************************************************************************** * Client-facing interface for the Xenbus driver. In other words, the * interface between the Xenbus and the device-specific code, be it the * frontend or the backend of that driver. * * Copyright (C) 2005 XenSource Ltd * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation; or, when distributed * separately from the Linux kernel or incorporated into other * software packages, subject to the following license: * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this source file (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE.
*/
/* Why do we need two arrays? See comment of __xenbus_map_ring */ unsignedlong addrs[XENBUS_MAX_RING_GRANTS];
phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
/** * xenbus_watch_path - register a watch * @dev: xenbus device * @path: path to watch * @watch: watch to register * @will_handle: events queuing determine callback * @callback: callback to register * * Register a @watch on the given path, using the given xenbus_watch structure * for storage, @will_handle function as the callback to determine if each * event need to be queued, and the given @callback function as the callback. * On success, the given @path will be saved as @watch->node, and remains the * caller's to free. On error, @watch->node will be NULL, the device will * switch to %XenbusStateClosing, and the error will be saved in the store. * * Returns: %0 on success or -errno on error
*/ int xenbus_watch_path(struct xenbus_device *dev, constchar *path, struct xenbus_watch *watch, bool (*will_handle)(struct xenbus_watch *, constchar *, constchar *), void (*callback)(struct xenbus_watch *, constchar *, constchar *))
{ int err;
/** * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path * @dev: xenbus device * @watch: watch to register * @will_handle: events queuing determine callback * @callback: callback to register * @pathfmt: format of path to watch * * Register a watch on the given @path, using the given xenbus_watch * structure for storage, @will_handle function as the callback to determine if * each event need to be queued, and the given @callback function as the * callback. On success, the watched path (@path/@path2) will be saved * as @watch->node, and becomes the caller's to kfree(). * On error, watch->node will be NULL, so the caller has nothing to * free, the device will switch to %XenbusStateClosing, and the error will be * saved in the store. * * Returns: %0 on success or -errno on error
*/ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch, bool (*will_handle)(struct xenbus_watch *, constchar *, constchar *), void (*callback)(struct xenbus_watch *, constchar *, constchar *), constchar *pathfmt, ...)
{ int err;
va_list ap; char *path;
staticint
__xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state, int depth)
{ /* We check whether the state is currently set to the given value, and if not, then the state is set. We don't want to unconditionally write the given state, because we don't want to fire watches unnecessarily. Furthermore, if the node has gone, we don't write to it, as the device will be tearing down, and we don't want to resurrect that directory.
Note that, because of this cached value of our state, this function will not take a caller's Xenstore transaction (something it was trying to in the past) because dev->state would not get reset if the transaction was aborted.
*/
struct xenbus_transaction xbt; int current_state; int err, abort;
/** * xenbus_switch_state - save the new state of a driver * @dev: xenbus device * @state: new state * * Advertise in the store a change of the given driver to the given new_state. * On error, the device will switch to XenbusStateClosing, and the error * will be saved in the store. * * Returns: %0 on success or -errno on error
*/ int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
{ return __xenbus_switch_state(dev, state, 0);
}
/** * xenbus_dev_error - place an error message into the store * @dev: xenbus device * @err: error to report * @fmt: error message format * * Report the given negative errno into the store, along with the given * formatted message.
*/ void xenbus_dev_error(struct xenbus_device *dev, int err, constchar *fmt, ...)
{
va_list ap;
/** * xenbus_dev_fatal - put an error messages into the store and then shutdown * @dev: xenbus device * @err: error to report * @fmt: error message format * * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly * closedown of this driver and its peer.
*/
if (!depth)
__xenbus_switch_state(dev, XenbusStateClosing, 1);
}
/* * xenbus_setup_ring * @dev: xenbus device * @vaddr: pointer to starting virtual address of the ring * @nr_pages: number of pages to be granted * @grefs: grant reference array to be filled in * * Allocate physically contiguous pages for a shared ring buffer and grant it * to the peer of the given device. The ring buffer is initially filled with * zeroes. The virtual address of the ring is stored at @vaddr and the * grant references are stored in the @grefs array. In case of error @vaddr * will be set to NULL and @grefs will be filled with INVALID_GRANT_REF.
*/ int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr, unsignedint nr_pages, grant_ref_t *grefs)
{ unsignedlong ring_size = nr_pages * XEN_PAGE_SIZE;
grant_ref_t gref_head; unsignedint i; void *addr; int ret;
addr = *vaddr = alloc_pages_exact(ring_size, gfp | __GFP_ZERO); if (!*vaddr) {
ret = -ENOMEM; goto err;
}
ret = gnttab_alloc_grant_references(nr_pages, &gref_head); if (ret) {
xenbus_dev_fatal(dev, ret, "granting access to %u ring pages",
nr_pages); goto err;
}
for (i = 0; i < nr_pages; i++) { unsignedlong gfn;
if (is_vmalloc_addr(*vaddr))
gfn = pfn_to_gfn(vmalloc_to_pfn(addr)); else
gfn = virt_to_gfn(addr);
/* * xenbus_teardown_ring * @vaddr: starting virtual address of the ring * @nr_pages: number of pages * @grefs: grant reference array * * Remove grants for the shared ring buffer and free the associated memory. * On return the grant reference array is filled with INVALID_GRANT_REF.
*/ void xenbus_teardown_ring(void **vaddr, unsignedint nr_pages,
grant_ref_t *grefs)
{ unsignedint i;
for (i = 0; i < nr_pages; i++) { if (grefs[i] != INVALID_GRANT_REF) {
gnttab_end_foreign_access(grefs[i], NULL);
grefs[i] = INVALID_GRANT_REF;
}
}
/* * Allocate an event channel for the given xenbus_device, assigning the newly * created local port to *port. Return 0 on success, or -errno on error. On * error, the device will switch to XenbusStateClosing, and the error will be * saved in the store.
*/ int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port)
{ struct evtchn_alloc_unbound alloc_unbound; int err;
/* * Free an existing event channel. Returns 0 on success or -errno on error.
*/ int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port)
{ struct evtchn_close close; int err;
/** * xenbus_map_ring_valloc - allocate & map pages of VA space * @dev: xenbus device * @gnt_refs: grant reference array * @nr_grefs: number of grant references * @vaddr: pointer to address to be filled out by mapping * * Map @nr_grefs pages of memory into this domain from another * domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs * pages of virtual address space, maps the pages to that address, and sets * *vaddr to that address. If an error is returned, device will switch to * XenbusStateClosing and the error message will be saved in XenStore. * * Returns: %0 on success or -errno on error
*/ int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs, unsignedint nr_grefs, void **vaddr)
{ int err; struct map_ring_valloc *info;
*vaddr = NULL;
if (nr_grefs > XENBUS_MAX_RING_GRANTS) return -EINVAL;
info = kzalloc(sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM;
/* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned * long), e.g. 32-on-64. Caller is responsible for preparing the
* right array to feed into this function */ staticint __xenbus_map_ring(struct xenbus_device *dev,
grant_ref_t *gnt_refs, unsignedint nr_grefs,
grant_handle_t *handles, struct map_ring_valloc *info, unsignedint flags, bool *leaked)
{ int i, j;
if (nr_grefs > XENBUS_MAX_RING_GRANTS) return -EINVAL;
for (i = 0; i < nr_grefs; i++) {
gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags,
gnt_refs[i], dev->otherend_id);
handles[i] = INVALID_GRANT_HANDLE;
}
gnttab_batch_map(info->map, i);
for (i = 0; i < nr_grefs; i++) { if (info->map[i].status != GNTST_okay) {
xenbus_dev_fatal(dev, info->map[i].status, "mapping in shared page %d from domain %d",
gnt_refs[i], dev->otherend_id); goto fail;
} else
handles[i] = info->map[i].handle;
}
return 0;
fail: for (i = j = 0; i < nr_grefs; i++) { if (handles[i] != INVALID_GRANT_HANDLE) {
gnttab_set_unmap_op(&info->unmap[j],
info->phys_addrs[i],
GNTMAP_host_map, handles[i]);
j++;
}
}
*leaked = false; for (i = 0; i < j; i++) { if (info->unmap[i].status != GNTST_okay) {
*leaked = true; break;
}
}
return -ENOENT;
}
/** * xenbus_unmap_ring - unmap memory from another domain * @dev: xenbus device * @handles: grant handle array * @nr_handles: number of handles in the array * @vaddrs: addresses to unmap * * Unmap memory in this domain that was imported from another domain. * * Returns: %0 on success or GNTST_* on error * (see xen/include/interface/grant_table.h).
*/ staticint xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles, unsignedint nr_handles, unsignedlong *vaddrs)
{ struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; int i; int err;
if (nr_handles > XENBUS_MAX_RING_GRANTS) return -EINVAL;
for (i = 0; i < nr_handles; i++)
gnttab_set_unmap_op(&unmap[i], vaddrs[i],
GNTMAP_host_map, handles[i]);
/** * xenbus_unmap_ring_vfree - unmap a page of memory from another domain * @dev: xenbus device * @vaddr: addr to unmap * * Based on Rusty Russell's skeleton driver's unmap_page. * Unmap a page of memory in this domain that was imported from another domain. * Use xenbus_unmap_ring_vfree if you mapped in your memory with * xenbus_map_ring_valloc (it will free the virtual address space). * * Returns: %0 on success or GNTST_* on error * (see xen/include/interface/grant_table.h).
*/ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
{ return ring_ops->unmap(dev, vaddr);
}
EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
/** * xenbus_read_driver_state - read state from a store path * @path: path for driver * * Returns: the state of the driver rooted at the given store path, or * XenbusStateUnknown if no state can be read.
*/ enum xenbus_state xenbus_read_driver_state(constchar *path)
{ enum xenbus_state result; int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL); if (err)
result = XenbusStateUnknown;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.