lte-ru.c 111 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.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
 */
Raymond Knopp's avatar
Raymond Knopp committed
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
/*! \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"

54
#include "PHY/defs_common.h"
Raymond Knopp's avatar
Raymond Knopp committed
55 56 57 58
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all


#include "../../ARCH/COMMON/common_lib.h"
59
#include "../../ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h"
Raymond Knopp's avatar
Raymond Knopp committed
60 61 62 63

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

64 65
#include "PHY/phy_extern.h"
#include "LAYER2/MAC/mac_extern.h"
66 67 68 69
#include "PHY/LTE_TRANSPORT/transport_proto.h"
#include "SCHED/sched_eNB.h"
#include "PHY/LTE_ESTIMATION/lte_estimation.h"
#include "PHY/INIT/phy_init.h"
Raymond Knopp's avatar
Raymond Knopp committed
70

71 72 73 74 75
#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"
Raymond Knopp's avatar
Raymond Knopp committed
76

77
#include "common/utils/LOG/log.h"
Raymond Knopp's avatar
Raymond Knopp committed
78 79 80
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
81
#include "common/utils/LOG/vcd_signal_dumper.h"
Raymond Knopp's avatar
Raymond Knopp committed
82 83
#include "UTIL/OPT/opt.h"
#include "enb_config.h"
84
#include "targets/RT/USER/lte-softmodem.h"
Raymond Knopp's avatar
Raymond Knopp committed
85 86
//#include "PHY/TOOLS/time_meas.h"

Cedric Roux's avatar
Cedric Roux committed
87 88 89 90
/* these variables have to be defined before including ENB_APP/enb_paramdef.h */
static int DEFBANDS[] = {7};
static int DEFENBS[] = {0};

91 92 93
#include "ENB_APP/enb_paramdef.h"
#include "common/config/config_userapi.h"

Raymond Knopp's avatar
Raymond Knopp committed
94
#ifndef OPENAIR2
95
  #include "UTIL/OTG/otg_extern.h"
Raymond Knopp's avatar
Raymond Knopp committed
96 97
#endif

98 99 100 101
#include "s1ap_eNB.h"
#include "SIMULATION/ETH_TRANSPORT/proto.h"


Raymond Knopp's avatar
Raymond Knopp committed
102 103 104

#include "T.h"

Wang Tsu-Han's avatar
Wang Tsu-Han committed
105
#include "pdcp.h"
Raymond Knopp's avatar
Raymond Knopp committed
106 107

extern volatile int                    oai_exit;
108
extern int emulate_rf;
109
extern int numerology;
110
extern clock_source_t clock_source;
111
extern uint8_t dlsch_ue_select_tbl_in_use;
112
extern uint8_t nfapi_mode;
Raymond Knopp's avatar
Raymond Knopp committed
113

114 115
extern PARALLEL_CONF_t get_thread_parallel_conf(void);
extern WORKER_CONF_t   get_thread_worker_conf(void);
116 117
extern void  phy_init_RU(RU_t *);
extern void  phy_free_RU(RU_t *);
118

119

Robert Schmidt's avatar
Robert Schmidt committed
120
void stop_RU(int nb_ru);
121
void do_ru_sync(RU_t *ru);
Raymond Knopp's avatar
Raymond Knopp committed
122

123
void configure_ru(int idx,
124
                  void *arg);
125 126

void configure_rru(int idx,
127
                   void *arg);
128 129

int attach_rru(RU_t *ru);
Raymond Knopp's avatar
Raymond Knopp committed
130

131
int connect_rau(RU_t *ru);
Raymond Knopp's avatar
Raymond Knopp committed
132

133
extern uint16_t sf_ahead;
134

135
#if defined(PRE_SCD_THREAD)
136
  void init_ru_vnf(void);
137 138 139
#endif


140 141 142
/*************************************************************/
/* Functions to attach and configure RRU                     */

Raymond Knopp's avatar
Raymond Knopp committed
143
extern void wait_eNBs(void);
144

