pre_processor.c 94.1 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
Cedric Roux's avatar
Cedric Roux committed
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
nikaeinn's avatar
nikaeinn committed
25
 * \date 2013 - 2014
26
 * \email navid.nikaein@eurecom.fr
nikaeinn's avatar
nikaeinn committed
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
39
40
41
42
#include "PHY/defs.h"
#include "PHY/extern.h"

#include "SCHED/defs.h"
#include "SCHED/extern.h"

#include "LAYER2/MAC/defs.h"
43
#include "LAYER2/MAC/proto.h"
44
45
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
46
#include "UTIL/LOG/vcd_signal_dumper.h"
47
48
49
50
51
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"
#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
52
#include "rlc.h"
53
54


gauthier's avatar
gauthier committed
55

56
57
58
59
60
61
#define DEBUG_eNB_SCHEDULER 1
#define DEBUG_HEADER_PARSING 1
//#define DEBUG_PACKET_TRACE 1

//#define ICIC 0

62
/* this function checks that get_eNB_UE_stats returns
Cedric Roux's avatar
Cedric Roux committed
63
 * a non-NULL pointer for all the active CCs of an UE
64
 */
65
/*
66
int phy_stats_exist(module_id_t Mod_id, int rnti)
67
68
{
  int CC_id;
Cedric Roux's avatar
Cedric Roux committed
69
70
  int i;
  int UE_id          = find_UE_id(Mod_id, rnti);
71
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
Cedric Roux's avatar
Cedric Roux committed
72
73
74
75
76
77
78
79
80
81
82
83
  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];
84
85
    if (mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti) == NULL)
      return 0;
86
  }
87
88
  return 1;
}
89
*/
90

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

97
98
99
100
101
    int UE_id, i;
    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;
knopp's avatar
   
knopp committed
102

103
104
105
    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
	if (UE_list->active[UE_id] != TRUE)
	    continue;
106

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
	rnti = UE_RNTI(Mod_id, UE_id);

116
117
118
119
120
#if defined(UE_EXPANSION) || defined(UE_EXPANSION_SIM2)
	for (i = DCCH; i <=DTCH; i++) {	// loop over DCCH, DCCH1 and DTCH
#else
    for (i = 0; i < MAX_NUM_LCID; i++) {    // loop over all the logical channels
#endif
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

	    rlc_status =
		mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
				   ENB_FLAG_YES, MBMS_FLAG_NO, i, 0);
	    UE_template->dl_buffer_info[i] = rlc_status.bytes_in_buffer;	//storing the dlsch buffer for each logical channel
	    UE_template->dl_pdus_in_buffer[i] = rlc_status.pdus_in_buffer;
	    UE_template->dl_buffer_head_sdu_creation_time[i] =
		rlc_status.head_sdu_creation_time;
	    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);
	    UE_template->dl_buffer_head_sdu_remaining_size_to_send[i] =
		rlc_status.head_sdu_remaining_size_to_send;
	    UE_template->dl_buffer_head_sdu_is_segmented[i] =
		rlc_status.head_sdu_is_segmented;
	    UE_template->dl_buffer_total += UE_template->dl_buffer_info[i];	//storing the total dlsch buffer
	    UE_template->dl_pdus_total +=
		UE_template->dl_pdus_in_buffer[i];
139

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

142
143
144
145
146
147
148
149
150
151
152
153
154
	    /* note for dl_buffer_head_sdu_remaining_size_to_send[i] :
	     * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
	     */
	    if (UE_template->dl_buffer_info[i] > 0)
		LOG_D(MAC,
		      "[eNB %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",
		      Mod_id, frameP, subframeP, UE_id,
		      i, UE_template->dl_pdus_in_buffer[i],
		      UE_template->dl_buffer_info[i],
		      UE_template->dl_buffer_head_sdu_creation_time[i],
		      UE_template->
		      dl_buffer_head_sdu_remaining_size_to_send[i],
		      UE_template->dl_buffer_head_sdu_is_segmented[i]);
155

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

158
	}
159

160
161
162
163
164
165
166
	//#ifdef DEBUG_eNB_SCHEDULER
	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);
167

168
169
	//#endif
    }
170
171
}

172

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

183
    uint16_t TBS = 0;
184

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

190
191
192
193
    // clear rb allocations across all CC_id
    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
	if (UE_list->active[UE_id] != TRUE)
	    continue;
194

195
	pCCid = UE_PCCID(Mod_id, UE_id);
196

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

200
201
	    CC_id = UE_list->ordered_CCids[n][UE_id];
	    eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
knopp's avatar
knopp committed
202

203
204
	    eNB_UE_stats->dlsch_mcs1 =
		cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]];
205

206
	}
207

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
	// 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 < MAX_NUM_CCs);
		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;
		}
	    }
	}
