summaryrefslogtreecommitdiff
path: root/cesar/mac/common/src/store.c
blob: 2c1a0a6ad4885f46dcba60375a966993aa0a9924 (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/common/src/store.c
 * \brief   MFS and STA store, store pointers to MFS and STA information.
 * \ingroup mac_common
 */
#include "common/std.h"
#include "mac/common/store.h"

#include "hal/arch/arch.h"
#include "mac/common/defs.h"
#include "lib/blk.h"

/** Define index into MFS tables tables. */
enum mac_store_kind_t
{
    MAC_STORE_KIND_TXRX_MASK = 1,
    MAC_STORE_KIND_RX = 0,
    MAC_STORE_KIND_TX = 1,
    MAC_STORE_KIND_BCAST_MASK = 2,
    MAC_STORE_KIND_RX_BCAST = 2,
    MAC_STORE_KIND_STA_NB = 3,
    MAC_STORE_KIND_TX_BCAST = 3
};
typedef enum mac_store_kind_t mac_store_kind_t;

/** Find MFS for any traffic from and to a peer.  MFS for non associated STA
 * are not recorded here (actually, they are recorded nowhere). */
struct mac_store_sta_t
{
    /** Other information about a STA. */
    sta_t sta;
    /** PLID MFS for this STA. */
    mfs_t *plid[MAC_STORE_KIND_STA_NB][MAC_PLID_NB];
    /** MME MFS for this STA. */
    mfs_t *mme[MAC_STORE_KIND_STA_NB];
    /** LLID MFS for RX from this STA.  Only allocated when really needed, the
     * first four pointers are not used. */
    mfs_t **rx_llid;
    /** Maximum RX LLID ever created, used for optimization. */
    uint rx_llid_max;
};
typedef struct mac_store_sta_t mac_store_sta_t;

/** MFS store context. */
struct mac_store_t
{
    /* PLID MFS for broadcast TX from us. */
    mfs_t *bcast_tx_plid[MAC_PLID_NB];
    /* MME MFS for broadcast TX from us. */
    mfs_t *bcast_tx_mme;
    /** LLID MFS for TX and GLID MFS for TX/RX table.
     *  - 0x00-0x03: unused
     *  - 0x04-0x7f: TX LLID
     *  - 0x80-0xf7: TX/RX GLID
     *  - 0xf8-0xfd: unused & discover and central beacons */
    mfs_t *lglid[MAC_LID_BEACON_MAX + 1];
    /** Maximum TX LLID ever created, used for optimization. */
    uint tx_llid_max;
    /** Maximum GLID ever created, used for optimization. */
    uint glid_max;
    /** Peers, indexed by TEI. */
    mac_store_sta_t *sta[MAC_TEI_STA_MAX + 1];
    /** Unassociated MFS. */
    list_t unassociated;
};
/* Forward declaration in mac/common/store.h. */

/** Global mac store context. */
mac_store_t mac_store_global_context;

mac_store_t *
mac_store_init (void)
{
    uint i;
    dbg_assert (BLK_SIZE == (MAC_LLID_MAX + 1) * sizeof (mfs_t *));
    mac_store_t *ctx = &mac_store_global_context;
    for (i = 0; i < COUNT (ctx->bcast_tx_plid); i++)
        ctx->bcast_tx_plid[i] = NULL;
    ctx->bcast_tx_mme = NULL;
    for (i = 0; i < COUNT (ctx->lglid); i++)
        ctx->lglid[i] = NULL;
    ctx->tx_llid_max = 0;
    ctx->glid_max = 0;
    for (i = 0; i < COUNT (ctx->sta); i++)
        ctx->sta[i] = NULL;
    list_init (&ctx->unassociated);
    return ctx;
}

void
mac_store_uninit (mac_store_t *ctx)
{
    uint i;
    for (i = 0; i < COUNT (ctx->bcast_tx_plid); i++)
        dbg_assert (ctx->bcast_tx_plid[i] == NULL);
    dbg_assert (ctx->bcast_tx_mme == NULL);
    for (i = 0; i < COUNT (ctx->lglid); i++)
        dbg_assert (ctx->lglid[i] == NULL);
    for (i = 0; i < COUNT (ctx->sta); i++)
        dbg_assert (ctx->sta[i] == NULL);
    dbg_assert (list_empty (&ctx->unassociated));
}

/**
 * Factorise code to find a MFS.
 * \param  ctx  store context
 * \param  tx  true for a TX MFS
 * \param  bcast  true for a broadcast MFS
 * \param  mme  true for a MME MFS
 * \param  lid  link id
 * \param  tei  peer terminal equipment id
 * \param  add  true if a MFS is being added
 * \return  pointer to an MFS pointer or NULL if no STA or no LLID table or
 * GLID slot used
 */
static inline mfs_t **
mac_store_mfs_slot_get (mac_store_t *ctx, bool tx, bool bcast, bool mme,
                        uint lid, uint tei, bool add __FL)
{
    dbg_assert (ctx);
    dbg_blame ((mme && lid == MAC_LID_NONE)
               || (!mme && (MAC_LID_IS_XLID (lid)
                            || MAC_LID_IS_BEACON (lid))));
    dbg_blame (((MAC_LID_IS_GLID (lid)    /* Needed when tei is unknown. */
                 || MAC_LID_IS_BEACON (lid)) && tei == 0)
               || (bcast && tx && tei == MAC_TEI_BCAST)
               || (!(bcast && tx) && MAC_TEI_IS_STA (tei)));
    if (!mme && (lid >= MAC_GLID_MIN))
    {
        /* GLID table contains TX and RX MFS, check it is the requested
         * type. */
        if (ctx->lglid[lid] && ctx->lglid[lid]->common.tx != tx)
        {
            dbg_blame (!add);
            return NULL;
        }
        else
        {
            if (add && lid <= MAC_GLID_MAX)
                ctx->glid_max = MAX (ctx->glid_max, lid);
            return &ctx->lglid[lid];
        }
    }
    else if (!mme && (tx && lid >= MAC_LLID_MIN))
    {
        if (add)
            ctx->tx_llid_max = MAX (ctx->tx_llid_max, lid);
        return &ctx->lglid[lid];
    }
    else if (tx && bcast && !mme && MAC_LID_IS_PLID (lid))
        return &ctx->bcast_tx_plid[lid];
    else if (tx && bcast && mme)
        return &ctx->bcast_tx_mme;
    else
    {
        /* Here, we need a STA. */
        if (add && !ctx->sta[tei])
            mac_store_sta_add_ (ctx, tei __fl);
        mac_store_sta_t *sta = ctx->sta[tei];
        if (!sta)
            return NULL;
        else
        {
            if (!mme && lid >= MAC_LLID_MIN)
            {
                dbg_blame (!tx && lid <= MAC_LLID_MAX);
                if (add)
                    sta->rx_llid_max = MAX (sta->rx_llid_max, lid);
                if (!sta->rx_llid)
                {
                    if (add)
                    {
                        /* Allocate a new table. */
                        sta->rx_llid = blk_alloc_zero_ (_fl);
                        return &sta->rx_llid[lid];
                    }
                    else
                        return NULL;
                }
                else
                    return &sta->rx_llid[lid];
            }
            else
            {
                mac_store_kind_t kind = (tx ? MAC_STORE_KIND_TXRX_MASK : 0)
                    | (bcast ? MAC_STORE_KIND_BCAST_MASK : 0);
                if (mme)
                    return &sta->mme[kind];
                else
                {
                    dbg_blame (lid <= MAC_PLID_MAX);
                    return &sta->plid[kind][lid];
                }
            }
        }
    }
}

mfs_t *
mac_store_mfs_get_ (mac_store_t *ctx, bool tx, bool bcast, bool mme, uint lid,
                    uint tei __FL)
{
    mfs_t *mfs = NULL;
    dbg_assert (ctx);
    arch_dsr_lock ();
    mfs_t **slot = mac_store_mfs_slot_get (ctx, tx, bcast, mme, lid, tei,
                                           false __fl);
    if (slot && *slot)
    {
        mfs = *slot;
        blk_addref_ (mfs __fl);
    }
    arch_dsr_unlock ();
    return mfs;
}

mfs_t *
mac_store_mfs_add_ (mac_store_t *ctx, bool tx, bool bcast, bool mme, uint lid,
                    uint tei, bool *added __FL)
{
    mfs_t *mfs;
    dbg_assert (ctx);
    dbg_assert (added);
    arch_dsr_lock ();
    mfs_t **slot = mac_store_mfs_slot_get (ctx, tx, bcast, mme, lid, tei,
                                           true __fl);
    dbg_assert (slot);
    *added = false;
    if (!*slot)
    {
        mfs = blk_alloc_ (_fl);
        if (!tx)
            mfs_rx_init (&mfs->rx, bcast, mme, lid, tei);
        else
            mfs_tx_init (&mfs->tx, bcast, mme, lid, tei);
        arch_reorder_barrier ();
        *slot = mfs;
        *added = true;
    }
    else
    {
        mfs = *slot;
    }
    blk_addref_ (mfs __fl);
    arch_dsr_unlock ();
    return mfs;
}

mfs_t *
mac_store_mfs_unassociated_add_ (mac_store_t *ctx, bool tx, bool bcast,
                                 bool mme, uint lid, uint tei __FL)
{
    mfs_t *mfs;
    dbg_assert (ctx);
    mfs = blk_alloc_ (_fl);
    if (!tx)
        mfs_rx_init_unassociated (&mfs->rx, bcast, mme, lid, tei);
    else
        mfs_tx_init_unassociated (&mfs->tx, bcast, mme, lid, tei);
    blk_addref_ (mfs __fl);
    arch_dsr_lock ();
    list_push (&ctx->unassociated, &mfs->common.store_unassociated_link);
    arch_dsr_unlock ();
    return mfs;
}

void
mac_store_mfs_alias_ (mac_store_t *ctx, mfs_t *mfs, uint glid __FL)
{
    dbg_assert (ctx);
    dbg_blame (mfs);
    dbg_blame (MAC_LID_IS_GLID (glid));
    dbg_blame (MAC_LID_IS_LLID (mfs->common.lid));
    dbg_blame (mfs->common.lid_alias == MAC_LID_NONE);
    /* Add the MFS to the GLID slot. */
    dbg_blame (ctx->lglid[glid] == NULL);
    arch_dsr_lock ();
    blk_addref_ (mfs __fl);
    ctx->glid_max = MAX (ctx->glid_max, glid);
    ctx->lglid[glid] = mfs;
    /* Modify the MFS. */
    mfs->common.lid_alias = mfs->common.lid;
    mfs->common.lid = glid;
    arch_dsr_unlock ();
}

void
mac_store_mfs_remove_ (mac_store_t *ctx, mfs_t *mfs __FL)
{
    dbg_assert (ctx);
    dbg_blame (mfs);
    arch_dsr_lock ();
    if (!mfs->common.unassociated)
    {
        bool alias = mfs->common.lid_alias != MAC_LID_NONE;
        /* Remove alias first. */
        uint lid = alias ? mfs->common.lid_alias : mfs->common.lid;
        mfs_t **slot =
            mac_store_mfs_slot_get (ctx, mfs->common.tx, mfs->common.bcast,
                                    mfs->common.mme, lid,
                                    mfs->common.tei, false __fl);
        if (slot && *slot)
        {
            /* Order is important.  The code could be interrupted... */
            dbg_assert (*slot == mfs);
            arch_reorder_barrier (); /* ...here... */
            *slot = NULL;
            arch_reorder_barrier (); /* ...or here... */
            blk_release_ (mfs __fl);
        }
        /* Now remove actual GLID if aliased. */
        if (alias)
        {
            dbg_assert (MAC_LID_IS_LLID (lid));
            dbg_assert (MAC_LID_IS_GLID (mfs->common.lid));
            slot = &ctx->lglid[mfs->common.lid];
            dbg_assert (*slot == mfs);
            arch_reorder_barrier ();
            *slot = NULL;
            arch_reorder_barrier ();
            blk_release_ (mfs __fl);
        }
    }
    else
    {
        list_remove (&ctx->unassociated,
                     &mfs->common.store_unassociated_link);
        blk_release_ (mfs __fl);
    }
    arch_dsr_unlock ();
}

/**
 * Helper for the travel functions.
 * \param  ctx  store context
 * \param  mfs  MFS to travel or NULL
 * \param  travel  callback function
 * \param  user  user data
 *
 * Will unlock DSR.
 */
void
mac_store_mfs_travel_mfs_and_unlock (mac_store_t *ctx, mfs_t *mfs,
                                     mac_store_travel_t travel, void *user)
{
    if (mfs)
    {
        blk_addref (mfs);
        arch_dsr_unlock ();
        travel (ctx, mfs, user);
        blk_release (mfs);
    }
    else
        arch_dsr_unlock ();
}

void
mac_store_mfs_travel (mac_store_t *ctx, mac_store_travel_t travel,
                      void *user)
{
    /* Will unlock DSR between each MFS fetch to reduce DSR latency. */
    uint i;
    dbg_assert (ctx);
    dbg_assert (travel);
    /* First travel local MFS. */
    for (i = 0; i < MAC_PLID_NB; i++)
    {
        if (ctx->bcast_tx_plid[i])
        {
            arch_dsr_lock ();
            mac_store_mfs_travel_mfs_and_unlock (ctx, ctx->bcast_tx_plid[i],
                                                 travel, user);
        }
    }
    if (ctx->bcast_tx_mme)
    {
        arch_dsr_lock ();
        mac_store_mfs_travel_mfs_and_unlock (ctx, ctx->bcast_tx_mme, travel,
                                             user);
    }
    for (i = MAC_LLID_MIN; i <= ctx->tx_llid_max; i++)
    {
        if (ctx->lglid[i])
        {
            arch_dsr_lock ();
            mac_store_mfs_travel_mfs_and_unlock (ctx, ctx->lglid[i], travel,
                                                 user);
        }
    }
    for (i = MAC_GLID_MIN; i <= ctx->glid_max; i++)
    {
        if (ctx->lglid[i])
        {
            arch_dsr_lock ();
            mac_store_mfs_travel_mfs_and_unlock (ctx, ctx->lglid[i], travel,
                                                 user);
        }
    }
    /* Then unassociated MFS (DSR locked, to preserve list integrity). */
    list_node_t *u, *uend, *unext;
    arch_dsr_lock ();
    uend = list_end (&ctx->unassociated);
    for (u = list_begin (&ctx->unassociated); u != uend; u = unext)
    {
        unext = list_next (u);
        travel (ctx, PARENT_OF (mfs_t, common.store_unassociated_link, u),
                user);
    }
    arch_dsr_unlock ();
    /* Do not forget beacon MFS. */
    if (ctx->lglid[MAC_LID_DISCOVER])
    {
        arch_dsr_lock ();
        mac_store_mfs_travel_mfs_and_unlock (ctx, ctx->lglid[MAC_LID_DISCOVER],
                                             travel, user);
    }
    if (ctx->lglid[MAC_LID_SPC_CENTRAL])
    {
        arch_dsr_lock ();
        mac_store_mfs_travel_mfs_and_unlock (ctx,
                                             ctx->lglid[MAC_LID_SPC_CENTRAL],
                                             travel, user);
    }
    /* Now travel by TEI. */
    for (i = MAC_TEI_STA_MIN; i <= MAC_TEI_STA_MAX; i++)
        if (ctx->sta[i])
            mac_store_mfs_travel_by_tei (ctx, i, travel, user);
}

void
mac_store_mfs_travel_by_tei (mac_store_t *ctx, uint tei,
                             mac_store_travel_t travel, void *user)
{
    uint i, j;
    mac_store_sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (MAC_TEI_IS_STA (tei));
    dbg_assert (travel);
    arch_dsr_lock ();
    sta = ctx->sta[tei];
    if (!sta)
    {
        arch_dsr_unlock ();
    }
    else
    {
        /* Ensure we do not lose the STA. */
        blk_addref (sta);
        arch_dsr_unlock ();
        /* Travel MFS. */
        for (i = 0; i < MAC_STORE_KIND_STA_NB; i++)
        {
            for (j = 0; j < MAC_PLID_NB; j++)
            {
                if (sta->plid[i][j])
                {
                    arch_dsr_lock ();
                    mac_store_mfs_travel_mfs_and_unlock (ctx, sta->plid[i][j],
                                                         travel, user);
                }
            }
            if (sta->mme[i])
            {
                arch_dsr_lock ();
                mac_store_mfs_travel_mfs_and_unlock (ctx, sta->mme[i], travel,
                                                     user);
            }
        }
        if (sta->rx_llid)
        {
            mfs_t **rx_llid;
            arch_dsr_lock ();
            rx_llid = sta->rx_llid;
            if (rx_llid)
            {
                blk_addref (rx_llid);
                arch_dsr_unlock ();
                for (i = MAC_LLID_MIN; i <= sta->rx_llid_max; i++)
                {
                    if (rx_llid[i])
                    {
                        arch_dsr_lock ();
                        mac_store_mfs_travel_mfs_and_unlock (
                            ctx, rx_llid[i], travel, user);
                    }
                }
                blk_release (rx_llid);
            }
            else
                arch_dsr_unlock ();
        }
        blk_release (sta);
    }
}

void
mac_store_mfs_travel_locked (mac_store_t *ctx, mac_store_travel_t travel,
                             void *user)
{
    uint i;
    dbg_assert (ctx);
    dbg_assert (travel);
    /* First travel local MFS. */
    for (i = 0; i < MAC_PLID_NB; i++)
    {
        if (ctx->bcast_tx_plid[i])
            travel (ctx, ctx->bcast_tx_plid[i], user);
    }
    if (ctx->bcast_tx_mme)
    {
        if (ctx->bcast_tx_mme)
            travel (ctx, ctx->bcast_tx_mme, user);
    }
    for (i = MAC_LLID_MIN; i <= ctx->tx_llid_max; i++)
    {
        if (ctx->lglid[i])
            travel (ctx, ctx->lglid[i], user);
    }
    for (i = MAC_GLID_MIN; i <= ctx->glid_max; i++)
    {
        if (ctx->lglid[i])
            travel (ctx, ctx->lglid[i], user);
    }
    /* Then unassociated MFS. */
    list_node_t *u, *uend, *unext;
    uend = list_end (&ctx->unassociated);
    for (u = list_begin (&ctx->unassociated); u != uend; u = unext)
    {
        unext = list_next (u);
        travel (ctx, PARENT_OF (mfs_t, common.store_unassociated_link, u),
                user);
    }
    /* Do not forget beacon MFS. */
    if (ctx->lglid[MAC_LID_DISCOVER])
        travel (ctx, ctx->lglid[MAC_LID_DISCOVER], user);
    if (ctx->lglid[MAC_LID_SPC_CENTRAL])
        travel (ctx, ctx->lglid[MAC_LID_SPC_CENTRAL], user);
    /* Now travel by TEI. */
    for (i = MAC_TEI_STA_MIN; i <= MAC_TEI_STA_MAX; i++)
        if (ctx->sta[i])
            mac_store_mfs_travel_by_tei_locked (ctx, i, travel, user);
}

void
mac_store_mfs_travel_by_tei_locked (mac_store_t *ctx, uint tei,
                                    mac_store_travel_t travel, void *user)
{
    uint i, j;
    mac_store_sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (MAC_TEI_IS_STA (tei));
    dbg_assert (travel);
    sta = ctx->sta[tei];
    if (sta)
    {
        /* Travel MFS. */
        for (i = 0; i < MAC_STORE_KIND_STA_NB; i++)
        {
            for (j = 0; j < MAC_PLID_NB; j++)
            {
                if (sta->plid[i][j])
                    travel (ctx, sta->plid[i][j], user);
            }
            if (sta->mme[i])
                travel (ctx, sta->mme[i], user);
        }
        if (sta->rx_llid)
        {
            mfs_t **rx_llid;
            rx_llid = sta->rx_llid;
            if (rx_llid)
            {
                for (i = MAC_LLID_MIN; i <= sta->rx_llid_max; i++)
                {
                    if (rx_llid[i])
                        travel (ctx, rx_llid[i], user);
                }
            }
        }
    }
}

sta_t *
mac_store_sta_get_ (mac_store_t *ctx, uint tei __FL)
{
    dbg_assert (ctx);
    dbg_blame (MAC_TEI_IS_STA (tei));
    arch_dsr_lock ();
    mac_store_sta_t *sta = ctx->sta[tei];
    if (sta)
    {
        blk_addref_ (sta __fl);
        arch_dsr_unlock ();
        return &sta->sta;
    }
    else
    {
        arch_dsr_unlock ();
        return NULL;
    }
}

void
mac_store_sta_add_ (mac_store_t *ctx, uint tei __FL)
{
    mac_store_sta_t *sta;
    uint i, j;
    dbg_assert (ctx);
    dbg_blame (MAC_TEI_IS_STA (tei));
    /* If the DSR lock time need to be decreased, the STA could be
     * initialised, then existence be checked again. */
    arch_dsr_lock ();
    if (!ctx->sta[tei])
    {
        dbg_assert (sizeof (sta_t) <= BLK_SIZE);
        sta = blk_new_ ((blk_destructor_t) sta_uninit __fl);
        sta_init (&sta->sta, tei);
        /* Clear the new STA tables. */
        for (i = 0; i < COUNT (sta->plid); i++)
        {
            for (j = 0; j < COUNT (sta->plid[i]); j++)
            {
                sta->plid[i][j] = NULL;
            }
            sta->mme[i] = NULL;
        }
        sta->rx_llid = NULL;
        sta->rx_llid_max = 0;
        /* Done, ready to be used (order is important). */
        arch_reorder_barrier ();
        ctx->sta[tei] = sta;
    }
    arch_dsr_unlock ();
}

bool
mac_store_sta_remove_ (mac_store_t *ctx, uint tei __FL)
{
    mac_store_sta_t *sta;
    uint i, j;
    dbg_assert (ctx);
    dbg_blame (MAC_TEI_IS_STA (tei));
    dbg_blame (ctx->sta[tei] != NULL);
    arch_dsr_lock ();
    /* Order is important! */
    sta = ctx->sta[tei];
    arch_reorder_barrier ();
    /* Check the MFS are freed. */
    bool ok = true;
    for (i = 0; ok && i < COUNT (sta->plid); i++)
    {
        for (j = 0; ok && j < COUNT (sta->plid[i]); j++)
            if (sta->plid[i][j])
                ok = false;
        if (sta->mme[i])
            ok = false;
    }
    if (ok && sta->rx_llid)
    {
        for (i = 0; ok && i < MAC_LLID_MAX; i++)
            if (sta->rx_llid[i])
                ok = false;
    }
    if (ok)
    {
        /* Can no longer be accessed by interrupts, order is important. */
        arch_reorder_barrier ();
        ctx->sta[tei] = NULL;
        arch_reorder_barrier ();
        /* Free the LLID table.  If someone owns a reference, this is not the
         * store and it does not care about rx_llid. */
        if (sta->rx_llid)
            blk_release_ (sta->rx_llid __fl);
        sta->rx_llid = NULL;
        /* Release the STA information. */
        blk_release_ (sta __fl);
    }
    arch_dsr_unlock ();
    return ok;
}

int
mac_store_get_free_tx_llid (mac_store_t *ctx)
{
    int i;
    dbg_assert (ctx);
    for (i = MAC_LLID_MIN; i <= MAC_LLID_MAX; i++)
    {
        if (ctx->lglid[i] == NULL)
            return i;
    }
    return -1;
}

int
mac_store_get_free_tei (mac_store_t *ctx)
{
    int i;
    dbg_assert (ctx);
    for (i = MAC_TEI_STA_MIN; i <= MAC_TEI_STA_MAX; i++)
    {
        if (ctx->sta[i] == NULL)
            return i;
    }
    return -1;
}