xen: add backend driver support
[pandora-kernel.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 <linux/wait.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/err.h>
37 #include <xen/xenbus.h>
38 #include <asm/xen/hypervisor.h>
39 #include <xen/events.h>
40 #include <xen/page.h>
41 #include "xenbus_comms.h"
42
43 static int xenbus_irq;
44
45 static DECLARE_WORK(probe_work, xenbus_probe);
46
47 static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
48
49 static irqreturn_t wake_waiting(int irq, void *unused)
50 {
51         if (unlikely(xenstored_ready == 0)) {
52                 printk(KERN_CRIT "xenbus_probe wake_waiting\n");
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 /**
87  * xb_write - low level write
88  * @data: buffer to send
89  * @len: length of buffer
90  *
91  * Returns 0 on success, error otherwise.
92  */
93 int xb_write(const void *data, unsigned len)
94 {
95         struct xenstore_domain_interface *intf = xen_store_interface;
96         XENSTORE_RING_IDX cons, prod;
97         int rc;
98
99         while (len != 0) {
100                 void *dst;
101                 unsigned int avail;
102
103                 rc = wait_event_interruptible(
104                         xb_waitq,
105                         (intf->req_prod - intf->req_cons) !=
106                         XENSTORE_RING_SIZE);
107                 if (rc < 0)
108                         return rc;
109
110                 /* Read indexes, then verify. */
111                 cons = intf->req_cons;
112                 prod = intf->req_prod;
113                 if (!check_indexes(cons, prod)) {
114                         intf->req_cons = intf->req_prod = 0;
115                         return -EIO;
116                 }
117
118                 dst = get_output_chunk(cons, prod, intf->req, &avail);
119                 if (avail == 0)
120                         continue;
121                 if (avail > len)
122                         avail = len;
123
124                 /* Must write data /after/ reading the consumer index. */
125                 mb();
126
127                 memcpy(dst, data, avail);
128                 data += avail;
129                 len -= avail;
130
131                 /* Other side must not see new producer until data is there. */
132                 wmb();
133                 intf->req_prod += avail;
134
135                 /* Implies mb(): other side will see the updated producer. */
136                 notify_remote_via_evtchn(xen_store_evtchn);
137         }
138
139         return 0;
140 }
141
142 int xb_data_to_read(void)
143 {
144         struct xenstore_domain_interface *intf = xen_store_interface;
145         return (intf->rsp_cons != intf->rsp_prod);
146 }
147
148 int xb_wait_for_data_to_read(void)
149 {
150         return wait_event_interruptible(xb_waitq, xb_data_to_read());
151 }
152
153 int xb_read(void *data, unsigned len)
154 {
155         struct xenstore_domain_interface *intf = xen_store_interface;
156         XENSTORE_RING_IDX cons, prod;
157         int rc;
158
159         while (len != 0) {
160                 unsigned int avail;
161                 const char *src;
162
163                 rc = xb_wait_for_data_to_read();
164                 if (rc < 0)
165                         return rc;
166
167                 /* Read indexes, then verify. */
168                 cons = intf->rsp_cons;
169                 prod = intf->rsp_prod;
170                 if (!check_indexes(cons, prod)) {
171                         intf->rsp_cons = intf->rsp_prod = 0;
172                         return -EIO;
173                 }
174
175                 src = get_input_chunk(cons, prod, intf->rsp, &avail);
176                 if (avail == 0)
177                         continue;
178                 if (avail > len)
179                         avail = len;
180
181                 /* Must read data /after/ reading the producer index. */
182                 rmb();
183
184                 memcpy(data, src, avail);
185                 data += avail;
186                 len -= avail;
187
188                 /* Other side must not see free space until we've copied out */
189                 mb();
190                 intf->rsp_cons += avail;
191
192                 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
193
194                 /* Implies mb(): other side will see the updated consumer. */
195                 notify_remote_via_evtchn(xen_store_evtchn);
196         }
197
198         return 0;
199 }
200
201 /**
202  * xb_init_comms - Set up interrupt handler off store event channel.
203  */
204 int xb_init_comms(void)
205 {
206         struct xenstore_domain_interface *intf = xen_store_interface;
207
208         if (intf->req_prod != intf->req_cons)
209                 printk(KERN_ERR "XENBUS request ring is not quiescent "
210                        "(%08x:%08x)!\n", intf->req_cons, intf->req_prod);
211
212         if (intf->rsp_prod != intf->rsp_cons) {
213                 printk(KERN_WARNING "XENBUS response ring is not quiescent "
214                        "(%08x:%08x): fixing up\n",
215                        intf->rsp_cons, intf->rsp_prod);
216                 intf->rsp_cons = intf->rsp_prod;
217         }
218
219         if (xenbus_irq) {
220                 /* Already have an irq; assume we're resuming */
221                 rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
222         } else {
223                 int err;
224                 err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
225                                                 0, "xenbus", &xb_waitq);
226                 if (err <= 0) {
227                         printk(KERN_ERR "XENBUS request irq failed %i\n", err);
228                         return err;
229                 }
230
231                 xenbus_irq = err;
232         }
233
234         return 0;
235 }