eNB_scheduler_primitives.c 56.9 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

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

knopp's avatar
knopp committed
65
66
67
68
69
70
71
72
73
74
  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]);
}
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


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);
}

100
void get_Msg3alloc(COMMON_channels_t *cc,
101
102
103
104
		   sub_frame_t current_subframe,
		   frame_t current_frame,
		   frame_t *frame,
		   sub_frame_t *subframe)
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
{

  // 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;
          }
        }
  }
}

211
212


213
void get_Msg3allocret(COMMON_channels_t *cc,
214
215
216
217
		      sub_frame_t current_subframe,
		      frame_t current_frame,
		      frame_t *frame,
		      sub_frame_t *subframe)
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
{
  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;
    }
  }
}

252
uint8_t subframe2harqpid(COMMON_channels_t *cc,frame_t frame,sub_frame_t subframe)
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
{
  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,
320
321
			frame_t frame,
			sub_frame_t current_subframe)
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
{

  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));

}

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

  // 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",
		subframeP,ccP->tdd_Config->subframeAssignment);
    break;

  }
}

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#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};

489
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) {
490
491

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


494
  int T;
495
  EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
496
497
498
499
500
501
502
503
504
505
506
507
508
509

  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");
510
    AssertFatal(rmax>0,"rmax is 0!\b");
511
512
513
514
515
516
517
518
519
    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:
520
521
522
523
524
525
526
527
528
529
530
    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] ;
    
    
    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];
    
531
532
533
534
535
    break;
  default:
    return(0);
  }

536
  AssertFatal(T>0,"T is 0!\n");
537
538
539
540
541
  if (((10*frameP) + subframeP)%T == 0) return(1);
  else return(0);

}

542
int narrowband_to_first_rb(COMMON_channels_t *cc, int nb_index) {
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
  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:
    AssertFatal(1==0,"Impossible dl_Bandwidth %d\n",cc->mib->message.dl_Bandwidth);
    break;
  }
}
571
572
#endif

573
//------------------------------------------------------------------------------
574
void init_ue_sched_info(void)
575
//------------------------------------------------------------------------------
576
{
577
  module_id_t i,j,k;
578
579

  for (i=0; i<NUMBER_OF_eNB_MAX; i++) {
Cedric Roux's avatar
Cedric Roux committed
580
    for (k=0; k<MAX_NUM_CCs; k++) {
581
582
583
584
585
586
587
588
589
590
      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;
591
      }
592
    }
593
594
595
596
597
  }
}



598
//------------------------------------------------------------------------------
599
unsigned char get_ue_weight(module_id_t module_idP, int CC_idP, int ue_idP)
600
//------------------------------------------------------------------------------
601
{
602

603
  return(eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
604
605
606

}

607
//------------------------------------------------------------------------------
608
int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
609
//------------------------------------------------------------------------------
610
{
knopp's avatar
   
knopp committed
611
  int UE_id;
612
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
613

614
615
  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
616
617
618
    if (UE_list->UE_template[UE_PCCID(mod_idP,UE_id)][UE_id].rnti==rntiP) {
      return(UE_id);
    }
619
  }
620

knopp's avatar
   
knopp committed
621
  return(-1);
622
623
}

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//------------------------------------------------------------------------------
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];

 

  for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++)
    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);
  return(-1);
}

642
//------------------------------------------------------------------------------
643
int UE_num_active_CC(UE_list_t *listP,int ue_idP)
644
//------------------------------------------------------------------------------
645
{
knopp's avatar
   
knopp committed
646
647
  return(listP->numactiveCCs[ue_idP]);
}
648

