summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/src/prep_mpdu.c
blob: 208faa3b1ec2cdd3bf6b57be093bc388a1d92efb (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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
/* 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  mpdu_count  MPDU index in burst
 * \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.
 *  - enqueue unacknowledged segments for return to MFS.
 *  - clear prepared MPDU.
 */
static void
pbproc_prep_mpdu_commit (pbproc_t *ctx, uint mpdu_count,
                         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)
{
    uint i;
    dbg_assert (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    memset (prep, 0, sizeof (*prep));
    for (i = 0; i < PBPROC_PREP_MPDU_NB_MAX; i++)
    {
        dbg_invalid_ptr (prep->mpdu[i].main_head);
        dbg_invalid_ptr (prep->mpdu[i].main_tail);
    }
    slist_init (prep->main_commit_return_);
}

void ARCH_ILRAM
pbproc_prep_beacon (pbproc_t *ctx, mfs_tx_t *mfs)
{
    ca_access_param_t *access;
    pbproc_prep_mpdu_t *prep;
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim_ptr (mfs);
    dbg_claim (mfs->common.tx && mfs->beacon);
    dbg_claim (mfs->ca_state != CA_MFS_STATE_REMOVED);
    access = &ctx->access;
    prep = &ctx->prep_mpdu;
    dbg_claim (!prep->valid);
    /* Is there a prepared beacon? */
    if (mfs->seg_nb <= 0)
        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_mpdu_nb = 1;
    prep->mpdu_count = 0;
    prep->current = &prep->mpdu[0];
    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->mpdu[0].tmi = 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->ifs_tck = MAC_B2BIFS_TCK;
    uint pre_fcs_tck = ctx->times_array[PBPROC_TIMES_ARRAY_INDEX
        (true, MAC_COEXISTENCE_FULL_HYBRID_MODE)].pre_fcs_tck;
    prep->flp_tck = pre_fcs_tck + data_tck + MAC_B2BIFS_TCK;
    /* Does it fit? */
    if (prep->flp_tck > access->duration_tck)
        return;
    /* Get beacon PB. */
    pb_t *seg;
    seg = mfs->head;
    dbg_assert_ptr (seg);
    prep->mpdu[0].main_head = prep->mpdu[0].main_tail = seg;
    prep->mpdu[0].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->mpdu[0].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);
    u32 bts = prep->tx_date + ctx->config->ntb_offset_tck;
    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, bts & 0xffffff));
    prep->fc_av.words[1] = BF_FILL (
        PBPROC_FC_BEACON_W1,
        (BTS_MSB8, (bts >> 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 - ctx->alloc.beacon_period_start_date;
        bitstream_direct_write (params->bpsto, 0, bpsto, 24);
    }
    /* Ok, ready to go! */
    prep->main_mfs = mfs;
    blk_addref (mfs);
    prep->valid = true;
}

void ARCH_ILRAM
pbproc_prep_mpdu (pbproc_t *ctx, mfs_tx_t *mfs)
{
    ca_access_param_t *access;
    pbproc_prep_mpdu_t *prep;
    pbproc_times_t * const times = &ctx->times;
    uint access_duration_tck;
    /* Check inputs. */
    dbg_claim (ctx);
    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)));
    dbg_claim (mfs->ca_state != CA_MFS_STATE_REMOVED);
    access = &ctx->access;
    prep = &ctx->prep_mpdu;
    dbg_claim (!prep->valid);
    /* 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->unassociated = mfs->common.unassociated;
    uint ack_dtei = mfs->common.bcast && !prep->unassociated
        ? ctx->config->partial_ack_tei_default : prep->dtei;
    prep->lid = mfs->common.lid != MAC_LID_NONE ? mfs->common.lid
        : MAC_LID_NO_DATA;
    prep->wack = MAC_TEI_IS_STA (ack_dtei);
    prep->fc_mode = PHY_FC_MODE (ctx->alloc.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;
    }
    /* No burst. */
    prep->burst_mpdu_nb = 1;
    prep->mpdu_count = 0;
    prep->current = &prep->mpdu[0];
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[0];
    /* Prepare tonemap. */
    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;
    mpdu->interval = TONEMAP_INTERVAL_NB;
    /* 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
            {
                uint offset_atu = MAC_TCK_TO_ATU (
                    prep->tx_date + times->pre_fcs_tck
                    - ctx->alloc.beacon_period_start_date);
                uint interval = tonemap_interval_find (tms, offset_atu);
                if (interval >= tms->intervals->intervals_nb)
                    tmi = default_tmi;
                else
                {
                    mpdu->interval = interval;
                    mpdu->intervals_version = tms->intervals->version;
                    uint itmi = tms->intervals->interval[interval].tmi;
                    if (itmi == TONEMAP_INDEX_INTERVAL_UNAVAILABLE)
                        sound_reason_code = TONEMAP_SRC_INTERVAL_UNAVAILABLE;
                    else if (itmi == TONEMAP_INDEX_INTERVAL_UNUSABLE)
                        sound_reason_code = TONEMAP_SRC_INTERVAL_UNUSABLE;
                    else if (itmi == TONEMAP_INDEX_INTERVAL_SOUND_COMPLETE)
                        tmi = default_tmi;
                    else
                        tmi = itmi;
                }
                /* Use found tonemap. */
                if (tmi >= PHY_MOD_ROBO_NB)
                {
                    tonemap_t *tmc = tms->tm[tmi];
                    if (tmc)
                    {
                        tm = tmc;
                        if (!ctx->alloc.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
                    {
                        if (!(tms->tm_sound_complete_bitmap & (1 << tmi)))
                            sound_reason_code = tmi;
                        tmi = PHY_MOD_ROBO;
                    }
                }
                else
                {
                    tm = &ctx->config->tonemask_info.tonemap_robo[tmi];
                }
            }
            blk_release (sta);
        }
    }
    /* No sound unless a CE is running and data is transfered. */
    if (!ctx->chandata_nb || mfs->common.mme)
        sound_reason_code = TONEMAP_SRC_NULL;
    /* Prepare parameters. */
    mpdu->tmi = tmi;
    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. */
    uint seg_nb_pending;
    if (old_max_seg_nb < max_seg_nb)
    {
        seg_nb_pending = max_seg_nb - old_max_seg_nb;
        max_seg_nb = old_max_seg_nb;
    }
    else
        seg_nb_pending = 0;
    /* Compute symbol number. */
    uint data_tck, rifs_tck;
    uint symb_nb = ((max_seg_nb + 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->ifs_tck = rifs_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, ack_dtei),
            (LID, prep->lid));
        prep->rts_fc_av.words[1] = BF_FILL (
            PBPROC_FC_RTS_CTS_W1,
            (CFS, access->cfp),
            (BDF, ctx->detect.beacon_detected),
            (HP10DF, ctx->detect.hp10_detected),
            (HP11DF, ctx->detect.hp11_detected),
            (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, ack_dtei),
            (LID, prep->lid));
        prep->fc_av.words[1] = BF_FILL (
            PBPROC_FC_SOF_W1,
            (CFS, access->cfp),
            (BDF, ctx->detect.beacon_detected),
            (HP10DF, ctx->detect.hp10_detected),
            (HP11DF, ctx->detect.hp11_detected),
            (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, prep->mpdu_count),
            (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;
    mpdu->main_head = seg;
    for (i = 1; i < seg_nb; i++, seg = seg->next)
    {
        dbg_claim_ptr (seg);
    }
    mpdu->main_tail = seg;
    /*  Set Oldest Pending Segment Flag. */
    mpdu->main_head->header.opsf = true;
    /*  Change MFS. */
    mfs->seg_nb -= max_seg_nb;
    slist_slice (mfs->, seg, bare);
    mpdu->main_seg_nb = seg_nb;
    prep->main_seg_nb_reserved = seg_nb_reserved;
    /* Prepare PB chain, simple one. */
    mpdu->pb_nb_total = max_seg_nb + seg_nb_pending;
    /* Loop over the last PB, PBDMA null PB is not working. */
    mpdu->main_tail->next = mpdu->main_tail;
    /* Ok, ready to go! */
    prep->main_mfs = mfs;
    blk_addref (mfs);
    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;
    pbproc_prep_mpdu_single_t *mpdu = prep->current;
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (mpdu->main_head);
    dbg_claim_ptr (mpdu->main_tail);
    dbg_claim (mpdu->main_seg_nb);
    /* Already chained? */
    if (prep->main_seg_nb_reserved != 0)
    {
        /* Do not chain if MFS has been removed. */
        if (prep->main_mfs->ca_state == CA_MFS_STATE_REMOVED)
        {
            prep->main_seg_nb_reserved = 0;
        }
        else
        {
            /* Chain remaining PB. */
            pb_t *seg, *head, *tail;
            dbg_claim (mpdu->main_seg_nb + prep->main_seg_nb_reserved
                       <= mpdu->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. */
            mpdu->main_tail->next = head;
            mpdu->main_tail = tail;
            mpdu->main_seg_nb += prep->main_seg_nb_reserved;
            prep->main_seg_nb_reserved = 0;
            /* Loop over the last PB, PBDMA null PB is not working. */
            mpdu->main_tail->next = mpdu->main_tail;
        }
    }
    /* If MFS is empty, forbid more segmentation on the last segment. */
    if (slist_empty (prep->main_mfs->, bare))
        prep->main_mfs->last_seg_offset = 0;
}

void ARCH_ILRAM
pbproc_prep_mpdu_cancel_burst (pbproc_t *ctx)
{
    int mpdu_count;
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    /* Cancel all MPDU. */
    for (mpdu_count = prep->burst_mpdu_nb - 1; mpdu_count >= 0; mpdu_count--)
        if (prep->mpdu[mpdu_count].pb_nb_total)
            pbproc_prep_mpdu_cancel (ctx, mpdu_count);
    pbproc_prep_mpdu_commit_burst (ctx);
}

void ARCH_ILRAM
pbproc_prep_mpdu_cancel (pbproc_t *ctx, uint mpdu_count)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (mpdu->main_head);
    dbg_claim_ptr (mpdu->main_tail);
    dbg_claim (mpdu->main_seg_nb);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, mpdu_count, NULL, NULL,
                             mpdu->main_head, mpdu->main_tail,
                             mpdu->main_seg_nb + prep->main_seg_nb_reserved);
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_all_burst (pbproc_t *ctx)
{
    int mpdu_count;
    /* Check inputs. */
    dbg_claim (ctx);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    /* Commit all MPDU. */
    for (mpdu_count = prep->burst_mpdu_nb - 1; mpdu_count >= 0; mpdu_count--)
        if (prep->mpdu[mpdu_count].pb_nb_total)
            pbproc_prep_mpdu_ack_all (ctx, mpdu_count);
    pbproc_prep_mpdu_commit_burst (ctx);
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_all (pbproc_t *ctx, uint mpdu_count)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (mpdu->main_head);
    dbg_claim_ptr (mpdu->main_tail);
    dbg_claim (mpdu->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (mpdu->pb_nb_total >= mpdu->main_seg_nb);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, mpdu_count,
                             mpdu->main_head, mpdu->main_tail,
                             NULL, NULL, 0);
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_bitmap (pbproc_t *ctx, uint mpdu_count,
                             pbproc_sacki_dec_t *sacki_dec)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    dbg_claim (sacki_dec);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (mpdu->main_head);
    dbg_claim_ptr (mpdu->main_tail);
    dbg_claim (mpdu->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (mpdu->pb_nb_total >= mpdu->main_seg_nb);
    /* Read bitmap and collect acknowledged PB. */
    u32 bmp0 = 0;
    uint bmp0l = 0;
    uint i, is = MIN (sacki_dec->sil, mpdu->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 = mpdu->main_head;
    for (i = 0; i < is; i++)
    {
        if (!bmp0l)
        {
            bmp0l = MIN (is - i, 32u);
            bmp0 = pbproc_sacki_dec_read (sacki_dec, bmp0l);
        }
        if (bmp0 & 1)
        {
            slist_push_back (nok_, p, paste_size);
        }
        else
        {
            slist_push_back (ok_, p, paste);
        }
        bmp0 >>= 1;
        bmp0l--;
        p = p->next;
    }
    /* If there is any PB left, there is no sack information, treat them as
     * error. */
    if (is < mpdu->main_seg_nb)
        slist_push_back_range (nok_, p, mpdu->main_tail,
                               mpdu->main_seg_nb - is, paste_size);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, mpdu_count,
                             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, uint mpdu_count,
                              pbproc_sacki_dec_t *sacki_dec)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    dbg_claim (sacki_dec);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim_ptr (mpdu->main_head);
    dbg_claim_ptr (mpdu->main_tail);
    dbg_claim (mpdu->main_seg_nb);
    dbg_claim (prep->main_seg_nb_reserved == 0);
    dbg_claim (mpdu->pb_nb_total >= mpdu->main_seg_nb);
    /* Uncompress. */
    pbproc_prep_mpdu_ack_encoded_t enc =
    {
        NULL, NULL, 0, NULL, NULL, NULL, 0
    };
    enc.p = mpdu->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 (sacki_dec, mpdu->main_seg_nb, &enc,
                              pbproc_prep_mpdu_ack_encoded_nok_cb);
    dbg_claim (enc.p_i <= mpdu->main_seg_nb);
    /* Handle last acknowledged blocks. */
    if (enc.p_i != mpdu->main_seg_nb)
        slist_push_back_range (enc.ok_, enc.p, mpdu->main_tail);
    /* Commit. */
    pbproc_prep_mpdu_commit (ctx, mpdu_count, enc.ok_head, enc.ok_tail,
                             enc.nok_head, enc.nok_tail, enc.nok_nb);
}

/**
 * Handle tone maps changes after SACKI reception.
 * \param  ctx  pbproc context
 * \param  mpdu_count  MPDU index in burst
 * \param  sacki  SACKI uniform information
 *
 * Part of pbproc_prep_mpdu_ack_uniform not in local ram.
 */
static __attribute__ ((noinline)) void
pbproc_prep_mpdu_ack_uniform_tmi (pbproc_t *ctx, uint mpdu_count, uint sacki)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    /* If valid default TMI or ROBO was used, change default TMI or intervals
     * according to receiver wish. */
    if (!prep->main_mfs->common.bcast)
    {
        dbg_claim (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;
            if (sacki != PBPROC_FC_SACKI_UNIFORM_TMI_RESTART
                && mpdu->interval != TONEMAP_INTERVAL_NB)
            {
                if (tms->default_tmi < TONEMAP_INDEX_NB
                    && mpdu->intervals_version == tms->intervals->version)
                    tms->intervals->interval[mpdu->interval].tmi =
                        TONEMAP_INDEX_INTERVAL_UNAVAILABLE;
            }
            else if (tms->default_tmi < TONEMAP_INDEX_NB
                     || mpdu->tmi < PHY_MOD_ROBO_NB)
            {
                tms->default_tmi =
                    sacki == PBPROC_FC_SACKI_UNIFORM_TMI_DEFAULT_ROBO
                    ? PHY_MOD_ROBO
                    : TONEMAP_INDEX_INITIAL_START;
            }
            blk_release (sta);
        }
    }
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack_uniform (pbproc_t *ctx, uint mpdu_count, uint sacki)
{
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (mpdu_count < PBPROC_PREP_MPDU_NB_MAX);
    /* Handle SACKI. */
    if (sacki == PBPROC_FC_SACKI_UNIFORM_ALL_OK)
        pbproc_prep_mpdu_ack_all (ctx, mpdu_count);
    else
    {
        if (sacki == PBPROC_FC_SACKI_UNIFORM_TMI_DEFAULT_ROBO
            || sacki == PBPROC_FC_SACKI_UNIFORM_TMI_DEFAULT_RESTART
            || sacki == PBPROC_FC_SACKI_UNIFORM_TMI_RESTART)
            pbproc_prep_mpdu_ack_uniform_tmi (ctx, mpdu_count, sacki);
        pbproc_prep_mpdu_cancel (ctx, mpdu_count);
    }
}

void ARCH_ILRAM
pbproc_prep_mpdu_ack (pbproc_t *ctx, const uint sackt[],
                      pbproc_sacki_dec_t *sacki_dec)
{
    int mpdu_count;
    /* Check inputs. */
    dbg_claim (ctx);
    dbg_claim (sackt);
    dbg_claim (sacki_dec);
    pbproc_prep_mpdu_t *prep = &ctx->prep_mpdu;
    dbg_claim (prep->valid);
    /* Handle SACKD for each MPDU. */
    for (mpdu_count = prep->burst_mpdu_nb - 1; mpdu_count >= 0; mpdu_count--)
    {
        if (sackt[mpdu_count] == PBPROC_FC_SACKT_MIXED)
        {
            pbproc_prep_mpdu_ack_bitmap (ctx, mpdu_count, sacki_dec);
        }
        else if (sackt[mpdu_count] == PBPROC_FC_SACKT_MIXED_COMPRESSED)
        {
            pbproc_prep_mpdu_ack_encoded (ctx, mpdu_count, sacki_dec);
        }
        else if (sackt[mpdu_count] == PBPROC_FC_SACKT_NOT_RECEIVED)
        {
            pbproc_prep_mpdu_cancel (ctx, mpdu_count);
        }
        else
        {
            uint sacki = pbproc_sacki_dec_read (sacki_dec, 4);
            pbproc_prep_mpdu_ack_uniform (ctx, mpdu_count, sacki);
        }
    }
    pbproc_prep_mpdu_commit_burst (ctx);
}

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, uint mpdu_count,
                         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;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[mpdu_count];
    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);
    }
    /* Keep returning PB to be put in MFS. */
    if (return_head)
    {
        slist_push_back_range (prep->main_commit_return_, return_head,
                               return_tail);
        prep->main_commit_return_seg_nb += return_nb;
    }
    /* Update link stats. */
    mfs->stats.num_segs_suc += mpdu->main_seg_nb + prep->main_seg_nb_reserved
        - return_nb;
    /* Unset prepared MPDU. */
    mpdu->pb_nb_total = 0;
    dbg_invalid_ptr (mpdu->main_head);
    dbg_invalid_ptr (mpdu->main_tail);
    mpdu->main_seg_nb = prep->main_seg_nb_reserved = 0;
}