227

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
280
	if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
	    LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);

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

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

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

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

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

		/* 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];

		    if (nb_rbs_required[CC_id][UE_id] > N_RB_DL) {
			TBS =
			    get_TBS_DL(eNB_UE_stats->dlsch_mcs1, N_RB_DL);
			nb_rbs_required[CC_id][UE_id] = N_RB_DL;
			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);
	    }
	}
knopp's avatar
   
knopp committed
281
282
    }
}
gauthier's avatar
gauthier committed
283
284


knopp's avatar
   
knopp committed
285
// This function scans all CC_ids for a particular UE to find the maximum round index of its HARQ processes
gauthier's avatar
gauthier committed
286

287
288
289
int
maxround(module_id_t Mod_id, uint16_t rnti, int frame,
	 sub_frame_t subframe, uint8_t ul_flag)
290
{
gauthier's avatar
gauthier committed
291

292
293
294
295
    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;
gauthier's avatar
gauthier committed
296

297
    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
298

299
	cc = &RC.mac[Mod_id]->common_channels[CC_id];
300

301
	UE_id = find_UE_id(Mod_id, rnti);
302
303

	harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frame ,subframe);
304

305
306
307
308
	round = UE_list->UE_sched_ctrl[UE_id].round[CC_id][harq_pid];
	if (round > round_max) {
	    round_max = round;
	}
309
    }
310

311
    return round_max;
knopp's avatar
   
knopp committed
312
}
gauthier's avatar
gauthier committed
313

knopp's avatar
   
knopp committed
314
// This function scans all CC_ids for a particular UE to find the maximum DL CQI
Cedric Roux's avatar
Cedric Roux committed
315
// it returns -1 if the UE is not found in PHY layer (get_eNB_UE_stats gives NULL)
316
int maxcqi(module_id_t Mod_id, int32_t UE_id)
317
{
318
319
320
    UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
    int CC_id, n;
    int CQI = 0;
gauthier's avatar
gauthier committed
321

322
323
    for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
	CC_id = UE_list->ordered_CCids[n][UE_id];
324

325
326
327
	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];
	}
328
    }
gauthier's avatar
gauthier committed
329

330
    return CQI;
knopp's avatar
   
knopp committed
331
}
gauthier's avatar
gauthier committed
332

333
struct sort_ue_dl_params {
334
335
336
    int Mod_idP;
    int frameP;
    int subframeP;
337
};
gauthier's avatar
gauthier committed
338

339
340
static int ue_dl_compare(const void *_a, const void *_b, void *_params)
{
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
    struct sort_ue_dl_params *params = _params;
    UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;

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

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

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

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

    if (round1 > round2)
	return -1;
    if (round1 < round2)
	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;
    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_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;

    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;

    if (cqi1 > cqi2)
	return -1;
    if (cqi1 < cqi2)
	return 1;

    return 0;
402
#if 0
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    /* The above order derives from the following.  */
    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);
	}
    }
431
432
#endif
}
gauthier's avatar
gauthier committed
433

knopp's avatar
   
knopp committed
434
// This fuction sorts the UE in order their dlsch buffer and CQI
435
void sort_UEs(module_id_t Mod_idP, int frameP, sub_frame_t subframeP)
436
{
437
438
439
440
441
    int i;
    int list[NUMBER_OF_UE_MAX];
    int list_size = 0;
    int rnti;
    struct sort_ue_dl_params params = { Mod_idP, frameP, subframeP };
442

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

445
    for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
446

447
448
449
450
	if (UE_list->active[i] == FALSE)
	    continue;
	if ((rnti = UE_RNTI(Mod_idP, i)) == NOT_A_RNTI)
	    continue;
451
#if 0
452
453
	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
	    continue;
454
#endif
455
456
457
	list[list_size] = i;
	list_size++;
    }
458

459
    qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
460

461
462
463
464
465
466
467
468
    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;
    }
469
470

#if 0
gauthier's avatar
gauthier committed
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
537
538
    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);
		}
	    }
	}
knopp's avatar
   
knopp committed
539
    }
540
#endif
541
542
}

Xu Bo's avatar
Xu Bo committed
543
#if defined(UE_EXPANSION) || defined(UE_EXPANSION_SIM2)
544
545
546
547
548
549
550
551
552
inline uint16_t search_rbs_required(uint16_t mcs, uint16_t TBS,uint16_t NB_RB, uint16_t step_size){
  uint16_t nb_rb,i_TBS,tmp_TBS;
  i_TBS=get_I_TBS(mcs);
  for(nb_rb=step_size;nb_rb<NB_RB;nb_rb+=step_size){
    tmp_TBS = TBStable[i_TBS][nb_rb-1]>>3;
    if(TBS<tmp_TBS)return(nb_rb);
  }
  return NB_RB;
}
Xu Bo's avatar
Xu Bo committed
553
554
555
556
557
558
void pre_scd_nb_rbs_required(    module_id_t     module_idP,
                                 frame_t         frameP,
                                 sub_frame_t     subframeP,
                                 int             min_rb_unit[MAX_NUM_CCs],
                                 uint16_t        nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX])
{
559
    int                          CC_id=0,UE_id, lc_id, N_RB_DL;
Xu Bo's avatar
Xu Bo committed
560
    UE_TEMPLATE                  UE_template;
561
    eNB_UE_STATS                 *eNB_UE_stats;
Xu Bo's avatar
Xu Bo committed
562
563
    rnti_t                       rnti;
    mac_rlc_status_resp_t        rlc_status;
564
565
566
567
568
    uint16_t                     step_size=2;
    
    N_RB_DL = to_prb(RC.mac[module_idP]->common_channels[CC_id].mib->message.dl_Bandwidth);
    if(N_RB_DL==50) step_size=3;
    if(N_RB_DL==100) step_size=4;
Xu Bo's avatar
Xu Bo committed
569
    memset(nb_rbs_required, 0, sizeof(uint16_t)*MAX_NUM_CCs*NUMBER_OF_UE_MAX);
570
    UE_list_t *UE_list = &RC.mac[module_idP]->UE_list;
Xu Bo's avatar
Xu Bo committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

    for (UE_id = 0; UE_id <NUMBER_OF_UE_MAX; UE_id++) {
        if (pre_scd_activeUE[UE_id] != TRUE)
            continue;

        // store dlsch buffer

        // clear logical channel interface variables
        UE_template.dl_buffer_total = 0;

        rnti = UE_RNTI(module_idP, UE_id);

        for (lc_id = DCCH; lc_id <= DTCH; lc_id++) {
            rlc_status =
                    mac_rlc_status_ind(module_idP, rnti, module_idP, frameP, subframeP,
                            ENB_FLAG_YES, MBMS_FLAG_NO, lc_id, 0);
587
            UE_template.dl_buffer_total += rlc_status.bytes_in_buffer; //storing the total dlsch buffer
Xu Bo's avatar
Xu Bo committed
588
589
590
591
592
593
        }
         // end of store dlsch buffer

        // assgin rbs required
        // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
        //update CQI information across component carriers
594
        eNB_UE_stats = &pre_scd_eNB_UE_stats[CC_id][UE_id];
Xu Bo's avatar
Xu Bo committed
595

596
        eNB_UE_stats->dlsch_mcs1 = cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]];
