nr-gnb.c 36.8 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
/*
 * 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 lte-enb.c
Guy De Souza's avatar
Guy De Souza committed
23
 * \brief Top-level threads for gNodeB
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
 * \date 2012
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
 * \note
 * \warning
 */

#define _GNU_SOURCE
#include <pthread.h>

#include "time_utils.h"

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

#include "rt_wrapper.h"

#include "assertions.h"


#include "PHY/types.h"

47 48 49 50
#include "PHY/INIT/phy_init.h"

#include "PHY/defs_gNB.h"
#include "SCHED/sched_eNB.h"
Guy De Souza's avatar
Guy De Souza committed
51
#include "SCHED_NR/sched_nr.h"
52
#include "SCHED_NR/fapi_nr_l1.h"
53 54
#include "PHY/LTE_TRANSPORT/transport_proto.h"

55 56 57 58 59 60 61 62 63 64
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#include "../../ARCH/COMMON/common_lib.h"

//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#include "PHY/LTE_TRANSPORT/if4_tools.h"
#include "PHY/LTE_TRANSPORT/if5_tools.h"

65 66 67 68 69 70 71 72
#include "PHY/phy_extern.h"


#include "LAYER2/MAC/mac.h"
#include "LAYER2/MAC/mac_extern.h"
#include "LAYER2/MAC/mac_proto.h"
#include "RRC/LTE/rrc_extern.h"
#include "PHY_INTERFACE/phy_interface.h"
73
#include "common/utils/LOG/log_extern.h"
74 75 76
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
77
#include "common/utils/LOG/vcd_signal_dumper.h"
78 79
#include "UTIL/OPT/opt.h"
#include "enb_config.h"
80

81 82 83 84 85 86 87 88 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 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

#ifndef OPENAIR2
#include "UTIL/OTG/otg_extern.h"
#endif

#if defined(ENABLE_ITTI)
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
#ifdef PDCP_USE_NETLINK
#   include "SIMULATION/ETH_TRANSPORT/proto.h"
#endif
# endif
#endif

#include "T.h"

//#define DEBUG_THREADS 1

//#define USRP_DEBUG 1
struct timing_info_t {
  //unsigned int frame, hw_slot, last_slot, next_slot;
  RTIME time_min, time_max, time_avg, time_last, time_now;
  //unsigned int mbox0, mbox1, mbox2, mbox_target;
  unsigned int n_samples;
} timing_info;

// Fix per CC openair rf/if device update
// extern openair0_device openair0;


#if defined(ENABLE_ITTI)
extern volatile int             start_gNB;
extern volatile int             start_UE;
#endif
extern volatile int                    oai_exit;

extern openair0_config_t openair0_cfg[MAX_CARDS];

extern int transmission_mode;

uint16_t sf_ahead=4;

//pthread_t                       main_gNB_thread;

time_stats_t softmodem_stats_mt; // main thread
time_stats_t softmodem_stats_hw; //  hw acquisition
time_stats_t softmodem_stats_rxtx_sf; // total tx time
time_stats_t nfapi_meas; // total tx time
time_stats_t softmodem_stats_rx_sf; // total rx time

/* mutex, cond and variable to serialize phy proc TX calls
 * (this mechanism may be relaxed in the future for better
 * performances)
 */
static struct {
  pthread_mutex_t  mutex_phy_proc_tx;
  pthread_cond_t   cond_phy_proc_tx;
  volatile uint8_t phy_proc_CC_id;
} sync_phy_proc;

extern double cpuf;

void init_gNB(int,int);
void stop_gNB(int nb_inst);

Wang Tsu-Han's avatar
Wang Tsu-Han committed
146 147 148 149 150
int wakeup_txfh(gNB_L1_rxtx_proc_t *proc,PHY_VARS_gNB *gNB);
int wakeup_tx(PHY_VARS_gNB *gNB);
extern PARALLEL_CONF_t get_thread_parallel_conf(void);
extern WORKER_CONF_t   get_thread_worker_conf(void);

151 152 153 154 155 156 157 158 159 160 161

void wakeup_prach_gNB(PHY_VARS_gNB *gNB,RU_t *ru,int frame,int subframe);

extern uint8_t nfapi_mode;
extern void oai_subframe_ind(uint16_t sfn, uint16_t sf);
extern void add_subframe(uint16_t *frameP, uint16_t *subframeP, int offset);

//#define TICK_TO_US(ts) (ts.diff)
#define TICK_TO_US(ts) (ts.trials==0?0:ts.diff/ts.trials)


Wang Tsu-Han's avatar
Wang Tsu-Han committed
162
static inline int rxtx(PHY_VARS_gNB *gNB,gNB_L1_rxtx_proc_t *proc, char *thread_name) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  start_meas(&softmodem_stats_rxtx_sf);

  // *******************************************************************

  if (nfapi_mode == 1) {

    // I am a PNF and I need to let nFAPI know that we have a (sub)frame tick
    uint16_t frame = proc->frame_rx;
    uint16_t subframe = proc->subframe_rx;

    //add_subframe(&frame, &subframe, 4);

    //oai_subframe_ind(proc->frame_tx, proc->subframe_tx);
    //LOG_D(PHY, "oai_subframe_ind(frame:%u, subframe:%d) - NOT CALLED ********\n", frame, subframe);
    start_meas(&nfapi_meas);
    oai_subframe_ind(frame, subframe);
    stop_meas(&nfapi_meas);

    if (gNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus||
        gNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs ||
        gNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs ||
        gNB->UL_INFO.rach_ind.rach_indication_body.number_of_preambles ||
        gNB->UL_INFO.cqi_ind.number_of_cqis
       ) {
Guy De Souza's avatar
Guy De Souza committed
187
      LOG_D(PHY, "UL_info[rx_ind:%05d:%d harqs:%05d:%d crcs:%05d:%d preambles:%05d:%d cqis:%d] RX:%04d%d TX:%04d%d \n", 
188 189 190 191 192 193
          NFAPI_SFNSF2DEC(gNB->UL_INFO.rx_ind.sfn_sf),   gNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus, 
          NFAPI_SFNSF2DEC(gNB->UL_INFO.harq_ind.sfn_sf), gNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs, 
          NFAPI_SFNSF2DEC(gNB->UL_INFO.crc_ind.sfn_sf),  gNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs, 
          NFAPI_SFNSF2DEC(gNB->UL_INFO.rach_ind.sfn_sf), gNB->UL_INFO.rach_ind.rach_indication_body.number_of_preambles,
          gNB->UL_INFO.cqi_ind.number_of_cqis, 
          proc->frame_rx, proc->subframe_rx, 
Guy De Souza's avatar
Guy De Souza committed
194
      proc->frame_tx, proc->subframe_tx);
195 196 197 198
    }
  }

  /// NR disabling
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
199

