summaryrefslogtreecommitdiff
path: root/cesar/hle/src/hle.c
blob: 782d3500a0c954b959f02a2a852bdf8964e51f48 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hle.c
 * \brief   function of the HLE
 * \ingroup hle
 *
 */
#include "common/std.h"
#include "common/defs/ethernet.h"
#include "hal/hle/defs.h"
#include "hal/hle/ipmbox.h"
#include "hle/hle.h"
#include "cl/cl.h"
#include "hle/inc/context.h"
#include "hle/inc/trace.h"
#include "mac/common/ntb.h"

#include "common/module.h"

/** Hle global context */
static hle_t hle_global;

/**
 * Send a data to the Convergence Layer to be sent over the PWL.
 * \param  hle  the hle context.
 * \param  buffer  the buffer containing the data to send.
 * \param  length  the data length
 * \param  tag  the priority tag provided by the upper layer.
 * \param  arrival_time_ntb  arrival time in the station in NTB.
 */
static inline void
hle_data_send (hle_t *ctx, u8 *buffer, uint length, uint tag,
               u32 arrival_time_ntb)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (length <= ETH_PACKET_MAX_SIZE);
    if (length < ETH_PACKET_MIN_SIZE_ALLOWED)
    {
        hle_send_done (ctx, buffer);
        HLE_TRACE (DROPPED, phy_date (), 1, length, buffer);
    }
    else
    {
        HLE_TRACE (DATA_SEND, phy_date (), length, buffer);
        cl_data_send (ctx->cl, buffer, length, tag, arrival_time_ntb);
    }
}

/**
 * Receives a data from the Convergence layer.
 * \param  hle  the hle context.
 * \param  buffer  the buffer used to receive the data
 * \param  length  the length of the data received.
 */
inline void
hle_data_recv (hle_t *ctx, u8 *buffer, uint length)
{
    uint word[2];
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    /* Tracing data. */
    HLE_TRACE (DATA_RECV, phy_date (), length, buffer);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_DATA),
             (MSG_LENGTH, 1), (PARAM_MSG_TYPE, 0),
             (PARAM_MSG_LENGTH, length));
    word[1] = (uint) buffer;
    ipmbox_tx (ctx->ipmbox, word, 2);
}

/**
 * Provides a buffer to the CL in order to receive new MMEs from the CP.
 * \param  hle  the hle context.
 * \param  buffer  the buffer to provide to the CL.
 */
static inline void
hle_data_buffer_add (hle_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->cl);
    /* Tracing data. */
    HLE_TRACE (DATA_BUFFER_ADD, phy_date (), buffer);
    cl_data_buffer_add (ctx->cl, buffer);
}

/**
 * Send a MME to the Convergence Layer to be provide to the CP.
 * \param  hle  the hle context.
 * \param  buffer  the buffer containing the data to send.
 * \param  length  the data length
 */
static inline void
hle_mme_send (hle_t *ctx, u8 *buffer, uint length)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (length <= ETH_PACKET_MAX_SIZE);
    dbg_assert (ctx->cl);
    if (length < ETH_PACKET_MIN_SIZE_ALLOWED)
    {
        hle_send_done (ctx, buffer);
        HLE_TRACE (DROPPED, phy_date (), 0, length, buffer);
    }
    else
    {
        HLE_TRACE (MME_SEND, phy_date (), length, buffer);
        cl_mme_ul_send (ctx->cl, buffer, length);
    }
}

/**
 * Receives a MME from the Convergence layer.
 * \param  hle  the hle context.
 * \param  buffer  the buffer used to receive the data
 * \param  length  the length of the data received.
 */
inline void
hle_mme_recv (hle_t *ctx, u8 *buffer, uint length)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE)
                && (length <= ETH_PACKET_MAX_SIZE));
    uint word[2];
    /* Tracing data. */
    HLE_TRACE (MME_RECV, phy_date (), length, buffer);
    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE,  HLE_MSG_TYPE_DATA),
            (MSG_LENGTH, 1), (PARAM_MSG_TYPE, 1),
            (PARAM_MSG_LENGTH, length));
    word[1] = (uint) buffer;
    ipmbox_tx (ctx->ipmbox, word, 2);
}

/**
 * Provides a buffer to the CL in order to receive new MMEs from the CP.
 * \param  hle  the hle context.
 * \param  buffer  the buffer to provide to the CL.
 */
static inline void
hle_mme_buffer_add (hle_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->cl);
    /* Tracing data. */
    HLE_TRACE (MME_BUFFER_ADD, phy_date (), buffer);
    cl_mme_buffer_add (ctx->cl, buffer);
}

/**
 * Receives a message from the Linux.
 *
 * \param  ctx  the hle context.
 * \param  msg_buffer  the pointer of the buffer containing the messages to read
 * \param  length  the length of the messages in words.
 */
