summaryrefslogtreecommitdiff
path: root/cesar/hal/hle/maximus/src/maximus_ether.c
blob: 77955ed2e9bec32908e4910a0a6ca65b257d5106 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/hle/maximus/src/maximus_ether.c
 * \brief   HAL HLE functions for Maximus.
 * \ingroup hal_hle_maximus
 */

#include "common/std.h"
#include "hal/hle/maximus/inc/maximus_ether.h"
#include "hal/hle/maximus/inc/maximus_ipmbox_ctx.h"
#include "hal/hle/defs.h" // for 'HLE_MSG_TYPE_...' and 'ipmbox_msg_hdr_t'
#include "host/fwd.h" // for 'sci_msg_t' and 'ether_msg_hdr_t'
#include "host/station/station.h" // for 'station_log()'
#include "maximus/common/types/ethernet_types.h" // for 'ETHERNET_TYPE_...'
#include "hal/hle/maximus/inc/maximus_interrupts.h" // for 'HAL_HLE_INTERRUPT_IPMBOX'
#include "ecos/packages/hal/maximus/arch/current/include/hal_host_intr.h"
#include "common/defs/ethernet.h" // for 'ETH_PACKET_MAX_SIZE' and 'ETH_PACKET_MIN_SIZE'
#include "interface/sniffer/defs.h" // for 'SNIFFER_BEACON' and 'SNIFFER_MME'
#include <stdlib.h> // for 'malloc'
#include <errno.h>

typedef int (*maximus_ether_recv_function)(ipmbox_t *ctx, sci_msg_t *msg);
maximus_ether_recv_function maximus_ether_function_array[ETHERNET_TYPE_NB] = { &maximus_ether_recv_invalid, // none
                                                                               &maximus_ether_recv_data, // data
                                                                               &maximus_ether_recv_data, // mme
                                                                               &maximus_ether_recv_buffer_add, // data buffer add
                                                                               &maximus_ether_recv_buffer_add, // mme buffer add
                                                                               &maximus_ether_recv_buffer_add, // interface buffer add
                                                                               &maximus_ether_recv_invalid, // buffer released
                                                                               &maximus_ether_recv_invalid }; // sniffer

/**
 * Fill a blank ether header
 * \param ctx current ipmbox context
 * \param msg pointer to sci message to fill header
 * \param type type of message (DATA, MME, DATA_BUFFER_ADD, MME_BUFFER_ADD, INTERFACE_BUFFER_ADD, BUFFER_RELEASED, or SNIFFER)
 * \param flags  not used
 * \return 0 if ok, -1 if it fails with errno =
 * - EINVAL if ctx or msg are NULL, or if arguments are out-of-range
 */
int
maximus_ether_fill_hdr(ipmbox_t *ctx, sci_msg_t *msg, u8 type, u8 flags)
{
    int ret = -1;

    dbg_assert_ptr(ctx);
    dbg_assert_ptr(msg);
    dbg_assert((ETHERNET_TYPE_NB > type) && (ETHERNET_TYPE_NONE < type));
    dbg_assert(ETHERNET_FLAG_MAX >= flags);
    if ((NULL == ctx)
        || (NULL == msg)
        || ((ETHERNET_TYPE_NB <= type) || (ETHERNET_TYPE_NONE >= type))
        || (ETHERNET_FLAG_MAX < flags))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        // reserve space
        if ((int)sizeof(ether_msg_hdr_t) != sci_msg_push(msg, sizeof(ether_msg_hdr_t)))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when pushing SCI message", errno);
        }
        else
        {
            // fill the reserved header
            msg->hdr.ether = (ether_msg_hdr_t*)msg->data_begin;
            msg->hdr.ether->version = ETHERNET_VERSION;
            msg->hdr.ether->type = type;
            msg->hdr.ether->flags = flags;
            msg->hdr.ether->reserved = 0;

            ret = 0;
        }
    }

    return ret; 
}

