[GITLAB] - A technical upgrade is planned on Thursday the 1st of July at noon on our GITLAB server.

eNB_scheduler_primitives.c 34.5 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 62
 * @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


63
//------------------------------------------------------------------------------
64
void init_ue_sched_info(void)
65
//------------------------------------------------------------------------------
66
{
67
  module_id_t i,j,k;
68 69

  for (i=0; i<NUMBER_OF_eNB_MAX; i++) {
Cedric Roux's avatar
Cedric Roux committed
70
    for (k=0; k<MAX_NUM_CCs; k++) {
71 72 73 74 75 76 77 78 79 80
      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;
81
      }
82
    }
83 84 85 86 87
  }
}



88
//------------------------------------------------------------------------------
89
unsigned char get_ue_weight(module_id_t module_idP, int CC_id, int ue_idP)
90
//------------------------------------------------------------------------------
91
{
92

93
  return(eNB_dlsch_info[module_idP][CC_id][ue_idP].weight);
94 95 96

}

97
//------------------------------------------------------------------------------
98
DCI_PDU *get_dci_sdu(module_id_t module_idP, int CC_id,frame_t frameP, sub_frame_t subframeP)
99
//------------------------------------------------------------------------------
100
{
101

knopp's avatar
 
knopp committed
102
  return(&eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu);
103 104 105

}

106
//------------------------------------------------------------------------------
107
int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
108
//------------------------------------------------------------------------------
109
{
110

knopp's avatar
 
knopp committed
111 112
  int UE_id;
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
113 114

  for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
knopp's avatar
 
knopp committed
115 116 117
    if (UE_list->UE_template[UE_PCCID(mod_idP,UE_id)][UE_id].rnti==rntiP) {
      return(UE_id);
    }
118
  }
119

knopp's avatar
 
knopp committed
120
  return(-1);
121 122 123

}

124
//------------------------------------------------------------------------------
125
int UE_num_active_CC(UE_list_t *listP,int ue_idP)
126
//------------------------------------------------------------------------------
127
{
knopp's avatar
 
knopp committed
128 129
  return(listP->numactiveCCs[ue_idP]);
}
130

131
//------------------------------------------------------------------------------
132
int UE_PCCID(module_id_t mod_idP,int ue_idP)
133
//------------------------------------------------------------------------------
134
{
knopp's avatar
 
knopp committed
135 136
  return(eNB_mac_inst[mod_idP].UE_list.pCC_id[ue_idP]);
}
137

138
//------------------------------------------------------------------------------
139
rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
140
//------------------------------------------------------------------------------
141
{
142

knopp's avatar
 
knopp committed
143
  rnti_t rnti = eNB_mac_inst[mod_idP].UE_list.UE_template[UE_PCCID(mod_idP,ue_idP)][ue_idP].rnti;
144

145
  if (rnti>0) {
knopp's avatar
 
knopp committed
146
    return (rnti);
147
  }
148

149
  LOG_D(MAC,"[eNB %d] Couldn't find RNTI for UE %d\n",mod_idP,ue_idP);
150 151
  //display_backtrace();
  return(NOT_A_RNTI);
152
}
knopp's avatar
 
knopp committed
153

154
//------------------------------------------------------------------------------
155
boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
156
//------------------------------------------------------------------------------
157
{
knopp's avatar
 
knopp committed
158
  return(eNB_mac_inst[mod_idP].UE_list.active[ue_idP]);
159
}
knopp's avatar
 
knopp committed
160 161 162

/*
uint8_t find_active_UEs(module_id_t module_idP,int CC_id){
163 164 165 166 167 168 169

  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
170
      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)){
171 172

          if (mac_xface->get_eNB_UE_stats(module_idP,rnti) != NULL){ // check at the phy enb_ue state for this rnti
173
      nb_active_ue++;
174 175
          }
          else { // this ue is removed at the phy => remove it at the mac as well
176
      mac_remove_ue(module_idP, CC_id, ue_mod_id);
177 178 179 180 181
          }
      }
  }
  return(nb_active_ue);
}
knopp's avatar
 
knopp committed
182
*/
183 184 185


