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

/*! \file gNB_scheduler.c
 * \brief gNB scheduler top level function operates on per subframe basis
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
24 25 26
 * \author  Navid Nikaein and Raymond Knopp, WEI-TAI CHEN
 * \date 2010 - 2014, 2018
 * \email: navid.nikaein@eurecom.fr, kroempa@gmail.com
27
 * \version 0.5
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
28
 * \company Eurecom, NTUST
29 30 31 32 33 34 35
 * @ingroup _mac

 */

#include "assertions.h"

#include "LAYER2/MAC/mac.h"
36
#include "NR_MAC_COMMON/nr_mac_extern.h"
37
#include "LAYER2/MAC/mac_proto.h"
38
#include "NR_MAC_gNB/mac_proto.h"
39

Raymond Knopp's avatar
Raymond Knopp committed
40 41
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
42 43 44 45
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"

46
#include "RRC/NR/nr_rrc_extern.h"
47 48 49 50

//#include "LAYER2/MAC/pre_processor.c"
#include "pdcp.h"

51
#include "openair1/PHY/defs_gNB.h"
52
#include "openair1/PHY/NR_TRANSPORT/nr_dlsch.h"
53

54 55 56 57 58 59
//Agent-related headers
#include "flexran_agent_extern.h"
#include "flexran_agent_mac.h"

#include "intertask_interface.h"

60
#include "executables/softmodem-common.h"
61

62
const uint8_t nr_slots_per_frame[5] = {10, 20, 40, 80, 160};
63 64
uint16_t nr_pdcch_order_table[6] = { 31, 31, 511, 2047, 2047, 8191 };

