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

/*! \file pre_processor.c
23
 * \brief eNB scheduler preprocessing fuction prior to scheduling
24
 * \author Navid Nikaein and Ankit Bhamri
25
 * \date 2013 - 2014
26
 * \email navid.nikaein@eurecom.fr
27
 * \version 1.0
28 29 30 31
 * @ingroup _mac

 */

32 33 34
#define _GNU_SOURCE
#include <stdlib.h>

35
#include "assertions.h"
36 37 38
#include "LAYER2/MAC/mac.h"
#include "LAYER2/MAC/mac_proto.h"
#include "LAYER2/MAC/mac_extern.h"
39 40
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
41 42 43
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"
44
#include "RRC/LTE/rrc_extern.h"
45
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
46
#include "rlc.h"
47
#include "PHY/LTE_TRANSPORT/transport_common_proto.h"
48

49
#include "common/ran_context.h"
50

51
extern RAN_CONTEXT_t RC;
52

53 54 55 56 57 58
#define DEBUG_eNB_SCHEDULER 1
#define DEBUG_HEADER_PARSING 1
//#define DEBUG_PACKET_TRACE 1

//#define ICIC 0

59
/* this function checks that get_eNB_UE_stats returns
Cedric Roux's avatar
Cedric Roux committed
60
 * a non-NULL pointer for all the active CCs of an UE
61
 */
62
/*
63
int phy_stats_exist(module_id_t Mod_id, int rnti)
64 65
{
  int CC_id;
Cedric Roux's avatar
Cedric Roux committed
66 67
  int i;
  int UE_id          = find_UE_id(Mod_id, rnti);
68
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
Cedric Roux's avatar
Cedric Roux committed
69 70 71 72 73 74 75 76 77 78 79 80
  if (UE_id == -1) {
    LOG_W(MAC, "[eNB %d] UE %x not found, should be there (in phy_stats_exist)\n",
	  Mod_id, rnti);
    return 0;
  }
  if (UE_list->numactiveCCs[UE_id] == 0) {
    LOG_W(MAC, "[eNB %d] UE %x has no active CC (in phy_stats_exist)\n",
	  Mod_id, rnti);
    return 0;
  }
  for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
    CC_id = UE_list->ordered_CCids[i][UE_id];
81 82
    if (mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti) == NULL)
      return 0;
83
  }
84 85
  return 1;
}
86
*/
87

88
// This function stores the downlink buffer for all the logical channels
89
void
90
store_dlsch_buffer(module_id_t Mod_id,
91
                   int slice_idx,
92 93
                   frame_t frameP,
                   sub_frame_t subframeP) {
Cedric Roux's avatar
Cedric Roux committed
94

95
  int UE_id, lcid;
96 97 98 99
  rnti_t rnti;
  mac_rlc_status_resp_t rlc_status;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_TEMPLATE *UE_template;
100

101 102
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
    if (UE_list->active[UE_id] != TRUE)
103 104
	    continue;

105
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx))
106
      continue;
107

108
    UE_template = &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
109

110 111 112
    // clear logical channel interface variables
    UE_template->dl_buffer_total = 0;
    UE_template->dl_pdus_total = 0;
113

114 115 116 117 118
    for (lcid = 0; lcid < MAX_NUM_LCID; ++lcid) {
      UE_template->dl_buffer_info[lcid] = 0;
      UE_template->dl_pdus_in_buffer[lcid] = 0;
      UE_template->dl_buffer_head_sdu_creation_time[lcid] = 0;
      UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid] = 0;
119
    }
120

121
    rnti = UE_RNTI(Mod_id, UE_id);
122

123
    for (lcid = 0; lcid < MAX_NUM_LCID; ++lcid) {    // loop over all the logical channels
124

125
	    rlc_status = mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
126
				   ENB_FLAG_YES, MBMS_FLAG_NO, lcid, 0
127
#if (RRC_VERSION >= MAKE_VERSION(14, 0, 0))
128 129 130
                   ,0, 0
#endif
                   );
131 132 133
      UE_template->dl_buffer_info[lcid] = rlc_status.bytes_in_buffer;    //storing the dlsch buffer for each logical channel
      UE_template->dl_pdus_in_buffer[lcid] = rlc_status.pdus_in_buffer;
      UE_template->dl_buffer_head_sdu_creation_time[lcid] = rlc_status.head_sdu_creation_time;
134 135
      UE_template->dl_buffer_head_sdu_creation_time_max =
              cmax(UE_template->dl_buffer_head_sdu_creation_time_max, rlc_status.head_sdu_creation_time);
136 137 138 139
      UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid] = rlc_status.head_sdu_remaining_size_to_send;
      UE_template->dl_buffer_head_sdu_is_segmented[lcid] = rlc_status.head_sdu_is_segmented;
      UE_template->dl_buffer_total += UE_template->dl_buffer_info[lcid];    //storing the total dlsch buffer
      UE_template->dl_pdus_total += UE_template->dl_pdus_in_buffer[lcid];
140

Cedric Roux's avatar
Cedric Roux committed
141
#ifdef DEBUG_eNB_SCHEDULER
142

143
      /* note for dl_buffer_head_sdu_remaining_size_to_send[lcid] :
144 145
       * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
       */
146
      if (UE_template->dl_buffer_info[lcid] > 0)
147 148
        LOG_D(MAC,
              "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
149 150
              Mod_id, RC.mac[Mod_id]->slice_info.dl[slice_idx].id, frameP,
              subframeP, UE_id, lcid, UE_template->dl_pdus_in_buffer[lcid],
151 152 153 154
              UE_template->dl_buffer_info[lcid],
              UE_template->dl_buffer_head_sdu_creation_time[lcid],
              UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid],
              UE_template->dl_buffer_head_sdu_is_segmented[lcid]);
155

Cedric Roux's avatar
Cedric Roux committed
156
#endif
157

158

159
    }
160

161 162 163 164 165 166 167
    if (UE_template->dl_buffer_total > 0)
      LOG_D(MAC,
            "[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
            Mod_id, frameP, subframeP, UE_id,
            UE_template->dl_buffer_total,
            UE_template->dl_pdus_total);
  }
168 169
}

Niccolò Iardella's avatar
Niccolò Iardella committed
170 171 172
int cqi2mcs(int cqi) {
  return cqi_to_mcs[cqi];
}
173

174
// This function returns the estimated number of RBs required by each UE for downlink scheduling
175 176
void
assign_rbs_required(module_id_t Mod_id,
177
                    int slice_idx,
178 179 180 181
                    frame_t frameP,
                    sub_frame_t subframe,
                    int min_rb_unit[NFAPI_CC_MAX],
                    uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB])
182
{
183

184
  uint16_t TBS = 0;
185

186 187
  int UE_id, n, i, j, CC_id, pCCid, tmp;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
188
  slice_info_t *sli = &RC.mac[Mod_id]->slice_info;
189 190
  eNB_UE_STATS *eNB_UE_stats, *eNB_UE_stats_i, *eNB_UE_stats_j;
  int N_RB_DL;
191

192 193 194
  // clear rb allocations across all CC_id
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
    if (UE_list->active[UE_id] != TRUE) continue;
195
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
196
    pCCid = UE_PCCID(Mod_id, UE_id);
197

198 199
    //update CQI information across component carriers
    for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
200

201 202
      CC_id = UE_list->ordered_CCids[n][UE_id];
      eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
203 204
//      eNB_UE_stats->dlsch_mcs1 = cmin(cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]], sli->dl[slice_idx].maxmcs);
      eNB_UE_stats->dlsch_mcs1 = cmin(cqi2mcs(UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]), sli->dl[slice_idx].maxmcs);
205

206
    }
207

208 209 210 211 212 213 214 215 216 217 218 219 220
    // provide the list of CCs sorted according to MCS
    for (i = 0; i < UE_list->numactiveCCs[UE_id]; ++i) {
      eNB_UE_stats_i = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]][UE_id];
      for (j = i + 1; j < UE_list->numactiveCCs[UE_id]; j++) {
        DevAssert(j < NFAPI_CC_MAX);
        eNB_UE_stats_j = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]][UE_id];
        if (eNB_UE_stats_j->dlsch_mcs1 > eNB_UE_stats_i->dlsch_mcs1) {
          tmp = UE_list->ordered_CCids[i][UE_id];
          UE_list->ordered_CCids[i][UE_id] = UE_list->ordered_CCids[j][UE_id];
          UE_list->ordered_CCids[j][UE_id] = tmp;
        }
      }
    }
221

222 223
    if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
      LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);
224

225 226 227
      for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
        CC_id = UE_list->ordered_CCids[i][UE_id];
        eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
228

229 230 231 232 233
        if (eNB_UE_stats->dlsch_mcs1 == 0) {
          nb_rbs_required[CC_id][UE_id] = 4;    // don't let the TBS get too small
        } else {
          nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
        }
234

235
        TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_rbs_required[CC_id][UE_id]);
236

237 238 239 240 241 242
        LOG_D(MAC,
              "[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
              UE_id, CC_id,
              UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
              nb_rbs_required[CC_id][UE_id],
              eNB_UE_stats->dlsch_mcs1, TBS);
243

244
        N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
245

246 247
        UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx] =
                nb_rbs_allowed_slice(sli->dl[slice_idx].pct, N_RB_DL);
248

249 250 251 252
        /* calculating required number of RBs for each UE */
        while (TBS < UE_list->UE_template[pCCid][UE_id].dl_buffer_total) {
          nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];

253 254 255
          if (nb_rbs_required[CC_id][UE_id] > UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx]) {
            TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx]);
            nb_rbs_required[CC_id][UE_id] = UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx];
256 257 258 259 260 261 262 263 264 265
            break;
          }
          TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_rbs_required[CC_id][UE_id]);
        } // end of while

        LOG_D(MAC,
              "[eNB %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
              Mod_id, frameP, UE_id, CC_id, min_rb_unit[CC_id],
              nb_rbs_required[CC_id][UE_id], TBS,
              eNB_UE_stats->dlsch_mcs1);
266

267
        sli->pre_processor_results[slice_idx].mcs[CC_id][UE_id] = eNB_UE_stats->dlsch_mcs1;
268
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
269
    }
270
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
271
}
272 273


Raymond Knopp's avatar
 
