eNB_scheduler_primitives.c 124 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

nikaeinn's avatar
nikaeinn committed
22
23
/*! \file eNB_scheduler_primitives.c
 * \brief primitives used by eNB for BCH, RACH, ULSCH, DLSCH scheduling
24
25
26
 * \author  Navid Nikaein and Raymond Knopp
 * \date 2010 - 2014
 * \email: navid.nikaein@eurecom.fr
nikaeinn's avatar
nikaeinn committed
27
 * \version 1.0
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
 * @ingroup _mac

 */

#include "assertions.h"
#include "PHY/defs.h"
#include "PHY/extern.h"

#include "SCHED/defs.h"
#include "SCHED/extern.h"

#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"

#include "LAYER2/MAC/proto.h"
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"

#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"

//#include "LAYER2/MAC/pre_processor.c"
#include "pdcp.h"

#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

#define ENABLE_MAC_PAYLOAD_DEBUG
#define DEBUG_eNB_SCHEDULER 1

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
int choose(int n,int k) {

  int res  = 1;
  int res2 = 1;
  int i;

  if (k>n) return(0);
  if (n==k) return(1);

  for (i=n;i>k;i--) res*=i;
  for (i=2;i<=(n-k);i++) res2*=i;

  return(res/res2);
}

// Patented algorithm from Yang et al, US Patent 2009, "Channel Quality Indexing and Reverse Indexing"
void reverse_index(int N,int M,int r,int *v) {

  int BaseValue=0;
  int IncreaseValue,ThresholdValue;
  int sumV;
  int i;

  r = choose(N,M) - 1 - r;
  memset((void*)v,0,M*sizeof(int));

  sumV=0;
  i=M;
  while (i>0 && r>0) {

    IncreaseValue = choose(N-M+1-sumV-v[i-1]+i-2,i-1);
    ThresholdValue = BaseValue+IncreaseValue;
    if (r>=ThresholdValue) {
      v[i-1]++;
      BaseValue=ThresholdValue;
    }
    else {
      r=r-BaseValue;
      sumV+=v[i-1];
      i--;
      BaseValue=0;
    }

  }

}
knopp's avatar
knopp committed
108
109
int to_prb(int dl_Bandwidth) {
  int prbmap[6] = {6,15,25,50,75,100};
110

knopp's avatar
knopp committed
111
112
113
114
115
116
117
118
119
120
  AssertFatal(dl_Bandwidth < 6,"dl_Bandwidth is 0..5\n");
  return(prbmap[dl_Bandwidth]);
}

int to_rbg(int dl_Bandwidth) {
  int rbgmap[6] = {6,8,13,17,19,25};

  AssertFatal(dl_Bandwidth < 6,"dl_Bandwidth is 0..5\n");
  return(rbgmap[dl_Bandwidth]);
}
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


int get_phich_resource_times6(COMMON_channels_t *cc) {
  int phichmap[4] = {1,3,6,12};
  AssertFatal(cc!=NULL,"cc is null\n");
  AssertFatal(cc->mib!=NULL,"cc->mib is null\n");
  AssertFatal((cc->mib->message.phich_Config.phich_Resource>=0) && 
	      (cc->mib->message.phich_Config.phich_Resource<4),
	      "phich_Resource %d not in 0..3\n",(int)cc->mib->message.phich_Config.phich_Resource);

  return(phichmap[cc->mib->message.phich_Config.phich_Resource]);
}

uint16_t mac_computeRIV(uint16_t N_RB_DL,uint16_t RBstart,uint16_t Lcrbs) {

  uint16_t RIV;

  if (Lcrbs<=(1+(N_RB_DL>>1)))
    RIV = (N_RB_DL*(Lcrbs-1)) + RBstart;
  else
    RIV = (N_RB_DL*(N_RB_DL+1-Lcrbs)) + (N_RB_DL-1-RBstart);

  return(RIV);
}

146
147
148
149
150
151
152
uint8_t getQm(uint8_t mcs) {

  if (mcs<10) return(2);
  else if (mcs<17) return(4);
  else return (6);
}

153
void get_Msg3alloc(COMMON_channels_t *cc,
154

155
156
157
158
		   sub_frame_t current_subframe,
		   frame_t current_frame,
		   frame_t *frame,
		   sub_frame_t *subframe)
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
{

  // Fill in other TDD Configuration!!!!

  if (cc->tdd_Config==NULL) { // FDD
    *subframe = current_subframe+6;

    if (*subframe>9) {
      *subframe = *subframe-10;
      *frame = (current_frame+1) & 1023;
    } else {
      *frame=current_frame;
    }
  } else { // TDD
    if (cc->tdd_Config->subframeAssignment == 1) {
      switch (current_subframe) {

      case 0:
        *subframe = 7;
        *frame = current_frame;
        break;

      case 4:
        *subframe = 2;
        *frame = (current_frame+1) & 1023;
        break;

      case 5:
        *subframe = 2;
        *frame = (current_frame+1) & 1023;
        break;

      case 9:
        *subframe = 7;
        *frame = (current_frame+1) & 1023;
        break;
      }
    } else if (cc->tdd_Config->subframeAssignment == 3) {
      switch (current_subframe) {

      case 0:
      case 5:
      case 6:
        *subframe = 2;
        *frame = (current_frame+1) & 1023;
        break;

      case 7:
        *subframe = 3;
        *frame = (current_frame+1) & 1023;
        break;

      case 8:
        *subframe = 4;
        *frame = (current_frame+1) & 1023;
        break;

      case 9:
        *subframe = 2;
        *frame = (current_frame+2) & 1023;
        break;
      }
    } else if (cc->tdd_Config->subframeAssignment == 4) {
        switch (current_subframe) {

        case 0:
        case 4:
        case 5:
        case 6:
          *subframe = 2;
          *frame = (current_frame+1) & 1023;
          break;

        case 7:
          *subframe = 3;
          *frame = (current_frame+1) & 1023;
          break;

        case 8:
        case 9:
          *subframe = 2;
          *frame = (current_frame+2) & 1023;
          break;
        }
      } else if (cc->tdd_Config->subframeAssignment == 5) {
          switch (current_subframe) {

          case 0:
          case 4:
          case 5:
          case 6:
            *subframe = 2;
            *frame = (current_frame+1) & 1023;
            break;

          case 7:
          case 8:
          case 9:
            *subframe = 2;
            *frame = (current_frame+2) & 1023;
            break;
          }
        }
  }
}

265
266


267
void get_Msg3allocret(COMMON_channels_t *cc,
268
269
270
271
		      sub_frame_t current_subframe,
		      frame_t current_frame,
		      frame_t *frame,
		      sub_frame_t *subframe)
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
{
  if (cc->tdd_Config == NULL) { //FDD
    /* always retransmit in n+8 */
    *subframe = current_subframe + 8;

    if (*subframe > 9) {
      *subframe = *subframe - 10;
      *frame = (current_frame + 1) & 1023;
    } else {
      *frame = current_frame;
    }
  } else {
    if (cc->tdd_Config->subframeAssignment == 1) {
      // original PUSCH in 2, PHICH in 6 (S), ret in 2
      // original PUSCH in 3, PHICH in 9, ret in 3
      // original PUSCH in 7, PHICH in 1 (S), ret in 7
      // original PUSCH in 8, PHICH in 4, ret in 8
      *frame = (current_frame+1) & 1023;
    } else if (cc->tdd_Config->subframeAssignment == 3) {
      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
      // original PUSCH in 3, PHICH in 9, ret in 3 next frame
      // original PUSCH in 4, PHICH in 0, ret in 4 next frame
      *frame=(current_frame+1) & 1023;
    } else if (cc->tdd_Config->subframeAssignment == 4) {
        // original PUSCH in 2, PHICH in 8, ret in 2 next frame
        // original PUSCH in 3, PHICH in 9, ret in 3 next frame
        *frame=(current_frame+1) & 1023;
    } else if (cc->tdd_Config->subframeAssignment == 5) {
        // original PUSCH in 2, PHICH in 8, ret in 2 next frame
        *frame=(current_frame+1) & 1023;
    }
  }
}