// get aggregatiob form phy for a give UE
186 187
unsigned char process_ue_cqi (module_id_t module_idP, int ue_idP)
{
188
  unsigned char aggregation=1;
189 190 191 192
  // check the MCS and SNR and set the aggregation accordingly
  return aggregation;
}
#ifdef CBA
193
/*
knopp's avatar
 
knopp committed
194
uint8_t find_num_active_UEs_in_cbagroup(module_id_t module_idP, int CC_id,unsigned char group_id){
195 196 197 198 199 200

  module_id_t    UE_id;
  rnti_t    rnti;
  unsigned char nb_ue_in_pusch=0;
  LTE_eNB_UE_stats* eNB_UE_stats;

201
  for (UE_id=group_id;UE_id<NUMBER_OF_UE_MAX;UE_id+=eNB_mac_inst[module_idP].common_channels[CC_id].num_active_cba_groups) {
202

knopp's avatar
 
knopp committed
203 204
      if (((rnti=eNB_mac_inst[module_idP][CC_id].UE_template[UE_id].rnti) !=0) &&
          (eNB_mac_inst[module_idP][CC_id].UE_template[UE_id].ul_active==TRUE)    &&
205
          (mac_get_rrc_status(module_idP,1,UE_id) > RRC_CONNECTED)){
206 207 208 209 210 211 212
  //  && (UE_is_to_be_scheduled(module_idP,UE_id)))
  // check at the phy enb_ue state for this rnti
  if ((eNB_UE_stats= mac_xface->get_eNB_UE_stats(module_idP,CC_id,rnti)) != NULL){
    if ((eNB_UE_stats->mode == PUSCH) && (UE_is_to_be_scheduled(module_idP,UE_id) == 0)){
      nb_ue_in_pusch++;
    }
  }
213 214 215 216
      }
  }
  return(nb_ue_in_pusch);
}
217
*/
218
#endif
knopp's avatar
 
knopp committed
219

220 221
void dump_ue_list(UE_list_t *listP, int ul_flag)
{
knopp's avatar
 
knopp committed
222
  int j;
223 224 225

  if ( ul_flag == 0 ) {
    for (j=listP->head; j>=0; j=listP->next[j]) {
226 227 228
      LOG_T(MAC,"node %d => %d\n",j,listP->next[j]);
    }
  } else {
229
    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
230 231
      LOG_T(MAC,"node %d => %d\n",j,listP->next_ul[j]);
    }
knopp's avatar
 
knopp committed
232 233 234
  }
}

235 236
int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP,int harq_pidP)
{
knopp's avatar
 
knopp committed
237 238
  int UE_id;
  int j;
239

knopp's avatar
 
knopp committed
240
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
241

knopp's avatar
 
knopp committed
242
  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);
243
  dump_ue_list(UE_list,0);
knopp's avatar
 
knopp committed
244 245 246

  if (UE_list->avail>=0) {
    UE_id = UE_list->avail;
247
    AssertFatal( UE_id < NUMBER_OF_UE_MAX, "BAD UE_id %u > NUMBER_OF_UE_MAX",UE_id );
knopp's avatar
 
knopp committed
248 249
    UE_list->avail = UE_list->next[UE_list->avail];
    UE_list->next[UE_id] = UE_list->head;
250
    UE_list->next_ul[UE_id] = UE_list->head_ul;
knopp's avatar
 
knopp committed
251
    UE_list->head = UE_id;
252
    UE_list->head_ul = UE_id;
knopp's avatar
 
knopp committed
253 254 255 256 257 258 259
    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
260
    UE_list->num_UEs++;
knopp's avatar
 
knopp committed
261
    UE_list->active[UE_id]                         = TRUE;
262
    memset((void*)&UE_list->UE_sched_ctrl[UE_id],0,sizeof(UE_sched_ctrl));
knopp's avatar
 
knopp committed
263

264 265
    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
266
      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
267
    }
