summaryrefslogtreecommitdiff
path: root/cesar/test_general/hard/bridgedma/src/bridgedma-tx-one-frame.c
blob: 5943b7e33340e60c76096bb43bb17a1c166db976 (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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/bridgedma-tx-one-frame.c
 * \brief   Request the fragmentation of only a Frame.
 * \ingroup hal_phy
 *
 */
#include "common/std.h"

#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>

#include "common/defs/ethernet.h"

#include "lib/test.h"
#include "lib/list.h"
#include "lib/read_word.h"
#include "lib/bitstream.h"
#include "mac/common/pb.h"

#include "hal/phy/inc/bridgedma.h"
#include "hal/phy/inc/bridgedma_regs.h"

#include <string.h>
#include <stdio.h>
#include <cyg/infra/diag.h>
#include "hal/arch/arch.h"

#define TEST_BRIDGEDMA_PB_NB 5
#define TEST_BRIDGEDMA_JOB_OFFSET 128

cyg_thread  my_test_thread;
cyg_handle_t my_test_thread_handle;
u8 my_test_thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];

static test_t test;
static uint it_recv = false;
static phy_bridgedma_job_t job1;
static u8 frame_buffer [2048];
static pb_t *pb_first;
static pb_t *pb_last;


void
bridgedma_verify_registers (void)
{
#ifdef __sparc__
    arch_reorder_barrier ();
    // Verify the register corresponding to the MF header1.
    volatile uint *job_header_3210 = (uint *) PHY_BRIDGEDMA_JOB_HEADER_3210;
    volatile uint *job_header_7654 = (uint *) PHY_BRIDGEDMA_JOB_HEADER_7654;

    volatile uint *control = (uint *) PHY_BRIDGEDMA_CONTROL;
    volatile uint *job_len = (uint *) PHY_BRIDGEDMA_JOB_LENGTH;
    arch_reorder_barrier ();

    diag_printf ("JOB HEADER BRIDGEDMA_3210 : %x\n", *job_header_3210);
    diag_printf ("JOB HEADER BRIDGEDMA_7654 : %x\n", *job_header_7654);
    diag_printf ("Control : %x\n", *control);
    diag_printf ("Job length : %x\n", *job_len);
#endif
}


/* WARNING: callback are not possible to test inside UNIT TEST */
bool _bridgedma_segmentation_cb(void *data, u32 status)
{
    it_recv = true;

    cyg_thread_resume (my_test_thread_handle);
    return true;
}

/* WARNING: callback are not possible to test inside UNIT TEST */
void _bridgedma_deffered_cb(void *data)
{
}

void bridgedma_segmentation_test_case(void)
{
    uint i;
    uint ats;
    pb_t *pb_current;
    phy_bridgedma_t *bridgedma_ctx;

    test_case_begin(test, "segmentation");


    pb_first = (pb_t*) blk_alloc_desc_range(TEST_BRIDGEDMA_PB_NB, (blk_t **) &pb_last);
    pb_last->next = NULL;

    pb_current = pb_first;
    for (i = 0; i < TEST_BRIDGEDMA_PB_NB; i++)
    {
        pb_current->header.ssn = i;
        pb_current = pb_current->next;
    }

    ats = 0x12345678;

    /************************************************************************
     * simple segmentation with 3 mac frames into 5 PBs
     * 1st mac frame: len=1518, offset=128, ATS, ICV into 1st to 4th PB
     * 2nd mac frame: len=64, ATS, ICV into 4th PB
     * 3nd mac frame: len=466, ATS, ICV, padding into 4th to 5th PB
     ***********************************************************************/

    memset(&job1, '\0', sizeof(job1));
    job1.next = NULL;
    job1.data_addr = frame_buffer;
    job1.header_len = 6;
    job1.data_len = ETH_PACKET_MAX_SIZE;
    job1.first_pb_desc = (blk_t *)pb_first;
    job1.first_pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
    job1.segment_len = 512;
    job1.direction = 0;
    job1.crc_reset = 1;
    job1.crc_store = 1;
    job1.job_it = 1;
    job1.eth_buffer_mask = 0x1fffff0; /* 2048 bytes mask */
    /* Payload + ATS */
    job1.mf_header1 = (((job1.data_len + 4 - 1) << 2) | 0x02) | (ats << 16);
    job1.mf_header2 = (ats >> 16);

    bridgedma_ctx = phy_bridgedma_init(NULL, _bridgedma_segmentation_cb,
                                       _bridgedma_deffered_cb);

    cyg_thread_delay (1);

    phy_bridgedma_start (bridgedma_ctx, &job1, &job1);
}

