eNB_scheduler.c 54.7 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
/*! \file eNB_scheduler.c
30
 * \brief eNB scheduler top level function operates on per subframe basis
31 32
 * \author  Navid Nikaein and Raymond Knopp
 * \date 2010 - 2014
33
 * \email: navid.nikaein@eurecom.fr
34 35
 * \version 0.5
 * @ingroup _mac
36

37
 */
38

39
#include "assertions.h"
40 41 42 43 44 45 46 47
#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"
48

49 50 51 52 53 54
#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"
55

56 57
#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
58

59 60
//#include "LAYER2/MAC/pre_processor.c"
#include "pdcp.h"
61

62 63
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
64 65
#endif

66 67 68 69
#define ENABLE_MAC_PAYLOAD_DEBUG
#define DEBUG_eNB_SCHEDULER 1
//#define DEBUG_HEADER_PARSING 1
//#define DEBUG_PACKET_TRACE 1
70

71 72 73 74 75
/*
  #ifndef USER_MODE
  #define msg debug_msg
  #endif
 */
76 77 78



79

80

81
#if FAPI
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* global variables to deal with TPC command - we send one only once per frame maximum */
static long global_subframe;                        /* global subframe - incremented at each TTI */
static long ue_last_pucch_tpc[NUMBER_OF_UE_MAX];    /* last global subframe where a PUCCH TPC was sent to the UE */
static long ue_last_pusch_tpc[NUMBER_OF_UE_MAX];    /* last global subframe where a PUSCH TPC was sent to the UE */

static void fapi_dl_tpc(int module_id, int CC_id, struct DlDciListElement_s *dci)
{
  int              UE_id                = find_UE_id(module_id, dci->rnti);
  LTE_eNB_UE_stats *eNB_UE_stats        = NULL;
  int32_t          normalized_rx_power;
  int32_t          target_rx_power;
  static int       tpc_accumulated = 0;

  if (UE_id == -1) { printf("%s:%d: rnti %x not found\n", __FILE__, __LINE__, dci->rnti); abort(); }

  // this assumes accumulated tpc
  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
  if (global_subframe < ue_last_pucch_tpc[UE_id] + 10) {
printf("TPC PUCCH global sf %ld (%ld/%ld) UE %x: no TPC, last one is too recent (accumulated %d)\n", global_subframe, global_subframe/10, global_subframe%10, dci->rnti, tpc_accumulated);
    dci->tpc = 0;
    return;
  }

  eNB_UE_stats        = mac_xface->get_eNB_UE_stats(module_id, CC_id, dci->rnti);
  if (eNB_UE_stats == NULL) { printf("%s:%d: stats for rnti %x not found\n", __FILE__, __LINE__, dci->rnti); abort(); }
  normalized_rx_power = eNB_UE_stats->Po_PUCCH_dBm;
  target_rx_power     = mac_xface->get_target_pucch_rx_power(module_id, CC_id) + 20;

  if (eNB_UE_stats->Po_PUCCH_update == 1) {
    eNB_UE_stats->Po_PUCCH_update = 0;

    ue_last_pucch_tpc[UE_id] = global_subframe;

    if (normalized_rx_power > target_rx_power + 1) {
      dci->tpc = -1;
      tpc_accumulated--;
    } else if (normalized_rx_power < target_rx_power - 1) {
      dci->tpc = 1;
      tpc_accumulated++;
    } else {
      dci->tpc = 0;
    }
  }
printf("TPC PUCCH global sf %ld (%ld/%ld) UE %x: TPC %d (accumulated %d)\n", global_subframe, global_subframe/10, global_subframe%10, dci->rnti, dci->tpc, tpc_accumulated);
}

