summaryrefslogtreecommitdiff
path: root/cesar/mac/sar/src/tx.c
blob: f4f8c25bfb1dbc8155745757036d2f8b73fdab22 (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
/* Cesar project {{{
 *
 * Copyright (C) 2012 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/sar/src/tx.c
 * \brief   All functions to send a frame.
 * \ingroup mac_sar
 */
#include "common/std.h"
#include "mac/sar/sar.h"
#include "mac/sar/inc/context.h"
#include "mac/sar/inc/brg.h"
#include "mac/sar/inc/sar.h"

/**
 * Allocate and fill the PB's headers to store a new MF.
 * \param  length  the length of the Mac Frame.
 * \param  ssn  the next SSN to put in the header.
 * \param  mme  if the segments will contain MMEs.
 * \param  begin  the first pb of the chain
 * \param  last  the last pb of the chain.
 * \param  expiration_time_ntb  the expiration PB date in NTB ticks.
 * \return  the number of PBs allocated.
 */
static inline uint
sar_tx_job_desc_create_complete_list (u16 length, u16 ssn, bool mme,
                                      pb_t **begin, pb_t **last,
                                      u32 expiration_time_ntb)
{
    pb_t *pb_first;
    pb_t *pb_last;
    pb_t *pb;
    blk_t *blk_last;
    u8 number_pb;
    pb_header_t header;

    number_pb = (length + BLK_SIZE - 1) / BLK_SIZE;

    pb_first = (pb_t *) blk_alloc_desc_range (number_pb,
                                              &blk_last);
    pb_last = (pb_t *) blk_last;
    pb_last->next = NULL;

    header.opsf = false;
    header.mfbf = 0x0;
    header.mfbo = 0x0;
    header.mmqf = mme;
    header.vpbf = true;
    header.rsvd = 0x0;

    /*  Fill the header for each PB */
    for (pb = pb_first; pb; pb = pb->next, ssn ++)
    {
        blk_addref_desc ((blk_t*) pb);
        header.ssn = ssn;
        pb->header = header;

        /* Add the expiration time. */
        pb->expiration_ntb = expiration_time_ntb;
    }

    *begin = pb_first;
    *last = pb_last;

    return number_pb;
}

/**
 * Create a bridge DMA job to segment the frame.
 * \param  ctx  the sar context.
 * \param  md_data  the msdu data structure.
 * \param  mfs  the MFS to segment to.
 * \param  job_mfs  the job to fill.
 * \return  the first PB of the job.
 */
PRIVATE pb_t * ARCH_ILRAM_PRIO (1) __attribute__ ((noinline))
sar_tx_job_desc_create (sar_t *ctx, sar_msdu_t *md_data, mfs_tx_t * mfs,
                        sar_job_mfs_t *job_mfs)
{
    u16 ssn;
    u16 mf_complete_length = 0;
    u16 missing_size;
    u16 available_size;
    uint pb_nb_allocated;
    uint last_seg_offset;
    pb_t *pb_first;
    pb_t *pb_last;
    phy_bridgedma_job_t *job;

    dbg_claim (ctx);
    dbg_claim (mfs);
    dbg_claim (job_mfs);

    job = &job_mfs->job;
    job->data_addr = md_data->buffer_address;
    job->data_len = md_data->length;

    if (mfs->common.mme || mfs->common.ats)
    {
        job->header_len = SAR_MF_MFH_SIZE + SAR_MF_ATS_SIZE;
        job->mf_header1 = (mfs->common.mme ? SAR_MF_TYPE_MME :
            SAR_MF_TYPE_DATA_ATS)
            | ((md_data->length - 1 + SAR_MF_ATS_SIZE) << 2)
            | (md_data->ats_confounder << 16);
        job->mf_header2 = md_data->ats_confounder >> 16;
        mf_complete_length = SAR_MF_MFH_SIZE + SAR_MF_ATS_SIZE
           + md_data->length + SAR_MF_ICV_SIZE;
    }
    else
    {
        job->header_len = SAR_MF_MFH_SIZE;
        job->mf_header1 = (SAR_MF_TYPE_DATA) | ((md_data->length -1) << 2);
        job->mf_header2 = 0;
        mf_complete_length = SAR_MF_MFH_SIZE + md_data->length
            + SAR_MF_ICV_SIZE;
    }

    /* We have the last PB of the MFS and the offset is different form 0. */
    if (job->first_pb_desc)
    {
        available_size = BLK_SIZE - mfs->last_seg_offset;

        if (available_size < mf_complete_length)
            missing_size = mf_complete_length - available_size;
        else
            missing_size = 0;
    }
    else
    {
        missing_size = mf_complete_length;
        available_size = 0;
    }

    /* Create the list of PBs */
    ssn = mfs->next_ssn;
    job->first_pb_offset = mfs->last_seg_offset;
    /* Compute the next offset. */
    last_seg_offset =
        (mfs->last_seg_offset + mf_complete_length) % BLK_SIZE;
    /* Reset append zero if no more bytes are available in PB. */
    job->append_zero = last_seg_offset != 0;
    /* Need to allocate new PBs. */
    if (missing_size)
    {
        pb_nb_allocated = sar_tx_job_desc_create_complete_list (
            missing_size, ssn, mfs->common.mme, &pb_first, &pb_last,
            md_data->arrival_ntb + mfs->common.expiration_delay_tck);
        /* Store the SSN in the MFS. */
        mfs->next_ssn = ssn + pb_nb_allocated;

        /* Chain the PBs allocated to the PB in the job if the job already
         * contains the last PB of the MFS. */
        if (job->first_pb_desc)
        {
            job_mfs->pb_quantity = 1 + pb_nb_allocated;
            ((pb_t*) job->first_pb_desc)->next = pb_first;
        }
        else
        {
            job->first_pb_desc = (blk_t *) pb_first;
            /* Store the MFBF. */
            pb_first->header.mfbf = true;
            job_mfs->pb_quantity = pb_nb_allocated;
        }

        /* Add the PB to the MFS */
        pbproc_mfs_insert (md_data->mfs, pb_first, pb_last, pb_nb_allocated);
        /* Avoid compiler to change this order. This protect the last offset
         * variable to not be changed by the PBProc if the compiler change the
         * instruction order. */
        arch_reorder_barrier ();
        mfs->last_seg_offset = last_seg_offset;
        /* Store the mac frame boundary in the last PB. */
        if (!pb_last->header.mfbf && mfs->last_seg_offset)
        {
            pb_last->header.mfbf = true;
            pb_last->header.mfbo = mfs->last_seg_offset;
        }

        return pb_last;
    }
    /* Enough space available in the PB present in the job. */
    else
    {
        job_mfs->pb_quantity = 1;
        mfs->last_seg_offset = last_seg_offset;
        return (pb_t*) job->first_pb_desc;
    }
}

/**
 * Segment the frame
 * \param  ctx  the sar context.
 * \param  md_data  the MSDU data structure.
 */
PRIVATE void
sar_tx_mac_framing (sar_t *ctx, sar_msdu_t *md_data)
{
    dbg_claim (ctx);
    dbg_claim (md_data);
    dbg_claim (md_data->buffer_address);
    dbg_claim (md_data->mfs);
    dbg_claim ((md_data->length >= ETH_PACKET_MIN_SIZE_ALLOWED)
               && (md_data->length <= ETH_PACKET_MAX_SIZE));
    /* initialise the job descriptor. */
    ctx->job_tx->mfs = (mfs_t *) md_data->mfs;
    ctx->job_tx->pb_last_taken = false;
    /* Take the last PB from the MFS if possible. */
    if ((ctx->job_tx->job.first_pb_desc =
        (blk_t*) pbproc_mfs_extract_tail (md_data->mfs)))
    {
        ctx->job_tx->pb_last_taken = true;
        /* Add a reference on the PB. */
        blk_addref_desc (ctx->job_tx->job.first_pb_desc);
        ((pb_t*) ctx->job_tx->job.first_pb_desc)->expiration_ntb =
            md_data->arrival_ntb + md_data->mfs->common.expiration_delay_tck;
    }
    dbg_assert ((ctx->job_tx->job.first_pb_desc == NULL
                 && ctx->job_tx->mfs->tx.last_seg_offset == 0)
                || (ctx->job_tx->job.first_pb_desc
                    && ctx->job_tx->mfs->tx.last_seg_offset != 0));
    /* Create the job for the Bridge DMA
     * Fill the job and create allocate the PBs necessary to stock the MF
     * contained in the md_data
     */
    ctx->job_tx->tail = sar_tx_job_desc_create (ctx, md_data, md_data->mfs,
                                                ctx->job_tx);
    phy_bridgedma_start (ctx->bridgedma_ctx, &ctx->job_tx->job,
                         &ctx->job_tx->job);
}

/**
 * The MFS MME/PLID is in the release state, recreate one.
 * \param  ctx  the sar context.
 * \param  mfs  the MFS to recreate.
 * \return  the reference on the new MFS.
 */
static inline mfs_tx_t*
sar_msdu_process__mfs_mme_plid_in_release (sar_t *ctx, mfs_tx_t *mfs)
{
    bool bcast;
    bool mme;
    uint lid;
    uint tei;
    bool added;

    bcast = mfs->common.bcast;
    mme = mfs->common.mme;
    lid = mfs->common.lid;
    tei = mfs->common.tei;
    /* Remove the MFS. */
    sar_mfs_free_tx (ctx, mfs);
    mfs = mac_store_mfs_add_tx (ctx->mac_store, bcast,
                                mme, lid,
                                tei, &added);
    dbg_assert (added);
    sar_mfs_add (ctx, (mfs_t *) mfs);
    blk_release (mfs);
    return mfs;
}

/**
 * Prepare the MSDU to be sent over the medium.
 * \param  ctx  the sar context.
 * \param  buffer  the buffer containing the frame.
 * \param  mfs  the MFS to use.
 * \param  arrival_time_ntb  the arrival NTB date.
 */
inline void
sar_msdu_process__send_frame (sar_t *ctx, u8 *buffer, u16 length,
                              mfs_tx_t *mfs, u32 arrival_time_ntb)
{
    sar_msdu_t msdu;

    blk_addref (mfs);
    if (mfs->common.mme)
        msdu.ats_confounder = lib_rnd32 (&ctx->rnd_gen);
    else
        msdu.ats_confounder = arrival_time_ntb;
    msdu.length = length;
    msdu.mfs = mfs;
    msdu.buffer_address = buffer;
    msdu.arrival_ntb = arrival_time_ntb;
    SAR_TRACE (TX, phy_date (), buffer, length, mfs->common.tei,
               mfs->common.lid, arrival_time_ntb);
    /* Lock DSR because this function can be called in DSR or task context. */
    arch_dsr_lock ();
    sar_tx_mac_framing (ctx, &msdu);
    sar_expiration_mfs_update_ntb (
        ctx, PARENT_OF (mfs_t, tx, msdu.mfs), msdu.arrival_ntb);
    phy_bridgedma_stopped (ctx->bridgedma_ctx);
    sar_bridge_dma_free_head (ctx);
    if (ctx->pbs_missing_for_pbproc)
        /* Refill the PB pool if missing block were registered. */
        sar_pb_pool_refill (ctx, 0);
    arch_dsr_unlock ();
}

void ARCH_ILRAM_PRIO (3)
sar_msdu_process (sar_t *ctx, u8 *buffer, u16 length, mfs_tx_t *mfs,
                  u32 arrival_time_ntb)
{
    dbg_claim (ctx);
    dbg_claim (mfs);
    dbg_claim (mfs->common.tx);
    dbg_claim (length >= ETH_PACKET_MIN_SIZE_ALLOWED
               && length <= ETH_PACKET_MAX_SIZE);

    /* Link statistics. */
    mfs->stats.num_msdus ++;
    mfs->stats.octets += length;

    /* Check sequence. */
    lib_seq_check_packet (&ctx->seq, buffer, length);

    /* Enough block available to send a frame, enough room in MFS, and SAR is
     * activated? */
    if (ctx->activate
        && mfs->seg_nb < mfs_tx_max_seg_nb
        && (blk_slack ()
            || mfs->seg_nb <= MAC_SAR_EM_MAX_PB_ALLOWED
            || (CONFIG_SAR_SLACK_LOW_ALLOW_MME && mfs->common.mme)))
    {
        if (mfs->fsm_state == MFS_FSM_CMD_RELEASE)
        {
            if (MAC_LID_IS_PLID (mfs->common.lid) || mfs->common.mme)
                mfs = sar_msdu_process__mfs_mme_plid_in_release (ctx, mfs);
            else
            {
                SAR_TRACE (MSDU_DROPPED_MFS_RELEASE, buffer, mfs);
                return;
            }
        }

        sar_msdu_process__send_frame (ctx, buffer, length, mfs,
                                  arrival_time_ntb);
    }
    else
    {
        SAR_TRACE (MSDU_DROPPED, buffer);
    }
}