NR_IF_Module.c 15.3 KB
Newer Older
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * 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 openair2/NR_PHY_INTERFACE/NR_IF_Module.c
* \brief data structures for PHY/MAC interface modules
* \author EURECOM/NTUST
* \date 2018
* \version 0.1
* \company Eurecom, NTUST
* \email: raymond.knopp@eurecom.fr, kroempa@gmail.com
* \note
* \warning
*/

33 34
#include "openair1/PHY/defs_eNB.h"
#include "openair1/PHY/phy_extern.h"
35 36
#include "openair1/SCHED_NR/fapi_nr_l1.h"
#include "openair2/NR_PHY_INTERFACE/NR_IF_Module.h"
37
#include "LAYER2/MAC/mac_extern.h"
38
#include "LAYER2/MAC/mac_proto.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
#include "common/ran_context.h"

#define MAX_IF_MODULES 100

NR_IF_Module_t *if_inst[MAX_IF_MODULES];
NR_Sched_Rsp_t Sched_INFO[MAX_IF_MODULES][MAX_NUM_CCs];

extern int oai_nfapi_harq_indication(nfapi_harq_indication_t *harq_ind);
extern int oai_nfapi_crc_indication(nfapi_crc_indication_t *crc_ind);
extern int oai_nfapi_cqi_indication(nfapi_cqi_indication_t *cqi_ind);
extern int oai_nfapi_sr_indication(nfapi_sr_indication_t *ind);
extern int oai_nfapi_rx_ind(nfapi_rx_indication_t *ind);
extern uint8_t nfapi_mode;
extern uint16_t sf_ahead;

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
55 56 57 58 59 60 61
void handle_nr_rach(NR_UL_IND_t *UL_info) {
  int i;

  if (UL_info->rach_ind.rach_indication_body.number_of_preambles>0) {

    AssertFatal(UL_info->rach_ind.rach_indication_body.number_of_preambles==1,"More than 1 preamble not supported\n");
    UL_info->rach_ind.rach_indication_body.number_of_preambles=0;
62
    LOG_D(MAC,"UL_info[Frame %d, Slot %d] Calling initiate_ra_proc RACH:SFN/SF:%d\n",UL_info->frame,UL_info->slot, NFAPI_SFNSF2DEC(UL_info->rach_ind.sfn_sf));
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
63 64 65 66 67 68 69
    initiate_ra_proc(UL_info->module_id,
         UL_info->CC_id,
         NFAPI_SFNSF2SFN(UL_info->rach_ind.sfn_sf),
         NFAPI_SFNSF2SF(UL_info->rach_ind.sfn_sf),
         UL_info->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.preamble,
         UL_info->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.timing_advance,
         UL_info->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.rnti
70
#if (RRC_VERSION >= MAKE_VERSION(14, 0, 0))
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
71 72 73 74 75
         ,0
#endif
         );
  }

76
#if (RRC_VERSION >= MAKE_VERSION(14, 0, 0))
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
77 78 79 80 81 82
  if (UL_info->rach_ind_br.rach_indication_body.number_of_preambles>0) {

    AssertFatal(UL_info->rach_ind_br.rach_indication_body.number_of_preambles<5,"More than 4 preambles not supported\n");
    for (i=0;i<UL_info->rach_ind_br.rach_indication_body.number_of_preambles;i++) {
      AssertFatal(UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel13.rach_resource_type>0,
      "Got regular PRACH preamble, not BL/CE\n");
83
      LOG_D(MAC,"Frame %d, Slot %d Calling initiate_ra_proc (CE_level %d)\n",UL_info->frame,UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
84 85 86 87
      UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel13.rach_resource_type-1);
      initiate_ra_proc(UL_info->module_id,
           UL_info->CC_id,
           UL_info->frame,
88
           UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
           UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel8.preamble,
           UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel8.timing_advance,
           UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel8.rnti,
           UL_info->rach_ind_br.rach_indication_body.preamble_list[i].preamble_rel13.rach_resource_type);
    }
    UL_info->rach_ind_br.rach_indication_body.number_of_preambles=0;
  }
#endif
}

void handle_nr_sr(NR_UL_IND_t *UL_info) {

  int i;

  if (nfapi_mode == 1)  // PNF
  {
    if (UL_info->sr_ind.sr_indication_body.number_of_srs>0)
    {
      oai_nfapi_sr_indication(&UL_info->sr_ind);
    }
  }
  else
  {
    for (i=0;i<UL_info->sr_ind.sr_indication_body.number_of_srs;i++)
      SR_indication(UL_info->module_id,
          UL_info->CC_id,
          UL_info->frame,
116
          UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
          UL_info->sr_ind.sr_indication_body.sr_pdu_list[i].rx_ue_information.rnti,
          UL_info->sr_ind.sr_indication_body.sr_pdu_list[i].ul_cqi_information.ul_cqi);
  }

  UL_info->sr_ind.sr_indication_body.number_of_srs=0;
}

