summaryrefslogtreecommitdiff
path: root/cesar/test_general/hard/bridgedma/src/bridgedma-rx-one-frame.c
blob: a4b9e1bc18e882d37edb70275d05ed2dad0be94a (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/bridgedma-rx-one-frame.c
 * \brief   Get a Frame from PB to buffer.
 * \ingroup test_general_hard
 *
 */
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cyg/infra/diag.h>

#include "common/std.h"
#include "common/defs/ethernet.h"

#include "mac/common/pb.h"

#include "lib/bitstream.h"
#include "lib/test.h"
#include "lib/read_word.h"
#include "lib/swap.h"

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

#define TEST_BRIDGEDMA_BUFFER_QTE 4

/** Thread context. */
cyg_thread  my_test_thread;
cyg_handle_t my_test_thread_handle;
u8 my_test_thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];
/* end of thread context. */

#ifdef __sparc__
/* Using Memory address outside the eCos memory map. */
static u8 frame_buffer[2048] __attribute__((section(".private")));
static u8 cmp_buffer[2048] __attribute__((section(".private")));
static u8 source_buffer[2048] __attribute__((section(".private")));
#else
static u8 frame_buffer[2048];
static u8 cmp_buffer[2048];
static u8 source_buffer[2048];
#endif

static phy_bridgedma_job_t *job;
static bool it_recv;
static test_t test;
static pb_t *pb_first;
static pb_t *pb_last;
static uint mf_header1;
static uint mf_header2;
static u8 *error_addr;

/* Job configuration. */
static uint TEST_BRIDGEDMA_JOB_OFFSET;
static uint TEST_BRIDGEDMA_JOB_LENGTH;
static uint TEST_BRIDGEDMA_JOB_PB_QTE;
static uint TEST_BRIDGEDMA_FRAME_OFFSET;

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

    return true;
}

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


void
bridgedma_test (void)
{
    phy_bridgedma_t *bridgedma;
    uint ats = 0x12345678;
    uint frame_buffer_offset;
    bitstream_t bitstream;
    pb_t *pb_current;

    /* Allocate the job. */
    job = blk_alloc ();

    /* Create the PBs. */
    pb_first = (pb_t *) blk_alloc_desc_range (TEST_BRIDGEDMA_JOB_PB_QTE,
                                              (blk_t **) &pb_last);
    pb_last->next = NULL;

    diag_printf (" ****** PBs Addresses  ******* \n");
    for (pb_current = pb_first; pb_current; pb_current = pb_current->next)
    {
        diag_printf ("PB Desc address : %p\n", pb_current);
        diag_printf ("PB Payload address: %p\n", pb_current->data);
    }

    memset (job, 0, sizeof (phy_bridgedma_job_t));
    job->next = NULL;
    job->data_addr = (u8 *) (frame_buffer + TEST_BRIDGEDMA_FRAME_OFFSET);
    job->header_len = 6;
    job->data_len = TEST_BRIDGEDMA_JOB_LENGTH;
    job->first_pb_desc = (blk_t *)pb_first;
    job->first_pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
    job->segment_len = 512;
    job->direction = 1;
    job->crc_reset = 1;
    job->crc_store = 1;
    job->job_it = 1;
    job->append_zero = 1;
    //job->eth_buffer_mask = 0x1fffff0; /* 2048 bytes mask */
    job->eth_buffer_mask = 0x0;
    job->mf_header1 = 0xFFFFFFFF;
    job->mf_header2 = 0xFFFFFFFF;

    /* Payload + ATS */
    mf_header1 = (((job->data_len + 4 - 1) << 2) | 0x02) | (ats << 16);
    mf_header2 = (ats >> 16);

    /* Reset the destination buffer. */
    memset ((u8 *)frame_buffer, 0, 2048);

    memset (source_buffer, 0xff, 2048);

    bitstream_init (&bitstream, (u8 *) cmp_buffer, 2048, BITSTREAM_WRITE);
    bitstream_access_buf (&bitstream, (u8 *) source_buffer, 2048);
    bitstream_finalise (&bitstream);


    /* Fill the PBs. */
    pb_first->header.ssn = 0;
    pb_first->header.mfbf = true;
    pb_first->header.mfbo = TEST_BRIDGEDMA_JOB_OFFSET;

    bitstream_init (&bitstream, pb_first->data + TEST_BRIDGEDMA_JOB_OFFSET,
                    BLK_SIZE - TEST_BRIDGEDMA_JOB_OFFSET,
                    BITSTREAM_WRITE);
    bitstream_access_buf (&bitstream, &mf_header1, 4);
    bitstream_access_buf (&bitstream, &mf_header2, 2);
    bitstream_access_buf (&bitstream, (u8 *) source_buffer,
                          BLK_SIZE - 6 - TEST_BRIDGEDMA_JOB_OFFSET);
    bitstream_finalise (&bitstream);
    frame_buffer_offset = BLK_SIZE - TEST_BRIDGEDMA_JOB_OFFSET - 6;

    pb_first->next->header.ssn = 1;
    bitstream_init (&bitstream, pb_first->next->data,
                    BLK_SIZE,
                    BITSTREAM_WRITE);

    bitstream_access_buf (&bitstream, (u8 *) source_buffer + frame_buffer_offset,
                          BLK_SIZE);
    bitstream_finalise (&bitstream);
    frame_buffer_offset += BLK_SIZE;

    pb_first->next->next->header.ssn = 2;
    bitstream_init (&bitstream, pb_first->next->next->data,
                    BLK_SIZE,
                    BITSTREAM_WRITE);
    bitstream_access_buf (&bitstream, (u8 *) source_buffer + frame_buffer_offset,
                          BLK_SIZE);
    bitstream_finalise (&bitstream);
    frame_buffer_offset += BLK_SIZE;

    pb_last->header.ssn = 3;
    bitstream_init (&bitstream, pb_first->next->next->next->data,
                    BLK_SIZE,
                    BITSTREAM_WRITE);
    bitstream_access_buf (&bitstream, (u8 *) source_buffer + frame_buffer_offset,
                          TEST_BRIDGEDMA_JOB_LENGTH - frame_buffer_offset);
    bitstream_access_buf (&bitstream, (u8 *) source_buffer + frame_buffer_offset,
            TEST_BRIDGEDMA_JOB_LENGTH - frame_buffer_offset);
    bitstream_finalise (&bitstream);

    /* Store the PBs in the job. */
    job->first_pb_desc = (blk_t *) pb_first;

    /* Initialise the bridgedma. */
    bridgedma = phy_bridgedma_init(NULL, _bridgedma_segmentation_cb,
                                   _bridgedma_deffered_cb);

    cyg_thread_delay (1);

    phy_bridgedma_start (bridgedma, job, job);
}

