phy_procedures_lte_eNb.c 69.7 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22 23

/*! \file phy_procedures_lte_eNB.c
 * \brief Implementation of eNB procedures from 36.213 LTE specifications
24
 * \author R. Knopp, F. Kaltenberger, N. Nikaein, X. Foukas
25 26 27
 * \date 2011
 * \version 0.1
 * \company Eurecom
28
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr,navid.nikaein@eurecom.fr, x.foukas@sms.ed.ac.uk
29 30 31 32 33 34 35 36
 * \note
 * \warning
 */

#include "PHY/defs.h"
#include "PHY/extern.h"
#include "SCHED/defs.h"
#include "SCHED/extern.h"
37
#include "nfapi_interface.h"
38
#include "fapi_l1.h"
39 40 41
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"

42 43
#include "T.h"

44
#include "assertions.h"
Lionel Gauthier's avatar
Lionel Gauthier committed
45
#include "msc.h"
46

47 48
#include <time.h>

49
#if defined(ENABLE_ITTI)
Lionel Gauthier's avatar
Lionel Gauthier committed
50
#   include "intertask_interface.h"
51 52
#endif

53 54
extern uint8_t nfapi_mode;
int oai_nfapi_rach_ind(nfapi_rach_indication_t *rach_ind);
55

Raymond Knopp's avatar
 
Raymond Knopp committed
56

57

58
void pmch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,PHY_VARS_RN *rn,relaying_type_t r_type) {
59 60


Cedric Roux's avatar
Cedric Roux committed
61
#if defined(Rel10) || defined(Rel14)
Raymond Knopp's avatar
Raymond Knopp committed
62
  MCH_PDU *mch_pduP=NULL;
63
  MCH_PDU  mch_pdu;
64
  //  uint8_t sync_area=255;
65
#endif
66

67
  int subframe = proc->subframe_tx;
68

Raymond Knopp's avatar
Raymond Knopp committed
69 70
  AssertFatal(1==0,"pmch not tested for the moment, exiting\n");

71 72 73 74 75 76
  // This is DL-Cell spec pilots in Control region
  generate_pilots_slot(eNB,
		       eNB->common_vars.txdataF,
		       AMP,
		       subframe<<1,1);

77
  
Cedric Roux's avatar
Cedric Roux committed
78
#if defined(Rel10) || defined(Rel14)
79 80
  // if mcch is active, send regardless of the node type: eNB or RN
  // when mcch is active, MAC sched does not allow MCCH and MTCH multiplexing
81
  /*
82 83
  mch_pduP = mac_xface->get_mch_sdu(eNB->Mod_id,
				    eNB->CC_id,
84
				    proc->frame_tx,
85
				    subframe);
86
  */
87 88 89
  switch (r_type) {
  case no_relay:
    if ((mch_pduP->Pdu_size > 0) && (mch_pduP->sync_area == 0)) // TEST: only transmit mcch for sync area 0
Raymond Knopp's avatar
Raymond Knopp committed
90
      LOG_D(PHY,"[eNB%"PRIu8"] Frame %d subframe %d : Got MCH pdu for MBSFN (MCS %"PRIu8", TBS %d) \n",
91 92
	    eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->mcs,
	    eNB->dlsch_MCH->harq_processes[0]->TBS>>3);
93 94
    else {
      LOG_D(PHY,"[DeNB %"PRIu8"] Frame %d subframe %d : Do not transmit MCH pdu for MBSFN sync area %"PRIu8" (%s)\n",
95
	    eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->sync_area,
96 97
	    (mch_pduP->Pdu_size == 0)? "Empty MCH PDU":"Let RN transmit for the moment");
      mch_pduP = NULL;
Lionel Gauthier's avatar
Lionel Gauthier committed
98
    }
99
    
Raymond Knopp's avatar
 
Raymond Knopp committed
100
    break;
101 102 103
    
  case multicast_relay:
    if ((mch_pduP->Pdu_size > 0) && ((mch_pduP->mcch_active == 1) || mch_pduP->msi_active==1)) {
Raymond Knopp's avatar
Raymond Knopp committed
104
      LOG_D(PHY,"[RN %"PRIu8"] Frame %d subframe %d: Got the MCH PDU for MBSFN  sync area %"PRIu8" (MCS %"PRIu8", TBS %"PRIu16")\n",
105 106 107 108 109 110 111 112 113
	    rn->Mod_id,rn->frame, subframe,
	    mch_pduP->sync_area,mch_pduP->mcs,mch_pduP->Pdu_size);
    } else if (rn->mch_avtive[subframe%5] == 1) { // SF2 -> SF7, SF3 -> SF8
      mch_pduP= &mch_pdu;
      memcpy(&mch_pduP->payload, // could be a simple copy
	     rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->b,
	     rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->TBS>>3);
      mch_pduP->Pdu_size = (uint16_t) (rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->TBS>>3);
      mch_pduP->mcs = rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->mcs;
Raymond Knopp's avatar
Raymond Knopp committed
114
      LOG_D(PHY,"[RN %"PRIu8"] Frame %d subframe %d: Forward the MCH PDU for MBSFN received on SF %d sync area %"PRIu8" (MCS %"PRIu8", TBS %"PRIu16")\n",
115 116
	    rn->Mod_id,rn->frame, subframe,subframe%5,
	    rn->sync_area[subframe%5],mch_pduP->mcs,mch_pduP->Pdu_size);
Raymond Knopp's avatar
 
Raymond Knopp committed
117
    } else {
118
      mch_pduP=NULL;
Raymond Knopp's avatar
 
Raymond Knopp committed
119
    }
120 121
    
    rn->mch_avtive[subframe]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
122
    break;
123
    
124
  default:
125
    LOG_W(PHY,"[eNB %"PRIu8"] Frame %d subframe %d: unknown relaying type %d \n",
126
	  eNB->Mod_id,proc->frame_tx,subframe,r_type);
127 128 129 130 131
    mch_pduP=NULL;
    break;
  }// switch
  
  if (mch_pduP) {
132
    fill_eNB_dlsch_MCH(eNB,mch_pduP->mcs,1,0);
133
    // Generate PMCH
134
    generate_mch(eNB,proc,(uint8_t*)mch_pduP->payload);
135
  } else {
136
    LOG_D(PHY,"[eNB/RN] Frame %d subframe %d: MCH not generated \n",proc->frame_tx,subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
137
  }
138
  
139
#endif
140
}
141

142
void common_signal_procedures (PHY_VARS_eNB *eNB,int frame, int subframe) {
143

144
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
145
  int **txdataF = eNB->common_vars.txdataF;
146
  uint8_t *pbch_pdu=&eNB->pbch_pdu[0];
147

148
  //LOG_D(PHY,"common_signal_procedures: frame %d, subframe %d fdd:%s dir:%s\n",frame,subframe,fp->frame_type == FDD?"FDD":"TDD", subframe_select(fp,subframe) == SF_DL?"DL":"UL?"); 
149

150
  // generate Cell-Specific Reference Signals for both slots
151 152 153 154 155 156 157
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,1);
  generate_pilots_slot(eNB,
		       txdataF,
		       AMP,
		       subframe<<1,0);
  // check that 2nd slot is for DL
  if (subframe_select(fp,subframe) == SF_DL)
158 159 160
    generate_pilots_slot(eNB,
			 txdataF,
			 AMP,
161 162 163 164
			 (subframe<<1)+1,0);
  
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,0);
      
165

166
  // First half of PSS/SSS (FDD, slot 0)
Raymond Knopp's avatar
 
Raymond Knopp committed
167
  if (subframe == 0) {
168
    if (fp->frame_type == FDD) {
169 170 171 172 173 174 175 176 177 178 179
      generate_pss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 6 : 5,
		   0);
      generate_sss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 5 : 4,
		   0);
      
180
    }
181
    
182 183


184 185 186
      
    /// First half of SSS (TDD, slot 1)
    
187
    if (fp->frame_type == TDD) {
188 189 190 191 192 193
      generate_sss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 6 : 5,
		   1);
    }
194

195 196
    // generate PBCH (Physical Broadcast CHannel) info

197
    /// generate PBCH
198
    if ((frame&3)==0) {
199 200
      //AssertFatal(eNB->pbch_configured==1,"PBCH was not configured by MAC\n");
      if (eNB->pbch_configured!=1) return;
201
      eNB->pbch_configured=0;
202
    }
203 204 205 206 207 208 209
    generate_pbch(&eNB->pbch,
		  txdataF,
		  AMP,
		  fp,
		  pbch_pdu,
		  frame&3);
  
210
  }
211
  else if ((subframe == 1) &&
212
	   (fp->frame_type == TDD)){
213 214 215 216 217
    generate_pss(txdataF,
		 AMP,
		 fp,
		 2,
		 2);
218
  }
219 220 221
  
  // Second half of PSS/SSS (FDD, slot 10)
  else if ((subframe == 5) && 
222
	   (fp->frame_type == FDD)) {
223 224 225 226 227 228 229 230 231 232
    generate_pss(txdataF,
		 AMP,
		 &eNB->frame_parms,
		 (fp->Ncp==NORMAL) ? 6 : 5,
		 10);
    generate_sss(txdataF,
		 AMP,
		 &eNB->frame_parms,
		 (fp->Ncp==NORMAL) ? 5 : 4,
		 10);
233

234
  }
235