200 201 202
  // ****************************************
  // Common RX procedures subframe n

203
  T(T_GNB_PHY_DL_TICK, T_INT(gNB->Mod_id), T_INT(proc->frame_tx), T_INT(proc->subframe_tx));
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
204
/*
205 206 207 208 209 210 211 212 213
  // if this is IF5 or 3GPP_gNB
  if (gNB && gNB->RU_list && gNB->RU_list[0] && gNB->RU_list[0]->function < NGFI_RAU_IF4p5) {
    wakeup_prach_gNB(gNB,NULL,proc->frame_rx,proc->subframe_rx);
  }

  // UE-specific RX processing for subframe n
  if (nfapi_mode == 0 || nfapi_mode == 1) {
    phy_procedures_gNB_uespec_RX(gNB, proc, no_relay );
  }
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
214
*/
215 216 217 218 219 220 221
  pthread_mutex_lock(&gNB->UL_INFO_mutex);

  gNB->UL_INFO.frame     = proc->frame_rx;
  gNB->UL_INFO.subframe  = proc->subframe_rx;
  gNB->UL_INFO.module_id = gNB->Mod_id;
  gNB->UL_INFO.CC_id     = gNB->CC_id;

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
222
  gNB->if_inst->NR_UL_indication(&gNB->UL_INFO);
223 224

  pthread_mutex_unlock(&gNB->UL_INFO_mutex);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
225

226 227 228 229 230 231 232 233 234
/// end
  // *****************************************
  // TX processing for subframe n+sf_ahead
  // run PHY TX procedures the one after the other for all CCs to avoid race conditions
  // (may be relaxed in the future for performance reasons)
  // *****************************************
  //if (wait_CCs(proc)<0) return(-1);
  
  if (oai_exit) return(-1);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
235 236 237
  if(get_thread_parallel_conf() == PARALLEL_SINGLE_THREAD){
    phy_procedures_gNB_TX(gNB, proc, 1);
  }
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 281 282 283 284 285 286 287

  stop_meas( &softmodem_stats_rxtx_sf );

  LOG_D(PHY,"%s() Exit proc[rx:%d%d tx:%d%d]\n", __FUNCTION__, proc->frame_rx, proc->subframe_rx, proc->frame_tx, proc->subframe_tx);

#if 0
  LOG_D(PHY, "rxtx:%lld nfapi:%lld phy:%lld tx:%lld rx:%lld prach:%lld ofdm:%lld ",
      softmodem_stats_rxtx_sf.diff_now, nfapi_meas.diff_now,
      TICK_TO_US(gNB->phy_proc),
      TICK_TO_US(gNB->phy_proc_tx),
      TICK_TO_US(gNB->phy_proc_rx),
      TICK_TO_US(gNB->rx_prach),
      TICK_TO_US(gNB->ofdm_mod_stats),
      softmodem_stats_rxtx_sf.diff_now, nfapi_meas.diff_now);
  LOG_D(PHY,
    "dlsch[enc:%lld mod:%lld scr:%lld rm:%lld t:%lld i:%lld] rx_dft:%lld ",
      TICK_TO_US(gNB->dlsch_encoding_stats),
      TICK_TO_US(gNB->dlsch_modulation_stats),
      TICK_TO_US(gNB->dlsch_scrambling_stats),
      TICK_TO_US(gNB->dlsch_rate_matching_stats),
      TICK_TO_US(gNB->dlsch_turbo_encoding_stats),
      TICK_TO_US(gNB->dlsch_interleaving_stats),
      TICK_TO_US(gNB->rx_dft_stats));

  LOG_D(PHY," ulsch[ch:%lld freq:%lld dec:%lld demod:%lld ru:%lld ",
      TICK_TO_US(gNB->ulsch_channel_estimation_stats),
      TICK_TO_US(gNB->ulsch_freq_offset_estimation_stats),
      TICK_TO_US(gNB->ulsch_decoding_stats),
      TICK_TO_US(gNB->ulsch_demodulation_stats),
      TICK_TO_US(gNB->ulsch_rate_unmatching_stats));

  LOG_D(PHY, "td:%lld dei:%lld dem:%lld llr:%lld tci:%lld ",
      TICK_TO_US(gNB->ulsch_turbo_decoding_stats),
      TICK_TO_US(gNB->ulsch_deinterleaving_stats),
      TICK_TO_US(gNB->ulsch_demultiplexing_stats),
      TICK_TO_US(gNB->ulsch_llr_stats),
      TICK_TO_US(gNB->ulsch_tc_init_stats));
  LOG_D(PHY, "tca:%lld tcb:%lld tcg:%lld tce:%lld l1:%lld l2:%lld]\n\n", 
      TICK_TO_US(gNB->ulsch_tc_alpha_stats),
      TICK_TO_US(gNB->ulsch_tc_beta_stats),
      TICK_TO_US(gNB->ulsch_tc_gamma_stats),
      TICK_TO_US(gNB->ulsch_tc_ext_stats),
      TICK_TO_US(gNB->ulsch_tc_intl1_stats),
      TICK_TO_US(gNB->ulsch_tc_intl2_stats)
      );
#endif
  
  return(0);
}

Wang Tsu-Han's avatar
Wang Tsu-Han committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static void* gNB_L1_thread_tx(void* param) {

  gNB_L1_proc_t *gNB_proc  = (gNB_L1_proc_t*)param;
  gNB_L1_rxtx_proc_t *proc = &gNB_proc->L1_proc_tx;
  PHY_VARS_gNB *gNB = RC.gNB[0][proc->CC_id];
  
  char thread_name[100];
  sprintf(thread_name,"TXnp4_%d\n",&gNB->proc.L1_proc == proc ? 0 : 1);
  thread_top_init(thread_name,1,470000,500000,500000);
  
  //wait_sync("tx_thread");
  
  while (!oai_exit) {
    

    if (wait_on_condition(&proc->mutex,&proc->cond,&proc->instance_cnt,thread_name)<0) break;
    if (oai_exit) break;    
    // *****************************************
    // TX processing for subframe n+4
    // run PHY TX procedures the one after the other for all CCs to avoid race conditions
    // (may be relaxed in the future for performance reasons)
    // *****************************************
    
    phy_procedures_gNB_TX(gNB, proc, 1);

    pthread_mutex_lock( &proc->mutex );
    proc->instance_cnt = -1;
    // the thread can now be woken up
    if (pthread_cond_signal(&proc->cond) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB TXnp4 thread\n");
      exit_fun( "ERROR pthread_cond_signal" );
    }
    pthread_mutex_unlock( &proc->mutex );
    wakeup_txfh(proc,gNB);
  }

  return 0;
}
326 327 328