void handle_nr_cqi(NR_UL_IND_t *UL_info) {

  int i;

  if (nfapi_mode == 1)
  {
    if (UL_info->cqi_ind.number_of_cqis>0)
    {
      LOG_D(PHY,"UL_info->cqi_ind.number_of_cqis:%d\n", UL_info->cqi_ind.number_of_cqis);
      nfapi_cqi_indication_t ind;

      ind.header.message_id = NFAPI_RX_CQI_INDICATION;
136
      ind.sfn_sf = UL_info->frame<<4 | UL_info->slot;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
137 138 139 140 141 142 143 144 145 146 147 148 149
      ind.cqi_indication_body = UL_info->cqi_ind;

      oai_nfapi_cqi_indication(&ind);

      UL_info->cqi_ind.number_of_cqis=0;
    }
  }
  else
  {
    for (i=0;i<UL_info->cqi_ind.number_of_cqis;i++) 
      cqi_indication(UL_info->module_id,
          UL_info->CC_id,
          UL_info->frame,
150
          UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
          UL_info->cqi_ind.cqi_pdu_list[i].rx_ue_information.rnti,
          &UL_info->cqi_ind.cqi_pdu_list[i].cqi_indication_rel9,
          UL_info->cqi_ind.cqi_raw_pdu_list[i].pdu,
          &UL_info->cqi_ind.cqi_pdu_list[i].ul_cqi_information);

    UL_info->cqi_ind.number_of_cqis=0;
  }
}

void handle_nr_harq(NR_UL_IND_t *UL_info) {

  int i;

  if (nfapi_mode == 1 && UL_info->harq_ind.harq_indication_body.number_of_harqs>0) // PNF
  {
    //LOG_D(PHY, "UL_info->harq_ind.harq_indication_body.number_of_harqs:%d Send to VNF\n", UL_info->harq_ind.harq_indication_body.number_of_harqs);

    int retval = oai_nfapi_harq_indication(&UL_info->harq_ind);

    if (retval!=0)
    {
      LOG_E(PHY, "Failed to encode NFAPI HARQ_IND retval:%d\n", retval);
    }

    UL_info->harq_ind.harq_indication_body.number_of_harqs = 0;
  }
  else
  {
    for (i=0;i<UL_info->harq_ind.harq_indication_body.number_of_harqs;i++)
      harq_indication(UL_info->module_id,
          UL_info->CC_id,
          NFAPI_SFNSF2SFN(UL_info->harq_ind.sfn_sf),
          NFAPI_SFNSF2SF(UL_info->harq_ind.sfn_sf),
          &UL_info->harq_ind.harq_indication_body.harq_pdu_list[i]);

    UL_info->harq_ind.harq_indication_body.number_of_harqs=0;
  }
}