306
uint8_t subframe2harqpid(COMMON_channels_t *cc,frame_t frame,sub_frame_t subframe)
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
{
  uint8_t ret = 255;

  AssertFatal(cc!=NULL,"cc is null\n");

  if (cc->tdd_Config == NULL) { // FDD
    ret = (((frame<<1)+subframe)&7);
  } else {

    switch (cc->tdd_Config->subframeAssignment) {

    case 1:
      if ((subframe==2) ||
          (subframe==3) ||
          (subframe==7) ||
          (subframe==8))
        switch (subframe) {
        case 2:
        case 3:
          ret = (subframe-2);
          break;

        case 7:
        case 8:
          ret = (subframe-5);
          break;

        default:
	  AssertFatal(1==0,"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
          break;
        }

      break;

    case 2:
      AssertFatal((subframe==2) || (subframe==7),
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
       
      ret = (subframe/7);
      break;

    case 3:
      AssertFatal((subframe>1) && (subframe<5), 
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
      ret = (subframe-2);
      break;

    case 4:
      AssertFatal((subframe>1) && (subframe<4),
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
      ret = (subframe-2);
      break;

    case 5:
      AssertFatal(subframe==2,
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframe,(int)cc->tdd_Config->subframeAssignment);
      ret = (subframe-2);
      break;

    default:
      AssertFatal(1==0,"subframe2_harq_pid, Unsupported TDD mode %d\n",(int)cc->tdd_Config->subframeAssignment);
    }
  }
  return ret;
}

uint8_t get_Msg3harqpid(COMMON_channels_t *cc,
374
375
			frame_t frame,
			sub_frame_t current_subframe)
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
{

  uint8_t ul_subframe=0;
  uint32_t ul_frame=0;

  if (cc->tdd_Config == NULL) { // FDD
    ul_subframe = (current_subframe>3) ? (current_subframe-4) : (current_subframe+6);
    ul_frame    = (current_subframe>3) ? ((frame+1)&1023) : frame;
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
    case 1:
      switch (current_subframe) {

      case 9:
      case 0:
        ul_subframe = 7;
        break;

      case 5:
      case 7:
        ul_subframe = 2;
        break;

      }

      break;

    case 3:
      switch (current_subframe) {

      case 0:
      case 5:
      case 6:
        ul_subframe = 2;
        break;

      case 7:
        ul_subframe = 3;
        break;

      case 8:
        ul_subframe = 4;
        break;

      case 9:
        ul_subframe = 2;
        break;
      }

      break;

    case 4:
      switch (current_subframe) {

      case 0:
      case 5:
      case 6:
      case 8:
      case 9:
        ul_subframe = 2;
        break;

      case 7:
        ul_subframe = 3;
        break;
      }

      break;

    case 5:
      ul_subframe =2;
      break;

    default:
      LOG_E(PHY,"get_Msg3_harq_pid: Unsupported TDD configuration %d\n",(int)cc->tdd_Config->subframeAssignment);
      AssertFatal(1==0,"get_Msg3_harq_pid: Unsupported TDD configuration");
      break;
    }
  }

  return(subframe2harqpid(cc,ul_frame,ul_subframe));

}

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
uint32_t pdcchalloc2ulframe(COMMON_channels_t *ccP,uint32_t frame, uint8_t n)
{
  uint32_t ul_frame;

  if ((ccP->tdd_Config) &&
      (ccP->tdd_Config->subframeAssignment == 1) &&
      ((n==1)||(n==6))) // tdd_config 0,1 SF 1,5
    ul_frame = (frame + (n==1 ? 0 : 1));
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
	   ((n==0)||(n==1)||(n==5)||(n==6)))
    ul_frame = (frame + (n>=5 ? 1 : 0));
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
           (n==9)) // tdd_config 6 SF 9
    ul_frame = (frame+1);
  else
    ul_frame = (frame+(n>=6 ? 1 : 0));

  LOG_D(PHY, "frame %d subframe %d: PUSCH frame = %d\n", frame, n, ul_frame);
  return ul_frame;

}

uint8_t pdcchalloc2ulsubframe(COMMON_channels_t *ccP,uint8_t n) {

  uint8_t ul_subframe;

  if ((ccP->tdd_Config) &&
      (ccP->tdd_Config->subframeAssignment == 1) &&
      ((n==1)||(n==6))) // tdd_config 0,1 SF 1,5
    ul_subframe = ((n+6)%10);
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
           ((n==0)||(n==1)||(n==5)||(n==6)))
    ul_subframe = ((n+7)%10);
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
           (n==9)) // tdd_config 6 SF 9
    ul_subframe = ((n+5)%10);
  else
    ul_subframe = ((n+4)%10);

  LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n", n, ul_subframe);
  return ul_subframe;
}

507
int is_UL_sf(COMMON_channels_t *ccP,sub_frame_t subframeP)
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
{

  // if FDD return dummy value
  if (ccP->tdd_Config == NULL)
    return(0);

  switch (ccP->tdd_Config->subframeAssignment) {

  case 1:
    switch (subframeP) {
    case 0:
    case 4:
    case 5:
    case 9:
      return(0);
      break;

    case 2:
    case 3:
    case 7:
    case 8:
      return(1);
      break;

    default:
      return(0);
      break;
    }
    break;

  case 3:
    if  ((subframeP<=1) || (subframeP>=5))
      return(0);
    else if ((subframeP>1) && (subframeP < 5))
      return(1);
    else AssertFatal(1==0,"Unknown subframe number\n");
    break;

  case 4:
    if  ((subframeP<=1) || (subframeP>=4))
      return(0);
    else if ((subframeP>1) && (subframeP < 4))
      return(1);
    else AssertFatal(1==0,"Unknown subframe number\n");
    break;
    
  case 5:
    if  ((subframeP<=1) || (subframeP>=3))
      return(0);
    else if ((subframeP>1) && (subframeP < 3))
      return(1);
    else AssertFatal(1==0,"Unknown subframe number\n");
    break;

  default:
    AssertFatal(1==0,"subframe %d Unsupported TDD configuration %d\n",
564
		subframeP,(int)ccP->tdd_Config->subframeAssignment);
565
566
567
568
569
    break;

  }
}

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
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
uint16_t get_pucch1_absSF(COMMON_channels_t *cc,uint16_t dlsch_absSF) {

  uint16_t sf,f,nextf;
  
  if (cc->tdd_Config==NULL) { //FDD n+4
    return((dlsch_absSF + 4)%10240);
  }
  else {
    sf    = dlsch_absSF%10;
    f     = dlsch_absSF/10;
    nextf = (f+1)&1023;

    switch (cc->tdd_Config->subframeAssignment) {
    case 0:
      AssertFatal(1==0,"SFA 0 to be filled in now, :-)\n");
      break;
    case 1:
      if      ((sf==5) || (sf==6)) return((10*nextf) + 2);                                        // ACK/NAK in SF 2 next frame
      else if (sf==9)              return((10*nextf) + 3);                                        // ACK/NAK in SF 3 next frame
      else if ((sf==0) || (sf==1)) return((10*f) + 2);                                            // ACK/NAK in SF 7 same frame
      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
      break;
    case 2:
      if      ((sf==4) || (sf==5) || (sf==6) || (sf==8)) return((10*nextf) + 2);                  // ACK/NAK in SF 2 next frame
      else if (sf==9)                                    return((10*nextf) + 7);                  // ACK/NAK in SF 7 next frame
      else if ((sf==0) || (sf==1) || (sf==3))            return((10*f) + 7);                      // ACK/NAK in SF 7 same frame
      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
      break;
    case 3:
      if      ((sf==5) || (sf==6) || (sf==7) || (sf==8) || (sf==9)) return((10*nextf) + (sf>>1)); // ACK/NAK in 2,3,4 resp. next frame 
      else if (sf==1)                                               return((10*nextf) + 2);       // ACK/NAK in 2 next frame 
      else if (sf==0)                                               return((10*f) + 4);           // ACK/NAK in 4 same frame 
      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 3\n",sf);
      break;
    case 4:
      if      ((sf==6) || (sf==7) || (sf==8) || (sf==9)) return(((10*nextf) + 3)%10240);          // ACK/NAK in SF 3 next frame
      else if ((sf==0) || (sf==1) || (sf==4) || (sf==5)) return(((10*nextf) + 2)%10240);          // ACK/NAK in SF 2 next frame
      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 4\n",sf);
      break;
    case 5:
      if      ((sf==0) || (sf==1) || (sf==3) || (sf==4) || (sf==5) || (sf==6) || (sf==7) || (sf==8)) return(((10*nextf) + 2)%10240);     // ACK/NAK in SF 3 next frame
      else if (sf==9)                                                                                return(((10*(1+nextf)) + 2)%10240); // ACK/NAK in SF 2 next frame           
      else AssertFatal(1==0,"Impossible dlsch subframe %d for TDD configuration 5\n",sf);      
      break;
    case 6:
      AssertFatal(1==0,"SFA 6 To be filled in now, :-)\n");
      break;
    default:
      AssertFatal(1==0,"Illegal TDD subframe Assigment %d\n",(int)cc->tdd_Config->subframeAssignment);
      break;
    }
  }
  AssertFatal(1==0,"Shouldn't get here\n");
}