void
hle_ipmbox_recv (hle_t *ctx, u32 *msg_buffer, uint length)
{
    dbg_assert (msg_buffer);
    dbg_assert (ctx);
    u32 *m, *mend;
    m = msg_buffer;
    mend = msg_buffer + length;
    uint msg, msg_length, type, data_length, data_type;
    while (m != mend)
    {
        msg = m[0];
        msg_length = 1 + BF_GET (IPMBOX_REG__MSG_LENGTH, msg);
        type = BF_GET (IPMBOX_REG__MSG_TYPE, msg);
        switch (type)
        {
            case HLE_MSG_TYPE_DATA /* Data type */:
                data_type = BF_GET(IPMBOX_REG__PARAM_MSG_TYPE, msg); /* data type MME or DATA */
                data_length = BF_GET (IPMBOX_REG__PARAM_MSG_LENGTH, msg);
                if (data_type == 0) /* Data */
                {
                    uint tag = BF_GET (IPMBOX_REG__PARAM_MSG_PRIO, msg);
                    hle_data_send (ctx, (u8 *) m[1], data_length, tag,
                                   mac_ntb ());
                }
                else
                    hle_mme_send (ctx, (u8 *) m[1], data_length);
                break;
            case HLE_MSG_TYPE_BUFFER_ADD /* Buffer ADD */ :
                data_type = BF_GET (IPMBOX_REG__PARAM_BUFFER_TYPE, msg);
                switch (data_type)
                {
                    case HLE_BUFFER_ALLOC_MME:
                        hle_mme_buffer_add (ctx, (u8 *) m[1]);
                        break;
                    case HLE_BUFFER_ALLOC_DATA:
                        hle_data_buffer_add (ctx, (u8 *) m[1]);
                        break;
                    case HLE_BUFFER_ALLOC_INTERFACE:
                        (*ctx->interface_buffer_add_cb)
                            (ctx->interface_user_data, (u8 *) m[1]);
                        break;
                }
                break;
            case HLE_MSG_TYPE_INTERFACE:
                (*ctx->interface_mme_recv_cb)
                    (ctx->interface_user_data, (u8 *) m[1]);
                break;
#if HLE_TOOLS
            case HLE_MSG_TYPE_RPC:
            case HLE_MSG_TYPE_DEBUG_DUMP:
                hle_tools_recv_msg (ctx->hle_tools, m, msg_length);
                break;
#endif
        }
        m += msg_length;
    }
}

hle_t *
hle_init (cl_t *cl)
{
    hle_t *ctx;
    dbg_assert (cl);
    ctx = &hle_global;
    hle_global.cl = cl;
    /** Initialize the CL data call backs. */
    cl_data_recv_init (cl, (cl_data_recv_cb_t) hle_data_recv, &hle_global);
    cl_data_send_done_init (cl, (cl_data_send_done_cb_t) hle_send_done,
            &hle_global);
    /** Initialize the CL MMEs call backs. */
    cl_mme_init_ul_as_data (cl, (cl_mme_ul_send_done_cb_t) hle_mme_recv,
            &hle_global);
    cl_mme_ul_init_send_done (cl,
            (cl_mme_ul_recv_done_cb_t) hle_send_done, &hle_global);
    /** Initialize the ipmbox */
    hle_global.ipmbox = ipmbox_init (&hle_global,
            (ipmbox_rx_cb_t) hle_ipmbox_recv);
    /** Initialize the trace system. */
    hle_trace_init (&hle_global);
    /* Initialize tools if enabled. */
#if HLE_TOOLS
    hle_global.hle_tools = hle_tools_init (hle_global.ipmbox);
#endif
    /* Tracing */
    HLE_TRACE (INIT, mac_ntb());
    return &hle_global;
}

void
hle_uninit (hle_t *ctx)
{
    dbg_assert (ctx);
    /* tracing data */
    HLE_TRACE(UNINIT, mac_ntb());
    hle_trace_uninit (ctx);
}

void
hle_init_interface_cb (hle_t *ctx, hle_interface_buffer_add_cb_t buffer_cb,
                       hle_interface_mme_recv_cb_t mme_cb,
                       void *user_data)
{
    dbg_assert (ctx);
    dbg_assert (buffer_cb);
    dbg_assert (mme_cb);
    ctx->interface_buffer_add_cb = buffer_cb;
    ctx->interface_mme_recv_cb = mme_cb;
    ctx->interface_user_data = user_data;
}

void
hle_send_done (hle_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->ipmbox);
    uint word[2];
    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_SEND_DONE),
             (MSG_LENGTH, 1));
    word[1] = (uint) buffer;
    ipmbox_tx (ctx->ipmbox, word, 2);
}

void
hle_activate (hle_t *ctx, bool active)
{
    dbg_assert (ctx);
    ipmbox_activate (ctx->ipmbox, active);
    /* Trace system. */
    HLE_TRACE (IPMBOX, phy_date (), active);
}

void
hle_ipmbox_send (hle_t *ctx, u32 *msg, uint length)
{
    dbg_assert (ctx);
    dbg_assert (msg);
    dbg_assert (length);
    ipmbox_tx (ctx->ipmbox, msg, length);
}