236 237
  //  Second-half of SSS (TDD, slot 11)
  else if ((subframe == 5) &&
238
	   (fp->frame_type == TDD)) {
239 240 241 242 243
    generate_sss(txdataF,
		 AMP,
		 fp,
		 (fp->Ncp==NORMAL) ? 6 : 5,
		 11);
Raymond Knopp's avatar
 
Raymond Knopp committed
244
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
245

246 247
  // Second half of PSS (TDD, slot 12)
  else if ((subframe == 6) &&
248
	   (fp->frame_type == TDD)) { 
249 250 251 252 253 254
    generate_pss(txdataF,
		 AMP,
		 fp,
		 2,
		 12);
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
255

256
}
Raymond Knopp's avatar
 
Raymond Knopp committed
257

258 259


260 261 262 263 264 265
void pdsch_procedures(PHY_VARS_eNB *eNB,
		      eNB_rxtx_proc_t *proc,
		      int harq_pid,
		      LTE_eNB_DLSCH_t *dlsch, 
		      LTE_eNB_DLSCH_t *dlsch1,
		      LTE_eNB_UE_stats *ue_stats,
266
		      int ra_flag) {
267

268 269
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
270 271
  LTE_DL_eNB_HARQ_t *dlsch_harq=dlsch->harq_processes[harq_pid];
  int input_buffer_length = dlsch_harq->TBS/8;
272
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
273

274
  if (dlsch->rnti == 0x02) {//frame < 200) {
275

276
    LOG_D(PHY,
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	  "[eNB %"PRIu8"][PDSCH %"PRIx16"/%"PRIu8"] Frame %d, subframe %d: Generating PDSCH/DLSCH with input size = %"PRIu16", pdsch_start %d, G %d, nb_rb %"PRIu16", rb0 %x, rb1 %x, TBS %"PRIu16", pmi_alloc %"PRIx64", rv %"PRIu8" (round %"PRIu8")\n",
	  eNB->Mod_id, dlsch->rnti,harq_pid,
	  frame, subframe, input_buffer_length, dlsch_harq->pdsch_start,
	  get_G(fp,
		dlsch_harq->nb_rb,
		dlsch_harq->rb_alloc,
		dlsch_harq->Qm,
		dlsch_harq->Nl,
		dlsch_harq->pdsch_start,
		frame,
		subframe,
		dlsch_harq->mimo_mode==TM7?7:0),
	  dlsch_harq->nb_rb,
	  dlsch_harq->rb_alloc[0],
	  dlsch_harq->rb_alloc[1],
	  dlsch_harq->TBS,
	  pmi2hex_2Ar1(dlsch_harq->pmi_alloc),
	  dlsch_harq->rvidx,
	  dlsch_harq->round);
  }    
297
#if defined(MESSAGE_CHART_GENERATOR_PHY)
298 299 300
  MSC_LOG_TX_MESSAGE(
		     MSC_PHY_ENB,MSC_PHY_UE,
		     NULL,0,
301
		     "%05u:%02u PDSCH/DLSCH input size = %"PRIu16", G %d, nb_rb %"PRIu16", TBS %"PRIu16", pmi_alloc %"PRIx16", rv %"PRIu8" (round %"PRIu8")",
302 303 304 305 306
		     frame, subframe,
		     input_buffer_length,
		     get_G(fp,
			   dlsch_harq->nb_rb,
			   dlsch_harq->rb_alloc,
307
			   dlsch_harq->Qm,
308
			   dlsch_harq->Nl,
309
			   dlsch_harq->pdsch_start,
310 311 312
			   frame,
			   subframe,
			   dlsch_harq->mimo_mode==TM7?7:0),
313
		     dlsch_harq->nb_rb,
314
		     dlsch_harq->TBS,
315 316 317 318
		     pmi2hex_2Ar1(dlsch_harq->pmi_alloc),
		     dlsch_harq->rvidx,
		     dlsch_harq->round);
#endif
319 320
  
  
321
  if (ue_stats) ue_stats->dlsch_sliding_cnt++;
322
  
323 324 325
  if (dlsch_harq->round == 0) {
    if (ue_stats)
      ue_stats->dlsch_trials[harq_pid][0]++;
326 327 328 329 330 331 332 333
  } else {
    ue_stats->dlsch_trials[harq_pid][dlsch_harq->round]++;
#ifdef DEBUG_PHY_PROC
#ifdef DEBUG_DLSCH
    LOG_D(PHY,"[eNB] This DLSCH is a retransmission\n");
#endif
#endif
  }
334 335


336 337
  LOG_D(PHY,"Generating DLSCH/PDSCH pdu:%p pdsch_start:%d frame:%d subframe:%d nb_rb:%d rb_alloc:%d Qm:%d Nl:%d round:%d\n",
      dlsch_harq->pdu,dlsch_harq->pdsch_start,frame,subframe,dlsch_harq->nb_rb,dlsch_harq->rb_alloc[0],dlsch_harq->Qm,dlsch_harq->Nl,dlsch_harq->round);
338
  // 36-212 
339 340 341 342 343 344
  if (nfapi_mode == 0 || nfapi_mode == 1) { // monolthic OR PNF - do not need turbo encoding on VNF

    if (dlsch_harq->pdu==NULL){
        LOG_E(PHY,"dlsch_harq->pdu == NULL SFN/SF:%04d%d dlsch[rnti:%x] dlsch_harq[pdu:%p pdsch_start:%d Qm:%d Nl:%d round:%d nb_rb:%d rb_alloc[0]:%d]\n", frame,subframe,dlsch->rnti, dlsch_harq->pdu,dlsch_harq->pdsch_start,dlsch_harq->Qm,dlsch_harq->Nl,dlsch_harq->round,dlsch_harq->nb_rb,dlsch_harq->rb_alloc[0]);
      return;
    }
345

346 347 348
    start_meas(&eNB->dlsch_encoding_stats);

    eNB->te(eNB,
349
	  dlsch_harq->pdu,
350
	  dlsch_harq->pdsch_start,
351
	  dlsch,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
352 353
	  frame,
	  subframe,
354 355
	  &eNB->dlsch_rate_matching_stats,
	  &eNB->dlsch_turbo_encoding_stats,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
356
	  &eNB->dlsch_turbo_encoding_waiting_stats,
357
      &eNB->dlsch_turbo_encoding_main_stats,
358 359
      &eNB->dlsch_turbo_encoding_wakeup_stats0,
      &eNB->dlsch_turbo_encoding_wakeup_stats1,
360
	  &eNB->dlsch_interleaving_stats);
361
    stop_meas(&eNB->dlsch_encoding_stats);
362 363 364 365 366
  //////////////////////////////////////////////////*******************************************
  if(eNB->dlsch_encoding_stats.diff_now>500*3000 && opp_enabled == 1)
  {
    print_meas_now(&eNB->dlsch_encoding_stats,"total coding",stderr);
  }
367
  // 36-211
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    start_meas(&eNB->dlsch_scrambling_stats);
    dlsch_scrambling(fp,
        0,
        dlsch,
        harq_pid,
        get_G(fp,
          dlsch_harq->nb_rb,
          dlsch_harq->rb_alloc,
          dlsch_harq->Qm,
          dlsch_harq->Nl,
          dlsch_harq->pdsch_start,
          frame,subframe,
          0),
        0,
        frame,
        subframe<<1);
    stop_meas(&eNB->dlsch_scrambling_stats);

    start_meas(&eNB->dlsch_modulation_stats);

388
  
389
    dlsch_modulation(eNB,
390 391 392
		   eNB->common_vars.txdataF,
		   AMP,
		   subframe,
393
		   dlsch_harq->pdsch_start,
394 395 396
		   dlsch,
		   dlsch1);
  
397 398
    stop_meas(&eNB->dlsch_modulation_stats);
  }
399

400
  dlsch->active = 0;
401
  dlsch_harq->round++;
402
//printf("  dlsch_harq ++ %p %d.%d harq %d rnti %d\n", dlsch_harq, frame, subframe, harq_pid, dlsch->rnti);
403 404

  LOG_D(PHY,"Generating DLSCH/PDSCH dlsch_harq[round:%d]\n",dlsch_harq->round);
405
}
406 407


408

409

410
void phy_procedures_eNB_TX(PHY_VARS_eNB *eNB,
411
			   eNB_rxtx_proc_t *proc,
412
                           relaying_type_t r_type,
413 414
			   PHY_VARS_RN *rn,
			   int do_meas)
415 416
{
  UNUSED(rn);
417 418
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
419
  uint32_t i,aa;
420
  uint8_t harq_pid;
421
  int8_t UE_id=0;
422
  uint8_t num_pdcch_symbols=0;
423
  uint8_t num_dci=0;
424 425
  uint8_t ul_subframe;
  uint32_t ul_frame;
426
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
427
  LTE_UL_eNB_HARQ_t *ulsch_harq;
428

429
  int offset = eNB->CC_id;//proc == &eNB->proc.proc_rxtx[0] ? 0 : 1;
430

431

432
  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)==SF_UL)) return;
433

434
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+offset,1);
435
  if (do_meas==1) start_meas(&eNB->phy_proc_tx);
436

437
  // clear the transmit data array for the current subframe
438 439 440
  for (aa=0; aa<fp->nb_antenna_ports_eNB; aa++) {      
    memset(&eNB->common_vars.txdataF[aa][subframe*fp->ofdm_symbol_size*(fp->symbols_per_tti)],
	   0,fp->ofdm_symbol_size*(fp->symbols_per_tti)*sizeof(int32_t));
Raymond Knopp's avatar
 
Raymond Knopp committed
441
  }
442
  
443

444

445 446 447 448 449 450 451 452
  if (nfapi_mode == 0 || nfapi_mode == 1) {
    if (is_pmch_subframe(frame,subframe,fp)) {
      pmch_procedures(eNB,proc,rn,r_type);
    }
    else {
      // this is not a pmch subframe, so generate PSS/SSS/PBCH
      common_signal_procedures(eNB,proc->frame_tx, proc->subframe_tx);
    }
453
  }
454

455

Raymond Knopp's avatar
 
Raymond Knopp committed
456
  // clear existing ulsch dci allocations before applying info from MAC  (this is table
457
  ul_subframe = pdcch_alloc2ul_subframe(fp,subframe);
458
  ul_frame = pdcch_alloc2ul_frame(fp,frame,subframe);
459 460 461



Raymond Knopp's avatar
 
Raymond Knopp committed
462
  // clear previous allocation information for all UEs
463
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
464 465
    if (eNB->dlsch[i][0])
      eNB->dlsch[i][0]->subframe_tx[subframe] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
466
  }
467

468
  /* save old HARQ information needed for PHICH generation */
469 470 471 472
  /* TODO: check the following test - in the meantime it is put back as it was before */
  //if ((ul_subframe < 10)&&
  //    (subframe_select(fp,ul_subframe)==SF_UL)) { // This means that there is a potential UL subframe that will be scheduled here
  if (ul_subframe < 10) { // This means that there is a potential UL subframe that will be scheduled here
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    for (i=0; i<NUMBER_OF_UE_MAX; i++) {
      harq_pid = subframe2harq_pid(fp,ul_frame,ul_subframe);
      if (eNB->ulsch[i]) {
	ulsch_harq = eNB->ulsch[i]->harq_processes[harq_pid];
	
	/* Store first_rb and n_DMRS for correct PHICH generation below.
	 * For PHICH generation we need "old" values of last scheduling
	 * for this HARQ process. 'generate_eNB_dlsch_params' below will
	 * overwrite first_rb and n_DMRS and 'generate_phich_top', done
	 * after 'generate_eNB_dlsch_params', would use the "new" values
	 * instead of the "old" ones.
	 *
	 * This has been tested for FDD only, may be wrong for TDD.
	 *
	 * TODO: maybe we should restructure the code to be sure it
	 *       is done correctly. The main concern is if the code
	 *       changes and first_rb and n_DMRS are modified before
	 *       we reach here, then the PHICH processing will be wrong,
	 *       using wrong first_rb and n_DMRS values to compute
	 *       ngroup_PHICH and nseq_PHICH.
	 *
	 * TODO: check if that works with TDD.
	 */
	ulsch_harq->previous_first_rb = ulsch_harq->first_rb;
	ulsch_harq->previous_n_DMRS   = ulsch_harq->n_DMRS;
	
499 500 501 502
      }
    }
  }

503

504

505 506 507 508 509
  //  num_pdcch_symbols = DCI_pdu->num_pdcch_symbols;
  num_pdcch_symbols = eNB->pdcch_vars[subframe&1].num_pdcch_symbols;
  num_dci           = eNB->pdcch_vars[subframe&1].num_dci;
  //  LOG_D(PHY,"num_pdcch_symbols %"PRIu8",(dci common %"PRIu8", dci uespec %"PRIu8"\n",num_pdcch_symbols,
  //        DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci);
Cedric Roux's avatar
Cedric Roux committed
510
  LOG_D(PHY,"num_pdcch_symbols %"PRIu8",number dci %"PRIu8"\n",num_pdcch_symbols, num_dci);
511
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,num_pdcch_symbols);
512

513

514 515
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,(frame*10)+subframe);

516
  if (num_dci > 0)
Cedric Roux's avatar
Cedric Roux committed
517 518
    LOG_D(PHY,"[eNB %"PRIu8"] Frame %d, subframe %d: Calling generate_dci_top (pdcch) (num_dci %"PRIu8") num_pdcch_symbols:%d\n",eNB->Mod_id,frame, subframe, num_dci, num_pdcch_symbols);

519 520 521
  //LOG_D(PHY,"Before generate_dci_top num_pdcch_symbols:%d num_dci:%d dci_alloc:dci_length:%d\n", num_pdcch_symbols, num_dci, eNB->pdcch_vars[subframe&1].dci_alloc[0].dci_length);

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,1);
522
//printf("//////////************before generate_dci_top UL %d.%d TX %d.%d \n", ul_frame, ul_subframe, frame, subframe);
523 524 525 526 527 528 529 530 531 532
  if (nfapi_mode == 0 || nfapi_mode == 1) {
    generate_dci_top(num_pdcch_symbols,
        num_dci,
        &eNB->pdcch_vars[subframe&1].dci_alloc[0],
        0,
        AMP,
        fp,
        eNB->common_vars.txdataF,
        subframe);
  }
533

534

535
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,0);
536

537
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_DLSCH,1);
Raymond Knopp's avatar
 