145 146 147 148
int attach_rru(RU_t *ru) {
  ssize_t      msg_len,len;
  RRU_CONFIG_msg_t rru_config_msg;
  int received_capabilities=0;
Raymond Knopp's avatar
Raymond Knopp committed
149
  wait_eNBs();
150

151 152
  // Wait for capabilities
  while (received_capabilities==0) {
153 154
    memset((void *)&rru_config_msg,0,sizeof(rru_config_msg));
    rru_config_msg.type = RAU_tick;
155 156 157
    rru_config_msg.len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE;
    LOG_I(PHY,"Sending RAU tick to RRU %d\n",ru->idx);
    AssertFatal((ru->ifdevice.trx_ctlsend_func(&ru->ifdevice,&rru_config_msg,rru_config_msg.len)!=-1),
158
                "RU %d cannot access remote radio\n",ru->idx);
159 160
    msg_len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE+sizeof(RRU_capabilities_t);

161
    // wait for answer with timeout
162
    if ((len = ru->ifdevice.trx_ctlrecv_func(&ru->ifdevice,
163 164 165 166
               &rru_config_msg,
               msg_len))<0) {
      LOG_I(PHY,"Waiting for RRU %d\n",ru->idx);
    } else if (rru_config_msg.type == RRU_capabilities) {
167 168
      AssertFatal(rru_config_msg.len==msg_len,"Received capabilities with incorrect length (%d!=%d)\n",(int)rru_config_msg.len,(int)msg_len);
      LOG_I(PHY,"Received capabilities from RRU %d (len %d/%d, num_bands %d,max_pdschReferenceSignalPower %d, max_rxgain %d, nb_tx %d, nb_rx %d)\n",ru->idx,
169 170 171 172 173 174
            (int)rru_config_msg.len,(int)msg_len,
            ((RRU_capabilities_t *)&rru_config_msg.msg[0])->num_bands,
            ((RRU_capabilities_t *)&rru_config_msg.msg[0])->max_pdschReferenceSignalPower[0],
            ((RRU_capabilities_t *)&rru_config_msg.msg[0])->max_rxgain[0],
            ((RRU_capabilities_t *)&rru_config_msg.msg[0])->nb_tx[0],
            ((RRU_capabilities_t *)&rru_config_msg.msg[0])->nb_rx[0]);
175
      received_capabilities=1;
176 177
    } else {
      LOG_E(PHY,"Received incorrect message %d from RRU %d\n",rru_config_msg.type,ru->idx);
178 179
    }
  }
180

181
  configure_ru(ru->idx,
182
               (RRU_capabilities_t *)&rru_config_msg.msg[0]);
183 184 185
  rru_config_msg.type = RRU_config;
  rru_config_msg.len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE+sizeof(RRU_config_t);
  LOG_I(PHY,"Sending Configuration to RRU %d (num_bands %d,band0 %d,txfreq %u,rxfreq %u,att_tx %d,att_rx %d,N_RB_DL %d,N_RB_UL %d,3/4FS %d, prach_FO %d, prach_CI %d)\n",ru->idx,
186 187 188 189 190 191 192 193 194 195 196
        ((RRU_config_t *)&rru_config_msg.msg[0])->num_bands,
        ((RRU_config_t *)&rru_config_msg.msg[0])->band_list[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->tx_freq[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->rx_freq[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->att_tx[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->att_rx[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->N_RB_DL[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->N_RB_UL[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->threequarter_fs[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->prach_FreqOffset[0],
        ((RRU_config_t *)&rru_config_msg.msg[0])->prach_ConfigIndex[0]);
197
  AssertFatal((ru->ifdevice.trx_ctlsend_func(&ru->ifdevice,&rru_config_msg,rru_config_msg.len)!=-1),
198
              "RU %d failed send configuration to remote radio\n",ru->idx);
199 200 201 202 203
  return 0;
}

int connect_rau(RU_t *ru) {
  RRU_CONFIG_msg_t   rru_config_msg;
204
  ssize_t      msg_len;
205 206 207 208 209 210 211 212 213 214 215
  int                tick_received          = 0;
  int                configuration_received = 0;
  RRU_capabilities_t *cap;
  int                i;
  int                len;

  // wait for RAU_tick
  while (tick_received == 0) {
    msg_len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE;

    if ((len = ru->ifdevice.trx_ctlrecv_func(&ru->ifdevice,
216 217 218 219
               &rru_config_msg,
               msg_len))<0) {
      LOG_I(PHY,"Waiting for RAU\n");
    } else {
220
      if (rru_config_msg.type == RAU_tick) {
221 222 223
        LOG_I(PHY,"Tick received from RAU\n");
        tick_received = 1;
      } else LOG_E(PHY,"Received erroneous message (%d)from RAU, expected RAU_tick\n",rru_config_msg.type);
224 225 226 227
    }
  }

  // send capabilities
228
  rru_config_msg.type = RRU_capabilities;
229
  rru_config_msg.len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE+sizeof(RRU_capabilities_t);
230
  cap                 = (RRU_capabilities_t *)&rru_config_msg.msg[0];
231
  LOG_I(PHY,"Sending Capabilities (len %d, num_bands %d,max_pdschReferenceSignalPower %d, max_rxgain %d, nb_tx %d, nb_rx %d)\n",
232 233
        (int)rru_config_msg.len,ru->num_bands,ru->max_pdschReferenceSignalPower,ru->max_rxgain,ru->nb_tx,ru->nb_rx);

234
  switch (ru->function) {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    case NGFI_RRU_IF4p5:
      cap->FH_fmt                                   = OAI_IF4p5_only;
      break;

    case NGFI_RRU_IF5:
      cap->FH_fmt                                   = OAI_IF5_only;
      break;

    case MBP_RRU_IF5:
      cap->FH_fmt                                   = MBP_IF5;
      break;

    default:
      AssertFatal(1==0,"RU_function is unknown %d\n",RC.ru[0]->function);
      break;
250
  }
251

252
  cap->num_bands                                  = ru->num_bands;
253 254 255 256

  for (i=0; i<ru->num_bands; i++) {
    LOG_I(PHY,"Band %d: nb_rx %d nb_tx %d pdschReferenceSignalPower %d rxgain %d\n",
          ru->band[i],ru->nb_rx,ru->nb_tx,ru->max_pdschReferenceSignalPower,ru->max_rxgain);
257 258 259 260 261 262 263
    cap->band_list[i]                             = ru->band[i];
    cap->nb_rx[i]                                 = ru->nb_rx;
    cap->nb_tx[i]                                 = ru->nb_tx;
    cap->max_pdschReferenceSignalPower[i]         = ru->max_pdschReferenceSignalPower;
    cap->max_rxgain[i]                            = ru->max_rxgain;
  }

264 265
  AssertFatal((ru->ifdevice.trx_ctlsend_func(&ru->ifdevice,&rru_config_msg,rru_config_msg.len)!=-1),
              "RU %d failed send capabilities to RAU\n",ru->idx);
266 267 268
  // wait for configuration
  rru_config_msg.len  = sizeof(RRU_CONFIG_msg_t)-MAX_RRU_CONFIG_SIZE+sizeof(RRU_config_t);

269
  while (configuration_received == 0) {
270
    if ((len = ru->ifdevice.trx_ctlrecv_func(&ru->ifdevice,
271 272 273 274
               &rru_config_msg,
               rru_config_msg.len))<0) {
      LOG_I(PHY,"Waiting for configuration from RAU\n");
    } else {
275
      LOG_I(PHY,"Configuration received from RAU  (num_bands %d,band0 %d,txfreq %u,rxfreq %u,att_tx %d,att_rx %d,N_RB_DL %d,N_RB_UL %d,3/4FS %d, prach_FO %d, prach_CI %d)\n",
276 277 278 279 280 281 282 283 284 285 286
            ((RRU_config_t *)&rru_config_msg.msg[0])->num_bands,
            ((RRU_config_t *)&rru_config_msg.msg[0])->band_list[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->tx_freq[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->rx_freq[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->att_tx[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->att_rx[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->N_RB_DL[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->N_RB_UL[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->threequarter_fs[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->prach_FreqOffset[0],
            ((RRU_config_t *)&rru_config_msg.msg[0])->prach_ConfigIndex[0]);
287
      configure_rru(ru->idx,
288
                    (void *)&rru_config_msg.msg[0]);
289 290 291
      configuration_received = 1;
    }
  }
292

293 294
  return 0;
}
Raymond Knopp's avatar
Raymond Knopp committed
295 296 297 298 299
/*************************************************************/
/* Southbound Fronthaul functions, RCC/RAU                   */

// southbound IF5 fronthaul for 16-bit OAI format
static inline void fh_if5_south_out(RU_t *ru) {
300
  if (ru == RC.ru[0]) VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, ru->proc.timestamp_tx&0xffffffff );
301

302
  send_IF5(ru, ru->proc.timestamp_tx, ru->proc.subframe_tx, &ru->seqno, IF5_RRH_GW_DL);
Raymond Knopp's avatar
Raymond Knopp committed
303 304 305 306
}

// southbound IF5 fronthaul for Mobipass packet format
static inline void fh_if5_mobipass_south_out(RU_t *ru) {
307
  if (ru == RC.ru[0]) VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, ru->proc.timestamp_tx&0xffffffff );
308 309

  send_IF5(ru, ru->proc.timestamp_tx, ru->proc.subframe_tx, &ru->seqno, IF5_MOBIPASS);
Raymond Knopp's avatar
Raymond Knopp committed
310 311 312 313
}

// southbound IF4p5 fronthaul
static inline void fh_if4p5_south_out(RU_t *ru) {
314
  if (ru == RC.ru[0]) VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, ru->proc.timestamp_tx&0xffffffff );
315

316
  LOG_D(PHY,"Sending IF4p5 for frame %d subframe %d\n",ru->proc.frame_tx,ru->proc.subframe_tx);
317 318

  if (subframe_select(&ru->frame_parms,ru->proc.subframe_tx)!=SF_UL)
319
    send_IF4p5(ru,ru->proc.frame_tx, ru->proc.subframe_tx, IF4p5_PDLFFT);
Raymond Knopp's avatar
Raymond Knopp committed
320 321 322 323 324
}

/*************************************************************/
/* Input Fronthaul from south RCC/RAU                        */

325
// Synchronous if5 from south
Raymond Knopp's avatar
Raymond Knopp committed
326 327
void fh_if5_south_in(RU_t *ru,int *frame, int *subframe) {
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
328
  RU_proc_t *proc = &ru->proc;
329
  recv_IF5(ru, &proc->timestamp_rx, *subframe, IF5_RRH_GW_UL);
Raymond Knopp's avatar
Raymond Knopp committed
330 331
  proc->frame_rx    = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
  proc->subframe_rx = (proc->timestamp_rx / fp->samples_per_tti)%10;
332

Raymond Knopp's avatar
Raymond Knopp committed
333
  if (proc->first_rx == 0) {
334
    if (proc->subframe_rx != *subframe) {
335
      LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
336 337
      exit_fun("Exiting");
    }
338

Raymond Knopp's avatar
Raymond Knopp committed
339
    if (proc->frame_rx != *frame) {
340
      LOG_E(PHY,"Received Timestamp doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,*frame);
Raymond Knopp's avatar
Raymond Knopp committed
341 342 343 344 345
      exit_fun("Exiting");
    }
  } else {
    proc->first_rx = 0;
    *frame = proc->frame_rx;
346 347
    *subframe = proc->subframe_rx;
  }
Raymond Knopp's avatar
Raymond Knopp committed
348

349
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
Raymond Knopp's avatar
Raymond Knopp committed
350 351
}

352
// Synchronous if4p5 from south
Raymond Knopp's avatar
Raymond Knopp committed
353 354
void fh_if4p5_south_in(RU_t *ru,int *frame,int *subframe) {
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
355 356
  RU_proc_t *proc = &ru->proc;
  int f,sf;
Raymond Knopp's avatar
Raymond Knopp committed
357 358
  uint16_t packet_type;
  uint32_t symbol_number=0;
359 360
  uint32_t symbol_mask_full;

361 362 363 364
  if ((fp->frame_type == TDD) && (subframe_select(fp,*subframe)==SF_S))
    symbol_mask_full = (1<<fp->ul_symbols_in_S_subframe)-1;
  else
    symbol_mask_full = (1<<fp->symbols_per_tti)-1;
Raymond Knopp's avatar
Raymond Knopp committed
365

366
  AssertFatal(proc->symbol_mask[*subframe]==0,"rx_fh_if4p5: proc->symbol_mask[%d] = %x\n",*subframe,proc->symbol_mask[*subframe]);
367

Raymond Knopp's avatar
Raymond Knopp committed
368
  do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
369
    recv_IF4p5(ru, &f, &sf, &packet_type, &symbol_number);
Raymond Knopp's avatar
Raymond Knopp committed
370

371
    if (packet_type == IF4p5_PULFFT) proc->symbol_mask[sf] = proc->symbol_mask[sf] | (1<<symbol_number);
372 373 374 375 376 377
    else if (packet_type == IF4p5_PULTICK) {
      if ((proc->first_rx==0) && (f!=*frame)) LOG_E(PHY,"rx_fh_if4p5: PULTICK received frame %d != expected %d\n",f,*frame);

      if ((proc->first_rx==0) && (sf!=*subframe)) LOG_E(PHY,"rx_fh_if4p5: PULTICK received subframe %d != expected %d (first_rx %d)\n",sf,*subframe,proc->first_rx);

      break;
Raymond Knopp's avatar
Raymond Knopp committed
378
    } else if (packet_type == IF4p5_PRACH) {
379
      // nothing in RU for RAU
Raymond Knopp's avatar
Raymond Knopp committed
380
    }
381

382
    LOG_D(PHY,"rx_fh_if4p5: subframe %d symbol mask %x\n",*subframe,proc->symbol_mask[*subframe]);
383
  } while(proc->symbol_mask[*subframe] != symbol_mask_full);
Raymond Knopp's avatar
Raymond Knopp committed
384 385

  //caculate timestamp_rx, timestamp_tx based on frame and subframe
386 387
  proc->subframe_rx  = sf;
  proc->frame_rx     = f;
388
  proc->timestamp_rx = ((proc->frame_rx * 10)  + proc->subframe_rx ) * fp->samples_per_tti ;
389
  //  proc->timestamp_tx = proc->timestamp_rx +  (4*fp->samples_per_tti);
390 391
  proc->subframe_tx  = (sf+sf_ahead)%10;
  proc->frame_tx     = (sf>(9-sf_ahead)) ? (f+1)&1023 : f;
392

Raymond Knopp's avatar
Raymond Knopp committed
393
  if (proc->first_rx == 0) {
394
    if (proc->subframe_rx != *subframe) {
Raymond Knopp's avatar
Raymond Knopp committed
395 396 397
      LOG_E(PHY,"Received Timestamp (IF4p5) doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",proc->subframe_rx,*subframe);
      exit_fun("Exiting");
    }
398

Raymond Knopp's avatar
Raymond Knopp committed
399 400 401 402 403 404 405
    if (proc->frame_rx != *frame) {
      LOG_E(PHY,"Received Timestamp (IF4p5) doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",proc->frame_rx,*frame);
      exit_fun("Exiting");
    }
  } else {
    proc->first_rx = 0;
    *frame = proc->frame_rx;
406
    *subframe = proc->subframe_rx;
Raymond Knopp's avatar
Raymond Knopp committed
407
  }
408

409 410 411 412 413 414 415
  if (ru == RC.ru[0]) {
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_RU, f );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_RU, sf );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_RU, proc->frame_tx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_RU, proc->subframe_tx );
  }

416
  proc->symbol_mask[sf] = 0;
Raymond Knopp's avatar
Raymond Knopp committed
417
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
418 419
  LOG_D(PHY,"RU %d: fh_if4p5_south_in sleeping ...\n",ru->idx);
  usleep(100);
Raymond Knopp's avatar
Raymond Knopp committed
420 421 422 423 424 425
}

// Dummy FH from south for getting synchronization from master RU
void fh_slave_south_in(RU_t *ru,int *frame,int *subframe) {
  // This case is for synchronization to another thread
  // it just waits for an external event.  The actual rx_fh is handle by the asynchronous RX thread
426
  RU_proc_t *proc=&ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
427 428 429 430 431 432 433

  if (wait_on_condition(&proc->mutex_FH,&proc->cond_FH,&proc->instance_cnt_FH,"fh_slave_south_in") < 0)
    return;

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

434 435 436 437
// asynchronous inbound if5 fronthaul from south (Mobipass)
void fh_if5_south_asynch_in_mobipass(RU_t *ru,int *frame,int *subframe) {
  RU_proc_t *proc       = &ru->proc;
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
438
  recv_IF5(ru, &proc->timestamp_rx, *subframe, IF5_MOBIPASS);
439 440 441 442 443 444 445
  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
  int offset_mobipass = 40120;
  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
  proc->subframe_rx = ((proc->timestamp_rx-offset_mobipass)/fp->samples_per_tti)%10;
  proc->frame_rx    = ((proc->timestamp_rx-offset_mobipass)/(fp->samples_per_tti*10))&1023;
  proc->subframe_rx = (proc->timestamp_rx/fp->samples_per_tti)%10;
  proc->frame_rx    = (proc->timestamp_rx/(10*fp->samples_per_tti))&1023;
Raymond Knopp's avatar
Raymond Knopp committed
446

447 448
  if (proc->first_rx == 1) {
    proc->first_rx =2;
Raymond Knopp's avatar
Raymond Knopp committed
449
    *subframe = proc->subframe_rx;
450
    *frame    = proc->frame_rx;
451
    LOG_E(PHY,"[Mobipass]timestamp_rx:%llu, frame_rx %d, subframe: %d\n",(unsigned long long int)proc->timestamp_rx,proc->frame_rx,proc->subframe_rx);
452
  } else {
Raymond Knopp's avatar
Raymond Knopp committed
453
    if (proc->subframe_rx != *subframe) {
454 455
      proc->first_rx++;
      LOG_E(PHY,"[Mobipass]timestamp:%llu, subframe_rx %d is not what we expect %d, first_rx:%d\n",(unsigned long long int)proc->timestamp_rx, proc->subframe_rx,*subframe, proc->first_rx);
456
      //exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
457
    }
458

Raymond Knopp's avatar
Raymond Knopp committed
459
    if (proc->frame_rx != *frame) {
460 461 462
      proc->first_rx++;
      LOG_E(PHY,"[Mobipass]timestamp:%llu, frame_rx %d is not what we expect %d, first_rx:%d\n",(unsigned long long int)proc->timestamp_rx,proc->frame_rx,*frame, proc->first_rx);
      // exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
463
    }
464

465
    // temporary solution
466 467
    *subframe = proc->subframe_rx;
    *frame    = proc->frame_rx;
Raymond Knopp's avatar
Raymond Knopp committed
468
  }
469 470

  pthread_mutex_unlock(&proc->mutex_asynch_rxtx);
471
} // eNodeB_3GPP_BBU
Raymond Knopp's avatar
Raymond Knopp committed
472 473 474 475

// asynchronous inbound if4p5 fronthaul from south
void fh_if4p5_south_asynch_in(RU_t *ru,int *frame,int *subframe) {
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
476
  RU_proc_t *proc       = &ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
477
  uint16_t packet_type;
Raymond Knopp's avatar
Raymond Knopp committed
478
  uint32_t symbol_number,symbol_mask,prach_rx;
479
  uint32_t got_prach_info=0;
Raymond Knopp's avatar
Raymond Knopp committed
480
  symbol_number = 0;
481 482
  symbol_mask   = (1<<fp->symbols_per_tti)-1;
  prach_rx      = 0;
Raymond Knopp's avatar
Raymond Knopp committed
483 484 485

  do {   // Blocking, we need a timeout on this !!!!!!!!!!!!!!!!!!!!!!!
    recv_IF4p5(ru, &proc->frame_rx, &proc->subframe_rx, &packet_type, &symbol_number);
486

487 488 489 490 491
    // grab first prach information for this new subframe
    if (got_prach_info==0) {
      prach_rx       = is_prach_subframe(fp, proc->frame_rx, proc->subframe_rx);
      got_prach_info = 1;
    }
492

Raymond Knopp's avatar
Raymond Knopp committed
493 494 495 496
    if (proc->first_rx != 0) {
      *frame = proc->frame_rx;
      *subframe = proc->subframe_rx;
      proc->first_rx = 0;
497
    } else {
Raymond Knopp's avatar
Raymond Knopp committed
498
      if (proc->frame_rx != *frame) {
499 500
        LOG_E(PHY,"frame_rx %d is not what we expect %d\n",proc->frame_rx,*frame);
        exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
501
      }
502

Raymond Knopp's avatar
Raymond Knopp committed
503
      if (proc->subframe_rx != *subframe) {
504 505
        LOG_E(PHY,"subframe_rx %d is not what we expect %d\n",proc->subframe_rx,*subframe);
        exit_fun("Exiting");
Raymond Knopp's avatar
Raymond Knopp committed
506 507
      }
    }
508

509 510
    if      (packet_type == IF4p5_PULFFT)       symbol_mask &= (~(1<<symbol_number));
    else if (packet_type == IF4p5_PRACH)        prach_rx    &= (~0x1);
511

512
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
513 514 515 516
    else if (packet_type == IF4p5_PRACH_BR_CE0) prach_rx    &= (~0x2);
    else if (packet_type == IF4p5_PRACH_BR_CE1) prach_rx    &= (~0x4);
    else if (packet_type == IF4p5_PRACH_BR_CE2) prach_rx    &= (~0x8);
    else if (packet_type == IF4p5_PRACH_BR_CE3) prach_rx    &= (~0x10);
517

518
#endif
519 520
  } while( (symbol_mask > 0) || (prach_rx >0));   // haven't received all PUSCH symbols and PRACH information
}
Raymond Knopp's avatar
Raymond Knopp committed
521 522 523 524 525 526 527





/*************************************************************/
/* Input Fronthaul from North RRU                            */
528 529

// RRU IF4p5 TX fronthaul receiver. Assumes an if_device on input and if or rf device on output
Raymond Knopp's avatar
Raymond Knopp committed
530
// receives one subframe's worth of IF4p5 OFDM symbols and OFDM modulates
531
void fh_if4p5_north_in(RU_t *ru,int *frame,int *subframe) {
Raymond Knopp's avatar
Raymond Knopp committed
532 533 534
  uint32_t symbol_number=0;
  uint32_t symbol_mask, symbol_mask_full;
  uint16_t packet_type;
535
  /// **** incoming IF4p5 from remote RCC/RAU **** ///
Raymond Knopp's avatar
Raymond Knopp committed
536 537 538
  symbol_number = 0;
  symbol_mask = 0;
  symbol_mask_full = (1<<ru->frame_parms.symbols_per_tti)-1;
539 540

  do {
541
    recv_IF4p5(ru, frame, subframe, &packet_type, &symbol_number);
Raymond Knopp's avatar
Raymond Knopp committed
542
    symbol_mask = symbol_mask | (1<<symbol_number);
543
  } while (symbol_mask != symbol_mask_full);
Raymond Knopp's avatar
Raymond Knopp committed
544

545 546
  // dump VCD output for first RU in list
  if (ru == RC.ru[0]) {
547 548
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_RU, *frame );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_RU, *subframe );
Raymond Knopp's avatar
Raymond Knopp committed
549 550 551 552 553
  }
}

void fh_if5_north_asynch_in(RU_t *ru,int *frame,int *subframe) {
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
554
  RU_proc_t *proc        = &ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
555 556
  int subframe_tx,frame_tx;
  openair0_timestamp timestamp_tx;
557 558
  recv_IF5(ru, &timestamp_tx, *subframe, IF5_RRH_GW_DL);
  //      printf("Received subframe %d (TS %llu) from RCC\n",subframe_tx,timestamp_tx);
Raymond Knopp's avatar
Raymond Knopp committed
559 560 561 562 563 564 565
  subframe_tx = (timestamp_tx/fp->samples_per_tti)%10;
  frame_tx    = (timestamp_tx/(fp->samples_per_tti*10))&1023;

  if (proc->first_tx != 0) {
    *subframe = subframe_tx;
    *frame    = frame_tx;
    proc->first_tx = 0;
566
  } else {
567 568
    AssertFatal(subframe_tx == *subframe,
                "subframe_tx %d is not what we expect %d\n",subframe_tx,*subframe);
569
    AssertFatal(frame_tx == *frame,
570
                "frame_tx %d is not what we expect %d\n",frame_tx,*frame);
Raymond Knopp's avatar
Raymond Knopp committed
571 572 573 574 575
  }
}

void fh_if4p5_north_asynch_in(RU_t *ru,int *frame,int *subframe) {
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
576
  RU_proc_t *proc        = &ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
577 578 579
  uint16_t packet_type;
  uint32_t symbol_number,symbol_mask,symbol_mask_full;
  int subframe_tx,frame_tx;
Cedric Roux's avatar
Cedric Roux committed
580
  LOG_D(PHY, "%s(ru:%p frame, subframe)\n", __FUNCTION__, ru);
Raymond Knopp's avatar
Raymond Knopp committed
581 582
  symbol_number = 0;
  symbol_mask = 0;
583
  symbol_mask_full = ((subframe_select(fp,*subframe) == SF_S) ? (1<<fp->dl_symbols_in_S_subframe) : (1<<fp->symbols_per_tti))-1;
584 585

  do {
Raymond Knopp's avatar
Raymond Knopp committed
586
    recv_IF4p5(ru, &frame_tx, &subframe_tx, &packet_type, &symbol_number);
587

588
    if ((subframe_select(fp,subframe_tx) == SF_DL) && (symbol_number == 0)) start_meas(&ru->rx_fhaul);
589

590
    LOG_D(PHY,"subframe %d (%d): frame %d, subframe %d, symbol %d\n",
591 592
          *subframe,subframe_select(fp,*subframe),frame_tx,subframe_tx,symbol_number);

Raymond Knopp's avatar
Raymond Knopp committed
593 594 595 596
    if (proc->first_tx != 0) {
      *frame    = frame_tx;
      *subframe = subframe_tx;
      proc->first_tx = 0;
597
      symbol_mask_full = ((subframe_select(fp,*subframe) == SF_S) ? (1<<fp->dl_symbols_in_S_subframe) : (1<<fp->symbols_per_tti))-1;
598
    } else {
599
      AssertFatal(frame_tx == *frame,
600
                  "frame_tx %d is not what we expect %d\n",frame_tx,*frame);
601
      AssertFatal(subframe_tx == *subframe,
602
                  "subframe_tx %d is not what we expect %d\n",subframe_tx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
603
    }
604

Raymond Knopp's avatar
Raymond Knopp committed
605 606
    if (packet_type == IF4p5_PDLFFT) {
      symbol_mask = symbol_mask | (1<<symbol_number);
607 608
    } else AssertFatal(1==0,"Illegal IF4p5 packet type (should only be IF4p5_PDLFFT%d\n",packet_type);
  } while (symbol_mask != symbol_mask_full);
609

610
  if (subframe_select(fp,subframe_tx) == SF_DL) stop_meas(&ru->rx_fhaul);
611

612 613 614 615
  proc->subframe_tx  = subframe_tx;
  proc->frame_tx     = frame_tx;

  if ((frame_tx == 0)&&(subframe_tx == 0)) proc->frame_tx_unwrap += 1024;
616

617
  proc->timestamp_tx = ((((uint64_t)frame_tx + (uint64_t)proc->frame_tx_unwrap) * 10) + (uint64_t)subframe_tx) * (uint64_t)fp->samples_per_tti;
618
  LOG_D(PHY,"RU %d/%d TST %llu, frame %d, subframe %d\n",ru->idx,0,(long long unsigned int)proc->timestamp_tx,frame_tx,subframe_tx);
619 620

  // dump VCD output for first RU in list
621 622 623 624
  if (ru == RC.ru[0]) {
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_RU, frame_tx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_RU, subframe_tx );
  }
625

626
  if (ru->feptx_ofdm) ru->feptx_ofdm(ru);
627

628
  if (ru->fh_south_out) ru->fh_south_out(ru);
629
}
Raymond Knopp's avatar
Raymond Knopp committed
630

631 632 633
void fh_if5_north_out(RU_t *ru) {
  RU_proc_t *proc=&ru->proc;
  uint8_t seqno=0;
634 635
  /// **** send_IF5 of rxdata to BBU **** ///
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 1 );
636
  send_IF5(ru, proc->timestamp_rx, proc->subframe_rx, &seqno, IF5_RRH_GW_UL);
637
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 0 );
638 639 640 641 642 643 644
}

// RRU IF4p5 northbound interface (RX)
void fh_if4p5_north_out(RU_t *ru) {
  RU_proc_t *proc=&ru->proc;
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
  const int subframe     = proc->subframe_rx;
645

646
  if (ru->idx==0) VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_RU, proc->subframe_rx );
647 648 649

  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)!=SF_UL)) {
    /// **** in TDD during DL send_IF4 of ULTICK to RCC **** ///
650
    send_IF4p5(ru, proc->frame_rx, proc->subframe_rx, IF4p5_PULTICK);
651 652 653
    return;
  }

654 655 656
  start_meas(&ru->tx_fhaul);
  send_IF4p5(ru, proc->frame_rx, proc->subframe_rx, IF4p5_PULFFT);
  stop_meas(&ru->tx_fhaul);
657
}
658

Wang Tsu-Han's avatar
Wang Tsu-Han committed
659 660
/* add fail safe for late command */
typedef enum {
661 662 663 664 665
  STATE_BURST_NORMAL = 0,
  STATE_BURST_TERMINATE = 1,
  STATE_BURST_STOP_1 = 2,
  STATE_BURST_STOP_2 = 3,
  STATE_BURST_RESTART = 4,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
666 667 668 669 670 671
} late_control_e;

volatile late_control_e late_control=STATE_BURST_NORMAL;

/* add fail safe for late command end */

672
static void *emulatedRF_thread(void *param) {
673 674 675
  RU_proc_t *proc = (RU_proc_t *) param;
  int microsec = 500; // length of time to sleep, in miliseconds
  struct timespec req = {0};
676
  int numerology = get_softmodem_params()->numerology;
677
  req.tv_sec = 0;
Eurecom's avatar
Eurecom committed
678
  req.tv_nsec = (numerology>0)? ((microsec * 1000L)/numerology):(microsec * 1000L)*2;
679 680 681 682 683 684 685
  cpu_set_t cpuset;
  CPU_SET(1,&cpuset);
  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  int policy;
  struct sched_param sparam;
  memset(&sparam, 0, sizeof(sparam));
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
686
  policy = SCHED_FIFO ;
687
  pthread_setschedparam(pthread_self(), policy, &sparam);
688
  wait_sync("emulatedRF_thread");
689 690

  while(!oai_exit) {
691
    nanosleep(&req, (struct timespec *)NULL);
692 693

    if(proc->emulate_rf_busy ) {
694 695
      LOG_E(PHY,"rf being delayed in emulated RF\n");
    }
696

697
    proc->emulate_rf_busy = 1;
698 699 700 701 702
    pthread_mutex_lock(&proc->mutex_emulateRF);
    ++proc->instance_cnt_emulateRF;
    pthread_mutex_unlock(&proc->mutex_emulateRF);
    pthread_cond_signal(&proc->cond_emulateRF);
  }
703

704 705 706
  return 0;
}

Raymond Knopp's avatar
Raymond Knopp committed
707
void rx_rf(RU_t *ru,int *frame,int *subframe) {
708
  RU_proc_t *proc = &ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
709
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
710
  void *rxp[ru->nb_rx];
711
  unsigned int rxs;
Raymond Knopp's avatar
Raymond Knopp committed
712
  int i;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
713
  openair0_timestamp ts=0,old_ts=0;
714

715
  for (i=0; i<ru->nb_rx; i++)
716
    rxp[i] = (void *)&ru->common.rxdata[i][*subframe*fp->samples_per_tti];
717

Raymond Knopp's avatar
Raymond Knopp committed
718
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 1 );
719
  old_ts = proc->timestamp_rx;
720 721

  if(get_softmodem_params()->emulate_rf) {
722 723 724
    wait_on_condition(&proc->mutex_emulateRF,&proc->cond_emulateRF,&proc->instance_cnt_emulateRF,"emulatedRF_thread");
    release_thread(&proc->mutex_emulateRF,&proc->instance_cnt_emulateRF,"emulatedRF_thread");
    rxs = fp->samples_per_tti;
725
  } else {
726
    rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
727 728 729 730
                                     &ts,
                                     rxp,
                                     fp->samples_per_tti,
                                     ru->nb_rx);
731
  }
732

Raymond Knopp's avatar
Raymond Knopp committed
733
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 0 );
734 735
  proc->timestamp_rx = ts-ru->ts_offset;

736 737 738
  //  AssertFatal(rxs == fp->samples_per_tti,
  //        "rx_rf: Asked for %d samples, got %d from SDR\n",fp->samples_per_tti,rxs);
  if(rxs != fp->samples_per_tti) {
739
    LOG_E(PHY,"rx_rf: Asked for %d samples, got %d from SDR\n",fp->samples_per_tti,rxs);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
740 741
    late_control=STATE_BURST_TERMINATE;
  }
742 743

  if (proc->first_rx == 1) {
744
    ru->ts_offset = proc->timestamp_rx;
745
    proc->timestamp_rx = 0;
746
  } else {
747
    if (proc->timestamp_rx - old_ts != fp->samples_per_tti) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
748
      //LOG_I(PHY,"rx_rf: rfdevice timing drift of %"PRId64" samples (ts_off %"PRId64")\n",proc->timestamp_rx - old_ts - fp->samples_per_tti,ru->ts_offset);
749 750 751 752
      ru->ts_offset += (proc->timestamp_rx - old_ts - fp->samples_per_tti);
      proc->timestamp_rx = ts-ru->ts_offset;
    }
  }
753

754 755
  proc->frame_rx     = (proc->timestamp_rx / (fp->samples_per_tti*10))&1023;
  proc->subframe_rx  = (proc->timestamp_rx / fp->samples_per_tti)%10;
Raymond Knopp's avatar
Raymond Knopp committed
756
  // synchronize first reception to frame 0 subframe 0
Wang Tsu-Han's avatar
Wang Tsu-Han committed
757 758
#ifdef PHY_TX_THREAD
  proc->timestamp_phy_tx = proc->timestamp_rx+((sf_ahead-1)*fp->samples_per_tti);
759
  proc->subframe_phy_tx  = (proc->subframe_rx+(sf_ahead-1))%10;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
760 761 762 763 764 765
  proc->frame_phy_tx     = (proc->subframe_rx>(9-(sf_ahead-1))) ? (proc->frame_rx+1)&1023 : proc->frame_rx;
#else
  proc->timestamp_tx = proc->timestamp_rx+(sf_ahead*fp->samples_per_tti);
  proc->subframe_tx  = (proc->subframe_rx+sf_ahead)%10;
  proc->frame_tx     = (proc->subframe_rx>(9-sf_ahead)) ? (proc->frame_rx+1)&1023 : proc->frame_rx;