/*!
 * \brief The RX UE-specific and TX thread of gNB.
Wang Tsu-Han's avatar
Wang Tsu-Han committed
329
 * \param param is a \ref gNB_L1_proc_t structure which contains the info what to process.
330 331 332
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */

Wang Tsu-Han's avatar
Wang Tsu-Han committed
333
static void* gNB_L1_thread( void* param ) {
334 335

  static int gNB_thread_rxtx_status;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
336 337
  gNB_L1_proc_t *gNB_proc  = (gNB_L1_proc_t*)param;
  gNB_L1_rxtx_proc_t *proc = &gNB_proc->L1_proc;
338 339 340 341 342 343 344 345 346
  PHY_VARS_gNB *gNB = RC.gNB[0][proc->CC_id];

  char thread_name[100];


  // set default return value
  gNB_thread_rxtx_status = 0;


Wang Tsu-Han's avatar
Wang Tsu-Han committed
347
  sprintf(thread_name,"RXn_TXnp4_%d",&gNB->proc.L1_proc == proc ? 0 : 1);
348 349 350 351
  thread_top_init(thread_name,1,850000L,1000000L,2000000L);

  while (!oai_exit) {

Wang Tsu-Han's avatar
Wang Tsu-Han committed
352
    if (wait_on_condition(&proc->mutex,&proc->cond,&proc->instance_cnt,thread_name)<0) break;
353 354 355 356 357 358 359 360 361


    if (oai_exit) break;

    if (gNB->CC_id==0)
    {
      if (rxtx(gNB,proc,thread_name) < 0) break;

    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
362 363 364
    if(get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT){
      phy_procedures_gNB_TX(gNB, proc, 1);
    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
365
    if (release_thread(&proc->mutex,&proc->instance_cnt,thread_name)<0) break;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
366 367 368 369 370 371
    if(get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT){
      wakeup_tx(gNB);
    }
    else if(get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT){
      wakeup_txfh(proc,gNB);
    }
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 402 403 404 405

  } // while !oai_exit


  LOG_D(PHY, " *** Exiting gNB thread RXn_TXnp4\n");

  gNB_thread_rxtx_status = 0;
  return &gNB_thread_rxtx_status;
}


#if 0 //defined(ENABLE_ITTI) && defined(ENABLE_USE_MME)
// Wait for gNB application initialization to be complete (gNB registration to MME)
static void wait_system_ready (char *message, volatile int *start_flag) {
  
  static char *indicator[] = {".    ", "..   ", "...  ", ".... ", ".....",
			      " ....", "  ...", "   ..", "    .", "     "};
  int i = 0;
  
  while ((!oai_exit) && (*start_flag == 0)) {
    LOG_N(EMU, message, indicator[i]);
    fflush(stdout);
    i = (i + 1) % (sizeof(indicator) / sizeof(indicator[0]));
    usleep(200000);
  }
  
  LOG_D(EMU,"\n");
}
#endif





Guy De Souza's avatar
Guy De Souza committed
406
void gNB_top(PHY_VARS_gNB *gNB, int frame_rx, int subframe_rx, char *string, struct RU_t_s *ru)
407
{
Wang Tsu-Han's avatar
Wang Tsu-Han committed
408 409
  gNB_L1_proc_t *proc           = &gNB->proc;
  gNB_L1_rxtx_proc_t *L1_proc = &proc->L1_proc;
Guy De Souza's avatar
Guy De Souza committed
410 411
  NR_DL_FRAME_PARMS *fp = ru->nr_frame_parms;
  RU_proc_t *ru_proc=&ru->proc;
412 413 414 415 416 417 418

  proc->frame_rx    = frame_rx;
  proc->subframe_rx = subframe_rx;

  if (!oai_exit) {
    T(T_ENB_MASTER_TICK, T_INT(0), T_INT(proc->frame_rx), T_INT(proc->subframe_rx));

Wang Tsu-Han's avatar
Wang Tsu-Han committed
419 420 421 422 423
    L1_proc->timestamp_tx = ru_proc->timestamp_rx + (sf_ahead*fp->samples_per_subframe);
    L1_proc->frame_rx     = ru_proc->frame_rx;
    L1_proc->subframe_rx  = ru_proc->subframe_rx;
    L1_proc->frame_tx     = (L1_proc->subframe_rx > (9-sf_ahead)) ? (L1_proc->frame_rx+1)&1023 : L1_proc->frame_rx;
    L1_proc->subframe_tx  = (L1_proc->subframe_rx + sf_ahead)%10;
424

Wang Tsu-Han's avatar
Wang Tsu-Han committed
425 426 427 428
    if (rxtx(gNB,L1_proc,string) < 0) LOG_E(PHY,"gNB %d CC_id %d failed during execution\n",gNB->Mod_id,gNB->CC_id);
    ru_proc->timestamp_tx = L1_proc->timestamp_tx;
    ru_proc->subframe_tx  = L1_proc->subframe_tx;
    ru_proc->frame_tx     = L1_proc->frame_tx;
429 430 431
  }
}

Wang Tsu-Han's avatar
Wang Tsu-Han committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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
int wakeup_txfh(gNB_L1_rxtx_proc_t *proc,PHY_VARS_gNB *gNB) {

  RU_t *ru;
  RU_proc_t *ru_proc;


  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
//printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~inside wakeup_txfh %d.%d IC_RU = %d\n", proc->frame_tx, proc->subframe_tx, proc->instance_cnt_RUs);

  if(wait_on_condition(&proc->mutex_RUs,&proc->cond_RUs,&proc->instance_cnt_RUs,"wakeup_txfh")<0) {
    LOG_E(PHY,"Frame %d, subframe %d: TX FH not ready\n", proc->frame_tx, proc->subframe_tx);
    return(-1);
  }
  pthread_mutex_lock(&gNB->proc.mutex_RU_tx);
  gNB->proc.RU_mask_tx = 0;
  pthread_mutex_unlock(&gNB->proc.mutex_RU_tx);
  if (release_thread(&proc->mutex_RUs,&proc->instance_cnt_RUs,"wakeup_txfh")<0) return(-1);

  for(int i=0; i<gNB->num_RU; i++)
  {
    ru      = gNB->RU_list[i];
    ru_proc = &ru->proc;
    if (ru_proc->instance_cnt_gNBs == 0) {
      LOG_E(PHY,"Frame %d, subframe %d: TX FH thread busy, dropping Frame %d, subframe %d\n", ru_proc->frame_tx, ru_proc->subframe_tx, proc->frame_rx, proc->subframe_rx);
      return(-1);
    }
    if (pthread_mutex_timedlock(&ru_proc->mutex_gNBs,&wait) != 0) {
      LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB TX1 thread %d (IC %d)\n", ru_proc->subframe_rx&1,ru_proc->instance_cnt_gNBs );
      exit_fun( "error locking mutex_gNB" );
      return(-1);
    }

    ru_proc->instance_cnt_gNBs = 0;
    ru_proc->timestamp_tx = proc->timestamp_tx;
    ru_proc->subframe_tx  = proc->subframe_tx;
    ru_proc->frame_tx     = proc->frame_tx;

  
    // the thread can now be woken up
    if (pthread_cond_signal(&ru_proc->cond_gNBs) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB TXnp4 thread\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return(-1);
    }
  
    pthread_mutex_unlock( &ru_proc->mutex_gNBs );
  }

  return(0);
}

int wakeup_tx(PHY_VARS_gNB *gNB) {

  gNB_L1_proc_t *proc=&gNB->proc;

  gNB_L1_rxtx_proc_t *L1_proc_tx = &proc->L1_proc_tx;
  gNB_L1_rxtx_proc_t *L1_proc    = &proc->L1_proc;

  
  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
  
  
  if (pthread_mutex_timedlock(&L1_proc_tx->mutex,&wait) != 0) {
    LOG_E(PHY, "[SCHED][eNB] ERROR locking mutex for eNB L1_thread_tx\n");
    exit_fun("ERROR pthread_lock");
    return(-1);
  }
  while(L1_proc_tx->instance_cnt == 0){
    pthread_cond_wait(&L1_proc_tx->cond,&L1_proc_tx->mutex);
  }

  L1_proc_tx->instance_cnt = 0;

  
  L1_proc_tx->subframe_rx   = L1_proc->subframe_rx;
  L1_proc_tx->frame_rx      = L1_proc->frame_rx;
  L1_proc_tx->subframe_tx   = L1_proc->subframe_tx;
  L1_proc_tx->frame_tx      = L1_proc->frame_tx;
  L1_proc_tx->timestamp_tx  = L1_proc->timestamp_tx;
  
  // the thread can now be woken up
  if (pthread_cond_signal(&L1_proc_tx->cond) != 0) {
    LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB TXnp4 thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }
  
  pthread_mutex_unlock( &L1_proc_tx->mutex);

  return(0);
}
527 528 529

int wakeup_rxtx(PHY_VARS_gNB *gNB,RU_t *ru) {

Wang Tsu-Han's avatar
Wang Tsu-Han committed
530
  gNB_L1_proc_t *proc=&gNB->proc;
531

Wang Tsu-Han's avatar
Wang Tsu-Han committed
532
  gNB_L1_rxtx_proc_t *L1_proc=&proc->L1_proc;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

  NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;

  int i;
  struct timespec wait;
  
  pthread_mutex_lock(&proc->mutex_RU);
  for (i=0;i<gNB->num_RU;i++) {
    if (ru == gNB->RU_list[i]) {
      if ((proc->RU_mask&(1<<i)) > 0)
	LOG_E(PHY,"gNB %d frame %d, subframe %d : previous information from RU %d (num_RU %d,mask %x) has not been served yet!\n",
	      gNB->Mod_id,proc->frame_rx,proc->subframe_rx,ru->idx,gNB->num_RU,proc->RU_mask);
      proc->RU_mask |= (1<<i);
    }
  }
  if (proc->RU_mask != (1<<gNB->num_RU)-1) {  // not all RUs have provided their information so return
    LOG_E(PHY,"Not all RUs have provided their info\n");
    pthread_mutex_unlock(&proc->mutex_RU);
    return(0);
  }
  else { // all RUs have provided their information so continue on and wakeup gNB processing
    proc->RU_mask = 0;
    pthread_mutex_unlock(&proc->mutex_RU);
  }


  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

  /* accept some delay in processing - up to 5ms */
Wang Tsu-Han's avatar
Wang Tsu-Han committed
563 564
  for (i = 0; i < 10 && L1_proc->instance_cnt == 0; i++) {
    LOG_W( PHY,"[gNB] Frame %d Subframe %d, gNB RXn-TXnp4 thread busy!! (cnt %i)\n", L1_proc->frame_tx, L1_proc->subframe_tx, L1_proc->instance_cnt);
565 566
    usleep(500);
  }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
567
  if (L1_proc->instance_cnt == 0) {
568 569 570 571 572 573
    exit_fun( "TX thread busy" );
    return(-1);
  }

  // wake up TX for subframe n+sf_ahead
  // lock the TX mutex and make sure the thread is ready
Wang Tsu-Han's avatar
Wang Tsu-Han committed
574 575 576
  if (pthread_mutex_timedlock(&L1_proc->mutex,&wait) != 0) {
    LOG_E( PHY, "[gNB] ERROR pthread_mutex_lock for gNB RXTX thread %d (IC %d)\n", L1_proc->subframe_rx&1,L1_proc->instance_cnt );
    exit_fun( "error locking mutex" );
577 578 579
    return(-1);
  }
  
Wang Tsu-Han's avatar
Wang Tsu-Han committed
580
  ++L1_proc->instance_cnt;
581 582 583 584 585 586 587
  
  // We have just received and processed the common part of a subframe, say n. 
  // TS_rx is the last received timestamp (start of 1st slot), TS_tx is the desired 
  // transmitted timestamp of the next TX slot (first).
  // The last (TS_rx mod samples_per_frame) was n*samples_per_tti, 
  // we want to generate subframe (n+sf_ahead), so TS_tx = TX_rx+sf_ahead*samples_per_tti,
  // and proc->subframe_tx = proc->subframe_rx+sf_ahead
Wang Tsu-Han's avatar
Wang Tsu-Han committed
588 589 590 591 592
  L1_proc->timestamp_tx = proc->timestamp_rx + (sf_ahead*fp->samples_per_subframe);
  L1_proc->frame_rx     = proc->frame_rx;
  L1_proc->subframe_rx  = proc->subframe_rx;
  L1_proc->frame_tx     = (L1_proc->subframe_rx > (9-sf_ahead)) ? (L1_proc->frame_rx+1)&1023 : L1_proc->frame_rx;
  L1_proc->subframe_tx  = (L1_proc->subframe_rx + sf_ahead)%10;
593 594

  // the thread can now be woken up
Wang Tsu-Han's avatar
Wang Tsu-Han committed
595
  if (pthread_cond_signal(&L1_proc->cond) != 0) {
596 597 598 599 600
    LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB RXn-TXnp4 thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }
  
Wang Tsu-Han's avatar
Wang Tsu-Han committed
601
  pthread_mutex_unlock( &L1_proc->mutex );
602 603 604 605 606 607

  return(0);
}
/*
void wakeup_prach_gNB(PHY_VARS_gNB *gNB,RU_t *ru,int frame,int subframe) {

Wang Tsu-Han's avatar
Wang Tsu-Han committed
608
  gNB_L1_proc_t *proc = &gNB->proc;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  LTE_DL_FRAME_PARMS *fp=&gNB->frame_parms;
  int i;

  if (ru!=NULL) {
    pthread_mutex_lock(&proc->mutex_RU_PRACH);
    for (i=0;i<gNB->num_RU;i++) {
      if (ru == gNB->RU_list[i]) {
	LOG_D(PHY,"frame %d, subframe %d: RU %d for gNB %d signals PRACH (mask %x, num_RU %d)\n",frame,subframe,i,gNB->Mod_id,proc->RU_mask_prach,gNB->num_RU);
	if ((proc->RU_mask_prach&(1<<i)) > 0)
	  LOG_E(PHY,"gNB %d frame %d, subframe %d : previous information (PRACH) from RU %d (num_RU %d, mask %x) has not been served yet!\n",
		gNB->Mod_id,frame,subframe,ru->idx,gNB->num_RU,proc->RU_mask_prach);
	proc->RU_mask_prach |= (1<<i);
      }
    }
    if (proc->RU_mask_prach != (1<<gNB->num_RU)-1) {  // not all RUs have provided their information so return
      pthread_mutex_unlock(&proc->mutex_RU_PRACH);
      return;
    }
    else { // all RUs have provided their information so continue on and wakeup gNB processing
      proc->RU_mask_prach = 0;
      pthread_mutex_unlock(&proc->mutex_RU_PRACH);
    }
  }
    
  // check if we have to detect PRACH first
  if (is_prach_subframe(fp,frame,subframe)>0) { 
    LOG_D(PHY,"Triggering prach processing, frame %d, subframe %d\n",frame,subframe);
    if (proc->instance_cnt_prach == 0) {
      LOG_W(PHY,"[gNB] Frame %d Subframe %d, dropping PRACH\n", frame,subframe);
      return;
    }
    
    // wake up thread for PRACH RX
    if (pthread_mutex_lock(&proc->mutex_prach) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_mutex_lock for gNB PRACH thread %d (IC %d)\n", proc->thread_index, proc->instance_cnt_prach);
      exit_fun( "error locking mutex_prach" );
      return;
    }
    
    ++proc->instance_cnt_prach;
    // set timing for prach thread
    proc->frame_prach = frame;
    proc->subframe_prach = subframe;
    
    // the thread can now be woken up
    if (pthread_cond_signal(&proc->cond_prach) != 0) {
      LOG_E( PHY, "[gNB] ERROR pthread_cond_signal for gNB PRACH thread %d\n", proc->thread_index);
      exit_fun( "ERROR pthread_cond_signal" );
      return;
    }
    
    pthread_mutex_unlock( &proc->mutex_prach );
  }

}*/

/*!
 * \brief The prach receive thread of gNB.
Wang Tsu-Han's avatar
Wang Tsu-Han committed
667
 * \param param is a \ref gNB_L1_proc_t structure which contains the info what to process.
668 669 670 671 672 673 674 675
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
 /*
static void* gNB_thread_prach( void* param ) {
  static int gNB_thread_prach_status;


  PHY_VARS_gNB *gNB= (PHY_VARS_gNB *)param;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
676
  gNB_L1_proc_t *proc = &gNB->proc;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

  // set default return value
  gNB_thread_prach_status = 0;

  thread_top_init("gNB_thread_prach",1,500000L,1000000L,20000000L);


  while (!oai_exit) {
    
    if (oai_exit) break;

    
    if (wait_on_condition(&proc->mutex_prach,&proc->cond_prach,&proc->instance_cnt_prach,"gNB_prach_thread") < 0) break;

    LOG_D(PHY,"Running gNB prach procedures\n");
    prach_procedures(gNB
693
#if (RRC_VERSION >= MAKE_VERSION(14, 0, 0))
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
		     ,0
#endif
		     );
    
    if (release_thread(&proc->mutex_prach,&proc->instance_cnt_prach,"gNB_prach_thread") < 0) break;
  }

  LOG_I(PHY, "Exiting gNB thread PRACH\n");

  gNB_thread_prach_status = 0;
  return &gNB_thread_prach_status;
}
*/

extern void init_td_thread(PHY_VARS_gNB *, pthread_attr_t *);
extern void init_te_thread(PHY_VARS_gNB *, pthread_attr_t *);

void init_gNB_proc(int inst) {
  
  int i=0;
  int CC_id;
  PHY_VARS_gNB *gNB;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
716 717
  gNB_L1_proc_t *proc;
  gNB_L1_rxtx_proc_t *L1_proc,*L1_proc_tx;
Guy De Souza's avatar
Guy De Souza committed
718 719
  pthread_attr_t *attr0=NULL,*attr1=NULL;
  //*attr_prach=NULL;
720

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
721
  LOG_I(PHY,"%s(inst:%d) RC.nb_nr_CC[inst]:%d \n",__FUNCTION__,inst,RC.nb_nr_CC[inst]);
722

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
723
  for (CC_id=0; CC_id<RC.nb_nr_CC[inst]; CC_id++) {
724 725 726 727 728 729
    gNB = RC.gNB[inst][CC_id];
#ifndef OCP_FRAMEWORK
    LOG_I(PHY,"Initializing gNB processes instance:%d CC_id %d \n",inst,CC_id);
#endif
    proc = &gNB->proc;

Wang Tsu-Han's avatar
Wang Tsu-Han committed
730 731 732 733 734 735
    L1_proc                        = &proc->L1_proc;
    L1_proc_tx                     = &proc->L1_proc_tx;
    L1_proc->instance_cnt          = -1;
    L1_proc_tx->instance_cnt       = -1;
    L1_proc->instance_cnt_RUs      = 0;
    L1_proc_tx->instance_cnt_RUs   = 0;
736 737 738 739
    proc->instance_cnt_prach       = -1;
    proc->instance_cnt_asynch_rxtx = -1;
    proc->CC_id                    = CC_id;    

Wang Tsu-Han's avatar
Wang Tsu-Han committed
740 741 742 743 744
    proc->first_rx                 =1;
    proc->first_tx                 =1;
    proc->RU_mask                  =0;
    proc->RU_mask_tx               = (1<<gNB->num_RU)-1;
    proc->RU_mask_prach            =0;
745 746

    pthread_mutex_init( &gNB->UL_INFO_mutex, NULL);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
747 748 749 750
    pthread_mutex_init( &L1_proc->mutex, NULL);
    pthread_mutex_init( &L1_proc_tx->mutex, NULL);
    pthread_cond_init( &L1_proc->cond, NULL);
    pthread_cond_init( &L1_proc_tx->cond, NULL);
751 752 753 754

    pthread_mutex_init( &proc->mutex_prach, NULL);
    pthread_mutex_init( &proc->mutex_asynch_rxtx, NULL);
    pthread_mutex_init( &proc->mutex_RU,NULL);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
755
    pthread_mutex_init( &proc->mutex_RU_tx,NULL);
756 757 758 759 760 761 762 763 764
    pthread_mutex_init( &proc->mutex_RU_PRACH,NULL);

    pthread_cond_init( &proc->cond_prach, NULL);
    pthread_cond_init( &proc->cond_asynch_rxtx, NULL);

    pthread_attr_init( &proc->attr_prach);
    pthread_attr_init( &proc->attr_asynch_rxtx);
    //    pthread_attr_init( &proc->attr_td);
    //    pthread_attr_init( &proc->attr_te);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
765 766
    pthread_attr_init( &L1_proc->attr);
    pthread_attr_init( &L1_proc_tx->attr);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
767 768
    attr0       = &L1_proc->attr;
    attr1       = &L1_proc_tx->attr;
769 770 771

    LOG_I(PHY,"gNB->single_thread_flag:%d\n", gNB->single_thread_flag);

Wang Tsu-Han's avatar
Wang Tsu-Han committed
772 773 774 775
    if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
      pthread_create( &L1_proc->pthread, attr0, gNB_L1_thread, proc );
      pthread_create( &L1_proc_tx->pthread, attr1, gNB_L1_thread_tx, proc);
    }
776 777 778 779
    //pthread_create( &proc->pthread_prach, attr_prach, gNB_thread_prach, gNB );

    char name[16];
    if (gNB->single_thread_flag==0) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
780 781 782 783
      snprintf( name, sizeof(name), "L1 %d", i );
      pthread_setname_np( L1_proc->pthread, name );
      snprintf( name, sizeof(name), "L1TX %d", i );
      pthread_setname_np( L1_proc_tx->pthread, name );
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    }

    AssertFatal(proc->instance_cnt_prach == -1,"instance_cnt_prach = %d\n",proc->instance_cnt_prach);

    
  }

  /* setup PHY proc TX sync mechanism */
  pthread_mutex_init(&sync_phy_proc.mutex_phy_proc_tx, NULL);
  pthread_cond_init(&sync_phy_proc.cond_phy_proc_tx, NULL);
  sync_phy_proc.phy_proc_CC_id = 0;
}