65
void clear_nr_nfapi_information(gNB_MAC_INST * gNB,
66
                                int CC_idP,
67
                                frame_t frameP,
68
                                sub_frame_t slotP){
69

Raymond Knopp's avatar
Raymond Knopp committed
70 71
  nfapi_nr_dl_tti_request_t    *DL_req = &gNB->DL_req[0];
  nfapi_nr_ul_tti_request_t    *UL_tti_req = &gNB->UL_tti_req[0];
72
  nfapi_nr_ul_dci_request_t    *UL_dci_req = &gNB->UL_dci_req[0];
Raymond Knopp's avatar
Raymond Knopp committed
73
  nfapi_nr_tx_data_request_t   *TX_req = &gNB->TX_req[0];
74 75 76 77 78

  gNB->pdu_index[CC_idP] = 0;

  if (nfapi_mode==0 || nfapi_mode == 1) { // monolithic or PNF

Raymond Knopp's avatar
Raymond Knopp committed
79 80 81 82 83
    DL_req[CC_idP].SFN                                   = frameP;
    DL_req[CC_idP].Slot                                  = slotP;
    DL_req[CC_idP].dl_tti_request_body.nPDUs             = 0;
    DL_req[CC_idP].dl_tti_request_body.nGroup            = 0;
    //DL_req[CC_idP].dl_tti_request_body.transmission_power_pcfich           = 6000;
84

85 86 87
    UL_dci_req[CC_idP].SFN                         = frameP;
    UL_dci_req[CC_idP].Slot                        = slotP;
    UL_dci_req[CC_idP].numPdus                     = 0;
88

Raymond Knopp's avatar
Raymond Knopp committed
89 90
    UL_tti_req[CC_idP].SFN                         = frameP;
    UL_tti_req[CC_idP].Slot                        = slotP;
91 92 93 94
    UL_tti_req[CC_idP].n_pdus                      = 0;
    UL_tti_req[CC_idP].n_ulsch                     = 0;
    UL_tti_req[CC_idP].n_ulcch                     = 0;
    UL_tti_req[CC_idP].n_group                     = 0;
95

Raymond Knopp's avatar
Raymond Knopp committed
96
    TX_req[CC_idP].Number_of_PDUs                  = 0;
97 98 99

  }
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
100
/*
101 102
void check_nr_ul_failure(module_id_t module_idP,
                         int CC_id,
103
                         int UE_id,
104
                         frame_t frameP,
105
                         sub_frame_t slotP) {
106

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
107
  UE_list_t                     *UE_list  = &RC.nrmac[module_idP]->UE_list;
Raymond Knopp's avatar
Raymond Knopp committed
108
  nfapi_nr_dl_dci_request_t  *DL_req   = &RC.nrmac[module_idP]->DL_req[0];
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
109 110
  uint16_t                      rnti      = UE_RNTI(module_idP, UE_id);
  NR_COMMON_channels_t          *cc       = RC.nrmac[module_idP]->common_channels;
111 112 113 114 115 116 117 118 119 120

  // check uplink failure
  if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer > 0) &&
      (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 0)) {
    LOG_I(MAC, "UE %d rnti %x: UL Failure timer %d \n", UE_id, rnti,
    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
    if (UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent == 0) {
      UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent = 1;

      // add a format 1A dci for this UE to request an RA procedure (only one UE per subframe)
Raymond Knopp's avatar
Raymond Knopp committed
121 122
      nfapi_nr_dl_dci_request_pdu_t *dl_config_pdu                    = &DL_req[CC_id].dl_tti_request_body.dl_config_pdu_list[DL_req[CC_id].dl_tti_request_body.number_pdu];
      memset((void *) dl_config_pdu, 0,sizeof(nfapi_dl_dci_request_pdu_t));
123 124
      dl_config_pdu->pdu_type                                         = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
      dl_config_pdu->pdu_size                                         = (uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
Raymond Knopp's avatar
Raymond Knopp committed
125
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tl.tag                = NFAPI_DL_DCI_REQUEST_DCI_DL_PDU_REL8_TAG;
126 127 128 129 130 131 132 133 134 135 136 137
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format            = NFAPI_DL_DCI_FORMAT_1A;
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level     = get_aggregation(get_bw_index(module_idP, CC_id),
                      UE_list->UE_sched_ctrl[UE_id].
                      dl_cqi[CC_id], format1A);
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                  = rnti;
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type             = 1;  // CRNTI : see Table 4-10 from SCF082 - nFAPI specifications
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power    = 6000; // equal to RS power

      AssertFatal((cc[CC_id].mib->message.dl_Bandwidth >= 0) && (cc[CC_id].mib->message.dl_Bandwidth < 6),
      "illegal dl_Bandwidth %d\n",
      (int) cc[CC_id].mib->message.dl_Bandwidth);
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding = nr_pdcch_order_table[cc[CC_id].mib->message.dl_Bandwidth];
Raymond Knopp's avatar
Raymond Knopp committed
138 139 140
      DL_req[CC_id].dl_tti_request_body.number_dci++;
      DL_req[CC_id].dl_tti_request_body.number_pdu++;
      DL_req[CC_id].dl_tti_request_body.tl.tag                      = NFAPI_DL_TTI_REQUEST_BODY_TAG;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      LOG_I(MAC,
      "UE %d rnti %x: sending PDCCH order for RAPROC (failure timer %d), resource_block_coding %d \n",
      UE_id, rnti,
      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer,
      dl_config_pdu->dci_dl_pdu.
      dci_dl_pdu_rel8.resource_block_coding);
    } else {    // ra_pdcch_sent==1
      LOG_I(MAC,
      "UE %d rnti %x: sent PDCCH order for RAPROC waiting (failure timer %d) \n",
      UE_id, rnti,
      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
      if ((UE_list->UE_sched_ctrl[UE_id].ul_failure_timer % 40) == 0) UE_list->UE_sched_ctrl[UE_id].ra_pdcch_order_sent = 0;  // resend every 4 frames
    }

    UE_list->UE_sched_ctrl[UE_id].ul_failure_timer++;
    // check threshold
    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer > 20000) {
      // inform RRC of failure and clear timer
      LOG_I(MAC,
      "UE %d rnti %x: UL Failure after repeated PDCCH orders: Triggering RRC \n",
      UE_id, rnti);
      mac_eNB_rrc_ul_failure(module_idP, CC_id, frameP, subframeP,rnti);
      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 0;
      UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync   = 1;

      //Inform the controller about the UE deactivation. Should be moved to RRC agent in the future
      if (rrc_agent_registered[module_idP]) {
        LOG_W(MAC, "notify flexran Agent of UE state change\n");
        agent_rrc_xface[module_idP]->flexran_agent_notify_ue_state_change(module_idP,
            rnti, PROTOCOL__FLEX_UE_STATE_CHANGE_TYPE__FLUESC_DEACTIVATED);
      }
    }
  }       // ul_failure_timer>0

}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
176
*/
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
177
/*
178 179 180 181 182 183
void schedule_nr_SRS(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP)
{
  gNB_MAC_INST *gNB = RC.nrmac[module_idP];
  UE_list_t *UE_list = &gNB->UE_list;
  nfapi_ul_config_request_body_t *ul_req;
  int CC_id, UE_id;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
184
  NR_COMMON_channels_t *cc = RC.nrmac[module_idP]->common_channels;
185 186 187 188 189
  SoundingRS_UL_ConfigCommon_t *soundingRS_UL_ConfigCommon;
  struct SoundingRS_UL_ConfigDedicated *soundingRS_UL_ConfigDedicated;
  uint8_t TSFC;
  uint16_t deltaTSFC;   // bitmap
  uint8_t srs_SubframeConfig;
190

191 192 193
  // table for TSFC (Period) and deltaSFC (offset)
  const uint16_t deltaTSFCTabType1[15][2] = { {1, 1}, {1, 2}, {2, 2}, {1, 5}, {2, 5}, {4, 5}, {8, 5}, {3, 5}, {12, 5}, {1, 10}, {2, 10}, {4, 10}, {8, 10}, {351, 10}, {383, 10} };  // Table 5.5.3.3-2 3GPP 36.211 FDD
  const uint16_t deltaTSFCTabType2[14][2] = { {2, 5}, {6, 5}, {10, 5}, {18, 5}, {14, 5}, {22, 5}, {26, 5}, {30, 5}, {70, 10}, {74, 10}, {194, 10}, {326, 10}, {586, 10}, {210, 10} }; // Table 5.5.3.3-2 3GPP 36.211 TDD
194

195
  uint16_t srsPeriodicity, srsOffset;
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210
  for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
    soundingRS_UL_ConfigCommon = &cc[CC_id].radioResourceConfigCommon->soundingRS_UL_ConfigCommon;
    // check if SRS is enabled in this frame/subframe
    if (soundingRS_UL_ConfigCommon) {
      srs_SubframeConfig = soundingRS_UL_ConfigCommon->choice.setup.srs_SubframeConfig;
      if (cc[CC_id].tdd_Config == NULL) { // FDD
  deltaTSFC = deltaTSFCTabType1[srs_SubframeConfig][0];
  TSFC = deltaTSFCTabType1[srs_SubframeConfig][1];
      } else {    // TDD
  deltaTSFC = deltaTSFCTabType2[srs_SubframeConfig][0];
  TSFC = deltaTSFCTabType2[srs_SubframeConfig][1];
      }
      // Sounding reference signal subframes are the subframes satisfying ns/2 mod TSFC (- deltaTSFC
      uint16_t tmp = (subframeP % TSFC);
211

212 213 214 215 216 217 218
      if ((1 << tmp) & deltaTSFC) {
  // This is an SRS subframe, loop over UEs
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_GNB; UE_id++) {
    if (!RC.nrmac[module_idP]->UE_list.active[UE_id]) continue;
    ul_req = &RC.nrmac[module_idP]->UL_req[CC_id].ul_config_request_body;
    // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
    if (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED) continue;
219

220 221 222
    AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated != NULL,
          "physicalConfigDedicated is null for UE %d\n",
          UE_id);
223

224 225 226 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
    if ((soundingRS_UL_ConfigDedicated = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->soundingRS_UL_ConfigDedicated) != NULL) {
      if (soundingRS_UL_ConfigDedicated->present == SoundingRS_UL_ConfigDedicated_PR_setup) {
        get_srs_pos(&cc[CC_id],
        soundingRS_UL_ConfigDedicated->choice.
        setup.srs_ConfigIndex,
        &srsPeriodicity, &srsOffset);
        if (((10 * frameP + subframeP) % srsPeriodicity) == srsOffset) {
    // Program SRS
    ul_req->srs_present = 1;
    nfapi_ul_config_request_pdu_t * ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
    memset((void *) ul_config_pdu, 0, sizeof(nfapi_ul_config_request_pdu_t));
    ul_config_pdu->pdu_type =  NFAPI_UL_CONFIG_SRS_PDU_TYPE;
    ul_config_pdu->pdu_size =  2 + (uint8_t) (2 + sizeof(nfapi_ul_config_srs_pdu));
    ul_config_pdu->srs_pdu.srs_pdu_rel8.tl.tag = NFAPI_UL_CONFIG_REQUEST_SRS_PDU_REL8_TAG;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.size = (uint8_t)sizeof(nfapi_ul_config_srs_pdu);
    ul_config_pdu->srs_pdu.srs_pdu_rel8.rnti = UE_list->UE_template[CC_id][UE_id].rnti;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.srs_bandwidth = soundingRS_UL_ConfigDedicated->choice.setup.srs_Bandwidth;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.frequency_domain_position = soundingRS_UL_ConfigDedicated->choice.setup.freqDomainPosition;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.srs_hopping_bandwidth = soundingRS_UL_ConfigDedicated->choice.setup.srs_HoppingBandwidth;;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.transmission_comb = soundingRS_UL_ConfigDedicated->choice.setup.transmissionComb;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.i_srs = soundingRS_UL_ConfigDedicated->choice.setup.srs_ConfigIndex;
    ul_config_pdu->srs_pdu.srs_pdu_rel8.sounding_reference_cyclic_shift = soundingRS_UL_ConfigDedicated->choice.setup.cyclicShift;    //              ul_config_pdu->srs_pdu.srs_pdu_rel10.antenna_port                   = ;//
    //              ul_config_pdu->srs_pdu.srs_pdu_rel13.number_of_combs                = ;//
    RC.nrmac[module_idP]->UL_req[CC_id].sfn_sf = (frameP << 4) + subframeP;
    RC.nrmac[module_idP]->UL_req[CC_id].header.message_id = NFAPI_UL_CONFIG_REQUEST;
    ul_req->number_of_pdus++;
        } // if (((10*frameP+subframeP) % srsPeriodicity) == srsOffset)
      } // if (soundingRS_UL_ConfigDedicated->present == SoundingRS_UL_ConfigDedicated_PR_setup)
    }   // if ((soundingRS_UL_ConfigDedicated = UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->soundingRS_UL_ConfigDedicated)!=NULL)
  }   // for (UE_id ...
      }     // if((1<<tmp) & deltaTSFC)
255

256 257 258
    }     // SRS config
  }
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
259
*/
260 261


262
/*
263
void copy_nr_ulreq(module_id_t module_idP, frame_t frameP, sub_frame_t slotP)
264 265 266 267 268 269
{
  int CC_id;
  gNB_MAC_INST *mac = RC.nrmac[module_idP];

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

270
    nfapi_ul_config_request_t *ul_req                 = &mac->UL_tti_req[CC_id];
271 272 273 274 275

    *ul_req = *ul_req_tmp;

    // Restore the pointer
    ul_req->ul_config_request_body.ul_config_pdu_list = ul_req_pdu;
276
    ul_req->sfn_sf                                    = (frameP<<7) + slotP;
277 278 279 280
    ul_req_tmp->ul_config_request_body.number_of_pdus = 0;

    if (ul_req->ul_config_request_body.number_of_pdus>0)
      {
281
        LOG_D(PHY, "%s() active NOW (frameP:%d slotP:%d) pdus:%d\n", __FUNCTION__, frameP, slotP, ul_req->ul_config_request_body.number_of_pdus);
282 283 284 285 286 287 288
      }

    memcpy((void*)ul_req->ul_config_request_body.ul_config_pdu_list,
     (void*)ul_req_tmp->ul_config_request_body.ul_config_pdu_list,
     ul_req->ul_config_request_body.number_of_pdus*sizeof(nfapi_ul_config_request_pdu_t));
  }
}
289
*/
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
void nr_schedule_pucch(int Mod_idP,
                       int UE_id,
                       frame_t frameP,
                       sub_frame_t slotP) {

  uint16_t O_uci;
  uint16_t O_ack;
  uint8_t SR_flag = 0; // no SR in PUCCH implemented for now
  NR_ServingCellConfigCommon_t *scc = RC.nrmac[Mod_idP]->common_channels->ServingCellConfigCommon;
  NR_UE_list_t *UE_list = &RC.nrmac[Mod_idP]->UE_list;
  AssertFatal(UE_list->active[UE_id] >=0,"Cannot find UE_id %d is not active\n",UE_id);

  NR_CellGroupConfig_t *secondaryCellGroup = UE_list->secondaryCellGroup[UE_id];
  int bwp_id=1;
  NR_BWP_Uplink_t *ubwp=secondaryCellGroup->spCellConfig->spCellConfigDedicated->uplinkConfig->uplinkBWP_ToAddModList->list.array[bwp_id-1];
  nfapi_nr_ul_tti_request_t *UL_tti_req = &RC.nrmac[Mod_idP]->UL_tti_req[0];

308 309 310 311 312 313 314 315
  NR_sched_pucch *curr_pucch;
  int nr_ulmix_slots = scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSlots;
  if (scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSymbols!=0)
    nr_ulmix_slots++;

  for (int k=0; k<nr_ulmix_slots; k++) {
    curr_pucch = &UE_list->UE_sched_ctrl[UE_id].sched_pucch[k];
    if ((curr_pucch->dai_c > 0) && (frameP == curr_pucch->frame) && (slotP == curr_pucch->ul_slot)) {
316 317 318 319 320 321
      UL_tti_req->SFN = frameP;
      UL_tti_req->Slot = slotP;
      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_type = NFAPI_NR_UL_CONFIG_PUCCH_PDU_TYPE;
      UL_tti_req->pdus_list[UL_tti_req->n_pdus].pdu_size = sizeof(nfapi_nr_pucch_pdu_t);
      nfapi_nr_pucch_pdu_t  *pucch_pdu = &UL_tti_req->pdus_list[UL_tti_req->n_pdus].pucch_pdu;
      memset(pucch_pdu,0,sizeof(nfapi_nr_pucch_pdu_t));
Francesco Mani's avatar
Francesco Mani committed
322
      UL_tti_req->n_pdus+=1;
323 324 325
      O_ack = curr_pucch->dai_c;
      O_uci = O_ack; // for now we are just sending acknacks in pucch

326 327
      LOG_I(MAC, "Scheduling pucch reception for frame %d slot %d\n", frameP, slotP);

328 329 330
      nr_configure_pucch(pucch_pdu,
			 scc,
			 ubwp,
Francesco Mani's avatar
Francesco Mani committed
331
                         curr_pucch->resource_indicator,
332 333 334 335
                         O_uci,
                         O_ack,
                         SR_flag);

336
      curr_pucch->dai_c = 0;
337 338 339 340
    }
  }
}

341 342 343 344 345 346 347 348
bool is_xlsch_in_slot(uint64_t bitmap, sub_frame_t slot){

  if((bitmap>>slot)&0x01)
    return true;
  else
    return false;
}

349
void gNB_dlsch_ulsch_scheduler(module_id_t module_idP,
350 351 352 353
                               frame_t frame_rxP,
                               sub_frame_t slot_rxP,
                               frame_t frame_txP,
                               sub_frame_t slot_txP){
354 355

  //printf("gNB_dlsch_ulsch_scheduler frameRX %d slotRX %d frameTX %d slotTX %d\n",frame_rxP,slot_rxP,frame_txP,slot_txP);
356
			       
357
  protocol_ctxt_t   ctxt;
358
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, ENB_FLAG_YES, NOT_A_RNTI, frame_txP, slot_txP,module_idP);
359
 
360
  int CC_id;
361 362 363
  int UE_id;
  uint64_t *dlsch_in_slot_bitmap=NULL;
  uint64_t *ulsch_in_slot_bitmap=NULL;
Francesco Mani's avatar
Francesco Mani committed
364
  int pucch_sched;
365

Francesco Mani's avatar
Francesco Mani committed
366 367
  UE_id=0;
  int bwp_id = 1;
368

369
  gNB_MAC_INST *gNB = RC.nrmac[module_idP];
cig's avatar
cig committed
370
  NR_UE_list_t *UE_list = &gNB->UE_list;
371
  NR_UE_sched_ctrl_t *ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
372
  NR_COMMON_channels_t *cc      = RC.nrmac[module_idP]->common_channels;
373 374
  NR_ServingCellConfigCommon_t        *scc     = cc->ServingCellConfigCommon;
  int num_slots_per_tdd = (nr_slots_per_frame[*scc->ssbSubcarrierSpacing])>>(7-scc->tdd_UL_DL_ConfigurationCommon->pattern1.dl_UL_TransmissionPeriodicity);
375

376 377 378 379
  int nr_ulmix_slots = scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSlots;
  if (scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSymbols!=0)
    nr_ulmix_slots++;

380
  if ((slot_txP == 0) && (UE_list->fiveG_connected[UE_id] || get_softmodem_params()->phy_test)) {
381 382 383 384 385 386 387
    for (int k=0; k<nr_ulmix_slots; k++) {
      memset((void *) &UE_list->UE_sched_ctrl[UE_id].sched_pucch[k],
             0,
             sizeof(NR_sched_pucch));
    }
  }

388 389
  start_meas(&RC.nrmac[module_idP]->eNB_scheduler);
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_IN);
390

