eNB_scheduler_primitives.c 35.6 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27 28 29 30 31

*******************************************************************************/

/*! \file eNB_scheduler_primitives.c
 * \brief primitives used by eNB for BCH, RACH, ULSCH, DLSCH scheduling
32 33 34
 * \author  Navid Nikaein and Raymond Knopp
 * \date 2010 - 2014
 * \email: navid.nikaein@eurecom.fr
35
 * \version 1.0
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 * @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


71
//------------------------------------------------------------------------------
72
void init_ue_sched_info(void)
73
//------------------------------------------------------------------------------
74
{
75
  module_id_t i,j,k;
76 77 78 79 80 81 82 83 84 85 86 87 88

  for (i=0; i<NUMBER_OF_eNB_MAX; i++) {
    for (k=0; i<MAX_NUM_CCs; i++) {
      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;
89
      }
90
    }
91 92 93 94 95
  }
}



96
//------------------------------------------------------------------------------
97
unsigned char get_ue_weight(module_id_t module_idP, int CC_id, int ue_idP)
98
//------------------------------------------------------------------------------
99
{
100

101
  return(eNB_dlsch_info[module_idP][CC_id][ue_idP].weight);
102 103 104

}

105
//------------------------------------------------------------------------------
106
DCI_PDU *get_dci_sdu(module_id_t module_idP, int CC_id,frame_t frameP, sub_frame_t subframeP)
107
//------------------------------------------------------------------------------
108
{
109

Raymond Knopp's avatar
 
Raymond Knopp committed
110
  return(&eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu);
111 112 113

}

114
//------------------------------------------------------------------------------
115
int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
116
//------------------------------------------------------------------------------
117
{
118

Raymond Knopp's avatar
 
Raymond Knopp committed
119 120
  int UE_id;
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
121 122

  for (UE_id=UE_list->head; UE_id>=0; UE_id=UE_list->next[UE_id]) {
Raymond Knopp's avatar
 
Raymond Knopp committed
123 124 125
    if (UE_list->UE_template[UE_PCCID(mod_idP,UE_id)][UE_id].rnti==rntiP) {
      return(UE_id);
    }
126
  }
127

Raymond Knopp's avatar
 
Raymond Knopp committed
128
  return(-1);
129 130 131

}

132
//------------------------------------------------------------------------------
133
int UE_num_active_CC(UE_list_t *listP,int ue_idP)
134
//------------------------------------------------------------------------------
135
{
Raymond Knopp's avatar
 
Raymond Knopp committed
136 137
  return(listP->numactiveCCs[ue_idP]);
}
138

139
//------------------------------------------------------------------------------
140
int UE_PCCID(module_id_t mod_idP,int ue_idP)
141
//------------------------------------------------------------------------------
142
{
Raymond Knopp's avatar
 
Raymond Knopp committed
143 144
  return(eNB_mac_inst[mod_idP].UE_list.pCC_id[ue_idP]);
}
145

146
//------------------------------------------------------------------------------
147
rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
148
//------------------------------------------------------------------------------
149
{
150

Raymond Knopp's avatar
 
Raymond Knopp committed
151
  rnti_t rnti = eNB_mac_inst[mod_idP].UE_list.UE_template[UE_PCCID(mod_idP,ue_idP)][ue_idP].rnti;
152

153
  if (rnti>0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
154
    return (rnti);
155
  }
156

157 158 159
  LOG_E(MAC,"[eNB %d] Couldn't find RNTI for UE %d\n",mod_idP,ue_idP);
  //display_backtrace();
  return(NOT_A_RNTI);
160
}
Raymond Knopp's avatar
 
Raymond Knopp committed
161

162
//------------------------------------------------------------------------------
163
boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
164
//------------------------------------------------------------------------------
165
{
Raymond Knopp's avatar
 
Raymond Knopp committed
166
  return(eNB_mac_inst[mod_idP].UE_list.active[ue_idP]);
167
}
Raymond Knopp's avatar
 
Raymond Knopp committed
168 169 170

/*
uint8_t find_active_UEs(module_id_t module_idP,int CC_id){
171 172 173 174 175 176 177

  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++) {

Raymond Knopp's avatar
 
Raymond Knopp committed
178
      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)){
179 180

          if (mac_xface->get_eNB_UE_stats(module_idP,rnti) != NULL){ // check at the phy enb_ue state for this rnti
181
      nb_active_ue++;
182 183
          }
          else { // this ue is removed at the phy => remove it at the mac as well
184
      mac_remove_ue(module_idP, CC_id, ue_mod_id);
185 186 187 188 189
          }
      }
  }
  return(nb_active_ue);
}
Raymond Knopp's avatar
 
Raymond Knopp committed
190
*/
191 192 193