inline void
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);
        /* Update link stats. */
        mfs->stats.num_sacks++;
        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:
    default: /* There is only two bits. */
        dbg_claim (mfs_rsp == 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;
    }
}

void ARCH_ILRAM
pbproc_prep_mpdu_commit_burst (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;
    /* Change MFS. */
    if (prep->main_commit_return_head)
    {
        /* If the MFS has been removed, do not give PB back.  Do not put more
         * than one PB in a beacon MFS. */
        if (mfs->ca_state == CA_MFS_STATE_REMOVED
            || (mfs->beacon && mfs->seg_nb))
        {
            slist_push_back_range (ctx->commit.release_,
                                   prep->main_commit_return_head,
                                   prep->main_commit_return_tail);
            pbproc_fsm_schedule_deferred (ctx);
        }
        else
        {
            slist_push_front_range (mfs->, prep->main_commit_return_head,
                                    prep->main_commit_return_tail, bare);
            mfs->seg_nb += prep->main_commit_return_seg_nb;
        }
        /* Empty commit list. */
        slist_init (prep->main_commit_return_);
        prep->main_commit_return_seg_nb = 0;
    }
    /* Commit FSM change. */
    pbproc_prep_mpdu_commit_fsm (ctx);
    /* Inform CA. */
    ca_access_done (ctx->ca);
    /* No longer valid. */
    blk_release (prep->main_mfs);
    prep->main_mfs = NULL;
    prep->valid = false;
}

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;
    /* SOUND without STEI is really bad. */
    dbg_assert (prep->stei);
    /* 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, ctx->detect.beacon_detected),
        (SAF, false),
        (SCF, false),
        (REQ_TM, TONEMAP_MAX),
        (FL_AV, MAC_TCK_TO_FL (fl_tck)),
        (MPDU_CNT, prep->mpdu_count),
        (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->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;
    pbproc_prep_mpdu_single_t *mpdu = &prep->mpdu[0];
    dbg_claim (prep->valid);
    dbg_claim_ptr (prep->main_mfs);
    dbg_claim (mpdu->main_seg_nb == 0);
    /* Update tone maps. */
    if (scf)
    {
        sta_t *sta = mac_store_sta_get (ctx->store, prep->dtei);
        if (sta)
        {
            tonemap_sound_reason_code_t sound_reason_code =
                prep->sound_reason_code;
            tonemaps_t *tms = sta->tx_tonemaps;
            if (sound_reason_code >= TONEMAP_SRC_TMI_MIN
                && sound_reason_code <= TONEMAP_SRC_TMI_MAX)
            {
                tms->tm_sound_complete_bitmap |= 1 << sound_reason_code;
            }
            else if (sound_reason_code == TONEMAP_SRC_ERROR
                     || sound_reason_code == TONEMAP_SRC_INITIAL)
            {
                if (tms->default_tmi == TONEMAP_INDEX_INITIAL_START
                    || tms->default_tmi == TONEMAP_INDEX_INITIAL_ERROR)
                {
                    tms->default_tmi = TONEMAP_INDEX_INITIAL_SOUND_COMPLETE;
                }
            }
            else if (sound_reason_code == TONEMAP_SRC_INTERVAL_UNAVAILABLE
                     || sound_reason_code == TONEMAP_SRC_INTERVAL_UNUSABLE)
            {
                if (tms->default_tmi < TONEMAP_INDEX_NB
                    && mpdu->intervals_version == tms->intervals->version)
                {
                    tms->intervals->interval[mpdu->interval].tmi =
                        TONEMAP_INDEX_INTERVAL_SOUND_COMPLETE;
                }
            }
            blk_release (sta);
        }
    }
    /* Inform CA. */
    ca_access_done (ctx->ca);
    /* No longer valid. */
    blk_release (prep->main_mfs);
    prep->main_mfs = NULL;
    prep->valid = false;
}