649
//------------------------------------------------------------------------------
650
int UE_PCCID(module_id_t mod_idP,int ue_idP)
651
//------------------------------------------------------------------------------
652
{
653
  return(RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
knopp's avatar
   
knopp committed
654
}
655

656
//------------------------------------------------------------------------------
657
rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
658
//------------------------------------------------------------------------------
659
{
660

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

663
  if (rnti>0) {
knopp's avatar
   
knopp committed
664
    return (rnti);
665
  }
666

667
  LOG_D(MAC,"[eNB %d] Couldn't find RNTI for UE %d\n",mod_idP,ue_idP);
668
669
  //display_backtrace();
  return(NOT_A_RNTI);
670
}
knopp's avatar
   
knopp committed
671

672
//------------------------------------------------------------------------------
673
boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
674
//------------------------------------------------------------------------------
675
{
676
  return(RC.mac[mod_idP]->UE_list.active[ue_idP]);
677
}
knopp's avatar
   
knopp committed
678
679
680

/*
uint8_t find_active_UEs(module_id_t module_idP,int CC_id){
681
682
683
684
685
686
687

  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
688
      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)){
689
690

          if (mac_xface->get_eNB_UE_stats(module_idP,rnti) != NULL){ // check at the phy enb_ue state for this rnti
691
      nb_active_ue++;
692
693
          }
          else { // this ue is removed at the phy => remove it at the mac as well
694
      mac_remove_ue(module_idP, CC_id, ue_mod_id);
695
696
697
698
699
          }
      }
  }
  return(nb_active_ue);
}
knopp's avatar
   
knopp committed
700
*/
701
702


703
704
// get aggregation (L) form phy for a give UE
unsigned char get_aggregation (uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt)
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
  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);
   
738
  return 1<<aggregation;
739
}
knopp's avatar
   
knopp committed
740

741
742
void dump_ue_list(UE_list_t *listP, int ul_flag)
{
knopp's avatar
   
knopp committed
743
  int j;
744
745
746

  if ( ul_flag == 0 ) {
    for (j=listP->head; j>=0; j=listP->next[j]) {
747
748
749
      LOG_T(MAC,"node %d => %d\n",j,listP->next[j]);
    }
  } else {
750
    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
751
752
      LOG_T(MAC,"node %d => %d\n",j,listP->next_ul[j]);
    }
knopp's avatar
   
knopp committed
753
754
755
  }
}

756
757
758
759
760
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
               )
761
{
knopp's avatar
   
knopp committed
762
  int UE_id;
763
  int i, j;
764

765
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
766

knopp's avatar
   
knopp committed
767
  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);
768
  dump_ue_list(UE_list,0);
knopp's avatar
   
knopp committed
769

770
771
772
  for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
    if (UE_list->active[i] == TRUE) continue;
    UE_id = i;
knopp's avatar
   
knopp committed
773
774
775
776
777
778
779
    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
780
    UE_list->num_UEs++;
knopp's avatar
   
knopp committed
781
    UE_list->active[UE_id]                         = TRUE;
782
783
784
785
786

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

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

790
791
    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
792
      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j==harq_pidP)?0:1; // 1st transmission is with Msg3;
knopp's avatar
   
knopp committed
793
    }
794

795
    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
796
    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
knopp's avatar
   
knopp committed
797
    LOG_D(MAC,"[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",mod_idP,UE_id,cc_idP,rntiP);
798
    dump_ue_list(UE_list,0);
knopp's avatar
   
knopp committed
799
    return(UE_id);
800
  }
knopp's avatar
   
knopp committed
801

802
  printf("MAC: cannot add new UE for rnti %x\n", rntiP);
knopp's avatar
   
knopp committed
803
  LOG_E(MAC,"error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
804
  dump_ue_list(UE_list,0);
805
806
807
  return(-1);
}