void
bridgedma_verify_it_reception (void)
{
    diag_printf ("Interruption received : %d\n", it_recv);
}

void
bridgedma_segmentation_verify_test_case (void)
{
    uint payload_cnt;
    pb_t *pb_current;
    phy_bridgedma_job_t *job_current;

    uint i;
    uint length;
    uint pb_offset;
    uint qte_pb;
    uint frame_offset;
    bool cont;


    test_begin(test, "segmentation of a frame")
    {
        for (i = 0, pb_current = pb_first; pb_current; pb_current =
             pb_current->next, i++)
        {
            test_fail_if (pb_current->header.ssn != i, "Wong sequence");
        }

        diag_printf ("length job1 in PB : %x\n", read_u32_from_word
                     (((pb_t *)job1.first_pb_desc)->data + 128));

        /* check pb content */
        /* check 1st mac frame into PB */
        payload_cnt = 0;

        pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
        job_current = &job1;
        frame_offset = 0;
        for (pb_current = pb_first, i = 0; i < 3; i++, job_current =
             job_current->next)
        {
            cont = true;
            length = 1 + (read_u16_from_word (pb_current->data + pb_offset) >> 2);

            // To get the payload length, the frame length is 10
            // bytes greater.
            length -= 4;

            diag_printf ("Length read in pb : %d\n", length);
            diag_printf ("Length read in pb : %x\n", length);
            diag_printf ("pb ssn : %d\n", ((pb_t *)pb_current)->header.ssn);
            diag_printf ("pb offset : %d\n", pb_offset);
            test_fail_unless (length == job_current->data_len,
                              "Data length is wrong.");

            // Copy the data of the current pb.

            // Jump directly to the start of the payload.
            pb_offset += 6;
            if (length + pb_offset > BLK_SIZE)
            {
                test_fail_unless (
                        bitstream_memcmp (frame_buffer + frame_offset,
                        pb_current->data + pb_offset,
                        BLK_SIZE - pb_offset),
                        "data corrupted");

                frame_offset += BLK_SIZE - pb_offset;
                length -= BLK_SIZE - pb_offset;
            }
            else
            {
                test_fail_unless (
                        bitstream_memcmp (frame_buffer + frame_offset,
                        pb_current->data + pb_offset,
                        length),
                        "data corrupted");

                frame_offset += length;
                cont = false;
            }


            if (cont)
            {
                // Add 10 to the length to use the complete MF length
                // (ATS + MFH + ICV), the ATS and MFH are already include in the
                // pb_offset.
                for (qte_pb = (pb_offset + length + 4) / BLK_SIZE + 1;
                     qte_pb;
                     qte_pb --)
                {
                    pb_current = pb_current->next;

                    if (length > BLK_SIZE)
                    {
                        test_fail_unless(
                            bitstream_memcmp (frame_buffer + frame_offset,
                            pb_current->data ,
                            BLK_SIZE),
                            "data corrupted");

                        length -= BLK_SIZE;
                        frame_offset += BLK_SIZE;
                    }
                    else
                    {
                        test_fail_unless(
                            bitstream_memcmp (frame_buffer + frame_offset,
                            pb_current->data ,
                            length),
                            "data corrupted");

                        frame_offset += length;
                    }
                }
            }

            // The futur offset is length + 4 cause of the ICV of the MF.
            // The ATS and the MFH has already been jumped above in the
            // pb_offset.
            pb_offset = (pb_offset + job_current->data_len + 4) % BLK_SIZE;
        }
    } test_end;

    blk_release_desc_range((blk_t *)pb_first, (blk_t *) pb_last);
}


void
test_thread_process (cyg_addrword_t data)
{
    bridgedma_segmentation_test_case ();
    bridgedma_verify_registers ();
    if (it_recv == false)
    {
        diag_printf ("gonna sleep\n");
        cyg_thread_suspend (my_test_thread_handle);
    }
    bridgedma_verify_it_reception ();

    bridgedma_segmentation_verify_test_case ();

    diag_printf ("Test ENDED\n");
    test_result (test);
#ifndef __sparc__
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
#endif
}

int
main (void)
{
    test_t test;
    uint i;

    test_init (test, 0, NULL);

    for (i = 0; i < 2048; i++)
        frame_buffer[i] = i;

    // Create the thread.
    cyg_thread_create( 9,
                      &test_thread_process,
                      (cyg_addrword_t) &test,
                      "TEST_THREAD",
                      my_test_thread_stack,
                      CYGNUM_HAL_STACK_SIZE_TYPICAL,
                      &my_test_thread_handle,
                      &my_test_thread);
    cyg_thread_resume (my_test_thread_handle);

    return 0;
}