#endif
766 767 768
  //proc->timestamp_tx = proc->timestamp_rx+(sf_ahead*fp->samples_per_tti);
  //proc->subframe_tx  = (proc->subframe_rx+sf_ahead)%10;
  //proc->frame_tx     = (proc->subframe_rx>(9-sf_ahead)) ? (proc->frame_rx+1)&1023 : proc->frame_rx;
769
  LOG_D(PHY,"RU %d/%d TS %llu (off %d), frame %d, subframe %d\n",
770 771 772 773
        ru->idx,
        0,
        (unsigned long long int)proc->timestamp_rx,
        (int)ru->ts_offset,proc->frame_rx,proc->subframe_rx);
774

775
  // dump VCD output for first RU in list
776 777 778 779
  if (ru == RC.ru[0]) {
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_RU, proc->frame_rx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_RU, proc->subframe_rx );
  }
780

Raymond Knopp's avatar
Raymond Knopp committed
781
  if (proc->first_rx == 0) {
782
    if (proc->subframe_rx != *subframe) {
783
      LOG_E(PHY,"Received Timestamp (%llu) doesn't correspond to the time we think it is (proc->subframe_rx %d, subframe %d)\n",(long long unsigned int)proc->timestamp_rx,proc->subframe_rx,*subframe);
Raymond Knopp's avatar
Raymond Knopp committed
784 785
      exit_fun("Exiting");
    }
786

Raymond Knopp's avatar
Raymond Knopp committed
787
    if (proc->frame_rx != *frame) {
788
      LOG_E(PHY,"Received Timestamp (%llu) doesn't correspond to the time we think it is (proc->frame_rx %d frame %d)\n",(long long unsigned int)proc->timestamp_rx,proc->frame_rx,*frame);
Raymond Knopp's avatar
Raymond Knopp committed
789 790 791 792 793
      exit_fun("Exiting");
    }
  } else {
    proc->first_rx = 0;
    *frame = proc->frame_rx;
794
    *subframe = proc->subframe_rx;
Raymond Knopp's avatar
Raymond Knopp committed
795
  }
796

Raymond Knopp's avatar
Raymond Knopp committed
797
  //printf("timestamp_rx %lu, frame %d(%d), subframe %d(%d)\n",ru->timestamp_rx,proc->frame_rx,frame,proc->subframe_rx,subframe);
798
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS, proc->timestamp_rx&0xffffffff );
799 800

  if (rxs != fp->samples_per_tti) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
801 802
#if defined(USRP_REC_PLAY)
    exit_fun("Exiting IQ record/playback");
803
#else
804
    //exit_fun( "problem receiving samples" );
Cedric Roux's avatar
Cedric Roux committed
805
    LOG_E(PHY, "problem receiving samples");
806
#endif
807
  }
Raymond Knopp's avatar
Raymond Knopp committed
808 809 810
}


811 812 813
void tx_rf(RU_t *ru) {
  RU_proc_t *proc = &ru->proc;
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
814
  void *txp[ru->nb_tx];
815 816
  unsigned int txs;
  int i;
Cedric Roux's avatar
Cedric Roux committed
817 818
  T(T_ENB_PHY_OUTPUT_SIGNAL, T_INT(0), T_INT(0), T_INT(proc->frame_tx), T_INT(proc->subframe_tx),
    T_INT(0), T_BUFFER(&ru->common.txdata[0][proc->subframe_tx * fp->samples_per_tti], fp->samples_per_tti * 4));
819 820 821
  lte_subframe_t SF_type     = subframe_select(fp,proc->subframe_tx%10);
  lte_subframe_t prevSF_type = subframe_select(fp,(proc->subframe_tx+9)%10);
  lte_subframe_t nextSF_type = subframe_select(fp,(proc->subframe_tx+1)%10);
822 823
  int sf_extension = 0;

824 825 826
  if ((SF_type == SF_DL) ||
      (SF_type == SF_S)) {
    int siglen=fp->samples_per_tti,flags=1;
827

828 829 830 831
    if (SF_type == SF_S) {
      siglen = fp->dl_symbols_in_S_subframe*(fp->ofdm_symbol_size+fp->nb_prefix_samples0);
      flags=3; // end of burst
    }
832

833
    if ((fp->frame_type == TDD) &&
834 835 836
        (SF_type == SF_DL)&&
        (prevSF_type == SF_UL) &&
        (nextSF_type == SF_DL)) {
837
      flags = 2; // start of burst
Wang Tsu-Han's avatar
Wang Tsu-Han committed
838
      sf_extension = ru->N_TA_offset;
839
    }
840

841
    if ((fp->frame_type == TDD) &&
842 843 844
        (SF_type == SF_DL)&&
        (prevSF_type == SF_UL) &&
        (nextSF_type == SF_UL)) {
845
      flags = 4; // start of burst and end of burst (only one DL SF between two UL)
Wang Tsu-Han's avatar
Wang Tsu-Han committed
846
      sf_extension = ru->N_TA_offset;
847 848
    }

Wang Tsu-Han's avatar
Wang Tsu-Han committed
849 850
#if defined(__x86_64) || defined(__i386__)
#ifdef __AVX2__
851
    sf_extension = (sf_extension)&0xfffffff8;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
852
#else
853
    sf_extension = (sf_extension)&0xfffffffc;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
854 855
#endif
#elif defined(__arm__)
856
    sf_extension = (sf_extension)&0xfffffffc;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
857
#endif
858

859
    for (i=0; i<ru->nb_tx; i++)
860
      txp[i] = (void *)&ru->common.txdata[i][(proc->subframe_tx*fp->samples_per_tti)-sf_extension];
861

Wang Tsu-Han's avatar
Wang Tsu-Han committed
862
    /* add fail safe for late command */
863
    if(late_control!=STATE_BURST_NORMAL) { //stop burst
Wang Tsu-Han's avatar
Wang Tsu-Han committed
864
      switch (late_control) {
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
        case STATE_BURST_TERMINATE:
          flags=10; // end of burst and no time spec
          late_control=STATE_BURST_STOP_1;
          break;

        case STATE_BURST_STOP_1:
          flags=0; // no send
          late_control=STATE_BURST_STOP_2;
          return;//no send
          break;

        case STATE_BURST_STOP_2:
          flags=0; // no send
          late_control=STATE_BURST_RESTART;
          return;//no send
          break;

        case STATE_BURST_RESTART:
          flags=2; // start burst
          late_control=STATE_BURST_NORMAL;
          break;

        default:
          LOG_D(PHY,"[TXPATH] RU %d late_control %d not implemented\n",ru->idx, late_control);
          break;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
890 891 892
      }
    }

893
    /* add fail safe for late command end */
Raymond Knopp's avatar
Raymond Knopp committed
894 895
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_RU, proc->frame_tx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_RU, proc->subframe_tx );
896 897 898 899
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST, (proc->timestamp_tx-ru->openair0_cfg.tx_sample_advance)&0xffffffff );
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 1 );
    // prepare tx buffer pointers
    txs = ru->rfdevice.trx_write_func(&ru->rfdevice,
900 901 902 903 904
                                      proc->timestamp_tx+ru->ts_offset-ru->openair0_cfg.tx_sample_advance-sf_extension,
                                      txp,
                                      siglen+sf_extension,
                                      ru->nb_tx,
                                      flags);
905
    LOG_D(PHY,"[TXPATH] RU %d tx_rf, writing to TS %llu, frame %d, unwrapped_frame %d, subframe %d\n",ru->idx,
906
          (long long unsigned int)proc->timestamp_tx,proc->frame_tx,proc->frame_tx_unwrap,proc->subframe_tx);
907
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
908 909 910

    //    AssertFatal(txs ==  siglen+sf_extension,"TX : Timeout (sent %d/%d)\n",txs, siglen);
    if( (txs !=  siglen+sf_extension) && (late_control==STATE_BURST_NORMAL) ) { /* add fail safe for late command */
Wang Tsu-Han's avatar
Wang Tsu-Han committed
911 912 913
      late_control=STATE_BURST_TERMINATE;
      LOG_E(PHY,"TX : Timeout (sent %d/%d) state =%d\n",txs, siglen,late_control);
    }
914
  }
915 916 917
}


Raymond Knopp's avatar
Raymond Knopp committed
918 919 920
/*!
 * \brief The Asynchronous RX/TX FH thread of RAU/RCC/eNB/RRU.
 * This handles the RX FH for an asynchronous RRU/UE
921
 * \param param is a \ref L1_proc_t structure which contains the info what to process.
Raymond Knopp's avatar
Raymond Knopp committed
922 923
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
924
static void *ru_thread_asynch_rxtx( void *param ) {
Raymond Knopp's avatar
Raymond Knopp committed
925
  static int ru_thread_asynch_rxtx_status;
926
  RU_t *ru         = (RU_t *)param;
927
  RU_proc_t *proc  = &ru->proc;
928
  int subframe=0, frame=0;
929
  thread_top_init("ru_thread_asynch_rxtx",1,870000,1000000,1000000);
Raymond Knopp's avatar
Raymond Knopp committed
930
  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe
931
  wait_sync("ru_thread_asynch_rxtx");
Raymond Knopp's avatar
Raymond Knopp committed
932
  // wait for top-level synchronization and do one acquisition to get timestamp for setting frame/subframe
933
  printf( "waiting for devices (ru_thread_asynch_rx)\n");
Raymond Knopp's avatar
Raymond Knopp committed
934
  wait_on_condition(&proc->mutex_asynch_rxtx,&proc->cond_asynch_rxtx,&proc->instance_cnt_asynch_rxtx,"thread_asynch");
935
  printf( "devices ok (ru_thread_asynch_rx)\n");
Raymond Knopp's avatar
Raymond Knopp committed
936

937 938
  while (!oai_exit) {
    if (oai_exit) break;
Raymond Knopp's avatar
Raymond Knopp committed
939

940
    if (subframe==9) {
Raymond Knopp's avatar
Raymond Knopp committed
941 942 943 944 945
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
946 947
    }

948
    LOG_D(PHY,"ru_thread_asynch_rxtx: Waiting on incoming fronthaul\n");
949 950

    // asynchronous receive from south (Mobipass)
951 952
    if (ru->fh_south_asynch_in) ru->fh_south_asynch_in(ru,&frame,&subframe);
    // asynchronous receive from north (RRU IF4/IF5)
953
    else if (ru->fh_north_asynch_in) {
954 955 956
      if (subframe_select(&ru->frame_parms,subframe)!=SF_UL)
        ru->fh_north_asynch_in(ru,&frame,&subframe);
    } else AssertFatal(1==0,"Unknown function in ru_thread_asynch_rxtx\n");
Raymond Knopp's avatar
Raymond Knopp committed
957 958 959 960 961 962 963 964 965
  }

  ru_thread_asynch_rxtx_status=0;
  return(&ru_thread_asynch_rxtx_status);
}




966
void wakeup_slaves(RU_proc_t *proc) {
Raymond Knopp's avatar
Raymond Knopp committed
967 968 969 970
  int i;
  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
971 972

  for (i=0; i<proc->num_slaves; i++) {
973
    RU_proc_t *slave_proc = proc->slave_proc[i];
974

Raymond Knopp's avatar
Raymond Knopp committed
975 976 977
    // 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) {
978
      LOG_E( PHY, "ERROR pthread_mutex_lock for RU %d slave %d (IC %d)\n",proc->ru->idx,slave_proc->ru->idx,slave_proc->instance_cnt_FH);
Raymond Knopp's avatar
Raymond Knopp committed
979 980 981
      exit_fun( "error locking mutex_rxtx" );
      break;
    }
982

Raymond Knopp's avatar
Raymond Knopp committed
983 984 985 986
    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;
987
    slave_proc->timestamp_tx = proc->timestamp_tx;
Raymond Knopp's avatar
Raymond Knopp committed
988
    pthread_mutex_unlock( &slave_proc->mutex_FH );
989

Raymond Knopp's avatar
Raymond Knopp committed
990 991 992
    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) {
993 994 995
        LOG_E( PHY, "ERROR pthread_cond_signal for RU %d, slave RU %d\n",proc->ru->idx,slave_proc->ru->idx);
        exit_fun( "ERROR pthread_cond_signal" );
        break;
Raymond Knopp's avatar
Raymond Knopp committed
996 997
      }
    } else {
998
      LOG_W( PHY,"[RU] Frame %d, slave %d thread busy!! (cnt_FH %i)\n",slave_proc->frame_rx,slave_proc->ru->idx, cnt_slave);
Raymond Knopp's avatar
Raymond Knopp committed
999 1000
      exit_fun( "FH thread busy" );
      break;
1001
    }
Raymond Knopp's avatar
Raymond Knopp committed
1002 1003 1004 1005 1006
  }
}

/*!
 * \brief The prach receive thread of RU.
1007
 * \param param is a \ref RU_proc_t structure which contains the info what to process.
Raymond Knopp's avatar
Raymond Knopp committed
1008 1009
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1010
static void *ru_thread_prach( void *param ) {
Raymond Knopp's avatar
Raymond Knopp committed
1011
  static int ru_thread_prach_status;
1012 1013
  RU_t *ru        = (RU_t *)param;
  RU_proc_t *proc = (RU_proc_t *)&ru->proc;
Raymond Knopp's avatar
Raymond Knopp committed
1014
  // set default return value
1015
  ru_thread_prach_status = 0;
1016
  thread_top_init("ru_thread_prach",1,500000,1000000,20000000);
1017
  //wait_sync("ru_thread_prach");
Raymond Knopp's avatar
Raymond Knopp committed
1018

1019 1020 1021 1022
  while (RC.ru_mask>0) {
    usleep(1e6);
    LOG_I(PHY,"%s() RACH waiting for RU to be configured\n", __FUNCTION__);
  }
1023

1024
  LOG_I(PHY,"%s() RU configured - RACH processing thread running\n", __FUNCTION__);
Raymond Knopp's avatar
Raymond Knopp committed
1025 1026

  while (!oai_exit) {
1027
    if (wait_on_condition(&proc->mutex_prach,&proc->cond_prach,&proc->instance_cnt_prach,"ru_prach_thread") < 0) break;
1028

1029
    if (oai_exit) break;
1030 1031 1032 1033

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_RU_PRACH_RX, 1 );

    if (ru->eNB_list[0]) {
1034
      prach_procedures(
1035
        ru->eNB_list[0]
1036
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1037
        ,0
1038
#endif
1039 1040 1041 1042 1043 1044 1045 1046 1047
      );
    } else {
      rx_prach(NULL,
               ru,
               NULL,
               NULL,
               NULL,
               proc->frame_prach,
               0
1048
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1049
               ,0
1050
#endif
1051 1052 1053 1054 1055
              );
    }

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_RU_PRACH_RX, 0 );

1056
    if (release_thread(&proc->mutex_prach,&proc->instance_cnt_prach,"ru_prach_thread") < 0) break;
Raymond Knopp's avatar
Raymond Knopp committed
1057 1058
  }

1059 1060 1061 1062 1063
  LOG_I(PHY, "Exiting RU thread PRACH\n");
  ru_thread_prach_status = 0;
  return &ru_thread_prach_status;
}

1064
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1065
static void *ru_thread_prach_br( void *param ) {
1066
  static int ru_thread_prach_status;
1067 1068
  RU_t *ru        = (RU_t *)param;
  RU_proc_t *proc = (RU_proc_t *)&ru->proc;
1069 1070
  // set default return value
  ru_thread_prach_status = 0;
1071
  thread_top_init("ru_thread_prach_br",1,500000,1000000,20000000);
1072
  //wait_sync("ru_thread_prach_br");
1073 1074 1075

  while (!oai_exit) {
    if (wait_on_condition(&proc->mutex_prach_br,&proc->cond_prach_br,&proc->instance_cnt_prach_br,"ru_prach_thread_br") < 0) break;
1076

1077
    if (oai_exit) break;
1078

1079
    rx_prach(NULL,
1080 1081
             ru,
             NULL,
1082 1083 1084 1085
             NULL,
             NULL,
             proc->frame_prach_br,
             0,
1086 1087
             1);

1088 1089 1090 1091
    if (release_thread(&proc->mutex_prach_br,&proc->instance_cnt_prach_br,"ru_prach_thread_br") < 0) break;
  }

  LOG_I(PHY, "Exiting RU thread PRACH BR\n");
1092 1093
  ru_thread_prach_status = 0;
  return &ru_thread_prach_status;
Raymond Knopp's avatar
Raymond Knopp committed
1094
}
1095
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1096

1097
int wakeup_synch(RU_t *ru) {
1098 1099 1100
  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
Raymond Knopp's avatar
Raymond Knopp committed
1101

1102 1103 1104 1105 1106 1107 1108
  // wake up synch thread
  // lock the synch mutex and make sure the thread is ready
  if (pthread_mutex_timedlock(&ru->proc.mutex_synch,&wait) != 0) {
    LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for RU synch thread (IC %d)\n", ru->proc.instance_cnt_synch );
    exit_fun( "error locking mutex_synch" );
    return(-1);
  }
1109

1110
  ++ru->proc.instance_cnt_synch;
1111

1112 1113 1114 1115 1116 1117 1118
  // the thread can now be woken up
  if (pthread_cond_signal(&ru->proc.cond_synch) != 0) {
    LOG_E( PHY, "[RU] ERROR pthread_cond_signal for RU synch thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return(-1);
  }

1119
  pthread_mutex_unlock( &ru->proc.mutex_synch );
1120 1121 1122 1123 1124 1125 1126
  return(0);
}

void do_ru_synch(RU_t *ru) {
  LTE_DL_FRAME_PARMS *fp  = &ru->frame_parms;
  RU_proc_t *proc         = &ru->proc;
  int i;
Raymond Knopp's avatar
Raymond Knopp committed
1127 1128
  void *rxp[2],*rxp2[2];
  int32_t dummy_rx[ru->nb_rx][fp->samples_per_tti] __attribute__((aligned(32)));
1129 1130
  int rxs;
  int ic;
Raymond Knopp's avatar
Raymond Knopp committed
1131 1132

  // initialize the synchronization buffer to the common_vars.rxdata
1133
  for (int i=0; i<ru->nb_rx; i++)
1134
    rxp[i] = &ru->common.rxdata[i][0];
Raymond Knopp's avatar
Raymond Knopp committed
1135 1136 1137

  double temp_freq1 = ru->rfdevice.openair0_cfg->rx_freq[0];
  double temp_freq2 = ru->rfdevice.openair0_cfg->tx_freq[0];
1138 1139

  for (i=0; i<4; i++) {
Raymond Knopp's avatar
Raymond Knopp committed
1140 1141 1142
    ru->rfdevice.openair0_cfg->rx_freq[i] = ru->rfdevice.openair0_cfg->tx_freq[i];
    ru->rfdevice.openair0_cfg->tx_freq[i] = temp_freq1;
  }
1143

Raymond Knopp's avatar
Raymond Knopp committed
1144
  ru->rfdevice.trx_set_freq_func(&ru->rfdevice,ru->rfdevice.openair0_cfg,0);
1145

Raymond Knopp's avatar
Raymond Knopp committed
1146 1147 1148
  while ((ru->in_synch ==0)&&(!oai_exit)) {
    // read in frame
    rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
1149 1150 1151 1152 1153
                                     &(proc->timestamp_rx),
                                     rxp,
                                     fp->samples_per_tti*10,
                                     ru->nb_rx);

1154
    if (rxs != fp->samples_per_tti*10) LOG_E(PHY,"requested %d samples, got %d\n",fp->samples_per_tti*10,rxs);
1155

Raymond Knopp's avatar
Raymond Knopp committed
1156 1157 1158
    // wakeup synchronization processing thread
    wakeup_synch(ru);
    ic=0;
1159

Raymond Knopp's avatar
Raymond Knopp committed
1160
    while ((ic>=0)&&(!oai_exit)) {
1161
      // continuously read in frames, 1ms at a time,
Raymond Knopp's avatar
Raymond Knopp committed
1162
      // until we are done with the synchronization procedure
1163
      for (i=0; i<ru->nb_rx; i++)
1164 1165 1166 1167 1168 1169 1170 1171 1172
        rxp2[i] = (void *)&dummy_rx[i][0];

      for (i=0; i<10; i++)
        rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
                                         &(proc->timestamp_rx),
                                         rxp2,
                                         fp->samples_per_tti,
                                         ru->nb_rx);

1173 1174 1175
      pthread_mutex_lock(&ru->proc.mutex_synch);
      ic = ru->proc.instance_cnt_synch;
      pthread_mutex_unlock(&ru->proc.mutex_synch);
Raymond Knopp's avatar
Raymond Knopp committed
1176 1177
    } // ic>=0
  } // in_synch==0
1178 1179

  // read in rx_offset samples
Raymond Knopp's avatar
Raymond Knopp committed
1180 1181
  LOG_I(PHY,"Resynchronizing by %d samples\n",ru->rx_offset);
  rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
1182 1183 1184 1185 1186 1187
                                   &(proc->timestamp_rx),
                                   rxp,
                                   ru->rx_offset,
                                   ru->nb_rx);

  for (i=0; i<4; i++) {
Raymond Knopp's avatar
Raymond Knopp committed
1188 1189 1190 1191 1192 1193 1194
    ru->rfdevice.openair0_cfg->rx_freq[i] = temp_freq1;
    ru->rfdevice.openair0_cfg->tx_freq[i] = temp_freq2;
  }

  ru->rfdevice.trx_set_freq_func(&ru->rfdevice,ru->rfdevice.openair0_cfg,0);
}

1195 1196


1197
void wakeup_L1s(RU_t *ru) {
1198 1199
  int i;
  PHY_VARS_eNB **eNB_list = ru->eNB_list;
1200
  LOG_D(PHY,"wakeup_L1s (num %d) for RU %d ru->eNB_top:%p\n",ru->num_eNB,ru->idx, ru->eNB_top);
1201

1202
  if (ru->num_eNB==1 && ru->eNB_top!=0 && get_thread_parallel_conf() == PARALLEL_SINGLE_THREAD) {
1203 1204 1205
    // call eNB function directly
    char string[20];
    sprintf(string,"Incoming RU %d",ru->idx);
Cedric Roux's avatar
Cedric Roux committed
1206
    LOG_D(PHY,"RU %d Call eNB_top\n",ru->idx);
1207
    ru->eNB_top(eNB_list[0],ru->proc.frame_rx,ru->proc.subframe_rx,string,ru);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1208
    ru->proc.emulate_rf_busy = 0;
1209
  } else {
1210 1211
    LOG_D(PHY,"ru->num_eNB:%d\n", ru->num_eNB);

1212
    for (i=0; i<ru->num_eNB; i++) {
1213
      LOG_D(PHY,"ru->wakeup_rxtx:%p\n", ru->wakeup_rxtx);
1214 1215

      if (ru->wakeup_rxtx!=0 && ru->wakeup_rxtx(eNB_list[i],ru) < 0) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1216
        LOG_E(PHY,"could not wakeup eNB rxtx process for subframe %d\n", ru->proc.subframe_rx);
1217
      }
1218

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1219
      ru->proc.emulate_rf_busy = 0;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1220
    }
1221 1222 1223
  }
}

1224
static inline int wakeup_prach_ru(RU_t *ru) {
1225 1226 1227 1228 1229 1230 1231 1232 1233
  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

  if (pthread_mutex_timedlock(&ru->proc.mutex_prach,&wait) !=0) {
    LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for RU prach thread (IC %d)\n", ru->proc.instance_cnt_prach);
    exit_fun( "error locking mutex_rxtx" );
    return(-1);
  }
1234

1235 1236 1237 1238
  if (ru->proc.instance_cnt_prach==-1) {
    ++ru->proc.instance_cnt_prach;
    ru->proc.frame_prach    = ru->proc.frame_rx;
    ru->proc.subframe_prach = ru->proc.subframe_rx;
1239

1240
    // DJP - think prach_procedures() is looking at eNB frame_prach
1241 1242 1243 1244
    if (ru->eNB_list[0]) {
      ru->eNB_list[0]->proc.frame_prach = ru->proc.frame_rx;
      ru->eNB_list[0]->proc.subframe_prach = ru->proc.subframe_rx;
    }
1245

1246
    LOG_D(PHY,"RU %d: waking up PRACH thread\n",ru->idx);
1247 1248
    // the thread can now be woken up
    AssertFatal(pthread_cond_signal(&ru->proc.cond_prach) == 0, "ERROR pthread_cond_signal for RU prach thread\n");
1249
  } else LOG_W(PHY,"RU prach thread busy, skipping\n");
1250

1251
  pthread_mutex_unlock( &ru->proc.mutex_prach );
1252 1253 1254
  return(0);
}

1255
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
static inline int wakeup_prach_ru_br(RU_t *ru) {
  struct timespec wait;
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

  if (pthread_mutex_timedlock(&ru->proc.mutex_prach_br,&wait) !=0) {
    LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for RU prach thread BR (IC %d)\n", ru->proc.instance_cnt_prach_br);
    exit_fun( "error locking mutex_rxtx" );
    return(-1);
  }
1266

1267 1268 1269 1270 1271 1272 1273
  if (ru->proc.instance_cnt_prach_br==-1) {
    ++ru->proc.instance_cnt_prach_br;
    ru->proc.frame_prach_br    = ru->proc.frame_rx;
    ru->proc.subframe_prach_br = ru->proc.subframe_rx;
    LOG_D(PHY,"RU %d: waking up PRACH thread\n",ru->idx);
    // the thread can now be woken up
    AssertFatal(pthread_cond_signal(&ru->proc.cond_prach_br) == 0, "ERROR pthread_cond_signal for RU prach thread BR\n");
1274
  } else LOG_W(PHY,"RU prach thread busy, skipping\n");
1275

1276
  pthread_mutex_unlock( &ru->proc.mutex_prach_br );
1277 1278 1279 1280
  return(0);
}
#endif

Raymond Knopp's avatar
Raymond Knopp committed
1281 1282 1283 1284 1285
// this is for RU with local RF unit
void fill_rf_config(RU_t *ru, char *rf_config_file) {
  int i;
  LTE_DL_FRAME_PARMS *fp   = &ru->frame_parms;
  openair0_config_t *cfg   = &ru->openair0_cfg;
1286
  //printf("////////////////numerology in config = %d\n",numerology);
1287
  int numerology = get_softmodem_params()->numerology;
1288

Raymond Knopp's avatar
Raymond Knopp committed
1289
  if(fp->N_RB_DL == 100) {
1290
    if(numerology == 0) {
1291 1292
      if (fp->threequarter_fs) {
        cfg->sample_rate=23.04e6;
1293
        cfg->samples_per_frame = 230400;
1294 1295
        cfg->tx_bw = 10e6;
        cfg->rx_bw = 10e6;
1296
      } else {
1297
        cfg->sample_rate=30.72e6;
1298
        cfg->samples_per_frame = 307200;
1299 1300 1301
        cfg->tx_bw = 10e6;
        cfg->rx_bw = 10e6;
      }
1302 1303 1304
    } else if(numerology == 1) {
      cfg->sample_rate=61.44e6;
      cfg->samples_per_frame = 307200;
1305 1306
      cfg->tx_bw = 20e6;
      cfg->rx_bw = 20e6;
1307 1308 1309
    } else if(numerology == 2) {
      cfg->sample_rate=122.88e6;
      cfg->samples_per_frame = 307200;
1310 1311
      cfg->tx_bw = 40e6;
      cfg->rx_bw = 40e6;
1312 1313 1314 1315
    } else {
      printf("Wrong input for numerology %d\n setting to 20MHz normal CP configuration",numerology);
      cfg->sample_rate=30.72e6;
      cfg->samples_per_frame = 307200;
Raymond Knopp's avatar
Raymond Knopp committed
1316 1317
      cfg->tx_bw = 10e6;
      cfg->rx_bw = 10e6;
1318
    }
Raymond Knopp's avatar
Raymond Knopp committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
  } else if(fp->N_RB_DL == 50) {
    cfg->sample_rate=15.36e6;
    cfg->samples_per_frame = 153600;
    cfg->tx_bw = 5e6;
    cfg->rx_bw = 5e6;
  } else if (fp->N_RB_DL == 25) {
    cfg->sample_rate=7.68e6;
    cfg->samples_per_frame = 76800;
    cfg->tx_bw = 2.5e6;
    cfg->rx_bw = 2.5e6;
  } else if (fp->N_RB_DL == 6) {
    cfg->sample_rate=1.92e6;
    cfg->samples_per_frame = 19200;
    cfg->tx_bw = 1.5e6;
    cfg->rx_bw = 1.5e6;
1334
  } else AssertFatal(1==0,"Unknown N_RB_DL %d\n",fp->N_RB_DL);
Raymond Knopp's avatar
Raymond Knopp committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344

  if (fp->frame_type==TDD)
    cfg->duplex_mode = duplex_mode_TDD;
  else //FDD
    cfg->duplex_mode = duplex_mode_FDD;

  cfg->Mod_id = 0;
  cfg->num_rb_dl=fp->N_RB_DL;
  cfg->tx_num_channels=ru->nb_tx;
  cfg->rx_num_channels=ru->nb_rx;
1345
  cfg->clock_source=get_softmodem_params()->clock_source;
1346

Raymond Knopp's avatar
Raymond Knopp committed
1347 1348 1349
  for (i=0; i<ru->nb_tx; i++) {
    cfg->tx_freq[i] = (double)fp->dl_CarrierFreq;
    cfg->rx_freq[i] = (double)fp->ul_CarrierFreq;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1350 1351
    cfg->tx_gain[i] = (double)ru->att_tx;
    cfg->rx_gain[i] = ru->max_rxgain-(double)ru->att_rx;
Raymond Knopp's avatar
Raymond Knopp committed
1352 1353
    cfg->configFilename = rf_config_file;
    printf("channel %d, Setting tx_gain offset %f, rx_gain offset %f, tx_freq %f, rx_freq %f\n",
1354 1355 1356 1357
           i, cfg->tx_gain[i],
           cfg->rx_gain[i],
           cfg->tx_freq[i],
           cfg->rx_freq[i]);
Raymond Knopp's avatar
Raymond Knopp committed
1358 1359 1360 1361 1362 1363 1364 1365
  }
}

/* this function maps the RU 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 antenna port, on which rf chain the mapping should start. Multiple
   antennas are mapped to successive RF chains on the same card. */
