ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_comms.c @ 876:bd7e30b58d12

xenbus: Allow lazy init in case xenstored runs in a separate minios domain.

Here's an explanation of the states:

It starts out in XENBUS_XSD_UNCOMMITTED.

As the master xenbus (the one local to xenstored), it will receive an
mmap from xenstore, putting it in XENBUS_XSD_LOCAL_INIT. This enables
the wake_waiting IRQ, which will put it in XENBUS_XSD_LOCAL_READY.

Alternatively, as a slave xenbus, it will receive an ioctl from the
xenstore domain builder, putting it in XENBUS_XSD_FOREIGN_INIT. This
enables the wake_waiting IRQ, which will put it in
XENBUS_XSD_FOREIGN_READY.

DomU's are immediately initialized to XENBUS_XSD_FOREIGN_READY.

Signed-off-by: Diego Ongaro <diego.ongaro@citrix.com>
Signed-off-by: Alex Zeffertt <alex.zeffertt@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 19 14:45:50 2009 +0100 (2009-05-19)
parents 5027cbb9e3eb
children
line source
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 */
33 #include <linux/wait.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/err.h>
37 #include <linux/ptrace.h>
38 #include <linux/workqueue.h>
39 #include <xen/evtchn.h>
40 #include <xen/xenbus.h>
42 #include <asm/hypervisor.h>
44 #include "xenbus_comms.h"
46 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
47 #include <xen/platform-compat.h>
48 #endif
50 static int xenbus_irq;
52 extern void xenbus_probe(void *);
53 static DECLARE_WORK(probe_work, xenbus_probe, NULL);
55 static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
57 static irqreturn_t wake_waiting(int irq, void *unused, struct pt_regs *regs)
58 {
59 int old, new;
61 old = atomic_read(&xenbus_xsd_state);
62 switch (old) {
63 case XENBUS_XSD_UNCOMMITTED:
64 BUG();
65 return IRQ_HANDLED;
67 case XENBUS_XSD_FOREIGN_INIT:
68 new = XENBUS_XSD_FOREIGN_READY;
69 break;
71 case XENBUS_XSD_LOCAL_INIT:
72 new = XENBUS_XSD_LOCAL_READY;
73 break;
75 case XENBUS_XSD_FOREIGN_READY:
76 case XENBUS_XSD_LOCAL_READY:
77 default:
78 goto wake;
79 }
81 old = atomic_cmpxchg(&xenbus_xsd_state, old, new);
82 if (old != new)
83 schedule_work(&probe_work);
85 wake:
86 wake_up(&xb_waitq);
87 return IRQ_HANDLED;
88 }
90 static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
91 {
92 return ((prod - cons) <= XENSTORE_RING_SIZE);
93 }
95 static void *get_output_chunk(XENSTORE_RING_IDX cons,
96 XENSTORE_RING_IDX prod,
97 char *buf, uint32_t *len)
98 {
99 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
100 if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
101 *len = XENSTORE_RING_SIZE - (prod - cons);
102 return buf + MASK_XENSTORE_IDX(prod);
103 }
105 static const void *get_input_chunk(XENSTORE_RING_IDX cons,
106 XENSTORE_RING_IDX prod,
107 const char *buf, uint32_t *len)
108 {
109 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
110 if ((prod - cons) < *len)
111 *len = prod - cons;
112 return buf + MASK_XENSTORE_IDX(cons);
113 }
115 int xb_write(const void *data, unsigned len)
116 {
117 struct xenstore_domain_interface *intf = xen_store_interface;
118 XENSTORE_RING_IDX cons, prod;
119 int rc;
121 while (len != 0) {
122 void *dst;
123 unsigned int avail;
125 rc = wait_event_interruptible(
126 xb_waitq,
127 (intf->req_prod - intf->req_cons) !=
128 XENSTORE_RING_SIZE);
129 if (rc < 0)
130 return rc;
132 /* Read indexes, then verify. */
133 cons = intf->req_cons;
134 prod = intf->req_prod;
135 if (!check_indexes(cons, prod)) {
136 intf->req_cons = intf->req_prod = 0;
137 return -EIO;
138 }
140 dst = get_output_chunk(cons, prod, intf->req, &avail);
141 if (avail == 0)
142 continue;
143 if (avail > len)
144 avail = len;
146 /* Must write data /after/ reading the consumer index. */
147 mb();
149 memcpy(dst, data, avail);
150 data += avail;
151 len -= avail;
153 /* Other side must not see new producer until data is there. */
154 wmb();
155 intf->req_prod += avail;
157 /* Implies mb(): other side will see the updated producer. */
158 notify_remote_via_evtchn(xen_store_evtchn);
159 }
161 return 0;
162 }
164 int xb_data_to_read(void)
165 {
166 struct xenstore_domain_interface *intf = xen_store_interface;
167 return (intf->rsp_cons != intf->rsp_prod);
168 }
170 int xb_wait_for_data_to_read(void)
171 {
172 return wait_event_interruptible(xb_waitq, xb_data_to_read());
173 }
175 int xb_read(void *data, unsigned len)
176 {
177 struct xenstore_domain_interface *intf = xen_store_interface;
178 XENSTORE_RING_IDX cons, prod;
179 int rc;
181 while (len != 0) {
182 unsigned int avail;
183 const char *src;
185 rc = xb_wait_for_data_to_read();
186 if (rc < 0)
187 return rc;
189 /* Read indexes, then verify. */
190 cons = intf->rsp_cons;
191 prod = intf->rsp_prod;
192 if (!check_indexes(cons, prod)) {
193 intf->rsp_cons = intf->rsp_prod = 0;
194 return -EIO;
195 }
197 src = get_input_chunk(cons, prod, intf->rsp, &avail);
198 if (avail == 0)
199 continue;
200 if (avail > len)
201 avail = len;
203 /* Must read data /after/ reading the producer index. */
204 rmb();
206 memcpy(data, src, avail);
207 data += avail;
208 len -= avail;
210 /* Other side must not see free space until we've copied out */
211 mb();
212 intf->rsp_cons += avail;
214 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
216 /* Implies mb(): other side will see the updated consumer. */
217 notify_remote_via_evtchn(xen_store_evtchn);
218 }
220 return 0;
221 }
223 /* Set up interrupt handler off store event channel. */
224 int xb_init_comms(void)
225 {
226 struct xenstore_domain_interface *intf = xen_store_interface;
227 int err;
229 if (intf->req_prod != intf->req_cons)
230 printk(KERN_ERR "XENBUS request ring is not quiescent "
231 "(%08x:%08x)!\n", intf->req_cons, intf->req_prod);
233 if (intf->rsp_prod != intf->rsp_cons) {
234 printk(KERN_WARNING "XENBUS response ring is not quiescent "
235 "(%08x:%08x): fixing up\n",
236 intf->rsp_cons, intf->rsp_prod);
237 intf->rsp_cons = intf->rsp_prod;
238 }
240 if (xenbus_irq)
241 unbind_from_irqhandler(xenbus_irq, &xb_waitq);
243 err = bind_caller_port_to_irqhandler(
244 xen_store_evtchn, wake_waiting,
245 0, "xenbus", &xb_waitq);
246 if (err <= 0) {
247 printk(KERN_ERR "XENBUS request irq failed %i\n", err);
248 return err;
249 }
251 xenbus_irq = err;
253 return 0;
254 }