/*!
 * \brief Terminate gNB TX and RX threads.
 */
void kill_gNB_proc(int inst) {

  int *status;
  PHY_VARS_gNB *gNB;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
806 807
  gNB_L1_proc_t *proc;
  gNB_L1_rxtx_proc_t *L1_proc, *L1_proc_tx;
808 809 810 811
  for (int CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    gNB=RC.gNB[inst][CC_id];
    
    proc = &gNB->proc;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
812 813
    L1_proc     = &proc->L1_proc;
    L1_proc_tx  = &proc->L1_proc_tx;
814 815 816

    LOG_I(PHY, "Killing TX CC_id %d inst %d\n", CC_id, inst );

Wang Tsu-Han's avatar
Wang Tsu-Han committed
817 818 819 820 821 822 823 824 825 826
    if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
      pthread_mutex_lock(&L1_proc->mutex);
      L1_proc->instance_cnt = 0;
      pthread_cond_signal(&L1_proc->cond);
      pthread_mutex_unlock(&L1_proc->mutex);

      pthread_mutex_lock(&L1_proc_tx->mutex);
      L1_proc_tx->instance_cnt = 0;
      pthread_cond_signal(&L1_proc_tx->cond);
      pthread_mutex_unlock(&L1_proc_tx->mutex);
827 828 829 830 831 832 833
    }
    proc->instance_cnt_prach = 0;
    pthread_cond_signal( &proc->cond_prach );

    pthread_cond_signal( &proc->cond_asynch_rxtx );
    pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);