/**
 * Process ether message received by the sci layer.
 * This function must be registred to SCI layer with SCI_MSG_TYPE_ETHERNET type.
 * \param msg message to process
 * \param ipmbox ipmbox current context
 * \return 0 if ok, -1 if it fails with errno =
 * - EINVAL if msg or ctx are NULL
 * - EPROTO if msg->hdr.ether is null, or if msg->hdr.ether values are out-of-range
 */
int
maximus_ether_recv (sci_msg_t *msg, void *ipmbox)
{
    int ret = -1;
    ipmbox_t *ctx;
    ether_msg_hdr_t ether_hdr;

    dbg_assert_ptr(msg);
    dbg_assert_ptr(ipmbox);
    if((NULL == msg) || (NULL == ipmbox))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        // set ipmbox context    
        ctx = (ipmbox_t *)ipmbox;

        // set header pointer in case of not already done
        memcpy(&ether_hdr , msg->data_begin, sizeof(ether_msg_hdr_t));
        if ((int)sizeof(ether_msg_hdr_t) != sci_msg_pop(msg, sizeof(ether_msg_hdr_t)))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when poping SCI message", errno);
        }
        else
        {
            msg->hdr.ether = &ether_hdr;

            /* Check ether header. */

            dbg_assert_ptr(msg->hdr.ether);
            dbg_assert(ETHERNET_TYPE_NB > msg->hdr.ether->type);
            dbg_assert(ETHERNET_FLAG_MAX >= msg->hdr.ether->flags);
            if ((NULL == msg->hdr.ether)
                || (ETHERNET_TYPE_NB <= msg->hdr.ether->type)
                || (ETHERNET_FLAG_MAX < msg->hdr.ether->flags))
            {
                errno = EPROTO;
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                            "%s: errno = %d", __FUNCTION__, errno);
            }
            else
            {
                // depending on the ether header type, different actions have to be done
                ret = (*(maximus_ether_function_array[msg->hdr.ether->type]))(ctx, msg);
            }
        }
    }

    return ret; 
}

/**
 * Process ether message received by the sci layer, depending on the ether header type.
 * \param ctx ipmbox current context
 * \param msg message to process
 * \return 0 if ok, -1 if it fails with errno =
 * - EINVAL if ctx, msg, msg->sci_hdr or ctx->rx_cb are null
 * - EPROTO if msg->hdr.ether->type is incorrect, or if msg->hdr.ether values are out-of-range
 */

int
maximus_ether_recv_invalid (ipmbox_t *ctx, sci_msg_t *msg)
{
    errno = EPROTO;
    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                "%s: errno = %d", __FUNCTION__, errno);
    dbg_assert_print(false, "errno = %d because ether hdr type is incorrect", errno);
    return -1;
}

int
maximus_ether_recv_data (ipmbox_t *ctx, sci_msg_t *msg)
{
    int ret = -1;

    dbg_assert_ptr(ctx);
    dbg_assert_ptr(msg);
    dbg_assert_ptr(msg->hdr.ether);
    dbg_assert_ptr(msg->sci_hdr);
    dbg_assert((ETH_PACKET_MIN_SIZE <= msg->length) && (ETH_PACKET_MAX_SIZE >= msg->length));
    dbg_assert_ptr(msg->data_begin);
    if((NULL == ctx)
       || (NULL == msg)
       || (NULL == msg->hdr.ether)
       || (NULL == msg->sci_hdr)
       || ((ETH_PACKET_MIN_SIZE > msg->length) || (ETH_PACKET_MAX_SIZE < msg->length))
       || (NULL == msg->data_begin))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        ipmbox_msg_hdr_t hdr;
        u8 *data;
        int len = msg->length;

        // set hdr type
        hdr.type = HLE_MSG_TYPE_DATA;

        // set hdr length
        hdr.length = 1;

        // set hdr param
        // DATA length (11 bits): length of data into DATA buffer
        // type (1 bit):
        // 0 for DATA type
        // 1 for MME type
        hdr.param = msg->length << 1;
        if (ETHERNET_TYPE_MME == msg->hdr.ether->type)
        {
            hdr.param |= 1;
        }

        // get data
        data = (u8 *)malloc(2048);
        memset(data, 0, 2048);
        memcpy(data, msg->data_begin, msg->length);
        if (len != sci_msg_pop(msg, len))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when poping SCI message", errno);
        }
        else
        {
            if (MAXIMUS_HLE_MAILBOX_MAX_SIZE <= ctx->rx.length + hdr.length + 1)
            {
                errno = ENOSPC;
                station_log(&my_station, STATION_LOG_WARNING, STATION_LOGTYPE_ETHER,
                            "%s: errno = %d because mailbox is full", __FUNCTION__, errno);
                dbg_assert_print(!ctx->warning_assert, "errno = %d because mailbox is full", errno);
                // reset total length of messages
                ctx->rx.length = 0;
            }

            /* Update mailbox. */
            // set hdr
            memcpy(&ctx->rx.mailbox[ctx->rx.length], &hdr, sizeof(ipmbox_msg_hdr_t));
            // set buffer pointer (32 bits): pointer to DATA buffer
            ctx->rx.mailbox[ctx->rx.length + 1] = (u32)data;
            // update total length of messages
            ctx->rx.length += hdr.length + 1;

            // raise interruption
            maximus_pending_isrs |= (1 << HAL_HLE_INTERRUPT_IPMBOX);

            ret = 0;
        }
    }

    return ret;
}

