xref: /netbsd-src/sys/rump/net/lib/libshmif/shmif_busops.c (revision 86ea9bb1a9ba2238963cf080e3574ae9ba191a41)
1 /*	$NetBSD: shmif_busops.c,v 1.6 2010/08/17 11:35:23 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5  *
6  * Development of this software was supported by The Nokia Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.6 2010/08/17 11:35:23 pooka Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/atomic.h>
35 
36 #include "shmifvar.h"
37 
38 #ifndef _KERNEL
39 #include <assert.h>
40 #define KASSERT(a) assert(a)
41 #else
42 #include <rump/rumpuser.h>
43 #endif
44 
45 uint32_t
46 shmif_advance(uint32_t oldoff, uint32_t delta)
47 {
48 	uint32_t newoff;
49 
50 	newoff = oldoff + delta;
51 	if (newoff >= BUSMEM_DATASIZE)
52 		newoff -= BUSMEM_DATASIZE;
53 	return newoff;
54 }
55 
56 uint32_t
57 shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
58 	bool *wrap)
59 {
60 	size_t chunk;
61 
62 	KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
63 	chunk = MIN(len, BUSMEM_DATASIZE - off);
64 	memcpy(dest, busmem->shm_data + off, chunk);
65 	len -= chunk;
66 
67 	if (off + chunk == BUSMEM_DATASIZE)
68 		*wrap = true;
69 
70 	if (len == 0) {
71 		return (off + chunk) % BUSMEM_DATASIZE;
72 	}
73 
74 	/* finish reading */
75 	memcpy((uint8_t *)dest + chunk, busmem->shm_data, len);
76 	return len;
77 }
78 
79 void
80 shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
81 {
82 
83 	while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr)
84 	    && off+len > busmem->shm_first) {
85 		DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
86 		busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
87 		DPRINTF(("new offset: %d\n", busmem->shm_first));
88 	}
89 }
90 
91 uint32_t
92 shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
93 	bool *wrap)
94 {
95 	size_t chunk;
96 
97 	KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
98 
99 	chunk = MIN(len, BUSMEM_DATASIZE - off);
100 	len -= chunk;
101 
102 	shmif_advancefirst(busmem, off, chunk + (len ? 1 : 0));
103 
104 	memcpy(busmem->shm_data + off, data, chunk);
105 
106 	DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
107 
108 	if (off + chunk == BUSMEM_DATASIZE)
109 		*wrap = true;
110 
111 	if (len == 0) {
112 		DPRINTF(("\n"));
113 		return (off + chunk) % BUSMEM_DATASIZE;
114 	}
115 
116 	DPRINTF((", wrapped bytes %d to 0\n", len));
117 
118 	shmif_advancefirst(busmem, 0, len);
119 
120 	/* finish writing */
121 	memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
122 	return len;
123 }
124 
125 uint32_t
126 shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
127 {
128 	struct shmif_pkthdr sp;
129 	bool dummy;
130 
131 	shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
132 	KASSERT(sp.sp_len < BUSMEM_DATASIZE);
133 
134 	return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
135 }
136