Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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 irqreturn_t wake_waiting(int irq, void *unused, struct pt_regs *regs)
51 {
52         if (unlikely(xenstored_ready == 0)) {
53                 xenstored_ready = 1;
54                 schedule_work(&probe_work);
55         }
56
57         wake_up(&xb_waitq);
58         return IRQ_HANDLED;
59 }
60
61 static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
62 {
63         return ((prod - cons) <= XENSTORE_RING_SIZE);
64 }
65
66 static void *get_output_chunk(XENSTORE_RING_IDX cons,
67                               XENSTORE_RING_IDX prod,
68                               char *buf, uint32_t *len)
69 {
70         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
71         if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
72                 *len = XENSTORE_RING_SIZE - (prod - cons);
73         return buf + MASK_XENSTORE_IDX(prod);
74 }
75
76 static const void *get_input_chunk(XENSTORE_RING_IDX cons,
77                                    XENSTORE_RING_IDX prod,
78                                    const char *buf, uint32_t *len)
79 {
80         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
81         if ((prod - cons) < *len)
82                 *len = prod - cons;
83         return buf + MASK_XENSTORE_IDX(cons);
84 }
85
86 int xb_write(const void *data, unsigned len)
87 {
88         struct xenstore_domain_interface *intf = xen_store_interface;
89         XENSTORE_RING_IDX cons, prod;
90         int rc;
91
92         while (len != 0) {
93                 void *dst;
94                 unsigned int avail;
95
96                 rc = wait_event_interruptible(
97                         xb_waitq,
98                         (intf->req_prod - intf->req_cons) !=
99                         XENSTORE_RING_SIZE);
100                 if (rc < 0)
101                         return rc;
102
103                 /* Read indexes, then verify. */
104                 cons = intf->req_cons;
105                 prod = intf->req_prod;
106                 mb();
107                 if (!check_indexes(cons, prod)) {
108                         intf->req_cons = intf->req_prod = 0;
109                         return -EIO;
110                 }
111
112                 dst = get_output_chunk(cons, prod, intf->req, &avail);
113                 if (avail == 0)
114                         continue;
115                 if (avail > len)
116                         avail = len;
117
118                 memcpy(dst, data, avail);
119                 data += avail;
120                 len -= avail;
121
122                 /* Other side must not see new header until data is there. */
123                 wmb();
124                 intf->req_prod += avail;
125
126                 /* This implies mb() before other side sees interrupt. */
127                 notify_remote_via_evtchn(xen_store_evtchn);
128         }
129
130         return 0;
131 }
132
133 int xb_read(void *data, unsigned len)
134 {
135         struct xenstore_domain_interface *intf = xen_store_interface;
136         XENSTORE_RING_IDX cons, prod;
137         int rc;
138
139         while (len != 0) {
140                 unsigned int avail;
141                 const char *src;
142
143                 rc = wait_event_interruptible(
144                         xb_waitq,
145                         intf->rsp_cons != intf->rsp_prod);
146                 if (rc < 0)
147                         return rc;
148
149                 /* Read indexes, then verify. */
150                 cons = intf->rsp_cons;
151                 prod = intf->rsp_prod;
152                 mb();
153                 if (!check_indexes(cons, prod)) {
154                         intf->rsp_cons = intf->rsp_prod = 0;
155                         return -EIO;
156                 }
157
158                 src = get_input_chunk(cons, prod, intf->rsp, &avail);
159                 if (avail == 0)
160                         continue;
161                 if (avail > len)
162                         avail = len;
163
164                 /* We must read header before we read data. */
165                 rmb();
166
167                 memcpy(data, src, avail);
168                 data += avail;
169                 len -= avail;
170
171                 /* Other side must not see free space until we've copied out */
172                 mb();
173                 intf->rsp_cons += avail;
174
175                 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
176
177                 /* Implies mb(): they will see new header. */
178                 notify_remote_via_evtchn(xen_store_evtchn);
179         }
180
181         return 0;
182 }
183
184 /* Set up interrupt handler off store event channel. */
185 int xb_init_comms(void)
186 {
187         int err;
188
189         if (xenbus_irq)
190                 unbind_from_irqhandler(xenbus_irq, &xb_waitq);
191
192         err = bind_evtchn_to_irqhandler(
193                 xen_store_evtchn, wake_waiting,
194                 0, "xenbus", &xb_waitq);
195         if (err <= 0) {
196                 printk(KERN_ERR "XENBUS request irq failed %i\n", err);
197                 return err;
198         }
199
200         xenbus_irq = err;
201
202         return 0;
203 }