Raymond Knopp committed
538
  // Now scan UE specific DLSCH
539
  LTE_eNB_DLSCH_t *dlsch0,*dlsch1;
540
  for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++)
541
    {
542 543 544 545 546 547
      dlsch0 = eNB->dlsch[(uint8_t)UE_id][0]; 
      dlsch1 = eNB->dlsch[(uint8_t)UE_id][1]; 

      if ((dlsch0)&&
	  (dlsch0->rnti>0)&&
	  (dlsch0->active == 1)) {
548

549
	// get harq_pid
550
	harq_pid = dlsch0->harq_ids[subframe];
551
	AssertFatal(harq_pid>=0,"harq_pid is negative\n");
552 553 554 555 556 557 558 559

        if (harq_pid>=8)
        {
          LOG_E(PHY,"harq_pid:%d corrupt must be 0-7 UE_id:%d frame:%d subframe:%d rnti:%x\n", harq_pid,UE_id,frame,subframe,dlsch0->rnti);
        }
        else
        {
          // generate pdsch
560
//printf("%d.%d pdsch_procedures ue rnti %d\n", frame, subframe, dlsch0->rnti);///////////////////////////////////**********************
561 562 563 564 565 566 567 568
          pdsch_procedures(eNB,
              proc,
              harq_pid,
              dlsch0,
              dlsch1,
              &eNB->UE_stats[(uint32_t)UE_id],
              0);
        }
569 570


Raymond Knopp's avatar
 
Raymond Knopp committed
571
      }
572

Lionel Gauthier's avatar
Lionel Gauthier committed
573

574
      else if ((dlsch0)&&
575 576
          (dlsch0->rnti>0)&&
          (dlsch0->active == 0)) {
577

578 579
        // clear subframe TX flag since UE is not scheduled for PDSCH in this subframe (so that we don't look for PUCCH later)
        dlsch0->subframe_tx[subframe]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
580
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
581
    }
582
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_DLSCH,0);
583

584 585


586
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_PHICH,1);
587 588 589
  generate_phich_top(eNB,
		     proc,
		     AMP);
590
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_PHICH,0);
591

592
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+offset,0);
593
  if (do_meas==1) stop_meas(&eNB->phy_proc_tx);
594
  
595 596
}

597

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
void srs_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {

  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;

  int i;

  if (is_srs_occasion_common(fp,frame,subframe)) { 
  
  // Do SRS processing 
  // check if there is SRS and we have to use shortened format
  // TODO: check for exceptions in transmission of SRS together with ACK/NACK
    for (i=0;i<NUMBER_OF_UE_MAX;i++) {

      if (eNB->soundingrs_ul_config_dedicated[i].active==1) {

      
	if (lte_srs_channel_estimation(fp,
				       &eNB->common_vars,
				       &eNB->srs_vars[i],
				       &eNB->soundingrs_ul_config_dedicated[i],
				       subframe,
				       0/*eNB_id*/)) {
	  LOG_E(PHY,"problem processing SRS\n");
	}
	eNB->soundingrs_ul_config_dedicated[i].active=0;
      }
    }
  }
}

630 631
void fill_sr_indication(PHY_VARS_eNB *eNB,uint16_t rnti,int frame,int subframe,uint32_t stat) {
  
632
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
633 634 635 636 637 638 639 640
  nfapi_sr_indication_t       *sr_ind =         &eNB->UL_INFO.sr_ind;
  nfapi_sr_indication_body_t  *sr_ind_body =    &sr_ind->sr_indication_body;
  nfapi_sr_indication_pdu_t *pdu =   &sr_ind_body->sr_pdu_list[sr_ind_body->number_of_srs];

  sr_ind->sfn_sf = frame<<4|subframe;
  sr_ind->header.message_id = NFAPI_RX_SR_INDICATION;

  sr_ind_body->tl.tag = NFAPI_SR_INDICATION_BODY_TAG;
641 642 643

  pdu->instance_length                                = 0; // don't know what to do with this
  //  pdu->rx_ue_information.handle                       = handle;
644
  pdu->rx_ue_information.tl.tag                       = NFAPI_RX_UE_INFORMATION_TAG;
645 646
  pdu->rx_ue_information.rnti                         = rnti;

647 648 649
  int SNRtimes10 = dB_fixed_times10(stat) - 200;//(10*eNB->measurements.n0_power_dB[0]);


650 651
  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;

652 653 654 655 656
  if      (SNRtimes10 < -640) pdu->ul_cqi_information.ul_cqi=0;
  else if (SNRtimes10 >  635) pdu->ul_cqi_information.ul_cqi=255;
  else                        pdu->ul_cqi_information.ul_cqi=(640+SNRtimes10)/5;
  pdu->ul_cqi_information.channel = 0;

657
  sr_ind_body->number_of_srs++;
658 659 660 661
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
}

void uci_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
Cedric Roux's avatar
Cedric Roux committed
662
{
663
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
664
  uint8_t SR_payload = 0,pucch_b0b1[4][2]= {{0,0},{0,0},{0,0},{0,0}},harq_ack[4]={0,0,0,0};
665
  int32_t metric[4]={0,0,0,0},metric_SR=0,max_metric=0;
666 667
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;
668 669 670 671
  int i;
  LTE_eNB_UCI *uci;
  uint16_t tdd_multiplexing_mask=0;
  int res;
672

673
  for (i=0;i<NUMBER_OF_UE_MAX;i++) {
Cedric Roux's avatar
Cedric Roux committed
674

675 676 677 678
    uci = &eNB->uci_vars[i];
    if ((uci->active == 1) &&
	(uci->frame == frame) &&
	(uci->subframe == subframe)) {
679

680
      LOG_D(PHY,"Frame %d, subframe %d: Running uci procedures (type %d) for %d \n",frame,subframe,uci->type,i);
681
      uci->active=0;
Cedric Roux's avatar
Cedric Roux committed
682

683 684 685
      // Null out PUCCH PRBs for noise measurement
      switch(fp->N_RB_UL) {
      case 6:
Cedric Roux's avatar
Cedric Roux committed
686 687
        eNB->rb_mask_ul[0] |= (0x1 | (1<<5)); //position 5
        break;
688
      case 15:
Cedric Roux's avatar
Cedric Roux committed
689 690
        eNB->rb_mask_ul[0] |= (0x1 | (1<<14)); // position 14
        break;
691
      case 25:
Cedric Roux's avatar
Cedric Roux committed
692 693
        eNB->rb_mask_ul[0] |= (0x1 | (1<<24)); // position 24
        break;
694
      case 50:
Cedric Roux's avatar
Cedric Roux committed
695 696 697
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[1] |= (1<<17); // position 49 (49-32)
        break;
698
      case 75:
Cedric Roux's avatar
Cedric Roux committed
699 700 701
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[2] |= (1<<10); // position 74 (74-64)
        break;
702
      case 100:
Cedric Roux's avatar
Cedric Roux committed
703 704 705
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[3] |= (1<<3); // position 99 (99-96)
        break;
706
      default:
Cedric Roux's avatar
Cedric Roux committed
707 708
        LOG_E(PHY,"Unknown number for N_RB_UL %d\n",fp->N_RB_UL);
        break;
709
      }
710

711 712 713 714 715 716 717 718
      switch (uci->type) {
      case SR:
      case HARQ_SR:
		
	metric_SR = rx_pucch(eNB,
			      uci->pucch_fmt,
			      i,
			      uci->n_pucch_1_0_sr[0],
719
			      0, // n2_pucch
720
			      uci->srs_active, // shortened format
721 722 723 724
			      &SR_payload,
			      frame,
			      subframe,
			      PUCCH1_THRES);
Raymond Knopp's avatar
Raymond Knopp committed
725
	LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR is %d (SR n1pucch is %d)\n",
726
	      eNB->Mod_id,
727
	      uci->rnti,
728 729 730
	      frame,
	      subframe,
	      SR_payload,
731 732 733
	      uci->n_pucch_1_0_sr[0]);
	if (uci->type == SR) {
	  if (SR_payload == 1) {
734
	    fill_sr_indication(eNB,uci->rnti,frame,subframe,metric_SR);
735
        continue;
736 737
	  }
	  else {
738
        continue;
739 740 741 742
	  }
	}
      case HARQ:
	if (fp->frame_type == FDD) {
743
	  LOG_D(PHY,"Frame %d Subframe %d Demodulating PUCCH (UCI %d) for ACK/NAK (uci->pucch_fmt %d,uci->type %d.uci->frame %d, uci->subframe %d): n1_pucch0 %d SR_payload %d\n",
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
		frame,subframe,i,
		uci->pucch_fmt,uci->type,
		uci->frame,uci->subframe,uci->n_pucch_1[0][0],
		SR_payload);
	  
	  metric[0] = rx_pucch(eNB,
			       uci->pucch_fmt,
			       i,
			       uci->n_pucch_1[0][0],
			       0, //n2_pucch
			       uci->srs_active, // shortened format
			       pucch_b0b1[0],
			       frame,
			       subframe,
			       PUCCH1a_THRES);
	  
	  
	  /* cancel SR detection if reception on n1_pucch0 is better than on SR PUCCH resource index, otherwise send it up to MAC */
	  if (uci->type==HARQ_SR && metric[0] > metric_SR) SR_payload = 0;
763
	  else if (SR_payload == 1) fill_sr_indication(eNB,uci->rnti,frame,subframe,metric_SR);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
 
	  if (uci->type==HARQ_SR && metric[0] <= metric_SR) {
	    /* when transmitting ACK/NACK on SR PUCCH resource index, SR payload is always 1 */
	    SR_payload = 1;
	    
	    metric[0]=rx_pucch(eNB,
			       uci->pucch_fmt,
			       i,
			       uci->n_pucch_1_0_sr[0],
			       0, //n2_pucch
			       uci->srs_active, // shortened format
			       pucch_b0b1[0],
			       frame,
			       subframe,
			       PUCCH1a_THRES);
	  }
	  
781

782
	  LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d pucch1a (FDD) payload %d (metric %d)\n",
783 784 785
		eNB->Mod_id,
		uci->rnti,
		frame,subframe,
786 787
		pucch_b0b1[0][0],metric[0]);

788
      uci->stat = metric[0]; 	  
789
	  fill_uci_harq_indication(eNB,uci,frame,subframe,pucch_b0b1[0],0,0xffff);
790

791 792
	}
	else { // frame_type == TDD
Raymond Knopp's avatar
Raymond Knopp committed
793

794

795 796
	  // if SR was detected, use the n1_pucch from SR
	  if (SR_payload==1) {
797
#ifdef DEBUG_PHY_PROC
798 799 800 801
	    LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d with SR\n",eNB->Mod_id,
		  eNB->dlsch[UE_id][0]->rnti,
		  frame,subframe,
		  n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
802
#endif
803 804 805 806 807 808 809 810 811 812 813 814
	    
	    metric[0] = rx_pucch(eNB,
				 pucch_format1b,
				 i,
				 uci->n_pucch_1_0_sr[0],
				 0, //n2_pucch
				 uci->srs_active, // shortened format
				 pucch_b0b1[0],
				 frame,
				 subframe,
				 PUCCH1a_THRES);
	  } else { //using assigned pucch resources
815
#ifdef DEBUG_PHY_PROC
816 817 818 819 820 821 822 823 824
	    LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK M=%d (%d,%d,%d,%d) format %d\n",eNB->Mod_id,
		  eNB->dlsch[UE_id][0]->rnti,
		  frame,subframe,
		  uci->num_pucch_resources,
		  uci->n_pucch_1[res][0],
		  uci->n_pucch_1[res][1],
		  uci->n_pucch_1[res][2],
		  uci->n_pucch_1[res][3],
		  uci->pucch_fmt);
825
#endif
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	    for (res=0;res<uci->num_pucch_resources;res++)
	      metric[res] = rx_pucch(eNB,
				     uci->pucch_fmt,
				     i,
				     uci->n_pucch_1[res][0],
				     0, // n2_pucch
				     uci->srs_active, // shortened format
				     pucch_b0b1[res],
				     frame,
				     subframe,
				     PUCCH1a_THRES);
	  	  
	    

	  }
841

842
	  
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	  if (SR_payload == 1) { // this implements Table 7.3.1 from 36.213
	    if (pucch_b0b1[0][0] == 4) { // there isn't a likely transmission
	      harq_ack[0] = 4; // DTX
	    }
	    else if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] == 1) { // 1/4/7 ACKs
	      harq_ack[0] = 1;
	    }
	    else if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] != 1) { // 2/5/8 ACKs
	      harq_ack[0] = 2;
	    }
	    else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] == 1) { // 3/6/9 ACKs
	      harq_ack[0] = 3;
	    }
	    else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] != 1) { // 0 ACKs, or at least one DL assignment missed
	      harq_ack[0] = 0;
	    }
