From f80f1f617d9ad7dcdf8afed622b023499dfece92 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 12 Oct 2017 14:39:55 +0200
Subject: [PATCH] bugfixes

- better ulsch harq management in phy (maybe not over)
- don't do schedule_CSI if UE not in RRC_CONNECTED (not sure if correct)
- don't call find_ulsch in fill_dci0
- some spaces cleaned
- log improved
---
 openair1/PHY/LTE_TRANSPORT/dci_tools.c  |  46 +++----
 openair1/SCHED/phy_procedures_lte_eNb.c | 173 +++++++++++-------------
 openair2/LAYER2/MAC/eNB_scheduler.c     |   3 +
 3 files changed, 100 insertions(+), 122 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/dci_tools.c b/openair1/PHY/LTE_TRANSPORT/dci_tools.c
index 942386c975..af7fe279e4 100644
--- a/openair1/PHY/LTE_TRANSPORT/dci_tools.c
+++ b/openair1/PHY/LTE_TRANSPORT/dci_tools.c
@@ -919,8 +919,8 @@ void fill_dci_and_dlsch(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,DCI_ALLOC_t *dci
   dlsch0->active        = 1;
   if (rel8->rnti_type == 2)
       dlsch0_harq->round    = 0;
-LOG_D(PHY,"NFAPI: harq_pid %d harq_mask %x, round %d ndi (%d,%d) \n",rel8->harq_process,dlsch0->harq_mask,dlsch0_harq->round,
-	dlsch0_harq->ndi,rel8->new_data_indicator_1);
+LOG_D(PHY,"NFAPI: harq_pid %d harq_mask %x, round %d ndi (%d,%d) rnti type %d\n",rel8->harq_process,dlsch0->harq_mask,dlsch0_harq->round,
+	dlsch0_harq->ndi,rel8->new_data_indicator_1, rel8->rnti_type);
 
   switch (rel8->dci_format) {
 
@@ -2265,11 +2265,6 @@ void fill_mdci_and_dlsch(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,mDCI_ALLOC_t *d
 void fill_dci0(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,DCI_ALLOC_t *dci_alloc,
                nfapi_hi_dci0_dci_pdu *pdu)
 {
-  uint8_t UE_id;
-
-  AssertFatal((UE_id=find_ulsch(pdu->dci_pdu_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE))>=0,
-	      "No existing UE ULSCH for rnti %x\n",pdu->dci_pdu_rel8.rnti);
-
   LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
 
   uint32_t cqi_req = pdu->dci_pdu_rel8.cqi_csi_request;
@@ -2429,12 +2424,10 @@ void fill_dci0(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,DCI_ALLOC_t *dci_alloc,
   }
 }
 
-void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame,int subframe) {
-
+void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame,int subframe)
+{
   uint8_t harq_pid;
-
   uint8_t UE_id;
-
   boolean_t new_ulsch = (find_ulsch(ulsch_pdu->ulsch_pdu_rel8.rnti,eNB,SEARCH_EXIST)==-1) ? TRUE : FALSE;
 
   AssertFatal((UE_id=find_ulsch(ulsch_pdu->ulsch_pdu_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE))>=0,
@@ -2447,6 +2440,7 @@ void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame
 
   harq_pid = ulsch_pdu->ulsch_pdu_rel8.harq_process_number;
 
+  ulsch->harq_mask |= 1 << harq_pid;
 
   ulsch->harq_processes[harq_pid]->frame                                 = frame;
   ulsch->harq_processes[harq_pid]->subframe                              = subframe;
@@ -2460,10 +2454,10 @@ void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame
   ulsch->harq_processes[harq_pid]->dci_alloc                             = 1;
   ulsch->harq_processes[harq_pid]->rar_alloc                             = 0;
   ulsch->harq_processes[harq_pid]->n_DMRS                                = ulsch_pdu->ulsch_pdu_rel8.cyclic_shift_2_for_drms;
-  
+
   ulsch->harq_processes[harq_pid]->Nsymb_pusch                           = 12-(frame_parms->Ncp<<1)-(use_srs==0?0:1);
   ulsch->harq_processes[harq_pid]->srs_active                            = use_srs;
-  
+
   //Mapping of cyclic shift field in DCI format0 to n_DMRS2 (3GPP 36.211, Table 5.5.2.1.1-1)
   if(ulsch->harq_processes[harq_pid]->n_DMRS == 0)
     ulsch->harq_processes[harq_pid]->n_DMRS2 = 0;
@@ -2481,25 +2475,23 @@ void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame
     ulsch->harq_processes[harq_pid]->n_DMRS2 = 10;
   else if(ulsch->harq_processes[harq_pid]->n_DMRS == 7)
     ulsch->harq_processes[harq_pid]->n_DMRS2 = 9;
-  
-   
+
   LOG_D(PHY,"[eNB %d][PUSCH %d] Programming PUSCH with n_DMRS2 %d (cshift %d) for Frame %d, Subframe %d\n",
 	eNB->Mod_id,harq_pid,ulsch->harq_processes[harq_pid]->n_DMRS2,ulsch->harq_processes[harq_pid]->n_DMRS,
 	frame,subframe);
-  
-  
+
   ulsch->harq_processes[harq_pid]->rvidx = ulsch_pdu->ulsch_pdu_rel8.redundancy_version;
   ulsch->harq_processes[harq_pid]->Qm    = ulsch_pdu->ulsch_pdu_rel8.modulation_type;
-  // Set O_ACK to 0 by default, will be set of DLSCH is scheduled and needs to be 
+  // Set O_ACK to 0 by default, will be set of DLSCH is scheduled and needs to be
   ulsch->harq_processes[harq_pid]->O_ACK         = 0;
 
   if ((ulsch->harq_processes[harq_pid]->status == SCH_IDLE) ||
       (ulsch->harq_processes[harq_pid]->ndi    != ulsch_pdu->ulsch_pdu_rel8.new_data_indication) ||
 	  (new_ulsch == TRUE)){
     ulsch->harq_processes[harq_pid]->status        = ACTIVE;
-    
+
     ulsch->harq_processes[harq_pid]->TBS           = ulsch_pdu->ulsch_pdu_rel8.size<<3;
-    
+
     ulsch->harq_processes[harq_pid]->Msc_initial   = 12*ulsch_pdu->ulsch_pdu_rel8.number_of_resource_blocks;
     ulsch->harq_processes[harq_pid]->Nsymb_initial = ulsch->harq_processes[harq_pid]->Nsymb_pusch;
     ulsch->harq_processes[harq_pid]->round         = 0;
@@ -2509,12 +2501,14 @@ void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame
     // will be set if MAC has activated ULSCH_CQI_RI_PDU or ULSCH_CQI_HARQ_RI_PDU
     ulsch->harq_processes[harq_pid]->Or1           = 0;
     ulsch->harq_processes[harq_pid]->Or2           = 0;
-  } 
+  }
   else  ulsch->harq_processes[harq_pid]->round++;
 
   ulsch->rnti = ulsch_pdu->ulsch_pdu_rel8.rnti;
-  LOG_D(PHY,"Filling ULSCH %x (new_ulsch %d) for Frame %d, Subframe %d : harq_pid %d, first_rb %d, nb_rb %d, rvidx %d, Qm %d, TBS %d, round %d \n",
-	ulsch->rnti, new_ulsch,
+  LOG_D(PHY,"Filling ULSCH %x (UE_id %d) (new_ulsch %d) for Frame %d, Subframe %d : harq_pid %d, first_rb %d, nb_rb %d, rvidx %d, Qm %d, TBS %d, round %d \n",
+	ulsch->rnti,
+        UE_id,
+        new_ulsch,
 	frame,
 	subframe,
 	harq_pid,
@@ -2523,13 +2517,9 @@ void fill_ulsch(PHY_VARS_eNB *eNB,nfapi_ul_config_ulsch_pdu *ulsch_pdu,int frame
 	ulsch->harq_processes[harq_pid]->rvidx,
 	ulsch->harq_processes[harq_pid]->Qm,
 	ulsch->harq_processes[harq_pid]->TBS,
-	ulsch->harq_processes[harq_pid]->round);  
-  
-  
+	ulsch->harq_processes[harq_pid]->round);
 }
 
-
-
 int dump_dci(LTE_DL_FRAME_PARMS *frame_parms, DCI_ALLOC_t *dci)
 {
   switch (dci->format) {
diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index ece4b6eca5..a701ca8590 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -1267,8 +1267,8 @@ void uci_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
   }
 }
 
-void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
-
+void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc)
+{
   uint32_t ret=0,i;
   uint32_t harq_pid;
   uint8_t nPRS;
@@ -1278,49 +1278,42 @@ void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
 
   const int subframe = proc->subframe_rx;
   const int frame    = proc->frame_rx;
-  
+
   if (fp->frame_type == FDD) harq_pid = ((10*frame) + subframe)&7;
   else                       harq_pid = subframe%10;
 
-
-
   for (i=0; i<NUMBER_OF_UE_MAX; i++) {
-
     ulsch = eNB->ulsch[i];
     ulsch_harq = ulsch->harq_processes[harq_pid];
     if (ulsch->rnti>0) LOG_D(PHY,"Frame %d, subframe %d: PUSCH procedures, harq_pid %d, UE %d/%x\n",
-			     frame,subframe,harq_pid,i,ulsch->rnti);
-    
+                             frame,subframe,harq_pid,i,ulsch->rnti);
+
     if ((ulsch) &&
         (ulsch->rnti>0) &&
         (ulsch_harq->status == ACTIVE) &&
-	    (ulsch_harq->frame == frame) &&
-	    (ulsch_harq->subframe == subframe) &&
+            (ulsch_harq->frame == frame) &&
+            (ulsch_harq->subframe == subframe) &&
         (ulsch_harq->handled == 0)) {
-      
-      
+
       // UE has ULSCH scheduling
- 
       for (int rb=0;
            rb<=ulsch_harq->nb_rb;
-	   rb++) {
-	int rb2 = rb+ulsch_harq->first_rb;
-	eNB->rb_mask_ul[rb2>>5] |= (1<<(rb2&31));
+           rb++) {
+        int rb2 = rb+ulsch_harq->first_rb;
+        eNB->rb_mask_ul[rb2>>5] |= (1<<(rb2&31));
       }
 
-
       LOG_D(PHY,"[eNB %d] frame %d, subframe %d: Scheduling ULSCH Reception for UE %d \n",
-	    eNB->Mod_id,
-	    frame,
-	    subframe,
-	    i);
-
+            eNB->Mod_id,
+            frame,
+            subframe,
+            i);
 
       nPRS = fp->pusch_config_common.ul_ReferenceSignalsPUSCH.nPRS[subframe<<1];
 
-      ulsch->cyclicShift = (ulsch_harq->n_DMRS2 + 
-			    fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift +
-			    nPRS)%12;
+      ulsch->cyclicShift = (ulsch_harq->n_DMRS2 +
+                            fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift +
+                            nPRS)%12;
 
       LOG_D(PHY,
             "[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, nprs %d), O_ACK %d, beta_cqi %d \n",
@@ -1338,26 +1331,21 @@ void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
             fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift,
             nPRS,
             ulsch_harq->O_ACK,
-	    ulsch->beta_offset_cqi_times8);
+            ulsch->beta_offset_cqi_times8);
 
       start_meas(&eNB->ulsch_demodulation_stats);
 
-      rx_ulsch(eNB,proc,
-	       i);
-      
+      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);
-      
-
+                           i,
+                           0, // control_only_flag
+                           ulsch_harq->V_UL_DAI,
+                           ulsch_harq->nb_rb>20 ? 1 : 0);
 
       stop_meas(&eNB->ulsch_decoding_stats);
 
