phy_procedures_lte_eNb.c 72.5 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
 * \note
 * \warning
 */

33 34 35 36
#include "PHY/defs_eNB.h"
#include "PHY/phy_extern.h"
#include "SCHED/sched_eNB.h"
#include "SCHED/sched_common_extern.h"
knopp's avatar
knopp committed
37
#include "PHY/LTE_ESTIMATION/lte_estimation.h"
38
#include "nfapi_interface.h"
39
#include "fapi_l1.h"
40 41
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
42

43
#include "assertions.h"
gauthier's avatar
gauthier committed
44
#include "msc.h"
45

46 47
#include <time.h>

48
#if defined(ENABLE_ITTI)
gauthier's avatar
gauthier committed
49
#   include "intertask_interface.h"
50 51
#endif

52
extern uint8_t nfapi_mode;
53 54 55



Wang Tsu-Han's avatar
Wang Tsu-Han committed
56
int16_t get_hundred_times_delta_IF_eNB(PHY_VARS_eNB *eNB,uint16_t UE_id,uint8_t harq_pid, uint8_t bw_factor)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{

  uint32_t Nre,sumKr,MPR_x100,Kr,r;
  uint16_t beta_offset_pusch;

  DevAssert( UE_id < NUMBER_OF_UE_MAX+1 );
  DevAssert( harq_pid < 8 );

  Nre = eNB->ulsch[UE_id]->harq_processes[harq_pid]->Nsymb_initial *
        eNB->ulsch[UE_id]->harq_processes[harq_pid]->nb_rb*12;

  sumKr = 0;

  for (r=0; r<eNB->ulsch[UE_id]->harq_processes[harq_pid]->C; r++) {
    if (r<eNB->ulsch[UE_id]->harq_processes[harq_pid]->Cminus)
      Kr = eNB->ulsch[UE_id]->harq_processes[harq_pid]->Kminus;
    else
      Kr = eNB->ulsch[UE_id]->harq_processes[harq_pid]->Kplus;

    sumKr += Kr;
  }

  if (Nre==0)
    return(0);

  MPR_x100 = 100*sumKr/Nre;
  // Note: MPR=is the effective spectral efficiency of the PUSCH
  // FK 20140908 sumKr is only set after the ulsch_encoding

  beta_offset_pusch = 8;
  //(eNB->ulsch[UE_id]->harq_processes[harq_pid]->control_only == 1) ? eNB->ulsch[UE_id]->beta_offset_cqi_times8:8;

  DevAssert( UE_id < NUMBER_OF_UE_MAX );
//#warning "This condition happens sometimes. Need more investigation" // navid
  //DevAssert( MPR_x100/6 < 100 );

  if (1==1) { //eNB->ul_power_control_dedicated[UE_id].deltaMCS_Enabled == 1) {
    // This is the formula from Section 5.1.1.1 in 36.213 10*log10(deltaIF_PUSCH = (2^(MPR*Ks)-1)*beta_offset_pusch)
    if (bw_factor == 1) {
      uint8_t nb_rb = eNB->ulsch[UE_id]->harq_processes[harq_pid]->nb_rb;
      return(hundred_times_delta_TF[MPR_x100/6]+10*dB_fixed_times10((beta_offset_pusch)>>3)) + hundred_times_log10_NPRB[nb_rb-1];
    } else
      return(hundred_times_delta_TF[MPR_x100/6]+10*dB_fixed_times10((beta_offset_pusch)>>3));
  } else {
    return(0);
  }
}


int16_t get_hundred_times_delta_IF_mac(module_id_t module_idP, uint8_t CC_id, rnti_t rnti, uint8_t harq_pid)
{

  int8_t UE_id;

  if ((RC.eNB == NULL) || (module_idP > RC.nb_inst) || (CC_id > RC.nb_CC[module_idP])) {
    LOG_E(PHY,"get_UE_stats: No eNB found (or not allocated) for Mod_id %d,CC_id %d\n",module_idP,CC_id);
    return -1;
  }

  UE_id = find_ulsch( rnti, RC.eNB[module_idP][CC_id],SEARCH_EXIST);

  if (UE_id == -1) {
    // not found
    return 0;
  }

  return get_hundred_times_delta_IF_eNB( RC.eNB[module_idP][CC_id], UE_id, harq_pid, 0 );
}