void get_srs_pos(COMMON_channels_t *cc,uint16_t isrs,uint16_t *psrsPeriodicity,uint16_t *psrsOffset) {

  if(cc->tdd_Config) { // TDD
    AssertFatal(isrs>=10,"2 ms SRS periodicity not supported");
    
    if ((isrs>9)&&(isrs<15)) {
      *psrsPeriodicity=5;
      *psrsOffset=isrs-10;
    }
    if ((isrs>14)&&(isrs<25)) {
      *psrsPeriodicity=10;
      *psrsOffset=isrs-15;
    }
    if ((isrs>24)&&(isrs<45)) {
      *psrsPeriodicity=20;
      *psrsOffset=isrs-25;
    }
    if ((isrs>44)&&(isrs<85)) {
      *psrsPeriodicity=40;
      *psrsOffset=isrs-45;
    }
    if ((isrs>84)&&(isrs<165)) {
      *psrsPeriodicity=80;
      *psrsOffset=isrs-85;
    }
    if ((isrs>164)&&(isrs<325)) {
      *psrsPeriodicity=160;
      *psrsOffset=isrs-165;
    }
    if ((isrs>324)&&(isrs<645)) {
      *psrsPeriodicity=320;
      *psrsOffset=isrs-325;
    }
    
    AssertFatal(isrs<=644,"Isrs out of range %d>644\n",isrs);
      
  } // TDD
  else { // FDD

    if (isrs<2) {
      *psrsPeriodicity=2;
      *psrsOffset=isrs;
    }
    if ((isrs>1)&&(isrs<7)) {
      *psrsPeriodicity=5;
      *psrsOffset=isrs-2;
    }
    if ((isrs>6)&&(isrs<17)) {
      *psrsPeriodicity=10;
      *psrsOffset=isrs-7;
    }
    if ((isrs>16)&&(isrs<37)) {
      *psrsPeriodicity=20;
      *psrsOffset=isrs-17;
    }
    if ((isrs>36)&&(isrs<77)) {
      *psrsPeriodicity=40;
      *psrsOffset=isrs-37;
    }
    if ((isrs>76)&&(isrs<157)) {
      *psrsPeriodicity=80;
      *psrsOffset=isrs-77;
    }
    if ((isrs>156)&&(isrs<317)) {
      *psrsPeriodicity=160;
      *psrsOffset=isrs-157;
    }
    if ((isrs>316)&&(isrs<637)) {
      *psrsPeriodicity=320;
      *psrsOffset=isrs-317;
    }
    AssertFatal(isrs<=636,"Isrs out of range %d>636\n",isrs);
	
  }
}

void get_csi_params(COMMON_channels_t *cc,struct CQI_ReportPeriodic *cqi_ReportPeriodic,uint16_t *Npd,uint16_t *N_OFFSET_CQI,int *H) {

  uint16_t cqi_PMI_ConfigIndex = cqi_ReportPeriodic->choice.setup.cqi_pmi_ConfigIndex;
  uint8_t Jtab[6] = {0,2,2,3,4,4};

  AssertFatal(cqi_ReportPeriodic!=NULL,"cqi_ReportPeriodic is null!\n");

  if (cc->tdd_Config==NULL) { //FDD
    if (cqi_PMI_ConfigIndex <= 1) {        // 2 ms CQI_PMI period
      *Npd = 2;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex;
    } else if (cqi_PMI_ConfigIndex <= 6) { // 5 ms CQI_PMI period
      *Npd = 5;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-2;
    } else if (cqi_PMI_ConfigIndex <=16) { // 10ms CQI_PMI period
      *Npd = 10;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-7;
    } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
      *Npd = 20;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-17;
    } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
      *Npd = 40;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-37;
    } else if (cqi_PMI_ConfigIndex <= 156) { // 80 ms CQI_PMI period
      *Npd = 80;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-77;
    } else if (cqi_PMI_ConfigIndex <= 316) { // 160 ms CQI_PMI period
      *Npd = 160;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-157;
    }
    else if (cqi_PMI_ConfigIndex > 317) {
      
      if (cqi_PMI_ConfigIndex <= 349) { // 32 ms CQI_PMI period
	*Npd = 32;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex-318;
      }
      else if (cqi_PMI_ConfigIndex <= 413) { // 64 ms CQI_PMI period
	*Npd = 64;
	*N_OFFSET_CQI = cqi_PMI_ConfigIndex-350;
      }
      else if (cqi_PMI_ConfigIndex <= 541) { // 128 ms CQI_PMI period
	*Npd = 128;
	*N_OFFSET_CQI = cqi_PMI_ConfigIndex-414;
      }  
    }
  }
  else { // TDD
   if (cqi_PMI_ConfigIndex == 0) {        // all UL subframes
     *Npd = 1;
     *N_OFFSET_CQI = 0;
   } else if (cqi_PMI_ConfigIndex <= 6) { // 5 ms CQI_PMI period
     *Npd = 5;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-1;
   } else if (cqi_PMI_ConfigIndex <=16) { // 10ms CQI_PMI period
     *Npd = 10;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-6;
   } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
     *Npd = 20;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-16;
   } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
     *Npd = 40;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-36;
   } else if (cqi_PMI_ConfigIndex <= 156) { // 80 ms CQI_PMI period
     *Npd = 80;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-76;
   } else if (cqi_PMI_ConfigIndex <= 316) { // 160 ms CQI_PMI period
     *Npd = 160;
     *N_OFFSET_CQI = cqi_PMI_ConfigIndex-156;
   }
  }

  // get H
  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
    *H = 1+(Jtab[cc->mib->message.dl_Bandwidth]*cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.choice.subbandCQI.k);
  else
    *H=1;
}


