summaryrefslogtreecommitdiffstats
path: root/libs/Network/rx.c
blob: 69e7391dad7b6922c893cd1cbc390d4dac6366fa (plain)
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
////////////////////////////////////////////////////////////////////////////////
///
/// @file       rx.c
///
/// @project
///
/// @brief      Network reception routines
///
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
///
/// @copyright Copyright (c) 2019, Evan Lojewski
/// @cond
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// 3. Neither the name of the copyright holder nor the
/// names of its contributors may be used to endorse or promote products
/// derived from this software without specific prior written permission.
///
////////////////////////////////////////////////////////////////////////////////
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
/// @endcond
////////////////////////////////////////////////////////////////////////////////

#include <APE_APE.h>
#include <APE_APE_PERI.h>
#include <APE_RX_PORT0.h>
#include <Ethernet.h>
#include <Network.h>
#include <types.h>

#ifdef CXX_SIMULATOR
#include <stdio.h>
#endif

bool Network_RxLePatcket(uint32_t *buffer, uint32_t *bytes, NetworkPort_t *port)
{
    RegAPERxbufoffset_t rxbuf;
    rxbuf = *((RegAPERxbufoffset_t *)port->rx_offset);
    if ((int)rxbuf.bits.Valid)
    {
        uint32_t rx_bytes = 0;
        network_control_t control;
        int count = rxbuf.bits.Count;
        // int tailid = rxbuf.bits.Tail;
        int blockid = rxbuf.bits.Head;
        // printf("Valid packet at offset %x\n", blockid);
        uint32_t buffer_pos = 0;

        do
        {
            // printf("Block at %x\n", blockid);
            RegRX_PORTIn_t *block = (RegRX_PORTIn_t *)&port->rx_port->In[RX_PORT_IN_ALL_BLOCK_WORDS * blockid];
            // printf("Control %x\n", (uint32_t)block[0].r32);
            control.r32 = block[0].r32;
            // printf(" Payload Len %d\n", control.bits.payload_length);
            // printf(" Next Block %d\n", control.bits.next_block);
            // printf(" First %d\n", control.bits.first);
            // printf(" Not Last %d\n", control.bits.not_last);
            int32_t words = DIVIDE_RND_UP(control.bits.payload_length, sizeof(uint32_t));
            rx_bytes += control.bits.payload_length;
            int32_t offset;
            if (control.bits.first)
            {
                offset = RX_PORT_IN_ALL_FIRST_PAYLOAD_WORD;
            }
            else
            {
                offset = RX_PORT_IN_ALL_ADDITIONAL_PAYLOAD_WORD;
            }
            // printf("Using offset %d\n", offset);
            for (int i = 0; i < words; i++)
            {
                uint32_t data = block[i + offset].r32;
                buffer[buffer_pos++] = data;
                // printf(" word %d: 0x%08X\n", i, data);
            }

            blockid = control.bits.next_block;
            count--;
        } while (count);

        // Transmit to NC
        // disableNCSIHandling();
        // enableNCSIHandling();

        RegAPERxPoolRetire_t retire;
        retire.r32 = 0;
        retire.bits.Head = rxbuf.bits.Head;
        retire.bits.Tail = rxbuf.bits.Tail;
        retire.bits.Count = rxbuf.bits.Count;
        retire.bits.Retire = 1;
        *((RegAPERxPoolRetire_t *)port->rx_retire) = retire;

        rxbuf.bits.Finished = 1;
        *((RegAPERxbufoffset_t *)port->rx_offset) = rxbuf;

        *bytes = rx_bytes;

        return true;
    }
    else
    {
        return false;
    }
}
#include <printf.h>
bool Network_PassthroughRxPatcket(NetworkPort_t *port)
{
    RegAPERxbufoffset_t rxbuf;
    rxbuf = *((RegAPERxbufoffset_t *)port->rx_offset);
    if ((int)rxbuf.bits.Valid)
    {
        printf("***RX***\n");
#if CXX_SIMULATOR
        rxbuf.print();
#endif
        network_control_t control;
        int count = rxbuf.bits.Count;
        // int tailid = rxbuf.bits.Tail;
        int blockid = rxbuf.bits.Head;

        while (count--)
        {
            // printf("Block at %x\n", blockid);
            RegRX_PORTIn_t *block = (RegRX_PORTIn_t *)&port->rx_port->In[RX_PORT_IN_ALL_BLOCK_WORDS * blockid];
            // printf("Control %x\n", (uint32_t)block[0].r32);
            control.r32 = block[0].r32;
            // printf(" Payload Len %d\n", control.bits.payload_length);
            // printf(" Next Block %d\n", control.bits.next_block);
            // printf(" First %d\n", control.bits.first);
            // printf(" Not Last %d\n", control.bits.not_last);
#if CXX_SIMULATOR
            printf("%d bytes in block.\n", control.bits.payload_length);
#endif
            int32_t words = DIVIDE_RND_UP(control.bits.payload_length, sizeof(uint32_t));
            int32_t offset;
            if (control.bits.first)
            {
                offset = RX_PORT_IN_ALL_FIRST_PAYLOAD_WORD;
            }
            else
            {
                offset = RX_PORT_IN_ALL_ADDITIONAL_PAYLOAD_WORD;
            }

            // Wait for enough free space.
            while (APE_PERI.BmcToNcTxStatus.bits.InFifo < words)
                ;

            int i;
            uint32_t data;
            if (!control.bits.not_last)
            {
                // Ignore last word - drop the FCS
                words--;
            }

            for (i = 0; i < words - 1; i++)
            {
                data = block[i + offset].r32;
                APE_PERI.BmcToNcTxBuffer.r32 = data;
            }

            data = block[i + offset].r32;
            if (control.bits.not_last)
            {
                APE_PERI.BmcToNcTxBuffer.r32 = data;
            }
            else
            {
                // data = block[i + offset].r32;
                // Last word to send.
                APE_PERI.BmcToNcTxControl.r32 = control.bits.payload_length % sizeof(uint32_t);
                APE_PERI.BmcToNcTxBufferLast.r32 = data;

                // Ignore last word - drop the FCS.
                // data = block[i + offset + 1].r32;
                // (void)data;
            }

            // Retire this block.
            RegAPERxPoolRetire_t retire;
            retire.r32 = (1 << 24);
            retire.bits.Head = blockid;
            retire.bits.Tail = blockid;
            retire.bits.Count = 1;
            *((RegAPERxPoolRetire_t *)port->rx_retire) = retire;

            blockid = control.bits.next_block;
        }

        // Mark the frame as read.
        rxbuf.bits.Finished = 1;
        *((RegAPERxbufoffset_t *)port->rx_offset) = rxbuf;

        return true;
    }
    else
    {
        return false;
    }
}
OpenPOWER on IntegriCloud