126
int oai_nfapi_rach_ind(nfapi_rach_indication_t *rach_ind);
127

knopp's avatar
knopp committed
128

129 130
lte_subframe_t get_subframe_direction(uint8_t Mod_id,uint8_t CC_id,uint8_t subframe)
{
131

132
  return(subframe_select(&RC.eNB[Mod_id][CC_id]->frame_parms,subframe));
133

134
}
135

136
void pmch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
137 138


139
#if (RRC_VERSION >= MAKE_VERSION(10, 0, 0))
knopp's avatar
knopp committed
140
  MCH_PDU *mch_pduP=NULL;
141
  //  uint8_t sync_area=255;
142
#endif
143

144
  int subframe = proc->subframe_tx;
145

knopp's avatar
knopp committed
146 147
  AssertFatal(1==0,"pmch not tested for the moment, exiting\n");

148 149 150 151 152 153
  // This is DL-Cell spec pilots in Control region
  generate_pilots_slot(eNB,
		       eNB->common_vars.txdataF,
		       AMP,
		       subframe<<1,1);

154
  
155
#if (RRC_VERSION >= MAKE_VERSION(10, 0, 0))
156 157
  // 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
158
  /*
159 160
  mch_pduP = mac_xface->get_mch_sdu(eNB->Mod_id,
				    eNB->CC_id,
161
				    proc->frame_tx,
162
				    subframe);
163
  */
164 165 166 167 168 169 170 171 172 173
  if ((mch_pduP->Pdu_size > 0) && (mch_pduP->sync_area == 0)) // TEST: only transmit mcch for sync area 0
    LOG_D(PHY,"[eNB%"PRIu8"] Frame %d subframe %d : Got MCH pdu for MBSFN (MCS %"PRIu8", TBS %d) \n",
	  eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->mcs,
	  eNB->dlsch_MCH->harq_processes[0]->TBS>>3);
  else {
    LOG_D(PHY,"[DeNB %"PRIu8"] Frame %d subframe %d : Do not transmit MCH pdu for MBSFN sync area %"PRIu8" (%s)\n",
	  eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->sync_area,
	  (mch_pduP->Pdu_size == 0)? "Empty MCH PDU":"Let RN transmit for the moment");
    mch_pduP = NULL;
  }
174 175
    
  if (mch_pduP) {
176
    fill_eNB_dlsch_MCH(eNB,mch_pduP->mcs,1,0);
177
    // Generate PMCH
178
    generate_mch(eNB,proc,(uint8_t*)mch_pduP->payload);
179
  } else {
180
    LOG_D(PHY,"[eNB/RN] Frame %d subframe %d: MCH not generated \n",proc->frame_tx,subframe);
knopp's avatar
knopp committed
181
  }
182
  
183
#endif
184
}
185

186
void common_signal_procedures (PHY_VARS_eNB *eNB,int frame, int subframe) {
187

188
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
189
  int **txdataF = eNB->common_vars.txdataF;
190
  uint8_t *pbch_pdu=&eNB->pbch_pdu[0];
191

192
  //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?"); 
193

194
  // generate Cell-Specific Reference Signals for both slots
195
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,1);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
196 197 198 199 200 201 202

   if(subframe_select(fp,subframe) == SF_S)
       generate_pilots_slot(eNB,
                    txdataF,
                    AMP,
                    subframe<<1,1);
   else
203 204 205 206
  generate_pilots_slot(eNB,
		       txdataF,
		       AMP,
		       subframe<<1,0);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
207 208 209

 // check that 2nd slot is for DL

210 211
  // check that 2nd slot is for DL
  if (subframe_select(fp,subframe) == SF_DL)
212 213 214
    generate_pilots_slot(eNB,
			 txdataF,
			 AMP,
215 216 217 218
			 (subframe<<1)+1,0);
  
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,0);
      
219

220
  // First half of PSS/SSS (FDD, slot 0)
knopp's avatar
knopp committed
221
  if (subframe == 0) {
222
    if (fp->frame_type == FDD) {
223 224 225 226 227 228 229 230 231 232 233
      generate_pss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 6 : 5,
		   0);
      generate_sss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 5 : 4,
		   0);
      
234
    }
235
    
236 237


238 239 240
      
    /// First half of SSS (TDD, slot 1)
    