268

269
    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
270
    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
knopp's avatar
 
knopp committed
271
    LOG_D(MAC,"[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",mod_idP,UE_id,cc_idP,rntiP);
272
    dump_ue_list(UE_list,0);
knopp's avatar
 
knopp committed
273
    return(UE_id);
274
  }
knopp's avatar
 
knopp committed
275 276

  LOG_E(MAC,"error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
277
  dump_ue_list(UE_list,0);
278 279 280
  return(-1);
}

281
//------------------------------------------------------------------------------
282
int rrc_mac_remove_ue(module_id_t mod_idP,rnti_t rntiP) 
283
//------------------------------------------------------------------------------
284
{
knopp's avatar
 
knopp committed
285

286
  int prev,i, ret=-1;
knopp's avatar
 
knopp committed
287

288

knopp's avatar
 
knopp committed
289
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
290 291
  int UE_id = find_UE_id(mod_idP,rntiP);
  int pCC_id = UE_PCCID(mod_idP,UE_id);
292

293 294 295 296 297 298
  if (UE_id == -1) {
    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n", rntiP);
    mac_phy_remove_ue(mod_idP,rntiP);
    return 0;
  }

299
  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
300
  dump_ue_list(UE_list,0);
301 302

  // clear all remaining pending transmissions
303 304 305 306 307 308 309 310 311 312 313 314
  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
315 316

  prev = UE_list->head;
317 318

  for (i=UE_list->head; i>=0; i=UE_list->next[i]) {
319
    if (i == UE_id) {
knopp's avatar
 
knopp committed
320
      // link prev to next in Active list
321
      if (i==UE_list->head) {
322
        UE_list->head = UE_list->next[i];
323
      } else {
324
        UE_list->next[prev] = UE_list->next[i];
325
      }
326 327

      // add UE id (i)to available
knopp's avatar
 
knopp committed
328 329 330
      UE_list->next[i] = UE_list->avail;
      UE_list->avail = i;
      UE_list->active[i] = FALSE;
331
      UE_list->num_UEs--;
332 333
      ret=0;
      break;
knopp's avatar
 
knopp committed
334
    }
335

knopp's avatar
 
knopp committed
336 337
    prev=i;
  }
338

339 340
  // do the same for UL
  prev = UE_list->head_ul;
341 342

  for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
343
    if (i == UE_id) {
344
      // link prev to next in Active list
345
      if (i==UE_list->head_ul) {
346
        UE_list->head_ul = UE_list->next_ul[i];
347
      } else {
348
        UE_list->next_ul[prev] = UE_list->next_ul[i];
349
      }
350 351

      // add UE id (i)to available
352 353 354 355
      UE_list->next_ul[i] = UE_list->avail;
      ret = 0;
      break;
    }
356

357 358
    prev=i;
  }
359

360 361
  mac_phy_remove_ue(mod_idP,rntiP);

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  // check if this has an RA process active
  RA_TEMPLATE *RA_template;
  for (i=0;i<NB_RA_PROC_MAX;i++) {
    RA_template = (RA_TEMPLATE *)&eNB_mac_inst[mod_idP].common_channels[pCC_id].RA_template[i];
    if ((RA_template->RA_active == TRUE) && 
	(RA_template->rnti == rntiP)){
      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;
      break;
    }
  }
378
  if (ret == 0) {
379
    return (0);
380
  }
381

382
  LOG_E(MAC,"error in mac_remove_ue(), could not find previous to %d in UE_list, should never happen, Dumping UE list\n",UE_id);
383
  dump_ue_list(UE_list,0);
