summaryrefslogtreecommitdiff
path: root/cesar/test_general/hard/bridgedma/src/bridgedma-tx-one-frame.c
blob: 4917a02da05d3a2f9e229fd90b6da2eafb272152 (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
/* 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/phy.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 4
#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 bool it_recv = false;

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

u8 *hard_buffer;
static phy_bridgedma_job_t *job;

/* 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_segmentation_test_case(void)
{
    uint i;
    uint ats;
    pb_t *pb_current, *pb_first, *pb_last;
    phy_bridgedma_t *bridgedma_ctx;

    /* Clear all the buffers. */
    hard_buffer = ARCH_CPU_TO_UNCACHEABLE (frame_buffer);
    memset (hard_buffer, 0, 2048);

    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;
    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 (&pb_current->header, 0, sizeof (pb_current->header));
        memset (pb_current->data, 0, BLK_SIZE);

        pb_current->header.ssn = i;
        pb_current = pb_current->next;
    }

    ats = 0x12345678;

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

    memset (job, 0, sizeof (phy_bridgedma_job_t));
    job->next = NULL;
    job->data_addr = hard_buffer;
    job->header_len = 6;
    job->data_len = ETH_PACKET_MAX_SIZE;
    job->first_pb_desc = (blk_t *)pb_first;
    job->first_pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
    job->segment_len = 512;
    job->direction = 0;
    job->crc_reset = 1;
    job->crc_store = 1;
    job->append_zero = 1;
    job->job_it = 1;
    job->eth_buffer_mask = 0x0;
    /* Payload + ATS */
    job->mf_header1 = (((job->data_len + 4 - 1) << 2) | 0x02) | (ats << 16);
    job->mf_header2 = (ats >> 16);

    /* Initialise the frame buffer. */
    for ( i = 0; i < 2048; i++)
        hard_buffer[i] = (u8) i;

    bridgedma_ctx = phy_bridgedma_init(NULL, _bridgedma_segmentation_cb,
                                       _bridgedma_deffered_cb);

    cyg_thread_delay (1);

#ifdef __sparc__
    arch_write_buffer_flush ();
#endif
    phy_bridgedma_start (bridgedma_ctx, job, job);
}