808
//------------------------------------------------------------------------------
809
int rrc_mac_remove_ue(module_id_t mod_idP,rnti_t rntiP) 
810
//------------------------------------------------------------------------------
811
{
812
  int i;
813
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
814
  int UE_id = find_UE_id(mod_idP,rntiP);
815
  int pCC_id;
816

817
  if (UE_id == -1) {
818
printf("MAC: cannot remove UE rnti %x\n", rntiP);
819
    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n", rntiP);
820
    mac_phy_remove_ue(mod_idP, rntiP);
821
822
823
    return 0;
  }

824
825
  pCC_id = UE_PCCID(mod_idP,UE_id);

826
  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
827
  dump_ue_list(UE_list,0);
828

829
830
831
  UE_list->active[UE_id] = FALSE;
  UE_list->num_UEs--;

832
  // clear all remaining pending transmissions
833
834
835
836
837
838
839
840
841
842
843
844
  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
845

846
847
  mac_phy_remove_ue(mod_idP,rntiP);

848
849
850
  // check if this has an RA process active
  RA_TEMPLATE *RA_template;
  for (i=0;i<NB_RA_PROC_MAX;i++) {
851
    RA_template = (RA_TEMPLATE *)&RC.mac[mod_idP]->common_channels[pCC_id].RA_template[i];
852
    if (RA_template->rnti == rntiP){
853
854
855
856
857
858
859
      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;
860
      //break;
861
862
    }
  }
knopp's avatar
   
knopp committed
863

864
  return 0;
865
866
867
}


knopp's avatar
   
knopp committed
868

869
870
int prev(UE_list_t *listP, int nodeP, int ul_flag)
{
knopp's avatar
   
knopp committed
871
  int j;
872

873
  if (ul_flag == 0 ) {
874
    if (nodeP==listP->head) {
875
      return(nodeP);
876
    }
877
878

    for (j=listP->head; j>=0; j=listP->next[j]) {
879
      if (listP->next[j]==nodeP) {
880
        return(j);
881
    }
882
    }
883
  } else {
884
    if (nodeP==listP->head_ul) {
885
      return(nodeP);
886
    }
887
888

    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
889
      if (listP->next_ul[j]==nodeP) {
890
        return(j);
891
      }
892
    }
knopp's avatar
   
knopp committed
893
  }
knopp's avatar
   
knopp committed
894

895
  LOG_E(MAC,"error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
896
        nodeP, (ul_flag == 0)? "DL" : "UL");
897
  dump_ue_list(listP, ul_flag);
knopp's avatar
   
knopp committed
898
899


knopp's avatar
   
knopp committed
900
901
  return(-1);
}
902