int
maximus_ether_recv_buffer_add (ipmbox_t *ctx, sci_msg_t *msg)
{
    int ret = -1;

    dbg_assert_ptr(ctx);
    dbg_assert_ptr(msg);
    dbg_assert_ptr(msg->hdr.ether);
    dbg_assert_ptr(msg->sci_hdr);
    dbg_assert((int)(2 * sizeof(u32)) <= msg->length);
    dbg_assert_ptr(msg->data_begin);
    if((NULL == ctx)
       || (NULL == msg)
       || (NULL == msg->hdr.ether)
       || (NULL == msg->sci_hdr)
       || ((int)(2 * sizeof(u32)) > msg->length)
       || (NULL == msg->data_begin))
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        ipmbox_msg_hdr_t hdr;
        u32 buffer_nb; // number of buffers to be allocated
        uint i;

        // set hdr type
        hdr.type = HLE_MSG_TYPE_BUFFER_ADD;

        // set hdr length
        hdr.length = 1;

        // set hdr param
        // alloc type (3 bits):
        // 0 for DATA buffer allocation
        // 1 for MME buffer allocation
        // 2 for INTERFACE buffer allocation
        hdr.param = 0;
        if (ETHERNET_TYPE_MME_BUFFER_ADD == msg->hdr.ether->type)
        {
            hdr.param = 1;
        }
        if (ETHERNET_TYPE_INTERFACE_BUFFER_ADD == msg->hdr.ether->type)
        {
            hdr.param = 2;
        }

        // get requested buffer nb
        memcpy(&buffer_nb, msg->data_begin, sizeof(u32));
        if ((int)sizeof(u32) != sci_msg_pop(msg, sizeof(u32)))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when poping SCI message", errno);
        }
        else
        {
            if (MAXIMUS_HLE_MAILBOX_MAX_SIZE <= ctx->rx.length + (hdr.length + 1) * buffer_nb)
            {
                errno = ENOSPC;
                station_log(&my_station, STATION_LOG_WARNING, STATION_LOGTYPE_ETHER,
                            "%s: errno = %d because mailbox is full", __FUNCTION__, errno);
                dbg_assert_print(!ctx->warning_assert, "errno = %d because mailbox is full", errno);
                // reset total length of messages
                ctx->rx.length = 0;
            }

            /* Update mailbox. */
            for (i = ctx->rx.length; i < ctx->rx.length + (hdr.length + 1) * buffer_nb; i += hdr.length + 1)
            {
                u32 buffer_id;
                maximus_hle_buffer_t *buffer = (maximus_hle_buffer_t *)malloc(sizeof(maximus_hle_buffer_t));

                // get buffer id
                memcpy(&buffer_id, msg->data_begin, sizeof(u32));
                if ((int)sizeof(u32) != sci_msg_pop(msg, sizeof(u32)))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when poping SCI message", errno);
                }
                else
                {
                    // set hdr
                    memcpy(&ctx->rx.mailbox[i], &hdr, sizeof(ipmbox_msg_hdr_t));

                    // set buffer pointer (32 bits): pointer to allocated buffer
                    ctx->last_buffer->next = buffer;
                    ctx->last_buffer = buffer;
                    ctx->last_buffer->next = NULL;
                    ctx->last_buffer->id = buffer_id;
                    ctx->last_buffer->data = (u32 *)malloc(2048);
                    ctx->rx.mailbox[i+1] = (u32)ctx->last_buffer->data;
                }
            }
            // update total length of messages
            ctx->rx.length += (hdr.length + 1) * buffer_nb;

            // raise interruption
            maximus_pending_isrs |= (1 << HAL_HLE_INTERRUPT_IPMBOX);

            ret = 0;
        }
    }

    return ret;
}