@@ -1373,92 +1361,91 @@ void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
             ulsch_harq->o_ACK[1],
             ret);
 
-
       //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
 
-
       if (ulsch_harq->cqi_crc_status == 1) {
 #ifdef DEBUG_PHY_PROC
         //if (((frame%10) == 0) || (frame < 50))
         print_CQI(ulsch_harq->o,ulsch_harq->uci_format,0,fp->N_RB_DL);
 #endif
 
-
-	fill_ulsch_cqi_indication(eNB,frame,subframe,
-				  ulsch_harq,
-				  ulsch->rnti);
+        fill_ulsch_cqi_indication(eNB,frame,subframe,
+                                  ulsch_harq,
+                                  ulsch->rnti);
       }
-      
+
       if (ret == (1+MAX_TURBO_ITERATIONS)) {
         T(T_ENB_PHY_ULSCH_UE_NACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(ulsch->rnti),
           T_INT(harq_pid));
 
-	fill_crc_indication(eNB,i,frame,subframe,1); // indicate NAK to MAC
-	fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
+        fill_crc_indication(eNB,i,frame,subframe,1); // indicate NAK to MAC
+        fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
 
-	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,
-	      ulsch_harq->round-1,
-	      ulsch->Mlimit,
-	      ulsch_harq->o_ACK[0],
-	      ulsch_harq->o_ACK[1]);
-	
-	/*if (dB_fixed_times10(eNB->pusch_vars[i]->ulsch_power[0]) > 300) {
-	  dump_ulsch(eNB,frame,subframe,i); exit(-1);
-	  }
-     */
+        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,
+              ulsch_harq->round-1,
+              ulsch->Mlimit,
+              ulsch_harq->o_ACK[0],
+              ulsch_harq->o_ACK[1]);
+
+        /*if (dB_fixed_times10(eNB->pusch_vars[i]->ulsch_power[0]) > 300) {
+          dump_ulsch(eNB,frame,subframe,i); exit(-1);
+          } */
 #if defined(MESSAGE_CHART_GENERATOR_PHY)
