Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_backend_client.c
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
4  * driver.
5  *
6  * Copyright (C) 2005-2006 XenSource Ltd
7  * 
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:
13  * 
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:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
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
30  * IN THE SOFTWARE.
31  */
32
33 #include <linux/err.h>
34 #include <linux/module.h>
35 #include <linux/vmalloc.h>
36 #include <xen/gnttab.h>
37 #include <xen/xenbus.h>
38
39 /* Based on Rusty Russell's skeleton driver's map_page */
40 struct vm_struct *xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t gnt_ref)
41 {
42         struct gnttab_map_grant_ref op;
43         struct vm_struct *area;
44
45         area = alloc_vm_area(PAGE_SIZE, NULL);
46         if (!area)
47                 return ERR_PTR(-ENOMEM);
48
49         gnttab_set_map_op(&op, (unsigned long)area->addr, GNTMAP_host_map,
50                           gnt_ref, dev->otherend_id);
51         
52         gnttab_check_GNTST_eagain_do_while(GNTTABOP_map_grant_ref, &op);
53
54         if (op.status != GNTST_okay) {
55                 free_vm_area(area);
56                 xenbus_dev_fatal(dev, op.status,
57                                  "mapping in shared page %d from domain %d",
58                                  gnt_ref, dev->otherend_id);
59                 BUG_ON(!IS_ERR(ERR_PTR(op.status)));
60                 return ERR_PTR(-EINVAL);
61         }
62
63         /* Stuff the handle in an unused field */
64         area->phys_addr = (unsigned long)op.handle;
65
66         return area;
67 }
68 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
69
70
71 /* Based on Rusty Russell's skeleton driver's unmap_page */
72 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, struct vm_struct *area)
73 {
74         struct gnttab_unmap_grant_ref op;
75
76         gnttab_set_unmap_op(&op, (unsigned long)area->addr, GNTMAP_host_map,
77                             (grant_handle_t)area->phys_addr);
78
79         if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
80                 BUG();
81
82         if (op.status == GNTST_okay)
83                 free_vm_area(area);
84         else
85                 xenbus_dev_error(dev, op.status,
86                                  "unmapping page at handle %d error %d",
87                                  (int16_t)area->phys_addr, op.status);
88
89         return op.status == GNTST_okay ? 0 : -EINVAL;
90 }
91 EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
92
93
94 int xenbus_dev_is_online(struct xenbus_device *dev)
95 {
96         int rc, val;
97
98         rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
99         if (rc != 1)
100                 val = 0; /* no online node present */
101
102         return val;
103 }
104 EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
105
106 MODULE_LICENSE("Dual BSD/GPL");