391 392
  pdcp_run(&ctxt);
  //rrc_rx_tx(&ctxt, CC_id);
393

394 395
  RC.nrmac[module_idP]->frame    = frame_rxP;
  RC.nrmac[module_idP]->slot     = slot_rxP;
396

397
  if (get_softmodem_params()->phy_test) {
398 399
    dlsch_in_slot_bitmap = &RC.nrmac[module_idP]->UE_list.UE_sched_ctrl[UE_id].dlsch_in_slot_bitmap;  // static bitmap signaling which slot in a tdd period contains dlsch
    ulsch_in_slot_bitmap = &RC.nrmac[module_idP]->UE_list.UE_sched_ctrl[UE_id].ulsch_in_slot_bitmap;  // static bitmap signaling which slot in a tdd period contains ulsch
400

401
    // hardcoding dlsch to be in slot 1
402
    if (!(slot_txP%num_slots_per_tdd)) {
403 404 405 406 407
      if(slot_txP==0)
        *dlsch_in_slot_bitmap = 0x02;
      else
        *dlsch_in_slot_bitmap = 0x00;
    }
408

409
    // hardcoding ulsch to be in slot 8
410
    if (!(slot_rxP%num_slots_per_tdd)) {
411 412 413 414 415
      if(slot_rxP==0)
        *ulsch_in_slot_bitmap = 0x100;
      else
        *ulsch_in_slot_bitmap = 0x00;
    }
416
  }
