summaryrefslogtreecommitdiff
path: root/cesar/test_general/hard/bridgedma/src/bridgedma-crc.c
blob: 85dadaaf9a538d78fd187b085910abe5add4f96a (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/bridgedma-crc.c
 * \brief   Test the CRC of the bridgedma.
 * \ingroup « module »
 *
 */
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>

#include "common/std.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"

#include "inc/bridgedma-regs.h"

#define TEST_BRIDGEDMA_PB_NB 1
#define TEST_BRIDGEDMA_JOB_OFFSET 18
#define TEST_BRIDGEDMA_JOB_LENGTH 125

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 phy_bridgedma_job_t job;

#ifdef __sparc__
static u8 frame_buffer [2048] __attribute__ ((section(".private")));
#else
static u8 frame_buffer [2048];
#endif

static bool it_recv = false;
static pb_t *pb;

u32 enc_tab[256];

/* 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)
{
}

/** Create a job and provide it to the bridgedma. */
void
test_case_crc_tx_create_job (void)
{
    uint ats = 0x12345678;
    phy_bridgedma_t *bridgedma;
    uint i;

    test_case_begin (test, "TX");

    // Allocate a PB.
    pb = (pb_t *) blk_alloc_desc ();

    // Insert null in the next pointer of the descriptor.
    pb->next = NULL;

    /* Initialise the PB. */
    memset (pb->data, 0, BLK_SIZE);

    // Store some data in the PB.
    memset(&job, '\0', sizeof(job));
    job.next = NULL;
    job.data_addr = frame_buffer;
    job.header_len = 6;
    job.data_len = TEST_BRIDGEDMA_JOB_LENGTH;
    job.first_pb_desc = (blk_t *)pb;
    job.first_pb_offset = TEST_BRIDGEDMA_JOB_OFFSET;
    job.segment_len = 512;
    job.direction = 0;
    job.crc_reset = 1;
    job.crc_store = 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);

    /* Fill the frame. */
    for (i = 0; i < job.data_len; i++)
    {
        frame_buffer [i] = (u8) i;
    }

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

    cyg_thread_delay (1);

    phy_bridgedma_start (bridgedma, &job, &job);
}

/** Verify the CRC. */
void
test_case_crc_tx_check (void)
{
    u32 crc;
    u32 crc_in_pb;
    crc_t crc_ctx;

    // Get the PB from the job.
    pb = (pb_t *) job.first_pb_desc;

    // Init the CRC context.
    crc_ctx.width = 32;
    crc_ctx.generator = HPAV_CRC32_GENERATOR;
    crc_ctx.init = HPAV_CRC32_INIT;
    crc_ctx.refin = true;
    crc_ctx.refout = true;
    crc_ctx.xorout = 0xffffffff;
    crc_ctx.reg_init = 0;
    crc_ctx.table.t32 = enc_tab;
    crc_init(&crc_ctx);

    /* For the interruption. */
    while (!it_recv)
        cyg_thread_delay (1);

    /* Compute the CRC on the PB.
     * From the ATS (4 bytes) to the end of the payload (job.data_len). */
    crc = bridgedma_crc_compute_block (&crc_ctx, pb->data + TEST_BRIDGEDMA_JOB_OFFSET
                                       + job.header_len,
                                       job.data_len);

    crc_in_pb = read_u32_from_word (pb->data + TEST_BRIDGEDMA_JOB_OFFSET +
                                    job.data_len + job.header_len);

    test_begin (test, "CRC control")
    {
        test_fail_if (crc != crc_in_pb);
    }
    test_end;

    if (crc != crc_in_pb)
    {
        printf ("CRC : %x\n", crc);
        printf ("CRC in PB : %x\n", crc_in_pb);
        printf ("CRC Error : %d\n", job.crc_error);
    }

    /* Don't release the PB it will be use for the RX way. */
}

void
test_case_crc_rx_create_job (void)
{
    phy_bridgedma_t *bridgedma;

    test_case_begin (test, "RX");

    // Erase the frame_buffer.
    memset (frame_buffer, 0, 2048);

    // Store some data in the PB.
    memset(&job, '\0', sizeof(job));
    job.next = NULL;
    job.data_addr = frame_buffer;
    job.header_len = 6;
    job.data_len =  TEST_BRIDGEDMA_JOB_LENGTH;
    job.first_pb_desc = (blk_t *)pb;
    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.eth_buffer_mask = 0x0;

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

    cyg_thread_delay (1);

    phy_bridgedma_start (bridgedma, &job, &job);
}

/** Verify the CRC. */
void
test_case_crc_rx_check (void)
{
    u32 crc;
    u32 crc_in_pb;
    crc_t crc_ctx;

    // Get the PB from the job.
    pb = (pb_t *) job.first_pb_desc;

    // Init the CRC context.
    crc_ctx.width = 32;
    crc_ctx.generator = HPAV_CRC32_GENERATOR;
    crc_ctx.init = HPAV_CRC32_INIT;
    crc_ctx.refin = true;
    crc_ctx.refout = true;
    crc_ctx.xorout = 0xffffffff;
    crc_ctx.reg_init = 0;
    crc_ctx.table.t32 = enc_tab;
    crc_init(&crc_ctx);

    /* For the interruption. */
    while (!it_recv)
    {
        diag_printf ("Interruption not received yet\n");
        cyg_thread_delay (1);
    }

    /* Compute the CRC on the PB.
     * From the ATS (4 bytes) to the end of the payload (job.data_len). */
    crc = bridgedma_crc_compute_block (&crc_ctx, frame_buffer, job.data_len);

    crc_in_pb = read_u32_from_word (pb->data + TEST_BRIDGEDMA_JOB_OFFSET +
                                    job.data_len + job.header_len);

    test_begin (test, "CRC control")
    {
        test_fail_if (crc != crc_in_pb);
    }
    test_end;

    if (crc != crc_in_pb)
    {
        printf ("Frame Address : %p\n", frame_buffer);
        printf ("PB address : %p\n", pb->data);
        printf ("Data length : %d\n", job.data_len);
        printf ("CRC : %x\n", crc);
        printf ("CRC in PB : %x\n", crc_in_pb);
        printf ("CRC Error : %d\n", job.crc_error);
    }

    // Release the PB.
    blk_release_desc ((blk_t *) pb);
}


void
test_thread_process (cyg_addrword_t data)
{
    // Verify the CRC on TX frames.
    test_case_crc_tx_create_job ();
    test_case_crc_tx_check ();

    // Verify the CRC on RX frames.
    test_case_crc_rx_create_job ();
    test_case_crc_rx_check ();

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

int
cyg_user_start (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;
}