static void fapi_ul_tpc(int module_id, int CC_id, struct UlDciListElement_s *dci)
{
  int              UE_id                = find_UE_id(module_id, dci->rnti);
  LTE_eNB_UE_stats *eNB_UE_stats        = NULL;
  int32_t          normalized_rx_power;
  int32_t          target_rx_power;
  static int       tpc_accumulated = 0;

  if (UE_id == -1) { printf("%s:%d: rnti %x not found\n", __FILE__, __LINE__, dci->rnti); abort(); }

  // this assumes accumulated tpc
  // make sure that we are only sending a tpc update once a frame, otherwise the control loop will freak out
  if (global_subframe < ue_last_pusch_tpc[UE_id] + 10) {
printf("TPC PUSCH global sf %ld (%ld/%ld) UE %x: no TPC, last one is too recent (accumulated %d)\n", global_subframe, global_subframe/10, global_subframe%10, dci->rnti, tpc_accumulated);
    dci->tpc = 0;
    return;
  }

  eNB_UE_stats        = mac_xface->get_eNB_UE_stats(module_id, CC_id, dci->rnti);
  if (eNB_UE_stats == NULL) { printf("%s:%d: stats for rnti %x not found\n", __FILE__, __LINE__, dci->rnti); abort(); }
  normalized_rx_power = eNB_UE_stats->UL_rssi[0];
  target_rx_power     = mac_xface->get_target_pusch_rx_power(module_id, CC_id);

  if (normalized_rx_power > target_rx_power + 1) {
    dci->tpc = -1;
    tpc_accumulated--;
  } else if (normalized_rx_power < target_rx_power - 1) {
    dci->tpc = 1;
    tpc_accumulated++;
  } else {
    dci->tpc = 0;
  }

  if (dci->tpc != 0) {
    ue_last_pusch_tpc[UE_id] = global_subframe;
  }
printf("TPC PUSCH global sf %ld (%ld/%ld) UE %x: TPC %d (accumulated %d)\n", global_subframe, global_subframe/10, global_subframe%10, dci->rnti, dci->tpc, tpc_accumulated);
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/* this structure is used to store downlink ack/nack information
 * to be sent to FAPI by SchedDlTriggerReq
 */
/* TODO: lock access to it or not? */
/* TODO: do it per CC */
static struct {
  struct {
    int rnti;
    int harq_pid;
    int ack[MAX_TB_LIST];
    int ack_count;
  } ack[MAX_DL_INFO_LIST];
  int count;
} fapi_dl_ack_nack_data;

/* this array is used to store uplink ack/nack information
 * to be sent to FAPI by SchedUlTriggerReq
 * one element per TTI
 */
/* TODO: do it per CC */
static struct {
  struct {
190
    int reception_subframe;       /* the subframe where the ACK/NACK has been received */
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    int rnti;
    int ack;
    int length[MAX_LC_LIST+1];
  } ack[MAX_UL_INFO_LIST];
  int count;
} fapi_ul_ack_nack_data[10];

/* this function is called by the PHY to signal UE's ACK/NACK */
/* TODO: do it per CC */
void fapi_dl_ack_nack(int rnti, int harq_pid, int transport_block, int ack)
{
  int pos = fapi_dl_ack_nack_data.count;
printf("GOT DOWNLINK ack %d for rnti %x harq_pid %d transport_block %d\n", ack, rnti, harq_pid, transport_block);
  /* TODO: handle more than 1 TB */
  if (transport_block) { printf("%s:%d:%s: TODO: tb != 0\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  if (pos == MAX_DL_INFO_LIST) {
    LOG_E(MAC, "fapi_dl_ack_nack: full!\n");
    abort();
  }

  fapi_dl_ack_nack_data.ack[pos].rnti      = rnti;
  fapi_dl_ack_nack_data.ack[pos].harq_pid  = harq_pid;
  fapi_dl_ack_nack_data.ack[pos].ack[0]    = ack;       /* TODO: use transport_block here */
  fapi_dl_ack_nack_data.ack[pos].ack_count = 1;         /* TODO: take care of transport block */

  fapi_dl_ack_nack_data.count++;
}

/* this function is called by the PHY to inform about correct or wrong
 * reception by the eNodeB of an uplink UE transmission
 */
/* TODO: do it per CC */
void fapi_ul_ack_nack(int frame, int subframe, int rnti, int ack)
{
printf("GOT UPLINK ack %d for rnti %x (f/sf %d/%d)\n", ack, rnti, frame, subframe);
  int pos = fapi_ul_ack_nack_data[subframe].count;
  if (pos == MAX_UL_INFO_LIST) {
    LOG_E(MAC, "fapi_ul_ack_nack: full! (f/sf %d/%d)\n", frame, subframe);
    abort();
  }
232 233 234
  fapi_ul_ack_nack_data[subframe].ack[pos].reception_subframe = subframe;
  fapi_ul_ack_nack_data[subframe].ack[pos].rnti               = rnti;
  fapi_ul_ack_nack_data[subframe].ack[pos].ack                = ack;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

  /* the values in length are set later in the function fapi_ul_lc_length */
  memset(fapi_ul_ack_nack_data[subframe].ack[pos].length, 0, sizeof(int) * MAX_LC_LIST+1);

  fapi_ul_ack_nack_data[subframe].count++;
}

/* this function is called by rx_sdu to indicate the number of
 * bytes received by the given Logical Channel
 */
void fapi_ul_lc_length(int frame, int subframe, int lcid, int length, int rnti)
{
printf("GOT lcid %d length %d (f/sf %d/%d)\n", lcid, length, frame, subframe);
  int pos = fapi_ul_ack_nack_data[subframe].count - 1;
  if (pos < 0) { printf("%s:%d:%s: fatal error\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

Cedric Roux's avatar
typo  
Cedric Roux committed
251
  /* TODO: remove this check (and the rnti parameter)? */
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  if (rnti != fapi_ul_ack_nack_data[subframe].ack[pos].rnti) {
    printf("%s:%d:%s: fatal error: LCID %d wrong RNTI %x (expected %x)\n",
           __FILE__, __LINE__, __FUNCTION__, lcid,
           rnti, fapi_ul_ack_nack_data[subframe].ack[pos].rnti);
    abort();
  }

  if (lcid < 0 || lcid > 3)
    { printf("%s:%d:%s: fatal error: unhandled LCID %d\n", __FILE__, __LINE__, __FUNCTION__, lcid); abort(); }

  fapi_ul_ack_nack_data[subframe].ack[pos].length[lcid] = length;
}

static void fapi_convert_dl_1A_5MHz_FDD(struct DlDciListElement_s *dci, DCI_ALLOC_t *a)
{
  DCI1A_5MHz_FDD_t *d = (DCI1A_5MHz_FDD_t *)a->dci_pdu;

  if (dci->nr_of_tbs != 1) { printf("%s:%d: TODO\n", __FUNCTION__, __LINE__); exit(1); }

  d->type       = 1;    /* type = 0 => DCI Format 0, type = 1 => DCI Format 1A */
  d->vrb_type   = dci->vrbFormat == VRB_LOCALIZED ? 0 :
                  dci->vrbFormat == VRB_DISTRIBUTED ? 1 :
                  (printf("%s:%d: error\n", __FUNCTION__, __LINE__), abort(), 0);
  d->rballoc    = dci->rbBitmap;
  d->mcs        = dci->mcs[0];        /* TODO: take care of transport block index */
  d->harq_pid   = dci->harqProcess;
  d->ndi        = dci->ndi[0];        /* TODO: take care of transport block index */
  d->rv         = dci->rv[0];         /* TODO: take care of transport block index */
280 281 282 283 284
  d->TPC        = dci->tpc == -1 ? 0 :    /* see 36.213 table 5.1.2.1-1 */
                  dci->tpc ==  0 ? 1 :
                  dci->tpc ==  1 ? 2 :
                  dci->tpc ==  3 ? 3 :
                  (printf("%s:%d: error\n", __FUNCTION__, __LINE__), abort(), 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
  d->padding    = 0;

  a->dci_length = sizeof_DCI1A_5MHz_FDD_t;
  a->format     = format1A;
}

static uint32_t revert(uint32_t x, int len)
{
  int i;
  int ret = 0;
  for (i = 0; i < len; i++) {
    ret <<= 1;
    ret |= x & 1;
    x >>= 1;
  }
  return ret;
}

static void fapi_convert_dl_1_5MHz_FDD(struct DlDciListElement_s *dci, DCI_ALLOC_t *a)
{
  DCI1_5MHz_FDD_t *d = (DCI1_5MHz_FDD_t *)a->dci_pdu;

  if (dci->nr_of_tbs != 1) { printf("%s:%d: TODO\n", __FUNCTION__, __LINE__); exit(1); }

  d->rah        = dci->resAlloc == 0 ? 0 :
                  dci->resAlloc == 1 ? 1 :
                  (printf("%s:%d: error\n", __FUNCTION__, __LINE__), abort(), 0);
  d->rballoc    = revert(dci->rbBitmap, 13);
  d->mcs        = dci->mcs[0];        /* TODO: take care of transport block index */
  d->harq_pid   = dci->harqProcess;
  d->ndi        = dci->ndi[0];        /* TODO: take care of transport block index */
  d->rv         = dci->rv[0];         /* TODO: take care of transport block index */
317 318 319 320 321
  d->TPC        = dci->tpc == -1 ? 0 :    /* see 36.213 table 5.1.2.1-1 */
                  dci->tpc ==  0 ? 1 :
                  dci->tpc ==  1 ? 2 :
                  dci->tpc ==  3 ? 3 :
                  (printf("%s:%d: error (dci->tpc = %d)\n", __FUNCTION__, __LINE__, dci->tpc), abort(), 0);
322 323 324 325 326 327
  d->dummy      = 0;

  a->dci_length = sizeof_DCI1_5MHz_FDD_t;
  a->format     = format1;
}

328 329
static void fapi_convert_dl_dci(int module_id, int CC_id,
    struct DlDciListElement_s *dci, DCI_ALLOC_t *a)
330
{
331 332 333 334 335
  /* set TPC in the DCI */
  /* TODO: remove it if/when the scheduler does it */
  if (dci->rnti != 0)
    fapi_dl_tpc(module_id, CC_id, dci);

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
  /* 5MHz FDD supposed, not checked */
  switch (dci->format) {
  case ONE_A:
    fapi_convert_dl_1A_5MHz_FDD(dci, a);
    break;
  case ONE:
    fapi_convert_dl_1_5MHz_FDD(dci, a);
    break;
  default: printf("%s:%d: TODO\n", __FUNCTION__, __LINE__); abort();
  }
  a->L = dci->aggrLevel == 1 ? 0 :
         dci->aggrLevel == 2 ? 1 :
         dci->aggrLevel == 4 ? 2 :
         dci->aggrLevel == 8 ? 3 :
         (printf("%s:%d: error\n", __FUNCTION__, __LINE__), abort(), 0);
  a->firstCCE = dci->cceIndex;
  a->ra_flag  = 0;              /* TODO: set to 1 only by fapi_schedule_RAR, is it ok? */
  a->rnti     = dci->rnti;
}

static void fapi_convert_ul_5MHz_FDD(module_id_t module_idP, int CC_id,
    struct UlDciListElement_s *dci, DCI_ALLOC_t *a)
{
  DCI0_5MHz_FDD_t *d = (DCI0_5MHz_FDD_t *)a->dci_pdu;

  d->type       = 0;    /* type = 0 => DCI Format 0, type = 1 => DCI Format 1A */
  d->hopping    = dci->hopping;
  d->rballoc    = mac_xface->computeRIV(PHY_vars_eNB_g[module_idP][CC_id]->lte_frame_parms.N_RB_DL, dci->rbStart, dci->rbLen);
  d->mcs        = dci->mcs;
  d->ndi        = dci->ndi;
366 367 368 369 370
  d->TPC        = dci->tpc == -1 ? 0 :    /* see 36.213 table 5.1.1.1-2, accumulated case supposed */
                  dci->tpc ==  0 ? 1 :
                  dci->tpc ==  1 ? 2 :
                  dci->tpc ==  3 ? 3 :
                  (printf("%s:%d: error (tpc = %d)\n", __FUNCTION__, __LINE__, dci->tpc), abort(), 0);
371 372 373 374 375 376 377 378 379 380 381
  d->cshift     = dci->n2Dmrs;
  d->cqi_req    = dci->cqiRequest;
  d->padding    = 0;

  a->dci_length = sizeof_DCI0_5MHz_FDD_t;
  a->format     = format0;
}

static void fapi_convert_ul_dci(module_id_t module_idP, int CC_id,
    struct UlDciListElement_s *dci, DCI_ALLOC_t *a)
{
382 383 384 385
  /* set TPC in the DCI */
  /* TODO: remove it if/when the scheduler does it */
  fapi_ul_tpc(module_idP, CC_id, dci);

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  /* 5MHz FDD supposed, not checked */
  fapi_convert_ul_5MHz_FDD(module_idP, CC_id, dci, a);

  a->L = dci->aggrLevel == 1 ? 0 :
         dci->aggrLevel == 2 ? 1 :
         dci->aggrLevel == 4 ? 2 :
         dci->aggrLevel == 8 ? 3 :
         (printf("%s:%d: error\n", __FUNCTION__, __LINE__), abort(), 0);
  a->firstCCE = dci->cceIndex;
  a->ra_flag  = 0;
  a->rnti     = dci->rnti;
}

/* index 0 for SIB1, 1 for SIB23 */
static void fapi_schedule_SI(module_id_t module_idP, int CC_id, frame_t frameP,
    sub_frame_t subframeP, uint8_t index, struct DlDciListElement_s *dci)
{
  eNB_MAC_INST *eNB = &eNB_mac_inst[module_idP];
  DCI_PDU *dci_pdu = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
  DCI_ALLOC_t *a = &dci_pdu->dci_alloc[dci_pdu->Num_common_dci];
  int bcch_sdu_length;

  if (dci_pdu->Num_common_dci >= NUM_DCI_MAX) { printf("%s:%d:%s: too much DCIs\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  /* we can increment Num_common_dci or Num_ue_spec_dci, there is no difference */
  dci_pdu->Num_common_dci++;

413
  fapi_convert_dl_dci(module_idP, CC_id, dci, a);
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

  /* bug in libscheduler.a? rnti is 0 */
  a->rnti = SI_RNTI;

  if (index == 0)
    bcch_sdu_length = mac_rrc_get_SIB1(module_idP, CC_id, &eNB->common_channels[CC_id].BCCH_pdu.payload[0]);
  else if (index == 1)
    bcch_sdu_length = mac_rrc_get_SIB23(module_idP, CC_id, &eNB->common_channels[CC_id].BCCH_pdu.payload[0]);
  else { printf("%s:%d: fatal error\n", __FUNCTION__, __LINE__); abort(); }

  eNB->eNB_stats[CC_id].total_num_bcch_pdu+=1;
  eNB->eNB_stats[CC_id].bcch_buffer=bcch_sdu_length;
  eNB->eNB_stats[CC_id].total_bcch_buffer+=bcch_sdu_length;
  eNB->eNB_stats[CC_id].bcch_mcs=dci->mcs[0];         /* TODO: take care of transport block index */
}

static void fapi_schedule_RAR(int module_idP, int CC_id, frame_t frameP,
    sub_frame_t subframeP, uint16_t rnti, uint32_t grant,
    struct DlDciListElement_s *dci)
{
  eNB_MAC_INST *eNB = &eNB_mac_inst[module_idP];
  DCI_PDU *dci_pdu = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
  DCI_ALLOC_t *a = &dci_pdu->dci_alloc[dci_pdu->Num_common_dci];
  RA_TEMPLATE *RA_template = NULL;
  int i;

  if (dci_pdu->Num_common_dci >= NUM_DCI_MAX) { printf("%s:%d:%s: too much DCIs\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  for (i = 0; i < NB_RA_PROC_MAX; i++) {
    RA_template = (RA_TEMPLATE *)&eNB->common_channels[CC_id].RA_template[i];
    if (RA_template->RA_active != TRUE) continue;
    if (RA_template->rnti != rnti) continue;
    break;
  }
  if (i == NB_RA_PROC_MAX) { printf("%s:%d:%s: possible?\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  RA_template->generate_rar = 1;
  RA_template->UL_grant = grant;

  /* we can increment Num_common_dci or Num_ue_spec_dci, there is no difference */
  dci_pdu->Num_common_dci++;

456
  fapi_convert_dl_dci(module_idP, CC_id, dci, a);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  a->ra_flag = 1;

  /* bug in libscheduler.a? rnti is 0 */
  a->rnti = RA_template->RA_rnti;
}

static void fapi_schedule_RA_Msg4(int module_idP, int CC_id, int RA,
    int frameP, int subframeP, struct DlDciListElement_s *dci, int pdu_maxsize)
{
  int           UE_id;
  int           rrc_sdu_length;
  eNB_MAC_INST  *eNB = &eNB_mac_inst[module_idP];
  RA_TEMPLATE   *RA_template;
  int           msg4_header_length;
  int           msg4_padding, msg4_post_padding;
  int           TBsize;
  DCI_PDU       *dci_pdu = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
  DCI_ALLOC_t   *a = &dci_pdu->dci_alloc[dci_pdu->Num_common_dci];
  int           offset;
  unsigned char lcid;

  if (dci_pdu->Num_common_dci >= NUM_DCI_MAX) { printf("%s:%d:%s: too much DCIs\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  RA_template = (RA_TEMPLATE *)&eNB->common_channels[CC_id].RA_template[RA];
  UE_id = find_UE_id(module_idP,RA_template->rnti);
  rrc_sdu_length = mac_rrc_data_req(module_idP,
                                    CC_id,
                                    frameP,
                                    CCCH,
                                    1, // 1 transport block
                                    &eNB->common_channels[CC_id].CCCH_pdu.payload[0],
                                    ENB_FLAG_YES,
                                    module_idP,
                                    0); // not used in this case
  if (rrc_sdu_length <= 0) {
    LOG_E(MAC, "fapi_schedule_RA_Msg4: rrc_sdu_length (%d) is not > 0\n", rrc_sdu_length);
    abort();
  }
  if (rrc_sdu_length > pdu_maxsize) {
    LOG_E(MAC, "fapi_schedule_RA_Msg4: rrc_sdu_length (%d) > pdu_maxsize (%d)\n", rrc_sdu_length, pdu_maxsize);
    abort();
  }

  msg4_header_length = 1+6+1;  // CR header, CR CE, SDU header

  TBsize = dci->tbsSize[CC_id] / 8;

  if ((TBsize - rrc_sdu_length - msg4_header_length) <= 2) {
    msg4_padding = TBsize - rrc_sdu_length - msg4_header_length;
    msg4_post_padding = 0;
  } else {
    msg4_padding = 0;
    msg4_post_padding = TBsize - rrc_sdu_length - msg4_header_length -1;
  }

  LOG_I(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
        module_idP, CC_id, frameP, subframeP, TBsize, rrc_sdu_length, msg4_header_length, msg4_padding, msg4_post_padding);

  lcid = 0;
  offset = generate_dlsch_header((unsigned char*)eNB->UE_list.DLSCH_pdu[CC_id][0][(unsigned char)UE_id].payload[0],
                                 1,                           //num_sdus
                                 (unsigned short*)&rrc_sdu_length,             //
                                 &lcid,                       // sdu_lcid
                                 255,                         // no drx
                                 0,                           // no timing advance
                                 RA_template->cont_res_id,  // contention res id
                                 msg4_padding,                // no padding
                                 msg4_post_padding);

  memcpy((void*)&eNB->UE_list.DLSCH_pdu[CC_id][0][UE_id].payload[0][offset],
         &eNB->common_channels[CC_id].CCCH_pdu.payload[0],
         rrc_sdu_length);

  RA_template->generate_Msg4=0;
  RA_template->wait_ack_Msg4=1;
  RA_template->RA_active = FALSE;

  /* we can increment Num_common_dci or Num_ue_spec_dci, there is no difference */
  dci_pdu->Num_common_dci++;

537
  fapi_convert_dl_dci(module_idP, CC_id, dci, a);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
}

/* returns 1 if the given LCID has a MAC header of fixed size (for DL-SCH) */
static int fixed_size(int lcid)
{
  /* see 36.321, especially table 6.2.1-1 (rel. 10 is used here) */
  return lcid >= 27;
}

/* TODO: deal with more than one transport block */
static void fapi_schedule_ue(int module_id, int CC_id, int frame, int subframe, struct BuildDataListElement_s *d)
{
  int header_size;
  int payload_size;
  int padding_size;
  int tbs;
  int i;
  mac_rlc_status_resp_t rlc_status;
  unsigned char         dlsch_buffer[MAX_DLSCH_PAYLOAD_BYTES];
  int                   dlsch_filled = 0;
  int                   output_length;
  int                   UE_id;
  eNB_MAC_INST          *eNB      = &eNB_mac_inst[module_id];
  UE_list_t             *UE_list  = &eNB->UE_list;
  int                   num_sdus;
  unsigned short        sdu_lengths[MAX_RLC_PDU_LIST];
  unsigned char         sdu_lcids[MAX_RLC_PDU_LIST];
  int                   offset;
  DCI_PDU               *dci_pdu = &eNB->common_channels[CC_id].DCI_pdu;
  DCI_ALLOC_t           *a = &dci_pdu->dci_alloc[dci_pdu->Num_common_dci];

  /* generate DCI */
  if (dci_pdu->Num_common_dci >= NUM_DCI_MAX) { printf("%s:%d:%s: too much DCIs\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  /* we can increment Num_common_dci or Num_ue_spec_dci, there is no difference */
  dci_pdu->Num_common_dci++;

575
  fapi_convert_dl_dci(module_id, CC_id, &d->dci, a);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
printf("RUN fapi_schedule_ue\n");

  if (d->nr_rlcPDU_List[0] != 1) { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
  if (d->nr_rlcPDU_List[1] != 0) { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
  if (d->ceBitmap[0])            { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
  if (d->ceBitmap[1])            { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
  if (d->servCellIndex != 0)     { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  tbs = d->dci.tbsSize[0] / 8;

  num_sdus = 0;

  /* get DLSCH buffer and adjust size according to what RLC says */
  for (i = 0; i < d->nr_rlcPDU_List[0]; i++) {
    rlc_status = mac_rlc_status_ind(
        module_id,
        d->rnti,
        module_id,
        frame,
        ENB_FLAG_YES,
        MBMS_FLAG_NO,
        d->rlcPduList[0][i].logicalChannelIdentity,
        d->rlcPduList[0][i].size);
printf("RLC_SIZE in fapi_schedule_ue %d (asked %d) lcid %d rnti %x f/sf %d/%d\n", rlc_status.bytes_in_buffer, d->rlcPduList[0][i].size, d->rlcPduList[0][i].logicalChannelIdentity, d->rnti, frame, subframe);
    if (rlc_status.bytes_in_buffer <= 0) { printf("%s:%d:%s: error\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
    if (rlc_status.bytes_in_buffer > d->rlcPduList[0][i].size) abort(); /* that can't happen */
    if (dlsch_filled + d->rlcPduList[0][i].size > MAX_DLSCH_PAYLOAD_BYTES) {
      printf("dlsch buffer filled too much\n");
      abort();
    }
    output_length = mac_rlc_data_req(
        module_id,
        d->rnti,
        module_id,
        frame,
        ENB_FLAG_YES,
        MBMS_FLAG_NO,
        d->rlcPduList[0][i].logicalChannelIdentity,
        (char *)&dlsch_buffer[dlsch_filled]);
    if (output_length <= 0 || output_length > d->rlcPduList[0][i].size) abort();
    d->rlcPduList[0][i].size = output_length;
    dlsch_filled += output_length;
    sdu_lengths[num_sdus] = output_length;
    sdu_lcids[num_sdus]   = d->rlcPduList[0][i].logicalChannelIdentity;
    num_sdus++;
printf("FILLED %d bytes\n", output_length);
  }

  /* get size of header and payload */
  header_size = 0;
  payload_size = 0;

  if (d->nr_rlcPDU_List[0]) {
    header_size++;
    payload_size += d->rlcPduList[0][0].size;
  }
  for (i = 1; i < d->nr_rlcPDU_List[0]; i++) {
    if (!fixed_size(d->rlcPduList[0][i-1].logicalChannelIdentity)) {
      header_size++;
      if (d->rlcPduList[0][i-1].size >= 128)
        header_size++;
    }
    header_size++;
    payload_size += d->rlcPduList[0][i].size;
  }

  padding_size = tbs - header_size - payload_size;
printf("PADDING_SIZE %d\n", padding_size);

  UE_id = find_UE_id(module_id, d->rnti);

  /* generate dlsch header */
  offset = generate_dlsch_header((unsigned char*)UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0],
      num_sdus,
      sdu_lengths,
      sdu_lcids,
      255,                                   // no drx
      0,                                     /* TODO: timing advance */
      NULL,                                  // contention res id
      padding_size <= 2 ? padding_size : 0,
      padding_size > 2 ? padding_size : 0);

  /* fill payload */
  memcpy(&UE_list->DLSCH_pdu[CC_id][0][UE_id].payload[0][offset], dlsch_buffer, dlsch_filled);

  add_ue_dlsch_info(module_id,
      CC_id,
      UE_id,
      subframe,
      /* S_DL_SCHEDULED */ S_DL_WAITING);
}

static void fapi_schedule_uplink(int module_idP, int CC_id, struct UlDciListElement_s *dci)
{
  DCI_PDU       *dci_pdu = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
  DCI_ALLOC_t   *a = &dci_pdu->dci_alloc[dci_pdu->Num_common_dci];

  if (dci_pdu->Num_common_dci >= NUM_DCI_MAX) { printf("%s:%d:%s: too much DCIs\n", __FILE__, __LINE__, __FUNCTION__); abort(); }

  /* we can increment Num_common_dci or Num_ue_spec_dci, there is no difference */
  dci_pdu->Num_common_dci++;

  fapi_convert_ul_dci(module_idP, CC_id, dci, a);
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694
char *binary(unsigned x)
{
  static char r[33];
  char *s = r+31;
  int i;
  r[32] = 0;
  for (i = 0; i < 32; i++) {
    *s = '0' + (x&1);
    s--;
    x >>= 1;
  }
  return r;
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
void eNB_dlsch_ulsch_scheduler(module_id_t module_idP,uint8_t cooperation_flag, frame_t frameP, sub_frame_t subframeP)  //, int calibration_flag) {
{
  int                                 CC_id;
  int                                 i, j;
  DCI_PDU                             *DCI_pdu[MAX_NUM_CCs];
  int                                 mbsfn_status[MAX_NUM_CCs];
  UE_list_t                           *UE_list = &eNB_mac_inst[module_idP].UE_list;
  fapi_interface_t                    *fapi;
  struct SchedDlTriggerReqParameters  dlreq;
  struct SchedDlConfigIndParameters   dlind;
  struct DlInfoListElement_s          dlinfo[MAX_DL_INFO_LIST];
  protocol_ctxt_t                     ctxt;
  int                                 UE_id;
  eNB_MAC_INST                        *eNB = &eNB_mac_inst[module_idP];
  struct SchedUlTriggerReqParameters  ulreq;
  struct SchedUlConfigIndParameters   ulind;
  struct UlInfoListElement_s          ulinfo[MAX_UL_INFO_LIST];
  int                                 ulsf;

714
printf("SCHEDULER called for f/sf %d/%d\n", frameP, subframeP);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
#if defined(ENABLE_ITTI)
  while (1) {
    // Checks if a message has been sent to MAC sub-task
    MessageDef   *msg_p;
    int           result;
    itti_poll_msg (TASK_MAC_ENB, &msg_p);
    if (msg_p == NULL) break;
    result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
  }
#endif

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    DCI_pdu[CC_id] = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
    DCI_pdu[CC_id]->nCCE=0;
    DCI_pdu[CC_id]->num_pdcch_symbols=1;
    mbsfn_status[CC_id]=0;
    // clear vrb_map
    memset(eNB_mac_inst[module_idP].common_channels[CC_id].vrb_map,0,100);
  }

  // refresh UE list based on UEs dropped by PHY in previous subframe
  i = UE_list->head;
  while (i>=0) {
    int rnti, next_i;
    rnti = UE_RNTI(module_idP, i);
    CC_id = UE_PCCID(module_idP, i);
    next_i= UE_list->next[i];
    if (mac_xface->get_eNB_UE_stats(module_idP, CC_id, rnti)==NULL) {
      mac_remove_ue(module_idP, i, frameP, subframeP);
    }
    i = next_i;
  }

  // clear DCI and BCCH contents before scheduling
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    DCI_pdu[CC_id]->Num_common_dci  = 0;
    DCI_pdu[CC_id]->Num_ue_spec_dci = 0;
#ifdef Rel10
    eNB_mac_inst[module_idP].common_channels[CC_id].mcch_active = 0;
#endif
    eNB_mac_inst[module_idP].frame    = frameP;
    eNB_mac_inst[module_idP].subframe = subframeP;
  }

  /* run PDCP */
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES, NOT_A_RNTI, frameP, 0, module_idP);
  pdcp_run(&ctxt);

  // check HO
  rrc_rx_tx(&ctxt,
            0, // eNB index, unused in eNB
            CC_id);

  /* MBMS thing not done */
  mbsfn_status[0]++; /* avoid gcc warning */

  /* let's FAPI schedule! */
  fapi = eNB_mac_inst[module_idP].fapi;

  /*************************************************/
  /*           downlink scheduling                 */
  /*************************************************/

  /* check RA - is there one in generate_Msg4 mode with some CCCH data ready in the RRC Srb0? */
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    for (i=0; i < NB_RA_PROC_MAX; i++) {
      if (eNB->common_channels[CC_id].RA_template[i].RA_active == TRUE &&
          eNB->common_channels[CC_id].RA_template[i].generate_Msg4 == 1 &&
          mac_rrc_get_ccch_size(module_idP, CC_id) > 0) {
        struct SchedDlRlcBufferReqParameters rlc;
        memset(&rlc, 0, sizeof(rlc));
        rlc.rnti = eNB->common_channels[CC_id].RA_template[i].rnti;
        rlc.logicalChannelIdentity = CCCH;
        rlc.rlcTransmissionQueueSize = mac_rrc_get_ccch_size(module_idP, CC_id);
        LOG_I(MAC, "calling SchedDlRlcBufferReq on CCCH rnti %x queue_size %d\n", rlc.rnti, rlc.rlcTransmissionQueueSize);
791
printf("MAC to FAPI downlink BUF CCCH %d\n", rlc.rlcTransmissionQueueSize);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
        SchedDlRlcBufferReq(fapi->sched, &rlc);
        /* let's report only once, so put generate_Msg4 to 2
         * (we will generate later on, when FAPI tells us to do so)
         */
        eNB->common_channels[CC_id].RA_template[i].generate_Msg4 = 2;
      }
    }
  }

  /* update RLC buffers status in the scheduler */
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
    struct SchedDlRlcBufferReqParameters rlc;
    mac_rlc_status_resp_t                rlc_status;

    memset(&rlc, 0, sizeof(rlc));
    rlc.rnti = UE_RNTI(module_idP, UE_id);

    /* this code is wrong: we should loop over all configured RaB */

    /* DCCH   (srb 1, lcid 1) */
    rlc_status = mac_rlc_get_buffer_occupancy(module_idP, rlc.rnti, frameP, DCCH);
    rlc.logicalChannelIdentity = DCCH;
    rlc.rlcTransmissionQueueSize = rlc_status.bytes_in_buffer;
    LOG_I(MAC, "calling SchedDlRlcBufferReq on DCCH rnti %x queue_size %d\n", rlc.rnti, rlc_status.bytes_in_buffer);
    SchedDlRlcBufferReq(fapi->sched, &rlc);
817
printf("MAC to FAPI downlink BUF DCCH %d\n", rlc_status.bytes_in_buffer);
818 819 820 821 822 823 824

    /* DCCH+1 (srb 2, lcid 2) */
    rlc_status = mac_rlc_get_buffer_occupancy(module_idP, rlc.rnti, frameP, DCCH+1);
    rlc.logicalChannelIdentity = DCCH+1;
    rlc.rlcTransmissionQueueSize = rlc_status.bytes_in_buffer;
    LOG_I(MAC, "calling SchedDlRlcBufferReq on DCCH+1 rnti %x queue_size %d\n", rlc.rnti, rlc_status.bytes_in_buffer);
    SchedDlRlcBufferReq(fapi->sched, &rlc);
825
printf("MAC to FAPI downlink BUF DCCH+1 %d\n", rlc_status.bytes_in_buffer);
826 827 828 829 830 831 832

    /* DTCH   (drb 1, lcid 3) */
    rlc_status = mac_rlc_get_buffer_occupancy(module_idP, rlc.rnti, frameP, DTCH);
    rlc.logicalChannelIdentity = DTCH;
    rlc.rlcTransmissionQueueSize = rlc_status.bytes_in_buffer;
    LOG_I(MAC, "calling SchedDlRlcBufferReq on DTCH rnti %x queue_size %d\n", rlc.rnti, rlc_status.bytes_in_buffer);
    SchedDlRlcBufferReq(fapi->sched, &rlc);
833
printf("MAC to FAPI downlink BUF DTCH %d\n", rlc_status.bytes_in_buffer);
834 835 836 837 838 839 840 841 842 843 844 845 846
  }

  dlreq.sfnSf                 = frameP * 16 + subframeP;
  dlreq.nr_dlInfoList         = 0;
  dlreq.dlInfoList            = NULL;
  dlreq.nr_vendorSpecificList = 0;
  dlreq.vendorSpecificList    = NULL;

  /* fill harq feedback data */
  /* TODO: deal with more than one TB */
  for (i = 0; i < fapi_dl_ack_nack_data.count; i++) {
    dlinfo[i].rnti          = fapi_dl_ack_nack_data.ack[i].rnti;
    dlinfo[i].harqProcessId = fapi_dl_ack_nack_data.ack[i].harq_pid;
847 848 849 850
    dlinfo[i].nr_harqStatus = 1;                                                         /* TODO: deal with more than 1 TB */
    dlinfo[i].harqStatus[0] = fapi_dl_ack_nack_data.ack[i].ack[0] ? ff_ACK : ff_NACK;    /* TODO: more than 1 TB */
    dlinfo[i].servCellIndex = 0;                                                         /* TODO: get real value for the servCellIndex */
printf("MAC to FAPI downlink ack/nack from PHY f/sf %d/%d rnti %x harq %d ack %d\n", frameP, subframeP, dlinfo[i].rnti, dlinfo[i].harqProcessId, fapi_dl_ack_nack_data.ack[i].ack[0]);
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  }
  if (fapi_dl_ack_nack_data.count) {
    dlreq.nr_dlInfoList = fapi_dl_ack_nack_data.count;
    dlreq.dlInfoList    = dlinfo;
  }
  fapi_dl_ack_nack_data.count = 0;

  LOG_I(MAC, "calling SchedDlTriggerReq\n");
  SchedDlTriggerReq(fapi->sched, &dlreq);

  LOG_I(MAC, "calling SchedDlConfigInd\n");
  SchedDlConfigInd(fapi, &dlind);

  LOG_I(MAC, "SchedDlConfigInd returns dlind.nr_buildDataList %d f/sf %d/%d\n", dlind.nr_buildDataList, frameP, subframeP);
  LOG_I(MAC, "SchedDlConfigInd returns dlind.nr_buildRARList %d f/sf %d/%d\n", dlind.nr_buildRARList, frameP, subframeP);
  LOG_I(MAC, "SchedDlConfigInd returns dlind.nr_buildBroadcastList %d f/sf %d/%d\n", dlind.nr_buildBroadcastList, frameP, subframeP);

  /* TODO: rewrite. All should go into fapi_schedule_ue where special cases should be handled */
  for (i = 0; i < dlind.nr_buildDataList; i++) {
    if (dlind.buildDataList[i].ceBitmap[1] != 0 || dlind.buildDataList[i].nr_rlcPDU_List[1] != 0) { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
    if (dlind.buildDataList[i].nr_rlcPDU_List[0] != 1) { printf("%s:%d:%s: TODO\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
872
printf("FAPI to MAC downlink schedule ue %x channel %d f/sf %d/%d\n", dlind.buildDataList[i].rnti, dlind.buildDataList[i].rlcPduList[0][0].logicalChannelIdentity, frameP, subframeP);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    switch (dlind.buildDataList[i].rlcPduList[0][0].logicalChannelIdentity) {
    case 0: /* CCCH */
      /* TODO: get the right CC_id from servCellIndex, depending on the UE rnti/pcell/scell settings */
      CC_id = dlind.buildDataList[i].servCellIndex;
      /* look for an active RA with generate_Msg4 == 2 for this rnti */
      for (j=0; j < NB_RA_PROC_MAX; j++) {
        if (eNB->common_channels[CC_id].RA_template[j].RA_active == TRUE &&
            eNB->common_channels[CC_id].RA_template[j].generate_Msg4 == 2 &&
            eNB->common_channels[CC_id].RA_template[j].rnti == dlind.buildDataList[i].rnti) {
          if (dlind.buildDataList[i].ceBitmap[CC_id] != ff_CR) { printf("%s:%d:%s: TODO)\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
          fapi_schedule_RA_Msg4(module_idP, CC_id, j, frameP, subframeP,
              &dlind.buildDataList[i].dci, dlind.buildDataList[i].rlcPduList[0][0].size);
          break;
        }
      }
      if (j == NB_RA_PROC_MAX) { printf("%s:%d:%s: possible?\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
      break;
    case 1: /* DCCH   (SRB1) */
    case 2: /* DCCH+1 (SRB2) */
    case 3: /* DTCH   (DRB1) */
      /* TODO: get the correct CC_id from servCellIndex */
      fapi_schedule_ue(module_idP, dlind.buildDataList[i].servCellIndex /* CC_id */, frameP, subframeP, &dlind.buildDataList[i]);
      break;
    default: printf("%s:%d:%s: bad channel ID (%d)\n", __FILE__, __LINE__, __FUNCTION__, dlind.buildDataList[i].rlcPduList[0][0].logicalChannelIdentity); abort();
    }
  }

  if (dlind.nr_buildRARList) {
    if (dlind.nr_buildRARList != 1) { printf("%s:%d: more than 1 RAR, todo\n", __FUNCTION__, __LINE__); exit(0); }
    if (dlind.buildRarList[0].carrierIndex != 0) { printf("%s:%d: 2nd CC: todo properly\n", __FUNCTION__, __LINE__); exit(0); }
    /* force bit 0 of grant to 0
     * according to 36.213 6.2 the value
     * is reserved but openair needs it to be 0
     * this is for contention-based RA
     * maybe we will need to review that if/when we do contention-free RA
     */
909
printf("FAPI to MAC downlink schedule RAR ue %x f/sf %d/%d\n", dlind.buildRarList[0].rnti, frameP, subframeP);
910 911 912 913 914 915 916 917 918
    dlind.buildRarList[0].grant &= ~1;
    fapi_schedule_RAR(module_idP, dlind.buildRarList[0].carrierIndex, frameP, subframeP,
        dlind.buildRarList[0].rnti, dlind.buildRarList[0].grant, &dlind.buildRarList[0].dci);
  }

  if (dlind.nr_buildBroadcastList) {
    if (dlind.nr_buildBroadcastList != 1) { printf("%s:%d: more than 1 broadcast SI, todo\n", __FUNCTION__, __LINE__); exit(0); }
    if (dlind.buildBroadcastList[0].type == ff_PCCH) { printf("%s:%d: PCCH: todo\n", __FUNCTION__, __LINE__); exit(0); }
    if (dlind.buildBroadcastList[0].carrierIndex != 0) { printf("%s:%d: 2nd CC: todo properly\n", __FUNCTION__, __LINE__); exit(0); }
919
printf("FAPI to MAC downlink schedule SI %d f/sf %d/%d\n", dlind.buildBroadcastList[0].index, frameP, subframeP);
920 921 922 923 924 925 926 927 928 929 930 931
    fapi_schedule_SI(module_idP, dlind.buildBroadcastList[0].carrierIndex, frameP, subframeP,
        dlind.buildBroadcastList[0].index,
        &dlind.buildBroadcastList[0].dci);
  }

  /* TODO: do it correctly */
  if (dlind.nr_ofdmSymbolsCount != 0) {
    if (dlind.nr_ofdmSymbolsCount != 1) { printf("%s:%d:%s: what to do?\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
      if (dlind.nrOfPdcchOfdmSymbols[CC_id] != NULL) {
        int cc = dlind.nrOfPdcchOfdmSymbols[CC_id]->carrierIndex;
        DCI_pdu[cc]->num_pdcch_symbols = dlind.nrOfPdcchOfdmSymbols[CC_id]->pdcchOfdmSymbolCount;
932
printf("FAPI to MAC downlink DCI_pdu[%d]->num_pdcch_symbols %d f/sf %d/%d\n", cc, DCI_pdu[cc]->num_pdcch_symbols, frameP, subframeP);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
      }
    }
  }

#if 0
  // Allocate CCEs for good after scheduling is done
  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++)
    allocate_CCEs(module_idP,CC_id,subframeP,0);
#endif

  /*************************************************/
  /*            uplink scheduling                  */
  /*************************************************/

  ulreq.sfnSf         = frameP * 16 + subframeP;
  ulreq.nr_ulInfoList = 0;
  ulreq.ulInfoList    = NULL;
  ulreq.nr_vendorSpecificList = 0;
  ulreq.vendorSpecificList    = NULL;

  /* fill ulInfoList */
954
  ulsf = (subframeP + 10 - 3) % 10;
955 956 957 958
  if (fapi_ul_ack_nack_data[ulsf].count) {
    ulreq.nr_ulInfoList = fapi_ul_ack_nack_data[ulsf].count;
    ulreq.ulInfoList    = ulinfo;
    for (i = 0; i < ulreq.nr_ulInfoList; i++) {
959 960 961 962 963 964
printf("MAC to FAPI uplink acknack ue %x f/sf %d/%d ulsf %d [reception_subframe %d] ack %d\n", fapi_ul_ack_nack_data[ulsf].ack[i].rnti, frameP, subframeP, ulsf, fapi_ul_ack_nack_data[ulsf].ack[i].reception_subframe, fapi_ul_ack_nack_data[ulsf].ack[i].ack);
      ulinfo[i].puschTransmissionTimestamp = fapi_ul_ack_nack_data[ulsf].ack[i].reception_subframe;
      ulinfo[i].rnti                       = fapi_ul_ack_nack_data[ulsf].ack[i].rnti;
      ulinfo[i].receptionStatus            = fapi_ul_ack_nack_data[ulsf].ack[i].ack == 1 ? Ok : NotOk;
      ulinfo[i].tpc                        = 0;           /* TODO */
      ulinfo[i].servCellIndex              = 0;           /* TODO: get correct value */
965
      for (j = 0; j < MAX_LC_LIST+1; j++) {
966
        ulinfo[i].ulReception[j] = fapi_ul_ack_nack_data[ulsf].ack[i].length[j];
967
printf("MAC to FAPI uplink ue %x f/sf %d/%d lcid %d size acked %d\n", fapi_ul_ack_nack_data[ulsf].ack[i].rnti, frameP, subframeP, j, fapi_ul_ack_nack_data[ulsf].ack[i].length[j]);
968
}
969
    }
970
    fapi_ul_ack_nack_data[ulsf].count = 0;
971 972 973 974 975 976 977 978
  }

  LOG_I(MAC, "calling SchedUlTriggerReq\n");
  SchedUlTriggerReq(fapi->sched, &ulreq);

  LOG_I(MAC, "calling SchedUlConfigInd\n");
  SchedUlConfigInd(fapi, &ulind);

979
printf("FAPI to MAC uplink nr_dclList %d nr_phichList %d\n", ulind.nr_dciList, ulind.nr_phichList);
980 981 982
  for (i = 0; i < ulind.nr_dciList; i++) {
    /* TODO: get the right CC_id from servCellIndex, depending on the UE rnti/pcell/scell settings */
    CC_id = ulind.dciList[i].servCellIndex;
983
printf("FAPI to MAC uplink schedule ue %x ndi %d (fsf %d %d)\n", ulind.dciList[i].rnti, ulind.dciList[i].ndi, frameP, subframeP);
984 985
    fapi_schedule_uplink(module_idP, CC_id, &ulind.dciList[i]);
  }
986 987 988 989 990 991 992 993 994

printf("RECAP dci pdu count %d\n", eNB_mac_inst[0].common_channels[0].DCI_pdu.Num_common_dci);
for (i = 0; i < eNB_mac_inst[0].common_channels[0].DCI_pdu.Num_common_dci; i++) {
printf("    RECAP %i rnti %x format %d dci pdu %s\n", i,
  eNB_mac_inst[0].common_channels[0].DCI_pdu.dci_alloc[i].rnti,
  eNB_mac_inst[0].common_channels[0].DCI_pdu.dci_alloc[i].format,
  binary(*(uint32_t *)eNB_mac_inst[0].common_channels[0].DCI_pdu.dci_alloc[i].dci_pdu)
  );
}
995 996

  global_subframe++;
997 998 999
}

#else /* FAPI */
1000

1001 1002
void eNB_dlsch_ulsch_scheduler(module_id_t module_idP,uint8_t cooperation_flag, frame_t frameP, sub_frame_t subframeP)  //, int calibration_flag) {
{
1003

Raymond Knopp's avatar
 
Raymond Knopp committed
1004
  int mbsfn_status[MAX_NUM_CCs];
1005
  protocol_ctxt_t   ctxt;
1006
#ifdef EXMIMO
1007
  int ret;
1008
#endif
1009
#if defined(ENABLE_ITTI)
1010 1011 1012 1013
  MessageDef   *msg_p;
  const char   *msg_name;
  instance_t    instance;
  int           result;
1014
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1015
  DCI_PDU *DCI_pdu[MAX_NUM_CCs];
Raymond Knopp's avatar
 
Raymond Knopp committed
1016
  int CC_id,i,next_i;
Raymond Knopp's avatar
 
Raymond Knopp committed
1017
  UE_list_t *UE_list=&eNB_mac_inst[module_idP].UE_list;
1018
  rnti_t rnti;
1019

1020
  LOG_D(MAC,"[eNB %d] Frame %d, Subframe %d, entering MAC scheduler (UE_list->head %d)\n",module_idP, frameP, subframeP,UE_list->head);
1021

1022
  start_meas(&eNB_mac_inst[module_idP].eNB_scheduler);
1023
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_IN);
1024

1025
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1026
    DCI_pdu[CC_id] = &eNB_mac_inst[module_idP].common_channels[CC_id].DCI_pdu;
1027 1028
    DCI_pdu[CC_id]->nCCE=0;
    DCI_pdu[CC_id]->num_pdcch_symbols=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1029
    mbsfn_status[CC_id]=0;
1030 1031
    // clear vrb_map
    memset(eNB_mac_inst[module_idP].common_channels[CC_id].vrb_map,0,100);
Raymond Knopp's avatar
 
Raymond Knopp committed
1032
  }
1033

Raymond Knopp's avatar
 
Raymond Knopp committed
1034
  // refresh UE list based on UEs dropped by PHY in previous subframe
1035 1036 1037
  i = UE_list->head;

  while (i>=0) {
1038 1039 1040 1041
    rnti = UE_RNTI(module_idP, i);
    CC_id = UE_PCCID(module_idP, i);
    LOG_D(MAC,"UE %d: rnti %x (%p)\n", i, rnti,
          mac_xface->get_eNB_UE_stats(module_idP, CC_id, rnti));
1042
    next_i= UE_list->next[i];
1043

1044 1045
    if (mac_xface->get_eNB_UE_stats(module_idP, CC_id, rnti)==NULL) {
      mac_remove_ue(module_idP, i, frameP, subframeP);
1046 1047
    }
    i = next_i;
Raymond Knopp's avatar
 
Raymond Knopp committed
1048
  }
1049

1050
#if defined(ENABLE_ITTI)
1051

1052
  do {
1053 1054
    // Checks if a message has been sent to MAC sub-task
    itti_poll_msg (TASK_MAC_ENB, &msg_p);
1055

1056 1057 1058
    if (msg_p != NULL) {
      msg_name = ITTI_MSG_NAME (msg_p);
      instance = ITTI_MSG_INSTANCE (msg_p);
1059

1060 1061 1062 1063
      switch (ITTI_MSG_ID(msg_p)) {
      case MESSAGE_TEST:
        LOG_D(MAC, "Received %s\n", ITTI_MSG_NAME(msg_p));
        break;
1064

1065 1066 1067 1068
      case RRC_MAC_BCCH_DATA_REQ:
        LOG_D(MAC, "Received %s from %s: instance %d, frameP %d, eNB_index %d\n",
              msg_name, ITTI_MSG_ORIGIN_NAME(msg_p), instance,
              RRC_MAC_BCCH_DATA_REQ (msg_p).frame, RRC_MAC_BCCH_DATA_REQ (msg_p).enb_index);
1069

1070 1071
        // TODO process BCCH data req.
        break;
1072

1073 1074 1075 1076
      case RRC_MAC_CCCH_DATA_REQ:
        LOG_D(MAC, "Received %s from %s: instance %d, frameP %d, eNB_index %d\n",
              msg_name, ITTI_MSG_ORIGIN_NAME(msg_p), instance,
              RRC_MAC_CCCH_DATA_REQ (msg_p).frame, RRC_MAC_CCCH_DATA_REQ (msg_p).enb_index);
1077

1078 1079
        // TODO process CCCH data req.
        break;
1080 1081 1082

#ifdef Rel10

1083 1084 1085 1086
      case RRC_MAC_MCCH_DATA_REQ:
        LOG_D(MAC, "Received %s from %s: instance %d, frameP %d, eNB_index %d, mbsfn_sync_area %d\n",
              msg_name, ITTI_MSG_ORIGIN_NAME(msg_p), instance,
              RRC_MAC_MCCH_DATA_REQ (msg_p).frame, RRC_MAC_MCCH_DATA_REQ (msg_p).enb_index, RRC_MAC_MCCH_DATA_REQ (msg_p).mbsfn_sync_area);
1087

1088 1089 1090
        // TODO process MCCH data req.
        break;
#endif
1091

1092 1093 1094
      default:
        LOG_E(MAC, "Received unexpected message %s\n", msg_name);
        break;
1095
      }
1096 1097 1098 1099

      result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
      AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
    }
1100
  } while(msg_p != NULL);
1101

1102 1103
#endif

1104
  // clear DCI and BCCH contents before scheduling
1105
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1106 1107
    DCI_pdu[CC_id]->Num_common_dci  = 0;
    DCI_pdu[CC_id]->Num_ue_spec_dci = 0;
1108

1109

1110
#ifdef Rel10
1111
    eNB_mac_inst[module_idP].common_channels[CC_id].mcch_active =0;
1112
#endif
1113

Raymond Knopp's avatar
 
Raymond Knopp committed
1114 1115
    eNB_mac_inst[module_idP].frame    = frameP;
    eNB_mac_inst[module_idP].subframe = subframeP;
1116 1117


Raymond Knopp's avatar
 
Raymond Knopp committed
1118
  }
1119

1120
  //if (subframeP%5 == 0)
Raymond Knopp's avatar
 
Raymond Knopp committed
1121
  //#ifdef EXMIMO
1122
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES, NOT_A_RNTI, frameP, 0,module_idP);
1123
  pdcp_run(&ctxt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1124
  //#endif
1125

1126
  // check HO
1127 1128
  rrc_rx_tx(&ctxt,
            0, // eNB index, unused in eNB
1129
            CC_id);
1130 1131

#ifdef Rel10
1132 1133

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1134 1135 1136 1137
    if (eNB_mac_inst[module_idP].common_channels[CC_id].MBMS_flag >0) {
      start_meas(&eNB_mac_inst[module_idP].schedule_mch);
      mbsfn_status[CC_id] = schedule_MBMS(module_idP,CC_id,frameP,subframeP);
      stop_meas(&eNB_mac_inst[module_idP].schedule_mch);
Raymond Knopp's avatar
 
Raymond Knopp committed
1138
    }
1139
  }
1140

1141
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1142
  // refresh UE list based on UEs dropped by PHY in previous subframe
1143
  /*
Raymond Knopp's avatar
 
Raymond Knopp committed
1144 1145 1146
  i=UE_list->head;
  while (i>=0) {
    next_i = UE_list->next[i];
1147
    LOG_T(MAC,"UE %d : rnti %x, stats %p\n",i,UE_RNTI(module_idP,i),mac_xface->get_eNB_UE_stats(module_idP,0,UE_RNTI(module_idP,i)));
Raymond Knopp's avatar
 
Raymond Knopp committed
1148
    if (mac_xface->get_eNB_UE_stats(module_idP,0,UE_RNTI(module_idP,i))==NULL) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1149
      mac_remove_ue(module_idP,i,frameP);
Raymond Knopp's avatar
 
Raymond Knopp committed
1150 1151 1152
    }
    i=next_i;
  }
1153
  */
1154

1155
  switch (subframeP) {
1156
  case 0:
1157

1158
    // FDD/TDD Schedule Downlink RA transmissions (RA response, Msg4 Contention resolution)
1159
    // Schedule ULSCH for FDD or subframeP 4 (TDD config 0,3,6)
1160
    // Schedule Normal DLSCH
1161

Raymond Knopp's avatar
Raymond Knopp committed
1162

1163 1164
    schedule_RA(module_idP,frameP,subframeP,2);

1165 1166

    if (mac_xface->lte_frame_parms->frame_type == FDD) {  //FDD
1167
      schedule_ulsch(module_idP,frameP,cooperation_flag,0,4);//,calibration_flag);
1168 1169 1170
    } else if  ((mac_xface->lte_frame_parms->tdd_config == TDD) || //TDD
                (mac_xface->lte_frame_parms->tdd_config == 3) ||
                (mac_xface->lte_frame_parms->tdd_config == 6)) {
1171
      //schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,4);//,calibration_flag);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1172
    }
1173

1174 1175
    schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
    fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1176

1177 1178 1179
    break;

  case 1:
1180

1181
    // TDD, schedule UL for subframeP 7 (TDD config 0,1) / subframeP 8 (TDD Config 6)
1182 1183
    // FDD, schedule normal UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == TDD) { // TDD
1184 1185 1186
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 0:
      case 1:
1187
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,7);
1188
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1189 1190 1191
        break;

      case 6:
1192
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,8);
1193
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1194 1195 1196 1197 1198 1199
        break;

      default:
        break;
      }
    } else { //FDD
1200
      schedule_ulsch(module_idP,frameP,cooperation_flag,1,5);
1201
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1202
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1203
    }
1204

1205
    break;
1206 1207

  case 2:
1208

1209
    // TDD, nothing
1210 1211
    // FDD, normal UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == FDD) {  //FDD
1212
      schedule_ulsch(module_idP,frameP,cooperation_flag,2,6);
1213
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1214
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1215
    }
1216

1217 1218 1219
    break;

  case 3:
1220

1221
    // TDD Config 2, ULSCH for subframeP 7
1222 1223 1224
    // TDD Config 2/5 normal DLSCH
    // FDD, normal UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == TDD) {
1225 1226
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 2:
1227
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,7);
1228 1229 1230

        // no break here!
      case 5:
1231 1232
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1233 1234 1235 1236 1237 1238
        break;

      default:
        break;
      }
    } else { //FDD
1239

1240
      schedule_ulsch(module_idP,frameP,cooperation_flag,3,7);
1241
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1242
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1243
    }
1244

1245
    break;
1246 1247

  case 4:
1248

1249
    // TDD Config 1, ULSCH for subframeP 8
1250 1251 1252
    // TDD Config 1/2/4/5 DLSCH
    // FDD UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == 1) { // TDD
1253 1254
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 1:
1255
        //        schedule_RA(module_idP,frameP,subframeP);
1256
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,8);
1257 1258 1259 1260 1261 1262 1263 1264 1265

        // no break here!
      case 2:

        // no break here!
      case 4:

        // no break here!
      case 5:
Raymond Knopp's avatar
Raymond Knopp committed
1266

1267 1268
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1269 1270 1271 1272 1273 1274 1275 1276
        break;

      default:
        break;
      }
    } else {
      if (mac_xface->lte_frame_parms->frame_type == FDD) {  //FDD

1277 1278
	//        schedule_RA(module_idP,frameP, subframeP, 0);
	schedule_ulsch(module_idP, frameP, cooperation_flag, 4, 8);
1279
	schedule_ue_spec(module_idP, frameP, subframeP,  mbsfn_status);
1280
        fill_DLSCH_dci(module_idP, frameP, subframeP,   mbsfn_status);
1281
      }
1282
    }
1283

1284 1285 1286 1287 1288 1289 1290
    break;

  case 5:
    // TDD/FDD Schedule SI
    // TDD Config 0,6 ULSCH for subframes 9,3 resp.
    // TDD normal DLSCH
    // FDD normal UL/DLSCH
1291
    schedule_SI(module_idP,frameP,subframeP);
1292

1293
    //schedule_RA(module_idP,frameP,subframeP,5);
1294
    if (mac_xface->lte_frame_parms->frame_type == FDD) {
1295 1296 1297 1298
      schedule_RA(module_idP,frameP,subframeP,1);
      schedule_ulsch(module_idP,frameP,cooperation_flag,5,9);
      schedule_ue_spec(module_idP, frameP, subframeP,  mbsfn_status);
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1299 1300
    } else if ((mac_xface->lte_frame_parms->tdd_config == 0) || // TDD Config 0
               (mac_xface->lte_frame_parms->tdd_config == 6)) { // TDD Config 6
1301
      //schedule_ulsch(module_idP,cooperation_flag,subframeP);
1302
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1303
    } else {
1304 1305
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1306
    }
1307

1308 1309 1310
    break;

  case 6:
1311

1312 1313 1314 1315
    // TDD Config 0,1,6 ULSCH for subframes 2,3
    // TDD Config 3,4,5 Normal DLSCH
    // FDD normal ULSCH/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == TDD) { // TDD
1316 1317 1318 1319 1320
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 0:
        break;

      case 1:
1321
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,2);
1322 1323
        //  schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1324 1325 1326
        break;

      case 6:
1327
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,3);
1328 1329
        //  schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1330 1331 1332
        break;

      case 5:
1333 1334 1335
        schedule_RA(module_idP,frameP,subframeP,2);
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1336 1337 1338 1339
        break;

      case 3:
      case 4:
1340 1341
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1342 1343 1344 1345 1346 1347
        break;

      default:
        break;
      }
    } else { //FDD
1348
      //      schedule_ulsch(module_idP,frameP,cooperation_flag,6,0);
1349
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1350
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1351
    }
1352

1353
    break;
1354

1355
  case 7:
1356

1357 1358 1359
    // TDD Config 3,4,5 Normal DLSCH
    // FDD Normal UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == TDD) { // TDD
1360 1361 1362
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 3:
      case 4:
1363 1364 1365
        schedule_RA(module_idP,frameP,subframeP,3);  // 3 = Msg3 subframeP, not
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1366 1367 1368
        break;

      case 5:
1369 1370
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1371 1372 1373 1374 1375 1376
        break;

      default:
        break;
      }
    } else { //FDD
1377
      //schedule_ulsch(module_idP,frameP,cooperation_flag,7,1);
1378
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1379
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1380
    }
1381

1382 1383 1384
    break;

  case 8:
1385

1386
    // TDD Config 2,3,4,5 ULSCH for subframeP 2
1387 1388 1389
    //
    // FDD Normal UL/DLSCH
    if (mac_xface->lte_frame_parms->frame_type == TDD) { // TDD
1390 1391 1392 1393 1394 1395
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 2:
      case 3:
      case 4:
      case 5:

1396
        //  schedule_RA(module_idP,subframeP);
1397
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,2);
1398 1399
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1400 1401 1402 1403 1404 1405
        break;

      default:
        break;
      }
    } else { //FDD
1406
      //schedule_ulsch(module_idP,frameP,cooperation_flag,8,2);
1407
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1408
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1409
    }
1410

1411 1412 1413
    break;

  case 9:
1414

1415 1416
    // TDD Config 1,3,4,6 ULSCH for subframes 3,3,3,4
    if (mac_xface->lte_frame_parms->frame_type == TDD) {
1417 1418
      switch (mac_xface->lte_frame_parms->tdd_config) {
      case 1:
1419
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,3);
1420 1421 1422
        schedule_RA(module_idP,frameP,subframeP,7);  // 7 = Msg3 subframeP, not
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1423 1424 1425 1426
        break;

      case 3:
      case 4:
1427
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,3);
1428 1429
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1430 1431 1432
        break;

      case 6:
1433
        schedule_ulsch(module_idP,frameP,cooperation_flag,subframeP,4);
1434 1435 1436
        //schedule_RA(module_idP,frameP,subframeP);
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1437 1438 1439 1440
        break;

      case 2:
      case 5:
1441 1442 1443
        //schedule_RA(module_idP,frameP,subframeP);
        schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
        fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1444 1445 1446 1447 1448 1449
        break;

      default:
        break;
      }
    } else { //FDD
1450
      //     schedule_ulsch(module_idP,frameP,cooperation_flag,9,3);
1451
      schedule_ue_spec(module_idP,frameP,subframeP,mbsfn_status);
1452
      fill_DLSCH_dci(module_idP,frameP,subframeP,mbsfn_status);
1453
    }
1454

1455
    break;
1456

1457
  }
1458

1459
  LOG_D(MAC,"FrameP %d, subframeP %d : Scheduling CCEs\n",frameP,subframeP);
1460

1461
  // Allocate CCEs for good after scheduling is done
1462 1463
  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++)
    allocate_CCEs(module_idP,CC_id,subframeP,0);
1464

1465
  LOG_D(MAC,"frameP %d, subframeP %d\n",frameP,subframeP);
1466

1467
  stop_meas(&eNB_mac_inst[module_idP].eNB_scheduler);
1468
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_OUT);
1469

1470
}
1471

1472
#endif /* FAPI */