This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / xen / xenbus / xenbus_comms.c
1 /******************************************************************************
2  * xenbus_comms.c
3  *
4  * Low level code to talks to Xen Store: ringbuffer and event channel.
5  *
6  * Copyright (C) 2005 Rusty Russell, IBM Corporation
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 <asm/hypervisor.h>
34 #include <xen/evtchn.h>
35 #include <linux/wait.h>
36 #include <linux/interrupt.h>
37 #include <linux/sched.h>
38 #include <linux/err.h>
39 #include <xen/xenbus.h>
40 #include "xenbus_comms.h"
41
42 static int xenbus_irq;
43
44 extern void xenbus_probe(void *);
45 extern int xenstored_ready;
46 static DECLARE_WORK(probe_work, xenbus_probe, NULL);
47
48 DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
49
50 static inline struct xenstore_domain_interface *xenstore_domain_interface(void)
51 {
52         return mfn_to_virt(xen_start_info->store_mfn);
53 }
54
55 static irqreturn_t wake_waiting(int irq, void *unused, struct pt_regs *regs)
56 {
57         if (unlikely(xenstored_ready == 0)) {
58                 xenstored_ready = 1;
59                 schedule_work(&probe_work);
60         }
61
62         wake_up(&xb_waitq);
63         return IRQ_HANDLED;
64 }
65
66 static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
67 {
68         return ((prod - cons) <= XENSTORE_RING_SIZE);
69 }
70
71 static void *get_output_chunk(XENSTORE_RING_IDX cons,
72                               XENSTORE_RING_IDX prod,
73                               char *buf, uint32_t *len)
74 {
75         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
76         if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
77                 *len = XENSTORE_RING_SIZE - (prod - cons);
78         return buf + MASK_XENSTORE_IDX(prod);
79 }
80
81 static const void *get_input_chunk(XENSTORE_RING_IDX cons,
82                                    XENSTORE_RING_IDX prod,
83                                    const char *buf, uint32_t *len)
84 {
85         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
86         if ((prod - cons) < *len)
87                 *len = prod - cons;
88         return buf + MASK_XENSTORE_IDX(cons);
89 }
90
91 int xb_write(const void *data, unsigned len)
92 {
93         struct xenstore_domain_interface *intf = xenstore_domain_interface();
94         XENSTORE_RING_IDX cons, prod;
95         int rc;
96
97         while (len != 0) {
98                 void *dst;
99                 unsigned int avail;
100
101                 rc = wait_event_interruptible(
102                         xb_waitq,
103                         (intf->req_prod - intf->req_cons) !=
104                         XENSTORE_RING_SIZE);
105                 if (rc < 0)
106                         return rc;
107
108                 /* Read indexes, then verify. */
109                 cons = intf->req_cons;
110                 prod = intf->req_prod;
111                 mb();
112                 if (!check_indexes(cons, prod)) {
113                         intf->req_cons = intf->req_prod = 0;
114                         return -EIO;
115                 }
116
117                 dst = get_output_chunk(cons, prod, intf->req, &avail);
118                 if (avail == 0)
119                         continue;
120                 if (avail > len)
121                         avail = len;
122
123                 memcpy(dst, data, avail);
124                 data += avail;
125                 len -= avail;
126
127                 /* Other side must not see new header until data is there. */
128                 wmb();
129                 intf->req_prod += avail;
130
131                 /* This implies mb() before other side sees interrupt. */
132                 notify_remote_via_evtchn(xen_start_info->store_evtchn);
133         }
134
135         return 0;
136 }
137
138 int xb_read(void *data, unsigned len)
139 {
140         struct xenstore_domain_interface *intf = xenstore_domain_interface();
141         XENSTORE_RING_IDX cons, prod;
142         int rc;
143
144         while (len != 0) {
145                 unsigned int avail;
146                 const char *src;
147
148                 rc = wait_event_interruptible(
149                         xb_waitq,
150                         intf->rsp_cons != intf->rsp_prod);
151                 if (rc < 0)
152                         return rc;
153
154                 /* Read indexes, then verify. */
155                 cons = intf->rsp_cons;
156                 prod = intf->rsp_prod;
157                 mb();
158                 if (!check_indexes(cons, prod)) {
159                         intf->rsp_cons = intf->rsp_prod = 0;
160                         return -EIO;
161                 }
162
163                 src = get_input_chunk(cons, prod, intf->rsp, &avail);
164                 if (avail == 0)
165                         continue;
166                 if (avail > len)
167                         avail = len;
168
169                 /* We must read header before we read data. */
170                 rmb();
171
172                 memcpy(data, src, avail);
173                 data += avail;
174                 len -= avail;
175
176                 /* Other side must not see free space until we've copied out */
177                 mb();
178                 intf->rsp_cons += avail;
179
180                 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
181
182                 /* Implies mb(): they will see new header. */
183                 notify_remote_via_evtchn(xen_start_info->store_evtchn);
184         }
185
186         return 0;
187 }
188
189 /* Set up interrupt handler off store event channel. */
190 int xb_init_comms(void)
191 {
192         int err;
193
194         if (xenbus_irq)
195                 unbind_from_irqhandler(xenbus_irq, &xb_waitq);
196
197         err = bind_evtchn_to_irqhandler(
198                 xen_start_info->store_evtchn, wake_waiting,
199                 0, "xenbus", &xb_waitq);
200         if (err <= 0) {
201                 printk(KERN_ERR "XENBUS request irq failed %i\n", err);
202                 return err;
203         }
204
205         xenbus_irq = err;
206
207         return 0;
208 }