Guy De Souza's avatar
Guy De Souza committed
834 835
//    LOG_D(PHY, "joining pthread_prach\n");
//    pthread_join( proc->pthread_prach, (void**)&status );    
836 837 838 839 840 841 842

    LOG_I(PHY, "Destroying prach mutex/cond\n");
    pthread_mutex_destroy( &proc->mutex_prach );
    pthread_cond_destroy( &proc->cond_prach );
       
    LOG_I(PHY, "Destroying UL_INFO mutex\n");
    pthread_mutex_destroy(&gNB->UL_INFO_mutex);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
843
    if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
844 845 846 847
      LOG_I(PHY, "Joining L1_proc mutex/cond\n");
      pthread_join( L1_proc->pthread, (void**)&status );
      LOG_I(PHY, "Joining L1_proc_tx mutex/cond\n");
      pthread_join( L1_proc_tx->pthread, (void**)&status );
848
    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
849 850 851 852 853 854 855 856 857 858
    LOG_I(PHY, "Destroying L1_proc mutex/cond\n");
    pthread_mutex_destroy( &L1_proc->mutex );
    pthread_cond_destroy( &L1_proc->cond );
    LOG_I(PHY, "Destroying L1_proc_tx mutex/cond\n");
    pthread_mutex_destroy( &L1_proc_tx->mutex );
    pthread_cond_destroy( &L1_proc_tx->cond );


    pthread_mutex_destroy( &proc->mutex_RU );
    pthread_mutex_destroy( &proc->mutex_RU_tx );
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
  }
}