241
    if (fp->frame_type == TDD) {
242 243 244 245 246 247
      generate_sss(txdataF,
		   AMP,
		   fp,
		   (fp->Ncp==NORMAL) ? 6 : 5,
		   1);
    }
248

249 250
    // generate PBCH (Physical Broadcast CHannel) info

251
    /// generate PBCH
252
    if ((frame&3)==0) {
253 254
      //AssertFatal(eNB->pbch_configured==1,"PBCH was not configured by MAC\n");
      if (eNB->pbch_configured!=1) return;
255
      eNB->pbch_configured=0;
256
    }
257 258 259 260 261 262 263
    generate_pbch(&eNB->pbch,
		  txdataF,
		  AMP,
		  fp,
		  pbch_pdu,
		  frame&3);
  
264
  }
265
  else if ((subframe == 1) &&
266
	   (fp->frame_type == TDD)){
267 268 269 270 271
    generate_pss(txdataF,
		 AMP,
		 fp,
		 2,
		 2);
272
  }
273 274 275
  
  // Second half of PSS/SSS (FDD, slot 10)
  else if ((subframe == 5) && 
276
	   (fp->frame_type == FDD)) {
277 278 279 280 281 282 283 284 285 286
    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);
287

288
  }
289

290 291
  //  Second-half of SSS (TDD, slot 11)
  else if ((subframe == 5) &&
292
	   (fp->frame_type == TDD)) {
293 294 295 296 297
    generate_sss(txdataF,
		 AMP,
		 fp,
		 (fp->Ncp==NORMAL) ? 6 : 5,
		 11);
knopp's avatar
knopp committed
298
  }
knopp's avatar
knopp committed
299

300 301
  // Second half of PSS (TDD, slot 12)
  else if ((subframe == 6) &&
302
	   (fp->frame_type == TDD)) { 
303 304 305 306 307 308
    generate_pss(txdataF,
		 AMP,
		 fp,
		 2,
		 12);
  }
knopp's avatar
knopp committed
309

310
}
knopp's avatar
knopp committed
311

312 313


314 315 316 317 318 319
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,
320
		      int ra_flag) {
321

322 323
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
324
  LTE_DL_eNB_HARQ_t *dlsch_harq=dlsch->harq_processes[harq_pid];
325
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
326

327
  if (dlsch->rnti == 0x02) {//frame < 200) {
328

329
    LOG_D(PHY,
330 331
	  "[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,
332
	  frame, subframe, dlsch_harq->TBS/8, dlsch_harq->pdsch_start,
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	  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);
  }    
350

351 352 353
  MSC_LOG_TX_MESSAGE(
		     MSC_PHY_ENB,MSC_PHY_UE,
		     NULL,0,
354
		     "%05u:%02u PDSCH/DLSCH input size = %"PRIu16", G %d, nb_rb %"PRIu16", TBS %"PRIu16", pmi_alloc %"PRIx16", rv %"PRIu8" (round %"PRIu8")",
355
		     frame, subframe,
356
		     dlsch_harq->TBS/8,
357 358 359
		     get_G(fp,
			   dlsch_harq->nb_rb,
			   dlsch_harq->rb_alloc,
360
			   dlsch_harq->Qm,
361
			   dlsch_harq->Nl,
362
			   dlsch_harq->pdsch_start,
363 364 365
			   frame,
			   subframe,
			   dlsch_harq->mimo_mode==TM7?7:0),
366
		     dlsch_harq->nb_rb,
367
		     dlsch_harq->TBS,
368 369 370
		     pmi2hex_2Ar1(dlsch_harq->pmi_alloc),
		     dlsch_harq->rvidx,
		     dlsch_harq->round);
371

372 373
  
  
374
  if (ue_stats) ue_stats->dlsch_sliding_cnt++;
375
  
376 377 378
  if (dlsch_harq->round == 0) {
    if (ue_stats)
      ue_stats->dlsch_trials[harq_pid][0]++;
379 380 381 382 383 384 385 386
  } 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
  }
387 388


389 390
  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);
391
  // 36-212 
392 393 394 395 396 397
  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;
    }
398