uint8_t get_dl_cqi_pmi_size_pusch(UE_sched_ctrl *sched_ctl,COMMON_channels_t *cc,uint8_t tmode,uint8_t ri, CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic) {

  int Ntab[6]       = {0,4,7,9,10,13};
  int Ntab_uesel[6] = {0,8,13,17,19,25};
  int N             = Ntab[cc->p_eNB];
  int Ltab_uesel[6] = {0,6,9,13,15,18};
  int L             = Ltab_uesel[cc->p_eNB];

  AssertFatal(cqi_ReportModeAperiodic != NULL,"cqi_ReportPeriodic is null!\n");

  switch (*cqi_ReportModeAperiodic) {
  
  case CQI_ReportModeAperiodic_rm12:
    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm12\n",tmode);
    AssertFatal(cc->p_eNB<=4,"only up to 4 antenna ports supported here\n");
    if (ri==1 && cc->p_eNB==2) return(4+(N<<1));
    else if (ri==2 && cc->p_eNB==2) return(8+N);
    else if (ri==1 && cc->p_eNB==4) return(4+(N<<2));
    else if (ri>1  && cc->p_eNB==4) return(8+(N<<2));		 
    break;
  case CQI_ReportModeAperiodic_rm20:
    // Table 5.2.2.6.3-1 (36.212)
    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm20\n",tmode);
    AssertFatal(tmode!=9 && tmode!=10,"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm20\n");
    return(4+2+L);
    break;
  case CQI_ReportModeAperiodic_rm22:
    // Table 5.2.2.6.3-2 (36.212)
    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm22\n",tmode);
    AssertFatal(tmode!=9 && tmode!=10,"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm22\n");
    if (ri==1 && cc->p_eNB==2) return(4+2+0+0+L+4);
    if (ri==2 && cc->p_eNB==2) return(4+2+4+2+L+2);
    if (ri==1 && cc->p_eNB==4) return(4+2+0+0+L+8);
    if (ri>=2 && cc->p_eNB==4) return(4+2+4+2+L+8);
    break;
  case CQI_ReportModeAperiodic_rm30:
    // Table 5.2.2.6.2-1 (36.212)
    AssertFatal(tmode==1 || tmode==2 || tmode==3 || tmode==7 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm30\n",tmode);
    AssertFatal(tmode!=8 && tmode!=9 && tmode!=10,"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm30\n");
    return(4+(N<<1));
    break;
  case CQI_ReportModeAperiodic_rm31:
    // Table 5.2.2.6.2-2 (36.212)
    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm31\n",tmode);
    AssertFatal(tmode!=8 && tmode!=9 && tmode!=10,"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm31\n");
    if (ri==1 && cc->p_eNB==2) return(4+(N<<1)+0+0+2);
    else if (ri==2 && cc->p_eNB==2) return(4+(N<<1)+4+(N<<1)+1);
    else if (ri==1 && cc->p_eNB==4) return(4+(N<<1)+0+0+4);
    else if (ri>=2 && cc->p_eNB==4) return(4+(N<<1)+4+(N<<1)+4);
    break;
  case CQI_ReportModeAperiodic_rm32_v1250:
    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm32\n",tmode);
    AssertFatal(1==0,"CQI_ReportModeAperiodic_rm32_v1250 not supported yet\n");
    break;
  case CQI_ReportModeAperiodic_rm10_v1310:
    // Table 5.2.2.6.1-1F/G (36.212)
    if (ri==1) return(4); // F
    else return(7); // G
    break;
  case CQI_ReportModeAperiodic_rm11_v1310:
    // Table 5.2.2.6.1-1H (36.212)
    AssertFatal(tmode==4 || tmode==6 || tmode==8 || tmode==9 || tmode==10,"Illegal TM (%d) for CQI_ReportModeAperiodic_rm11\n",tmode);
    AssertFatal(cc->p_eNB<=4,"only up to 4 antenna ports supported here\n");
    if (ri==1 && cc->p_eNB==2) return(4+0+2);
    else if (ri==2 && cc->p_eNB==2) return(4+4+1);
    else if (ri==1 && cc->p_eNB==4) return(4+0+4);
    else if (ri>1  && cc->p_eNB==4) return(4+4+4);		 

    break;
  }

}

uint8_t get_rel8_dl_cqi_pmi_size(UE_sched_ctrl *sched_ctl,int CC_idP,COMMON_channels_t *cc,uint8_t tmode, struct CQI_ReportPeriodic *cqi_ReportPeriodic) {

  int no_pmi=0;
  //    Ltab[6] = {0,log2(15/4/2),log2(25/4/2),log2(50/6/3),log2(75/8/4),log2(100/8/4)};

  uint8_t Ltab[6] = {0,1,2,2,2,2};
  uint8_t ri = sched_ctl->periodic_ri_received[CC_idP];

  AssertFatal(cqi_ReportPeriodic != NULL,"cqi_ReportPeriodic is null!\n");
  AssertFatal(cqi_ReportPeriodic->present != CQI_ReportPeriodic_PR_NOTHING,
	      "cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
	      "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");

  switch(tmode) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:
    no_pmi=1;
    break;
  default:
    no_pmi=0;
  }



  if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI) ||
      (sched_ctl->feedback_cnt[CC_idP] == 0)) {
    // send wideband report every opportunity if wideband reporting mode is selected, else every H opportunities
    if (no_pmi == 1)
      return(4);
    else if ((cc->p_eNB==2) && (ri==1)) return(6);
    else if ((cc->p_eNB==2) && (ri==2)) return(8);
    else if ((cc->p_eNB==4) && (ri==1)) return(8);
    else if ((cc->p_eNB==4) && (ri==2)) return(11);
    else AssertFatal(1==0,"illegal combination p %d, ri %d, no_pmi %d\n",cc->p_eNB,ri,no_pmi);
  }
  else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
    if ((no_pmi == 1)||ri==1) return(4+Ltab[cc->mib->message.dl_Bandwidth]);
    else 
      return(7+Ltab[cc->mib->message.dl_Bandwidth]);    
  }
  AssertFatal(1==0,"Shouldn't get here : cqi_ReportPeriodic->present %d\n",cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present);
}

void fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t   *dl_config_pdu,
			  uint8_t aggregation_level,
			  uint16_t rnti,
			  uint8_t rnti_type,
			  uint8_t harq_process,
			  uint8_t tpc,
			  uint16_t resource_block_coding,
			  uint8_t mcs,
			  uint8_t ndi,
			  uint8_t rv,
			  uint8_t vrb_flag) {
  
  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
  dl_config_pdu->pdu_type                                                          = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE; 
  dl_config_pdu->pdu_size                                                          = (uint8_t)(2+sizeof(nfapi_dl_config_dci_dl_pdu));
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                             = NFAPI_DL_DCI_FORMAT_1A;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level                      = aggregation_level;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                                   = rnti;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                              = rnti_type;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power                     = 6000; // equal to RS power
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                           = harq_process;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                                    = tpc; // no TPC
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding                  = resource_block_coding;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                                  = mcs;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1                   = ndi;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1                   = rv;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = vrb_flag;
  
}

