lte-enb.c 70.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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.0  (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
 */
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

/*! \file lte-enb.c
 * \brief Top-level threads for eNodeB
 * \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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sched.h>
#include <linux/sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
#include <sys/sysinfo.h>
#include "rt_wrapper.h"

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

#include "assertions.h"
#include "msc.h"

#include "PHY/types.h"

#include "PHY/defs.h"
#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

63
#include "PHY/LTE_TRANSPORT/if4_tools.h"
64
#include "PHY/LTE_TRANSPORT/if5_tools.h"
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include "PHY/extern.h"
#include "SCHED/extern.h"
#include "LAYER2/MAC/extern.h"

#include "../../SIMU/USER/init_lte.h"

#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/proto.h"
#include "RRC/LITE/extern.h"
#include "PHY_INTERFACE/extern.h"

#ifdef SMBV
#include "PHY/TOOLS/smbv.h"
unsigned short config_frames[4] = {2,9,11,13};
#endif
#include "UTIL/LOG/log_extern.h"
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
#include "enb_config.h"
//#include "PHY/TOOLS/time_meas.h"

#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

Rohit Gupta's avatar
Rohit Gupta committed
104 105
#include "T.h"

106 107 108 109 110 111 112 113 114 115
//#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;

116 117
// Fix per CC openair rf/if device update
// extern openair0_device openair0;
118 119 120 121 122 123 124 125 126 127 128 129 130

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

extern openair0_config_t openair0_cfg[MAX_CARDS];

extern pthread_cond_t sync_cond;
extern pthread_mutex_t sync_mutex;
extern int sync_var;

Raymond.Knopp's avatar
Raymond.Knopp committed
131
extern int transmission_mode;
132

133
//pthread_t                       main_eNB_thread;
134 135 136

time_stats_t softmodem_stats_mt; // main thread
time_stats_t softmodem_stats_hw; //  hw acquisition
137
time_stats_t softmodem_stats_rxtx_sf; // total tx time
138
time_stats_t softmodem_stats_rx_sf; // total rx time
139 140
//int32_t **rxdata;
//int32_t **txdata;
141

142 143
uint8_t seqno; //sequence number

144 145 146 147 148 149 150 151 152 153
static int                      time_offset[4] = {0,0,0,0};

/* 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;
154
} sync_phy_proc;
155

156 157
extern double cpuf;

158 159
void exit_fun(const char* s);

160
void init_eNB(eNB_func_t node_function[], eNB_timing_t node_timing[],int nb_inst,eth_params_t *,int,int);
161
void stop_eNB(int nb_inst);
162 163


Raymond Knopp's avatar
Raymond Knopp committed
164
static inline void thread_top_init(char *thread_name,
165
				   int affinity,
Raymond Knopp's avatar
Raymond Knopp committed
166 167 168
				   uint64_t runtime,
				   uint64_t deadline,
				   uint64_t period) {
Raymond Knopp's avatar
Raymond Knopp committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

  MSC_START_USE();

#ifdef DEADLINE_SCHEDULER
  struct sched_attr attr;

  unsigned int flags = 0;

  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;

  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = runtime;
  attr.sched_deadline = deadline;
  attr.sched_period   = period; 

  if (sched_setattr(0, &attr, flags) < 0 ) {
    perror("[SCHED] eNB tx thread: sched_setattr failed\n");
laurent's avatar
laurent committed
189
    exit(1);
Raymond Knopp's avatar
Raymond Knopp committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  }

#else //LOW_LATENCY
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD threads */
  /* CPU 1 is reserved for all RX_TX threads */
  /* Enable CPU Affinity only if number of CPUs >2 */
  CPU_ZERO(&cpuset);

#ifdef CPU_AFFINITY
  if (get_nprocs() > 2)
  {
207 208 209 210
    if (affinity == 0)
      CPU_SET(0,&cpuset);
    else
      for (j = 1; j < get_nprocs(); j++)
Raymond Knopp's avatar
Raymond Knopp committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        CPU_SET(j, &cpuset);
    s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
    if (s != 0)
    {
      perror( "pthread_setaffinity_np");
      exit_fun("Error setting processor affinity");
    }
  }
#endif //CPU_AFFINITY

  /* Check the actual affinity mask assigned to the thread */
  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  if (s != 0) {
    perror( "pthread_getaffinity_np");
    exit_fun("Error getting processor affinity ");
  }
  memset(cpu_affinity,0,sizeof(cpu_affinity));
  for (j = 0; j < CPU_SETSIZE; j++)
    if (CPU_ISSET(j, &cpuset)) {  
      char temp[1024];
      sprintf (temp, " CPU_%d", j);
      strcat(cpu_affinity, temp);
    }

  memset(&sparam, 0, sizeof(sparam));
236
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
Raymond Knopp's avatar
Raymond Knopp committed
237 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
  policy = SCHED_FIFO ; 
  
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
  if (s != 0) {
    perror("pthread_setschedparam : ");
    exit_fun("Error setting thread priority");
  }
  
  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
  if (s != 0) {
    perror("pthread_getschedparam : ");
    exit_fun("Error getting thread priority");
  }

  LOG_I(HW, "[SCHED][eNB] %s started on CPU %d TID %ld, sched_policy = %s , priority = %d, CPU Affinity=%s \n",thread_name,sched_getcpu(),gettid(),
                   (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                   (policy == SCHED_RR)    ? "SCHED_RR" :
                   (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                   "???",
                   sparam.sched_priority, cpu_affinity );

#endif //LOW_LATENCY

  mlockall(MCL_CURRENT | MCL_FUTURE);

}

Raymond Knopp's avatar
Raymond Knopp committed
264
static inline void wait_sync(char *thread_name) {
Raymond Knopp's avatar
Raymond Knopp committed
265 266 267 268 269 270 271 272 273 274 275 276 277

  printf( "waiting for sync (%s)\n",thread_name);
  pthread_mutex_lock( &sync_mutex );
  
  while (sync_var<0)
    pthread_cond_wait( &sync_cond, &sync_mutex );
  
  pthread_mutex_unlock(&sync_mutex);
  
  printf( "got sync (%s)\n", thread_name);

}

278

279 280
void do_OFDM_mod_rt(int subframe,PHY_VARS_eNB *phy_vars_eNB)
{
281

282 283
  unsigned int aa,slot_offset;
  //int dummy_tx_b[7680*4] __attribute__((aligned(32)));
284
  int i, tx_offset;
285
  //int slot_sizeF = (phy_vars_eNB->frame_parms.ofdm_symbol_size)* ((phy_vars_eNB->frame_parms.Ncp==1) ? 6 : 7);
286
  int len;
287
  //int slot_offset_F = (subframe<<1)*slot_sizeF;
288

289
  slot_offset = subframe*phy_vars_eNB->frame_parms.samples_per_tti;
290

291
  
292 293
  if ((subframe_select(&phy_vars_eNB->frame_parms,subframe)==SF_DL)||
      ((subframe_select(&phy_vars_eNB->frame_parms,subframe)==SF_S))) {
294 295
    //    LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);

296 297
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_OFDM_MODULATION,1);

298
    do_OFDM_mod_symbol(&phy_vars_eNB->common_vars,
299 300
                  0,
                  subframe<<1,
301 302
                  &phy_vars_eNB->frame_parms,
		  phy_vars_eNB->do_precoding);
303 304
 
    // if S-subframe generate first slot only 
305 306
    if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_DL) {
      do_OFDM_mod_symbol(&phy_vars_eNB->common_vars,
307 308
                    0,
                    1+(subframe<<1),
309 310
                    &phy_vars_eNB->frame_parms,
                    phy_vars_eNB->do_precoding);
311 312 313 314 315 316
    }

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_OFDM_MODULATION,0);
    

/*
317 318 319
    for (aa=0; aa<phy_vars_eNB->frame_parms.nb_antennas_tx; aa++) {
      if (phy_vars_eNB->frame_parms.Ncp == EXTENDED) {
        PHY_ofdm_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F],
320
                     dummy_tx_b,
321
                     phy_vars_eNB->frame_parms.ofdm_symbol_size,
322
                     6,
323
                     phy_vars_eNB->frame_parms.nb_prefix_samples,
324
                     CYCLIC_PREFIX);
325 326 327 328 329 330 331
	if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_DL) 
	  PHY_ofdm_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
		       dummy_tx_b+(phy_vars_eNB->frame_parms.samples_per_tti>>1),
		       phy_vars_eNB->frame_parms.ofdm_symbol_size,
		       6,
		       phy_vars_eNB->frame_parms.nb_prefix_samples,
		       CYCLIC_PREFIX);
332
      } else {
333
        normal_prefix_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F],
334 335
                          dummy_tx_b,
                          7,
336
                          &(phy_vars_eNB->frame_parms));
337
	// if S-subframe generate first slot only
338
	if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_DL)
339 340
	  normal_prefix_mod(&phy_vars_eNB->common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
			    dummy_tx_b+(phy_vars_eNB->frame_parms.samples_per_tti>>1),
341
			    7,
342
			    &(phy_vars_eNB->frame_parms));
343
      }
344
    } */
345

346
    for (aa=0; aa<phy_vars_eNB->frame_parms.nb_antennas_tx; aa++) {
347
      // if S-subframe generate first slot only
348 349
      if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_S)
	len = phy_vars_eNB->frame_parms.samples_per_tti>>1;
350
      else
351
	len = phy_vars_eNB->frame_parms.samples_per_tti;
352 353 354 355 356 357 358
      /*
      for (i=0;i<len;i+=4) {
	dummy_tx_b[i] = 0x100;
	dummy_tx_b[i+1] = 0x01000000;
	dummy_tx_b[i+2] = 0xff00;
	dummy_tx_b[i+3] = 0xff000000;
	}*/
359 360
      for (i=0; i<len; i++) {
        tx_offset = (int)slot_offset+time_offset[aa]+i;
361

362 363
	
        if (tx_offset<0)
364
          tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
365

366 367
        if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti))
          tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
368

369
/*	((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[0] = ((short*)dummy_tx_b)[2*i]<<openair0_cfg[0].iq_txshift;
370
	
371
	((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[1] = ((short*)dummy_tx_b)[2*i+1]<<openair0_cfg[0].iq_txshift; */
372

373
	((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[0] = ((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[0]<<openair0_cfg[0].iq_txshift;
374
	
375
	((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[1] = ((short*)&phy_vars_eNB->common_vars.txdata[0][aa][tx_offset])[1]<<openair0_cfg[0].iq_txshift;
376
     }
377
     // if S-subframe switch to RX in second subframe
378
     if (subframe_select(&phy_vars_eNB->frame_parms,subframe) == SF_S) {
379
       for (i=0; i<len; i++) {
380
	 phy_vars_eNB->common_vars.txdata[0][aa][tx_offset++] = 0x00010001;
381 382
       }
     }
383

384
     if ((((phy_vars_eNB->frame_parms.tdd_config==0) ||
385 386 387
	  (phy_vars_eNB->frame_parms.tdd_config==1) ||
	  (phy_vars_eNB->frame_parms.tdd_config==2) ||
	  (phy_vars_eNB->frame_parms.tdd_config==6)) && 
388
	  (subframe==0)) || (subframe==5)) {
389 390 391
       // turn on tx switch N_TA_offset before
       //LOG_D(HW,"subframe %d, time to switch to tx (N_TA_offset %d, slot_offset %d) \n",subframe,phy_vars_eNB->N_TA_offset,slot_offset);
       for (i=0; i<phy_vars_eNB->N_TA_offset; i++) {
392
         tx_offset = (int)slot_offset+time_offset[aa]+i-phy_vars_eNB->N_TA_offset;
393 394
         if (tx_offset<0)
           tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
395
	 
396 397
	 if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti))
	   tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->frame_parms.samples_per_tti;
398
	 
399
	 phy_vars_eNB->common_vars.txdata[0][aa][tx_offset] = 0x00000000;
400 401 402 403
       }
     }
    }
  }
404 405
}

406

407
void tx_fh_if5(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
408
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, proc->timestamp_tx&0xffffffff );
Raymond.Knopp's avatar
Raymond.Knopp committed
409 410 411
  if ((eNB->frame_parms.frame_type==TDD) &&
      (subframe_select(&eNB->frame_parms,proc->subframe_tx) != SF_UL))    
    send_IF5(eNB, proc->timestamp_tx, proc->subframe_tx, &seqno, IF5_RRH_GW_DL);