void reset_opp_meas(void) {

  int sfn;
  reset_meas(&softmodem_stats_mt);
  reset_meas(&softmodem_stats_hw);
  
  for (sfn=0; sfn < 10; sfn++) {
    reset_meas(&softmodem_stats_rxtx_sf);
    reset_meas(&softmodem_stats_rx_sf);
  }
}


void print_opp_meas(void) {

  int sfn=0;
  print_meas(&softmodem_stats_mt, "Main gNB Thread", NULL, NULL);
  print_meas(&softmodem_stats_hw, "HW Acquisation", NULL, NULL);
  
  for (sfn=0; sfn < 10; sfn++) {
    print_meas(&softmodem_stats_rxtx_sf,"[gNB][total_phy_proc_rxtx]",NULL, NULL);
    print_meas(&softmodem_stats_rx_sf,"[gNB][total_phy_proc_rx]",NULL,NULL);
  }
}
/*
void free_transport(PHY_VARS_gNB *gNB)
{
  int i;
  int j;

  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    LOG_I(PHY, "Freeing Transport Channel Buffers for DLSCH, UE %d\n",i);
    for (j=0; j<2; j++) free_gNB_dlsch(gNB->dlsch[i][j]);

    LOG_I(PHY, "Freeing Transport Channel Buffer for ULSCH, UE %d\n",i);
    free_gNB_ulsch(gNB->ulsch[1+i]);
  }
  free_gNB_ulsch(gNB->ulsch[0]);
}*/