903
904
void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag)
{
905

knopp's avatar
   
knopp committed
906
907
  int prev_i,prev_j,next_i,next_j;

908
909
  LOG_T(MAC,"Swapping UE %d,%d\n",nodeiP,nodejP);
  dump_ue_list(listP,ul_flag);
knopp's avatar
   
knopp committed
910

911
912
  prev_i = prev(listP,nodeiP,ul_flag);
  prev_j = prev(listP,nodejP,ul_flag);
913

914
915
  AssertFatal((prev_i>=0) && (prev_j>=0),
	      "swap_UEs: problem");
knopp's avatar
   
knopp committed
916

917
  if (ul_flag == 0) {
918
919
920
921
922
    next_i = listP->next[nodeiP];
    next_j = listP->next[nodejP];
  } else {
    next_i = listP->next_ul[nodeiP];
    next_j = listP->next_ul[nodejP];
knopp's avatar
   
knopp committed
923
  }
knopp's avatar
   
knopp committed
924

925
  LOG_T(MAC,"[%s] next_i %d, next_i, next_j %d, head %d \n",
926
927
        (ul_flag == 0)? "DL" : "UL",
        next_i,next_j,listP->head);
928
929

  if (ul_flag == 0 ) {
knopp's avatar
   
knopp committed
930

931
932
    if (next_i == nodejP) {   // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
      LOG_T(MAC,"Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
933

934
935
      listP->next[nodeiP] = next_j;
      listP->next[nodejP] = nodeiP;
936

937
      if (nodeiP==listP->head) { // case i j n(j)
938
        listP->head = nodejP;
939
      } else {
940
        listP->next[prev_i] = nodejP;
941
      }
942
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
943
944
945
      LOG_T(MAC,"Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = nodejP;
946

947
      if (nodejP==listP->head) { // case j i n(i)
948
        listP->head = nodeiP;
949
      } else {
950
        listP->next[prev_j] = nodeiP;
951
      }
952
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...
953
954
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = next_j;
955
956


957
      if (nodeiP==listP->head) {
958
959
960
961
962
963
964
965
966
967
        LOG_T(MAC,"changing head to %d\n",nodejP);
        listP->head=nodejP;
        listP->next[prev_j] = nodeiP;
      } else if (nodejP==listP->head) {
        LOG_D(MAC,"changing head to %d\n",nodeiP);
        listP->head=nodeiP;
        listP->next[prev_i] = nodejP;
      } else {
        listP->next[prev_i] = nodejP;
        listP->next[prev_j] = nodeiP;
968
969
970
971
972
973
      }
    }
  } else { // ul_flag

    if (next_i == nodejP) {   // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
      LOG_T(MAC,"[UL] Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
974

975
976
      listP->next_ul[nodeiP] = next_j;
      listP->next_ul[nodejP] = nodeiP;
977

978
      if (nodeiP==listP->head_ul) { // case i j n(j)
979
        listP->head_ul = nodejP;
980
      } else {
981
        listP->next_ul[prev_i] = nodejP;
982
      }
983
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
984
985
986
      LOG_T(MAC,"[UL]Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = nodejP;
987

988
      if (nodejP==listP->head_ul) { // case j i n(i)
989
        listP->head_ul = nodeiP;
990
      } else {
991
        listP->next_ul[prev_j] = nodeiP;
992
      }
993
994
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...

995
996
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = next_j;
997
998


999
      if (nodeiP==listP->head_ul) {
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
        LOG_T(MAC,"[UL]changing head to %d\n",nodejP);
        listP->head_ul=nodejP;
        listP->next_ul[prev_j] = nodeiP;
      } else if (nodejP==listP->head_ul) {
        LOG_T(MAC,"[UL]changing head to %d\n",nodeiP);
        listP->head_ul=nodeiP;
        listP->next_ul[prev_i] = nodejP;
      } else {
        listP->next_ul[prev_i] = nodejP;
        listP->next_ul[prev_j] = nodeiP;
1010
      }
knopp's avatar
   
knopp committed
1011
1012
    }
  }
1013

1014
1015
  LOG_T(MAC,"After swap\n");
  dump_ue_list(listP,ul_flag);
knopp's avatar
   
knopp committed
1016
1017
1018
1019
}



1020

1021
1022
1023
1024
1025




/*
Cedric Roux's avatar
Cedric Roux committed
1026
  #if defined(Rel10) || defined(Rel14)
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
  unsigned char generate_mch_header( unsigned char *mac_header,
  unsigned char num_sdus,
  unsigned short *sdu_lengths,
  unsigned char *sdu_lcids,
  unsigned char msi,
  unsigned char short_padding,
  unsigned short post_padding) {

  SCH_SUBHEADER_FIXED *mac_header_ptr = (SCH_SUBHEADER_FIXED *)mac_header;
  uint8_t first_element=0,last_size=0,i;
  uint8_t mac_header_control_elements[2*num_sdus],*ce_ptr;

  ce_ptr = &mac_header_control_elements[0];

  if ((short_padding == 1) || (short_padding == 2)) {
  mac_header_ptr->R    = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  first_element=1;
  last_size=1;
  }
  if (short_padding == 2) {
  mac_header_ptr->E = 1;
  mac_header_ptr++;
  mac_header_ptr->R = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  last_size=1;
  }

  // SUBHEADER for MSI CE
  if (msi != 0) {// there is MSI MAC Control Element
  if (first_element>0) {
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  }
  else {
  first_element = 1;
  }
  if (num_sdus*2 < 128) {
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = MCH_SCHDL_INFO;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = num_sdus*2;
  last_size=2;
  }
  else {
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = MCH_SCHDL_INFO;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L    = (num_sdus*2)&0x7fff;
  last_size=3;
  }
  // Create the MSI MAC Control Element here
  }

  // SUBHEADER for MAC SDU (MCCH+MTCHs)
  for (i=0;i<num_sdus;i++) {
  if (first_element>0) {
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  }
  else {
  first_element = 1;
  }
  if (sdu_lengths[i] < 128) {
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = sdu_lcids[i];
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = (unsigned char)sdu_lengths[i];
  last_size=2;
  }
  else {
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = sdu_lcids[i];
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L    = (unsigned short) sdu_lengths[i]&0x7fff;
  last_size=3;
  }
  }

  if (post_padding>0) {// we have lots of padding at the end of the packet
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  // add a padding element
  mac_header_ptr->R    = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  mac_header_ptr++;
  }
  else { // no end of packet padding
  // last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
  mac_header_ptr++;
  }

  // Copy MSI Control Element to the end of the MAC Header if it presents
  if ((ce_ptr-mac_header_control_elements) > 0) {
  // printf("Copying %d bytes for control elements\n",ce_ptr-mac_header_control_elements);
  memcpy((void*)mac_header_ptr,mac_header_control_elements,ce_ptr-mac_header_control_elements);
  mac_header_ptr+=(unsigned char)(ce_ptr-mac_header_control_elements);
  }

  return((unsigned char*)mac_header_ptr - mac_header);
  }
  #endif
 */



// This has to be updated to include BSR information
1141
1142
uint8_t UE_is_to_be_scheduled(module_id_t module_idP,int CC_id,uint8_t UE_id)
{
1143

1144
1145
  UE_TEMPLATE *UE_template    = &RC.mac[module_idP]->UE_list.UE_template[CC_id][UE_id];
  UE_sched_ctrl *UE_sched_ctl = &RC.mac[module_idP]->UE_list.UE_sched_ctrl[UE_id];
1146

1147

Florian Kaltenberger's avatar
Florian Kaltenberger committed
1148
1149
1150
1151
1152
1153
  // do not schedule UE if UL is not working
  if (UE_sched_ctl->ul_failure_timer>0)
    return(0);
  if (UE_sched_ctl->ul_out_of_sync>0)
    return(0);

1154
1155
  LOG_D(MAC,"[eNB %d][PUSCH] Checking UL requirements UE %d/%x\n",module_idP,UE_id,UE_RNTI(module_idP,UE_id));

knopp's avatar
   
knopp committed
1156
1157
1158
1159
  if ((UE_template->bsr_info[LCGID0]>0) ||
      (UE_template->bsr_info[LCGID1]>0) ||
      (UE_template->bsr_info[LCGID2]>0) ||
      (UE_template->bsr_info[LCGID3]>0) ||
gauthier's avatar
gauthier committed
1160
      (UE_template->ul_SR>0) || // uplink scheduling request
1161
      ((UE_sched_ctl->ul_inactivity_timer>20)&&
1162
1163
1164
1165
1166
       (UE_sched_ctl->ul_scheduled==0))||  // every 2 frames when RRC_CONNECTED
      ((UE_sched_ctl->ul_inactivity_timer>10)&&
       (UE_sched_ctl->ul_scheduled==0)&&
       (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED))) // every Frame when not RRC_CONNECTED
    { 
1167

1168
    LOG_D(MAC,"[eNB %d][PUSCH] UE %d/%x should be scheduled\n",module_idP,UE_id,UE_RNTI(module_idP,UE_id));
1169
    return(1);
1170
  } else {
1171
    return(0);
1172
  }
1173
1174
1175
}


1176
1177
1178
1179
1180
1181
1182
uint8_t get_tmode(module_id_t module_idP,int CC_idP,int UE_idP) {

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

  struct PhysicalConfigDedicated  *physicalConfigDedicated = eNB->UE_list.physicalConfigDedicated[CC_idP][UE_idP];

1183
  if (physicalConfigDedicated == NULL ) { // RRCConnectionSetup not received by UE yet
1184
1185
1186
    AssertFatal(cc->p_eNB<=2,"p_eNB is %d, should be <2\n",cc->p_eNB);
    return(cc->p_eNB);
  }
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
  else {
    AssertFatal(physicalConfigDedicated->antennaInfo!=NULL,
		"antennaInfo is null for CCId %d, UEid %d\n",CC_idP,UE_idP);
    
    AssertFatal(physicalConfigDedicated->antennaInfo->present != PhysicalConfigDedicated__antennaInfo_PR_NOTHING, 
		"antennaInfo (mod_id %d, CC_id %d) is set to NOTHING\n",module_idP,CC_idP);
    
    if (physicalConfigDedicated->antennaInfo->present == PhysicalConfigDedicated__antennaInfo_PR_explicitValue) {
      return(physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode);
    }
    else if (physicalConfigDedicated->antennaInfo->present == PhysicalConfigDedicated__antennaInfo_PR_defaultValue) {
      AssertFatal(cc->p_eNB<=2,"p_eNB is %d, should be <2\n",cc->p_eNB);
      return(cc->p_eNB);
    }
    else AssertFatal(1==0,"Shouldn't be here\n");
  }
1203
1204
}

1205
int8_t get_ULharq(module_id_t module_idP,int CC_idP,uint16_t frameP,uint8_t subframeP) {
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
1232
1233

  uint8_t           ret       = -1;
  eNB_MAC_INST      *eNB      = RC.mac[module_idP];
  COMMON_channels_t *cc       = &eNB->common_channels[CC_idP];

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

    switch (cc->tdd_Config->subframeAssignment) {

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

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

        default:
1234
          AssertFatal(1==0,"subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",subframeP,(int)cc->tdd_Config->subframeAssignment);
1235
1236
1237
1238
1239
1240
          break;
        }

      break;

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

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

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

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

    default:
1265
1266
      AssertFatal(1==0,"subframe2_harq_pid, Unsupported TDD mode %d\n",(int)cc->tdd_Config->subframeAssignment);
      break;
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
    }
  }

  AssertFatal(ret!=-1,
	      "invalid harq_pid(%d) at SFN/SF = %d/%d\n", (int8_t)ret, frameP, subframeP);
  return ret;
}


uint16_t getRIV(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);
1285

1286
1287
  return(RIV);
}
1288

knopp's avatar
knopp committed
1289
uint32_t allocate_prbs(int UE_id,unsigned char nb_rb, int N_RB_DL, uint32_t *rballoc)
1290
{
1291
1292
1293
1294
1295

  int i;
  uint32_t rballoc_dci=0;
  unsigned char nb_rb_alloc=0;

knopp's avatar
knopp committed
1296
  for (i=0; i<(N_RB_DL-2); i+=2) {
1297
1298
1299
1300
1301
1302
    if (((*rballoc>>i)&3)==0) {
      *rballoc |= (3<<i);
      rballoc_dci |= (1<<((12-i)>>1));
      nb_rb_alloc+=2;
    }

1303
    if (nb_rb_alloc==nb_rb) {
1304
      return(rballoc_dci);
1305
    }
1306
1307
  }

knopp's avatar
knopp committed
1308
1309
1310
1311
  if ((N_RB_DL&1)==1) {
    if ((*rballoc>>(N_RB_DL-1)&1)==0) {
      *rballoc |= (1<<(N_RB_DL-1));
      rballoc_dci |= 1;
1312
    }
1313
  }
1314

1315
1316
1317
  return(rballoc_dci);
}

1318
1319
1320
1321
int get_bw_index(module_id_t module_id, uint8_t CC_id)
{

  int bw_index=0;
knopp's avatar
knopp committed
1322
1323
 
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
1324

knopp's avatar
knopp committed
1325
  switch (N_RB_DL) {
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
  case 6: // 1.4 MHz
    bw_index=0;
    break;

  case 25: // 5HMz
    bw_index=1;
    break;

  case 50: // 10HMz
    bw_index=2;
    break;

  case 100: // 20HMz
    bw_index=3;
    break;

  default:
    bw_index=1;
knopp's avatar
knopp committed
1344
    LOG_W(MAC,"[eNB %d] N_RB_DL %d unknown for CC_id %d, setting bw_index to 1\n", module_id, N_RB_DL,CC_id);
1345
1346
1347
1348
1349
1350
    break;
  }

  return bw_index;
}

1351
1352
1353
int get_min_rb_unit(module_id_t module_id, uint8_t CC_id)
{

1354
  int min_rb_unit=0;
knopp's avatar
knopp committed
1355
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
1356

knopp's avatar
knopp committed
1357
  switch (N_RB_DL) {