417

418 419
  // Check if there are downlink symbols in the slot, 
  if (is_nr_DL_slot(cc->ServingCellConfigCommon,slot_txP)) {
Francesco Mani's avatar
Francesco Mani committed
420 421
    memset(RC.nrmac[module_idP]->cce_list[bwp_id][0],0,MAX_NUM_CCE*sizeof(int)); // coreset0
    memset(RC.nrmac[module_idP]->cce_list[bwp_id][1],0,MAX_NUM_CCE*sizeof(int)); // coresetid 1
422 423
    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
      //mbsfn_status[CC_id] = 0;
424

425 426 427
      // clear vrb_maps
      memset(cc[CC_id].vrb_map, 0, 100);
      memset(cc[CC_id].vrb_map_UL, 0, 100);
428

429 430
      clear_nr_nfapi_information(RC.nrmac[module_idP], CC_id, frame_txP, slot_txP);
    }
431

432 433 434 435
    // refresh UE list based on UEs dropped by PHY in previous subframe
    /*
    for (i = 0; i < MAX_MOBILES_PER_GNB; i++) {
      if (UE_list->active[i]) {
436

437
        nfapi_nr_config_request_t *cfg = &RC.nrmac[module_idP]->config[CC_id];      
Raymond Knopp's avatar
Raymond Knopp committed
438
      
439 440
        rnti = 0;//UE_RNTI(module_idP, i);
        CC_id = 0;//UE_PCCID(module_idP, i);
441
      
442 443 444
      } //END if (UE_list->active[i])
    } //END for (i = 0; i < MAX_MOBILES_PER_GNB; i++)
    */