412 413
}

414 415
void tx_fh_if5_mobipass(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, proc->timestamp_tx&0xffffffff );
Raymond.Knopp's avatar
Raymond.Knopp committed
416 417 418
  if ((eNB->frame_parms.frame_type==TDD) &&
      (subframe_select(&eNB->frame_parms,proc->subframe_tx) != SF_UL))    
    send_IF5(eNB, proc->timestamp_tx, proc->subframe_tx, &seqno, IF5_MOBIPASS); 
419 420
}

Raymond.Knopp's avatar
Raymond.Knopp committed
421 422 423 424
void tx_fh_if4p5(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
  if ((eNB->frame_parms.frame_type==TDD) &&
      (subframe_select(&eNB->frame_parms,proc->subframe_tx) != SF_UL))    
    send_IF4p5(eNB,proc->frame_tx,proc->subframe_tx, IF4p5_PDLFFT, 0);
425 426
}

427 428 429 430
void proc_tx_high0(PHY_VARS_eNB *eNB,
		   eNB_rxtx_proc_t *proc,
		   relaying_type_t r_type,
		   PHY_VARS_RN *rn) {
431

Raymond.Knopp's avatar
Raymond.Knopp committed
432
  int offset = proc == &eNB->proc.proc_rxtx[0] ? 0 : 1;
Raymond Knopp's avatar
Raymond Knopp committed
433 434

  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB+offset, proc->frame_tx );
435
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB+offset, proc->subframe_tx );
Raymond Knopp's avatar
Raymond Knopp committed
436

437
  phy_procedures_eNB_TX(eNB,proc,r_type,rn,1);
438 439 440 441 442 443 444 445 446 447 448 449 450 451

  /* we're done, let the next one proceed */
  if (pthread_mutex_lock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
    LOG_E(PHY, "[SCHED][eNB] error locking PHY proc mutex for eNB TX proc\n");
    exit_fun("nothing to add");
  }	
  sync_phy_proc.phy_proc_CC_id++;
  sync_phy_proc.phy_proc_CC_id %= MAX_NUM_CCs;
  pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);
  if (pthread_mutex_unlock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
    LOG_E(PHY, "[SCHED][eNB] error unlocking PHY proc mutex for eNB TX proc\n");
    exit_fun("nothing to add");
  }

452 453 454 455 456 457
}

void proc_tx_high(PHY_VARS_eNB *eNB,
		  eNB_rxtx_proc_t *proc,
		  relaying_type_t r_type,
		  PHY_VARS_RN *rn) {
458

Raymond Knopp's avatar
Raymond Knopp committed
459

460 461 462 463 464 465 466 467 468 469 470 471
  // do PHY high
  proc_tx_high0(eNB,proc,r_type,rn);

  // if TX fronthaul go ahead 
  if (eNB->tx_fh) eNB->tx_fh(eNB,proc);

}

void proc_tx_full(PHY_VARS_eNB *eNB,
		  eNB_rxtx_proc_t *proc,
		  relaying_type_t r_type,
		  PHY_VARS_RN *rn) {
472

473 474 475 476 477 478

  // do PHY high
  proc_tx_high0(eNB,proc,r_type,rn);
  // do OFDM modulation
  do_OFDM_mod_rt(proc->subframe_tx,eNB);
  // if TX fronthaul go ahead 
479 480
  if (eNB->tx_fh) eNB->tx_fh(eNB,proc);

481
  /*
482 483 484 485 486 487 488 489 490 491
  if (proc->frame_tx>1000) {
    write_output("/tmp/txsig0.m","txs0", &eNB->common_vars.txdata[eNB->Mod_id][0][0], eNB->frame_parms.samples_per_tti*10,1,1);
    write_output("/tmp/txsigF0.m","txsF0", &eNB->common_vars.txdataF[eNB->Mod_id][0][0],eNB->frame_parms.symbols_per_tti*eNB->frame_parms.ofdm_symbol_size*10,1,1);
    write_output("/tmp/txsig1.m","txs1", &eNB->common_vars.txdata[eNB->Mod_id][1][0], eNB->frame_parms.samples_per_tti*10,1,1);
    write_output("/tmp/txsigF1.m","txsF1", &eNB->common_vars.txdataF[eNB->Mod_id][1][0],eNB->frame_parms.symbols_per_tti*eNB->frame_parms.ofdm_symbol_size*10,1,1);
    if (transmission_mode == 7) 
      write_output("/tmp/txsigF5.m","txsF5", &eNB->common_vars.txdataF[eNB->Mod_id][5][0],eNB->frame_parms.symbols_per_tti*eNB->frame_parms.ofdm_symbol_size*10,1,1);
    exit_fun("");
  }
  */
492 493
}

494 495 496 497
void proc_tx_rru_if4p5(PHY_VARS_eNB *eNB,
		       eNB_rxtx_proc_t *proc,
		       relaying_type_t r_type,
		       PHY_VARS_RN *rn) {
498 499 500 501 502

  uint32_t symbol_number=0;
  uint32_t symbol_mask, symbol_mask_full;
  uint16_t packet_type;

Raymond Knopp's avatar
Raymond Knopp committed
503 504 505
  int offset = proc == &eNB->proc.proc_rxtx[0] ? 0 : 1;

  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB+offset, proc->frame_tx );
506
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB+offset, proc->subframe_tx );
Raymond Knopp's avatar
Raymond Knopp committed
507

508 509 510 511 512
  /// **** recv_IF4 of txdataF from RCC **** ///             
  symbol_number = 0;
  symbol_mask = 0;
  symbol_mask_full = (1<<eNB->frame_parms.symbols_per_tti)-1;
  
Raymond Knopp's avatar
Raymond Knopp committed
513

514 515 516 517 518 519 520 521 522
  do { 
    recv_IF4p5(eNB, &proc->frame_tx, &proc->subframe_tx, &packet_type, &symbol_number);
    symbol_mask = symbol_mask | (1<<symbol_number);
  } while (symbol_mask != symbol_mask_full); 

  do_OFDM_mod_rt(proc->subframe_tx, eNB);
}

void proc_tx_rru_if5(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
Raymond Knopp's avatar
Raymond Knopp committed
523 524 525
  int offset = proc == &eNB->proc.proc_rxtx[0] ? 0 : 1;

  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB+offset, proc->frame_tx );
526
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB+offset, proc->subframe_tx );
527 528
  /// **** recv_IF5 of txdata from BBU **** ///       
  recv_IF5(eNB, &proc->timestamp_tx, proc->subframe_tx, IF5_RRH_GW_DL);
529 530
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
int wait_CCs(eNB_rxtx_proc_t *proc) {

  struct timespec wait;

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

  if (pthread_mutex_timedlock(&sync_phy_proc.mutex_phy_proc_tx,&wait) != 0) {
    LOG_E(PHY, "[SCHED][eNB] error locking PHY proc mutex for eNB TX\n");
    exit_fun("nothing to add");
    return(-1);
  }
  
  // wait for our turn or oai_exit
  while (sync_phy_proc.phy_proc_CC_id != proc->CC_id && !oai_exit) {
    pthread_cond_wait(&sync_phy_proc.cond_phy_proc_tx,
		      &sync_phy_proc.mutex_phy_proc_tx);
  }
  
  if (pthread_mutex_unlock(&sync_phy_proc.mutex_phy_proc_tx) != 0) {
    LOG_E(PHY, "[SCHED][eNB] error unlocking PHY proc mutex for eNB TX\n");
    exit_fun("nothing to add");
    return(-1);
  }
  return(0);
}
557

Raymond Knopp's avatar
Raymond Knopp committed
558 559 560
static inline int rxtx(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc, char *thread_name) {

  start_meas(&softmodem_stats_rxtx_sf);
561

Raymond Knopp's avatar
Raymond Knopp committed
562 563
  // ****************************************
  // Common RX procedures subframe n
564

565 566
  if ((eNB->do_prach)&&((eNB->node_function != NGFI_RCC_IF4p5)))
    eNB->do_prach(eNB,proc->frame_rx,proc->subframe_rx);
567
  phy_procedures_eNB_common_RX(eNB,proc);
Raymond Knopp's avatar
Raymond Knopp committed
568 569 570 571 572 573 574 575 576
  
  // UE-specific RX processing for subframe n
  if (eNB->proc_uespec_rx) eNB->proc_uespec_rx(eNB, proc, no_relay );
  
  // *****************************************
  // 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)
  // *****************************************
577
  //if (wait_CCs(proc)<0) return(-1);
Raymond Knopp's avatar
Raymond Knopp committed
578 579 580 581 582 583 584 585 586 587 588 589
  
  if (oai_exit) return(-1);
  
  if (eNB->proc_tx)	eNB->proc_tx(eNB, proc, no_relay, NULL );
  
  if (release_thread(&proc->mutex_rxtx,&proc->instance_cnt_rxtx,thread_name)<0) return(-1);

  stop_meas( &softmodem_stats_rxtx_sf );
  
  return(0);
}

590
/*!
591
 * \brief The RX UE-specific and TX thread of eNB.
592 593 594
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
595 596
static void* eNB_thread_rxtx( void* param ) {

597
  static int eNB_thread_rxtx_status;
598

599
  eNB_rxtx_proc_t *proc = (eNB_rxtx_proc_t*)param;
600 601
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];

Raymond Knopp's avatar
Raymond Knopp committed
602 603
  char thread_name[100];

604

605
  // set default return value
606
  eNB_thread_rxtx_status = 0;
607 608


Raymond Knopp's avatar
Raymond Knopp committed
609
  sprintf(thread_name,"RXn_TXnp4_%d\n",&eNB->proc.proc_rxtx[0] == proc ? 0 : 1);
610
  thread_top_init(thread_name,1,850000L,1000000L,2000000L);
611 612

  while (!oai_exit) {
613
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
614

Raymond Knopp's avatar
Raymond Knopp committed
615
    if (wait_on_condition(&proc->mutex_rxtx,&proc->cond_rxtx,&proc->instance_cnt_rxtx,thread_name)<0) break;
616

617
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 1 );
618

Raymond Knopp's avatar
Raymond Knopp committed
619
    
620
  
621 622
    if (oai_exit) break;

623 624
    if (eNB->CC_id==0)
      if (rxtx(eNB,proc,thread_name) < 0) break;
625

Raymond Knopp's avatar
Raymond Knopp committed
626
  } // while !oai_exit
627

628
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RXTX0+(proc->subframe_rx&1), 0 );
629

Raymond Knopp's avatar
Raymond Knopp committed
630
  printf( "Exiting eNB thread RXn_TXnp4\n");
631

632 633
  eNB_thread_rxtx_status = 0;
  return &eNB_thread_rxtx_status;
634 635
}

636
#if defined(ENABLE_ITTI) && defined(ENABLE_USE_MME)
637 638 639
/* Wait for eNB application initialization to be complete (eNB registration to MME) */
static void wait_system_ready (char *message, volatile int *start_flag) {
  
640
  static char *indicator[] = {".    ", "..   ", "...  ", ".... ", ".....",
641
			      " ....", "  ...", "   ..", "    .", "     "};
642 643 644 645 646 647 648
  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);
649
  }
650 651
  
  LOG_D(EMU,"\n");
652 653
}
#endif
654

Raymond Knopp's avatar
Raymond Knopp committed
655