int setup_RU_buffers(RU_t *ru) {
1366
  int i,j;
Raymond Knopp's avatar
Raymond Knopp committed
1367 1368 1369
  int card,ant;
  //uint16_t N_TA_offset = 0;
  LTE_DL_FRAME_PARMS *frame_parms;
1370

Raymond Knopp's avatar
Raymond Knopp committed
1371 1372 1373 1374
  if (ru) {
    frame_parms = &ru->frame_parms;
    printf("setup_RU_buffers: frame_parms = %p\n",frame_parms);
  } else {
1375
    printf("RU not initialized (NULL pointer)\n");
Raymond Knopp's avatar
Raymond Knopp committed
1376 1377
    return(-1);
  }
1378

1379 1380 1381 1382
  if (frame_parms->frame_type == TDD) {
    if      (frame_parms->N_RB_DL == 100) ru->N_TA_offset = 624;
    else if (frame_parms->N_RB_DL == 50)  ru->N_TA_offset = 624/2;
    else if (frame_parms->N_RB_DL == 25)  ru->N_TA_offset = 624/4;
1383

1384 1385 1386 1387 1388 1389
#if BASIC_SIMULATOR
    /* this is required for the basic simulator in TDD mode
     * TODO: find a proper cleaner solution
     */
    ru->N_TA_offset = 0;
#endif
1390 1391
  }

Raymond Knopp's avatar
Raymond Knopp committed
1392 1393 1394 1395 1396 1397 1398 1399 1400
  if (ru->openair0_cfg.mmapped_dma == 1) {
    // replace RX signal buffers with mmaped HW versions
    for (i=0; i<ru->nb_rx; i++) {
      card = i/4;
      ant = i%4;
      printf("Mapping RU id %d, rx_ant %d, on card %d, chain %d\n",ru->idx,i,ru->rf_map.card+card, ru->rf_map.chain+ant);
      free(ru->common.rxdata[i]);
      ru->common.rxdata[i] = ru->openair0_cfg.rxbase[ru->rf_map.chain+ant];
      printf("rxdata[%d] @ %p\n",i,ru->common.rxdata[i]);
1401

Raymond Knopp's avatar
Raymond Knopp committed
1402
      for (j=0; j<16; j++) {
1403 1404
        printf("rxbuffer %d: %x\n",j,ru->common.rxdata[i][j]);
        ru->common.rxdata[i][j] = 16-j;
Raymond Knopp's avatar
Raymond Knopp committed
1405 1406
      }
    }
1407

Raymond Knopp's avatar
Raymond Knopp committed
1408 1409 1410 1411 1412 1413 1414
    for (i=0; i<ru->nb_tx; i++) {
      card = i/4;
      ant = i%4;
      printf("Mapping RU id %d, tx_ant %d, on card %d, chain %d\n",ru->idx,i,ru->rf_map.card+card, ru->rf_map.chain+ant);
      free(ru->common.txdata[i]);
      ru->common.txdata[i] = ru->openair0_cfg.txbase[ru->rf_map.chain+ant];
      printf("txdata[%d] @ %p\n",i,ru->common.txdata[i]);
1415

Raymond Knopp's avatar
Raymond Knopp committed
1416
      for (j=0; j<16; j++) {
1417 1418
        printf("txbuffer %d: %x\n",j,ru->common.txdata[i][j]);
        ru->common.txdata[i][j] = 16-j;
Raymond Knopp's avatar
Raymond Knopp committed
1419 1420
      }
    }
1421
  } else { // not memory-mapped DMA
Raymond Knopp's avatar
Raymond Knopp committed
1422 1423
    //nothing to do, everything already allocated in lte_init
  }
1424

Raymond Knopp's avatar
Raymond Knopp committed
1425 1426 1427
  return(0);
}

1428 1429
static void *ru_stats_thread(void *param) {
  RU_t               *ru      = (RU_t *)param;
1430 1431 1432
  wait_sync("ru_stats_thread");

  while (!oai_exit) {
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    sleep(1);

    if (opp_enabled) {
      if (ru->feprx) print_meas(&ru->ofdm_demod_stats,"feprx",NULL,NULL);

      if (ru->feptx_ofdm) print_meas(&ru->ofdm_mod_stats,"feptx_ofdm",NULL,NULL);

      if (ru->fh_north_asynch_in) print_meas(&ru->rx_fhaul,"rx_fhaul",NULL,NULL);

      if (ru->fh_north_out) {
        print_meas(&ru->tx_fhaul,"tx_fhaul",NULL,NULL);
        print_meas(&ru->compression,"compression",NULL,NULL);
        print_meas(&ru->transport,"transport",NULL,NULL);
      }
    }
1448
  }
1449

1450
  return(NULL);
1451 1452
}

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1453
#ifdef PHY_TX_THREAD
1454 1455 1456
  int first_phy_tx = 1;
  volatile int16_t phy_tx_txdataF_end;
  volatile int16_t phy_tx_end;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1457 1458
#endif

1459 1460
static void *ru_thread_tx( void *param ) {
  RU_t *ru              = (RU_t *)param;
1461 1462
  RU_proc_t *proc       = &ru->proc;
  PHY_VARS_eNB *eNB;
1463 1464
  L1_proc_t *eNB_proc;
  L1_rxtx_proc_t *L1_proc;
1465 1466
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
1467 1468 1469
  thread_top_init("ru_thread_tx",1,400000,500000,500000);
  //CPU_SET(5, &cpuset);
  //pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1470
  //wait_sync("ru_thread_tx");
1471 1472
  wait_on_condition(&proc->mutex_FH1,&proc->cond_FH1,&proc->instance_cnt_FH1,"ru_thread_tx");
  printf( "ru_thread_tx ready\n");
1473

1474 1475
  while (!oai_exit) {
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD_TX,sched_getcpu());
1476

1477
    if (oai_exit) break;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1478

1479 1480
    LOG_D(PHY,"ru_thread_tx: Waiting for TX processing\n");
    // wait until eNBs are finished subframe RX n and TX n+4
1481
    wait_on_condition(&proc->mutex_eNBs,&proc->cond_eNBs,&proc->instance_cnt_eNBs,"ru_thread_tx");
1482

WANG Tsu-Han's avatar
WANG Tsu-Han committed
1483
    if (oai_exit) break;
1484

1485 1486
    // do TX front-end processing if needed (precoding and/or IDFTs)
    if (ru->feptx_prec) ru->feptx_prec(ru);
1487

1488 1489
    // do OFDM if needed
    if ((ru->fh_north_asynch_in == NULL) && (ru->feptx_ofdm)) ru->feptx_ofdm(ru);
1490 1491

    if(!(get_softmodem_params()->emulate_rf)) {
1492 1493
      // do outgoing fronthaul (south) if needed
      if ((ru->fh_north_asynch_in == NULL) && (ru->fh_south_out)) ru->fh_south_out(ru);
1494

1495
      if (ru->fh_north_out) ru->fh_north_out(ru);
1496 1497
    }

1498
    release_thread(&proc->mutex_eNBs,&proc->instance_cnt_eNBs,"ru_thread_tx");
1499 1500

    for(int i = 0; i<ru->num_eNB; i++) {
1501 1502 1503
      eNB       = ru->eNB_list[i];
      eNB_proc  = &eNB->proc;
      L1_proc   = (get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT)? &eNB_proc->L1_proc_tx : &eNB_proc->L1_proc;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1504
      pthread_mutex_lock(&eNB_proc->mutex_RU_tx);
1505 1506

      for (int j=0; j<eNB->num_RU; j++) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1507
        if (ru == eNB->RU_list[j]) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1508
          if ((eNB_proc->RU_mask_tx&(1<<j)) > 0)
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1509
            LOG_E(PHY,"eNB %d frame %d, subframe %d : previous information from RU tx %d (num_RU %d,mask %x) has not been served yet!\n",
1510 1511
                  eNB->Mod_id,eNB_proc->frame_rx,eNB_proc->subframe_rx,ru->idx,eNB->num_RU,eNB_proc->RU_mask_tx);

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1512
          eNB_proc->RU_mask_tx |= (1<<j);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1513 1514
        }
      }