Raymond Knopp committed
274
// This function scans all CC_ids for a particular UE to find the maximum round index of its HARQ processes
275 276
int
maxround(module_id_t Mod_id, uint16_t rnti, int frame,
277
         sub_frame_t subframe, uint8_t ul_flag) {
278

279 280 281 282
  uint8_t round, round_max = 0, UE_id;
  int CC_id, harq_pid;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  COMMON_channels_t *cc;
283

284 285
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
    cc = &RC.mac[Mod_id]->common_channels[CC_id];
286

287 288
    UE_id = find_UE_id(Mod_id, rnti);
    harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frame ,subframe);
289

290 291 292
    round = UE_list->UE_sched_ctrl[UE_id].round[CC_id][harq_pid];
    if (round > round_max) {
      round_max = round;
293
    }
294
  }
295

296
  return round_max;
Raymond Knopp's avatar
 
Raymond Knopp committed
297
}
298

Raymond Knopp's avatar
 
Raymond Knopp committed
299
// This function scans all CC_ids for a particular UE to find the maximum DL CQI
300
// it returns -1 if the UE is not found in PHY layer (get_eNB_UE_stats gives NULL)
301 302 303 304
int maxcqi(module_id_t Mod_id, int32_t UE_id) {
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  int CC_id, n;
  int CQI = 0;
305

306 307
  for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
    CC_id = UE_list->ordered_CCids[n][UE_id];
308

309 310
    if (UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id] > CQI) {
      CQI = UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id];
311
    }
312
  }
313

314
  return CQI;
Raymond Knopp's avatar
 
Raymond Knopp committed
315
}
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330
long min_lcgidpriority(module_id_t Mod_id, int32_t UE_id) {
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  int i;
  int pCC_id = UE_PCCID(Mod_id, UE_id);
  long ret = UE_list->UE_template[pCC_id][UE_id].lcgidpriority[0];

  for (i = 1; i < 11; ++i) {
    if (UE_list->UE_template[pCC_id][UE_id].lcgidpriority[i] < ret)
      ret = UE_list->UE_template[pCC_id][UE_id].lcgidpriority[i];
  }

  return ret;
}

331
struct sort_ue_dl_params {
332 333 334
    int Mod_idP;
    int frameP;
    int subframeP;
335
    int slice_idx;
336
};
337

338 339
static int ue_dl_compare(const void *_a, const void *_b, void *_params)
{
340 341
  struct sort_ue_dl_params *params = _params;
  UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;
342

343
  int i;
344
  int slice_idx = params->slice_idx;
345 346
  int UE_id1 = *(const int *) _a;
  int UE_id2 = *(const int *) _b;
347

348 349 350
  int rnti1 = UE_RNTI(params->Mod_idP, UE_id1);
  int pCC_id1 = UE_PCCID(params->Mod_idP, UE_id1);
  int round1 = maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP, 1);
351

352 353 354
  int rnti2 = UE_RNTI(params->Mod_idP, UE_id2);
  int pCC_id2 = UE_PCCID(params->Mod_idP, UE_id2);
  int round2 = maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP, 1);
355

356 357
  int cqi1 = maxcqi(params->Mod_idP, UE_id1);
  int cqi2 = maxcqi(params->Mod_idP, UE_id2);
358

359 360 361
  long lcgid1 = min_lcgidpriority(params->Mod_idP, UE_id1);
  long lcgid2 = min_lcgidpriority(params->Mod_idP, UE_id2);

362
  for (i = 0; i < CR_NUM; ++i) {
363
    switch (UE_list->sorting_criteria[slice_idx][i]) {
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

      case CR_ROUND :
        if (round1 > round2)
          return -1;
        if (round1 < round2)
          return 1;
        break;

      case CR_SRB12 :
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
          return -1;
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
          return 1;
        break;

      case CR_HOL :
        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
          return -1;
        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
          return 1;
        break;

      case CR_LC :
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
          return -1;
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
          return 1;
        break;

      case CR_CQI :
        if (cqi1 > cqi2)
          return -1;
        if (cqi1 < cqi2)
          return 1;
Niccolò Iardella's avatar
Niccolò Iardella committed
408
        break;
409

410 411 412 413 414 415
      case CR_LCP :
        if (lcgid1 < lcgid2)
          return -1;
        if (lcgid1 > lcgid2)
          return 1;

416 417 418 419
      default :
        break;
    }
  }
420 421

    return 0;
422
}
423

424
void decode_sorting_policy(module_id_t Mod_idP, int slice_idx) {
425 426 427
  int i;

  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
428
  uint32_t policy = RC.mac[Mod_idP]->slice_info.dl[slice_idx].sorting;
429 430 431 432 433 434
  uint32_t mask = 0x0000000F;
  uint16_t criterion;

  for (i = 0; i < CR_NUM; ++i) {
    criterion = (uint16_t) (policy >> 4 * (CR_NUM - 1 - i) & mask);
    if (criterion >= CR_NUM) {
435 436 437 438
      LOG_W(MAC,
            "Invalid criterion in slice index %d ID %d policy, revert to default policy \n",
            slice_idx, RC.mac[Mod_idP]->slice_info.dl[slice_idx].id);
      RC.mac[Mod_idP]->slice_info.dl[slice_idx].sorting = 0x12345;
439 440
      break;
    }
441
    UE_list->sorting_criteria[slice_idx][i] = criterion;
442
  }
443 444
}

445
void decode_slice_positioning(module_id_t Mod_idP,
446 447 448
                              int slice_idx,
                              uint8_t slice_allocation_mask[NFAPI_CC_MAX][N_RBG_MAX])
{
449 450 451 452 453 454 455 456 457 458
  uint8_t CC_id;
  int RBG, start_frequency, end_frequency;

  // Init slice_alloc_mask
  for (CC_id = 0; CC_id < NFAPI_CC_MAX; ++CC_id) {
    for (RBG = 0; RBG < N_RBG_MAX; ++RBG) {
      slice_allocation_mask[CC_id][RBG] = 0;
    }
  }

459 460
  start_frequency = RC.mac[Mod_idP]->slice_info.dl[slice_idx].pos_low;
  end_frequency = RC.mac[Mod_idP]->slice_info.dl[slice_idx].pos_high;
461 462 463 464 465 466 467
  for (CC_id = 0; CC_id < NFAPI_CC_MAX; ++CC_id) {
    for (RBG = start_frequency; RBG <= end_frequency; ++RBG) {
      slice_allocation_mask[CC_id][RBG] = 1;
    }
  }
}

468

Raymond Knopp's avatar
 
Raymond Knopp committed
469
// This fuction sorts the UE in order their dlsch buffer and CQI
470
void sort_UEs(module_id_t Mod_idP, int slice_idx, int frameP, sub_frame_t subframeP)
471
{
472 473 474
  int i;
  int list[MAX_MOBILES_PER_ENB];
  int list_size = 0;
475
  struct sort_ue_dl_params params = {Mod_idP, frameP, subframeP, slice_idx};
476

477
  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
478

479
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
480

481 482 483
    if (UE_list->active[i] == FALSE) continue;
    if (UE_RNTI(Mod_idP, i) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1) continue;
484
    if (!ue_dl_slice_membership(Mod_idP, i, slice_idx)) continue;
485

486 487 488
    list[list_size] = i;
    list_size++;
  }
489

490
  decode_sorting_policy(Mod_idP, slice_idx);
491

492
  qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  if (list_size) {
    for (i = 0; i < list_size - 1; ++i)
      UE_list->next[list[i]] = list[i + 1];
    UE_list->next[list[list_size - 1]] = -1;
    UE_list->head = list[0];
  } else {
    UE_list->head = -1;
  }

#if 0


    int UE_id1, UE_id2;
    int pCC_id1, pCC_id2;
    int cqi1, cqi2, round1, round2;
    int i = 0, ii = 0;		//,j=0;
    rnti_t rnti1, rnti2;

    UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;

    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {

	for (ii = UE_list->next[i]; ii >= 0; ii = UE_list->next[ii]) {

	    UE_id1 = i;
	    rnti1 = UE_RNTI(Mod_idP, UE_id1);
	    if (rnti1 == NOT_A_RNTI)
		continue;
	    if (UE_list->UE_sched_ctrl[UE_id1].ul_out_of_sync == 1)
		continue;
	    pCC_id1 = UE_PCCID(Mod_idP, UE_id1);
	    cqi1 = maxcqi(Mod_idP, UE_id1);	//
	    round1 = maxround(Mod_idP, rnti1, frameP, subframeP, 0);

	    UE_id2 = ii;
	    rnti2 = UE_RNTI(Mod_idP, UE_id2);
	    if (rnti2 == NOT_A_RNTI)
		continue;
	    if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
		continue;
	    cqi2 = maxcqi(Mod_idP, UE_id2);
	    round2 = maxround(Mod_idP, rnti2, frameP, subframeP, 0);	//mac_xface->get_ue_active_harq_pid(Mod_id,rnti2,subframe,&harq_pid2,&round2,0);
	    pCC_id2 = UE_PCCID(Mod_idP, UE_id2);

	    if (round2 > round1) {	// Check first if one of the UEs has an active HARQ process which needs service and swap order
		swap_UEs(UE_list, UE_id1, UE_id2, 0);
	    } else if (round2 == round1) {
		// RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
		//  for(j=0;j<MAX_NUM_LCID;j++){
		//    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
		//      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){

		// first check the buffer status for SRB1 and SRB2

		if ((UE_list->UE_template[pCC_id1][UE_id1].
		     dl_buffer_info[1] +
		     UE_list->UE_template[pCC_id1][UE_id1].
		     dl_buffer_info[2]) <
		    (UE_list->UE_template[pCC_id2][UE_id2].
		     dl_buffer_info[1] +
		     UE_list->UE_template[pCC_id2][UE_id2].
		     dl_buffer_info[2])) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (UE_list->UE_template[pCC_id1]
			   [UE_id1].dl_buffer_head_sdu_creation_time_max <
			   UE_list->UE_template[pCC_id2]
			   [UE_id2].dl_buffer_head_sdu_creation_time_max) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (UE_list->UE_template[pCC_id1][UE_id1].
			   dl_buffer_total <
			   UE_list->UE_template[pCC_id2][UE_id2].
			   dl_buffer_total) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (cqi1 < cqi2) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		}
	    }
571
	}
572 573
    }
#endif
574 575
}

