NR_IF_Module.c 9.89 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/SCHED_NR/fapi_nr_l1.h"
#include "openair2/NR_PHY_INTERFACE/NR_IF_Module.h"
35
#include "LAYER2/NR_MAC_COMMON/nr_mac_extern.h"
36 37
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
#include "common/ran_context.h"
38
#include "executables/softmodem-common.h"
39 40

#define MAX_IF_MODULES 100
Francesco Mani's avatar
Francesco Mani committed
41
//#define UL_HARQ_PRINT
42 43 44 45 46 47 48 49 50 51 52

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;
53
extern uint16_t sl_ahead;
54

55
void handle_nr_rach(NR_UL_IND_t *UL_info) {
56

57
  if (UL_info->rach_ind.number_of_pdus>0) {
kn.raju's avatar
kn.raju committed
58
    LOG_I(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);
Francesco Mani's avatar
Francesco Mani committed
59 60 61 62 63 64
    int npdus = UL_info->rach_ind.number_of_pdus;
    for(int i = 0; i < npdus; i++) {
      UL_info->rach_ind.number_of_pdus--;
      if (UL_info->rach_ind.pdu_list[i].num_preamble>0)
      AssertFatal(UL_info->rach_ind.pdu_list[i].num_preamble==1,
                  "More than 1 preamble not supported\n");
65
    
Francesco Mani's avatar
Francesco Mani committed
66 67 68 69 70 71 72 73 74
      nr_initiate_ra_proc(UL_info->module_id,
                          UL_info->CC_id,
                          UL_info->rach_ind.sfn,
                          UL_info->rach_ind.slot,
                          UL_info->rach_ind.pdu_list[i].preamble_list[0].preamble_index,
                          UL_info->rach_ind.pdu_list[i].freq_index,
                          UL_info->rach_ind.pdu_list[i].symbol_index,
                          UL_info->rach_ind.pdu_list[i].preamble_list[0].timing_advance);
    }
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
75 76 77
  }
}

78

79 80 81 82 83
void handle_nr_uci(NR_UL_IND_t *UL_info)
{
  const module_id_t mod_id = UL_info->module_id;
  const frame_t frame = UL_info->frame;
  const sub_frame_t slot = UL_info->slot;
84 85 86 87 88
  int num_ucis = UL_info->uci_ind.num_ucis;
  nfapi_nr_uci_t *uci_list = UL_info->uci_ind.uci_list;

  for (int i = 0; i < num_ucis; i++) {
    switch (uci_list[i].pdu_type) {
89 90 91
      case NFAPI_NR_UCI_PUSCH_PDU_TYPE:
        LOG_E(MAC, "%s(): unhandled NFAPI_NR_UCI_PUSCH_PDU_TYPE\n", __func__);
        break;
92 93

      case NFAPI_NR_UCI_FORMAT_0_1_PDU_TYPE: {
94 95
        const nfapi_nr_uci_pucch_pdu_format_0_1_t *uci_pdu = &uci_list[i].pucch_pdu_format_0_1;
        handle_nr_uci_pucch_0_1(mod_id, frame, slot, uci_pdu);
96 97 98
        break;
      }
      case NFAPI_NR_UCI_FORMAT_2_3_4_PDU_TYPE: {
99 100
        const nfapi_nr_uci_pucch_pdu_format_2_3_4_t *uci_pdu = &uci_list[i].pucch_pdu_format_2_3_4;
        handle_nr_uci_pucch_2_3_4(mod_id, frame, slot, uci_pdu);
101 102 103 104 105 106
        break;
      }
    }
  }

  UL_info->uci_ind.num_ucis = 0;
107 108 109
  // mark corresponding PUCCH resources as free
  // NOTE: we just assume it is BWP ID 1, to be revised for multiple BWPs
  RC.nrmac[mod_id]->pucch_index_used[1][slot] = 0;
110 111
}

112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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
void handle_nr_ulsch(NR_UL_IND_t *UL_info)
{
  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++) {
        // find crc_indication j corresponding rx_indication i
        const nfapi_nr_rx_data_pdu_t *rx = &UL_info->rx_ind.pdu_list[i];
        const nfapi_nr_crc_t *crc = &UL_info->crc_ind.crc_list[j];
        LOG_D(PHY,
              "UL_info->crc_ind.pdu_list[%d].rnti:%04x "
              "UL_info->rx_ind.pdu_list[%d].rnti:%04x\n",
              j,
              crc->rnti,
              i,
              rx->rnti);

        if (crc->rnti != rx->rnti)
          continue;

        LOG_D(MAC,
              "%4d.%2d Calling rx_sdu (CRC %s/tb_crc_status %d)\n",
              UL_info->frame,
              UL_info->slot,
              crc->tb_crc_status ? "error" : "ok",
              crc->tb_crc_status);

        /* if CRC passes, pass PDU, otherwise pass NULL as error indication */
        nr_rx_sdu(UL_info->module_id,
                  UL_info->CC_id,
                  UL_info->rx_ind.sfn,
                  UL_info->rx_ind.slot,
                  rx->rnti,
                  crc->tb_crc_status ? NULL : rx->pdu,
                  rx->pdu_length,
                  rx->timing_advance,
                  rx->ul_cqi,
                  rx->rssi);
        handle_nr_ul_harq(UL_info->module_id, UL_info->frame, UL_info->slot, crc);
        break;
      } //    for (j=0;j<UL_info->crc_ind.number_crcs;j++)
    } //   for (i=0;i<UL_info->rx_ind.number_of_pdus;i++)

    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
