xref: /netbsd-src/sys/rump/net/lib/libshmif/shmif_busops.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: shmif_busops.c,v 1.7 2010/08/17 12:59:53 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.7 2010/08/17 12:59:53 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 	bool filledbus;
97 
98 	KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
99 
100 	chunk = MIN(len, BUSMEM_DATASIZE - off);
101 	len -= chunk;
102 	filledbus = (off+chunk == BUSMEM_DATASIZE);
103 
104 	shmif_advancefirst(busmem, off, chunk + (filledbus ? 1 : 0));
105 
106 	memcpy(busmem->shm_data + off, data, chunk);
107 
108 	DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
109 
110 	if (filledbus) {
111 		*wrap = true;
112 	}
113 
114 	if (len == 0) {
115 		DPRINTF(("\n"));
116 		return (off + chunk) % BUSMEM_DATASIZE;
117 	}
118 
119 	DPRINTF((", wrapped bytes %d to 0\n", len));
120 
121 	shmif_advancefirst(busmem, 0, len);
122 
123 	/* finish writing */
124 	memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
125 	return len;
126 }
127 
128 uint32_t
129 shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
130 {
131 	struct shmif_pkthdr sp;
132 	bool dummy;
133 
134 	shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
135 	KASSERT(sp.sp_len < BUSMEM_DATASIZE);
136 
137 	return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
138 }
139