Xu Bo's avatar
Xu Bo committed
597
598
599


        if (UE_template.dl_buffer_total > 0) {
600
          nb_rbs_required[CC_id][UE_id] = search_rbs_required(eNB_UE_stats->dlsch_mcs1, UE_template.dl_buffer_total, N_RB_DL, step_size);
Xu Bo's avatar
Xu Bo committed
601
602
603
604
605
        }
    }
}
#endif

Xu Bo's avatar
Xu Bo committed
606
#ifdef UE_EXPANSION
Xu Bo's avatar
Xu Bo committed
607
608
609
610
611
612
613
614
615
616
617
618
int cc_id_end(uint8_t *cc_id_flag )
{
  int end_flag = 1;
  for (int CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    if (cc_id_flag[CC_id]==0) {
      end_flag = 0;
      break;
    }
  }
  return end_flag;
}

Xu Bo's avatar
Xu Bo committed
619
620
621
622
623
624
625
626
627
628
629
630
void dlsch_scheduler_pre_ue_select(
    module_id_t     module_idP,
    frame_t         frameP,
    sub_frame_t     subframeP,
    int*            mbsfn_flag,
    uint16_t        nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX],
    DLSCH_UE_SELECT dlsch_ue_select[MAX_NUM_CCs])
{
  eNB_MAC_INST                   *eNB      = RC.mac[module_idP];
  COMMON_channels_t              *cc       = eNB->common_channels;
  UE_list_t                      *UE_list  = &eNB->UE_list;
  UE_sched_ctrl                  *ue_sched_ctl;
Xu Bo's avatar
Xu Bo committed
631
632
633
634
  uint8_t                        CC_id;
  int                            UE_id;
  unsigned char                  round             = 0;
  unsigned char                  harq_pid          = 0;
Xu Bo's avatar
Xu Bo committed
635
  rnti_t                         rnti;
Xu Bo's avatar
Xu Bo committed
636
  uint16_t                       i;
Xu Bo's avatar
Xu Bo committed
637
638
639
640
  unsigned char                  aggregation;
  int                            format_flag;
  nfapi_dl_config_request_body_t *DL_req;
  nfapi_dl_config_request_pdu_t  *dl_config_pdu;
Xu Bo's avatar
Xu Bo committed
641
  uint16_t                       dlsch_ue_max_num[MAX_NUM_CCs] = {0};
Xu Bo's avatar
Xu Bo committed
642
  uint16_t                       saved_dlsch_dci[MAX_NUM_CCs] = {0};
Xu Bo's avatar
Xu Bo committed
643
644
  uint8_t                        end_flag[MAX_NUM_CCs] = {0};

Xu Bo's avatar
Xu Bo committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  // Initialization
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
    dlsch_ue_max_num[CC_id] = (uint16_t)RC.rrc[module_idP]->configuration.ue_multiple_max[CC_id];

    // save origin DL PDU number
    DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
    saved_dlsch_dci[CC_id] = DL_req->number_pdu;
  }

  // Insert DLSCH(retransmission) UE into selected UE list
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
    if (mbsfn_flag[CC_id]>0) {
      continue;
    }

    DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