1515

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1516 1517
      if (eNB_proc->RU_mask_tx != (1<<eNB->num_RU)-1) {  // not all RUs have provided their information so return
        pthread_mutex_unlock(&eNB_proc->mutex_RU_tx);
1518
      } else { // all RUs TX are finished so send the ready signal to eNB processing
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1519 1520 1521 1522
        eNB_proc->RU_mask_tx = 0;
        pthread_mutex_unlock(&eNB_proc->mutex_RU_tx);
        pthread_mutex_lock( &L1_proc->mutex_RUs);
        L1_proc->instance_cnt_RUs = 0;
1523

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1524 1525 1526 1527 1528
        // the thread can now be woken up
        if (pthread_cond_signal(&L1_proc->cond_RUs) != 0) {
          LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB TXnp4 thread\n");
          exit_fun( "ERROR pthread_cond_signal" );
        }
1529

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1530
        pthread_mutex_unlock( &L1_proc->mutex_RUs );
1531
      }
1532
    }
1533
  }
1534

1535
  release_thread(&proc->mutex_FH1,&proc->instance_cnt_FH1,"ru_thread_tx");
1536 1537 1538
  return 0;
}

1539
static void *ru_thread( void *param ) {
Raymond Knopp's avatar
Raymond Knopp committed
1540
  static int ru_thread_status;
1541
  RU_t               *ru      = (RU_t *)param;
1542 1543 1544
  RU_proc_t          *proc    = &ru->proc;
  LTE_DL_FRAME_PARMS *fp      = &ru->frame_parms;
  int                ret;
1545
  int                subframe =9;
1546
  int                frame    =1023;
1547 1548
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
Raymond Knopp's avatar
Raymond Knopp committed
1549 1550
  // set default return value
  ru_thread_status = 0;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1551 1552 1553
#if defined(PRE_SCD_THREAD)
  dlsch_ue_select_tbl_in_use = 1;
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1554
  // set default return value
1555
  thread_top_init("ru_thread",1,400000,500000,500000);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1556 1557
  //CPU_SET(1, &cpuset);
  //pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1558 1559
  pthread_setname_np( pthread_self(),"ru thread");
  LOG_I(PHY,"thread ru created id=%ld\n", syscall(__NR_gettid));
1560 1561
  LOG_I(PHY,"Starting RU %d (%s,%s),\n",ru->idx,eNB_functions[ru->function],eNB_timing[ru->if_timing]);

1562
  if(get_softmodem_params()->emulate_rf) {
1563 1564 1565
    fill_rf_config(ru,ru->rf_config_file);
    init_frame_parms(&ru->frame_parms,1);
    phy_init_RU(ru);
1566

1567
    if (setup_RU_buffers(ru)!=0) {
1568 1569
      printf("Exiting, cannot initialize RU Buffers\n");
      exit(-1);
1570
    }
1571
  } else {
1572 1573 1574 1575
    // Start IF device if any
    if (ru->start_if) {
      LOG_I(PHY,"Starting IF interface for RU %d\n",ru->idx);
      AssertFatal(ru->start_if(ru,NULL) == 0, "Could not start the IF device\n");
1576

1577 1578
      if (ru->if_south == LOCAL_RF) ret = connect_rau(ru);
      else ret = attach_rru(ru);
1579

1580 1581
      AssertFatal(ret==0,"Cannot connect to radio\n");
    }
1582 1583 1584 1585 1586 1587

    if (ru->if_south == LOCAL_RF) { // configure RF parameters only
      fill_rf_config(ru,ru->rf_config_file);
      init_frame_parms(&ru->frame_parms,1);
      phy_init_RU(ru);
      ret = openair0_device_load(&ru->rfdevice,&ru->openair0_cfg);
1588
    }
1589

1590
    if (setup_RU_buffers(ru)!=0) {
1591 1592
      printf("Exiting, cannot initialize RU Buffers\n");
      exit(-1);
1593
    }
1594
  }
1595

1596 1597 1598 1599 1600
  LOG_I(PHY, "Signaling main thread that RU %d is ready\n",ru->idx);
  pthread_mutex_lock(&RC.ru_mutex);
  RC.ru_mask &= ~(1<<ru->idx);
  pthread_cond_signal(&RC.ru_cond);
  pthread_mutex_unlock(&RC.ru_mutex);
1601 1602 1603 1604 1605
  pthread_mutex_lock(&proc->mutex_FH1);
  proc->instance_cnt_FH1 = 0;
  pthread_mutex_unlock(&proc->mutex_FH1);
  pthread_cond_signal(&proc->cond_FH1);
  wait_sync("ru_thread");
Raymond Knopp's avatar
Raymond Knopp committed
1606

1607
  if(!(get_softmodem_params()->emulate_rf)) {
1608 1609 1610 1611 1612
    // Start RF device if any
    if (ru->start_rf) {
      if (ru->start_rf(ru) != 0)
        LOG_E(HW,"Could not start the RF device\n");
      else LOG_I(PHY,"RU %d rf device ready\n",ru->idx);
1613 1614
    } else LOG_I(PHY,"RU %d no rf device\n",ru->idx);

1615 1616
    // if an asnych_rxtx thread exists
    // wakeup the thread because the devices are ready at this point
1617

1618 1619 1620 1621 1622
    if ((ru->fh_south_asynch_in)||(ru->fh_north_asynch_in)) {
      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);
1623 1624
    } else LOG_I(PHY,"RU %d no asynch_south interface\n",ru->idx);

1625 1626
    // if this is a slave RRU, try to synchronize on the DL frequency
    if ((ru->is_slave) && (ru->if_south == LOCAL_RF)) do_ru_synch(ru);
1627 1628
  }

Raymond Knopp's avatar
Raymond Knopp committed
1629 1630
  // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
  while (!oai_exit) {
1631 1632
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_CPUID_RU_THREAD,sched_getcpu());

Raymond Knopp's avatar
Raymond Knopp committed
1633 1634
    // 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.
1635
    if (subframe==9) {
Raymond Knopp's avatar
Raymond Knopp committed
1636 1637 1638 1639 1640
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
1641
    }
Raymond Knopp's avatar
Raymond Knopp committed
1642 1643

    // synchronization on input FH interface, acquire signals/data and block
1644
    if (ru->fh_south_in) ru->fh_south_in(ru,&frame,&subframe);
Raymond Knopp's avatar
Raymond Knopp committed
1645 1646
    else AssertFatal(1==0, "No fronthaul interface at south port");

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1647
#ifdef PHY_TX_THREAD
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

    if(first_phy_tx == 0) {
      phy_tx_end = 0;
      phy_tx_txdataF_end = 0;

      if(pthread_mutex_lock(&ru->proc.mutex_phy_tx) != 0) {
        LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for phy tx thread (IC %d)\n", ru->proc.instance_cnt_phy_tx);
        exit_fun( "error locking mutex_rxtx" );
      }

      if (ru->proc.instance_cnt_phy_tx==-1) {
        ++ru->proc.instance_cnt_phy_tx;
        // the thread can now be woken up
        AssertFatal(pthread_cond_signal(&ru->proc.cond_phy_tx) == 0, "ERROR pthread_cond_signal for phy_tx thread\n");
      } else {
        LOG_E(PHY,"phy tx thread busy, skipping\n");
        ++ru->proc.instance_cnt_phy_tx;
      }

      pthread_mutex_unlock( &ru->proc.mutex_phy_tx );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1668
    } else {
1669 1670
      phy_tx_end = 1;
      phy_tx_txdataF_end = 1;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1671
    }
1672

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1673 1674
    first_phy_tx = 0;
#endif
1675
    LOG_D(PHY,"RU thread (do_prach %d, is_prach_subframe %d), received frame %d, subframe %d\n",
1676 1677 1678
          ru->do_prach,
          is_prach_subframe(fp, proc->frame_rx, proc->subframe_rx),
          proc->frame_rx,proc->subframe_rx);
1679

Cedric Roux's avatar
Cedric Roux committed
1680
    if ((ru->do_prach>0) && (is_prach_subframe(fp, proc->frame_rx, proc->subframe_rx)==1)) {
1681 1682
      wakeup_prach_ru(ru);
    }
1683

1684
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1685 1686 1687
    else if ((ru->do_prach>0) && (is_prach_subframe(fp, proc->frame_rx, proc->subframe_rx)>1)) {
      wakeup_prach_ru_br(ru);
    }
1688

1689
#endif
1690 1691 1692 1693

    // adjust for timing offset between RU
    if (ru->idx!=0) proc->frame_tx = (proc->frame_tx+proc->frame_offset)&1023;

Raymond Knopp's avatar
Raymond Knopp committed
1694
    // do RX front-end processing (frequency-shift, dft) if needed
1695
    if (ru->feprx) ru->feprx(ru);
Raymond Knopp's avatar
Raymond Knopp committed
1696 1697 1698 1699

    // 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);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1700 1701 1702 1703 1704
#if defined(PRE_SCD_THREAD)
    new_dlsch_ue_select_tbl_in_use = dlsch_ue_select_tbl_in_use;
    dlsch_ue_select_tbl_in_use = !dlsch_ue_select_tbl_in_use;
    memcpy(&pre_scd_eNB_UE_stats,&RC.mac[ru->eNB_list[0]->Mod_id]->UE_list.eNB_UE_stats, sizeof(eNB_UE_STATS)*MAX_NUM_CCs*NUMBER_OF_UE_MAX);
    memcpy(&pre_scd_activeUE, &RC.mac[ru->eNB_list[0]->Mod_id]->UE_list.active, sizeof(boolean_t)*NUMBER_OF_UE_MAX);
1705

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1706
    if (pthread_mutex_lock(&ru->proc.mutex_pre_scd)!= 0) {
1707 1708
      LOG_E( PHY, "[eNB] error locking proc mutex for eNB pre scd\n");
      exit_fun("error locking mutex_time");
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1709
    }
1710

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1711
    ru->proc.instance_pre_scd++;
Raymond Knopp's avatar
Raymond Knopp committed
1712

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1713
    if (ru->proc.instance_pre_scd == 0) {
1714 1715 1716 1717 1718 1719 1720
      if (pthread_cond_signal(&ru->proc.cond_pre_scd) != 0) {
        LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB pre scd\n" );
        exit_fun( "ERROR pthread_cond_signal cond_pre_scd" );
      }
    } else {
      LOG_E( PHY, "[eNB] frame %d subframe %d rxtx busy instance_pre_scd %d\n",
             frame,subframe,ru->proc.instance_pre_scd );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1721
    }
1722

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1723
    if (pthread_mutex_unlock(&ru->proc.mutex_pre_scd)!= 0) {
1724 1725
      LOG_E( PHY, "[eNB] error unlocking mutex_pre_scd mutex for eNB pre scd\n");
      exit_fun("error unlocking mutex_pre_scd");
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1726
    }
1727

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1728
#endif
1729

Raymond Knopp's avatar
Raymond Knopp committed
1730
    // wakeup all eNB processes waiting for this RU
1731
    if (ru->num_eNB>0) wakeup_L1s(ru);
1732

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1733
#ifndef PHY_TX_THREAD
1734 1735

    if(get_thread_parallel_conf() == PARALLEL_SINGLE_THREAD || ru->num_eNB==0) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1736 1737
      // do TX front-end processing if needed (precoding and/or IDFTs)
      if (ru->feptx_prec) ru->feptx_prec(ru);
1738

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1739 1740
      // do OFDM if needed
      if ((ru->fh_north_asynch_in == NULL) && (ru->feptx_ofdm)) ru->feptx_ofdm(ru);
1741 1742

      if(!(get_softmodem_params()->emulate_rf)) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1743 1744
        // do outgoing fronthaul (south) if needed
        if ((ru->fh_north_asynch_in == NULL) && (ru->fh_south_out)) ru->fh_south_out(ru);
1745

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1746
        if (ru->fh_north_out) ru->fh_north_out(ru);
1747
      }
1748

1749
      proc->emulate_rf_busy = 0;
1750
    }
1751

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1752 1753 1754 1755 1756
#else
    struct timespec time_req, time_rem;
    time_req.tv_sec = 0;
    time_req.tv_nsec = 10000;

1757 1758 1759
    while((!oai_exit)&&(phy_tx_end == 0)) {
      nanosleep(&time_req,&time_rem);
      continue;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1760
    }
1761

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1762
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1763 1764 1765
  }

  printf( "Exiting ru_thread \n");
1766

1767
  if (!(get_softmodem_params()->emulate_rf)) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1768 1769 1770 1771 1772
    if (ru->stop_rf != NULL) {
      if (ru->stop_rf(ru) != 0)
        LOG_E(HW,"Could not stop the RF device\n");
      else LOG_I(PHY,"RU %d rf device stopped\n",ru->idx);
    }
1773 1774
  }

Raymond Knopp's avatar
Raymond Knopp committed
1775 1776 1777 1778 1779 1780 1781
  ru_thread_status = 0;
  return &ru_thread_status;
}


// This thread run the initial synchronization like a UE
void *ru_thread_synch(void *arg) {
1782
  RU_t *ru = (RU_t *)arg;
Raymond Knopp's avatar
Raymond Knopp committed
1783 1784 1785 1786
  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;
  int32_t sync_pos,sync_pos2;
  uint32_t peak_val;
  uint32_t sync_corr[307200] __attribute__((aligned(32)));
1787
  static int ru_thread_synch_status;
Raymond Knopp's avatar
Raymond Knopp committed
1788 1789 1790
  thread_top_init("ru_thread_synch",0,5000000,10000000,10000000);
  wait_sync("ru_thread_synch");
  // initialize variables for PSS detection
1791
  lte_sync_time_init(&ru->frame_parms);
Raymond Knopp's avatar
Raymond Knopp committed
1792 1793 1794

  while (!oai_exit) {
    // wait to be woken up
1795
    if (wait_on_condition(&ru->proc.mutex_synch,&ru->proc.cond_synch,&ru->proc.instance_cnt_synch,"ru_thread_synch")<0) break;
Raymond Knopp's avatar
Raymond Knopp committed
1796 1797

    // if we're not in synch, then run initial synch
1798
    if (ru->in_synch == 0) {
Raymond Knopp's avatar
Raymond Knopp committed
1799 1800
      // run intial synch like UE
      LOG_I(PHY,"Running initial synchronization\n");
1801
      sync_pos = lte_sync_time_eNB(ru->common.rxdata,
1802 1803 1804 1805
                                   fp,
                                   fp->samples_per_tti*5,
                                   &peak_val,
                                   sync_corr);
Raymond Knopp's avatar
Raymond Knopp committed
1806 1807 1808
      LOG_I(PHY,"RU synch: %d, val %d\n",sync_pos,peak_val);

      if (sync_pos >= 0) {
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
        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)
            ru->rx_offset = sync_pos2 - sync_pos_slot;
          else
            ru->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,ru->rx_offset);

        if (LOG_DEBUGFLAG(RU)) {
          if ((peak_val > 300000) && (sync_pos > 0)) {
            LOG_M("ru_sync.m","sync",(void *)&sync_corr[0],fp->samples_per_tti*5,1,2);
            LOG_M("ru_rx.m","rxs",&(ru->eNB_list[0]->common_vars.rxdata[0][0]),fp->samples_per_tti*10,1,1);
            exit(-1);
          }
1833
        }
1834 1835

        ru->in_synch=1;
Raymond Knopp's avatar
Raymond Knopp committed
1836 1837 1838 1839 1840 1841
      }
    }

    if (release_thread(&ru->proc.mutex_synch,&ru->proc.instance_cnt_synch,"ru_synch_thread") < 0) break;
  } // oai_exit

1842 1843
  ru_thread_synch_status = 0;
  return &ru_thread_synch_status;