/**
 * Send an Ether SCI message of type DATA, MME, BUFFER_RELEASED, or SNIFFER to Maximus.
 * \param ctx  ipmbox current context
 * \param type  type of message (DATA, MME, BUFFER_RELEASED, or SNIFFER)
 * \param flags  not used
 * \param data_length  sci message data length to send
 * \param data  pointer to sci message data to send
 * \return  0 if ok, -1 if it fails with errno =
 * - EINVAL if ctx or data are null, or of data length equals 0
 * if 'sci_send()' fails, it sets errno
 */
int
maximus_ether_send (ipmbox_t *ctx, u8 type, u8 flags, uint data_length, u32 *data)
{
    int ret = -1;

    dbg_assert_ptr(ctx);
    dbg_assert((ETHERNET_TYPE_NB > type)
               && (ETHERNET_TYPE_NONE < type)
               && (ETHERNET_TYPE_DATA_BUFFER_ADD != type)
               && (ETHERNET_TYPE_MME_BUFFER_ADD != type)
               && (ETHERNET_TYPE_INTERFACE_BUFFER_ADD != type));
    dbg_assert(ETHERNET_FLAG_MAX >= flags);
    dbg_assert(0 != data_length);
    dbg_assert_ptr(data);
    if ( (NULL == ctx)
         || (ETHERNET_TYPE_NB <= type)
         || (ETHERNET_TYPE_NONE >= type)
         || (ETHERNET_TYPE_DATA_BUFFER_ADD == type)
         || (ETHERNET_TYPE_MME_BUFFER_ADD == type)
         || (ETHERNET_TYPE_INTERFACE_BUFFER_ADD == type)
         || (ETHERNET_FLAG_MAX < flags)
         || (0 == data_length)
         || (NULL == data) )
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        // for ether message
        sci_msg_t msg;

        // init for ether message
        memset(ctx->buffer, '\0', SCI_MSG_MAX_SIZE);
        if (0 != sci_msg_init(&msg, ctx->buffer, SCI_MSG_MAX_SIZE))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when initializing SCI message", errno);
        }
        else
        {
            // fill sci data
            if ((int)data_length != sci_msg_push(&msg, data_length))
            {
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                            "%s: errno = %d", __FUNCTION__, errno);
                dbg_assert_print(false, "errno = %d when pushing SCI message", errno); 
            }
            else
            {
                memcpy(msg.data_begin, data, data_length);

                // fill ether and sci header
                if ( (0 != maximus_ether_fill_hdr(ctx, &msg, type, flags))
                     || (0 != sci_fill_hdr(my_station.sci, &msg, SCI_MSG_TYPE_ETHERNET, 0 /* flags */)) )
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when filling header", errno);
                }
                else
                {
                    // send the message
                    if (msg.length != sci_send(my_station.sci, &msg))
                    {
                        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_ETHER,
                                    "%s: errno = %d", __FUNCTION__, errno);
                        dbg_assert_print(false, "errno = %d when sending an Ether SCI message", errno);
                    }
                    else
                    {
                        ret = 0;
                    }
                }
            }
        }
    }

    return ret;
}