summaryrefslogtreecommitdiff
path: root/hle/src/hle.c
blob: 5410d87f1c08bd4280df095cb89c37a4bb478ce8 (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
/* 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"

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

/**
 * Initialize the HLE.
 * 
 * \param  cl  the convergence layer context.
 * \return  the hle context
 */
hle_t *hle_init (cl_t *cl)
{
    hle_t *ctx;
    dbg_assert (cl);

    ctx = &hle_global;
    hle_global.cl = cl;

    /** Intialize 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_data_send_done,
            &hle_global);

    /** Intialize 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_mme_send_done, &hle_global);

    /** Intialize 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);

    /* Tracing */
    HLE_TRACE (INIT, mac_ntb());

    return &hle_global;
}

/**
 * Unitialize the HLE.
 * 
 * \param  ctx  the hle context.
 */
void hle_uninit (hle_t *ctx)
{
    dbg_assert (ctx);
    /* tracing data */
    HLE_TRACE(UNINIT, mac_ntb());

    hle_trace_uninit (ctx);
}

/** 
 * Initialise the interface to add an Interface buffer.
 *
 * \param  ctx  the hle context.
 * \param  cb  the function to call on interface buffer reception.
 * \param  user_data  the user_data to provide on function call.
 */
void
hle_init_interface_buffer_add_cb (hle_t *ctx, hle_interface_buffer_add_cb_t
                                  cb, void *user_data)
{
    dbg_assert (ctx);
    dbg_assert (cb);

    ctx->interface_buffer_add_cb = cb;
    ctx->interface_buffer_add_user_data = user_data;
}

/**
 * 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
 */
void hle_data_send (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);

    /* tracing data */
    HLE_TRACE(DATA_SEND, mac_ntb(), length, buffer);

    cl_data_send (ctx->cl, buffer, length);
}

/**
 * Called by the Convergence layer when the data has been sent to the PWL.
 * This allows the HLE to know which buffer is newly available to be used or
 * give it back to the ARM. (the buffer is borrowed by the linux).
 * 
 * \param  hle  the hle context.
 * \param  buffer the buffer used to send the data.
 */
void hle_data_send_done (hle_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->ipmbox);

    uint word[2];

    /* Tracing data. */
    HLE_TRACE (DATA_SEND_DONE, mac_ntb(), buffer);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_SEND_DONE), 
             (MSG_LENGTH, 1), (PARAM_BUFFER_TYPE, 0)); 

    word[1] = (uint) buffer;

    ipmbox_tx (ctx->ipmbox, word, 2);
}

/**
 * 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.
 */
void hle_data_recv (hle_t *ctx, u8 *buffer, uint length)
{
    uint word[2];

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ETH_PACKET_MIN_SIZE <= length && length <=
                ETH_PACKET_MAX_SIZE);

    /* Tracing data. */
    HLE_TRACE (DATA_RECV, mac_ntb(), 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.
 */
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, mac_ntb(), 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
 */
void hle_mme_send (hle_t *ctx, u8 *buffer, uint length)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ETH_PACKET_MIN_SIZE <= length && length <= ETH_PACKET_MAX_SIZE);

    /* Tracing data. */
    HLE_TRACE (MME_BUFFER_ADD, mac_ntb(), buffer);

    cl_mme_ul_send (ctx->cl, buffer, length);
}

/**
 * Called by the Convergence layer when the data has been sent to the CP.
 * This allows the HLE to know which buffer is newly available to be used or
 * give it back to the ARM. (the buffer is borrowed by the linux).
 * 
 * \param  hle  the hle context.
 * \param  buffer the buffer used to send the data.
 */
void hle_mme_send_done (hle_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->ipmbox);

    uint word[2];

    /* Tracing data. */
    HLE_TRACE (MME_SEND_DONE, mac_ntb(), buffer);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_SEND_DONE), 
             (MSG_LENGTH, 1), (PARAM_BUFFER_TYPE, 1)); 
    word[1] = (uint) buffer;

    ipmbox_tx (ctx->ipmbox, word, 2);
}

/**
 * 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.
 */
void hle_mme_recv (hle_t *ctx, u8 *buffer, uint length)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (60 <= length && length <= 1518);

    uint word[2];

    /* Tracing data. */
    HLE_TRACE (MME_RECV, mac_ntb(), 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.
 */
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, mac_ntb(), 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.
 */
bool hle_ipmbox_recv (hle_t *ctx, u32 *msg_buffer, uint length)
{
    dbg_assert (msg_buffer);
    dbg_assert (ctx);

    uint length_processed;
    uint msg;
    uint type;

    uint data_length;
    uint data_type;

    length_processed = 0;
    while (length_processed < length)
    {
        msg = read_u32_from_word ((u8 *) (msg_buffer + length_processed));
        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 */
                    hle_data_send (ctx,
                            (u8 *) msg_buffer[length_processed + 1], data_length);
                else
                    hle_mme_send (ctx,
                            (u8 *) msg_buffer[length_processed + 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 *) *(msg_buffer
                            + length_processed + 1));
                        break;
                    case HLE_BUFFER_ALLOC_DATA:
                        hle_data_buffer_add (ctx, (u8 *) *(msg_buffer
                            + length_processed + 1));
                        break;
                    case HLE_BUFFER_ALLOC_INTERFACE:
                        (*ctx->interface_buffer_add_cb)
                            (ctx->interface_buffer_add_user_data, (u8 *)
                             *(msg_buffer + length_processed + 1));
                        break;
                }
                break;
        }

        /** Message length + the message header. */
        length_processed += BF_GET (IPMBOX_REG__MSG_LENGTH, msg) + 1;
    }

    return true;
}

/**
 * Activate the HLE to receive messages from the IPMbox.
 *
 * \param  ctx the HLE context.
 * \param  active  boolean to active or unactive the reception of messages.
 */
void
hle_activate (hle_t *ctx, bool active)
{
    dbg_assert (ctx);

    ipmbox_activate (ctx->ipmbox, active);

    /* Trace system. */
    HLE_TRACE (IPMBOX, mac_ntb, active);    
}

/** 
 * Send a packet to the ipmbox.
 *
 * \param  ctx  the hle context.
 * \param  msg  the message address to post the message in the ipmbox.
 * \param  length  the length of the message in words.
 */
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);
}