void program_dlsch_acknak(module_id_t module_idP, int CC_idP,int UE_idP, frame_t frameP, sub_frame_t subframeP,uint8_t cce_idx) {

  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
  COMMON_channels_t              *cc       = eNB->common_channels;
  UE_list_t                      *UE_list  = &eNB->UE_list;
  rnti_t                         rnti      = UE_RNTI(module_idP,UE_idP);
  nfapi_ul_config_request_body_t *ul_req;
  nfapi_ul_config_request_pdu_t  *ul_config_pdu;

  int use_simultaneous_pucch_pusch=0;
  nfapi_ul_config_ulsch_harq_information *ulsch_harq_information = NULL;
  nfapi_ul_config_harq_information *harq_information = NULL;
  
#if defined(Rel10) || defined(Rel14)
  
  if ((UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2) &&
      (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020) &&
      (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10) &&	    
      (*UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10 == PUCCH_ConfigDedicated_v1020__simultaneousPUCCH_PUSCH_r10_true)) use_simultaneous_pucch_pusch=1;
#endif
  
  // pucch1 and pusch feedback is similar, namely in n+k subframes from now
  // This is used in the following "if/else" condition to check if there isn't or is already an UL grant in n+k
  int16_t ul_absSF = get_pucch1_absSF(&cc[CC_idP],subframeP+(10*frameP));
  
  if ((ul_config_pdu = has_ul_grant(module_idP,CC_idP,
				    ul_absSF,
				    rnti)) == NULL) {
    // no UL grant so
    // Program ACK/NAK alone Format 1a/b or 3
    
    ul_req        = &RC.mac[module_idP]->UL_req_tmp[CC_idP][ul_absSF%10].ul_config_request_body;
    ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];	    
    // Do PUCCH
    fill_nfapi_uci_acknak(module_idP,
			  CC_idP,
			  rnti,
			  (frameP*10)+subframeP,
			  cce_idx);
  }
  else { // there is already an existing UL grant so update it if needed
    // on top of some other UL resource (PUSCH,combined SR/CQI/HARQ on PUCCH, etc)
    switch(ul_config_pdu->pdu_type) {
    case NFAPI_UL_CONFIG_ULSCH_PDU_TYPE:
    case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
    case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
      if (use_simultaneous_pucch_pusch==1) {
	AssertFatal(ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE,
		    "Cannot be NFAPI_UL_CONFIG_ULSCH_PDU_TYPE, simultaneous_pucch_pusch is active\n");
	// Convert it to an NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE 
	harq_information = &ul_config_pdu->ulsch_uci_harq_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE;
      }
      else {
	AssertFatal(ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE,
		    "Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
	// Convert it to an NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE 
	ulsch_harq_information = &ul_config_pdu->ulsch_harq_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE;
	ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial=0; // last symbol not punctured
	ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks=
	  ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks; // we don't change the number of resource blocks across retransmissions yet
      }
      break;
    case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
    case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
    case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
    case NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE:
      if (use_simultaneous_pucch_pusch==1) {
	AssertFatal(ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE||
		    ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE,
		    "Cannot be NFAPI_UL_CONFIG_ULSCH_CQI_RI_xxx_PDU_TYPE, simultaneous_pucch_pusch is active\n");
	// convert it to an NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE
	harq_information = &ul_config_pdu->ulsch_csi_uci_harq_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE;
	
      }
      else { 
	AssertFatal(ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE ||
		    ul_config_pdu->pdu_type==NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE,
		    "Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_xxx_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
	// Convert it to an NFAPI_UL_CONFIG_ULSCH_CQI_RI_HARQ_PDU_TYPE 
	ulsch_harq_information = &ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE;
	ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial=0; // last symbol not punctured
	ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks=
	  ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks; // we don't change the number of resource blocks across retransmissions yet		
      }
      
      break;
      
    case NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE:
    case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
      // convert/keep it to NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
      harq_information = &ul_config_pdu->uci_sr_harq_pdu.harq_information;
      break;
    case NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE:
    case NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE:
      // convert/keep it to NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE
      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE;
      harq_information = &ul_config_pdu->uci_cqi_harq_pdu.harq_information;
      break;
      
    case NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE:
    case NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE:
      // convert/keep it to NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE
      ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE;
      harq_information = &ul_config_pdu->uci_cqi_sr_harq_pdu.harq_information;
      break;
    }
  }
  
  if (ulsch_harq_information) fill_nfapi_ulsch_harq_information(module_idP,CC_idP,
								rnti,
								ulsch_harq_information);
  
  
  if (harq_information) fill_nfapi_harq_information(module_idP,CC_idP,
						    rnti,
						    (frameP*10)+subframeP,
						    harq_information,
						    cce_idx);
}

uint8_t get_V_UL_DAI(module_id_t module_idP,int CC_idP,uint16_t rntiP) {

  nfapi_hi_dci0_request_body_t        *HI_DCI0_req         = &RC.mac[module_idP]->HI_DCI0_req[CC_idP].hi_dci0_request_body;
  nfapi_hi_dci0_request_pdu_t         *hi_dci0_pdu         = &HI_DCI0_req->hi_dci0_pdu_list[0];

  for (int i=0;i<HI_DCI0_req->number_of_dci;i++) {
    if ((hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE)&&
	(hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP))
	return(hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.dl_assignment_index);
  }
  return(4); // this is rule from Section 7.3 in 36.213
}
void fill_nfapi_ulsch_harq_information(module_id_t module_idP,
				       int CC_idP,
				       uint16_t rntiP,
				       nfapi_ul_config_ulsch_harq_information *harq_information) {


  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
  COMMON_channels_t              *cc       = &eNB->common_channels[CC_idP];
  UE_list_t                      *UE_list  = &eNB->UE_list;

  int UE_id                                = find_UE_id(module_idP,rntiP);

  PUSCH_ConfigDedicated_t              *puschConfigDedicated;
  //  PUSCH_ConfigDedicated_v1020_t        *puschConfigDedicated_v1020;
  //  PUSCH_ConfigDedicated_v1130_t        *puschConfigDedicated_v1130;
  //  PUSCH_ConfigDedicated_v1250_t        *puschConfigDedicated_v1250;

  AssertFatal(UE_id>=0,"UE_id cannot be found, impossible\n");
  AssertFatal(UE_list!=NULL,"UE_list is null\n");
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated!=NULL,"physicalConfigDedicated for rnti %x is null\n",rntiP);
  AssertFatal((puschConfigDedicated = (PUSCH_ConfigDedicated_t *)UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pusch_ConfigDedicated)!=NULL,"physicalConfigDedicated->puschConfigDedicated for rnti %x is null\n",rntiP);
#if defined(Rel14) || defined(Rel14)
  /*  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2) puschConfigDedicated_v1020 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2->pusch_ConfigDedicated_v1020;
#endif
#ifdef Rel14
  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4) puschConfigDedicated_v1130 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4->pusch_ConfigDedicated_v1130;
  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5) puschConfigDedicated_v1250 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5->pusch_ConfigDedicated_v1250;
  */
#endif
  harq_information->harq_information_rel10.delta_offset_harq = puschConfigDedicated->betaOffset_ACK_Index;
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,"pucch_ConfigDedicated is null!\n");
  if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
      (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
      harq_information->harq_information_rel10.ack_nack_mode = 1;  // multiplexing
  else
      harq_information->harq_information_rel10.ack_nack_mode = 0;  // bundling

  switch(get_tmode(module_idP,CC_idP,UE_id)) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:

    if (cc->tdd_Config==NULL) // FDD
      harq_information->harq_information_rel10.harq_size = 1;
    else {
      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
	harq_information->harq_information_rel10.harq_size = get_V_UL_DAI(module_idP,CC_idP,rntiP);
      else
	harq_information->harq_information_rel10.harq_size = 1;
    }
    break;
  default: // for any other TM we need 2 bits harq 
    
    if (cc->tdd_Config==NULL) {
	harq_information->harq_information_rel10.harq_size     = 2;  
    }
    else {
      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
	harq_information->harq_information_rel10.harq_size     = get_V_UL_DAI(module_idP,CC_idP,rntiP);  
      else
	harq_information->harq_information_rel10.harq_size     = 2;  
    }
    break;
  } // get Tmode
}
  
void fill_nfapi_harq_information(module_id_t module_idP,
				 int CC_idP,
				 uint16_t rntiP,
				 uint16_t absSFP,
				 nfapi_ul_config_harq_information *harq_information,
				 uint8_t cce_idxP) {
  
  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
  COMMON_channels_t              *cc       = &eNB->common_channels[CC_idP];
  UE_list_t                      *UE_list  = &eNB->UE_list;

  int UE_id                                = find_UE_id(module_idP,rntiP);


  AssertFatal(UE_id>=0,"UE_id cannot be found, impossible\n");
  AssertFatal(UE_list!=NULL,"UE_list is null\n");
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated!=NULL,"physicalConfigDedicated for rnti %x is null\n",rntiP);

  harq_information->harq_information_rel11.num_ant_ports = 1;

  switch(get_tmode(module_idP,CC_idP,UE_id)) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:
    if (cc->tdd_Config!=NULL) {
      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,
		  "pucch_ConfigDedicated is null for TDD!\n");
      if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
	  (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)) {
	harq_information->harq_information_rel10_tdd.harq_size     = 2;  // 2-bit ACK/NAK
	harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;  // multiplexing
      }
      else {
	harq_information->harq_information_rel10_tdd.harq_size     = 1;  // 1-bit ACK/NAK
	harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;  // bundling
      }
      harq_information->harq_information_rel10_tdd.n_pucch_1_0   = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
      harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
    } else {
      harq_information->harq_information_rel9_fdd.number_of_pucch_resources = 1;
      harq_information->harq_information_rel9_fdd.harq_size                 = 1;  // 1-bit ACK/NAK
      harq_information->harq_information_rel9_fdd.n_pucch_1_0               = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
    }
    break;
  default: // for any other TM we need 2 bits harq 
    if (cc->tdd_Config!=NULL) {
      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated!=NULL,
		  "pucch_ConfigDedicated is null for TDD!\n");
      if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode!=NULL)&&
	  (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode==PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)) {
	harq_information->harq_information_rel10_tdd.ack_nack_mode = 1;  // multiplexing
      }
      else {
	harq_information->harq_information_rel10_tdd.ack_nack_mode = 0;  // bundling
      }
      harq_information->harq_information_rel10_tdd.harq_size       = 2;  
      harq_information->harq_information_rel10_tdd.n_pucch_1_0   = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
      harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
    }
    else {
      harq_information->harq_information_rel9_fdd.number_of_pucch_resources = 1;
      harq_information->harq_information_rel9_fdd.ack_nack_mode    = 0;  // 1a/b
      harq_information->harq_information_rel9_fdd.harq_size        = 2;
      harq_information->harq_information_rel9_fdd.n_pucch_1_0      = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;  
    }
    break;
  } // get Tmode
}
 