859
        uci->stat = metric[0];
860 861
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,2,0xffff); // special_bundling mode
	  } 
Raymond Knopp's avatar
Raymond Knopp committed
862
	  else if ((uci->tdd_bundling == 0) && (uci->num_pucch_resources==2)){ // multiplexing + no SR, implement Table 10.1.3-5 (Rel14) for multiplexing with M=2
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	    if (pucch_b0b1[0][0] == 4 ||
		pucch_b0b1[1][0] == 4) { // there isn't a likely transmission
	      harq_ack[0] = 4; // DTX
	      harq_ack[1] = 6; // NACK/DTX
	    } 
	    else {
	      if (metric[1]>metric[0]) {
		if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] != 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  tdd_multiplexing_mask = 0x3;
		}
		else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] == 1){
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  tdd_multiplexing_mask = 0x2;
		}
		else {
		  harq_ack[0] = 4; // DTX
		  harq_ack[1] = 4; // DTX
		}
	      }
	      else {
		if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x1;
		}
		else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] != 1){
		  harq_ack[0] = 2; // NACK
		  harq_ack[1] = 6; // NACK/DTX
		}
		else {
		  harq_ack[0] = 4; // DTX
		  harq_ack[1] = 4; // DTX
		}
	      }
	    }
901
        uci->stat = max(metric[0],metric[1]);
902
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
903
	  } //else if ((uci->tdd_bundling == 0) && (res==2))
Raymond Knopp's avatar
Raymond Knopp committed
904
	  else if ((uci->tdd_bundling == 0) && (uci->num_pucch_resources==3)){ // multiplexing + no SR, implement Table 10.1.3-6 (Rel14) for multiplexing with M=3
905 906 907 908 909 910 911
	    
	    if (harq_ack[0] == 4 ||
		harq_ack[1] == 4 ||
		harq_ack[2] == 4) { // there isn't a likely transmission
	      harq_ack[0] = 4; // DTX
	      harq_ack[1] = 6; // NACK/DTX
	      harq_ack[2] = 6; // NACK/DTX
Cedric Roux's avatar
Cedric Roux committed
912
              max_metric = 0;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	    } 
	    else {
	      
	      max_metric = max(metric[0],max(metric[1],metric[2]));
	      
	      if (metric[0]==max_metric) {
		if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x1;
		}
		else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] != 1){
		  harq_ack[0] = 2; // NACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		}
		else {
		  harq_ack[0] = 4; // DTX
		  harq_ack[1] = 4; // DTX
		  harq_ack[2] = 4; // DTX
		}
	      } // if (metric[0]==max_metric) {
	      else if (metric[1]==max_metric) {
	      
	        if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] != 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x3;
		}
		else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] == 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x2;
		}
		else {
		  harq_ack[0] = 4; // DTX
		  harq_ack[1] = 4; // DTX
		  harq_ack[2] = 4; // DTX
		}
	      } // if (metric[1]==max_metric) {
	      else {
  	        if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  tdd_multiplexing_mask = 0x7;
		}
		else if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  tdd_multiplexing_mask = 0x5;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] == 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  tdd_multiplexing_mask = 0x6;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  tdd_multiplexing_mask = 0x4;
		}
	      }
982
            uci->stat = max_metric;
983
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
984
	    }
985
	  } //else if ((uci->tdd_bundling == 0) && (res==3)) 
Raymond Knopp's avatar
Raymond Knopp committed
986
	  else if ((uci->tdd_bundling == 0) && (uci->num_pucch_resources==4)){ // multiplexing + no SR, implement Table 10.1.3-7 (Rel14) for multiplexing with M=4
987 988 989 990 991 992 993 994
	    if (pucch_b0b1[0][0] == 4 ||
		pucch_b0b1[1][0] == 4 ||
		pucch_b0b1[2][0] == 4 ||
		pucch_b0b1[3][0] == 4) { // there isn't a likely transmission
	      harq_ack[0] = 4; // DTX
	      harq_ack[1] = 6; // NACK/DTX
	      harq_ack[2] = 6; // NACK/DTX
	      harq_ack[3] = 6; // NACK/DTX
Cedric Roux's avatar
Cedric Roux committed
995
              max_metric = 0;
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	    } else {

	      max_metric = max(metric[0],max(metric[1],max(metric[2],metric[3])));
	      
	      if (metric[0]==max_metric) {
		if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] != 1){
		  harq_ack[0] = 2; // NACK
		  harq_ack[1] = 4; // DTX
		  harq_ack[2] = 4; // DTX
		  harq_ack[3] = 4; // DTX
		}
		else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0x9;
		}
		else if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x1;
		}
		else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] != 1){
		  harq_ack[0] = 2; // NACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 6; // NACK/DTX
		}
		
	      } 
	      else if (metric[1]==max_metric) {
		if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0xF;
		}
		else if (pucch_b0b1[1][0] == 1 && pucch_b0b1[1][1] != 1 ) {
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x3;
		}
		else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] != 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0xE;
		}
		else if (pucch_b0b1[1][0] != 1 && pucch_b0b1[1][1] == 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x2;
		}
	      } 
	      else if (metric[2]==max_metric) {
		if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x7;
		}
		else if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 6; // NACK/DTX
		  tdd_multiplexing_mask = 0x5;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] == 1 ) {
		  harq_ack[0] = 4; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 4; // NACK/DTX
		  tdd_multiplexing_mask = 0x6;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 4; // NACK/DTX
		  harq_ack[1] = 4; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 4; // NACK/DTX
		  tdd_multiplexing_mask = 0x4;
		}
	      } 
	      else { // max_metric[3]=max_metric
		if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] == 1){
		  harq_ack[0] = 1; // ACK
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0xD;
		}
		else if (pucch_b0b1[2][0] == 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 1; // ACK
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0xA;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] == 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 1; // ACK
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0xC;
		}
		else if (pucch_b0b1[2][0] != 1 && pucch_b0b1[2][1] != 1 ) {
		  harq_ack[0] = 6; // NACK/DTX
		  harq_ack[1] = 6; // NACK/DTX
		  harq_ack[2] = 6; // NACK/DTX
		  harq_ack[3] = 1; // ACK
		  tdd_multiplexing_mask = 0x8;
		}
	      }
	    }
1120
        uci->stat = max_metric;
1121
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
1122
	  } // else if ((uci->tdd_bundling == 0) && (res==4))
1123 1124 1125
	  else { // bundling
	    harq_ack[0] = pucch_b0b1[0][0];
	    harq_ack[1] = pucch_b0b1[0][1];
1126
        uci->stat = metric[0];
1127 1128
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,0,0xffff); // special_bundling mode
	  }
1129
	  
1130
#ifdef DEBUG_PHY_PROC
Raymond Knopp's avatar
Raymond Knopp committed
1131
	  LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d ACK/NAK metric 0 %d, metric 1 %d, (%d,%d)\n",eNB->Mod_id,
1132 1133
		eNB->dlsch[UE_id][0]->rnti,
		frame,subframe,
Raymond Knopp's avatar
Raymond Knopp committed
1134
		metric0,metric1,pucch_b0b1[0],pucch_b0b1[1]);
1135
#endif
1136 1137 1138 1139 1140
	}
	break;
      default:
	AssertFatal(1==0,"Unsupported UCI type %d\n",uci->type);
	break;
Cedric Roux's avatar
Cedric Roux committed
1141
      }
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    
      if (SR_payload == 1) {
	LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Got SR for PUSCH, transmitting to MAC\n",eNB->Mod_id,
	      uci->rnti,frame,subframe);
	
	if (eNB->first_sr[i] == 1) { // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
	  eNB->first_sr[i] = 0;
	  eNB->dlsch[i][0]->harq_processes[0]->round=0;
	  eNB->dlsch[i][0]->harq_processes[0]->status=SCH_IDLE;
	  LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d First SR\n",
		eNB->Mod_id,
		eNB->ulsch[i]->rnti,frame,subframe);
	}
Cedric Roux's avatar
Cedric Roux committed
1155 1156 1157
      }
    }
  }
1158
}
1159

