NR_IF_Module.c 10.6 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/NR_MAC_COMMON/nr_mac_extern.h"
38
#include "LAYER2/MAC/mac_proto.h"
39 40
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
#include "common/ran_context.h"
41
#include "executables/softmodem-common.h"
42 43

#define MAX_IF_MODULES 100
44
//#define UL_HARQ_PRINT
45 46 47 48 49 50 51 52 53 54 55

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;
56
extern uint16_t sl_ahead;
57

58
void handle_nr_rach(NR_UL_IND_t *UL_info) {
59 60 61 62 63 64
  if (UL_info->rach_ind.number_of_pdus>0) {
    AssertFatal(UL_info->rach_ind.number_of_pdus==1,"More than 1 RACH pdu not supported\n");
    UL_info->rach_ind.number_of_pdus=0;
    LOG_D(MAC,"UL_info[Frame %d, Slot %d] Calling initiate_ra_proc RACH:SFN/SLOT:%d/%d\n",UL_info->frame,UL_info->slot, UL_info->rach_ind.sfn,UL_info->rach_ind.slot);

    if (UL_info->rach_ind.pdu_list[0].num_preamble>0)
65 66
    AssertFatal(UL_info->rach_ind.pdu_list[0].num_preamble==1,
		"More than 1 preamble not supported\n");
67 68
    
    nr_initiate_ra_proc(UL_info->module_id,
69 70 71 72 73 74
                        UL_info->CC_id,
                        UL_info->rach_ind.sfn,
                        UL_info->rach_ind.slot,
                        UL_info->rach_ind.pdu_list[0].preamble_list[0].preamble_index,
                        UL_info->rach_ind.pdu_list[0].freq_index,
                        UL_info->rach_ind.pdu_list[0].symbol_index,
75
                        UL_info->rach_ind.pdu_list[0].preamble_list[0].timing_advance);
76

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
77 78 79 80
  }
}


81
void handle_nr_ulsch(NR_UL_IND_t *UL_info, NR_UE_sched_ctrl_t *sched_ctrl, NR_mac_stats_t *stats) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
82

83
  if(nfapi_mode == 1) {
84
    if (UL_info->crc_ind.number_crcs>0) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
85
      //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));
Raymond Knopp's avatar
Raymond Knopp committed
86
      //      oai_nfapi_crc_indication(&UL_info->crc_ind);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
87

88
      UL_info->crc_ind.number_crcs = 0;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
89 90
    }

91
    if (UL_info->rx_ind.number_of_pdus>0) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
92
      //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));
Raymond Knopp's avatar
Raymond Knopp committed
93
      //      oai_nfapi_rx_ind(&UL_info->rx_ind);
94
      UL_info->rx_ind.number_of_pdus = 0;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
95
    }
96
  } else {
cig's avatar
cig committed
97

98 99 100
    if (UL_info->rx_ind.number_of_pdus>0 && UL_info->crc_ind.number_crcs>0) {
      for (int i=0; i<UL_info->rx_ind.number_of_pdus; i++) {
        for (int j=0; j<UL_info->crc_ind.number_crcs; j++) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
101
          // find crc_indication j corresponding rx_indication i
102
          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,
103
                UL_info->crc_ind.crc_list[j].rnti, i, UL_info->rx_ind.pdu_list[i].rnti);
104

105 106 107
          if (UL_info->crc_ind.crc_list[j].rnti ==
              UL_info->rx_ind.pdu_list[i].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_list[j].tb_crc_status);
108

109 110
            handle_nr_ul_harq(UL_info->slot, sched_ctrl, stats, UL_info->crc_ind.crc_list[j]);

111
            if (UL_info->crc_ind.crc_list[j].tb_crc_status == 1) { // CRC error indication
112
              LOG_D(MAC,"Frame %d, Slot %d Calling rx_sdu (CRC error) \n",UL_info->frame,UL_info->slot);
cig's avatar
cig committed
113

114
              nr_rx_sdu(UL_info->module_id,
cig's avatar
cig committed
115
                        UL_info->CC_id,
116 117 118
                        UL_info->rx_ind.sfn, //UL_info->frame,
                        UL_info->rx_ind.slot, //UL_info->slot,
                        UL_info->rx_ind.pdu_list[i].rnti,
cig's avatar
cig committed
119
                        (uint8_t *)NULL,
120 121
                        UL_info->rx_ind.pdu_list[i].pdu_length,
                        UL_info->rx_ind.pdu_list[i].timing_advance,
122 123
                        UL_info->rx_ind.pdu_list[i].ul_cqi,
                        UL_info->rx_ind.pdu_list[i].rssi);
cig's avatar
cig committed
124
            } else {
125
              LOG_D(MAC,"Frame %d, Slot %d Calling rx_sdu (CRC ok) \n",UL_info->frame,UL_info->slot);
126
              nr_rx_sdu(UL_info->module_id,
cig's avatar
cig committed
127
                        UL_info->CC_id,
128 129 130 131 132 133
                        UL_info->rx_ind.sfn, //UL_info->frame,
                        UL_info->rx_ind.slot, //UL_info->slot,
                        UL_info->rx_ind.pdu_list[i].rnti,
                        UL_info->rx_ind.pdu_list[i].pdu,
                        UL_info->rx_ind.pdu_list[i].pdu_length,
                        UL_info->rx_ind.pdu_list[i].timing_advance,
134 135
                        UL_info->rx_ind.pdu_list[i].ul_cqi,
                        UL_info->rx_ind.pdu_list[i].rssi);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
136 137
            }
            break;
138 139
          }
        } //    for (j=0;j<UL_info->crc_ind.number_crcs;j++)
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
140
      } //   for (i=0;i<UL_info->rx_ind.number_of_pdus;i++)