Xu Bo's avatar
Xu Bo committed
661
    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++) {
Xu Bo's avatar
Xu Bo committed
662
663
664
665
666
667
668
669
670
671
      if (UE_list->active[UE_id] == FALSE) {
        continue;
      }

      rnti = UE_RNTI(module_idP, UE_id);
      if (rnti == NOT_A_RNTI) {
        continue;
      }

      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
672
#if 0
Xu Bo's avatar
Xu Bo committed
673
674
675
      if (ue_sched_ctl->ul_out_of_sync == 1) {
        continue;
      }
676
#endif
677
      harq_pid = frame_subframe2_dl_harq_pid(cc[CC_id].tdd_Config,frameP ,subframeP);
Xu Bo's avatar
Xu Bo committed
678
679
680

      round = ue_sched_ctl->round[CC_id][harq_pid];
      if (round != 8) {  // retransmission
Xu Bo's avatar
Xu Bo committed
681
682
683
        if(UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid] == 0){
          continue;
        }
Xu Bo's avatar
Xu Bo committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
        switch (get_tmode(module_idP, CC_id, UE_id)) {
          case 1:
          case 2:
          case 7:
            aggregation = get_aggregation(get_bw_index(module_idP, CC_id),
                  ue_sched_ctl->dl_cqi[CC_id],
                  format1);
            break;
          case 3:
            aggregation = get_aggregation(get_bw_index(module_idP,CC_id),
                  ue_sched_ctl->dl_cqi[CC_id],
                  format2A);
            break;
          default:
            LOG_W(MAC,"Unsupported transmission mode %d\n", get_tmode(module_idP,CC_id,UE_id));
            aggregation = 2;
Xu Bo's avatar
Xu Bo committed
700
            break;
Xu Bo's avatar
Xu Bo committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
        }
        format_flag = 1;
        if (!CCE_allocation_infeasible(module_idP,
                                      CC_id,
                                      format_flag,
                                      subframeP,
                                      aggregation,
                                      rnti)) {
          dl_config_pdu = &DL_req->dl_config_pdu_list[DL_req->number_pdu];
          dl_config_pdu->pdu_type                                     = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti              = rnti;
          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type         = (format_flag == 0)?2:1;
          dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level = aggregation;
          DL_req->number_pdu++;

          nb_rbs_required[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
          // Insert DLSCH(retransmission) UE into selected UE list
          dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].UE_id = UE_id;
          dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].ue_priority = SCH_DL_RETRANS;
          dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].rnti = rnti;
          dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].nb_rb = nb_rbs_required[CC_id][UE_id];
          dlsch_ue_select[CC_id].ue_num++;
          if (dlsch_ue_select[CC_id].ue_num == dlsch_ue_max_num[CC_id]) {
Xu Bo's avatar
Xu Bo committed
724
            end_flag[CC_id] = 1;
Xu Bo's avatar
Xu Bo committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
            break;
          }
        } else {
          if (cc[CC_id].tdd_Config != NULL) { //TDD
            set_ue_dai (subframeP,
                         UE_id,
                         CC_id,
            cc[CC_id].tdd_Config->subframeAssignment,
                         UE_list);
            // update UL DAI after DLSCH scheduling
            set_ul_DAI(module_idP,UE_id,CC_id,frameP,subframeP);
          }

          add_ue_dlsch_info(module_idP,
                            CC_id,
                            UE_id,
                            subframeP,
                            S_DL_NONE);
Xu Bo's avatar
Xu Bo committed
743
744
          end_flag[CC_id] = 1;
          break;
Xu Bo's avatar
Xu Bo committed
745
746
747
748
        }
      }
    }
  }
Xu Bo's avatar
Xu Bo committed
749
750
751
752
753
754
755
  if(cc_id_end(end_flag) == 1){
    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
      DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
      DL_req->number_pdu = saved_dlsch_dci[CC_id];
    }
    return;
  }
Xu Bo's avatar
Xu Bo committed
756
757
758

  // Insert DLSCH(first transmission) UE into selected UE list (UE_id > last_dlsch_ue_id[CC_id])
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
Xu Bo's avatar
Xu Bo committed
759
760
761
      if (mbsfn_flag[CC_id]>0) {
        continue;
      }
Xu Bo's avatar
Xu Bo committed
762
763

      DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
Xu Bo's avatar
Xu Bo committed
764
765
      for (UE_id = (last_dlsch_ue_id[CC_id]+1); UE_id <NUMBER_OF_UE_MAX; UE_id++) {
        if(end_flag[CC_id] == 1){
Xu Bo's avatar
Xu Bo committed
766
767
768
769
770
771
772
773
774
775
776
777
          break;
        }

        if (UE_list->active[UE_id] == FALSE) {
          continue;
        }

        rnti = UE_RNTI(module_idP,UE_id);
        if (rnti == NOT_A_RNTI)
          continue;

        ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
778
#if 0
Xu Bo's avatar
Xu Bo committed
779
780
781
        if (ue_sched_ctl->ul_out_of_sync == 1) {
          continue;
        }
782
#endif
Xu Bo's avatar
Xu Bo committed
783
784
785
786
787
788
789
       for(i = 0;i<dlsch_ue_select[CC_id].ue_num;i++){
          if(dlsch_ue_select[CC_id].list[i].UE_id == UE_id){
           break;
          }
        }
        if(i < dlsch_ue_select[CC_id].ue_num)
          continue;
Xu Bo's avatar
Xu Bo committed
790

791
        harq_pid = frame_subframe2_dl_harq_pid(cc[CC_id].tdd_Config,frameP ,subframeP);
Xu Bo's avatar
Xu Bo committed
792
793
794
795
796
797

        round = ue_sched_ctl->round[CC_id][harq_pid];
        if (round == 8) {
            if (nb_rbs_required[CC_id][UE_id] == 0) {
              continue;
            }
Xu Bo's avatar
Xu Bo committed
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
            switch (get_tmode(module_idP, CC_id, UE_id)) {
              case 1:
              case 2:
              case 7:
                aggregation = get_aggregation(get_bw_index(module_idP, CC_id),
                      ue_sched_ctl->dl_cqi[CC_id],
                      format1);
                break;
              case 3:
                aggregation = get_aggregation(get_bw_index(module_idP,CC_id),
                      ue_sched_ctl->dl_cqi[CC_id],
                      format2A);
                break;
              default:
                LOG_W(MAC,"Unsupported transmission mode %d\n", get_tmode(module_idP,CC_id,UE_id));
                aggregation = 2;
Xu Bo's avatar
Xu Bo committed
814
                break;
Xu Bo's avatar
Xu Bo committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
            }
            format_flag = 1;
            if (!CCE_allocation_infeasible(module_idP,
                                           CC_id,
                                           format_flag,
                                           subframeP,
                                           aggregation,
                                           rnti)) {
              dl_config_pdu = &DL_req->dl_config_pdu_list[DL_req->number_pdu];
              dl_config_pdu->pdu_type                                     = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti              = rnti;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type         = (format_flag == 0)?2:1;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level = aggregation;
              DL_req->number_pdu++;

              // Insert DLSCH(first transmission) UE into selected selected UE list
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].ue_priority = SCH_DL_FIRST;
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].nb_rb = nb_rbs_required[CC_id][UE_id];
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].UE_id = UE_id;
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].rnti = rnti;
              dlsch_ue_select[CC_id].ue_num++;

