xref: /netbsd-src/sys/rump/net/lib/libshmif/shmif_busops.c (revision 58dd466be8cfd3459783e670524a4a50e78be3af)
1*58dd466bSozaki-r /*	$NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $	*/
2ce68b7aeSpooka 
3ce68b7aeSpooka /*
423bbd0e0Spooka  * Copyright (c) 2009, 2010 Antti Kantee.  All Rights Reserved.
5ce68b7aeSpooka  *
6ce68b7aeSpooka  * Development of this software was supported by The Nokia Foundation.
7ce68b7aeSpooka  *
8ce68b7aeSpooka  * Redistribution and use in source and binary forms, with or without
9ce68b7aeSpooka  * modification, are permitted provided that the following conditions
10ce68b7aeSpooka  * are met:
11ce68b7aeSpooka  * 1. Redistributions of source code must retain the above copyright
12ce68b7aeSpooka  *    notice, this list of conditions and the following disclaimer.
13ce68b7aeSpooka  * 2. Redistributions in binary form must reproduce the above copyright
14ce68b7aeSpooka  *    notice, this list of conditions and the following disclaimer in the
15ce68b7aeSpooka  *    documentation and/or other materials provided with the distribution.
16ce68b7aeSpooka  *
17ce68b7aeSpooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18ce68b7aeSpooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19ce68b7aeSpooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20ce68b7aeSpooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21ce68b7aeSpooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ce68b7aeSpooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23ce68b7aeSpooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ce68b7aeSpooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ce68b7aeSpooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ce68b7aeSpooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ce68b7aeSpooka  * SUCH DAMAGE.
28ce68b7aeSpooka  */
29ce68b7aeSpooka 
301150b99eSpooka #ifdef _KERNEL
31ce68b7aeSpooka #include <sys/cdefs.h>
32*58dd466bSozaki-r __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $");
331150b99eSpooka #else
341150b99eSpooka #include <rump/rumpuser_port.h>
35*58dd466bSozaki-r __RCSID("$NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $");
361150b99eSpooka #endif
37ce68b7aeSpooka 
38ce68b7aeSpooka #include <sys/param.h>
39ce68b7aeSpooka 
40ce68b7aeSpooka #ifndef _KERNEL
41ce68b7aeSpooka #include <assert.h>
421a7f9ac6Spooka #include <inttypes.h>
436f53b4a3Spooka #include <stdbool.h>
446f53b4a3Spooka #include <string.h>
456f53b4a3Spooka 
46ce68b7aeSpooka #define KASSERT(a) assert(a)
47ce68b7aeSpooka #endif
48ce68b7aeSpooka 
496f53b4a3Spooka #include "shmifvar.h"
506f53b4a3Spooka 
51ce68b7aeSpooka uint32_t
shmif_advance(uint32_t oldoff,uint32_t delta)52ce68b7aeSpooka shmif_advance(uint32_t oldoff, uint32_t delta)
53ce68b7aeSpooka {
54ce68b7aeSpooka 	uint32_t newoff;
55ce68b7aeSpooka 
56ce68b7aeSpooka 	newoff = oldoff + delta;
57ce68b7aeSpooka 	if (newoff >= BUSMEM_DATASIZE)
5811082373Spooka 		newoff -= BUSMEM_DATASIZE;
59ce68b7aeSpooka 	return newoff;
60ce68b7aeSpooka }
61ce68b7aeSpooka 
62ce68b7aeSpooka uint32_t
shmif_busread(struct shmif_mem * busmem,void * dest,uint32_t off,size_t len,bool * wrap)63ce68b7aeSpooka shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
64ce68b7aeSpooka 	bool *wrap)
65ce68b7aeSpooka {
66ce68b7aeSpooka 	size_t chunk;
67ce68b7aeSpooka 
6811082373Spooka 	KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
69ce68b7aeSpooka 	chunk = MIN(len, BUSMEM_DATASIZE - off);
70ce68b7aeSpooka 	memcpy(dest, busmem->shm_data + off, chunk);
71ce68b7aeSpooka 	len -= chunk;
72ce68b7aeSpooka 
7386ea9bb1Spooka 	if (off + chunk == BUSMEM_DATASIZE)
74ce68b7aeSpooka 		*wrap = true;
75ce68b7aeSpooka 
7686ea9bb1Spooka 	if (len == 0) {
7786ea9bb1Spooka 		return (off + chunk) % BUSMEM_DATASIZE;
7886ea9bb1Spooka 	}
7986ea9bb1Spooka 
80ce68b7aeSpooka 	/* finish reading */
8111082373Spooka 	memcpy((uint8_t *)dest + chunk, busmem->shm_data, len);
8211082373Spooka 	return len;
83ce68b7aeSpooka }
84ce68b7aeSpooka 
85ce68b7aeSpooka void
shmif_advancefirst(struct shmif_mem * busmem,uint32_t off,size_t len)86ce68b7aeSpooka shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
87ce68b7aeSpooka {
88ce68b7aeSpooka 
89b97bdf94Spooka 	while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr)
90ce68b7aeSpooka 	    && off+len > busmem->shm_first) {
91ce68b7aeSpooka 		DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
92ce68b7aeSpooka 		busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
93ce68b7aeSpooka 		DPRINTF(("new offset: %d\n", busmem->shm_first));
94ce68b7aeSpooka 	}
95ce68b7aeSpooka }
96ce68b7aeSpooka 
97ce68b7aeSpooka uint32_t
shmif_buswrite(struct shmif_mem * busmem,uint32_t off,void * data,size_t len,bool * wrap)98ce68b7aeSpooka shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
99ce68b7aeSpooka 	bool *wrap)
100ce68b7aeSpooka {
101ce68b7aeSpooka 	size_t chunk;
102c2fbfedcSpooka 	bool filledbus;
103ce68b7aeSpooka 
10411082373Spooka 	KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
105ce68b7aeSpooka 
106ce68b7aeSpooka 	chunk = MIN(len, BUSMEM_DATASIZE - off);
107ce68b7aeSpooka 	len -= chunk;
108c2fbfedcSpooka 	filledbus = (off+chunk == BUSMEM_DATASIZE);
109ce68b7aeSpooka 
110c2fbfedcSpooka 	shmif_advancefirst(busmem, off, chunk + (filledbus ? 1 : 0));
111ce68b7aeSpooka 
112ce68b7aeSpooka 	memcpy(busmem->shm_data + off, data, chunk);
113ce68b7aeSpooka 
114*58dd466bSozaki-r 	DPRINTF(("buswrite: wrote %zu bytes to %d", chunk, off));
115ce68b7aeSpooka 
116c2fbfedcSpooka 	if (filledbus) {
11786ea9bb1Spooka 		*wrap = true;
118c2fbfedcSpooka 	}
11986ea9bb1Spooka 
120ce68b7aeSpooka 	if (len == 0) {
121ce68b7aeSpooka 		DPRINTF(("\n"));
12286ea9bb1Spooka 		return (off + chunk) % BUSMEM_DATASIZE;
123ce68b7aeSpooka 	}
124ce68b7aeSpooka 
125*58dd466bSozaki-r 	DPRINTF((", wrapped bytes %zu to 0\n", len));
126ce68b7aeSpooka 
12786ea9bb1Spooka 	shmif_advancefirst(busmem, 0, len);
128ce68b7aeSpooka 
129ce68b7aeSpooka 	/* finish writing */
13086ea9bb1Spooka 	memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
13186ea9bb1Spooka 	return len;
132ce68b7aeSpooka }
133ce68b7aeSpooka 
134ce68b7aeSpooka uint32_t
shmif_nextpktoff(struct shmif_mem * busmem,uint32_t oldoff)135ce68b7aeSpooka shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
136ce68b7aeSpooka {
137b97bdf94Spooka 	struct shmif_pkthdr sp;
138ce68b7aeSpooka 	bool dummy;
139ce68b7aeSpooka 
140b97bdf94Spooka 	shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
141b97bdf94Spooka 	KASSERT(sp.sp_len < BUSMEM_DATASIZE);
142ce68b7aeSpooka 
143b97bdf94Spooka 	return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
144ce68b7aeSpooka }
145