399 400 401
    start_meas(&eNB->dlsch_encoding_stats);

    eNB->te(eNB,
402
	  dlsch_harq->pdu,
403
	  dlsch_harq->pdsch_start,
404
	  dlsch,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
405 406
	  frame,
	  subframe,
407 408
	  &eNB->dlsch_rate_matching_stats,
	  &eNB->dlsch_turbo_encoding_stats,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
409
	  &eNB->dlsch_turbo_encoding_waiting_stats,
410
      &eNB->dlsch_turbo_encoding_main_stats,
411 412
      &eNB->dlsch_turbo_encoding_wakeup_stats0,
      &eNB->dlsch_turbo_encoding_wakeup_stats1,
413
	  &eNB->dlsch_interleaving_stats);
414
    stop_meas(&eNB->dlsch_encoding_stats);
Thomas Laurent's avatar
Thomas Laurent committed
415
  if(eNB->dlsch_encoding_stats.p_time>500*3000 && opp_enabled == 1)
416 417 418
  {
    print_meas_now(&eNB->dlsch_encoding_stats,"total coding",stderr);
  }
419
  // 36-211
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    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);

440
  
441
    dlsch_modulation(eNB,
442 443
		   eNB->common_vars.txdataF,
		   AMP,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
444
		   frame,
445
		   subframe,
446
		   dlsch_harq->pdsch_start,
447 448 449
		   dlsch,
		   dlsch1);
  
Wang Tsu-Han's avatar
Wang Tsu-Han committed
450
  stop_meas(&eNB->dlsch_modulation_stats);
451
  }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
452 453 454
#ifdef PHY_TX_THREAD
  dlsch->active[subframe] = 0;
#else
455
  dlsch->active = 0;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
456
#endif
457
  dlsch_harq->round++;
458 459

  LOG_D(PHY,"Generating DLSCH/PDSCH dlsch_harq[round:%d]\n",dlsch_harq->round);
460
}
461 462


463

knopp's avatar
knopp committed
464

465
void phy_procedures_eNB_TX(PHY_VARS_eNB *eNB,
knopp's avatar
knopp committed
466
			   eNB_rxtx_proc_t *proc,
467
			   int do_meas)
468
{
469 470
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
471
  uint32_t i,aa;
472
  uint8_t harq_pid;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
473
  int16_t UE_id=0;
474
  uint8_t num_pdcch_symbols=0;
475
  uint8_t num_dci=0;
476 477
  uint8_t ul_subframe;
  uint32_t ul_frame;
478
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
479
  LTE_UL_eNB_HARQ_t *ulsch_harq;
480 481


knopp's avatar
knopp committed
482

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

485
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+(eNB->CC_id),1);
486
  if (do_meas==1) start_meas(&eNB->phy_proc_tx);
487
  if (do_meas==1) start_meas(&eNB->dlsch_common_and_dci);
488

489
  // clear the transmit data array for the current subframe
490 491 492
  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));
knopp's avatar
knopp committed
493
  }
494
  
495

knopp's avatar
knopp committed
496

497 498
  if (nfapi_mode == 0 || nfapi_mode == 1) {
    if (is_pmch_subframe(frame,subframe,fp)) {
499
      pmch_procedures(eNB,proc);
500 501 502 503 504
    }
    else {
      // this is not a pmch subframe, so generate PSS/SSS/PBCH
      common_signal_procedures(eNB,proc->frame_tx, proc->subframe_tx);
    }
505
  }
506

knopp's avatar
knopp committed
507

knopp's avatar
knopp committed
508
  // clear existing ulsch dci allocations before applying info from MAC  (this is table
509
  ul_subframe = pdcch_alloc2ul_subframe(fp,subframe);
510
  ul_frame = pdcch_alloc2ul_frame(fp,frame,subframe);
511 512 513



knopp's avatar
knopp committed
514
  // clear previous allocation information for all UEs
515
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
516 517
    if (eNB->dlsch[i][0])
      eNB->dlsch[i][0]->subframe_tx[subframe] = 0;
knopp's avatar
knopp committed
518
  }
519

520
  /* save old HARQ information needed for PHICH generation */