656
// asynchronous UL with IF5 (RCC,RAU,eNodeB_BBU)
Raymond Knopp's avatar
Raymond Knopp committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
void fh_if5_asynch_UL(PHY_VARS_eNB *eNB,int *frame,int *subframe) {

  eNB_proc_t *proc       = &eNB->proc;
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;

  recv_IF5(eNB, &proc->timestamp_rx, *subframe, IF5_RRH_GW_UL); 

  proc->subframe_rx = (proc->timestamp_rx/fp->samples_per_tti)%10;
  proc->frame_rx    = (proc->timestamp_rx/(10*fp->samples_per_tti))&1023;

  if (proc->first_rx != 0) {
    proc->first_rx = 0;
    *subframe = proc->subframe_rx;
    *frame    = proc->frame_rx; 
  }
  else {
    if (proc->subframe_rx != *subframe) {
674
      LOG_E(PHY,"fh_if5_asynch_UL: subframe_rx %d is not what we expect %d\n",proc->subframe_rx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
675 676 677
      exit_fun("Exiting");
    }
    if (proc->frame_rx != *frame) {
678
      LOG_E(PHY,"fh_if5_asynch_UL: subframe_rx %d is not what we expect %d\n",proc->frame_rx,*frame);  
Raymond Knopp's avatar
Raymond Knopp committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
      exit_fun("Exiting");
    }
  }
} // eNodeB_3GPP_BBU 

// asynchronous UL with IF4p5 (RCC,RAU,eNodeB_BBU)
void fh_if4p5_asynch_UL(PHY_VARS_eNB *eNB,int *frame,int *subframe) {

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  eNB_proc_t *proc       = &eNB->proc;

  uint16_t packet_type;
  uint32_t symbol_number,symbol_mask,symbol_mask_full,prach_rx;


  symbol_number = 0;
  symbol_mask = 0;
  symbol_mask_full = (1<<fp->symbols_per_tti)-1;
  prach_rx = 0;

  do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
    recv_IF4p5(eNB, &proc->frame_rx, &proc->subframe_rx, &packet_type, &symbol_number);
    if (proc->first_rx != 0) {
      *frame = proc->frame_rx;
      *subframe = proc->subframe_rx;
      proc->first_rx = 0;
    }
    else {
      if (proc->frame_rx != *frame) {
708
	LOG_E(PHY,"fh_if4p5_asynch_UL: frame_rx %d is not what we expect %d\n",proc->frame_rx,*frame);
Raymond Knopp's avatar
Raymond Knopp committed
709 710 711
	exit_fun("Exiting");
      }
      if (proc->subframe_rx != *subframe) {
712
	LOG_E(PHY,"fh_if4p5_asynch_UL: subframe_rx %d is not what we expect %d\n",proc->subframe_rx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	exit_fun("Exiting");
      }
    }
    if (packet_type == IF4p5_PULFFT) {
      symbol_mask = symbol_mask | (1<<symbol_number);
      prach_rx = (is_prach_subframe(fp, proc->frame_rx, proc->subframe_rx)>0) ? 1 : 0;                            
    } else if (packet_type == IF4p5_PRACH) {
      prach_rx = 0;
    }
  } while( (symbol_mask != symbol_mask_full) || (prach_rx == 1));    
  

} 


void fh_if5_asynch_DL(PHY_VARS_eNB *eNB,int *frame,int *subframe) {

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  eNB_proc_t *proc       = &eNB->proc;
  int subframe_tx,frame_tx;
  openair0_timestamp timestamp_tx;

  recv_IF5(eNB, &timestamp_tx, *subframe, IF5_RRH_GW_DL); 
Raymond Knopp's avatar
Raymond Knopp committed
736
  //printf("Received subframe %d (TS %llu) from RCC\n",subframe_tx,timestamp_tx);
Raymond Knopp's avatar
Raymond Knopp committed
737 738 739 740

  subframe_tx = (timestamp_tx/fp->samples_per_tti)%10;
  frame_tx    = (timestamp_tx/(fp->samples_per_tti*10))&1023;

741 742 743
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB, frame_tx );
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB, subframe_tx );

Raymond Knopp's avatar
Raymond Knopp committed
744 745 746 747 748 749 750
  if (proc->first_tx != 0) {
    *subframe = subframe_tx;
    *frame    = frame_tx;
    proc->first_tx = 0;
  }
  else {
    if (subframe_tx != *subframe) {
751
      LOG_E(PHY,"fh_if5_asynch_DL: subframe_tx %d is not what we expect %d\n",subframe_tx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
752 753 754
      exit_fun("Exiting");
    }
    if (frame_tx != *frame) { 
755
      LOG_E(PHY,"fh_if5_asynch_DL: frame_tx %d is not what we expect %d\n",frame_tx,*frame);
Raymond Knopp's avatar
Raymond Knopp committed
756 757 758 759 760 761 762 763 764 765 766
      exit_fun("Exiting");
    }
  }
}

void fh_if4p5_asynch_DL(PHY_VARS_eNB *eNB,int *frame,int *subframe) {

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  eNB_proc_t *proc       = &eNB->proc;

  uint16_t packet_type;
767
  uint32_t symbol_number,symbol_mask_full;
Raymond Knopp's avatar
Raymond Knopp committed
768 769 770 771 772 773 774 775 776 777 778
  int subframe_tx,frame_tx;

  symbol_number = 0;
  symbol_mask_full = (1<<fp->symbols_per_tti)-1;

  do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
    recv_IF4p5(eNB, &frame_tx, &subframe_tx, &packet_type, &symbol_number);
    if (proc->first_tx != 0) {
      *frame    = frame_tx;
      *subframe = subframe_tx;
      proc->first_tx = 0;
779
      proc->frame_offset = frame_tx - proc->frame_tx;
Raymond Knopp's avatar
Raymond Knopp committed
780 781 782
    }
    else {
      if (frame_tx != *frame) {
783
	LOG_E(PHY,"fh_if4p5_asynch_DL: frame_tx %d is not what we expect %d\n",frame_tx,*frame);
784
	exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
785 786
      }
      if (subframe_tx != *subframe) {
787 788
	LOG_E(PHY,"fh_if4p5_asynch_DL: (frame %d) subframe_tx %d is not what we expect %d\n",frame_tx,subframe_tx,*subframe);
	//*subframe = subframe_tx;
789
	exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
790 791 792
      }
    }
    if (packet_type == IF4p5_PDLFFT) {
793
      proc->symbol_mask[subframe_tx] =proc->symbol_mask[subframe_tx] | (1<<symbol_number);
Raymond Knopp's avatar
Raymond Knopp committed
794 795 796 797 798
    }
    else {
      LOG_E(PHY,"Illegal IF4p5 packet type (should only be IF4p5_PDLFFT%d\n",packet_type);
      exit_fun("Exiting");
    }
799 800
  } while (proc->symbol_mask[*subframe] != symbol_mask_full);    

801 802
  *frame = frame_tx;

803

804 805 806
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB, frame_tx );
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_ENB, subframe_tx );

807 808
  // intialize this to zero after we're done with the subframe
  proc->symbol_mask[*subframe] = 0;
Raymond Knopp's avatar
Raymond Knopp committed
809
  
810
  do_OFDM_mod_rt(*subframe, eNB);
Raymond Knopp's avatar
Raymond Knopp committed
811 812
} 