Cedric Roux's avatar
Cedric Roux committed
1160 1161
void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
{
Raymond Knopp's avatar
Raymond Knopp committed
1162 1163
  uint32_t ret=0,i;
  uint32_t harq_pid;
1164 1165 1166 1167
  uint8_t nPRS;
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  LTE_eNB_ULSCH_t *ulsch;
  LTE_UL_eNB_HARQ_t *ulsch_harq;
1168

1169 1170
  const int subframe = proc->subframe_rx;
  const int frame    = proc->frame_rx;
1171
  
1172 1173
  if (fp->frame_type == FDD) harq_pid = ((10*frame) + subframe)&7;
  else                       harq_pid = subframe%10;
1174

1175 1176 1177
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    ulsch = eNB->ulsch[i];
    ulsch_harq = ulsch->harq_processes[harq_pid];
1178 1179 1180 1181
    if (ulsch->rnti>0) LOG_D(PHY,"eNB->ulsch[%d]->harq_processes[harq_pid:%d] SFN/SF:%04d%d: PUSCH procedures, UE %d/%x ulsch_harq[status:%d SFN/SF:%04d%d handled:%d]\n",
			     i, harq_pid, frame,subframe,i,ulsch->rnti,
                             ulsch_harq->status, ulsch_harq->frame, ulsch_harq->subframe, ulsch_harq->handled);
    
1182 1183 1184
    if ((ulsch) &&
        (ulsch->rnti>0) &&
        (ulsch_harq->status == ACTIVE) &&
1185 1186
	    (ulsch_harq->frame == frame) &&
	    (ulsch_harq->subframe == subframe) &&
1187
        (ulsch_harq->handled == 0)) {
1188
      
1189
      // UE has ULSCH scheduling
1190 1191
      for (int rb=0;
           rb<=ulsch_harq->nb_rb;
1192 1193 1194
	   rb++) {
	int rb2 = rb+ulsch_harq->first_rb;
	eNB->rb_mask_ul[rb2>>5] |= (1<<(rb2&31));
1195
      }
1196

Cedric Roux's avatar
Cedric Roux committed
1197
      LOG_D(PHY,"[eNB %d] frame %d, subframe %d: Scheduling ULSCH Reception for UE %d \n", eNB->Mod_id, frame, subframe, i);
1198

1199
      nPRS = fp->pusch_config_common.ul_ReferenceSignalsPUSCH.nPRS[subframe<<1];
1200

Cedric Roux's avatar
Cedric Roux committed
1201
      ulsch->cyclicShift = (ulsch_harq->n_DMRS2 +
1202 1203
          fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift +
          nPRS)%12;
1204

1205
      LOG_D(PHY,
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
          "[eNB %d][PUSCH %d] Frame %d Subframe %d Demodulating PUSCH: dci_alloc %d, rar_alloc %d, round %d, first_rb %d, nb_rb %d, Qm %d, TBS %d, rv %d, cyclic_shift %d (n_DMRS2 %d, cyclicShift_common %d, ), O_ACK %d, beta_cqi %d \n",
          eNB->Mod_id,harq_pid,frame,subframe,
          ulsch_harq->dci_alloc,
          ulsch_harq->rar_alloc,
          ulsch_harq->round,
          ulsch_harq->first_rb,
          ulsch_harq->nb_rb,
          ulsch_harq->Qm,
          ulsch_harq->TBS,
          ulsch_harq->rvidx,
          ulsch->cyclicShift,
          ulsch_harq->n_DMRS2,
          fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift,
          ulsch_harq->O_ACK,
          ulsch->beta_offset_cqi_times8);

        start_meas(&eNB->ulsch_demodulation_stats);
1223

1224
	rx_ulsch(eNB,proc, i);
1225

1226
        stop_meas(&eNB->ulsch_demodulation_stats);
1227

1228
        start_meas(&eNB->ulsch_decoding_stats);
1229

1230 1231 1232 1233 1234
        ret = ulsch_decoding(eNB,proc,
            i,
            0, // control_only_flag
            ulsch_harq->V_UL_DAI,
            ulsch_harq->nb_rb>20 ? 1 : 0);
Sandeep Kumar's avatar
Sandeep Kumar committed
1235

1236
        stop_meas(&eNB->ulsch_decoding_stats);
1237

1238
        LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d RNTI %x RX power (%d,%d) N0 (%d,%d) dB ACK (%d,%d), decoding iter %d ulsch_harq->cqi_crc_status:%d ackBits:%d ulsch_decoding_stats[t:%lld max:%lld]\n",
1239 1240 1241 1242 1243 1244 1245 1246 1247
            eNB->Mod_id,harq_pid,
            frame,subframe,
            ulsch->rnti,
            dB_fixed(eNB->pusch_vars[i]->ulsch_power[0]),
            dB_fixed(eNB->pusch_vars[i]->ulsch_power[1]),
            20,//eNB->measurements.n0_power_dB[0],
            20,//eNB->measurements.n0_power_dB[1],
            ulsch_harq->o_ACK[0],
            ulsch_harq->o_ACK[1],
1248 1249 1250 1251
            ret,
            ulsch_harq->cqi_crc_status,
            ulsch_harq->O_ACK,
            eNB->ulsch_decoding_stats.diff_now, eNB->ulsch_decoding_stats.max);
1252

1253 1254
        //compute the expected ULSCH RX power (for the stats)
        ulsch_harq->delta_TF = get_hundred_times_delta_IF_eNB(eNB,i,harq_pid, 0); // 0 means bw_factor is not considered
1255

1256
        if (ulsch_harq->cqi_crc_status == 1) {
1257
#ifdef DEBUG_PHY_PROC
1258 1259
          //if (((frame%10) == 0) || (frame < 50))
          print_CQI(ulsch_harq->o,ulsch_harq->uci_format,0,fp->N_RB_DL);
1260 1261
#endif

1262 1263 1264
	fill_ulsch_cqi_indication(eNB,frame,subframe,
				  ulsch_harq,
				  ulsch->rnti);
1265
      }
1266
      
1267
      if (ret == (1+MAX_TURBO_ITERATIONS)) {
Cedric Roux's avatar
Cedric Roux committed
1268
        T(T_ENB_PHY_ULSCH_UE_NACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(ulsch->rnti),
1269 1270
          T_INT(harq_pid));

1271 1272
	fill_crc_indication(eNB,i,frame,subframe,1); // indicate NAK to MAC
	fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
1273

1274 1275 1276
	LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d UE %d Error receiving ULSCH, round %d/%d (ACK %d,%d)\n",
	      eNB->Mod_id,harq_pid,
	      frame,subframe, i,
1277
	      ulsch_harq->round,
1278 1279 1280
	      ulsch->Mlimit,
	      ulsch_harq->o_ACK[0],
	      ulsch_harq->o_ACK[1]);
1281

1282 1283 1284 1285 1286
        if (ulsch_harq->round >= 3)  {
           ulsch_harq->status  = SCH_IDLE;
           ulsch_harq->handled = 0;
           ulsch->harq_mask   &= ~(1 << harq_pid);
           ulsch_harq->round   = 0;
1287
	  }
1288
#if defined(MESSAGE_CHART_GENERATOR_PHY)
Cedric Roux's avatar
Cedric Roux committed
1289
        MSC_LOG_RX_DISCARDED_MESSAGE(
1290 1291 1292 1293 1294 1295 1296
            MSC_PHY_ENB,MSC_PHY_UE,
            NULL,0,
            "%05u:%02u ULSCH received rnti %x harq id %u round %d",
            frame,subframe,
            ulsch->rnti,harq_pid,
            ulsch_harq->round-1
            );
1297
#endif
Cedric Roux's avatar
Cedric Roux committed
1298 1299 1300 1301 1302 1303 1304

        /* Mark the HARQ process to release it later if max transmission reached
         * (see below).
         * MAC does not send the max transmission count, we have to deal with it
         * locally in PHY.
         */
        ulsch_harq->handled = 1;
1305 1306
      }  // ulsch in error
      else {
1307 1308
	fill_crc_indication(eNB,i,frame,subframe,0); // indicate ACK to MAC
	fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
Cedric Roux's avatar
Cedric Roux committed
1309 1310 1311

        ulsch_harq->status = SCH_IDLE;
        ulsch->harq_mask   &= ~(1 << harq_pid);
1312

Cedric Roux's avatar
Cedric Roux committed
1313
        T(T_ENB_PHY_ULSCH_UE_ACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(ulsch->rnti),
1314 1315 1316
          T_INT(harq_pid));

#if defined(MESSAGE_CHART_GENERATOR_PHY)
1317 1318 1319 1320 1321 1322 1323
          MSC_LOG_RX_MESSAGE(
              MSC_PHY_ENB,MSC_PHY_UE,
              NULL,0,
              "%05u:%02u ULSCH received rnti %x harq id %u",
              frame,subframe,
              ulsch->rnti,harq_pid
              );
1324 1325 1326 1327
#endif

#ifdef DEBUG_PHY_PROC
#ifdef DEBUG_ULSCH
1328 1329
          LOG_D(PHY,"[eNB] Frame %d, Subframe %d : ULSCH SDU (RX harq_pid %d) %d bytes:",frame,subframe,
              harq_pid,ulsch_harq->TBS>>3);
Cedric Roux's avatar
Cedric Roux committed
1330

1331 1332
          for (j=0; j<ulsch_harq->TBS>>3; j++)
            LOG_T(PHY,"%x.",ulsch->harq_processes[harq_pid]->b[j]);
Cedric Roux's avatar
Cedric Roux committed
1333

1334
          LOG_T(PHY,"\n");
1335 1336
#endif
#endif
1337
        }  // ulsch not in error
1338 1339 1340

      if (ulsch_harq->O_ACK>0) fill_ulsch_harq_indication(eNB,ulsch_harq,ulsch->rnti,frame,subframe,ulsch->bundling);

1341
      LOG_D(PHY,"[eNB %d] Frame %d subframe %d: received ULSCH harq_pid %d for UE %d, ret = %d, CQI CRC Status %d, ACK %d,%d, ulsch_errors %d/%d\n",
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
            eNB->Mod_id,frame,subframe,
            harq_pid,
            i,
            ret,
            ulsch_harq->cqi_crc_status,
            ulsch_harq->o_ACK[0],
            ulsch_harq->o_ACK[1],
            eNB->UE_stats[i].ulsch_errors[harq_pid],
            eNB->UE_stats[i].ulsch_decoding_attempts[harq_pid][0]);
    } //     if ((ulsch) &&
      //         (ulsch->rnti>0) &&
      //         (ulsch_harq->status == ACTIVE))
1354 1355 1356 1357 1358 1359
    else if ((ulsch) &&
             (ulsch->rnti>0) &&
             (ulsch_harq->status == ACTIVE) &&
             (ulsch_harq->frame == frame) &&
             (ulsch_harq->subframe == subframe) &&
             (ulsch_harq->handled == 1)) {
1360 1361
          // this harq process is stale, kill it, this 1024 frames later (10s), consider reducing that
           ulsch_harq->status = SCH_IDLE;
Cedric Roux's avatar
Cedric Roux committed
1362 1363 1364 1365
      ulsch_harq->handled = 0;
      ulsch->harq_mask   &= ~(1 << harq_pid);
      LOG_W(PHY,"Removing stale ULSCH config for UE %x harq_pid %d (harq_mask is now 0x%2.2x)\n",
            ulsch->rnti, harq_pid, ulsch->harq_mask);
1366
    }
1367
  }   //   for (i=0; i<NUMBER_OF_UE_MAX; i++)
1368 1369 1370 1371 1372 1373
}

extern int oai_exit;

extern void *td_thread(void*);

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1374
void init_td_thread(PHY_VARS_eNB *eNB) {
1375 1376 1377 1378 1379

  eNB_proc_t *proc = &eNB->proc;

  proc->tdp.eNB = eNB;
  proc->instance_cnt_td         = -1;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1380 1381
  
  pthread_attr_init( &proc->attr_td);  
1382 1383
  pthread_mutex_init( &proc->mutex_td, NULL);
  pthread_cond_init( &proc->cond_td, NULL);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1384 1385
  
  pthread_create(&proc->pthread_td, &proc->attr_td, td_thread, (void*)&proc->tdp);
1386

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1387 1388
}
void kill_td_thread(PHY_VARS_eNB *eNB) {
1389

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1390 1391 1392 1393 1394 1395 1396
  eNB_proc_t *proc = &eNB->proc;
  proc->instance_cnt_td         = 0;
  pthread_cond_signal(&proc->cond_td);
  
  pthread_join(proc->pthread_td, NULL);
  pthread_mutex_destroy( &proc->mutex_td );
  pthread_cond_destroy( &proc->cond_td );
1397 1398 1399
}

extern void *te_thread(void*);
1400

Eurecom's avatar
Eurecom committed
1401
void init_te_thread(PHY_VARS_eNB *eNB) {
1402

1403
  eNB_proc_t *proc = &eNB->proc;
1404

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1405 1406 1407 1408 1409 1410 1411
  for(int i=0; i<3 ;i++){
    proc->tep[i].eNB = eNB;
    proc->tep[i].instance_cnt_te         = -1;
      
    pthread_mutex_init( &proc->tep[i].mutex_te, NULL);
    pthread_cond_init( &proc->tep[i].cond_te, NULL);
    pthread_attr_init( &proc->tep[i].attr_te);
1412
    
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1413 1414 1415 1416 1417
    printf("Creating te_thread 0\n");
    pthread_create(&proc->tep[i].pthread_te, &proc->tep[i].attr_te, te_thread, (void*)&proc->tep[i]);
  }
}
void kill_te_thread(PHY_VARS_eNB *eNB) {
1418

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1419
  eNB_proc_t *proc = &eNB->proc;
1420

Wang Tsu-Han's avatar
Wang Tsu-Han committed
1421 1422 1423 1424
  for(int i=0; i<3 ;i++){
    proc->tep[i].instance_cnt_te         = 0;
    pthread_cond_signal(&proc->tep[i].cond_te);
    pthread_join(proc->tep[i].pthread_te, NULL);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1425 1426
    pthread_mutex_destroy( &proc->tep[i].mutex_te);
    pthread_cond_destroy( &proc->tep[i].cond_te);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1427
  }
1428 1429
}

1430