Xu Bo's avatar
Xu Bo committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
          if (dlsch_ue_select[CC_id].ue_num == dlsch_ue_max_num[CC_id]) {
                  end_flag[CC_id] = 1;
                  break;
              }
          }else {
            if (cc[CC_id].tdd_Config != NULL) { //TDD
              set_ue_dai (subframeP,
                           UE_id,
                           CC_id,
              cc[CC_id].tdd_Config->subframeAssignment,
                           UE_list);
              // update UL DAI after DLSCH scheduling
              set_ul_DAI(module_idP,UE_id,CC_id,frameP,subframeP);
            }
           add_ue_dlsch_info(module_idP,
Xu Bo's avatar
Xu Bo committed
852
853
854
855
                            CC_id,
                            UE_id,
                            subframeP,
                            S_DL_NONE);
Xu Bo's avatar
Xu Bo committed
856
857
858
            end_flag[CC_id] = 1;
            break;
          }
Xu Bo's avatar
Xu Bo committed
859
860
861
      }
    }
  }
Xu Bo's avatar
Xu Bo committed
862
863
864
865
866
867
868
  if(cc_id_end(end_flag) == 1){
    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
      DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
      DL_req->number_pdu = saved_dlsch_dci[CC_id];
    }
    return;
  }
Xu Bo's avatar
Xu Bo committed
869
870
871

  // Insert DLSCH(first transmission) UE into selected UE list (UE_id <= last_dlsch_ue_id[CC_id])
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
Xu Bo's avatar
Xu Bo committed
872
873
874
      if (mbsfn_flag[CC_id]>0) {
        continue;
      }
Xu Bo's avatar
Xu Bo committed
875
876

      DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
Xu Bo's avatar
Xu Bo committed
877
878
      for (UE_id = 0; UE_id <= last_dlsch_ue_id[CC_id]; UE_id++) {
        if(end_flag[CC_id] == 1){
Xu Bo's avatar
Xu Bo committed
879
880
881
882
883
884
885
886
887
888
889
890
          break;
        }

        if (UE_list->active[UE_id] == FALSE) {
          continue;
        }

        rnti = UE_RNTI(module_idP,UE_id);
        if (rnti == NOT_A_RNTI)
          continue;

        ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
891
#if 0
Xu Bo's avatar
Xu Bo committed
892
893
894
        if (ue_sched_ctl->ul_out_of_sync == 1) {
          continue;
        }
895
#endif
Xu Bo's avatar
Xu Bo committed
896
897
898
899
900
901
902
        for(i = 0;i<dlsch_ue_select[CC_id].ue_num;i++){
          if(dlsch_ue_select[CC_id].list[i].UE_id == UE_id){
           break;
          }
        }
        if(i < dlsch_ue_select[CC_id].ue_num)
          continue;
Xu Bo's avatar
Xu Bo committed
903

904
        harq_pid = frame_subframe2_dl_harq_pid(cc[CC_id].tdd_Config,frameP ,subframeP);
Xu Bo's avatar
Xu Bo committed
905
906
907
908
909
910

        round = ue_sched_ctl->round[CC_id][harq_pid];
        if (round == 8) {
            if (nb_rbs_required[CC_id][UE_id] == 0) {
                continue;
             }
Xu Bo's avatar
Xu Bo committed
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
            switch (get_tmode(module_idP, CC_id, UE_id)) {
              case 1:
              case 2:
              case 7:
                aggregation = get_aggregation(get_bw_index(module_idP, CC_id),
                      ue_sched_ctl->dl_cqi[CC_id],
                      format1);
                break;
              case 3:
                aggregation = get_aggregation(get_bw_index(module_idP,CC_id),
                      ue_sched_ctl->dl_cqi[CC_id],
                      format2A);
                break;
              default:
                LOG_W(MAC,"Unsupported transmission mode %d\n", get_tmode(module_idP,CC_id,UE_id));
                aggregation = 2;
Xu Bo's avatar
Xu Bo committed
927
                break;
Xu Bo's avatar
Xu Bo committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
            }
            format_flag = 1;
            if (!CCE_allocation_infeasible(module_idP,
                                           CC_id,
                                           format_flag,
                                           subframeP,
                                           aggregation,
                                           rnti)) {
              dl_config_pdu = &DL_req->dl_config_pdu_list[DL_req->number_pdu];
              dl_config_pdu->pdu_type                                     = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti              = rnti;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type         = (format_flag == 0)?2:1;
              dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level = aggregation;
              DL_req->number_pdu++;

              // Insert DLSCH(first transmission) UE into selected selected UE list
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].ue_priority = SCH_DL_FIRST;
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].nb_rb = nb_rbs_required[CC_id][UE_id];
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].UE_id = UE_id;
              dlsch_ue_select[CC_id].list[dlsch_ue_select[CC_id].ue_num].rnti = rnti;
              dlsch_ue_select[CC_id].ue_num++;