void
bridgedma_segmentation_verify_test_case (void)
{
    uint payload_cnt;
    pb_t *pb_current;
    pb_t *pb_first;
    pb_t *pb_last;

    uint length;
    uint pb_offset;
    uint frame_offset;
    uint i;

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

    test_begin(test, "segmentation of a frame")
    {
        /* check pb content */
        /* check 1st mac frame into PB */
        payload_cnt = 0;

        arch_load_cache ((u32*) job, sizeof (phy_bridgedma_job_t) / 4 + 1);

        pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
        frame_offset = 0;
        pb_current = (pb_t *) job->first_pb_desc;
        arch_load_cache ((u32*) pb_current, sizeof (pb_header_t) / 4);
        arch_load_cache ((u32*) pb_current->data, BLK_SIZE / 4);

        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 : %d\n", length);
        diag_printf ("pb offset : %d\n", pb_offset);
        test_fail_unless (length == job->data_len, "Data length is wrong.");


        pb_current = (pb_t *) job->first_pb_desc;
        payload_cnt = TEST_BRIDGEDMA_JOB_OFFSET + job->header_len;

        diag_printf ("PB ADDR : %p\n", pb_current);
        diag_printf ("PB payload ADDR : %p\n", pb_current->data);
        diag_printf ("data ADDR : %p\n", pb_current->data + payload_cnt);
        for (i = 0; i < BLK_SIZE - payload_cnt; i++)
            test_fail_if (read_u8_from_word(hard_buffer + i) !=
                          read_u8_from_word(pb_current->data + i + payload_cnt),
                          "Frame at index %d != pb->data at index %d", i, i +
                          payload_cnt);

        pb_current = pb_current->next;
        arch_load_cache ((u32*) pb_current, sizeof (pb_header_t) / 4);
        arch_load_cache ((u32*) pb_current->data, BLK_SIZE / 4);
        payload_cnt = BLK_SIZE - payload_cnt;

        for (i = 0; i < BLK_SIZE; i++)
            test_fail_if (read_u8_from_word(hard_buffer + i + payload_cnt) !=
                          read_u8_from_word(pb_current->data + i),
                          "Frame at index %d != pb->data at index %d", i +
                          payload_cnt, i);


        pb_current = pb_current->next;
        arch_load_cache ((u32*) pb_current, sizeof (pb_header_t) / 4);
        arch_load_cache ((u32*) pb_current->data, BLK_SIZE / 4);
        payload_cnt += BLK_SIZE;
        for (i = 0; i < BLK_SIZE; i++)
            test_fail_if (read_u8_from_word(hard_buffer + i + payload_cnt) !=
                          read_u8_from_word(pb_current->data + i),
                          "Frame at index %d != pb->data at index %d", i +
                          payload_cnt, i);

        pb_current = pb_current->next;
        arch_load_cache ((u32*) pb_current, sizeof (pb_header_t) / 4);
        arch_load_cache ((u32*) pb_current->data, BLK_SIZE / 4);
        payload_cnt += BLK_SIZE;
        for (i = 0; i < 116; i++)
            test_fail_if (read_u8_from_word(hard_buffer + i + payload_cnt) !=
                          read_u8_from_word(pb_current->data + i),
                          "Frame at index %d != pb->data at index %d", i, i);

        diag_printf ("frame_buffer : %p\n", hard_buffer);
    }
    test_end;

    pb_first = (pb_t *) job->first_pb_desc;
    for (pb_current = pb_first ; pb_current;
         pb_current = pb_current->next)
    {
        pb_last = pb_current;
    }

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


void
test_thread_process (cyg_addrword_t data)
{
    bridgedma_segmentation_test_case ();

#ifdef __sparc__
    // Verify the register corresponding to the MF header1.
    test_begin (test, "Verify registers")
    {
        arch_load_cache ((u32 *) &job, 36);
        test_fail_if (PHY_BDGDMA_JOB_HEADER_3210 != job->mf_header1);
        test_fail_if (PHY_BDGDMA_JOB_HEADER_7654 != job->mf_header2);
        test_fail_if (phy_bridgedma_status (INVALID_PTR) != false);

        test_fail_if (BF_GET (PHY_BRIDGEDMA_PB_CONF__OFFSET,
                              PHY_BDGDMA_PB_CONF) !=
                      TEST_BRIDGEDMA_JOB_OFFSET);
        test_fail_if (BF_GET (PHY_BRIDGEDMA_PB_CONF__LENGTH,
                              PHY_BDGDMA_PB_CONF) !=
                      BLK_SIZE);

        test_fail_if (BF_GET (PHY_BRIDGEDMA_JOB_LENGTH__LENGTH,
                              PHY_BDGDMA_JOB_LENGTH) != job->data_len);
    }
    test_end;


    diag_printf ("******************* Debug *********************\n");
    diag_printf ("BRIDGEDMA Status ; %x\n",
                 phy_bridgedma_status (INVALID_PTR));
    diag_printf ("[JOBD current] bridge : %x \t Cesar :%p\n",
                 PHY_BDGDMA_CURRENT_JOBD_PTR, &job);

    diag_printf ("[JOBD length] bridge : %x \t Cesar :%x\n",
                 BF_GET(PHY_BRIDGEDMA_JOB_LENGTH__LENGTH,
                        PHY_BDGDMA_JOB_LENGTH), job->data_len);
    diag_printf ("[JOBD header length] bridge : %x \t Cesar :%x\n",
                 BF_GET(PHY_BRIDGEDMA_JOB_LENGTH__HEADER_LEN,
                        PHY_BDGDMA_JOB_LENGTH),
                 job->header_len);
    diag_printf ("[JOBD header_3210] bridge : %x \t Cesar : %x\n",
                 PHY_BDGDMA_JOB_HEADER_3210, job->mf_header1);
    diag_printf ("[JOBD header_7654] bridge : %x \t Cesar : %x\n",
                 PHY_BDGDMA_JOB_HEADER_7654, job->mf_header2);
    diag_printf ("[JOB BUFF ADDR] bridge : %x \t Cesar : %p\n",
                PHY_BDGDMA_JOB_BUF_ADD, hard_buffer);

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

    diag_printf ("[First PB] bridged : %x \t Cesar : %p\n",
                 PHY_BDGDMA_CURRENT_PBD_PTR, job->first_pb_desc);

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

    bridgedma_segmentation_verify_test_case ();

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