Raymond Knopp's avatar
Raymond Knopp committed
1844 1845
}

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1846
#if defined(PRE_SCD_THREAD)
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
void *pre_scd_thread( void *param ) {
  static int              eNB_pre_scd_status;
  protocol_ctxt_t         ctxt;
  int                     frame;
  int                     subframe;
  int                     min_rb_unit[MAX_NUM_CCs];
  int                     CC_id;
  int                     Mod_id;
  RU_t               *ru      = (RU_t *)param;

  // L2-emulator can work only one eNB
  if( nfapi_mode == 2)
    Mod_id = 0;
  else
    Mod_id = ru->eNB_list[0]->Mod_id;

  frame = 0;
  subframe = 4;
  thread_top_init("pre_scd_thread",0,870000,1000000,1000000);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1866

1867 1868 1869 1870
  while (!oai_exit) {
    if(oai_exit) {
      break;
    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1871

1872
    pthread_mutex_lock(&ru->proc.mutex_pre_scd );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1873

1874 1875 1876
    if (ru->proc.instance_pre_scd < 0) {
      pthread_cond_wait(&ru->proc.cond_pre_scd, &ru->proc.mutex_pre_scd);
    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1877

1878 1879 1880 1881 1882 1883 1884 1885
    pthread_mutex_unlock(&ru->proc.mutex_pre_scd);
    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, Mod_id, ENB_FLAG_YES,
                                   NOT_A_RNTI, frame, subframe,Mod_id);
    pdcp_run(&ctxt);

    for (CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
      rrc_rx_tx(&ctxt, CC_id);
      min_rb_unit[CC_id] = get_min_rb_unit(Mod_id, CC_id);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1886
    }
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904

    pre_scd_nb_rbs_required(Mod_id, frame, subframe,min_rb_unit,pre_nb_rbs_required[new_dlsch_ue_select_tbl_in_use]);

    if (subframe==9) {
      subframe=0;
      frame++;
      frame&=1023;
    } else {
      subframe++;
    }

    pthread_mutex_lock(&ru->proc.mutex_pre_scd );
    ru->proc.instance_pre_scd--;
    pthread_mutex_unlock(&ru->proc.mutex_pre_scd);
  }

  eNB_pre_scd_status = 0;
  return &eNB_pre_scd_status;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1905 1906 1907 1908 1909 1910
}
#endif

#ifdef PHY_TX_THREAD
/*!
 * \brief The phy tx thread of eNB.
1911
 * \param param is a \ref L1_proc_t structure which contains the info what to process.
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1912 1913
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1914
static void *eNB_thread_phy_tx( void *param ) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1915
  static int eNB_thread_phy_tx_status;
1916
  RU_t *ru      = (RU_t *)param;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1917 1918
  RU_proc_t *proc = &ru->proc;
  PHY_VARS_eNB **eNB_list = ru->eNB_list;
1919
  L1_rxtx_proc_t L1_proc;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
  // set default return value
  eNB_thread_phy_tx_status = 0;
  thread_top_init("eNB_thread_phy_tx",1,500000L,1000000L,20000000L);

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

    if (wait_on_condition(&proc->mutex_phy_tx,&proc->cond_phy_tx,&proc->instance_cnt_phy_tx,"eNB_phy_tx_thread") < 0) break;

    LOG_D(PHY,"Running eNB phy tx procedures\n");
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954

    if(ru->num_eNB == 1) {
      L1_proc.subframe_tx = proc->subframe_phy_tx;
      L1_proc.frame_tx = proc->frame_phy_tx;
      phy_procedures_eNB_TX(eNB_list[0], &L1_proc, 1);
      phy_tx_txdataF_end = 1;

      if(pthread_mutex_lock(&ru->proc.mutex_rf_tx) != 0) {
        LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for rf tx thread (IC %d)\n", ru->proc.instance_cnt_rf_tx);
        exit_fun( "error locking mutex_rf_tx" );
      }

      if (ru->proc.instance_cnt_rf_tx==-1) {
        ++ru->proc.instance_cnt_rf_tx;
        ru->proc.frame_tx = proc->frame_phy_tx;
        ru->proc.subframe_tx = proc->subframe_phy_tx;
        ru->proc.timestamp_tx = proc->timestamp_phy_tx;
        // the thread can now be woken up
        AssertFatal(pthread_cond_signal(&ru->proc.cond_rf_tx) == 0, "ERROR pthread_cond_signal for rf_tx thread\n");
      } else {
        LOG_E(PHY,"rf tx thread busy, skipping\n");
        late_control=STATE_BURST_TERMINATE;
      }

      pthread_mutex_unlock( &ru->proc.mutex_rf_tx );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1955
    }
1956

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1957
    if (release_thread(&proc->mutex_phy_tx,&proc->instance_cnt_phy_tx,"eNB_thread_phy_tx") < 0) break;
1958

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1959 1960 1961 1962 1963 1964 1965 1966 1967
    phy_tx_end = 1;
  }

  LOG_I(PHY, "Exiting eNB thread PHY TX\n");
  eNB_thread_phy_tx_status = 0;
  return &eNB_thread_phy_tx_status;
}


1968
static void *rf_tx( void *param ) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1969
  static int rf_tx_status;
1970
  RU_t *ru      = (RU_t *)param;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1971 1972 1973 1974 1975
  RU_proc_t *proc = &ru->proc;
  // set default return value
  rf_tx_status = 0;
  thread_top_init("rf_tx",1,500000L,1000000L,20000000L);

1976
  while (!oai_exit) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1977 1978 1979 1980 1981
    if (oai_exit) break;

    if (wait_on_condition(&proc->mutex_rf_tx,&proc->cond_rf_tx,&proc->instance_cnt_rf_tx,"rf_tx_thread") < 0) break;

    LOG_D(PHY,"Running eNB rf tx procedures\n");
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995

    if(ru->num_eNB == 1) {
      // do TX front-end processing if needed (precoding and/or IDFTs)
      if (ru->feptx_prec) ru->feptx_prec(ru);

      // do OFDM if needed
      if ((ru->fh_north_asynch_in == NULL) && (ru->feptx_ofdm)) ru->feptx_ofdm(ru);

      if(!emulate_rf) {
        // do outgoing fronthaul (south) if needed
        if ((ru->fh_north_asynch_in == NULL) && (ru->fh_south_out)) ru->fh_south_out(ru);

        if (ru->fh_north_out) ru->fh_north_out(ru);
      }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1996
    }
1997

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1998
    if (release_thread(&proc->mutex_rf_tx,&proc->instance_cnt_rf_tx,"rf_tx") < 0) break;
1999 2000

    if(proc->instance_cnt_rf_tx >= 0) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
      late_control=STATE_BURST_TERMINATE;
      LOG_E(PHY,"detect rf tx busy change mode TX failsafe\n");
    }
  }

  LOG_I(PHY, "Exiting rf TX\n");
  rf_tx_status = 0;
  return &rf_tx_status;
}
#endif

Raymond Knopp's avatar
Raymond Knopp committed
2012

2013

2014 2015 2016 2017 2018 2019 2020 2021
int start_if(struct RU_t_s *ru,struct PHY_VARS_eNB_s *eNB) {
  return(ru->ifdevice.trx_start_func(&ru->ifdevice));
}

int start_rf(RU_t *ru) {
  return(ru->rfdevice.trx_start_func(&ru->rfdevice));
}

2022
int stop_rf(RU_t *ru) {
2023 2024 2025 2026
  ru->rfdevice.trx_end_func(&ru->rfdevice);
  return 0;
}

2027
extern void fep_full(RU_t *ru);
2028
extern void ru_fep_full_2thread(RU_t *ru);
2029
extern void feptx_ofdm(RU_t *ru);
2030
extern void feptx_ofdm_2thread(RU_t *ru);
2031
extern void feptx_prec(RU_t *ru);
2032 2033
extern void init_fep_thread(RU_t *ru,pthread_attr_t *attr);
extern void init_feptx_thread(RU_t *ru,pthread_attr_t *attr);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2034 2035
extern void kill_fep_thread(RU_t *ru);
extern void kill_feptx_thread(RU_t *ru);
2036 2037 2038 2039

void init_RU_proc(RU_t *ru) {
  int i=0;
  RU_proc_t *proc;
2040
  pthread_attr_t *attr_FH=NULL,*attr_FH1=NULL,*attr_prach=NULL,*attr_asynch=NULL,*attr_synch=NULL,*attr_emulateRF=NULL;
2041
  //pthread_attr_t *attr_fep=NULL;
2042
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
2043 2044
  pthread_attr_t *attr_prach_br=NULL;
#endif
2045 2046
  char name[100];
#ifndef OCP_FRAMEWORK
2047
  LOG_I(PHY,"Initializing RU proc %d (%s,%s),\n",ru->idx,eNB_functions[ru->function],eNB_timing[ru->if_timing]);
2048 2049
#endif
  proc = &ru->proc;
2050
  memset((void *)proc,0,sizeof(RU_proc_t));
2051 2052
  proc->ru = ru;
  proc->instance_cnt_prach       = -1;
2053
  proc->instance_cnt_synch       = -1;
2054
  proc->instance_cnt_FH          = -1;
2055
  proc->instance_cnt_FH1         = -1;
2056
  proc->instance_cnt_emulateRF   = -1;
2057
  proc->instance_cnt_asynch_rxtx = -1;
2058
  proc->instance_cnt_eNBs        = -1;
2059 2060 2061 2062
  proc->first_rx                 = 1;
  proc->first_tx                 = 1;
  proc->frame_offset             = 0;
  proc->num_slaves               = 0;
2063 2064
  proc->frame_tx_unwrap          = 0;

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

2067 2068 2069 2070
  pthread_mutex_init( &proc->mutex_prach, NULL);
  pthread_mutex_init( &proc->mutex_asynch_rxtx, NULL);
  pthread_mutex_init( &proc->mutex_synch,NULL);
  pthread_mutex_init( &proc->mutex_FH,NULL);
2071
  pthread_mutex_init( &proc->mutex_FH1,NULL);
2072
  pthread_mutex_init( &proc->mutex_emulateRF,NULL);
2073
  pthread_mutex_init( &proc->mutex_eNBs, NULL);
2074 2075
  pthread_cond_init( &proc->cond_prach, NULL);
  pthread_cond_init( &proc->cond_FH, NULL);
2076
  pthread_cond_init( &proc->cond_FH1, NULL);
2077
  pthread_cond_init( &proc->cond_emulateRF, NULL);
2078 2079
  pthread_cond_init( &proc->cond_asynch_rxtx, NULL);
  pthread_cond_init( &proc->cond_synch,NULL);
2080
  pthread_cond_init( &proc->cond_eNBs, NULL);
2081
  pthread_attr_init( &proc->attr_FH);
2082
  pthread_attr_init( &proc->attr_FH1);
2083
  pthread_attr_init( &proc->attr_emulateRF);
2084 2085 2086 2087
  pthread_attr_init( &proc->attr_prach);
  pthread_attr_init( &proc->attr_synch);
  pthread_attr_init( &proc->attr_asynch_rxtx);
  pthread_attr_init( &proc->attr_fep);
2088
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
2089 2090 2091 2092
  proc->instance_cnt_prach_br       = -1;
  pthread_mutex_init( &proc->mutex_prach_br, NULL);
  pthread_cond_init( &proc->cond_prach_br, NULL);
  pthread_attr_init( &proc->attr_prach_br);
2093
#endif
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2094 2095 2096 2097 2098 2099 2100 2101
#ifdef PHY_TX_THREAD
  proc->instance_cnt_phy_tx       = -1;
  pthread_mutex_init( &proc->mutex_phy_tx, NULL);
  pthread_cond_init( &proc->cond_phy_tx, NULL);
  proc->instance_cnt_rf_tx       = -1;
  pthread_mutex_init( &proc->mutex_rf_tx, NULL);
  pthread_cond_init( &proc->cond_rf_tx, NULL);
#endif
2102
#ifndef DEADLINE_SCHEDULER
2103
  attr_FH        = &proc->attr_FH;
2104
  attr_FH1       = &proc->attr_FH1;
2105 2106 2107
  attr_prach     = &proc->attr_prach;
  attr_synch     = &proc->attr_synch;
  attr_asynch    = &proc->attr_asynch_rxtx;
2108
  attr_emulateRF = &proc->attr_emulateRF;
2109
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
2110 2111
  attr_prach_br  = &proc->attr_prach_br;
#endif
2112
#endif
2113
  pthread_create( &proc->pthread_FH, attr_FH, ru_thread, (void *)ru );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2114
#if defined(PRE_SCD_THREAD)
2115 2116 2117 2118 2119
  proc->instance_pre_scd = -1;
  pthread_mutex_init( &proc->mutex_pre_scd, NULL);
  pthread_cond_init( &proc->cond_pre_scd, NULL);
  pthread_create(&proc->pthread_pre_scd, NULL, pre_scd_thread, (void *)ru);
  pthread_setname_np(proc->pthread_pre_scd, "pre_scd_thread");
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2120 2121
#endif
#ifdef PHY_TX_THREAD
2122 2123 2124
  pthread_create( &proc->pthread_phy_tx, NULL, eNB_thread_phy_tx, (void *)ru );
  pthread_setname_np( proc->pthread_phy_tx, "phy_tx_thread" );
  pthread_create( &proc->pthread_rf_tx, NULL, rf_tx, (void *)ru );
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2125 2126
#endif

2127
  if(get_softmodem_params()->emulate_rf)
2128
    pthread_create( &proc->pthread_emulateRF, attr_emulateRF, emulatedRF_thread, (void *)proc );
2129

2130
  if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT)
2131
    pthread_create( &proc->pthread_FH1, attr_FH1, ru_thread_tx, (void *)ru );
Raymond Knopp's avatar
Raymond Knopp committed
2132

2133
  if (ru->function == NGFI_RRU_IF4p5) {
2134 2135 2136
    pthread_create( &proc->pthread_prach, attr_prach, ru_thread_prach, (void *)ru );
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
    pthread_create( &proc->pthread_prach_br, attr_prach_br, ru_thread_prach_br, (void *)ru );
2137
#endif
2138 2139 2140

    if (ru->is_slave == 1) pthread_create( &proc->pthread_synch, attr_synch, ru_thread_synch, (void *)ru);

2141
    if ((ru->if_timing == synch_to_other) ||
2142 2143 2144 2145 2146
        (ru->function == NGFI_RRU_IF5) ||
        (ru->function == NGFI_RRU_IF4p5)) {
      pthread_create( &proc->pthread_asynch_rxtx, attr_asynch, ru_thread_asynch_rxtx, (void *)ru );
    }

2147 2148
    snprintf( name, sizeof(name), "ru_thread_FH %d", ru->idx );
    pthread_setname_np( proc->pthread_FH, name );
2149
  } else if (ru->function == eNodeB_3GPP && ru->if_south == LOCAL_RF) { // DJP - need something else to distinguish between monolithic and PNF
Cedric Roux's avatar
Cedric Roux committed
2150
    LOG_I(PHY,"%s() DJP - added creation of pthread_prach\n", __FUNCTION__);
2151
    pthread_create( &proc->pthread_prach, attr_prach, ru_thread_prach, (void *)ru );
2152
  }
2153

2154 2155
  if (get_thread_worker_conf() == WORKER_ENABLE) {
    init_fep_thread(ru,NULL);
2156
    init_feptx_thread(ru,NULL);
2157 2158 2159
  }

  if (opp_enabled == 1) pthread_create(&ru->ru_stats_thread,NULL,ru_stats_thread,(void *)ru);
2160 2161
}

2162
void kill_RU_proc(RU_t *ru) {
Robert Schmidt's avatar
Robert Schmidt committed
2163
  RU_proc_t *proc = &ru->proc;
Robert Schmidt's avatar
Robert Schmidt committed
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
#if defined(PRE_SCD_THREAD)
  pthread_mutex_lock(&proc->mutex_pre_scd);
  ru->proc.instance_pre_scd = 0;
  pthread_cond_signal(&proc->cond_pre_scd);
  pthread_mutex_unlock(&proc->mutex_pre_scd);
  pthread_join(proc->pthread_pre_scd, NULL);
  pthread_mutex_destroy(&proc->mutex_pre_scd);
  pthread_cond_destroy(&proc->cond_pre_scd);
#endif
#ifdef PHY_TX_THREAD
  pthread_mutex_lock(&proc->mutex_phy_tx);
  proc->instance_cnt_phy_tx = 0;
  pthread_cond_signal(&proc->cond_phy_tx);
  pthread_mutex_unlock(&proc->mutex_phy_tx);
  pthread_join(ru->proc.pthread_phy_tx, NULL);
  pthread_mutex_destroy( &proc->mutex_phy_tx);
  pthread_cond_destroy( &proc->cond_phy_tx);
  pthread_mutex_lock(&proc->mutex_rf_tx);
  proc->instance_cnt_rf_tx = 0;
  pthread_cond_signal(&proc->cond_rf_tx);
  pthread_mutex_unlock(&proc->mutex_rf_tx);
  pthread_join(proc->pthread_rf_tx, NULL);
  pthread_mutex_destroy( &proc->mutex_rf_tx);
  pthread_cond_destroy( &proc->cond_rf_tx);
#endif

2190
  if (get_thread_worker_conf() == WORKER_ENABLE) {
2191 2192 2193 2194
    LOG_D(PHY, "killing FEP thread\n");
    kill_fep_thread(ru);
    LOG_D(PHY, "killing FEP TX thread\n");
    kill_feptx_thread(ru);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2195 2196
  }

Robert Schmidt's avatar
Robert Schmidt committed
2197 2198 2199
  pthread_mutex_lock(&proc->mutex_FH);
  proc->instance_cnt_FH = 0;
  pthread_cond_signal(&proc->cond_FH);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2200 2201 2202 2203 2204
  pthread_mutex_unlock(&proc->mutex_FH);
  pthread_mutex_lock(&proc->mutex_FH1);
  proc->instance_cnt_FH1 = 0;
  pthread_cond_signal(&proc->cond_FH1);
  pthread_mutex_unlock(&proc->mutex_FH1);
Robert Schmidt's avatar
Robert Schmidt committed
2205 2206 2207
  pthread_mutex_lock(&proc->mutex_prach);
  proc->instance_cnt_prach = 0;
  pthread_cond_signal(&proc->cond_prach);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2208
  pthread_mutex_unlock(&proc->mutex_prach);
2209
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
Robert Schmidt's avatar
Robert Schmidt committed
2210 2211 2212
  pthread_mutex_lock(&proc->mutex_prach_br);
  proc->instance_cnt_prach_br = 0;
  pthread_cond_signal(&proc->cond_prach_br);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2213
  pthread_mutex_unlock(&proc->mutex_prach_br);
Robert Schmidt's avatar
Robert Schmidt committed
2214 2215 2216 2217
#endif
  pthread_mutex_lock(&proc->mutex_synch);
  proc->instance_cnt_synch = 0;
  pthread_cond_signal(&proc->cond_synch);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2218
  pthread_mutex_unlock(&proc->mutex_synch);
Robert Schmidt's avatar
Robert Schmidt committed
2219
  pthread_mutex_lock(&proc->mutex_eNBs);
Robert Schmidt's avatar
Robert Schmidt committed
2220 2221 2222 2223
  proc->instance_cnt_eNBs = 1;
  // cond_eNBs is used by both ru_thread and ru_thread_tx, so we need to send
  // a broadcast to wake up both threads
  pthread_cond_broadcast(&proc->cond_eNBs);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2224
  pthread_mutex_unlock(&proc->mutex_eNBs);
Robert Schmidt's avatar
Robert Schmidt committed
2225 2226 2227
  pthread_mutex_lock(&proc->mutex_asynch_rxtx);
  proc->instance_cnt_asynch_rxtx = 0;
  pthread_cond_signal(&proc->cond_asynch_rxtx);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2228
  pthread_mutex_unlock(&proc->mutex_asynch_rxtx);
Robert Schmidt's avatar
Robert Schmidt committed
2229
  LOG_D(PHY, "Joining pthread_FH\n");
Robert Schmidt's avatar
Robert Schmidt committed
2230
  pthread_join(proc->pthread_FH, NULL);
2231

2232
  if (get_thread_parallel_conf() == PARALLEL_RU_L1_SPLIT || get_thread_parallel_conf() == PARALLEL_RU_L1_TRX_SPLIT) {
Robert Schmidt's avatar
Robert Schmidt committed
2233 2234 2235
    LOG_D(PHY, "Joining pthread_FHTX\n");
    pthread_join(proc->pthread_FH1, NULL);
  }
2236

Robert Schmidt's avatar
Robert Schmidt committed
2237 2238 2239
  if (ru->function == NGFI_RRU_IF4p5) {
    LOG_D(PHY, "Joining pthread_prach\n");
    pthread_join(proc->pthread_prach, NULL);
2240
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
Robert Schmidt's avatar
Robert Schmidt committed
2241 2242 2243
    LOG_D(PHY, "Joining pthread_prach_br\n");
    pthread_join(proc->pthread_prach_br, NULL);
#endif
2244

Robert Schmidt's avatar
Robert Schmidt committed
2245 2246 2247 2248
    if (ru->is_slave) {
      LOG_D(PHY, "Joining pthread_\n");
      pthread_join(proc->pthread_synch, NULL);
    }
2249

Robert Schmidt's avatar
Robert Schmidt committed
2250 2251 2252 2253 2254 2255 2256
    if ((ru->if_timing == synch_to_other) ||
        (ru->function == NGFI_RRU_IF5) ||
        (ru->function == NGFI_RRU_IF4p5)) {
      LOG_D(PHY, "Joining pthread_asynch_rxtx\n");
      pthread_join(proc->pthread_asynch_rxtx, NULL);
    }
  }
2257

Robert Schmidt's avatar
Robert Schmidt committed
2258 2259 2260 2261 2262 2263 2264 2265 2266
  if (opp_enabled) {
    LOG_D(PHY, "Joining ru_stats_thread\n");
    pthread_join(ru->ru_stats_thread, NULL);
  }

  pthread_mutex_destroy(&proc->mutex_prach);
  pthread_mutex_destroy(&proc->mutex_asynch_rxtx);
  pthread_mutex_destroy(&proc->mutex_synch);
  pthread_mutex_destroy(&proc->mutex_FH);
2267
  pthread_mutex_destroy(&proc->mutex_FH1);
Robert Schmidt's avatar
Robert Schmidt committed
2268 2269 2270
  pthread_mutex_destroy(&proc->mutex_eNBs);
  pthread_cond_destroy(&proc->cond_prach);
  pthread_cond_destroy(&proc->cond_FH);
2271
  pthread_cond_destroy(&proc->cond_FH1);
Robert Schmidt's avatar
Robert Schmidt committed
2272 2273 2274 2275
  pthread_cond_destroy(&proc->cond_asynch_rxtx);
  pthread_cond_destroy(&proc->cond_synch);
  pthread_cond_destroy(&proc->cond_eNBs);
  pthread_attr_destroy(&proc->attr_FH);
2276
  pthread_attr_destroy(&proc->attr_FH1);
Robert Schmidt's avatar
Robert Schmidt committed
2277 2278 2279 2280
  pthread_attr_destroy(&proc->attr_prach);
  pthread_attr_destroy(&proc->attr_synch);
  pthread_attr_destroy(&proc->attr_asynch_rxtx);
  pthread_attr_destroy(&proc->attr_fep);
2281
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
Robert Schmidt's avatar
Robert Schmidt committed
2282 2283 2284 2285 2286
  pthread_mutex_destroy(&proc->mutex_prach_br);
  pthread_cond_destroy(&proc->cond_prach_br);
  pthread_attr_destroy(&proc->attr_prach_br);
#endif
}
Raymond Knopp's avatar
Raymond Knopp committed
2287

2288 2289 2290 2291 2292
int check_capabilities(RU_t *ru,RRU_capabilities_t *cap) {
  FH_fmt_options_t fmt = cap->FH_fmt;
  int i;
  int found_band=0;
  LOG_I(PHY,"RRU %d, num_bands %d, looking for band %d\n",ru->idx,cap->num_bands,ru->frame_parms.eutra_band);
2293 2294

  for (i=0; i<cap->num_bands; i++) {
2295
    LOG_I(PHY,"band %d on RRU %d\n",cap->band_list[i],ru->idx);
2296

2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
    if (ru->frame_parms.eutra_band == cap->band_list[i]) {
      found_band=1;
      break;
    }
  }

  if (found_band == 0) {
    LOG_I(PHY,"Couldn't find target EUTRA band %d on RRU %d\n",ru->frame_parms.eutra_band,ru->idx);
    return(-1);
  }

  switch (ru->if_south) {
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
    case LOCAL_RF:
      AssertFatal(1==0, "This RU should not have a local RF, exiting\n");
      return(0);
      break;

    case REMOTE_IF5:
      if (fmt == OAI_IF5_only || fmt == OAI_IF5_and_IF4p5) return(0);

      break;

    case REMOTE_IF4p5:
      if (fmt == OAI_IF4p5_only || fmt == OAI_IF5_and_IF4p5) return(0);

      break;

    case REMOTE_MBP_IF5:
      if (fmt == MBP_IF5) return(0);

      break;

    default:
      LOG_I(PHY,"No compatible Fronthaul interface found for RRU %d\n", ru->idx);
      return(-1);
2332 2333
  }

Raymond Knopp's avatar
Raymond Knopp committed
2334
  return(-1);
Raymond Knopp's avatar
Raymond Knopp committed
2335 2336 2337
}


2338
char rru_format_options[4][20] = {"OAI_IF5_only","OAI_IF4p5_only","OAI_IF5_and_IF4p5","MBP_IF5"};
2339

2340
char rru_formats[3][20] = {"OAI_IF5","MBP_IF5","OAI_IF4p5"};
2341
char ru_if_formats[4][20] = {"LOCAL_RF","REMOTE_OAI_IF5","REMOTE_MBP_IF5","REMOTE_OAI_IF4p5"};
2342 2343

void configure_ru(int idx,
2344
                  void *arg) {
2345 2346
  RU_t               *ru           = RC.ru[idx];
  RRU_config_t       *config       = (RRU_config_t *)arg;
2347
  RRU_capabilities_t *capabilities = (RRU_capabilities_t *)arg;
2348 2349 2350 2351
  int ret;
  LOG_I(PHY, "Received capabilities from RRU %d\n",idx);

  if (capabilities->FH_fmt < MAX_FH_FMTs) LOG_I(PHY, "RU FH options %s\n",rru_format_options[capabilities->FH_fmt]);
2352

2353 2354
  ret=check_capabilities(ru,capabilities);
  AssertFatal((ret == 0),
2355
              "Cannot configure RRU %d, check_capabilities returned %d\n", idx,ret);
2356 2357 2358
  // take antenna capabilities of RRU
  ru->nb_tx                      = capabilities->nb_tx[0];
  ru->nb_rx                      = capabilities->nb_rx[0];
Raymond Knopp's avatar
Raymond Knopp committed
2359 2360
  // Pass configuration to RRU
  LOG_I(PHY, "Using %s fronthaul (%d), band %d \n",ru_if_formats[ru->if_south],ru->if_south,ru->frame_parms.eutra_band);
2361
  // wait for configuration
Raymond Knopp's avatar
Raymond Knopp committed
2362 2363 2364
  config->FH_fmt                 = ru->if_south;
  config->num_bands              = 1;
  config->band_list[0]           = ru->frame_parms.eutra_band;
2365 2366
  config->tx_freq[0]             = ru->frame_parms.dl_CarrierFreq;
  config->rx_freq[0]             = ru->frame_parms.ul_CarrierFreq;
2367 2368
  config->tdd_config[0]          = ru->frame_parms.tdd_config;
  config->tdd_config_S[0]        = ru->frame_parms.tdd_config_S;
Raymond Knopp's avatar
Raymond Knopp committed
2369 2370 2371 2372 2373
  config->att_tx[0]              = ru->att_tx;
  config->att_rx[0]              = ru->att_rx;
  config->N_RB_DL[0]             = ru->frame_parms.N_RB_DL;
  config->N_RB_UL[0]             = ru->frame_parms.N_RB_UL;
  config->threequarter_fs[0]     = ru->frame_parms.threequarter_fs;
2374

Raymond Knopp's avatar
Raymond Knopp committed
2375 2376 2377 2378
  if (ru->if_south==REMOTE_IF4p5) {
    config->prach_FreqOffset[0]  = ru->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset;
    config->prach_ConfigIndex[0] = ru->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
    LOG_I(PHY,"REMOTE_IF4p5: prach_FrequOffset %d, prach_ConfigIndex %d\n",
2379
          config->prach_FreqOffset[0],config->prach_ConfigIndex[0]);
2380
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
Cedric Roux's avatar
Cedric Roux committed
2381
    int i;
2382 2383

    for (i=0; i<4; i++) {
Raymond Knopp's avatar
Raymond Knopp committed
2384 2385 2386
      config->emtc_prach_CElevel_enable[0][i]  = ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[i];
      config->emtc_prach_FreqOffset[0][i]      = ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[i];
      config->emtc_prach_ConfigIndex[0][i]     = ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_ConfigIndex[i];
2387
    }
2388

Raymond Knopp's avatar
Raymond Knopp committed
2389
#endif
2390 2391 2392 2393 2394 2395
  }

  init_frame_parms(&ru->frame_parms,1);
  phy_init_RU(ru);
}

2396
void configure_rru(int idx,
2397
                   void *arg) {
2398 2399 2400 2401 2402
  RRU_config_t *config = (RRU_config_t *)arg;
  RU_t         *ru         = RC.ru[idx];
  ru->frame_parms.eutra_band                                               = config->band_list[0];
  ru->frame_parms.dl_CarrierFreq                                           = config->tx_freq[0];
  ru->frame_parms.ul_CarrierFreq                                           = config->rx_freq[0];
2403

2404
  if (ru->frame_parms.dl_CarrierFreq == ru->frame_parms.ul_CarrierFreq) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2405 2406 2407
    LOG_I(PHY,"Setting RRU to TDD frame type\n");
    ru->frame_parms.frame_type                                            = TDD;
    ru->frame_parms.tdd_config                                            = config->tdd_config[0];
2408 2409 2410
    ru->frame_parms.tdd_config_S                                          = config->tdd_config_S[0];
  } else ru->frame_parms.frame_type                                            = FDD;

2411 2412 2413 2414 2415 2416
  ru->att_tx                                                               = config->att_tx[0];
  ru->att_rx                                                               = config->att_rx[0];
  ru->frame_parms.N_RB_DL                                                  = config->N_RB_DL[0];
  ru->frame_parms.N_RB_UL                                                  = config->N_RB_UL[0];
  ru->frame_parms.threequarter_fs                                          = config->threequarter_fs[0];
  ru->frame_parms.pdsch_config_common.referenceSignalPower                 = ru->max_pdschReferenceSignalPower-config->att_tx[0];
2417

2418
  if (ru->function==NGFI_RRU_IF4p5) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2419 2420
    ru->frame_parms.att_rx = ru->att_rx;
    ru->frame_parms.att_tx = ru->att_tx;
2421
    LOG_I(PHY,"Setting ru->function to NGFI_RRU_IF4p5, prach_FrequOffset %d, prach_ConfigIndex %d, att (%d,%d)\n",
2422 2423 2424
          config->prach_FreqOffset[0],config->prach_ConfigIndex[0],ru->att_tx,ru->att_rx);
    ru->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset  = config->prach_FreqOffset[0];
    ru->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex = config->prach_ConfigIndex[0];
2425
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
2426 2427

    for (int i=0; i<4; i++) {
2428 2429 2430 2431
      ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[i] = config->emtc_prach_CElevel_enable[0][i];
      ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[i]     = config->emtc_prach_FreqOffset[0][i];
      ru->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_ConfigIndex[i]    = config->emtc_prach_ConfigIndex[0][i];
    }
2432

2433
#endif
2434 2435
  }

2436
  init_frame_parms(&ru->frame_parms,1);
2437
  fill_rf_config(ru,ru->rf_config_file);
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
  phy_init_RU(ru);
}

void init_precoding_weights(PHY_VARS_eNB *eNB) {
  int layer,ru_id,aa,re,ue,tb;
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  RU_t *ru;
  LTE_eNB_DLSCH_t *dlsch;

  // init precoding weigths
2448 2449
  for (ue=0; ue<NUMBER_OF_UE_MAX; ue++) {
    for (tb=0; tb<2; tb++) {
2450
      dlsch = eNB->dlsch[ue][tb];
2451

2452
      for (layer=0; layer<4; layer++) {
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
        int nb_tx=0;

        for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
          ru = RC.ru[ru_id];
          nb_tx+=ru->nb_tx;
        }

        dlsch->ue_spec_bf_weights[layer] = (int32_t **)malloc16(nb_tx*sizeof(int32_t *));

        for (aa=0; aa<nb_tx; aa++) {
          dlsch->ue_spec_bf_weights[layer][aa] = (int32_t *)malloc16(fp->ofdm_symbol_size*sizeof(int32_t));

          for (re=0; re<fp->ofdm_symbol_size; re++) {
            dlsch->ue_spec_bf_weights[layer][aa][re] = 0x00007fff;
          }
        }
2469 2470 2471 2472 2473
      }
    }
  }
}

2474
void set_function_spec_param(RU_t *ru) {
2475 2476 2477
  int ret;

  switch (ru->if_south) {
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
    case LOCAL_RF:   // this is an RU with integrated RF (RRU, eNB)
      if (ru->function ==  NGFI_RRU_IF5) {                 // IF5 RRU
        ru->do_prach              = 0;                      // no prach processing in RU
        ru->fh_north_in           = NULL;                   // no shynchronous incoming fronthaul from north
        ru->fh_north_out          = fh_if5_north_out;       // need only to do send_IF5  reception
        ru->fh_south_out          = tx_rf;                  // send output to RF
        ru->fh_north_asynch_in    = fh_if5_north_asynch_in; // TX packets come asynchronously
        ru->feprx                 = NULL;                   // nothing (this is a time-domain signal)
        ru->feptx_ofdm            = NULL;                   // nothing (this is a time-domain signal)
        ru->feptx_prec            = NULL;                   // nothing (this is a time-domain signal)
        ru->start_if              = start_if;               // need to start the if interface for if5
        ru->ifdevice.host_type    = RRU_HOST;
        ru->rfdevice.host_type    = RRU_HOST;
        ru->ifdevice.eth_params   = &ru->eth_params;
        reset_meas(&ru->rx_fhaul);
        reset_meas(&ru->tx_fhaul);
        reset_meas(&ru->compression);
        reset_meas(&ru->transport);
        ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
        printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);

        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }
      } else if (ru->function == NGFI_RRU_IF4p5) {
        ru->do_prach              = 1;                        // do part of prach processing in RU
        ru->fh_north_in           = NULL;                     // no synchronous incoming fronthaul from north
        ru->fh_north_out          = fh_if4p5_north_out;       // send_IF4p5 on reception
        ru->fh_south_out          = tx_rf;                    // send output to RF
        ru->fh_north_asynch_in    = fh_if4p5_north_asynch_in; // TX packets come asynchronously
        ru->feprx                 = (get_thread_worker_conf() == WORKER_DISABLE) ? fep_full :ru_fep_full_2thread;                 // RX DFTs
        ru->feptx_ofdm            = (get_thread_worker_conf() == WORKER_DISABLE) ? feptx_ofdm : feptx_ofdm_2thread;               // this is fep with idft only (no precoding in RRU)
        ru->feptx_prec            = NULL;
        ru->start_if              = start_if;                 // need to start the if interface for if4p5
        ru->ifdevice.host_type    = RRU_HOST;
        ru->rfdevice.host_type    = RRU_HOST;
        ru->ifdevice.eth_params   = &ru->eth_params;
        reset_meas(&ru->rx_fhaul);
        reset_meas(&ru->tx_fhaul);
        reset_meas(&ru->compression);
        reset_meas(&ru->transport);
        ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
        printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);

        if (ret<0) {
          printf("Exiting, cannot initialize transport protocol\n");
          exit(-1);
        }

        malloc_IF4p5_buffer(ru);
      } else if (ru->function == eNodeB_3GPP) {
        ru->do_prach             = 0;                       // no prach processing in RU
        ru->feprx                = (get_thread_worker_conf() == WORKER_DISABLE) ? fep_full : ru_fep_full_2thread;                // RX DFTs
        ru->feptx_ofdm           = (get_thread_worker_conf() == WORKER_DISABLE) ? feptx_ofdm : feptx_ofdm_2thread;              // this is fep with idft and precoding
        ru->feptx_prec           = feptx_prec;              // this is fep with idft and precoding
        ru->fh_north_in          = NULL;                    // no incoming fronthaul from north
        ru->fh_north_out         = NULL;                    // no outgoing fronthaul to north
        ru->start_if             = NULL;                    // no if interface
        ru->rfdevice.host_type   = RAU_HOST;
      }
2539

2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
      ru->fh_south_in            = rx_rf;                               // local synchronous RF RX
      ru->fh_south_out           = tx_rf;                               // local synchronous RF TX
      ru->start_rf               = start_rf;                            // need to start the local RF interface
      ru->stop_rf                = stop_rf;
      printf("configuring ru_id %d (start_rf %p)\n", ru->idx, start_rf);
      /*
          if (ru->function == eNodeB_3GPP) { // configure RF parameters only for 3GPP eNodeB, we need to get them from RAU otherwise
            fill_rf_config(ru,rf_config_file);
            init_frame_parms(&ru->frame_parms,1);
            phy_init_RU(ru);
          }

          ret = openair0_device_load(&ru->rfdevice,&ru->openair0_cfg);
          if (setup_RU_buffers(ru)!=0) {
            printf("Exiting, cannot initialize RU Buffers\n");
            exit(-1);
          }*/
      break;

    case REMOTE_IF5: // the remote unit is IF5 RRU
      ru->do_prach               = 0;
      ru->feprx                  = (get_thread_worker_conf() == WORKER_DISABLE) ? fep_full : fep_full;                   // this is frequency-shift + DFTs
      ru->feptx_prec             = feptx_prec;                 // need to do transmit Precoding + IDFTs
      ru->feptx_ofdm             = (get_thread_worker_conf() == WORKER_DISABLE) ? feptx_ofdm : feptx_ofdm_2thread;                 // need to do transmit Precoding + IDFTs

      if (ru->if_timing == synch_to_other) {
        ru->fh_south_in          = fh_slave_south_in;                  // synchronize to master
        ru->fh_south_out         = fh_if5_mobipass_south_out;          // use send_IF5 for mobipass
        ru->fh_south_asynch_in   = fh_if5_south_asynch_in_mobipass;    // UL is asynchronous
      } else {
        ru->fh_south_in          = fh_if5_south_in;     // synchronous IF5 reception
        ru->fh_south_out         = fh_if5_south_out;    // synchronous IF5 transmission
        ru->fh_south_asynch_in   = NULL;                // no asynchronous UL
      }

      ru->start_rf               = NULL;                 // no local RF
      ru->stop_rf                = NULL;
      ru->start_if               = start_if;             // need to start if interface for IF5
      ru->ifdevice.host_type     = RAU_HOST;
      ru->ifdevice.eth_params    = &ru->eth_params;
      ru->ifdevice.configure_rru = configure_ru;
2581 2582
      ret = openair0_transport_load(&ru->ifdevice,&ru->openair0_cfg,&ru->eth_params);
      printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
2583

2584 2585 2586 2587 2588
      if (ret<0) {
        printf("Exiting, cannot initialize transport protocol\n");
        exit(-1);
      }

2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
      break;

    case REMOTE_IF4p5:
      ru->do_prach               = 0;
      ru->feprx                  = NULL;                // DFTs
      ru->feptx_prec             = feptx_prec;          // Precoding operation
      ru->feptx_ofdm             = NULL;                // no OFDM mod
      ru->fh_south_in            = fh_if4p5_south_in;   // synchronous IF4p5 reception
      ru->fh_south_out           = fh_if4p5_south_out;  // synchronous IF4p5 transmission
      ru->fh_south_asynch_in     = (ru->if_timing == synch_to_other) ? fh_if4p5_south_in : NULL;                // asynchronous UL if synch_to_other
      ru->fh_north_out           = NULL;
      ru->fh_north_asynch_in     = NULL;
      ru->start_rf               = NULL;                // no local RF
      ru->stop_rf                = NULL;
      ru->start_if               = start_if;            // need to start if interface for IF4p5
      ru->ifdevice.host_type     = RAU_HOST;
      ru->ifdevice.eth_params    = &ru->eth_params;
      ru->ifdevice.configure_rru = configure_ru;
      ret = openair0_transport_load(&ru->ifdevice, &ru->openair0_cfg, &ru->eth_params);
2608
      printf("openair0_transport_init returns %d for ru_id %d\n", ret, ru->idx);
2609

2610 2611 2612 2613 2614
      if (ret<0) {
        printf("Exiting, cannot initialize transport protocol\n");
        exit(-1);
      }

2615 2616
      malloc_IF4p5_buffer(ru);
      break;
2617

2618 2619 2620
    default:
      LOG_E(PHY,"RU with invalid or unknown southbound interface type %d\n",ru->if_south);
      break;
2621 2622 2623
  } // switch on interface type
}