uint16_t fill_nfapi_uci_acknak(module_id_t module_idP,
			       int CC_idP,
			       uint16_t rntiP,
			       uint16_t absSFP,
			       uint8_t cce_idxP) {

  eNB_MAC_INST                   *eNB          = RC.mac[module_idP];
  COMMON_channels_t              *cc           = &eNB->common_channels[CC_idP];
  
  int ackNAK_absSF                             = get_pucch1_absSF(cc,absSFP);
  nfapi_ul_config_request_body_t *ul_req       = &eNB->UL_req_tmp[CC_idP][ackNAK_absSF%10].ul_config_request_body;
  nfapi_ul_config_request_pdu_t *ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus]; 


  memset((void*)ul_config_pdu,0,sizeof(nfapi_ul_config_request_pdu_t));
  ul_config_pdu->pdu_type                                                              = NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE; 
  ul_config_pdu->pdu_size                                                              = (uint8_t)(2+sizeof(nfapi_ul_config_uci_harq_pdu));
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle                = 0; // don't know how to use this
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti                  = rntiP;

  fill_nfapi_harq_information(module_idP,CC_idP,
			      rntiP,
			      absSFP,
			      &ul_config_pdu->uci_harq_pdu.harq_information,
			      cce_idxP);
knopp's avatar
knopp committed
1232
  LOG_D(MAC,"Filled in HARQ for rnti %x, cce_idxP %d-> n1_pucch %d\n",rntiP,cce_idxP,ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel9_fdd.n_pucch_1_0);
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298

  ul_req->number_of_pdus++;

  return(((ackNAK_absSF/10)<<4) + (ackNAK_absSF%10));
}

void fill_nfapi_dlsch_config(eNB_MAC_INST *eNB, 
			     nfapi_dl_config_request_body_t *dl_req,
			     uint16_t length,
			     uint16_t pdu_index,
			     uint16_t rnti,
			     uint8_t resource_allocation_type,
			     uint8_t virtual_resource_block_assignment_flag,
			     uint16_t resource_block_coding,
			     uint8_t modulation,
			     uint8_t redundancy_version,
			     uint8_t transport_blocks,
			     uint8_t transport_block_to_codeword_swap_flag,
			     uint8_t transmission_scheme,
			     uint8_t number_of_layers,
			     uint8_t number_of_subbands,
			     //			     uint8_t codebook_index,
			     uint8_t ue_category_capacity,
			     uint8_t pa,
			     uint8_t delta_power_offset_index,
			     uint8_t ngap,
			     uint8_t nprb,
			     uint8_t transmission_mode,
			     uint8_t num_bf_prb_per_subband,
			     uint8_t num_bf_vector
			     ) {

  nfapi_dl_config_request_pdu_t   *dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 

  dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
  memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
  dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE; 
  dl_config_pdu->pdu_size                                                        = (uint8_t)(2+sizeof(nfapi_dl_config_dlsch_pdu));
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length                                 = length;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = pdu_index;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = rnti;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = resource_allocation_type;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = virtual_resource_block_assignment_flag;   
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = resource_block_coding;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = modulation; 
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = redundancy_version;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = transport_blocks;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = transport_block_to_codeword_swap_flag;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = transmission_scheme;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = number_of_layers;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = number_of_subbands;
  //  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = codebook_index;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = ue_category_capacity;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = pa;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = delta_power_offset_index;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = ngap;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = nprb;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = transmission_mode;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = num_bf_prb_per_subband;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = num_bf_vector;
  dl_req->number_pdu++;
}

uint16_t fill_nfapi_tx_req(nfapi_tx_request_body_t *tx_req_body,uint16_t absSF,uint16_t pdu_length, uint16_t *pdu_index, uint8_t *pdu) {

  nfapi_tx_request_pdu_t *TX_req        = &tx_req_body->tx_pdu_list[tx_req_body->number_of_pdus]; 
knopp's avatar
knopp committed
1299
  LOG_D(MAC,"Filling TX_req %d for pdu length %d\n",tx_req_body->number_of_pdus,pdu_length);
1300
1301
1302
1303
1304
1305
1306
1307
1308
  TX_req->pdu_length                    = pdu_length;
  TX_req->pdu_index                     = *pdu_index++;
  TX_req->num_segments                  = 1;
  TX_req->segments[0].segment_length    = pdu_length;
  TX_req->segments[0].segment_data      = pdu;
  tx_req_body->number_of_pdus++;

  return(((absSF/10)<<4) + (absSF%10));
}
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
#ifdef Rel14

int get_numnarrowbands(long dl_Bandwidth) {
  int nb_tab[6] = {1,2,4,8,12,16};

  AssertFatal(dl_Bandwidth<7 || dl_Bandwidth>=0,"dl_Bandwidth not in [0..6]\n");
  return(nb_tab[dl_Bandwidth]);
}

int get_numnarrowbandbits(long dl_Bandwidth) {
  int nbbits_tab[6] = {0,1,2,3,4,4};

  AssertFatal(dl_Bandwidth<7 || dl_Bandwidth>=0,"dl_Bandwidth not in [0..6]\n");
  return(nbbits_tab[dl_Bandwidth]);
}

//This implements the frame/subframe condition for first subframe of MPDCCH transmission (Section 9.1.5 36.213, Rel 13/14)
int startSF_fdd_RA_times2[8] = {2,3,4,5,8,10,16,20};
int startSF_tdd_RA[7]        = {1,2,4,5,8,10,20};

1329
int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t subframeP,int rmax,MPDCCH_TYPES_t mpdcch_type,int UE_id) {
1330
1331

  struct PRACH_ConfigSIB_v1310 *ext4_prach = eNB->common_channels[CC_id].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
1332
1333


1334
  int T;
1335
  EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349

  switch (mpdcch_type) {
  case TYPE0:
    AssertFatal(1==0,"MPDCCH Type 0 not handled yet\n");
    break;
  case TYPE1:
    AssertFatal(1==0,"MPDCCH Type 1 not handled yet\n");
    break;
  case TYPE1A:
    AssertFatal(1==0,"MPDCCH Type 1A not handled yet\n");
    break;
  case TYPE2: // RAR
    AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13!=NULL,
		"mpdcch_startSF_CSS_RA_r13 is null\n");
1350
    AssertFatal(rmax>0,"rmax is 0!\b");
1351
1352
1353
1354
1355
1356
1357
1358
1359
    if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
      T = rmax*startSF_fdd_RA_times2[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.fdd_r13]>>1;
    else //TDD
      T = rmax*startSF_tdd_RA[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.tdd_r13];
    break;
  case TYPE2A:
    AssertFatal(1==0,"MPDCCH Type 2A not handled yet\n");
    break;
  case TYPEUESPEC:
1360
    epdcch_setconfig_r11= eNB->UE_list.UE_template[CC_id][UE_id].physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.choice.setup.setConfigToAddModList_r11->list.array[0] ;
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
    
    
    AssertFatal(epdcch_setconfig_r11 != NULL," epdcch_setconfig_r11 is null for UE specific \n");
    AssertFatal(epdcch_setconfig_r11->ext2 != NULL," ext2 doesn't exist in epdcch config ' \n");
    
    if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
      T = rmax*startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13]>>1;
    else //TDD
      T = rmax*startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
    