/*
void init_transport(PHY_VARS_gNB *gNB) {

  int i;
  int j;
  NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;

  LOG_I(PHY, "Initialise transport\n");

  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    LOG_I(PHY,"Allocating Transport Channel Buffers for DLSCH, UE %d\n",i);
    for (j=0; j<2; j++) {
      gNB->dlsch[i][j] = new_gNB_dlsch(1,8,NSOFT,fp->N_RB_DL,0,fp);
      if (!gNB->dlsch[i][j]) {
	LOG_E(PHY,"Can't get gNB dlsch structures for UE %d \n", i);
	exit(-1);
      } else {
	gNB->dlsch[i][j]->rnti=0;
	LOG_D(PHY,"dlsch[%d][%d] => %p rnti:%d\n",i,j,gNB->dlsch[i][j], gNB->dlsch[i][j]->rnti);
      }
    }
    
    LOG_I(PHY,"Allocating Transport Channel Buffer for ULSCH, UE %d\n",i);
    gNB->ulsch[1+i] = new_gNB_ulsch(MAX_TURBO_ITERATIONS,fp->N_RB_UL, 0);
    
    if (!gNB->ulsch[1+i]) {
      LOG_E(PHY,"Can't get gNB ulsch structures\n");
      exit(-1);
    }
    
    // this is the transmission mode for the signalling channels
    // this will be overwritten with the real transmission mode by the RRC once the UE is connected
    gNB->transmission_mode[i] = fp->nb_antenna_ports_gNB==1 ? 1 : 2;
  }
  // ULSCH for RA
  gNB->ulsch[0] = new_gNB_ulsch(MAX_TURBO_ITERATIONS, fp->N_RB_UL, 0);
  
  if (!gNB->ulsch[0]) {
    LOG_E(PHY,"Can't get gNB ulsch structures\n");
    exit(-1);
  }
  gNB->dlsch_SI  = new_gNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"gNB %d.%d : SI %p\n",gNB->Mod_id,gNB->CC_id,gNB->dlsch_SI);
  gNB->dlsch_ra  = new_gNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"gNB %d.%d : RA %p\n",gNB->Mod_id,gNB->CC_id,gNB->dlsch_ra);
  gNB->dlsch_MCH = new_gNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"gNB %d.%d : MCH %p\n",gNB->Mod_id,gNB->CC_id,gNB->dlsch_MCH);
  
  
  gNB->rx_total_gain_dB=130;
  
  for(i=0; i<NUMBER_OF_UE_MAX; i++)
    gNB->mu_mimo_mode[i].dl_pow_off = 2;
  
  gNB->check_for_total_transmissions = 0;
  
  gNB->check_for_MUMIMO_transmissions = 0;
  
  gNB->FULL_MUMIMO_transmissions = 0;
  
  gNB->check_for_SUMIMO_transmissions = 0;
  
  fp->pucch_config_common.deltaPUCCH_Shift = 1;
    
} */

Guy De Souza's avatar
Guy De Souza committed
971 972
/// eNB kept in function name for nffapi calls, TO FIX
void init_eNB_afterRU(void) {
973 974 975 976

  int inst,CC_id,ru_id,i,aa;
  PHY_VARS_gNB *gNB;

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
977
  LOG_I(PHY,"%s() RC.nb_nr_inst:%d\n", __FUNCTION__, RC.nb_nr_inst);
978

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
979 980 981
  for (inst=0;inst<RC.nb_nr_inst;inst++) {
    LOG_I(PHY,"RC.nb_nr_CC[inst]:%d\n", RC.nb_nr_CC[inst]);
    for (CC_id=0;CC_id<RC.nb_nr_CC[inst];CC_id++) {
982

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
983
      LOG_I(PHY,"RC.nb_nr_CC[inst:%d][CC_id:%d]:%p\n", inst, CC_id, RC.gNB[inst][CC_id]);
984 985 986 987 988 989

      gNB                                  =  RC.gNB[inst][CC_id];
      phy_init_nr_gNB(gNB,0,0);
      // map antennas and PRACH signals to gNB RX
      if (0) AssertFatal(gNB->num_RU>0,"Number of RU attached to gNB %d is zero\n",gNB->Mod_id);
      LOG_I(PHY,"Mapping RX ports from %d RUs to gNB %d\n",gNB->num_RU,gNB->Mod_id);
Guy De Souza's avatar
Guy De Souza committed
990
      gNB->gNB_config.rf_config.tx_antenna_ports.value  = 0;
991 992 993 994 995 996 997 998

      //LOG_I(PHY,"Overwriting gNB->prach_vars.rxsigF[0]:%p\n", gNB->prach_vars.rxsigF[0]);

      gNB->prach_vars.rxsigF[0] = (int16_t**)malloc16(64*sizeof(int16_t*));

      LOG_I(PHY,"gNB->num_RU:%d\n", gNB->num_RU);

      for (ru_id=0,aa=0;ru_id<gNB->num_RU;ru_id++) {
Guy De Souza's avatar
Guy De Souza committed
999
	gNB->gNB_config.rf_config.tx_antenna_ports.value    += gNB->RU_list[ru_id]->nb_rx;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	AssertFatal(gNB->RU_list[ru_id]->common.rxdataF!=NULL,
		    "RU %d : common.rxdataF is NULL\n",
		    gNB->RU_list[ru_id]->idx);

	AssertFatal(gNB->RU_list[ru_id]->prach_rxsigF!=NULL,
		    "RU %d : prach_rxsigF is NULL\n",
		    gNB->RU_list[ru_id]->idx);

	for (i=0;i<gNB->RU_list[ru_id]->nb_rx;aa++,i++) { 
	  LOG_I(PHY,"Attaching RU %d antenna %d to gNB antenna %d\n",gNB->RU_list[ru_id]->idx,i,aa);
	  gNB->prach_vars.rxsigF[0][aa]    =  gNB->RU_list[ru_id]->prach_rxsigF[i];
	  gNB->common_vars.rxdataF[aa]     =  gNB->RU_list[ru_id]->common.rxdataF[i];
	}
      }



      /* TODO: review this code, there is something wrong.
       * In monolithic mode, we come here with nb_antennas_rx == 0
       * (not tested in other modes).
       */
Guy De Souza's avatar
Guy De Souza committed
1022
      if (gNB->gNB_config.rf_config.tx_antenna_ports.value < 1)
1023
      {
Guy De Souza's avatar
Guy De Souza committed
1024 1025
        LOG_I(PHY, "%s() ************* DJP ***** gNB->gNB_config.rf_config.tx_antenna_ports:%d - GOING TO HARD CODE TO 1", __FUNCTION__, gNB->gNB_config.rf_config.tx_antenna_ports.value);
        gNB->gNB_config.rf_config.tx_antenna_ports.value = 1;
1026 1027 1028 1029 1030 1031
      }
      else
      {
        //LOG_I(PHY," Delete code\n");
      }

Guy De Souza's avatar
Guy De Souza committed
1032
      if (gNB->gNB_config.rf_config.tx_antenna_ports.value < 1)
1033
      {
Guy De Souza's avatar
Guy De Souza committed
1034 1035
        LOG_I(PHY, "%s() ************* DJP ***** gNB->gNB_config.rf_config.tx_antenna_ports:%d - GOING TO HARD CODE TO 1", __FUNCTION__, gNB->gNB_config.rf_config.tx_antenna_ports.value);
        gNB->gNB_config.rf_config.tx_antenna_ports.value = 1;
1036 1037 1038 1039 1040 1041
      }
      else
      {
        //LOG_I(PHY," Delete code\n");
      }

Guy De Souza's avatar
Guy De Souza committed
1042 1043 1044
      AssertFatal(gNB->gNB_config.rf_config.tx_antenna_ports.value >0,
		  "inst %d, CC_id %d : nb_antennas_rx %d\n",inst,CC_id,gNB->gNB_config.rf_config.tx_antenna_ports.value);
      LOG_I(PHY,"inst %d, CC_id %d : nb_antennas_rx %d\n",inst,CC_id,gNB->gNB_config.rf_config.tx_antenna_ports.value);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
/// Transport init necessary for NR synchro
      //init_transport(gNB);
      //init_precoding_weights(RC.gNB[inst][CC_id]);
    }
    init_gNB_proc(inst);
  }

  for (ru_id=0;ru_id<RC.nb_RU;ru_id++) {

    AssertFatal(RC.ru[ru_id]!=NULL,"ru_id %d is null\n",ru_id);
    
Guy De Souza's avatar
Guy De Souza committed
1056
    RC.ru[ru_id]->nr_wakeup_rxtx         = wakeup_rxtx;
1057
//    RC.ru[ru_id]->wakeup_prach_eNB    = wakeup_prach_gNB;
Guy De Souza's avatar
Guy De Souza committed
1058
    RC.ru[ru_id]->gNB_top             = gNB_top;
1059 1060 1061 1062 1063 1064 1065 1066 1067
  }
}