813
/*!
814
 * \brief The Asynchronous RX/TX FH thread of RAU/RCC/eNB/RRU.
815 816 817 818
 * This handles the RX FH for an asynchronous RRU/UE
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
819
static void* eNB_thread_asynch_rxtx( void* param ) {
820

821
  static int eNB_thread_asynch_rxtx_status;
822

823 824
  eNB_proc_t *proc = (eNB_proc_t*)param;
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];
Raymond Knopp's avatar
Raymond Knopp committed
825 826


827

Raymond Knopp's avatar
Raymond Knopp committed
828
  int subframe=0, frame=0; 
829

830
  thread_top_init("thread_asynch",1,870000L,1000000L,1000000L);
831 832 833

  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe

Raymond Knopp's avatar
Raymond Knopp committed
834
  wait_sync("thread_asynch");
835

836 837
  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe
  printf( "waiting for devices (eNB_thread_asynch_rx)\n");
838

Raymond Knopp's avatar
Raymond Knopp committed
839
  wait_on_condition(&proc->mutex_asynch_rxtx,&proc->cond_asynch_rxtx,&proc->instance_cnt_asynch_rxtx,"thread_asynch");
840

841 842 843
  printf( "devices ok (eNB_thread_asynch_rx)\n");


Raymond Knopp's avatar
Raymond Knopp committed
844 845 846
  while (!oai_exit) { 
   
    if (oai_exit) break;   
847

Raymond Knopp's avatar
Raymond Knopp committed
848 849 850 851 852 853 854
    if (subframe==9) { 
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
    }      
855

Raymond Knopp's avatar
Raymond Knopp committed
856 857 858
    if (eNB->fh_asynch) eNB->fh_asynch(eNB,&frame,&subframe);
    else AssertFatal(1==0, "Unknown eNB->node_function %d",eNB->node_function);
    
859
  }
860

861 862
  eNB_thread_asynch_rxtx_status=0;
  return(&eNB_thread_asynch_rxtx_status);
863
}
864

865

Raymond Knopp's avatar
Raymond Knopp committed
866 867 868 869 870 871



void rx_rf(PHY_VARS_eNB *eNB,int *frame,int *subframe) {

  eNB_proc_t *proc = &eNB->proc;
872 873 874 875
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  void *rxp[fp->nb_antennas_rx],*txp[fp->nb_antennas_tx]; 
  unsigned int rxs,txs;
  int i;
876
  int tx_sfoffset = (eNB->single_thread_flag == 1) ? 3 : 2;
877
  openair0_timestamp ts,old_ts;
878

879 880
  if (proc->first_rx==0) {
    
881
    // Transmit TX buffer based on timestamp from RX
882
    //    printf("trx_write -> USRP TS %llu (sf %d)\n", (proc->timestamp_rx+(3*fp->samples_per_tti)),(proc->subframe_rx+2)%10);
883
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, (proc->timestamp_rx+(tx_sfoffset*fp->samples_per_tti)-openair0_cfg[0].tx_sample_advance)&0xffffffff );
884 885
    // prepare tx buffer pointers
	
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    if ((subframe_select(fp,proc->subframe_rx+tx_sfoffset) == SF_DL) ||
	(subframe_select(fp,proc->subframe_rx+tx_sfoffset) == SF_S)) {
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 1 );

      for (i=0; i<fp->nb_antennas_tx; i++)
	txp[i] = (void*)&eNB->common_vars.txdata[0][i][((proc->subframe_rx+tx_sfoffset)%10)*fp->samples_per_tti]; 
      
      txs = eNB->rfdevice.trx_write_func(&eNB->rfdevice,
					 proc->timestamp_rx+eNB->ts_offset+(tx_sfoffset*fp->samples_per_tti)-openair0_cfg[0].tx_sample_advance,
					 txp,
					 fp->samples_per_tti,
					 fp->nb_antennas_tx,
					 1);
      
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
      
      
      
      if (txs !=  fp->samples_per_tti) {
	LOG_E(PHY,"TX : Timeout (sent %d/%d)\n",txs, fp->samples_per_tti);
	exit_fun( "problem transmitting samples" );
      }	
    }
909
  }
910

911 912 913 914
  for (i=0; i<fp->nb_antennas_rx; i++)
    rxp[i] = (void*)&eNB->common_vars.rxdata[0][i][*subframe*fp->samples_per_tti];
  
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 1 );
915

916 917
  old_ts = proc->timestamp_rx;

918
  rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
919
				    &ts,
920 921 922
				    rxp,
				    fp->samples_per_tti,
				    fp->nb_antennas_rx);
923

924 925
  proc->timestamp_rx = ts-eNB->ts_offset;

926
  if (rxs != fp->samples_per_tti)
927
    LOG_E(PHY,"rx_rf: Asked for %d samples, got %d from USRP\n",fp->samples_per_tti,rxs);
928

929
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 0 );
Raymond Knopp's avatar
Raymond Knopp committed
930
 
931
  if (proc->first_rx == 1) {
Raymond Knopp's avatar
Raymond Knopp committed
932
    eNB->ts_offset = proc->timestamp_rx;
933 934
    proc->timestamp_rx=0;
  }
935
  else {
936

937
    if (proc->timestamp_rx - old_ts != fp->samples_per_tti) {
938 939
      LOG_I(PHY,"rx_rf: rfdevice timing drift of %d samples\n",proc->timestamp_rx - old_ts - fp->samples_per_tti);
      eNB->ts_offset += (proc->timestamp_rx - old_ts - fp->samples_per_tti);
940
      proc->timestamp_rx = ts-eNB->ts_offset;
941 942
    }
  }
943 944
  proc->frame_rx    = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
  proc->subframe_rx = (proc->timestamp_rx / fp->samples_per_tti)%10;
945
  proc->frame_rx    = (proc->frame_rx+proc->frame_offset)&1023;
946
  proc->frame_tx    = proc->frame_rx;
947
  if (proc->subframe_rx > 5) proc->frame_tx=(proc->frame_tx+1)&1023;
948 949
  // synchronize first reception to frame 0 subframe 0

950
  proc->timestamp_tx = proc->timestamp_rx+(4*fp->samples_per_tti);
951
  //  printf("trx_read <- USRP TS %lu (offset %d sf %d, f %d, first_rx %d)\n", proc->timestamp_rx,eNB->ts_offset,proc->subframe_rx,proc->frame_rx,proc->first_rx);  
952 953 954
  
  if (proc->first_rx == 0) {
    if (proc->subframe_rx != *subframe){
955
      LOG_E(PHY,"rx_rf: Received Timestamp (%llu) doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->timestamp_rx,proc->subframe_rx,*subframe);
956 957
      exit_fun("Exiting");
    }
958
    int f2 = (*frame+proc->frame_offset)&1023;    
959
    if (proc->frame_rx != f2) {
960
      LOG_E(PHY,"rx_rf: Received Timestamp (%llu) doesn't correspond to the time we think it is (proc->frame_rx %d frame %d, frame_offset %d, f2 %d)\n",proc->timestamp_rx,proc->frame_rx,*frame,proc->frame_offset,f2);
961 962 963 964 965 966 967 968 969 970 971 972
      exit_fun("Exiting");
    }
  } else {
    proc->first_rx = 0;
    *frame = proc->frame_rx;
    *subframe = proc->subframe_rx;        
  }
  
  //printf("timestamp_rx %lu, frame %d(%d), subframe %d(%d)\n",proc->timestamp_rx,proc->frame_rx,frame,proc->subframe_rx,subframe);
  
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
  
973 974 975 976 977
  if (rxs != fp->samples_per_tti)
    exit_fun( "problem receiving samples" );
  

  
978 979
}

Raymond Knopp's avatar
Raymond Knopp committed
980
void rx_fh_if5(PHY_VARS_eNB *eNB,int *frame, int *subframe) {
981 982

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
Raymond Knopp's avatar
Raymond Knopp committed
983
  eNB_proc_t *proc = &eNB->proc;
984 985 986

  recv_IF5(eNB, &proc->timestamp_rx, *subframe, IF5_RRH_GW_UL); 

987 988
  proc->frame_rx    = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
  proc->subframe_rx = (proc->timestamp_rx / fp->samples_per_tti)%10;
989 990
  
  if (proc->first_rx == 0) {
991
    if (proc->subframe_rx != *subframe){
992
      LOG_E(PHY,"rx_fh_if5: Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,subframe);
993 994 995
      exit_fun("Exiting");
    }
    
996
    if (proc->frame_rx != *frame) {
997
      LOG_E(PHY,"rx_fh_if5: Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,frame);
998 999 1000 1001
      exit_fun("Exiting");
    }
  } else {
    proc->first_rx = 0;
1002 1003
    *frame = proc->frame_rx;
    *subframe = proc->subframe_rx;        
1004
  }      
Raymond.Knopp's avatar
Raymond.Knopp committed
1005

1006 1007


Raymond.Knopp's avatar
Raymond.Knopp committed
1008
  proc->timestamp_tx = proc->timestamp_rx +  (4*fp->samples_per_tti);
1009 1010 1011 1012 1013
  
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );

}

Raymond Knopp's avatar
Raymond Knopp committed
1014

1015
void rx_fh_if4p5(PHY_VARS_eNB *eNB,int *frame,int *subframe) {
1016 1017

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
1018
  eNB_proc_t *proc = &eNB->proc;
1019
  int f,sf;
1020 1021 1022

  uint16_t packet_type;
  uint32_t symbol_number=0;
Raymond.Knopp's avatar
Raymond.Knopp committed
1023
  uint32_t symbol_mask_full;
1024 1025

  symbol_mask_full = (1<<fp->symbols_per_tti)-1;
1026

1027
  if (eNB->CC_id==1) LOG_I(PHY,"rx_fh_if4p5: frame %d, subframe %d\n",*frame,*subframe);
1028
  do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
1029
    recv_IF4p5(eNB, &f, &sf, &packet_type, &symbol_number);
1030

1031
    //proc->frame_rx = (proc->frame_rx + proc->frame_offset)&1023;
1032
    if (packet_type == IF4p5_PULFFT) {
1033
      LOG_D(PHY,"rx_fh:if4p5: frame %d, subframe %d, PULFFT symbol %d\n",f,sf,symbol_number);
1034

Raymond.Knopp's avatar
Raymond.Knopp committed
1035
      proc->symbol_mask[sf] = proc->symbol_mask[sf] | (1<<symbol_number);
1036
    } else if (packet_type == IF4p5_PRACH) {
1037
      LOG_D(PHY,"rx_fh:if4p5: frame %d, subframe %d, PRACH\n",f,sf);
1038 1039
      // wakeup prach processing
      if (eNB->do_prach) eNB->do_prach(eNB,f,sf);
1040
    }
1041

1042
    if (eNB->CC_id==1) LOG_I(PHY,"rx_fh_if4p5: symbol_mask[%d] %x\n",*subframe,proc->symbol_mask[*subframe]);
1043

1044
  } while(proc->symbol_mask[*subframe] != symbol_mask_full);    
Raymond.Knopp's avatar
Raymond.Knopp committed
1045 1046 1047
  proc->subframe_rx = *subframe;
  proc->frame_rx = *frame;

1048 1049 1050 1051
  proc->symbol_mask[*subframe] = 0;
  proc->symbol_mask[(9+*subframe)%10]= 0; // to handle a resynchronization event

  if (eNB->CC_id==1) LOG_I(PHY,"Clearing symbol_mask[%d]\n",*subframe);
1052 1053

  //caculate timestamp_rx, timestamp_tx based on frame and subframe
1054 1055 1056
  proc->timestamp_rx = ((proc->frame_rx * 10)  + proc->subframe_rx ) * fp->samples_per_tti ;
  proc->timestamp_tx = proc->timestamp_rx +  (4*fp->samples_per_tti);
  
1057
 
1058 1059
  if (proc->first_rx == 0) {
    if (proc->subframe_rx != *subframe){
1060
      LOG_E(PHY,"rx_fh_if4p5, CC_id %d: Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d,CCid %d)\n",eNB->CC_id,proc->subframe_rx,*subframe,eNB->CC_id);
1061 1062
    }
    if (proc->frame_rx != *frame) {
1063 1064 1065 1066
      if (proc->frame_rx == proc->frame_offset) // This means that the RRU has adjusted its frame timing
	proc->frame_offset = 0;
      else 
	LOG_E(PHY,"rx_fh_if4p5: Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d,CCid %d)\n",proc->frame_rx,*frame,eNB->CC_id);
1067 1068 1069
    }
  } else {
    proc->first_rx = 0;
1070 1071 1072 1073 1074
    if (eNB->CC_id==0)
      proc->frame_offset = 0;
    else
      proc->frame_offset = PHY_vars_eNB_g[0][0]->proc.frame_rx;

1075
    *frame = proc->frame_rx;//(proc->frame_rx + proc->frame_offset)&1023;
1076 1077 1078
    *subframe = proc->subframe_rx;        
  }
  
1079 1080 1081


  if (eNB->CC_id==0) VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
1082 1083 1084
  
}

Raymond Knopp's avatar
Raymond Knopp committed
1085 1086
void rx_fh_slave(PHY_VARS_eNB *eNB,int *frame,int *subframe) {
  // This case is for synchronization to another thread
1087
  // it just waits for an external event.  The actual rx_fh is handle by the asynchronous RX thread
Raymond Knopp's avatar
Raymond Knopp committed
1088 1089
  eNB_proc_t *proc=&eNB->proc;

Raymond Knopp's avatar
Raymond Knopp committed
1090 1091 1092 1093 1094
  if (wait_on_condition(&proc->mutex_FH,&proc->cond_FH,&proc->instance_cnt_FH,"rx_fh_slave") < 0)
    return;

  release_thread(&proc->mutex_FH,&proc->instance_cnt_FH,"rx_fh_slave");

Raymond Knopp's avatar
Raymond Knopp committed
1095 1096 1097 1098
  
}


1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
int wakeup_rxtx(eNB_proc_t *proc,eNB_rxtx_proc_t *proc_rxtx,LTE_DL_FRAME_PARMS *fp) {

  int i;
  struct timespec wait;
  
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

  /* accept some delay in processing - up to 5ms */
  for (i = 0; i < 10 && proc_rxtx->instance_cnt_rxtx == 0; i++) {
    LOG_W( PHY,"[eNB] Frame %d, eNB RXn-TXnp4 thread busy!! (cnt_rxtx %i)\n", proc_rxtx->frame_tx, proc_rxtx->instance_cnt_rxtx);
    usleep(500);
  }
  if (proc_rxtx->instance_cnt_rxtx == 0) {
    exit_fun( "TX thread busy" );
    return(-1);
  }

  // wake up TX for subframe n+4
  // lock the TX mutex and make sure the thread is ready
  if (pthread_mutex_timedlock(&proc_rxtx->mutex_rxtx,&wait) != 0) {
    LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB RXTX thread %d (IC %d)\n", proc_rxtx->subframe_rx&1,proc_rxtx->instance_cnt_rxtx );
    exit_fun( "error locking mutex_rxtx" );
    return(-1);
  }
  
  ++proc_rxtx->instance_cnt_rxtx;
  
  // 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, 
Raymond Knopp's avatar
Raymond Knopp committed
1131 1132
  // we want to generate subframe (n+4), so TS_tx = TX_rx+4*samples_per_tti,
  // and proc->subframe_tx = proc->subframe_rx+4
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  proc_rxtx->timestamp_tx = proc->timestamp_rx + (4*fp->samples_per_tti);
  proc_rxtx->frame_rx     = proc->frame_rx;
  proc_rxtx->subframe_rx  = proc->subframe_rx;
  proc_rxtx->frame_tx     = (proc_rxtx->subframe_rx > 5) ? (proc_rxtx->frame_rx+1)&1023 : proc_rxtx->frame_rx;
  proc_rxtx->subframe_tx  = (proc_rxtx->subframe_rx + 4)%10;
  
  // the thread can now be woken up
  if (pthread_cond_signal(&proc_rxtx->cond_rxtx) != 0) {
    LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB RXn-TXnp4 thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }
  
  pthread_mutex_unlock( &proc_rxtx->mutex_rxtx );

  return(0);
}

