1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/* -*- c++ -*- */
/*
* Copyright 2007,2008 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Double Buffering State Machine
*/
#include "dbsm.h"
#include "memory_map.h"
#include "buffer_pool.h"
#include <stdbool.h>
#include "nonstdio.h"
#include <stdlib.h>
typedef enum {
BS_EMPTY,
BS_FILLING,
BS_FULL,
BS_EMPTYING,
} buffer_state_t;
static buffer_state_t buffer_state[NBUFFERS];
bool
dbsm_nop_inspector(dbsm_t *sm, int buf_this)
{
return false;
}
void
dbsm_init(dbsm_t *sm, int buf0,
const buf_cmd_args_t *recv, const buf_cmd_args_t *send,
inspector_t inspect)
{
if (buf0 & 0x1) // must be even
abort();
sm->buf0 = buf0;
sm->running = false;
sm->recv_args = *recv;
sm->send_args = *send;
sm->rx_idle = true;
sm->tx_idle = true;
sm->inspect = inspect;
// How much to adjust the last_line register.
// It's 1 for everything but the ethernet.
//sm->last_line_adj = recv->port == PORT_ETH ? 3 : 1;
sm->last_line_adj = 1;
buffer_state[sm->buf0] = BS_EMPTY;
buffer_state[sm->buf0 ^ 1] = BS_EMPTY;
sm->precomputed_receive_to_buf_ctrl_word[0] =
(BPC_READ
| BPC_BUFFER(sm->buf0)
| BPC_PORT(sm->recv_args.port)
| BPC_STEP(1)
| BPC_FIRST_LINE(sm->recv_args.first_line)
| BPC_LAST_LINE(sm->recv_args.last_line));
sm->precomputed_receive_to_buf_ctrl_word[1] =
(BPC_READ
| BPC_BUFFER(sm->buf0 ^ 1)
| BPC_PORT(sm->recv_args.port)
| BPC_STEP(1)
| BPC_FIRST_LINE(sm->recv_args.first_line)
| BPC_LAST_LINE(sm->recv_args.last_line));
sm->precomputed_send_from_buf_ctrl_word[0] =
(BPC_WRITE
| BPC_BUFFER(sm->buf0)
| BPC_PORT(sm->send_args.port)
| BPC_STEP(1)
| BPC_FIRST_LINE(sm->send_args.first_line)
| BPC_LAST_LINE(0)); // last line filled in at runtime
sm->precomputed_send_from_buf_ctrl_word[1] =
(BPC_WRITE
| BPC_BUFFER(sm->buf0 ^ 1)
| BPC_PORT(sm->send_args.port)
| BPC_STEP(1)
| BPC_FIRST_LINE(sm->send_args.first_line)
| BPC_LAST_LINE(0)); // last line filled in at runtime
}
static inline void
dbsm_receive_to_buf(dbsm_t *sm, int bufno)
{
buffer_pool_ctrl->ctrl = sm->precomputed_receive_to_buf_ctrl_word[bufno & 1];
}
static inline void
dbsm_send_from_buf(dbsm_t *sm, int bufno)
{
buffer_pool_ctrl->ctrl =
(sm->precomputed_send_from_buf_ctrl_word[bufno & 1]
| BPC_LAST_LINE(buffer_pool_status->last_line[bufno] - sm->last_line_adj));
}
void
dbsm_start(dbsm_t *sm)
{
// printf("dbsm_start: buf0 = %d, recv_port = %d\n", sm->buf0, sm->recv_args.port);
sm->running = true;
buffer_state[sm->buf0] = BS_EMPTY;
buffer_state[sm->buf0 ^ 1] = BS_EMPTY;
bp_clear_buf(sm->buf0);
bp_clear_buf(sm->buf0 ^ 1);
sm->tx_idle = true;
sm->rx_idle = false;
dbsm_receive_to_buf(sm, sm->buf0);
buffer_state[sm->buf0] = BS_FILLING;
}
void
dbsm_stop(dbsm_t *sm)
{
sm->running = false;
bp_clear_buf(sm->buf0);
bp_clear_buf(sm->buf0 ^ 1);
buffer_state[sm->buf0] = BS_EMPTY;
buffer_state[sm->buf0 ^ 1] = BS_EMPTY;
}
static void dbsm_process_helper(dbsm_t *sm, int buf_this);
static void dbsm_error_helper(dbsm_t *sm, int buf_this);
void
dbsm_process_status(dbsm_t *sm, uint32_t status)
{
if (!sm->running)
return;
if (status & (BPS_ERROR(sm->buf0) | BPS_ERROR(sm->buf0 ^ 1))){
putchar('E');
// Most likely an ethernet Rx error. We just restart the transfer.
if (status & (BPS_ERROR(sm->buf0)))
dbsm_error_helper(sm, sm->buf0);
//dbsm_process_helper(sm, sm->buf0); //forward errors
if (status & (BPS_ERROR(sm->buf0 ^ 1)))
dbsm_error_helper(sm, sm->buf0 ^ 1);
//dbsm_process_helper(sm, sm->buf0 ^ 1); //forward errors
}
if (status & BPS_DONE(sm->buf0))
dbsm_process_helper(sm, sm->buf0);
if (status & BPS_DONE(sm->buf0 ^ 1))
dbsm_process_helper(sm, sm->buf0 ^ 1);
}
static void
dbsm_process_helper(dbsm_t *sm, int buf_this)
{
int buf_other = buf_this ^ 1;
bp_clear_buf(buf_this);
if (buffer_state[buf_this] == BS_FILLING){
buffer_state[buf_this] = BS_FULL;
//
// does s/w handle this packet?
//
if (sm->inspect(sm, buf_this)){
// s/w handled the packet; refill the buffer
dbsm_receive_to_buf(sm, buf_this);
buffer_state[buf_this] = BS_FILLING;
}
else { // s/w didn't handle this; pass it on
if(buffer_state[buf_other] == BS_EMPTY){
dbsm_receive_to_buf(sm, buf_other);
buffer_state[buf_other] = BS_FILLING;
}
else
sm->rx_idle = true;
if (sm->tx_idle){
sm->tx_idle = false;
dbsm_send_from_buf(sm, buf_this);
buffer_state[buf_this] = BS_EMPTYING;
}
}
}
else { // buffer was emptying
buffer_state[buf_this] = BS_EMPTY;
if (sm->rx_idle){
sm->rx_idle = false;
dbsm_receive_to_buf(sm, buf_this);
buffer_state[buf_this] = BS_FILLING;
}
if (buffer_state[buf_other] == BS_FULL){
dbsm_send_from_buf(sm, buf_other);
buffer_state[buf_other] = BS_EMPTYING;
}
else
sm->tx_idle = true;
}
}
static void
dbsm_error_helper(dbsm_t *sm, int buf_this)
{
bp_clear_buf(buf_this); // clears ERROR flag
if (buffer_state[buf_this] == BS_FILLING){
dbsm_receive_to_buf(sm, buf_this); // restart the xfer
}
else { // buffer was emptying
dbsm_send_from_buf(sm, buf_this); // restart the xfer
}
}
/*
* Handle DSP Tx underrun
*/
void
dbsm_handle_tx_underrun(dbsm_t *sm)
{
// clear the DSP Tx state machine
sr_tx_ctrl->clear_state = 1;
// If there's a buffer that's empyting, clear it & flush xfer
if (buffer_state[sm->buf0] == BS_EMPTYING){
bp_clear_buf(sm->buf0);
sr_tx_ctrl->clear_state = 1; // flush partial packet
// drop frame in progress on ground. Pretend it finished
dbsm_process_helper(sm, sm->buf0);
}
else if (buffer_state[sm->buf0 ^ 1] == BS_EMPTYING){
bp_clear_buf(sm->buf0 ^ 1);
sr_tx_ctrl->clear_state = 1; // flush partial packet
// drop frame in progress on ground. Pretend it finished
dbsm_process_helper(sm, sm->buf0 ^ 1);
}
}
/*
* Handle DSP Rx overrun
*/
void
dbsm_handle_rx_overrun(dbsm_t *sm)
{
sr_rx_ctrl->clear_overrun = 1;
// If there's a buffer that's filling, clear it.
// Any restart will be the job of the caller.
if (buffer_state[sm->buf0] == BS_FILLING)
bp_clear_buf(sm->buf0);
if (buffer_state[sm->buf0 ^1] == BS_FILLING)
bp_clear_buf(sm->buf0 ^ 1);
}
void
dbsm_wait_for_opening(dbsm_t *sm)
{
if (buffer_state[sm->buf0] == BS_EMPTYING){
// wait for xfer to complete
int mask = BPS_DONE(sm->buf0) | BPS_ERROR(sm->buf0) | BPS_IDLE(sm->buf0);
while ((buffer_pool_status->status & mask) == 0)
;
}
else if (buffer_state[sm->buf0 ^ 1] == BS_EMPTYING){
// wait for xfer to complete
int mask = BPS_DONE(sm->buf0 ^ 1) | BPS_ERROR(sm->buf0 ^ 1) | BPS_IDLE(sm->buf0 ^ 1);
while ((buffer_pool_status->status & mask) == 0)
;
}
}
|