169 170 171
  }
}

172
void NR_UL_indication(NR_UL_IND_t *UL_info) {
173 174 175 176
  AssertFatal(UL_info!=NULL,"UL_INFO is null\n");
#ifdef DUMP_FAPI
  dump_ul(UL_info);
#endif
177 178 179 180 181
  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];
182
  LOG_D(PHY,"SFN/SF:%d%d module_id:%d CC_id:%d UL_info[rach_pdus:%d rx_ind:%d crcs:%d]\n",
183
        UL_info->frame,UL_info->slot,
184
        module_id,CC_id, UL_info->rach_ind.number_of_pdus,
185
        UL_info->rx_ind.number_of_pdus, UL_info->crc_ind.number_crcs);
186

187
  if (nfapi_mode != 1) {
188 189
    if (ifi->CC_mask==0) {
      ifi->current_frame    = UL_info->frame;
190
      ifi->current_slot = UL_info->slot;
191
    } else {
192
      AssertFatal(UL_info->frame != ifi->current_frame,"CC_mask %x is not full and frame has changed\n",ifi->CC_mask);
193
      AssertFatal(UL_info->slot != ifi->current_slot,"CC_mask %x is not full and slot has changed\n",ifi->CC_mask);
194
    }
195

196 197 198
    ifi->CC_mask |= (1<<CC_id);
  }

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
199
  handle_nr_rach(UL_info);
200
  
201
  handle_nr_uci(UL_info);
202
  // clear HI prior to handling ULSCH
203
  mac->UL_dci_req[CC_id].numPdus = 0;
204
  handle_nr_ulsch(UL_info);
205

206
  if (nfapi_mode != 1) {
207
    if (ifi->CC_mask == ((1<<MAX_NUM_CCs)-1)) {
208
      /*
209
      eNB_dlsch_ulsch_scheduler(module_id,
210 211
          (UL_info->frame+((UL_info->slot>(9-sl_ahead))?1:0)) % 1024,
          (UL_info->slot+sl_ahead)%10);
212
      */
213
      nfapi_nr_config_request_scf_t *cfg = &mac->config[CC_id];
214
      int spf = get_spf(cfg);
215
      gNB_dlsch_ulsch_scheduler(module_id,
216 217
				(UL_info->frame+((UL_info->slot>(spf-1-sl_ahead))?1:0)) % 1024,
				(UL_info->slot+sl_ahead)%spf);
218
      
219 220 221
      ifi->CC_mask            = 0;
      sched_info->module_id   = module_id;
      sched_info->CC_id       = CC_id;
222 223
      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;
224
      sched_info->DL_req      = &mac->DL_req[CC_id];
225
      sched_info->UL_dci_req  = &mac->UL_dci_req[CC_id];
226

227
      sched_info->UL_tti_req  = mac->UL_tti_req[CC_id];
228 229 230 231 232 233

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

234
      if (ifi->NR_Schedule_response) {
235 236 237 238 239
        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);
240 241
      }

Raymond Knopp's avatar
Raymond Knopp committed
242 243 244 245
      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_tti_request_body.nPDUs);
246 247 248 249
    }
  }
}

250
NR_IF_Module_t *NR_IF_Module_init(int Mod_id) {
251
  AssertFatal(Mod_id<MAX_MODULES,"Asking for Module %d > %d\n",Mod_id,MAX_IF_MODULES);
Raymond Knopp's avatar
Raymond Knopp committed
252
  LOG_I(PHY,"Installing callbacks for IF_Module - UL_indication\n");
253 254 255 256 257

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

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

260
    if_inst[Mod_id]->CC_mask=0;
261
    if_inst[Mod_id]->NR_UL_indication = NR_UL_indication;
262
    AssertFatal(pthread_mutex_init(&if_inst[Mod_id]->if_mutex,NULL)==0,
263
                "allocation of if_inst[%d]->if_mutex fails\n",Mod_id);
264
  }
265

266
  return if_inst[Mod_id];
267
}