void handle_nr_ulsch(NR_UL_IND_t *UL_info) {

  int i,j;

  if(nfapi_mode == 1)
  {
    if (UL_info->crc_ind.crc_indication_body.number_of_crcs>0)
    {
      //LOG_D(PHY,"UL_info->crc_ind.crc_indication_body.number_of_crcs:%d CRC_IND:SFN/SF:%d\n", UL_info->crc_ind.crc_indication_body.number_of_crcs, NFAPI_SFNSF2DEC(UL_info->crc_ind.sfn_sf));

      oai_nfapi_crc_indication(&UL_info->crc_ind);

      UL_info->crc_ind.crc_indication_body.number_of_crcs = 0;
    }

    if (UL_info->rx_ind.rx_indication_body.number_of_pdus>0)
    {
      //LOG_D(PHY,"UL_info->rx_ind.number_of_pdus:%d RX_IND:SFN/SF:%d\n", UL_info->rx_ind.rx_indication_body.number_of_pdus, NFAPI_SFNSF2DEC(UL_info->rx_ind.sfn_sf));
      oai_nfapi_rx_ind(&UL_info->rx_ind);
      UL_info->rx_ind.rx_indication_body.number_of_pdus = 0;
    }
  }
  else
  {
    if (UL_info->rx_ind.rx_indication_body.number_of_pdus>0 && UL_info->crc_ind.crc_indication_body.number_of_crcs>0) {
      for (i=0;i<UL_info->rx_ind.rx_indication_body.number_of_pdus;i++) {
        for (j=0;j<UL_info->crc_ind.crc_indication_body.number_of_crcs;j++) {
          // find crc_indication j corresponding rx_indication i
          LOG_D(PHY,"UL_info->crc_ind.crc_indication_body.crc_pdu_list[%d].rx_ue_information.rnti:%04x UL_info->rx_ind.rx_indication_body.rx_pdu_list[%d].rx_ue_information.rnti:%04x\n", j, UL_info->crc_ind.crc_indication_body.crc_pdu_list[j].rx_ue_information.rnti, i, UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_ue_information.rnti);
          if (UL_info->crc_ind.crc_indication_body.crc_pdu_list[j].rx_ue_information.rnti ==
              UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_ue_information.rnti) {
            LOG_D(PHY, "UL_info->crc_ind.crc_indication_body.crc_pdu_list[%d].crc_indication_rel8.crc_flag:%d\n", j, UL_info->crc_ind.crc_indication_body.crc_pdu_list[j].crc_indication_rel8.crc_flag);
            if (UL_info->crc_ind.crc_indication_body.crc_pdu_list[j].crc_indication_rel8.crc_flag == 1) { // CRC error indication
223
              LOG_D(MAC,"Frame %d, Slot %d Calling rx_sdu (CRC error) \n",UL_info->frame,UL_info->slot);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
224 225 226
              rx_sdu(UL_info->module_id,
                  UL_info->CC_id,
                  NFAPI_SFNSF2SFN(UL_info->rx_ind.sfn_sf), //UL_info->frame,
227
                  NFAPI_SFNSF2SF(UL_info->rx_ind.sfn_sf), //UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
228 229 230 231 232 233 234
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_ue_information.rnti,
                  (uint8_t *)NULL,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.length,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.timing_advance,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.ul_cqi);
            }
            else {
235
              LOG_D(MAC,"Frame %d, Slot %d Calling rx_sdu (CRC ok) \n",UL_info->frame,UL_info->slot);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
236 237 238
              rx_sdu(UL_info->module_id,
                  UL_info->CC_id,
                  NFAPI_SFNSF2SFN(UL_info->rx_ind.sfn_sf), //UL_info->frame,
239
                  NFAPI_SFNSF2SF(UL_info->rx_ind.sfn_sf), //UL_info->slot,
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
240 241 242 243 244 245 246 247 248 249 250 251 252
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_ue_information.rnti,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].data,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.length,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.timing_advance,
                  UL_info->rx_ind.rx_indication_body.rx_pdu_list[i].rx_indication_rel8.ul_cqi);
            }
            break;
          } //if (UL_info->crc_ind.crc_pdu_list[j].rx_ue_information.rnti ==
          //    UL_info->rx_ind.rx_pdu_list[i].rx_ue_information.rnti)
        } //    for (j=0;j<UL_info->crc_ind.crc_indication_body.number_of_crcs;j++)
      } //   for (i=0;i<UL_info->rx_ind.number_of_pdus;i++)
      UL_info->crc_ind.crc_indication_body.number_of_crcs=0;
      UL_info->rx_ind.rx_indication_body.number_of_pdus = 0;
253
    } // UL_info->rx_ind.rx_indication_body.number_of_pdus>0 && UL_info->slot && UL_info->crc_ind.crc_indication_body.number_of_crcs>0
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
254 255 256 257
    else if (UL_info->rx_ind.rx_indication_body.number_of_pdus!=0 || UL_info->crc_ind.crc_indication_body.number_of_crcs!=0) {
      LOG_E(PHY,"hoping not to have mis-match between CRC ind and RX ind - hopefully the missing message is coming shortly rx_ind:%d(SFN/SF:%05d) crc_ind:%d(SFN/SF:%05d) UL_info(SFN/SF):%04d%d\n",
          UL_info->rx_ind.rx_indication_body.number_of_pdus, NFAPI_SFNSF2DEC(UL_info->rx_ind.sfn_sf),
          UL_info->crc_ind.crc_indication_body.number_of_crcs, NFAPI_SFNSF2DEC(UL_info->crc_ind.sfn_sf),
258
          UL_info->frame, UL_info->slot);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
259 260 261 262
    }
  }
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
void NR_UL_indication(NR_UL_IND_t *UL_info)
{

  AssertFatal(UL_info!=NULL,"UL_INFO is null\n");

#ifdef DUMP_FAPI
  dump_ul(UL_info);
#endif

  module_id_t  module_id   = UL_info->module_id;
  int          CC_id       = UL_info->CC_id;
  NR_Sched_Rsp_t  *sched_info = &Sched_INFO[module_id][CC_id];
  NR_IF_Module_t  *ifi        = if_inst[module_id];
  gNB_MAC_INST *mac        = RC.nrmac[module_id];

  LOG_D(PHY,"SFN/SF:%d%d module_id:%d CC_id:%d UL_info[rx_ind:%d harqs:%d crcs:%d cqis:%d preambles:%d sr_ind:%d]\n",
279
        UL_info->frame,UL_info->slot,
280 281 282 283 284 285 286
        module_id,CC_id,
        UL_info->rx_ind.rx_indication_body.number_of_pdus, UL_info->harq_ind.harq_indication_body.number_of_harqs, UL_info->crc_ind.crc_indication_body.number_of_crcs, UL_info->cqi_ind.number_of_cqis, UL_info->rach_ind.rach_indication_body.number_of_preambles, UL_info->sr_ind.sr_indication_body.number_of_srs);

  if (nfapi_mode != 1)
  {
    if (ifi->CC_mask==0) {
      ifi->current_frame    = UL_info->frame;
287
      ifi->current_slot = UL_info->slot;
288 289 290
    }
    else {
      AssertFatal(UL_info->frame != ifi->current_frame,"CC_mask %x is not full and frame has changed\n",ifi->CC_mask);
291
      AssertFatal(UL_info->slot != ifi->current_slot,"CC_mask %x is not full and slot has changed\n",ifi->CC_mask);
292 293 294 295 296 297
    }
    ifi->CC_mask |= (1<<CC_id);
  }


  // clear DL/UL info for new scheduling round
298
  clear_nr_nfapi_information(RC.nrmac[module_id],CC_id,UL_info->frame,UL_info->slot);
299

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
300
  handle_nr_rach(UL_info);
301

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
302
  handle_nr_sr(UL_info);
303

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
304
  handle_nr_cqi(UL_info);
305

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
306
  handle_nr_harq(UL_info);
307 308 309 310

  // clear HI prior to handling ULSCH
  mac->HI_DCI0_req[CC_id].hi_dci0_request_body.number_of_hi                     = 0;
  
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
311
  handle_nr_ulsch(UL_info);
312 313 314 315

  if (nfapi_mode != 1)
  {
    if (ifi->CC_mask == ((1<<MAX_NUM_CCs)-1)) {
316
      /*
317
      eNB_dlsch_ulsch_scheduler(module_id,
318 319
          (UL_info->frame+((UL_info->slot>(9-sf_ahead))?1:0)) % 1024,
          (UL_info->slot+sf_ahead)%10);
320 321
      */
      
322
      gNB_dlsch_ulsch_scheduler(module_id,
323 324
          (UL_info->frame+((UL_info->slot>(9-sf_ahead))?1:0)) % 1024,
          (UL_info->slot+sf_ahead)%10);
325
      
326 327 328 329
      ifi->CC_mask            = 0;

      sched_info->module_id   = module_id;
      sched_info->CC_id       = CC_id;
330 331
      sched_info->frame       = (UL_info->frame + ((UL_info->slot>(9-sf_ahead)) ? 1 : 0)) % 1024;
      sched_info->slot    = (UL_info->slot+sf_ahead)%10;
332 333 334
      sched_info->DL_req      = &mac->DL_req[CC_id];
      sched_info->HI_DCI0_req = &mac->HI_DCI0_req[CC_id];
      if ((mac->common_channels[CC_id].tdd_Config==NULL) ||
335
          (is_nr_UL_sf(&mac->common_channels[CC_id],(sched_info->slot+sf_ahead)%10)>0))
336 337 338 339 340 341 342 343 344 345
        sched_info->UL_req      = &mac->UL_req[CC_id];
      else
        sched_info->UL_req      = NULL;

      sched_info->TX_req      = &mac->TX_req[CC_id];

#ifdef DUMP_FAPI
      dump_dl(sched_info);
#endif

346
      if (ifi->NR_Schedule_response)
347
      {
348 349 350 351 352
        AssertFatal(ifi->NR_Schedule_response!=NULL,
                    "nr_schedule_response is null (mod %d, cc %d)\n",
                    module_id,
                    CC_id);
        ifi->NR_Schedule_response(sched_info);
353 354
      }

355
      LOG_D(PHY,"NR_Schedule_response: SFN_SF:%d%d dl_pdus:%d\n",sched_info->frame,sched_info->slot,sched_info->DL_req->dl_config_request_body.number_pdu);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    }
  }
}

NR_IF_Module_t *NR_IF_Module_init(int Mod_id){

  AssertFatal(Mod_id<MAX_MODULES,"Asking for Module %d > %d\n",Mod_id,MAX_IF_MODULES);

  LOG_D(PHY,"Installing callbacks for IF_Module - UL_indication\n");

  if (if_inst[Mod_id]==NULL) {
    if_inst[Mod_id] = (NR_IF_Module_t*)malloc(sizeof(NR_IF_Module_t));
    memset((void*)if_inst[Mod_id],0,sizeof(NR_IF_Module_t));

    if_inst[Mod_id]->CC_mask=0;
371
    if_inst[Mod_id]->NR_UL_indication = NR_UL_indication;
372 373 374 375 376

    AssertFatal(pthread_mutex_init(&if_inst[Mod_id]->if_mutex,NULL)==0,
        "allocation of if_inst[%d]->if_mutex fails\n",Mod_id);
  }
  return if_inst[Mod_id];
377
}