summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/src/prep_mpdu.c
blob: 776a0efe3c7d2c1ac640512394056f72c98e3bc0 (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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/pbproc/src/prep_mpdu.c
 * \brief   Prepare MPDU complex function.
 * \ingroup mac_pbproc
 */
#include "common/std.h"
#include "inc/prep_mpdu.h"
#include "inc/context.h"

#include "mac/common/timings.h"
#include "lib/bitstream.h"

#include "inc/sacki_dec.h"

#include <string.h>

/** Number of prepared PB before ACCESS_CONF. */
#define PBPROC_PB_START_NB 4

/**
 * Commit MPDU.
 * \param  ctx  pbproc context
 * \param  release_head  segments to release list head
 * \param  release_tail  segments to release list tail
 * \param  return_head  segments to return to MFS list head
 * \param  return_tail  segments to return to MFS list tail
 * \param  return_nb  number of segments to return to MFS
 *
 * This means:
 *  - release acknowledged segments.
 *  - give unacknowledged segments to MFS.
 *  - clear prepared MPDU.
 *  - update MFS in CA.
 */
static void
pbproc_prep_mpdu_commit (pbproc_t *ctx,
                         pb_t *release_head, pb_t *release_tail,
                         pb_t *return_head, pb_t *return_tail,
                         uint return_nb);

/**
 * Commit FSM changes.
 * \param  ctx  pbproc context
 */
static void
pbproc_prep_mpdu_commit_fsm (pbproc_t *ctx);

/**
 * Prepare a SOUND MPDU.
 * \param  ctx  pbproc context
 * \param  mfs  mfs for which SOUND is sent
 * \param  fl_tck  frame length
 */
static void
pbproc_prep_mpdu_sound (pbproc_t *ctx, mfs_tx_t *mfs, uint fl_tck);

void
pbproc_prep_mpdu_init (pbproc_t *ctx)
{
    dbg_assert (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    memset (prep, 0, sizeof (*prep));
    dbg_invalid_ptr (prep->main_head);
    dbg_invalid_ptr (prep->main_tail);
    dbg_invalid_ptr (prep->combined_head);
    dbg_invalid_ptr (prep->combined_tail);
    dbg_invalid_ptr (prep->head);
    dbg_invalid_ptr (prep->tail);
}

void ARCH_ILRAM
pbproc_prep_beacon (pbproc_t *ctx)
{
    ca_access_param_t *access;
    mfs_tx_t *mfs;
    pbproc_prep_mpdu_t *prep;
    pbproc_times_t *times;
    /* Check inputs. */
    access = &ctx->access;
    mfs = access->mfs;
    dbg_claim_ptr (mfs);
    dbg_claim (mfs->common.tx && mfs->beacon);
    prep = &ctx->prep_mpdu;
    dbg_claim (!prep->valid);
    times = &ctx->times[access->hybrid];
    /* Early return if removed MFS. */
    if (mfs->ca_state == CA_MFS_STATE_REMOVED)
        return;
    /* Is there a prepared beacon? */
    if (!mfs->seg_nb)
        return;
    /* Fill easy fields. */
    prep->stei = 0;
    prep->dtei = MAC_TEI_BCAST;
    prep->lid = MAC_LID_NONE;
    prep->wack = false;
    prep->unassociated = false;
    prep->rts_cts = false;
    prep->burst = false;
    prep->fc_mode = PHY_FC_MODE (true, ctx->config->fc_symbols_nb);
    prep->sound_reason_code = TONEMAP_SRC_NULL;
    prep->bypass_aes = true;
    /* Modulation. */
    prep->mod = PHY_MOD_MINI_ROBO;
    tonemap_t *tm;
    tm = &ctx->config->tonemask_info.tonemap_robo[PHY_MOD_MINI_ROBO];
    prep->phy_combo_params = tm->phy_combo_params[PHY_PB_SIZE_136];
    prep->gil = tm->gil;
    prep->tonemap = tm->tmdma_desc_head;
    /* Compute number of symbols. */
    prep->tx_date = access->access_date;
    u32 symb_nb = (1 * tm->bits_per_pb[PHY_PB_SIZE_136]
                   + tm->bits_per_symbol - 1) / tm->bits_per_symbol;
    u32 data_tck = MAC_PAYLOAD_TCK (symb_nb, ctx->symbol_tck[tm->gil]);
    prep->flp_tck = times->pre_fcs_tck + data_tck + MAC_B2BIFS_TCK;
    /* Does it fit? */
    if (times->pre_fcs_tck + data_tck + MAC_B2BIFS_TCK > access->duration_tck)
        return;
    /* Get beacon PB. */
    pb_t *seg;
    seg = mfs->head;
    dbg_assert_ptr (seg);
    prep->main_head = prep->main_tail = seg;
    prep->main_seg_nb = 1;
    prep->main_seg_nb_reserved = 0;
    /* Change MFS. */
    mfs->seg_nb--;
    slist_pop_front (mfs->, bare);
    /* Prepare PB chain, simple one. */
    prep->head = prep->tail = seg;
    prep->pb_nb_total = 1;
    /* Prepare FC. */
    prep->main_mfs_cmd = MFS_FSM_CMD_NOP;
    prep->main_mfs_rsp = MFS_FSM_RSP_NB;
    pbproc_tx_beacon_params_t *params =
        (void *) (seg->data + MAC_PB136_BYTES);
    prep->fc_av.words[0] = BF_FILL (
        PBPROC_FC_BEACON_W0,
        (DT_AV, PBPROC_FC_DT_BEACON),
        (ACCESS, false),
        (SNID, ctx->alloc.snid),
        (BTS_LSB24, prep->tx_date & 0xffffff));
    prep->fc_av.words[1] = BF_FILL (
        PBPROC_FC_BEACON_W1,
        (BTS_MSB8, (prep->tx_date >> 24) & 0xff),
        (BTO0, params->bto[0]),
        (BTO1_LSB8, params->bto[1] & 0xff));
    prep->fc_av.words[2] = BF_FILL (
        PBPROC_FC_BEACON_W2,
        (BTO1_MSB8, (params->bto[1] >> 8) & 0xff),
        (BTO2, params->bto[2]),
        (BTO3_LSB8, params->bto[3] & 0xff));
    prep->fc_av.words[3] = BF_FILL (
        PBPROC_FC_BEACON_W3,
        (BTO3_MSB8, (params->bto[3] >> 8) & 0xff));
    /* FC 1.0: SOF with no response, non ROBO, 40 symbols, no CC. */
    prep->fc10 = 0x2f00;
    /* Patch payload. */
    if (params->bpsto)
    {
        dbg_claim (params->bpsto > seg->data
                   && params->bpsto <= seg->data + MAC_PB136_BYTES - 3);
        u32 bpsto = prep->tx_date - access->beacon_period_start_date;
        bitstream_direct_write (params->bpsto, 0, bpsto, 24);
    }
    /* Ok, ready to go! */
    prep->main_mfs = mfs;
    blk_addref (mfs);
    prep->combined_mfs = NULL;
    prep->seg_nb_pending = 0;
    prep->valid = true;
}

void ARCH_ILRAM
pbproc_prep_mpdu (pbproc_t *ctx)
{
    ca_access_param_t *access;
    mfs_tx_t *mfs;
    pbproc_prep_mpdu_t *prep;
    pbproc_times_t *times;
    uint access_duration_tck;
    uint access_data_offset_tck;
    /* Check inputs. */
    dbg_claim (ctx);
    access = &ctx->access;
    mfs = access->mfs;
    dbg_claim_ptr (mfs);
    dbg_claim (
        mfs->common.tx
        && !mfs->beacon
        && (mfs->common.mme || MAC_LID_IS_XLID (mfs->common.lid))
        && ((!mfs->common.bcast && MAC_TEI_IS_STA (mfs->common.tei))
            || (mfs->common.bcast && mfs->common.tei == MAC_TEI_BCAST)));
    prep = &ctx->prep_mpdu;
    dbg_claim (!prep->valid);
    times = &ctx->times[access->hybrid];
    /* Early return if removed MFS. */
    if (mfs->ca_state == CA_MFS_STATE_REMOVED)
        return;
    /* Fill easy fields.  Work around Intellon behaviour: they do not accept
     * our MAC_LID_NONE value for MME only MPDU. */
    prep->dtei = mfs->common.tei;
    prep->lid = mfs->common.lid != MAC_LID_NONE ? mfs->common.lid
        : MAC_LID_NO_DATA;
    prep->wack = !mfs->common.bcast;
    prep->unassociated = mfs->common.unassociated;
    prep->burst = false; /* TODO */
    prep->fc_mode = PHY_FC_MODE (access->hybrid, ctx->config->fc_symbols_nb);
    prep->stei = (!mfs->common.bcast && prep->unassociated) ? 0 : ctx->config->tei;
    /* Should we use RTS CTS?
     * TODO: real test is more complex. */
    prep->rts_cts = mfs->common.bcast && ctx->config->rts_broadcast;
    /* Prepare start dates and raw access duration. */
    if (prep->rts_cts)
    {
        uint rts_cts_dur_tck;
        prep->rts_tx_date = access->access_date;
        rts_cts_dur_tck = times->rts_rcg_cts_cmg_tck;
        prep->tx_date = prep->rts_tx_date + rts_cts_dur_tck;
        access_duration_tck = access->duration_tck - rts_cts_dur_tck;
    }
    else
    {
        prep->tx_date = access->access_date;
        access_duration_tck = access->duration_tck;
    }
    /* Prepare tonemap. */
    access_data_offset_tck = prep->tx_date - access->beacon_period_start_date
        + times->pre_fcs_tck;
    uint tm_data_max_tck;
    uint tmi;
    tonemap_sound_reason_code_t sound_reason_code = TONEMAP_SRC_NULL;
    tonemap_t *tm;
    uint max_fl_tck;
    uint rifs_av_one_sym_tck, rifs_av_two_sym_tck, rifs_av_g2_sym_tck;
    /* Assume non adapted ROBO mode.  No limit on tonemap validity. */
    tm_data_max_tck = access_duration_tck;
    tmi = PHY_MOD_ROBO;
    tm = &ctx->config->tonemask_info.tonemap_robo[tmi];
    max_fl_tck = times->max_fl_tck;
    rifs_av_one_sym_tck = MAC_RIFS_DEFAULT_TCK;
    rifs_av_two_sym_tck = MAC_RIFS_DEFAULT_TCK;
    rifs_av_g2_sym_tck = MAC_RIFS_DEFAULT_TCK;
    /* Find tonemap. */
    if (MAC_TEI_IS_STA (prep->dtei))
    {
        sta_t *sta = mac_store_sta_get (ctx->store, prep->dtei);
        if (sta)
        {
            tonemaps_t *tms = sta->tx_tonemaps;
            uint default_tmi = tms->default_tmi;
            if (default_tmi == TONEMAP_INDEX_INITIAL_START)
                sound_reason_code = TONEMAP_SRC_INITIAL;
            else if (default_tmi == TONEMAP_INDEX_INITIAL_ERROR)
                sound_reason_code = TONEMAP_SRC_ERROR;
            else if (default_tmi == TONEMAP_INDEX_INITIAL_SOUND_COMPLETE)
                /* ROBO */;
            else
            {
                tmi = default_tmi;
                /* TODO: no interval lookup for the moment. */
                if (tmi >= PHY_MOD_ROBO_NB)
                {
                    tm = tms->tm[tmi];
                    if (!access->hybrid && access->cfp)
                        max_fl_tck = MAC_FL_TO_TCK (tms->max_fl_av);
                    rifs_av_one_sym_tck = tms->rifs_av_one_sym_tck;
                    rifs_av_two_sym_tck = tms->rifs_av_two_sym_tck;
                    rifs_av_g2_sym_tck = tms->rifs_av_g2_sym_tck;
                }
                else
                {
                    tm = &ctx->config->tonemask_info.tonemap_robo[tmi];
                }
            }
            blk_release (sta);
        }
    }
    /* No sound unless a CE is running. */
    if (!ctx->chandata_nb)
        sound_reason_code = TONEMAP_SRC_NULL;
    /* Prepare parameters. */
    prep->mod = tmi < PHY_MOD_ROBO_NB ? tmi : PHY_MOD_TM;
    prep->phy_combo_params = tm->phy_combo_params[PHY_PB_SIZE_520];
    prep->gil = tm->gil;
    prep->tonemap = tm->tmdma_desc_head;
    prep->sound_reason_code = sound_reason_code;
    /* How many symbols could be send.  Note: time is reserved for SACK even
     * for broadcast. */
    uint max_seg_nb, max_symb_nb;
    uint bits_per_pb;
    /* TODO: implement late segment addition. */
    if (sound_reason_code != TONEMAP_SRC_NULL)
        max_seg_nb = 1;
    else
    {
        /* Early return if no segments (can be -1 if tail is currently
         * extracted). */
        if (mfs->seg_nb <= 0)
            return;
        max_seg_nb = mfs->seg_nb;
    }
    bits_per_pb = tm->bits_per_pb[PHY_PB_SIZE_520];
    /* Do not put more than one segment in the last symbol.  Add one symbol if
     * this arise. */
    uint ba = tm->bits_per_symbol > bits_per_pb
        ? tm->bits_per_symbol - bits_per_pb : 0;
    max_symb_nb = (max_seg_nb * bits_per_pb + ba + tm->bits_per_symbol - 1)
        / tm->bits_per_symbol;
    /* Can we fit more than two symbols? */
    if (max_symb_nb > 2
        && (2 * MAC_DX567_TCK + ctx->symbol_tck[tm->gil] <= tm_data_max_tck)
        && (times->pre_fcs_tck + 2 * MAC_DX567_TCK + ctx->symbol_tck[tm->gil]
            + rifs_av_g2_sym_tck + times->sack_tck <= access_duration_tck))
    {
        /* Can fit more than two symbols, how many? */
        uint data_max_tck = max_fl_tck - rifs_av_g2_sym_tck;
        data_max_tck = MIN (data_max_tck, tm_data_max_tck);
        data_max_tck =
            MIN (data_max_tck, access_duration_tck - times->pre_fcs_tck
                 - rifs_av_g2_sym_tck - times->sack_tck);
        max_symb_nb = MIN (max_symb_nb,
                           (uint) (2 + (data_max_tck - 2 * MAC_DX567_TCK)
                                   / ctx->symbol_tck[tm->gil]));
    }
    else if (max_symb_nb > 1
             && 2 * MAC_DX567_TCK <= tm_data_max_tck
             && (times->pre_fcs_tck + 2 * MAC_DX567_TCK + rifs_av_two_sym_tck
                 + times->sack_tck <= access_duration_tck))
    {
        /* Can fit two symbols. */
        max_symb_nb = 2;
    }
    else if (max_symb_nb > 0
             && MAC_DX567_TCK <= tm_data_max_tck
             && (times->pre_fcs_tck + MAC_DX567_TCK + rifs_av_one_sym_tck
                 + times->sack_tck <= access_duration_tck))
    {
        /* Can fit one symbol. */
        max_symb_nb = 1;
    }
    else
    {
        /* Can not fit anything. */
        max_symb_nb = 0;
    }
    uint old_max_seg_nb = max_seg_nb;
    /* Do not put more than one segment in the last symbol.  Remove one or two
     * segments if this arise. */
    if (bits_per_pb >= tm->bits_per_symbol)
        max_seg_nb = max_symb_nb * tm->bits_per_symbol / bits_per_pb;
    else if (max_symb_nb > 0)
        max_seg_nb = ((max_symb_nb - 1) * tm->bits_per_symbol
                      + bits_per_pb) / bits_per_pb;
    else
        max_seg_nb = 0;
    /* Add pending segment if not enough segments are available. */
    if (old_max_seg_nb < max_seg_nb)
    {
        prep->seg_nb_pending = max_seg_nb - old_max_seg_nb;
        max_seg_nb = old_max_seg_nb;
    }
    else
        prep->seg_nb_pending = 0;
    /* Compute symbol number. */
    uint data_tck, rifs_tck;
    uint symb_nb = ((max_seg_nb + prep->seg_nb_pending) * bits_per_pb
                    + tm->bits_per_symbol - 1) / tm->bits_per_symbol;
    if (symb_nb > 2)
    {
        data_tck = MAC_PAYLOAD_TCK (symb_nb, ctx->symbol_tck[tm->gil]);
        rifs_tck = rifs_av_g2_sym_tck;
    }
    else if (symb_nb == 2)
    {
        data_tck = 2 * MAC_DX567_TCK;
        rifs_tck = rifs_av_two_sym_tck;
    }
    else if (symb_nb == 1)
    {
        data_tck = MAC_DX567_TCK;
        rifs_tck = rifs_av_one_sym_tck;
    }
    else
    {
        data_tck = 0;
        rifs_tck = MAC_RIFS_DEFAULT_TCK;
    }
    prep->flp_tck = times->pre_fcs_tck + data_tck + rifs_tck;
    /* Early return if no symbol. */
    if (max_seg_nb == 0)
        return;
    /* Prepare RTS FC. */
    if (prep->rts_cts)
    {
        /* RTS FC. */
        prep->rts_fc_av.words[0] = BF_FILL (
            PBPROC_FC_RTS_CTS_W0,
            (DT_AV, PBPROC_FC_DT_RTS_CTS),
            (ACCESS, false),
            (SNID, ctx->alloc.snid),
            (STEI, prep->stei),
            (DTEI, prep->dtei),
            (LID, prep->lid));
        prep->rts_fc_av.words[1] = BF_FILL (
            PBPROC_FC_RTS_CTS_W1,
            (CFS, access->cfp),
            (BDF, true), /* TODO */
            (HP10DF, false), /* TODO */
            (HP11DF, false), /* TODO */
            (RTSF, true),
            (IGF, false),
            (MNBF, mfs->common.bcast && prep->unassociated),
            (MCF, mfs->common.bcast),
            (DUR, (MAC_RCG_TCK + times->pre_fcs_tck + MAC_CMG_TCK
                   + times->pre_fcs_tck + data_tck + rifs_tck
                   + times->pre_fcs_tck) / MAC_TCK_PER_FL),
            (RESERVED0, 0));
        prep->rts_fc_av.words[2] = BF_FILL (
            PBPROC_FC_RTS_CTS_W2,
            (RESERVED1, 0));
        prep->rts_fc_av.words[3] = BF_FILL (
            PBPROC_FC_RTS_CTS_W3,
            (RESERVED2, 0));
        /* Invalid FC 1.0, HP 1.0 stations will defer (20 symbols and ROBO). */
        prep->rts_fc10 = 0x200000;
    }
    /* Stop here for SOUND. */
    if (sound_reason_code != TONEMAP_SRC_NULL)
    {
        pbproc_prep_mpdu_sound (ctx, mfs, data_tck + rifs_tck);
        return;
    }
    /* Prepare NEK. */
    uint eks = MAC_EKS_CLEAR;
    if (prep->unassociated)
    {
        /* No encryption. */
    }
    else if (mfs->common.bcast)
    {
        if (ctx->config->authenticated)
        {
            mac_nek_t *nek = &ctx->config->nek[ctx->alloc.nek_switch];
            if (nek->eks < MAC_EKS_NB)
            {
                eks = nek->eks;
                prep->nek = nek->nek_enc;
            }
        }
    }
    else if (MAC_TEI_IS_STA (prep->dtei))
    {
        sta_t *sta = mac_store_sta_get (ctx->store, prep->dtei);
        if (sta)
        {
            if (sta->authenticated)
            {
                mac_nek_t *nek = sta->nek
                    ? &(*sta->nek)[ctx->alloc.nek_switch]
                    : &ctx->config->nek[ctx->alloc.nek_switch];
                if (nek->eks < MAC_EKS_NB)
                {
                    eks = nek->eks;
                    prep->nek = nek->nek_enc;
                }
            }
            blk_release (sta);
        }
    }
    prep->bypass_aes = eks == MAC_EKS_CLEAR;
    // TODO: dbg_assert (mfs->common.mme || !prep->bypass_aes);
    /* Data FC. */
    prep->main_mfs_cmd = mfs->fsm_state;
    prep->main_mfs_rsp = MFS_FSM_RSP_NB;
    if (1 /* TODO */)
    {
        mfs_fsm_cmd_t mfs_cmd_mgmt, mfs_cmd_data;
        if (mfs->common.mme)
        {
            mfs_cmd_mgmt = prep->main_mfs_cmd;
            mfs_cmd_data = MFS_FSM_CMD_NOP;
        }
        else
        {
            mfs_cmd_mgmt = MFS_FSM_CMD_NOP;
            mfs_cmd_data = prep->main_mfs_cmd;
        }
        prep->fc_av.words[0] = BF_FILL (
            PBPROC_FC_SOF_W0,
            (DT_AV, PBPROC_FC_DT_SOF),
            (ACCESS, false),
            (SNID, ctx->alloc.snid),
            (STEI, prep->stei),
            (DTEI, prep->dtei),
            (LID, prep->lid));
        prep->fc_av.words[1] = BF_FILL (
            PBPROC_FC_SOF_W1,
            (CFS, access->cfp),
            (BDF, true), /* TODO */
            (HP10DF, false), /* TODO */
            (HP11DF, false), /* TODO */
            (EKS, eks),
            (PPB, pbproc_fc_pbb (mfs->seg_nb - max_seg_nb)),
            (BLE, tm->ble),
            (PBSZ, false), /* TODO */
            (NUM_SYM, symb_nb < 3 ? symb_nb : 3),
            (TMI_AV, tmi));
        prep->fc_av.words[2] = BF_FILL (
            PBPROC_FC_SOF_W2,
            (FL_AV, MAC_TCK_TO_FL (data_tck + rifs_tck)),
            (MPDU_CNT, 0), /* TODO */
            (BURST_CNT, mfs->burst_count),
            (BBF, false), /* TODO */
            (MRTFL, 0), /* TODO */
            (DCPPCF, false), /* TODO */
            (MCF, mfs->common.bcast),
            (MNBF, mfs->common.bcast && prep->unassociated),
            (RSR, false), /* TODO */
            (CLST, false),
            (MFS_CMD_MGMT, mfs_cmd_mgmt),
            (MFS_CMD_DATA, mfs_cmd_data));
        prep->fc_av.words[3] = BF_FILL (
            PBPROC_FC_SOF_W3,
            (MFS_RSP_MGMT, MFS_FSM_RSP_ACK), /* TODO */
            (MFS_RSP_DATA, MFS_FSM_RSP_ACK), /* TODO */
            (BM_SACKI, 0xf)); /* TODO */
    }
    /* Invalid FC 1.0, HP 1.0 stations will defer (20 symbols and ROBO). */
    prep->fc10 = 0x200000;
    /* Prepare PB. */
    dbg_claim ((int) max_seg_nb <= mfs->seg_nb);
    /*  Split in two parts: immediately queued and reserved. */
    uint seg_nb, seg_nb_reserved, i;
    seg_nb = MIN ((int) max_seg_nb, PBPROC_PB_START_NB);
    seg_nb_reserved = max_seg_nb - seg_nb;
    /*  Get head and tail. */
    pb_t *seg;
    seg = mfs->head;
    prep->main_head = seg;
    for (i = 1; i < seg_nb; i++, seg = seg->next)
    {
        dbg_claim_ptr (seg);
    }
    prep->main_tail = seg;
    /*  Set Oldest Pending Segment Flag. */
    prep->main_head->header.opsf = true;
    /*  Change MFS. */
    mfs->seg_nb -= max_seg_nb;
    slist_slice (mfs->, seg, bare);
    prep->main_seg_nb = seg_nb;
    prep->main_seg_nb_reserved = seg_nb_reserved;
    /* Prepare PB chain, simple one. */
    prep->head = prep->main_head;
    prep->tail = prep->main_tail;
    prep->pb_nb_total = max_seg_nb + prep->seg_nb_pending;
    /* Ok, ready to go! */
    prep->main_mfs = mfs;
    blk_addref (mfs);
    prep->combined_mfs = NULL;
    prep->valid = true;
}

void ARCH_ILRAM
pbproc_prep_mpdu_chain (pbproc_t *ctx)
{
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (prep->main_head);
    dbg_claim_ptr (prep->main_tail);
    dbg_claim (prep->main_seg_nb);
    dbg_claim (!prep->combined_mfs);
    dbg_claim_ptr (prep->head);
    dbg_claim_ptr (prep->tail);
    /* Already chained? */
    if (prep->main_seg_nb_reserved == 0)
        return;
    /* Do not chain if MFS has been removed. */
    if (prep->main_mfs->ca_state == CA_MFS_STATE_REMOVED)
    {
        prep->seg_nb_pending += prep->main_seg_nb_reserved;
        prep->main_seg_nb_reserved = 0;
    }
    else
    {
        /* Chain remaining PB. */
        pb_t *seg, *head, *tail;
        dbg_claim (prep->main_seg_nb + prep->main_seg_nb_reserved
                   + prep->seg_nb_pending == prep->pb_nb_total);
        /*  Get head and tail. */
        seg = prep->main_mfs->head;
        head = seg;
        uint i;
        for (i = 1; i < prep->main_seg_nb_reserved; i++, seg = seg->next)
            dbg_claim_ptr (seg);
        tail = seg;
        /*  Change MFS. */
        slist_slice (prep->main_mfs->, seg, bare);
        /*  Chain for TX. */
        prep->main_tail = tail;
        prep->main_seg_nb += prep->main_seg_nb_reserved;
        prep->main_seg_nb_reserved = 0;
        prep->tail->next = head;
        prep->tail = tail;
    }
}

void ARCH_ILRAM
pbproc_prep_mpdu_cancel (pbproc_t *ctx)
{
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (prep->main_head);
    dbg_claim_ptr (prep->main_tail);
    dbg_claim (prep->main_seg_nb);
    dbg_claim (!prep->combined_mfs);
    dbg_claim_ptr (prep->head);
    dbg_claim_ptr (prep->tail);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, NULL, NULL, prep->main_head,
                             prep->main_tail,
                             prep->main_seg_nb + prep->main_seg_nb_reserved);
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_all (pbproc_t *ctx)
{
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (prep->main_head);
    dbg_claim_ptr (prep->main_tail);
    dbg_claim (prep->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (!prep->combined_mfs);
    dbg_claim_ptr (prep->head);
    dbg_claim_ptr (prep->tail);
    dbg_claim (prep->pb_nb_total == prep->main_seg_nb + prep->seg_nb_pending);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, prep->head, prep->tail, NULL, NULL, 0);
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_bitmap (pbproc_t *ctx, const u32 *bmp,
                             uint bmps, uint bmpl)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (bmp);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (prep->main_head);
    dbg_claim_ptr (prep->main_tail);
    dbg_claim (prep->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (!prep->combined_mfs);
    dbg_claim_ptr (prep->head);
    dbg_claim_ptr (prep->tail);
    dbg_claim (prep->pb_nb_total == prep->main_seg_nb + prep->seg_nb_pending);
    /* Read bitmap and collect acknowledged PB. */
    uint bmpsw = bmps / 32;
    uint bmpsb = bmps % 32;
    u32 bmp0 = bmp[bmpsw] >> bmpsb;
    uint i, is = MIN (bmpl - bmps, prep->main_seg_nb);
    pb_t *ok_head, *ok_tail;
    pb_t *nok_head, *nok_tail;
    uint nok_size = 0;
    slist_init (ok_, paste);
    slist_init (nok_, paste_size);
    pb_t *p = prep->main_head;
    for (i = 0; i < is; i++)
    {
        if (bmpsb == 32)
        {
            bmpsw++;
            bmpsb = 0;
            bmp0 = bmp[bmpsw];
        }
        if (bmp0 & 1)
        {
            slist_push_back (nok_, p, paste_size);
        }
        else
        {
            slist_push_back (ok_, p, paste);
        }
        bmpsb++;
        bmp0 >>= 1;
        p = p->next;
    }
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, ok_head, ok_tail, nok_head, nok_tail,
                             nok_size);
}

/** Structure used to handle encoded acknowledgement information. */
struct pbproc_prep_mpdu_ack_encoded_t
{
    /** Not OK chain. */
    pb_t *nok_head, *nok_tail;
    uint nok_nb;
    /** OK chain. */
    pb_t *ok_head, *ok_tail;
    /** Current block. */
    pb_t *p;
    /** Current block number. */
    uint p_i;
};
typedef struct pbproc_prep_mpdu_ack_encoded_t pbproc_prep_mpdu_ack_encoded_t;

/**
 * Callback for compressed SACKI.
 * \param  user  above structure
 * \param  first  first not OK PB
 * \param  nb  number of continuous not OK PB
 */
void ARCH_ILRAM
pbproc_prep_mpdu_ack_encoded_nok_cb (void *user, uint first, uint nb)
{
    dbg_claim (user);
    pbproc_prep_mpdu_ack_encoded_t *ctx = user;
    if (ctx->p_i != first)
    {
        dbg_claim (first > ctx->p_i);
        /* Handle acknowledged blocks. */
        if (ctx->ok_head)
            ctx->ok_tail->next = ctx->p;
        else
            ctx->ok_head = ctx->p;
        while (ctx->p_i != first - 1)
        {
            ctx->p = ctx->p->next;
            ctx->p_i++;
        }
        ctx->ok_tail = ctx->p;
        ctx->p = ctx->p->next;
        ctx->p_i++;
    }
    dbg_claim (ctx->p_i == first);
    /* Handle unacknowledged blocks. */
    if (ctx->nok_head)
        ctx->nok_tail->next = ctx->p;
    else
        ctx->nok_head = ctx->p;
    ctx->nok_nb += nb;
    while (ctx->p_i != first + nb - 1)
    {
        ctx->p = ctx->p->next;
        ctx->p_i++;
    }
    ctx->nok_tail = ctx->p;
    ctx->p = ctx->p->next;
    ctx->p_i++;
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_encoded (pbproc_t *ctx, u32 si[3], uint sil)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (si);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (prep->main_head);
    dbg_claim_ptr (prep->main_tail);
    dbg_claim (prep->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (!prep->combined_mfs);
    dbg_claim_ptr (prep->head);
    dbg_claim_ptr (prep->tail);
    dbg_claim (prep->pb_nb_total == prep->main_seg_nb + prep->seg_nb_pending);
    /* Uncompress. */
    pbproc_prep_mpdu_ack_encoded_t enc =
    {
        NULL, NULL, 0, NULL, NULL, NULL, 0
    };
    enc.p = prep->main_head;
    /* TODO: may cause problem with PB null and burst.  There is more PB than
     * segments and the PB count must be used in order to advance to the right
     * bit for the next MPDU. */
    pbproc_sacki_dec_process (si, sil, prep->main_seg_nb, &enc,
                              pbproc_prep_mpdu_ack_encoded_nok_cb);
    dbg_claim (enc.p_i <= prep->main_seg_nb);
    /* Handle last acknowledged blocks. */
    if (enc.p_i != prep->main_seg_nb)
        slist_push_back_range (enc.ok_, enc.p, prep->main_tail);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, enc.ok_head, enc.ok_tail, enc.nok_head,
                             enc.nok_tail, enc.nok_nb);
}

void ARCH_ILRAM
pbproc_prep_mpdu_fsm_response (pbproc_t *ctx, mfs_fsm_rsp_t mfs_rsp_data,
                               mfs_fsm_rsp_t mfs_rsp_mgmt)
{
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    /* Only one MFS for the moment. */
    prep->main_mfs_rsp = prep->main_mfs->common.mme
        ? mfs_rsp_mgmt : mfs_rsp_data;
}

void ARCH_ILRAM
pbproc_prep_mpdu_commit (pbproc_t *ctx,
                         pb_t *release_head, pb_t *release_tail,
                         pb_t *return_head, pb_t *return_tail, uint return_nb)
{
    dbg_claim (ctx);
    PBPROC_TRACE (PREP_MPDU_COMMIT, return_nb);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    mfs_tx_t *mfs = prep->main_mfs;
    /* Release acknowledged PB in DSR. */
    if (release_head)
    {
        slist_push_back_range (ctx->commit.release_,
                               release_head, release_tail);
        pbproc_fsm_schedule_deferred (ctx);
    }
    /* Change MFS. */
    if (return_head)
    {
        /* If the MFS has been removed, do not give PB back. */
        if (mfs->ca_state == CA_MFS_STATE_REMOVED)
        {
            slist_push_back_range (ctx->commit.release_,
                                   return_head, return_tail);
            pbproc_fsm_schedule_deferred (ctx);
        }
        else
        {
            slist_push_front_range (mfs->, return_head, return_tail, bare);
            mfs->seg_nb += return_nb;
        }
    }
    /* Unset prepared MPDU. */
    dbg_invalid_ptr (prep->head);
    dbg_invalid_ptr (prep->tail);
    prep->pb_nb_total = 0;
    prep->seg_nb_pending = 0;
    dbg_invalid_ptr (prep->main_head);
    dbg_invalid_ptr (prep->main_tail);
    prep->main_seg_nb = prep->main_seg_nb_reserved = 0;
    /* Handle MFS FSM. */
    pbproc_prep_mpdu_commit_fsm (ctx);
    /* Inform CA. */
    if (mfs->ca_state != CA_MFS_STATE_REMOVED)
        ca_mfs_update_locked (ctx->ca, mfs);
    /* No longer valid. */
    blk_release (prep->main_mfs);
    prep->main_mfs = NULL;
    prep->combined_mfs = NULL;
    prep->valid = false;
}

void ARCH_ILRAM
pbproc_prep_mpdu_commit_fsm (pbproc_t *ctx)
{
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    mfs_tx_t *mfs = prep->main_mfs;
    mfs_fsm_cmd_t mfs_cmd = prep->main_mfs_cmd;
    mfs_fsm_rsp_t mfs_rsp = prep->main_mfs_rsp;
    /* If no response or no command, stop here. */
    if (mfs_cmd == MFS_FSM_CMD_NOP || mfs_rsp == MFS_FSM_RSP_NB)
        return;
    /* If MFS has been released during transmission, stop here. */
    if (mfs->fsm_state == MFS_FSM_CMD_RELEASE
        && mfs_cmd != MFS_FSM_CMD_RELEASE)
        return;
    dbg_claim (mfs_cmd == mfs->fsm_state);
    /* Handle response. */
    switch (mfs_rsp)
    {
    case MFS_FSM_RSP_ACK:
        /* Initialisation acknowledged or resynchronised. */
        if (mfs_cmd == MFS_FSM_CMD_INIT
            || (mfs_cmd == MFS_FSM_CMD_RE_SYNC
                && (!mfs->head || !mfs->head->header.opsf)))
            mfs->fsm_state = MFS_FSM_CMD_IN_SYNC;
        /* Release acknowledged. */
        else if (mfs_cmd == MFS_FSM_CMD_RELEASE)
            /* TODO: change MFS FSM state to IDLE and give it to SAR. */
            dbg_claim (0);
        break;
    case MFS_FSM_RSP_NACK:
        /* Receiver reduced its window size. */
        if (mfs_cmd == MFS_FSM_CMD_IN_SYNC)
            mfs->fsm_state = MFS_FSM_CMD_RE_SYNC;
        break;
    case MFS_FSM_RSP_FAIL:
        /* TODO: change MFS FSM state to FAIL and give it to SAR. */
        dbg_claim (0);
        break;
    case MFS_FSM_RSP_HOLD:
        /* Hold any transmission until next period. */
        if (mfs->ca_state != CA_MFS_STATE_REMOVED)
            ca_mfs_hold_locked (ctx->ca, mfs);
        break;
    default:
        /* Not possible, there is only two bits. */
        dbg_assert_default ();
    }
}

static void
pbproc_prep_mpdu_sound (pbproc_t *ctx, mfs_tx_t *mfs, uint fl_tck)
{
    dbg_claim (ctx);
    dbg_claim (mfs);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    /* No encryption. */
    prep->bypass_aes = true;
    /* SOUND FC. */
    prep->fc_av.words[0] = BF_FILL (
        PBPROC_FC_SOUND_W0,
        (DT_AV, PBPROC_FC_DT_SOUND),
        (ACCESS, false),
        (SNID, ctx->alloc.snid),
        (STEI, prep->stei),
        (DTEI, prep->dtei),
        (LID, prep->lid));
    prep->fc_av.words[1] = BF_FILL (
        PBPROC_FC_SOUND_W1,
        (CFS, ctx->access.cfp),
        (PBSZ, false), /* TODO */
        (BDF, true), /* TODO */
        (SAF, false),
        (SCF, false),
        (REQ_TM, TONEMAP_MAX),
        (FL_AV, MAC_TCK_TO_FL (fl_tck)),
        (MPDU_CNT, 0), /* TODO */
        (RESERVED0, 0),
        (PPB, pbproc_fc_pbb (mfs->seg_nb)));
    prep->fc_av.words[2] = BF_FILL (
        PBPROC_FC_SOUND_W2,
        (SRC, prep->sound_reason_code),
        (RESERVED1, 0));
    prep->fc_av.words[3] = BF_FILL (
        PBPROC_FC_SOUND_W3,
        (RESERVED2, 0));
    /* Invalid FC 1.0, HP 1.0 stations will defer (20 symbols and ROBO). */
    prep->fc10 = 0x200000;
    /* Ok, ready to go! */
    prep->main_mfs = mfs;
    blk_addref (mfs);
    prep->combined_mfs = NULL;
    prep->valid = true;
}

void
pbproc_prep_mpdu_sound_ack (pbproc_t *ctx, bool scf)
{
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim (prep->main_seg_nb == 0);
    dbg_claim (!prep->combined_mfs);
    /* Update tone maps. */
    if (scf)
    {
        sta_t *sta = mac_store_sta_get (ctx->store, prep->dtei);
        if (sta)
        {
            tonemaps_t *tms = sta->tx_tonemaps;
            if (tms->default_tmi == TONEMAP_INDEX_INITIAL_START
                || tms->default_tmi == TONEMAP_INDEX_INITIAL_ERROR)
            {
                tms->default_tmi = TONEMAP_INDEX_INITIAL_SOUND_COMPLETE;
            }
            blk_release (sta);
        }
    }
    /* No longer valid. */
    blk_release (prep->main_mfs);
    prep->main_mfs = NULL;
    prep->combined_mfs = NULL;
    prep->valid = false;
}