1 /* $NetBSD: xenbus_comms.c,v 1.11 2008/12/18 12:19:03 cegger Exp $ */
2 /******************************************************************************
5 * Low level code to talks to Xen Store: ringbuffer and event channel.
7 * Copyright (C) 2005 Rusty Russell, IBM Corporation
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.11 2008/12/18 12:19:03 cegger Exp $");
34 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <xen/xen.h> /* for xendomain_is_dom0() */
42 #include <xen/hypervisor.h>
43 #include <xen/evtchn.h>
44 #include <xen/xenbus.h>
45 #include "xenbus_comms.h"
49 #define XENPRINTF(x) printf x
54 struct xenstore_domain_interface
*xenstore_interface
;
56 static int xenbus_irq
= 0;
58 extern int xenstored_ready
;
59 // static DECLARE_WORK(probe_work, xenbus_probe, NULL);
61 static int wake_waiting(void *);
62 static int check_indexes(XENSTORE_RING_IDX
, XENSTORE_RING_IDX
);
63 static void *get_output_chunk(XENSTORE_RING_IDX
, XENSTORE_RING_IDX
,
65 static const void *get_input_chunk(XENSTORE_RING_IDX
, XENSTORE_RING_IDX
,
66 const char *, uint32_t *);
69 static inline struct xenstore_domain_interface
*
70 xenstore_domain_interface(void)
72 return xenstore_interface
;
76 wake_waiting(void *arg
)
78 if (__predict_false(xenstored_ready
== 0 && xendomain_is_dom0())) {
80 wakeup(&xenstored_ready
);
83 wakeup(&xenstore_interface
);
88 check_indexes(XENSTORE_RING_IDX cons
, XENSTORE_RING_IDX prod
)
90 return ((prod
- cons
) <= XENSTORE_RING_SIZE
);
94 get_output_chunk(XENSTORE_RING_IDX cons
,
95 XENSTORE_RING_IDX prod
,
96 char *buf
, uint32_t *len
)
98 *len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX(prod
);
99 if ((XENSTORE_RING_SIZE
- (prod
- cons
)) < *len
)
100 *len
= XENSTORE_RING_SIZE
- (prod
- cons
);
101 return buf
+ MASK_XENSTORE_IDX(prod
);
105 get_input_chunk(XENSTORE_RING_IDX cons
,
106 XENSTORE_RING_IDX prod
,
107 const char *buf
, uint32_t *len
)
109 *len
= XENSTORE_RING_SIZE
- MASK_XENSTORE_IDX(cons
);
110 if ((prod
- cons
) < *len
)
112 return buf
+ MASK_XENSTORE_IDX(cons
);
116 xb_write(const void *data
, unsigned len
)
118 struct xenstore_domain_interface
*intf
= xenstore_domain_interface();
119 XENSTORE_RING_IDX cons
, prod
;
127 while ((intf
->req_prod
- intf
->req_cons
) == XENSTORE_RING_SIZE
) {
128 XENPRINTF(("xb_write tsleep\n"));
129 tsleep(&xenstore_interface
, PRIBIO
, "wrst", 0);
130 XENPRINTF(("xb_write tsleep done\n"));
133 /* Read indexes, then verify. */
134 cons
= intf
->req_cons
;
135 prod
= intf
->req_prod
;
137 if (!check_indexes(cons
, prod
)) {
142 dst
= get_output_chunk(cons
, prod
, intf
->req
, &avail
);
148 memcpy(dst
, data
, avail
);
149 data
= (const char *)data
+ avail
;
152 /* Other side must not see new header until data is there. */
154 intf
->req_prod
+= avail
;
157 hypervisor_notify_via_evtchn(xen_start_info
.store_evtchn
);
165 xb_read(void *data
, unsigned len
)
167 struct xenstore_domain_interface
*intf
= xenstore_domain_interface();
168 XENSTORE_RING_IDX cons
, prod
;
176 while (intf
->rsp_cons
== intf
->rsp_prod
)
177 tsleep(&xenstore_interface
, PRIBIO
, "rdst", 0);
179 /* Read indexes, then verify. */
180 cons
= intf
->rsp_cons
;
181 prod
= intf
->rsp_prod
;
183 if (!check_indexes(cons
, prod
)) {
184 XENPRINTF(("xb_read EIO\n"));
189 src
= get_input_chunk(cons
, prod
, intf
->rsp
, &avail
);
195 /* We must read header before we read data. */
198 memcpy(data
, src
, avail
);
199 data
= (char *)data
+ avail
;
202 /* Other side must not see free space until we've copied out */
204 intf
->rsp_cons
+= avail
;
207 XENPRINTF(("Finished read of %i bytes (%i to go)\n",
210 hypervisor_notify_via_evtchn(xen_start_info
.store_evtchn
);
217 /* Set up interrupt handler of store event channel. */
219 xb_init_comms(device_t dev
)
224 event_remove_handler(xenbus_irq
, wake_waiting
, NULL
);
226 err
= event_set_handler(xen_start_info
.store_evtchn
, wake_waiting
,
227 NULL
, IPL_TTY
, "xenbus");
229 aprint_error_dev(dev
, "request irq failed %i\n", err
);
232 xenbus_irq
= xen_start_info
.store_evtchn
;
233 aprint_verbose_dev(dev
, "using event channel %d\n", xenbus_irq
);
234 hypervisor_enable_event(xenbus_irq
);
240 * c-file-style: "linux"
241 * indent-tabs-mode: t