void wakeup_slaves(eNB_proc_t *proc) {

  int i;
  struct timespec wait;
  
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
  
  for (i=0;i<proc->num_slaves;i++) {
    eNB_proc_t *slave_proc = proc->slave_proc[i];
    // wake up slave FH thread
    // lock the FH mutex and make sure the thread is ready
    if (pthread_mutex_timedlock(&slave_proc->mutex_FH,&wait) != 0) {
      LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB CCid %d slave CCid %d (IC %d)\n",proc->CC_id,slave_proc->CC_id);
      exit_fun( "error locking mutex_rxtx" );
      break;
    }
    
    int cnt_slave            = ++slave_proc->instance_cnt_FH;
    slave_proc->frame_rx     = proc->frame_rx;
    slave_proc->subframe_rx  = proc->subframe_rx;
    slave_proc->timestamp_rx = proc->timestamp_rx;
1173 1174
    slave_proc->timestamp_tx = proc->timestamp_tx; 

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    pthread_mutex_unlock( &slave_proc->mutex_FH );
    
    if (cnt_slave == 0) {
      // the thread was presumably waiting where it should and can now be woken up
      if (pthread_cond_signal(&slave_proc->cond_FH) != 0) {
	LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB CCid %d, slave CCid %d\n",proc->CC_id,slave_proc->CC_id);
          exit_fun( "ERROR pthread_cond_signal" );
	  break;
      }
    } else {
1185
      LOG_W( PHY,"[eNB] Frame %d, slave CC_id %d thread busy!! (cnt_FH %i)\n",slave_proc->frame_rx,slave_proc->CC_id, cnt_slave);
1186 1187 1188 1189 1190 1191
      exit_fun( "FH thread busy" );
      break;
    }             
  }
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
uint32_t sync_corr[307200] __attribute__((aligned(32)));

// This thread run the initial synchronization like a UE
void *eNB_thread_synch(void *arg) {

  PHY_VARS_eNB *eNB = (PHY_VARS_eNB*)arg;
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  int32_t sync_pos,sync_pos2;
  uint32_t peak_val;

  thread_top_init("eNB_thread_synch",0,5000000,10000000,10000000);

  wait_sync("eNB_thread_synch");

  // initialize variables for PSS detection
  lte_sync_time_init(&eNB->frame_parms);

  while (!oai_exit) {

    // wait to be woken up
    pthread_mutex_lock(&eNB->proc.mutex_synch);
    while (eNB->proc.instance_cnt_synch < 0)
      pthread_cond_wait(&eNB->proc.cond_synch,&eNB->proc.mutex_synch);
    pthread_mutex_unlock(&eNB->proc.mutex_synch);

    // if we're not in synch, then run initial synch
    if (eNB->in_synch == 0) { 
      // run intial synch like UE
      LOG_I(PHY,"Running initial synchronization\n");
      
      sync_pos = lte_sync_time_eNB(eNB->common_vars.rxdata[0],
				   fp,
				   fp->samples_per_tti*5,
				   &peak_val,
				   sync_corr);
      LOG_I(PHY,"eNB synch: %d, val %d\n",sync_pos,peak_val);

      if (sync_pos >= 0) {
	if (sync_pos >= fp->nb_prefix_samples)
	  sync_pos2 = sync_pos - fp->nb_prefix_samples;
	else
	  sync_pos2 = sync_pos + (fp->samples_per_tti*10) - fp->nb_prefix_samples;
	
	if (fp->frame_type == FDD) {
	  
	  // PSS is hypothesized in last symbol of first slot in Frame
	  int sync_pos_slot = (fp->samples_per_tti>>1) - fp->ofdm_symbol_size - fp->nb_prefix_samples;
	  
	  if (sync_pos2 >= sync_pos_slot)
	    eNB->rx_offset = sync_pos2 - sync_pos_slot;
	  else
	    eNB->rx_offset = (fp->samples_per_tti*10) + sync_pos2 - sync_pos_slot;
	}
	else {
	  
	}

	LOG_I(PHY,"Estimated sync_pos %d, peak_val %d => timing offset %d\n",sync_pos,peak_val,eNB->rx_offset);
	
1251 1252
	/*
	if ((peak_val > 300000) && (sync_pos > 0)) {
1253 1254 1255 1256
	//      if (sync_pos++ > 3) {
	write_output("eNB_sync.m","sync",(void*)&sync_corr[0],fp->samples_per_tti*5,1,2);
	write_output("eNB_rx.m","rxs",(void*)eNB->common_vars.rxdata[0][0],fp->samples_per_tti*10,1,1);
	exit(-1);
1257 1258 1259
	}
	*/
	eNB->in_synch=1;
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
      }
    }

    // release thread
    pthread_mutex_lock(&eNB->proc.mutex_synch);
    eNB->proc.instance_cnt_synch--;
    pthread_mutex_unlock(&eNB->proc.mutex_synch);
  } // oai_exit

  lte_sync_time_free();

Raymond.Knopp's avatar
Raymond.Knopp committed
1271
  return NULL;
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
}

int wakeup_synch(PHY_VARS_eNB *eNB){

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

  // wake up synch thread
  // lock the synch mutex and make sure the thread is ready
  if (pthread_mutex_timedlock(&eNB->proc.mutex_synch,&wait) != 0) {
    LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB synch thread (IC %d)\n", eNB->proc.instance_cnt_synch );
    exit_fun( "error locking mutex_synch" );
    return(-1);
  }
  
  ++eNB->proc.instance_cnt_synch;
  
  // the thread can now be woken up
  if (pthread_cond_signal(&eNB->proc.cond_synch) != 0) {
    LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB synch thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }
  
  pthread_mutex_unlock( &eNB->proc.mutex_synch );

  return(0);
}

1303
/*!
1304 1305 1306
 * \brief The Fronthaul thread of RRU/RAU/RCC/eNB
 * In the case of RRU/eNB, handles interface with external RF
 * In the case of RAU/RCC, handles fronthaul interface with RRU/RAU
1307 1308 1309
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
Raymond Knopp's avatar
Raymond Knopp committed
1310

1311
static void* eNB_thread_FH( void* param ) {
1312
  
1313
  static int eNB_thread_FH_status;
1314 1315

  eNB_proc_t *proc = (eNB_proc_t*)param;
1316 1317
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
1318

Sandeep Kumar's avatar
Sandeep Kumar committed
1319
  int subframe=0, frame=0; 
1320

1321
  // set default return value
1322
  eNB_thread_FH_status = 0;
1323

1324
  thread_top_init("eNB_thread_FH",0,870000,1000000,1000000);
1325

Raymond Knopp's avatar
Raymond Knopp committed
1326
  wait_sync("eNB_thread_FH");
1327

1328
#if defined(ENABLE_ITTI) && defined(ENABLE_USE_MME)
Raymond Knopp's avatar
Raymond Knopp committed
1329 1330
  if (eNB->node_function < NGFI_RRU_IF5)
    wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
1331
#endif 
1332

1333 1334 1335
  // Start IF device if any
  if (eNB->start_if) 
    if (eNB->start_if(eNB) != 0)
1336
      LOG_E(HW,"Could not start the IF device\n");
1337

Raymond Knopp's avatar
Raymond Knopp committed
1338 1339 1340 1341 1342
  // Start RF device if any
  if (eNB->start_rf)
    if (eNB->start_rf(eNB) != 0)
      LOG_E(HW,"Could not start the RF device\n");

Raymond Knopp's avatar
Raymond Knopp committed
1343
  // wakeup asnych_rxtx thread because the devices are ready at this point
1344 1345 1346 1347 1348
  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
  proc->instance_cnt_asynch_rxtx=0;
  pthread_mutex_unlock(&proc->mutex_asynch_rxtx);
  pthread_cond_signal(&proc->cond_asynch_rxtx);

1349 1350
  // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
  while (!oai_exit) {
1351

Raymond Knopp's avatar
Raymond Knopp committed
1352 1353
    // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
    // They are set on the first rx/tx in the underly FH routines.
1354 1355 1356 1357 1358 1359 1360 1361
    if (subframe==9) { 
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
    }      

Raymond Knopp's avatar
Raymond Knopp committed
1362 1363 1364 1365
 
    // synchronization on FH interface, acquire signals/data and block
    if (eNB->rx_fh) eNB->rx_fh(eNB,&frame,&subframe);
    else AssertFatal(1==0, "No fronthaul interface : eNB->node_function %d",eNB->node_function);
1366

Cedric Roux's avatar
Cedric Roux committed
1367 1368
    T(T_ENB_MASTER_TICK, T_INT(0), T_INT(proc->frame_rx), T_INT(proc->subframe_rx));

1369 1370
    // At this point, all information for subframe has been received on FH interface
    // If this proc is to provide synchronization, do so
1371
    wakeup_slaves(proc);
1372 1373
      
    // wake up RXn_TXnp4 thread for the subframe
1374
    // choose even or odd thread for RXn-TXnp4 processing 
1375
    if (wakeup_rxtx(proc,&proc->proc_rxtx[proc->subframe_rx&1],fp) < 0)
1376
      break;
1377

Raymond Knopp's avatar
Raymond Knopp committed
1378
    // artifical sleep for very slow fronthaul
1379 1380
    if (eNB->frame_parms.N_RB_DL==6)
      rt_sleep_ns(800000LL);
1381 1382 1383
  }
    
  printf( "Exiting FH thread \n");
Raymond Knopp's avatar
Raymond Knopp committed
1384
 
1385 1386
  eNB_thread_FH_status = 0;
  return &eNB_thread_FH_status;
1387 1388 1389 1390 1391 1392 1393 1394
}


/*!
 * \brief The prach receive thread of eNB.
 * \param param is a \ref eNB_proc_t structure which contains the info what to process.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1395
static void* eNB_thread_prach( void* param ) {
1396 1397 1398 1399
  static int eNB_thread_prach_status;

  eNB_proc_t *proc = (eNB_proc_t*)param;
  PHY_VARS_eNB *eNB= PHY_vars_eNB_g[0][proc->CC_id];
Raymond Knopp's avatar
Raymond Knopp committed
1400

1401 1402 1403
  // set default return value
  eNB_thread_prach_status = 0;

1404
  thread_top_init("eNB_thread_prach",1,500000L,1000000L,20000000L);
1405

1406 1407 1408 1409
  while (!oai_exit) {
    
    if (oai_exit) break;

Raymond Knopp's avatar
Raymond Knopp committed
1410
    if (wait_on_condition(&proc->mutex_prach,&proc->cond_prach,&proc->instance_cnt_prach,"eNB_prach_thread") < 0) break;
Raymond Knopp's avatar
Raymond Knopp committed
1411
    
1412
    prach_procedures(eNB);
1413
    
Raymond Knopp's avatar
Raymond Knopp committed
1414
    if (release_thread(&proc->mutex_prach,&proc->instance_cnt_prach,"eNB_prach_thread") < 0) break;
1415
  }
1416

1417
  printf( "Exiting eNB thread PRACH\n");
Raymond Knopp's avatar
Raymond Knopp committed
1418

1419 1420
  eNB_thread_prach_status = 0;
  return &eNB_thread_prach_status;
1421 1422
}

1423 1424


Raymond Knopp's avatar
Raymond Knopp committed
1425 1426 1427 1428 1429 1430 1431
static void* eNB_thread_single( void* param ) {

  static int eNB_thread_single_status;

  eNB_proc_t *proc = (eNB_proc_t*)param;
  eNB_rxtx_proc_t *proc_rxtx = &proc->proc_rxtx[0];
  PHY_VARS_eNB *eNB = PHY_vars_eNB_g[0][proc->CC_id];
1432 1433 1434
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;

  void *rxp[2],*rxp2[2];
Raymond Knopp's avatar
Raymond Knopp committed
1435 1436 1437

  int subframe=0, frame=0; 

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
  int32_t dummy_rx[fp->nb_antennas_rx][fp->samples_per_tti] __attribute__((aligned(32)));

  int ic;

  int rxs;

  int i;

  // initialize the synchronization buffer to the common_vars.rxdata
  for (int i=0;i<fp->nb_antennas_rx;i++)
    rxp[i] = &eNB->common_vars.rxdata[0][i][0];

Raymond Knopp's avatar
Raymond Knopp committed
1450 1451 1452
  // set default return value
  eNB_thread_single_status = 0;

1453
  thread_top_init("eNB_thread_single",0,870000,1000000,1000000);
Raymond Knopp's avatar
Raymond Knopp committed
1454 1455 1456

  wait_sync("eNB_thread_single");

1457
#if defined(ENABLE_ITTI) && defined(ENABLE_USE_MME)
Raymond Knopp's avatar
Raymond Knopp committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
  if (eNB->node_function < NGFI_RRU_IF5)
    wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
#endif 

  // Start IF device if any
  if (eNB->start_if) 
    if (eNB->start_if(eNB) != 0)
      LOG_E(HW,"Could not start the IF device\n");

  // Start RF device if any
  if (eNB->start_rf)
    if (eNB->start_rf(eNB) != 0)
      LOG_E(HW,"Could not start the RF device\n");

  // wakeup asnych_rxtx thread because the devices are ready at this point
  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
  proc->instance_cnt_asynch_rxtx=0;
  pthread_mutex_unlock(&proc->mutex_asynch_rxtx);
  pthread_cond_signal(&proc->cond_asynch_rxtx);

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499


  // if this is a slave eNB, try to synchronize on the DL frequency
  if ((eNB->is_slave) &&
      ((eNB->node_function >= NGFI_RRU_IF5))) {
    // if FDD, switch RX on DL frequency
    
    double temp_freq1 = eNB->rfdevice.openair0_cfg->rx_freq[0];
    double temp_freq2 = eNB->rfdevice.openair0_cfg->tx_freq[0];
    for (i=0;i<4;i++) {
      eNB->rfdevice.openair0_cfg->rx_freq[i] = eNB->rfdevice.openair0_cfg->tx_freq[i];
      eNB->rfdevice.openair0_cfg->tx_freq[i] = temp_freq1;
    }
    eNB->rfdevice.trx_set_freq_func(&eNB->rfdevice,eNB->rfdevice.openair0_cfg,0);

    while ((eNB->in_synch ==0)&&(!oai_exit)) {
      // read in frame
      rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
					&(proc->timestamp_rx),
					rxp,
					fp->samples_per_tti*10,
					fp->nb_antennas_rx);
Raymond.Knopp's avatar
Raymond.Knopp committed
1500 1501 1502 1503

      if (rxs != (fp->samples_per_tti*10))
	exit_fun("Problem receiving samples\n");

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
      // wakeup synchronization processing thread
      wakeup_synch(eNB);
      ic=0;
      
      while ((ic>=0)&&(!oai_exit)) {
	// continuously read in frames, 1ms at a time, 
	// until we are done with the synchronization procedure
	
	for (i=0; i<fp->nb_antennas_rx; i++)
	  rxp2[i] = (void*)&dummy_rx[i][0];
	for (i=0;i<10;i++)
	  rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
					    &(proc->timestamp_rx),
					    rxp2,
					    fp->samples_per_tti,
					    fp->nb_antennas_rx);
Raymond.Knopp's avatar
Raymond.Knopp committed
1520 1521 1522
	if (rxs != fp->samples_per_tti)
	  exit_fun( "problem receiving samples" );

1523 1524 1525 1526 1527
	pthread_mutex_lock(&eNB->proc.mutex_synch);
	ic = eNB->proc.instance_cnt_synch;
	pthread_mutex_unlock(&eNB->proc.mutex_synch);
      } // ic>=0
    } // in_synch==0
1528 1529 1530 1531 1532 1533 1534
    // read in rx_offset samples
    LOG_I(PHY,"Resynchronizing by %d samples\n",eNB->rx_offset);
    rxs = eNB->rfdevice.trx_read_func(&eNB->rfdevice,
				      &(proc->timestamp_rx),
				      rxp,
				      eNB->rx_offset,
				      fp->nb_antennas_rx);
Raymond.Knopp's avatar
Raymond.Knopp committed
1535 1536 1537
    if (rxs != eNB->rx_offset)
      exit_fun( "problem receiving samples" );

1538 1539
    for (i=0;i<4;i++) {
      eNB->rfdevice.openair0_cfg->rx_freq[i] = temp_freq1;
1540
      eNB->rfdevice.openair0_cfg->tx_freq[i] = temp_freq2;
1541
    }
1542
    eNB->rfdevice.trx_set_freq_func(&eNB->rfdevice,eNB->rfdevice.openair0_cfg,1);
1543 1544 1545
  } // if RRU and slave


Raymond Knopp's avatar
Raymond Knopp committed
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
  // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
  while (!oai_exit) {

    // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
    // They are set on the first rx/tx in the underly FH routines.
    if (subframe==9) { 
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
    }      

Raymond.Knopp's avatar
Raymond.Knopp committed
1559 1560
    if (eNB->CC_id==1) 
	LOG_I(PHY,"eNB thread single %p (proc %p, CC_id %d), frame %d (%p), subframe %d (%p)\n",
luhan wang's avatar
luhan wang committed
1561
	  pthread_self(), proc, eNB->CC_id, frame,&frame,subframe,&subframe);
Raymond Knopp's avatar
Raymond Knopp committed
1562 1563 1564 1565 1566 1567 1568
 
    // synchronization on FH interface, acquire signals/data and block
    if (eNB->rx_fh) eNB->rx_fh(eNB,&frame,&subframe);
    else AssertFatal(1==0, "No fronthaul interface : eNB->node_function %d",eNB->node_function);

    T(T_ENB_MASTER_TICK, T_INT(0), T_INT(proc->frame_rx), T_INT(proc->subframe_rx));

1569 1570 1571
    proc_rxtx->subframe_rx = proc->subframe_rx;
    proc_rxtx->frame_rx    = proc->frame_rx;
    proc_rxtx->subframe_tx = (proc->subframe_rx+4)%10;
1572 1573
    proc_rxtx->frame_tx    = (proc->subframe_rx>5) ? (1+proc->frame_rx)&1023 : proc->frame_rx;
    proc->frame_tx         = proc_rxtx->frame_tx;
1574
    proc_rxtx->timestamp_tx = proc->timestamp_tx;
1575 1576
    // adjust for timing offset between RRU
    if (eNB->CC_id!=0) proc_rxtx->frame_tx = (proc_rxtx->frame_tx+proc->frame_offset)&1023;
1577

Raymond Knopp's avatar
Raymond Knopp committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
    // At this point, all information for subframe has been received on FH interface
    // If this proc is to provide synchronization, do so
    wakeup_slaves(proc);

    if (rxtx(eNB,proc_rxtx,"eNB_thread_single") < 0) break;
  }
  

  printf( "Exiting eNB_single thread \n");
 
  eNB_thread_single_status = 0;
  return &eNB_thread_single_status;

}

1593
extern void init_fep_thread(PHY_VARS_eNB *, pthread_attr_t *);
1594 1595
extern void init_td_thread(PHY_VARS_eNB *, pthread_attr_t *);
extern void init_te_thread(PHY_VARS_eNB *, pthread_attr_t *);
1596

1597
void init_eNB_proc(int inst) {
1598
  
Rohit Gupta's avatar
Rohit Gupta committed
1599
  int i=0;
1600
  int CC_id;
1601 1602
  PHY_VARS_eNB *eNB;
  eNB_proc_t *proc;
1603
  eNB_rxtx_proc_t *proc_rxtx;
1604
  pthread_attr_t *attr0=NULL,*attr1=NULL,*attr_FH=NULL,*attr_prach=NULL,*attr_asynch=NULL,*attr_single=NULL,*attr_fep=NULL,*attr_td=NULL,*attr_te=NULL,*attr_synch=NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1605

1606 1607
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    eNB = PHY_vars_eNB_g[inst][CC_id];
laurent's avatar
laurent committed
1608
#ifndef OCP_FRAMEWORK
1609
    LOG_I(PHY,"Initializing eNB %d CC_id %d (%s,%s),\n",inst,CC_id,eNB_functions[eNB->node_function],eNB_timing[eNB->node_timing]);
laurent's avatar
laurent committed
1610
#endif
1611
    proc = &eNB->proc;
Raymond Knopp's avatar
Raymond Knopp committed
1612

1613 1614 1615
    proc_rxtx = proc->proc_rxtx;
    proc_rxtx[0].instance_cnt_rxtx = -1;
    proc_rxtx[1].instance_cnt_rxtx = -1;
1616 1617
    proc->instance_cnt_prach       = -1;
    proc->instance_cnt_FH          = -1;
1618
    proc->instance_cnt_asynch_rxtx = -1;
1619
    proc->CC_id = CC_id;    
1620 1621
    proc->instance_cnt_synch        =  -1;

Raymond Knopp's avatar
Raymond Knopp committed
1622 1623
    proc->first_rx=1;
    proc->first_tx=1;
1624
    proc->frame_offset = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1625

1626 1627
    for (i=0;i<10;i++) proc->symbol_mask[i]=0;

1628 1629 1630 1631
    pthread_mutex_init( &proc_rxtx[0].mutex_rxtx, NULL);
    pthread_mutex_init( &proc_rxtx[1].mutex_rxtx, NULL);
    pthread_cond_init( &proc_rxtx[0].cond_rxtx, NULL);
    pthread_cond_init( &proc_rxtx[1].cond_rxtx, NULL);
Raymond Knopp's avatar
Raymond Knopp committed
1632 1633 1634

    pthread_mutex_init( &proc->mutex_prach, NULL);
    pthread_mutex_init( &proc->mutex_asynch_rxtx, NULL);
1635
    pthread_mutex_init( &proc->mutex_synch,NULL);
Raymond Knopp's avatar
Raymond Knopp committed
1636

1637 1638
    pthread_cond_init( &proc->cond_prach, NULL);
    pthread_cond_init( &proc->cond_FH, NULL);
1639
    pthread_cond_init( &proc->cond_asynch_rxtx, NULL);
1640
    pthread_cond_init( &proc->cond_synch,NULL);
1641 1642 1643

    pthread_attr_init( &proc->attr_FH);
    pthread_attr_init( &proc->attr_prach);
1644
    pthread_attr_init( &proc->attr_synch);
1645 1646 1647
    pthread_attr_init( &proc->attr_asynch_rxtx);
    pthread_attr_init( &proc->attr_single);
    pthread_attr_init( &proc->attr_fep);
1648 1649
    pthread_attr_init( &proc->attr_td);
    pthread_attr_init( &proc->attr_te);
1650 1651
    pthread_attr_init( &proc_rxtx[0].attr_rxtx);
    pthread_attr_init( &proc_rxtx[1].attr_rxtx);
1652
#ifndef DEADLINE_SCHEDULER
Raymond Knopp's avatar
Raymond Knopp committed
1653 1654 1655 1656
    attr0       = &proc_rxtx[0].attr_rxtx;
    attr1       = &proc_rxtx[1].attr_rxtx;
    attr_FH     = &proc->attr_FH;
    attr_prach  = &proc->attr_prach;
1657
    attr_synch  = &proc->attr_synch;
Raymond Knopp's avatar
Raymond Knopp committed
1658 1659
    attr_asynch = &proc->attr_asynch_rxtx;
    attr_single = &proc->attr_single;
1660
    attr_fep    = &proc->attr_fep;
1661 1662
    attr_td     = &proc->attr_td;
    attr_te     = &proc->attr_te; 
Raymond Knopp's avatar
Raymond Knopp committed
1663 1664 1665 1666 1667 1668 1669
#endif

    if (eNB->single_thread_flag==0) {
      pthread_create( &proc_rxtx[0].pthread_rxtx, attr0, eNB_thread_rxtx, &proc_rxtx[0] );
      pthread_create( &proc_rxtx[1].pthread_rxtx, attr1, eNB_thread_rxtx, &proc_rxtx[1] );
      pthread_create( &proc->pthread_FH, attr_FH, eNB_thread_FH, &eNB->proc );
    }
1670
    else {
Raymond Knopp's avatar
Raymond Knopp committed
1671
      pthread_create(&proc->pthread_single, attr_single, eNB_thread_single, &eNB->proc);
1672
      init_fep_thread(eNB,attr_fep);
1673 1674
      init_td_thread(eNB,attr_td);
      init_te_thread(eNB,attr_te);
1675
    }
Raymond Knopp's avatar
Raymond Knopp committed
1676
    pthread_create( &proc->pthread_prach, attr_prach, eNB_thread_prach, &eNB->proc );
1677
    pthread_create( &proc->pthread_synch, attr_synch, eNB_thread_synch, eNB);
1678
    if ((eNB->node_timing == synch_to_other) ||
Raymond Knopp's avatar
Raymond Knopp committed
1679 1680
	(eNB->node_function == NGFI_RRU_IF5) ||
	(eNB->node_function == NGFI_RRU_IF4p5))
1681 1682


Raymond Knopp's avatar
Raymond Knopp committed
1683 1684
      pthread_create( &proc->pthread_asynch_rxtx, attr_asynch, eNB_thread_asynch_rxtx, &eNB->proc );

1685
    char name[16];
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
    if (eNB->single_thread_flag == 0) {
      snprintf( name, sizeof(name), "RXTX0 %d", i );
      pthread_setname_np( proc_rxtx[0].pthread_rxtx, name );
      snprintf( name, sizeof(name), "RXTX1 %d", i );
      pthread_setname_np( proc_rxtx[1].pthread_rxtx, name );
      snprintf( name, sizeof(name), "FH %d", i );
      pthread_setname_np( proc->pthread_FH, name );
    }
    else {
      snprintf( name, sizeof(name), " %d", i );
      pthread_setname_np( proc->pthread_single, name );
    }
1698
  }
1699

1700
  //for multiple CCs: setup master and slaves
luhan wang's avatar
luhan wang committed
1701
  /*
1702
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1703 1704
    eNB = PHY_vars_eNB_g[inst][CC_id];

1705
    if (eNB->node_timing == synch_to_ext_device) { //master
1706 1707 1708 1709
      eNB->proc.num_slaves = MAX_NUM_CCs-1;
      eNB->proc.slave_proc = (eNB_proc_t**)malloc(eNB->proc.num_slaves*sizeof(eNB_proc_t*));

      for (i=0; i< eNB->proc.num_slaves; i++) {
1710 1711
        if (i < CC_id)  eNB->proc.slave_proc[i] = &(PHY_vars_eNB_g[inst][i]->proc);
        if (i >= CC_id)  eNB->proc.slave_proc[i] = &(PHY_vars_eNB_g[inst][i+1]->proc);
1712 1713
      }
    }
luhan wang's avatar
luhan wang committed
1714
    }*/
1715 1716


1717 1718 1719 1720
  /* 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;
1721 1722
}

1723

1724

1725 1726 1727
/*!
 * \brief Terminate eNB TX and RX threads.
 */
1728
void kill_eNB_proc(int inst) {
1729

1730
  int *status;
1731 1732
  PHY_VARS_eNB *eNB;
  eNB_proc_t *proc;
1733
  eNB_rxtx_proc_t *proc_rxtx;
1734
  for (int CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1735
    eNB=PHY_vars_eNB_g[inst][CC_id];
1736 1737
    
    proc = &eNB->proc;
1738
    proc_rxtx = &proc->proc_rxtx[0];
1739
    
1740
#ifdef DEBUG_THREADS
1741
    printf( "Killing TX CC_id %d thread %d\n", CC_id, i );
1742
#endif
1743
    
1744 1745
    proc_rxtx[0].instance_cnt_rxtx = 0; // FIXME data race!
    proc_rxtx[1].instance_cnt_rxtx = 0; // FIXME data race!
Raymond Knopp's avatar
Raymond Knopp committed
1746
    proc->instance_cnt_prach = 0;
1747
    proc->instance_cnt_FH = 0;
1748
    pthread_cond_signal( &proc_rxtx[0].cond_rxtx );    
Raymond Knopp's avatar
Raymond Knopp committed
1749 1750
    pthread_cond_signal( &proc_rxtx[1].cond_rxtx );
    pthread_cond_signal( &proc->cond_prach );
1751
    pthread_cond_signal( &proc->cond_FH );
1752
    pthread_cond_broadcast(&sync_phy_proc.cond_phy_proc_tx);
Raymond Knopp's avatar
Raymond Knopp committed
1753

1754 1755 1756 1757 1758 1759 1760
    pthread_join( proc->pthread_FH, (void**)&status ); 
    pthread_mutex_destroy( &proc->mutex_FH );
    pthread_cond_destroy( &proc->cond_FH );
            
    pthread_join( proc->pthread_prach, (void**)&status );    
    pthread_mutex_destroy( &proc->mutex_prach );
    pthread_cond_destroy( &proc->cond_prach );         
1761

1762
    int i;
Raymond Knopp's avatar
Raymond Knopp committed
1763
    for (i=0;i<2;i++) {
1764 1765 1766 1767
      pthread_join( proc_rxtx[i].pthread_rxtx, (void**)&status );
      pthread_mutex_destroy( &proc_rxtx[i].mutex_rxtx );
      pthread_cond_destroy( &proc_rxtx[i].cond_rxtx );
    }
1768
  }
1769 1770
}

1771

1772 1773 1774 1775
/* this function maps the phy_vars_eNB tx and rx buffers to the available rf chains.
   Each rf chain is is addressed by the card number and the chain on the card. The
   rf_map specifies for each CC, on which rf chain the mapping should start. Multiple
   antennas are mapped to successive RF chains on the same card. */
1776
int setup_eNB_buffers(PHY_VARS_eNB **phy_vars_eNB, openair0_config_t *openair0_cfg) {
1777

1778 1779
  int i,j; 
  int CC_id,card,ant;
1780

1781
  //uint16_t N_TA_offset = 0;
1782

1783 1784 1785 1786
  LTE_DL_FRAME_PARMS *frame_parms;

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    if (phy_vars_eNB[CC_id]) {
1787
      frame_parms = &(phy_vars_eNB[CC_id]->frame_parms);
1788 1789 1790 1791 1792 1793
      printf("setup_eNB_buffers: frame_parms = %p\n",frame_parms);
    } else {
      printf("phy_vars_eNB[%d] not initialized\n", CC_id);
      return(-1);
    }

1794
    /*
1795 1796 1797 1798 1799 1800 1801 1802
    if (frame_parms->frame_type == TDD) {
      if (frame_parms->N_RB_DL == 100)
        N_TA_offset = 624;
      else if (frame_parms->N_RB_DL == 50)
        N_TA_offset = 624/2;
      else if (frame_parms->N_RB_DL == 25)
        N_TA_offset = 624/4;
    }
1803
    */
Raymond Knopp's avatar
Raymond Knopp committed
1804
 
1805

Raymond Knopp's avatar
Raymond Knopp committed
1806
    if (openair0_cfg[CC_id].mmapped_dma == 1) {
1807
    // replace RX signal buffers with mmaped HW versions
Raymond Knopp's avatar
Raymond Knopp committed
1808 1809
      
      for (i=0; i<frame_parms->nb_antennas_rx; i++) {
1810 1811 1812
	card = i/4;
	ant = i%4;
	printf("Mapping eNB CC_id %d, rx_ant %d, on card %d, chain %d\n",CC_id,i,phy_vars_eNB[CC_id]->rf_map.card+card, phy_vars_eNB[CC_id]->rf_map.chain+ant);
Raymond Knopp's avatar
Raymond Knopp committed
1813
	free(phy_vars_eNB[CC_id]->common_vars.rxdata[0][i]);
1814
	phy_vars_eNB[CC_id]->common_vars.rxdata[0][i] = openair0_cfg[phy_vars_eNB[CC_id]->rf_map.card+card].rxbase[phy_vars_eNB[CC_id]->rf_map.chain+ant];
Raymond Knopp's avatar
Raymond Knopp committed
1815 1816 1817 1818 1819 1820
	
	printf("rxdata[%d] @ %p\n",i,phy_vars_eNB[CC_id]->common_vars.rxdata[0][i]);
	for (j=0; j<16; j++) {
	  printf("rxbuffer %d: %x\n",j,phy_vars_eNB[CC_id]->common_vars.rxdata[0][i][j]);
	  phy_vars_eNB[CC_id]->common_vars.rxdata[0][i][j] = 16-j;
	}
1821
      }
Raymond Knopp's avatar
Raymond Knopp committed
1822 1823
      
      for (i=0; i<frame_parms->nb_antennas_tx; i++) {
1824 1825 1826
	card = i/4;
	ant = i%4;
	printf("Mapping eNB CC_id %d, tx_ant %d, on card %d, chain %d\n",CC_id,i,phy_vars_eNB[CC_id]->rf_map.card+card, phy_vars_eNB[CC_id]->rf_map.chain+ant);
Raymond Knopp's avatar
Raymond Knopp committed
1827
	free(phy_vars_eNB[CC_id]->common_vars.txdata[0][i]);
1828
	phy_vars_eNB[CC_id]->common_vars.txdata[0][i] = openair0_cfg[phy_vars_eNB[CC_id]->rf_map.card+card].txbase[phy_vars_eNB[CC_id]->rf_map.chain+ant];
Raymond Knopp's avatar
Raymond Knopp committed
1829 1830 1831 1832 1833 1834 1835
	
	printf("txdata[%d] @ %p\n",i,phy_vars_eNB[CC_id]->common_vars.txdata[0][i]);
	
	for (j=0; j<16; j++) {
	  printf("txbuffer %d: %x\n",j,phy_vars_eNB[CC_id]->common_vars.txdata[0][i][j]);
	  phy_vars_eNB[CC_id]->common_vars.txdata[0][i][j] = 16-j;
	}
1836 1837
      }
    }
Raymond Knopp's avatar
Raymond Knopp committed
1838
    else {  // not memory-mapped DMA 
1839 1840
      //nothing to do, everything already allocated in lte_init
      /*
Raymond Knopp's avatar
Raymond Knopp committed
1841 1842 1843 1844 1845
      rxdata = (int32_t**)malloc16(frame_parms->nb_antennas_rx*sizeof(int32_t*));
      txdata = (int32_t**)malloc16(frame_parms->nb_antennas_tx*sizeof(int32_t*));
      
      for (i=0; i<frame_parms->nb_antennas_rx; i++) {
	free(phy_vars_eNB[CC_id]->common_vars.rxdata[0][i]);
1846
	rxdata[i] = (int32_t*)(32 + malloc16(32+frame_parms->samples_per_tti*10*sizeof(int32_t))); // FIXME broken memory allocation
1847
	phy_vars_eNB[CC_id]->common_vars.rxdata[0][i] = rxdata[i]; //-N_TA_offset; // N_TA offset for TDD         FIXME! N_TA_offset > 16 => access of unallocated memory
1848
	memset(rxdata[i], 0, frame_parms->samples_per_tti*10*sizeof(int32_t));
Raymond Knopp's avatar
Raymond Knopp committed
1849
	printf("rxdata[%d] @ %p (%p) (N_TA_OFFSET %d)\n", i, phy_vars_eNB[CC_id]->common_vars.rxdata[0][i],rxdata[i],N_TA_offset);      
1850 1851
      }
      
Raymond Knopp's avatar
Raymond Knopp committed
1852 1853
      for (i=0; i<frame_parms->nb_antennas_tx; i++) {
	free(phy_vars_eNB[CC_id]->common_vars.txdata[0][i]);
1854
	txdata[i] = (int32_t*)(32 + malloc16(32 + frame_parms->samples_per_tti*10*sizeof(int32_t))); // FIXME broken memory allocation
Raymond Knopp's avatar
Raymond Knopp committed
1855
	phy_vars_eNB[CC_id]->common_vars.txdata[0][i] = txdata[i];
1856
	memset(txdata[i],0, frame_parms->samples_per_tti*10*sizeof(int32_t));
Raymond Knopp's avatar
Raymond Knopp committed
1857
	printf("txdata[%d] @ %p\n", i, phy_vars_eNB[CC_id]->common_vars.txdata[0][i]);
1858
      }
1859
      */
1860 1861 1862 1863 1864 1865 1866 1867
    }
  }

  return(0);
}