1371
1372
1373
1374
1375
    break;
  default:
    return(0);
  }

1376
  AssertFatal(T>0,"T is 0!\n");
1377
1378
1379
1380
1381
  if (((10*frameP) + subframeP)%T == 0) return(1);
  else return(0);

}

1382
int narrowband_to_first_rb(COMMON_channels_t *cc, int nb_index) {
1383

1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
  switch (cc->mib->message.dl_Bandwidth) {
  case 0: // 6 PRBs, N_NB=1, i_0=0
    return(0);
    break;
  case 3: // 50 PRBs, N_NB=8, i_0=1
    return((int)(1+(6*nb_index)));
    break;
  case 5: // 100 PRBs, N_NB=16, i_0=2
    return((int)(2+(6*nb_index)));
    break;
  case 1: // 15 PRBs  N_NB=2, i_0=1
    if (nb_index>0) return(1);
    else            return(0);
    break;
  case 2: // 25 PRBs, N_NB=4, i_0=0
    if (nb_index>1) return(1+(6*nb_index));
    else            return((6*nb_index));
    break;
  case 4: // 75 PRBs, N_NB=12, i_0=1
    if (nb_index>5) return(2+(6*nb_index));
    else            return(1+(6*nb_index));
    break;
  default:
1407
    AssertFatal(1==0,"Impossible dl_Bandwidth %d\n",(int)cc->mib->message.dl_Bandwidth);
1408
1409
1410
    break;
  }
}
1411
1412
#endif

1413
//------------------------------------------------------------------------------
1414
void init_ue_sched_info(void)
1415
//------------------------------------------------------------------------------
1416
{
1417
  module_id_t i,j,k;
1418
1419

  for (i=0; i<NUMBER_OF_eNB_MAX; i++) {
Cedric Roux's avatar
Cedric Roux committed
1420
    for (k=0; k<MAX_NUM_CCs; k++) {
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
      for (j=0; j<NUMBER_OF_UE_MAX; j++) {
        // init DL
        eNB_dlsch_info[i][k][j].weight           = 0;
        eNB_dlsch_info[i][k][j].subframe         = 0;
        eNB_dlsch_info[i][k][j].serving_num      = 0;
        eNB_dlsch_info[i][k][j].status           = S_DL_NONE;
        // init UL
        eNB_ulsch_info[i][k][j].subframe         = 0;
        eNB_ulsch_info[i][k][j].serving_num      = 0;
        eNB_ulsch_info[i][k][j].status           = S_UL_NONE;
1431
      }
1432
    }
1433
1434
1435
1436
1437
  }
}



1438
//------------------------------------------------------------------------------
1439
unsigned char get_ue_weight(module_id_t module_idP, int CC_idP, int ue_idP)
1440
//------------------------------------------------------------------------------
1441
{
1442

1443
  return(eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
1444
1445
1446

}

1447
//------------------------------------------------------------------------------
1448
int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
1449
//------------------------------------------------------------------------------
1450
{
knopp's avatar
   
knopp committed
1451
  int UE_id;
1452
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
1453

1454
1455
  for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
    if (UE_list->active[UE_id] != TRUE) continue;
knopp's avatar
   
knopp committed
1456
1457
1458
    if (UE_list->UE_template[UE_PCCID(mod_idP,UE_id)][UE_id].rnti==rntiP) {
      return(UE_id);
    }
1459
  }
1460

knopp's avatar
   
knopp committed
1461
  return(-1);
1462
1463
}

1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
//------------------------------------------------------------------------------
int find_RA_id(module_id_t mod_idP, int CC_idP, rnti_t rntiP)
//------------------------------------------------------------------------------
{
  int RA_id;
  AssertFatal(RC.mac[mod_idP],"RC.mac[%d] is null\n",mod_idP);
  
  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&RC.mac[mod_idP]->common_channels[CC_idP].RA_template[0];

 

1475
  for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++) {
knopp's avatar
knopp committed
1476
    LOG_D(MAC,"Checking RA_id %d for %x : RA_active %d, wait_ack_Msg4 %d\n",
1477
1478
1479
1480
	  RA_id,rntiP,
	  RA_template[RA_id].RA_active,
	  RA_template[RA_id].wait_ack_Msg4);
	  
1481
1482
1483
    if (RA_template[RA_id].RA_active==TRUE && 
        RA_template[RA_id].wait_ack_Msg4 == 0 &&
	RA_template[RA_id].rnti == rntiP) return(RA_id);
1484
  }
1485
1486
1487
  return(-1);
}

1488
//------------------------------------------------------------------------------
1489
int UE_num_active_CC(UE_list_t *listP,int ue_idP)
1490
//------------------------------------------------------------------------------
1491
{
knopp's avatar
   
knopp committed
1492
1493
  return(listP->numactiveCCs[ue_idP]);
}
1494

1495
//------------------------------------------------------------------------------
1496
int UE_PCCID(module_id_t mod_idP,int ue_idP)
1497
//------------------------------------------------------------------------------
1498
{
1499
  return(RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
knopp's avatar
   
knopp committed
1500
}
1501

1502
//------------------------------------------------------------------------------
1503
rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
1504
//------------------------------------------------------------------------------
1505
{
1506

1507
  rnti_t rnti = RC.mac[mod_idP]->UE_list.UE_template[UE_PCCID(mod_idP,ue_idP)][ue_idP].rnti;
1508

1509
  if (rnti>0) {
knopp's avatar
   
knopp committed
1510
    return (rnti);
1511
  }
1512

1513
  LOG_D(MAC,"[eNB %d] Couldn't find RNTI for UE %d\n",mod_idP,ue_idP);
1514
1515
  //display_backtrace();
  return(NOT_A_RNTI);
1516
}
knopp's avatar
   
knopp committed
1517

1518
//------------------------------------------------------------------------------
1519
boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
1520
//------------------------------------------------------------------------------
1521
{
1522
  return(RC.mac[mod_idP]->UE_list.active[ue_idP]);
1523
}
knopp's avatar
   
knopp committed
1524
1525
1526

/*
uint8_t find_active_UEs(module_id_t module_idP,int CC_id){
1527
1528
1529
1530
1531
1532
1533

  module_id_t        ue_mod_id      = 0;
  rnti_t        rnti         = 0;
  uint8_t            nb_active_ue = 0;

  for (ue_mod_id=0;ue_mod_id<NUMBER_OF_UE_MAX;ue_mod_id++) {

knopp's avatar
   
knopp committed
1534
      if (((rnti=eNB_mac_inst[module_idP][CC_id].UE_template[ue_mod_id].rnti) !=0)&&(eNB_mac_inst[module_idP][CC_id].UE_template[ue_mod_id].ul_active==TRUE)){
1535
1536

          if (mac_xface->get_eNB_UE_stats(module_idP,rnti) != NULL){ // check at the phy enb_ue state for this rnti
1537
      nb_active_ue++;
1538
1539
          }
          else { // this ue is removed at the phy => remove it at the mac as well
1540
      mac_remove_ue(module_idP, CC_id, ue_mod_id);
1541
1542
1543
1544
1545
          }
      }
  }
  return(nb_active_ue);
}
knopp's avatar
   
knopp committed
1546
*/
1547
1548


1549
1550
// get aggregation (L) form phy for a give UE
unsigned char get_aggregation (uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt)
1551
{
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
  unsigned char aggregation=3;
  
  switch (dci_fmt){
    
  case format0:
    aggregation = cqi2fmt0_agg[bw_index][cqi];
    break;
  case format1:
  case format1A:
  case format1B:
  case format1D:
    aggregation = cqi2fmt1x_agg[bw_index][cqi]; 
    break;
  case format2:
  case format2A:
  case format2B:
  case format2C:
  case format2D:
    aggregation = cqi2fmt2x_agg[bw_index][cqi]; 
    break;
  case format1C:
  case format1E_2A_M10PRB:
  case format3:
  case format3A:
  case format4:
  default:
    LOG_W(MAC,"unsupported DCI format %d\n",dci_fmt);
  }

   LOG_D(MAC,"Aggregation level %d (cqi %d, bw_index %d, format %d)\n", 
   	1<<aggregation, cqi,bw_index,dci_fmt);
   
1584
  return 1<<aggregation;
1585
}
knopp's avatar
   
knopp committed
1586

1587
1588
void dump_ue_list(UE_list_t *listP, int ul_flag)
{
knopp's avatar
   
knopp committed
1589
  int j;
1590
1591
1592

  if ( ul_flag == 0 ) {
    for (j=listP->head; j>=0; j=listP->next[j]) {
1593
1594
1595
      LOG_T(MAC,"node %d => %d\n",j,listP->next[j]);
    }
  } else {
1596
    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
1597
1598
      LOG_T(MAC,"node %d => %d\n",j,listP->next_ul[j]);
    }
knopp's avatar
   
knopp committed
1599
1600
1601
  }
}