Cedric Roux's avatar
Cedric Roux committed
1431 1432
void fill_rx_indication(PHY_VARS_eNB *eNB,int UE_id,int frame,int subframe)
{
1433 1434 1435 1436 1437 1438
  nfapi_rx_indication_pdu_t *pdu;

  int timing_advance_update;
  int sync_pos;

  uint32_t harq_pid = subframe2harq_pid(&eNB->frame_parms,
1439
					frame,subframe);
1440 1441

  pthread_mutex_lock(&eNB->UL_INFO_mutex);
1442 1443 1444 1445 1446

  eNB->UL_INFO.rx_ind.sfn_sf                    = frame<<4| subframe;
  eNB->UL_INFO.rx_ind.rx_indication_body.tl.tag = NFAPI_RX_INDICATION_BODY_TAG;

  pdu                                    = &eNB->UL_INFO.rx_ind.rx_indication_body.rx_pdu_list[eNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus];
1447 1448

  //  pdu->rx_ue_information.handle          = eNB->ulsch[UE_id]->handle;
1449
  pdu->rx_ue_information.tl.tag          = NFAPI_RX_UE_INFORMATION_TAG;
1450
  pdu->rx_ue_information.rnti            = eNB->ulsch[UE_id]->rnti;
1451
  pdu->rx_indication_rel8.tl.tag         = NFAPI_RX_INDICATION_REL8_TAG;
1452
  pdu->rx_indication_rel8.length         = eNB->ulsch[UE_id]->harq_processes[harq_pid]->TBS>>3;
1453 1454
  pdu->rx_indication_rel8.offset         = 1;   // DJP - I dont understand - but broken unless 1 ????  0;  // filled in at the end of the UL_INFO formation
  pdu->data                              = eNB->ulsch[UE_id]->harq_processes[harq_pid]->b;  
1455 1456
  // estimate timing advance for MAC
  sync_pos                               = lte_est_timing_advance_pusch(eNB,UE_id);
Cedric Roux's avatar
Cedric Roux committed
1457
  timing_advance_update                  = sync_pos; // - eNB->frame_parms.nb_prefix_samples/4; //to check
1458 1459 1460

  //  if (timing_advance_update > 10) { dump_ulsch(eNB,frame,subframe,UE_id); exit(-1);}
  //  if (timing_advance_update < -10) { dump_ulsch(eNB,frame,subframe,UE_id); exit(-1);}
1461
  switch (eNB->frame_parms.N_RB_DL) {
Cedric Roux's avatar
Cedric Roux committed
1462 1463 1464 1465 1466 1467 1468
  case 6:   /* nothing to do */          break;
  case 15:  timing_advance_update /= 2;  break;
  case 25:  timing_advance_update /= 4;  break;
  case 50:  timing_advance_update /= 8;  break;
  case 75:  timing_advance_update /= 12; break;
  case 100: timing_advance_update /= 16; break;
  default: abort();
1469 1470
  }
  // put timing advance command in 0..63 range
Cedric Roux's avatar
Cedric Roux committed
1471 1472 1473 1474
  timing_advance_update += 31;
  if (timing_advance_update < 0)  timing_advance_update = 0;
  if (timing_advance_update > 63) timing_advance_update = 63;
  pdu->rx_indication_rel8.timing_advance = timing_advance_update;
1475 1476 1477 1478 1479 1480 1481 1482

  // estimate UL_CQI for MAC (from antenna port 0 only)
  int SNRtimes10 = dB_fixed_times10(eNB->pusch_vars[UE_id]->ulsch_power[0]) - 200;//(10*eNB->measurements.n0_power_dB[0]);

  if      (SNRtimes10 < -640) pdu->rx_indication_rel8.ul_cqi=0;
  else if (SNRtimes10 >  635) pdu->rx_indication_rel8.ul_cqi=255;
  else                        pdu->rx_indication_rel8.ul_cqi=(640+SNRtimes10)/5;

1483 1484 1485
  LOG_D(PHY,"[PUSCH %d] Frame %d Subframe %d Filling RX_indication with SNR %d (%d), timing_advance %d (update %d)\n",
	harq_pid,frame,subframe,SNRtimes10,pdu->rx_indication_rel8.ul_cqi,pdu->rx_indication_rel8.timing_advance,
	timing_advance_update);
1486

1487 1488
  eNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus++;
  eNB->UL_INFO.rx_ind.sfn_sf = frame<<4 | subframe;
1489 1490 1491
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
}

1492 1493 1494
/* release the harq if its round is >= 'after_rounds' */
static void do_release_harq(PHY_VARS_eNB *eNB,int UE_id,int tb,uint16_t frame,uint8_t subframe,uint16_t mask, int after_rounds)
{
1495

1496 1497 1498 1499 1500
  LTE_eNB_DLSCH_t *dlsch0=NULL,*dlsch1=NULL;
  LTE_DL_eNB_HARQ_t *dlsch0_harq=NULL,*dlsch1_harq=NULL;
  int harq_pid;
  int subframe_tx;
  int M,m;
1501

1502 1503 1504 1505
  AssertFatal(UE_id!=-1,"no existing dlsch context\n");
  AssertFatal(UE_id<NUMBER_OF_UE_MAX,"returned UE_id %d >= %d(NUMBER_OF_UE_MAX)\n",UE_id,NUMBER_OF_UE_MAX);
  dlsch0 = eNB->dlsch[UE_id][0];
  dlsch1 = eNB->dlsch[UE_id][1];
1506

1507 1508 1509 1510 1511 1512 1513 1514
  if (eNB->frame_parms.frame_type == FDD) {  
    subframe_tx = (subframe+6)%10;
    harq_pid = dlsch0->harq_ids[subframe_tx];
    AssertFatal((harq_pid>=0) && (harq_pid<10),"harq_pid %d not in 0...9\n",harq_pid);
    dlsch0_harq     = dlsch0->harq_processes[harq_pid];
    dlsch1_harq     = dlsch1->harq_processes[harq_pid];
    AssertFatal(dlsch0_harq!=NULL,"dlsch0_harq is null\n");

1515 1516 1517 1518 1519 1520 1521
    if (dlsch0_harq->round >= after_rounds) {
      dlsch0_harq->status = SCH_IDLE;
      /*if ((dlsch1_harq == NULL)||
	  ((dlsch1_harq!=NULL)&&
	   (dlsch1_harq->status == SCH_IDLE)))*/
      dlsch0->harq_mask   &= ~(1<<harq_pid);
    }
1522 1523
    LOG_D(PHY,"Frame %d, subframe %d: Releasing harq %d for UE %x\n",frame,subframe,harq_pid,dlsch0->rnti);

1524 1525 1526 1527 1528
  }
  else { // release all processes in the bundle that was acked, based on mask
         // This is at most 4 for multiplexing and 9 for bundling/special bundling
    M=ul_ACK_subframe2_M(&eNB->frame_parms,
                         subframe);
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
    for (m=0; m<M; m++) {
      subframe_tx = ul_ACK_subframe2_dl_subframe(&eNB->frame_parms,
						 subframe,
						 m);
      if (((1<<m)&mask) > 0) {
	harq_pid = dlsch0->harq_ids[subframe_tx];
	if ((harq_pid>=0) && (harq_pid<10)) {
	  dlsch0_harq     = dlsch0->harq_processes[harq_pid];
	  dlsch1_harq     = dlsch1->harq_processes[harq_pid];
	  AssertFatal(dlsch0_harq!=NULL,"dlsch0_harq is null\n");
      
1541 1542 1543 1544 1545 1546 1547
          if (dlsch0_harq->round >= after_rounds) {
	    dlsch0_harq->status = SCH_IDLE;
	    if ((dlsch1_harq == NULL)||
	        ((dlsch1_harq!=NULL)&&
	         (dlsch1_harq->status == SCH_IDLE)))
	      dlsch0->harq_mask   &= ~(1<<harq_pid);
          }
1548 1549 1550
	}
      }
    }
1551 1552 1553
  }
}

1554 1555 1556 1557 1558 1559 1560 1561 1562
static void release_harq(PHY_VARS_eNB *eNB,int UE_id,int tb,uint16_t frame,uint8_t subframe,uint16_t mask, int is_ack)
{
  /* Maximum number of DL transmissions = 4.
   * TODO: get the value from configuration.
   * If is_ack is true then we release immediately. The value -1 can be used for that.
   */
  do_release_harq(eNB, UE_id, tb, frame, subframe, mask, is_ack ? -1 : 4);
}

1563
int getM(PHY_VARS_eNB *eNB,int frame,int subframe) {
1564

1565 1566 1567 1568 1569 1570
  int M,Mtx=0;
  LTE_eNB_DLSCH_t *dlsch0=NULL,*dlsch1=NULL;
  LTE_DL_eNB_HARQ_t *dlsch0_harq=NULL,*dlsch1_harq=NULL;
  int harq_pid;
  int subframe_tx;
  int m;
1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
  M=ul_ACK_subframe2_M(&eNB->frame_parms,
		       subframe);
  
  for (m=0; m<M; m++) {
    subframe_tx = ul_ACK_subframe2_dl_subframe(&eNB->frame_parms,
					       subframe,
					       m);
    harq_pid = dlsch0->harq_ids[subframe_tx];
    if (harq_pid>=0 && harq_pid<10) {
      dlsch0_harq     = dlsch0->harq_processes[harq_pid];
      dlsch1_harq     = dlsch1->harq_processes[harq_pid];
      AssertFatal(dlsch0_harq!=NULL,"dlsch0_harq is null\n");
      if (dlsch0_harq->status == ACTIVE||
	  (dlsch1_harq!=NULL && dlsch1_harq->status == ACTIVE)) Mtx ++;
    }
  }
  return(Mtx);
}
1590

1591

1592
void fill_ulsch_cqi_indication(PHY_VARS_eNB *eNB,uint16_t frame,uint8_t subframe,LTE_UL_eNB_HARQ_t *ulsch_harq,uint16_t rnti) {
1593

1594 1595 1596 1597
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
  nfapi_cqi_indication_pdu_t *pdu         = &eNB->UL_INFO.cqi_ind.cqi_pdu_list[eNB->UL_INFO.cqi_ind.number_of_cqis];
  nfapi_cqi_indication_raw_pdu_t *raw_pdu = &eNB->UL_INFO.cqi_ind.cqi_raw_pdu_list[eNB->UL_INFO.cqi_ind.number_of_cqis];

1598
  pdu->rx_ue_information.tl.tag          = NFAPI_RX_UE_INFORMATION_TAG;
1599 1600 1601 1602 1603
  pdu->rx_ue_information.rnti = rnti;
  if (ulsch_harq->cqi_crc_status != 1) pdu->cqi_indication_rel9.data_offset = 0;
  else               pdu->cqi_indication_rel9.data_offset = 1; // fill in after all cqi_indications have been generated when non-zero

  // by default set O to rank 1 value
1604
  pdu->cqi_indication_rel9.tl.tag = NFAPI_CQI_INDICATION_REL9_TAG;
Cedric Roux's avatar
Cedric Roux committed
1605
  pdu->cqi_indication_rel9.length = (ulsch_harq->Or1>>3) + ((ulsch_harq->Or1&7) > 0 ? 1 : 0);
1606 1607 1608 1609 1610
  pdu->cqi_indication_rel9.ri[0]  = 0;

  // if we have RI bits, set them and if rank2 overwrite O
  if (ulsch_harq->O_RI>0) {
    pdu->cqi_indication_rel9.ri[0] = ulsch_harq->o_RI[0];
Cedric Roux's avatar
Cedric Roux committed
1611
    if (ulsch_harq->o_RI[0] == 2)   pdu->cqi_indication_rel9.length = (ulsch_harq->Or2>>3) + ((ulsch_harq->Or2&7) > 0 ? 1 : 0);
1612 1613
    pdu->cqi_indication_rel9.timing_advance = 0;
  }
1614
  
1615 1616 1617 1618
  pdu->cqi_indication_rel9.number_of_cc_reported = 1;
  pdu->ul_cqi_information.channel = 1; // PUSCH
  memcpy((void*)raw_pdu->pdu,ulsch_harq->o,pdu->cqi_indication_rel9.length);
  eNB->UL_INFO.cqi_ind.number_of_cqis++;
1619 1620
  LOG_D(PHY,"eNB->UL_INFO.cqi_ind.number_of_cqis:%d\n", eNB->UL_INFO.cqi_ind.number_of_cqis);

1621
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
1622

1623
}
1624