445

446 447 448 449
    // This schedules MIB
    if((slot_txP == 0) && (frame_txP & 7) == 0){
      schedule_nr_mib(module_idP, frame_txP, slot_txP);
    }
450

451 452 453 454 455 456 457 458 459 460 461 462 463
    // TbD once RACH is available, start ta_timer when UE is connected
    if (ue_sched_ctl->ta_timer) ue_sched_ctl->ta_timer--;

    if (ue_sched_ctl->ta_timer == 0) {
      gNB->ta_command = ue_sched_ctl->ta_update;
      /* if time is up, then set the timer to not send it for 5 frames
      // regardless of the TA value */
      ue_sched_ctl->ta_timer = 100;
      /* reset ta_update */
      ue_sched_ctl->ta_update = 31;
      /* MAC CE flag indicating TA length */
      gNB->ta_len = 2;
    }
464

465 466
    if (get_softmodem_params()->phy_test == 0)
      nr_schedule_RA(module_idP, frame_txP, slot_txP);
467

468
    // Phytest scheduling
469
    if (get_softmodem_params()->phy_test && (is_xlsch_in_slot(*dlsch_in_slot_bitmap,slot_txP%num_slots_per_tdd))) {
470
      ue_sched_ctl->current_harq_pid = slot_txP % num_slots_per_tdd;
Francesco Mani's avatar
Francesco Mani committed
471 472
      nr_update_pucch_scheduling(module_idP, UE_id, frame_txP, slot_txP, num_slots_per_tdd,&pucch_sched);
      nr_schedule_uss_dlsch_phytest(module_idP, frame_txP, slot_txP, &UE_list->UE_sched_ctrl[UE_id].sched_pucch[pucch_sched], NULL);
473 474
      // resetting ta flag
      gNB->ta_len = 0;
475
    }