576
void dlsch_scheduler_pre_processor_partitioning(module_id_t Mod_id,
577 578 579
                                                int slice_idx,
                                                const uint8_t rbs_retx[NFAPI_CC_MAX])
{
580 581 582
  int UE_id, CC_id, N_RB_DL, i;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_sched_ctrl *ue_sched_ctl;
583
  uint16_t available_rbs;
584 585 586 587 588

  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {

    if (UE_RNTI(Mod_id, UE_id) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
589
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
590 591 592 593 594 595

    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];

    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
596
      available_rbs = nb_rbs_allowed_slice(RC.mac[Mod_id]->slice_info.dl[slice_idx].pct, N_RB_DL);
597
      if (rbs_retx[CC_id] < available_rbs)
598
        ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx] = available_rbs - rbs_retx[CC_id];
599
      else
600
        ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx] = 0;
601 602 603 604
    }
  }
}

605
void dlsch_scheduler_pre_processor_accounting(module_id_t Mod_id,
606
                                              int slice_idx,
607 608
                                              frame_t frameP,
                                              sub_frame_t subframeP,
609
                                              int min_rb_unit[NFAPI_CC_MAX],
610
                                              uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
611
                                              uint16_t nb_rbs_accounted[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB])
612
{
613
  int UE_id, CC_id;
614
  int i;
Raymond Knopp's avatar
 
Raymond Knopp committed
615

616
  rnti_t rnti;
617
  uint8_t harq_pid, round;
618
  uint16_t available_rbs;
619
  uint8_t rbs_retx[NFAPI_CC_MAX];
620
  uint16_t average_rbs_per_user[NFAPI_CC_MAX];
621
  int total_ue_count[NFAPI_CC_MAX];
622 623
  int ue_count_newtx[NFAPI_CC_MAX];
  int ue_count_retx[NFAPI_CC_MAX];
624
  //uint8_t ue_retx_flag[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB];
625

626
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
627
  UE_sched_ctrl *ue_sched_ctl;
628
  COMMON_channels_t *cc;
629

630
  // Reset
631
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
632
    total_ue_count[CC_id] = 0;
633 634 635
    ue_count_newtx[CC_id] = 0;
    ue_count_retx[CC_id] = 0;
    rbs_retx[CC_id] = 0;
636
    average_rbs_per_user[CC_id] = 0;
637 638 639
    //for (UE_id = 0; UE_id < NFAPI_CC_MAX; ++UE_id) {
    //  ue_retx_flag[CC_id][UE_id] = 0;
    //}
640
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
641

642
  // Find total UE count, and account the RBs required for retransmissions
643 644
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
    rnti = UE_RNTI(Mod_id, UE_id);
645 646
    if (rnti == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
647
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
648

649
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
650
      CC_id = UE_list->ordered_CCids[i][UE_id];
651 652
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
      cc = &RC.mac[Mod_id]->common_channels[CC_id];
653
      // FIXME: Can we use subframe2harqpid() here?
654 655 656 657 658 659
      if (cc->tdd_Config)
        harq_pid = ((frameP * 10) + subframeP) % 10;
      else
        harq_pid = ((frameP * 10) + subframeP) & 7;
      round = ue_sched_ctl->round[CC_id][harq_pid];

660 661 662
      if (nb_rbs_required[CC_id][UE_id] > 0) {
        total_ue_count[CC_id]++;
      }
663 664 665

      if (round != 8) {
        nb_rbs_required[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
666 667
        rbs_retx[CC_id] += nb_rbs_required[CC_id][UE_id];
        ue_count_retx[CC_id]++;
668
        //ue_retx_flag[CC_id][UE_id] = 1;
669 670
      } else {
        ue_count_newtx[CC_id]++;
671 672 673
      }
    }
  }
674

675 676
  // PARTITIONING
  // Reduces the available RBs according to slicing configuration
677
  dlsch_scheduler_pre_processor_partitioning(Mod_id, slice_idx, rbs_retx);
678

679
  switch (RC.mac[Mod_id]->slice_info.dl[slice_idx].accounting) {
680

681
    // If greedy scheduling, try to account all the required RBs
682
    case POL_GREEDY:
683 684 685 686
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);
        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
687
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
Raymond Knopp's avatar
 
Raymond Knopp committed
688

689 690 691 692 693 694 695 696 697
        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
          CC_id = UE_list->ordered_CCids[i][UE_id];
          nb_rbs_accounted[CC_id][UE_id] = nb_rbs_required[CC_id][UE_id];
        }
      }
      break;

    // Use the old, fair algorithm
    // Loop over all active UEs and account the avg number of RBs to each UE, based on all non-retx UEs.
698
    // case POL_FAIR:
699 700 701 702 703 704 705 706
    default:
      // FIXME: This is not ideal, why loop on UEs to find average_rbs_per_user[], that is per-CC?
      // TODO: Look how to loop on active CCs only without using the UE_num_active_CC() function.
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);

        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
707
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
708 709

        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
710

711 712
          CC_id = UE_list->ordered_CCids[i][UE_id];
          ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
713
          available_rbs = ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx];
714

715
          if (ue_count_newtx[CC_id] == 0) {
716
            average_rbs_per_user[CC_id] = 0;
717 718
          } else if (min_rb_unit[CC_id]*ue_count_newtx[CC_id] <= available_rbs) {
            average_rbs_per_user[CC_id] = (uint16_t)floor(available_rbs/ue_count_newtx[CC_id]);
719 720 721 722 723 724
          } else {
            // consider the total number of use that can be scheduled UE
            average_rbs_per_user[CC_id] = (uint16_t)min_rb_unit[CC_id];
          }
        }
      }
725

726 727 728 729 730 731
      // note: nb_rbs_required is assigned according to total_buffer_dl
      // extend nb_rbs_required to capture per LCID RB required
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);
        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
732
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
733 734 735 736 737

        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
          CC_id = UE_list->ordered_CCids[i][UE_id];
          nb_rbs_accounted[CC_id][UE_id] = cmin(average_rbs_per_user[CC_id], nb_rbs_required[CC_id][UE_id]);
        }
738
      }
739
      break;
740
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
741

742 743 744 745 746



  // Check retransmissions
  // TODO: Do this once at the beginning
747 748
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
    rnti = UE_RNTI(Mod_id, UE_id);
749 750
    if (rnti == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
751
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
Raymond Knopp's avatar
 
Raymond Knopp committed
752

753 754
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
755 756
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
      cc = &RC.mac[Mod_id]->common_channels[CC_id];
757
      harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frameP ,subframeP);
758 759 760 761
      round = ue_sched_ctl->round[CC_id][harq_pid];

      // control channel or retransmission
      /* TODO: do we have to check for retransmission? */
762
      if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED || round != 8) {
763
        nb_rbs_accounted[CC_id][UE_id] = nb_rbs_required[CC_id][UE_id];
764
      }
765
    }
766
  }
767 768 769
}