// get aggregatiob form phy for a give UE
194 195
unsigned char process_ue_cqi (module_id_t module_idP, int ue_idP)
{
196 197 198 199 200
  unsigned char aggregation=2;
  // check the MCS and SNR and set the aggregation accordingly
  return aggregation;
}
#ifdef CBA
201
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
202
uint8_t find_num_active_UEs_in_cbagroup(module_id_t module_idP, int CC_id,unsigned char group_id){
203 204 205 206 207 208

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

209
  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) {
210

Raymond Knopp's avatar
 
Raymond Knopp committed
211 212
      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)    &&
213
          (mac_get_rrc_status(module_idP,1,UE_id) > RRC_CONNECTED)){
214 215 216 217 218 219 220
  //  && (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++;
    }
  }
221 222 223 224
      }
  }
  return(nb_ue_in_pusch);
}
225
*/
226
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
227

228 229
void dump_ue_list(UE_list_t *listP, int ul_flag)
{
Raymond Knopp's avatar
 
Raymond Knopp committed
230
  int j;
231 232 233

  if ( ul_flag == 0 ) {
    for (j=listP->head; j>=0; j=listP->next[j]) {
234 235 236
      LOG_T(MAC,"node %d => %d\n",j,listP->next[j]);
    }
  } else {
237
    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
238 239
      LOG_T(MAC,"node %d => %d\n",j,listP->next_ul[j]);
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
240 241 242
  }
}

243 244
int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP,int harq_pidP)
{
Raymond Knopp's avatar
 
Raymond Knopp committed
245 246
  int UE_id;
  int j;
247 248 249 250
#if FAPI
  struct CschedUeConfigReqParameters p;
  struct CschedUeConfigCnfParameters r;
#endif
251

Raymond Knopp's avatar
 
Raymond Knopp committed
252
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
253

Raymond Knopp's avatar
 
Raymond Knopp committed
254
  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);
255
  dump_ue_list(UE_list,0);
Raymond Knopp's avatar
 
Raymond Knopp committed
256 257 258

  if (UE_list->avail>=0) {
    UE_id = UE_list->avail;
259
    AssertFatal( UE_id < NUMBER_OF_UE_MAX, "BAD UE_id %u > NUMBER_OF_UE_MAX",UE_id );
Raymond Knopp's avatar
 
Raymond Knopp committed
260 261
    UE_list->avail = UE_list->next[UE_list->avail];
    UE_list->next[UE_id] = UE_list->head;
262
    UE_list->next_ul[UE_id] = UE_list->head_ul;
Raymond Knopp's avatar
 
Raymond Knopp committed
263
    UE_list->head = UE_id;
264
    UE_list->head_ul = UE_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
265 266 267 268 269 270 271
    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;
Raymond Knopp's avatar
 
Raymond Knopp committed
272
    UE_list->num_UEs++;
Raymond Knopp's avatar
 
Raymond Knopp committed
273
    UE_list->active[UE_id]                         = TRUE;
274
    memset((void*)&UE_list->UE_sched_ctrl[UE_id],0,sizeof(UE_sched_ctrl));
Raymond Knopp's avatar
 
Raymond Knopp committed
275

276 277
    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
Raymond Knopp's avatar
 
Raymond Knopp committed
278
      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j==harq_pidP)?0:1; // 1st transmission is with Msg3;
Raymond Knopp's avatar
 
Raymond Knopp committed
279
    }
280

281
    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
282
    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
Raymond Knopp's avatar
 
Raymond Knopp committed
283
    LOG_D(MAC,"[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",mod_idP,UE_id,cc_idP,rntiP);
284
    dump_ue_list(UE_list,0);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