384
  mac_xface->macphy_exit("mac_remove_ue: Problem in UE_list");
knopp's avatar
 
knopp committed
385 386
  return(-1);

387 388 389
}


knopp's avatar
 
knopp committed
390

391 392
int prev(UE_list_t *listP, int nodeP, int ul_flag)
{
knopp's avatar
 
knopp committed
393
  int j;
394

395
  if (ul_flag == 0 ) {
396
    if (nodeP==listP->head) {
397
      return(nodeP);
398
    }
399 400

    for (j=listP->head; j>=0; j=listP->next[j]) {
401
      if (listP->next[j]==nodeP) {
402
        return(j);
403
    }
404
    }
405
  } else {
406
    if (nodeP==listP->head_ul) {
407
      return(nodeP);
408
    }
409 410

    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
411
      if (listP->next_ul[j]==nodeP) {
412
        return(j);
413
      }
414
    }
knopp's avatar
 
knopp committed
415
  }
knopp's avatar
 
knopp committed
416

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


knopp's avatar
 
knopp committed
422 423
  return(-1);
}
424

425 426
void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag)
{
427

knopp's avatar
 
knopp committed
428 429
  int prev_i,prev_j,next_i,next_j;

430 431
  LOG_T(MAC,"Swapping UE %d,%d\n",nodeiP,nodejP);
  dump_ue_list(listP,ul_flag);
knopp's avatar
 
knopp committed
432

433 434
  prev_i = prev(listP,nodeiP,ul_flag);
  prev_j = prev(listP,nodejP,ul_flag);
435

436
  if ((prev_i<0) || (prev_j<0)) {
437
    mac_xface->macphy_exit("swap_UEs: problem");
438 439
    return; // not reached
  }
knopp's avatar
 
knopp committed
440

441
  if (ul_flag == 0) {
442 443 444 445 446
    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
447
  }
knopp's avatar
 
knopp committed
448

449
  LOG_T(MAC,"[%s] next_i %d, next_i, next_j %d, head %d \n",
450 451
        (ul_flag == 0)? "DL" : "UL",
        next_i,next_j,listP->head);
452 453

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

455 456
    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");
457

458 459
      listP->next[nodeiP] = next_j;
      listP->next[nodejP] = nodeiP;
460

461
      if (nodeiP==listP->head) { // case i j n(j)
462
        listP->head = nodejP;
463
      } else {
464
        listP->next[prev_i] = nodejP;
465
      }
466
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
467 468 469
      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;
470

471
      if (nodejP==listP->head) { // case j i n(i)
472
        listP->head = nodeiP;
473
      } else {
474
        listP->next[prev_j] = nodeiP;
475
      }
476
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...
477 478
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = next_j;
479 480


481
      if (nodeiP==listP->head) {
482 483 484 485 486 487 488 489 490 491
        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;
492 493 494 495 496 497
      }
    }
  } 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");
498

499 500
      listP->next_ul[nodeiP] = next_j;
      listP->next_ul[nodejP] = nodeiP;
501

502
      if (nodeiP==listP->head_ul) { // case i j n(j)
503
        listP->head_ul = nodejP;
504
      } else {
505
        listP->next_ul[prev_i] = nodejP;
506
      }
507
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
508 509 510
      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;
511

512
      if (nodejP==listP->head_ul) { // case j i n(i)
513
        listP->head_ul = nodeiP;
514
      } else {
515
        listP->next_ul[prev_j] = nodeiP;
516
      }
517 518
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...

519 520
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = next_j;
521 522


523
      if (nodeiP==listP->head_ul) {
524 525 526 527 528 529 530 531 532 533
        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;
534
      }
knopp's avatar
 
knopp committed
535 536
    }
  }
537

538 539
  LOG_T(MAC,"After swap\n");
  dump_ue_list(listP,ul_flag);
knopp's avatar
 
knopp committed
540 541 542 543
}



544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661