141

142 143 144 145 146 147 148
      UL_info->crc_ind.number_crcs=0;
      UL_info->rx_ind.number_of_pdus = 0;
    }
    else if (UL_info->rx_ind.number_of_pdus!=0 || UL_info->crc_ind.number_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/SL:%d/%d) crc_ind:%d(SFN/SL:%d/%d) \n",
            UL_info->rx_ind.number_of_pdus, UL_info->rx_ind.sfn, UL_info->rx_ind.slot,
            UL_info->crc_ind.number_crcs, UL_info->rx_ind.sfn, UL_info->rx_ind.slot);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
149 150 151 152
    }
  }
}

153
void NR_UL_indication(NR_UL_IND_t *UL_info) {
154 155 156 157
  AssertFatal(UL_info!=NULL,"UL_INFO is null\n");
#ifdef DUMP_FAPI
  dump_ul(UL_info);
#endif
158 159 160 161 162
  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];
163
  LOG_D(PHY,"SFN/SF:%d%d module_id:%d CC_id:%d UL_info[rach_pdus:%d rx_ind:%d crcs:%d]\n",
164
        UL_info->frame,UL_info->slot,
165
        module_id,CC_id, UL_info->rach_ind.number_of_pdus,
166
        UL_info->rx_ind.number_of_pdus, UL_info->crc_ind.number_crcs);
167

168
  if (nfapi_mode != 1) {
169 170
    if (ifi->CC_mask==0) {
      ifi->current_frame    = UL_info->frame;
171
      ifi->current_slot = UL_info->slot;
172
    } else {
173
      AssertFatal(UL_info->frame != ifi->current_frame,"CC_mask %x is not full and frame has changed\n",ifi->CC_mask);
174
      AssertFatal(UL_info->slot != ifi->current_slot,"CC_mask %x is not full and slot has changed\n",ifi->CC_mask);
175
    }
176

177 178 179 180
    ifi->CC_mask |= (1<<CC_id);
  }

  // clear DL/UL info for new scheduling round
181
  clear_nr_nfapi_information(mac,CC_id,UL_info->frame,UL_info->slot);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
182
  handle_nr_rach(UL_info);
183 184
  
  handle_nr_uci(UL_info,&mac->UE_list.UE_sched_ctrl[0],&mac->UE_list.mac_stats[0],mac->pucch_target_snrx10);
185
  // clear HI prior to handling ULSCH
186
  mac->UL_dci_req[CC_id].numPdus = 0;
187
  handle_nr_ulsch(UL_info, &mac->UE_list.UE_sched_ctrl[0],&mac->UE_list.mac_stats[0]);
188

189
  if (nfapi_mode != 1) {
190
    if (ifi->CC_mask == ((1<<MAX_NUM_CCs)-1)) {
191
      /*
192
      eNB_dlsch_ulsch_scheduler(module_id,
193 194
          (UL_info->frame+((UL_info->slot>(9-sl_ahead))?1:0)) % 1024,
          (UL_info->slot+sl_ahead)%10);
195
      */
196
      nfapi_nr_config_request_scf_t *cfg = &mac->config[CC_id];
197
      int spf = get_spf(cfg);
198
      gNB_dlsch_ulsch_scheduler(module_id,
199 200
				(UL_info->frame+((UL_info->slot>(spf-1-sl_ahead))?1:0)) % 1024,
				(UL_info->slot+sl_ahead)%spf);
201
      
202 203 204
      ifi->CC_mask            = 0;
      sched_info->module_id   = module_id;
      sched_info->CC_id       = CC_id;
205 206
      sched_info->frame       = (UL_info->frame + ((UL_info->slot>(spf-1-sl_ahead)) ? 1 : 0)) % 1024;
      sched_info->slot        = (UL_info->slot+sl_ahead)%spf;
207
      sched_info->DL_req      = &mac->DL_req[CC_id];
208
      sched_info->UL_dci_req  = &mac->UL_dci_req[CC_id];
209

210
      sched_info->UL_tti_req  = &mac->UL_tti_req[CC_id];
211 212 213 214 215 216

      sched_info->TX_req      = &mac->TX_req[CC_id];
#ifdef DUMP_FAPI
      dump_dl(sched_info);
#endif

217
      if (ifi->NR_Schedule_response) {
218 219 220 221 222
        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);
223 224
      }

Raymond Knopp's avatar
Raymond Knopp committed
225 226 227
      LOG_D(PHY,"NR_Schedule_response: SFN_SF:%d%d dl_pdus:%d\n",
	    sched_info->frame,
	    sched_info->slot,
228
	    sched_info->DL_req->nPDUs);
229 230 231 232
    }
  }
}

233
NR_IF_Module_t *NR_IF_Module_init(int Mod_id) {
234
  AssertFatal(Mod_id<MAX_MODULES,"Asking for Module %d > %d\n",Mod_id,MAX_IF_MODULES);
Raymond Knopp's avatar
Raymond Knopp committed
235
  LOG_I(PHY,"Installing callbacks for IF_Module - UL_indication\n");
236 237 238 239 240

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

241 242
    LOG_I(MAC,"Allocating shared L1/L2 interface structure for instance %d @ %p\n",Mod_id,if_inst[Mod_id]);

243
    if_inst[Mod_id]->CC_mask=0;
244
    if_inst[Mod_id]->NR_UL_indication = NR_UL_indication;
245
    AssertFatal(pthread_mutex_init(&if_inst[Mod_id]->if_mutex,NULL)==0,
246
                "allocation of if_inst[%d]->if_mutex fails\n",Mod_id);
247
  }
248

249
  return if_inst[Mod_id];
250
}