#if FAPI
    LOG_W(MAC, "%s:%d:%s: set correct values in CschedUeConfigReqParameters\n", __FILE__, __LINE__, __FUNCTION__);
    p.rnti                               = rntiP;
    p.reconfigureFlag                    = false;
    p.drxConfigPresent                   = false;
    p.timeAlignmentTimer                 = 0;                       /* TBC */
    p.measGapConfigPattern               = OFF;                     /* TBC */
    p.spsConfigPresent                   = false;
    p.srConfigPresent                    = false;                   /* TODO? */
    p.cqiConfigPresent                   = false;                   /* TODO? */
    p.transmissionMode                   = 1;                       /* get real one */
    p.ueAggregatedMaximumBitrateUl       = 900000000L;              /* TODO */
    p.ueAggregatedMaximumBitrateDl       = 900000000L;              /* TODO */
    /* initial UE capabilities - let's pretend to be cat3 */
    p.ueCapabilities.halfDuplex          = false;
    p.ueCapabilities.intraSfHopping      = false;
    p.ueCapabilities.type2Sb1            = false;
    p.ueCapabilities.ueCategory          = 3;
    p.ueCapabilities.resAllocType1       = true;                    /* TBC */
    p.ueTransmitAntennaSelection         = noneloop;                /* TBC */
    p.ttiBundling                        = false;                   /* TBC */
    p.maxHarqTx                          = 8;                       /* get real one */
    p.betaOffsetAckIndex                 = 0;                       /* TODO */
    p.betaOffsetRiIndex                  = 0;                       /* TODO */
    p.betaOffsetCqiIndex                 = 0;                       /* TODO */
    p.ackNackSrsSimultaneousTransmission = false;                   /* get real one */
    p.simultaneousAckNackAndCqi          = true;                    /* get real one */
    p.aperiodicCqiRepMode                = ff_rm30;                 /* get real one */
    p.tddAckNackFeedbackMode             = ff_bundling;             /* get real one */
    p.ackNackRepetitionFactor            = 0;                       /* get real one */
    p.extendedBSRSizes                   = false;
    p.caSupport                          = false;
    p.crossCarrierSchedSupport           = false;
    p.pcellCarrierIndex                  = 0;                       /* TBC */
    p.nr_scells                          = 0;
    p.scellConfigList[0]                 = NULL;
    p.scellDeactivationTimer             = 0;

    LOG_I(MAC, "calling CschedUeConfigReq\n");
    CschedUeConfigReq(eNB_mac_inst[mod_idP].fapi->sched, &p);
    LOG_I(MAC, "calling CschedUeConfigCnf\n");
    CschedUeConfigCnf(eNB_mac_inst[mod_idP].fapi, &r);
    if (r.rnti != rntiP || r.result != ff_SUCCESS) {
      LOG_E(MAC, "FAPI fatal error: rnti is %x (expected %x); result is %s (expected ff_SUCCESS)\n",
            r.rnti, rntiP, r.result == ff_SUCCESS ? "ff_SUCCESS" : "ff_FAILURE");
      abort();
    }
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
335
    return(UE_id);
336
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
337 338

  LOG_E(MAC,"error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
339
  dump_ue_list(UE_list,0);
340 341 342
  return(-1);
}

343 344 345
//------------------------------------------------------------------------------
int mac_remove_ue(module_id_t mod_idP, int ue_idP, int frameP, sub_frame_t subframeP)
//------------------------------------------------------------------------------
346
{
Raymond Knopp's avatar
 
Raymond Knopp committed
347

348
  int prev,i, ret=-1;
Raymond Knopp's avatar
 
Raymond Knopp committed
349

350
  rnti_t  rnti;
Raymond Knopp's avatar
 
Raymond Knopp committed
351 352
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;
  int pCC_id = UE_PCCID(mod_idP,ue_idP);
353

354 355
  rnti = UE_list->UE_template[pCC_id][ue_idP].rnti;
  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",ue_idP,pCC_id, rnti);
356
  dump_ue_list(UE_list,0);
357 358

  // clear all remaining pending transmissions
Raymond Knopp's avatar
 
Raymond Knopp committed
359 360 361 362 363 364
  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID0]  = 0;
  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID1]  = 0;
  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID2]  = 0;
  UE_list->UE_template[pCC_id][ue_idP].bsr_info[LCGID3]  = 0;

  UE_list->UE_template[pCC_id][ue_idP].ul_SR             = 0;
365
  UE_list->UE_template[pCC_id][ue_idP].rnti              = NOT_A_RNTI;
Raymond Knopp's avatar
 
Raymond Knopp committed
366
  UE_list->UE_template[pCC_id][ue_idP].ul_active         = FALSE;
367
  eNB_ulsch_info[mod_idP][pCC_id][ue_idP].rnti                        = NOT_A_RNTI;
368
  eNB_ulsch_info[mod_idP][pCC_id][ue_idP].status                      = S_UL_NONE;
369
  eNB_dlsch_info[mod_idP][pCC_id][ue_idP].rnti                        = NOT_A_RNTI;
370
  eNB_dlsch_info[mod_idP][pCC_id][ue_idP].status                      = S_DL_NONE;
Raymond Knopp's avatar
 
Raymond Knopp committed
371

372 373 374 375 376
  rrc_eNB_free_UE(
    mod_idP,
    rnti,
    frameP,
    subframeP);
Raymond Knopp's avatar
 
Raymond Knopp committed
377 378

  prev = UE_list->head;