Xu Bo's avatar
Xu Bo committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
              if (dlsch_ue_select[CC_id].ue_num == dlsch_ue_max_num[CC_id]) {
                end_flag[CC_id] = 1;
                break;
              }
            } else {
              if (cc[CC_id].tdd_Config != NULL) { //TDD
                set_ue_dai (subframeP,
                            UE_id,
                            CC_id,
                            cc[CC_id].tdd_Config->subframeAssignment,
                            UE_list);
              // update UL DAI after DLSCH scheduling
                set_ul_DAI(module_idP,UE_id,CC_id,frameP,subframeP);
              }
Xu Bo's avatar
Xu Bo committed
964
965
966
967
968
          add_ue_dlsch_info(module_idP,
                            CC_id,
                            UE_id,
                            subframeP,
                            S_DL_NONE);
Xu Bo's avatar
Xu Bo committed
969
970
971
              end_flag[CC_id] = 1;
              break;
            }
Xu Bo's avatar
Xu Bo committed
972
973
974
975
976
977
978
979
      }
    }
  }

  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
    DL_req          = &eNB->DL_req[CC_id].dl_config_request_body;
    DL_req->number_pdu = saved_dlsch_dci[CC_id];
  }
Xu Bo's avatar
Xu Bo committed
980
  return;
Xu Bo's avatar
Xu Bo committed
981
982
983
984
985
}



#endif
knopp's avatar
   
knopp committed
986
987


988
// This function assigns pre-available RBS to each UE in specified sub-bands before scheduling is done
knopp's avatar
   
knopp committed
989
void dlsch_scheduler_pre_processor (module_id_t   Mod_id,
990
991
992
                                    frame_t       frameP,
                                    sub_frame_t   subframeP,
                                    int           N_RBG[MAX_NUM_CCs],
Xu Bo's avatar
Xu Bo committed
993
                                    int           *mbsfn_flag)
994
{
knopp's avatar
   
knopp committed
995

Xu Bo's avatar
Xu Bo committed
996
#ifndef UE_EXPANSION
997
  unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],harq_pid=0,round=0,total_ue_count;
Xu Bo's avatar
Xu Bo committed
998
999
1000
1001
1002
1003
  uint16_t ii;
  uint16_t                nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
  uint16_t r1=0;
//  int rrc_status           = RRC_IDLE;
#else
  unsigned char rballoc_sub[MAX_NUM_CCs][N_RBG_MAX],harq_pid=0,Round=0;
Xu Bo's avatar
Xu Bo committed
1004
1005
  uint16_t                temp_total_rbs_count;
  unsigned char           temp_total_ue_count;
Xu Bo's avatar
Xu Bo committed
1006
#endif 
knopp's avatar
   
knopp committed
1007
  unsigned char MIMO_mode_indicator[MAX_NUM_CCs][N_RBG_MAX];
1008
  int                     UE_id, i; 
Xu Bo's avatar
Xu Bo committed
1009
  uint16_t                j;
knopp's avatar
   