void reset_opp_meas(void) {
1868

1869 1870 1871 1872 1873
  int sfn;
  reset_meas(&softmodem_stats_mt);
  reset_meas(&softmodem_stats_hw);
  
  for (sfn=0; sfn < 10; sfn++) {
1874
    reset_meas(&softmodem_stats_rxtx_sf);
1875
    reset_meas(&softmodem_stats_rx_sf);
1876 1877 1878
  }
}

1879

1880 1881 1882 1883 1884 1885 1886
void print_opp_meas(void) {

  int sfn=0;
  print_meas(&softmodem_stats_mt, "Main ENB Thread", NULL, NULL);
  print_meas(&softmodem_stats_hw, "HW Acquisation", NULL, NULL);
  
  for (sfn=0; sfn < 10; sfn++) {
1887
    print_meas(&softmodem_stats_rxtx_sf,"[eNB][total_phy_proc_rxtx]",NULL, NULL);
1888
    print_meas(&softmodem_stats_rx_sf,"[eNB][total_phy_proc_rx]",NULL,NULL);
1889 1890
  }
}
1891
 
1892 1893 1894 1895 1896 1897 1898
int start_if(PHY_VARS_eNB *eNB) {
  return(eNB->ifdevice.trx_start_func(&eNB->ifdevice));
}