void dlsch_scheduler_pre_processor_positioning(module_id_t Mod_id,
770
                                               int slice_idx,
771 772
                                               int min_rb_unit[NFAPI_CC_MAX],
                                               uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
773
                                               uint16_t nb_rbs_accounted[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
774
                                               uint16_t nb_rbs_remaining[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
775
                                               uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX],
776 777
                                               uint8_t MIMO_mode_indicator[NFAPI_CC_MAX][N_RBG_MAX])
{
778 779
  int UE_id, CC_id;
  int i;
780
#ifdef TM5
781
  uint8_t transmission_mode;
782
#endif
783 784 785 786
  uint8_t slice_allocation_mask[NFAPI_CC_MAX][N_RBG_MAX];
  int N_RBG[NFAPI_CC_MAX];
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;

787
  decode_slice_positioning(Mod_id, slice_idx, slice_allocation_mask);
788

789 790 791 792
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
    COMMON_channels_t *cc = &RC.mac[Mod_id]->common_channels[CC_id];
    N_RBG[CC_id] = to_rbg(cc->mib->message.dl_Bandwidth);
  }
Xu Bo's avatar
Xu Bo committed
793

794 795
  // Try to allocate accounted RBs
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
796

797 798
    if (UE_RNTI(Mod_id, UE_id) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
799
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
800

801 802 803
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      nb_rbs_remaining[CC_id][UE_id] = nb_rbs_accounted[CC_id][UE_id];
804
#ifdef TM5
805
      transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
806
#endif
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

      if (nb_rbs_required[CC_id][UE_id] > 0)
        LOG_D(MAC,
              "Step 1: nb_rbs_remaining[%d][%d]= %d (accounted %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
              CC_id,
              UE_id,
              nb_rbs_remaining[CC_id][UE_id],
              nb_rbs_accounted[CC_id][UE_id],
              nb_rbs_required[CC_id][UE_id],
              UE_list->UE_sched_ctrl[UE_id].pre_nb_available_rbs[CC_id],
              N_RBG[CC_id],
              min_rb_unit[CC_id]);

      LOG_T(MAC, "calling dlsch_scheduler_pre_processor_allocate .. \n ");
      dlsch_scheduler_pre_processor_allocate(Mod_id,
                                             UE_id,
                                             CC_id,
                                             N_RBG[CC_id],
                                             min_rb_unit[CC_id],
                                             nb_rbs_required,
                                             nb_rbs_remaining,
                                             rballoc_sub,
                                             slice_allocation_mask,
                                             MIMO_mode_indicator);
831

832
#ifdef TM5
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
      // data chanel TM5: to be revisited
        if ((round == 0) &&
        (transmission_mode == 5) &&
        (ue_sched_ctl->dl_pow_off[CC_id] != 1)) {

        for (j = 0; j < N_RBG[CC_id]; j += 2) {

            if ((((j == (N_RBG[CC_id] - 1))
              && (rballoc_sub[CC_id][j] == 0)
              && (ue_sched_ctl->
                  rballoc_sub_UE[CC_id][j] == 0))
             || ((j < (N_RBG[CC_id] - 1))
                 && (rballoc_sub[CC_id][j + 1] == 0)
                 &&
                 (ue_sched_ctl->rballoc_sub_UE
                  [CC_id][j + 1] == 0)))
            && (nb_rbs_remaining[CC_id][UE_id]
                > 0)) {

            for (i = UE_list->next[UE_id + 1]; i >= 0;
                 i = UE_list->next[i]) {

                UE_id2 = i;
                rnti2 = UE_RNTI(Mod_id, UE_id2);
                ue_sched_ctl2 =
                &UE_list->UE_sched_ctrl[UE_id2];
                round2 = ue_sched_ctl2->round[CC_id];
                if (rnti2 == NOT_A_RNTI)
                continue;
                if (UE_list->
                UE_sched_ctrl
                [UE_id2].ul_out_of_sync == 1)
                continue;

                eNB_UE_stats2 =
                UE_list->
                eNB_UE_stats[CC_id][UE_id2];
                //mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti2,frameP,subframeP,&harq_pid2,&round2,0);

                if ((mac_eNB_get_rrc_status
                 (Mod_id,
                  rnti2) >= RRC_RECONFIGURED)
                && (round2 == 0)
                &&
                (get_tmode(Mod_id, CC_id, UE_id2)
                 == 5)
879
                && (ue_sched_ctl->
880 881 882
                    dl_pow_off[CC_id] != 1)) {

                if ((((j == (N_RBG[CC_id] - 1))
883
                      &&
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
                      (ue_sched_ctl->rballoc_sub_UE
                       [CC_id][j] == 0))
                     || ((j < (N_RBG[CC_id] - 1))
                     &&
                     (ue_sched_ctl->
                      rballoc_sub_UE[CC_id][j +
                                1]
                      == 0)))
                    &&
                    (nb_rbs_remaining
                     [CC_id]
                     [UE_id2] > 0)) {

                    if ((((eNB_UE_stats2->
                       DL_pmi_single ^
                       eNB_UE_stats1->
                       DL_pmi_single)
                      << (14 - j)) & 0xc000) == 0x4000) {	//MU-MIMO only for 25 RBs configuration

                    rballoc_sub[CC_id][j] = 1;
                    ue_sched_ctl->
                        rballoc_sub_UE[CC_id]
                        [j] = 1;
                    ue_sched_ctl2->
                        rballoc_sub_UE[CC_id]
                        [j] = 1;
                    MIMO_mode_indicator[CC_id]
                        [j] = 0;

                    if (j < N_RBG[CC_id] - 1) {
                        rballoc_sub[CC_id][j +
                                   1] =
                        1;
                        ue_sched_ctl->
                        rballoc_sub_UE
                        [CC_id][j + 1] = 1;
                        ue_sched_ctl2->rballoc_sub_UE
                        [CC_id][j + 1] = 1;
                        MIMO_mode_indicator
                        [CC_id][j + 1]
                        = 0;
                    }

                    ue_sched_ctl->
                        dl_pow_off[CC_id]
                        = 0;
                    ue_sched_ctl2->
                        dl_pow_off[CC_id]
                        = 0;


                    if ((j == N_RBG[CC_id] - 1)
                        && ((N_RB_DL == 25)
                        || (N_RB_DL ==
                            50))) {

                        nb_rbs_remaining
                        [CC_id][UE_id] =
                        nb_rbs_remaining
                        [CC_id][UE_id] -
                        min_rb_unit[CC_id]
                        + 1;
                        ue_sched_ctl->pre_nb_available_rbs
                        [CC_id] =
                        ue_sched_ctl->pre_nb_available_rbs
                        [CC_id] +
                        min_rb_unit[CC_id]
                        - 1;
                        nb_rbs_remaining
                        [CC_id][UE_id2] =
                        nb_rbs_remaining
                        [CC_id][UE_id2] -
                        min_rb_unit[CC_id]
                        + 1;
                        ue_sched_ctl2->pre_nb_available_rbs
                        [CC_id] =
                        ue_sched_ctl2->pre_nb_available_rbs
                        [CC_id] +
                        min_rb_unit[CC_id]
                        - 1;
                    } else {

                        nb_rbs_remaining
                        [CC_id][UE_id] =
                        nb_rbs_remaining
                        [CC_id][UE_id] - 4;
                        ue_sched_ctl->pre_nb_available_rbs
                        [CC_id] =
                        ue_sched_ctl->pre_nb_available_rbs
                        [CC_id] + 4;
                        nb_rbs_remaining
                        [CC_id][UE_id2] =
                        nb_rbs_remaining
                        [CC_id][UE_id2] -
                        4;
                        ue_sched_ctl2->pre_nb_available_rbs
                        [CC_id] =
                        ue_sched_ctl2->pre_nb_available_rbs
                        [CC_id] + 4;
                    }

                    break;
                    }
                }
                }
            }
            }
        }
        }
993
#endif
994 995 996 997 998
    }
  }
}

void dlsch_scheduler_pre_processor_intraslice_sharing(module_id_t Mod_id,
999
                                                      int slice_idx,
1000 1001 1002 1003 1004
                                                      int min_rb_unit[NFAPI_CC_MAX],
                                                      uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
                                                      uint16_t nb_rbs_accounted[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
                                                      uint16_t nb_rbs_remaining[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
                                                      uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX],
1005 1006
                                                      uint8_t MIMO_mode_indicator[NFAPI_CC_MAX][N_RBG_MAX])
{
1007 1008
  int UE_id, CC_id;
  int i;
1009
#ifdef TM5
1010
  uint8_t transmission_mode;
1011
#endif
1012 1013
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  int N_RBG[NFAPI_CC_MAX];
1014
  slice_info_t *sli = &RC.mac[Mod_id]->slice_info;
1015
  uint8_t (*slice_allocation_mask)[N_RBG_MAX] = sli->pre_processor_results[slice_idx].slice_allocation_mask;
1016

1017
  decode_slice_positioning(Mod_id, slice_idx, slice_allocation_mask);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
    COMMON_channels_t *cc = &RC.mac[Mod_id]->common_channels[CC_id];
    N_RBG[CC_id] = to_rbg(cc->mib->message.dl_Bandwidth);
  }

  // Remaining RBs are allocated to high priority UEs
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {

    if (UE_RNTI(Mod_id, UE_id) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
1029
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
1030 1031 1032 1033 1034 1035 1036

    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      nb_rbs_remaining[CC_id][UE_id] =
              nb_rbs_required[CC_id][UE_id] - nb_rbs_accounted[CC_id][UE_id] + nb_rbs_remaining[CC_id][UE_id];
      if (nb_rbs_remaining[CC_id][UE_id] < 0)
        abort();
1037
#ifdef TM5
1038
      transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
1039
#endif
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

      if (nb_rbs_required[CC_id][UE_id] > 0)
        LOG_D(MAC,
              "Step 2: nb_rbs_remaining[%d][%d]= %d (accounted %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
              CC_id,
              UE_id,
              nb_rbs_remaining[CC_id][UE_id],
              nb_rbs_accounted[CC_id][UE_id],
              nb_rbs_required[CC_id][UE_id],
              UE_list->UE_sched_ctrl[UE_id].pre_nb_available_rbs[CC_id],
              N_RBG[CC_id],
              min_rb_unit[CC_id]);

      LOG_T(MAC, "calling dlsch_scheduler_pre_processor_allocate .. \n ");
      dlsch_scheduler_pre_processor_allocate(Mod_id,
                                             UE_id,
                                             CC_id,
                                             N_RBG[CC_id],
                                             min_rb_unit[CC_id],
                                             nb_rbs_required,
                                             nb_rbs_remaining,
                                             rballoc_sub,
                                             slice_allocation_mask,
                                             MIMO_mode_indicator);

#ifdef TM5
      // data chanel TM5: to be revisited
    if ((round == 0) &&
    (transmission_mode == 5) &&
    (ue_sched_ctl->dl_pow_off[CC_id] != 1)) {

    for (j = 0; j < N_RBG[CC_id]; j += 2) {

        if ((((j == (N_RBG[CC_id] - 1))
          && (rballoc_sub[CC_id][j] == 0)
          && (ue_sched_ctl->
              rballoc_sub_UE[CC_id][j] == 0))
         || ((j < (N_RBG[CC_id] - 1))
             && (rballoc_sub[CC_id][j + 1] == 0)
             &&
             (ue_sched_ctl->rballoc_sub_UE
              [CC_id][j + 1] == 0)))
        && (nb_rbs_remaining[CC_id][UE_id]
            > 0)) {

        for (i = UE_list->next[UE_id + 1]; i >= 0;
             i = UE_list->next[i]) {

            UE_id2 = i;
            rnti2 = UE_RNTI(Mod_id, UE_id2);
            ue_sched_ctl2 =
            &UE_list->UE_sched_ctrl[UE_id2];
            round2 = ue_sched_ctl2->round[CC_id];
            if (rnti2 == NOT_A_RNTI)
            continue;
            if (UE_list->
            UE_sched_ctrl
            [UE_id2].ul_out_of_sync == 1)
            continue;

            eNB_UE_stats2 =
            UE_list->
            eNB_UE_stats[CC_id][UE_id2];
            //mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti2,frameP,subframeP,&harq_pid2,&round2,0);

            if ((mac_eNB_get_rrc_status
             (Mod_id,
              rnti2) >= RRC_RECONFIGURED)
            && (round2 == 0)
            &&
            (get_tmode(Mod_id, CC_id, UE_id2)
             == 5)
            && (ue_sched_ctl->
                dl_pow_off[CC_id] != 1)) {

            if ((((j == (N_RBG[CC_id] - 1))
                  &&
                  (ue_sched_ctl->rballoc_sub_UE
                   [CC_id][j] == 0))
                 || ((j < (N_RBG[CC_id] - 1))
                 &&
                 (ue_sched_ctl->
                  rballoc_sub_UE[CC_id][j +
                            1]
                  == 0)))
                &&
                (nb_rbs_remaining
                 [CC_id]
                 [UE_id2] > 0)) {

                if ((((eNB_UE_stats2->
                   DL_pmi_single ^
                   eNB_UE_stats1->
                   DL_pmi_single)
                  << (14 - j)) & 0xc000) == 0x4000) {	//MU-MIMO only for 25 RBs configuration

                rballoc_sub[CC_id][j] = 1;
                ue_sched_ctl->
                    rballoc_sub_UE[CC_id]
                    [j] = 1;
                ue_sched_ctl2->
                    rballoc_sub_UE[CC_id]
                    [j] = 1;
                MIMO_mode_indicator[CC_id]
                    [j] = 0;

                if (j < N_RBG[CC_id] - 1) {
                    rballoc_sub[CC_id][j +
                               1] =
                    1;
                    ue_sched_ctl->
                    rballoc_sub_UE
                    [CC_id][j + 1] = 1;
                    ue_sched_ctl2->rballoc_sub_UE
                    [CC_id][j + 1] = 1;
                    MIMO_mode_indicator
                    [CC_id][j + 1]
                    = 0;
                }

                ue_sched_ctl->
                    dl_pow_off[CC_id]
                    = 0;
                ue_sched_ctl2->
                    dl_pow_off[CC_id]
                    = 0;


                if ((j == N_RBG[CC_id] - 1)
                    && ((N_RB_DL == 25)
                    || (N_RB_DL ==
                        50))) {

                    nb_rbs_remaining
                    [CC_id][UE_id] =
                    nb_rbs_remaining
                    [CC_id][UE_id] -
                    min_rb_unit[CC_id]
                    + 1;
                    ue_sched_ctl->pre_nb_available_rbs
                    [CC_id] =
                    ue_sched_ctl->pre_nb_available_rbs
                    [CC_id] +
                    min_rb_unit[CC_id]
                    - 1;
                    nb_rbs_remaining
                    [CC_id][UE_id2] =
                    nb_rbs_remaining
                    [CC_id][UE_id2] -
                    min_rb_unit[CC_id]
                    + 1;
                    ue_sched_ctl2->pre_nb_available_rbs
                    [CC_id] =
                    ue_sched_ctl2->pre_nb_available_rbs
                    [CC_id] +
                    min_rb_unit[CC_id]
                    - 1;
                } else {

                    nb_rbs_remaining
                    [CC_id][UE_id] =
                    nb_rbs_remaining
                    [CC_id][UE_id] - 4;
                    ue_sched_ctl->pre_nb_available_rbs
                    [CC_id] =
                    ue_sched_ctl->pre_nb_available_rbs
                    [CC_id] + 4;
                    nb_rbs_remaining
                    [CC_id][UE_id2] =
                    nb_rbs_remaining
                    [CC_id][UE_id2] -
                    4;
                    ue_sched_ctl2->pre_nb_available_rbs
                    [CC_id] =
                    ue_sched_ctl2->pre_nb_available_rbs
                    [CC_id] + 4;
                }

                break;
                }
            }
            }
        }
        }
    }
    }
#endif
    }
  }
1229
}
1230

