1/* $NetBSD: xenbus_comms.c,v 1.13 2011/07/02 19:07:56 jym Exp $ */
2/******************************************************************************
3 * xenbus_comms.c
4 *
5 * Low level code to talks to Xen Store: ringbuffer and event channel.
6 *
7 * Copyright (C) 2005 Rusty Russell, IBM Corporation
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
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
28 * IN THE SOFTWARE.
29 */
30
31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.13 2011/07/02 19:07:56 jym Exp $");
33
34#include <sys/types.h>
35#include <sys/null.h>
36#include <sys/errno.h>
37#include <sys/param.h>
38#include <sys/proc.h>
39#include <sys/systm.h>
40
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"
46
47#undef XENDEBUG
48#ifdef XENDEBUG
49#define XENPRINTF(x) printf x
50#else
51#define XENPRINTF(x)
52#endif
53
54struct xenstore_domain_interface *xenstore_interface;
55
56extern int xenstored_ready;
57// static DECLARE_WORK(probe_work, xenbus_probe, NULL);
58
59static int wake_waiting(void *);
60static int check_indexes(XENSTORE_RING_IDX, XENSTORE_RING_IDX);
61static void *get_output_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
62    char *, uint32_t *);
63static const void *get_input_chunk(XENSTORE_RING_IDX, XENSTORE_RING_IDX,
64    const char *, uint32_t *);
65
66
67static inline struct xenstore_domain_interface *
68xenstore_domain_interface(void)
69{
70	return xenstore_interface;
71}
72
73static int
74wake_waiting(void *arg)
75{
76	if (__predict_false(xenstored_ready == 0 && xendomain_is_dom0())) {
77		xenstored_ready = 1;
78		wakeup(&xenstored_ready);
79	}
80
81	wakeup(&xenstore_interface);
82	return 1;
83}
84
85static int
86check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
87{
88	return ((prod - cons) <= XENSTORE_RING_SIZE);
89}
90
91static void *
92get_output_chunk(XENSTORE_RING_IDX cons,
93			      XENSTORE_RING_IDX prod,
94			      char *buf, uint32_t *len)
95{
96	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
97	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
98		*len = XENSTORE_RING_SIZE - (prod - cons);
99	return buf + MASK_XENSTORE_IDX(prod);
100}
101
102static const void *
103get_input_chunk(XENSTORE_RING_IDX cons,
104				   XENSTORE_RING_IDX prod,
105				   const char *buf, uint32_t *len)
106{
107	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
108	if ((prod - cons) < *len)
109		*len = prod - cons;
110	return buf + MASK_XENSTORE_IDX(cons);
111}
112
113int
114xb_write(const void *data, unsigned len)
115{
116	struct xenstore_domain_interface *intf = xenstore_domain_interface();
117	XENSTORE_RING_IDX cons, prod;
118
119	int s = spltty();
120
121	while (len != 0) {
122		void *dst;
123		unsigned int avail;
124
125		while ((intf->req_prod - intf->req_cons) == XENSTORE_RING_SIZE) {
126			XENPRINTF(("xb_write tsleep\n"));
127			tsleep(&xenstore_interface, PRIBIO, "wrst", 0);
128			XENPRINTF(("xb_write tsleep done\n"));
129		}
130
131		/* Read indexes, then verify. */
132		cons = intf->req_cons;
133		prod = intf->req_prod;
134		xen_rmb();
135		if (!check_indexes(cons, prod)) {
136			splx(s);
137			return EIO;
138		}
139
140		dst = get_output_chunk(cons, prod, intf->req, &avail);
141		if (avail == 0)
142			continue;
143		if (avail > len)
144			avail = len;
145
146		memcpy(dst, data, avail);
147		data = (const char *)data + avail;
148		len -= avail;
149
150		/* Other side must not see new header until data is there. */
151		xen_rmb();
152		intf->req_prod += avail;
153		xen_rmb();
154
155		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
156	}
157
158	splx(s);
159	return 0;
160}
161
162int
163xb_read(void *data, unsigned len)
164{
165	struct xenstore_domain_interface *intf = xenstore_domain_interface();
166	XENSTORE_RING_IDX cons, prod;
167
168	int s = spltty();
169
170	while (len != 0) {
171		unsigned int avail;
172		const char *src;
173
174		while (intf->rsp_cons == intf->rsp_prod)
175			tsleep(&xenstore_interface, PRIBIO, "rdst", 0);
176
177		/* Read indexes, then verify. */
178		cons = intf->rsp_cons;
179		prod = intf->rsp_prod;
180		xen_rmb();
181		if (!check_indexes(cons, prod)) {
182			XENPRINTF(("xb_read EIO\n"));
183			splx(s);
184			return EIO;
185		}
186
187		src = get_input_chunk(cons, prod, intf->rsp, &avail);
188		if (avail == 0)
189			continue;
190		if (avail > len)
191			avail = len;
192
193		/* We must read header before we read data. */
194		xen_rmb();
195
196		memcpy(data, src, avail);
197		data = (char *)data + avail;
198		len -= avail;
199
200		/* Other side must not see free space until we've copied out */
201		xen_rmb();
202		intf->rsp_cons += avail;
203		xen_rmb();
204
205		XENPRINTF(("Finished read of %i bytes (%i to go)\n",
206		    avail, len));
207
208		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
209	}
210
211	splx(s);
212	return 0;
213}
214
215/* Set up interrupt handler of store event channel. */
216int
217xb_init_comms(device_t dev)
218{
219	int evtchn;
220
221	evtchn = xen_start_info.store_evtchn;
222
223	event_set_handler(evtchn, wake_waiting, NULL, IPL_TTY, "xenbus");
224	hypervisor_enable_event(evtchn);
225	aprint_verbose_dev(dev, "using event channel %d\n", evtchn);
226
227	return 0;
228}
229
230void
231xb_suspend_comms(device_t dev)
232{
233	int evtchn;
234
235	evtchn = xen_start_info.store_evtchn;
236
237	hypervisor_mask_event(evtchn);
238	event_remove_handler(evtchn, wake_waiting, NULL);
239	aprint_verbose_dev(dev, "removed event channel %d\n", evtchn);
240}
241
242/*
243 * Local variables:
244 *  c-file-style: "linux"
245 *  indent-tabs-mode: t
246 *  c-indent-level: 8
247 *  c-basic-offset: 8
248 *  tab-width: 8
249 * End:
250 */
251