knopp committed
1010
1011
  uint16_t                nb_rbs_required[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
  uint16_t                nb_rbs_required_remaining[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
Xu Bo's avatar
Xu Bo committed
1012
//  uint16_t                nb_rbs_required_remaining_1[MAX_NUM_CCs][NUMBER_OF_UE_MAX];
1013
  uint16_t                average_rbs_per_user[MAX_NUM_CCs] = {0};
1014
  rnti_t             rnti;
knopp's avatar
   
knopp committed
1015
  int                min_rb_unit[MAX_NUM_CCs];
Xu Bo's avatar
Xu Bo committed
1016
//  uint16_t r1=0;
1017
  uint8_t CC_id;
1018
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
1019

knopp's avatar
knopp committed
1020
  int N_RB_DL;
1021
  int transmission_mode = 0;
1022
1023
  UE_sched_ctrl *ue_sched_ctl;
  //  int rrc_status           = RRC_IDLE;
1024
  COMMON_channels_t *cc;
1025
1026

#ifdef TM5
1027
1028
1029
1030
1031
1032
1033
1034
    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;
1035
#endif
1036
1037

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
knopp's avatar
   
knopp committed
1038

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


knopp's avatar
   
knopp committed
1043

1044
	min_rb_unit[CC_id] = get_min_rb_unit(Mod_id, CC_id);
1045

1046
1047
1048
	for (i = 0; i < NUMBER_OF_UE_MAX; i++) {
	    if (UE_list->active[i] != TRUE)
		continue;
1049

1050
1051
	    UE_id = i;
	    // Initialize scheduling information for all active UEs
1052

1053
1054


1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
	    dlsch_scheduler_pre_processor_reset(Mod_id,
						UE_id,
						CC_id,
						frameP,
						subframeP,
						N_RBG[CC_id],
						nb_rbs_required,
						nb_rbs_required_remaining,
						rballoc_sub,
						MIMO_mode_indicator);
knopp's avatar
   
knopp committed
1065

1066
1067
	}
    }
knopp's avatar
   
knopp committed
1068

Xu Bo's avatar
Xu Bo committed
1069
#if (!defined(UE_EXPANSION_SIM2)) &&(!defined(UE_EXPANSION))
1070
1071
    // Store the DLSCH buffer for each logical channel
    store_dlsch_buffer(Mod_id, frameP, subframeP);
knopp's avatar
   
knopp committed
1072
1073


1074

1075
1076
1077
    // Calculate the number of RBs required by each UE on the basis of logical channel's buffer
    assign_rbs_required(Mod_id, frameP, subframeP, nb_rbs_required,
			min_rb_unit);
Xu Bo's avatar
Xu Bo committed
1078
1079
1080
#else
    memcpy(nb_rbs_required, pre_nb_rbs_required[dlsch_ue_select_tbl_in_use] , sizeof(uint16_t)*MAX_NUM_CCs*NUMBER_OF_UE_MAX);
#endif
knopp's avatar
   
knopp committed
1081

Xu Bo's avatar
Xu Bo committed
1082
1083
1084
#ifdef UE_EXPANSION
  dlsch_scheduler_pre_ue_select(Mod_id,frameP,subframeP, mbsfn_flag,nb_rbs_required,dlsch_ue_select);
#else
1085
  // Sorts the user on the basis of dlsch logical channel buffer and CQI
knopp's avatar
   
knopp committed
1086
1087
  sort_UEs (Mod_id,frameP,subframeP);

1088
  total_ue_count =0;
Xu Bo's avatar
Xu Bo committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
#endif


 //total_ue_count =0;

#ifdef UE_EXPANSION
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
    average_rbs_per_user[CC_id] = 0;
    cc = &RC.mac[Mod_id]->common_channels[CC_id];
    // Get total available RBS count and total UE count
    N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
    temp_total_rbs_count = RC.mac[Mod_id]->eNB_stats[CC_id].available_prbs;
    temp_total_ue_count = dlsch_ue_select[CC_id].ue_num;

    for (i = 0; i < dlsch_ue_select[CC_id].ue_num; i++) {
Xu Bo's avatar
Xu Bo committed
1104
1105
1106
1107
1108
1109
1110
1111
      if(dlsch_ue_select[CC_id].list[i].ue_priority == SCH_DL_MSG2){
          temp_total_ue_count--;
          continue;
      }
      if(dlsch_ue_select[CC_id].list[i].ue_priority == SCH_DL_MSG4){
          temp_total_ue_count--;
          continue;
      }
Xu Bo's avatar
Xu Bo committed
1112
1113
1114
      UE_id = dlsch_ue_select[CC_id].list[i].UE_id;
      nb_rbs_required[CC_id][UE_id] = dlsch_ue_select[CC_id].list[i].nb_rb;

Xu Bo's avatar
Xu Bo committed
1115
1116
1117
1118
1119
1120
      average_rbs_per_user[CC_id] = (uint16_t)round((double)temp_total_rbs_count/(double)temp_total_ue_count);
      if( average_rbs_per_user[CC_id] < min_rb_unit[CC_id] ){
        temp_total_ue_count--;
        dlsch_ue_select[CC_id].ue_num--;
        i--;
        continue;
Xu Bo's avatar
Xu Bo committed
1121
1122
1123
1124
1125
      }

      rnti = dlsch_ue_select[CC_id].list[i].rnti;

      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
1126
      harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frameP ,subframeP);
Xu Bo's avatar
Xu Bo committed
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
      Round    = ue_sched_ctl->round[CC_id][harq_pid];

      //if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED || round > 0) {
      if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED || Round != 8) {  // FIXME
        nb_rbs_required_remaining[CC_id][UE_id] = dlsch_ue_select[CC_id].list[i].nb_rb;
      } else {
        nb_rbs_required_remaining[CC_id][UE_id] = cmin(average_rbs_per_user[CC_id], dlsch_ue_select[CC_id].list[i].nb_rb);
      }

      transmission_mode = get_tmode(Mod_id,CC_id,UE_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],
                                              transmission_mode,
                                              min_rb_unit[CC_id],
                                              N_RB_DL,
                                              nb_rbs_required,
                                              nb_rbs_required_remaining,
                                              rballoc_sub,
                                              MIMO_mode_indicator);
Xu Bo's avatar
Xu Bo committed
1150
1151
1152
      temp_total_rbs_count -= ue_sched_ctl->pre_nb_available_rbs[CC_id];
      temp_total_ue_count--;

Xu Bo's avatar
Xu Bo committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
      if (ue_sched_ctl->pre_nb_available_rbs[CC_id] == 0) {
        dlsch_ue_select[CC_id].ue_num = i;
        break;
      }

      if (temp_total_rbs_count == 0) {
        dlsch_ue_select[CC_id].ue_num = i+1;
        break;
      }
#ifdef TM5
      // TODO: data channel TM5: to be re-visited
#endif
    }
  }

#else
1169
1170
1171
    // loop over all active UEs
    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
	rnti = UE_RNTI(Mod_id, i);
1172

1173
1174
	if (rnti == NOT_A_RNTI)
	    continue;
1175
#if 0
1176
1177
	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
	    continue;
1178
#endif
1179
	UE_id = i;
1180

1181
1182
1183
1184
	for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
	    CC_id = UE_list->ordered_CCids[ii][UE_id];
	    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
	    cc = &RC.mac[Mod_id]->common_channels[ii];
1185
	    harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frameP ,subframeP);
1186
	    round = ue_sched_ctl->round[CC_id][harq_pid];
1187