int start_rf(PHY_VARS_eNB *eNB) {
  return(eNB->rfdevice.trx_start_func(&eNB->rfdevice));
}
1899

Raymond.Knopp's avatar
Raymond.Knopp committed
1900
extern void eNB_fep_rru_if5(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc);
1901 1902
extern void eNB_fep_full(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc);
extern void eNB_fep_full_2thread(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc);
1903
extern void do_prach(PHY_VARS_eNB *eNB,int frame,int subframe);
1904

1905
void init_eNB(eNB_func_t node_function[], eNB_timing_t node_timing[],int nb_inst,eth_params_t *eth_params,int single_thread_flag,int wait_for_sync) {
1906
  
1907
  int CC_id;
1908
  int inst;
1909
  PHY_VARS_eNB *eNB;
Raymond Knopp's avatar
Raymond Knopp committed
1910 1911
  int ret;

1912 1913
  for (inst=0;inst<nb_inst;inst++) {
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
1914 1915 1916 1917
      eNB = PHY_vars_eNB_g[inst][CC_id]; 
      eNB->node_function      = node_function[CC_id];
      eNB->node_timing        = node_timing[CC_id];
      eNB->abstraction_flag   = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1918
      eNB->single_thread_flag = single_thread_flag;
Raymond Knopp's avatar
Raymond Knopp committed
1919
      eNB->ts_offset          = 0;
1920 1921 1922
      eNB->in_synch           = 0;
      eNB->is_slave           = (wait_for_sync>0) ? 1 : 0;

1923

laurent's avatar
laurent committed
1924
#ifndef OCP_FRAMEWORK
1925
      LOG_I(PHY,"Initializing eNB %d CC_id %d : (%s,%s)\n",inst,CC_id,eNB_functions[node_function[CC_id]],eNB_timing[node_timing[CC_id]]);
laurent's avatar
laurent committed
1926
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1927

1928 1929
      switch (node_function[CC_id]) {
      case NGFI_RRU_IF5:
Raymond Knopp's avatar
Raymond Knopp committed
1930
	eNB->do_prach             = NULL;
1931
	eNB->do_precoding         = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1932
	eNB->fep                  = eNB_fep_rru_if5;
1933 1934
	eNB->td                   = NULL;
	eNB->te                   = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1935
	eNB->proc_uespec_rx       = NULL;
1936
	eNB->proc_tx              = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1937 1938 1939 1940
	eNB->tx_fh                = NULL;
	eNB->rx_fh                = rx_rf;
	eNB->start_rf             = start_rf;
	eNB->start_if             = start_if;
Raymond Knopp's avatar
Raymond Knopp committed
1941
	eNB->fh_asynch            = fh_if5_asynch_DL;
1942
	ret = openair0_device_load(&eNB->rfdevice, &openair0_cfg[CC_id]);
Raymond Knopp's avatar
Raymond Knopp committed
1943 1944 1945 1946 1947 1948
        if (ret<0) {
          printf("Exiting, cannot initialize rf device\n");
          exit(-1);
        }
	eNB->rfdevice.host_type   = RRH_HOST;
	eNB->ifdevice.host_type   = RRH_HOST;
1949
        ret = openair0_transport_load(&eNB->ifdevice, &openair0_cfg[CC_id], (eth_params+CC_id));
Raymond Knopp's avatar
Raymond Knopp committed
1950 1951 1952 1953 1954
	printf("openair0_transport_init returns %d for CC_id %d\n",ret,CC_id);
        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }
1955 1956
	break;
      case NGFI_RRU_IF4p5:
1957
	eNB->do_precoding         = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1958
	eNB->do_prach             = do_prach;
hutch's avatar
hutch committed
1959
	eNB->fep                  = eNB_fep_full;//(single_thread_flag==1) ? eNB_fep_full_2thread : eNB_fep_full;
1960 1961
	eNB->td                   = NULL;
	eNB->te                   = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1962
	eNB->proc_uespec_rx       = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1963
	eNB->proc_tx              = NULL;//proc_tx_rru_if4p5;
Raymond Knopp's avatar
Raymond Knopp committed
1964 1965
	eNB->tx_fh                = NULL;
	eNB->rx_fh                = rx_rf;
Raymond Knopp's avatar
Raymond Knopp committed
1966
	eNB->fh_asynch            = fh_if4p5_asynch_DL;
Raymond Knopp's avatar
Raymond Knopp committed
1967 1968
	eNB->start_rf             = start_rf;
	eNB->start_if             = start_if;
1969
	ret = openair0_device_load(&eNB->rfdevice, &openair0_cfg[CC_id]);
Raymond Knopp's avatar
Raymond Knopp committed
1970 1971 1972 1973 1974 1975
        if (ret<0) {
          printf("Exiting, cannot initialize rf device\n");
          exit(-1);
        }
	eNB->rfdevice.host_type   = RRH_HOST;
	eNB->ifdevice.host_type   = RRH_HOST;
1976
	printf("loading transport interface ...\n");
1977
        ret = openair0_transport_load(&eNB->ifdevice, &openair0_cfg[CC_id], (eth_params+CC_id));
Raymond Knopp's avatar
Raymond Knopp committed
1978 1979 1980 1981 1982 1983
	printf("openair0_transport_init returns %d for CC_id %d\n",ret,CC_id);
        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }

1984
	malloc_IF4p5_buffer(eNB);
Raymond Knopp's avatar
Raymond Knopp committed
1985

1986 1987
	break;
      case eNodeB_3GPP:
Raymond.Knopp's avatar
Raymond.Knopp committed
1988
	eNB->do_precoding         = (eNB->frame_parms.nb_antennas_tx==1) ? 0 : 1;
Raymond Knopp's avatar
Raymond Knopp committed
1989
	eNB->do_prach             = do_prach;
hutch's avatar
hutch committed
1990
	eNB->fep                  = eNB_fep_full;//(single_thread_flag==1) ? eNB_fep_full_2thread : eNB_fep_full;
1991 1992
	eNB->td                   = ulsch_decoding_data;//(single_thread_flag==1) ? ulsch_decoding_data_2thread : ulsch_decoding_data;
	eNB->te                   = dlsch_encoding;//(single_thread_flag==1) ? dlsch_encoding_2threads : dlsch_encoding;
Raymond Knopp's avatar
Raymond Knopp committed
1993 1994 1995 1996 1997 1998
	eNB->proc_uespec_rx       = phy_procedures_eNB_uespec_RX;
	eNB->proc_tx              = proc_tx_full;
	eNB->tx_fh                = NULL;
	eNB->rx_fh                = rx_rf;
	eNB->start_rf             = start_rf;
	eNB->start_if             = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
1999
        eNB->fh_asynch            = NULL;
2000
	ret = openair0_device_load(&eNB->rfdevice, &openair0_cfg[CC_id]);
Raymond Knopp's avatar
Raymond Knopp committed
2001 2002 2003 2004 2005 2006
        if (ret<0) {
          printf("Exiting, cannot initialize rf device\n");
          exit(-1);
        }
	eNB->rfdevice.host_type   = BBU_HOST;
	eNB->ifdevice.host_type   = BBU_HOST;
2007 2008
	break;
      case eNodeB_3GPP_BBU:
Raymond.Knopp's avatar
Raymond.Knopp committed
2009
	eNB->do_precoding         = (eNB->frame_parms.nb_antennas_tx==1) ? 0 : 1;
2010 2011 2012 2013 2014 2015
	eNB->do_prach             = do_prach;
	eNB->fep                  = eNB_fep_full;//(single_thread_flag==1) ? eNB_fep_full_2thread : eNB_fep_full;
	eNB->td                   = ulsch_decoding_data;//(single_thread_flag==1) ? ulsch_decoding_data_2thread : ulsch_decoding_data;
	eNB->te                   = dlsch_encoding;//(single_thread_flag==1) ? dlsch_encoding_2threads : dlsch_encoding;
	eNB->proc_uespec_rx       = phy_procedures_eNB_uespec_RX;
	eNB->proc_tx              = proc_tx_full;
2016
        if (eNB->node_timing == synch_to_other) {
2017 2018 2019
           eNB->tx_fh             = tx_fh_if5_mobipass;
           eNB->rx_fh             = rx_fh_slave;
           eNB->fh_asynch         = fh_if5_asynch_UL;
2020 2021 2022

        }
        else {
2023 2024 2025
           eNB->tx_fh             = tx_fh_if5;
           eNB->rx_fh             = rx_fh_if5;
           eNB->fh_asynch         = NULL;
2026
        }
Raymond Knopp's avatar
Raymond Knopp committed
2027

2028 2029
	eNB->start_rf             = NULL;
	eNB->start_if             = start_if;
Raymond Knopp's avatar
Raymond Knopp committed
2030 2031 2032
	eNB->rfdevice.host_type   = BBU_HOST;

	eNB->ifdevice.host_type   = BBU_HOST;
Raymond Knopp's avatar
Raymond Knopp committed
2033

2034
        ret = openair0_transport_load(&eNB->ifdevice, &openair0_cfg[CC_id], (eth_params+CC_id));
Raymond Knopp's avatar
Raymond Knopp committed
2035 2036 2037 2038 2039
        printf("openair0_transport_init returns %d for CC_id %d\n",ret,CC_id);
        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }
2040 2041
	break;
      case NGFI_RCC_IF4p5:
2042
	eNB->do_precoding         = 0;
Raymond Knopp's avatar
Raymond Knopp committed
2043 2044
	eNB->do_prach             = do_prach;
	eNB->fep                  = NULL;
hutch's avatar
hutch committed
2045 2046
	eNB->td                   = ulsch_decoding_data;//(single_thread_flag==1) ? ulsch_decoding_data_2thread : ulsch_decoding_data;
	eNB->te                   = dlsch_encoding;//(single_thread_flag==1) ? dlsch_encoding_2threads : dlsch_encoding;
Raymond Knopp's avatar
Raymond Knopp committed
2047 2048 2049 2050 2051 2052 2053
	eNB->proc_uespec_rx       = phy_procedures_eNB_uespec_RX;
	eNB->proc_tx              = proc_tx_high;
	eNB->tx_fh                = tx_fh_if4p5;
	eNB->rx_fh                = rx_fh_if4p5;
	eNB->start_rf             = NULL;
	eNB->start_if             = start_if;
        eNB->fh_asynch            = (eNB->node_timing == synch_to_other) ? fh_if4p5_asynch_UL : NULL;
Raymond Knopp's avatar
Raymond Knopp committed
2054 2055
	eNB->rfdevice.host_type   = BBU_HOST;
	eNB->ifdevice.host_type   = BBU_HOST;
2056
        ret = openair0_transport_load(&eNB->ifdevice, &openair0_cfg[CC_id], (eth_params+CC_id));
Raymond Knopp's avatar
Raymond Knopp committed
2057 2058 2059 2060 2061
        printf("openair0_transport_init returns %d for CC_id %d\n",ret,CC_id);
        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }
2062
	malloc_IF4p5_buffer(eNB);
Raymond Knopp's avatar
Raymond Knopp committed
2063

2064 2065
	break;
      case NGFI_RAU_IF4p5:
2066
	eNB->do_precoding   = 0;
2067
	eNB->do_prach       = do_prach;
2068
	eNB->fep            = NULL;
2069

hutch's avatar
hutch committed
2070 2071
	eNB->td             = ulsch_decoding_data;//(single_thread_flag==1) ? ulsch_decoding_data_2thread : ulsch_decoding_data;
	eNB->te             = dlsch_encoding;//(single_thread_flag==1) ? dlsch_encoding_2threads : dlsch_encoding;
2072
	eNB->proc_uespec_rx = phy_procedures_eNB_uespec_RX;
2073
	eNB->proc_tx        = proc_tx_high;
2074 2075
	eNB->tx_fh          = tx_fh_if4p5; 
	eNB->rx_fh          = rx_fh_if4p5; 
Raymond Knopp's avatar
Raymond Knopp committed
2076
        eNB->fh_asynch      = (eNB->node_timing == synch_to_other) ? fh_if4p5_asynch_UL : NULL;
2077 2078
	eNB->start_rf       = NULL;
	eNB->start_if       = start_if;
Raymond Knopp's avatar
Raymond Knopp committed
2079 2080 2081

	eNB->rfdevice.host_type   = BBU_HOST;
	eNB->ifdevice.host_type   = BBU_HOST;
2082
        ret = openair0_transport_load(&eNB->ifdevice, &openair0_cfg[CC_id], (eth_params+CC_id));
Raymond Knopp's avatar
Raymond Knopp committed
2083 2084 2085 2086 2087
        printf("openair0_transport_init returns %d for CC_id %d\n",ret,CC_id);
        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }
2088
	break;	
Raymond Knopp's avatar
Raymond Knopp committed
2089 2090
	malloc_IF4p5_buffer(eNB);

2091
      }
2092
    }
2093

Florian Kaltenberger's avatar
Florian Kaltenberger committed
2094
    if (setup_eNB_buffers(PHY_vars_eNB_g[inst],&openair0_cfg[0])!=0) {
2095 2096 2097 2098
      printf("Exiting, cannot initialize eNodeB Buffers\n");
      exit(-1);
    }

2099
    init_eNB_proc(inst);
2100
  }
2101

2102 2103 2104
  sleep(1);
  LOG_D(HW,"[lte-softmodem.c] eNB threads created\n");
  
Raymond Knopp's avatar
Raymond Knopp committed
2105

2106 2107 2108
}


2109
void stop_eNB(int nb_inst) {
2110

2111 2112 2113 2114
  for (int inst=0;inst<nb_inst;inst++) {
    printf("Killing eNB %d processing threads\n",inst);
    kill_eNB_proc(inst);
  }
2115
}