void
bridgedma_verify_reception (void)
{
    uint i;
    bool result_t1 = false;
    bool result_t2 = false;
    test_case_begin (test, "RX - Frame reception");

    test_begin (test, "It reception")
    {
        test_fail_if (it_recv == false);
    }
    test_end;

#ifdef __sparc__
    uint data;
    // Verify the register corresponding to the MF header1.
    volatile u32 *job_header_3210 = (u32 *) PHY_BRIDGEDMA_JOB_HEADER_3210;
    volatile u32 *job_header_7654 = (u32 *) PHY_BRIDGEDMA_JOB_HEADER_7654;
    volatile u32 *control = (u32 *) PHY_BRIDGEDMA_CONTROL;
    volatile u32 *job_len = (u32 *) PHY_BRIDGEDMA_JOB_LENGTH;
    volatile u32 *jobd_current = (u32 *) PHY_BRIDGEDMA_JOBD_PTR;
    volatile u32 *pb_conf = (u32 *) PHY_BRIDGEDMA_PB_CONF;
    volatile u32 *first_pb = (u32 *) PHY_BRIDGEDMA_JOB_FIRST_PB_DESC;
    volatile u32 *job_buf_addr = (u32 *) PHY_BRIDGEDMA_JOB_BUFF_ADD;

    test_begin (test, "Verify registers")
    {
        test_fail_if (*job_header_3210 != job->mf_header1);
        test_fail_if (*job_header_7654 != job->mf_header2);

        data = *control;
        test_fail_if (BF_GET (PHY_BRIDGEDMA_CONTROL__STATUS, data) != 0);

        data = *pb_conf;
        test_fail_if (BF_GET (PHY_BRIDGEDMA_PB_CONF__OFFSET, data) !=
                      TEST_BRIDGEDMA_JOB_OFFSET);
        test_fail_if (BF_GET (PHY_BRIDGEDMA_PB_CONF__LENGTH, data) !=
                      BLK_SIZE);

        data = *job_len;
        test_fail_if (BF_GET (PHY_BRIDGEDMA_JOB_LENGTH__LENGTH, data) != job->data_len);

        result_t1 = true;
    }
    test_end;


    diag_printf ("******************* Debug *********************\n");
    diag_printf ("CMP Buffer : %p\n", cmp_buffer);
    diag_printf ("Frame Buffer : %p\n", job->data_addr);
    data = *control;
    diag_printf ("BRIDGEDMA Status ; %x\n",
                 BF_GET(PHY_BRIDGEDMA_CONTROL__STATUS, data));
    diag_printf ("[JOBD current] bridge : %x \t Cesar :%p\n", *jobd_current, job);

    data = *job_len;
    diag_printf ("[JOBD length] bridge : %x \t Cesar :%x\n",
                 BF_GET(PHY_BRIDGEDMA_JOB_LENGTH__LENGTH, data), job->data_len);
    diag_printf ("[JOBD header length] bridge : %x \t Cesar :%x\n",
                 BF_GET(PHY_BRIDGEDMA_JOB_LENGTH__HEADER_LEN, data),
                 job->header_len);
    diag_printf ("[JOBD header_3210] bridge : %x \t Cesar : %x\n",
                 *job_header_3210, job->mf_header1);
    diag_printf ("[JOBD header_7654] bridge : %x \t Cesar : %x\n",
                 *job_header_7654, job->mf_header2);
    diag_printf ("[JOB BUFF ADDR] bridge : %x \t Cesar : %p\n", *job_buf_addr,
                 job->data_addr);

    data = *pb_conf;
    diag_printf ("[PB Conf length] bridge : %x \t Cesar : %x\n",
                 BF_GET (PHY_BRIDGEDMA_PB_CONF__LENGTH, data), job->segment_len);
    diag_printf ("[PB Conf offset] bridge : %x \t Cesar : %x\n",
                 BF_GET (PHY_BRIDGEDMA_PB_CONF__OFFSET, data), job->first_pb_offset);

    diag_printf ("[First PB] bridged : %x \t Cesar : %p\n", *first_pb,
                 pb_first);

    diag_printf ("**************** END Debug *******************\n");
#endif


    test_begin (test, "Verifying entry buffer with output buffer")
    {
        for (i = 0; i < job->data_len; i++)
            test_fail_if (read_u8_from_word(cmp_buffer + i)
                          != read_u8_from_word(job->data_addr + i),
                          "Wrong result on buffer at index : %d, value \
                          expected : %x, value read : %x\n", i,
                          read_u8_from_word(cmp_buffer + i),
                          read_u8_from_word(job->data_addr + i));

        result_t2 = true;
    }
    test_end;

    if (i < (uint) (job->data_len - 1))
    {
        error_addr = job->data_addr + i;
        diag_printf ("Error address : %p\n", error_addr);

        diag_printf ("************* CMP Buffer *************\n");
        for (i = 0; i < job->data_len; i++)
        {
            diag_printf ("%8x ", read_u8_from_word ((u8 *) cmp_buffer + i));
            if ((i % 10)  == 0)
                diag_printf ("\n");
        }
        diag_printf ("\n");
        diag_printf ("************* CMP Buffer *************\n");

        diag_printf ("Frame buffer addr : %p\n", job->data_addr);

        diag_printf ("************* Frame Buffer *************\n");
        for (i = 0; i < job->data_len; i++)
        {
            diag_printf ("%8x ", read_u8_from_word (job->data_addr + i));
            if ((i % 10)  == 0)
                diag_printf ("\n");
        }
        diag_printf ("\n");
        diag_printf ("************* Frame Buffer *************\n");

        diag_printf ("\n\n*****************************************************\n");
        if (result_t1 && result_t2)
            diag_printf ("******************* SUCCEED *********************\n");
        else
            diag_printf ("******************* FAILED  *********************\n");
        diag_printf ("*****************************************************\n\n");
    }

    /** release resources. */
    blk_release_desc_range ((blk_t *) pb_first, (blk_t *) pb_last);
    blk_release (job);
}

void
test_thread_process (cyg_addrword_t data)
{
    uint i;

    for (i = 0; i < 4; i++)
    {
        memset ((u8 *) frame_buffer, 0, 2048);
        memset ((u8 *) cmp_buffer, 0, 2048);
        memset ((u8 *) source_buffer, 0, 2048);
        it_recv = false;

        TEST_BRIDGEDMA_JOB_OFFSET = 128 + i;
        TEST_BRIDGEDMA_FRAME_OFFSET = i;

        TEST_BRIDGEDMA_JOB_LENGTH = ETH_PACKET_MAX_SIZE;

        TEST_BRIDGEDMA_JOB_PB_QTE =
            ((TEST_BRIDGEDMA_JOB_LENGTH + TEST_BRIDGEDMA_JOB_OFFSET) / BLK_SIZE + 1);

        bridgedma_test ();

        cyg_thread_delay(2);
        bridgedma_verify_reception ();

        test_result (test);
    }

#ifndef __sparc__
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
#endif
}


int
main (void)
{
    test_init (test, 0, NULL);

    // 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;
}