/*
  #ifdef Rel10
  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
 */
void add_common_dci(DCI_PDU *DCI_pdu,
662 663 664 665 666 667 668 669
                    void *pdu,
                    rnti_t rnti,
                    unsigned char dci_size_bytes,
                    unsigned char aggregation,
                    unsigned char dci_size_bits,
                    unsigned char dci_fmt,
                    uint8_t ra_flag)
{
670 671 672 673 674 675 676 677 678 679

  memcpy(&DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].dci_pdu[0],pdu,dci_size_bytes);
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].dci_length = dci_size_bits;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].L          = aggregation;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].rnti       = rnti;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].format     = dci_fmt;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].ra_flag    = ra_flag;


  DCI_pdu->Num_common_dci++;
680
  LOG_D(MAC,"add common dci format %d for rnti %x \n",dci_fmt,rnti);
681 682
}

683 684
void add_ue_spec_dci(DCI_PDU *DCI_pdu,void *pdu,rnti_t rnti,unsigned char dci_size_bytes,unsigned char aggregation,unsigned char dci_size_bits,unsigned char dci_fmt,uint8_t ra_flag)
{
685 686 687 688 689 690 691 692 693

  memcpy(&DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].dci_pdu[0],pdu,dci_size_bytes);
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].dci_length = dci_size_bits;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].L          = aggregation;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].rnti       = rnti;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].format     = dci_fmt;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].ra_flag    = ra_flag;

  DCI_pdu->Num_ue_spec_dci++;
nikaeinn's avatar
nikaeinn committed
694

695
  LOG_D(MAC,"add ue specific dci format %d for rnti %x \n",dci_fmt,rnti);
696 697 698 699 700 701 702
}