Wang Tsu-Han's avatar
Wang Tsu-Han committed
2624
//extern void RCconfig_RU(void);
Raymond Knopp's avatar
Raymond Knopp committed
2625 2626

void init_RU(char *rf_config_file) {
Raymond Knopp's avatar
Raymond Knopp committed
2627
  int ru_id;
2628
  RU_t *ru;
2629
  PHY_VARS_eNB *eNB0= (PHY_VARS_eNB *)NULL;
2630 2631 2632 2633 2634 2635
  int i;
  int CC_id;
  // create status mask
  RC.ru_mask = 0;
  pthread_mutex_init(&RC.ru_mutex,NULL);
  pthread_cond_init(&RC.ru_cond,NULL);
2636 2637
  // read in configuration file)
  printf("configuring RU from file\n");
2638
  RCconfig_RU();
2639
  LOG_I(PHY,"number of L1 instances %d, number of RU %d, number of CPU cores %d\n",RC.nb_L1_inst,RC.nb_RU,get_nprocs());
2640

2641
  if (RC.nb_CC != 0)
2642 2643
    for (i=0; i<RC.nb_L1_inst; i++)
      for (CC_id=0; CC_id<RC.nb_CC[i]; CC_id++) RC.eNB[i][CC_id]->num_RU=0;
2644

Cedric Roux's avatar
Cedric Roux committed
2645
  LOG_D(PHY,"Process RUs RC.nb_RU:%d\n",RC.nb_RU);
2646 2647

  for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
Cedric Roux's avatar
Cedric Roux committed
2648
    LOG_D(PHY,"Process RC.ru[%d]\n",ru_id);
2649
    ru               = RC.ru[ru_id];
2650
    ru->rf_config_file = rf_config_file;
2651
    ru->idx          = ru_id;
2652 2653 2654
    ru->ts_offset    = 0;
    // use eNB_list[0] as a reference for RU frame parameters
    // NOTE: multiple CC_id are not handled here yet!
Raymond Knopp's avatar
Raymond Knopp committed
2655

2656 2657
    if (ru->num_eNB > 0) {
      LOG_D(PHY, "%s() RC.ru[%d].num_eNB:%d ru->eNB_list[0]:%p RC.eNB[0][0]:%p rf_config_file:%s\n", __FUNCTION__, ru_id, ru->num_eNB, ru->eNB_list[0], RC.eNB[0][0], ru->rf_config_file);
2658

2659
      if (ru->eNB_list[0] == 0) {
2660 2661 2662
        LOG_E(PHY,"%s() DJP - ru->eNB_list ru->num_eNB are not initialized - so do it manually\n", __FUNCTION__);
        ru->eNB_list[0] = RC.eNB[0][0];
        ru->num_eNB=1;
2663 2664 2665 2666
        //
        // DJP - feptx_prec() / feptx_ofdm() parses the eNB_list (based on num_eNB) and copies the txdata_F to txdata in RU
        //
      } else {
2667
        LOG_E(PHY,"DJP - delete code above this %s:%d\n", __FILE__, __LINE__);
Raymond Knopp's avatar
Raymond Knopp committed
2668
      }
2669
    }
2670

2671
    eNB0             = ru->eNB_list[0];
Cedric Roux's avatar
Cedric Roux committed
2672 2673
    LOG_D(PHY, "RU FUnction:%d ru->if_south:%d\n", ru->function, ru->if_south);
    LOG_D(PHY, "eNB0:%p\n", eNB0);
2674 2675

    if (eNB0) {
2676 2677 2678 2679 2680
      if ((ru->function != NGFI_RRU_IF5) && (ru->function != NGFI_RRU_IF4p5))
        AssertFatal(eNB0!=NULL,"eNB0 is null!\n");

      if (eNB0) {
        LOG_I(PHY,"Copying frame parms from eNB %d to ru %d\n",eNB0->Mod_id,ru->idx);
2681
        memcpy((void *)&ru->frame_parms,(void *)&eNB0->frame_parms,sizeof(LTE_DL_FRAME_PARMS));
2682
        // attach all RU to all eNBs in its list/
Cedric Roux's avatar
Cedric Roux committed
2683
        LOG_D(PHY,"ru->num_eNB:%d eNB0->num_RU:%d\n", ru->num_eNB, eNB0->num_RU);
2684 2685

        for (i=0; i<ru->num_eNB; i++) {
2686 2687 2688
          eNB0 = ru->eNB_list[i];
          eNB0->RU_list[eNB0->num_RU++] = ru;
        }
Raymond Knopp's avatar
Raymond Knopp committed
2689
      }
2690
    }
Raymond Knopp's avatar
Raymond Knopp committed
2691

2692
    LOG_I(PHY,"Initializing RRU descriptor %d : (%s,%s,%d)\n",ru_id,ru_if_types[ru->if_south],eNB_timing[ru->if_timing],ru->function);
2693
    set_function_spec_param(ru);
2694 2695
    LOG_I(PHY,"Starting ru_thread %d\n",ru_id);
    init_RU_proc(ru);
Raymond Knopp's avatar
Raymond Knopp committed
2696 2697
  } // for ru_id