379 380

  for (i=UE_list->head; i>=0; i=UE_list->next[i]) {
Raymond Knopp's avatar
 
Raymond Knopp committed
381 382
    if (i == ue_idP) {
      // link prev to next in Active list
383
      //if (prev==UE_list->head)
384
      if (i==UE_list->head) {
385
        UE_list->head = UE_list->next[i];
386
      } else {
387
        UE_list->next[prev] = UE_list->next[i];
388
      }
389 390

      // add UE id (i)to available
Raymond Knopp's avatar
 
Raymond Knopp committed
391 392 393
      UE_list->next[i] = UE_list->avail;
      UE_list->avail = i;
      UE_list->active[i] = FALSE;
394
      UE_list->num_UEs--;
395 396
      ret=0;
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
397
    }
398

Raymond Knopp's avatar
 
Raymond Knopp committed
399 400
    prev=i;
  }
401

402 403
  // do the same for UL
  prev = UE_list->head_ul;
404 405

  for (i=UE_list->head_ul; i>=0; i=UE_list->next_ul[i]) {
406 407
    if (i == ue_idP) {
      // link prev to next in Active list
408
      if (prev==UE_list->head_ul) {
409
        UE_list->head_ul = UE_list->next_ul[i];
410
      } else {
411
        UE_list->next_ul[prev] = UE_list->next_ul[i];
412
      }
413 414

      // add UE id (i)to available
415 416 417 418
      UE_list->next_ul[i] = UE_list->avail;
      ret = 0;
      break;
    }
419

420 421
    prev=i;
  }
422

423 424 425 426 427
#if FAPI
printf("FAPI: remove UE: TODO\n");
abort();
#endif

428
  if (ret == 0) {
429
    return (0);
430
  }
431

Raymond Knopp's avatar
 
Raymond Knopp committed
432
  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_idP);
433
  dump_ue_list(UE_list,0);
434
  mac_xface->macphy_exit("mac_remove_ue: Problem in UE_list");
Raymond Knopp's avatar
 
Raymond Knopp committed
435 436
  return(-1);

437 438 439
}


Raymond Knopp's avatar
 
Raymond Knopp committed
440