521 522 523 524
  /* 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
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    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;
	
551 552 553 554
      }
    }
  }

555

556

557 558
  num_pdcch_symbols = eNB->pdcch_vars[subframe&1].num_pdcch_symbols;
  num_dci           = eNB->pdcch_vars[subframe&1].num_dci;
Cedric Roux's avatar
Cedric Roux committed
559
  LOG_D(PHY,"num_pdcch_symbols %"PRIu8",number dci %"PRIu8"\n",num_pdcch_symbols, num_dci);
560
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,num_pdcch_symbols);
561

562

563 564
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,(frame*10)+subframe);

565
  if (num_dci > 0)
Cedric Roux's avatar
Cedric Roux committed
566 567
    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);

568 569

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,1);
knopp's avatar
knopp committed
570

571 572 573 574 575 576 577 578 579 580
  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);
  }
581

582
  if (do_meas==1) stop_meas(&eNB->dlsch_common_and_dci);
583

gauthier's avatar
gauthier committed
584
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,0);
585

586
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_DLSCH,1);
knopp's avatar
knopp committed
587
  // Now scan UE specific DLSCH
588
  LTE_eNB_DLSCH_t *dlsch0,*dlsch1;
589
  for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++)
590
    {
591 592 593 594 595
      dlsch0 = eNB->dlsch[(uint8_t)UE_id][0]; 
      dlsch1 = eNB->dlsch[(uint8_t)UE_id][1]; 

      if ((dlsch0)&&
	  (dlsch0->rnti>0)&&
Wang Tsu-Han's avatar
Wang Tsu-Han committed
596 597 598
#ifdef PHY_TX_THREAD
	  (dlsch0->active[subframe] == 1)) {
#else
599
	  (dlsch0->active == 1)) {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
600
#endif
601

602
	// get harq_pid
Wang Tsu-Han's avatar
Wang Tsu-Han committed
603
	harq_pid = dlsch0->harq_ids[frame%2][subframe];
604
	AssertFatal(harq_pid>=0,"harq_pid is negative\n");
605 606 607 608 609 610 611 612

        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
knopp's avatar
knopp committed
613

614 615 616 617 618 619 620 621
          pdsch_procedures(eNB,
              proc,
              harq_pid,
              dlsch0,
              dlsch1,
              &eNB->UE_stats[(uint32_t)UE_id],
              0);
        }
622 623


knopp's avatar
knopp committed
624
      }
625

gauthier's avatar
gauthier committed
626

627
      else if ((dlsch0)&&
Wang Tsu-Han's avatar
Wang Tsu-Han committed
628 629 630 631 632 633
	       (dlsch0->rnti>0)&&
#ifdef PHY_TX_THREAD
	       (dlsch0->active[subframe] == 0)) {
#else
           (dlsch0->active == 0)) {
#endif
634

635 636
        // 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;
knopp's avatar
knopp committed
637
      }
knopp's avatar
knopp committed
638
    }
639
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_DLSCH,0);
640

641 642


643
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_PHICH,1);
644 645 646
  generate_phich_top(eNB,
		     proc,
		     AMP);
647
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_GENERATE_PHICH,0);
648

649
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+(eNB->CC_id),0);
650
  if (do_meas==1) stop_meas(&eNB->phy_proc_tx);
651
  
652 653
}

654

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
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;
      }
    }
  }
}

687 688
void fill_sr_indication(PHY_VARS_eNB *eNB,uint16_t rnti,int frame,int subframe,uint32_t stat) {
  
689
  pthread_mutex_lock(&eNB->UL_INFO_mutex);
690 691 692 693 694 695 696 697
  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;
698 699 700

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

Wang Tsu-Han's avatar
Wang Tsu-Han committed
704
  int SNRtimes10 = dB_fixed_times10(stat) - 300;//(10*eNB->measurements.n0_power_dB[0]);
705 706


707 708
  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;

709 710 711 712 713
  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;

714
  sr_ind_body->number_of_srs++;
715 716 717 718
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
}

void uci_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
719
{
720
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
721
  uint8_t SR_payload = 0,pucch_b0b1[4][2]= {{0,0},{0,0},{0,0},{0,0}},harq_ack[4]={0,0,0,0};
722
  int32_t metric[4]={0,0,0,0},metric_SR=0,max_metric=0;
723 724
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;
725 726 727
  int i;
  LTE_eNB_UCI *uci;
  uint16_t tdd_multiplexing_mask=0;
728

729
  for (i=0;i<NUMBER_OF_UE_MAX;i++) {
730

731 732 733 734
    uci = &eNB->uci_vars[i];
    if ((uci->active == 1) &&
	(uci->frame == frame) &&
	(uci->subframe == subframe)) {
735

736
      LOG_D(PHY,"Frame %d, subframe %d: Running uci procedures (type %d) for %d \n",frame,subframe,uci->type,i);
737
      uci->active=0;
738

knopp's avatar
knopp committed
739 740 741
      // Null out PUCCH PRBs for noise measurement
      switch(fp->N_RB_UL) {
      case 6:
742 743
        eNB->rb_mask_ul[0] |= (0x1 | (1<<5)); //position 5
        break;
knopp's avatar
knopp committed
744
      case 15:
745 746
        eNB->rb_mask_ul[0] |= (0x1 | (1<<14)); // position 14
        break;
knopp's avatar
knopp committed
747
      case 25:
748 749
        eNB->rb_mask_ul[0] |= (0x1 | (1<<24)); // position 24
        break;
knopp's avatar
knopp committed
750
      case 50:
751 752 753
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[1] |= (1<<17); // position 49 (49-32)
        break;
knopp's avatar
knopp committed
754
      case 75:
755 756 757
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[2] |= (1<<10); // position 74 (74-64)
        break;
knopp's avatar
knopp committed
758
      case 100:
759 760 761
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[3] |= (1<<3); // position 99 (99-96)
        break;
knopp's avatar
knopp committed
762
      default:
763 764
        LOG_E(PHY,"Unknown number for N_RB_UL %d\n",fp->N_RB_UL);
        break;
knopp's avatar
knopp committed
765
      }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
766
      SR_payload = 0;
767 768 769 770 771
      switch (uci->type) {
      case SR:
      case HARQ_SR:
		
	metric_SR = rx_pucch(eNB,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
772 773 774 775 776 777 778 779 780 781
			     uci->pucch_fmt,
			     i,
			     uci->n_pucch_1_0_sr[0],
			     0, // n2_pucch
			     uci->srs_active, // shortened format
			     &SR_payload,
			     frame,
			     subframe,
			     PUCCH1_THRES);
	LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR is %d (uci.type %d SR n1pucch is %d)\n",
782
	      eNB->Mod_id,
783
	      uci->rnti,
784 785 786
	      frame,
	      subframe,
	      SR_payload,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
787
              uci->type,
788 789 790
	      uci->n_pucch_1_0_sr[0]);
	if (uci->type == SR) {
	  if (SR_payload == 1) {
791
	    fill_sr_indication(eNB,uci->rnti,frame,subframe,metric_SR);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
792
	    break;
793 794
	  }
	  else {
Wang Tsu-Han's avatar
Wang Tsu-Han committed
795
	    break;
796 797 798 799
	  }
	}
      case HARQ:
	if (fp->frame_type == FDD) {
800
	  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",
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		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;
820
	  else if (SR_payload == 1) fill_sr_indication(eNB,uci->rnti,frame,subframe,metric_SR);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
 
	  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);
	  }
	  
838

839
	  LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d pucch1a (FDD) payload %d (metric %d)\n",
840 841 842
		eNB->Mod_id,
		uci->rnti,
		frame,subframe,
843 844
		pucch_b0b1[0][0],metric[0]);

Wang Tsu-Han's avatar
Wang Tsu-Han committed
845
	  uci->stat = metric[0]; 	  
846
	  fill_uci_harq_indication(eNB,uci,frame,subframe,pucch_b0b1[0],0,0xffff);
847

848 849
	}
	else { // frame_type == TDD
Wang Tsu-Han's avatar
Wang Tsu-Han committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
	  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",
		frame,subframe,i,
		uci->pucch_fmt,uci->type,
		uci->frame,uci->subframe,uci->n_pucch_1[0][0],
		SR_payload);
#if 1
	      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);
	      if (uci->type==HARQ_SR && metric[0] > metric_SR) SR_payload = 0;
	      else if (SR_payload == 1) fill_sr_indication(eNB,uci->rnti,frame,subframe,metric_SR);

	      if (uci->type==HARQ_SR && metric[0] <= metric_SR) {
	          SR_payload = 1;
	          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
883 884
	  // if SR was detected, use the n1_pucch from SR
	  if (SR_payload==1) {
885
#ifdef DEBUG_PHY_PROC
886 887 888 889
	    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);
890
#endif
891 892 893 894 895 896 897 898 899 900 901 902
	    
	    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
903
#ifdef DEBUG_PHY_PROC
904 905 906 907 908 909 910 911 912
	    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);
913
#endif
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	    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);
	  	  
	    

	  }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
929 930 931 932 933
#ifdef DEBUG_PHY_PROC
 	      LOG_D(PHY,"RNTI %x type %d SR_payload %d  Frame %d Subframe %d  pucch_b0b1[0][0] %d pucch_b0b1[0][1] %d pucch_b0b1[1][0] %d pucch_b0b1[1][1] %d  \n",
	              uci->rnti,uci->type,SR_payload,frame,subframe,pucch_b0b1[0][0],pucch_b0b1[0][1],pucch_b0b1[1][0],pucch_b0b1[1][1]);
#endif
#endif
934 935 936 937
	  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
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
938
	    else if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] == 1) { // 1/4/7 ACKs
939 940
	      harq_ack[0] = 1;
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
941
	    else if (pucch_b0b1[0][0] == 1 && pucch_b0b1[0][1] != 1) { // 2/5/8 ACKs
942 943
	      harq_ack[0] = 2;
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
944
	    else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] == 1) { // 3/6/9 ACKs
945 946
	      harq_ack[0] = 3;
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
947
	    else if (pucch_b0b1[0][0] != 1 && pucch_b0b1[0][1] != 1) { // 0 ACKs, or at least one DL assignment missed
948 949
	      harq_ack[0] = 0;
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
950
	    uci->stat = metric[0];
951 952
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,2,0xffff); // special_bundling mode
	  } 
knopp's avatar
knopp committed
953
	  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
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 982 983 984 985 986 987 988 989 990 991
	    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
		}
	      }
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
992
	    uci->stat = max(metric[0],metric[1]);
993
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
994
	  } //else if ((uci->tdd_bundling == 0) && (res==2))
knopp's avatar
knopp committed
995
	  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
996 997 998 999 1000 1001 1002
	    
	    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
1003
              max_metric = 0;
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
	    } 
	    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;
		}
	      }
1073
            uci->stat = max_metric;
1074
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
1075
	    }
1076
	  } //else if ((uci->tdd_bundling == 0) && (res==3)) 
knopp's avatar
knopp committed
1077
	  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
1078 1079 1080 1081 1082 1083 1084 1085
	    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
1086
              max_metric = 0;
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 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	    } 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;
		}
	      }
	    }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1211
	    uci->stat = max_metric;
1212
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,1,tdd_multiplexing_mask); // multiplexing mode
1213
	  } // else if ((uci->tdd_bundling == 0) && (res==4))
1214 1215 1216
	  else { // bundling
	    harq_ack[0] = pucch_b0b1[0][0];
	    harq_ack[1] = pucch_b0b1[0][1];
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1217 1218
	    uci->stat = metric[0];
	    LOG_D(PHY,"bundling: (%d,%d), metric %d\n",harq_ack[0],harq_ack[1],uci->stat);
1219 1220
	    fill_uci_harq_indication(eNB,uci,frame,subframe,harq_ack,0,0xffff); // special_bundling mode
	  }
1221
	  
1222
#ifdef DEBUG_PHY_PROC
knopp's avatar
knopp committed
1223
	  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,
1224 1225
		eNB->dlsch[UE_id][0]->rnti,
		frame,subframe,
knopp's avatar
knopp committed
1226
		metric0,metric1,pucch_b0b1[0],pucch_b0b1[1]);
1227
#endif
1228 1229 1230 1231 1232
	}
	break;
      default:
	AssertFatal(1==0,"Unsupported UCI type %d\n",uci->type);
	break;
1233
      }
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    
      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);
	}
1247 1248 1249
      }
    }
  }
1250
}
1251

Cedric Roux's avatar
Cedric Roux committed
1252 1253
void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
{
knopp's avatar
knopp committed
1254 1255
  uint32_t ret=0,i;
  uint32_t harq_pid;
1256 1257 1258 1259
  uint8_t nPRS;
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  LTE_eNB_ULSCH_t *ulsch;
  LTE_UL_eNB_HARQ_t *ulsch_harq;
1260

1261 1262
  const int subframe = proc->subframe_rx;
  const int frame    = proc->frame_rx;
1263
  
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1264
  harq_pid = subframe2harq_pid(&eNB->frame_parms,frame,subframe);
1265

1266 1267 1268
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    ulsch = eNB->ulsch[i];
    ulsch_harq = ulsch->harq_processes[harq_pid];
1269 1270 1271 1272
    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);
    
1273 1274 1275
    if ((ulsch) &&
        (ulsch->rnti>0) &&
        (ulsch_harq->status == ACTIVE) &&
1276 1277
	    (ulsch_harq->frame == frame) &&
	    (ulsch_harq->subframe == subframe) &&
1278
        (ulsch_harq->handled == 0)) {
1279
      
1280
      // UE has ULSCH scheduling
1281 1282
      for (int rb=0;
           rb<=ulsch_harq->nb_rb;
1283 1284 1285
	   rb++) {
	int rb2 = rb+ulsch_harq->first_rb;
	eNB->rb_mask_ul[rb2>>5] |= (1<<(rb2&31));
1286
      }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1287
      
Cedric Roux's avatar
Cedric Roux committed
1288
      LOG_D(PHY,"[eNB %d] frame %d, subframe %d: Scheduling ULSCH Reception for UE %d \n", eNB->Mod_id, frame, subframe, i);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1289
      
1290
      nPRS = fp->pusch_config_common.ul_ReferenceSignalsPUSCH.nPRS[subframe<<1];
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1291
      
Cedric Roux's avatar
Cedric Roux committed
1292
      ulsch->cyclicShift = (ulsch_harq->n_DMRS2 +
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1293 1294 1295
			    fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift +
			    nPRS)%12;
      AssertFatal(ulsch_harq->TBS>0,"illegal TBS %d\n",ulsch_harq->TBS); 
1296
      LOG_D(PHY,
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	    "[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);
      
      rx_ulsch(eNB,proc, i);
      
      stop_meas(&eNB->ulsch_demodulation_stats);
      
      start_meas(&eNB->ulsch_decoding_stats);
      
      ret = ulsch_decoding(eNB,proc,
			   i,
			   0, // control_only_flag
			   ulsch_harq->V_UL_DAI,
			   ulsch_harq->nb_rb>20 ? 1 : 0);
      
      stop_meas(&eNB->ulsch_decoding_stats);
      
      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",
1330 1331 1332 1333 1334
            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]),
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1335 1336
            30,//eNB->measurements.n0_power_dB[0],
            30,//eNB->measurements.n0_power_dB[1],
1337 1338
            ulsch_harq->o_ACK[0],
            ulsch_harq->o_ACK[1],
1339 1340 1341
            ret,
            ulsch_harq->cqi_crc_status,
            ulsch_harq->O_ACK,
Thomas Laurent's avatar
Thomas Laurent committed
1342
            eNB->ulsch_decoding_stats.p_time, eNB->ulsch_decoding_stats.max);
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1343 1344 1345 1346
      
      //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
      
1347 1348
      if (RC.mac != NULL) { /* ulsim dose not use RC.mac context. */
        if (ulsch_harq->cqi_crc_status == 1) {
1349
#ifdef DEBUG_PHY_PROC
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1350 1351
	//if (((frame%10) == 0) || (frame < 50))
	print_CQI(ulsch_harq->o,ulsch_harq->uci_format,0,fp->N_RB_DL);
1352
#endif
1353
          fill_ulsch_cqi_indication(eNB,frame,subframe,ulsch_harq,ulsch->rnti);
1354
          RC.mac[eNB->Mod_id]->UE_list.UE_sched_ctrl[i].cqi_req_flag &= (~(1 << subframe));
1355 1356 1357 1358 1359 1360
        } else {
          if(RC.mac[eNB->Mod_id]->UE_list.UE_sched_ctrl[i].cqi_req_flag & (1 << subframe) ){
            RC.mac[eNB->Mod_id]->UE_list.UE_sched_ctrl[i].cqi_req_flag &= (~(1 << subframe));
            RC.mac[eNB->Mod_id]->UE_list.UE_sched_ctrl[i].cqi_req_timer=30;
            LOG_D(PHY,"Frame %d,Subframe %d, We're supposed to get a cqi here. Set cqi_req_timer to 30.\n",frame,subframe);
          }
1361
        }
1362
      }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
1363
    
1364
      if (ret == (1+MAX_TURBO_ITERATIONS)) {