1 /******************************************************************************
2 * Backend-client-facing interface for the Xenbus driver. In other words, the
3 * interface between the Xenbus and the device-specific code in the backend
6 * Copyright (C) 2005-2006 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/err.h>
34 #include <xen/gnttab.h>
35 #include <xen/xenbus.h>
36 #include <xen/driver_util.h>
38 /* Based on Rusty Russell's skeleton driver's map_page */
39 struct vm_struct *xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref)
41 struct gnttab_map_grant_ref op;
42 struct vm_struct *area;
44 area = alloc_vm_area(PAGE_SIZE);
46 return ERR_PTR(-ENOMEM);
48 gnttab_set_map_op(&op, (unsigned long)area->addr, GNTMAP_host_map,
49 gnt_ref, dev->otherend_id);
52 BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1));
55 if (op.status != GNTST_okay) {
57 xenbus_dev_fatal(dev, op.status,
58 "mapping in shared page %d from domain %d",
59 gnt_ref, dev->otherend_id);
60 BUG_ON(!IS_ERR(ERR_PTR(op.status)));
61 return ERR_PTR(op.status);
64 /* Stuff the handle in an unused field */
65 area->phys_addr = (unsigned long)op.handle;
69 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
72 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
73 grant_handle_t *handle, void *vaddr)
75 struct gnttab_map_grant_ref op;
77 gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map,
78 gnt_ref, dev->otherend_id);
79 BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1));
81 if (op.status != GNTST_okay) {
82 xenbus_dev_fatal(dev, op.status,
83 "mapping in shared page %d from domain %d",
84 gnt_ref, dev->otherend_id);
90 EXPORT_SYMBOL_GPL(xenbus_map_ring);
93 /* Based on Rusty Russell's skeleton driver's unmap_page */
94 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, struct vm_struct *area)
96 struct gnttab_unmap_grant_ref op;
98 gnttab_set_unmap_op(&op, (unsigned long)area->addr, GNTMAP_host_map,
99 (grant_handle_t)area->phys_addr);
102 BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1));
103 unlock_vm_area(area);
105 if (op.status == GNTST_okay)
108 xenbus_dev_error(dev, op.status,
109 "unmapping page at handle %d error %d",
110 (int16_t)area->phys_addr, op.status);
114 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
117 int xenbus_unmap_ring(struct xenbus_device *dev,
118 grant_handle_t handle, void *vaddr)
120 struct gnttab_unmap_grant_ref op;
122 gnttab_set_unmap_op(&op, (unsigned long)vaddr, GNTMAP_host_map,
124 BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1));
126 if (op.status != GNTST_okay)
127 xenbus_dev_error(dev, op.status,
128 "unmapping page at handle %d error %d",
133 EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
135 MODULE_LICENSE("Dual BSD/GPL");