// This has to be updated to include BSR information
703 704
uint8_t UE_is_to_be_scheduled(module_id_t module_idP,int CC_id,uint8_t UE_id)
{
705

706 707
  UE_TEMPLATE *UE_template    = &eNB_mac_inst[module_idP].UE_list.UE_template[CC_id][UE_id];
  UE_sched_ctrl *UE_sched_ctl = &eNB_mac_inst[module_idP].UE_list.UE_sched_ctrl[UE_id];
708

709

Florian Kaltenberger's avatar
Florian Kaltenberger committed
710 711 712 713 714 715
  // 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);

716 717
  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
718 719 720 721
  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
722
      (UE_template->ul_SR>0) || // uplink scheduling request
723
      ((UE_sched_ctl->ul_inactivity_timer>20)&&
724 725 726 727 728
       (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
    { 
729

730
    LOG_D(MAC,"[eNB %d][PUSCH] UE %d/%x should be scheduled\n",module_idP,UE_id,UE_RNTI(module_idP,UE_id));
731
    return(1);
732
  } else {
733
    return(0);
734
  }
735 736 737 738 739
}




740 741
uint32_t allocate_prbs(int UE_id,unsigned char nb_rb, uint32_t *rballoc)
{
742 743 744 745 746

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

747
  for (i=0; i<(mac_xface->frame_parms->N_RB_DL-2); i+=2) {
748 749 750 751 752 753
    if (((*rballoc>>i)&3)==0) {
      *rballoc |= (3<<i);
      rballoc_dci |= (1<<((12-i)>>1));
      nb_rb_alloc+=2;
    }

754
    if (nb_rb_alloc==nb_rb) {
755
      return(rballoc_dci);
756
    }
757 758
  }

759 760 761 762
  if ((mac_xface->frame_parms->N_RB_DL&1)==1) {
    if ((*rballoc>>(mac_xface->frame_parms->N_RB_DL-1)&1)==0) {
      *rballoc |= (1<<(mac_xface->frame_parms->N_RB_DL-1));
      rballoc_dci |= 1;//(1<<(mac_xface->frame_parms->N_RB_DL>>1));
763
    }
764
  }
765

766 767 768
  return(rballoc_dci);
}

769 770 771
int get_min_rb_unit(module_id_t module_id, uint8_t CC_id)
{

772
  int min_rb_unit=0;
773 774
  LTE_DL_FRAME_PARMS* frame_parms = mac_xface->get_lte_frame_parms(module_id,CC_id);

775 776 777 778
  switch (frame_parms->N_RB_DL) {
  case 6: // 1.4 MHz
    min_rb_unit=1;
    break;
779

780 781 782
  case 25: // 5HMz
    min_rb_unit=2;
    break;
783

784 785 786
  case 50: // 10HMz
    min_rb_unit=3;
    break;
787

788 789 790
  case 100: // 20HMz
    min_rb_unit=4;
    break;
791

792 793
  default:
    min_rb_unit=2;
Cedric Roux's avatar
Cedric Roux committed
794 795
    LOG_W(MAC,"[eNB %d] N_DL_RB %d unknown for CC_id %d, setting min_rb_unit to 2\n",
          module_id, frame_parms->N_RB_DL, CC_id);
796 797
    break;
  }
798

799 800
  return min_rb_unit;
}
801

802 803
uint32_t allocate_prbs_sub(int nb_rb, uint8_t *rballoc)
{
804 805 806 807 808

  int check=0;//check1=0,check2=0;
  uint32_t rballoc_dci=0;
  //uint8_t number_of_subbands=13;

809
  LOG_T(MAC,"*****Check1RBALLOC****: %d%d%d%d (nb_rb %d,N_RBG %d)\n",
810
        rballoc[3],rballoc[2],rballoc[1],rballoc[0],nb_rb,mac_xface->frame_parms->N_RBG);
811

812
  while((nb_rb >0) && (check < mac_xface->frame_parms->N_RBG)) {
knopp's avatar
 
knopp committed
813
    //printf("rballoc[%d] %d\n",check,rballoc[check]);
814
    if(rballoc[check] == 1) {
815
      rballoc_dci |= (1<<((mac_xface->frame_parms->N_RBG-1)-check));
816

817
      switch (mac_xface->frame_parms->N_RB_DL) {
818 819 820 821 822
      case 6:
        nb_rb--;
        break;

      case 25:
823
        if ((check == mac_xface->frame_parms->N_RBG-1)) {
824
          nb_rb--;
825
        } else {
826
          nb_rb-=2;
827
        }
828 829 830 831

        break;

      case 50:
832
        if ((check == mac_xface->frame_parms->N_RBG-1)) {
833
          nb_rb-=2;
834
        } else {
835
          nb_rb-=3;
836
        }
837 838 839 840 841 842

        break;

      case 100:
        nb_rb-=4;
        break;
843
      }
844 845 846 847 848
    }

    //      printf("rb_alloc %x\n",rballoc_dci);
    check = check+1;
    //    check1 = check1+2;
849
  }
850

851 852 853 854 855 856 857
  // rballoc_dci = (rballoc_dci)&(0x1fff);
  LOG_T(MAC,"*********RBALLOC : %x\n",rballoc_dci);
  // exit(-1);
  return (rballoc_dci);
}


858 859 860
int get_nb_subband(void)
{

861 862
  int nb_sb=0;

863
  switch (mac_xface->frame_parms->N_RB_DL) {
864
  case 6:
865
    nb_sb=0;
866
    break;
867

868 869
  case 15:
    nb_sb = 4;  // sb_size =4
870 871

  case 25:
872 873
    nb_sb = 7; // sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
    break;
874

875 876 877
  case 50:    // sb_size =6
    nb_sb = 9;
    break;
878

879 880 881
  case 75:  // sb_size =8
    nb_sb = 10;
    break;
882

883 884 885
  case 100: // sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
    nb_sb = 13;
    break;
886

887 888 889 890
  default:
    nb_sb=0;
    break;
  }
891

892
  return nb_sb;
893 894

}