1231 1232 1233
// This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
void
dlsch_scheduler_pre_processor(module_id_t Mod_id,
1234
                              int slice_idx,
1235 1236
                              frame_t frameP,
                              sub_frame_t subframeP,
1237 1238
                              int *mbsfn_flag,
                              uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX])
1239
{
1240 1241 1242
  int UE_id;
  uint8_t CC_id;
  uint16_t i, j;
1243

1244
  int min_rb_unit[NFAPI_CC_MAX];
1245

1246
  slice_info_t *sli = &RC.mac[Mod_id]->slice_info;
1247 1248 1249 1250
  uint16_t (*nb_rbs_required)[MAX_MOBILES_PER_ENB]  = sli->pre_processor_results[slice_idx].nb_rbs_required;
  uint16_t (*nb_rbs_accounted)[MAX_MOBILES_PER_ENB] = sli->pre_processor_results[slice_idx].nb_rbs_accounted;
  uint16_t (*nb_rbs_remaining)[MAX_MOBILES_PER_ENB] = sli->pre_processor_results[slice_idx].nb_rbs_remaining;
  uint8_t  (*MIMO_mode_indicator)[N_RBG_MAX]     = sli->pre_processor_results[slice_idx].MIMO_mode_indicator;
1251

1252 1253 1254
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_sched_ctrl *ue_sched_ctl;
//  int rrc_status = RRC_IDLE;
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264
#ifdef TM5
  int harq_pid1 = 0;
  int round1 = 0, round2 = 0;
  int UE_id2;
  uint16_t i1, i2, i3;
  rnti_t rnti1, rnti2;
  LTE_eNB_UE_stats *eNB_UE_stats1 = NULL;
  LTE_eNB_UE_stats *eNB_UE_stats2 = NULL;
  UE_sched_ctrl *ue_sched_ctl1, *ue_sched_ctl2;
1265
#endif
1266

1267
  // Initialize scheduling information for all active UEs
1268
  memset(&sli->pre_processor_results[slice_idx], 0, sizeof(sli->pre_processor_results[slice_idx]));
1269
  // FIXME: After the memset above, some of the resets in reset() are redundant
1270
  dlsch_scheduler_pre_processor_reset(Mod_id, slice_idx, frameP, subframeP,
1271 1272 1273 1274
                                      min_rb_unit,
                                      nb_rbs_required,
                                      rballoc_sub,
                                      MIMO_mode_indicator,
1275
                                      mbsfn_flag); // FIXME: Not sure if useful
1276

1277
  // STATUS
1278
  // Store the DLSCH buffer for each logical channel
1279
  store_dlsch_buffer(Mod_id, slice_idx, frameP, subframeP);
1280

1281
  // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
1282
  assign_rbs_required(Mod_id, slice_idx, frameP, subframeP,
1283 1284
                      min_rb_unit,
                      nb_rbs_required);
1285

1286
  // Sorts the user on the basis of dlsch logical channel buffer and CQI
1287
  sort_UEs(Mod_id, slice_idx, frameP, subframeP);
1288

1289
  // ACCOUNTING
1290
  // This procedure decides the number of RBs to allocate
1291
  dlsch_scheduler_pre_processor_accounting(Mod_id, slice_idx, frameP, subframeP,
1292
                                           min_rb_unit,
1293 1294 1295 1296
                                           nb_rbs_required,
                                           nb_rbs_accounted);
  // POSITIONING
  // This procedure does the main allocation of the RBs
1297
  dlsch_scheduler_pre_processor_positioning(Mod_id, slice_idx,
1298 1299 1300
                                            min_rb_unit,
                                            nb_rbs_required,
                                            nb_rbs_accounted,
1301
                                            nb_rbs_remaining,
1302 1303
                                            rballoc_sub,
                                            MIMO_mode_indicator);
1304

1305 1306
  // SHARING
  // If there are available RBs left in the slice, allocate them to the highest priority UEs
1307
  if (RC.mac[Mod_id]->slice_info.intraslice_share_active) {
1308
    dlsch_scheduler_pre_processor_intraslice_sharing(Mod_id, slice_idx,
1309 1310 1311 1312 1313 1314 1315
                                                     min_rb_unit,
                                                     nb_rbs_required,
                                                     nb_rbs_accounted,
                                                     nb_rbs_remaining,
                                                     rballoc_sub,
                                                     MIMO_mode_indicator);
  }
1316

1317
#ifdef TM5
1318
  // This has to be revisited!!!!
1319
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
1320 1321 1322 1323 1324
    COMMON_channels_t *cc = &RC.mac[Mod_id]->common_channels[CC_id];
    int N_RBG = to_rbg(cc->mib->message.dl_Bandwidth);
    i1 = 0;
    i2 = 0;
    i3 = 0;
1325

1326
    for (j = 0; j < N_RBG; j++) {
1327 1328 1329 1330 1331 1332 1333
      if (MIMO_mode_indicator[CC_id][j] == 2) {
      i1 = i1 + 1;
      } else if (MIMO_mode_indicator[CC_id][j] == 1) {
      i2 = i2 + 1;
      } else if (MIMO_mode_indicator[CC_id][j] == 0) {
      i3 = i3 + 1;
      }
1334
    }
1335

1336
    if ((i1 < N_RBG) && (i2 > 0) && (i3 == 0)) {
1337 1338 1339
      PHY_vars_eNB_g[Mod_id][CC_id]->check_for_SUMIMO_transmissions =
      PHY_vars_eNB_g[Mod_id][CC_id]->
      check_for_SUMIMO_transmissions + 1;
1340
    }
1341

1342
    if (i3 == N_RBG && i1 == 0 && i2 == 0) {
1343 1344 1345
      PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions =
      PHY_vars_eNB_g[Mod_id][CC_id]->FULL_MUMIMO_transmissions +
      1;
1346
    }
1347

1348
    if ((i1 < N_RBG) && (i3 > 0)) {
1349 1350 1351
      PHY_vars_eNB_g[Mod_id][CC_id]->check_for_MUMIMO_transmissions =
      PHY_vars_eNB_g[Mod_id][CC_id]->
      check_for_MUMIMO_transmissions + 1;
1352
    }
1353

1354
    PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions =
1355 1356
      PHY_vars_eNB_g[Mod_id][CC_id]->check_for_total_transmissions +
      1;
1357

1358
  }
1359 1360
#endif

1361
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
1362

1363 1364 1365 1366
    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].dl_pow_off = dl_pow_off[UE_id];
1367 1368
      COMMON_channels_t *cc = &RC.mac[Mod_id]->common_channels[CC_id];
      int N_RBG = to_rbg(cc->mib->message.dl_Bandwidth);
1369

1370 1371 1372 1373
      if (ue_sched_ctl->pre_nb_available_rbs[CC_id] > 0) {
        LOG_D(MAC, "******************DL Scheduling Information for UE%d ************************\n", UE_id);
        LOG_D(MAC, "dl power offset UE%d = %d \n", UE_id, ue_sched_ctl->dl_pow_off[CC_id]);
        LOG_D(MAC, "***********RB Alloc for every subband for UE%d ***********\n", UE_id);
1374

1375
        for (j = 0; j < N_RBG; j++) {
1376 1377 1378 1379 1380 1381
          //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].rballoc_sub[UE_id] = rballoc_sub_UE[CC_id][UE_id][UE_id];
          LOG_D(MAC, "RB Alloc for UE%d and Subband%d = %d\n", UE_id, j, ue_sched_ctl->rballoc_sub_UE[CC_id][j]);
        }

        //PHY_vars_eNB_g[Mod_id]->mu_mimo_mode[UE_id].pre_nb_available_rbs = pre_nb_available_rbs[CC_id][UE_id];
        LOG_D(MAC, "[eNB %d][SLICE %d]Total RBs allocated for UE%d = %d\n",
1382 1383
              Mod_id, RC.mac[Mod_id]->slice_info.dl[slice_idx].id, UE_id,
              ue_sched_ctl->pre_nb_available_rbs[CC_id]);
1384
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1385
    }
1386
  }
1387 1388
}

Cedric Roux's avatar
Cedric Roux committed
1389
#define SF0_LIMIT 1
1390