1188
	    average_rbs_per_user[CC_id] = 0;
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
	    if (round != 8) {
		nb_rbs_required[CC_id][UE_id] =
		    UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
	    }
	    //nb_rbs_required_remaining[UE_id] = nb_rbs_required[UE_id];
	    if (nb_rbs_required[CC_id][UE_id] > 0) {
		total_ue_count = total_ue_count + 1;
	    }
	    // hypothetical assignment
	    /*
	     * If schedule is enabled and if the priority of the UEs is modified
	     * The average rbs per logical channel per user will depend on the level of
	     * priority. Concerning the hypothetical assignement, we should assign more
	     * rbs to prioritized users. Maybe, we can do a mapping between the
	     * average rbs per user and the level of priority or multiply the average rbs
	     * per user by a coefficient which represents the degree of priority.
	     */

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

	    if (total_ue_count == 0) {
		average_rbs_per_user[CC_id] = 0;
	    } else if ((min_rb_unit[CC_id] * total_ue_count) <= (N_RB_DL)) {
		average_rbs_per_user[CC_id] =
		    (uint16_t) floor(N_RB_DL / total_ue_count);
	    } else {
		average_rbs_per_user[CC_id] = min_rb_unit[CC_id];	// consider the total number of use that can be scheduled UE
	    }
	}
knopp's avatar
   
knopp committed
1222
    }
1223

1224
1225
1226
1227
    // note: nb_rbs_required is assigned according to total_buffer_dl
    // extend nb_rbs_required to capture per LCID RB required
    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
	rnti = UE_RNTI(Mod_id, i);
1228

1229
1230
	if (rnti == NOT_A_RNTI)
	    continue;
1231
#if 0
1232
1233
	if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1)
	    continue;
1234
#endif
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
	for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
	    CC_id = UE_list->ordered_CCids[ii][i];
	    ue_sched_ctl = &UE_list->UE_sched_ctrl[i];
	    round = ue_sched_ctl->round[CC_id][harq_pid];

	    // control channel or retransmission
	    /* TODO: do we have to check for retransmission? */
	    if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED
		|| round > 0) {
		nb_rbs_required_remaining_1[CC_id][i] =
		    nb_rbs_required[CC_id][i];
	    } else {
		nb_rbs_required_remaining_1[CC_id][i] =
		    cmin(average_rbs_per_user[CC_id],
			 nb_rbs_required[CC_id][i]);
1250

1251
1252
	    }
	}
knopp's avatar
   
knopp committed
1253
    }
1254

1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
    //Allocation to UEs is done in 2 rounds,
    // 1st stage: average number of RBs allocated to each UE
    // 2nd stage: remaining RBs are allocated to high priority UEs
    for (r1 = 0; r1 < 2; r1++) {

	for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
	    for (ii = 0; ii < UE_num_active_CC(UE_list, i); ii++) {
		CC_id = UE_list->ordered_CCids[ii][i];

		if (r1 == 0) {
		    nb_rbs_required_remaining[CC_id][i] =
			nb_rbs_required_remaining_1[CC_id][i];
		} else {	// rb required based only on the buffer - rb allloctaed in the 1st round + extra reaming rb form the 1st round
		    nb_rbs_required_remaining[CC_id][i] =
			nb_rbs_required[CC_id][i] -
			nb_rbs_required_remaining_1[CC_id][i] +
			nb_rbs_required_remaining[CC_id][i];
		    if (nb_rbs_required_remaining[CC_id][i] < 0)
			abort();
		}

		if (nb_rbs_required[CC_id][i] > 0)
		    LOG_D(MAC,
			  "round %d : nb_rbs_required_remaining[%d][%d]= %d (remaining_1 %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
			  r1, CC_id, i,
			  nb_rbs_required_remaining[CC_id][i],
			  nb_rbs_required_remaining_1[CC_id][i],
			  nb_rbs_required[CC_id][i],
			  UE_list->UE_sched_ctrl[i].
			  pre_nb_available_rbs[CC_id], N_RBG[CC_id],
			  min_rb_unit[CC_id]);
1286

1287
1288
	    }
	}
1289

1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
	if (total_ue_count > 0) {
	    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {
		UE_id = i;

		for (ii = 0; ii < UE_num_active_CC(UE_list, UE_id); ii++) {
		    CC_id = UE_list->ordered_CCids[ii][UE_id];
		    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
		    round = ue_sched_ctl->round[CC_id][harq_pid];

		    rnti = UE_RNTI(Mod_id, UE_id);

		    // LOG_D(MAC,"UE %d rnti 0x\n", UE_id, rnti );
		    if (rnti == NOT_A_RNTI)
			continue;
1304
#if 0
1305
1306
		    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1)
			continue;
1307
#endif
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
		    transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
		    //          mac_xface->get_ue_active_harq_pid(Mod_id,CC_id,rnti,frameP,subframeP,&harq_pid,&round,0);
		    //rrc_status = mac_eNB_get_rrc_status(Mod_id,rnti);
		    /* 1st allocate for the retx */

		    // retransmission in data channels
		    // control channel in the 1st transmission
		    // data channel for all TM
		    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],
							   transmission_mode,
							   min_rb_unit
							   [CC_id],
							   to_prb(RC.mac
								  [Mod_id]->common_channels
								  [CC_id].mib->message.dl_Bandwidth),
							   nb_rbs_required,
							   nb_rbs_required_remaining,
							   rballoc_sub,
							   MIMO_mode_indicator);