1602
1603
1604
1605
1606
int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP,int harq_pidP
               #ifdef Rel14
                 ,uint8_t rach_resource_type
               #endif
               )
1607
{
knopp's avatar
   
knopp committed
1608
  int UE_id;
1609
  int i, j;
1610

1611
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
1612

knopp's avatar
   
knopp committed
1613
  LOG_D(MAC,"[eNB %d, CC_id %d] Adding UE with rnti %x (next avail %d, num_UEs %d)\n",mod_idP,cc_idP,rntiP,UE_list->avail,UE_list->num_UEs);
1614
  dump_ue_list(UE_list,0);
knopp's avatar
   
knopp committed
1615

1616
1617
1618
  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
    if (UE_list->active[i] == TRUE) continue;
    UE_id = i;
knopp's avatar
   
knopp committed
1619
1620
1621
1622
1623
1624
1625
    UE_list->UE_template[cc_idP][UE_id].rnti       = rntiP;
    UE_list->UE_template[cc_idP][UE_id].configured = FALSE;
    UE_list->numactiveCCs[UE_id]                   = 1;
    UE_list->numactiveULCCs[UE_id]                 = 1;
    UE_list->pCC_id[UE_id]                         = cc_idP;
    UE_list->ordered_CCids[0][UE_id]               = cc_idP;
    UE_list->ordered_ULCCids[0][UE_id]             = cc_idP;
knopp's avatar
   
knopp committed
1626
    UE_list->num_UEs++;
knopp's avatar
   
knopp committed
1627
    UE_list->active[UE_id]                         = TRUE;
1628
1629
1630
1631
1632

#ifdef Rel14
    UE_list->UE_template[cc_idP][UE_id].rach_resource_type = rach_resource_type ;
#endif

1633
    memset((void*)&UE_list->UE_sched_ctrl[UE_id],0,sizeof(UE_sched_ctrl));
1634
    memset((void*)&UE_list->eNB_UE_stats[cc_idP][UE_id],0,sizeof(eNB_UE_STATS));
knopp's avatar
   
knopp committed
1635

1636
1637
    for (j=0; j<8; j++) {
      UE_list->UE_template[cc_idP][UE_id].oldNDI[j]    = (j==0)?1:0;   // 1 because first transmission is with format1A (Msg4) for harq_pid 0
knopp's avatar
   
knopp committed
1638
      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j==harq_pidP)?0:1; // 1st transmission is with Msg3;
1639
      UE_list->UE_sched_ctrl[UE_id].round[cc_idP][j]   = 8; 
knopp's avatar
knopp committed
1640
      UE_list->UE_sched_ctrl[UE_id].round_UL[cc_idP][j]   = 0; 
knopp's avatar
   
knopp committed
1641
    }
1642

1643
    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
1644
    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
knopp's avatar
   
knopp committed
1645
    LOG_D(MAC,"[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",mod_idP,UE_id,cc_idP,rntiP);
1646
    dump_ue_list(UE_list,0);
knopp's avatar
   
knopp committed
1647
    return(UE_id);
1648
  }
knopp's avatar
   
knopp committed
1649

1650
  printf("MAC: cannot add new UE for rnti %x\n", rntiP);
knopp's avatar
   
knopp committed
1651
  LOG_E(MAC,"error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
1652
  dump_ue_list(UE_list,0);
1653
1654
1655
  return(-1);
}

1656
//------------------------------------------------------------------------------
1657
int rrc_mac_remove_ue(module_id_t mod_idP,rnti_t rntiP) 
1658
//------------------------------------------------------------------------------
1659
{
1660
  int i;
1661
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
1662
  int UE_id = find_UE_id(mod_idP,rntiP);
1663
  int pCC_id;
1664

1665
1666
1667
1668
1669
  if (UE_id == -1) {
    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n", rntiP);
    return 0;
  }

1670
1671
  pCC_id = UE_PCCID(mod_idP,UE_id);

knopp's avatar
knopp committed
1672
  LOG_D(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
1673
  dump_ue_list(UE_list,0);
1674

1675
1676
1677
  UE_list->active[UE_id] = FALSE;
  UE_list->num_UEs--;

1678
  // clear all remaining pending transmissions
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID0]  = 0;
  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID1]  = 0;
  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID2]  = 0;
  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID3]  = 0;

  UE_list->UE_template[pCC_id][UE_id].ul_SR             = 0;
  UE_list->UE_template[pCC_id][UE_id].rnti              = NOT_A_RNTI;
  UE_list->UE_template[pCC_id][UE_id].ul_active         = FALSE;
  eNB_ulsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
  eNB_ulsch_info[mod_idP][pCC_id][UE_id].status                      = S_UL_NONE;
  eNB_dlsch_info[mod_idP][pCC_id][UE_id].rnti                        = NOT_A_RNTI;
  eNB_dlsch_info[mod_idP][pCC_id][UE_id].status                      = S_DL_NONE;
knopp's avatar
   
knopp committed
1691

1692

1693
1694
1695
  // check if this has an RA process active
  RA_TEMPLATE *RA_template;
  for (i=0;i<NB_RA_PROC_MAX;i++) {
1696
    RA_template = (RA_TEMPLATE *)&RC.mac[mod_idP]->common_channels[pCC_id].RA_template[i];
1697
    if (RA_template->rnti == rntiP){
1698
1699
1700
1701
1702
1703
1704
      RA_template->RA_active=FALSE;
      RA_template->generate_rar=0;
      RA_template->generate_Msg4=0;
      RA_template->wait_ack_Msg4=0;
      RA_template->timing_offset=0;
      RA_template->RRC_timer=20;
      RA_template->rnti = 0;
1705
      //break;
1706
1707
    }
  }
knopp's avatar
   
knopp committed
1708

1709
  return 0;
1710
1711
1712
}


knopp's avatar
   
knopp committed
1713

1714
1715
int prev(UE_list_t *listP, int nodeP, int ul_flag)
{
knopp's avatar
   
knopp committed
1716
  int j;
1717

1718
  if (ul_flag == 0 ) {
1719
    if (nodeP==listP->head) {
1720
      return(nodeP);
1721
    }
1722
1723

    for (j=listP->head; j>=0; j=listP->next[j]) {
1724
      if (listP->next[j]==nodeP) {
1725
        return(j);
1726
    }
1727
    }
1728
  } else {
1729
    if (nodeP==listP->head_ul) {
nikaeinn's avatar