1391
void
1392
dlsch_scheduler_pre_processor_reset(module_id_t module_idP,
1393
                                    int slice_idx,
1394 1395 1396 1397
                                    frame_t frameP,
                                    sub_frame_t subframeP,
                                    int min_rb_unit[NFAPI_CC_MAX],
                                    uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
1398 1399
                                    uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX],
                                    uint8_t MIMO_mode_indicator[NFAPI_CC_MAX][N_RBG_MAX],
1400
                                    int *mbsfn_flag)
1401
{
1402 1403 1404

  int UE_id;
  uint8_t CC_id;
1405 1406 1407
  int i, j;
  UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
1408 1409
  int N_RB_DL, RBGsize, RBGsize_last;
  int N_RBG[NFAPI_CC_MAX];
1410

Cedric Roux's avatar
Cedric Roux committed
1411
#ifdef SF0_LIMIT
1412
  int sf0_lower, sf0_upper;
1413
#endif
1414

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  rnti_t rnti;
  UE_list_t *UE_list;
  UE_sched_ctrl *ue_sched_ctl;
  uint8_t *vrb_map;
  COMMON_channels_t *cc;
//
  for (CC_id = 0; CC_id < NFAPI_CC_MAX; CC_id++) {

    LOG_D(MAC, "Running preprocessor for UE %d (%x)\n", UE_id,(int)(UE_RNTI(module_idP, UE_id)));
    // initialize harq_pid and round
    cc = &RC.mac[module_idP]->common_channels[CC_id];
    N_RBG[CC_id] = to_rbg(cc->mib->message.dl_Bandwidth);
    min_rb_unit[CC_id] = get_min_rb_unit(module_idP, CC_id);

    if (mbsfn_flag[CC_id] > 0)    // If this CC is allocated for MBSFN skip it here
      continue;

    for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; ++UE_id) {


      UE_list = &RC.mac[module_idP]->UE_list;
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
      rnti = UE_RNTI(module_idP, UE_id);
1438

1439 1440 1441 1442 1443 1444 1445

      if (rnti == NOT_A_RNTI)
        continue;

      if (UE_list->active[UE_id] != TRUE)
        continue;

1446
      if (!ue_dl_slice_membership(module_idP, UE_id, slice_idx))
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
        continue;

      LOG_D(MAC, "Running preprocessor for UE %d (%x)\n", UE_id, rnti);
      // initialize harq_pid and round
      if (ue_sched_ctl->ta_timer)
        ue_sched_ctl->ta_timer--;


      /*
         eNB_UE_stats *eNB_UE_stats;

         if (eNB_UE_stats == NULL)
         return;


         mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,
         frameP,subframeP,
         &ue_sched_ctl->harq_pid[CC_id],
         &ue_sched_ctl->round[CC_id],
         openair_harq_DL);


         if (ue_sched_ctl->ta_timer == 0) {

         // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...

         ue_sched_ctl->ta_timer = 20;  // wait 20 subframes before taking TA measurement from PHY
         switch (N_RB_DL) {
         case 6:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update;
         break;

         case 15:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/2;
         break;

         case 25:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/4;
         break;

         case 50:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/8;
         break;

         case 75:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/12;
         break;

         case 100:
         ue_sched_ctl->ta_update = eNB_UE_stats->timing_advance_update/16;
         break;
         }
         // clear the update in case PHY does not have a new measurement after timer expiry
         eNB_UE_stats->timing_advance_update =  0;
         }
         else {
         ue_sched_ctl->ta_timer--;
         ue_sched_ctl->ta_update =0; // don't trigger a timing advance command
         }


         if (UE_id==0) {
         VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_TIMING_ADVANCE,ue_sched_ctl->ta_update);
         }
       */

      nb_rbs_required[CC_id][UE_id] = 0;
      ue_sched_ctl->pre_nb_available_rbs[CC_id] = 0;
      ue_sched_ctl->dl_pow_off[CC_id] = 2;

1517 1518
      for (i = 0; i < N_RBG[CC_id]; i++) {
        ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 0;
1519
      }
1520 1521 1522
    }

    N_RB_DL = to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->message.dl_Bandwidth);
1523 1524

#ifdef SF0_LIMIT
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    switch (N_RBG[CC_id]) {
      case 6:
        sf0_lower = 0;
        sf0_upper = 5;
        break;
      case 8:
        sf0_lower = 2;
        sf0_upper = 5;
        break;
      case 13:
        sf0_lower = 4;
        sf0_upper = 7;
        break;
      case 17:
        sf0_lower = 7;
        sf0_upper = 9;
        break;
      case 25:
        sf0_lower = 11;
        sf0_upper = 13;
        break;
      default:
        AssertFatal(1 == 0, "unsupported RBs (%d)\n", N_RB_DL);
    }
1549
#endif
Cedric Roux's avatar
Cedric Roux committed
1550

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
    switch (N_RB_DL) {
      case 6:
        RBGsize = 1;
        RBGsize_last = 1;
        break;
      case 15:
        RBGsize = 2;
        RBGsize_last = 1;
        break;
      case 25:
        RBGsize = 2;
        RBGsize_last = 1;
        break;
      case 50:
        RBGsize = 3;
        RBGsize_last = 2;
        break;
      case 75:
        RBGsize = 4;
        RBGsize_last = 3;
        break;
      case 100:
        RBGsize = 4;
        RBGsize_last = 4;
        break;
      default:
        AssertFatal(1 == 0, "unsupported RBs (%d)\n", N_RB_DL);
    }

    vrb_map = RC.mac[module_idP]->common_channels[CC_id].vrb_map;
    // Initialize Subbands according to VRB map
    for (i = 0; i < N_RBG[CC_id]; i++) {
      int rb_size = i == N_RBG[CC_id] - 1 ? RBGsize_last : RBGsize;
1584 1585


Cedric Roux's avatar
Cedric Roux committed
1586
#ifdef SF0_LIMIT
1587 1588 1589 1590 1591 1592 1593
      // for avoiding 6+ PRBs around DC in subframe 0 (avoid excessive errors)
      /* TODO: make it proper - allocate those RBs, do not "protect" them, but
        * compute number of available REs and limit MCS according to the
        * TBS table 36.213 7.1.7.2.1-1 (can be done after pre-processor)
        */
      if (subframeP == 0 && i >= sf0_lower && i <= sf0_upper)
        rballoc_sub[CC_id][i] = 1;
1594
#endif
1595

1596 1597 1598 1599 1600 1601
      // for SI-RNTI,RA-RNTI and P-RNTI allocations
      for (j = 0; j < rb_size; j++) {
        if (vrb_map[j + (i*RBGsize)] != 0) {
          rballoc_sub[CC_id][i] = 1;
          LOG_D(MAC, "Frame %d, subframe %d : vrb %d allocated\n", frameP, subframeP, j + (i*RBGsize));
          break;
1602
        }
1603
      }
1604 1605 1606
      LOG_D(MAC, "Frame %d Subframe %d CC_id %d RBG %i : rb_alloc %d\n",
            frameP, subframeP, CC_id, i, rballoc_sub[CC_id][i]);
      MIMO_mode_indicator[CC_id][i] = 2;
1607
    }
1608
  }
1609 1610 1611
}


1612 1613
void
dlsch_scheduler_pre_processor_allocate(module_id_t Mod_id,
1614 1615 1616 1617 1618 1619
                                       int UE_id,
                                       uint8_t CC_id,
                                       int N_RBG,
                                       int min_rb_unit,
                                       uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
                                       uint16_t nb_rbs_remaining[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
1620
                                       uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX],
1621
                                       uint8_t slice_allocation_mask[NFAPI_CC_MAX][N_RBG_MAX],
1622
                                       uint8_t MIMO_mode_indicator[NFAPI_CC_MAX][N_RBG_MAX])
1623
{
1624
  int i;
1625
  int tm = get_tmode(Mod_id, CC_id, UE_id);
1626 1627 1628 1629 1630 1631
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
  int N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);

  for (i = 0; i < N_RBG; i++) {

1632 1633 1634 1635 1636 1637
    if (rballoc_sub[CC_id][i] != 0) continue;
    if (ue_sched_ctl->rballoc_sub_UE[CC_id][i] != 0) continue;
    if (nb_rbs_remaining[CC_id][UE_id] <= 0) continue;
    if (ue_sched_ctl->pre_nb_available_rbs[CC_id] >= nb_rbs_required[CC_id][UE_id]) continue;
    if (ue_sched_ctl->dl_pow_off[CC_id] == 0) continue;
    if (slice_allocation_mask[CC_id][i] == 0) continue;
1638 1639

    if ((i == N_RBG - 1) && ((N_RB_DL == 25) || (N_RB_DL == 50))) {
1640 1641
      // Allocating last, smaller RBG
      if (nb_rbs_remaining[CC_id][UE_id] >= min_rb_unit - 1) {
1642 1643 1644
        rballoc_sub[CC_id][i] = 1;
        ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
        MIMO_mode_indicator[CC_id][i] = 1;
1645
        if (tm == 5) {
1646 1647
          ue_sched_ctl->dl_pow_off[CC_id] = 1;
        }
1648
        nb_rbs_remaining[CC_id][UE_id] = nb_rbs_remaining[CC_id][UE_id] - min_rb_unit + 1;
1649 1650 1651
        ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit - 1;
      }
    } else {
1652 1653
      // Allocating a standard-sized RBG
      if (nb_rbs_remaining[CC_id][UE_id] >= min_rb_unit) {
1654 1655 1656
        rballoc_sub[CC_id][i] = 1;
        ue_sched_ctl->rballoc_sub_UE[CC_id][i] = 1;
        MIMO_mode_indicator[CC_id][i] = 1;
1657
        if (tm == 5) {
1658 1659
          ue_sched_ctl->dl_pow_off[CC_id] = 1;
        }
1660
        nb_rbs_remaining[CC_id][UE_id] = nb_rbs_remaining[CC_id][UE_id] - min_rb_unit;
1661 1662
        ue_sched_ctl->pre_nb_available_rbs[CC_id] = ue_sched_ctl->pre_nb_available_rbs[CC_id] + min_rb_unit;
      }
1663
    }
1664
  }
1665 1666 1667
}


1668
/// ULSCH PRE_PROCESSOR
1669

