xenbus_comms.c revision 1.14 1 1.14 jym /* $NetBSD: xenbus_comms.c,v 1.14 2011/09/20 00:12:24 jym Exp $ */
2 1.1 bouyer /******************************************************************************
3 1.1 bouyer * xenbus_comms.c
4 1.1 bouyer *
5 1.1 bouyer * Low level code to talks to Xen Store: ringbuffer and event channel.
6 1.1 bouyer *
7 1.1 bouyer * Copyright (C) 2005 Rusty Russell, IBM Corporation
8 1.1 bouyer *
9 1.1 bouyer * This file may be distributed separately from the Linux kernel, or
10 1.1 bouyer * incorporated into other software packages, subject to the following license:
11 1.1 bouyer *
12 1.1 bouyer * Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 bouyer * of this source file (the "Software"), to deal in the Software without
14 1.1 bouyer * restriction, including without limitation the rights to use, copy, modify,
15 1.1 bouyer * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 1.1 bouyer * and to permit persons to whom the Software is furnished to do so, subject to
17 1.1 bouyer * the following conditions:
18 1.1 bouyer *
19 1.1 bouyer * The above copyright notice and this permission notice shall be included in
20 1.1 bouyer * all copies or substantial portions of the Software.
21 1.1 bouyer *
22 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 bouyer * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 bouyer * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 bouyer * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 1.1 bouyer * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 1.1 bouyer * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 1.1 bouyer * IN THE SOFTWARE.
29 1.1 bouyer */
30 1.1 bouyer
31 1.2 bouyer #include <sys/cdefs.h>
32 1.14 jym __KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.14 2011/09/20 00:12:24 jym Exp $");
33 1.2 bouyer
34 1.2 bouyer #include <sys/types.h>
35 1.2 bouyer #include <sys/null.h>
36 1.2 bouyer #include <sys/errno.h>
37 1.2 bouyer #include <sys/param.h>
38 1.2 bouyer #include <sys/proc.h>
39 1.2 bouyer #include <sys/systm.h>
40 1.2 bouyer
41 1.10 cegger #include <xen/xen.h> /* for xendomain_is_dom0() */
42 1.5 bouyer #include <xen/hypervisor.h>
43 1.5 bouyer #include <xen/evtchn.h>
44 1.5 bouyer #include <xen/xenbus.h>
45 1.1 bouyer #include "xenbus_comms.h"
46 1.1 bouyer
47 1.2 bouyer #undef XENDEBUG
48 1.2 bouyer #ifdef XENDEBUG
49 1.2 bouyer #define XENPRINTF(x) printf x
50 1.2 bouyer #else
51 1.2 bouyer #define XENPRINTF(x)
52 1.2 bouyer #endif
53 1.2 bouyer
54 1.2 bouyer struct xenstore_domain_interface *xenstore_interface;
55 1.2 bouyer
56 1.1 bouyer extern int xenstored_ready;
57 1.2 bouyer // static DECLARE_WORK(probe_work, xenbus_probe, NULL);
58 1.2 bouyer
59 1.2 bouyer static int wake_waiting(void *);
60 1.2 bouyer static int check_indexes(XENSTORE_RING_IDX, XENSTORE_RING_IDX);
61 1.4 christos static void *get_output_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
62 1.2 bouyer char *, uint32_t *);
63 1.4 christos static const void *get_input_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
64 1.2 bouyer const char *, uint32_t *);
65 1.1 bouyer
66 1.1 bouyer
67 1.2 bouyer static inline struct xenstore_domain_interface *
68 1.2 bouyer xenstore_domain_interface(void)
69 1.1 bouyer {
70 1.2 bouyer return xenstore_interface;
71 1.1 bouyer }
72 1.1 bouyer
73 1.2 bouyer static int
74 1.2 bouyer wake_waiting(void *arg)
75 1.1 bouyer {
76 1.8 cegger if (__predict_false(xenstored_ready == 0 && xendomain_is_dom0())) {
77 1.1 bouyer xenstored_ready = 1;
78 1.3 bouyer wakeup(&xenstored_ready);
79 1.1 bouyer }
80 1.1 bouyer
81 1.2 bouyer wakeup(&xenstore_interface);
82 1.2 bouyer return 1;
83 1.1 bouyer }
84 1.1 bouyer
85 1.2 bouyer static int
86 1.2 bouyer check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
87 1.1 bouyer {
88 1.1 bouyer return ((prod - cons) <= XENSTORE_RING_SIZE);
89 1.1 bouyer }
90 1.1 bouyer
91 1.2 bouyer static void *
92 1.2 bouyer get_output_chunk(XENSTORE_RING_IDX cons,
93 1.1 bouyer XENSTORE_RING_IDX prod,
94 1.1 bouyer char *buf, uint32_t *len)
95 1.1 bouyer {
96 1.1 bouyer *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
97 1.1 bouyer if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
98 1.1 bouyer *len = XENSTORE_RING_SIZE - (prod - cons);
99 1.1 bouyer return buf + MASK_XENSTORE_IDX(prod);
100 1.1 bouyer }
101 1.1 bouyer
102 1.2 bouyer static const void *
103 1.2 bouyer get_input_chunk(XENSTORE_RING_IDX cons,
104 1.1 bouyer XENSTORE_RING_IDX prod,
105 1.1 bouyer const char *buf, uint32_t *len)
106 1.1 bouyer {
107 1.1 bouyer *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
108 1.1 bouyer if ((prod - cons) < *len)
109 1.1 bouyer *len = prod - cons;
110 1.1 bouyer return buf + MASK_XENSTORE_IDX(cons);
111 1.1 bouyer }
112 1.1 bouyer
113 1.2 bouyer int
114 1.2 bouyer xb_write(const void *data, unsigned len)
115 1.1 bouyer {
116 1.1 bouyer struct xenstore_domain_interface *intf = xenstore_domain_interface();
117 1.1 bouyer XENSTORE_RING_IDX cons, prod;
118 1.1 bouyer
119 1.2 bouyer int s = spltty();
120 1.2 bouyer
121 1.1 bouyer while (len != 0) {
122 1.1 bouyer void *dst;
123 1.1 bouyer unsigned int avail;
124 1.1 bouyer
125 1.2 bouyer while ((intf->req_prod - intf->req_cons) == XENSTORE_RING_SIZE) {
126 1.2 bouyer XENPRINTF(("xb_write tsleep\n"));
127 1.2 bouyer tsleep(&xenstore_interface, PRIBIO, "wrst", 0);
128 1.2 bouyer XENPRINTF(("xb_write tsleep done\n"));
129 1.2 bouyer }
130 1.1 bouyer
131 1.1 bouyer /* Read indexes, then verify. */
132 1.1 bouyer cons = intf->req_cons;
133 1.1 bouyer prod = intf->req_prod;
134 1.12 jym xen_rmb();
135 1.2 bouyer if (!check_indexes(cons, prod)) {
136 1.2 bouyer splx(s);
137 1.2 bouyer return EIO;
138 1.2 bouyer }
139 1.1 bouyer
140 1.1 bouyer dst = get_output_chunk(cons, prod, intf->req, &avail);
141 1.1 bouyer if (avail == 0)
142 1.1 bouyer continue;
143 1.1 bouyer if (avail > len)
144 1.1 bouyer avail = len;
145 1.1 bouyer
146 1.1 bouyer memcpy(dst, data, avail);
147 1.2 bouyer data = (const char *)data + avail;
148 1.1 bouyer len -= avail;
149 1.1 bouyer
150 1.1 bouyer /* Other side must not see new header until data is there. */
151 1.12 jym xen_rmb();
152 1.1 bouyer intf->req_prod += avail;
153 1.12 jym xen_rmb();
154 1.1 bouyer
155 1.2 bouyer hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
156 1.1 bouyer }
157 1.1 bouyer
158 1.2 bouyer splx(s);
159 1.1 bouyer return 0;
160 1.1 bouyer }
161 1.1 bouyer
162 1.2 bouyer int
163 1.2 bouyer xb_read(void *data, unsigned len)
164 1.1 bouyer {
165 1.1 bouyer struct xenstore_domain_interface *intf = xenstore_domain_interface();
166 1.1 bouyer XENSTORE_RING_IDX cons, prod;
167 1.1 bouyer
168 1.2 bouyer int s = spltty();
169 1.2 bouyer
170 1.1 bouyer while (len != 0) {
171 1.1 bouyer unsigned int avail;
172 1.1 bouyer const char *src;
173 1.1 bouyer
174 1.2 bouyer while (intf->rsp_cons == intf->rsp_prod)
175 1.2 bouyer tsleep(&xenstore_interface, PRIBIO, "rdst", 0);
176 1.1 bouyer
177 1.1 bouyer /* Read indexes, then verify. */
178 1.1 bouyer cons = intf->rsp_cons;
179 1.1 bouyer prod = intf->rsp_prod;
180 1.12 jym xen_rmb();
181 1.2 bouyer if (!check_indexes(cons, prod)) {
182 1.2 bouyer XENPRINTF(("xb_read EIO\n"));
183 1.2 bouyer splx(s);
184 1.2 bouyer return EIO;
185 1.2 bouyer }
186 1.1 bouyer
187 1.1 bouyer src = get_input_chunk(cons, prod, intf->rsp, &avail);
188 1.1 bouyer if (avail == 0)
189 1.1 bouyer continue;
190 1.1 bouyer if (avail > len)
191 1.1 bouyer avail = len;
192 1.1 bouyer
193 1.1 bouyer /* We must read header before we read data. */
194 1.12 jym xen_rmb();
195 1.1 bouyer
196 1.1 bouyer memcpy(data, src, avail);
197 1.2 bouyer data = (char *)data + avail;
198 1.1 bouyer len -= avail;
199 1.1 bouyer
200 1.1 bouyer /* Other side must not see free space until we've copied out */
201 1.12 jym xen_rmb();
202 1.1 bouyer intf->rsp_cons += avail;
203 1.12 jym xen_rmb();
204 1.1 bouyer
205 1.2 bouyer XENPRINTF(("Finished read of %i bytes (%i to go)\n",
206 1.2 bouyer avail, len));
207 1.1 bouyer
208 1.2 bouyer hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
209 1.1 bouyer }
210 1.1 bouyer
211 1.2 bouyer splx(s);
212 1.1 bouyer return 0;
213 1.1 bouyer }
214 1.1 bouyer
215 1.9 jym /* Set up interrupt handler of store event channel. */
216 1.2 bouyer int
217 1.7 cegger xb_init_comms(device_t dev)
218 1.1 bouyer {
219 1.14 jym int evtchn;
220 1.14 jym
221 1.14 jym evtchn = xen_start_info.store_evtchn;
222 1.1 bouyer
223 1.14 jym event_set_handler(evtchn, wake_waiting, NULL, IPL_TTY, "xenbus");
224 1.14 jym hypervisor_enable_event(evtchn);
225 1.14 jym aprint_verbose_dev(dev, "using event channel %d\n", evtchn);
226 1.13 jym
227 1.1 bouyer return 0;
228 1.1 bouyer }
229 1.1 bouyer
230 1.14 jym void
231 1.14 jym xb_suspend_comms(device_t dev)
232 1.14 jym {
233 1.14 jym int evtchn;
234 1.14 jym
235 1.14 jym evtchn = xen_start_info.store_evtchn;
236 1.14 jym
237 1.14 jym hypervisor_mask_event(evtchn);
238 1.14 jym event_remove_handler(evtchn, wake_waiting, NULL);
239 1.14 jym aprint_verbose_dev(dev, "removed event channel %d\n", evtchn);
240 1.14 jym }
241 1.14 jym
242 1.1 bouyer /*
243 1.1 bouyer * Local variables:
244 1.1 bouyer * c-file-style: "linux"
245 1.1 bouyer * indent-tabs-mode: t
246 1.1 bouyer * c-indent-level: 8
247 1.1 bouyer * c-basic-offset: 8
248 1.1 bouyer * tab-width: 8
249 1.1 bouyer * End:
250 1.1 bouyer */
251