2698
  //  sleep(1);
2699
  LOG_D(HW,"[lte-softmodem.c] RU threads created\n");
Raymond Knopp's avatar
Raymond Knopp committed
2700 2701
}

2702
void stop_RU(int nb_ru) {
Robert Schmidt's avatar
Robert Schmidt committed
2703 2704
  for (int inst = 0; inst < nb_ru; inst++) {
    LOG_I(PHY, "Stopping RU %d processing threads\n", inst);
Robert Schmidt's avatar
Robert Schmidt committed
2705
    kill_RU_proc(RC.ru[inst]);
Robert Schmidt's avatar
Robert Schmidt committed
2706
  }
Raymond Knopp's avatar
Raymond Knopp committed
2707 2708
}

2709 2710 2711 2712 2713 2714 2715
//Some of the member of ru pointer is used in pre_scd.
//This funtion is for initializing ru pointer for L2 FAPI simulator.
#if defined(PRE_SCD_THREAD)
void init_ru_vnf(void) {
  int ru_id;
  RU_t *ru;
  RU_proc_t *proc;
2716
  //  PHY_VARS_eNB *eNB0= (PHY_VARS_eNB *)NULL;
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
  int i;
  int CC_id;
  dlsch_ue_select_tbl_in_use = 1;
  // create status mask
  RC.ru_mask = 0;
  pthread_mutex_init(&RC.ru_mutex,NULL);
  pthread_cond_init(&RC.ru_cond,NULL);
  // read in configuration file)
  printf("configuring RU from file\n");
  RCconfig_RU();
  LOG_I(PHY,"number of L1 instances %d, number of RU %d, number of CPU cores %d\n",RC.nb_L1_inst,RC.nb_RU,get_nprocs());

  if (RC.nb_CC != 0)
2730 2731
    for (i=0; i<RC.nb_L1_inst; i++)
      for (CC_id=0; CC_id<RC.nb_CC[i]; CC_id++) RC.eNB[i][CC_id]->num_RU=0;
2732 2733

  LOG_D(PHY,"Process RUs RC.nb_RU:%d\n",RC.nb_RU);
2734 2735

  for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
2736 2737
    LOG_D(PHY,"Process RC.ru[%d]\n",ru_id);
    ru               = RC.ru[ru_id];
2738 2739
    //    ru->rf_config_file = rf_config_file;
    ru->idx          = ru_id;
2740 2741 2742 2743 2744
    ru->ts_offset    = 0;
    // use eNB_list[0] as a reference for RU frame parameters
    // NOTE: multiple CC_id are not handled here yet!

    if (ru->num_eNB > 0) {
2745 2746
      //      LOG_D(PHY, "%s() RC.ru[%d].num_eNB:%d ru->eNB_list[0]:%p RC.eNB[0][0]:%p rf_config_file:%s\n", __FUNCTION__, ru_id, ru->num_eNB, ru->eNB_list[0], RC.eNB[0][0], ru->rf_config_file);
      if (ru->eNB_list[0] == 0) {
2747 2748 2749
        LOG_E(PHY,"%s() DJP - ru->eNB_list ru->num_eNB are not initialized - so do it manually\n", __FUNCTION__);
        ru->eNB_list[0] = RC.eNB[0][0];
        ru->num_eNB=1;
2750 2751 2752 2753
        //
        // DJP - feptx_prec() / feptx_ofdm() parses the eNB_list (based on num_eNB) and copies the txdata_F to txdata in RU
        //
      } else {
2754 2755 2756 2757
        LOG_E(PHY,"DJP - delete code above this %s:%d\n", __FILE__, __LINE__);
      }
    }

2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
    // frame_parms is not used in L2 FAPI simulator
    /*
        eNB0             = ru->eNB_list[0];
        LOG_D(PHY, "RU FUnction:%d ru->if_south:%d\n", ru->function, ru->if_south);
        LOG_D(PHY, "eNB0:%p\n", eNB0);
        if (eNB0)
        {
          if ((ru->function != NGFI_RRU_IF5) && (ru->function != NGFI_RRU_IF4p5))
            AssertFatal(eNB0!=NULL,"eNB0 is null!\n");

          if (eNB0) {
            LOG_I(PHY,"Copying frame parms from eNB %d to ru %d\n",eNB0->Mod_id,ru->idx);
            memcpy((void*)&ru->frame_parms,(void*)&eNB0->frame_parms,sizeof(LTE_DL_FRAME_PARMS));

            // attach all RU to all eNBs in its list/
            LOG_D(PHY,"ru->num_eNB:%d eNB0->num_RU:%d\n", ru->num_eNB, eNB0->num_RU);
            for (i=0;i<ru->num_eNB;i++) {
              eNB0 = ru->eNB_list[i];
              eNB0->RU_list[eNB0->num_RU++] = ru;
            }
          }
2779
        }
2780
    */
2781
    LOG_I(PHY,"Initializing RRU descriptor %d : (%s,%s,%d)\n",ru_id,ru_if_types[ru->if_south],eNB_timing[ru->if_timing],ru->function);
2782
    //    set_function_spec_param(ru);
2783
    LOG_I(PHY,"Starting ru_thread %d\n",ru_id);
2784
    //    init_RU_proc(ru);
2785
    proc = &ru->proc;
2786
    memset((void *)proc,0,sizeof(RU_proc_t));
2787 2788 2789
    proc->instance_pre_scd = -1;
    pthread_mutex_init( &proc->mutex_pre_scd, NULL);
    pthread_cond_init( &proc->cond_pre_scd, NULL);
2790
    pthread_create(&proc->pthread_pre_scd, NULL, pre_scd_thread, (void *)ru);
2791 2792 2793 2794 2795 2796 2797 2798
    pthread_setname_np(proc->pthread_pre_scd, "pre_scd_thread");
  } // for ru_id

  //  sleep(1);
  LOG_D(HW,"[lte-softmodem.c] RU threads created\n");
}
#endif

Raymond Knopp's avatar
Raymond Knopp committed
2799

2800 2801 2802 2803 2804 2805 2806
/* --------------------------------------------------------*/
/* from here function to use configuration module          */
void RCconfig_RU(void) {
  int               j                             = 0;
  int               i                             = 0;
  paramdef_t RUParams[] = RUPARAMS_DESC;
  paramlist_def_t RUParamList = {CONFIG_STRING_RU_LIST,NULL,0};
2807
  config_getlist( &RUParamList,RUParams,sizeof(RUParams)/sizeof(paramdef_t), NULL);
Raymond Knopp's avatar
Raymond Knopp committed
2808

2809
  if ( RUParamList.numelt > 0) {
2810
    RC.ru = (RU_t **)malloc(RC.nb_RU*sizeof(RU_t *));
Wang Tsu-Han's avatar
Wang Tsu-Han committed
2811
    RC.ru_mask=(1<<RC.nb_RU) - 1;
2812 2813 2814
    printf("Set RU mask to %lx\n",RC.ru_mask);

    for (j = 0; j < RC.nb_RU; j++) {
2815 2816
      RC.ru[j]                                    = (RU_t *)malloc(sizeof(RU_t));
      memset((void *)RC.ru[j],0,sizeof(RU_t));
2817 2818 2819
      RC.ru[j]->idx                                 = j;
      printf("Creating RC.ru[%d]:%p\n", j, RC.ru[j]);
      RC.ru[j]->if_timing                           = synch_to_ext_device;
2820

2821 2822 2823
      if (RC.nb_L1_inst >0)
        RC.ru[j]->num_eNB                           = RUParamList.paramarray[j][RU_ENB_LIST_IDX].numelt;
      else
2824 2825 2826
        RC.ru[j]->num_eNB                           = 0;

      for (i=0; i<RC.ru[j]->num_eNB; i++) RC.ru[j]->eNB_list[i] = RC.eNB[RUParamList.paramarray[j][RU_ENB_LIST_IDX].iptr[i]][0];
2827

2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
      if (config_isparamset(RUParamList.paramarray[j], RU_SDR_ADDRS)) {
        RC.ru[j]->openair0_cfg.sdr_addrs = strdup(*(RUParamList.paramarray[j][RU_SDR_ADDRS].strptr));
      }

      if (config_isparamset(RUParamList.paramarray[j], RU_SDR_CLK_SRC)) {
        if (strcmp(*(RUParamList.paramarray[j][RU_SDR_CLK_SRC].strptr), "internal") == 0) {
          RC.ru[j]->openair0_cfg.clock_source = internal;
          LOG_D(PHY, "RU clock source set as internal\n");
        } else if (strcmp(*(RUParamList.paramarray[j][RU_SDR_CLK_SRC].strptr), "external") == 0) {
          RC.ru[j]->openair0_cfg.clock_source = external;
          LOG_D(PHY, "RU clock source set as external\n");
        } else if (strcmp(*(RUParamList.paramarray[j][RU_SDR_CLK_SRC].strptr), "gpsdo") == 0) {
          RC.ru[j]->openair0_cfg.clock_source = gpsdo;
          LOG_D(PHY, "RU clock source set as gpsdo\n");
        } else {
          LOG_E(PHY, "Erroneous RU clock source in the provided configuration file: '%s'\n", *(RUParamList.paramarray[j][RU_SDR_CLK_SRC].strptr));
        }
      }
2846 2847

      if (strcmp(*(RUParamList.paramarray[j][RU_LOCAL_RF_IDX].strptr), "yes") == 0) {
2848 2849 2850 2851 2852 2853 2854
        if ( !(config_isparamset(RUParamList.paramarray[j],RU_LOCAL_IF_NAME_IDX)) ) {
          RC.ru[j]->if_south                        = LOCAL_RF;
          RC.ru[j]->function                        = eNodeB_3GPP;
          printf("Setting function for RU %d to eNodeB_3GPP\n",j);
        } else {
          RC.ru[j]->eth_params.local_if_name            = strdup(*(RUParamList.paramarray[j][RU_LOCAL_IF_NAME_IDX].strptr));
          RC.ru[j]->eth_params.my_addr                  = strdup(*(RUParamList.paramarray[j][RU_LOCAL_ADDRESS_IDX].strptr));
2855 2856 2857 2858 2859 2860
          RC.ru[j]->eth_params.remote_addr              = strdup(*(RUParamList.paramarray[j][RU_REMOTE_ADDRESS_IDX].strptr));
          RC.ru[j]->eth_params.my_portc                 = *(RUParamList.paramarray[j][RU_LOCAL_PORTC_IDX].uptr);
          RC.ru[j]->eth_params.remote_portc             = *(RUParamList.paramarray[j][RU_REMOTE_PORTC_IDX].uptr);
          RC.ru[j]->eth_params.my_portd                 = *(RUParamList.paramarray[j][RU_LOCAL_PORTD_IDX].uptr);
          RC.ru[j]->eth_params.remote_portd             = *(RUParamList.paramarray[j][RU_REMOTE_PORTD_IDX].uptr);

2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
          if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "udp") == 0) {
            RC.ru[j]->if_south                        = LOCAL_RF;
            RC.ru[j]->function                        = NGFI_RRU_IF5;
            RC.ru[j]->eth_params.transp_preference    = ETH_UDP_MODE;
            printf("Setting function for RU %d to NGFI_RRU_IF5 (udp)\n",j);
          } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "raw") == 0) {
            RC.ru[j]->if_south                        = LOCAL_RF;
            RC.ru[j]->function                        = NGFI_RRU_IF5;
            RC.ru[j]->eth_params.transp_preference    = ETH_RAW_MODE;
            printf("Setting function for RU %d to NGFI_RRU_IF5 (raw)\n",j);
          } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "udp_if4p5") == 0) {
            RC.ru[j]->if_south                        = LOCAL_RF;
            RC.ru[j]->function                        = NGFI_RRU_IF4p5;
            RC.ru[j]->eth_params.transp_preference    = ETH_UDP_IF4p5_MODE;
            printf("Setting function for RU %d to NGFI_RRU_IF4p5 (udp)\n",j);
          } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "raw_if4p5") == 0) {
            RC.ru[j]->if_south                        = LOCAL_RF;
            RC.ru[j]->function                        = NGFI_RRU_IF4p5;
            RC.ru[j]->eth_params.transp_preference    = ETH_RAW_IF4p5_MODE;
            printf("Setting function for RU %d to NGFI_RRU_IF4p5 (raw)\n",j);
          }
        }

        RC.ru[j]->max_pdschReferenceSignalPower     = *(RUParamList.paramarray[j][RU_MAX_RS_EPRE_IDX].uptr);;
        RC.ru[j]->max_rxgain                        = *(RUParamList.paramarray[j][RU_MAX_RXGAIN_IDX].uptr);
        RC.ru[j]->num_bands                         = RUParamList.paramarray[j][RU_BAND_LIST_IDX].numelt;

        for (i=0; i<RC.ru[j]->num_bands; i++) RC.ru[j]->band[i] = RUParamList.paramarray[j][RU_BAND_LIST_IDX].iptr[i];
2889 2890
      } //strcmp(local_rf, "yes") == 0
      else {
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
        printf("RU %d: Transport %s\n",j,*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr));
        RC.ru[j]->eth_params.local_if_name        = strdup(*(RUParamList.paramarray[j][RU_LOCAL_IF_NAME_IDX].strptr));
        RC.ru[j]->eth_params.my_addr          = strdup(*(RUParamList.paramarray[j][RU_LOCAL_ADDRESS_IDX].strptr));
        RC.ru[j]->eth_params.remote_addr        = strdup(*(RUParamList.paramarray[j][RU_REMOTE_ADDRESS_IDX].strptr));
        RC.ru[j]->eth_params.my_portc         = *(RUParamList.paramarray[j][RU_LOCAL_PORTC_IDX].uptr);
        RC.ru[j]->eth_params.remote_portc       = *(RUParamList.paramarray[j][RU_REMOTE_PORTC_IDX].uptr);
        RC.ru[j]->eth_params.my_portd         = *(RUParamList.paramarray[j][RU_LOCAL_PORTD_IDX].uptr);
        RC.ru[j]->eth_params.remote_portd       = *(RUParamList.paramarray[j][RU_REMOTE_PORTD_IDX].uptr);

        if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "udp") == 0) {
          RC.ru[j]->if_south                     = REMOTE_IF5;
          RC.ru[j]->function                     = NGFI_RAU_IF5;
          RC.ru[j]->eth_params.transp_preference = ETH_UDP_MODE;
        } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "raw") == 0) {
          RC.ru[j]->if_south                     = REMOTE_IF5;
          RC.ru[j]->function                     = NGFI_RAU_IF5;
          RC.ru[j]->eth_params.transp_preference = ETH_RAW_MODE;
        } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "udp_if4p5") == 0) {
          RC.ru[j]->if_south                     = REMOTE_IF4p5;
          RC.ru[j]->function                     = NGFI_RAU_IF4p5;
          RC.ru[j]->eth_params.transp_preference = ETH_UDP_IF4p5_MODE;
        } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "raw_if4p5") == 0) {
          RC.ru[j]->if_south                     = REMOTE_IF4p5;
          RC.ru[j]->function                     = NGFI_RAU_IF4p5;
          RC.ru[j]->eth_params.transp_preference = ETH_RAW_IF4p5_MODE;
        } else if (strcmp(*(RUParamList.paramarray[j][RU_TRANSPORT_PREFERENCE_IDX].strptr), "raw_if5_mobipass") == 0) {
          RC.ru[j]->if_south                     = REMOTE_IF5;
          RC.ru[j]->function                     = NGFI_RAU_IF5;
          RC.ru[j]->if_timing                    = synch_to_other;
          RC.ru[j]->eth_params.transp_preference = ETH_RAW_IF5_MOBIPASS;
        }
2922 2923 2924 2925
      }  /* strcmp(local_rf, "yes") != 0 */

      RC.ru[j]->nb_tx                             = *(RUParamList.paramarray[j][RU_NB_TX_IDX].uptr);
      RC.ru[j]->nb_rx                             = *(RUParamList.paramarray[j][RU_NB_RX_IDX].uptr);
2926 2927
      RC.ru[j]->att_tx                            = *(RUParamList.paramarray[j][RU_ATT_TX_IDX].uptr);
      RC.ru[j]->att_rx                            = *(RUParamList.paramarray[j][RU_ATT_RX_IDX].uptr);
2928 2929
    }// j=0..num_rus
  } else {
2930
    RC.nb_RU = 0;
2931 2932 2933 2934
  } // setting != NULL

  return;
}