1670
void ulsch_scheduler_pre_processor(module_id_t module_idP,
1671
                                   int slice_idx,
1672 1673
                                   int frameP,
                                   sub_frame_t subframeP,
1674
                                   unsigned char sched_subframeP,
1675
                                   uint16_t *first_rb)
1676
{
1677 1678 1679
    int16_t i;
    uint16_t UE_id, n, r;
    uint8_t CC_id, harq_pid;
1680 1681 1682 1683 1684
    uint16_t nb_allocated_rbs[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
	total_allocated_rbs[NFAPI_CC_MAX],
	average_rbs_per_user[NFAPI_CC_MAX];
    int16_t total_remaining_rbs[NFAPI_CC_MAX];
    uint16_t total_ue_count[NFAPI_CC_MAX];
1685 1686
    rnti_t rnti = -1;
    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
1687
    slice_info_t *sli = &RC.mac[module_idP]->slice_info;
1688
    UE_TEMPLATE *UE_template = 0;
1689 1690
    UE_sched_ctrl *ue_sched_ctl;
    int N_RB_UL = 0;
Niccolò Iardella's avatar
Niccolò Iardella committed
1691
    uint16_t available_rbs, first_rb_offset;
1692

1693 1694
    LOG_D(MAC, "In ulsch_preprocessor: assign max mcs min rb\n");
    // maximize MCS and then allocate required RB according to the buffer occupancy with the limit of max available UL RB
1695
    assign_max_mcs_min_rb(module_idP, slice_idx, frameP, subframeP, first_rb);
1696 1697 1698 1699 1700 1701 1702 1703

    LOG_D(MAC, "In ulsch_preprocessor: sort ue \n");
    // sort ues
    sort_ue_ul(module_idP, frameP, subframeP);


    // we need to distribute RBs among UEs
    // step1:  reset the vars
1704
    for (CC_id = 0; CC_id < RC.nb_mac_CC[module_idP]; CC_id++) {
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
      total_allocated_rbs[CC_id] = 0;
      total_remaining_rbs[CC_id] = 0;
      average_rbs_per_user[CC_id] = 0;
      total_ue_count[CC_id] = 0;
    }

    // Step 1.5: Calculate total_ue_count
    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
      for (n = 0; n < UE_list->numactiveULCCs[i]; n++) {
        // This is the actual CC_id in the list
        CC_id = UE_list->ordered_ULCCids[n][i];
        UE_template = &UE_list->UE_template[CC_id][i];
1717
        if (!ue_ul_slice_membership(module_idP, i, slice_idx))
1718
          continue;
1719
        if (UE_template->pre_allocated_nb_rb_ul[slice_idx] > 0) {
1720 1721 1722
          total_ue_count[CC_id] += 1;
        }
      }
1723 1724
    }

1725 1726 1727
    LOG_D(MAC, "In ulsch_preprocessor: step2 \n");
    // step 2: calculate the average rb per UE
    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
1728

1729
      rnti = UE_RNTI(module_idP, i);
1730
      UE_id = i;
1731 1732 1733 1734 1735 1736 1737

      if (rnti == NOT_A_RNTI)
        continue;

      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
        continue;

1738
      if (!ue_ul_slice_membership(module_idP, UE_id, slice_idx))
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
          continue;

      LOG_D(MAC, "In ulsch_preprocessor: handling UE %d/%x\n", UE_id,
            rnti);
      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
        // This is the actual CC_id in the list
        CC_id = UE_list->ordered_ULCCids[n][UE_id];
        LOG_D(MAC,
              "In ulsch_preprocessor: handling UE %d/%x CCid %d\n",
              UE_id, rnti, CC_id);

        average_rbs_per_user[CC_id] = 0;

        /*
           if((mac_xface->get_nCCE_max(module_idP,CC_id,3,subframeP) - nCCE_to_be_used[CC_id])  > (1<<aggregation)) {
           nCCE_to_be_used[CC_id] = nCCE_to_be_used[CC_id] + (1<<aggregation);
           max_num_ue_to_be_scheduled+=1;
           } */



        N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
        ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
1762 1763
        ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_idx] =
                nb_rbs_allowed_slice(sli->ul[slice_idx].pct, N_RB_UL);
1764

1765 1766
        first_rb_offset = UE_list->first_rb_offset[CC_id][slice_idx];
        available_rbs = cmin(ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_idx],
Niccolò Iardella's avatar
Niccolò Iardella committed
1767 1768
                             N_RB_UL - first_rb[CC_id] - first_rb_offset);

1769 1770 1771
        if (total_ue_count[CC_id] == 0) {
          average_rbs_per_user[CC_id] = 0;
        } else if (total_ue_count[CC_id] == 1) {    // increase the available RBs, special case,
Niccolò Iardella's avatar
Niccolò Iardella committed
1772 1773 1774
          average_rbs_per_user[CC_id] = (uint16_t) (available_rbs + 1);
        } else if (total_ue_count[CC_id] <= available_rbs) {
          average_rbs_per_user[CC_id] = (uint16_t) floor(available_rbs / total_ue_count[CC_id]);
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
        } else {
          average_rbs_per_user[CC_id] = 1;
          LOG_W(MAC,
                "[eNB %d] frame %d subframe %d: UE %d CC %d: can't get average rb per user (should not be here)\n",
                module_idP, frameP, subframeP, UE_id, CC_id);
        }
        if (total_ue_count[CC_id] > 0)
          LOG_D(MAC, "[eNB %d] Frame %d subframe %d: total ue to be scheduled %d\n",
                module_idP, frameP, subframeP, total_ue_count[CC_id]);
      }
1785
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1786

1787 1788
    // step 3: assigne RBS
    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
1789 1790 1791 1792 1793 1794
      rnti = UE_RNTI(module_idP, i);

      if (rnti == NOT_A_RNTI)
        continue;
      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
        continue;
1795
      if (!ue_ul_slice_membership(module_idP, i, slice_idx))
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
        continue;


      UE_id = i;

      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
        // This is the actual CC_id in the list
        CC_id = UE_list->ordered_ULCCids[n][UE_id];
        UE_template = &UE_list->UE_template[CC_id][UE_id];
        harq_pid = subframe2harqpid(&RC.mac[module_idP]->common_channels[CC_id],
1806
                                    frameP, sched_subframeP);
1807 1808 1809 1810 1811 1812 1813

        //      mac_xface->get_ue_active_harq_pid(module_idP,CC_id,rnti,frameP,subframeP,&harq_pid,&round,openair_harq_UL);

        if (UE_list->UE_sched_ctrl[UE_id].round_UL[CC_id] > 0) {
          nb_allocated_rbs[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb_ul[harq_pid];
        } else {
          nb_allocated_rbs[CC_id][UE_id] =
1814
                  cmin(UE_list->UE_template[CC_id][UE_id].pre_allocated_nb_rb_ul[slice_idx],
1815 1816 1817 1818 1819 1820 1821 1822 1823
                       average_rbs_per_user[CC_id]);
        }

        total_allocated_rbs[CC_id] += nb_allocated_rbs[CC_id][UE_id];
        LOG_D(MAC,
              "In ulsch_preprocessor: assigning %d RBs for UE %d/%x CCid %d, harq_pid %d\n",
              nb_allocated_rbs[CC_id][UE_id], UE_id, rnti, CC_id,
              harq_pid);
      }
1824
    }
1825

1826
    // step 4: assigne the remaining RBs and set the pre_allocated rbs accordingly
1827
  for (r = 0; r < 2; r++) {
1828

1829 1830 1831 1832 1833 1834 1835
    for (i = UE_list->head_ul; i >= 0; i = UE_list->next_ul[i]) {
      rnti = UE_RNTI(module_idP, i);

      if (rnti == NOT_A_RNTI)
        continue;
      if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
        continue;
1836
      if (!ue_ul_slice_membership(module_idP, i, slice_idx))
1837 1838 1839
        continue;

      UE_id = i;
1840
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
1841 1842 1843 1844 1845

      for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
        // This is the actual CC_id in the list
        CC_id = UE_list->ordered_ULCCids[n][UE_id];
        UE_template = &UE_list->UE_template[CC_id][UE_id];
Niccolò Iardella's avatar
Niccolò Iardella committed
1846
        N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
1847 1848
        first_rb_offset = UE_list->first_rb_offset[CC_id][slice_idx];
        available_rbs = cmin(ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_idx],
Niccolò Iardella's avatar
Niccolò Iardella committed
1849 1850
                             N_RB_UL - first_rb[CC_id] - first_rb_offset);
        total_remaining_rbs[CC_id] = available_rbs - total_allocated_rbs[CC_id];
1851 1852 1853 1854 1855 1856

        if (total_ue_count[CC_id] == 1) {
          total_remaining_rbs[CC_id] += 1;
        }

        if (r == 0) {
1857 1858
          while ((UE_template->pre_allocated_nb_rb_ul[slice_idx] > 0)
                 && (nb_allocated_rbs[CC_id][UE_id] < UE_template->pre_allocated_nb_rb_ul[slice_idx])
1859 1860 1861
                 && (total_remaining_rbs[CC_id] > 0)) {
            nb_allocated_rbs[CC_id][UE_id] =
                    cmin(nb_allocated_rbs[CC_id][UE_id] + 1,
1862
                         UE_template->pre_allocated_nb_rb_ul[slice_idx]);
1863 1864 1865 1866
            total_remaining_rbs[CC_id]--;
            total_allocated_rbs[CC_id]++;
          }
        } else {
1867
          UE_template->pre_allocated_nb_rb_ul[slice_idx] =
1868 1869 1870 1871 1872 1873 1874
                  nb_allocated_rbs[CC_id][UE_id];
          LOG_D(MAC,
                "******************UL Scheduling Information for UE%d CC_id %d ************************\n",
                UE_id, CC_id);
          LOG_D(MAC,
                "[eNB %d] total RB allocated for UE%d CC_id %d  = %d\n",
                module_idP, UE_id, CC_id,
1875
                UE_template->pre_allocated_nb_rb_ul[slice_idx]);
1876 1877
        }
      }
1878
    }
1879
  }
1880 1881
}

1882
void
1883
assign_max_mcs_min_rb(module_id_t module_idP, int slice_idx, int frameP,
1884
		      sub_frame_t subframeP, uint16_t * first_rb)