-	MSC_LOG_RX_DISCARDED_MESSAGE(
-				     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
-				     );
+        MSC_LOG_RX_DISCARDED_MESSAGE(
+                                     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
+                                     );
 #endif
-	
+
+        /* 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;
       }  // ulsch in error
       else {
-	
+        fill_crc_indication(eNB,i,frame,subframe,0); // indicate ACK to MAC
+        fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
+
+        ulsch_harq->status = SCH_IDLE;
+        ulsch->harq_mask   &= ~(1 << harq_pid);
 
-	fill_crc_indication(eNB,i,frame,subframe,0); // indicate ACK to MAC
-	fill_rx_indication(eNB,i,frame,subframe);  // indicate SDU to MAC
         T(T_ENB_PHY_ULSCH_UE_ACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(ulsch->rnti),
           T_INT(harq_pid));
-	ulsch_harq->status = SCH_IDLE;
 
 #if defined(MESSAGE_CHART_GENERATOR_PHY)
         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
-			   );
+                           MSC_PHY_ENB,MSC_PHY_UE,
+                           NULL,0,
+                           "%05u:%02u ULSCH received rnti %x harq id %u",
+                           frame,subframe,
+                           ulsch->rnti,harq_pid
+                           );
 #endif
 
 #ifdef DEBUG_PHY_PROC
 #ifdef DEBUG_ULSCH
-	LOG_D(PHY,"[eNB] Frame %d, Subframe %d : ULSCH SDU (RX harq_pid %d) %d bytes:",frame,subframe,
+        LOG_D(PHY,"[eNB] Frame %d, Subframe %d : ULSCH SDU (RX harq_pid %d) %d bytes:",frame,subframe,
                 harq_pid,ulsch_harq->TBS>>3);
-	
-	for (j=0; j<ulsch_harq->TBS>>3; j++)
-	  LOG_T(PHY,"%x.",ulsch->harq_processes[harq_pid]->b[j]);
-	
-	LOG_T(PHY,"\n");
+
+        for (j=0; j<ulsch_harq->TBS>>3; j++)
+          LOG_T(PHY,"%x.",ulsch->harq_processes[harq_pid]->b[j]);
+
+        LOG_T(PHY,"\n");
 #endif
 #endif
-
-        
-	
       }  // ulsch not in error
 
       if (ulsch_harq->O_ACK>0) fill_ulsch_harq_indication(eNB,ulsch_harq,ulsch->rnti,frame,subframe,ulsch->bundling);
 
-
       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",
             eNB->Mod_id,frame,subframe,
             harq_pid,
@@ -1469,24 +1456,22 @@ void pusch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
             ulsch_harq->o_ACK[1],
             eNB->UE_stats[i].ulsch_errors[harq_pid],
             eNB->UE_stats[i].ulsch_decoding_attempts[harq_pid][0]);
-      
-           ulsch_harq->handled = 1;
     } //     if ((ulsch) &&
       //         (ulsch->rnti>0) &&
       //         (ulsch_harq->status == ACTIVE))
-
     else if ((ulsch) &&
              (ulsch->rnti>0) &&
              (ulsch_harq->status == ACTIVE) &&
              (ulsch_harq->frame == frame) &&
              (ulsch_harq->subframe == subframe) &&
              (ulsch_harq->handled == 1)) {
-          // this harq process is stale, kill it, this 1024 frames later (10s), consider reducing that
-           ulsch_harq->status = SCH_IDLE;
-           ulsch->harq_mask   = 0;
-           LOG_W(PHY,"Removing stale ULSCH config for UE %x\n",ulsch->rnti);
+      // this harq process is stale, kill it, this 1024 frames later (10s), consider reducing that
+      ulsch_harq->status = SCH_IDLE;
+      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);
     }
-
   }   //   for (i=0; i<NUMBER_OF_UE_MAX; i++) {
 }
 
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index 0d7c2fcb39..36e9419898 100644
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -166,6 +166,9 @@ void schedule_CSI(module_id_t module_idP,frame_t frameP,sub_frame_t subframeP)
 
       ul_req        = &RC.mac[module_idP]->UL_req[CC_id].ul_config_request_body;
 
+      // drop the allocation if the UE hasn't send RRCConnectionSetupComplete yet
+      if (mac_eNB_get_rrc_status(module_idP,UE_RNTI(module_idP,UE_id)) < RRC_CONNECTED) continue;
+
       AssertFatal(UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",UE_id);
 
       if (UE_list->UE_template[CC_id][UE_id].physicalConfigDedicated->cqi_ReportConfig) {
-- 
GitLab