Cedric Roux's avatar
Cedric Roux committed
1625 1626
void fill_ulsch_harq_indication(PHY_VARS_eNB *eNB,LTE_UL_eNB_HARQ_t *ulsch_harq,uint16_t rnti, int frame,int subframe,int bundling)
{
1627
  int UE_id = find_dlsch(rnti,eNB,SEARCH_EXIST);
1628 1629 1630 1631 1632 1633 1634 1635
  //AssertFatal(UE_id>=0,"UE_id doesn't exist\n");

  if (UE_id < 0)
  {
    LOG_E(PHY,"%s(eNB, ulsch_harq, rnti:%04x, frame:%d, subframe:%d, bundling:%d) harq_pdus:%d - Could not find rnti - abort fill of ulsch harq ind\n", __FUNCTION__, rnti, frame, subframe, bundling,eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs);
    return;
  }

Cedric Roux's avatar
Cedric Roux committed
1636
  LOG_D(PHY,"%s(eNB, ulsch_harq, rnti:%04x, frame:%d, subframe:%d, bundling:%d) harq_pdus:%d O_ACK:%d\n", __FUNCTION__, rnti, frame, subframe, bundling,eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs,ulsch_harq->O_ACK);
1637

1638
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
1639
  nfapi_harq_indication_pdu_t *pdu =   &eNB->UL_INFO.harq_ind.harq_indication_body.harq_pdu_list[eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs];
1640 1641
  int M;
  int i;
1642

1643 1644 1645 1646 1647
  eNB->UL_INFO.harq_ind.header.message_id = NFAPI_HARQ_INDICATION;
  eNB->UL_INFO.harq_ind.sfn_sf = frame<<4|subframe;

  eNB->UL_INFO.harq_ind.harq_indication_body.tl.tag = NFAPI_HARQ_INDICATION_BODY_TAG;

1648 1649
  pdu->instance_length                                = 0; // don't know what to do with this
  //  pdu->rx_ue_information.handle                       = handle;
1650
  pdu->rx_ue_information.tl.tag                       = NFAPI_RX_UE_INFORMATION_TAG;
1651
  pdu->rx_ue_information.rnti                         = rnti;
1652

1653
  if (eNB->frame_parms.frame_type == FDD) {
1654
    pdu->harq_indication_fdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
Cedric Roux's avatar
Cedric Roux committed
1655
    pdu->harq_indication_fdd_rel13.mode = 0;
1656
    pdu->harq_indication_fdd_rel13.number_of_ack_nack = ulsch_harq->O_ACK;
1657

1658 1659
    for (i=0;i<ulsch_harq->O_ACK;i++) {
      AssertFatal(ulsch_harq->o_ACK[i] == 0 || ulsch_harq->o_ACK[i] == 1, "harq_ack[%d] is %d, should be 1,2 or 4\n",i,ulsch_harq->o_ACK[i]);
1660

1661 1662
      pdu->harq_indication_fdd_rel13.harq_tb_n[i] = 2-ulsch_harq->o_ACK[i];
      // release DLSCH if needed
1663
      release_harq(eNB,UE_id,i,frame,subframe,0xffff, ulsch_harq->o_ACK[i] == 1);
Cedric Roux's avatar
Cedric Roux committed
1664

1665 1666 1667
//if (ulsch_harq->o_ACK[i] != 1)
//printf("got NACK %d.%d rnti %d harq %d\n", frame, subframe, rnti, eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]);

Cedric Roux's avatar
Cedric Roux committed
1668 1669 1670 1671 1672 1673 1674 1675 1676
#if T_TRACER
      /* TODO: get correct harq pid */
      if (ulsch_harq->o_ACK[i] != 1)
        T(T_ENB_PHY_DLSCH_UE_NACK, T_INT(0), T_INT(frame), T_INT(subframe),
          T_INT(rnti), T_INT(eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]));
      else
        T(T_ENB_PHY_DLSCH_UE_ACK, T_INT(0), T_INT(frame), T_INT(subframe),
          T_INT(rnti), T_INT(eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]));
#endif
1677 1678 1679 1680 1681 1682
    }
  }
  else { // TDD
    M=ul_ACK_subframe2_M(&eNB->frame_parms,
			 subframe);

1683 1684 1685
    pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
    pdu->harq_indication_tdd_rel13.mode = 1-bundling;
    pdu->harq_indication_tdd_rel13.number_of_ack_nack = ulsch_harq->O_ACK;
1686 1687 1688 1689 1690 1691

    for (i=0;i<ulsch_harq->O_ACK;i++) {
      AssertFatal(ulsch_harq->o_ACK[i] == 0 || ulsch_harq->o_ACK[i] == 1, "harq_ack[%d] is %d, should be 1,2 or 4\n",i,ulsch_harq->o_ACK[i]);

      pdu->harq_indication_tdd_rel13.harq_data[0].multiplex.value_0 = 2-ulsch_harq->o_ACK[i];
      // release DLSCH if needed
1692 1693 1694 1695 1696 1697 1698
      /* TODO: review this code, it's most certainly wrong.
       * We have to release the proper HARQ in case of ACK or NACK if max retransmission reached.
       * Basically, call release_harq with 1 as last argument when ACK and 0 when NACK.
       */
      release_harq(eNB,UE_id,i,frame,subframe,0xffff, ulsch_harq->o_ACK[i] == 1);
      if      (M==1 && ulsch_harq->O_ACK==1 && ulsch_harq->o_ACK[i] == 1) release_harq(eNB,UE_id,0,frame,subframe,0xffff, ulsch_harq->o_ACK[i] == 1);
      else if (M==1 && ulsch_harq->O_ACK==2 && ulsch_harq->o_ACK[i] == 1) release_harq(eNB,UE_id,i,frame,subframe,0xffff, ulsch_harq->o_ACK[i] == 1);
1699 1700
      else if (M>1 && ulsch_harq->o_ACK[i] == 1) {
	// spatial bundling
1701 1702
	release_harq(eNB,UE_id,0,frame,subframe,1<<i, ulsch_harq->o_ACK[i] == 1);
	release_harq(eNB,UE_id,1,frame,subframe,1<<i, ulsch_harq->o_ACK[i] == 1);
1703
      }
1704 1705
    }	
  }
Cedric Roux's avatar
Cedric Roux committed
1706

1707 1708 1709
  //LOG_E(PHY,"eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs:%d\n", eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs);
  eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs++;

1710 1711
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
}
1712

1713 1714 1715 1716 1717 1718 1719
void fill_uci_harq_indication(PHY_VARS_eNB *eNB,
			      LTE_eNB_UCI *uci,
			      int frame,
			      int subframe,
			      uint8_t *harq_ack,
			      uint8_t tdd_mapping_mode,
			      uint16_t tdd_multiplexing_mask) {
1720

Raymond Knopp's avatar
Raymond Knopp committed
1721
  int UE_id=find_dlsch(uci->rnti,eNB,SEARCH_EXIST);
1722 1723 1724 1725 1726
  //AssertFatal(UE_id>=0,"UE_id doesn't exist rnti:%x\n", uci->rnti);
  if (UE_id < 0) {
    LOG_E(PHY,"SFN/SF:%04d%d Unable to find rnti:%x do not send HARQ\n", frame, subframe, uci->rnti);
    return;
  }
1727

1728
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
1729

1730 1731 1732 1733 1734 1735 1736 1737 1738
  nfapi_harq_indication_t *ind       = &eNB->UL_INFO.harq_ind;
  nfapi_harq_indication_body_t *body = &ind->harq_indication_body;
  nfapi_harq_indication_pdu_t *pdu   = &body->harq_pdu_list[eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs];

  ind->sfn_sf = frame<<4|subframe;
  ind->header.message_id = NFAPI_HARQ_INDICATION;

  body->tl.tag = NFAPI_HARQ_INDICATION_BODY_TAG;
 
1739 1740
  pdu->instance_length                                = 0; // don't know what to do with this
  //  pdu->rx_ue_information.handle                       = handle;
1741
  pdu->rx_ue_information.tl.tag                       = NFAPI_RX_UE_INFORMATION_TAG;
1742
  pdu->rx_ue_information.rnti                         = uci->rnti;
1743

1744
  // estimate UL_CQI for MAC (from antenna port 0 only)
1745
  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
1746 1747
  int SNRtimes10 = dB_fixed_times10(uci->stat) - 200;//(10*eNB->measurements.n0_power_dB[0]);

1748
  if (SNRtimes10 < -100) LOG_I(PHY,"uci->stat %d \n",uci->stat);
1749 1750 1751 1752 1753

  if      (SNRtimes10 < -640) pdu->ul_cqi_information.ul_cqi=0;
  else if (SNRtimes10 >  635) pdu->ul_cqi_information.ul_cqi=255;
  else                        pdu->ul_cqi_information.ul_cqi=(640+SNRtimes10)/5;
  pdu->ul_cqi_information.channel = 0;
1754

1755 1756
  if (eNB->frame_parms.frame_type == FDD) {
    if (uci->pucch_fmt == pucch_format1a) {
1757
      pdu->harq_indication_fdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
1758 1759
      pdu->harq_indication_fdd_rel13.mode = 0;  
      pdu->harq_indication_fdd_rel13.number_of_ack_nack = 1;
1760
      
1761 1762 1763
      AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[0] == 4, "harq_ack[0] is %d, should be 1,2 or 4\n",harq_ack[0]);
      pdu->harq_indication_fdd_rel13.harq_tb_n[0] = harq_ack[0];
      // release DLSCH if needed
1764
      release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
Cedric Roux's avatar
Cedric Roux committed
1765

1766 1767 1768
//if (harq_ack[0] != 1)
//printf("got NACK %d.%d rnti %d harq %d\n", frame, subframe, uci->rnti, eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]);

Cedric Roux's avatar
Cedric Roux committed
1769 1770 1771 1772 1773 1774 1775 1776
#if T_TRACER
      if (harq_ack[0] != 1)
        T(T_ENB_PHY_DLSCH_UE_NACK, T_INT(0), T_INT(frame), T_INT(subframe),
          T_INT(uci->rnti), T_INT(eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]));
      else
        T(T_ENB_PHY_DLSCH_UE_ACK, T_INT(0), T_INT(frame), T_INT(subframe),
          T_INT(uci->rnti), T_INT(eNB->dlsch[UE_id][0]->harq_ids[(subframe+6)%10]));
#endif
1777 1778
    }
    else if (uci->pucch_fmt == pucch_format1b) {
1779
      pdu->harq_indication_fdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
1780 1781 1782 1783 1784 1785 1786
      pdu->harq_indication_fdd_rel13.mode = 0;  
      pdu->harq_indication_fdd_rel13.number_of_ack_nack = 2;
      AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[1] == 4, "harq_ack[0] is %d, should be 0,1 or 4\n",harq_ack[0]);
      AssertFatal(harq_ack[1] == 1 || harq_ack[1] == 2 || harq_ack[1] == 4, "harq_ack[1] is %d, should be 0,1 or 4\n",harq_ack[1]);
      pdu->harq_indication_fdd_rel13.harq_tb_n[0] = harq_ack[0];
      pdu->harq_indication_fdd_rel13.harq_tb_n[1] = harq_ack[1]; 
      // release DLSCH if needed
1787 1788
      release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
      release_harq(eNB,UE_id,1,frame,subframe,0xffff, harq_ack[1] == 1);