void init_gNB(int single_thread_flag,int wait_for_sync) {

  int CC_id;
  int inst;
  PHY_VARS_gNB *gNB;

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1068
  LOG_I(PHY,"[nr-softmodem.c] gNB structure about to allocated RC.nb_nr_L1_inst:%d RC.nb_nr_L1_CC[0]:%d\n",RC.nb_nr_L1_inst,RC.nb_nr_L1_CC[0]);
1069

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1070
  if (RC.gNB == NULL) RC.gNB = (PHY_VARS_gNB***) malloc(RC.nb_nr_L1_inst*sizeof(PHY_VARS_gNB **));
1071
  LOG_I(PHY,"[lte-softmodem.c] gNB structure RC.gNB allocated\n");
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1072 1073 1074
  for (inst=0;inst<RC.nb_nr_L1_inst;inst++) {
    if (RC.gNB[inst] == NULL) RC.gNB[inst] = (PHY_VARS_gNB**) malloc(RC.nb_nr_CC[inst]*sizeof(PHY_VARS_gNB *));
    for (CC_id=0;CC_id<RC.nb_nr_L1_CC[inst];CC_id++) {
1075 1076 1077 1078
      if (RC.gNB[inst][CC_id] == NULL) RC.gNB[inst][CC_id] = (PHY_VARS_gNB*) malloc(sizeof(PHY_VARS_gNB));
      gNB                     = RC.gNB[inst][CC_id]; 
      gNB->abstraction_flag   = 0;
      gNB->single_thread_flag = single_thread_flag;
Guy De Souza's avatar
Guy De Souza committed
1079
      /*nr_polar_init(&gNB->nrPolar_params,
1080 1081
    		  	  	NR_POLAR_PBCH_MESSAGE_TYPE,
					NR_POLAR_PBCH_PAYLOAD_BITS,
Guy De Souza's avatar
Guy De Souza committed
1082
					NR_POLAR_PBCH_AGGREGATION_LEVEL);*/
1083 1084 1085 1086 1087 1088 1089

      LOG_I(PHY,"Initializing gNB %d CC_id %d single_thread_flag:%d\n",inst,CC_id,single_thread_flag);
#ifndef OCP_FRAMEWORK
      LOG_I(PHY,"Initializing gNB %d CC_id %d\n",inst,CC_id);
#endif

      LOG_I(PHY,"Registering with MAC interface module\n");
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1090
      AssertFatal((gNB->if_inst         = NR_IF_Module_init(inst))!=NULL,"Cannot register interface");
1091 1092
      gNB->if_inst->NR_Schedule_response   = nr_schedule_response;
      gNB->if_inst->NR_PHY_config_req      = nr_phy_config_request;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
      memset((void*)&gNB->UL_INFO,0,sizeof(gNB->UL_INFO));
      memset((void*)&gNB->Sched_INFO,0,sizeof(gNB->Sched_INFO));
      LOG_I(PHY,"Setting indication lists\n");
      gNB->UL_INFO.rx_ind.rx_indication_body.rx_pdu_list   = gNB->rx_pdu_list;
      gNB->UL_INFO.crc_ind.crc_indication_body.crc_pdu_list = gNB->crc_pdu_list;
      gNB->UL_INFO.sr_ind.sr_indication_body.sr_pdu_list = gNB->sr_pdu_list;
      gNB->UL_INFO.harq_ind.harq_indication_body.harq_pdu_list = gNB->harq_pdu_list;
      gNB->UL_INFO.cqi_ind.cqi_pdu_list = gNB->cqi_pdu_list;
      gNB->UL_INFO.cqi_ind.cqi_raw_pdu_list = gNB->cqi_raw_pdu_list;
      gNB->prach_energy_counter = 0;
    }

  }

  LOG_I(PHY,"[nr-softmodem.c] gNB structure allocated\n");
}


void stop_gNB(int nb_inst) {

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1113
  for (int inst=0;inst<nb_inst;inst++) {
1114 1115 1116 1117
    LOG_I(PHY,"Killing gNB %d processing threads\n",inst);
    kill_gNB_proc(inst);
  }
}