441 442
int prev(UE_list_t *listP, int nodeP, int ul_flag)
{
Raymond Knopp's avatar
 
Raymond Knopp committed
443
  int j;
444

445
  if (ul_flag == 0 ) {
446
    if (nodeP==listP->head) {
447
      return(nodeP);
448
    }
449 450

    for (j=listP->head; j>=0; j=listP->next[j]) {
451
      if (listP->next[j]==nodeP) {
452
        return(j);
453
    }
454
    }
455
  } else {
456
    if (nodeP==listP->head_ul) {
457
      return(nodeP);
458
    }
459 460

    for (j=listP->head_ul; j>=0; j=listP->next_ul[j]) {
461
      if (listP->next_ul[j]==nodeP) {
462
        return(j);
463
      }
464
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
465
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
466

467
  LOG_E(MAC,"error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
468
        nodeP, (ul_flag == 0)? "DL" : "UL");
469
  dump_ue_list(listP, ul_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
470 471


Raymond Knopp's avatar
 
Raymond Knopp committed
472 473
  return(-1);
}
474

475 476
void swap_UEs(UE_list_t *listP,int nodeiP, int nodejP, int ul_flag)
{
477

Raymond Knopp's avatar
 
Raymond Knopp committed
478 479
  int prev_i,prev_j,next_i,next_j;

480 481
  LOG_T(MAC,"Swapping UE %d,%d\n",nodeiP,nodejP);
  dump_ue_list(listP,ul_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
482

483 484
  prev_i = prev(listP,nodeiP,ul_flag);
  prev_j = prev(listP,nodejP,ul_flag);
485

486
  if ((prev_i<0) || (prev_j<0)) {
487
    mac_xface->macphy_exit("swap_UEs: problem");
488 489
    return; // not reached
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
490

491
  if (ul_flag == 0) {
492 493 494 495 496
    next_i = listP->next[nodeiP];
    next_j = listP->next[nodejP];
  } else {
    next_i = listP->next_ul[nodeiP];
    next_j = listP->next_ul[nodejP];
Raymond Knopp's avatar
 
Raymond Knopp committed
497
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
498

499
  LOG_T(MAC,"[%s] next_i %d, next_i, next_j %d, head %d \n",
500 501
        (ul_flag == 0)? "DL" : "UL",
        next_i,next_j,listP->head);
502 503

  if (ul_flag == 0 ) {
Raymond Knopp's avatar
 
Raymond Knopp committed
504

505 506
    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");
507

508 509
      listP->next[nodeiP] = next_j;
      listP->next[nodejP] = nodeiP;
510

511
      if (nodeiP==listP->head) { // case i j n(j)
512
        listP->head = nodejP;
513
      } else {
514
        listP->next[prev_i] = nodejP;
515
      }
516
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
517 518 519
      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;
520

521
      if (nodejP==listP->head) { // case j i n(i)
522
        listP->head = nodeiP;
523
      } else {
524
        listP->next[prev_j] = nodeiP;
525
      }
526
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...
527 528
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = next_j;
529 530


531
      if (nodeiP==listP->head) {
532 533 534 535 536 537 538 539 540 541
        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;
542 543 544 545 546 547
      }
    }
  } 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");
548

549 550
      listP->next_ul[nodeiP] = next_j;
      listP->next_ul[nodejP] = nodeiP;
551

552
      if (nodeiP==listP->head_ul) { // case i j n(j)
553
        listP->head_ul = nodejP;
554
      } else {
555
        listP->next_ul[prev_i] = nodejP;
556
      }
557
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
558 559 560
      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;
561

562
      if (nodejP==listP->head_ul) { // case j i n(i)
563
        listP->head_ul = nodeiP;
564
      } else {
565
        listP->next_ul[prev_j] = nodeiP;
566
      }
567 568
    } else {  // case ...  p(i) i n(i) ... p(j) j n(j) ...

569 570
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = next_j;
571 572


573
      if (nodeiP==listP->head_ul) {
574 575 576 577 578 579 580 581 582 583
        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;
584
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
585 586
    }
  }
587

588 589
  LOG_T(MAC,"After swap\n");
  dump_ue_list(listP,ul_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
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
#if FAPI

void SR_indication(module_id_t mod_idP, int cc_idP, frame_t frameP, rnti_t rntiP, sub_frame_t subframeP)
{
  fapi_interface_t                  *fapi;
  int                               UE_id;
  struct SchedUlSrInfoReqParameters p;
  struct SrListElement_s            sr;

  UE_id = find_UE_id(mod_idP, rntiP);
  if (UE_id == -1) {
    LOG_E(MAC, "%s:%d:%s: rnti %x: no such UE\n", __FILE__, __LINE__, __FUNCTION__, rntiP);
    abort();
  }

  fapi = eNB_mac_inst[mod_idP].fapi;

  p.sfnSf = frameP * 16 + subframeP;
  p.nr_srList = 1;
  p.srList    = &sr;
  p.nr_vendorSpecificList = 0;
  p.vendorSpecificList    = NULL;

  sr.rnti = rntiP;

  LOG_I(MAC, "eNB %d/%d f/sf %d/%d calling SchedUlSrInfoReq for rnti %x\n", mod_idP, cc_idP, frameP, subframeP, rntiP);
618
printf("MAC to FAPI SR indication rnti %x f/sf %d/%d\n", rntiP, frameP, subframeP);
619 620 621 622 623
  SchedUlSrInfoReq(fapi->sched, &p);
}

#else /* FAPI */

624 625
void SR_indication(module_id_t mod_idP, int cc_idP, frame_t frameP, rnti_t rntiP, sub_frame_t subframeP)
{
Raymond Knopp's avatar
 
Raymond Knopp committed
626 627 628 629

  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &eNB_mac_inst[mod_idP].UE_list;

630
  if (UE_id  != -1) {
Raymond Knopp's avatar
 
Raymond Knopp committed
631
    LOG_D(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d on CC_id %d\n",mod_idP,rntiP,frameP,subframeP, UE_id,cc_idP);
632 633
    UE_list->UE_template[cc_idP][UE_id].ul_SR = 1;
    UE_list->UE_template[cc_idP][UE_id].ul_active = TRUE;
634 635
  } else {
    //     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
Raymond Knopp's avatar
 
Raymond Knopp committed
636
    //    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
637
    LOG_D(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d (unknown UEid) on CC_id %d\n",mod_idP,rntiP,frameP,subframeP, UE_id,cc_idP);
638 639 640
  }
}

641
#endif /* FAPI */
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



/*
  #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,
758 759 760 761 762 763 764 765
                    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)
{
766 767 768 769 770 771 772 773 774 775

  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++;
776
  LOG_D(MAC,"add common dci format %d for rnti %x \n",dci_fmt,rnti);
777 778
}

779 780
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)
{
781 782 783 784 785 786 787 788 789

  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++;
790

791
  LOG_D(MAC,"add ue specific dci format %d for rnti %x \n",dci_fmt,rnti);
792 793 794 795 796 797 798
}





// This has to be updated to include BSR information
799 800
uint8_t UE_is_to_be_scheduled(module_id_t module_idP,int CC_id,uint8_t UE_id)
{
801

Raymond Knopp's avatar
 
Raymond Knopp committed
802
  UE_TEMPLATE *UE_template = &eNB_mac_inst[module_idP].UE_list.UE_template[CC_id][UE_id];
803 804

  //  LOG_D(MAC,"[eNB %d][PUSCH] Frame %d subframeP %d Scheduling UE %d\n",module_idP,rnti,frameP,subframeP,
805
  //  UE_id);
806

Raymond Knopp's avatar
 
Raymond Knopp committed
807 808 809 810
  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) ||
811
      (UE_template->ul_SR>0)) { // uplink scheduling request
812
    return(1);
813
  } else {
814
    return(0);
815
  }
816 817 818 819 820
}




821 822
uint32_t allocate_prbs(int UE_id,unsigned char nb_rb, uint32_t *rballoc)
{
823 824 825 826 827

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

828 829 830 831 832 833 834
  for (i=0; i<(mac_xface->lte_frame_parms->N_RB_DL-2); i+=2) {
    if (((*rballoc>>i)&3)==0) {
      *rballoc |= (3<<i);
      rballoc_dci |= (1<<((12-i)>>1));
      nb_rb_alloc+=2;
    }

835
    if (nb_rb_alloc==nb_rb) {
836
      return(rballoc_dci);
837
    }
838 839 840
  }

  if ((mac_xface->lte_frame_parms->N_RB_DL&1)==1) {
841 842 843 844
    if ((*rballoc>>(mac_xface->lte_frame_parms->N_RB_DL-1)&1)==0) {
      *rballoc |= (1<<(mac_xface->lte_frame_parms->N_RB_DL-1));
      rballoc_dci |= 1;//(1<<(mac_xface->lte_frame_parms->N_RB_DL>>1));
    }
845
  }
846

847 848 849
  return(rballoc_dci);
}

850 851 852
int get_min_rb_unit(module_id_t module_id, uint8_t CC_id)
{

853
  int min_rb_unit=0;
854 855
  LTE_DL_FRAME_PARMS* frame_parms = mac_xface->get_lte_frame_parms(module_id,CC_id);

856 857 858 859
  switch (frame_parms->N_RB_DL) {
  case 6: // 1.4 MHz
    min_rb_unit=1;
    break;
860

861 862 863
  case 25: // 5HMz
    min_rb_unit=2;
    break;
864

865 866 867
  case 50: // 10HMz
    min_rb_unit=3;
    break;
868

869 870 871
  case 100: // 20HMz
    min_rb_unit=4;
    break;
872

873 874 875 876 877
  default:
    min_rb_unit=2;
    LOG_W(MAC,"[eNB %d] N_DL_RB %d unknown for CC_id %d, setting min_rb_unit to 2\n", module_id, CC_id);
    break;
  }
878

879 880
  return min_rb_unit;
}
881

882 883
uint32_t allocate_prbs_sub(int nb_rb, uint8_t *rballoc)
{
884 885 886 887 888

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

889
  LOG_T(MAC,"*****Check1RBALLOC****: %d%d%d%d (nb_rb %d,N_RBG %d)\n",
890 891 892
        rballoc[3],rballoc[2],rballoc[1],rballoc[0],nb_rb,mac_xface->lte_frame_parms->N_RBG);

  while((nb_rb >0) && (check < mac_xface->lte_frame_parms->N_RBG)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
893
    //printf("rballoc[%d] %d\n",check,rballoc[check]);
894 895 896 897 898 899 900 901 902
    if(rballoc[check] == 1) {
      rballoc_dci |= (1<<((mac_xface->lte_frame_parms->N_RBG-1)-check));

      switch (mac_xface->lte_frame_parms->N_RB_DL) {
      case 6:
        nb_rb--;
        break;

      case 25:
903
        if ((check == mac_xface->lte_frame_parms->N_RBG-1)) {
904
          nb_rb--;
905
        } else {
906
          nb_rb-=2;
907
        }
908 909 910 911

        break;

      case 50:
912
        if ((check == mac_xface->lte_frame_parms->N_RBG-1)) {
913
          nb_rb-=2;
914
        } else {
915
          nb_rb-=3;
916
        }
917 918 919 920 921 922

        break;

      case 100:
        nb_rb-=4;
        break;
923
      }
924 925 926 927 928
    }

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

931 932 933 934 935 936 937
  // rballoc_dci = (rballoc_dci)&(0x1fff);
  LOG_T(MAC,"*********RBALLOC : %x\n",rballoc_dci);
  // exit(-1);
  return (rballoc_dci);
}


938 939 940
int get_nb_subband(void)
{

941 942 943 944
  int nb_sb=0;

  switch (mac_xface->lte_frame_parms->N_RB_DL) {
  case 6:
945
    nb_sb=0;
946
    break;
947

948 949
  case 15:
    nb_sb = 4;  // sb_size =4
950 951

  case 25:
952 953
    nb_sb = 7; // sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
    break;
954

955 956 957
  case 50:    // sb_size =6
    nb_sb = 9;
    break;
958

959 960 961
  case 75:  // sb_size =8
    nb_sb = 10;
    break;
962

963 964 965
  case 100: // sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
    nb_sb = 13;
    break;
966

967 968 969 970
  default:
    nb_sb=0;
    break;
  }
971

972
  return nb_sb;
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

void init_CCE_table(int module_idP,int CC_idP)
{
  memset(eNB_mac_inst[module_idP].CCE_table[CC_idP],0,800*sizeof(int));
} 


int get_nCCE_offset(int *CCE_table,
		    const unsigned char L, 
		    const int nCCE, 
		    const int common_dci, 
		    const unsigned short rnti, 
		    const unsigned char subframe)
{

  int search_space_free,m,nb_candidates = 0,l,i;
  unsigned int Yk;
   /*
    printf("CCE Allocation: ");
    for (i=0;i<nCCE;i++)
    printf("%d.",CCE_table[i]);
    printf("\n");
  */
  if (common_dci == 1) {
    // check CCE(0 ... L-1)
    nb_candidates = (L==4) ? 4 : 2;
    nb_candidates = min(nb_candidates,nCCE/L);
Raymond Knopp's avatar
Raymond Knopp committed
1002

1003
    //    printf("Common DCI nb_candidates %d, L %d\n",nb_candidates,L);
Raymond Knopp's avatar
Raymond Knopp committed
1004

1005 1006 1007 1008
    for (m = nb_candidates-1 ; m >=0 ; m--) {

      search_space_free = 1;
      for (l=0; l<L; l++) {
Raymond Knopp's avatar
Raymond Knopp committed
1009

1010
	//	printf("CCE_table[%d] %d\n",(m*L)+l,CCE_table[(m*L)+l]);
1011 1012 1013 1014 1015 1016 1017
        if (CCE_table[(m*L) + l] == 1) {
          search_space_free = 0;
          break;
        }
      }
     
      if (search_space_free == 1) {
Raymond Knopp's avatar
Raymond Knopp committed
1018

1019
	//	printf("returning %d\n",m*L);
Raymond Knopp's avatar
Raymond Knopp committed
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
        for (l=0; l<L; l++)
          CCE_table[(m*L)+l]=1;
        return(m*L);
      }
    }

    return(-1);

  } else { // Find first available in ue specific search space
    // according to procedure in Section 9.1.1 of 36.213 (v. 8.6)
    // compute Yk
    Yk = (unsigned int)rnti;

    for (i=0; i<=subframe; i++)
      Yk = (Yk*39827)%65537;

    Yk = Yk % (nCCE/L);


    switch (L) {
    case 1:
    case 2:
      nb_candidates = 6;
      break;

    case 4:
    case 8:
      nb_candidates = 2;
      break;

    default:
      DevParam(L, nCCE, rnti);
      break;
    }

Raymond Knopp's avatar
Raymond Knopp committed
1056

1057
    LOG_D(MAC,"rnti %x, Yk = %d, nCCE %d (nCCE/L %d),nb_cand %d\n",rnti,Yk,nCCE,nCCE/L,nb_candidates);
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

    for (m = 0 ; m < nb_candidates ; m++) {
      search_space_free = 1;

      for (l=0; l<L; l++) {
        if (CCE_table[(((Yk+m)%(nCCE/L))*L) + l] == 1) {
          search_space_free = 0;
          break;
        }
      }

      if (search_space_free == 1) {
        for (l=0; l<L; l++)
          CCE_table[(((Yk+m)%(nCCE/L))*L)+l]=1;

        return(((Yk+m)%(nCCE/L))*L);
      }
    }

    return(-1);
  }
}

// Allocate the CCEs
int allocate_CCEs(int module_idP,
		  int CC_idP,
		  int subframeP,
		  int test_onlyP) {


  int *CCE_table = eNB_mac_inst[module_idP].CCE_table[CC_idP];
  DCI_PDU *DCI_pdu = &eNB_mac_inst[module_idP].common_channels[CC_idP].DCI_pdu;
  int nCCE_max = mac_xface->get_nCCE_max(module_idP,CC_idP,DCI_pdu->num_pdcch_symbols,subframeP);
  int fCCE;
1092
  int i,j;
1093 1094 1095
  int allocation_is_feasible = 1;
  DCI_ALLOC_t *dci_alloc;

Raymond Knopp's avatar
Raymond Knopp committed
1096

1097
  LOG_D(MAC,"Allocate CCEs subframe %d, test %d : (common %d,uspec %d)\n",subframeP,test_onlyP,DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci);
Raymond Knopp's avatar
Raymond Knopp committed
1098

1099 1100 1101 1102 1103 1104 1105
  init_CCE_table(module_idP,CC_idP);
  DCI_pdu->nCCE=0;

  while (allocation_is_feasible == 1) {

    for (i=0;i<DCI_pdu->Num_common_dci + DCI_pdu->Num_ue_spec_dci;i++) {
      dci_alloc = &DCI_pdu->dci_alloc[i];
1106
      LOG_D(MAC,"Trying to allocate DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	    i,DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci,
	    DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci,
	    dci_alloc->rnti,1<<dci_alloc->L,
	    DCI_pdu->nCCE,nCCE_max,DCI_pdu->num_pdcch_symbols);

      if (DCI_pdu->nCCE + (1<<dci_alloc->L) > nCCE_max) {
	if (DCI_pdu->num_pdcch_symbols == 3)
	  allocation_is_feasible = 0;
	else {
	  DCI_pdu->num_pdcch_symbols++;
	  nCCE_max = mac_xface->get_nCCE_max(module_idP,CC_idP,DCI_pdu->num_pdcch_symbols,subframeP);
	}
	break;
      }
      else { // number of CCEs left can potentially hold this allocation
	if ((fCCE = get_nCCE_offset(CCE_table,
1123 1124 1125 1126
				    1<<(dci_alloc->L), 
				    nCCE_max,
				    (i<DCI_pdu->Num_common_dci) ? 1 : 0, 
				    dci_alloc->rnti, 
1127
				    subframeP))>=0) {// the allocation is feasible, rnti rule passes
Raymond Knopp's avatar
Raymond Knopp committed
1128

1129
	  LOG_D(MAC,"Allocating at nCCE %d\n",fCCE);
1130 1131 1132
	  if (test_onlyP == 0) {
	    DCI_pdu->nCCE += (1<<dci_alloc->L);
	    dci_alloc->firstCCE=fCCE;
1133
	    LOG_D(MAC,"Allocate CCEs subframe %d, test %d\n",subframeP,test_onlyP);
1134 1135 1136
	  }
	} // fCCE>=0
	else {
1137
	  if (DCI_pdu->num_pdcch_symbols == 3) {
1138
	    allocation_is_feasible = 0;
1139 1140
	    LOG_I(MAC,"subframe %d: Dropping Allocation for RNTI %x\n",
		  subframeP,dci_alloc->rnti);
1141 1142 1143 1144 1145 1146 1147 1148 1149
	    for (j=0;j<=i;j++){
	     
	      LOG_I(MAC,"DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
		    i,DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci,
		    DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci,
		    DCI_pdu->dci_alloc[j].rnti,DCI_pdu->dci_alloc[j].format,
		    1<<DCI_pdu->dci_alloc[j].L,
		    DCI_pdu->nCCE,nCCE_max,DCI_pdu->num_pdcch_symbols);
	    }
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
	  else {
	    DCI_pdu->num_pdcch_symbols++;
	    nCCE_max = mac_xface->get_nCCE_max(module_idP,CC_idP,DCI_pdu->num_pdcch_symbols,subframeP);
	  }
	  break;
	} // fCCE==-1
      } // nCCE <= nCCE_max
    } // for i = 0 ... num_dcis  
    if (allocation_is_feasible==1)
      return (0);
  } // allocation_is_feasible == 1

  return(-1);
  

}

boolean_t CCE_allocation_infeasible(int module_idP,
				    int CC_idP,
				    int common_flag,
				    int subframe,
				    int aggregation,
				    int rnti) {


  DCI_PDU *DCI_pdu = &eNB_mac_inst[module_idP].common_channels[CC_idP].DCI_pdu;
  DCI_ALLOC_t *dci_alloc;
  int ret;
  boolean_t res=FALSE;

  if (common_flag==1) {
    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].L = aggregation;
    DCI_pdu->Num_common_dci++;
    ret = allocate_CCEs(module_idP,CC_idP,subframe,1);
    if (ret==-1)
      res = TRUE;
    DCI_pdu->Num_common_dci--;
  }
  else {
    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].L = aggregation;
    DCI_pdu->Num_ue_spec_dci++;
    ret = allocate_CCEs(module_idP,CC_idP,subframe,1);
    if (ret==-1)
1196
      res = TRUE;
1197 1198
    DCI_pdu->Num_ue_spec_dci--;
  }
1199 1200

  return res;
1201 1202
}