ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_comms.c @ 13665:140afd7a5462

merge with xen-unstable.hg
author awilliam@xenbuild2.aw
date Sat Jan 27 14:07:19 2007 -0700 (2007-01-27)
parents d275951acf10
children b67c253d1cdb
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 <xen/evtchn.h>
39 #include <xen/xenbus.h>
41 #include <asm/hypervisor.h>
43 #include "xenbus_comms.h"
45 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
46 #include <xen/platform-compat.h>
47 #endif
49 static int xenbus_irq;
51 extern void xenbus_probe(void *);
52 extern int xenstored_ready;
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 if (unlikely(xenstored_ready == 0)) {
60 xenstored_ready = 1;
61 schedule_work(&probe_work);
62 }
64 wake_up(&xb_waitq);
65 return IRQ_HANDLED;
66 }
68 static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
69 {
70 return ((prod - cons) <= XENSTORE_RING_SIZE);
71 }
73 static void *get_output_chunk(XENSTORE_RING_IDX cons,
74 XENSTORE_RING_IDX prod,
75 char *buf, uint32_t *len)
76 {
77 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
78 if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
79 *len = XENSTORE_RING_SIZE - (prod - cons);
80 return buf + MASK_XENSTORE_IDX(prod);
81 }
83 static const void *get_input_chunk(XENSTORE_RING_IDX cons,
84 XENSTORE_RING_IDX prod,
85 const char *buf, uint32_t *len)
86 {
87 *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
88 if ((prod - cons) < *len)
89 *len = prod - cons;
90 return buf + MASK_XENSTORE_IDX(cons);
91 }
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;
99 while (len != 0) {
100 void *dst;
101 unsigned int avail;
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;
110 /* Read indexes, then verify. */
111 cons = intf->req_cons;
112 prod = intf->req_prod;
113 mb();
114 if (!check_indexes(cons, prod)) {
115 intf->req_cons = intf->req_prod = 0;
116 return -EIO;
117 }
119 dst = get_output_chunk(cons, prod, intf->req, &avail);
120 if (avail == 0)
121 continue;
122 if (avail > len)
123 avail = len;
125 memcpy(dst, data, avail);
126 data += avail;
127 len -= avail;
129 /* Other side must not see new header until data is there. */
130 wmb();
131 intf->req_prod += avail;
133 /* This implies mb() before other side sees interrupt. */
134 notify_remote_via_evtchn(xen_store_evtchn);
135 }
137 return 0;
138 }
140 int xb_read(void *data, unsigned len)
141 {
142 struct xenstore_domain_interface *intf = xen_store_interface;
143 XENSTORE_RING_IDX cons, prod;
144 int rc;
146 while (len != 0) {
147 unsigned int avail;
148 const char *src;
150 rc = wait_event_interruptible(
151 xb_waitq,
152 intf->rsp_cons != intf->rsp_prod);
153 if (rc < 0)
154 return rc;
156 /* Read indexes, then verify. */
157 cons = intf->rsp_cons;
158 prod = intf->rsp_prod;
159 mb();
160 if (!check_indexes(cons, prod)) {
161 intf->rsp_cons = intf->rsp_prod = 0;
162 return -EIO;
163 }
165 src = get_input_chunk(cons, prod, intf->rsp, &avail);
166 if (avail == 0)
167 continue;
168 if (avail > len)
169 avail = len;
171 /* We must read header before we read data. */
172 rmb();
174 memcpy(data, src, avail);
175 data += avail;
176 len -= avail;
178 /* Other side must not see free space until we've copied out */
179 mb();
180 intf->rsp_cons += avail;
182 pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
184 /* Implies mb(): they will see new header. */
185 notify_remote_via_evtchn(xen_store_evtchn);
186 }
188 return 0;
189 }
191 /* Set up interrupt handler off store event channel. */
192 int xb_init_comms(void)
193 {
194 int err;
196 if (xenbus_irq)
197 unbind_from_irqhandler(xenbus_irq, &xb_waitq);
199 err = bind_caller_port_to_irqhandler(
200 xen_store_evtchn, wake_waiting,
201 0, "xenbus", &xb_waitq);
202 if (err <= 0) {
203 printk(KERN_ERR "XENBUS request irq failed %i\n", err);
204 return err;
205 }
207 xenbus_irq = err;
209 return 0;
210 }