476

477
    // Test DL scheduling
478
    if (get_softmodem_params()->phy_test == 0 && slot_txP>0 && slot_txP<7 && UE_list->fiveG_connected[UE_id]) {
479
      ue_sched_ctl->current_harq_pid = slot_txP % num_slots_per_tdd;
Francesco Mani's avatar
Francesco Mani committed
480 481
      nr_update_pucch_scheduling(module_idP, UE_id, frame_txP, slot_txP, num_slots_per_tdd,&pucch_sched);
      nr_schedule_uss_dlsch_phytest(module_idP, frame_txP, slot_txP, &UE_list->UE_sched_ctrl[UE_id].sched_pucch[pucch_sched], NULL);
482 483 484 485
      // resetting ta flag
      gNB->ta_len = 0;
    }

486

487 488 489 490 491
    /*
    // Allocate CCEs for good after scheduling is done
    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++)
      allocate_CCEs(module_idP, CC_id, subframeP, 0);
    */
492

493 494 495
  } //is_nr_DL_slot

  if (is_nr_UL_slot(cc->ServingCellConfigCommon,slot_rxP)) { 
496

497
    if (get_softmodem_params()->phy_test == 0) {
498
      if (UE_list->fiveG_connected[UE_id])
499
        nr_schedule_pucch(module_idP, UE_id, frame_rxP, slot_rxP);
500 501 502
      schedule_nr_prach(module_idP, (frame_rxP+1)&1023, slot_rxP);
      nr_schedule_reception_msg3(module_idP, 0, frame_rxP, slot_rxP);
    }
503
    if (get_softmodem_params()->phy_test){
504 505 506 507
      nr_schedule_pucch(module_idP, UE_id, frame_rxP, slot_rxP);
      if (is_xlsch_in_slot(*ulsch_in_slot_bitmap,slot_rxP%num_slots_per_tdd)){
        nr_schedule_uss_ulsch_phytest(module_idP, frame_rxP, slot_rxP);
      }
508 509 510
    }
  }

511
  stop_meas(&RC.nrmac[module_idP]->eNB_scheduler);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
512
  
513
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_OUT);
Raymond Knopp's avatar
Raymond Knopp committed
514
}