1885 1886
{

1887 1888 1889 1890
  int i;
  uint16_t n, UE_id;
  uint8_t CC_id;
  rnti_t rnti = -1;
1891
  int mcs;
1892 1893 1894
  int rb_table_index = 0, tbs, tx_power;
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  UE_list_t *UE_list = &eNB->UE_list;
1895
  slice_info_t *sli = &RC.mac[module_idP]->slice_info;
1896

1897 1898 1899 1900
  UE_TEMPLATE *UE_template;
  UE_sched_ctrl *ue_sched_ctl;
  int Ncp;
  int N_RB_UL;
Niccolò Iardella's avatar
Niccolò Iardella committed
1901
  int first_rb_offset, available_rbs;
1902

1903
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
1904 1905 1906 1907 1908 1909 1910 1911 1912
    if (UE_list->active[i] != TRUE)
      continue;

    rnti = UE_RNTI(module_idP, i);

    if (rnti == NOT_A_RNTI)
      continue;
    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
      continue;
1913
    if (!ue_ul_slice_membership(module_idP, i, slice_idx))
1914 1915
      continue;

1916 1917 1918
    if (UE_list->UE_sched_ctrl[i].phr_received == 1) {
      /* if we've received the power headroom information the UE, we can go to
       * maximum mcs */
1919
      mcs = cmin(20, sli->ul[slice_idx].maxmcs);
1920 1921
    } else {
      /* otherwise, limit to QPSK PUSCH */
1922
      mcs = cmin(10, sli->ul[slice_idx].maxmcs);
1923
    }
1924

1925
    UE_id = i;
1926

1927 1928 1929 1930 1931
    for (n = 0; n < UE_list->numactiveULCCs[UE_id]; n++) {
      // This is the actual CC_id in the list
      CC_id = UE_list->ordered_ULCCids[n][UE_id];


1932 1933

      AssertFatal(CC_id < RC.nb_mac_CC[module_idP],
1934
                  "CC_id %u should be < %u, loop n=%u < numactiveULCCs[%u]=%u",
1935
                  CC_id, NFAPI_CC_MAX, n, UE_id,
1936 1937 1938
                  UE_list->numactiveULCCs[UE_id]);

      UE_template = &UE_list->UE_template[CC_id][UE_id];
1939
      UE_template->pre_assigned_mcs_ul = mcs;
1940 1941 1942 1943
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];

      Ncp = RC.mac[module_idP]->common_channels[CC_id].Ncp;
      N_RB_UL = to_prb(RC.mac[module_idP]->common_channels[CC_id].ul_Bandwidth);
1944 1945
      ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_idx] =
              nb_rbs_allowed_slice(sli->ul[slice_idx].pct, N_RB_UL);
1946

1947 1948 1949 1950
      int bytes_to_schedule = UE_template->estimated_ul_buffer - UE_template->scheduled_ul_bytes;
      if (bytes_to_schedule < 0) bytes_to_schedule = 0;
      int bits_to_schedule = bytes_to_schedule * 8;

1951
      // if this UE has UL traffic
1952
      if (bits_to_schedule > 0) {
1953
        tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, 3) << 3; // 1 or 2 PRB with cqi enabled does not work well!
1954 1955 1956 1957 1958 1959
        rb_table_index = 2;

        // fixme: set use_srs flag
        tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);

        while ((((UE_template->phr_info - tx_power) < 0)
1960
              || (tbs > bits_to_schedule))
1961
              && (UE_template->pre_assigned_mcs_ul > 3)) {
1962
          // LOG_I(MAC,"UE_template->phr_info %d tx_power %d mcs %d\n", UE_template->phr_info,tx_power, mcs);
1963 1964
          UE_template->pre_assigned_mcs_ul--;
          tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, rb_table[rb_table_index]) << 3;
1965 1966 1967
          tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);   // fixme: set use_srs
        }

1968 1969
        first_rb_offset = UE_list->first_rb_offset[CC_id][slice_idx];
        available_rbs = cmin(ue_sched_ctl->max_rbs_allowed_slice_uplink[CC_id][slice_idx],
Niccolò Iardella's avatar
Niccolò Iardella committed
1970 1971
                             N_RB_UL - first_rb[CC_id] - first_rb_offset);

1972
        while ((tbs < bits_to_schedule)
Niccolò Iardella's avatar
Niccolò Iardella committed
1973 1974 1975
               && (rb_table[rb_table_index] < available_rbs)
               && ((UE_template->phr_info - tx_power) > 0)
               && (rb_table_index < 32)) {
1976
          rb_table_index++;
1977
          tbs = get_TBS_UL(UE_template->pre_assigned_mcs_ul, rb_table[rb_table_index]) << 3;
1978 1979 1980
          tx_power = estimate_ue_tx_power(tbs, rb_table[rb_table_index], 0, Ncp, 0);
        }

Niccolò Iardella's avatar
Niccolò Iardella committed
1981
        if (rb_table[rb_table_index] > (available_rbs - 1)) {
1982 1983 1984 1985 1986 1987 1988 1989
          rb_table_index--;
        }
        // 1 or 2 PRB with cqi enabled does not work well
        if (rb_table[rb_table_index] < 3) {
            rb_table_index = 2;	//3PRB
        }

        UE_template->pre_allocated_rb_table_index_ul = rb_table_index;
1990
        UE_template->pre_allocated_nb_rb_ul[slice_idx] = rb_table[rb_table_index];
1991 1992 1993 1994 1995
        LOG_D(MAC,
              "[eNB %d] frame %d subframe %d: for UE %d CC %d: pre-assigned mcs %d, pre-allocated rb_table[%d]=%d RBs (phr %d, tx power %d)\n",
              module_idP, frameP, subframeP, UE_id, CC_id,
              UE_template->pre_assigned_mcs_ul,
              UE_template->pre_allocated_rb_table_index_ul,
1996
              UE_template->pre_allocated_nb_rb_ul[slice_idx],
1997 1998 1999 2000 2001 2002 2003 2004
              UE_template->phr_info, tx_power);
      } else {
        /* if UE has pending scheduling request then pre-allocate 3 RBs */
        //if (UE_template->ul_active == 1 && UE_template->ul_SR == 1) {
        if (UE_is_to_be_scheduled(module_idP, CC_id, i)) {
          /* use QPSK mcs */
          UE_template->pre_assigned_mcs_ul = 10;
          UE_template->pre_allocated_rb_table_index_ul = 2;
2005
          UE_template->pre_allocated_nb_rb_ul[slice_idx] = 3;
2006 2007 2008
        } else {
          UE_template->pre_assigned_mcs_ul = 0;
          UE_template->pre_allocated_rb_table_index_ul = -1;
2009
          UE_template->pre_allocated_nb_rb_ul[slice_idx] = 0;
2010 2011
        }
      }
2012
    }
2013
  }
2014 2015
}

2016
struct sort_ue_ul_params {
2017 2018 2019
    int module_idP;
    int frameP;
    int subframeP;
2020 2021 2022 2023
};

static int ue_ul_compare(const void *_a, const void *_b, void *_params)
{
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
    struct sort_ue_ul_params *params = _params;
    UE_list_t *UE_list = &RC.mac[params->module_idP]->UE_list;

    int UE_id1 = *(const int *) _a;
    int UE_id2 = *(const int *) _b;

    int rnti1 = UE_RNTI(params->module_idP, UE_id1);
    int pCCid1 = UE_PCCID(params->module_idP, UE_id1);
    int round1 = maxround(params->module_idP, rnti1, params->frameP,
			  params->subframeP, 1);

    int rnti2 = UE_RNTI(params->module_idP, UE_id2);
    int pCCid2 = UE_PCCID(params->module_idP, UE_id2);
    int round2 = maxround(params->module_idP, rnti2, params->frameP,
			  params->subframeP, 1);

    if (round1 > round2)
	return -1;
    if (round1 < round2)
	return 1;

    if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] >
	UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
	return -1;
    if (UE_list->UE_template[pCCid1][UE_id1].ul_buffer_info[LCGID0] <
	UE_list->UE_template[pCCid2][UE_id2].ul_buffer_info[LCGID0])
	return 1;

2052 2053 2054 2055 2056 2057
    int bytes_to_schedule1 = UE_list->UE_template[pCCid1][UE_id1].estimated_ul_buffer - UE_list->UE_template[pCCid1][UE_id1].scheduled_ul_bytes;
    if (bytes_to_schedule1 < 0) bytes_to_schedule1 = 0;
    int bytes_to_schedule2 = UE_list->UE_template[pCCid2][UE_id2].estimated_ul_buffer - UE_list->UE_template[pCCid2][UE_id2].scheduled_ul_bytes;
    if (bytes_to_schedule2 < 0) bytes_to_schedule2 = 0;

    if (bytes_to_schedule1 > bytes_to_schedule2)
2058
	return -1;
2059
    if (bytes_to_schedule1 < bytes_to_schedule2)
2060 2061 2062 2063 2064 2065 2066 2067
	return 1;

    if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul >
	UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
	return -1;
    if (UE_list->UE_template[pCCid1][UE_id1].pre_assigned_mcs_ul <
	UE_list->UE_template[pCCid2][UE_id2].pre_assigned_mcs_ul)
	return 1;
2068

2069
    return 0;
2070
}
2071

2072
void sort_ue_ul(module_id_t module_idP, int frameP, sub_frame_t subframeP)
2073
{
2074
    int i;
2075
    int list[MAX_MOBILES_PER_ENB];
2076 2077 2078
    int list_size = 0;
    int rnti;
    struct sort_ue_ul_params params = { module_idP, frameP, subframeP };
2079

2080
    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
2081

2082
    for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
2083 2084 2085 2086 2087 2088
	if (UE_list->active[i] == FALSE)
	    continue;
	if ((rnti = UE_RNTI(module_idP, i)) == NOT_A_RNTI)
	    continue;
	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
	    continue;
2089

2090 2091 2092
	list[list_size] = i;
	list_size++;
    }
2093

2094
    qsort_r(list, list_size, sizeof(int), ue_ul_compare, &params);
2095

2096 2097 2098 2099 2100 2101 2102 2103
    if (list_size) {
	for (i = 0; i < list_size - 1; i++)
	    UE_list->next_ul[list[i]] = list[i + 1];
	UE_list->next_ul[list[list_size - 1]] = -1;
	UE_list->head_ul = list[0];
    } else {
	UE_list->head_ul = -1;
    }
2104
}