1789 1790 1791 1792
    }
    else AssertFatal(1==0,"only format 1a/b for now, received %d\n",uci->pucch_fmt); 
  }
  else { // TDD
1793

1794 1795
    AssertFatal(tdd_mapping_mode==0 || tdd_mapping_mode==1 || tdd_mapping_mode==2,
		"Illegal tdd_mapping_mode %d\n",tdd_mapping_mode);
1796

1797
    pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1798
    pdu->harq_indication_tdd_rel13.mode = tdd_mapping_mode;  
1799

1800 1801
    switch (tdd_mapping_mode) {
    case 0: // bundling
1802

1803
      if (uci->pucch_fmt == pucch_format1a) {
1804
        pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1805 1806 1807 1808
	pdu->harq_indication_tdd_rel13.number_of_ack_nack = 1;	
	AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[0] == 4, "harq_ack[0] is %d, should be 1,2 or 4\n",harq_ack[0]);
	pdu->harq_indication_tdd_rel13.harq_data[0].bundling.value_0 = harq_ack[0];
	// release all bundled DLSCH if needed
1809
	release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
1810 1811 1812 1813 1814
      }
      else if (uci->pucch_fmt == pucch_format1b) {
	pdu->harq_indication_tdd_rel13.number_of_ack_nack = 2;
	AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[1] == 4, "harq_ack[0] is %d, should be 0,1 or 4\n",harq_ack[0]);
	AssertFatal(harq_ack[1] == 1 || harq_ack[1] == 2 || harq_ack[1] == 4, "harq_ack[1] is %d, should be 0,1 or 4\n",harq_ack[1]);
1815
        pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1816 1817 1818
	pdu->harq_indication_tdd_rel13.harq_data[0].bundling.value_0 = harq_ack[0];
	pdu->harq_indication_tdd_rel13.harq_data[1].bundling.value_0 = harq_ack[1]; 
	// release all DLSCH if needed
1819 1820
	release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
	release_harq(eNB,UE_id,1,frame,subframe,0xffff, harq_ack[1] == 1);
1821 1822 1823 1824
      }
      break;
    case 1: // multiplexing
      AssertFatal(uci->pucch_fmt == pucch_format1b,"uci->pucch_format %d is not format1b\n",uci->pucch_fmt);
1825
      
1826
      if (uci->num_pucch_resources == 1 && uci->pucch_fmt == pucch_format1a) {
1827
        pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1828 1829 1830 1831
	pdu->harq_indication_tdd_rel13.number_of_ack_nack = 1;	
	AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[0] == 4, "harq_ack[0] is %d, should be 1,2 or 4\n",harq_ack[0]);
	pdu->harq_indication_tdd_rel13.harq_data[0].multiplex.value_0 = harq_ack[0];
	// release all DLSCH if needed
1832
	release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
1833
      }
1834
      else if (uci->num_pucch_resources == 1 && uci->pucch_fmt == pucch_format1b) {
1835
        pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1836 1837 1838 1839 1840 1841
	pdu->harq_indication_tdd_rel13.number_of_ack_nack = 2;
	AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[1] == 4, "harq_ack[0] is %d, should be 0,1 or 4\n",harq_ack[0]);
	AssertFatal(harq_ack[1] == 1 || harq_ack[1] == 2 || harq_ack[1] == 4, "harq_ack[1] is %d, should be 0,1 or 4\n",harq_ack[1]);
	pdu->harq_indication_tdd_rel13.harq_data[0].multiplex.value_0 = harq_ack[0];
	pdu->harq_indication_tdd_rel13.harq_data[1].multiplex.value_0 = harq_ack[1]; 
	// release all DLSCH if needed
1842 1843
	release_harq(eNB,UE_id,0,frame,subframe,0xffff, harq_ack[0] == 1);
	release_harq(eNB,UE_id,1,frame,subframe,0xffff, harq_ack[1] == 1);
1844 1845
      }
      else { // num_pucch_resources (M) > 1
1846
        pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1847 1848 1849 1850 1851 1852 1853
	pdu->harq_indication_tdd_rel13.number_of_ack_nack = uci->num_pucch_resources;

	pdu->harq_indication_tdd_rel13.harq_data[0].multiplex.value_0 = harq_ack[0];
	pdu->harq_indication_tdd_rel13.harq_data[1].multiplex.value_0 = harq_ack[1];
	if (uci->num_pucch_resources == 3) 	pdu->harq_indication_tdd_rel13.harq_data[2].multiplex.value_0 = harq_ack[2];
	if (uci->num_pucch_resources == 4) 	pdu->harq_indication_tdd_rel13.harq_data[3].multiplex.value_0 = harq_ack[3];
	// spatial-bundling in this case so release both HARQ if necessary
1854 1855
	release_harq(eNB,UE_id,0,frame,subframe,tdd_multiplexing_mask, 1 /* force release? previous code was unconditional */);
	release_harq(eNB,UE_id,1,frame,subframe,tdd_multiplexing_mask, 1 /* force release? previous code was unconditional */);
1856 1857 1858
      }
      break;
    case 2: // special bundling (SR collision)
1859
      pdu->harq_indication_tdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
1860 1861 1862 1863 1864 1865
      pdu->harq_indication_tdd_rel13.number_of_ack_nack = 1;
      int tdd_config5_sf2scheds=0;
      if (eNB->frame_parms.tdd_config==5) tdd_config5_sf2scheds = getM(eNB,frame,subframe);
 
      switch (harq_ack[0]) {
      case 0:
1866
        /* TODO: release_harq here? this whole code looks suspicious */
1867 1868 1869 1870
	break;
      case 1: // check if M=1,4,7
	if (uci->num_pucch_resources == 1 || uci->num_pucch_resources == 4 ||
	    tdd_config5_sf2scheds == 1 || tdd_config5_sf2scheds == 4 || tdd_config5_sf2scheds == 7) {
1871 1872
	  release_harq(eNB,UE_id,0,frame,subframe,0xffff, 1);
	  release_harq(eNB,UE_id,1,frame,subframe,0xffff, 1);
1873
	}
1874 1875 1876 1877
	break;
      case 2: // check if M=2,5,8
	if (uci->num_pucch_resources == 2 || tdd_config5_sf2scheds == 2 || 
	    tdd_config5_sf2scheds == 5 || tdd_config5_sf2scheds == 8) {
1878 1879
	  release_harq(eNB,UE_id,0,frame,subframe,0xffff, 1);
	  release_harq(eNB,UE_id,1,frame,subframe,0xffff, 1);
1880 1881 1882 1883 1884
	}
	break;
      case 3: // check if M=3,6,9
	if (uci->num_pucch_resources == 3 || tdd_config5_sf2scheds == 3 || 
	    tdd_config5_sf2scheds == 6 || tdd_config5_sf2scheds == 9) {
1885 1886
	  release_harq(eNB,UE_id,0,frame,subframe,0xffff, 1);
	  release_harq(eNB,UE_id,1,frame,subframe,0xffff, 1);
1887 1888 1889 1890
	}
	break;
      }
      break;
1891

1892 1893
    }
  } //TDD
1894

1895

1896 1897
  eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs++;
  LOG_D(PHY,"Incremented eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs:%d\n", eNB->UL_INFO.harq_ind.harq_indication_body.number_of_harqs);
1898
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);  
1899

1900
}
1901 1902


1903
void fill_crc_indication(PHY_VARS_eNB *eNB,int UE_id,int frame,int subframe,uint8_t crc_flag) {
1904

1905
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
1906 1907 1908 1909 1910
  nfapi_crc_indication_pdu_t *pdu =   &eNB->UL_INFO.crc_ind.crc_indication_body.crc_pdu_list[eNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs];

  eNB->UL_INFO.crc_ind.sfn_sf                         = frame<<4 | subframe;
  eNB->UL_INFO.crc_ind.header.message_id              = NFAPI_CRC_INDICATION;
  eNB->UL_INFO.crc_ind.crc_indication_body.tl.tag     = NFAPI_CRC_INDICATION_BODY_TAG;
1911

1912 1913
  pdu->instance_length                                = 0; // don't know what to do with this
  //  pdu->rx_ue_information.handle                       = handle;
1914
  pdu->rx_ue_information.tl.tag                       = NFAPI_RX_UE_INFORMATION_TAG;
1915
  pdu->rx_ue_information.rnti                         = eNB->ulsch[UE_id]->rnti;
Cedric Roux's avatar
Cedric Roux committed
1916
  pdu->crc_indication_rel8.tl.tag                     = NFAPI_CRC_INDICATION_REL8_TAG;
1917
  pdu->crc_indication_rel8.crc_flag                   = crc_flag;
1918

1919 1920 1921 1922
  eNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs++;

  //LOG_D(PHY, "%s() rnti:%04x crcs:%d crc_flag:%d\n", __FUNCTION__, pdu->rx_ue_information.rnti, eNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs, crc_flag);

1923 1924
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
}
1925

1926 1927 1928 1929 1930 1931
void phy_procedures_eNB_uespec_RX(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,const relaying_type_t r_type)
{
  //RX processing for ue-specific resources (i
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  const int subframe = proc->subframe_rx;
  const int frame    = proc->frame_rx;
1932

1933

1934
  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)!=SF_UL)) return;
1935

1936
  T(T_ENB_PHY_UL_TICK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe));
1937

Cedric Roux's avatar
Cedric Roux committed
1938
  /* TODO: use correct rxdata */
1939
  T(T_ENB_PHY_INPUT_SIGNAL, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(0),
Cedric Roux's avatar
Cedric Roux committed
1940
    T_BUFFER(&eNB->RU_list[0]->common.rxdata[0][subframe*eNB->frame_parms.samples_per_tti],
1941
             eNB->frame_parms.samples_per_tti * 4));
1942

1943
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_UESPEC, 1 );
1944

Raymond Knopp's avatar
Raymond Knopp committed
1945
  LOG_D(PHY,"[eNB %d] Frame %d: Doing phy_procedures_eNB_uespec_RX(%d)\n",eNB->Mod_id,frame, subframe);
1946

1947 1948 1949 1950
  eNB->rb_mask_ul[0]=0;
  eNB->rb_mask_ul[1]=0;
  eNB->rb_mask_ul[2]=0;
  eNB->rb_mask_ul[3]=0;
1951

Raymond Knopp's avatar
Raymond Knopp committed
1952
  // Fix me here, these should be locked
Cedric Roux's avatar
Cedric Roux committed
1953 1954
  eNB->UL_INFO.rx_ind.rx_indication_body.number_of_pdus  = 0;
  eNB->UL_INFO.crc_ind.crc_indication_body.number_of_crcs = 0;
1955

Raymond Knopp's avatar
Raymond Knopp committed
1956
  // Call SRS first since all others depend on presence of SRS or lack thereof
1957 1958
  srs_procedures(eNB,proc);

1959
  eNB->first_run_I0_measurements = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1960 1961 1962

  uci_procedures(eNB,proc);

1963 1964 1965
  if (nfapi_mode == 0 || nfapi_mode == 1) { // If PNF or monolithic
    pusch_procedures(eNB,proc);
  }
Raymond Knopp's avatar
Raymond Knopp committed
1966

1967 1968 1969 1970 1971 1972
  lte_eNB_I0_measurements(eNB,
                          subframe,
                          0,
                          eNB->first_run_I0_measurements);

  int min_I0=1000,max_I0=0;
1973
  if ((frame==0) && (subframe==4)) { 
1974 1975 1976 1977 1978 1979 1980 1981
    for (int i=0;i<eNB->frame_parms.N_RB_UL;i++) {
      if (i==(eNB->frame_parms.N_RB_UL>>1) - 1) i+=2;
 
      if (eNB->measurements.n0_subband_power_tot_dB[i]<min_I0) min_I0 = eNB->measurements.n0_subband_power_tot_dB[i];
      if (eNB->measurements.n0_subband_power_tot_dB[i]>max_I0) max_I0 = eNB->measurements.n0_subband_power_tot_dB[i];
    }
    LOG_I(PHY,"max_I0 %d, min_I0 %d\n",max_I0,min_I0);
  }
Raymond Knopp's avatar
Raymond Knopp committed
1982

1983
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_UESPEC, 0 );
1984

1985
  stop_meas(&eNB->phy_proc_rx);
1986

1987
}