- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.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 #if defined(CONFIG_XEN) || defined(MODULE)
39 #include <xen/evtchn.h>
40 #include <asm/hypervisor.h>
41 #else
42 #include <asm/xen/hypervisor.h>
43 #include <xen/events.h>
44 #include <xen/page.h>
45 #endif
46
47 #include "xenbus_comms.h"
48
49 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
50 #include <xen/platform-compat.h>
51 #endif
52
53 static int xenbus_irq;
54
55 static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
56
57 static irqreturn_t wake_waiting(int irq, void *unused)
58 {
59 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
60         static DECLARE_WORK(probe_work, xenbus_probe);
61         int old, new;
62
63         old = atomic_read(&xenbus_xsd_state);
64         switch (old) {
65                 case XENBUS_XSD_UNCOMMITTED:
66                         BUG();
67                         return IRQ_HANDLED;
68
69                 case XENBUS_XSD_FOREIGN_INIT:
70                         new = XENBUS_XSD_FOREIGN_READY;
71                         break;
72
73                 case XENBUS_XSD_LOCAL_INIT:
74                         new = XENBUS_XSD_LOCAL_READY;
75                         break;
76
77                 case XENBUS_XSD_FOREIGN_READY:
78                 case XENBUS_XSD_LOCAL_READY:
79                 default:
80                         goto wake;
81         }
82
83         old = atomic_cmpxchg(&xenbus_xsd_state, old, new);
84         if (old != new)
85                 schedule_work(&probe_work);
86
87 wake:
88 #endif
89         wake_up(&xb_waitq);
90         return IRQ_HANDLED;
91 }
92
93 static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
94 {
95         return ((prod - cons) <= XENSTORE_RING_SIZE);
96 }
97
98 static void *get_output_chunk(XENSTORE_RING_IDX cons,
99                               XENSTORE_RING_IDX prod,
100                               char *buf, uint32_t *len)
101 {
102         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
103         if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
104                 *len = XENSTORE_RING_SIZE - (prod - cons);
105         return buf + MASK_XENSTORE_IDX(prod);
106 }
107
108 static const void *get_input_chunk(XENSTORE_RING_IDX cons,
109                                    XENSTORE_RING_IDX prod,
110                                    const char *buf, uint32_t *len)
111 {
112         *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
113         if ((prod - cons) < *len)
114                 *len = prod - cons;
115         return buf + MASK_XENSTORE_IDX(cons);
116 }
117
118 /**
119  * xb_write - low level write
120  * @data: buffer to send
121  * @len: length of buffer
122  *
123  * Returns 0 on success, error otherwise.
124  */
125 int xb_write(const void *data, unsigned len)
126 {
127         struct xenstore_domain_interface *intf = xen_store_interface;
128         XENSTORE_RING_IDX cons, prod;
129         int rc;
130
131         while (len != 0) {
132                 void *dst;
133                 unsigned int avail;
134
135                 rc = wait_event_interruptible(
136                         xb_waitq,
137                         (intf->req_prod - intf->req_cons) !=
138                         XENSTORE_RING_SIZE);
139                 if (rc < 0)
140                         return rc;
141
142                 /* Read indexes, then verify. */
143                 cons = intf->req_cons;
144                 prod = intf->req_prod;
145                 if (!check_indexes(cons, prod)) {
146                         intf->req_cons = intf->req_prod = 0;
147                         return -EIO;
148                 }
149
150                 dst = get_output_chunk(cons, prod, intf->req, &avail);
151                 if (avail == 0)
152                         continue;
153                 if (avail > len)
154                         avail = len;
155
156                 /* Must write data /after/ reading the consumer index. */
157                 mb();
158
159                 memcpy(dst, data, avail);
160                 data += avail;
161                 len -= avail;
162
163                 /* Other side must not see new producer until data is there. */
164                 wmb();
165                 intf->req_prod += avail;
166
167                 /* Implies mb(): other side will see the updated producer. */
168                 notify_remote_via_evtchn(xen_store_evtchn);
169         }
170
171         return 0;
172 }
173
174 int xb_data_to_read(void)
175 {
176         struct xenstore_domain_interface *intf = xen_store_interface;
177         return (intf->rsp_cons != intf->rsp_prod);
178 }
179
180 int xb_wait_for_data_to_read(void)
181 {
182         return wait_event_interruptible(xb_waitq, xb_data_to_read());
183 }
184
185 int xb_read(void *data, unsigned len)
186 {
187         struct xenstore_domain_interface *intf = xen_store_interface;
188         XENSTORE_RING_IDX cons, prod;
189         int rc;
190
191         while (len != 0) {
192                 unsigned int avail;
193                 const char *src;
194
195                 rc = xb_wait_for_data_to_read();
196                 if (rc < 0)
197                         return rc;
198
199                 /* Read indexes, then verify. */
200                 cons = intf->rsp_cons;
201                 prod = intf->rsp_prod;
202                 if (!check_indexes(cons, prod)) {
203                         intf->rsp_cons = intf->rsp_prod = 0;
204                         return -EIO;
205                 }
206
207                 src = get_input_chunk(cons, prod, intf->rsp, &avail);
208                 if (avail == 0)
209                         continue;
210                 if (avail > len)
211                         avail = len;
212
213                 /* Must read data /after/ reading the producer index. */
214                 rmb();
215
216                 memcpy(data, src, avail);
217                 data += avail;
218                 len -= avail;
219
220                 /* Other side must not see free space until we've copied out */
221                 mb();
222                 intf->rsp_cons += avail;
223
224                 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
225
226                 /* Implies mb(): other side will see the updated consumer. */
227                 notify_remote_via_evtchn(xen_store_evtchn);
228         }
229
230         return 0;
231 }
232
233 /**
234  * xb_init_comms - Set up interrupt handler off store event channel.
235  */
236 int xb_init_comms(void)
237 {
238         struct xenstore_domain_interface *intf = xen_store_interface;
239         int err;
240
241         if (intf->req_prod != intf->req_cons)
242                 pr_err("XENBUS request ring is not quiescent "
243                        "(%08x:%08x)!\n", intf->req_cons, intf->req_prod);
244
245         if (intf->rsp_prod != intf->rsp_cons) {
246                 pr_warning("XENBUS response ring is not quiescent"
247                            " (%08x:%08x): fixing up\n",
248                            intf->rsp_cons, intf->rsp_prod);
249                 /* breaks kdump */
250                 if (!reset_devices)
251                         intf->rsp_cons = intf->rsp_prod;
252         }
253
254 #if defined(CONFIG_XEN) || defined(MODULE)
255         if (xenbus_irq)
256                 unbind_from_irqhandler(xenbus_irq, &xb_waitq);
257
258         err = bind_caller_port_to_irqhandler(
259                 xen_store_evtchn, wake_waiting,
260                 0, "xenbus", &xb_waitq);
261         if (err <= 0) {
262                 pr_err("XENBUS request irq failed %i\n", err);
263                 return err;
264         }
265
266         xenbus_irq = err;
267 #else
268         if (xenbus_irq) {
269                 /* Already have an irq; assume we're resuming */
270                 rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
271         } else {
272                 err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
273                                                 0, "xenbus", &xb_waitq);
274                 if (err <= 0) {
275                         pr_err("XENBUS request irq failed %i\n", err);
276                         return err;
277                 }
278                 xenbus_irq = err;
279         }
280 #endif
281
282         return 0;
283 }